1
2
3
4
5
6
7
8
9#include <common.h>
10#include <command.h>
11#include <i2c.h>
12#include <log.h>
13#include <time.h>
14#include <asm/io.h>
15#include <asm/fsl_i2c.h>
16#include <clk.h>
17#include <dm.h>
18#include <mapmem.h>
19#include <linux/delay.h>
20
21
22
23
24
25#ifndef CONFIG_I2C_MBB_TIMEOUT
26#define CONFIG_I2C_MBB_TIMEOUT 100000
27#endif
28
29
30
31
32
33#ifndef CONFIG_I2C_TIMEOUT
34#define CONFIG_I2C_TIMEOUT 100000
35#endif
36
37#define I2C_READ_BIT 1
38#define I2C_WRITE_BIT 0
39
40DECLARE_GLOBAL_DATA_PTR;
41
42#ifndef CONFIG_DM_I2C
43static const struct fsl_i2c_base *i2c_base[4] = {
44 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
45#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
46 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
47#endif
48#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
49 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
50#endif
51#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
52 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
53#endif
54};
55#endif
56
57
58
59#ifdef __M68K__
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87static const struct {
88 unsigned short divider;
89 u8 fdr;
90} fsl_i2c_speed_map[] = {
91 {20, 32}, {22, 33}, {24, 34}, {26, 35},
92 {28, 0}, {28, 36}, {30, 1}, {32, 37},
93 {34, 2}, {36, 38}, {40, 3}, {40, 39},
94 {44, 4}, {48, 5}, {48, 40}, {56, 6},
95 {56, 41}, {64, 42}, {68, 7}, {72, 43},
96 {80, 8}, {80, 44}, {88, 9}, {96, 41},
97 {104, 10}, {112, 42}, {128, 11}, {128, 43},
98 {144, 12}, {160, 13}, {160, 48}, {192, 14},
99 {192, 49}, {224, 50}, {240, 15}, {256, 51},
100 {288, 16}, {320, 17}, {320, 52}, {384, 18},
101 {384, 53}, {448, 54}, {480, 19}, {512, 55},
102 {576, 20}, {640, 21}, {640, 56}, {768, 22},
103 {768, 57}, {960, 23}, {896, 58}, {1024, 59},
104 {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
105 {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
106 {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
107 {-1, 31}
108};
109#endif
110
111
112
113
114
115
116
117
118
119
120
121
122static uint set_i2c_bus_speed(const struct fsl_i2c_base *base,
123 uint i2c_clk, uint speed)
124{
125 ushort divider = min(i2c_clk / speed, (uint)USHRT_MAX);
126
127
128
129
130
131
132
133#ifdef __PPC__
134 u8 dfsr, fdr = 0x31;
135
136 ushort a, b, ga, gb;
137 ulong c_div, est_div;
138
139#ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
140 dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
141#else
142
143 dfsr = (5 * (i2c_clk / 1000)) / 100000;
144#endif
145#ifdef CONFIG_FSL_I2C_CUSTOM_FDR
146 fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
147 speed = i2c_clk / divider;
148#else
149 debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
150 if (!dfsr)
151 dfsr = 1;
152
153 est_div = ~0;
154 for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
155 for (gb = 0; gb < 8; gb++) {
156 b = 16 << gb;
157 c_div = b * (a + ((3 * dfsr) / b) * 2);
158 if (c_div > divider && c_div < est_div) {
159 ushort bin_gb, bin_ga;
160
161 est_div = c_div;
162 bin_gb = gb << 2;
163 bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
164 fdr = bin_gb | bin_ga;
165 speed = i2c_clk / est_div;
166
167 debug("FDR: 0x%.2x, ", fdr);
168 debug("div: %ld, ", est_div);
169 debug("ga: 0x%x, gb: 0x%x, ", ga, gb);
170 debug("a: %d, b: %d, speed: %d\n", a, b, speed);
171
172
173 debug("Tr <= %d ns\n",
174 (b - 3 * dfsr) * 1000000 /
175 (i2c_clk / 1000));
176 }
177 }
178 if (a == 20)
179 a += 2;
180 if (a == 24)
181 a += 4;
182 }
183 debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr);
184 debug("FDR: 0x%.2x, speed: %d\n", fdr, speed);
185#endif
186 writeb(dfsr, &base->dfsrr);
187 writeb(fdr, &base->fdr);
188#else
189 uint i;
190
191 for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
192 if (fsl_i2c_speed_map[i].divider >= divider) {
193 u8 fdr;
194
195 fdr = fsl_i2c_speed_map[i].fdr;
196 speed = i2c_clk / fsl_i2c_speed_map[i].divider;
197 writeb(fdr, &base->fdr);
198
199 break;
200 }
201#endif
202 return speed;
203}
204
205#ifndef CONFIG_DM_I2C
206static uint get_i2c_clock(int bus)
207{
208 if (bus)
209 return gd->arch.i2c2_clk;
210 else
211 return gd->arch.i2c1_clk;
212}
213#endif
214
215static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
216{
217 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
218 unsigned long long timeval = 0;
219 int ret = -1;
220 uint flags = 0;
221
222#ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
223 uint svr = get_svr();
224
225 if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
226 (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
227 flags = I2C_CR_BIT6;
228#endif
229
230 writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
231
232 timeval = get_ticks();
233 while (!(readb(&base->sr) & I2C_SR_MBB)) {
234 if ((get_ticks() - timeval) > timeout)
235 goto err;
236 }
237
238 if (readb(&base->sr) & I2C_SR_MAL) {
239
240 writeb(0, &base->cr);
241 udelay(100);
242 writeb(I2C_CR_MSTA | flags, &base->cr);
243 writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
244 }
245
246 readb(&base->dr);
247
248 timeval = get_ticks();
249 while (!(readb(&base->sr) & I2C_SR_MIF)) {
250 if ((get_ticks() - timeval) > timeout)
251 goto err;
252 }
253 ret = 0;
254
255err:
256 writeb(I2C_CR_MEN | flags, &base->cr);
257 writeb(0, &base->sr);
258 udelay(100);
259
260 return ret;
261}
262
263static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
264 slaveadd, int i2c_clk, int busnum)
265{
266 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
267 unsigned long long timeval;
268
269#ifdef CONFIG_SYS_I2C_INIT_BOARD
270
271
272
273
274 i2c_init_board();
275#endif
276 writeb(0, &base->cr);
277 udelay(5);
278 set_i2c_bus_speed(base, i2c_clk, speed);
279 writeb(slaveadd << 1, &base->adr);
280 writeb(0x0, &base->sr);
281 writeb(I2C_CR_MEN, &base->cr);
282
283 timeval = get_ticks();
284 while (readb(&base->sr) & I2C_SR_MBB) {
285 if ((get_ticks() - timeval) < timeout)
286 continue;
287
288 if (fsl_i2c_fixup(base))
289 debug("i2c_init: BUS#%d failed to init\n",
290 busnum);
291
292 break;
293 }
294}
295
296static int i2c_wait4bus(const struct fsl_i2c_base *base)
297{
298 unsigned long long timeval = get_ticks();
299 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
300
301 while (readb(&base->sr) & I2C_SR_MBB) {
302 if ((get_ticks() - timeval) > timeout)
303 return -1;
304 }
305
306 return 0;
307}
308
309static int i2c_wait(const struct fsl_i2c_base *base, int write)
310{
311 u32 csr;
312 unsigned long long timeval = get_ticks();
313 const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
314
315 do {
316 csr = readb(&base->sr);
317 if (!(csr & I2C_SR_MIF))
318 continue;
319
320 csr = readb(&base->sr);
321
322 writeb(0x0, &base->sr);
323
324 if (csr & I2C_SR_MAL) {
325 debug("%s: MAL\n", __func__);
326 return -1;
327 }
328
329 if (!(csr & I2C_SR_MCF)) {
330 debug("%s: unfinished\n", __func__);
331 return -1;
332 }
333
334 if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
335 debug("%s: No RXACK\n", __func__);
336 return -1;
337 }
338
339 return 0;
340 } while ((get_ticks() - timeval) < timeout);
341
342 debug("%s: timed out\n", __func__);
343 return -1;
344}
345
346static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev,
347 u8 dir, int rsta)
348{
349 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
350 | (rsta ? I2C_CR_RSTA : 0),
351 &base->cr);
352
353 writeb((dev << 1) | dir, &base->dr);
354
355 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
356 return 0;
357
358 return 1;
359}
360
361static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data,
362 int length)
363{
364 int i;
365
366 for (i = 0; i < length; i++) {
367 writeb(data[i], &base->dr);
368
369 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
370 break;
371 }
372
373 return i;
374}
375
376static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data,
377 int length)
378{
379 int i;
380
381 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
382 &base->cr);
383
384
385 readb(&base->dr);
386
387 for (i = 0; i < length; i++) {
388 if (i2c_wait(base, I2C_READ_BIT) < 0)
389 break;
390
391
392 if (i == length - 2)
393 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
394 &base->cr);
395
396
397 if (i == length - 1)
398 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
399 &base->cr);
400
401 data[i] = readb(&base->dr);
402 }
403
404 return i;
405}
406
407static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset,
408 int olen, u8 *data, int dlen)
409{
410 int ret = -1;
411
412 if (i2c_wait4bus(base) < 0)
413 return -1;
414
415
416
417
418
419
420
421
422 if (olen < 0) {
423 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
424 ret = __i2c_write_data(base, data, -olen);
425
426 if (ret != -olen)
427 return -1;
428
429 if (dlen && i2c_write_addr(base, chip_addr,
430 I2C_READ_BIT, 1) != 0)
431 ret = __i2c_read_data(base, data, dlen);
432 } else {
433 if ((!dlen || olen > 0) &&
434 i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
435 __i2c_write_data(base, offset, olen) == olen)
436 ret = 0;
437
438 if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
439 olen ? 1 : 0) != 0)
440 ret = __i2c_read_data(base, data, dlen);
441 }
442
443 writeb(I2C_CR_MEN, &base->cr);
444
445 if (i2c_wait4bus(base))
446 debug("i2c_read: wait4bus timed out\n");
447
448 if (ret == dlen)
449 return 0;
450
451 return -1;
452}
453
454static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr,
455 u8 *offset, int olen, u8 *data, int dlen)
456{
457 int ret = -1;
458
459 if (i2c_wait4bus(base) < 0)
460 return -1;
461
462 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
463 __i2c_write_data(base, offset, olen) == olen) {
464 ret = __i2c_write_data(base, data, dlen);
465 }
466
467 writeb(I2C_CR_MEN, &base->cr);
468 if (i2c_wait4bus(base))
469 debug("i2c_write: wait4bus timed out\n");
470
471 if (ret == dlen)
472 return 0;
473
474 return -1;
475}
476
477static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
478{
479
480
481
482
483 if (chip == (readb(&base->adr) >> 1))
484 return -1;
485
486 return __i2c_read(base, chip, 0, 0, NULL, 0);
487}
488
489static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base,
490 uint speed, int i2c_clk)
491{
492 writeb(0, &base->cr);
493 set_i2c_bus_speed(base, i2c_clk, speed);
494 writeb(I2C_CR_MEN, &base->cr);
495
496 return 0;
497}
498
499#ifndef CONFIG_DM_I2C
500static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
501{
502 __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
503 get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
504}
505
506static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
507{
508 return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
509}
510
511static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset,
512 int olen, u8 *data, int dlen)
513{
514 u8 *o = (u8 *)&offset;
515
516 return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
517 olen, data, dlen);
518}
519
520static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset,
521 int olen, u8 *data, int dlen)
522{
523 u8 *o = (u8 *)&offset;
524
525 return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
526 olen, data, dlen);
527}
528
529static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
530{
531 return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
532 get_i2c_clock(adap->hwadapnr));
533}
534
535
536
537
538U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
539 fsl_i2c_write, fsl_i2c_set_bus_speed,
540 CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
541 0)
542#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
543U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
544 fsl_i2c_write, fsl_i2c_set_bus_speed,
545 CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
546 1)
547#endif
548#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
549U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
550 fsl_i2c_write, fsl_i2c_set_bus_speed,
551 CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
552 2)
553#endif
554#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
555U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
556 fsl_i2c_write, fsl_i2c_set_bus_speed,
557 CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
558 3)
559#endif
560#else
561static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
562 u32 chip_flags)
563{
564 struct fsl_i2c_dev *dev = dev_get_priv(bus);
565
566 return __i2c_probe_chip(dev->base, chip_addr);
567}
568
569static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed)
570{
571 struct fsl_i2c_dev *dev = dev_get_priv(bus);
572
573 return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
574}
575
576static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
577{
578 struct fsl_i2c_dev *dev = dev_get_priv(bus);
579 struct clk clock;
580
581 dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base));
582
583 if (!dev->base)
584 return -ENOMEM;
585
586 dev->index = dev_read_u32_default(bus, "cell-index", -1);
587 dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr",
588 0x7f);
589 dev->speed = dev_read_u32_default(bus, "clock-frequency",
590 I2C_SPEED_FAST_RATE);
591
592 if (!clk_get_by_index(bus, 0, &clock))
593 dev->i2c_clk = clk_get_rate(&clock);
594 else
595 dev->i2c_clk = dev->index ? gd->arch.i2c2_clk :
596 gd->arch.i2c1_clk;
597
598 return 0;
599}
600
601static int fsl_i2c_probe(struct udevice *bus)
602{
603 struct fsl_i2c_dev *dev = dev_get_priv(bus);
604
605 __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
606 dev->index);
607 return 0;
608}
609
610static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
611{
612 struct fsl_i2c_dev *dev = dev_get_priv(bus);
613 struct i2c_msg *dmsg, *omsg, dummy;
614
615 memset(&dummy, 0, sizeof(struct i2c_msg));
616
617
618
619
620 if (nmsgs > 2 || nmsgs == 0) {
621 debug("%s: Only one or two messages are supported.", __func__);
622 return -1;
623 }
624
625 omsg = nmsgs == 1 ? &dummy : msg;
626 dmsg = nmsgs == 1 ? msg : msg + 1;
627
628 if (dmsg->flags & I2C_M_RD)
629 return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
630 dmsg->buf, dmsg->len);
631 else
632 return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
633 dmsg->buf, dmsg->len);
634}
635
636static const struct dm_i2c_ops fsl_i2c_ops = {
637 .xfer = fsl_i2c_xfer,
638 .probe_chip = fsl_i2c_probe_chip,
639 .set_bus_speed = fsl_i2c_set_bus_speed,
640};
641
642static const struct udevice_id fsl_i2c_ids[] = {
643 { .compatible = "fsl-i2c", },
644 { }
645};
646
647U_BOOT_DRIVER(i2c_fsl) = {
648 .name = "i2c_fsl",
649 .id = UCLASS_I2C,
650 .of_match = fsl_i2c_ids,
651 .probe = fsl_i2c_probe,
652 .ofdata_to_platdata = fsl_i2c_ofdata_to_platdata,
653 .priv_auto_alloc_size = sizeof(struct fsl_i2c_dev),
654 .ops = &fsl_i2c_ops,
655};
656
657#endif
658