1080c646dSJean-Christophe PLAGNIOL-VILLARD /*
292477a63STimur Tabi * Copyright 2006,2009 Freescale Semiconductor, Inc.
3080c646dSJean-Christophe PLAGNIOL-VILLARD *
400f792e0SHeiko Schocher * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de.
500f792e0SHeiko Schocher * Changes for multibus/multiadapter I2C support.
600f792e0SHeiko Schocher *
75b8031ccSTom Rini * SPDX-License-Identifier: GPL-2.0
8080c646dSJean-Christophe PLAGNIOL-VILLARD */
9080c646dSJean-Christophe PLAGNIOL-VILLARD
10080c646dSJean-Christophe PLAGNIOL-VILLARD #include <common.h>
11080c646dSJean-Christophe PLAGNIOL-VILLARD #include <command.h>
12080c646dSJean-Christophe PLAGNIOL-VILLARD #include <i2c.h> /* Functional interface */
13080c646dSJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
14080c646dSJean-Christophe PLAGNIOL-VILLARD #include <asm/fsl_i2c.h> /* HW definitions */
15dbc82ce3Smario.six@gdsys.cc #include <dm.h>
16dbc82ce3Smario.six@gdsys.cc #include <mapmem.h>
17080c646dSJean-Christophe PLAGNIOL-VILLARD
1892477a63STimur Tabi /* The maximum number of microseconds we will wait until another master has
1992477a63STimur Tabi * released the bus. If not defined in the board header file, then use a
2092477a63STimur Tabi * generic value.
2192477a63STimur Tabi */
2292477a63STimur Tabi #ifndef CONFIG_I2C_MBB_TIMEOUT
2392477a63STimur Tabi #define CONFIG_I2C_MBB_TIMEOUT 100000
2492477a63STimur Tabi #endif
2592477a63STimur Tabi
2692477a63STimur Tabi /* The maximum number of microseconds we will wait for a read or write
2792477a63STimur Tabi * operation to complete. If not defined in the board header file, then use a
2892477a63STimur Tabi * generic value.
2992477a63STimur Tabi */
3092477a63STimur Tabi #ifndef CONFIG_I2C_TIMEOUT
316dd38cc3SShaveta Leekha #define CONFIG_I2C_TIMEOUT 100000
3292477a63STimur Tabi #endif
33080c646dSJean-Christophe PLAGNIOL-VILLARD
34080c646dSJean-Christophe PLAGNIOL-VILLARD #define I2C_READ_BIT 1
35080c646dSJean-Christophe PLAGNIOL-VILLARD #define I2C_WRITE_BIT 0
36080c646dSJean-Christophe PLAGNIOL-VILLARD
37d8c82db4STimur Tabi DECLARE_GLOBAL_DATA_PTR;
38d8c82db4STimur Tabi
39dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
40ec2c81c5Smario.six@gdsys.cc static const struct fsl_i2c_base *i2c_base[4] = {
41ec2c81c5Smario.six@gdsys.cc (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
4200f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
43ec2c81c5Smario.six@gdsys.cc (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
44a17fd10fSShengzhou Liu #endif
45a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
46ec2c81c5Smario.six@gdsys.cc (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
47a17fd10fSShengzhou Liu #endif
48a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
49ec2c81c5Smario.six@gdsys.cc (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
50080c646dSJean-Christophe PLAGNIOL-VILLARD #endif
51080c646dSJean-Christophe PLAGNIOL-VILLARD };
52dbc82ce3Smario.six@gdsys.cc #endif
53080c646dSJean-Christophe PLAGNIOL-VILLARD
54d8c82db4STimur Tabi /* I2C speed map for a DFSR value of 1 */
55d8c82db4STimur Tabi
56*645cb46eSTom Rini #ifdef __M68K__
57d8c82db4STimur Tabi /*
58d8c82db4STimur Tabi * Map I2C frequency dividers to FDR and DFSR values
59d8c82db4STimur Tabi *
60d8c82db4STimur Tabi * This structure is used to define the elements of a table that maps I2C
61d8c82db4STimur Tabi * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
62d8c82db4STimur Tabi * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
63d8c82db4STimur Tabi * Sampling Rate (DFSR) registers.
64d8c82db4STimur Tabi *
65d8c82db4STimur Tabi * The actual table should be defined in the board file, and it must be called
66d8c82db4STimur Tabi * fsl_i2c_speed_map[].
67d8c82db4STimur Tabi *
68d8c82db4STimur Tabi * The last entry of the table must have a value of {-1, X}, where X is same
69d8c82db4STimur Tabi * FDR/DFSR values as the second-to-last entry. This guarantees that any
70d8c82db4STimur Tabi * search through the array will always find a match.
71d8c82db4STimur Tabi *
72d8c82db4STimur Tabi * The values of the divider must be in increasing numerical order, i.e.
73d8c82db4STimur Tabi * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
74d8c82db4STimur Tabi *
75d8c82db4STimur Tabi * For this table, the values are based on a value of 1 for the DFSR
76d8c82db4STimur Tabi * register. See the application note AN2919 "Determining the I2C Frequency
77d8c82db4STimur Tabi * Divider Ratio for SCL"
785d9a5efaSTsiChung Liew *
795d9a5efaSTsiChung Liew * ColdFire I2C frequency dividers for FDR values are different from
805d9a5efaSTsiChung Liew * PowerPC. The protocol to use the I2C module is still the same.
815d9a5efaSTsiChung Liew * A different table is defined and are based on MCF5xxx user manual.
825d9a5efaSTsiChung Liew *
83d8c82db4STimur Tabi */
84d8c82db4STimur Tabi static const struct {
85d8c82db4STimur Tabi unsigned short divider;
86d8c82db4STimur Tabi u8 fdr;
87d8c82db4STimur Tabi } fsl_i2c_speed_map[] = {
885d9a5efaSTsiChung Liew {20, 32}, {22, 33}, {24, 34}, {26, 35},
895d9a5efaSTsiChung Liew {28, 0}, {28, 36}, {30, 1}, {32, 37},
905d9a5efaSTsiChung Liew {34, 2}, {36, 38}, {40, 3}, {40, 39},
915d9a5efaSTsiChung Liew {44, 4}, {48, 5}, {48, 40}, {56, 6},
925d9a5efaSTsiChung Liew {56, 41}, {64, 42}, {68, 7}, {72, 43},
935d9a5efaSTsiChung Liew {80, 8}, {80, 44}, {88, 9}, {96, 41},
945d9a5efaSTsiChung Liew {104, 10}, {112, 42}, {128, 11}, {128, 43},
955d9a5efaSTsiChung Liew {144, 12}, {160, 13}, {160, 48}, {192, 14},
965d9a5efaSTsiChung Liew {192, 49}, {224, 50}, {240, 15}, {256, 51},
975d9a5efaSTsiChung Liew {288, 16}, {320, 17}, {320, 52}, {384, 18},
985d9a5efaSTsiChung Liew {384, 53}, {448, 54}, {480, 19}, {512, 55},
995d9a5efaSTsiChung Liew {576, 20}, {640, 21}, {640, 56}, {768, 22},
1005d9a5efaSTsiChung Liew {768, 57}, {960, 23}, {896, 58}, {1024, 59},
1015d9a5efaSTsiChung Liew {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
1025d9a5efaSTsiChung Liew {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
1035d9a5efaSTsiChung Liew {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
1045d9a5efaSTsiChung Liew {-1, 31}
105d8c82db4STimur Tabi };
106*645cb46eSTom Rini #endif
107d8c82db4STimur Tabi
108d8c82db4STimur Tabi /**
109d8c82db4STimur Tabi * Set the I2C bus speed for a given I2C device
110d8c82db4STimur Tabi *
111ec2c81c5Smario.six@gdsys.cc * @param base: the I2C device registers
112d8c82db4STimur Tabi * @i2c_clk: I2C bus clock frequency
113d8c82db4STimur Tabi * @speed: the desired speed of the bus
114d8c82db4STimur Tabi *
115d8c82db4STimur Tabi * The I2C device must be stopped before calling this function.
116d8c82db4STimur Tabi *
117d8c82db4STimur Tabi * The return value is the actual bus speed that is set.
118d8c82db4STimur Tabi */
set_i2c_bus_speed(const struct fsl_i2c_base * base,unsigned int i2c_clk,unsigned int speed)119ec2c81c5Smario.six@gdsys.cc static unsigned int set_i2c_bus_speed(const struct fsl_i2c_base *base,
120d8c82db4STimur Tabi unsigned int i2c_clk, unsigned int speed)
121d8c82db4STimur Tabi {
122b4141195SMasahiro Yamada unsigned short divider = min(i2c_clk / speed, (unsigned int)USHRT_MAX);
123d8c82db4STimur Tabi
124d8c82db4STimur Tabi /*
125d8c82db4STimur Tabi * We want to choose an FDR/DFSR that generates an I2C bus speed that
126d8c82db4STimur Tabi * is equal to or lower than the requested speed. That means that we
127d8c82db4STimur Tabi * want the first divider that is equal to or greater than the
128d8c82db4STimur Tabi * calculated divider.
129d8c82db4STimur Tabi */
1305d9a5efaSTsiChung Liew #ifdef __PPC__
13199404202SJoakim Tjernlund u8 dfsr, fdr = 0x31; /* Default if no FDR found */
13299404202SJoakim Tjernlund /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
13399404202SJoakim Tjernlund unsigned short a, b, ga, gb;
13499404202SJoakim Tjernlund unsigned long c_div, est_div;
13599404202SJoakim Tjernlund
136d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
137d01ee4dbSJoakim Tjernlund dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
138d01ee4dbSJoakim Tjernlund #else
13999404202SJoakim Tjernlund /* Condition 1: dfsr <= 50/T */
14099404202SJoakim Tjernlund dfsr = (5 * (i2c_clk / 1000)) / 100000;
1415d9a5efaSTsiChung Liew #endif
142d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_FDR
143d01ee4dbSJoakim Tjernlund fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
144d01ee4dbSJoakim Tjernlund speed = i2c_clk / divider; /* Fake something */
145d01ee4dbSJoakim Tjernlund #else
14699404202SJoakim Tjernlund debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
14799404202SJoakim Tjernlund if (!dfsr)
14899404202SJoakim Tjernlund dfsr = 1;
14999404202SJoakim Tjernlund
15099404202SJoakim Tjernlund est_div = ~0;
15199404202SJoakim Tjernlund for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
15299404202SJoakim Tjernlund for (gb = 0; gb < 8; gb++) {
15399404202SJoakim Tjernlund b = 16 << gb;
15499404202SJoakim Tjernlund c_div = b * (a + ((3*dfsr)/b)*2);
15599404202SJoakim Tjernlund if ((c_div > divider) && (c_div < est_div)) {
15699404202SJoakim Tjernlund unsigned short bin_gb, bin_ga;
15799404202SJoakim Tjernlund
15899404202SJoakim Tjernlund est_div = c_div;
15999404202SJoakim Tjernlund bin_gb = gb << 2;
16099404202SJoakim Tjernlund bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
16199404202SJoakim Tjernlund fdr = bin_gb | bin_ga;
16299404202SJoakim Tjernlund speed = i2c_clk / est_div;
16399404202SJoakim Tjernlund debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, "
16499404202SJoakim Tjernlund "a:%d, b:%d, speed:%d\n",
16599404202SJoakim Tjernlund fdr, est_div, ga, gb, a, b, speed);
16699404202SJoakim Tjernlund /* Condition 2 not accounted for */
16799404202SJoakim Tjernlund debug("Tr <= %d ns\n",
16899404202SJoakim Tjernlund (b - 3 * dfsr) * 1000000 /
16999404202SJoakim Tjernlund (i2c_clk / 1000));
17099404202SJoakim Tjernlund }
17199404202SJoakim Tjernlund }
17299404202SJoakim Tjernlund if (a == 20)
17399404202SJoakim Tjernlund a += 2;
17499404202SJoakim Tjernlund if (a == 24)
17599404202SJoakim Tjernlund a += 4;
17699404202SJoakim Tjernlund }
17799404202SJoakim Tjernlund debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr);
17899404202SJoakim Tjernlund debug("FDR:0x%.2x, speed:%d\n", fdr, speed);
17999404202SJoakim Tjernlund #endif
180ec2c81c5Smario.six@gdsys.cc writeb(dfsr, &base->dfsrr); /* set default filter */
181ec2c81c5Smario.six@gdsys.cc writeb(fdr, &base->fdr); /* set bus speed */
18299404202SJoakim Tjernlund #else
18399404202SJoakim Tjernlund unsigned int i;
18499404202SJoakim Tjernlund
18599404202SJoakim Tjernlund for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
18699404202SJoakim Tjernlund if (fsl_i2c_speed_map[i].divider >= divider) {
18799404202SJoakim Tjernlund u8 fdr;
18899404202SJoakim Tjernlund
189d01ee4dbSJoakim Tjernlund fdr = fsl_i2c_speed_map[i].fdr;
190d01ee4dbSJoakim Tjernlund speed = i2c_clk / fsl_i2c_speed_map[i].divider;
191ec2c81c5Smario.six@gdsys.cc writeb(fdr, &base->fdr); /* set bus speed */
192d01ee4dbSJoakim Tjernlund
1933e3f766aSKumar Gala break;
1943e3f766aSKumar Gala }
19599404202SJoakim Tjernlund #endif
196d8c82db4STimur Tabi return speed;
197d8c82db4STimur Tabi }
198d8c82db4STimur Tabi
199dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
get_i2c_clock(int bus)20062f730ffSKim Phillips static unsigned int get_i2c_clock(int bus)
201c9a8b25eSJerry Huang {
202c9a8b25eSJerry Huang if (bus)
203609e6ec3SSimon Glass return gd->arch.i2c2_clk; /* I2C2 clock */
204c9a8b25eSJerry Huang else
205609e6ec3SSimon Glass return gd->arch.i2c1_clk; /* I2C1 clock */
206c9a8b25eSJerry Huang }
207dbc82ce3Smario.six@gdsys.cc #endif
208c9a8b25eSJerry Huang
fsl_i2c_fixup(const struct fsl_i2c_base * base)209ec2c81c5Smario.six@gdsys.cc static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
210b8ce3343SChunhe Lan {
211b8ce3343SChunhe Lan const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
212b8ce3343SChunhe Lan unsigned long long timeval = 0;
213b8ce3343SChunhe Lan int ret = -1;
2149c3f77ebSChunhe Lan unsigned int flags = 0;
2159c3f77ebSChunhe Lan
2169c3f77ebSChunhe Lan #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
2179c3f77ebSChunhe Lan unsigned int svr = get_svr();
2189c3f77ebSChunhe Lan if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
2199c3f77ebSChunhe Lan (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
2209c3f77ebSChunhe Lan flags = I2C_CR_BIT6;
2219c3f77ebSChunhe Lan #endif
222b8ce3343SChunhe Lan
223ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
224b8ce3343SChunhe Lan
225b8ce3343SChunhe Lan timeval = get_ticks();
226ec2c81c5Smario.six@gdsys.cc while (!(readb(&base->sr) & I2C_SR_MBB)) {
227b8ce3343SChunhe Lan if ((get_ticks() - timeval) > timeout)
228b8ce3343SChunhe Lan goto err;
229b8ce3343SChunhe Lan }
230b8ce3343SChunhe Lan
231ec2c81c5Smario.six@gdsys.cc if (readb(&base->sr) & I2C_SR_MAL) {
232b8ce3343SChunhe Lan /* SDA is stuck low */
233ec2c81c5Smario.six@gdsys.cc writeb(0, &base->cr);
234b8ce3343SChunhe Lan udelay(100);
235ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MSTA | flags, &base->cr);
236ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
237b8ce3343SChunhe Lan }
238b8ce3343SChunhe Lan
239ec2c81c5Smario.six@gdsys.cc readb(&base->dr);
240b8ce3343SChunhe Lan
241b8ce3343SChunhe Lan timeval = get_ticks();
242ec2c81c5Smario.six@gdsys.cc while (!(readb(&base->sr) & I2C_SR_MIF)) {
243b8ce3343SChunhe Lan if ((get_ticks() - timeval) > timeout)
244b8ce3343SChunhe Lan goto err;
245b8ce3343SChunhe Lan }
246b8ce3343SChunhe Lan ret = 0;
247b8ce3343SChunhe Lan
248b8ce3343SChunhe Lan err:
249ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN | flags, &base->cr);
250ec2c81c5Smario.six@gdsys.cc writeb(0, &base->sr);
251b8ce3343SChunhe Lan udelay(100);
252b8ce3343SChunhe Lan
253b8ce3343SChunhe Lan return ret;
254b8ce3343SChunhe Lan }
255b8ce3343SChunhe Lan
__i2c_init(const struct fsl_i2c_base * base,int speed,int slaveadd,int i2c_clk,int busnum)256ecf591e3Smario.six@gdsys.cc static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
257ecf591e3Smario.six@gdsys.cc slaveadd, int i2c_clk, int busnum)
258080c646dSJean-Christophe PLAGNIOL-VILLARD {
259b8ce3343SChunhe Lan const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
260b8ce3343SChunhe Lan unsigned long long timeval;
261080c646dSJean-Christophe PLAGNIOL-VILLARD
26239df00d9SHeiko Schocher #ifdef CONFIG_SYS_I2C_INIT_BOARD
26326a33504SRichard Retanubun /* Call board specific i2c bus reset routine before accessing the
26426a33504SRichard Retanubun * environment, which might be in a chip on that bus. For details
26526a33504SRichard Retanubun * about this problem see doc/I2C_Edge_Conditions.
26626a33504SRichard Retanubun */
26739df00d9SHeiko Schocher i2c_init_board();
26839df00d9SHeiko Schocher #endif
269ec2c81c5Smario.six@gdsys.cc writeb(0, &base->cr); /* stop I2C controller */
270080c646dSJean-Christophe PLAGNIOL-VILLARD udelay(5); /* let it shutdown in peace */
271ecf591e3Smario.six@gdsys.cc set_i2c_bus_speed(base, i2c_clk, speed);
272ec2c81c5Smario.six@gdsys.cc writeb(slaveadd << 1, &base->adr);/* write slave address */
273ec2c81c5Smario.six@gdsys.cc writeb(0x0, &base->sr); /* clear status register */
274ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */
27526a33504SRichard Retanubun
276b8ce3343SChunhe Lan timeval = get_ticks();
277ec2c81c5Smario.six@gdsys.cc while (readb(&base->sr) & I2C_SR_MBB) {
278b8ce3343SChunhe Lan if ((get_ticks() - timeval) < timeout)
279b8ce3343SChunhe Lan continue;
280b8ce3343SChunhe Lan
281ec2c81c5Smario.six@gdsys.cc if (fsl_i2c_fixup(base))
282b8ce3343SChunhe Lan debug("i2c_init: BUS#%d failed to init\n",
283ecf591e3Smario.six@gdsys.cc busnum);
284b8ce3343SChunhe Lan
285b8ce3343SChunhe Lan break;
286b8ce3343SChunhe Lan }
287080c646dSJean-Christophe PLAGNIOL-VILLARD }
288080c646dSJean-Christophe PLAGNIOL-VILLARD
28921f4cbb7SJoakim Tjernlund static int
i2c_wait4bus(const struct fsl_i2c_base * base)290ecf591e3Smario.six@gdsys.cc i2c_wait4bus(const struct fsl_i2c_base *base)
291080c646dSJean-Christophe PLAGNIOL-VILLARD {
292f2302d44SStefan Roese unsigned long long timeval = get_ticks();
29392477a63STimur Tabi const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
294080c646dSJean-Christophe PLAGNIOL-VILLARD
295ec2c81c5Smario.six@gdsys.cc while (readb(&base->sr) & I2C_SR_MBB) {
29692477a63STimur Tabi if ((get_ticks() - timeval) > timeout)
297080c646dSJean-Christophe PLAGNIOL-VILLARD return -1;
298080c646dSJean-Christophe PLAGNIOL-VILLARD }
299080c646dSJean-Christophe PLAGNIOL-VILLARD
300080c646dSJean-Christophe PLAGNIOL-VILLARD return 0;
301080c646dSJean-Christophe PLAGNIOL-VILLARD }
302080c646dSJean-Christophe PLAGNIOL-VILLARD
303ecf591e3Smario.six@gdsys.cc static inline int
i2c_wait(const struct fsl_i2c_base * base,int write)304ecf591e3Smario.six@gdsys.cc i2c_wait(const struct fsl_i2c_base *base, int write)
305080c646dSJean-Christophe PLAGNIOL-VILLARD {
306080c646dSJean-Christophe PLAGNIOL-VILLARD u32 csr;
307f2302d44SStefan Roese unsigned long long timeval = get_ticks();
30892477a63STimur Tabi const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
309080c646dSJean-Christophe PLAGNIOL-VILLARD
310080c646dSJean-Christophe PLAGNIOL-VILLARD do {
311ec2c81c5Smario.six@gdsys.cc csr = readb(&base->sr);
312080c646dSJean-Christophe PLAGNIOL-VILLARD if (!(csr & I2C_SR_MIF))
313080c646dSJean-Christophe PLAGNIOL-VILLARD continue;
31421f4cbb7SJoakim Tjernlund /* Read again to allow register to stabilise */
315ec2c81c5Smario.six@gdsys.cc csr = readb(&base->sr);
316080c646dSJean-Christophe PLAGNIOL-VILLARD
317ec2c81c5Smario.six@gdsys.cc writeb(0x0, &base->sr);
318080c646dSJean-Christophe PLAGNIOL-VILLARD
319080c646dSJean-Christophe PLAGNIOL-VILLARD if (csr & I2C_SR_MAL) {
320080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: MAL\n");
321080c646dSJean-Christophe PLAGNIOL-VILLARD return -1;
322080c646dSJean-Christophe PLAGNIOL-VILLARD }
323080c646dSJean-Christophe PLAGNIOL-VILLARD
324080c646dSJean-Christophe PLAGNIOL-VILLARD if (!(csr & I2C_SR_MCF)) {
325080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: unfinished\n");
326080c646dSJean-Christophe PLAGNIOL-VILLARD return -1;
327080c646dSJean-Christophe PLAGNIOL-VILLARD }
328080c646dSJean-Christophe PLAGNIOL-VILLARD
329080c646dSJean-Christophe PLAGNIOL-VILLARD if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
330080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: No RXACK\n");
331080c646dSJean-Christophe PLAGNIOL-VILLARD return -1;
332080c646dSJean-Christophe PLAGNIOL-VILLARD }
333080c646dSJean-Christophe PLAGNIOL-VILLARD
334080c646dSJean-Christophe PLAGNIOL-VILLARD return 0;
33592477a63STimur Tabi } while ((get_ticks() - timeval) < timeout);
336080c646dSJean-Christophe PLAGNIOL-VILLARD
337080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: timed out\n");
338080c646dSJean-Christophe PLAGNIOL-VILLARD return -1;
339080c646dSJean-Christophe PLAGNIOL-VILLARD }
340080c646dSJean-Christophe PLAGNIOL-VILLARD
341ecf591e3Smario.six@gdsys.cc static inline int
i2c_write_addr(const struct fsl_i2c_base * base,u8 dev,u8 dir,int rsta)342ecf591e3Smario.six@gdsys.cc i2c_write_addr(const struct fsl_i2c_base *base, u8 dev, u8 dir, int rsta)
343080c646dSJean-Christophe PLAGNIOL-VILLARD {
344080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
345080c646dSJean-Christophe PLAGNIOL-VILLARD | (rsta ? I2C_CR_RSTA : 0),
346ec2c81c5Smario.six@gdsys.cc &base->cr);
347080c646dSJean-Christophe PLAGNIOL-VILLARD
348ec2c81c5Smario.six@gdsys.cc writeb((dev << 1) | dir, &base->dr);
349080c646dSJean-Christophe PLAGNIOL-VILLARD
350ecf591e3Smario.six@gdsys.cc if (i2c_wait(base, I2C_WRITE_BIT) < 0)
351080c646dSJean-Christophe PLAGNIOL-VILLARD return 0;
352080c646dSJean-Christophe PLAGNIOL-VILLARD
353080c646dSJean-Christophe PLAGNIOL-VILLARD return 1;
354080c646dSJean-Christophe PLAGNIOL-VILLARD }
355080c646dSJean-Christophe PLAGNIOL-VILLARD
356ecf591e3Smario.six@gdsys.cc static inline int
__i2c_write_data(const struct fsl_i2c_base * base,u8 * data,int length)357ecf591e3Smario.six@gdsys.cc __i2c_write_data(const struct fsl_i2c_base *base, u8 *data, int length)
358080c646dSJean-Christophe PLAGNIOL-VILLARD {
359080c646dSJean-Christophe PLAGNIOL-VILLARD int i;
360080c646dSJean-Christophe PLAGNIOL-VILLARD
361080c646dSJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < length; i++) {
362ec2c81c5Smario.six@gdsys.cc writeb(data[i], &base->dr);
363080c646dSJean-Christophe PLAGNIOL-VILLARD
364ecf591e3Smario.six@gdsys.cc if (i2c_wait(base, I2C_WRITE_BIT) < 0)
365080c646dSJean-Christophe PLAGNIOL-VILLARD break;
366080c646dSJean-Christophe PLAGNIOL-VILLARD }
367080c646dSJean-Christophe PLAGNIOL-VILLARD
368080c646dSJean-Christophe PLAGNIOL-VILLARD return i;
369080c646dSJean-Christophe PLAGNIOL-VILLARD }
370080c646dSJean-Christophe PLAGNIOL-VILLARD
371ecf591e3Smario.six@gdsys.cc static inline int
__i2c_read_data(const struct fsl_i2c_base * base,u8 * data,int length)372ecf591e3Smario.six@gdsys.cc __i2c_read_data(const struct fsl_i2c_base *base, u8 *data, int length)
373080c646dSJean-Christophe PLAGNIOL-VILLARD {
374080c646dSJean-Christophe PLAGNIOL-VILLARD int i;
375080c646dSJean-Christophe PLAGNIOL-VILLARD
376080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
377ec2c81c5Smario.six@gdsys.cc &base->cr);
378080c646dSJean-Christophe PLAGNIOL-VILLARD
379080c646dSJean-Christophe PLAGNIOL-VILLARD /* dummy read */
380ec2c81c5Smario.six@gdsys.cc readb(&base->dr);
381080c646dSJean-Christophe PLAGNIOL-VILLARD
382080c646dSJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < length; i++) {
383ecf591e3Smario.six@gdsys.cc if (i2c_wait(base, I2C_READ_BIT) < 0)
384080c646dSJean-Christophe PLAGNIOL-VILLARD break;
385080c646dSJean-Christophe PLAGNIOL-VILLARD
386080c646dSJean-Christophe PLAGNIOL-VILLARD /* Generate ack on last next to last byte */
387080c646dSJean-Christophe PLAGNIOL-VILLARD if (i == length - 2)
388080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
389ec2c81c5Smario.six@gdsys.cc &base->cr);
390080c646dSJean-Christophe PLAGNIOL-VILLARD
391d1c9e5b3SJoakim Tjernlund /* Do not generate stop on last byte */
392080c646dSJean-Christophe PLAGNIOL-VILLARD if (i == length - 1)
393d1c9e5b3SJoakim Tjernlund writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
394ec2c81c5Smario.six@gdsys.cc &base->cr);
395080c646dSJean-Christophe PLAGNIOL-VILLARD
396ec2c81c5Smario.six@gdsys.cc data[i] = readb(&base->dr);
397080c646dSJean-Christophe PLAGNIOL-VILLARD }
398080c646dSJean-Christophe PLAGNIOL-VILLARD
399080c646dSJean-Christophe PLAGNIOL-VILLARD return i;
400080c646dSJean-Christophe PLAGNIOL-VILLARD }
401080c646dSJean-Christophe PLAGNIOL-VILLARD
40200f792e0SHeiko Schocher static int
__i2c_read(const struct fsl_i2c_base * base,u8 chip_addr,u8 * offset,int olen,u8 * data,int dlen)403ecf591e3Smario.six@gdsys.cc __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen,
4042b21e960Smario.six@gdsys.cc u8 *data, int dlen)
405080c646dSJean-Christophe PLAGNIOL-VILLARD {
4062b21e960Smario.six@gdsys.cc int ret = -1; /* signal error */
407080c646dSJean-Christophe PLAGNIOL-VILLARD
408ecf591e3Smario.six@gdsys.cc if (i2c_wait4bus(base) < 0)
409b778c1b5SReinhard Pfau return -1;
410b778c1b5SReinhard Pfau
411386b2769Smario.six@gdsys.cc /* Some drivers use offset lengths in excess of 4 bytes. These drivers
412386b2769Smario.six@gdsys.cc * adhere to the following convention:
413386b2769Smario.six@gdsys.cc * - the offset length is passed as negative (that is, the absolute
414386b2769Smario.six@gdsys.cc * value of olen is the actual offset length)
415386b2769Smario.six@gdsys.cc * - the offset itself is passed in data, which is overwritten by the
416386b2769Smario.six@gdsys.cc * subsequent read operation
417a405764cSShaveta Leekha */
4182b21e960Smario.six@gdsys.cc if (olen < 0) {
419ecf591e3Smario.six@gdsys.cc if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
420ecf591e3Smario.six@gdsys.cc ret = __i2c_write_data(base, data, -olen);
421a405764cSShaveta Leekha
42203a112aaSmario.six@gdsys.cc if (ret != -olen)
423a405764cSShaveta Leekha return -1;
424a405764cSShaveta Leekha
425ecf591e3Smario.six@gdsys.cc if (dlen && i2c_write_addr(base, chip_addr,
4262b21e960Smario.six@gdsys.cc I2C_READ_BIT, 1) != 0)
427ecf591e3Smario.six@gdsys.cc ret = __i2c_read_data(base, data, dlen);
428a405764cSShaveta Leekha } else {
4292b21e960Smario.six@gdsys.cc if ((!dlen || olen > 0) &&
430ecf591e3Smario.six@gdsys.cc i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
431ecf591e3Smario.six@gdsys.cc __i2c_write_data(base, offset, olen) == olen)
4322b21e960Smario.six@gdsys.cc ret = 0; /* No error so far */
433080c646dSJean-Christophe PLAGNIOL-VILLARD
434ecf591e3Smario.six@gdsys.cc if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
4352b21e960Smario.six@gdsys.cc olen ? 1 : 0) != 0)
436ecf591e3Smario.six@gdsys.cc ret = __i2c_read_data(base, data, dlen);
437a405764cSShaveta Leekha }
438080c646dSJean-Christophe PLAGNIOL-VILLARD
439ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN, &base->cr);
440080c646dSJean-Christophe PLAGNIOL-VILLARD
441ecf591e3Smario.six@gdsys.cc if (i2c_wait4bus(base)) /* Wait until STOP */
442d1c9e5b3SJoakim Tjernlund debug("i2c_read: wait4bus timed out\n");
443d1c9e5b3SJoakim Tjernlund
4442b21e960Smario.six@gdsys.cc if (ret == dlen)
445080c646dSJean-Christophe PLAGNIOL-VILLARD return 0;
446080c646dSJean-Christophe PLAGNIOL-VILLARD
447080c646dSJean-Christophe PLAGNIOL-VILLARD return -1;
448080c646dSJean-Christophe PLAGNIOL-VILLARD }
449080c646dSJean-Christophe PLAGNIOL-VILLARD
45000f792e0SHeiko Schocher static int
__i2c_write(const struct fsl_i2c_base * base,u8 chip_addr,u8 * offset,int olen,u8 * data,int dlen)451ecf591e3Smario.six@gdsys.cc __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen,
4522b21e960Smario.six@gdsys.cc u8 *data, int dlen)
453080c646dSJean-Christophe PLAGNIOL-VILLARD {
4542b21e960Smario.six@gdsys.cc int ret = -1; /* signal error */
455080c646dSJean-Christophe PLAGNIOL-VILLARD
456ecf591e3Smario.six@gdsys.cc if (i2c_wait4bus(base) < 0)
457b8ce3343SChunhe Lan return -1;
458b8ce3343SChunhe Lan
459ecf591e3Smario.six@gdsys.cc if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
460ecf591e3Smario.six@gdsys.cc __i2c_write_data(base, offset, olen) == olen) {
461ecf591e3Smario.six@gdsys.cc ret = __i2c_write_data(base, data, dlen);
462080c646dSJean-Christophe PLAGNIOL-VILLARD }
463080c646dSJean-Christophe PLAGNIOL-VILLARD
464ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN, &base->cr);
465ecf591e3Smario.six@gdsys.cc if (i2c_wait4bus(base)) /* Wait until STOP */
46621f4cbb7SJoakim Tjernlund debug("i2c_write: wait4bus timed out\n");
467080c646dSJean-Christophe PLAGNIOL-VILLARD
4682b21e960Smario.six@gdsys.cc if (ret == dlen)
469080c646dSJean-Christophe PLAGNIOL-VILLARD return 0;
470080c646dSJean-Christophe PLAGNIOL-VILLARD
471080c646dSJean-Christophe PLAGNIOL-VILLARD return -1;
472080c646dSJean-Christophe PLAGNIOL-VILLARD }
473080c646dSJean-Christophe PLAGNIOL-VILLARD
47400f792e0SHeiko Schocher static int
__i2c_probe_chip(const struct fsl_i2c_base * base,uchar chip)475ecf591e3Smario.six@gdsys.cc __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
476080c646dSJean-Christophe PLAGNIOL-VILLARD {
477080c646dSJean-Christophe PLAGNIOL-VILLARD /* For unknow reason the controller will ACK when
478080c646dSJean-Christophe PLAGNIOL-VILLARD * probing for a slave with the same address, so skip
479080c646dSJean-Christophe PLAGNIOL-VILLARD * it.
480080c646dSJean-Christophe PLAGNIOL-VILLARD */
481ec2c81c5Smario.six@gdsys.cc if (chip == (readb(&base->adr) >> 1))
482080c646dSJean-Christophe PLAGNIOL-VILLARD return -1;
483080c646dSJean-Christophe PLAGNIOL-VILLARD
484ecf591e3Smario.six@gdsys.cc return __i2c_read(base, chip, 0, 0, NULL, 0);
485080c646dSJean-Christophe PLAGNIOL-VILLARD }
486080c646dSJean-Christophe PLAGNIOL-VILLARD
__i2c_set_bus_speed(const struct fsl_i2c_base * base,unsigned int speed,int i2c_clk)487ecf591e3Smario.six@gdsys.cc static unsigned int __i2c_set_bus_speed(const struct fsl_i2c_base *base,
488ecf591e3Smario.six@gdsys.cc unsigned int speed, int i2c_clk)
489080c646dSJean-Christophe PLAGNIOL-VILLARD {
490ec2c81c5Smario.six@gdsys.cc writeb(0, &base->cr); /* stop controller */
491ecf591e3Smario.six@gdsys.cc set_i2c_bus_speed(base, i2c_clk, speed);
492ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN, &base->cr); /* start controller */
493d8c82db4STimur Tabi
494d8c82db4STimur Tabi return 0;
495080c646dSJean-Christophe PLAGNIOL-VILLARD }
496080c646dSJean-Christophe PLAGNIOL-VILLARD
497dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
fsl_i2c_init(struct i2c_adapter * adap,int speed,int slaveadd)498ad7e657cSmario.six@gdsys.cc static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
499ad7e657cSmario.six@gdsys.cc {
500ecf591e3Smario.six@gdsys.cc __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
501ecf591e3Smario.six@gdsys.cc get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
502ad7e657cSmario.six@gdsys.cc }
503ad7e657cSmario.six@gdsys.cc
504ad7e657cSmario.six@gdsys.cc static int
fsl_i2c_probe_chip(struct i2c_adapter * adap,uchar chip)505ad7e657cSmario.six@gdsys.cc fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
506ad7e657cSmario.six@gdsys.cc {
507ecf591e3Smario.six@gdsys.cc return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
508ad7e657cSmario.six@gdsys.cc }
509ad7e657cSmario.six@gdsys.cc
510ad7e657cSmario.six@gdsys.cc static int
fsl_i2c_read(struct i2c_adapter * adap,u8 chip_addr,uint offset,int olen,u8 * data,int dlen)511ad7e657cSmario.six@gdsys.cc fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen,
512ad7e657cSmario.six@gdsys.cc u8 *data, int dlen)
513ad7e657cSmario.six@gdsys.cc {
514ecf591e3Smario.six@gdsys.cc u8 *o = (u8 *)&offset;
515ecf591e3Smario.six@gdsys.cc return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
516ecf591e3Smario.six@gdsys.cc olen, data, dlen);
517ad7e657cSmario.six@gdsys.cc }
518ad7e657cSmario.six@gdsys.cc
519ad7e657cSmario.six@gdsys.cc static int
fsl_i2c_write(struct i2c_adapter * adap,u8 chip_addr,uint offset,int olen,u8 * data,int dlen)520ad7e657cSmario.six@gdsys.cc fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen,
521ad7e657cSmario.six@gdsys.cc u8 *data, int dlen)
522ad7e657cSmario.six@gdsys.cc {
523ecf591e3Smario.six@gdsys.cc u8 *o = (u8 *)&offset;
524ecf591e3Smario.six@gdsys.cc return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
525ecf591e3Smario.six@gdsys.cc olen, data, dlen);
526ad7e657cSmario.six@gdsys.cc }
527ad7e657cSmario.six@gdsys.cc
fsl_i2c_set_bus_speed(struct i2c_adapter * adap,unsigned int speed)528ad7e657cSmario.six@gdsys.cc static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap,
529ad7e657cSmario.six@gdsys.cc unsigned int speed)
530ad7e657cSmario.six@gdsys.cc {
531ecf591e3Smario.six@gdsys.cc return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
532ecf591e3Smario.six@gdsys.cc get_i2c_clock(adap->hwadapnr));
533ad7e657cSmario.six@gdsys.cc }
534ad7e657cSmario.six@gdsys.cc
53500f792e0SHeiko Schocher /*
53600f792e0SHeiko Schocher * Register fsl i2c adapters
53700f792e0SHeiko Schocher */
53816579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
53900f792e0SHeiko Schocher fsl_i2c_write, fsl_i2c_set_bus_speed,
54000f792e0SHeiko Schocher CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
54100f792e0SHeiko Schocher 0)
54200f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
54316579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
54400f792e0SHeiko Schocher fsl_i2c_write, fsl_i2c_set_bus_speed,
54500f792e0SHeiko Schocher CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
54600f792e0SHeiko Schocher 1)
547c1bce4ffSHeiko Schocher #endif
548a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
54916579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
550a17fd10fSShengzhou Liu fsl_i2c_write, fsl_i2c_set_bus_speed,
551a17fd10fSShengzhou Liu CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
552a17fd10fSShengzhou Liu 2)
553a17fd10fSShengzhou Liu #endif
554a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
55516579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
556a17fd10fSShengzhou Liu fsl_i2c_write, fsl_i2c_set_bus_speed,
557a17fd10fSShengzhou Liu CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
558a17fd10fSShengzhou Liu 3)
559a17fd10fSShengzhou Liu #endif
560dbc82ce3Smario.six@gdsys.cc #else /* CONFIG_DM_I2C */
561dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
562dbc82ce3Smario.six@gdsys.cc u32 chip_flags)
563dbc82ce3Smario.six@gdsys.cc {
564dbc82ce3Smario.six@gdsys.cc struct fsl_i2c_dev *dev = dev_get_priv(bus);
565dbc82ce3Smario.six@gdsys.cc return __i2c_probe_chip(dev->base, chip_addr);
566dbc82ce3Smario.six@gdsys.cc }
567dbc82ce3Smario.six@gdsys.cc
568dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
569dbc82ce3Smario.six@gdsys.cc {
570dbc82ce3Smario.six@gdsys.cc struct fsl_i2c_dev *dev = dev_get_priv(bus);
571dbc82ce3Smario.six@gdsys.cc return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
572dbc82ce3Smario.six@gdsys.cc }
573dbc82ce3Smario.six@gdsys.cc
574dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
575dbc82ce3Smario.six@gdsys.cc {
576dbc82ce3Smario.six@gdsys.cc struct fsl_i2c_dev *dev = dev_get_priv(bus);
57727059c3eSmario.six@gdsys.cc fdt_addr_t addr;
57827059c3eSmario.six@gdsys.cc fdt_size_t size;
579e160f7d4SSimon Glass int node = dev_of_offset(bus);
580dbc82ce3Smario.six@gdsys.cc
581e160f7d4SSimon Glass addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, node, "reg", 0,
582e160f7d4SSimon Glass &size, false);
583dbc82ce3Smario.six@gdsys.cc
584dbc82ce3Smario.six@gdsys.cc dev->base = map_sysmem(CONFIG_SYS_IMMR + addr, size);
585dbc82ce3Smario.six@gdsys.cc
586dbc82ce3Smario.six@gdsys.cc if (!dev->base)
587dbc82ce3Smario.six@gdsys.cc return -ENOMEM;
588dbc82ce3Smario.six@gdsys.cc
589e160f7d4SSimon Glass dev->index = fdtdec_get_int(gd->fdt_blob, node, "cell-index", -1);
590e160f7d4SSimon Glass dev->slaveadd = fdtdec_get_int(gd->fdt_blob, node,
591dbc82ce3Smario.six@gdsys.cc "u-boot,i2c-slave-addr", 0x7f);
592e160f7d4SSimon Glass dev->speed = fdtdec_get_int(gd->fdt_blob, node, "clock-frequency",
593e160f7d4SSimon Glass 400000);
594dbc82ce3Smario.six@gdsys.cc
595dbc82ce3Smario.six@gdsys.cc dev->i2c_clk = dev->index ? gd->arch.i2c2_clk : gd->arch.i2c1_clk;
596dbc82ce3Smario.six@gdsys.cc
597dbc82ce3Smario.six@gdsys.cc return 0;
598dbc82ce3Smario.six@gdsys.cc }
599dbc82ce3Smario.six@gdsys.cc
600dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_probe(struct udevice *bus)
601dbc82ce3Smario.six@gdsys.cc {
602dbc82ce3Smario.six@gdsys.cc struct fsl_i2c_dev *dev = dev_get_priv(bus);
603dbc82ce3Smario.six@gdsys.cc __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
604dbc82ce3Smario.six@gdsys.cc dev->index);
605dbc82ce3Smario.six@gdsys.cc return 0;
606dbc82ce3Smario.six@gdsys.cc }
607dbc82ce3Smario.six@gdsys.cc
608dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
609dbc82ce3Smario.six@gdsys.cc {
610dbc82ce3Smario.six@gdsys.cc struct fsl_i2c_dev *dev = dev_get_priv(bus);
611dbc82ce3Smario.six@gdsys.cc struct i2c_msg *dmsg, *omsg, dummy;
612dbc82ce3Smario.six@gdsys.cc
613dbc82ce3Smario.six@gdsys.cc memset(&dummy, 0, sizeof(struct i2c_msg));
614dbc82ce3Smario.six@gdsys.cc
615dbc82ce3Smario.six@gdsys.cc /* We expect either two messages (one with an offset and one with the
616dbc82ce3Smario.six@gdsys.cc * actucal data) or one message (just data) */
617dbc82ce3Smario.six@gdsys.cc if (nmsgs > 2 || nmsgs == 0) {
618dbc82ce3Smario.six@gdsys.cc debug("%s: Only one or two messages are supported.", __func__);
619dbc82ce3Smario.six@gdsys.cc return -1;
620dbc82ce3Smario.six@gdsys.cc }
621dbc82ce3Smario.six@gdsys.cc
622dbc82ce3Smario.six@gdsys.cc omsg = nmsgs == 1 ? &dummy : msg;
623dbc82ce3Smario.six@gdsys.cc dmsg = nmsgs == 1 ? msg : msg + 1;
624dbc82ce3Smario.six@gdsys.cc
625dbc82ce3Smario.six@gdsys.cc if (dmsg->flags & I2C_M_RD)
626dbc82ce3Smario.six@gdsys.cc return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
627dbc82ce3Smario.six@gdsys.cc dmsg->buf, dmsg->len);
628dbc82ce3Smario.six@gdsys.cc else
629dbc82ce3Smario.six@gdsys.cc return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
630dbc82ce3Smario.six@gdsys.cc dmsg->buf, dmsg->len);
631dbc82ce3Smario.six@gdsys.cc }
632dbc82ce3Smario.six@gdsys.cc
633dbc82ce3Smario.six@gdsys.cc static const struct dm_i2c_ops fsl_i2c_ops = {
634dbc82ce3Smario.six@gdsys.cc .xfer = fsl_i2c_xfer,
635dbc82ce3Smario.six@gdsys.cc .probe_chip = fsl_i2c_probe_chip,
636dbc82ce3Smario.six@gdsys.cc .set_bus_speed = fsl_i2c_set_bus_speed,
637dbc82ce3Smario.six@gdsys.cc };
638dbc82ce3Smario.six@gdsys.cc
639dbc82ce3Smario.six@gdsys.cc static const struct udevice_id fsl_i2c_ids[] = {
640dbc82ce3Smario.six@gdsys.cc { .compatible = "fsl-i2c", },
641dbc82ce3Smario.six@gdsys.cc { /* sentinel */ }
642dbc82ce3Smario.six@gdsys.cc };
643dbc82ce3Smario.six@gdsys.cc
644dbc82ce3Smario.six@gdsys.cc U_BOOT_DRIVER(i2c_fsl) = {
645dbc82ce3Smario.six@gdsys.cc .name = "i2c_fsl",
646dbc82ce3Smario.six@gdsys.cc .id = UCLASS_I2C,
647dbc82ce3Smario.six@gdsys.cc .of_match = fsl_i2c_ids,
648dbc82ce3Smario.six@gdsys.cc .probe = fsl_i2c_probe,
649dbc82ce3Smario.six@gdsys.cc .ofdata_to_platdata = fsl_i2c_ofdata_to_platdata,
650dbc82ce3Smario.six@gdsys.cc .priv_auto_alloc_size = sizeof(struct fsl_i2c_dev),
651dbc82ce3Smario.six@gdsys.cc .ops = &fsl_i2c_ops,
652dbc82ce3Smario.six@gdsys.cc };
653dbc82ce3Smario.six@gdsys.cc
654dbc82ce3Smario.six@gdsys.cc #endif /* CONFIG_DM_I2C */
655