4 * Copyright (c) 2003 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
23 static void wait_for_bb (void);
24 static u16 wait_for_pin (void);
26 void i2c_init (int speed, int slaveadd)
30 if (inw (I2C_CON) & I2C_CON_EN) {
35 /* 12MHz I2C module clock */
37 outw (I2C_CON_EN, I2C_CON);
38 outw (0, I2C_SYSTEST);
39 /* have to enable intrrupts or OMAP i2c module doesn't work */
40 outw (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
41 I2C_IE_NACK_IE | I2C_IE_AL_IE, I2C_IE);
42 scl = (12000000 / 2) / speed - 6;
46 outw (slaveadd, I2C_OA);
51 static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
56 /* wait until bus not busy */
61 /* set slave address */
62 outw (devaddr, I2C_SA);
63 /* no stop bit needed here */
64 outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, I2C_CON);
66 status = wait_for_pin ();
68 if (status & I2C_STAT_XRDY) {
69 /* Important: have to use byte access */
70 *(volatile u8 *) (I2C_DATA) = regoffset;
72 if (inw (I2C_STAT) & I2C_STAT_NACK) {
80 /* free bus, otherwise we can't use a combined transction */
82 while (inw (I2C_STAT) || (inw (I2C_CON) & I2C_CON_MST)) {
84 /* Have to clear pending interrupt to clear I2C_STAT */
89 /* set slave address */
90 outw (devaddr, I2C_SA);
91 /* read one byte from slave */
93 /* need stop bit here */
94 outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
97 status = wait_for_pin ();
98 if (status & I2C_STAT_RRDY) {
99 *value = inw (I2C_DATA);
106 outw (I2C_CON_EN, I2C_CON);
107 while (inw (I2C_STAT)
108 || (inw (I2C_CON) & I2C_CON_MST)) {
118 static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
123 /* wait until bus not busy */
128 /* set slave address */
129 outw (devaddr, I2C_SA);
130 /* stop bit needed here */
131 outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
132 I2C_CON_STP, I2C_CON);
134 /* wait until state change */
135 status = wait_for_pin ();
137 if (status & I2C_STAT_XRDY) {
138 /* send out two bytes */
139 outw ((value << 8) + regoffset, I2C_DATA);
140 /* must have enough delay to allow BB bit to go low */
142 if (inw (I2C_STAT) & I2C_STAT_NACK) {
150 outw (I2C_CON_EN, I2C_CON);
151 while (inw (I2C_STAT) || (inw (I2C_CON) & I2C_CON_MST)) {
153 /* have to read to clear intrrupt */
161 int i2c_probe (uchar chip)
165 if (chip == inw (I2C_OA)) {
169 /* wait until bus not busy */
172 /* try to read one byte */
174 /* set slave address */
176 /* stop bit needed here */
177 outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON);
178 /* enough delay for the NACK bit set */
180 if (!(inw (I2C_STAT) & I2C_STAT_NACK)) {
183 outw (inw (I2C_CON) | I2C_CON_STP, I2C_CON);
191 int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
196 printf ("I2C read: addr len %d not supported\n", alen);
200 if (addr + len > 256) {
201 printf ("I2C read: address out of range\n");
205 for (i = 0; i < len; i++) {
206 if (i2c_read_byte (chip, addr + i, &buffer[i])) {
207 printf ("I2C read: I/O error\n");
208 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
216 int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
221 printf ("I2C read: addr len %d not supported\n", alen);
225 if (addr + len > 256) {
226 printf ("I2C read: address out of range\n");
230 for (i = 0; i < len; i++) {
231 if (i2c_write_byte (chip, addr + i, buffer[i])) {
232 printf ("I2C read: I/O error\n");
233 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
241 static void wait_for_bb (void)
245 while ((inw (I2C_STAT) & I2C_STAT_BB) && timeout--) {
251 printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
256 static u16 wait_for_pin (void)
263 status = inw (I2C_STAT);
267 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
268 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
269 I2C_STAT_AL)) && timeout--);
272 printf ("timed out in wait_for_pin: I2C_STAT=%x\n",