blob: 1a4c8c9ad2701b550fc1ad0ab2a9fde741902080 [file] [log] [blame]
wdenk8ed96042005-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>
wdenk289f9322005-01-12 00:15:14 +000024
wdenk8ed96042005-01-09 23:16:25 +000025#include <asm/arch/i2c.h>
26#include <asm/io.h>
27
wdenk8ed96042005-01-09 23:16:25 +000028static void wait_for_bb (void);
29static u16 wait_for_pin (void);
Wolfgang Denk49a75812005-09-25 18:41:04 +020030static void flush_fifo(void);
wdenk8ed96042005-01-09 23:16:25 +000031
32void i2c_init (int speed, int slaveadd)
33{
Tom Rix7f79dfb2009-06-28 12:52:27 -050034 int psc, fsscll, fssclh;
35 int hsscll = 0, hssclh = 0;
36 u32 scll, sclh;
37
38 /* Only handle standard, fast and high speeds */
39 if ((speed != OMAP_I2C_STANDARD) &&
40 (speed != OMAP_I2C_FAST_MODE) &&
41 (speed != OMAP_I2C_HIGH_SPEED)) {
42 printf("Error : I2C unsupported speed %d\n", speed);
43 return;
44 }
45
46 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
47 psc -= 1;
48 if (psc < I2C_PSC_MIN) {
49 printf("Error : I2C unsupported prescalar %d\n", psc);
50 return;
51 }
52
53 if (speed == OMAP_I2C_HIGH_SPEED) {
54 /* High speed */
55
56 /* For first phase of HS mode */
57 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK /
58 (2 * OMAP_I2C_FAST_MODE);
59
60 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
61 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
62 if (((fsscll < 0) || (fssclh < 0)) ||
63 ((fsscll > 255) || (fssclh > 255))) {
64 printf("Error : I2C initializing first phase clock\n");
65 return;
66 }
67
68 /* For second phase of HS mode */
69 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
70
71 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
72 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
73 if (((fsscll < 0) || (fssclh < 0)) ||
74 ((fsscll > 255) || (fssclh > 255))) {
75 printf("Error : I2C initializing second phase clock\n");
76 return;
77 }
78
79 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
80 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
81
82 } else {
83 /* Standard and fast speed */
84 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
85
86 fsscll -= I2C_FASTSPEED_SCLL_TRIM;
87 fssclh -= I2C_FASTSPEED_SCLH_TRIM;
88 if (((fsscll < 0) || (fssclh < 0)) ||
89 ((fsscll > 255) || (fssclh > 255))) {
90 printf("Error : I2C initializing clock\n");
91 return;
92 }
93
94 scll = (unsigned int)fsscll;
95 sclh = (unsigned int)fssclh;
96 }
wdenk8ed96042005-01-09 23:16:25 +000097
Dirk Behmee23c7c92008-11-10 20:15:25 +010098 writew(0x2, I2C_SYSC); /* for ES2 after soft reset */
Wolfgang Denk49a75812005-09-25 18:41:04 +020099 udelay(1000);
Dirk Behmee23c7c92008-11-10 20:15:25 +0100100 writew(0x0, I2C_SYSC); /* will probably self clear but */
Wolfgang Denk49a75812005-09-25 18:41:04 +0200101
Dirk Behmee23c7c92008-11-10 20:15:25 +0100102 if (readw (I2C_CON) & I2C_CON_EN) {
103 writew (0, I2C_CON);
wdenk8ed96042005-01-09 23:16:25 +0000104 udelay (50000);
105 }
106
Tom Rix7f79dfb2009-06-28 12:52:27 -0500107 writew(psc, I2C_PSC);
108 writew(scll, I2C_SCLL);
109 writew(sclh, I2C_SCLH);
110
wdenk8ed96042005-01-09 23:16:25 +0000111 /* own address */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100112 writew (slaveadd, I2C_OA);
113 writew (I2C_CON_EN, I2C_CON);
Wolfgang Denk49a75812005-09-25 18:41:04 +0200114
wdenk8ed96042005-01-09 23:16:25 +0000115 /* have to enable intrrupts or OMAP i2c module doesn't work */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100116 writew (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
117 I2C_IE_NACK_IE | I2C_IE_AL_IE, I2C_IE);
wdenk8ed96042005-01-09 23:16:25 +0000118 udelay (1000);
Wolfgang Denk49a75812005-09-25 18:41:04 +0200119 flush_fifo();
Dirk Behmee23c7c92008-11-10 20:15:25 +0100120 writew (0xFFFF, I2C_STAT);
121 writew (0, I2C_CNT);
wdenk8ed96042005-01-09 23:16:25 +0000122}
123
124static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
125{
126 int i2c_error = 0;
127 u16 status;
128
129 /* wait until bus not busy */
130 wait_for_bb ();
131
132 /* one byte only */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100133 writew (1, I2C_CNT);
wdenk8ed96042005-01-09 23:16:25 +0000134 /* set slave address */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100135 writew (devaddr, I2C_SA);
wdenk8ed96042005-01-09 23:16:25 +0000136 /* no stop bit needed here */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100137 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, I2C_CON);
wdenk8ed96042005-01-09 23:16:25 +0000138
139 status = wait_for_pin ();
140
141 if (status & I2C_STAT_XRDY) {
142 /* Important: have to use byte access */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100143 writeb (regoffset, I2C_DATA);
wdenk8ed96042005-01-09 23:16:25 +0000144 udelay (20000);
Dirk Behmee23c7c92008-11-10 20:15:25 +0100145 if (readw (I2C_STAT) & I2C_STAT_NACK) {
wdenk8ed96042005-01-09 23:16:25 +0000146 i2c_error = 1;
147 }
148 } else {
149 i2c_error = 1;
150 }
151
152 if (!i2c_error) {
153 /* free bus, otherwise we can't use a combined transction */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100154 writew (0, I2C_CON);
155 while (readw (I2C_STAT) || (readw (I2C_CON) & I2C_CON_MST)) {
wdenk8ed96042005-01-09 23:16:25 +0000156 udelay (10000);
157 /* Have to clear pending interrupt to clear I2C_STAT */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100158 writew (0xFFFF, I2C_STAT);
wdenk8ed96042005-01-09 23:16:25 +0000159 }
160
161 wait_for_bb ();
162 /* set slave address */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100163 writew (devaddr, I2C_SA);
wdenk8ed96042005-01-09 23:16:25 +0000164 /* read one byte from slave */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100165 writew (1, I2C_CNT);
wdenk8ed96042005-01-09 23:16:25 +0000166 /* need stop bit here */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100167 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
168 I2C_CON);
wdenk8ed96042005-01-09 23:16:25 +0000169
170 status = wait_for_pin ();
171 if (status & I2C_STAT_RRDY) {
Dirk Behme7d264c12008-12-14 09:47:18 +0100172#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
173 *value = readb (I2C_DATA);
174#else
Dirk Behmee23c7c92008-11-10 20:15:25 +0100175 *value = readw (I2C_DATA);
Dirk Behme7d264c12008-12-14 09:47:18 +0100176#endif
wdenk8ed96042005-01-09 23:16:25 +0000177 udelay (20000);
178 } else {
179 i2c_error = 1;
180 }
181
182 if (!i2c_error) {
Dirk Behmee23c7c92008-11-10 20:15:25 +0100183 writew (I2C_CON_EN, I2C_CON);
184 while (readw (I2C_STAT)
185 || (readw (I2C_CON) & I2C_CON_MST)) {
wdenk8ed96042005-01-09 23:16:25 +0000186 udelay (10000);
Dirk Behmee23c7c92008-11-10 20:15:25 +0100187 writew (0xFFFF, I2C_STAT);
wdenk8ed96042005-01-09 23:16:25 +0000188 }
189 }
190 }
191 flush_fifo();
Dirk Behmee23c7c92008-11-10 20:15:25 +0100192 writew (0xFFFF, I2C_STAT);
193 writew (0, I2C_CNT);
wdenk8ed96042005-01-09 23:16:25 +0000194 return i2c_error;
195}
196
197static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
198{
199 int i2c_error = 0;
200 u16 status, stat;
201
202 /* wait until bus not busy */
203 wait_for_bb ();
204
205 /* two bytes */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100206 writew (2, I2C_CNT);
wdenk8ed96042005-01-09 23:16:25 +0000207 /* set slave address */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100208 writew (devaddr, I2C_SA);
wdenk8ed96042005-01-09 23:16:25 +0000209 /* stop bit needed here */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100210 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
211 I2C_CON_STP, I2C_CON);
wdenk8ed96042005-01-09 23:16:25 +0000212
213 /* wait until state change */
214 status = wait_for_pin ();
215
216 if (status & I2C_STAT_XRDY) {
Dirk Behme7d264c12008-12-14 09:47:18 +0100217#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
218 /* send out 1 byte */
219 writeb (regoffset, I2C_DATA);
220 writew (I2C_STAT_XRDY, I2C_STAT);
221
222 status = wait_for_pin ();
223 if ((status & I2C_STAT_XRDY)) {
224 /* send out next 1 byte */
225 writeb (value, I2C_DATA);
226 writew (I2C_STAT_XRDY, I2C_STAT);
227 } else {
228 i2c_error = 1;
229 }
230#else
wdenk8ed96042005-01-09 23:16:25 +0000231 /* send out two bytes */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100232 writew ((value << 8) + regoffset, I2C_DATA);
Dirk Behme7d264c12008-12-14 09:47:18 +0100233#endif
wdenk8ed96042005-01-09 23:16:25 +0000234 /* must have enough delay to allow BB bit to go low */
235 udelay (50000);
Dirk Behmee23c7c92008-11-10 20:15:25 +0100236 if (readw (I2C_STAT) & I2C_STAT_NACK) {
wdenk8ed96042005-01-09 23:16:25 +0000237 i2c_error = 1;
238 }
239 } else {
240 i2c_error = 1;
241 }
242
243 if (!i2c_error) {
Wolfgang Denk49a75812005-09-25 18:41:04 +0200244 int eout = 200;
245
Dirk Behmee23c7c92008-11-10 20:15:25 +0100246 writew (I2C_CON_EN, I2C_CON);
247 while ((stat = readw (I2C_STAT)) || (readw (I2C_CON) & I2C_CON_MST)) {
wdenk8ed96042005-01-09 23:16:25 +0000248 udelay (1000);
249 /* have to read to clear intrrupt */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100250 writew (0xFFFF, I2C_STAT);
Wolfgang Denk49a75812005-09-25 18:41:04 +0200251 if(--eout == 0) /* better leave with error than hang */
252 break;
wdenk8ed96042005-01-09 23:16:25 +0000253 }
254 }
255 flush_fifo();
Dirk Behmee23c7c92008-11-10 20:15:25 +0100256 writew (0xFFFF, I2C_STAT);
257 writew (0, I2C_CNT);
wdenk8ed96042005-01-09 23:16:25 +0000258 return i2c_error;
259}
260
Wolfgang Denk49a75812005-09-25 18:41:04 +0200261static void flush_fifo(void)
wdenk8ed96042005-01-09 23:16:25 +0000262{ u16 stat;
wdenk082acfd2005-01-10 00:01:04 +0000263
264 /* note: if you try and read data when its not there or ready
265 * you get a bus error
266 */
wdenk8ed96042005-01-09 23:16:25 +0000267 while(1){
Dirk Behmee23c7c92008-11-10 20:15:25 +0100268 stat = readw(I2C_STAT);
wdenk8ed96042005-01-09 23:16:25 +0000269 if(stat == I2C_STAT_RRDY){
Dirk Behme7d264c12008-12-14 09:47:18 +0100270#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
271 readb(I2C_DATA);
272#else
Dirk Behmee23c7c92008-11-10 20:15:25 +0100273 readw(I2C_DATA);
Dirk Behme7d264c12008-12-14 09:47:18 +0100274#endif
Dirk Behmee23c7c92008-11-10 20:15:25 +0100275 writew(I2C_STAT_RRDY,I2C_STAT);
wdenk8ed96042005-01-09 23:16:25 +0000276 udelay(1000);
277 }else
278 break;
279 }
280}
281
282int i2c_probe (uchar chip)
283{
284 int res = 1; /* default = fail */
285
Dirk Behmee23c7c92008-11-10 20:15:25 +0100286 if (chip == readw (I2C_OA)) {
wdenk8ed96042005-01-09 23:16:25 +0000287 return res;
288 }
289
290 /* wait until bus not busy */
291 wait_for_bb ();
292
293 /* try to read one byte */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100294 writew (1, I2C_CNT);
wdenk8ed96042005-01-09 23:16:25 +0000295 /* set slave address */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100296 writew (chip, I2C_SA);
wdenk8ed96042005-01-09 23:16:25 +0000297 /* stop bit needed here */
Dirk Behmee23c7c92008-11-10 20:15:25 +0100298 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON);
wdenk8ed96042005-01-09 23:16:25 +0000299 /* enough delay for the NACK bit set */
300 udelay (50000);
301
Dirk Behmee23c7c92008-11-10 20:15:25 +0100302 if (!(readw (I2C_STAT) & I2C_STAT_NACK)) {
wdenk082acfd2005-01-10 00:01:04 +0000303 res = 0; /* success case */
wdenk8ed96042005-01-09 23:16:25 +0000304 flush_fifo();
Dirk Behmee23c7c92008-11-10 20:15:25 +0100305 writew(0xFFFF, I2C_STAT);
wdenk8ed96042005-01-09 23:16:25 +0000306 } else {
Dirk Behmee23c7c92008-11-10 20:15:25 +0100307 writew(0xFFFF, I2C_STAT); /* failue, clear sources*/
308 writew (readw (I2C_CON) | I2C_CON_STP, I2C_CON); /* finish up xfer */
wdenk8ed96042005-01-09 23:16:25 +0000309 udelay(20000);
310 wait_for_bb ();
311 }
312 flush_fifo();
Dirk Behmee23c7c92008-11-10 20:15:25 +0100313 writew (0, I2C_CNT); /* don't allow any more data in...we don't want it.*/
314 writew(0xFFFF, I2C_STAT);
wdenk8ed96042005-01-09 23:16:25 +0000315 return res;
316}
317
318int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
319{
320 int i;
321
322 if (alen > 1) {
323 printf ("I2C read: addr len %d not supported\n", alen);
324 return 1;
325 }
326
327 if (addr + len > 256) {
328 printf ("I2C read: address out of range\n");
329 return 1;
330 }
331
332 for (i = 0; i < len; i++) {
333 if (i2c_read_byte (chip, addr + i, &buffer[i])) {
334 printf ("I2C read: I/O error\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200335 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenk8ed96042005-01-09 23:16:25 +0000336 return 1;
337 }
338 }
339
340 return 0;
341}
342
343int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
344{
345 int i;
346
347 if (alen > 1) {
348 printf ("I2C read: addr len %d not supported\n", alen);
349 return 1;
350 }
351
352 if (addr + len > 256) {
353 printf ("I2C read: address out of range\n");
354 return 1;
355 }
356
357 for (i = 0; i < len; i++) {
358 if (i2c_write_byte (chip, addr + i, buffer[i])) {
359 printf ("I2C read: I/O error\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200360 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenk8ed96042005-01-09 23:16:25 +0000361 return 1;
362 }
363 }
364
365 return 0;
366}
367
368static void wait_for_bb (void)
369{
370 int timeout = 10;
371 u16 stat;
372
Dirk Behmee23c7c92008-11-10 20:15:25 +0100373 writew(0xFFFF, I2C_STAT); /* clear current interruts...*/
374 while ((stat = readw (I2C_STAT) & I2C_STAT_BB) && timeout--) {
375 writew (stat, I2C_STAT);
wdenk8ed96042005-01-09 23:16:25 +0000376 udelay (50000);
377 }
378
379 if (timeout <= 0) {
380 printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
Dirk Behmee23c7c92008-11-10 20:15:25 +0100381 readw (I2C_STAT));
wdenk8ed96042005-01-09 23:16:25 +0000382 }
Dirk Behmee23c7c92008-11-10 20:15:25 +0100383 writew(0xFFFF, I2C_STAT); /* clear delayed stuff*/
wdenk8ed96042005-01-09 23:16:25 +0000384}
385
386static u16 wait_for_pin (void)
387{
388 u16 status;
389 int timeout = 10;
390
391 do {
392 udelay (1000);
Dirk Behmee23c7c92008-11-10 20:15:25 +0100393 status = readw (I2C_STAT);
wdenk8ed96042005-01-09 23:16:25 +0000394 } while ( !(status &
395 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
396 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
397 I2C_STAT_AL)) && timeout--);
398
399 if (timeout <= 0) {
400 printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
Dirk Behmee23c7c92008-11-10 20:15:25 +0100401 readw (I2C_STAT));
402 writew(0xFFFF, I2C_STAT);
wdenk8ed96042005-01-09 23:16:25 +0000403}
404 return status;
405}