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 #ifdef CONFIG_DRIVER_OMAP1510_I2C
25 static void wait_for_bb (void);
26 static u16 wait_for_pin (void);
28 void i2c_init (int speed, int slaveadd)
32 if (inw (I2C_CON) & I2C_CON_EN) {
37 /* 12Mhz I2C module clock */
39 outw (I2C_CON_EN, I2C_CON);
40 outw (0, I2C_SYSTEST);
41 /* have to enable intrrupts or OMAP i2c module doesn't work */
42 outw (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
43 I2C_IE_NACK_IE | I2C_IE_AL_IE, I2C_IE);
44 scl = (12000000 / 2) / speed - 6;
48 outw (slaveadd, I2C_OA);
53 static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
58 /* wait until bus not busy */
63 /* set slave address */
64 outw (devaddr, I2C_SA);
65 /* no stop bit needed here */
66 outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, I2C_CON);
68 status = wait_for_pin ();
70 if (status & I2C_STAT_XRDY) {
71 /* Important: have to use byte access */
72 *(volatile u8 *) (I2C_DATA) = regoffset;
74 if (inw (I2C_STAT) & I2C_STAT_NACK) {
82 /* free bus, otherwise we can't use a combined transction */
84 while (inw (I2C_STAT) || (inw (I2C_CON) & I2C_CON_MST)) {
86 /* Have to clear pending interrupt to clear I2C_STAT */
91 /* set slave address */
92 outw (devaddr, I2C_SA);
93 /* read one byte from slave */
95 /* need stop bit here */
96 outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
99 status = wait_for_pin ();
100 if (status & I2C_STAT_RRDY) {
101 *value = inw (I2C_DATA);
108 outw (I2C_CON_EN, I2C_CON);
109 while (inw (I2C_STAT)
110 || (inw (I2C_CON) & I2C_CON_MST)) {
120 static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
125 /* wait until bus not busy */
130 /* set slave address */
131 outw (devaddr, I2C_SA);
132 /* stop bit needed here */
133 outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
134 I2C_CON_STP, I2C_CON);
136 /* wait until state change */
137 status = wait_for_pin ();
139 if (status & I2C_STAT_XRDY) {
140 /* send out two bytes */
141 outw ((value << 8) + regoffset, I2C_DATA);
142 /* must have enough delay to allow BB bit to go low */
144 if (inw (I2C_STAT) & I2C_STAT_NACK) {
152 outw (I2C_CON_EN, I2C_CON);
153 while (inw (I2C_STAT) || (inw (I2C_CON) & I2C_CON_MST)) {
155 /* have to read to clear intrrupt */
163 int i2c_probe (uchar chip)
167 if (chip == inw (I2C_OA)) {
171 /* wait until bus not busy */
174 /* try to read one byte */
176 /* set slave address */
178 /* stop bit needed here */
179 outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON);
180 /* enough delay for the NACK bit set */
182 if (!(inw (I2C_STAT) & I2C_STAT_NACK)) {
185 outw (inw (I2C_CON) | I2C_CON_STP, I2C_CON);
193 int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
198 printf ("I2C read: addr len %d not supported\n", alen);
202 if (addr + len > 256) {
203 printf ("I2C read: address out of range\n");
207 for (i = 0; i < len; i++) {
208 if (i2c_read_byte (chip, addr + i, &buffer[i])) {
209 printf ("I2C read: I/O error\n");
210 i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
218 int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
223 printf ("I2C read: addr len %d not supported\n", alen);
227 if (addr + len > 256) {
228 printf ("I2C read: address out of range\n");
232 for (i = 0; i < len; i++) {
233 if (i2c_write_byte (chip, addr + i, buffer[i])) {
234 printf ("I2C read: I/O error\n");
235 i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
243 static void wait_for_bb (void)
247 while ((inw (I2C_STAT) & I2C_STAT_BB) && timeout--) {
253 printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
258 static u16 wait_for_pin (void)
265 status = inw (I2C_STAT);
269 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
270 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
271 I2C_STAT_AL)) && timeout--);
274 printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
281 #endif /* CONFIG_DRIVER_OMAP1510_I2C */