]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0 |
debb7354 | 2 | /* |
92477a63 | 3 | * Copyright 2006,2009 Freescale Semiconductor, Inc. |
debb7354 | 4 | * |
00f792e0 HS |
5 | * 2012, Heiko Schocher, DENX Software Engineering, [email protected]. |
6 | * Changes for multibus/multiadapter I2C support. | |
debb7354 JL |
7 | */ |
8 | ||
9 | #include <common.h> | |
4d45f69e | 10 | #include <command.h> |
20476726 | 11 | #include <i2c.h> /* Functional interface */ |
f7ae49fc | 12 | #include <log.h> |
6887c5be | 13 | #include <time.h> |
7237c033 | 14 | #include <asm/io.h> |
20476726 | 15 | #include <asm/fsl_i2c.h> /* HW definitions */ |
e5c762f5 | 16 | #include <clk.h> |
dbc82ce3 | 17 | #include <dm.h> |
18 | #include <mapmem.h> | |
debb7354 | 19 | |
92477a63 TT |
20 | /* The maximum number of microseconds we will wait until another master has |
21 | * released the bus. If not defined in the board header file, then use a | |
22 | * generic value. | |
23 | */ | |
24 | #ifndef CONFIG_I2C_MBB_TIMEOUT | |
25 | #define CONFIG_I2C_MBB_TIMEOUT 100000 | |
26 | #endif | |
27 | ||
28 | /* The maximum number of microseconds we will wait for a read or write | |
29 | * operation to complete. If not defined in the board header file, then use a | |
30 | * generic value. | |
31 | */ | |
32 | #ifndef CONFIG_I2C_TIMEOUT | |
6dd38cc3 | 33 | #define CONFIG_I2C_TIMEOUT 100000 |
92477a63 | 34 | #endif |
debb7354 | 35 | |
1939d969 JT |
36 | #define I2C_READ_BIT 1 |
37 | #define I2C_WRITE_BIT 0 | |
38 | ||
d8c82db4 TT |
39 | DECLARE_GLOBAL_DATA_PTR; |
40 | ||
dbc82ce3 | 41 | #ifndef CONFIG_DM_I2C |
ec2c81c5 | 42 | static const struct fsl_i2c_base *i2c_base[4] = { |
43 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET), | |
00f792e0 | 44 | #ifdef CONFIG_SYS_FSL_I2C2_OFFSET |
ec2c81c5 | 45 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET), |
a17fd10f SL |
46 | #endif |
47 | #ifdef CONFIG_SYS_FSL_I2C3_OFFSET | |
ec2c81c5 | 48 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET), |
a17fd10f SL |
49 | #endif |
50 | #ifdef CONFIG_SYS_FSL_I2C4_OFFSET | |
ec2c81c5 | 51 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET) |
be5e6181 TT |
52 | #endif |
53 | }; | |
dbc82ce3 | 54 | #endif |
debb7354 | 55 | |
d8c82db4 TT |
56 | /* I2C speed map for a DFSR value of 1 */ |
57 | ||
645cb46e | 58 | #ifdef __M68K__ |
d8c82db4 TT |
59 | /* |
60 | * Map I2C frequency dividers to FDR and DFSR values | |
61 | * | |
62 | * This structure is used to define the elements of a table that maps I2C | |
63 | * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be | |
64 | * programmed into the Frequency Divider Ratio (FDR) and Digital Filter | |
65 | * Sampling Rate (DFSR) registers. | |
66 | * | |
67 | * The actual table should be defined in the board file, and it must be called | |
68 | * fsl_i2c_speed_map[]. | |
69 | * | |
70 | * The last entry of the table must have a value of {-1, X}, where X is same | |
71 | * FDR/DFSR values as the second-to-last entry. This guarantees that any | |
72 | * search through the array will always find a match. | |
73 | * | |
74 | * The values of the divider must be in increasing numerical order, i.e. | |
75 | * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider. | |
76 | * | |
77 | * For this table, the values are based on a value of 1 for the DFSR | |
78 | * register. See the application note AN2919 "Determining the I2C Frequency | |
79 | * Divider Ratio for SCL" | |
5d9a5efa TL |
80 | * |
81 | * ColdFire I2C frequency dividers for FDR values are different from | |
82 | * PowerPC. The protocol to use the I2C module is still the same. | |
83 | * A different table is defined and are based on MCF5xxx user manual. | |
84 | * | |
d8c82db4 TT |
85 | */ |
86 | static const struct { | |
87 | unsigned short divider; | |
d8c82db4 TT |
88 | u8 fdr; |
89 | } fsl_i2c_speed_map[] = { | |
5d9a5efa TL |
90 | {20, 32}, {22, 33}, {24, 34}, {26, 35}, |
91 | {28, 0}, {28, 36}, {30, 1}, {32, 37}, | |
92 | {34, 2}, {36, 38}, {40, 3}, {40, 39}, | |
93 | {44, 4}, {48, 5}, {48, 40}, {56, 6}, | |
94 | {56, 41}, {64, 42}, {68, 7}, {72, 43}, | |
95 | {80, 8}, {80, 44}, {88, 9}, {96, 41}, | |
96 | {104, 10}, {112, 42}, {128, 11}, {128, 43}, | |
97 | {144, 12}, {160, 13}, {160, 48}, {192, 14}, | |
98 | {192, 49}, {224, 50}, {240, 15}, {256, 51}, | |
99 | {288, 16}, {320, 17}, {320, 52}, {384, 18}, | |
100 | {384, 53}, {448, 54}, {480, 19}, {512, 55}, | |
101 | {576, 20}, {640, 21}, {640, 56}, {768, 22}, | |
102 | {768, 57}, {960, 23}, {896, 58}, {1024, 59}, | |
103 | {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26}, | |
104 | {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63}, | |
105 | {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31}, | |
106 | {-1, 31} | |
d8c82db4 | 107 | }; |
645cb46e | 108 | #endif |
d8c82db4 TT |
109 | |
110 | /** | |
111 | * Set the I2C bus speed for a given I2C device | |
112 | * | |
ec2c81c5 | 113 | * @param base: the I2C device registers |
d8c82db4 TT |
114 | * @i2c_clk: I2C bus clock frequency |
115 | * @speed: the desired speed of the bus | |
116 | * | |
117 | * The I2C device must be stopped before calling this function. | |
118 | * | |
119 | * The return value is the actual bus speed that is set. | |
120 | */ | |
a059de11 MS |
121 | static uint set_i2c_bus_speed(const struct fsl_i2c_base *base, |
122 | uint i2c_clk, uint speed) | |
d8c82db4 | 123 | { |
a059de11 | 124 | ushort divider = min(i2c_clk / speed, (uint)USHRT_MAX); |
d8c82db4 TT |
125 | |
126 | /* | |
127 | * We want to choose an FDR/DFSR that generates an I2C bus speed that | |
128 | * is equal to or lower than the requested speed. That means that we | |
129 | * want the first divider that is equal to or greater than the | |
130 | * calculated divider. | |
131 | */ | |
5d9a5efa | 132 | #ifdef __PPC__ |
99404202 JT |
133 | u8 dfsr, fdr = 0x31; /* Default if no FDR found */ |
134 | /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */ | |
a059de11 MS |
135 | ushort a, b, ga, gb; |
136 | ulong c_div, est_div; | |
99404202 | 137 | |
d01ee4db | 138 | #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR |
99404202 | 139 | dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR; |
d01ee4db | 140 | #else |
99404202 JT |
141 | /* Condition 1: dfsr <= 50/T */ |
142 | dfsr = (5 * (i2c_clk / 1000)) / 100000; | |
d01ee4db JT |
143 | #endif |
144 | #ifdef CONFIG_FSL_I2C_CUSTOM_FDR | |
99404202 JT |
145 | fdr = CONFIG_FSL_I2C_CUSTOM_FDR; |
146 | speed = i2c_clk / divider; /* Fake something */ | |
147 | #else | |
148 | debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk); | |
149 | if (!dfsr) | |
150 | dfsr = 1; | |
151 | ||
152 | est_div = ~0; | |
153 | for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) { | |
154 | for (gb = 0; gb < 8; gb++) { | |
155 | b = 16 << gb; | |
a059de11 MS |
156 | c_div = b * (a + ((3 * dfsr) / b) * 2); |
157 | if (c_div > divider && c_div < est_div) { | |
158 | ushort bin_gb, bin_ga; | |
99404202 JT |
159 | |
160 | est_div = c_div; | |
161 | bin_gb = gb << 2; | |
162 | bin_ga = (ga & 0x3) | ((ga & 0x4) << 3); | |
163 | fdr = bin_gb | bin_ga; | |
164 | speed = i2c_clk / est_div; | |
a059de11 MS |
165 | |
166 | debug("FDR: 0x%.2x, ", fdr); | |
167 | debug("div: %ld, ", est_div); | |
168 | debug("ga: 0x%x, gb: 0x%x, ", ga, gb); | |
169 | debug("a: %d, b: %d, speed: %d\n", a, b, speed); | |
170 | ||
99404202 JT |
171 | /* Condition 2 not accounted for */ |
172 | debug("Tr <= %d ns\n", | |
173 | (b - 3 * dfsr) * 1000000 / | |
174 | (i2c_clk / 1000)); | |
175 | } | |
176 | } | |
177 | if (a == 20) | |
178 | a += 2; | |
179 | if (a == 24) | |
180 | a += 4; | |
181 | } | |
a059de11 MS |
182 | debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr); |
183 | debug("FDR: 0x%.2x, speed: %d\n", fdr, speed); | |
99404202 | 184 | #endif |
ec2c81c5 | 185 | writeb(dfsr, &base->dfsrr); /* set default filter */ |
186 | writeb(fdr, &base->fdr); /* set bus speed */ | |
d01ee4db | 187 | #else |
a059de11 | 188 | uint i; |
99404202 JT |
189 | |
190 | for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++) | |
191 | if (fsl_i2c_speed_map[i].divider >= divider) { | |
192 | u8 fdr; | |
193 | ||
d8c82db4 TT |
194 | fdr = fsl_i2c_speed_map[i].fdr; |
195 | speed = i2c_clk / fsl_i2c_speed_map[i].divider; | |
ec2c81c5 | 196 | writeb(fdr, &base->fdr); /* set bus speed */ |
d01ee4db | 197 | |
d8c82db4 TT |
198 | break; |
199 | } | |
99404202 | 200 | #endif |
d8c82db4 TT |
201 | return speed; |
202 | } | |
203 | ||
dbc82ce3 | 204 | #ifndef CONFIG_DM_I2C |
a059de11 | 205 | static uint get_i2c_clock(int bus) |
c9a8b25e JH |
206 | { |
207 | if (bus) | |
609e6ec3 | 208 | return gd->arch.i2c2_clk; /* I2C2 clock */ |
c9a8b25e | 209 | else |
609e6ec3 | 210 | return gd->arch.i2c1_clk; /* I2C1 clock */ |
c9a8b25e | 211 | } |
dbc82ce3 | 212 | #endif |
c9a8b25e | 213 | |
ec2c81c5 | 214 | static int fsl_i2c_fixup(const struct fsl_i2c_base *base) |
b8ce3343 CL |
215 | { |
216 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); | |
217 | unsigned long long timeval = 0; | |
218 | int ret = -1; | |
a059de11 | 219 | uint flags = 0; |
9c3f77eb CL |
220 | |
221 | #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447 | |
a059de11 MS |
222 | uint svr = get_svr(); |
223 | ||
9c3f77eb CL |
224 | if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) || |
225 | (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV)) | |
226 | flags = I2C_CR_BIT6; | |
227 | #endif | |
b8ce3343 | 228 | |
ec2c81c5 | 229 | writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr); |
b8ce3343 CL |
230 | |
231 | timeval = get_ticks(); | |
ec2c81c5 | 232 | while (!(readb(&base->sr) & I2C_SR_MBB)) { |
b8ce3343 CL |
233 | if ((get_ticks() - timeval) > timeout) |
234 | goto err; | |
235 | } | |
236 | ||
ec2c81c5 | 237 | if (readb(&base->sr) & I2C_SR_MAL) { |
b8ce3343 | 238 | /* SDA is stuck low */ |
ec2c81c5 | 239 | writeb(0, &base->cr); |
b8ce3343 | 240 | udelay(100); |
ec2c81c5 | 241 | writeb(I2C_CR_MSTA | flags, &base->cr); |
242 | writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr); | |
b8ce3343 CL |
243 | } |
244 | ||
ec2c81c5 | 245 | readb(&base->dr); |
b8ce3343 CL |
246 | |
247 | timeval = get_ticks(); | |
ec2c81c5 | 248 | while (!(readb(&base->sr) & I2C_SR_MIF)) { |
b8ce3343 CL |
249 | if ((get_ticks() - timeval) > timeout) |
250 | goto err; | |
251 | } | |
252 | ret = 0; | |
253 | ||
254 | err: | |
ec2c81c5 | 255 | writeb(I2C_CR_MEN | flags, &base->cr); |
256 | writeb(0, &base->sr); | |
b8ce3343 CL |
257 | udelay(100); |
258 | ||
259 | return ret; | |
260 | } | |
261 | ||
ecf591e3 | 262 | static void __i2c_init(const struct fsl_i2c_base *base, int speed, int |
263 | slaveadd, int i2c_clk, int busnum) | |
debb7354 | 264 | { |
b8ce3343 CL |
265 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
266 | unsigned long long timeval; | |
be5e6181 | 267 | |
39df00d9 | 268 | #ifdef CONFIG_SYS_I2C_INIT_BOARD |
26a33504 RR |
269 | /* Call board specific i2c bus reset routine before accessing the |
270 | * environment, which might be in a chip on that bus. For details | |
271 | * about this problem see doc/I2C_Edge_Conditions. | |
a059de11 | 272 | */ |
39df00d9 HS |
273 | i2c_init_board(); |
274 | #endif | |
ec2c81c5 | 275 | writeb(0, &base->cr); /* stop I2C controller */ |
00f792e0 | 276 | udelay(5); /* let it shutdown in peace */ |
ecf591e3 | 277 | set_i2c_bus_speed(base, i2c_clk, speed); |
ec2c81c5 | 278 | writeb(slaveadd << 1, &base->adr);/* write slave address */ |
279 | writeb(0x0, &base->sr); /* clear status register */ | |
280 | writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */ | |
26a33504 | 281 | |
b8ce3343 | 282 | timeval = get_ticks(); |
ec2c81c5 | 283 | while (readb(&base->sr) & I2C_SR_MBB) { |
b8ce3343 CL |
284 | if ((get_ticks() - timeval) < timeout) |
285 | continue; | |
286 | ||
ec2c81c5 | 287 | if (fsl_i2c_fixup(base)) |
b8ce3343 | 288 | debug("i2c_init: BUS#%d failed to init\n", |
ecf591e3 | 289 | busnum); |
b8ce3343 CL |
290 | |
291 | break; | |
292 | } | |
debb7354 JL |
293 | } |
294 | ||
a059de11 | 295 | static int i2c_wait4bus(const struct fsl_i2c_base *base) |
debb7354 | 296 | { |
f2302d44 | 297 | unsigned long long timeval = get_ticks(); |
92477a63 | 298 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
debb7354 | 299 | |
ec2c81c5 | 300 | while (readb(&base->sr) & I2C_SR_MBB) { |
92477a63 | 301 | if ((get_ticks() - timeval) > timeout) |
debb7354 | 302 | return -1; |
debb7354 JL |
303 | } |
304 | ||
5c9efb36 | 305 | return 0; |
debb7354 JL |
306 | } |
307 | ||
d4f422f8 | 308 | static int i2c_wait(const struct fsl_i2c_base *base, int write) |
debb7354 JL |
309 | { |
310 | u32 csr; | |
f2302d44 | 311 | unsigned long long timeval = get_ticks(); |
92477a63 | 312 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT); |
debb7354 JL |
313 | |
314 | do { | |
ec2c81c5 | 315 | csr = readb(&base->sr); |
7237c033 | 316 | if (!(csr & I2C_SR_MIF)) |
debb7354 | 317 | continue; |
21f4cbb7 | 318 | /* Read again to allow register to stabilise */ |
ec2c81c5 | 319 | csr = readb(&base->sr); |
debb7354 | 320 | |
ec2c81c5 | 321 | writeb(0x0, &base->sr); |
debb7354 | 322 | |
7237c033 | 323 | if (csr & I2C_SR_MAL) { |
a059de11 | 324 | debug("%s: MAL\n", __func__); |
debb7354 JL |
325 | return -1; |
326 | } | |
327 | ||
7237c033 | 328 | if (!(csr & I2C_SR_MCF)) { |
a059de11 | 329 | debug("%s: unfinished\n", __func__); |
debb7354 JL |
330 | return -1; |
331 | } | |
332 | ||
1939d969 | 333 | if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) { |
a059de11 | 334 | debug("%s: No RXACK\n", __func__); |
debb7354 JL |
335 | return -1; |
336 | } | |
337 | ||
338 | return 0; | |
92477a63 | 339 | } while ((get_ticks() - timeval) < timeout); |
debb7354 | 340 | |
a059de11 | 341 | debug("%s: timed out\n", __func__); |
debb7354 JL |
342 | return -1; |
343 | } | |
344 | ||
d4f422f8 MS |
345 | static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev, |
346 | u8 dir, int rsta) | |
debb7354 | 347 | { |
7237c033 JL |
348 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
349 | | (rsta ? I2C_CR_RSTA : 0), | |
ec2c81c5 | 350 | &base->cr); |
debb7354 | 351 | |
ec2c81c5 | 352 | writeb((dev << 1) | dir, &base->dr); |
debb7354 | 353 | |
ecf591e3 | 354 | if (i2c_wait(base, I2C_WRITE_BIT) < 0) |
debb7354 JL |
355 | return 0; |
356 | ||
357 | return 1; | |
358 | } | |
359 | ||
d4f422f8 MS |
360 | static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data, |
361 | int length) | |
debb7354 JL |
362 | { |
363 | int i; | |
5c9efb36 | 364 | |
5c9efb36 | 365 | for (i = 0; i < length; i++) { |
ec2c81c5 | 366 | writeb(data[i], &base->dr); |
debb7354 | 367 | |
ecf591e3 | 368 | if (i2c_wait(base, I2C_WRITE_BIT) < 0) |
debb7354 JL |
369 | break; |
370 | } | |
371 | ||
372 | return i; | |
373 | } | |
374 | ||
d4f422f8 MS |
375 | static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data, |
376 | int length) | |
debb7354 JL |
377 | { |
378 | int i; | |
379 | ||
7237c033 | 380 | writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0), |
ec2c81c5 | 381 | &base->cr); |
debb7354 JL |
382 | |
383 | /* dummy read */ | |
ec2c81c5 | 384 | readb(&base->dr); |
debb7354 | 385 | |
5c9efb36 | 386 | for (i = 0; i < length; i++) { |
ecf591e3 | 387 | if (i2c_wait(base, I2C_READ_BIT) < 0) |
debb7354 JL |
388 | break; |
389 | ||
390 | /* Generate ack on last next to last byte */ | |
391 | if (i == length - 2) | |
7237c033 | 392 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK, |
ec2c81c5 | 393 | &base->cr); |
debb7354 | 394 | |
d1c9e5b3 | 395 | /* Do not generate stop on last byte */ |
debb7354 | 396 | if (i == length - 1) |
d1c9e5b3 | 397 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX, |
ec2c81c5 | 398 | &base->cr); |
debb7354 | 399 | |
ec2c81c5 | 400 | data[i] = readb(&base->dr); |
debb7354 | 401 | } |
5c9efb36 | 402 | |
debb7354 JL |
403 | return i; |
404 | } | |
405 | ||
a059de11 MS |
406 | static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, |
407 | int olen, u8 *data, int dlen) | |
debb7354 | 408 | { |
2b21e960 | 409 | int ret = -1; /* signal error */ |
debb7354 | 410 | |
ecf591e3 | 411 | if (i2c_wait4bus(base) < 0) |
b778c1b5 RP |
412 | return -1; |
413 | ||
386b2769 | 414 | /* Some drivers use offset lengths in excess of 4 bytes. These drivers |
415 | * adhere to the following convention: | |
416 | * - the offset length is passed as negative (that is, the absolute | |
417 | * value of olen is the actual offset length) | |
418 | * - the offset itself is passed in data, which is overwritten by the | |
419 | * subsequent read operation | |
a405764c | 420 | */ |
2b21e960 | 421 | if (olen < 0) { |
ecf591e3 | 422 | if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0) |
423 | ret = __i2c_write_data(base, data, -olen); | |
a405764c | 424 | |
03a112aa | 425 | if (ret != -olen) |
a405764c | 426 | return -1; |
f6f5f709 | 427 | |
ecf591e3 | 428 | if (dlen && i2c_write_addr(base, chip_addr, |
2b21e960 | 429 | I2C_READ_BIT, 1) != 0) |
ecf591e3 | 430 | ret = __i2c_read_data(base, data, dlen); |
a405764c | 431 | } else { |
2b21e960 | 432 | if ((!dlen || olen > 0) && |
ecf591e3 | 433 | i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 && |
434 | __i2c_write_data(base, offset, olen) == olen) | |
2b21e960 | 435 | ret = 0; /* No error so far */ |
436 | ||
ecf591e3 | 437 | if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT, |
2b21e960 | 438 | olen ? 1 : 0) != 0) |
ecf591e3 | 439 | ret = __i2c_read_data(base, data, dlen); |
a405764c | 440 | } |
debb7354 | 441 | |
ec2c81c5 | 442 | writeb(I2C_CR_MEN, &base->cr); |
debb7354 | 443 | |
ecf591e3 | 444 | if (i2c_wait4bus(base)) /* Wait until STOP */ |
d1c9e5b3 JT |
445 | debug("i2c_read: wait4bus timed out\n"); |
446 | ||
2b21e960 | 447 | if (ret == dlen) |
448 | return 0; | |
4d45f69e JL |
449 | |
450 | return -1; | |
debb7354 JL |
451 | } |
452 | ||
a059de11 MS |
453 | static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr, |
454 | u8 *offset, int olen, u8 *data, int dlen) | |
debb7354 | 455 | { |
2b21e960 | 456 | int ret = -1; /* signal error */ |
debb7354 | 457 | |
ecf591e3 | 458 | if (i2c_wait4bus(base) < 0) |
b8ce3343 CL |
459 | return -1; |
460 | ||
ecf591e3 | 461 | if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 && |
462 | __i2c_write_data(base, offset, olen) == olen) { | |
463 | ret = __i2c_write_data(base, data, dlen); | |
4d45f69e | 464 | } |
debb7354 | 465 | |
ec2c81c5 | 466 | writeb(I2C_CR_MEN, &base->cr); |
ecf591e3 | 467 | if (i2c_wait4bus(base)) /* Wait until STOP */ |
21f4cbb7 | 468 | debug("i2c_write: wait4bus timed out\n"); |
debb7354 | 469 | |
2b21e960 | 470 | if (ret == dlen) |
471 | return 0; | |
4d45f69e JL |
472 | |
473 | return -1; | |
debb7354 JL |
474 | } |
475 | ||
a059de11 | 476 | static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip) |
debb7354 | 477 | { |
a059de11 | 478 | /* For unknown reason the controller will ACK when |
f6f5f709 JT |
479 | * probing for a slave with the same address, so skip |
480 | * it. | |
debb7354 | 481 | */ |
ec2c81c5 | 482 | if (chip == (readb(&base->adr) >> 1)) |
be5e6181 | 483 | return -1; |
be5e6181 | 484 | |
ecf591e3 | 485 | return __i2c_read(base, chip, 0, 0, NULL, 0); |
be5e6181 TT |
486 | } |
487 | ||
a059de11 MS |
488 | static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base, |
489 | uint speed, int i2c_clk) | |
be5e6181 | 490 | { |
ec2c81c5 | 491 | writeb(0, &base->cr); /* stop controller */ |
ecf591e3 | 492 | set_i2c_bus_speed(base, i2c_clk, speed); |
ec2c81c5 | 493 | writeb(I2C_CR_MEN, &base->cr); /* start controller */ |
d8c82db4 TT |
494 | |
495 | return 0; | |
be5e6181 TT |
496 | } |
497 | ||
dbc82ce3 | 498 | #ifndef CONFIG_DM_I2C |
ad7e657c | 499 | static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) |
500 | { | |
ecf591e3 | 501 | __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd, |
502 | get_i2c_clock(adap->hwadapnr), adap->hwadapnr); | |
ad7e657c | 503 | } |
504 | ||
a059de11 | 505 | static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip) |
ad7e657c | 506 | { |
ecf591e3 | 507 | return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip); |
ad7e657c | 508 | } |
509 | ||
a059de11 MS |
510 | static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset, |
511 | int olen, u8 *data, int dlen) | |
ad7e657c | 512 | { |
ecf591e3 | 513 | u8 *o = (u8 *)&offset; |
a059de11 | 514 | |
ecf591e3 | 515 | return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen], |
516 | olen, data, dlen); | |
ad7e657c | 517 | } |
518 | ||
a059de11 MS |
519 | static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset, |
520 | int olen, u8 *data, int dlen) | |
ad7e657c | 521 | { |
ecf591e3 | 522 | u8 *o = (u8 *)&offset; |
a059de11 | 523 | |
ecf591e3 | 524 | return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen], |
525 | olen, data, dlen); | |
ad7e657c | 526 | } |
527 | ||
a059de11 | 528 | static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed) |
ad7e657c | 529 | { |
ecf591e3 | 530 | return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed, |
531 | get_i2c_clock(adap->hwadapnr)); | |
ad7e657c | 532 | } |
533 | ||
00f792e0 HS |
534 | /* |
535 | * Register fsl i2c adapters | |
536 | */ | |
16579ecb | 537 | U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
00f792e0 HS |
538 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
539 | CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE, | |
540 | 0) | |
541 | #ifdef CONFIG_SYS_FSL_I2C2_OFFSET | |
16579ecb | 542 | U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
00f792e0 HS |
543 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
544 | CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE, | |
545 | 1) | |
c1bce4ff | 546 | #endif |
a17fd10f | 547 | #ifdef CONFIG_SYS_FSL_I2C3_OFFSET |
16579ecb | 548 | U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
a17fd10f SL |
549 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
550 | CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE, | |
551 | 2) | |
552 | #endif | |
553 | #ifdef CONFIG_SYS_FSL_I2C4_OFFSET | |
16579ecb | 554 | U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
a17fd10f SL |
555 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
556 | CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE, | |
557 | 3) | |
558 | #endif | |
dbc82ce3 | 559 | #else /* CONFIG_DM_I2C */ |
560 | static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr, | |
561 | u32 chip_flags) | |
562 | { | |
563 | struct fsl_i2c_dev *dev = dev_get_priv(bus); | |
a059de11 | 564 | |
dbc82ce3 | 565 | return __i2c_probe_chip(dev->base, chip_addr); |
566 | } | |
567 | ||
a059de11 | 568 | static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed) |
dbc82ce3 | 569 | { |
570 | struct fsl_i2c_dev *dev = dev_get_priv(bus); | |
a059de11 | 571 | |
dbc82ce3 | 572 | return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk); |
573 | } | |
574 | ||
575 | static int fsl_i2c_ofdata_to_platdata(struct udevice *bus) | |
576 | { | |
577 | struct fsl_i2c_dev *dev = dev_get_priv(bus); | |
e5c762f5 | 578 | struct clk clock; |
dbc82ce3 | 579 | |
d934832d | 580 | dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base)); |
dbc82ce3 | 581 | |
582 | if (!dev->base) | |
583 | return -ENOMEM; | |
584 | ||
84a4d34e MS |
585 | dev->index = dev_read_u32_default(bus, "cell-index", -1); |
586 | dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr", | |
587 | 0x7f); | |
f3d46152 SG |
588 | dev->speed = dev_read_u32_default(bus, "clock-frequency", |
589 | I2C_SPEED_FAST_RATE); | |
dbc82ce3 | 590 | |
e5c762f5 MS |
591 | if (!clk_get_by_index(bus, 0, &clock)) |
592 | dev->i2c_clk = clk_get_rate(&clock); | |
593 | else | |
594 | dev->i2c_clk = dev->index ? gd->arch.i2c2_clk : | |
595 | gd->arch.i2c1_clk; | |
dbc82ce3 | 596 | |
597 | return 0; | |
598 | } | |
599 | ||
600 | static int fsl_i2c_probe(struct udevice *bus) | |
601 | { | |
602 | struct fsl_i2c_dev *dev = dev_get_priv(bus); | |
a059de11 | 603 | |
dbc82ce3 | 604 | __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk, |
605 | dev->index); | |
606 | return 0; | |
607 | } | |
608 | ||
609 | static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) | |
610 | { | |
611 | struct fsl_i2c_dev *dev = dev_get_priv(bus); | |
612 | struct i2c_msg *dmsg, *omsg, dummy; | |
613 | ||
614 | memset(&dummy, 0, sizeof(struct i2c_msg)); | |
615 | ||
616 | /* We expect either two messages (one with an offset and one with the | |
a059de11 MS |
617 | * actual data) or one message (just data) |
618 | */ | |
dbc82ce3 | 619 | if (nmsgs > 2 || nmsgs == 0) { |
620 | debug("%s: Only one or two messages are supported.", __func__); | |
621 | return -1; | |
622 | } | |
623 | ||
624 | omsg = nmsgs == 1 ? &dummy : msg; | |
625 | dmsg = nmsgs == 1 ? msg : msg + 1; | |
626 | ||
627 | if (dmsg->flags & I2C_M_RD) | |
628 | return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len, | |
629 | dmsg->buf, dmsg->len); | |
630 | else | |
631 | return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len, | |
632 | dmsg->buf, dmsg->len); | |
633 | } | |
634 | ||
635 | static const struct dm_i2c_ops fsl_i2c_ops = { | |
636 | .xfer = fsl_i2c_xfer, | |
637 | .probe_chip = fsl_i2c_probe_chip, | |
638 | .set_bus_speed = fsl_i2c_set_bus_speed, | |
639 | }; | |
640 | ||
641 | static const struct udevice_id fsl_i2c_ids[] = { | |
642 | { .compatible = "fsl-i2c", }, | |
643 | { /* sentinel */ } | |
644 | }; | |
645 | ||
646 | U_BOOT_DRIVER(i2c_fsl) = { | |
647 | .name = "i2c_fsl", | |
648 | .id = UCLASS_I2C, | |
649 | .of_match = fsl_i2c_ids, | |
650 | .probe = fsl_i2c_probe, | |
651 | .ofdata_to_platdata = fsl_i2c_ofdata_to_platdata, | |
652 | .priv_auto_alloc_size = sizeof(struct fsl_i2c_dev), | |
653 | .ops = &fsl_i2c_ops, | |
654 | }; | |
655 | ||
656 | #endif /* CONFIG_DM_I2C */ |