1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2006,2009 Freescale Semiconductor, Inc.
6 * Changes for multibus/multiadapter I2C support.
11 #include <i2c.h> /* Functional interface */
15 #include <asm/fsl_i2c.h> /* HW definitions */
19 #include <linux/delay.h>
21 /* The maximum number of microseconds we will wait until another master has
22 * released the bus. If not defined in the board header file, then use a
25 #ifndef CONFIG_I2C_MBB_TIMEOUT
26 #define CONFIG_I2C_MBB_TIMEOUT 100000
29 /* The maximum number of microseconds we will wait for a read or write
30 * operation to complete. If not defined in the board header file, then use a
33 #ifndef CONFIG_I2C_TIMEOUT
34 #define CONFIG_I2C_TIMEOUT 100000
37 #define I2C_READ_BIT 1
38 #define I2C_WRITE_BIT 0
40 DECLARE_GLOBAL_DATA_PTR;
43 static 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),
48 #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
49 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
51 #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
52 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
57 /* I2C speed map for a DFSR value of 1 */
61 * Map I2C frequency dividers to FDR and DFSR values
63 * This structure is used to define the elements of a table that maps I2C
64 * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
65 * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
66 * Sampling Rate (DFSR) registers.
68 * The actual table should be defined in the board file, and it must be called
69 * fsl_i2c_speed_map[].
71 * The last entry of the table must have a value of {-1, X}, where X is same
72 * FDR/DFSR values as the second-to-last entry. This guarantees that any
73 * search through the array will always find a match.
75 * The values of the divider must be in increasing numerical order, i.e.
76 * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
78 * For this table, the values are based on a value of 1 for the DFSR
79 * register. See the application note AN2919 "Determining the I2C Frequency
80 * Divider Ratio for SCL"
82 * ColdFire I2C frequency dividers for FDR values are different from
83 * PowerPC. The protocol to use the I2C module is still the same.
84 * A different table is defined and are based on MCF5xxx user manual.
88 unsigned short divider;
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},
112 * Set the I2C bus speed for a given I2C device
114 * @param base: the I2C device registers
115 * @i2c_clk: I2C bus clock frequency
116 * @speed: the desired speed of the bus
118 * The I2C device must be stopped before calling this function.
120 * The return value is the actual bus speed that is set.
122 static uint set_i2c_bus_speed(const struct fsl_i2c_base *base,
123 uint i2c_clk, uint speed)
125 ushort divider = min(i2c_clk / speed, (uint)USHRT_MAX);
128 * We want to choose an FDR/DFSR that generates an I2C bus speed that
129 * is equal to or lower than the requested speed. That means that we
130 * want the first divider that is equal to or greater than the
131 * calculated divider.
134 u8 dfsr, fdr = 0x31; /* Default if no FDR found */
135 /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
137 ulong c_div, est_div;
139 #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
140 dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
142 /* Condition 1: dfsr <= 50/T */
143 dfsr = (5 * (i2c_clk / 1000)) / 100000;
145 #ifdef CONFIG_FSL_I2C_CUSTOM_FDR
146 fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
147 speed = i2c_clk / divider; /* Fake something */
149 debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
154 for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
155 for (gb = 0; gb < 8; 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;
163 bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
164 fdr = bin_gb | bin_ga;
165 speed = i2c_clk / est_div;
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);
172 /* Condition 2 not accounted for */
173 debug("Tr <= %d ns\n",
174 (b - 3 * dfsr) * 1000000 /
183 debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr);
184 debug("FDR: 0x%.2x, speed: %d\n", fdr, speed);
186 writeb(dfsr, &base->dfsrr); /* set default filter */
187 writeb(fdr, &base->fdr); /* set bus speed */
191 for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
192 if (fsl_i2c_speed_map[i].divider >= divider) {
195 fdr = fsl_i2c_speed_map[i].fdr;
196 speed = i2c_clk / fsl_i2c_speed_map[i].divider;
197 writeb(fdr, &base->fdr); /* set bus speed */
205 #ifndef CONFIG_DM_I2C
206 static uint get_i2c_clock(int bus)
209 return gd->arch.i2c2_clk; /* I2C2 clock */
211 return gd->arch.i2c1_clk; /* I2C1 clock */
215 static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
217 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
218 unsigned long long timeval = 0;
222 #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
223 uint svr = get_svr();
225 if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
226 (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
230 writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
232 timeval = get_ticks();
233 while (!(readb(&base->sr) & I2C_SR_MBB)) {
234 if ((get_ticks() - timeval) > timeout)
238 if (readb(&base->sr) & I2C_SR_MAL) {
239 /* SDA is stuck low */
240 writeb(0, &base->cr);
242 writeb(I2C_CR_MSTA | flags, &base->cr);
243 writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
248 timeval = get_ticks();
249 while (!(readb(&base->sr) & I2C_SR_MIF)) {
250 if ((get_ticks() - timeval) > timeout)
256 writeb(I2C_CR_MEN | flags, &base->cr);
257 writeb(0, &base->sr);
263 static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
264 slaveadd, int i2c_clk, int busnum)
266 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
267 unsigned long long timeval;
269 #ifdef CONFIG_SYS_I2C_INIT_BOARD
270 /* Call board specific i2c bus reset routine before accessing the
271 * environment, which might be in a chip on that bus. For details
272 * about this problem see doc/I2C_Edge_Conditions.
276 writeb(0, &base->cr); /* stop I2C controller */
277 udelay(5); /* let it shutdown in peace */
278 set_i2c_bus_speed(base, i2c_clk, speed);
279 writeb(slaveadd << 1, &base->adr);/* write slave address */
280 writeb(0x0, &base->sr); /* clear status register */
281 writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */
283 timeval = get_ticks();
284 while (readb(&base->sr) & I2C_SR_MBB) {
285 if ((get_ticks() - timeval) < timeout)
288 if (fsl_i2c_fixup(base))
289 debug("i2c_init: BUS#%d failed to init\n",
296 static int i2c_wait4bus(const struct fsl_i2c_base *base)
298 unsigned long long timeval = get_ticks();
299 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
301 while (readb(&base->sr) & I2C_SR_MBB) {
302 if ((get_ticks() - timeval) > timeout)
309 static int i2c_wait(const struct fsl_i2c_base *base, int write)
312 unsigned long long timeval = get_ticks();
313 const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
316 csr = readb(&base->sr);
317 if (!(csr & I2C_SR_MIF))
319 /* Read again to allow register to stabilise */
320 csr = readb(&base->sr);
322 writeb(0x0, &base->sr);
324 if (csr & I2C_SR_MAL) {
325 debug("%s: MAL\n", __func__);
329 if (!(csr & I2C_SR_MCF)) {
330 debug("%s: unfinished\n", __func__);
334 if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
335 debug("%s: No RXACK\n", __func__);
340 } while ((get_ticks() - timeval) < timeout);
342 debug("%s: timed out\n", __func__);
346 static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev,
349 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
350 | (rsta ? I2C_CR_RSTA : 0),
353 writeb((dev << 1) | dir, &base->dr);
355 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
361 static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data,
366 for (i = 0; i < length; i++) {
367 writeb(data[i], &base->dr);
369 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
376 static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data,
381 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
387 for (i = 0; i < length; i++) {
388 if (i2c_wait(base, I2C_READ_BIT) < 0)
391 /* Generate ack on last next to last byte */
393 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
396 /* Do not generate stop on last byte */
398 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
401 data[i] = readb(&base->dr);
407 static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset,
408 int olen, u8 *data, int dlen)
410 int ret = -1; /* signal error */
412 if (i2c_wait4bus(base) < 0)
415 /* Some drivers use offset lengths in excess of 4 bytes. These drivers
416 * adhere to the following convention:
417 * - the offset length is passed as negative (that is, the absolute
418 * value of olen is the actual offset length)
419 * - the offset itself is passed in data, which is overwritten by the
420 * subsequent read operation
423 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
424 ret = __i2c_write_data(base, data, -olen);
429 if (dlen && i2c_write_addr(base, chip_addr,
430 I2C_READ_BIT, 1) != 0)
431 ret = __i2c_read_data(base, data, dlen);
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; /* No error so far */
438 if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
440 ret = __i2c_read_data(base, data, dlen);
443 writeb(I2C_CR_MEN, &base->cr);
445 if (i2c_wait4bus(base)) /* Wait until STOP */
446 debug("i2c_read: wait4bus timed out\n");
454 static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr,
455 u8 *offset, int olen, u8 *data, int dlen)
457 int ret = -1; /* signal error */
459 if (i2c_wait4bus(base) < 0)
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);
467 writeb(I2C_CR_MEN, &base->cr);
468 if (i2c_wait4bus(base)) /* Wait until STOP */
469 debug("i2c_write: wait4bus timed out\n");
477 static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
479 /* For unknown reason the controller will ACK when
480 * probing for a slave with the same address, so skip
483 if (chip == (readb(&base->adr) >> 1))
486 return __i2c_read(base, chip, 0, 0, NULL, 0);
489 static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base,
490 uint speed, int i2c_clk)
492 writeb(0, &base->cr); /* stop controller */
493 set_i2c_bus_speed(base, i2c_clk, speed);
494 writeb(I2C_CR_MEN, &base->cr); /* start controller */
499 #ifndef CONFIG_DM_I2C
500 static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
502 __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
503 get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
506 static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
508 return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
511 static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset,
512 int olen, u8 *data, int dlen)
514 u8 *o = (u8 *)&offset;
516 return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
520 static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset,
521 int olen, u8 *data, int dlen)
523 u8 *o = (u8 *)&offset;
525 return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
529 static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
531 return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
532 get_i2c_clock(adap->hwadapnr));
536 * Register fsl i2c adapters
538 U_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,
542 #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
543 U_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,
548 #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
549 U_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,
554 #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
555 U_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,
560 #else /* CONFIG_DM_I2C */
561 static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
564 struct fsl_i2c_dev *dev = dev_get_priv(bus);
566 return __i2c_probe_chip(dev->base, chip_addr);
569 static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed)
571 struct fsl_i2c_dev *dev = dev_get_priv(bus);
573 return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
576 static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
578 struct fsl_i2c_dev *dev = dev_get_priv(bus);
581 dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base));
586 dev->index = dev_read_u32_default(bus, "cell-index", -1);
587 dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr",
589 dev->speed = dev_read_u32_default(bus, "clock-frequency",
590 I2C_SPEED_FAST_RATE);
592 if (!clk_get_by_index(bus, 0, &clock))
593 dev->i2c_clk = clk_get_rate(&clock);
595 dev->i2c_clk = dev->index ? gd->arch.i2c2_clk :
601 static int fsl_i2c_probe(struct udevice *bus)
603 struct fsl_i2c_dev *dev = dev_get_priv(bus);
605 __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
610 static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
612 struct fsl_i2c_dev *dev = dev_get_priv(bus);
613 struct i2c_msg *dmsg, *omsg, dummy;
615 memset(&dummy, 0, sizeof(struct i2c_msg));
617 /* We expect either two messages (one with an offset and one with the
618 * actual data) or one message (just data)
620 if (nmsgs > 2 || nmsgs == 0) {
621 debug("%s: Only one or two messages are supported.", __func__);
625 omsg = nmsgs == 1 ? &dummy : msg;
626 dmsg = nmsgs == 1 ? msg : msg + 1;
628 if (dmsg->flags & I2C_M_RD)
629 return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
630 dmsg->buf, dmsg->len);
632 return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
633 dmsg->buf, dmsg->len);
636 static 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,
642 static const struct udevice_id fsl_i2c_ids[] = {
643 { .compatible = "fsl-i2c", },
647 U_BOOT_DRIVER(i2c_fsl) = {
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),
657 #endif /* CONFIG_DM_I2C */