blob: 1bbb2ca54c1f7a0606567f94f01c25e64065a8b6 [file] [log] [blame]
wdenkf8062712005-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>
wdenkcb99da52005-01-12 00:15:14 +000024
wdenkf8062712005-01-09 23:16:25 +000025#include <asm/arch/i2c.h>
26#include <asm/io.h>
27
Steve Sakoman10acc712010-06-12 06:42:57 -070028#include "omap24xx_i2c.h"
29
John Rigby0d21ed02010-12-20 18:27:51 -070030DECLARE_GLOBAL_DATA_PTR;
31
Tom Rini49fbf672012-02-20 18:49:16 +000032#define I2C_TIMEOUT 1000
Steve Sakomane2bdc132010-07-19 20:31:55 -070033
Vincent Stehlé33205e32012-12-03 05:23:16 +000034static int wait_for_bb(void);
Tom Rini49fbf672012-02-20 18:49:16 +000035static u16 wait_for_pin(void);
Wolfgang Denke1e46792005-09-25 18:41:04 +020036static void flush_fifo(void);
wdenkf8062712005-01-09 23:16:25 +000037
Andreas Müller1e96e9e2012-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 Behme7a8f6572009-11-02 20:36:26 +010047
Michael Jones4db67862011-07-27 14:01:55 -040048void i2c_init(int speed, int slaveadd)
wdenkf8062712005-01-09 23:16:25 +000049{
Tom Rix03b2a742009-06-28 12:52:27 -050050 int psc, fsscll, fssclh;
51 int hsscll = 0, hssclh = 0;
52 u32 scll, sclh;
Tom Rini49fbf672012-02-20 18:49:16 +000053 int timeout = I2C_TIMEOUT;
Tom Rix03b2a742009-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üllera30293f2012-01-04 15:26:19 +000081 puts("Error : I2C initializing first phase clock\n");
Tom Rix03b2a742009-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üllera30293f2012-01-04 15:26:19 +000092 puts("Error : I2C initializing second phase clock\n");
Tom Rix03b2a742009-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üllera30293f2012-01-04 15:26:19 +0000107 puts("Error : I2C initializing clock\n");
Tom Rix03b2a742009-06-28 12:52:27 -0500108 return;
109 }
110
111 scll = (unsigned int)fsscll;
112 sclh = (unsigned int)fssclh;
113 }
wdenkf8062712005-01-09 23:16:25 +0000114
Michael Jones4db67862011-07-27 14:01:55 -0400115 if (readw(&i2c_base->con) & I2C_CON_EN) {
116 writew(0, &i2c_base->con);
117 udelay(50000);
wdenkf8062712005-01-09 23:16:25 +0000118 }
119
Tom Rini49fbf672012-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 Behme7a8f6572009-11-02 20:36:26 +0100133 writew(psc, &i2c_base->psc);
134 writew(scll, &i2c_base->scll);
135 writew(sclh, &i2c_base->sclh);
Tom Rix03b2a742009-06-28 12:52:27 -0500136
wdenkf8062712005-01-09 23:16:25 +0000137 /* own address */
Michael Jones4db67862011-07-27 14:01:55 -0400138 writew(slaveadd, &i2c_base->oa);
139 writew(I2C_CON_EN, &i2c_base->con);
Wolfgang Denke1e46792005-09-25 18:41:04 +0200140
wdenkf8062712005-01-09 23:16:25 +0000141 /* have to enable intrrupts or OMAP i2c module doesn't work */
Michael Jones4db67862011-07-27 14:01:55 -0400142 writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
Dirk Behme7a8f6572009-11-02 20:36:26 +0100143 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
Michael Jones4db67862011-07-27 14:01:55 -0400144 udelay(1000);
Wolfgang Denke1e46792005-09-25 18:41:04 +0200145 flush_fifo();
Michael Jones4db67862011-07-27 14:01:55 -0400146 writew(0xFFFF, &i2c_base->stat);
147 writew(0, &i2c_base->cnt);
Tom Rini49fbf672012-02-20 18:49:16 +0000148
149 if (gd->flags & GD_FLG_RELOC)
150 bus_initialized[current_bus] = 1;
wdenkf8062712005-01-09 23:16:25 +0000151}
152
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000153static int i2c_read_byte(u8 devaddr, u16 regoffset, u8 alen, u8 *value)
Tom Rini49fbf672012-02-20 18:49:16 +0000154{
155 int i2c_error = 0;
156 u16 status;
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000157 int i = 2 - alen;
158 u8 tmpbuf[2] = {(regoffset) >> 8, regoffset & 0xff};
159 u16 w;
Tom Rini49fbf672012-02-20 18:49:16 +0000160
161 /* wait until bus not busy */
Vincent Stehlé33205e32012-12-03 05:23:16 +0000162 if (wait_for_bb())
163 return 1;
Tom Rini49fbf672012-02-20 18:49:16 +0000164
165 /* one byte only */
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000166 writew(alen, &i2c_base->cnt);
Tom Rini49fbf672012-02-20 18:49:16 +0000167 /* set slave address */
168 writew(devaddr, &i2c_base->sa);
169 /* no stop bit needed here */
170 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
171 I2C_CON_TRX, &i2c_base->con);
172
173 /* send register offset */
174 while (1) {
175 status = wait_for_pin();
176 if (status == 0 || status & I2C_STAT_NACK) {
177 i2c_error = 1;
178 goto read_exit;
179 }
180 if (status & I2C_STAT_XRDY) {
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000181 w = tmpbuf[i++];
182#if !(defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
183 defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX))
184 w |= tmpbuf[i++] << 8;
185#endif
186 writew(w, &i2c_base->data);
Tom Rini49fbf672012-02-20 18:49:16 +0000187 writew(I2C_STAT_XRDY, &i2c_base->stat);
188 }
189 if (status & I2C_STAT_ARDY) {
190 writew(I2C_STAT_ARDY, &i2c_base->stat);
191 break;
192 }
193 }
194
195 /* set slave address */
196 writew(devaddr, &i2c_base->sa);
197 /* read one byte from slave */
198 writew(1, &i2c_base->cnt);
199 /* need stop bit here */
200 writew(I2C_CON_EN | I2C_CON_MST |
201 I2C_CON_STT | I2C_CON_STP,
202 &i2c_base->con);
203
204 /* receive data */
205 while (1) {
206 status = wait_for_pin();
207 if (status == 0 || status & I2C_STAT_NACK) {
208 i2c_error = 1;
209 goto read_exit;
210 }
211 if (status & I2C_STAT_RRDY) {
212#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
Tom Rini3fdfc282012-05-21 06:46:33 +0000213 defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX)
Tom Rini49fbf672012-02-20 18:49:16 +0000214 *value = readb(&i2c_base->data);
215#else
216 *value = readw(&i2c_base->data);
217#endif
218 writew(I2C_STAT_RRDY, &i2c_base->stat);
219 }
220 if (status & I2C_STAT_ARDY) {
221 writew(I2C_STAT_ARDY, &i2c_base->stat);
222 break;
223 }
224 }
225
226read_exit:
227 flush_fifo();
228 writew(0xFFFF, &i2c_base->stat);
229 writew(0, &i2c_base->cnt);
230 return i2c_error;
231}
232
Wolfgang Denke1e46792005-09-25 18:41:04 +0200233static void flush_fifo(void)
wdenkf8062712005-01-09 23:16:25 +0000234{ u16 stat;
wdenk2e405bf2005-01-10 00:01:04 +0000235
236 /* note: if you try and read data when its not there or ready
237 * you get a bus error
238 */
Michael Jones4db67862011-07-27 14:01:55 -0400239 while (1) {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100240 stat = readw(&i2c_base->stat);
Michael Jones4db67862011-07-27 14:01:55 -0400241 if (stat == I2C_STAT_RRDY) {
Steve Sakoman10acc712010-06-12 06:42:57 -0700242#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
Tom Rini3fdfc282012-05-21 06:46:33 +0000243 defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX)
Dirk Behme7a8f6572009-11-02 20:36:26 +0100244 readb(&i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100245#else
Dirk Behme7a8f6572009-11-02 20:36:26 +0100246 readw(&i2c_base->data);
Dirk Behme5648e512008-12-14 09:47:18 +0100247#endif
Michael Jones4db67862011-07-27 14:01:55 -0400248 writew(I2C_STAT_RRDY, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000249 udelay(1000);
Michael Jones4db67862011-07-27 14:01:55 -0400250 } else
wdenkf8062712005-01-09 23:16:25 +0000251 break;
252 }
253}
254
Michael Jones4db67862011-07-27 14:01:55 -0400255int i2c_probe(uchar chip)
wdenkf8062712005-01-09 23:16:25 +0000256{
Tom Rini49fbf672012-02-20 18:49:16 +0000257 u16 status;
wdenkf8062712005-01-09 23:16:25 +0000258 int res = 1; /* default = fail */
259
Michael Jones4db67862011-07-27 14:01:55 -0400260 if (chip == readw(&i2c_base->oa))
wdenkf8062712005-01-09 23:16:25 +0000261 return res;
wdenkf8062712005-01-09 23:16:25 +0000262
263 /* wait until bus not busy */
Vincent Stehlé33205e32012-12-03 05:23:16 +0000264 if (wait_for_bb())
265 return res;
wdenkf8062712005-01-09 23:16:25 +0000266
Tom Rini27eed8b2012-05-21 06:46:29 +0000267 /* try to read one byte */
Michael Jones4db67862011-07-27 14:01:55 -0400268 writew(1, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000269 /* set slave address */
Michael Jones4db67862011-07-27 14:01:55 -0400270 writew(chip, &i2c_base->sa);
wdenkf8062712005-01-09 23:16:25 +0000271 /* stop bit needed here */
Tom Rini27eed8b2012-05-21 06:46:29 +0000272 writew (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, &i2c_base->con);
Nick Thompson48f7ae42011-04-11 22:37:41 +0000273
Tom Rini27eed8b2012-05-21 06:46:29 +0000274 while (1) {
275 status = wait_for_pin();
276 if (status == 0 || status & I2C_STAT_AL) {
277 res = 1;
278 goto probe_exit;
279 }
280 if (status & I2C_STAT_NACK) {
281 res = 1;
282 writew(0xff, &i2c_base->stat);
283 writew (readw (&i2c_base->con) | I2C_CON_STP, &i2c_base->con);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000284
285 if (wait_for_bb())
286 res = 1;
287
Tom Rini27eed8b2012-05-21 06:46:29 +0000288 break;
289 }
290 if (status & I2C_STAT_ARDY) {
291 writew(I2C_STAT_ARDY, &i2c_base->stat);
292 break;
293 }
294 if (status & I2C_STAT_RRDY) {
295 res = 0;
296#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
Tom Rini3fdfc282012-05-21 06:46:33 +0000297 defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX)
Tom Rini27eed8b2012-05-21 06:46:29 +0000298 readb(&i2c_base->data);
299#else
300 readw(&i2c_base->data);
301#endif
302 writew(I2C_STAT_RRDY, &i2c_base->stat);
303 }
304 }
Tom Rini49fbf672012-02-20 18:49:16 +0000305
Tom Rini27eed8b2012-05-21 06:46:29 +0000306probe_exit:
wdenkf8062712005-01-09 23:16:25 +0000307 flush_fifo();
Michael Jones4db67862011-07-27 14:01:55 -0400308 /* don't allow any more data in... we don't want it. */
309 writew(0, &i2c_base->cnt);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100310 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000311 return res;
312}
313
Michael Jones4db67862011-07-27 14:01:55 -0400314int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenkf8062712005-01-09 23:16:25 +0000315{
Tom Rini49fbf672012-02-20 18:49:16 +0000316 int i;
wdenkf8062712005-01-09 23:16:25 +0000317
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000318 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000319 printf("I2C read: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000320 return 1;
321 }
322
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000323 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000324 puts("I2C read: address out of range\n");
wdenkf8062712005-01-09 23:16:25 +0000325 return 1;
326 }
327
Tom Rini49fbf672012-02-20 18:49:16 +0000328 for (i = 0; i < len; i++) {
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000329 if (i2c_read_byte(chip, addr + i, alen, &buffer[i])) {
Tom Rini49fbf672012-02-20 18:49:16 +0000330 puts("I2C read: I/O error\n");
331 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
332 return 1;
wdenkf8062712005-01-09 23:16:25 +0000333 }
334 }
335
336 return 0;
337}
338
Michael Jones4db67862011-07-27 14:01:55 -0400339int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenkf8062712005-01-09 23:16:25 +0000340{
Tom Rini49fbf672012-02-20 18:49:16 +0000341 int i;
342 u16 status;
343 int i2c_error = 0;
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000344 u16 w;
345 u8 tmpbuf[2] = {addr >> 8, addr & 0xff};
wdenkf8062712005-01-09 23:16:25 +0000346
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000347 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000348 printf("I2C write: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000349 return 1;
Tom Rini49fbf672012-02-20 18:49:16 +0000350 }
wdenkf8062712005-01-09 23:16:25 +0000351
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000352 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000353 printf("I2C write: address 0x%x + 0x%x out of range\n",
354 addr, len);
wdenkf8062712005-01-09 23:16:25 +0000355 return 1;
356 }
357
Michael Jonesbb54d572011-09-04 14:01:55 -0400358 /* wait until bus not busy */
Vincent Stehlé33205e32012-12-03 05:23:16 +0000359 if (wait_for_bb())
360 return 1;
Michael Jonesbb54d572011-09-04 14:01:55 -0400361
Tom Rini49fbf672012-02-20 18:49:16 +0000362 /* start address phase - will write regoffset + len bytes data */
363 /* TODO consider case when !CONFIG_OMAP243X/34XX/44XX */
364 writew(alen + len, &i2c_base->cnt);
Michael Jonesbb54d572011-09-04 14:01:55 -0400365 /* set slave address */
366 writew(chip, &i2c_base->sa);
367 /* stop bit needed here */
368 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
369 I2C_CON_STP, &i2c_base->con);
370
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000371 /* Send address and data */
372 for (i = -alen; i < len; i++) {
Tom Rini49fbf672012-02-20 18:49:16 +0000373 status = wait_for_pin();
Patil, Rachnaa9e18c22012-01-22 23:44:12 +0000374
Tom Rini49fbf672012-02-20 18:49:16 +0000375 if (status == 0 || status & I2C_STAT_NACK) {
376 i2c_error = 1;
377 printf("i2c error waiting for data ACK (status=0x%x)\n",
378 status);
379 goto write_exit;
380 }
381
382 if (status & I2C_STAT_XRDY) {
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000383 w = (i < 0) ? tmpbuf[2+i] : buffer[i];
384#if !(defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
385 defined(CONFIG_OMAP44XX) || defined(CONFIG_AM33XX))
386 w |= ((++i < 0) ? tmpbuf[2+i] : buffer[i]) << 8;
387#endif
388 writew(w, &i2c_base->data);
Tom Rini49fbf672012-02-20 18:49:16 +0000389 writew(I2C_STAT_XRDY, &i2c_base->stat);
390 } else {
391 i2c_error = 1;
392 printf("i2c bus not ready for Tx (i=%d)\n", i);
393 goto write_exit;
Patil, Rachnaa9e18c22012-01-22 23:44:12 +0000394 }
395 }
396
Tom Rini49fbf672012-02-20 18:49:16 +0000397write_exit:
Michael Jonesbb54d572011-09-04 14:01:55 -0400398 flush_fifo();
399 writew(0xFFFF, &i2c_base->stat);
Tom Rini49fbf672012-02-20 18:49:16 +0000400 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000401}
402
Vincent Stehlé33205e32012-12-03 05:23:16 +0000403static int wait_for_bb(void)
wdenkf8062712005-01-09 23:16:25 +0000404{
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700405 int timeout = I2C_TIMEOUT;
Tom Rini49fbf672012-02-20 18:49:16 +0000406 u16 stat;
wdenkf8062712005-01-09 23:16:25 +0000407
Tom Rini49fbf672012-02-20 18:49:16 +0000408 writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
Michael Jones4db67862011-07-27 14:01:55 -0400409 while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
410 writew(stat, &i2c_base->stat);
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700411 udelay(1000);
wdenkf8062712005-01-09 23:16:25 +0000412 }
413
414 if (timeout <= 0) {
Michael Jones4db67862011-07-27 14:01:55 -0400415 printf("timed out in wait_for_bb: I2C_STAT=%x\n",
416 readw(&i2c_base->stat));
Vincent Stehlé33205e32012-12-03 05:23:16 +0000417 return 1;
wdenkf8062712005-01-09 23:16:25 +0000418 }
Dirk Behme7a8f6572009-11-02 20:36:26 +0100419 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
Vincent Stehlé33205e32012-12-03 05:23:16 +0000420 return 0;
wdenkf8062712005-01-09 23:16:25 +0000421}
422
Tom Rini49fbf672012-02-20 18:49:16 +0000423static u16 wait_for_pin(void)
wdenkf8062712005-01-09 23:16:25 +0000424{
Tom Rini49fbf672012-02-20 18:49:16 +0000425 u16 status;
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700426 int timeout = I2C_TIMEOUT;
wdenkf8062712005-01-09 23:16:25 +0000427
428 do {
Michael Jones4db67862011-07-27 14:01:55 -0400429 udelay(1000);
430 status = readw(&i2c_base->stat);
Tom Rini49fbf672012-02-20 18:49:16 +0000431 } while (!(status &
432 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
433 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
434 I2C_STAT_AL)) && timeout--);
wdenkf8062712005-01-09 23:16:25 +0000435
436 if (timeout <= 0) {
Tom Rini49fbf672012-02-20 18:49:16 +0000437 printf("timed out in wait_for_pin: I2C_STAT=%x\n",
Michael Jones4db67862011-07-27 14:01:55 -0400438 readw(&i2c_base->stat));
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700439 writew(0xFFFF, &i2c_base->stat);
Tom Rini49fbf672012-02-20 18:49:16 +0000440 status = 0;
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700441 }
Tom Rini49fbf672012-02-20 18:49:16 +0000442
wdenkf8062712005-01-09 23:16:25 +0000443 return status;
444}
Dirk Behme7a8f6572009-11-02 20:36:26 +0100445
446int i2c_set_bus_num(unsigned int bus)
447{
448 if ((bus < 0) || (bus >= I2C_BUS_MAX)) {
449 printf("Bad bus: %d\n", bus);
450 return -1;
451 }
452
Koen Kooi584ff5f2012-08-08 00:57:35 +0000453#if I2C_BUS_MAX == 4
454 if (bus == 3)
455 i2c_base = (struct i2c *)I2C_BASE4;
456 else
457 if (bus == 2)
458 i2c_base = (struct i2c *)I2C_BASE3;
459 else
460#endif
Michael Jones4db67862011-07-27 14:01:55 -0400461#if I2C_BUS_MAX == 3
Dirk Behme7a8f6572009-11-02 20:36:26 +0100462 if (bus == 2)
463 i2c_base = (struct i2c *)I2C_BASE3;
464 else
465#endif
466 if (bus == 1)
467 i2c_base = (struct i2c *)I2C_BASE2;
468 else
469 i2c_base = (struct i2c *)I2C_BASE1;
470
471 current_bus = bus;
472
Michael Jones4db67862011-07-27 14:01:55 -0400473 if (!bus_initialized[current_bus])
Dirk Behme7a8f6572009-11-02 20:36:26 +0100474 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
475
476 return 0;
477}
Steve Sakoman10acc712010-06-12 06:42:57 -0700478
479int i2c_get_bus_num(void)
480{
481 return (int) current_bus;
482}