blob: 3256133dc2ebbf897524af0d48411a0a44c55252 [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
wdenkf8062712005-01-09 23:16:25 +000030static void wait_for_bb (void);
31static u16 wait_for_pin (void);
Wolfgang Denke1e46792005-09-25 18:41:04 +020032static void flush_fifo(void);
wdenkf8062712005-01-09 23:16:25 +000033
Dirk Behme7a8f6572009-11-02 20:36:26 +010034static struct i2c *i2c_base = (struct i2c *)I2C_DEFAULT_BASE;
35
36static unsigned int bus_initialized[I2C_BUS_MAX];
37static unsigned int current_bus;
38
wdenkf8062712005-01-09 23:16:25 +000039void i2c_init (int speed, int slaveadd)
40{
Tom Rix03b2a742009-06-28 12:52:27 -050041 int psc, fsscll, fssclh;
42 int hsscll = 0, hssclh = 0;
43 u32 scll, sclh;
44
45 /* Only handle standard, fast and high speeds */
46 if ((speed != OMAP_I2C_STANDARD) &&
47 (speed != OMAP_I2C_FAST_MODE) &&
48 (speed != OMAP_I2C_HIGH_SPEED)) {
49 printf("Error : I2C unsupported speed %d\n", speed);
50 return;
51 }
52
53 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
54 psc -= 1;
55 if (psc < I2C_PSC_MIN) {
56 printf("Error : I2C unsupported prescalar %d\n", psc);
57 return;
58 }
59
60 if (speed == OMAP_I2C_HIGH_SPEED) {
61 /* High speed */
62
63 /* For first phase of HS mode */
64 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK /
65 (2 * OMAP_I2C_FAST_MODE);
66
67 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
68 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
69 if (((fsscll < 0) || (fssclh < 0)) ||
70 ((fsscll > 255) || (fssclh > 255))) {
71 printf("Error : I2C initializing first phase clock\n");
72 return;
73 }
74
75 /* For second phase of HS mode */
76 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
77
78 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
79 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
80 if (((fsscll < 0) || (fssclh < 0)) ||
81 ((fsscll > 255) || (fssclh > 255))) {
82 printf("Error : I2C initializing second phase clock\n");
83 return;
84 }
85
86 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
87 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
88
89 } else {
90 /* Standard and fast speed */
91 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
92
93 fsscll -= I2C_FASTSPEED_SCLL_TRIM;
94 fssclh -= I2C_FASTSPEED_SCLH_TRIM;
95 if (((fsscll < 0) || (fssclh < 0)) ||
96 ((fsscll > 255) || (fssclh > 255))) {
97 printf("Error : I2C initializing clock\n");
98 return;
99 }
100
101 scll = (unsigned int)fsscll;
102 sclh = (unsigned int)fssclh;
103 }
wdenkf8062712005-01-09 23:16:25 +0000104
Dirk Behme7a8f6572009-11-02 20:36:26 +0100105 writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
Wolfgang Denke1e46792005-09-25 18:41:04 +0200106 udelay(1000);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100107 writew(0x0, &i2c_base->sysc); /* will probably self clear but */
Wolfgang Denke1e46792005-09-25 18:41:04 +0200108
Dirk Behme7a8f6572009-11-02 20:36:26 +0100109 if (readw (&i2c_base->con) & I2C_CON_EN) {
110 writew (0, &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000111 udelay (50000);
112 }
113
Dirk Behme7a8f6572009-11-02 20:36:26 +0100114 writew(psc, &i2c_base->psc);
115 writew(scll, &i2c_base->scll);
116 writew(sclh, &i2c_base->sclh);
Tom Rix03b2a742009-06-28 12:52:27 -0500117
wdenkf8062712005-01-09 23:16:25 +0000118 /* own address */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100119 writew (slaveadd, &i2c_base->oa);
120 writew (I2C_CON_EN, &i2c_base->con);
Wolfgang Denke1e46792005-09-25 18:41:04 +0200121
wdenkf8062712005-01-09 23:16:25 +0000122 /* have to enable intrrupts or OMAP i2c module doesn't work */
Dirk Behme5a6dc872008-11-10 20:15:25 +0100123 writew (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
Dirk Behme7a8f6572009-11-02 20:36:26 +0100124 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
wdenkf8062712005-01-09 23:16:25 +0000125 udelay (1000);
Wolfgang Denke1e46792005-09-25 18:41:04 +0200126 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100127 writew (0xFFFF, &i2c_base->stat);
128 writew (0, &i2c_base->cnt);
129
130 bus_initialized[current_bus] = 1;
wdenkf8062712005-01-09 23:16:25 +0000131}
132
133static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
134{
135 int i2c_error = 0;
136 u16 status;
137
138 /* wait until bus not busy */
139 wait_for_bb ();
140
141 /* one byte only */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100142 writew (1, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000143 /* set slave address */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100144 writew (devaddr, &i2c_base->sa);
wdenkf8062712005-01-09 23:16:25 +0000145 /* no stop bit needed here */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100146 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000147
148 status = wait_for_pin ();
149
150 if (status & I2C_STAT_XRDY) {
151 /* Important: have to use byte access */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100152 writeb (regoffset, &i2c_base->data);
wdenkf8062712005-01-09 23:16:25 +0000153 udelay (20000);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100154 if (readw (&i2c_base->stat) & I2C_STAT_NACK) {
wdenkf8062712005-01-09 23:16:25 +0000155 i2c_error = 1;
156 }
157 } else {
158 i2c_error = 1;
159 }
160
161 if (!i2c_error) {
162 /* free bus, otherwise we can't use a combined transction */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100163 writew (0, &i2c_base->con);
164 while (readw (&i2c_base->stat) || (readw (&i2c_base->con) & I2C_CON_MST)) {
wdenkf8062712005-01-09 23:16:25 +0000165 udelay (10000);
166 /* Have to clear pending interrupt to clear I2C_STAT */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100167 writew (0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000168 }
169
170 wait_for_bb ();
171 /* set slave address */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100172 writew (devaddr, &i2c_base->sa);
wdenkf8062712005-01-09 23:16:25 +0000173 /* read one byte from slave */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100174 writew (1, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000175 /* need stop bit here */
Dirk Behme5a6dc872008-11-10 20:15:25 +0100176 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
Dirk Behme7a8f6572009-11-02 20:36:26 +0100177 &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000178
179 status = wait_for_pin ();
180 if (status & I2C_STAT_RRDY) {
Steve Sakoman10acc712010-06-12 06:42:57 -0700181#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
182 defined(CONFIG_OMAP44XX)
Dirk Behme7a8f6572009-11-02 20:36:26 +0100183 *value = readb (&i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100184#else
Dirk Behme7a8f6572009-11-02 20:36:26 +0100185 *value = readw (&i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100186#endif
wdenkf8062712005-01-09 23:16:25 +0000187 udelay (20000);
188 } else {
189 i2c_error = 1;
190 }
191
192 if (!i2c_error) {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100193 writew (I2C_CON_EN, &i2c_base->con);
194 while (readw (&i2c_base->stat)
195 || (readw (&i2c_base->con) & I2C_CON_MST)) {
wdenkf8062712005-01-09 23:16:25 +0000196 udelay (10000);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100197 writew (0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000198 }
199 }
200 }
201 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100202 writew (0xFFFF, &i2c_base->stat);
203 writew (0, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000204 return i2c_error;
205}
206
207static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
208{
209 int i2c_error = 0;
210 u16 status, stat;
211
212 /* wait until bus not busy */
213 wait_for_bb ();
214
215 /* two bytes */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100216 writew (2, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000217 /* set slave address */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100218 writew (devaddr, &i2c_base->sa);
wdenkf8062712005-01-09 23:16:25 +0000219 /* stop bit needed here */
Dirk Behme5a6dc872008-11-10 20:15:25 +0100220 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
Dirk Behme7a8f6572009-11-02 20:36:26 +0100221 I2C_CON_STP, &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000222
223 /* wait until state change */
224 status = wait_for_pin ();
225
226 if (status & I2C_STAT_XRDY) {
Steve Sakoman10acc712010-06-12 06:42:57 -0700227#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
228 defined(CONFIG_OMAP44XX)
Dirk Behme5648e512008-12-14 09:47:18 +0100229 /* send out 1 byte */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100230 writeb (regoffset, &i2c_base->data);
231 writew (I2C_STAT_XRDY, &i2c_base->stat);
Dirk Behme5648e512008-12-14 09:47:18 +0100232
233 status = wait_for_pin ();
234 if ((status & I2C_STAT_XRDY)) {
235 /* send out next 1 byte */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100236 writeb (value, &i2c_base->data);
237 writew (I2C_STAT_XRDY, &i2c_base->stat);
Dirk Behme5648e512008-12-14 09:47:18 +0100238 } else {
239 i2c_error = 1;
240 }
241#else
wdenkf8062712005-01-09 23:16:25 +0000242 /* send out two bytes */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100243 writew ((value << 8) + regoffset, &i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100244#endif
wdenkf8062712005-01-09 23:16:25 +0000245 /* must have enough delay to allow BB bit to go low */
246 udelay (50000);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100247 if (readw (&i2c_base->stat) & I2C_STAT_NACK) {
wdenkf8062712005-01-09 23:16:25 +0000248 i2c_error = 1;
249 }
250 } else {
251 i2c_error = 1;
252 }
253
254 if (!i2c_error) {
Wolfgang Denke1e46792005-09-25 18:41:04 +0200255 int eout = 200;
256
Dirk Behme7a8f6572009-11-02 20:36:26 +0100257 writew (I2C_CON_EN, &i2c_base->con);
258 while ((stat = readw (&i2c_base->stat)) || (readw (&i2c_base->con) & I2C_CON_MST)) {
wdenkf8062712005-01-09 23:16:25 +0000259 udelay (1000);
260 /* have to read to clear intrrupt */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100261 writew (0xFFFF, &i2c_base->stat);
Wolfgang Denke1e46792005-09-25 18:41:04 +0200262 if(--eout == 0) /* better leave with error than hang */
263 break;
wdenkf8062712005-01-09 23:16:25 +0000264 }
265 }
266 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100267 writew (0xFFFF, &i2c_base->stat);
268 writew (0, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000269 return i2c_error;
270}
271
Wolfgang Denke1e46792005-09-25 18:41:04 +0200272static void flush_fifo(void)
wdenkf8062712005-01-09 23:16:25 +0000273{ u16 stat;
wdenk2e405bf2005-01-10 00:01:04 +0000274
275 /* note: if you try and read data when its not there or ready
276 * you get a bus error
277 */
wdenkf8062712005-01-09 23:16:25 +0000278 while(1){
Dirk Behme7a8f6572009-11-02 20:36:26 +0100279 stat = readw(&i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000280 if(stat == I2C_STAT_RRDY){
Steve Sakoman10acc712010-06-12 06:42:57 -0700281#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
282 defined(CONFIG_OMAP44XX)
Dirk Behme7a8f6572009-11-02 20:36:26 +0100283 readb(&i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100284#else
Dirk Behme7a8f6572009-11-02 20:36:26 +0100285 readw(&i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100286#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100287 writew(I2C_STAT_RRDY,&i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000288 udelay(1000);
289 }else
290 break;
291 }
292}
293
294int i2c_probe (uchar chip)
295{
296 int res = 1; /* default = fail */
297
Dirk Behme7a8f6572009-11-02 20:36:26 +0100298 if (chip == readw (&i2c_base->oa)) {
wdenkf8062712005-01-09 23:16:25 +0000299 return res;
300 }
301
302 /* wait until bus not busy */
303 wait_for_bb ();
304
305 /* try to read one byte */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100306 writew (1, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000307 /* set slave address */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100308 writew (chip, &i2c_base->sa);
wdenkf8062712005-01-09 23:16:25 +0000309 /* stop bit needed here */
Dirk Behme7a8f6572009-11-02 20:36:26 +0100310 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000311 /* enough delay for the NACK bit set */
312 udelay (50000);
313
Dirk Behme7a8f6572009-11-02 20:36:26 +0100314 if (!(readw (&i2c_base->stat) & I2C_STAT_NACK)) {
wdenk2e405bf2005-01-10 00:01:04 +0000315 res = 0; /* success case */
wdenkf8062712005-01-09 23:16:25 +0000316 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100317 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000318 } else {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100319 writew(0xFFFF, &i2c_base->stat); /* failue, clear sources*/
320 writew (readw (&i2c_base->con) | I2C_CON_STP, &i2c_base->con); /* finish up xfer */
wdenkf8062712005-01-09 23:16:25 +0000321 udelay(20000);
322 wait_for_bb ();
323 }
324 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100325 writew (0, &i2c_base->cnt); /* don't allow any more data in...we don't want it.*/
326 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000327 return res;
328}
329
330int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
331{
332 int i;
333
334 if (alen > 1) {
335 printf ("I2C read: addr len %d not supported\n", alen);
336 return 1;
337 }
338
339 if (addr + len > 256) {
340 printf ("I2C read: address out of range\n");
341 return 1;
342 }
343
344 for (i = 0; i < len; i++) {
345 if (i2c_read_byte (chip, addr + i, &buffer[i])) {
346 printf ("I2C read: I/O error\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200347 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenkf8062712005-01-09 23:16:25 +0000348 return 1;
349 }
350 }
351
352 return 0;
353}
354
355int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
356{
357 int i;
358
359 if (alen > 1) {
360 printf ("I2C read: addr len %d not supported\n", alen);
361 return 1;
362 }
363
364 if (addr + len > 256) {
365 printf ("I2C read: address out of range\n");
366 return 1;
367 }
368
369 for (i = 0; i < len; i++) {
370 if (i2c_write_byte (chip, addr + i, buffer[i])) {
371 printf ("I2C read: I/O error\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200372 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenkf8062712005-01-09 23:16:25 +0000373 return 1;
374 }
375 }
376
377 return 0;
378}
379
380static void wait_for_bb (void)
381{
382 int timeout = 10;
383 u16 stat;
384
Dirk Behme7a8f6572009-11-02 20:36:26 +0100385 writew(0xFFFF, &i2c_base->stat); /* clear current interruts...*/
386 while ((stat = readw (&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
387 writew (stat, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000388 udelay (50000);
389 }
390
391 if (timeout <= 0) {
392 printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
Dirk Behme7a8f6572009-11-02 20:36:26 +0100393 readw (&i2c_base->stat));
wdenkf8062712005-01-09 23:16:25 +0000394 }
Dirk Behme7a8f6572009-11-02 20:36:26 +0100395 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
wdenkf8062712005-01-09 23:16:25 +0000396}
397
398static u16 wait_for_pin (void)
399{
400 u16 status;
401 int timeout = 10;
402
403 do {
404 udelay (1000);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100405 status = readw (&i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000406 } while ( !(status &
407 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
408 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
409 I2C_STAT_AL)) && timeout--);
410
411 if (timeout <= 0) {
412 printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
Dirk Behme7a8f6572009-11-02 20:36:26 +0100413 readw (&i2c_base->stat));
414 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000415}
416 return status;
417}
Dirk Behme7a8f6572009-11-02 20:36:26 +0100418
419int i2c_set_bus_num(unsigned int bus)
420{
421 if ((bus < 0) || (bus >= I2C_BUS_MAX)) {
422 printf("Bad bus: %d\n", bus);
423 return -1;
424 }
425
426#if I2C_BUS_MAX==3
427 if (bus == 2)
428 i2c_base = (struct i2c *)I2C_BASE3;
429 else
430#endif
431 if (bus == 1)
432 i2c_base = (struct i2c *)I2C_BASE2;
433 else
434 i2c_base = (struct i2c *)I2C_BASE1;
435
436 current_bus = bus;
437
438 if(!bus_initialized[current_bus])
439 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
440
441 return 0;
442}
Steve Sakoman10acc712010-06-12 06:42:57 -0700443
444int i2c_get_bus_num(void)
445{
446 return (int) current_bus;
447}
448