4 * Copyright (c) 2004 Texas Instruments
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.
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.
14 * Author: Jian Zhang jzhang@ti.com, Texas Instruments
16 * Copyright (c) 2003 Wolfgang Denk, wd@denx.de
17 * Rewritten to fit into the current U-Boot framework
19 * Adapted for OMAP2420 I2C, r-woodruff2@ti.com
25 #include <asm/arch/i2c.h>
28 static void wait_for_bb (void);
29 static u16 wait_for_pin (void);
30 static void flush_fifo(void);
32 void i2c_init (int speed, int slaveadd)
36 writew(0x2, I2C_SYSC); /* for ES2 after soft reset */
38 writew(0x0, I2C_SYSC); /* will probably self clear but */
40 if (readw (I2C_CON) & I2C_CON_EN) {
45 /* 12MHz I2C module clock */
47 speed = speed/1000; /* 100 or 400 */
48 scl = ((12000/(speed*2)) - 7); /* use 7 when PSC = 0 */
49 writew (scl, I2C_SCLL);
50 writew (scl, I2C_SCLH);
52 writew (slaveadd, I2C_OA);
53 writew (I2C_CON_EN, I2C_CON);
55 /* have to enable intrrupts or OMAP i2c module doesn't work */
56 writew (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
57 I2C_IE_NACK_IE | I2C_IE_AL_IE, I2C_IE);
60 writew (0xFFFF, I2C_STAT);
64 static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
69 /* wait until bus not busy */
74 /* set slave address */
75 writew (devaddr, I2C_SA);
76 /* no stop bit needed here */
77 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, I2C_CON);
79 status = wait_for_pin ();
81 if (status & I2C_STAT_XRDY) {
82 /* Important: have to use byte access */
83 writeb (regoffset, I2C_DATA);
85 if (readw (I2C_STAT) & I2C_STAT_NACK) {
93 /* free bus, otherwise we can't use a combined transction */
95 while (readw (I2C_STAT) || (readw (I2C_CON) & I2C_CON_MST)) {
97 /* Have to clear pending interrupt to clear I2C_STAT */
98 writew (0xFFFF, I2C_STAT);
102 /* set slave address */
103 writew (devaddr, I2C_SA);
104 /* read one byte from slave */
106 /* need stop bit here */
107 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
110 status = wait_for_pin ();
111 if (status & I2C_STAT_RRDY) {
112 #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
113 *value = readb (I2C_DATA);
115 *value = readw (I2C_DATA);
123 writew (I2C_CON_EN, I2C_CON);
124 while (readw (I2C_STAT)
125 || (readw (I2C_CON) & I2C_CON_MST)) {
127 writew (0xFFFF, I2C_STAT);
132 writew (0xFFFF, I2C_STAT);
137 static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
142 /* wait until bus not busy */
147 /* set slave address */
148 writew (devaddr, I2C_SA);
149 /* stop bit needed here */
150 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
151 I2C_CON_STP, I2C_CON);
153 /* wait until state change */
154 status = wait_for_pin ();
156 if (status & I2C_STAT_XRDY) {
157 #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
158 /* send out 1 byte */
159 writeb (regoffset, I2C_DATA);
160 writew (I2C_STAT_XRDY, I2C_STAT);
162 status = wait_for_pin ();
163 if ((status & I2C_STAT_XRDY)) {
164 /* send out next 1 byte */
165 writeb (value, I2C_DATA);
166 writew (I2C_STAT_XRDY, I2C_STAT);
171 /* send out two bytes */
172 writew ((value << 8) + regoffset, I2C_DATA);
174 /* must have enough delay to allow BB bit to go low */
176 if (readw (I2C_STAT) & I2C_STAT_NACK) {
186 writew (I2C_CON_EN, I2C_CON);
187 while ((stat = readw (I2C_STAT)) || (readw (I2C_CON) & I2C_CON_MST)) {
189 /* have to read to clear intrrupt */
190 writew (0xFFFF, I2C_STAT);
191 if(--eout == 0) /* better leave with error than hang */
196 writew (0xFFFF, I2C_STAT);
201 static void flush_fifo(void)
204 /* note: if you try and read data when its not there or ready
205 * you get a bus error
208 stat = readw(I2C_STAT);
209 if(stat == I2C_STAT_RRDY){
210 #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
215 writew(I2C_STAT_RRDY,I2C_STAT);
222 int i2c_probe (uchar chip)
224 int res = 1; /* default = fail */
226 if (chip == readw (I2C_OA)) {
230 /* wait until bus not busy */
233 /* try to read one byte */
235 /* set slave address */
236 writew (chip, I2C_SA);
237 /* stop bit needed here */
238 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON);
239 /* enough delay for the NACK bit set */
242 if (!(readw (I2C_STAT) & I2C_STAT_NACK)) {
243 res = 0; /* success case */
245 writew(0xFFFF, I2C_STAT);
247 writew(0xFFFF, I2C_STAT); /* failue, clear sources*/
248 writew (readw (I2C_CON) | I2C_CON_STP, I2C_CON); /* finish up xfer */
253 writew (0, I2C_CNT); /* don't allow any more data in...we don't want it.*/
254 writew(0xFFFF, I2C_STAT);
258 int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
263 printf ("I2C read: addr len %d not supported\n", alen);
267 if (addr + len > 256) {
268 printf ("I2C read: address out of range\n");
272 for (i = 0; i < len; i++) {
273 if (i2c_read_byte (chip, addr + i, &buffer[i])) {
274 printf ("I2C read: I/O error\n");
275 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
283 int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
288 printf ("I2C read: addr len %d not supported\n", alen);
292 if (addr + len > 256) {
293 printf ("I2C read: address out of range\n");
297 for (i = 0; i < len; i++) {
298 if (i2c_write_byte (chip, addr + i, buffer[i])) {
299 printf ("I2C read: I/O error\n");
300 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
308 static void wait_for_bb (void)
313 writew(0xFFFF, I2C_STAT); /* clear current interruts...*/
314 while ((stat = readw (I2C_STAT) & I2C_STAT_BB) && timeout--) {
315 writew (stat, I2C_STAT);
320 printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
323 writew(0xFFFF, I2C_STAT); /* clear delayed stuff*/
326 static u16 wait_for_pin (void)
333 status = readw (I2C_STAT);
335 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
336 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
337 I2C_STAT_AL)) && timeout--);
340 printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
342 writew(0xFFFF, I2C_STAT);