recognize another memory MCP chip
[pandora-x-loader.git] / drivers / omap24xx_i2c.c
1 /*
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>
24
25 #if defined(CONFIG_DRIVER_OMAP24XX_I2C) || defined(CONFIG_DRIVER_OMAP34XX_I2C)
26
27 #include <asm/arch/i2c.h>
28 #include <asm/io.h>
29
30 #define inb(a) __raw_readb(a)
31 #define outb(a,v) __raw_writeb(a,v)
32 #define inw(a) __raw_readw(a)
33 #define outw(a,v) __raw_writew(a,v)
34
35 static void wait_for_bb (void);
36 static u16 wait_for_pin (void);
37 static void flush_fifo(void);
38
39 void i2c_init (int speed, int slaveadd)
40 {
41         u16 scl;
42
43         outw(0x2, I2C_SYSC); /* for ES2 after soft reset */
44         udelay(1000);
45         outw(0x0, I2C_SYSC); /* will probably self clear but */
46
47         if (inw (I2C_CON) & I2C_CON_EN) {
48                 outw (0, I2C_CON);
49                 udelay (50000);
50         }
51
52         /* 12Mhz I2C module clock */
53         outw (0, I2C_PSC);
54         speed = speed/1000;                 /* 100 or 400 */
55         scl = ((12000/(speed*2)) - 7);  /* use 7 when PSC = 0 */
56         outw (scl, I2C_SCLL);
57         outw (scl, I2C_SCLH);
58         /* own address */
59         outw (slaveadd, I2C_OA);
60         outw (I2C_CON_EN, I2C_CON);
61
62         /* have to enable intrrupts or OMAP i2c module doesn't work */
63         outw (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
64               I2C_IE_NACK_IE | I2C_IE_AL_IE, I2C_IE);
65         udelay (1000);
66         flush_fifo();
67         outw (0xFFFF, I2C_STAT);
68         outw (0, I2C_CNT);
69 }
70
71 static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
72 {
73         int i2c_error = 0;
74         u16 status;
75
76         /* wait until bus not busy */
77         wait_for_bb ();
78
79         /* one byte only */
80         outw (1, I2C_CNT);
81         /* set slave address */
82         outw (devaddr, I2C_SA);
83         /* no stop bit needed here */
84         outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, I2C_CON);
85
86         status = wait_for_pin ();
87
88         if (status & I2C_STAT_XRDY) {
89                 /* Important: have to use byte access */
90                 *(volatile u8 *) (I2C_DATA) = regoffset;
91                 udelay (20000);
92                 if (inw (I2C_STAT) & I2C_STAT_NACK) {
93                         i2c_error = 1;
94                 }
95         } else {
96                 i2c_error = 1;
97         }
98
99         if (!i2c_error) {
100                 /* free bus, otherwise we can't use a combined transction */
101                 outw (0, I2C_CON);
102                 while (inw (I2C_STAT) || (inw (I2C_CON) & I2C_CON_MST)) {
103                         udelay (10000);
104                         /* Have to clear pending interrupt to clear I2C_STAT */
105                         outw (0xFFFF, I2C_STAT);
106                 }
107
108                 wait_for_bb ();
109                 /* set slave address */
110                 outw (devaddr, I2C_SA);
111                 /* read one byte from slave */
112                 outw (1, I2C_CNT);
113                 /* need stop bit here */
114                 outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
115                       I2C_CON);
116
117                 status = wait_for_pin ();
118                 if (status & I2C_STAT_RRDY) {
119 #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
120                         *value = inb(I2C_DATA);
121 #else
122                         *value = inw(I2C_DATA);
123 #endif
124                         udelay (20000);
125                 } else {
126                         i2c_error = 1;
127                 }
128
129                 if (!i2c_error) {
130                         outw (I2C_CON_EN, I2C_CON);
131                         while (inw (I2C_STAT)
132                                || (inw (I2C_CON) & I2C_CON_MST)) {
133                                 udelay (10000);
134                                 outw (0xFFFF, I2C_STAT);
135                         }
136                 }
137         }
138         flush_fifo();
139         outw (0xFFFF, I2C_STAT);
140         outw (0, I2C_CNT);
141         return i2c_error;
142 }
143
144 static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
145 {
146         int i2c_error = 0;
147         u16 status, stat;
148
149         /* wait until bus not busy */
150         wait_for_bb ();
151
152         /* two bytes */
153         outw (2, I2C_CNT);
154         /* set slave address */
155         outw (devaddr, I2C_SA);
156         /* stop bit needed here */
157         outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
158               I2C_CON_STP, I2C_CON);
159
160         /* wait until state change */
161         status = wait_for_pin ();
162
163         if (status & I2C_STAT_XRDY) {
164 #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
165                 /* send out 1 byte */
166                 outb(regoffset, I2C_DATA);
167                 outw(I2C_STAT_XRDY, I2C_STAT);
168                 status = wait_for_pin();
169                 if ((status & I2C_STAT_XRDY)) {
170                         /* send out next 1 byte */
171                         outb(value, I2C_DATA);
172                         outw(I2C_STAT_XRDY, I2C_STAT);
173                 } else {
174                         i2c_error = 1;
175                 }
176 #else
177                 /* send out two bytes */
178                 outw ((value << 8) + regoffset, I2C_DATA);
179 #endif
180
181                 /* must have enough delay to allow BB bit to go low */
182                 udelay (50000);
183                 if (inw (I2C_STAT) & I2C_STAT_NACK) {
184                         i2c_error = 1;
185                 }
186         } else {
187                 i2c_error = 1;
188         }
189
190         if (!i2c_error) {
191                 int eout = 200;
192
193                 outw (I2C_CON_EN, I2C_CON);
194                 while ((stat = inw (I2C_STAT)) || (inw (I2C_CON) & I2C_CON_MST)) {
195                         udelay (1000);
196                         /* have to read to clear intrrupt */
197                         outw (0xFFFF, I2C_STAT);
198                         if(--eout == 0) /* better leave with error than hang */
199                                 break;
200                 }
201         }
202         flush_fifo();
203         outw (0xFFFF, I2C_STAT);
204         outw (0, I2C_CNT);
205         return i2c_error;
206 }
207
208 static void flush_fifo(void)
209 {       u16 stat;
210
211         /* note: if you try and read data when its not there or ready
212          * you get a bus error
213          */
214         while(1){
215                 stat = inw(I2C_STAT);
216                 if(stat == I2C_STAT_RRDY){
217 #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
218                         inb(I2C_DATA);
219 #else
220                         inw(I2C_DATA);
221 #endif
222                         outw(I2C_STAT_RRDY,I2C_STAT);
223                         udelay(1000);
224                 }else
225                         break;
226         }
227 }
228
229 int i2c_probe (uchar chip)
230 {
231         int res = 1; /* default = fail */
232
233         if (chip == inw (I2C_OA)) {
234                 return res;
235         }
236
237         /* wait until bus not busy */
238         wait_for_bb ();
239
240         /* try to read one byte */
241         outw (1, I2C_CNT);
242         /* set slave address */
243         outw (chip, I2C_SA);
244         /* stop bit needed here */
245         outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON);
246         /* enough delay for the NACK bit set */
247         udelay (50000);
248
249         if (!(inw (I2C_STAT) & I2C_STAT_NACK)) {
250                 res = 0;      /* success case */
251                 flush_fifo();
252                 outw(0xFFFF, I2C_STAT);
253         } else {
254                 outw(0xFFFF, I2C_STAT);  /* failue, clear sources*/
255                 outw (inw (I2C_CON) | I2C_CON_STP, I2C_CON); /* finish up xfer */
256                 udelay(20000);
257                 wait_for_bb ();
258         }
259         flush_fifo();
260         outw (0, I2C_CNT); /* don't allow any more data in...we don't want it.*/
261         outw(0xFFFF, I2C_STAT);
262         return res;
263 }
264
265 int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
266 {
267         int i;
268
269         if (alen > 1) {
270                 printf ("I2C read: addr len %d not supported\n", alen);
271                 return 1;
272         }
273
274         if (addr + len > 256) {
275                 printf ("I2C read: address out of range\n");
276                 return 1;
277         }
278
279         for (i = 0; i < len; i++) {
280                 if (i2c_read_byte (chip, addr + i, &buffer[i])) {
281                         printf ("I2C read: I/O error\n");
282                         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
283                         return 1;
284                 }
285         }
286
287         return 0;
288 }
289
290 int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
291 {
292         int i;
293
294         if (alen > 1) {
295                 printf ("I2C read: addr len %d not supported\n", alen);
296                 return 1;
297         }
298
299         if (addr + len > 256) {
300                 printf ("I2C read: address out of range\n");
301                 return 1;
302         }
303
304         for (i = 0; i < len; i++) {
305                 if (i2c_write_byte (chip, addr + i, buffer[i])) {
306                         printf ("I2C read: I/O error\n");
307                         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
308                         return 1;
309                 }
310         }
311
312         return 0;
313 }
314
315 static void wait_for_bb (void)
316 {
317         int timeout = 10;
318         u16 stat;
319
320         outw(0xFFFF, I2C_STAT);  /* clear current interruts...*/
321         while ((stat = inw (I2C_STAT) & I2C_STAT_BB) && timeout--) {
322                 outw (stat, I2C_STAT);
323                 udelay (50000);
324         }
325
326         if (timeout <= 0) {
327                 printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
328                         inw (I2C_STAT));
329         }
330         outw(0xFFFF, I2C_STAT);  /* clear delayed stuff*/
331 }
332
333 static u16 wait_for_pin (void)
334 {
335         u16 status;
336         int timeout = 10;
337
338         do {
339                 udelay (1000);
340                 status = inw (I2C_STAT);
341         } while (  !(status &
342                    (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
343                     I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
344                     I2C_STAT_AL)) && timeout--);
345
346         if (timeout <= 0) {
347                 printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
348                         inw (I2C_STAT));
349                         outw(0xFFFF, I2C_STAT);
350 }
351         return status;
352 }
353
354 #endif /* CONFIG_DRIVER_OMAP24XX_I2C */