blob: ef38d7172522e517e8ccc5c99bb642eec56160e7 [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 *
Lubomir Popov4d98efd2013-06-01 06:44:38 +000021 * Copyright (c) 2013 Lubomir Popov <lpopov@mm-sol.com>, MM Solutions
22 * New i2c_read, i2c_write and i2c_probe functions, tested on OMAP4
23 * (4430/60/70), OMAP5 (5430) and AM335X (3359); should work on older
24 * OMAPs and derivatives as well. The only anticipated exception would
25 * be the OMAP2420, which shall require driver modification.
26 * - Rewritten i2c_read to operate correctly with all types of chips
27 * (old function could not read consistent data from some I2C slaves).
28 * - Optimized i2c_write.
29 * - New i2c_probe, performs write access vs read. The old probe could
30 * hang the system under certain conditions (e.g. unconfigured pads).
31 * - The read/write/probe functions try to identify unconfigured bus.
32 * - Status functions now read irqstatus_raw as per TRM guidelines
33 * (except for OMAP243X and OMAP34XX).
34 * - Driver now supports up to I2C5 (OMAP5).
wdenkf8062712005-01-09 23:16:25 +000035 */
36
37#include <common.h>
wdenkcb99da52005-01-12 00:15:14 +000038
wdenkf8062712005-01-09 23:16:25 +000039#include <asm/arch/i2c.h>
40#include <asm/io.h>
41
Steve Sakoman10acc712010-06-12 06:42:57 -070042#include "omap24xx_i2c.h"
43
John Rigby0d21ed02010-12-20 18:27:51 -070044DECLARE_GLOBAL_DATA_PTR;
45
Tom Rini49fbf672012-02-20 18:49:16 +000046#define I2C_TIMEOUT 1000
Steve Sakomane2bdc132010-07-19 20:31:55 -070047
Lubomir Popov4d98efd2013-06-01 06:44:38 +000048/* Absolutely safe for status update at 100 kHz I2C: */
49#define I2C_WAIT 200
50
Vincent Stehlé33205e32012-12-03 05:23:16 +000051static int wait_for_bb(void);
Lubomir Popov4d98efd2013-06-01 06:44:38 +000052static u16 wait_for_event(void);
Wolfgang Denke1e46792005-09-25 18:41:04 +020053static void flush_fifo(void);
wdenkf8062712005-01-09 23:16:25 +000054
Andreas Müller1e96e9e2012-01-04 15:26:22 +000055/*
56 * For SPL boot some boards need i2c before SDRAM is initialised so force
57 * variables to live in SRAM
58 */
59static struct i2c __attribute__((section (".data"))) *i2c_base =
60 (struct i2c *)I2C_DEFAULT_BASE;
61static unsigned int __attribute__((section (".data"))) bus_initialized[I2C_BUS_MAX] =
62 { [0 ... (I2C_BUS_MAX-1)] = 0 };
63static unsigned int __attribute__((section (".data"))) current_bus = 0;
Dirk Behme7a8f6572009-11-02 20:36:26 +010064
Michael Jones4db67862011-07-27 14:01:55 -040065void i2c_init(int speed, int slaveadd)
wdenkf8062712005-01-09 23:16:25 +000066{
Tom Rix03b2a742009-06-28 12:52:27 -050067 int psc, fsscll, fssclh;
68 int hsscll = 0, hssclh = 0;
69 u32 scll, sclh;
Tom Rini49fbf672012-02-20 18:49:16 +000070 int timeout = I2C_TIMEOUT;
Tom Rix03b2a742009-06-28 12:52:27 -050071
72 /* Only handle standard, fast and high speeds */
73 if ((speed != OMAP_I2C_STANDARD) &&
74 (speed != OMAP_I2C_FAST_MODE) &&
75 (speed != OMAP_I2C_HIGH_SPEED)) {
76 printf("Error : I2C unsupported speed %d\n", speed);
77 return;
78 }
79
80 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
81 psc -= 1;
82 if (psc < I2C_PSC_MIN) {
83 printf("Error : I2C unsupported prescalar %d\n", psc);
84 return;
85 }
86
87 if (speed == OMAP_I2C_HIGH_SPEED) {
88 /* High speed */
89
90 /* For first phase of HS mode */
91 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK /
92 (2 * OMAP_I2C_FAST_MODE);
93
94 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
95 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
96 if (((fsscll < 0) || (fssclh < 0)) ||
97 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +000098 puts("Error : I2C initializing first phase clock\n");
Tom Rix03b2a742009-06-28 12:52:27 -050099 return;
100 }
101
102 /* For second phase of HS mode */
103 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
104
105 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
106 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
107 if (((fsscll < 0) || (fssclh < 0)) ||
108 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000109 puts("Error : I2C initializing second phase clock\n");
Tom Rix03b2a742009-06-28 12:52:27 -0500110 return;
111 }
112
113 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
114 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
115
116 } else {
117 /* Standard and fast speed */
118 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
119
120 fsscll -= I2C_FASTSPEED_SCLL_TRIM;
121 fssclh -= I2C_FASTSPEED_SCLH_TRIM;
122 if (((fsscll < 0) || (fssclh < 0)) ||
123 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000124 puts("Error : I2C initializing clock\n");
Tom Rix03b2a742009-06-28 12:52:27 -0500125 return;
126 }
127
128 scll = (unsigned int)fsscll;
129 sclh = (unsigned int)fssclh;
130 }
wdenkf8062712005-01-09 23:16:25 +0000131
Michael Jones4db67862011-07-27 14:01:55 -0400132 if (readw(&i2c_base->con) & I2C_CON_EN) {
133 writew(0, &i2c_base->con);
134 udelay(50000);
wdenkf8062712005-01-09 23:16:25 +0000135 }
136
Tom Rini49fbf672012-02-20 18:49:16 +0000137 writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
138 udelay(1000);
139
140 writew(I2C_CON_EN, &i2c_base->con);
141 while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
142 if (timeout <= 0) {
143 puts("ERROR: Timeout in soft-reset\n");
144 return;
145 }
146 udelay(1000);
147 }
148
149 writew(0, &i2c_base->con);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100150 writew(psc, &i2c_base->psc);
151 writew(scll, &i2c_base->scll);
152 writew(sclh, &i2c_base->sclh);
Tom Rix03b2a742009-06-28 12:52:27 -0500153
wdenkf8062712005-01-09 23:16:25 +0000154 /* own address */
Michael Jones4db67862011-07-27 14:01:55 -0400155 writew(slaveadd, &i2c_base->oa);
156 writew(I2C_CON_EN, &i2c_base->con);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000157#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
158 /*
159 * Have to enable interrupts for OMAP2/3, these IPs don't have
160 * an 'irqstatus_raw' register and we shall have to poll 'stat'
161 */
Michael Jones4db67862011-07-27 14:01:55 -0400162 writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000163 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
164#endif
Michael Jones4db67862011-07-27 14:01:55 -0400165 udelay(1000);
Wolfgang Denke1e46792005-09-25 18:41:04 +0200166 flush_fifo();
Michael Jones4db67862011-07-27 14:01:55 -0400167 writew(0xFFFF, &i2c_base->stat);
168 writew(0, &i2c_base->cnt);
Tom Rini49fbf672012-02-20 18:49:16 +0000169
170 if (gd->flags & GD_FLG_RELOC)
171 bus_initialized[current_bus] = 1;
wdenkf8062712005-01-09 23:16:25 +0000172}
173
Wolfgang Denke1e46792005-09-25 18:41:04 +0200174static void flush_fifo(void)
wdenkf8062712005-01-09 23:16:25 +0000175{ u16 stat;
wdenk2e405bf2005-01-10 00:01:04 +0000176
177 /* note: if you try and read data when its not there or ready
178 * you get a bus error
179 */
Michael Jones4db67862011-07-27 14:01:55 -0400180 while (1) {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100181 stat = readw(&i2c_base->stat);
Michael Jones4db67862011-07-27 14:01:55 -0400182 if (stat == I2C_STAT_RRDY) {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100183 readb(&i2c_base->data);
Michael Jones4db67862011-07-27 14:01:55 -0400184 writew(I2C_STAT_RRDY, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000185 udelay(1000);
Michael Jones4db67862011-07-27 14:01:55 -0400186 } else
wdenkf8062712005-01-09 23:16:25 +0000187 break;
188 }
189}
190
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000191/*
192 * i2c_probe: Use write access. Allows to identify addresses that are
193 * write-only (like the config register of dual-port EEPROMs)
194 */
Michael Jones4db67862011-07-27 14:01:55 -0400195int i2c_probe(uchar chip)
wdenkf8062712005-01-09 23:16:25 +0000196{
Tom Rini49fbf672012-02-20 18:49:16 +0000197 u16 status;
wdenkf8062712005-01-09 23:16:25 +0000198 int res = 1; /* default = fail */
199
Michael Jones4db67862011-07-27 14:01:55 -0400200 if (chip == readw(&i2c_base->oa))
wdenkf8062712005-01-09 23:16:25 +0000201 return res;
wdenkf8062712005-01-09 23:16:25 +0000202
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000203 /* Wait until bus is free */
Vincent Stehlé33205e32012-12-03 05:23:16 +0000204 if (wait_for_bb())
205 return res;
wdenkf8062712005-01-09 23:16:25 +0000206
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000207 /* No data transfer, slave addr only */
208 writew(0, &i2c_base->cnt);
209 /* Set slave address */
Michael Jones4db67862011-07-27 14:01:55 -0400210 writew(chip, &i2c_base->sa);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000211 /* Stop bit needed here */
212 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
213 I2C_CON_STP, &i2c_base->con);
Nick Thompson48f7ae42011-04-11 22:37:41 +0000214
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000215 status = wait_for_event();
Vincent Stehlé33205e32012-12-03 05:23:16 +0000216
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000217 if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
218 /*
219 * With current high-level command implementation, notifying
220 * the user shall flood the console with 127 messages. If
221 * silent exit is desired upon unconfigured bus, remove the
222 * following 'if' section:
223 */
224 if (status == I2C_STAT_XRDY)
225 printf("i2c_probe: pads on bus %d probably not configured (status=0x%x)\n",
226 current_bus, status);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000227
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000228 goto pr_exit;
Tom Rini27eed8b2012-05-21 06:46:29 +0000229 }
Tom Rini49fbf672012-02-20 18:49:16 +0000230
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000231 /* Check for ACK (!NAK) */
232 if (!(status & I2C_STAT_NACK)) {
233 res = 0; /* Device found */
234 udelay(I2C_WAIT); /* Required by AM335X in SPL */
235 /* Abort transfer (force idle state) */
236 writew(I2C_CON_MST | I2C_CON_TRX, &i2c_base->con); /* Reset */
237 udelay(1000);
238 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX |
239 I2C_CON_STP, &i2c_base->con); /* STP */
240 }
241pr_exit:
wdenkf8062712005-01-09 23:16:25 +0000242 flush_fifo();
Dirk Behme7a8f6572009-11-02 20:36:26 +0100243 writew(0xFFFF, &i2c_base->stat);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000244 writew(0, &i2c_base->cnt);
wdenkf8062712005-01-09 23:16:25 +0000245 return res;
246}
247
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000248/*
249 * i2c_read: Function now uses a single I2C read transaction with bulk transfer
250 * of the requested number of bytes (note that the 'i2c md' command
251 * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
252 * defined in the board config header, this transaction shall be with
253 * Repeated Start (Sr) between the address and data phases; otherwise
254 * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
255 * The address (reg offset) may be 0, 1 or 2 bytes long.
256 * Function now reads correctly from chips that return more than one
257 * byte of data per addressed register (like TI temperature sensors),
258 * or that do not need a register address at all (such as some clock
259 * distributors).
260 */
Michael Jones4db67862011-07-27 14:01:55 -0400261int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenkf8062712005-01-09 23:16:25 +0000262{
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000263 int i2c_error = 0;
264 u16 status;
265
266 if (alen < 0) {
267 puts("I2C read: addr len < 0\n");
268 return 1;
269 }
270 if (len < 0) {
271 puts("I2C read: data len < 0\n");
272 return 1;
273 }
274 if (buffer == NULL) {
275 puts("I2C read: NULL pointer passed\n");
276 return 1;
277 }
wdenkf8062712005-01-09 23:16:25 +0000278
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000279 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000280 printf("I2C read: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000281 return 1;
282 }
283
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000284 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000285 puts("I2C read: address out of range\n");
wdenkf8062712005-01-09 23:16:25 +0000286 return 1;
287 }
288
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000289 /* Wait until bus not busy */
290 if (wait_for_bb())
291 return 1;
292
293 /* Zero, one or two bytes reg address (offset) */
294 writew(alen, &i2c_base->cnt);
295 /* Set slave address */
296 writew(chip, &i2c_base->sa);
297
298 if (alen) {
299 /* Must write reg offset first */
300#ifdef CONFIG_I2C_REPEATED_START
301 /* No stop bit, use Repeated Start (Sr) */
302 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
303 I2C_CON_TRX, &i2c_base->con);
304#else
305 /* Stop - Start (P-S) */
306 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP |
307 I2C_CON_TRX, &i2c_base->con);
308#endif
309 /* Send register offset */
310 while (1) {
311 status = wait_for_event();
312 /* Try to identify bus that is not padconf'd for I2C */
313 if (status == I2C_STAT_XRDY) {
314 i2c_error = 2;
315 printf("i2c_read (addr phase): pads on bus %d probably not configured (status=0x%x)\n",
316 current_bus, status);
317 goto rd_exit;
318 }
319 if (status == 0 || status & I2C_STAT_NACK) {
320 i2c_error = 1;
321 printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
322 status);
323 goto rd_exit;
324 }
325 if (alen) {
326 if (status & I2C_STAT_XRDY) {
327 alen--;
328 /* Do we have to use byte access? */
329 writeb((addr >> (8 * alen)) & 0xff,
330 &i2c_base->data);
331 writew(I2C_STAT_XRDY, &i2c_base->stat);
332 }
333 }
334 if (status & I2C_STAT_ARDY) {
335 writew(I2C_STAT_ARDY, &i2c_base->stat);
336 break;
337 }
wdenkf8062712005-01-09 23:16:25 +0000338 }
339 }
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000340 /* Set slave address */
341 writew(chip, &i2c_base->sa);
342 /* Read len bytes from slave */
343 writew(len, &i2c_base->cnt);
344 /* Need stop bit here */
345 writew(I2C_CON_EN | I2C_CON_MST |
346 I2C_CON_STT | I2C_CON_STP,
347 &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000348
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000349 /* Receive data */
350 while (1) {
351 status = wait_for_event();
352 /*
353 * Try to identify bus that is not padconf'd for I2C. This
354 * state could be left over from previous transactions if
355 * the address phase is skipped due to alen=0.
356 */
357 if (status == I2C_STAT_XRDY) {
358 i2c_error = 2;
359 printf("i2c_read (data phase): pads on bus %d probably not configured (status=0x%x)\n",
360 current_bus, status);
361 goto rd_exit;
362 }
363 if (status == 0 || status & I2C_STAT_NACK) {
364 i2c_error = 1;
365 goto rd_exit;
366 }
367 if (status & I2C_STAT_RRDY) {
368 *buffer++ = readb(&i2c_base->data);
369 writew(I2C_STAT_RRDY, &i2c_base->stat);
370 }
371 if (status & I2C_STAT_ARDY) {
372 writew(I2C_STAT_ARDY, &i2c_base->stat);
373 break;
374 }
375 }
376
377rd_exit:
378 flush_fifo();
379 writew(0xFFFF, &i2c_base->stat);
380 writew(0, &i2c_base->cnt);
381 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000382}
383
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000384/* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
Michael Jones4db67862011-07-27 14:01:55 -0400385int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenkf8062712005-01-09 23:16:25 +0000386{
Tom Rini49fbf672012-02-20 18:49:16 +0000387 int i;
388 u16 status;
389 int i2c_error = 0;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000390
391 if (alen < 0) {
392 puts("I2C write: addr len < 0\n");
393 return 1;
394 }
395
396 if (len < 0) {
397 puts("I2C write: data len < 0\n");
398 return 1;
399 }
400
401 if (buffer == NULL) {
402 puts("I2C write: NULL pointer passed\n");
403 return 1;
404 }
wdenkf8062712005-01-09 23:16:25 +0000405
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000406 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000407 printf("I2C write: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000408 return 1;
Tom Rini49fbf672012-02-20 18:49:16 +0000409 }
wdenkf8062712005-01-09 23:16:25 +0000410
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000411 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000412 printf("I2C write: address 0x%x + 0x%x out of range\n",
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000413 addr, len);
wdenkf8062712005-01-09 23:16:25 +0000414 return 1;
415 }
416
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000417 /* Wait until bus not busy */
Vincent Stehlé33205e32012-12-03 05:23:16 +0000418 if (wait_for_bb())
419 return 1;
Michael Jonesbb54d572011-09-04 14:01:55 -0400420
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000421 /* Start address phase - will write regoffset + len bytes data */
Tom Rini49fbf672012-02-20 18:49:16 +0000422 writew(alen + len, &i2c_base->cnt);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000423 /* Set slave address */
Michael Jonesbb54d572011-09-04 14:01:55 -0400424 writew(chip, &i2c_base->sa);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000425 /* Stop bit needed here */
Michael Jonesbb54d572011-09-04 14:01:55 -0400426 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000427 I2C_CON_STP, &i2c_base->con);
Michael Jonesbb54d572011-09-04 14:01:55 -0400428
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000429 while (alen) {
430 /* Must write reg offset (one or two bytes) */
431 status = wait_for_event();
432 /* Try to identify bus that is not padconf'd for I2C */
433 if (status == I2C_STAT_XRDY) {
434 i2c_error = 2;
435 printf("i2c_write: pads on bus %d probably not configured (status=0x%x)\n",
436 current_bus, status);
437 goto wr_exit;
438 }
Tom Rini49fbf672012-02-20 18:49:16 +0000439 if (status == 0 || status & I2C_STAT_NACK) {
440 i2c_error = 1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000441 printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
442 status);
443 goto wr_exit;
Tom Rini49fbf672012-02-20 18:49:16 +0000444 }
Tom Rini49fbf672012-02-20 18:49:16 +0000445 if (status & I2C_STAT_XRDY) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000446 alen--;
447 writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data);
448 writew(I2C_STAT_XRDY, &i2c_base->stat);
449 } else {
450 i2c_error = 1;
451 printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
452 status);
453 goto wr_exit;
454 }
455 }
456 /* Address phase is over, now write data */
457 for (i = 0; i < len; i++) {
458 status = wait_for_event();
459 if (status == 0 || status & I2C_STAT_NACK) {
460 i2c_error = 1;
461 printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
462 status);
463 goto wr_exit;
464 }
465 if (status & I2C_STAT_XRDY) {
466 writeb(buffer[i], &i2c_base->data);
Tom Rini49fbf672012-02-20 18:49:16 +0000467 writew(I2C_STAT_XRDY, &i2c_base->stat);
468 } else {
469 i2c_error = 1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000470 printf("i2c_write: bus not ready for data Tx (i=%d)\n",
471 i);
472 goto wr_exit;
Patil, Rachnaa9e18c22012-01-22 23:44:12 +0000473 }
474 }
475
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000476wr_exit:
Michael Jonesbb54d572011-09-04 14:01:55 -0400477 flush_fifo();
478 writew(0xFFFF, &i2c_base->stat);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000479 writew(0, &i2c_base->cnt);
Tom Rini49fbf672012-02-20 18:49:16 +0000480 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000481}
482
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000483/*
484 * Wait for the bus to be free by checking the Bus Busy (BB)
485 * bit to become clear
486 */
Vincent Stehlé33205e32012-12-03 05:23:16 +0000487static int wait_for_bb(void)
wdenkf8062712005-01-09 23:16:25 +0000488{
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700489 int timeout = I2C_TIMEOUT;
Tom Rini49fbf672012-02-20 18:49:16 +0000490 u16 stat;
wdenkf8062712005-01-09 23:16:25 +0000491
Tom Rini49fbf672012-02-20 18:49:16 +0000492 writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000493#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
Michael Jones4db67862011-07-27 14:01:55 -0400494 while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000495#else
496 /* Read RAW status */
497 while ((stat = readw(&i2c_base->irqstatus_raw) &
498 I2C_STAT_BB) && timeout--) {
499#endif
Michael Jones4db67862011-07-27 14:01:55 -0400500 writew(stat, &i2c_base->stat);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000501 udelay(I2C_WAIT);
wdenkf8062712005-01-09 23:16:25 +0000502 }
503
504 if (timeout <= 0) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000505 printf("Timed out in wait_for_bb: status=%04x\n",
506 stat);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000507 return 1;
wdenkf8062712005-01-09 23:16:25 +0000508 }
Dirk Behme7a8f6572009-11-02 20:36:26 +0100509 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
Vincent Stehlé33205e32012-12-03 05:23:16 +0000510 return 0;
wdenkf8062712005-01-09 23:16:25 +0000511}
512
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000513/*
514 * Wait for the I2C controller to complete current action
515 * and update status
516 */
517static u16 wait_for_event(void)
wdenkf8062712005-01-09 23:16:25 +0000518{
Tom Rini49fbf672012-02-20 18:49:16 +0000519 u16 status;
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700520 int timeout = I2C_TIMEOUT;
wdenkf8062712005-01-09 23:16:25 +0000521
522 do {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000523 udelay(I2C_WAIT);
524#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
Michael Jones4db67862011-07-27 14:01:55 -0400525 status = readw(&i2c_base->stat);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000526#else
527 /* Read RAW status */
528 status = readw(&i2c_base->irqstatus_raw);
529#endif
Tom Rini49fbf672012-02-20 18:49:16 +0000530 } while (!(status &
531 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
532 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
533 I2C_STAT_AL)) && timeout--);
wdenkf8062712005-01-09 23:16:25 +0000534
535 if (timeout <= 0) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000536 printf("Timed out in wait_for_event: status=%04x\n",
537 status);
538 /*
539 * If status is still 0 here, probably the bus pads have
540 * not been configured for I2C, and/or pull-ups are missing.
541 */
542 printf("Check if pads/pull-ups of bus %d are properly configured\n",
543 current_bus);
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700544 writew(0xFFFF, &i2c_base->stat);
Tom Rini49fbf672012-02-20 18:49:16 +0000545 status = 0;
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700546 }
Tom Rini49fbf672012-02-20 18:49:16 +0000547
wdenkf8062712005-01-09 23:16:25 +0000548 return status;
549}
Dirk Behme7a8f6572009-11-02 20:36:26 +0100550
551int i2c_set_bus_num(unsigned int bus)
552{
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000553 if (bus >= I2C_BUS_MAX) {
554 printf("Bad bus: %x\n", bus);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100555 return -1;
556 }
557
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000558 switch (bus) {
559 default:
560 bus = 0; /* Fall through */
561 case 0:
562 i2c_base = (struct i2c *)I2C_BASE1;
563 break;
564 case 1:
565 i2c_base = (struct i2c *)I2C_BASE2;
566 break;
567#if (I2C_BUS_MAX > 2)
568 case 2:
Koen Kooi584ff5f2012-08-08 00:57:35 +0000569 i2c_base = (struct i2c *)I2C_BASE3;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000570 break;
571#if (I2C_BUS_MAX > 3)
572 case 3:
573 i2c_base = (struct i2c *)I2C_BASE4;
574 break;
575#if (I2C_BUS_MAX > 4)
576 case 4:
577 i2c_base = (struct i2c *)I2C_BASE5;
578 break;
Koen Kooi584ff5f2012-08-08 00:57:35 +0000579#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100580#endif
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000581#endif
582 }
Dirk Behme7a8f6572009-11-02 20:36:26 +0100583
584 current_bus = bus;
585
Michael Jones4db67862011-07-27 14:01:55 -0400586 if (!bus_initialized[current_bus])
Dirk Behme7a8f6572009-11-02 20:36:26 +0100587 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
588
589 return 0;
590}
Steve Sakoman10acc712010-06-12 06:42:57 -0700591
592int i2c_get_bus_num(void)
593{
594 return (int) current_bus;
595}