blob: c5bd38c67b88934dc7f29b3a7291ef99a21e71dd [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+
Simon Glass28527092016-11-23 06:34:44 -070010 *
11 * NOTE: This driver should be converted to driver model before June 2017.
12 * Please see doc/driver-model/i2c-howto.txt for instructions.
Sergey Kubushync74b2102007-08-10 20:26:18 +020013 */
14
15#include <common.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020016#include <i2c.h>
17#include <asm/arch/hardware.h>
18#include <asm/arch/i2c_defs.h>
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040019#include <asm/io.h>
Karicheri, Muralidharan356d15e2014-04-04 13:16:51 -040020#include "davinci_i2c.h"
Sergey Kubushync74b2102007-08-10 20:26:18 +020021
22#define CHECK_NACK() \
23 do {\
24 if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {\
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040025 REG(&(i2c_base->i2c_con)) = 0;\
26 return 1;\
27 } \
Sergey Kubushync74b2102007-08-10 20:26:18 +020028 } while (0)
29
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040030static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap);
Sergey Kubushync74b2102007-08-10 20:26:18 +020031
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040032static int wait_for_bus(struct i2c_adapter *adap)
Sergey Kubushync74b2102007-08-10 20:26:18 +020033{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040034 struct i2c_regs *i2c_base = davinci_get_base(adap);
Sergey Kubushync74b2102007-08-10 20:26:18 +020035 int stat, timeout;
36
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040037 REG(&(i2c_base->i2c_stat)) = 0xffff;
Sergey Kubushync74b2102007-08-10 20:26:18 +020038
39 for (timeout = 0; timeout < 10; timeout++) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040040 stat = REG(&(i2c_base->i2c_stat));
41 if (!((stat) & I2C_STAT_BB)) {
42 REG(&(i2c_base->i2c_stat)) = 0xffff;
43 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +020044 }
45
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040046 REG(&(i2c_base->i2c_stat)) = stat;
Sergey Kubushync74b2102007-08-10 20:26:18 +020047 udelay(50000);
48 }
49
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040050 REG(&(i2c_base->i2c_stat)) = 0xffff;
51 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +020052}
53
54
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040055static int poll_i2c_irq(struct i2c_adapter *adap, int mask)
Sergey Kubushync74b2102007-08-10 20:26:18 +020056{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040057 struct i2c_regs *i2c_base = davinci_get_base(adap);
Sergey Kubushync74b2102007-08-10 20:26:18 +020058 int stat, timeout;
59
60 for (timeout = 0; timeout < 10; timeout++) {
61 udelay(1000);
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040062 stat = REG(&(i2c_base->i2c_stat));
63 if (stat & mask)
64 return stat;
Sergey Kubushync74b2102007-08-10 20:26:18 +020065 }
66
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040067 REG(&(i2c_base->i2c_stat)) = 0xffff;
68 return stat | I2C_TIMEOUT;
Sergey Kubushync74b2102007-08-10 20:26:18 +020069}
70
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040071static void flush_rx(struct i2c_adapter *adap)
Sergey Kubushync74b2102007-08-10 20:26:18 +020072{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040073 struct i2c_regs *i2c_base = davinci_get_base(adap);
74
Sergey Kubushync74b2102007-08-10 20:26:18 +020075 while (1) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040076 if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_RRDY))
Sergey Kubushync74b2102007-08-10 20:26:18 +020077 break;
78
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040079 REG(&(i2c_base->i2c_drr));
80 REG(&(i2c_base->i2c_stat)) = I2C_STAT_RRDY;
Sergey Kubushync74b2102007-08-10 20:26:18 +020081 udelay(1000);
82 }
83}
84
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040085static uint davinci_i2c_setspeed(struct i2c_adapter *adap, uint speed)
Sergey Kubushync74b2102007-08-10 20:26:18 +020086{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040087 struct i2c_regs *i2c_base = davinci_get_base(adap);
88 uint32_t div, psc;
Sergey Kubushync74b2102007-08-10 20:26:18 +020089
90 psc = 2;
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040091 /* SCLL + SCLH */
92 div = (CONFIG_SYS_HZ_CLOCK / ((psc + 1) * speed)) - 10;
93 REG(&(i2c_base->i2c_psc)) = psc; /* 27MHz / (2 + 1) = 9MHz */
94 REG(&(i2c_base->i2c_scll)) = (div * 50) / 100; /* 50% Duty */
95 REG(&(i2c_base->i2c_sclh)) = div - REG(&(i2c_base->i2c_scll));
Sergey Kubushync74b2102007-08-10 20:26:18 +020096
Vitaly Andrianove8459dc2014-04-04 13:16:52 -040097 adap->speed = speed;
98 return 0;
99}
100
101static void davinci_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
102{
103 struct i2c_regs *i2c_base = davinci_get_base(adap);
104
105 if (REG(&(i2c_base->i2c_con)) & I2C_CON_EN) {
106 REG(&(i2c_base->i2c_con)) = 0;
107 udelay(50000);
108 }
109
110 davinci_i2c_setspeed(adap, speed);
111
112 REG(&(i2c_base->i2c_oa)) = slaveadd;
113 REG(&(i2c_base->i2c_cnt)) = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200114
115 /* Interrupts must be enabled or I2C module won't work */
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400116 REG(&(i2c_base->i2c_ie)) = I2C_IE_SCD_IE | I2C_IE_XRDY_IE |
Sergey Kubushync74b2102007-08-10 20:26:18 +0200117 I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | I2C_IE_NACK_IE;
118
119 /* Now enable I2C controller (get it out of reset) */
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400120 REG(&(i2c_base->i2c_con)) = I2C_CON_EN;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200121
122 udelay(1000);
123}
124
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400125static int davinci_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
Heiko Schocher49d6da62011-09-14 19:25:12 +0000126{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400127 struct i2c_regs *i2c_base = davinci_get_base(adap);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200128 int rc = 1;
129
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400130 if (chip == REG(&(i2c_base->i2c_oa)))
131 return rc;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200132
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400133 REG(&(i2c_base->i2c_con)) = 0;
134 if (wait_for_bus(adap))
135 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200136
137 /* try to read one byte from current (or only) address */
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400138 REG(&(i2c_base->i2c_cnt)) = 1;
139 REG(&(i2c_base->i2c_sa)) = chip;
140 REG(&(i2c_base->i2c_con)) = (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
141 I2C_CON_STP);
142 udelay(50000);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200143
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400144 if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_NACK)) {
Sergey Kubushync74b2102007-08-10 20:26:18 +0200145 rc = 0;
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400146 flush_rx(adap);
147 REG(&(i2c_base->i2c_stat)) = 0xffff;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200148 } else {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400149 REG(&(i2c_base->i2c_stat)) = 0xffff;
150 REG(&(i2c_base->i2c_con)) |= I2C_CON_STP;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200151 udelay(20000);
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400152 if (wait_for_bus(adap))
153 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200154 }
155
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400156 flush_rx(adap);
157 REG(&(i2c_base->i2c_stat)) = 0xffff;
158 REG(&(i2c_base->i2c_cnt)) = 0;
159 return rc;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200160}
161
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400162static int davinci_i2c_read(struct i2c_adapter *adap, uint8_t chip,
163 uint32_t addr, int alen, uint8_t *buf, int len)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200164{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400165 struct i2c_regs *i2c_base = davinci_get_base(adap);
166 uint32_t tmp;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200167 int i;
168
169 if ((alen < 0) || (alen > 2)) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400170 printf("%s(): bogus address length %x\n", __func__, alen);
171 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200172 }
173
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400174 if (wait_for_bus(adap))
175 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200176
177 if (alen != 0) {
178 /* Start address phase */
179 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX;
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400180 REG(&(i2c_base->i2c_cnt)) = alen;
181 REG(&(i2c_base->i2c_sa)) = chip;
182 REG(&(i2c_base->i2c_con)) = tmp;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200183
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400184 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200185
186 CHECK_NACK();
187
188 switch (alen) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400189 case 2:
190 /* Send address MSByte */
191 if (tmp & I2C_STAT_XRDY) {
192 REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
193 } else {
194 REG(&(i2c_base->i2c_con)) = 0;
195 return 1;
196 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200197
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400198 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200199
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400200 CHECK_NACK();
201 /* No break, fall through */
202 case 1:
203 /* Send address LSByte */
204 if (tmp & I2C_STAT_XRDY) {
205 REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
206 } else {
207 REG(&(i2c_base->i2c_con)) = 0;
208 return 1;
209 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200210
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400211 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY |
212 I2C_STAT_NACK | I2C_STAT_ARDY);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200213
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400214 CHECK_NACK();
Sergey Kubushync74b2102007-08-10 20:26:18 +0200215
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400216 if (!(tmp & I2C_STAT_ARDY)) {
217 REG(&(i2c_base->i2c_con)) = 0;
218 return 1;
219 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200220 }
221 }
222
223 /* Address phase is over, now read 'len' bytes and stop */
224 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400225 REG(&(i2c_base->i2c_cnt)) = len & 0xffff;
226 REG(&(i2c_base->i2c_sa)) = chip;
227 REG(&(i2c_base->i2c_con)) = tmp;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200228
229 for (i = 0; i < len; i++) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400230 tmp = poll_i2c_irq(adap, I2C_STAT_RRDY | I2C_STAT_NACK |
231 I2C_STAT_ROVR);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200232
233 CHECK_NACK();
234
235 if (tmp & I2C_STAT_RRDY) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400236 buf[i] = REG(&(i2c_base->i2c_drr));
Sergey Kubushync74b2102007-08-10 20:26:18 +0200237 } else {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400238 REG(&(i2c_base->i2c_con)) = 0;
239 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200240 }
241 }
242
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400243 tmp = poll_i2c_irq(adap, I2C_STAT_SCD | I2C_STAT_NACK);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200244
245 CHECK_NACK();
246
247 if (!(tmp & I2C_STAT_SCD)) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400248 REG(&(i2c_base->i2c_con)) = 0;
249 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200250 }
251
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400252 flush_rx(adap);
253 REG(&(i2c_base->i2c_stat)) = 0xffff;
254 REG(&(i2c_base->i2c_cnt)) = 0;
255 REG(&(i2c_base->i2c_con)) = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200256
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400257 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200258}
259
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400260static int davinci_i2c_write(struct i2c_adapter *adap, uint8_t chip,
261 uint32_t addr, int alen, uint8_t *buf, int len)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200262{
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400263 struct i2c_regs *i2c_base = davinci_get_base(adap);
264 uint32_t tmp;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200265 int i;
266
267 if ((alen < 0) || (alen > 2)) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400268 printf("%s(): bogus address length %x\n", __func__, alen);
269 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200270 }
271 if (len < 0) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400272 printf("%s(): bogus length %x\n", __func__, len);
273 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200274 }
275
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400276 if (wait_for_bus(adap))
277 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200278
279 /* Start address phase */
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400280 tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
281 I2C_CON_TRX | I2C_CON_STP;
282 REG(&(i2c_base->i2c_cnt)) = (alen == 0) ?
283 len & 0xffff : (len & 0xffff) + alen;
284 REG(&(i2c_base->i2c_sa)) = chip;
285 REG(&(i2c_base->i2c_con)) = tmp;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200286
287 switch (alen) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400288 case 2:
289 /* Send address MSByte */
290 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200291
292 CHECK_NACK();
293
294 if (tmp & I2C_STAT_XRDY) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400295 REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200296 } else {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400297 REG(&(i2c_base->i2c_con)) = 0;
298 return 1;
299 }
300 /* No break, fall through */
301 case 1:
302 /* Send address LSByte */
303 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
304
305 CHECK_NACK();
306
307 if (tmp & I2C_STAT_XRDY) {
308 REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
309 } else {
310 REG(&(i2c_base->i2c_con)) = 0;
311 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200312 }
313 }
314
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400315 for (i = 0; i < len; i++) {
316 tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
317
318 CHECK_NACK();
319
320 if (tmp & I2C_STAT_XRDY)
321 REG(&(i2c_base->i2c_dxr)) = buf[i];
322 else
323 return 1;
324 }
325
326 tmp = poll_i2c_irq(adap, I2C_STAT_SCD | I2C_STAT_NACK);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200327
328 CHECK_NACK();
329
330 if (!(tmp & I2C_STAT_SCD)) {
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400331 REG(&(i2c_base->i2c_con)) = 0;
332 return 1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200333 }
334
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400335 flush_rx(adap);
336 REG(&(i2c_base->i2c_stat)) = 0xffff;
337 REG(&(i2c_base->i2c_cnt)) = 0;
338 REG(&(i2c_base->i2c_con)) = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200339
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400340 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200341}
Vitaly Andrianove8459dc2014-04-04 13:16:52 -0400342
343static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap)
344{
345 switch (adap->hwadapnr) {
346#if I2C_BUS_MAX >= 3
347 case 2:
348 return (struct i2c_regs *)I2C2_BASE;
349#endif
350#if I2C_BUS_MAX >= 2
351 case 1:
352 return (struct i2c_regs *)I2C1_BASE;
353#endif
354 case 0:
355 return (struct i2c_regs *)I2C_BASE;
356
357 default:
358 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
359 }
360
361 return NULL;
362}
363
364U_BOOT_I2C_ADAP_COMPLETE(davinci_0, davinci_i2c_init, davinci_i2c_probe,
365 davinci_i2c_read, davinci_i2c_write,
366 davinci_i2c_setspeed,
367 CONFIG_SYS_DAVINCI_I2C_SPEED,
368 CONFIG_SYS_DAVINCI_I2C_SLAVE,
369 0)
370
371#if I2C_BUS_MAX >= 2
372U_BOOT_I2C_ADAP_COMPLETE(davinci_1, davinci_i2c_init, davinci_i2c_probe,
373 davinci_i2c_read, davinci_i2c_write,
374 davinci_i2c_setspeed,
375 CONFIG_SYS_DAVINCI_I2C_SPEED1,
376 CONFIG_SYS_DAVINCI_I2C_SLAVE1,
377 1)
378#endif
379
380#if I2C_BUS_MAX >= 3
381U_BOOT_I2C_ADAP_COMPLETE(davinci_2, davinci_i2c_init, davinci_i2c_probe,
382 davinci_i2c_read, davinci_i2c_write,
383 davinci_i2c_setspeed,
384 CONFIG_SYS_DAVINCI_I2C_SPEED2,
385 CONFIG_SYS_DAVINCI_I2C_SLAVE2,
386 2)
387#endif