blob: c7840049b11c707d1eaa9c90899f3d3120d22e2d [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>
Heiko Schocherf53f2b82013-10-22 11:03:18 +020038#include <i2c.h>
wdenkcb99da52005-01-12 00:15:14 +000039
wdenkf8062712005-01-09 23:16:25 +000040#include <asm/arch/i2c.h>
41#include <asm/io.h>
42
Steve Sakoman10acc712010-06-12 06:42:57 -070043#include "omap24xx_i2c.h"
44
John Rigby0d21ed02010-12-20 18:27:51 -070045DECLARE_GLOBAL_DATA_PTR;
46
Tom Rini49fbf672012-02-20 18:49:16 +000047#define I2C_TIMEOUT 1000
Steve Sakomane2bdc132010-07-19 20:31:55 -070048
Lubomir Popov4d98efd2013-06-01 06:44:38 +000049/* Absolutely safe for status update at 100 kHz I2C: */
50#define I2C_WAIT 200
51
Heiko Schocherf53f2b82013-10-22 11:03:18 +020052static int wait_for_bb(struct i2c_adapter *adap);
53static struct i2c *omap24_get_base(struct i2c_adapter *adap);
54static u16 wait_for_event(struct i2c_adapter *adap);
55static void flush_fifo(struct i2c_adapter *adap);
wdenkf8062712005-01-09 23:16:25 +000056
Heiko Schocherf53f2b82013-10-22 11:03:18 +020057static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
wdenkf8062712005-01-09 23:16:25 +000058{
Heiko Schocherf53f2b82013-10-22 11:03:18 +020059 struct i2c *i2c_base = omap24_get_base(adap);
Tom Rix03b2a742009-06-28 12:52:27 -050060 int psc, fsscll, fssclh;
61 int hsscll = 0, hssclh = 0;
62 u32 scll, sclh;
Tom Rini49fbf672012-02-20 18:49:16 +000063 int timeout = I2C_TIMEOUT;
Tom Rix03b2a742009-06-28 12:52:27 -050064
65 /* Only handle standard, fast and high speeds */
66 if ((speed != OMAP_I2C_STANDARD) &&
67 (speed != OMAP_I2C_FAST_MODE) &&
68 (speed != OMAP_I2C_HIGH_SPEED)) {
69 printf("Error : I2C unsupported speed %d\n", speed);
70 return;
71 }
72
73 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
74 psc -= 1;
75 if (psc < I2C_PSC_MIN) {
76 printf("Error : I2C unsupported prescalar %d\n", psc);
77 return;
78 }
79
80 if (speed == OMAP_I2C_HIGH_SPEED) {
81 /* High speed */
82
83 /* For first phase of HS mode */
84 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK /
85 (2 * OMAP_I2C_FAST_MODE);
86
87 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
88 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
89 if (((fsscll < 0) || (fssclh < 0)) ||
90 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +000091 puts("Error : I2C initializing first phase clock\n");
Tom Rix03b2a742009-06-28 12:52:27 -050092 return;
93 }
94
95 /* For second phase of HS mode */
96 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
97
98 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
99 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
100 if (((fsscll < 0) || (fssclh < 0)) ||
101 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000102 puts("Error : I2C initializing second phase clock\n");
Tom Rix03b2a742009-06-28 12:52:27 -0500103 return;
104 }
105
106 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
107 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
108
109 } else {
110 /* Standard and fast speed */
111 fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
112
113 fsscll -= I2C_FASTSPEED_SCLL_TRIM;
114 fssclh -= I2C_FASTSPEED_SCLH_TRIM;
115 if (((fsscll < 0) || (fssclh < 0)) ||
116 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000117 puts("Error : I2C initializing clock\n");
Tom Rix03b2a742009-06-28 12:52:27 -0500118 return;
119 }
120
121 scll = (unsigned int)fsscll;
122 sclh = (unsigned int)fssclh;
123 }
wdenkf8062712005-01-09 23:16:25 +0000124
Michael Jones4db67862011-07-27 14:01:55 -0400125 if (readw(&i2c_base->con) & I2C_CON_EN) {
126 writew(0, &i2c_base->con);
127 udelay(50000);
wdenkf8062712005-01-09 23:16:25 +0000128 }
129
Tom Rini49fbf672012-02-20 18:49:16 +0000130 writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
131 udelay(1000);
132
133 writew(I2C_CON_EN, &i2c_base->con);
134 while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
135 if (timeout <= 0) {
136 puts("ERROR: Timeout in soft-reset\n");
137 return;
138 }
139 udelay(1000);
140 }
141
142 writew(0, &i2c_base->con);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100143 writew(psc, &i2c_base->psc);
144 writew(scll, &i2c_base->scll);
145 writew(sclh, &i2c_base->sclh);
Tom Rix03b2a742009-06-28 12:52:27 -0500146
wdenkf8062712005-01-09 23:16:25 +0000147 /* own address */
Michael Jones4db67862011-07-27 14:01:55 -0400148 writew(slaveadd, &i2c_base->oa);
149 writew(I2C_CON_EN, &i2c_base->con);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000150#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
151 /*
152 * Have to enable interrupts for OMAP2/3, these IPs don't have
153 * an 'irqstatus_raw' register and we shall have to poll 'stat'
154 */
Michael Jones4db67862011-07-27 14:01:55 -0400155 writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000156 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
157#endif
Michael Jones4db67862011-07-27 14:01:55 -0400158 udelay(1000);
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200159 flush_fifo(adap);
Michael Jones4db67862011-07-27 14:01:55 -0400160 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000161}
162
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200163static void flush_fifo(struct i2c_adapter *adap)
164{
165 struct i2c *i2c_base = omap24_get_base(adap);
166 u16 stat;
wdenk2e405bf2005-01-10 00:01:04 +0000167
168 /* note: if you try and read data when its not there or ready
169 * you get a bus error
170 */
Michael Jones4db67862011-07-27 14:01:55 -0400171 while (1) {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100172 stat = readw(&i2c_base->stat);
Michael Jones4db67862011-07-27 14:01:55 -0400173 if (stat == I2C_STAT_RRDY) {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100174 readb(&i2c_base->data);
Michael Jones4db67862011-07-27 14:01:55 -0400175 writew(I2C_STAT_RRDY, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000176 udelay(1000);
Michael Jones4db67862011-07-27 14:01:55 -0400177 } else
wdenkf8062712005-01-09 23:16:25 +0000178 break;
179 }
180}
181
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000182/*
183 * i2c_probe: Use write access. Allows to identify addresses that are
184 * write-only (like the config register of dual-port EEPROMs)
185 */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200186static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
wdenkf8062712005-01-09 23:16:25 +0000187{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200188 struct i2c *i2c_base = omap24_get_base(adap);
Tom Rini49fbf672012-02-20 18:49:16 +0000189 u16 status;
wdenkf8062712005-01-09 23:16:25 +0000190 int res = 1; /* default = fail */
191
Michael Jones4db67862011-07-27 14:01:55 -0400192 if (chip == readw(&i2c_base->oa))
wdenkf8062712005-01-09 23:16:25 +0000193 return res;
wdenkf8062712005-01-09 23:16:25 +0000194
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000195 /* Wait until bus is free */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200196 if (wait_for_bb(adap))
Vincent Stehlé33205e32012-12-03 05:23:16 +0000197 return res;
wdenkf8062712005-01-09 23:16:25 +0000198
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000199 /* No data transfer, slave addr only */
Michael Jones4db67862011-07-27 14:01:55 -0400200 writew(chip, &i2c_base->sa);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000201 /* Stop bit needed here */
202 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
203 I2C_CON_STP, &i2c_base->con);
Nick Thompson48f7ae42011-04-11 22:37:41 +0000204
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200205 status = wait_for_event(adap);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000206
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000207 if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
208 /*
209 * With current high-level command implementation, notifying
210 * the user shall flood the console with 127 messages. If
211 * silent exit is desired upon unconfigured bus, remove the
212 * following 'if' section:
213 */
214 if (status == I2C_STAT_XRDY)
215 printf("i2c_probe: pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200216 adap->hwadapnr, status);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000217
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000218 goto pr_exit;
Tom Rini27eed8b2012-05-21 06:46:29 +0000219 }
Tom Rini49fbf672012-02-20 18:49:16 +0000220
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000221 /* Check for ACK (!NAK) */
222 if (!(status & I2C_STAT_NACK)) {
223 res = 0; /* Device found */
224 udelay(I2C_WAIT); /* Required by AM335X in SPL */
225 /* Abort transfer (force idle state) */
226 writew(I2C_CON_MST | I2C_CON_TRX, &i2c_base->con); /* Reset */
227 udelay(1000);
228 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX |
229 I2C_CON_STP, &i2c_base->con); /* STP */
230 }
231pr_exit:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200232 flush_fifo(adap);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100233 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000234 return res;
235}
236
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000237/*
238 * i2c_read: Function now uses a single I2C read transaction with bulk transfer
239 * of the requested number of bytes (note that the 'i2c md' command
240 * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
241 * defined in the board config header, this transaction shall be with
242 * Repeated Start (Sr) between the address and data phases; otherwise
243 * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
244 * The address (reg offset) may be 0, 1 or 2 bytes long.
245 * Function now reads correctly from chips that return more than one
246 * byte of data per addressed register (like TI temperature sensors),
247 * or that do not need a register address at all (such as some clock
248 * distributors).
249 */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200250static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
251 int alen, uchar *buffer, int len)
wdenkf8062712005-01-09 23:16:25 +0000252{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200253 struct i2c *i2c_base = omap24_get_base(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000254 int i2c_error = 0;
255 u16 status;
256
257 if (alen < 0) {
258 puts("I2C read: addr len < 0\n");
259 return 1;
260 }
261 if (len < 0) {
262 puts("I2C read: data len < 0\n");
263 return 1;
264 }
265 if (buffer == NULL) {
266 puts("I2C read: NULL pointer passed\n");
267 return 1;
268 }
wdenkf8062712005-01-09 23:16:25 +0000269
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000270 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000271 printf("I2C read: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000272 return 1;
273 }
274
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000275 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000276 puts("I2C read: address out of range\n");
wdenkf8062712005-01-09 23:16:25 +0000277 return 1;
278 }
279
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000280 /* Wait until bus not busy */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200281 if (wait_for_bb(adap))
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000282 return 1;
283
284 /* Zero, one or two bytes reg address (offset) */
285 writew(alen, &i2c_base->cnt);
286 /* Set slave address */
287 writew(chip, &i2c_base->sa);
288
289 if (alen) {
290 /* Must write reg offset first */
291#ifdef CONFIG_I2C_REPEATED_START
292 /* No stop bit, use Repeated Start (Sr) */
293 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
294 I2C_CON_TRX, &i2c_base->con);
295#else
296 /* Stop - Start (P-S) */
297 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP |
298 I2C_CON_TRX, &i2c_base->con);
299#endif
300 /* Send register offset */
301 while (1) {
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200302 status = wait_for_event(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000303 /* Try to identify bus that is not padconf'd for I2C */
304 if (status == I2C_STAT_XRDY) {
305 i2c_error = 2;
306 printf("i2c_read (addr phase): pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200307 adap->hwadapnr, status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000308 goto rd_exit;
309 }
310 if (status == 0 || status & I2C_STAT_NACK) {
311 i2c_error = 1;
312 printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
313 status);
314 goto rd_exit;
315 }
316 if (alen) {
317 if (status & I2C_STAT_XRDY) {
318 alen--;
319 /* Do we have to use byte access? */
320 writeb((addr >> (8 * alen)) & 0xff,
321 &i2c_base->data);
322 writew(I2C_STAT_XRDY, &i2c_base->stat);
323 }
324 }
325 if (status & I2C_STAT_ARDY) {
326 writew(I2C_STAT_ARDY, &i2c_base->stat);
327 break;
328 }
wdenkf8062712005-01-09 23:16:25 +0000329 }
330 }
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000331 /* Set slave address */
332 writew(chip, &i2c_base->sa);
333 /* Read len bytes from slave */
334 writew(len, &i2c_base->cnt);
335 /* Need stop bit here */
336 writew(I2C_CON_EN | I2C_CON_MST |
337 I2C_CON_STT | I2C_CON_STP,
338 &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000339
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000340 /* Receive data */
341 while (1) {
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200342 status = wait_for_event(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000343 /*
344 * Try to identify bus that is not padconf'd for I2C. This
345 * state could be left over from previous transactions if
346 * the address phase is skipped due to alen=0.
347 */
348 if (status == I2C_STAT_XRDY) {
349 i2c_error = 2;
350 printf("i2c_read (data phase): pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200351 adap->hwadapnr, status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000352 goto rd_exit;
353 }
354 if (status == 0 || status & I2C_STAT_NACK) {
355 i2c_error = 1;
356 goto rd_exit;
357 }
358 if (status & I2C_STAT_RRDY) {
359 *buffer++ = readb(&i2c_base->data);
360 writew(I2C_STAT_RRDY, &i2c_base->stat);
361 }
362 if (status & I2C_STAT_ARDY) {
363 writew(I2C_STAT_ARDY, &i2c_base->stat);
364 break;
365 }
366 }
367
368rd_exit:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200369 flush_fifo(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000370 writew(0xFFFF, &i2c_base->stat);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000371 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000372}
373
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000374/* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200375static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
376 int alen, uchar *buffer, int len)
wdenkf8062712005-01-09 23:16:25 +0000377{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200378 struct i2c *i2c_base = omap24_get_base(adap);
Tom Rini49fbf672012-02-20 18:49:16 +0000379 int i;
380 u16 status;
381 int i2c_error = 0;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000382
383 if (alen < 0) {
384 puts("I2C write: addr len < 0\n");
385 return 1;
386 }
387
388 if (len < 0) {
389 puts("I2C write: data len < 0\n");
390 return 1;
391 }
392
393 if (buffer == NULL) {
394 puts("I2C write: NULL pointer passed\n");
395 return 1;
396 }
wdenkf8062712005-01-09 23:16:25 +0000397
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000398 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000399 printf("I2C write: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000400 return 1;
Tom Rini49fbf672012-02-20 18:49:16 +0000401 }
wdenkf8062712005-01-09 23:16:25 +0000402
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000403 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000404 printf("I2C write: address 0x%x + 0x%x out of range\n",
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000405 addr, len);
wdenkf8062712005-01-09 23:16:25 +0000406 return 1;
407 }
408
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000409 /* Wait until bus not busy */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200410 if (wait_for_bb(adap))
Vincent Stehlé33205e32012-12-03 05:23:16 +0000411 return 1;
Michael Jonesbb54d572011-09-04 14:01:55 -0400412
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000413 /* Start address phase - will write regoffset + len bytes data */
Tom Rini49fbf672012-02-20 18:49:16 +0000414 writew(alen + len, &i2c_base->cnt);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000415 /* Set slave address */
Michael Jonesbb54d572011-09-04 14:01:55 -0400416 writew(chip, &i2c_base->sa);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000417 /* Stop bit needed here */
Michael Jonesbb54d572011-09-04 14:01:55 -0400418 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000419 I2C_CON_STP, &i2c_base->con);
Michael Jonesbb54d572011-09-04 14:01:55 -0400420
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000421 while (alen) {
422 /* Must write reg offset (one or two bytes) */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200423 status = wait_for_event(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000424 /* Try to identify bus that is not padconf'd for I2C */
425 if (status == I2C_STAT_XRDY) {
426 i2c_error = 2;
427 printf("i2c_write: pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200428 adap->hwadapnr, status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000429 goto wr_exit;
430 }
Tom Rini49fbf672012-02-20 18:49:16 +0000431 if (status == 0 || status & I2C_STAT_NACK) {
432 i2c_error = 1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000433 printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
434 status);
435 goto wr_exit;
Tom Rini49fbf672012-02-20 18:49:16 +0000436 }
Tom Rini49fbf672012-02-20 18:49:16 +0000437 if (status & I2C_STAT_XRDY) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000438 alen--;
439 writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data);
440 writew(I2C_STAT_XRDY, &i2c_base->stat);
441 } else {
442 i2c_error = 1;
443 printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
444 status);
445 goto wr_exit;
446 }
447 }
448 /* Address phase is over, now write data */
449 for (i = 0; i < len; i++) {
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200450 status = wait_for_event(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000451 if (status == 0 || status & I2C_STAT_NACK) {
452 i2c_error = 1;
453 printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
454 status);
455 goto wr_exit;
456 }
457 if (status & I2C_STAT_XRDY) {
458 writeb(buffer[i], &i2c_base->data);
Tom Rini49fbf672012-02-20 18:49:16 +0000459 writew(I2C_STAT_XRDY, &i2c_base->stat);
460 } else {
461 i2c_error = 1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000462 printf("i2c_write: bus not ready for data Tx (i=%d)\n",
463 i);
464 goto wr_exit;
Patil, Rachnaa9e18c22012-01-22 23:44:12 +0000465 }
466 }
467
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000468wr_exit:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200469 flush_fifo(adap);
Michael Jonesbb54d572011-09-04 14:01:55 -0400470 writew(0xFFFF, &i2c_base->stat);
Tom Rini49fbf672012-02-20 18:49:16 +0000471 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000472}
473
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000474/*
475 * Wait for the bus to be free by checking the Bus Busy (BB)
476 * bit to become clear
477 */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200478static int wait_for_bb(struct i2c_adapter *adap)
wdenkf8062712005-01-09 23:16:25 +0000479{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200480 struct i2c *i2c_base = omap24_get_base(adap);
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700481 int timeout = I2C_TIMEOUT;
Tom Rini49fbf672012-02-20 18:49:16 +0000482 u16 stat;
wdenkf8062712005-01-09 23:16:25 +0000483
Tom Rini49fbf672012-02-20 18:49:16 +0000484 writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000485#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
Michael Jones4db67862011-07-27 14:01:55 -0400486 while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000487#else
488 /* Read RAW status */
489 while ((stat = readw(&i2c_base->irqstatus_raw) &
490 I2C_STAT_BB) && timeout--) {
491#endif
Michael Jones4db67862011-07-27 14:01:55 -0400492 writew(stat, &i2c_base->stat);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000493 udelay(I2C_WAIT);
wdenkf8062712005-01-09 23:16:25 +0000494 }
495
496 if (timeout <= 0) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000497 printf("Timed out in wait_for_bb: status=%04x\n",
498 stat);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000499 return 1;
wdenkf8062712005-01-09 23:16:25 +0000500 }
Dirk Behme7a8f6572009-11-02 20:36:26 +0100501 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
Vincent Stehlé33205e32012-12-03 05:23:16 +0000502 return 0;
wdenkf8062712005-01-09 23:16:25 +0000503}
504
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000505/*
506 * Wait for the I2C controller to complete current action
507 * and update status
508 */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200509static u16 wait_for_event(struct i2c_adapter *adap)
wdenkf8062712005-01-09 23:16:25 +0000510{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200511 struct i2c *i2c_base = omap24_get_base(adap);
Tom Rini49fbf672012-02-20 18:49:16 +0000512 u16 status;
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700513 int timeout = I2C_TIMEOUT;
wdenkf8062712005-01-09 23:16:25 +0000514
515 do {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000516 udelay(I2C_WAIT);
517#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
Michael Jones4db67862011-07-27 14:01:55 -0400518 status = readw(&i2c_base->stat);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000519#else
520 /* Read RAW status */
521 status = readw(&i2c_base->irqstatus_raw);
522#endif
Tom Rini49fbf672012-02-20 18:49:16 +0000523 } while (!(status &
524 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
525 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
526 I2C_STAT_AL)) && timeout--);
wdenkf8062712005-01-09 23:16:25 +0000527
528 if (timeout <= 0) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000529 printf("Timed out in wait_for_event: status=%04x\n",
530 status);
531 /*
532 * If status is still 0 here, probably the bus pads have
533 * not been configured for I2C, and/or pull-ups are missing.
534 */
535 printf("Check if pads/pull-ups of bus %d are properly configured\n",
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200536 adap->hwadapnr);
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700537 writew(0xFFFF, &i2c_base->stat);
Tom Rini49fbf672012-02-20 18:49:16 +0000538 status = 0;
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700539 }
Tom Rini49fbf672012-02-20 18:49:16 +0000540
wdenkf8062712005-01-09 23:16:25 +0000541 return status;
542}
Dirk Behme7a8f6572009-11-02 20:36:26 +0100543
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200544static struct i2c *omap24_get_base(struct i2c_adapter *adap)
Dirk Behme7a8f6572009-11-02 20:36:26 +0100545{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200546 switch (adap->hwadapnr) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000547 case 0:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200548 return (struct i2c *)I2C_BASE1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000549 break;
550 case 1:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200551 return (struct i2c *)I2C_BASE2;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000552 break;
553#if (I2C_BUS_MAX > 2)
554 case 2:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200555 return (struct i2c *)I2C_BASE3;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000556 break;
557#if (I2C_BUS_MAX > 3)
558 case 3:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200559 return (struct i2c *)I2C_BASE4;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000560 break;
561#if (I2C_BUS_MAX > 4)
562 case 4:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200563 return (struct i2c *)I2C_BASE5;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000564 break;
Koen Kooi584ff5f2012-08-08 00:57:35 +0000565#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100566#endif
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000567#endif
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200568 default:
569 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
570 break;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000571 }
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200572 return NULL;
573}
Dirk Behme7a8f6572009-11-02 20:36:26 +0100574
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200575#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
576#define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
577#endif
578#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
579#define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
580#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100581
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200582U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
583 omap24_i2c_read, omap24_i2c_write, NULL,
584 CONFIG_SYS_OMAP24_I2C_SPEED,
585 CONFIG_SYS_OMAP24_I2C_SLAVE,
586 0)
587U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
588 omap24_i2c_read, omap24_i2c_write, NULL,
589 CONFIG_SYS_OMAP24_I2C_SPEED1,
590 CONFIG_SYS_OMAP24_I2C_SLAVE1,
591 1)
592#if (I2C_BUS_MAX > 2)
593#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
594#define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
595#endif
596#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
597#define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
598#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100599
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200600U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
601 omap24_i2c_read, omap24_i2c_write, NULL,
602 CONFIG_SYS_OMAP24_I2C_SPEED2,
603 CONFIG_SYS_OMAP24_I2C_SLAVE2,
604 2)
605#if (I2C_BUS_MAX > 3)
606#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
607#define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
608#endif
609#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
610#define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
611#endif
Steve Sakoman10acc712010-06-12 06:42:57 -0700612
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200613U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
614 omap24_i2c_read, omap24_i2c_write, NULL,
615 CONFIG_SYS_OMAP24_I2C_SPEED3,
616 CONFIG_SYS_OMAP24_I2C_SLAVE3,
617 3)
618#if (I2C_BUS_MAX > 4)
619#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
620#define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
621#endif
622#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
623#define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
624#endif
625
626U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
627 omap24_i2c_read, omap24_i2c_write, NULL,
628 CONFIG_SYS_OMAP24_I2C_SPEED4,
629 CONFIG_SYS_OMAP24_I2C_SLAVE4,
630 4)
631#endif
632#endif
633#endif