]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/omap24xx_i2c.c
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
24 #include <asm/arch/i2c.h>
27 #define inw(a) __raw_readw(a)
28 #define outw(a,v) __raw_writew(a,v)
30 #ifdef CONFIG_DRIVER_OMAP24XX_I2C
32 static void wait_for_bb (void);
33 static u16
wait_for_pin (void);
34 void flush_fifo(void);
36 void i2c_init (int speed
, int slaveadd
)
40 if (inw (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 */
52 outw (slaveadd
, I2C_OA
);
53 outw (I2C_CON_EN
, I2C_CON
);
55 /* have to enable intrrupts or OMAP i2c module doesn't work */
56 outw (I2C_IE_XRDY_IE
| I2C_IE_RRDY_IE
| I2C_IE_ARDY_IE
|
57 I2C_IE_NACK_IE
| I2C_IE_AL_IE
, I2C_IE
);
61 static int i2c_read_byte (u8 devaddr
, u8 regoffset
, u8
* value
)
66 /* wait until bus not busy */
71 /* set slave address */
72 outw (devaddr
, I2C_SA
);
73 /* no stop bit needed here */
74 outw (I2C_CON_EN
| I2C_CON_MST
| I2C_CON_STT
| I2C_CON_TRX
, I2C_CON
);
76 status
= wait_for_pin ();
78 if (status
& I2C_STAT_XRDY
) {
79 /* Important: have to use byte access */
80 *(volatile u8
*) (I2C_DATA
) = regoffset
;
82 if (inw (I2C_STAT
) & I2C_STAT_NACK
) {
90 /* free bus, otherwise we can't use a combined transction */
92 while (inw (I2C_STAT
) || (inw (I2C_CON
) & I2C_CON_MST
)) {
94 /* Have to clear pending interrupt to clear I2C_STAT */
95 outw (0xFFFF, I2C_STAT
);
99 /* set slave address */
100 outw (devaddr
, I2C_SA
);
101 /* read one byte from slave */
103 /* need stop bit here */
104 outw (I2C_CON_EN
| I2C_CON_MST
| I2C_CON_STT
| I2C_CON_STP
,
107 status
= wait_for_pin ();
108 if (status
& I2C_STAT_RRDY
) {
109 *value
= inw (I2C_DATA
);
116 outw (I2C_CON_EN
, I2C_CON
);
117 while (inw (I2C_STAT
)
118 || (inw (I2C_CON
) & I2C_CON_MST
)) {
120 outw (0xFFFF, I2C_STAT
);
125 outw (0xFFFF, I2C_STAT
);
130 static int i2c_write_byte (u8 devaddr
, u8 regoffset
, u8 value
)
135 /* wait until bus not busy */
140 /* set slave address */
141 outw (devaddr
, I2C_SA
);
142 /* stop bit needed here */
143 outw (I2C_CON_EN
| I2C_CON_MST
| I2C_CON_STT
| I2C_CON_TRX
|
144 I2C_CON_STP
, I2C_CON
);
146 /* wait until state change */
147 status
= wait_for_pin ();
149 if (status
& I2C_STAT_XRDY
) {
150 /* send out two bytes */
151 outw ((value
<< 8) + regoffset
, I2C_DATA
);
152 /* must have enough delay to allow BB bit to go low */
154 if (inw (I2C_STAT
) & I2C_STAT_NACK
) {
162 outw (I2C_CON_EN
, I2C_CON
);
163 while ((stat
= inw (I2C_STAT
)) || (inw (I2C_CON
) & I2C_CON_MST
)) {
165 /* have to read to clear intrrupt */
166 outw (0xFFFF, I2C_STAT
);
170 outw (0xFFFF, I2C_STAT
);
175 void flush_fifo(void)
178 /* note: if you try and read data when its not there or ready
179 * you get a bus error
182 stat
= inw(I2C_STAT
);
183 if(stat
== I2C_STAT_RRDY
){
185 outw(I2C_STAT_RRDY
,I2C_STAT
);
192 int i2c_probe (uchar chip
)
194 int res
= 1; /* default = fail */
196 if (chip
== inw (I2C_OA
)) {
200 /* wait until bus not busy */
203 /* try to read one byte */
205 /* set slave address */
207 /* stop bit needed here */
208 outw (I2C_CON_EN
| I2C_CON_MST
| I2C_CON_STT
| I2C_CON_STP
, I2C_CON
);
209 /* enough delay for the NACK bit set */
212 if (!(inw (I2C_STAT
) & I2C_STAT_NACK
)) {
213 res
= 0; /* success case */
215 outw(0xFFFF, I2C_STAT
);
217 outw(0xFFFF, I2C_STAT
); /* failue, clear sources*/
218 outw (inw (I2C_CON
) | I2C_CON_STP
, I2C_CON
); /* finish up xfer */
223 outw (0, I2C_CNT
); /* don't allow any more data in...we don't want it.*/
224 outw(0xFFFF, I2C_STAT
);
228 int i2c_read (uchar chip
, uint addr
, int alen
, uchar
* buffer
, int len
)
233 printf ("I2C read: addr len %d not supported\n", alen
);
237 if (addr
+ len
> 256) {
238 printf ("I2C read: address out of range\n");
242 for (i
= 0; i
< len
; i
++) {
243 if (i2c_read_byte (chip
, addr
+ i
, &buffer
[i
])) {
244 printf ("I2C read: I/O error\n");
245 i2c_init (CFG_I2C_SPEED
, CFG_I2C_SLAVE
);
253 int i2c_write (uchar chip
, uint addr
, int alen
, uchar
* buffer
, int len
)
258 printf ("I2C read: addr len %d not supported\n", alen
);
262 if (addr
+ len
> 256) {
263 printf ("I2C read: address out of range\n");
267 for (i
= 0; i
< len
; i
++) {
268 if (i2c_write_byte (chip
, addr
+ i
, buffer
[i
])) {
269 printf ("I2C read: I/O error\n");
270 i2c_init (CFG_I2C_SPEED
, CFG_I2C_SLAVE
);
278 static void wait_for_bb (void)
283 outw(0xFFFF, I2C_STAT
); /* clear current interruts...*/
284 while ((stat
= inw (I2C_STAT
) & I2C_STAT_BB
) && timeout
--) {
285 outw (stat
, I2C_STAT
);
290 printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
293 outw(0xFFFF, I2C_STAT
); /* clear delayed stuff*/
296 static u16
wait_for_pin (void)
303 status
= inw (I2C_STAT
);
305 (I2C_STAT_ROVR
| I2C_STAT_XUDF
| I2C_STAT_XRDY
|
306 I2C_STAT_RRDY
| I2C_STAT_ARDY
| I2C_STAT_NACK
|
307 I2C_STAT_AL
)) && timeout
--);
310 printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
312 outw(0xFFFF, I2C_STAT
);
317 #endif /* CONFIG_DRIVER_OMAP24XX_I2C */