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.txt for instructions.
17 #include <asm/arch/hardware.h>
18 #include <asm/arch/i2c_defs.h>
20 #include "davinci_i2c.h"
23 /* Information about i2c controller */
27 struct i2c_regs *regs;
31 #define CHECK_NACK() \
33 if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {\
34 REG(&(i2c_base->i2c_con)) = 0;\
39 static int _wait_for_bus(struct i2c_regs *i2c_base)
43 REG(&(i2c_base->i2c_stat)) = 0xffff;
45 for (timeout = 0; timeout < 10; timeout++) {
46 stat = REG(&(i2c_base->i2c_stat));
47 if (!((stat) & I2C_STAT_BB)) {
48 REG(&(i2c_base->i2c_stat)) = 0xffff;
52 REG(&(i2c_base->i2c_stat)) = stat;
56 REG(&(i2c_base->i2c_stat)) = 0xffff;
60 static int _poll_i2c_irq(struct i2c_regs *i2c_base, int mask)
64 for (timeout = 0; timeout < 10; timeout++) {
66 stat = REG(&(i2c_base->i2c_stat));
71 REG(&(i2c_base->i2c_stat)) = 0xffff;
72 return stat | I2C_TIMEOUT;
75 static void _flush_rx(struct i2c_regs *i2c_base)
78 if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_RRDY))
81 REG(&(i2c_base->i2c_drr));
82 REG(&(i2c_base->i2c_stat)) = I2C_STAT_RRDY;
87 static uint _davinci_i2c_setspeed(struct i2c_regs *i2c_base,
94 div = (CONFIG_SYS_HZ_CLOCK / ((psc + 1) * speed)) - 10;
95 REG(&(i2c_base->i2c_psc)) = psc; /* 27MHz / (2 + 1) = 9MHz */
96 REG(&(i2c_base->i2c_scll)) = (div * 50) / 100; /* 50% Duty */
97 REG(&(i2c_base->i2c_sclh)) = div - REG(&(i2c_base->i2c_scll));
102 static void _davinci_i2c_init(struct i2c_regs *i2c_base,
103 uint speed, int slaveadd)
105 if (REG(&(i2c_base->i2c_con)) & I2C_CON_EN) {
106 REG(&(i2c_base->i2c_con)) = 0;
110 _davinci_i2c_setspeed(i2c_base, speed);
112 REG(&(i2c_base->i2c_oa)) = slaveadd;
113 REG(&(i2c_base->i2c_cnt)) = 0;
115 /* Interrupts must be enabled or I2C module won't work */
116 REG(&(i2c_base->i2c_ie)) = I2C_IE_SCD_IE | I2C_IE_XRDY_IE |
117 I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | I2C_IE_NACK_IE;
119 /* Now enable I2C controller (get it out of reset) */
120 REG(&(i2c_base->i2c_con)) = I2C_CON_EN;
125 static int _davinci_i2c_read(struct i2c_regs *i2c_base, uint8_t chip,
126 uint32_t addr, int alen, uint8_t *buf, int len)
131 if ((alen < 0) || (alen > 2)) {
132 printf("%s(): bogus address length %x\n", __func__, alen);
136 if (_wait_for_bus(i2c_base))
140 /* Start address phase */
141 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX;
142 REG(&(i2c_base->i2c_cnt)) = alen;
143 REG(&(i2c_base->i2c_sa)) = chip;
144 REG(&(i2c_base->i2c_con)) = tmp;
146 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
152 /* Send address MSByte */
153 if (tmp & I2C_STAT_XRDY) {
154 REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
156 REG(&(i2c_base->i2c_con)) = 0;
160 tmp = _poll_i2c_irq(i2c_base,
161 I2C_STAT_XRDY | I2C_STAT_NACK);
164 /* No break, fall through */
166 /* Send address LSByte */
167 if (tmp & I2C_STAT_XRDY) {
168 REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
170 REG(&(i2c_base->i2c_con)) = 0;
174 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY |
175 I2C_STAT_NACK | I2C_STAT_ARDY);
179 if (!(tmp & I2C_STAT_ARDY)) {
180 REG(&(i2c_base->i2c_con)) = 0;
186 /* Address phase is over, now read 'len' bytes and stop */
187 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
188 REG(&(i2c_base->i2c_cnt)) = len & 0xffff;
189 REG(&(i2c_base->i2c_sa)) = chip;
190 REG(&(i2c_base->i2c_con)) = tmp;
192 for (i = 0; i < len; i++) {
193 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_RRDY | I2C_STAT_NACK |
198 if (tmp & I2C_STAT_RRDY) {
199 buf[i] = REG(&(i2c_base->i2c_drr));
201 REG(&(i2c_base->i2c_con)) = 0;
206 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_SCD | I2C_STAT_NACK);
210 if (!(tmp & I2C_STAT_SCD)) {
211 REG(&(i2c_base->i2c_con)) = 0;
216 REG(&(i2c_base->i2c_stat)) = 0xffff;
217 REG(&(i2c_base->i2c_cnt)) = 0;
218 REG(&(i2c_base->i2c_con)) = 0;
223 static int _davinci_i2c_write(struct i2c_regs *i2c_base, uint8_t chip,
224 uint32_t addr, int alen, uint8_t *buf, int len)
229 if ((alen < 0) || (alen > 2)) {
230 printf("%s(): bogus address length %x\n", __func__, alen);
234 printf("%s(): bogus length %x\n", __func__, len);
238 if (_wait_for_bus(i2c_base))
241 /* Start address phase */
242 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
243 I2C_CON_TRX | I2C_CON_STP;
244 REG(&(i2c_base->i2c_cnt)) = (alen == 0) ?
245 len & 0xffff : (len & 0xffff) + alen;
246 REG(&(i2c_base->i2c_sa)) = chip;
247 REG(&(i2c_base->i2c_con)) = tmp;
251 /* Send address MSByte */
252 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
256 if (tmp & I2C_STAT_XRDY) {
257 REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
259 REG(&(i2c_base->i2c_con)) = 0;
262 /* No break, fall through */
264 /* Send address LSByte */
265 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
269 if (tmp & I2C_STAT_XRDY) {
270 REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
272 REG(&(i2c_base->i2c_con)) = 0;
277 for (i = 0; i < len; i++) {
278 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
282 if (tmp & I2C_STAT_XRDY)
283 REG(&(i2c_base->i2c_dxr)) = buf[i];
288 tmp = _poll_i2c_irq(i2c_base, I2C_STAT_SCD | I2C_STAT_NACK);
292 if (!(tmp & I2C_STAT_SCD)) {
293 REG(&(i2c_base->i2c_con)) = 0;
298 REG(&(i2c_base->i2c_stat)) = 0xffff;
299 REG(&(i2c_base->i2c_cnt)) = 0;
300 REG(&(i2c_base->i2c_con)) = 0;
305 static int _davinci_i2c_probe_chip(struct i2c_regs *i2c_base, uint8_t chip)
309 if (chip == REG(&(i2c_base->i2c_oa)))
312 REG(&(i2c_base->i2c_con)) = 0;
313 if (_wait_for_bus(i2c_base))
316 /* try to read one byte from current (or only) address */
317 REG(&(i2c_base->i2c_cnt)) = 1;
318 REG(&(i2c_base->i2c_sa)) = chip;
319 REG(&(i2c_base->i2c_con)) = (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
323 if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_NACK)) {
326 REG(&(i2c_base->i2c_stat)) = 0xffff;
328 REG(&(i2c_base->i2c_stat)) = 0xffff;
329 REG(&(i2c_base->i2c_con)) |= I2C_CON_STP;
331 if (_wait_for_bus(i2c_base))
336 REG(&(i2c_base->i2c_stat)) = 0xffff;
337 REG(&(i2c_base->i2c_cnt)) = 0;
341 #ifndef CONFIG_DM_I2C
342 static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap)
344 switch (adap->hwadapnr) {
345 #if CONFIG_SYS_I2C_BUS_MAX >= 3
347 return (struct i2c_regs *)I2C2_BASE;
349 #if CONFIG_SYS_I2C_BUS_MAX >= 2
351 return (struct i2c_regs *)I2C1_BASE;
354 return (struct i2c_regs *)I2C_BASE;
357 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
363 static uint davinci_i2c_setspeed(struct i2c_adapter *adap, uint speed)
365 struct i2c_regs *i2c_base = davinci_get_base(adap);
369 ret = _davinci_i2c_setspeed(i2c_base, speed);
374 static void davinci_i2c_init(struct i2c_adapter *adap, int speed,
377 struct i2c_regs *i2c_base = davinci_get_base(adap);
380 _davinci_i2c_init(i2c_base, speed, slaveadd);
385 static int davinci_i2c_read(struct i2c_adapter *adap, uint8_t chip,
386 uint32_t addr, int alen, uint8_t *buf, int len)
388 struct i2c_regs *i2c_base = davinci_get_base(adap);
389 return _davinci_i2c_read(i2c_base, chip, addr, alen, buf, len);
392 static int davinci_i2c_write(struct i2c_adapter *adap, uint8_t chip,
393 uint32_t addr, int alen, uint8_t *buf, int len)
395 struct i2c_regs *i2c_base = davinci_get_base(adap);
397 return _davinci_i2c_write(i2c_base, chip, addr, alen, buf, len);
400 static int davinci_i2c_probe_chip(struct i2c_adapter *adap, uint8_t chip)
402 struct i2c_regs *i2c_base = davinci_get_base(adap);
404 return _davinci_i2c_probe_chip(i2c_base, chip);
407 U_BOOT_I2C_ADAP_COMPLETE(davinci_0, davinci_i2c_init, davinci_i2c_probe_chip,
408 davinci_i2c_read, davinci_i2c_write,
409 davinci_i2c_setspeed,
410 CONFIG_SYS_DAVINCI_I2C_SPEED,
411 CONFIG_SYS_DAVINCI_I2C_SLAVE,
414 #if CONFIG_SYS_I2C_BUS_MAX >= 2
415 U_BOOT_I2C_ADAP_COMPLETE(davinci_1, davinci_i2c_init, davinci_i2c_probe_chip,
416 davinci_i2c_read, davinci_i2c_write,
417 davinci_i2c_setspeed,
418 CONFIG_SYS_DAVINCI_I2C_SPEED1,
419 CONFIG_SYS_DAVINCI_I2C_SLAVE1,
423 #if CONFIG_SYS_I2C_BUS_MAX >= 3
424 U_BOOT_I2C_ADAP_COMPLETE(davinci_2, davinci_i2c_init, davinci_i2c_probe_chip,
425 davinci_i2c_read, davinci_i2c_write,
426 davinci_i2c_setspeed,
427 CONFIG_SYS_DAVINCI_I2C_SPEED2,
428 CONFIG_SYS_DAVINCI_I2C_SLAVE2,
432 #else /* CONFIG_DM_I2C */
434 static int davinci_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
437 struct i2c_bus *i2c_bus = dev_get_priv(bus);
440 debug("i2c_xfer: %d messages\n", nmsgs);
441 for (; nmsgs > 0; nmsgs--, msg++) {
442 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
443 if (msg->flags & I2C_M_RD) {
444 ret = _davinci_i2c_read(i2c_bus->regs, msg->addr,
445 0, 0, msg->buf, msg->len);
447 ret = _davinci_i2c_write(i2c_bus->regs, msg->addr,
448 0, 0, msg->buf, msg->len);
451 debug("i2c_write: error sending\n");
459 static int davinci_i2c_set_speed(struct udevice *dev, uint speed)
461 struct i2c_bus *i2c_bus = dev_get_priv(dev);
463 i2c_bus->speed = speed;
464 return _davinci_i2c_setspeed(i2c_bus->regs, speed);
467 static int davinci_i2c_probe(struct udevice *dev)
469 struct i2c_bus *i2c_bus = dev_get_priv(dev);
471 i2c_bus->id = dev->seq;
472 i2c_bus->regs = (struct i2c_regs *)devfdt_get_addr(dev);
474 i2c_bus->speed = 100000;
475 _davinci_i2c_init(i2c_bus->regs, i2c_bus->speed, 0);
480 static int davinci_i2c_probe_chip(struct udevice *bus, uint chip_addr,
483 struct i2c_bus *i2c_bus = dev_get_priv(bus);
485 return _davinci_i2c_probe_chip(i2c_bus->regs, chip_addr);
488 static const struct dm_i2c_ops davinci_i2c_ops = {
489 .xfer = davinci_i2c_xfer,
490 .probe_chip = davinci_i2c_probe_chip,
491 .set_bus_speed = davinci_i2c_set_speed,
494 static const struct udevice_id davinci_i2c_ids[] = {
495 { .compatible = "ti,davinci-i2c"},
496 { .compatible = "ti,keystone-i2c"},
500 U_BOOT_DRIVER(i2c_davinci) = {
501 .name = "i2c_davinci",
503 .of_match = davinci_i2c_ids,
504 .probe = davinci_i2c_probe,
505 .priv_auto_alloc_size = sizeof(struct i2c_bus),
506 .ops = &davinci_i2c_ops,
509 #endif /* CONFIG_DM_I2C */