blob: 9ca99c4abd0b04d345c8251aef0203a3abc4c082 [file] [log] [blame]
Sergey Kubushync74b2102007-08-10 20:26:18 +02001/*
2 * TI DaVinci (TMS320DM644x) I2C driver.
3 *
Vitaly Andrianove8459dc2014-04-04 13:16:52 -04004 * (C) Copyright 2012-2014
5 * Texas Instruments Incorporated, <www.ti.com>
6 * (C) Copyright 2007 Sergey Kubushyn <ksi@koi8.net>
Sergey Kubushync74b2102007-08-10 20:26:18 +02007 * --------------------------------------------------------
8 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Sergey Kubushync74b2102007-08-10 20:26:18 +020010 */
11
12#include <common.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020013#include <i2c.h>
14#include <asm/arch/hardware.h>
15#include <asm/arch/i2c_defs.h>
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040016#include <asm/io.h>
Karicheri, Muralidharan356d15e2014-04-04 13:16:51 -040017#include "davinci_i2c.h"
Sergey Kubushync74b2102007-08-10 20:26:18 +020018
19#define CHECK_NACK() \
20 do {\
21 if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {\
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040022 REG(&(i2c_base->i2c_con)) = 0;\
23 return 1;\
24 } \
Sergey Kubushync74b2102007-08-10 20:26:18 +020025 } while (0)
26
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040027static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap);
Sergey Kubushync74b2102007-08-10 20:26:18 +020028
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040029static int wait_for_bus(struct i2c_adapter *adap)
Sergey Kubushync74b2102007-08-10 20:26:18 +020030{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040031 struct i2c_regs *i2c_base = davinci_get_base(adap);
Sergey Kubushync74b2102007-08-10 20:26:18 +020032 int stat, timeout;
33
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040034 REG(&(i2c_base->i2c_stat)) = 0xffff;
Sergey Kubushync74b2102007-08-10 20:26:18 +020035
36 for (timeout = 0; timeout < 10; timeout++) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040037 stat = REG(&(i2c_base->i2c_stat));
38 if (!((stat) & I2C_STAT_BB)) {
39 REG(&(i2c_base->i2c_stat)) = 0xffff;
40 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +020041 }
42
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040043 REG(&(i2c_base->i2c_stat)) = stat;
Sergey Kubushync74b2102007-08-10 20:26:18 +020044 udelay(50000);
45 }
46
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040047 REG(&(i2c_base->i2c_stat)) = 0xffff;
48 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +020049}
50
51
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040052static int poll_i2c_irq(struct i2c_adapter *adap, int mask)
Sergey Kubushync74b2102007-08-10 20:26:18 +020053{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040054 struct i2c_regs *i2c_base = davinci_get_base(adap);
Sergey Kubushync74b2102007-08-10 20:26:18 +020055 int stat, timeout;
56
57 for (timeout = 0; timeout < 10; timeout++) {
58 udelay(1000);
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040059 stat = REG(&(i2c_base->i2c_stat));
60 if (stat & mask)
61 return stat;
Sergey Kubushync74b2102007-08-10 20:26:18 +020062 }
63
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040064 REG(&(i2c_base->i2c_stat)) = 0xffff;
65 return stat | I2C_TIMEOUT;
Sergey Kubushync74b2102007-08-10 20:26:18 +020066}
67
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040068static void flush_rx(struct i2c_adapter *adap)
Sergey Kubushync74b2102007-08-10 20:26:18 +020069{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040070 struct i2c_regs *i2c_base = davinci_get_base(adap);
71
Sergey Kubushync74b2102007-08-10 20:26:18 +020072 while (1) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040073 if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_RRDY))
Sergey Kubushync74b2102007-08-10 20:26:18 +020074 break;
75
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040076 REG(&(i2c_base->i2c_drr));
77 REG(&(i2c_base->i2c_stat)) = I2C_STAT_RRDY;
Sergey Kubushync74b2102007-08-10 20:26:18 +020078 udelay(1000);
79 }
80}
81
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040082static uint davinci_i2c_setspeed(struct i2c_adapter *adap, uint speed)
Sergey Kubushync74b2102007-08-10 20:26:18 +020083{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040084 struct i2c_regs *i2c_base = davinci_get_base(adap);
85 uint32_t div, psc;
Sergey Kubushync74b2102007-08-10 20:26:18 +020086
87 psc = 2;
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040088 /* SCLL + SCLH */
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));
Sergey Kubushync74b2102007-08-10 20:26:18 +020093
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040094 adap->speed = speed;
95 return 0;
96}
97
98static void davinci_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
99{
100 struct i2c_regs *i2c_base = davinci_get_base(adap);
101
102 if (REG(&(i2c_base->i2c_con)) & I2C_CON_EN) {
103 REG(&(i2c_base->i2c_con)) = 0;
104 udelay(50000);
105 }
106
107 davinci_i2c_setspeed(adap, speed);
108
109 REG(&(i2c_base->i2c_oa)) = slaveadd;
110 REG(&(i2c_base->i2c_cnt)) = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200111
112 /* Interrupts must be enabled or I2C module won't work */
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400113 REG(&(i2c_base->i2c_ie)) = I2C_IE_SCD_IE | I2C_IE_XRDY_IE |
Sergey Kubushync74b2102007-08-10 20:26:18 +0200114 I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | I2C_IE_NACK_IE;
115
116 /* Now enable I2C controller (get it out of reset) */
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400117 REG(&(i2c_base->i2c_con)) = I2C_CON_EN;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200118
119 udelay(1000);
120}
121
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400122static int davinci_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
Heiko Schocher49d6da62011-09-14 19:25:12 +0000123{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400124 struct i2c_regs *i2c_base = davinci_get_base(adap);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200125 int rc = 1;
126
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400127 if (chip == REG(&(i2c_base->i2c_oa)))
128 return rc;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200129
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400130 REG(&(i2c_base->i2c_con)) = 0;
131 if (wait_for_bus(adap))
132 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200133
134 /* try to read one byte from current (or only) address */
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400135 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 |
138 I2C_CON_STP);
139 udelay(50000);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200140
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400141 if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_NACK)) {
Sergey Kubushync74b2102007-08-10 20:26:18 +0200142 rc = 0;
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400143 flush_rx(adap);
144 REG(&(i2c_base->i2c_stat)) = 0xffff;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200145 } else {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400146 REG(&(i2c_base->i2c_stat)) = 0xffff;
147 REG(&(i2c_base->i2c_con)) |= I2C_CON_STP;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200148 udelay(20000);
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400149 if (wait_for_bus(adap))
150 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200151 }
152
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400153 flush_rx(adap);
154 REG(&(i2c_base->i2c_stat)) = 0xffff;
155 REG(&(i2c_base->i2c_cnt)) = 0;
156 return rc;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200157}
158
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400159static int davinci_i2c_read(struct i2c_adapter *adap, uint8_t chip,
160 uint32_t addr, int alen, uint8_t *buf, int len)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200161{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400162 struct i2c_regs *i2c_base = davinci_get_base(adap);
163 uint32_t tmp;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200164 int i;
165
166 if ((alen < 0) || (alen > 2)) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400167 printf("%s(): bogus address length %x\n", __func__, alen);
168 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200169 }
170
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400171 if (wait_for_bus(adap))
172 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200173
174 if (alen != 0) {
175 /* Start address phase */
176 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX;
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400177 REG(&(i2c_base->i2c_cnt)) = alen;
178 REG(&(i2c_base->i2c_sa)) = chip;
179 REG(&(i2c_base->i2c_con)) = tmp;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200180
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400181 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200182
183 CHECK_NACK();
184
185 switch (alen) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400186 case 2:
187 /* Send address MSByte */
188 if (tmp & I2C_STAT_XRDY) {
189 REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
190 } else {
191 REG(&(i2c_base->i2c_con)) = 0;
192 return 1;
193 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200194
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400195 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200196
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400197 CHECK_NACK();
198 /* No break, fall through */
199 case 1:
200 /* Send address LSByte */
201 if (tmp & I2C_STAT_XRDY) {
202 REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
203 } else {
204 REG(&(i2c_base->i2c_con)) = 0;
205 return 1;
206 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200207
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400208 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY |
209 I2C_STAT_NACK | I2C_STAT_ARDY);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200210
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400211 CHECK_NACK();
Sergey Kubushync74b2102007-08-10 20:26:18 +0200212
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400213 if (!(tmp & I2C_STAT_ARDY)) {
214 REG(&(i2c_base->i2c_con)) = 0;
215 return 1;
216 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200217 }
218 }
219
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;
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400222 REG(&(i2c_base->i2c_cnt)) = len & 0xffff;
223 REG(&(i2c_base->i2c_sa)) = chip;
224 REG(&(i2c_base->i2c_con)) = tmp;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200225
226 for (i = 0; i < len; i++) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400227 tmp = poll_i2c_irq(adap, I2C_STAT_RRDY | I2C_STAT_NACK |
228 I2C_STAT_ROVR);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200229
230 CHECK_NACK();
231
232 if (tmp & I2C_STAT_RRDY) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400233 buf[i] = REG(&(i2c_base->i2c_drr));
Sergey Kubushync74b2102007-08-10 20:26:18 +0200234 } else {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400235 REG(&(i2c_base->i2c_con)) = 0;
236 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200237 }
238 }
239
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400240 tmp = poll_i2c_irq(adap, I2C_STAT_SCD | I2C_STAT_NACK);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200241
242 CHECK_NACK();
243
244 if (!(tmp & I2C_STAT_SCD)) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400245 REG(&(i2c_base->i2c_con)) = 0;
246 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200247 }
248
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400249 flush_rx(adap);
250 REG(&(i2c_base->i2c_stat)) = 0xffff;
251 REG(&(i2c_base->i2c_cnt)) = 0;
252 REG(&(i2c_base->i2c_con)) = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200253
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400254 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200255}
256
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400257static int davinci_i2c_write(struct i2c_adapter *adap, uint8_t chip,
258 uint32_t addr, int alen, uint8_t *buf, int len)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200259{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400260 struct i2c_regs *i2c_base = davinci_get_base(adap);
261 uint32_t tmp;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200262 int i;
263
264 if ((alen < 0) || (alen > 2)) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400265 printf("%s(): bogus address length %x\n", __func__, alen);
266 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200267 }
268 if (len < 0) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400269 printf("%s(): bogus length %x\n", __func__, len);
270 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200271 }
272
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400273 if (wait_for_bus(adap))
274 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200275
276 /* Start address phase */
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400277 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;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200283
284 switch (alen) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400285 case 2:
286 /* Send address MSByte */
287 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200288
289 CHECK_NACK();
290
291 if (tmp & I2C_STAT_XRDY) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400292 REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200293 } else {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400294 REG(&(i2c_base->i2c_con)) = 0;
295 return 1;
296 }
297 /* No break, fall through */
298 case 1:
299 /* Send address LSByte */
300 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
301
302 CHECK_NACK();
303
304 if (tmp & I2C_STAT_XRDY) {
305 REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
306 } else {
307 REG(&(i2c_base->i2c_con)) = 0;
308 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200309 }
310 }
311
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400312 for (i = 0; i < len; i++) {
313 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
314
315 CHECK_NACK();
316
317 if (tmp & I2C_STAT_XRDY)
318 REG(&(i2c_base->i2c_dxr)) = buf[i];
319 else
320 return 1;
321 }
322
323 tmp = poll_i2c_irq(adap, I2C_STAT_SCD | I2C_STAT_NACK);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200324
325 CHECK_NACK();
326
327 if (!(tmp & I2C_STAT_SCD)) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400328 REG(&(i2c_base->i2c_con)) = 0;
329 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200330 }
331
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400332 flush_rx(adap);
333 REG(&(i2c_base->i2c_stat)) = 0xffff;
334 REG(&(i2c_base->i2c_cnt)) = 0;
335 REG(&(i2c_base->i2c_con)) = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200336
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400337 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200338}
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400339
340static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap)
341{
342 switch (adap->hwadapnr) {
343#if I2C_BUS_MAX >= 3
344 case 2:
345 return (struct i2c_regs *)I2C2_BASE;
346#endif
347#if I2C_BUS_MAX >= 2
348 case 1:
349 return (struct i2c_regs *)I2C1_BASE;
350#endif
351 case 0:
352 return (struct i2c_regs *)I2C_BASE;
353
354 default:
355 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
356 }
357
358 return NULL;
359}
360
361U_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,
366 0)
367
368#if I2C_BUS_MAX >= 2
369U_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,
374 1)
375#endif
376
377#if I2C_BUS_MAX >= 3
378U_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,
383 2)
384#endif