blob: 978507ba29870c9845e752fa59cc14705c3abd24 [file] [log] [blame]
wdenk8ed96042005-01-09 23:16:25 +00001/*
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>
wdenk289f9322005-01-12 00:15:14 +000024
wdenk8ed96042005-01-09 23:16:25 +000025#include <asm/arch/i2c.h>
26#include <asm/io.h>
27
Steve Sakoman938717c2010-06-12 06:42:57 -070028#include "omap24xx_i2c.h"
29
John Rigby29565322010-12-20 18:27:51 -070030DECLARE_GLOBAL_DATA_PTR;
31
Tom Rinicec487a2012-02-20 18:49:16 +000032#define I2C_TIMEOUT 1000
Steve Sakomand7083952010-07-19 20:31:55 -070033
Tom Rinicec487a2012-02-20 18:49:16 +000034static void wait_for_bb(void);
35static u16 wait_for_pin(void);
Wolfgang Denk49a75812005-09-25 18:41:04 +020036static void flush_fifo(void);
wdenk8ed96042005-01-09 23:16:25 +000037
Andreas Müller0b620ec2012-01-04 15:26:22 +000038/*
39 * For SPL boot some boards need i2c before SDRAM is initialised so force
40 * variables to live in SRAM
41 */
42static struct i2c __attribute__((section (".data"))) *i2c_base =
43 (struct i2c *)I2C_DEFAULT_BASE;
44static unsigned int __attribute__((section (".data"))) bus_initialized[I2C_BUS_MAX] =
45 { [0 ... (I2C_BUS_MAX-1)] = 0 };
46static unsigned int __attribute__((section (".data"))) current_bus = 0;
Dirk Behme1d2e96d2009-11-02 20:36:26 +010047
Michael Jones89677b22011-07-27 14:01:55 -040048void i2c_init(int speed, int slaveadd)
wdenk8ed96042005-01-09 23:16:25 +000049{
Tom Rix7f79dfb2009-06-28 12:52:27 -050050 int psc, fsscll, fssclh;
51 int hsscll = 0, hssclh = 0;
52 u32 scll, sclh;
Tom Rinicec487a2012-02-20 18:49:16 +000053 int timeout = I2C_TIMEOUT;
Tom Rix7f79dfb2009-06-28 12:52:27 -050054
55 /* Only handle standard, fast and high speeds */
56 if ((speed != OMAP_I2C_STANDARD) &&
57 (speed != OMAP_I2C_FAST_MODE) &&
58 (speed != OMAP_I2C_HIGH_SPEED)) {
59 printf("Error : I2C unsupported speed %d\n", speed);
60 return;
61 }
62
63 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
64 psc -= 1;
65 if (psc < I2C_PSC_MIN) {
66 printf("Error : I2C unsupported prescalar %d\n", psc);
67 return;
68 }
69
70 if (speed == OMAP_I2C_HIGH_SPEED) {
71 /* High speed */
72
73 /* For first phase of HS mode */
74 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK /
75 (2 * OMAP_I2C_FAST_MODE);
76
77 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
78 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
79 if (((fsscll < 0) || (fssclh < 0)) ||
80 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müller49e9b4b2012-01-04 15:26:19 +000081 puts("Error : I2C initializing first phase clock\n");
Tom Rix7f79dfb2009-06-28 12:52:27 -050082 return;
83 }
84
85 /* For second phase of HS mode */
86 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
87
88 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
89 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
90 if (((fsscll < 0) || (fssclh < 0)) ||
91 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müller49e9b4b2012-01-04 15:26:19 +000092 puts("Error : I2C initializing second phase clock\n");
Tom Rix7f79dfb2009-06-28 12:52:27 -050093 return;
94 }
95
96 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
97 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
98
99 } else {
100 /* Standard and fast speed */
101 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
102
103 fsscll -= I2C_FASTSPEED_SCLL_TRIM;
104 fssclh -= I2C_FASTSPEED_SCLH_TRIM;
105 if (((fsscll < 0) || (fssclh < 0)) ||
106 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müller49e9b4b2012-01-04 15:26:19 +0000107 puts("Error : I2C initializing clock\n");
Tom Rix7f79dfb2009-06-28 12:52:27 -0500108 return;
109 }
110
111 scll = (unsigned int)fsscll;
112 sclh = (unsigned int)fssclh;
113 }
wdenk8ed96042005-01-09 23:16:25 +0000114
Michael Jones89677b22011-07-27 14:01:55 -0400115 if (readw(&i2c_base->con) & I2C_CON_EN) {
116 writew(0, &i2c_base->con);
117 udelay(50000);
wdenk8ed96042005-01-09 23:16:25 +0000118 }
119
Tom Rinicec487a2012-02-20 18:49:16 +0000120 writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
121 udelay(1000);
122
123 writew(I2C_CON_EN, &i2c_base->con);
124 while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
125 if (timeout <= 0) {
126 puts("ERROR: Timeout in soft-reset\n");
127 return;
128 }
129 udelay(1000);
130 }
131
132 writew(0, &i2c_base->con);
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100133 writew(psc, &i2c_base->psc);
134 writew(scll, &i2c_base->scll);
135 writew(sclh, &i2c_base->sclh);
Tom Rix7f79dfb2009-06-28 12:52:27 -0500136
wdenk8ed96042005-01-09 23:16:25 +0000137 /* own address */
Michael Jones89677b22011-07-27 14:01:55 -0400138 writew(slaveadd, &i2c_base->oa);
139 writew(I2C_CON_EN, &i2c_base->con);
Wolfgang Denk49a75812005-09-25 18:41:04 +0200140
wdenk8ed96042005-01-09 23:16:25 +0000141 /* have to enable intrrupts or OMAP i2c module doesn't work */
Michael Jones89677b22011-07-27 14:01:55 -0400142 writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100143 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
Michael Jones89677b22011-07-27 14:01:55 -0400144 udelay(1000);
Wolfgang Denk49a75812005-09-25 18:41:04 +0200145 flush_fifo();
Michael Jones89677b22011-07-27 14:01:55 -0400146 writew(0xFFFF, &i2c_base->stat);
147 writew(0, &i2c_base->cnt);
Tom Rinicec487a2012-02-20 18:49:16 +0000148
149 if (gd->flags & GD_FLG_RELOC)
150 bus_initialized[current_bus] = 1;
151}
152
Ilya Yanok55faa582012-06-08 03:12:09 +0000153static int i2c_read_byte(u8 devaddr, u16 regoffset, u8 alen, u8 *value)
Tom Rinicec487a2012-02-20 18:49:16 +0000154{
155 int i2c_error = 0;
156 u16 status;
Ilya Yanok55faa582012-06-08 03:12:09 +0000157 int i = 2 - alen;
158 u8 tmpbuf[2] = {(regoffset) >> 8, regoffset & 0xff};
159 u16 w;
Tom Rinicec487a2012-02-20 18:49:16 +0000160
161 /* wait until bus not busy */
162 wait_for_bb();
163
164 /* one byte only */
Ilya Yanok55faa582012-06-08 03:12:09 +0000165 writew(alen, &i2c_base->cnt);
Tom Rinicec487a2012-02-20 18:49:16 +0000166 /* set slave address */
167 writew(devaddr, &i2c_base->sa);
168 /* no stop bit needed here */
169 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
170 I2C_CON_TRX, &i2c_base->con);
171
172 /* send register offset */
173 while (1) {
174 status = wait_for_pin();
175 if (status == 0 || status & I2C_STAT_NACK) {
176 i2c_error = 1;
177 goto read_exit;
178 }
179 if (status & I2C_STAT_XRDY) {
Ilya Yanok55faa582012-06-08 03:12:09 +0000180 w = tmpbuf[i++];
181#if !(defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
182 defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX))
183 w |= tmpbuf[i++] << 8;
184#endif
185 writew(w, &i2c_base->data);
Tom Rinicec487a2012-02-20 18:49:16 +0000186 writew(I2C_STAT_XRDY, &i2c_base->stat);
187 }
188 if (status & I2C_STAT_ARDY) {
189 writew(I2C_STAT_ARDY, &i2c_base->stat);
190 break;
191 }
192 }
193
194 /* set slave address */
195 writew(devaddr, &i2c_base->sa);
196 /* read one byte from slave */
197 writew(1, &i2c_base->cnt);
198 /* need stop bit here */
199 writew(I2C_CON_EN | I2C_CON_MST |
200 I2C_CON_STT | I2C_CON_STP,
201 &i2c_base->con);
202
203 /* receive data */
204 while (1) {
205 status = wait_for_pin();
206 if (status == 0 || status & I2C_STAT_NACK) {
207 i2c_error = 1;
208 goto read_exit;
209 }
210 if (status & I2C_STAT_RRDY) {
211#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
Tom Rini402d17f2012-05-21 06:46:33 +0000212 defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX)
Tom Rinicec487a2012-02-20 18:49:16 +0000213 *value = readb(&i2c_base->data);
214#else
215 *value = readw(&i2c_base->data);
216#endif
217 writew(I2C_STAT_RRDY, &i2c_base->stat);
218 }
219 if (status & I2C_STAT_ARDY) {
220 writew(I2C_STAT_ARDY, &i2c_base->stat);
221 break;
222 }
223 }
224
225read_exit:
226 flush_fifo();
227 writew(0xFFFF, &i2c_base->stat);
228 writew(0, &i2c_base->cnt);
229 return i2c_error;
wdenk8ed96042005-01-09 23:16:25 +0000230}
231
Wolfgang Denk49a75812005-09-25 18:41:04 +0200232static void flush_fifo(void)
wdenk8ed96042005-01-09 23:16:25 +0000233{ u16 stat;
wdenk082acfd2005-01-10 00:01:04 +0000234
235 /* note: if you try and read data when its not there or ready
236 * you get a bus error
237 */
Michael Jones89677b22011-07-27 14:01:55 -0400238 while (1) {
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100239 stat = readw(&i2c_base->stat);
Michael Jones89677b22011-07-27 14:01:55 -0400240 if (stat == I2C_STAT_RRDY) {
Steve Sakoman938717c2010-06-12 06:42:57 -0700241#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
Tom Rini402d17f2012-05-21 06:46:33 +0000242 defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX)
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100243 readb(&i2c_base->data);
Dirk Behme7d264c12008-12-14 09:47:18 +0100244#else
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100245 readw(&i2c_base->data);
Dirk Behme7d264c12008-12-14 09:47:18 +0100246#endif
Michael Jones89677b22011-07-27 14:01:55 -0400247 writew(I2C_STAT_RRDY, &i2c_base->stat);
wdenk8ed96042005-01-09 23:16:25 +0000248 udelay(1000);
Michael Jones89677b22011-07-27 14:01:55 -0400249 } else
wdenk8ed96042005-01-09 23:16:25 +0000250 break;
251 }
252}
253
Michael Jones89677b22011-07-27 14:01:55 -0400254int i2c_probe(uchar chip)
wdenk8ed96042005-01-09 23:16:25 +0000255{
Tom Rinicec487a2012-02-20 18:49:16 +0000256 u16 status;
wdenk8ed96042005-01-09 23:16:25 +0000257 int res = 1; /* default = fail */
258
Michael Jones89677b22011-07-27 14:01:55 -0400259 if (chip == readw(&i2c_base->oa))
wdenk8ed96042005-01-09 23:16:25 +0000260 return res;
wdenk8ed96042005-01-09 23:16:25 +0000261
262 /* wait until bus not busy */
Tom Rinicec487a2012-02-20 18:49:16 +0000263 wait_for_bb();
wdenk8ed96042005-01-09 23:16:25 +0000264
Tom Rini168a5ac2012-05-21 06:46:29 +0000265 /* try to read one byte */
Michael Jones89677b22011-07-27 14:01:55 -0400266 writew(1, &i2c_base->cnt);
wdenk8ed96042005-01-09 23:16:25 +0000267 /* set slave address */
Michael Jones89677b22011-07-27 14:01:55 -0400268 writew(chip, &i2c_base->sa);
wdenk8ed96042005-01-09 23:16:25 +0000269 /* stop bit needed here */
Tom Rini168a5ac2012-05-21 06:46:29 +0000270 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, &i2c_base->con);
wdenk8ed96042005-01-09 23:16:25 +0000271
Tom Rini168a5ac2012-05-21 06:46:29 +0000272 while (1) {
273 status = wait_for_pin();
274 if (status == 0 || status & I2C_STAT_AL) {
275 res = 1;
276 goto probe_exit;
277 }
278 if (status & I2C_STAT_NACK) {
279 res = 1;
280 writew(0xff, &i2c_base->stat);
281 writew (readw (&i2c_base->con) | I2C_CON_STP, &i2c_base->con);
282 wait_for_bb ();
283 break;
284 }
285 if (status & I2C_STAT_ARDY) {
286 writew(I2C_STAT_ARDY, &i2c_base->stat);
287 break;
288 }
289 if (status & I2C_STAT_RRDY) {
290 res = 0;
291#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
Tom Rini402d17f2012-05-21 06:46:33 +0000292 defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX)
Tom Rini168a5ac2012-05-21 06:46:29 +0000293 readb(&i2c_base->data);
294#else
295 readw(&i2c_base->data);
296#endif
297 writew(I2C_STAT_RRDY, &i2c_base->stat);
298 }
299 }
Tom Rinicec487a2012-02-20 18:49:16 +0000300
Tom Rini168a5ac2012-05-21 06:46:29 +0000301probe_exit:
wdenk8ed96042005-01-09 23:16:25 +0000302 flush_fifo();
Michael Jones89677b22011-07-27 14:01:55 -0400303 /* don't allow any more data in... we don't want it. */
304 writew(0, &i2c_base->cnt);
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100305 writew(0xFFFF, &i2c_base->stat);
wdenk8ed96042005-01-09 23:16:25 +0000306 return res;
307}
308
Michael Jones89677b22011-07-27 14:01:55 -0400309int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk8ed96042005-01-09 23:16:25 +0000310{
Tom Rinicec487a2012-02-20 18:49:16 +0000311 int i;
wdenk8ed96042005-01-09 23:16:25 +0000312
Ilya Yanok55faa582012-06-08 03:12:09 +0000313 if (alen > 2) {
Tom Rinicec487a2012-02-20 18:49:16 +0000314 printf("I2C read: addr len %d not supported\n", alen);
wdenk8ed96042005-01-09 23:16:25 +0000315 return 1;
Tom Rinicec487a2012-02-20 18:49:16 +0000316 }
wdenk8ed96042005-01-09 23:16:25 +0000317
Ilya Yanok55faa582012-06-08 03:12:09 +0000318 if (addr + len > (1 << 16)) {
Tom Rinicec487a2012-02-20 18:49:16 +0000319 puts("I2C read: address out of range\n");
320 return 1;
321 }
322
323 for (i = 0; i < len; i++) {
Ilya Yanok55faa582012-06-08 03:12:09 +0000324 if (i2c_read_byte(chip, addr + i, alen, &buffer[i])) {
Tom Rinicec487a2012-02-20 18:49:16 +0000325 puts("I2C read: I/O error\n");
326 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenk8ed96042005-01-09 23:16:25 +0000327 return 1;
328 }
329 }
330
331 return 0;
332}
333
Michael Jones89677b22011-07-27 14:01:55 -0400334int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk8ed96042005-01-09 23:16:25 +0000335{
Tom Rinicec487a2012-02-20 18:49:16 +0000336 int i;
337 u16 status;
338 int i2c_error = 0;
Ilya Yanok55faa582012-06-08 03:12:09 +0000339 u16 w;
340 u8 tmpbuf[2] = {addr >> 8, addr & 0xff};
wdenk8ed96042005-01-09 23:16:25 +0000341
Ilya Yanok55faa582012-06-08 03:12:09 +0000342 if (alen > 2) {
Tom Rinicec487a2012-02-20 18:49:16 +0000343 printf("I2C write: addr len %d not supported\n", alen);
wdenk8ed96042005-01-09 23:16:25 +0000344 return 1;
Tom Rinicec487a2012-02-20 18:49:16 +0000345 }
wdenk8ed96042005-01-09 23:16:25 +0000346
Ilya Yanok55faa582012-06-08 03:12:09 +0000347 if (addr + len > (1 << 16)) {
Tom Rinicec487a2012-02-20 18:49:16 +0000348 printf("I2C write: address 0x%x + 0x%x out of range\n",
349 addr, len);
wdenk8ed96042005-01-09 23:16:25 +0000350 return 1;
351 }
352
Michael Jones0607e2b2011-09-04 14:01:55 -0400353 /* wait until bus not busy */
Tom Rinicec487a2012-02-20 18:49:16 +0000354 wait_for_bb();
Michael Jones0607e2b2011-09-04 14:01:55 -0400355
Tom Rinicec487a2012-02-20 18:49:16 +0000356 /* start address phase - will write regoffset + len bytes data */
357 /* TODO consider case when !CONFIG_OMAP243X/34XX/44XX */
358 writew(alen + len, &i2c_base->cnt);
Michael Jones0607e2b2011-09-04 14:01:55 -0400359 /* set slave address */
360 writew(chip, &i2c_base->sa);
361 /* stop bit needed here */
362 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
363 I2C_CON_STP, &i2c_base->con);
364
Ilya Yanok55faa582012-06-08 03:12:09 +0000365 /* Send address and data */
366 for (i = -alen; i < len; i++) {
Tom Rinicec487a2012-02-20 18:49:16 +0000367 status = wait_for_pin();
Patil, Rachna2faa7612012-01-22 23:44:12 +0000368
Tom Rinicec487a2012-02-20 18:49:16 +0000369 if (status == 0 || status & I2C_STAT_NACK) {
Michael Jones0607e2b2011-09-04 14:01:55 -0400370 i2c_error = 1;
Tom Rinicec487a2012-02-20 18:49:16 +0000371 printf("i2c error waiting for data ACK (status=0x%x)\n",
372 status);
373 goto write_exit;
374 }
Patil, Rachna2faa7612012-01-22 23:44:12 +0000375
Tom Rinicec487a2012-02-20 18:49:16 +0000376 if (status & I2C_STAT_XRDY) {
Ilya Yanok55faa582012-06-08 03:12:09 +0000377 w = (i < 0) ? tmpbuf[2+i] : buffer[i];
378#if !(defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
379 defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX))
380 w |= ((++i < 0) ? tmpbuf[2+i] : buffer[i]) << 8;
381#endif
382 writew(w, &i2c_base->data);
Tom Rinicec487a2012-02-20 18:49:16 +0000383 writew(I2C_STAT_XRDY, &i2c_base->stat);
384 } else {
385 i2c_error = 1;
386 printf("i2c bus not ready for Tx (i=%d)\n", i);
387 goto write_exit;
wdenk8ed96042005-01-09 23:16:25 +0000388 }
389 }
390
Tom Rinicec487a2012-02-20 18:49:16 +0000391write_exit:
Michael Jones0607e2b2011-09-04 14:01:55 -0400392 flush_fifo();
393 writew(0xFFFF, &i2c_base->stat);
Tom Rinicec487a2012-02-20 18:49:16 +0000394 return i2c_error;
wdenk8ed96042005-01-09 23:16:25 +0000395}
396
Tom Rinicec487a2012-02-20 18:49:16 +0000397static void wait_for_bb(void)
wdenk8ed96042005-01-09 23:16:25 +0000398{
Steve Sakoman73e87472010-10-20 06:07:44 -0700399 int timeout = I2C_TIMEOUT;
Tom Rinicec487a2012-02-20 18:49:16 +0000400 u16 stat;
wdenk8ed96042005-01-09 23:16:25 +0000401
Tom Rinicec487a2012-02-20 18:49:16 +0000402 writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
Michael Jones89677b22011-07-27 14:01:55 -0400403 while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
404 writew(stat, &i2c_base->stat);
Steve Sakoman73e87472010-10-20 06:07:44 -0700405 udelay(1000);
wdenk8ed96042005-01-09 23:16:25 +0000406 }
407
408 if (timeout <= 0) {
Michael Jones89677b22011-07-27 14:01:55 -0400409 printf("timed out in wait_for_bb: I2C_STAT=%x\n",
410 readw(&i2c_base->stat));
wdenk8ed96042005-01-09 23:16:25 +0000411 }
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100412 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
wdenk8ed96042005-01-09 23:16:25 +0000413}
414
Tom Rinicec487a2012-02-20 18:49:16 +0000415static u16 wait_for_pin(void)
wdenk8ed96042005-01-09 23:16:25 +0000416{
Tom Rinicec487a2012-02-20 18:49:16 +0000417 u16 status;
Steve Sakoman73e87472010-10-20 06:07:44 -0700418 int timeout = I2C_TIMEOUT;
wdenk8ed96042005-01-09 23:16:25 +0000419
420 do {
Michael Jones89677b22011-07-27 14:01:55 -0400421 udelay(1000);
422 status = readw(&i2c_base->stat);
Tom Rinicec487a2012-02-20 18:49:16 +0000423 } while (!(status &
424 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
425 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
426 I2C_STAT_AL)) && timeout--);
wdenk8ed96042005-01-09 23:16:25 +0000427
428 if (timeout <= 0) {
Tom Rinicec487a2012-02-20 18:49:16 +0000429 printf("timed out in wait_for_pin: I2C_STAT=%x\n",
Michael Jones89677b22011-07-27 14:01:55 -0400430 readw(&i2c_base->stat));
Steve Sakoman73e87472010-10-20 06:07:44 -0700431 writew(0xFFFF, &i2c_base->stat);
Tom Rinicec487a2012-02-20 18:49:16 +0000432 status = 0;
Steve Sakoman73e87472010-10-20 06:07:44 -0700433 }
Tom Rinicec487a2012-02-20 18:49:16 +0000434
wdenk8ed96042005-01-09 23:16:25 +0000435 return status;
436}
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100437
438int i2c_set_bus_num(unsigned int bus)
439{
440 if ((bus < 0) || (bus >= I2C_BUS_MAX)) {
441 printf("Bad bus: %d\n", bus);
442 return -1;
443 }
444
Michael Jones89677b22011-07-27 14:01:55 -0400445#if I2C_BUS_MAX == 3
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100446 if (bus == 2)
447 i2c_base = (struct i2c *)I2C_BASE3;
448 else
449#endif
450 if (bus == 1)
451 i2c_base = (struct i2c *)I2C_BASE2;
452 else
453 i2c_base = (struct i2c *)I2C_BASE1;
454
455 current_bus = bus;
456
Michael Jones89677b22011-07-27 14:01:55 -0400457 if (!bus_initialized[current_bus])
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100458 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
459
460 return 0;
461}
Steve Sakoman938717c2010-06-12 06:42:57 -0700462
463int i2c_get_bus_num(void)
464{
465 return (int) current_bus;
466}