66a90e4258629caef93476cafac9847751e5747b
[oweals/u-boot.git] / drivers / i2c / mxc_i2c.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * i2c driver for Freescale i.MX series
4  *
5  * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
6  * (c) 2011 Marek Vasut <marek.vasut@gmail.com>
7  *
8  * Based on i2c-imx.c from linux kernel:
9  *  Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de>
10  *  Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de>
11  *  Copyright (C) 2007 RightHand Technologies, Inc.
12  *  Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
13  *
14  */
15
16 #include <common.h>
17 #include <log.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/imx-regs.h>
20 #include <dm/device_compat.h>
21 #include <linux/errno.h>
22 #include <asm/mach-imx/mxc_i2c.h>
23 #include <asm/mach-imx/sys_proto.h>
24 #include <asm/io.h>
25 #include <i2c.h>
26 #include <watchdog.h>
27 #include <dm.h>
28 #include <dm/pinctrl.h>
29 #include <fdtdec.h>
30
31 DECLARE_GLOBAL_DATA_PTR;
32
33 #define I2C_QUIRK_FLAG          (1 << 0)
34
35 #define IMX_I2C_REGSHIFT        2
36 #define VF610_I2C_REGSHIFT      0
37
38 #define I2C_EARLY_INIT_INDEX            0
39 #ifdef CONFIG_SYS_I2C_IFDR_DIV
40 #define I2C_IFDR_DIV_CONSERVATIVE       CONFIG_SYS_I2C_IFDR_DIV
41 #else
42 #define I2C_IFDR_DIV_CONSERVATIVE       0x7e
43 #endif
44
45 /* Register index */
46 #define IADR    0
47 #define IFDR    1
48 #define I2CR    2
49 #define I2SR    3
50 #define I2DR    4
51
52 #define I2CR_IIEN       (1 << 6)
53 #define I2CR_MSTA       (1 << 5)
54 #define I2CR_MTX        (1 << 4)
55 #define I2CR_TX_NO_AK   (1 << 3)
56 #define I2CR_RSTA       (1 << 2)
57
58 #define I2SR_ICF        (1 << 7)
59 #define I2SR_IBB        (1 << 5)
60 #define I2SR_IAL        (1 << 4)
61 #define I2SR_IIF        (1 << 1)
62 #define I2SR_RX_NO_AK   (1 << 0)
63
64 #ifdef I2C_QUIRK_REG
65 #define I2CR_IEN        (0 << 7)
66 #define I2CR_IDIS       (1 << 7)
67 #define I2SR_IIF_CLEAR  (1 << 1)
68 #else
69 #define I2CR_IEN        (1 << 7)
70 #define I2CR_IDIS       (0 << 7)
71 #define I2SR_IIF_CLEAR  (0 << 1)
72 #endif
73
74 #ifdef I2C_QUIRK_REG
75 static u16 i2c_clk_div[60][2] = {
76         { 20,   0x00 }, { 22,   0x01 }, { 24,   0x02 }, { 26,   0x03 },
77         { 28,   0x04 }, { 30,   0x05 }, { 32,   0x09 }, { 34,   0x06 },
78         { 36,   0x0A }, { 40,   0x07 }, { 44,   0x0C }, { 48,   0x0D },
79         { 52,   0x43 }, { 56,   0x0E }, { 60,   0x45 }, { 64,   0x12 },
80         { 68,   0x0F }, { 72,   0x13 }, { 80,   0x14 }, { 88,   0x15 },
81         { 96,   0x19 }, { 104,  0x16 }, { 112,  0x1A }, { 128,  0x17 },
82         { 136,  0x4F }, { 144,  0x1C }, { 160,  0x1D }, { 176,  0x55 },
83         { 192,  0x1E }, { 208,  0x56 }, { 224,  0x22 }, { 228,  0x24 },
84         { 240,  0x1F }, { 256,  0x23 }, { 288,  0x5C }, { 320,  0x25 },
85         { 384,  0x26 }, { 448,  0x2A }, { 480,  0x27 }, { 512,  0x2B },
86         { 576,  0x2C }, { 640,  0x2D }, { 768,  0x31 }, { 896,  0x32 },
87         { 960,  0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
88         { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
89         { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
90         { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
91 };
92 #else
93 static u16 i2c_clk_div[50][2] = {
94         { 22,   0x20 }, { 24,   0x21 }, { 26,   0x22 }, { 28,   0x23 },
95         { 30,   0x00 }, { 32,   0x24 }, { 36,   0x25 }, { 40,   0x26 },
96         { 42,   0x03 }, { 44,   0x27 }, { 48,   0x28 }, { 52,   0x05 },
97         { 56,   0x29 }, { 60,   0x06 }, { 64,   0x2A }, { 72,   0x2B },
98         { 80,   0x2C }, { 88,   0x09 }, { 96,   0x2D }, { 104,  0x0A },
99         { 112,  0x2E }, { 128,  0x2F }, { 144,  0x0C }, { 160,  0x30 },
100         { 192,  0x31 }, { 224,  0x32 }, { 240,  0x0F }, { 256,  0x33 },
101         { 288,  0x10 }, { 320,  0x34 }, { 384,  0x35 }, { 448,  0x36 },
102         { 480,  0x13 }, { 512,  0x37 }, { 576,  0x14 }, { 640,  0x38 },
103         { 768,  0x39 }, { 896,  0x3A }, { 960,  0x17 }, { 1024, 0x3B },
104         { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
105         { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
106         { 3072, 0x1E }, { 3840, 0x1F }
107 };
108 #endif
109
110 #ifndef CONFIG_SYS_MXC_I2C1_SPEED
111 #define CONFIG_SYS_MXC_I2C1_SPEED 100000
112 #endif
113 #ifndef CONFIG_SYS_MXC_I2C2_SPEED
114 #define CONFIG_SYS_MXC_I2C2_SPEED 100000
115 #endif
116 #ifndef CONFIG_SYS_MXC_I2C3_SPEED
117 #define CONFIG_SYS_MXC_I2C3_SPEED 100000
118 #endif
119 #ifndef CONFIG_SYS_MXC_I2C4_SPEED
120 #define CONFIG_SYS_MXC_I2C4_SPEED 100000
121 #endif
122
123 #ifndef CONFIG_SYS_MXC_I2C1_SLAVE
124 #define CONFIG_SYS_MXC_I2C1_SLAVE 0
125 #endif
126 #ifndef CONFIG_SYS_MXC_I2C2_SLAVE
127 #define CONFIG_SYS_MXC_I2C2_SLAVE 0
128 #endif
129 #ifndef CONFIG_SYS_MXC_I2C3_SLAVE
130 #define CONFIG_SYS_MXC_I2C3_SLAVE 0
131 #endif
132 #ifndef CONFIG_SYS_MXC_I2C4_SLAVE
133 #define CONFIG_SYS_MXC_I2C4_SLAVE 0
134 #endif
135
136 /*
137  * Calculate and set proper clock divider
138  */
139 static uint8_t i2c_imx_get_clk(struct mxc_i2c_bus *i2c_bus, unsigned int rate)
140 {
141         unsigned int i2c_clk_rate;
142         unsigned int div;
143         u8 clk_div;
144
145 #if defined(CONFIG_MX31)
146         struct clock_control_regs *sc_regs =
147                 (struct clock_control_regs *)CCM_BASE;
148
149         /* start the required I2C clock */
150         writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
151                 &sc_regs->cgr0);
152 #endif
153
154         /* Divider value calculation */
155 #if CONFIG_IS_ENABLED(CLK)
156         i2c_clk_rate = clk_get_rate(&i2c_bus->per_clk);
157 #else
158         i2c_clk_rate = mxc_get_clock(MXC_I2C_CLK);
159 #endif
160
161         div = (i2c_clk_rate + rate - 1) / rate;
162         if (div < i2c_clk_div[0][0])
163                 clk_div = 0;
164         else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
165                 clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
166         else
167                 for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
168                         ;
169
170         /* Store divider value */
171         return clk_div;
172 }
173
174 /*
175  * Set I2C Bus speed
176  */
177 static int bus_i2c_set_bus_speed(struct mxc_i2c_bus *i2c_bus, int speed)
178 {
179         ulong base = i2c_bus->base;
180         bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
181         u8 clk_idx = i2c_imx_get_clk(i2c_bus, speed);
182         u8 idx = i2c_clk_div[clk_idx][1];
183         int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
184
185         if (!base)
186                 return -EINVAL;
187
188         /* Store divider value */
189         writeb(idx, base + (IFDR << reg_shift));
190
191         /* Reset module */
192         writeb(I2CR_IDIS, base + (I2CR << reg_shift));
193         writeb(0, base + (I2SR << reg_shift));
194         return 0;
195 }
196
197 #define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
198 #define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
199 #define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
200
201 static int wait_for_sr_state(struct mxc_i2c_bus *i2c_bus, unsigned state)
202 {
203         unsigned sr;
204         ulong elapsed;
205         bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
206         int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
207         ulong base = i2c_bus->base;
208         ulong start_time = get_timer(0);
209         for (;;) {
210                 sr = readb(base + (I2SR << reg_shift));
211                 if (sr & I2SR_IAL) {
212                         if (quirk)
213                                 writeb(sr | I2SR_IAL, base +
214                                        (I2SR << reg_shift));
215                         else
216                                 writeb(sr & ~I2SR_IAL, base +
217                                        (I2SR << reg_shift));
218                         printf("%s: Arbitration lost sr=%x cr=%x state=%x\n",
219                                 __func__, sr, readb(base + (I2CR << reg_shift)),
220                                 state);
221                         return -ERESTART;
222                 }
223                 if ((sr & (state >> 8)) == (unsigned char)state)
224                         return sr;
225                 WATCHDOG_RESET();
226                 elapsed = get_timer(start_time);
227                 if (elapsed > (CONFIG_SYS_HZ / 10))     /* .1 seconds */
228                         break;
229         }
230         printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
231                sr, readb(base + (I2CR << reg_shift)), state);
232         return -ETIMEDOUT;
233 }
234
235 static int tx_byte(struct mxc_i2c_bus *i2c_bus, u8 byte)
236 {
237         int ret;
238         int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
239                         VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
240         ulong base = i2c_bus->base;
241
242         writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
243         writeb(byte, base + (I2DR << reg_shift));
244
245         ret = wait_for_sr_state(i2c_bus, ST_IIF);
246         if (ret < 0)
247                 return ret;
248         if (ret & I2SR_RX_NO_AK)
249                 return -EREMOTEIO;
250         return 0;
251 }
252
253 /*
254  * Stub implementations for outer i2c slave operations.
255  */
256 void __i2c_force_reset_slave(void)
257 {
258 }
259 void i2c_force_reset_slave(void)
260         __attribute__((weak, alias("__i2c_force_reset_slave")));
261
262 /*
263  * Stop I2C transaction
264  */
265 static void i2c_imx_stop(struct mxc_i2c_bus *i2c_bus)
266 {
267         int ret;
268         int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
269                         VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
270         ulong base = i2c_bus->base;
271         unsigned int temp = readb(base + (I2CR << reg_shift));
272
273         temp &= ~(I2CR_MSTA | I2CR_MTX);
274         writeb(temp, base + (I2CR << reg_shift));
275         ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
276         if (ret < 0)
277                 printf("%s:trigger stop failed\n", __func__);
278 }
279
280 /*
281  * Send start signal, chip address and
282  * write register address
283  */
284 static int i2c_init_transfer_(struct mxc_i2c_bus *i2c_bus, u8 chip,
285                               u32 addr, int alen)
286 {
287         unsigned int temp;
288         int ret;
289         bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
290         ulong base = i2c_bus->base;
291         int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
292
293         /* Reset i2c slave */
294         i2c_force_reset_slave();
295
296         /* Enable I2C controller */
297         if (quirk)
298                 ret = readb(base + (I2CR << reg_shift)) & I2CR_IDIS;
299         else
300                 ret = !(readb(base + (I2CR << reg_shift)) & I2CR_IEN);
301
302         if (ret) {
303                 writeb(I2CR_IEN, base + (I2CR << reg_shift));
304                 /* Wait for controller to be stable */
305                 udelay(50);
306         }
307
308         if (readb(base + (IADR << reg_shift)) == (chip << 1))
309                 writeb((chip << 1) ^ 2, base + (IADR << reg_shift));
310         writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
311         ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
312         if (ret < 0)
313                 return ret;
314
315         /* Start I2C transaction */
316         temp = readb(base + (I2CR << reg_shift));
317         temp |= I2CR_MSTA;
318         writeb(temp, base + (I2CR << reg_shift));
319
320         ret = wait_for_sr_state(i2c_bus, ST_BUS_BUSY);
321         if (ret < 0)
322                 return ret;
323
324         temp |= I2CR_MTX | I2CR_TX_NO_AK;
325         writeb(temp, base + (I2CR << reg_shift));
326
327         if (alen >= 0)  {
328                 /* write slave address */
329                 ret = tx_byte(i2c_bus, chip << 1);
330                 if (ret < 0)
331                         return ret;
332
333                 while (alen--) {
334                         ret = tx_byte(i2c_bus, (addr >> (alen * 8)) & 0xff);
335                         if (ret < 0)
336                                 return ret;
337                 }
338         }
339
340         return 0;
341 }
342
343 #ifndef CONFIG_DM_I2C
344 int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
345 {
346         if (i2c_bus && i2c_bus->idle_bus_fn)
347                 return i2c_bus->idle_bus_fn(i2c_bus->idle_bus_data);
348         return 0;
349 }
350 #else
351 /*
352  * See Linux Documentation/devicetree/bindings/i2c/i2c-imx.txt
353  * "
354  *  scl-gpios: specify the gpio related to SCL pin
355  *  sda-gpios: specify the gpio related to SDA pin
356  *  add pinctrl to configure i2c pins to gpio function for i2c
357  *  bus recovery, call it "gpio" state
358  * "
359  *
360  * The i2c_idle_bus is an implementation following Linux Kernel.
361  */
362 int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
363 {
364         struct udevice *bus = i2c_bus->bus;
365         struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
366         struct gpio_desc *scl_gpio = &i2c_bus->scl_gpio;
367         struct gpio_desc *sda_gpio = &i2c_bus->sda_gpio;
368         int sda, scl, idle_sclks;
369         int i, ret = 0;
370         ulong elapsed, start_time;
371
372         if (pinctrl_select_state(bus, "gpio")) {
373                 dev_dbg(bus, "Can not to switch to use gpio pinmux\n");
374                 /*
375                  * GPIO pinctrl for i2c force idle is not a must,
376                  * but it is strongly recommended to be used.
377                  * Because it can help you to recover from bad
378                  * i2c bus state. Do not return failure, because
379                  * it is not a must.
380                  */
381                 return 0;
382         }
383
384         dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
385         dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
386         scl = dm_gpio_get_value(scl_gpio);
387         sda = dm_gpio_get_value(sda_gpio);
388
389         if ((sda & scl) == 1)
390                 goto exit;              /* Bus is idle already */
391
392         /*
393          * In most cases it is just enough to generate 8 + 1 SCLK
394          * clocks to recover I2C slave device from 'stuck' state
395          * (when for example SW reset was performed, in the middle of
396          * I2C transmission).
397          *
398          * However, there are devices which send data in packets of
399          * N bytes (N > 1). In such case we do need N * 8 + 1 SCLK
400          * clocks.
401          */
402         idle_sclks = 8 + 1;
403
404         if (i2c->max_transaction_bytes > 0)
405                 idle_sclks = i2c->max_transaction_bytes * 8 + 1;
406         /* Send high and low on the SCL line */
407         for (i = 0; i < idle_sclks; i++) {
408                 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_OUT);
409                 dm_gpio_set_value(scl_gpio, 0);
410                 udelay(50);
411                 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
412                 udelay(50);
413         }
414         start_time = get_timer(0);
415         for (;;) {
416                 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
417                 dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
418                 scl = dm_gpio_get_value(scl_gpio);
419                 sda = dm_gpio_get_value(sda_gpio);
420                 if ((sda & scl) == 1)
421                         break;
422                 WATCHDOG_RESET();
423                 elapsed = get_timer(start_time);
424                 if (elapsed > (CONFIG_SYS_HZ / 5)) {    /* .2 seconds */
425                         ret = -EBUSY;
426                         printf("%s: failed to clear bus, sda=%d scl=%d\n", __func__, sda, scl);
427                         break;
428                 }
429         }
430
431 exit:
432         pinctrl_select_state(bus, "default");
433         return ret;
434 }
435 #endif
436
437 static int i2c_init_transfer(struct mxc_i2c_bus *i2c_bus, u8 chip,
438                              u32 addr, int alen)
439 {
440         int retry;
441         int ret;
442         int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
443                         VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
444
445         if (!i2c_bus->base)
446                 return -EINVAL;
447
448         for (retry = 0; retry < 3; retry++) {
449                 ret = i2c_init_transfer_(i2c_bus, chip, addr, alen);
450                 if (ret >= 0)
451                         return 0;
452                 i2c_imx_stop(i2c_bus);
453                 if (ret == -EREMOTEIO)
454                         return ret;
455
456                 printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip,
457                                 retry);
458                 if (ret != -ERESTART)
459                         /* Disable controller */
460                         writeb(I2CR_IDIS, i2c_bus->base + (I2CR << reg_shift));
461                 udelay(100);
462                 if (i2c_idle_bus(i2c_bus) < 0)
463                         break;
464         }
465         printf("%s: give up i2c_regs=0x%lx\n", __func__, i2c_bus->base);
466         return ret;
467 }
468
469
470 static int i2c_write_data(struct mxc_i2c_bus *i2c_bus, u8 chip, const u8 *buf,
471                           int len)
472 {
473         int i, ret = 0;
474
475         debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
476         debug("write_data: ");
477         /* use rc for counter */
478         for (i = 0; i < len; ++i)
479                 debug(" 0x%02x", buf[i]);
480         debug("\n");
481
482         for (i = 0; i < len; i++) {
483                 ret = tx_byte(i2c_bus, buf[i]);
484                 if (ret < 0) {
485                         debug("i2c_write_data(): rc=%d\n", ret);
486                         break;
487                 }
488         }
489
490         return ret;
491 }
492
493 /* Will generate a STOP after the last byte if "last" is true, i.e. this is the
494  * final message of a transaction.  If not, it switches the bus back to TX mode
495  * and does not send a STOP, leaving the bus in a state where a repeated start
496  * and address can be sent for another message.
497  */
498 static int i2c_read_data(struct mxc_i2c_bus *i2c_bus, uchar chip, uchar *buf,
499                          int len, bool last)
500 {
501         int ret;
502         unsigned int temp;
503         int i;
504         int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
505                         VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
506         ulong base = i2c_bus->base;
507
508         debug("i2c_read_data: chip=0x%x, len=0x%x\n", chip, len);
509
510         /* setup bus to read data */
511         temp = readb(base + (I2CR << reg_shift));
512         temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
513         if (len == 1)
514                 temp |= I2CR_TX_NO_AK;
515         writeb(temp, base + (I2CR << reg_shift));
516         writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
517         /* dummy read to clear ICF */
518         readb(base + (I2DR << reg_shift));
519
520         /* read data */
521         for (i = 0; i < len; i++) {
522                 ret = wait_for_sr_state(i2c_bus, ST_IIF);
523                 if (ret < 0) {
524                         debug("i2c_read_data(): ret=%d\n", ret);
525                         i2c_imx_stop(i2c_bus);
526                         return ret;
527                 }
528
529                 if (i == (len - 1)) {
530                         /* Final byte has already been received by master!  When
531                          * we read it from I2DR, the master will start another
532                          * cycle.  We must program it first to send a STOP or
533                          * switch to TX to avoid this.
534                          */
535                         if (last) {
536                                 i2c_imx_stop(i2c_bus);
537                         } else {
538                                 /* Final read, no stop, switch back to tx */
539                                 temp = readb(base + (I2CR << reg_shift));
540                                 temp |= I2CR_MTX | I2CR_TX_NO_AK;
541                                 writeb(temp, base + (I2CR << reg_shift));
542                         }
543                 } else if (i == (len - 2)) {
544                         /* Master has already recevied penultimate byte.  When
545                          * we read it from I2DR, master will start RX of final
546                          * byte.  We must set TX_NO_AK now so it does not ACK
547                          * that final byte.
548                          */
549                         temp = readb(base + (I2CR << reg_shift));
550                         temp |= I2CR_TX_NO_AK;
551                         writeb(temp, base + (I2CR << reg_shift));
552                 }
553
554                 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
555                 buf[i] = readb(base + (I2DR << reg_shift));
556         }
557
558         /* reuse ret for counter*/
559         for (ret = 0; ret < len; ++ret)
560                 debug(" 0x%02x", buf[ret]);
561         debug("\n");
562
563         /* It is not clear to me that this is necessary */
564         if (last)
565                 i2c_imx_stop(i2c_bus);
566         return 0;
567 }
568
569 int __enable_i2c_clk(unsigned char enable, unsigned int i2c_num)
570 {
571         return 1;
572 }
573
574 int enable_i2c_clk(unsigned char enable, unsigned int i2c_num)
575         __attribute__((weak, alias("__enable_i2c_clk")));
576
577 #ifndef CONFIG_DM_I2C
578 /*
579  * Read data from I2C device
580  *
581  * The transactions use the syntax defined in the Linux kernel I2C docs.
582  *
583  * If alen is > 0, then this function will send a transaction of the form:
584  *     S Chip Wr [A] Addr [A] S Chip Rd [A] [data] A ... NA P
585  * This is a normal I2C register read: writing the register address, then doing
586  * a repeated start and reading the data.
587  *
588  * If alen == 0, then we get this transaction:
589  *     S Chip Wr [A] S Chip Rd [A] [data] A ... NA P
590  * This is somewhat unusual, though valid, transaction.  It addresses the chip
591  * in write mode, but doesn't actually write any register address or data, then
592  * does a repeated start and reads data.
593  *
594  * If alen < 0, then we get this transaction:
595  *     S Chip Rd [A] [data] A ... NA P
596  * The chip is addressed in read mode and then data is read.  No register
597  * address is written first.  This is perfectly valid on most devices and
598  * required on some (usually those that don't act like an array of registers).
599  */
600 static int bus_i2c_read(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
601                         int alen, u8 *buf, int len)
602 {
603         int ret = 0;
604         u32 temp;
605         int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
606                 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
607         ulong base = i2c_bus->base;
608
609         ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
610         if (ret < 0)
611                 return ret;
612
613         if (alen >= 0) {
614                 temp = readb(base + (I2CR << reg_shift));
615                 temp |= I2CR_RSTA;
616                 writeb(temp, base + (I2CR << reg_shift));
617         }
618
619         ret = tx_byte(i2c_bus, (chip << 1) | 1);
620         if (ret < 0) {
621                 i2c_imx_stop(i2c_bus);
622                 return ret;
623         }
624
625         ret = i2c_read_data(i2c_bus, chip, buf, len, true);
626
627         i2c_imx_stop(i2c_bus);
628         return ret;
629 }
630
631 /*
632  * Write data to I2C device
633  *
634  * If alen > 0, we get this transaction:
635  *    S Chip Wr [A] addr [A] data [A] ... [A] P
636  * An ordinary write register command.
637  *
638  * If alen == 0, then we get this:
639  *    S Chip Wr [A] data [A] ... [A] P
640  * This is a simple I2C write.
641  *
642  * If alen < 0, then we get this:
643  *    S data [A] ... [A] P
644  * This is most likely NOT something that should be used.  It doesn't send the
645  * chip address first, so in effect, the first byte of data will be used as the
646  * address.
647  */
648 static int bus_i2c_write(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
649                          int alen, const u8 *buf, int len)
650 {
651         int ret = 0;
652
653         ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
654         if (ret < 0)
655                 return ret;
656
657         ret = i2c_write_data(i2c_bus, chip, buf, len);
658
659         i2c_imx_stop(i2c_bus);
660
661         return ret;
662 }
663
664 #if !defined(I2C2_BASE_ADDR)
665 #define I2C2_BASE_ADDR  0
666 #endif
667
668 #if !defined(I2C3_BASE_ADDR)
669 #define I2C3_BASE_ADDR  0
670 #endif
671
672 #if !defined(I2C4_BASE_ADDR)
673 #define I2C4_BASE_ADDR  0
674 #endif
675
676 #if !defined(I2C5_BASE_ADDR)
677 #define I2C5_BASE_ADDR 0
678 #endif
679
680 #if !defined(I2C6_BASE_ADDR)
681 #define I2C6_BASE_ADDR 0
682 #endif
683
684 #if !defined(I2C7_BASE_ADDR)
685 #define I2C7_BASE_ADDR 0
686 #endif
687
688 #if !defined(I2C8_BASE_ADDR)
689 #define I2C8_BASE_ADDR 0
690 #endif
691
692 static struct mxc_i2c_bus mxc_i2c_buses[] = {
693 #if defined(CONFIG_ARCH_LS1021A) || defined(CONFIG_VF610) || \
694         defined(CONFIG_FSL_LAYERSCAPE)
695         { 0, I2C1_BASE_ADDR, I2C_QUIRK_FLAG },
696         { 1, I2C2_BASE_ADDR, I2C_QUIRK_FLAG },
697         { 2, I2C3_BASE_ADDR, I2C_QUIRK_FLAG },
698         { 3, I2C4_BASE_ADDR, I2C_QUIRK_FLAG },
699         { 4, I2C5_BASE_ADDR, I2C_QUIRK_FLAG },
700         { 5, I2C6_BASE_ADDR, I2C_QUIRK_FLAG },
701         { 6, I2C7_BASE_ADDR, I2C_QUIRK_FLAG },
702         { 7, I2C8_BASE_ADDR, I2C_QUIRK_FLAG },
703 #else
704         { 0, I2C1_BASE_ADDR, 0 },
705         { 1, I2C2_BASE_ADDR, 0 },
706         { 2, I2C3_BASE_ADDR, 0 },
707         { 3, I2C4_BASE_ADDR, 0 },
708         { 4, I2C5_BASE_ADDR, 0 },
709         { 5, I2C6_BASE_ADDR, 0 },
710         { 6, I2C7_BASE_ADDR, 0 },
711         { 7, I2C8_BASE_ADDR, 0 },
712 #endif
713 };
714
715 struct mxc_i2c_bus *i2c_get_base(struct i2c_adapter *adap)
716 {
717         return &mxc_i2c_buses[adap->hwadapnr];
718 }
719
720 static int mxc_i2c_read(struct i2c_adapter *adap, uint8_t chip,
721                                 uint addr, int alen, uint8_t *buffer,
722                                 int len)
723 {
724         return bus_i2c_read(i2c_get_base(adap), chip, addr, alen, buffer, len);
725 }
726
727 static int mxc_i2c_write(struct i2c_adapter *adap, uint8_t chip,
728                                 uint addr, int alen, uint8_t *buffer,
729                                 int len)
730 {
731         return bus_i2c_write(i2c_get_base(adap), chip, addr, alen, buffer, len);
732 }
733
734 /*
735  * Test if a chip at a given address responds (probe the chip)
736  */
737 static int mxc_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
738 {
739         return bus_i2c_write(i2c_get_base(adap), chip, 0, 0, NULL, 0);
740 }
741
742 void bus_i2c_init(int index, int speed, int unused,
743                   int (*idle_bus_fn)(void *p), void *idle_bus_data)
744 {
745         int ret;
746
747         if (index >= ARRAY_SIZE(mxc_i2c_buses)) {
748                 debug("Error i2c index\n");
749                 return;
750         }
751
752         if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
753                 if (i2c_fused((ulong)mxc_i2c_buses[index].base)) {
754                         printf("SoC fuse indicates I2C@0x%lx is unavailable.\n",
755                                (ulong)mxc_i2c_buses[index].base);
756                         return;
757                 }
758         }
759
760         /*
761          * Warning: Be careful to allow the assignment to a static
762          * variable here. This function could be called while U-Boot is
763          * still running in flash memory. So such assignment is equal
764          * to write data to flash without erasing.
765          */
766         if (idle_bus_fn)
767                 mxc_i2c_buses[index].idle_bus_fn = idle_bus_fn;
768         if (idle_bus_data)
769                 mxc_i2c_buses[index].idle_bus_data = idle_bus_data;
770
771         ret = enable_i2c_clk(1, index);
772         if (ret < 0) {
773                 debug("I2C-%d clk fail to enable.\n", index);
774                 return;
775         }
776
777         bus_i2c_set_bus_speed(&mxc_i2c_buses[index], speed);
778 }
779
780 /*
781  * Early init I2C for prepare read the clk through I2C.
782  */
783 void i2c_early_init_f(void)
784 {
785         ulong base = mxc_i2c_buses[I2C_EARLY_INIT_INDEX].base;
786         bool quirk = mxc_i2c_buses[I2C_EARLY_INIT_INDEX].driver_data
787                                         & I2C_QUIRK_FLAG ? true : false;
788         int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
789
790         /* Set I2C divider value */
791         writeb(I2C_IFDR_DIV_CONSERVATIVE, base + (IFDR << reg_shift));
792         /* Reset module */
793         writeb(I2CR_IDIS, base + (I2CR << reg_shift));
794         writeb(0, base + (I2SR << reg_shift));
795         /* Enable I2C */
796         writeb(I2CR_IEN, base + (I2CR << reg_shift));
797 }
798
799 /*
800  * Init I2C Bus
801  */
802 static void mxc_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
803 {
804         bus_i2c_init(adap->hwadapnr, speed, slaveaddr, NULL, NULL);
805 }
806
807 /*
808  * Set I2C Speed
809  */
810 static u32 mxc_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
811 {
812         return bus_i2c_set_bus_speed(i2c_get_base(adap), speed);
813 }
814
815 /*
816  * Register mxc i2c adapters
817  */
818 #ifdef CONFIG_SYS_I2C_MXC_I2C1
819 U_BOOT_I2C_ADAP_COMPLETE(mxc0, mxc_i2c_init, mxc_i2c_probe,
820                          mxc_i2c_read, mxc_i2c_write,
821                          mxc_i2c_set_bus_speed,
822                          CONFIG_SYS_MXC_I2C1_SPEED,
823                          CONFIG_SYS_MXC_I2C1_SLAVE, 0)
824 #endif
825
826 #ifdef CONFIG_SYS_I2C_MXC_I2C2
827 U_BOOT_I2C_ADAP_COMPLETE(mxc1, mxc_i2c_init, mxc_i2c_probe,
828                          mxc_i2c_read, mxc_i2c_write,
829                          mxc_i2c_set_bus_speed,
830                          CONFIG_SYS_MXC_I2C2_SPEED,
831                          CONFIG_SYS_MXC_I2C2_SLAVE, 1)
832 #endif
833
834 #ifdef CONFIG_SYS_I2C_MXC_I2C3
835 U_BOOT_I2C_ADAP_COMPLETE(mxc2, mxc_i2c_init, mxc_i2c_probe,
836                          mxc_i2c_read, mxc_i2c_write,
837                          mxc_i2c_set_bus_speed,
838                          CONFIG_SYS_MXC_I2C3_SPEED,
839                          CONFIG_SYS_MXC_I2C3_SLAVE, 2)
840 #endif
841
842 #ifdef CONFIG_SYS_I2C_MXC_I2C4
843 U_BOOT_I2C_ADAP_COMPLETE(mxc3, mxc_i2c_init, mxc_i2c_probe,
844                          mxc_i2c_read, mxc_i2c_write,
845                          mxc_i2c_set_bus_speed,
846                          CONFIG_SYS_MXC_I2C4_SPEED,
847                          CONFIG_SYS_MXC_I2C4_SLAVE, 3)
848 #endif
849
850 #ifdef CONFIG_SYS_I2C_MXC_I2C5
851 U_BOOT_I2C_ADAP_COMPLETE(mxc4, mxc_i2c_init, mxc_i2c_probe,
852                          mxc_i2c_read, mxc_i2c_write,
853                          mxc_i2c_set_bus_speed,
854                          CONFIG_SYS_MXC_I2C5_SPEED,
855                          CONFIG_SYS_MXC_I2C5_SLAVE, 4)
856 #endif
857
858 #ifdef CONFIG_SYS_I2C_MXC_I2C6
859 U_BOOT_I2C_ADAP_COMPLETE(mxc5, mxc_i2c_init, mxc_i2c_probe,
860                          mxc_i2c_read, mxc_i2c_write,
861                          mxc_i2c_set_bus_speed,
862                          CONFIG_SYS_MXC_I2C6_SPEED,
863                          CONFIG_SYS_MXC_I2C6_SLAVE, 5)
864 #endif
865
866 #ifdef CONFIG_SYS_I2C_MXC_I2C7
867 U_BOOT_I2C_ADAP_COMPLETE(mxc6, mxc_i2c_init, mxc_i2c_probe,
868                          mxc_i2c_read, mxc_i2c_write,
869                          mxc_i2c_set_bus_speed,
870                          CONFIG_SYS_MXC_I2C7_SPEED,
871                          CONFIG_SYS_MXC_I2C7_SLAVE, 6)
872 #endif
873
874 #ifdef CONFIG_SYS_I2C_MXC_I2C8
875 U_BOOT_I2C_ADAP_COMPLETE(mxc7, mxc_i2c_init, mxc_i2c_probe,
876                          mxc_i2c_read, mxc_i2c_write,
877                          mxc_i2c_set_bus_speed,
878                          CONFIG_SYS_MXC_I2C8_SPEED,
879                          CONFIG_SYS_MXC_I2C8_SLAVE, 7)
880 #endif
881
882 #else
883
884 static int mxc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
885 {
886         struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
887
888         return bus_i2c_set_bus_speed(i2c_bus, speed);
889 }
890
891 static int mxc_i2c_probe(struct udevice *bus)
892 {
893         struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
894         const void *fdt = gd->fdt_blob;
895         int node = dev_of_offset(bus);
896         fdt_addr_t addr;
897         int ret, ret2;
898
899         i2c_bus->driver_data = dev_get_driver_data(bus);
900
901         addr = devfdt_get_addr(bus);
902         if (addr == FDT_ADDR_T_NONE)
903                 return -EINVAL;
904
905         if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
906                 if (i2c_fused((ulong)addr)) {
907                         printf("SoC fuse indicates I2C@0x%lx is unavailable.\n",
908                                (ulong)addr);
909                         return -ENODEV;
910                 }
911         }
912
913         i2c_bus->base = addr;
914         i2c_bus->index = bus->seq;
915         i2c_bus->bus = bus;
916
917         /* Enable clk */
918 #if CONFIG_IS_ENABLED(CLK)
919         ret = clk_get_by_index(bus, 0, &i2c_bus->per_clk);
920         if (ret) {
921                 printf("Failed to get i2c clk\n");
922                 return ret;
923         }
924         ret = clk_enable(&i2c_bus->per_clk);
925         if (ret) {
926                 printf("Failed to enable i2c clk\n");
927                 return ret;
928         }
929 #else
930         ret = enable_i2c_clk(1, bus->seq);
931         if (ret < 0)
932                 return ret;
933 #endif
934
935         /*
936          * See Documentation/devicetree/bindings/i2c/i2c-imx.txt
937          * Use gpio to force bus idle when necessary.
938          */
939         ret = fdt_stringlist_search(fdt, node, "pinctrl-names", "gpio");
940         if (ret < 0) {
941                 debug("i2c bus %d at 0x%2lx, no gpio pinctrl state.\n", bus->seq, i2c_bus->base);
942         } else {
943                 ret = gpio_request_by_name_nodev(offset_to_ofnode(node),
944                                 "scl-gpios", 0, &i2c_bus->scl_gpio,
945                                 GPIOD_IS_OUT);
946                 ret2 = gpio_request_by_name_nodev(offset_to_ofnode(node),
947                                 "sda-gpios", 0, &i2c_bus->sda_gpio,
948                                 GPIOD_IS_OUT);
949                 if (!dm_gpio_is_valid(&i2c_bus->sda_gpio) ||
950                     !dm_gpio_is_valid(&i2c_bus->scl_gpio) ||
951                     ret || ret2) {
952                         dev_err(dev, "i2c bus %d at %lu, fail to request scl/sda gpio\n", bus->seq, i2c_bus->base);
953                         return -EINVAL;
954                 }
955         }
956
957         /*
958          * Pinmux settings are in board file now, until pinmux is supported,
959          * we can set pinmux here in probe function.
960          */
961
962         debug("i2c : controller bus %d at %lu , speed %d: ",
963               bus->seq, i2c_bus->base,
964               i2c_bus->speed);
965
966         return 0;
967 }
968
969 /* Sends: S Addr Wr [A|NA] P */
970 static int mxc_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
971                               u32 chip_flags)
972 {
973         int ret;
974         struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
975
976         ret = i2c_init_transfer(i2c_bus, chip_addr, 0, 0);
977         if (ret < 0) {
978                 debug("%s failed, ret = %d\n", __func__, ret);
979                 return ret;
980         }
981
982         i2c_imx_stop(i2c_bus);
983
984         return 0;
985 }
986
987 static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
988 {
989         struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
990         int ret = 0;
991         ulong base = i2c_bus->base;
992         int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
993                 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
994         int read_mode;
995
996         /* Here address len is set to -1 to not send any address at first.
997          * Otherwise i2c_init_transfer will send the chip address with write
998          * mode set.  This is wrong if the 1st message is read.
999          */
1000         ret = i2c_init_transfer(i2c_bus, msg->addr, 0, -1);
1001         if (ret < 0) {
1002                 debug("i2c_init_transfer error: %d\n", ret);
1003                 return ret;
1004         }
1005
1006         read_mode = -1; /* So it's always different on the first message */
1007         for (; nmsgs > 0; nmsgs--, msg++) {
1008                 const int msg_is_read = !!(msg->flags & I2C_M_RD);
1009
1010                 debug("i2c_xfer: chip=0x%x, len=0x%x, dir=%c\n", msg->addr,
1011                       msg->len, msg_is_read ? 'R' : 'W');
1012
1013                 if (msg_is_read != read_mode) {
1014                         /* Send repeated start if not 1st message */
1015                         if (read_mode != -1) {
1016                                 debug("i2c_xfer: [RSTART]\n");
1017                                 ret = readb(base + (I2CR << reg_shift));
1018                                 ret |= I2CR_RSTA;
1019                                 writeb(ret, base + (I2CR << reg_shift));
1020                         }
1021                         debug("i2c_xfer: [ADDR %02x | %c]\n", msg->addr,
1022                               msg_is_read ? 'R' : 'W');
1023                         ret = tx_byte(i2c_bus, (msg->addr << 1) | msg_is_read);
1024                         if (ret < 0) {
1025                                 debug("i2c_xfer: [STOP]\n");
1026                                 i2c_imx_stop(i2c_bus);
1027                                 break;
1028                         }
1029                         read_mode = msg_is_read;
1030                 }
1031
1032                 if (msg->flags & I2C_M_RD)
1033                         ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
1034                                             msg->len, nmsgs == 1 ||
1035                                                       (msg->flags & I2C_M_STOP));
1036                 else
1037                         ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
1038                                              msg->len);
1039
1040                 if (ret < 0)
1041                         break;
1042         }
1043
1044         if (ret)
1045                 debug("i2c_write: error sending\n");
1046
1047         i2c_imx_stop(i2c_bus);
1048
1049         return ret;
1050 }
1051
1052 static const struct dm_i2c_ops mxc_i2c_ops = {
1053         .xfer           = mxc_i2c_xfer,
1054         .probe_chip     = mxc_i2c_probe_chip,
1055         .set_bus_speed  = mxc_i2c_set_bus_speed,
1056 };
1057
1058 static const struct udevice_id mxc_i2c_ids[] = {
1059         { .compatible = "fsl,imx21-i2c", },
1060         { .compatible = "fsl,vf610-i2c", .data = I2C_QUIRK_FLAG, },
1061         {}
1062 };
1063
1064 U_BOOT_DRIVER(i2c_mxc) = {
1065         .name = "i2c_mxc",
1066         .id = UCLASS_I2C,
1067         .of_match = mxc_i2c_ids,
1068         .probe = mxc_i2c_probe,
1069         .priv_auto_alloc_size = sizeof(struct mxc_i2c_bus),
1070         .ops = &mxc_i2c_ops,
1071         .flags = DM_FLAG_PRE_RELOC,
1072 };
1073 #endif