1 // SPDX-License-Identifier: GPL-2.0+
3 * TI DaVinci (TMS320DM644x) I2C driver.
5 * (C) Copyright 2012-2014
6 * Texas Instruments Incorporated, <www.ti.com>
7 * (C) Copyright 2007 Sergey Kubushyn <ksi@koi8.net>
8 * --------------------------------------------------------
10 * NOTE: This driver should be converted to driver model before June 2017.
11 * Please see doc/driver-model/i2c-howto.rst for instructions.
18 #include <asm/arch/hardware.h>
19 #include <asm/arch/i2c_defs.h>
21 #include <linux/delay.h>
22 #include "davinci_i2c.h"
24 #if CONFIG_IS_ENABLED(DM_I2C)
25 /* Information about i2c controller */
29 struct i2c_regs *regs;
33 #define CHECK_NACK() \
35 if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {\
36 REG(&(i2c_base->i2c_con)) = 0;\
41 static int _wait_for_bus(struct i2c_regs *i2c_base)
45 REG(&(i2c_base->i2c_stat)) = 0xffff;
47 for (timeout = 0; timeout < 10; timeout++) {
48 stat = REG(&(i2c_base->i2c_stat));
49 if (!((stat) & I2C_STAT_BB)) {
50 REG(&(i2c_base->i2c_stat)) = 0xffff;
54 REG(&(i2c_base->i2c_stat)) = stat;
58 REG(&(i2c_base->i2c_stat)) = 0xffff;
62 static int _poll_i2c_irq(struct i2c_regs *i2c_base, int mask)
66 for (timeout = 0; timeout < 10; timeout++) {
68 stat = REG(&(i2c_base->i2c_stat));
73 REG(&(i2c_base->i2c_stat)) = 0xffff;
74 return stat | I2C_TIMEOUT;
77 static void _flush_rx(struct i2c_regs *i2c_base)
80 if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_RRDY))
83 REG(&(i2c_base->i2c_drr));
84 REG(&(i2c_base->i2c_stat)) = I2C_STAT_RRDY;
89 static uint _davinci_i2c_setspeed(struct i2c_regs *i2c_base,
96 div = (CONFIG_SYS_HZ_CLOCK / ((psc + 1) * speed)) - 10;
97 REG(&(i2c_base->i2c_psc)) = psc; /* 27MHz / (2 + 1) = 9MHz */
98 REG(&(i2c_base->i2c_scll)) = (div * 50) / 100; /* 50% Duty */
99 REG(&(i2c_base->i2c_sclh)) = div - REG(&(i2c_base->i2c_scll));
104 static void _davinci_i2c_init(struct i2c_regs *i2c_base,
105 uint speed, int slaveadd)
107 if (REG(&(i2c_base->i2c_con)) & I2C_CON_EN) {
108 REG(&(i2c_base->i2c_con)) = 0;
112 _davinci_i2c_setspeed(i2c_base, speed);
114 REG(&(i2c_base->i2c_oa)) = slaveadd;
115 REG(&(i2c_base->i2c_cnt)) = 0;
117 /* Interrupts must be enabled or I2C module won't work */
118 REG(&(i2c_base->i2c_ie)) = I2C_IE_SCD_IE | I2C_IE_XRDY_IE |
119 I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | I2C_IE_NACK_IE;
121 /* Now enable I2C controller (get it out of reset) */
122 REG(&(i2c_base->i2c_con)) = I2C_CON_EN;
127 static int _davinci_i2c_read(struct i2c_regs *i2c_base, uint8_t chip,
128 uint32_t addr, int alen, uint8_t *buf, int len)
133 if ((alen < 0) || (alen > 2)) {
134 printf("%s(): bogus address length %x\n", __func__, alen);
138 if (_wait_for_bus(i2c_base))
142 /* Start address phase */
143 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX;
144 REG(&(i2c_base->i2c_cnt)) = alen;
145 REG(&(i2c_base->i2c_sa)) = chip;
146 REG(&(i2c_base->i2c_con)) = tmp;
148 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
154 /* Send address MSByte */
155 if (tmp & I2C_STAT_XRDY) {
156 REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
158 REG(&(i2c_base->i2c_con)) = 0;
162 tmp = _poll_i2c_irq(i2c_base,
163 I2C_STAT_XRDY | I2C_STAT_NACK);
166 /* No break, fall through */
168 /* Send address LSByte */
169 if (tmp & I2C_STAT_XRDY) {
170 REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
172 REG(&(i2c_base->i2c_con)) = 0;
176 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY |
177 I2C_STAT_NACK | I2C_STAT_ARDY);
181 if (!(tmp & I2C_STAT_ARDY)) {
182 REG(&(i2c_base->i2c_con)) = 0;
188 /* Address phase is over, now read 'len' bytes and stop */
189 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
190 REG(&(i2c_base->i2c_cnt)) = len & 0xffff;
191 REG(&(i2c_base->i2c_sa)) = chip;
192 REG(&(i2c_base->i2c_con)) = tmp;
194 for (i = 0; i < len; i++) {
195 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_RRDY | I2C_STAT_NACK |
200 if (tmp & I2C_STAT_RRDY) {
201 buf[i] = REG(&(i2c_base->i2c_drr));
203 REG(&(i2c_base->i2c_con)) = 0;
208 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_SCD | I2C_STAT_NACK);
212 if (!(tmp & I2C_STAT_SCD)) {
213 REG(&(i2c_base->i2c_con)) = 0;
218 REG(&(i2c_base->i2c_stat)) = 0xffff;
219 REG(&(i2c_base->i2c_cnt)) = 0;
220 REG(&(i2c_base->i2c_con)) = 0;
225 static int _davinci_i2c_write(struct i2c_regs *i2c_base, uint8_t chip,
226 uint32_t addr, int alen, uint8_t *buf, int len)
231 if ((alen < 0) || (alen > 2)) {
232 printf("%s(): bogus address length %x\n", __func__, alen);
236 printf("%s(): bogus length %x\n", __func__, len);
240 if (_wait_for_bus(i2c_base))
243 /* Start address phase */
244 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
245 I2C_CON_TRX | I2C_CON_STP;
246 REG(&(i2c_base->i2c_cnt)) = (alen == 0) ?
247 len & 0xffff : (len & 0xffff) + alen;
248 REG(&(i2c_base->i2c_sa)) = chip;
249 REG(&(i2c_base->i2c_con)) = tmp;
253 /* Send address MSByte */
254 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
258 if (tmp & I2C_STAT_XRDY) {
259 REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
261 REG(&(i2c_base->i2c_con)) = 0;
264 /* No break, fall through */
266 /* Send address LSByte */
267 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
271 if (tmp & I2C_STAT_XRDY) {
272 REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
274 REG(&(i2c_base->i2c_con)) = 0;
279 for (i = 0; i < len; i++) {
280 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
284 if (tmp & I2C_STAT_XRDY)
285 REG(&(i2c_base->i2c_dxr)) = buf[i];
290 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_SCD | I2C_STAT_NACK);
294 if (!(tmp & I2C_STAT_SCD)) {
295 REG(&(i2c_base->i2c_con)) = 0;
300 REG(&(i2c_base->i2c_stat)) = 0xffff;
301 REG(&(i2c_base->i2c_cnt)) = 0;
302 REG(&(i2c_base->i2c_con)) = 0;
307 static int _davinci_i2c_probe_chip(struct i2c_regs *i2c_base, uint8_t chip)
311 if (chip == REG(&(i2c_base->i2c_oa)))
314 REG(&(i2c_base->i2c_con)) = 0;
315 if (_wait_for_bus(i2c_base))
318 /* try to read one byte from current (or only) address */
319 REG(&(i2c_base->i2c_cnt)) = 1;
320 REG(&(i2c_base->i2c_sa)) = chip;
321 REG(&(i2c_base->i2c_con)) = (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
325 if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_NACK)) {
328 REG(&(i2c_base->i2c_stat)) = 0xffff;
330 REG(&(i2c_base->i2c_stat)) = 0xffff;
331 REG(&(i2c_base->i2c_con)) |= I2C_CON_STP;
333 if (_wait_for_bus(i2c_base))
338 REG(&(i2c_base->i2c_stat)) = 0xffff;
339 REG(&(i2c_base->i2c_cnt)) = 0;
343 #if !CONFIG_IS_ENABLED(DM_I2C)
344 static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap)
346 switch (adap->hwadapnr) {
347 #if CONFIG_SYS_I2C_BUS_MAX >= 3
349 return (struct i2c_regs *)I2C2_BASE;
351 #if CONFIG_SYS_I2C_BUS_MAX >= 2
353 return (struct i2c_regs *)I2C1_BASE;
356 return (struct i2c_regs *)I2C_BASE;
359 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
365 static uint davinci_i2c_setspeed(struct i2c_adapter *adap, uint speed)
367 struct i2c_regs *i2c_base = davinci_get_base(adap);
371 ret = _davinci_i2c_setspeed(i2c_base, speed);
376 static void davinci_i2c_init(struct i2c_adapter *adap, int speed,
379 struct i2c_regs *i2c_base = davinci_get_base(adap);
382 _davinci_i2c_init(i2c_base, speed, slaveadd);
387 static int davinci_i2c_read(struct i2c_adapter *adap, uint8_t chip,
388 uint32_t addr, int alen, uint8_t *buf, int len)
390 struct i2c_regs *i2c_base = davinci_get_base(adap);
391 return _davinci_i2c_read(i2c_base, chip, addr, alen, buf, len);
394 static int davinci_i2c_write(struct i2c_adapter *adap, uint8_t chip,
395 uint32_t addr, int alen, uint8_t *buf, int len)
397 struct i2c_regs *i2c_base = davinci_get_base(adap);
399 return _davinci_i2c_write(i2c_base, chip, addr, alen, buf, len);
402 static int davinci_i2c_probe_chip(struct i2c_adapter *adap, uint8_t chip)
404 struct i2c_regs *i2c_base = davinci_get_base(adap);
406 return _davinci_i2c_probe_chip(i2c_base, chip);
409 U_BOOT_I2C_ADAP_COMPLETE(davinci_0, davinci_i2c_init, davinci_i2c_probe_chip,
410 davinci_i2c_read, davinci_i2c_write,
411 davinci_i2c_setspeed,
412 CONFIG_SYS_DAVINCI_I2C_SPEED,
413 CONFIG_SYS_DAVINCI_I2C_SLAVE,
416 #if CONFIG_SYS_I2C_BUS_MAX >= 2
417 U_BOOT_I2C_ADAP_COMPLETE(davinci_1, davinci_i2c_init, davinci_i2c_probe_chip,
418 davinci_i2c_read, davinci_i2c_write,
419 davinci_i2c_setspeed,
420 CONFIG_SYS_DAVINCI_I2C_SPEED1,
421 CONFIG_SYS_DAVINCI_I2C_SLAVE1,
425 #if CONFIG_SYS_I2C_BUS_MAX >= 3
426 U_BOOT_I2C_ADAP_COMPLETE(davinci_2, davinci_i2c_init, davinci_i2c_probe_chip,
427 davinci_i2c_read, davinci_i2c_write,
428 davinci_i2c_setspeed,
429 CONFIG_SYS_DAVINCI_I2C_SPEED2,
430 CONFIG_SYS_DAVINCI_I2C_SLAVE2,
434 #else /* CONFIG_DM_I2C */
436 static int davinci_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
439 struct i2c_bus *i2c_bus = dev_get_priv(bus);
442 debug("i2c_xfer: %d messages\n", nmsgs);
443 for (; nmsgs > 0; nmsgs--, msg++) {
444 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
445 if (msg->flags & I2C_M_RD) {
446 ret = _davinci_i2c_read(i2c_bus->regs, msg->addr,
447 0, 0, msg->buf, msg->len);
449 ret = _davinci_i2c_write(i2c_bus->regs, msg->addr,
450 0, 0, msg->buf, msg->len);
453 debug("i2c_write: error sending\n");
461 static int davinci_i2c_set_speed(struct udevice *dev, uint speed)
463 struct i2c_bus *i2c_bus = dev_get_priv(dev);
465 i2c_bus->speed = speed;
466 return _davinci_i2c_setspeed(i2c_bus->regs, speed);
469 static int davinci_i2c_probe(struct udevice *dev)
471 struct i2c_bus *i2c_bus = dev_get_priv(dev);
473 i2c_bus->id = dev_seq(dev);
474 i2c_bus->regs = dev_read_addr_ptr(dev);
476 i2c_bus->speed = 100000;
477 _davinci_i2c_init(i2c_bus->regs, i2c_bus->speed, 0);
482 static int davinci_i2c_probe_chip(struct udevice *bus, uint chip_addr,
485 struct i2c_bus *i2c_bus = dev_get_priv(bus);
487 return _davinci_i2c_probe_chip(i2c_bus->regs, chip_addr);
490 static const struct dm_i2c_ops davinci_i2c_ops = {
491 .xfer = davinci_i2c_xfer,
492 .probe_chip = davinci_i2c_probe_chip,
493 .set_bus_speed = davinci_i2c_set_speed,
496 static const struct udevice_id davinci_i2c_ids[] = {
497 { .compatible = "ti,davinci-i2c"},
498 { .compatible = "ti,keystone-i2c"},
502 U_BOOT_DRIVER(i2c_davinci) = {
503 .name = "i2c_davinci",
505 .of_match = davinci_i2c_ids,
506 .probe = davinci_i2c_probe,
507 .priv_auto = sizeof(struct i2c_bus),
508 .ops = &davinci_i2c_ops,
511 #endif /* CONFIG_DM_I2C */