2 * TI DaVinci (TMS320DM644x) I2C driver.
4 * (C) Copyright 2012-2014
5 * Texas Instruments Incorporated, <www.ti.com>
6 * (C) Copyright 2007 Sergey Kubushyn <ksi@koi8.net>
7 * --------------------------------------------------------
9 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/arch/hardware.h>
15 #include <asm/arch/i2c_defs.h>
17 #include "davinci_i2c.h"
19 #define CHECK_NACK() \
21 if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {\
22 REG(&(i2c_base->i2c_con)) = 0;\
27 static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap);
29 static int wait_for_bus(struct i2c_adapter *adap)
31 struct i2c_regs *i2c_base = davinci_get_base(adap);
34 REG(&(i2c_base->i2c_stat)) = 0xffff;
36 for (timeout = 0; timeout < 10; timeout++) {
37 stat = REG(&(i2c_base->i2c_stat));
38 if (!((stat) & I2C_STAT_BB)) {
39 REG(&(i2c_base->i2c_stat)) = 0xffff;
43 REG(&(i2c_base->i2c_stat)) = stat;
47 REG(&(i2c_base->i2c_stat)) = 0xffff;
52 static int poll_i2c_irq(struct i2c_adapter *adap, int mask)
54 struct i2c_regs *i2c_base = davinci_get_base(adap);
57 for (timeout = 0; timeout < 10; timeout++) {
59 stat = REG(&(i2c_base->i2c_stat));
64 REG(&(i2c_base->i2c_stat)) = 0xffff;
65 return stat | I2C_TIMEOUT;
68 static void flush_rx(struct i2c_adapter *adap)
70 struct i2c_regs *i2c_base = davinci_get_base(adap);
73 if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_RRDY))
76 REG(&(i2c_base->i2c_drr));
77 REG(&(i2c_base->i2c_stat)) = I2C_STAT_RRDY;
82 static uint davinci_i2c_setspeed(struct i2c_adapter *adap, uint speed)
84 struct i2c_regs *i2c_base = davinci_get_base(adap);
89 div = (CONFIG_SYS_HZ_CLOCK / ((psc + 1) * speed)) - 10;
90 REG(&(i2c_base->i2c_psc)) = psc; /* 27MHz / (2 + 1) = 9MHz */
91 REG(&(i2c_base->i2c_scll)) = (div * 50) / 100; /* 50% Duty */
92 REG(&(i2c_base->i2c_sclh)) = div - REG(&(i2c_base->i2c_scll));
98 static void davinci_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
100 struct i2c_regs *i2c_base = davinci_get_base(adap);
102 if (REG(&(i2c_base->i2c_con)) & I2C_CON_EN) {
103 REG(&(i2c_base->i2c_con)) = 0;
107 davinci_i2c_setspeed(adap, speed);
109 REG(&(i2c_base->i2c_oa)) = slaveadd;
110 REG(&(i2c_base->i2c_cnt)) = 0;
112 /* Interrupts must be enabled or I2C module won't work */
113 REG(&(i2c_base->i2c_ie)) = I2C_IE_SCD_IE | I2C_IE_XRDY_IE |
114 I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | I2C_IE_NACK_IE;
116 /* Now enable I2C controller (get it out of reset) */
117 REG(&(i2c_base->i2c_con)) = I2C_CON_EN;
122 static int davinci_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
124 struct i2c_regs *i2c_base = davinci_get_base(adap);
127 if (chip == REG(&(i2c_base->i2c_oa)))
130 REG(&(i2c_base->i2c_con)) = 0;
131 if (wait_for_bus(adap))
134 /* try to read one byte from current (or only) address */
135 REG(&(i2c_base->i2c_cnt)) = 1;
136 REG(&(i2c_base->i2c_sa)) = chip;
137 REG(&(i2c_base->i2c_con)) = (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
141 if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_NACK)) {
144 REG(&(i2c_base->i2c_stat)) = 0xffff;
146 REG(&(i2c_base->i2c_stat)) = 0xffff;
147 REG(&(i2c_base->i2c_con)) |= I2C_CON_STP;
149 if (wait_for_bus(adap))
154 REG(&(i2c_base->i2c_stat)) = 0xffff;
155 REG(&(i2c_base->i2c_cnt)) = 0;
159 static int davinci_i2c_read(struct i2c_adapter *adap, uint8_t chip,
160 uint32_t addr, int alen, uint8_t *buf, int len)
162 struct i2c_regs *i2c_base = davinci_get_base(adap);
166 if ((alen < 0) || (alen > 2)) {
167 printf("%s(): bogus address length %x\n", __func__, alen);
171 if (wait_for_bus(adap))
175 /* Start address phase */
176 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX;
177 REG(&(i2c_base->i2c_cnt)) = alen;
178 REG(&(i2c_base->i2c_sa)) = chip;
179 REG(&(i2c_base->i2c_con)) = tmp;
181 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
187 /* Send address MSByte */
188 if (tmp & I2C_STAT_XRDY) {
189 REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
191 REG(&(i2c_base->i2c_con)) = 0;
195 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
198 /* No break, fall through */
200 /* Send address LSByte */
201 if (tmp & I2C_STAT_XRDY) {
202 REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
204 REG(&(i2c_base->i2c_con)) = 0;
208 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY |
209 I2C_STAT_NACK | I2C_STAT_ARDY);
213 if (!(tmp & I2C_STAT_ARDY)) {
214 REG(&(i2c_base->i2c_con)) = 0;
220 /* Address phase is over, now read 'len' bytes and stop */
221 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
222 REG(&(i2c_base->i2c_cnt)) = len & 0xffff;
223 REG(&(i2c_base->i2c_sa)) = chip;
224 REG(&(i2c_base->i2c_con)) = tmp;
226 for (i = 0; i < len; i++) {
227 tmp = poll_i2c_irq(adap, I2C_STAT_RRDY | I2C_STAT_NACK |
232 if (tmp & I2C_STAT_RRDY) {
233 buf[i] = REG(&(i2c_base->i2c_drr));
235 REG(&(i2c_base->i2c_con)) = 0;
240 tmp = poll_i2c_irq(adap, I2C_STAT_SCD | I2C_STAT_NACK);
244 if (!(tmp & I2C_STAT_SCD)) {
245 REG(&(i2c_base->i2c_con)) = 0;
250 REG(&(i2c_base->i2c_stat)) = 0xffff;
251 REG(&(i2c_base->i2c_cnt)) = 0;
252 REG(&(i2c_base->i2c_con)) = 0;
257 static int davinci_i2c_write(struct i2c_adapter *adap, uint8_t chip,
258 uint32_t addr, int alen, uint8_t *buf, int len)
260 struct i2c_regs *i2c_base = davinci_get_base(adap);
264 if ((alen < 0) || (alen > 2)) {
265 printf("%s(): bogus address length %x\n", __func__, alen);
269 printf("%s(): bogus length %x\n", __func__, len);
273 if (wait_for_bus(adap))
276 /* Start address phase */
277 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
278 I2C_CON_TRX | I2C_CON_STP;
279 REG(&(i2c_base->i2c_cnt)) = (alen == 0) ?
280 len & 0xffff : (len & 0xffff) + alen;
281 REG(&(i2c_base->i2c_sa)) = chip;
282 REG(&(i2c_base->i2c_con)) = tmp;
286 /* Send address MSByte */
287 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
291 if (tmp & I2C_STAT_XRDY) {
292 REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
294 REG(&(i2c_base->i2c_con)) = 0;
297 /* No break, fall through */
299 /* Send address LSByte */
300 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
304 if (tmp & I2C_STAT_XRDY) {
305 REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
307 REG(&(i2c_base->i2c_con)) = 0;
312 for (i = 0; i < len; i++) {
313 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
317 if (tmp & I2C_STAT_XRDY)
318 REG(&(i2c_base->i2c_dxr)) = buf[i];
323 tmp = poll_i2c_irq(adap, I2C_STAT_SCD | I2C_STAT_NACK);
327 if (!(tmp & I2C_STAT_SCD)) {
328 REG(&(i2c_base->i2c_con)) = 0;
333 REG(&(i2c_base->i2c_stat)) = 0xffff;
334 REG(&(i2c_base->i2c_cnt)) = 0;
335 REG(&(i2c_base->i2c_con)) = 0;
340 static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap)
342 switch (adap->hwadapnr) {
345 return (struct i2c_regs *)I2C2_BASE;
349 return (struct i2c_regs *)I2C1_BASE;
352 return (struct i2c_regs *)I2C_BASE;
355 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
361 U_BOOT_I2C_ADAP_COMPLETE(davinci_0, davinci_i2c_init, davinci_i2c_probe,
362 davinci_i2c_read, davinci_i2c_write,
363 davinci_i2c_setspeed,
364 CONFIG_SYS_DAVINCI_I2C_SPEED,
365 CONFIG_SYS_DAVINCI_I2C_SLAVE,
369 U_BOOT_I2C_ADAP_COMPLETE(davinci_1, davinci_i2c_init, davinci_i2c_probe,
370 davinci_i2c_read, davinci_i2c_write,
371 davinci_i2c_setspeed,
372 CONFIG_SYS_DAVINCI_I2C_SPEED1,
373 CONFIG_SYS_DAVINCI_I2C_SLAVE1,
378 U_BOOT_I2C_ADAP_COMPLETE(davinci_2, davinci_i2c_init, davinci_i2c_probe,
379 davinci_i2c_read, davinci_i2c_write,
380 davinci_i2c_setspeed,
381 CONFIG_SYS_DAVINCI_I2C_SPEED2,
382 CONFIG_SYS_DAVINCI_I2C_SLAVE2,