blob: 7c98f150d74f3d27d0035077ca6f00952b133600 [file] [log] [blame]
wdenkf8062712005-01-09 23:16:25 +00001/*
2 * Basic I2C functions
3 *
4 * Copyright (c) 2004 Texas Instruments
5 *
6 * This package is free software; you can redistribute it and/or
7 * modify it under the terms of the license found in the file
8 * named COPYING that should have accompanied this file.
9 *
10 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
11 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Author: Jian Zhang jzhang@ti.com, Texas Instruments
15 *
16 * Copyright (c) 2003 Wolfgang Denk, wd@denx.de
17 * Rewritten to fit into the current U-Boot framework
18 *
19 * Adapted for OMAP2420 I2C, r-woodruff2@ti.com
20 *
21 */
22
23#include <common.h>
wdenkcb99da52005-01-12 00:15:14 +000024
wdenkf8062712005-01-09 23:16:25 +000025#include <asm/arch/i2c.h>
26#include <asm/io.h>
27
Steve Sakoman10acc712010-06-12 06:42:57 -070028#include "omap24xx_i2c.h"
29
Steve Sakomane2bdc132010-07-19 20:31:55 -070030#define I2C_TIMEOUT 10
31
wdenkf8062712005-01-09 23:16:25 +000032static void wait_for_bb (void);
33static u16 wait_for_pin (void);
Wolfgang Denke1e46792005-09-25 18:41:04 +020034static void flush_fifo(void);
wdenkf8062712005-01-09 23:16:25 +000035
Dirk Behme7a8f6572009-11-02 20:36:26 +010036static struct i2c *i2c_base = (struct i2c *)I2C_DEFAULT_BASE;
37
38static unsigned int bus_initialized[I2C_BUS_MAX];
39static unsigned int current_bus;
40
wdenkf8062712005-01-09 23:16:25 +000041void i2c_init (int speed, int slaveadd)
42{
Tom Rix03b2a742009-06-28 12:52:27 -050043 int psc, fsscll, fssclh;
44 int hsscll = 0, hssclh = 0;
45 u32 scll, sclh;
Steve Sakomane2bdc132010-07-19 20:31:55 -070046 int timeout = I2C_TIMEOUT;
Tom Rix03b2a742009-06-28 12:52:27 -050047
48 /* Only handle standard, fast and high speeds */
49 if ((speed != OMAP_I2C_STANDARD) &&
50 (speed != OMAP_I2C_FAST_MODE) &&
51 (speed != OMAP_I2C_HIGH_SPEED)) {
52 printf("Error : I2C unsupported speed %d\n", speed);
53 return;
54 }
55
56 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
57 psc -= 1;
58 if (psc < I2C_PSC_MIN) {
59 printf("Error : I2C unsupported prescalar %d\n", psc);
60 return;
61 }
62
63 if (speed == OMAP_I2C_HIGH_SPEED) {
64 /* High speed */
65
66 /* For first phase of HS mode */
67 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK /
68 (2 * OMAP_I2C_FAST_MODE);
69
70 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
71 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
72 if (((fsscll < 0) || (fssclh < 0)) ||
73 ((fsscll > 255) || (fssclh > 255))) {
74 printf("Error : I2C initializing first phase clock\n");
75 return;
76 }
77
78 /* For second phase of HS mode */
79 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
80
81 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
82 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
83 if (((fsscll < 0) || (fssclh < 0)) ||
84 ((fsscll > 255) || (fssclh > 255))) {
85 printf("Error : I2C initializing second phase clock\n");
86 return;
87 }
88
89 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
90 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
91
92 } else {
93 /* Standard and fast speed */
94 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
95
96 fsscll -= I2C_FASTSPEED_SCLL_TRIM;
97 fssclh -= I2C_FASTSPEED_SCLH_TRIM;
98 if (((fsscll < 0) || (fssclh < 0)) ||
99 ((fsscll > 255) || (fssclh > 255))) {
100 printf("Error : I2C initializing clock\n");
101 return;
102 }
103
104 scll = (unsigned int)fsscll;
105 sclh = (unsigned int)fssclh;
106 }
wdenkf8062712005-01-09 23:16:25 +0000107
Dirk Behme7a8f6572009-11-02 20:36:26 +0100108 if (readw (&i2c_base->con) & I2C_CON_EN) {
109 writew (0, &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000110 udelay (50000);
111 }
112
Steve Sakomane2bdc132010-07-19 20:31:55 -0700113 writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
114 udelay(1000);
115
116 writew(I2C_CON_EN, &i2c_base->con);
117 while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
118 if (timeout <= 0) {
119 printf("ERROR: Timeout in soft-reset\n");
120 return;
121 }
122 udelay(1000);
123 }
124
125 writew(0, &i2c_base->con);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100126 writew(psc, &i2c_base->psc);
127 writew(scll, &i2c_base->scll);
128 writew(sclh, &i2c_base->sclh);
Tom Rix03b2a742009-06-28 12:52:27 -0500129
wdenkf8062712005-01-09 23:16:25 +0000130 /* own address */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100131 writew (slaveadd, &i2c_base->oa);
132 writew (I2C_CON_EN, &i2c_base->con);
Wolfgang Denke1e46792005-09-25 18:41:04 +0200133
wdenkf8062712005-01-09 23:16:25 +0000134 /* have to enable intrrupts or OMAP i2c module doesn't work */
Dirk Behme5a6dc872008-11-10 20:15:25 +0100135 writew (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
Dirk Behme7a8f6572009-11-02 20:36:26 +0100136 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
wdenkf8062712005-01-09 23:16:25 +0000137 udelay (1000);
Wolfgang Denke1e46792005-09-25 18:41:04 +0200138 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100139 writew (0xFFFF, &i2c_base->stat);
140 writew (0, &i2c_base->cnt);
141
142 bus_initialized[current_bus] = 1;
wdenkf8062712005-01-09 23:16:25 +0000143}
144
145static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
146{
147 int i2c_error = 0;
148 u16 status;
149
150 /* wait until bus not busy */
151 wait_for_bb ();
152
153 /* one byte only */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100154 writew (1, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000155 /* set slave address */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100156 writew (devaddr, &i2c_base->sa);
wdenkf8062712005-01-09 23:16:25 +0000157 /* no stop bit needed here */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100158 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000159
160 status = wait_for_pin ();
161
162 if (status & I2C_STAT_XRDY) {
163 /* Important: have to use byte access */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100164 writeb (regoffset, &i2c_base->data);
wdenkf8062712005-01-09 23:16:25 +0000165 udelay (20000);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100166 if (readw (&i2c_base->stat) & I2C_STAT_NACK) {
wdenkf8062712005-01-09 23:16:25 +0000167 i2c_error = 1;
168 }
169 } else {
170 i2c_error = 1;
171 }
172
173 if (!i2c_error) {
Steve Sakomane2bdc132010-07-19 20:31:55 -0700174 writew (I2C_CON_EN, &i2c_base->con);
175 while (readw(&i2c_base->stat) &
176 (I2C_STAT_XRDY | I2C_STAT_ARDY)) {
wdenkf8062712005-01-09 23:16:25 +0000177 udelay (10000);
178 /* Have to clear pending interrupt to clear I2C_STAT */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100179 writew (0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000180 }
181
wdenkf8062712005-01-09 23:16:25 +0000182 /* set slave address */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100183 writew (devaddr, &i2c_base->sa);
wdenkf8062712005-01-09 23:16:25 +0000184 /* read one byte from slave */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100185 writew (1, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000186 /* need stop bit here */
Dirk Behme5a6dc872008-11-10 20:15:25 +0100187 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
Dirk Behme7a8f6572009-11-02 20:36:26 +0100188 &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000189
190 status = wait_for_pin ();
191 if (status & I2C_STAT_RRDY) {
Steve Sakoman10acc712010-06-12 06:42:57 -0700192#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
193 defined(CONFIG_OMAP44XX)
Dirk Behme7a8f6572009-11-02 20:36:26 +0100194 *value = readb (&i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100195#else
Dirk Behme7a8f6572009-11-02 20:36:26 +0100196 *value = readw (&i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100197#endif
wdenkf8062712005-01-09 23:16:25 +0000198 udelay (20000);
199 } else {
200 i2c_error = 1;
201 }
202
203 if (!i2c_error) {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100204 writew (I2C_CON_EN, &i2c_base->con);
Steve Sakomane2bdc132010-07-19 20:31:55 -0700205 while (readw (&i2c_base->stat) &
206 (I2C_STAT_RRDY | I2C_STAT_ARDY)) {
wdenkf8062712005-01-09 23:16:25 +0000207 udelay (10000);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100208 writew (0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000209 }
210 }
211 }
212 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100213 writew (0xFFFF, &i2c_base->stat);
214 writew (0, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000215 return i2c_error;
216}
217
218static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
219{
220 int i2c_error = 0;
221 u16 status, stat;
222
223 /* wait until bus not busy */
224 wait_for_bb ();
225
226 /* two bytes */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100227 writew (2, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000228 /* set slave address */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100229 writew (devaddr, &i2c_base->sa);
wdenkf8062712005-01-09 23:16:25 +0000230 /* stop bit needed here */
Dirk Behme5a6dc872008-11-10 20:15:25 +0100231 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
Dirk Behme7a8f6572009-11-02 20:36:26 +0100232 I2C_CON_STP, &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000233
234 /* wait until state change */
235 status = wait_for_pin ();
236
237 if (status & I2C_STAT_XRDY) {
Steve Sakoman10acc712010-06-12 06:42:57 -0700238#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
239 defined(CONFIG_OMAP44XX)
Dirk Behme5648e512008-12-14 09:47:18 +0100240 /* send out 1 byte */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100241 writeb (regoffset, &i2c_base->data);
242 writew (I2C_STAT_XRDY, &i2c_base->stat);
Dirk Behme5648e512008-12-14 09:47:18 +0100243
244 status = wait_for_pin ();
245 if ((status & I2C_STAT_XRDY)) {
246 /* send out next 1 byte */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100247 writeb (value, &i2c_base->data);
248 writew (I2C_STAT_XRDY, &i2c_base->stat);
Dirk Behme5648e512008-12-14 09:47:18 +0100249 } else {
250 i2c_error = 1;
251 }
252#else
wdenkf8062712005-01-09 23:16:25 +0000253 /* send out two bytes */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100254 writew ((value << 8) + regoffset, &i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100255#endif
wdenkf8062712005-01-09 23:16:25 +0000256 /* must have enough delay to allow BB bit to go low */
257 udelay (50000);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100258 if (readw (&i2c_base->stat) & I2C_STAT_NACK) {
wdenkf8062712005-01-09 23:16:25 +0000259 i2c_error = 1;
260 }
261 } else {
262 i2c_error = 1;
263 }
264
265 if (!i2c_error) {
Wolfgang Denke1e46792005-09-25 18:41:04 +0200266 int eout = 200;
267
Dirk Behme7a8f6572009-11-02 20:36:26 +0100268 writew (I2C_CON_EN, &i2c_base->con);
269 while ((stat = readw (&i2c_base->stat)) || (readw (&i2c_base->con) & I2C_CON_MST)) {
wdenkf8062712005-01-09 23:16:25 +0000270 udelay (1000);
271 /* have to read to clear intrrupt */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100272 writew (0xFFFF, &i2c_base->stat);
Wolfgang Denke1e46792005-09-25 18:41:04 +0200273 if(--eout == 0) /* better leave with error than hang */
274 break;
wdenkf8062712005-01-09 23:16:25 +0000275 }
276 }
277 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100278 writew (0xFFFF, &i2c_base->stat);
279 writew (0, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000280 return i2c_error;
281}
282
Wolfgang Denke1e46792005-09-25 18:41:04 +0200283static void flush_fifo(void)
wdenkf8062712005-01-09 23:16:25 +0000284{ u16 stat;
wdenk2e405bf2005-01-10 00:01:04 +0000285
286 /* note: if you try and read data when its not there or ready
287 * you get a bus error
288 */
wdenkf8062712005-01-09 23:16:25 +0000289 while(1){
Dirk Behme7a8f6572009-11-02 20:36:26 +0100290 stat = readw(&i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000291 if(stat == I2C_STAT_RRDY){
Steve Sakoman10acc712010-06-12 06:42:57 -0700292#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
293 defined(CONFIG_OMAP44XX)
Dirk Behme7a8f6572009-11-02 20:36:26 +0100294 readb(&i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100295#else
Dirk Behme7a8f6572009-11-02 20:36:26 +0100296 readw(&i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100297#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100298 writew(I2C_STAT_RRDY,&i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000299 udelay(1000);
300 }else
301 break;
302 }
303}
304
305int i2c_probe (uchar chip)
306{
307 int res = 1; /* default = fail */
308
Dirk Behme7a8f6572009-11-02 20:36:26 +0100309 if (chip == readw (&i2c_base->oa)) {
wdenkf8062712005-01-09 23:16:25 +0000310 return res;
311 }
312
313 /* wait until bus not busy */
314 wait_for_bb ();
315
316 /* try to read one byte */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100317 writew (1, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000318 /* set slave address */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100319 writew (chip, &i2c_base->sa);
wdenkf8062712005-01-09 23:16:25 +0000320 /* stop bit needed here */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100321 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000322 /* enough delay for the NACK bit set */
323 udelay (50000);
324
Dirk Behme7a8f6572009-11-02 20:36:26 +0100325 if (!(readw (&i2c_base->stat) & I2C_STAT_NACK)) {
wdenk2e405bf2005-01-10 00:01:04 +0000326 res = 0; /* success case */
wdenkf8062712005-01-09 23:16:25 +0000327 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100328 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000329 } else {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100330 writew(0xFFFF, &i2c_base->stat); /* failue, clear sources*/
331 writew (readw (&i2c_base->con) | I2C_CON_STP, &i2c_base->con); /* finish up xfer */
wdenkf8062712005-01-09 23:16:25 +0000332 udelay(20000);
333 wait_for_bb ();
334 }
335 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100336 writew (0, &i2c_base->cnt); /* don't allow any more data in...we don't want it.*/
337 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000338 return res;
339}
340
341int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
342{
343 int i;
344
345 if (alen > 1) {
346 printf ("I2C read: addr len %d not supported\n", alen);
347 return 1;
348 }
349
350 if (addr + len > 256) {
351 printf ("I2C read: address out of range\n");
352 return 1;
353 }
354
355 for (i = 0; i < len; i++) {
356 if (i2c_read_byte (chip, addr + i, &buffer[i])) {
357 printf ("I2C read: I/O error\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200358 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenkf8062712005-01-09 23:16:25 +0000359 return 1;
360 }
361 }
362
363 return 0;
364}
365
366int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
367{
368 int i;
369
370 if (alen > 1) {
371 printf ("I2C read: addr len %d not supported\n", alen);
372 return 1;
373 }
374
375 if (addr + len > 256) {
376 printf ("I2C read: address out of range\n");
377 return 1;
378 }
379
380 for (i = 0; i < len; i++) {
381 if (i2c_write_byte (chip, addr + i, buffer[i])) {
382 printf ("I2C read: I/O error\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200383 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenkf8062712005-01-09 23:16:25 +0000384 return 1;
385 }
386 }
387
388 return 0;
389}
390
391static void wait_for_bb (void)
392{
393 int timeout = 10;
394 u16 stat;
395
Dirk Behme7a8f6572009-11-02 20:36:26 +0100396 writew(0xFFFF, &i2c_base->stat); /* clear current interruts...*/
397 while ((stat = readw (&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
398 writew (stat, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000399 udelay (50000);
400 }
401
402 if (timeout <= 0) {
403 printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
Dirk Behme7a8f6572009-11-02 20:36:26 +0100404 readw (&i2c_base->stat));
wdenkf8062712005-01-09 23:16:25 +0000405 }
Dirk Behme7a8f6572009-11-02 20:36:26 +0100406 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
wdenkf8062712005-01-09 23:16:25 +0000407}
408
409static u16 wait_for_pin (void)
410{
411 u16 status;
412 int timeout = 10;
413
414 do {
415 udelay (1000);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100416 status = readw (&i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000417 } while ( !(status &
418 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
419 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
420 I2C_STAT_AL)) && timeout--);
421
422 if (timeout <= 0) {
423 printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
Dirk Behme7a8f6572009-11-02 20:36:26 +0100424 readw (&i2c_base->stat));
425 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000426}
427 return status;
428}
Dirk Behme7a8f6572009-11-02 20:36:26 +0100429
430int i2c_set_bus_num(unsigned int bus)
431{
432 if ((bus < 0) || (bus >= I2C_BUS_MAX)) {
433 printf("Bad bus: %d\n", bus);
434 return -1;
435 }
436
437#if I2C_BUS_MAX==3
438 if (bus == 2)
439 i2c_base = (struct i2c *)I2C_BASE3;
440 else
441#endif
442 if (bus == 1)
443 i2c_base = (struct i2c *)I2C_BASE2;
444 else
445 i2c_base = (struct i2c *)I2C_BASE1;
446
447 current_bus = bus;
448
449 if(!bus_initialized[current_bus])
450 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
451
452 return 0;
453}
Steve Sakoman10acc712010-06-12 06:42:57 -0700454
455int i2c_get_bus_num(void)
456{
457 return (int) current_bus;
458}
459