blob: 0f1e35c460ca375aa517dcb4a9d098927ef91d37 [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).
Hannes Petermaierd5885052014-02-03 21:22:18 +010035 *
36 * Copyright (c) 2014 Hannes Petermaier <oe5hpm@oevsv.at>, B&R
37 * - Added support for set_speed
38 *
wdenkf8062712005-01-09 23:16:25 +000039 */
40
41#include <common.h>
Heiko Schocherf53f2b82013-10-22 11:03:18 +020042#include <i2c.h>
wdenkcb99da52005-01-12 00:15:14 +000043
wdenkf8062712005-01-09 23:16:25 +000044#include <asm/arch/i2c.h>
45#include <asm/io.h>
46
Steve Sakoman10acc712010-06-12 06:42:57 -070047#include "omap24xx_i2c.h"
48
John Rigby0d21ed02010-12-20 18:27:51 -070049DECLARE_GLOBAL_DATA_PTR;
50
Tom Rini49fbf672012-02-20 18:49:16 +000051#define I2C_TIMEOUT 1000
Steve Sakomane2bdc132010-07-19 20:31:55 -070052
Lubomir Popov4d98efd2013-06-01 06:44:38 +000053/* Absolutely safe for status update at 100 kHz I2C: */
54#define I2C_WAIT 200
55
Heiko Schocherf53f2b82013-10-22 11:03:18 +020056static int wait_for_bb(struct i2c_adapter *adap);
57static struct i2c *omap24_get_base(struct i2c_adapter *adap);
58static u16 wait_for_event(struct i2c_adapter *adap);
59static void flush_fifo(struct i2c_adapter *adap);
Hannes Petermaierd5885052014-02-03 21:22:18 +010060static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
wdenkf8062712005-01-09 23:16:25 +000061{
Hannes Petermaierd5885052014-02-03 21:22:18 +010062 unsigned int sampleclk, prescaler;
63 int fsscll, fssclh;
Tom Rix03b2a742009-06-28 12:52:27 -050064
Hannes Petermaierd5885052014-02-03 21:22:18 +010065 speed <<= 1;
66 prescaler = 0;
67 /*
68 * some divisors may cause a precission loss, but shouldn't
69 * be a big thing, because i2c_clk is then allready very slow.
70 */
71 while (prescaler <= 0xFF) {
72 sampleclk = I2C_IP_CLK / (prescaler+1);
Tom Rix03b2a742009-06-28 12:52:27 -050073
Hannes Petermaierd5885052014-02-03 21:22:18 +010074 fsscll = sampleclk / speed;
75 fssclh = fsscll;
76 fsscll -= I2C_FASTSPEED_SCLL_TRIM;
77 fssclh -= I2C_FASTSPEED_SCLH_TRIM;
78
79 if (((fsscll > 0) && (fssclh > 0)) &&
80 ((fsscll <= (255-I2C_FASTSPEED_SCLL_TRIM)) &&
81 (fssclh <= (255-I2C_FASTSPEED_SCLH_TRIM)))) {
82 if (pscl)
83 *pscl = fsscll;
84 if (psch)
85 *psch = fssclh;
86
87 return prescaler;
88 }
89 prescaler++;
Tom Rix03b2a742009-06-28 12:52:27 -050090 }
Hannes Petermaierd5885052014-02-03 21:22:18 +010091 return -1;
92}
93static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
94{
95 struct i2c *i2c_base = omap24_get_base(adap);
96 int psc, fsscll = 0, fssclh = 0;
97 int hsscll = 0, hssclh = 0;
98 u32 scll = 0, sclh = 0;
Tom Rix03b2a742009-06-28 12:52:27 -050099
Hannes Petermaierd5885052014-02-03 21:22:18 +0100100 if (speed >= OMAP_I2C_HIGH_SPEED) {
Tom Rix03b2a742009-06-28 12:52:27 -0500101 /* High speed */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100102 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
103 psc -= 1;
104 if (psc < I2C_PSC_MIN) {
105 printf("Error : I2C unsupported prescaler %d\n", psc);
106 return -1;
107 }
Tom Rix03b2a742009-06-28 12:52:27 -0500108
109 /* For first phase of HS mode */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100110 fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
111
112 fssclh = fsscll;
Tom Rix03b2a742009-06-28 12:52:27 -0500113
114 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
115 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
116 if (((fsscll < 0) || (fssclh < 0)) ||
117 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000118 puts("Error : I2C initializing first phase clock\n");
Hannes Petermaierd5885052014-02-03 21:22:18 +0100119 return -1;
Tom Rix03b2a742009-06-28 12:52:27 -0500120 }
121
122 /* For second phase of HS mode */
123 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
124
125 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
126 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
127 if (((fsscll < 0) || (fssclh < 0)) ||
128 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000129 puts("Error : I2C initializing second phase clock\n");
Hannes Petermaierd5885052014-02-03 21:22:18 +0100130 return -1;
Tom Rix03b2a742009-06-28 12:52:27 -0500131 }
132
133 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
134 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
135
136 } else {
137 /* Standard and fast speed */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100138 psc = omap24_i2c_findpsc(&scll, &sclh, speed);
139 if (0 > psc) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000140 puts("Error : I2C initializing clock\n");
Hannes Petermaierd5885052014-02-03 21:22:18 +0100141 return -1;
Tom Rix03b2a742009-06-28 12:52:27 -0500142 }
Tom Rix03b2a742009-06-28 12:52:27 -0500143 }
wdenkf8062712005-01-09 23:16:25 +0000144
Hannes Petermaierd5885052014-02-03 21:22:18 +0100145 adap->speed = speed;
146 adap->waitdelay = (10000000 / speed) * 2; /* wait for 20 clkperiods */
147 writew(0, &i2c_base->con);
148 writew(psc, &i2c_base->psc);
149 writew(scll, &i2c_base->scll);
150 writew(sclh, &i2c_base->sclh);
151 writew(I2C_CON_EN, &i2c_base->con);
152 writew(0xFFFF, &i2c_base->stat); /* clear all pending status */
153
154 return 0;
155}
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200156
157static void omap24_i2c_deblock(struct i2c_adapter *adap)
158{
159 struct i2c *i2c_base = omap24_get_base(adap);
160 int i;
161 u16 systest;
162 u16 orgsystest;
163
164 /* set test mode ST_EN = 1 */
165 orgsystest = readw(&i2c_base->systest);
166 systest = orgsystest;
167 /* enable testmode */
168 systest |= I2C_SYSTEST_ST_EN;
169 writew(systest, &i2c_base->systest);
170 systest &= ~I2C_SYSTEST_TMODE_MASK;
171 systest |= 3 << I2C_SYSTEST_TMODE_SHIFT;
172 writew(systest, &i2c_base->systest);
173
174 /* set SCL, SDA = 1 */
175 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
176 writew(systest, &i2c_base->systest);
177 udelay(10);
178
179 /* toggle scl 9 clocks */
180 for (i = 0; i < 9; i++) {
181 /* SCL = 0 */
182 systest &= ~I2C_SYSTEST_SCL_O;
183 writew(systest, &i2c_base->systest);
184 udelay(10);
185 /* SCL = 1 */
186 systest |= I2C_SYSTEST_SCL_O;
187 writew(systest, &i2c_base->systest);
188 udelay(10);
189 }
190
191 /* send stop */
192 systest &= ~I2C_SYSTEST_SDA_O;
193 writew(systest, &i2c_base->systest);
194 udelay(10);
195 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
196 writew(systest, &i2c_base->systest);
197 udelay(10);
198
199 /* restore original mode */
200 writew(orgsystest, &i2c_base->systest);
201}
202
Hannes Petermaierd5885052014-02-03 21:22:18 +0100203static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
204{
205 struct i2c *i2c_base = omap24_get_base(adap);
206 int timeout = I2C_TIMEOUT;
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200207 int deblock = 1;
Hannes Petermaierd5885052014-02-03 21:22:18 +0100208
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200209retry:
Michael Jones4db67862011-07-27 14:01:55 -0400210 if (readw(&i2c_base->con) & I2C_CON_EN) {
211 writew(0, &i2c_base->con);
212 udelay(50000);
wdenkf8062712005-01-09 23:16:25 +0000213 }
214
Tom Rini49fbf672012-02-20 18:49:16 +0000215 writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
216 udelay(1000);
217
218 writew(I2C_CON_EN, &i2c_base->con);
219 while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
220 if (timeout <= 0) {
221 puts("ERROR: Timeout in soft-reset\n");
222 return;
223 }
224 udelay(1000);
225 }
226
Hannes Petermaierd5885052014-02-03 21:22:18 +0100227 if (0 != omap24_i2c_setspeed(adap, speed)) {
228 printf("ERROR: failed to setup I2C bus-speed!\n");
229 return;
230 }
Tom Rix03b2a742009-06-28 12:52:27 -0500231
wdenkf8062712005-01-09 23:16:25 +0000232 /* own address */
Michael Jones4db67862011-07-27 14:01:55 -0400233 writew(slaveadd, &i2c_base->oa);
Hannes Petermaierd5885052014-02-03 21:22:18 +0100234
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000235#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
236 /*
237 * Have to enable interrupts for OMAP2/3, these IPs don't have
238 * an 'irqstatus_raw' register and we shall have to poll 'stat'
239 */
Michael Jones4db67862011-07-27 14:01:55 -0400240 writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000241 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
242#endif
Michael Jones4db67862011-07-27 14:01:55 -0400243 udelay(1000);
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200244 flush_fifo(adap);
Michael Jones4db67862011-07-27 14:01:55 -0400245 writew(0xFFFF, &i2c_base->stat);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200246
247 /* Handle possible failed I2C state */
248 if (wait_for_bb(adap))
249 if (deblock == 1) {
250 omap24_i2c_deblock(adap);
251 deblock = 0;
252 goto retry;
253 }
wdenkf8062712005-01-09 23:16:25 +0000254}
255
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200256static void flush_fifo(struct i2c_adapter *adap)
257{
258 struct i2c *i2c_base = omap24_get_base(adap);
259 u16 stat;
wdenk2e405bf2005-01-10 00:01:04 +0000260
Hannes Petermaierd5885052014-02-03 21:22:18 +0100261 /*
262 * note: if you try and read data when its not there or ready
wdenk2e405bf2005-01-10 00:01:04 +0000263 * you get a bus error
264 */
Michael Jones4db67862011-07-27 14:01:55 -0400265 while (1) {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100266 stat = readw(&i2c_base->stat);
Michael Jones4db67862011-07-27 14:01:55 -0400267 if (stat == I2C_STAT_RRDY) {
Dirk Behme7a8f6572009-11-02 20:36:26 +0100268 readb(&i2c_base->data);
Michael Jones4db67862011-07-27 14:01:55 -0400269 writew(I2C_STAT_RRDY, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000270 udelay(1000);
Michael Jones4db67862011-07-27 14:01:55 -0400271 } else
wdenkf8062712005-01-09 23:16:25 +0000272 break;
273 }
274}
275
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000276/*
277 * i2c_probe: Use write access. Allows to identify addresses that are
278 * write-only (like the config register of dual-port EEPROMs)
279 */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200280static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
wdenkf8062712005-01-09 23:16:25 +0000281{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200282 struct i2c *i2c_base = omap24_get_base(adap);
Tom Rini49fbf672012-02-20 18:49:16 +0000283 u16 status;
wdenkf8062712005-01-09 23:16:25 +0000284 int res = 1; /* default = fail */
285
Michael Jones4db67862011-07-27 14:01:55 -0400286 if (chip == readw(&i2c_base->oa))
wdenkf8062712005-01-09 23:16:25 +0000287 return res;
wdenkf8062712005-01-09 23:16:25 +0000288
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000289 /* Wait until bus is free */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200290 if (wait_for_bb(adap))
Vincent Stehlé33205e32012-12-03 05:23:16 +0000291 return res;
wdenkf8062712005-01-09 23:16:25 +0000292
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000293 /* No data transfer, slave addr only */
Michael Jones4db67862011-07-27 14:01:55 -0400294 writew(chip, &i2c_base->sa);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000295 /* Stop bit needed here */
296 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
297 I2C_CON_STP, &i2c_base->con);
Nick Thompson48f7ae42011-04-11 22:37:41 +0000298
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200299 status = wait_for_event(adap);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000300
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000301 if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
302 /*
303 * With current high-level command implementation, notifying
304 * the user shall flood the console with 127 messages. If
305 * silent exit is desired upon unconfigured bus, remove the
306 * following 'if' section:
307 */
308 if (status == I2C_STAT_XRDY)
309 printf("i2c_probe: pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200310 adap->hwadapnr, status);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000311
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000312 goto pr_exit;
Tom Rini27eed8b2012-05-21 06:46:29 +0000313 }
Tom Rini49fbf672012-02-20 18:49:16 +0000314
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000315 /* Check for ACK (!NAK) */
316 if (!(status & I2C_STAT_NACK)) {
Hannes Petermaierd5885052014-02-03 21:22:18 +0100317 res = 0; /* Device found */
318 udelay(adap->waitdelay);/* Required by AM335X in SPL */
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000319 /* Abort transfer (force idle state) */
320 writew(I2C_CON_MST | I2C_CON_TRX, &i2c_base->con); /* Reset */
321 udelay(1000);
322 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX |
323 I2C_CON_STP, &i2c_base->con); /* STP */
324 }
325pr_exit:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200326 flush_fifo(adap);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100327 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000328 return res;
329}
330
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000331/*
332 * i2c_read: Function now uses a single I2C read transaction with bulk transfer
333 * of the requested number of bytes (note that the 'i2c md' command
334 * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
335 * defined in the board config header, this transaction shall be with
336 * Repeated Start (Sr) between the address and data phases; otherwise
337 * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
338 * The address (reg offset) may be 0, 1 or 2 bytes long.
339 * Function now reads correctly from chips that return more than one
340 * byte of data per addressed register (like TI temperature sensors),
341 * or that do not need a register address at all (such as some clock
342 * distributors).
343 */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200344static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
345 int alen, uchar *buffer, int len)
wdenkf8062712005-01-09 23:16:25 +0000346{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200347 struct i2c *i2c_base = omap24_get_base(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000348 int i2c_error = 0;
349 u16 status;
350
351 if (alen < 0) {
352 puts("I2C read: addr len < 0\n");
353 return 1;
354 }
355 if (len < 0) {
356 puts("I2C read: data len < 0\n");
357 return 1;
358 }
359 if (buffer == NULL) {
360 puts("I2C read: NULL pointer passed\n");
361 return 1;
362 }
wdenkf8062712005-01-09 23:16:25 +0000363
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000364 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000365 printf("I2C read: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000366 return 1;
367 }
368
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000369 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000370 puts("I2C read: address out of range\n");
wdenkf8062712005-01-09 23:16:25 +0000371 return 1;
372 }
373
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000374 /* Wait until bus not busy */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200375 if (wait_for_bb(adap))
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000376 return 1;
377
378 /* Zero, one or two bytes reg address (offset) */
379 writew(alen, &i2c_base->cnt);
380 /* Set slave address */
381 writew(chip, &i2c_base->sa);
382
383 if (alen) {
384 /* Must write reg offset first */
385#ifdef CONFIG_I2C_REPEATED_START
386 /* No stop bit, use Repeated Start (Sr) */
387 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
388 I2C_CON_TRX, &i2c_base->con);
389#else
390 /* Stop - Start (P-S) */
391 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP |
392 I2C_CON_TRX, &i2c_base->con);
393#endif
394 /* Send register offset */
395 while (1) {
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200396 status = wait_for_event(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000397 /* Try to identify bus that is not padconf'd for I2C */
398 if (status == I2C_STAT_XRDY) {
399 i2c_error = 2;
400 printf("i2c_read (addr phase): pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200401 adap->hwadapnr, status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000402 goto rd_exit;
403 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100404 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000405 i2c_error = 1;
406 printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
407 status);
408 goto rd_exit;
409 }
410 if (alen) {
411 if (status & I2C_STAT_XRDY) {
412 alen--;
413 /* Do we have to use byte access? */
414 writeb((addr >> (8 * alen)) & 0xff,
415 &i2c_base->data);
416 writew(I2C_STAT_XRDY, &i2c_base->stat);
417 }
418 }
419 if (status & I2C_STAT_ARDY) {
420 writew(I2C_STAT_ARDY, &i2c_base->stat);
421 break;
422 }
wdenkf8062712005-01-09 23:16:25 +0000423 }
424 }
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000425 /* Set slave address */
426 writew(chip, &i2c_base->sa);
427 /* Read len bytes from slave */
428 writew(len, &i2c_base->cnt);
429 /* Need stop bit here */
430 writew(I2C_CON_EN | I2C_CON_MST |
431 I2C_CON_STT | I2C_CON_STP,
432 &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000433
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000434 /* Receive data */
435 while (1) {
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200436 status = wait_for_event(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000437 /*
438 * Try to identify bus that is not padconf'd for I2C. This
439 * state could be left over from previous transactions if
440 * the address phase is skipped due to alen=0.
441 */
442 if (status == I2C_STAT_XRDY) {
443 i2c_error = 2;
444 printf("i2c_read (data phase): pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200445 adap->hwadapnr, status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000446 goto rd_exit;
447 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100448 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000449 i2c_error = 1;
450 goto rd_exit;
451 }
452 if (status & I2C_STAT_RRDY) {
453 *buffer++ = readb(&i2c_base->data);
454 writew(I2C_STAT_RRDY, &i2c_base->stat);
455 }
456 if (status & I2C_STAT_ARDY) {
457 writew(I2C_STAT_ARDY, &i2c_base->stat);
458 break;
459 }
460 }
461
462rd_exit:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200463 flush_fifo(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000464 writew(0xFFFF, &i2c_base->stat);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000465 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000466}
467
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000468/* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200469static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
470 int alen, uchar *buffer, int len)
wdenkf8062712005-01-09 23:16:25 +0000471{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200472 struct i2c *i2c_base = omap24_get_base(adap);
Tom Rini49fbf672012-02-20 18:49:16 +0000473 int i;
474 u16 status;
475 int i2c_error = 0;
Hannes Petermaierd5885052014-02-03 21:22:18 +0100476 int timeout = I2C_TIMEOUT;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000477
478 if (alen < 0) {
479 puts("I2C write: addr len < 0\n");
480 return 1;
481 }
482
483 if (len < 0) {
484 puts("I2C write: data len < 0\n");
485 return 1;
486 }
487
488 if (buffer == NULL) {
489 puts("I2C write: NULL pointer passed\n");
490 return 1;
491 }
wdenkf8062712005-01-09 23:16:25 +0000492
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000493 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000494 printf("I2C write: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000495 return 1;
Tom Rini49fbf672012-02-20 18:49:16 +0000496 }
wdenkf8062712005-01-09 23:16:25 +0000497
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000498 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000499 printf("I2C write: address 0x%x + 0x%x out of range\n",
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000500 addr, len);
wdenkf8062712005-01-09 23:16:25 +0000501 return 1;
502 }
503
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000504 /* Wait until bus not busy */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200505 if (wait_for_bb(adap))
Vincent Stehlé33205e32012-12-03 05:23:16 +0000506 return 1;
Michael Jonesbb54d572011-09-04 14:01:55 -0400507
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000508 /* Start address phase - will write regoffset + len bytes data */
Tom Rini49fbf672012-02-20 18:49:16 +0000509 writew(alen + len, &i2c_base->cnt);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000510 /* Set slave address */
Michael Jonesbb54d572011-09-04 14:01:55 -0400511 writew(chip, &i2c_base->sa);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000512 /* Stop bit needed here */
Michael Jonesbb54d572011-09-04 14:01:55 -0400513 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000514 I2C_CON_STP, &i2c_base->con);
Michael Jonesbb54d572011-09-04 14:01:55 -0400515
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000516 while (alen) {
517 /* Must write reg offset (one or two bytes) */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200518 status = wait_for_event(adap);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000519 /* Try to identify bus that is not padconf'd for I2C */
520 if (status == I2C_STAT_XRDY) {
521 i2c_error = 2;
522 printf("i2c_write: pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200523 adap->hwadapnr, status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000524 goto wr_exit;
525 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100526 if (status == 0 || (status & I2C_STAT_NACK)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000527 i2c_error = 1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000528 printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
529 status);
530 goto wr_exit;
Tom Rini49fbf672012-02-20 18:49:16 +0000531 }
Tom Rini49fbf672012-02-20 18:49:16 +0000532 if (status & I2C_STAT_XRDY) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000533 alen--;
534 writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data);
535 writew(I2C_STAT_XRDY, &i2c_base->stat);
536 } else {
537 i2c_error = 1;
538 printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
539 status);
540 goto wr_exit;
541 }
542 }
543 /* Address phase is over, now write data */
544 for (i = 0; i < len; i++) {
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200545 status = wait_for_event(adap);
Hannes Petermaierd5885052014-02-03 21:22:18 +0100546 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000547 i2c_error = 1;
548 printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
549 status);
550 goto wr_exit;
551 }
552 if (status & I2C_STAT_XRDY) {
553 writeb(buffer[i], &i2c_base->data);
Tom Rini49fbf672012-02-20 18:49:16 +0000554 writew(I2C_STAT_XRDY, &i2c_base->stat);
555 } else {
556 i2c_error = 1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000557 printf("i2c_write: bus not ready for data Tx (i=%d)\n",
558 i);
559 goto wr_exit;
Patil, Rachnaa9e18c22012-01-22 23:44:12 +0000560 }
561 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100562 /*
563 * poll ARDY bit for making sure that last byte really has been
564 * transferred on the bus.
565 */
566 do {
567 status = wait_for_event(adap);
568 } while (!(status & I2C_STAT_ARDY) && timeout--);
569 if (timeout <= 0)
570 printf("i2c_write: timed out writig last byte!\n");
Patil, Rachnaa9e18c22012-01-22 23:44:12 +0000571
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000572wr_exit:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200573 flush_fifo(adap);
Michael Jonesbb54d572011-09-04 14:01:55 -0400574 writew(0xFFFF, &i2c_base->stat);
Tom Rini49fbf672012-02-20 18:49:16 +0000575 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000576}
577
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000578/*
579 * Wait for the bus to be free by checking the Bus Busy (BB)
580 * bit to become clear
581 */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200582static int wait_for_bb(struct i2c_adapter *adap)
wdenkf8062712005-01-09 23:16:25 +0000583{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200584 struct i2c *i2c_base = omap24_get_base(adap);
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700585 int timeout = I2C_TIMEOUT;
Tom Rini49fbf672012-02-20 18:49:16 +0000586 u16 stat;
wdenkf8062712005-01-09 23:16:25 +0000587
Tom Rini49fbf672012-02-20 18:49:16 +0000588 writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000589#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
Michael Jones4db67862011-07-27 14:01:55 -0400590 while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000591#else
592 /* Read RAW status */
593 while ((stat = readw(&i2c_base->irqstatus_raw) &
594 I2C_STAT_BB) && timeout--) {
595#endif
Michael Jones4db67862011-07-27 14:01:55 -0400596 writew(stat, &i2c_base->stat);
Hannes Petermaierd5885052014-02-03 21:22:18 +0100597 udelay(adap->waitdelay);
wdenkf8062712005-01-09 23:16:25 +0000598 }
599
600 if (timeout <= 0) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000601 printf("Timed out in wait_for_bb: status=%04x\n",
602 stat);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000603 return 1;
wdenkf8062712005-01-09 23:16:25 +0000604 }
Dirk Behme7a8f6572009-11-02 20:36:26 +0100605 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
Vincent Stehlé33205e32012-12-03 05:23:16 +0000606 return 0;
wdenkf8062712005-01-09 23:16:25 +0000607}
608
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000609/*
610 * Wait for the I2C controller to complete current action
611 * and update status
612 */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200613static u16 wait_for_event(struct i2c_adapter *adap)
wdenkf8062712005-01-09 23:16:25 +0000614{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200615 struct i2c *i2c_base = omap24_get_base(adap);
Tom Rini49fbf672012-02-20 18:49:16 +0000616 u16 status;
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700617 int timeout = I2C_TIMEOUT;
wdenkf8062712005-01-09 23:16:25 +0000618
619 do {
Hannes Petermaierd5885052014-02-03 21:22:18 +0100620 udelay(adap->waitdelay);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000621#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
Michael Jones4db67862011-07-27 14:01:55 -0400622 status = readw(&i2c_base->stat);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000623#else
624 /* Read RAW status */
625 status = readw(&i2c_base->irqstatus_raw);
626#endif
Tom Rini49fbf672012-02-20 18:49:16 +0000627 } while (!(status &
628 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
629 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
630 I2C_STAT_AL)) && timeout--);
wdenkf8062712005-01-09 23:16:25 +0000631
632 if (timeout <= 0) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000633 printf("Timed out in wait_for_event: status=%04x\n",
634 status);
635 /*
636 * If status is still 0 here, probably the bus pads have
637 * not been configured for I2C, and/or pull-ups are missing.
638 */
639 printf("Check if pads/pull-ups of bus %d are properly configured\n",
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200640 adap->hwadapnr);
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700641 writew(0xFFFF, &i2c_base->stat);
Tom Rini49fbf672012-02-20 18:49:16 +0000642 status = 0;
Steve Sakomanfb5c39a2010-10-20 06:07:44 -0700643 }
Tom Rini49fbf672012-02-20 18:49:16 +0000644
wdenkf8062712005-01-09 23:16:25 +0000645 return status;
646}
Dirk Behme7a8f6572009-11-02 20:36:26 +0100647
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200648static struct i2c *omap24_get_base(struct i2c_adapter *adap)
Dirk Behme7a8f6572009-11-02 20:36:26 +0100649{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200650 switch (adap->hwadapnr) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000651 case 0:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200652 return (struct i2c *)I2C_BASE1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000653 break;
654 case 1:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200655 return (struct i2c *)I2C_BASE2;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000656 break;
657#if (I2C_BUS_MAX > 2)
658 case 2:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200659 return (struct i2c *)I2C_BASE3;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000660 break;
661#if (I2C_BUS_MAX > 3)
662 case 3:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200663 return (struct i2c *)I2C_BASE4;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000664 break;
665#if (I2C_BUS_MAX > 4)
666 case 4:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200667 return (struct i2c *)I2C_BASE5;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000668 break;
Koen Kooi584ff5f2012-08-08 00:57:35 +0000669#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100670#endif
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000671#endif
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200672 default:
673 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
674 break;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000675 }
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200676 return NULL;
677}
Dirk Behme7a8f6572009-11-02 20:36:26 +0100678
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200679#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
680#define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
681#endif
682#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
683#define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
684#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100685
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200686U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5885052014-02-03 21:22:18 +0100687 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200688 CONFIG_SYS_OMAP24_I2C_SPEED,
689 CONFIG_SYS_OMAP24_I2C_SLAVE,
690 0)
691U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5885052014-02-03 21:22:18 +0100692 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200693 CONFIG_SYS_OMAP24_I2C_SPEED1,
694 CONFIG_SYS_OMAP24_I2C_SLAVE1,
695 1)
696#if (I2C_BUS_MAX > 2)
697#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
698#define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
699#endif
700#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
701#define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
702#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100703
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200704U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
705 omap24_i2c_read, omap24_i2c_write, NULL,
706 CONFIG_SYS_OMAP24_I2C_SPEED2,
707 CONFIG_SYS_OMAP24_I2C_SLAVE2,
708 2)
709#if (I2C_BUS_MAX > 3)
710#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
711#define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
712#endif
713#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
714#define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
715#endif
Steve Sakoman10acc712010-06-12 06:42:57 -0700716
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200717U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
718 omap24_i2c_read, omap24_i2c_write, NULL,
719 CONFIG_SYS_OMAP24_I2C_SPEED3,
720 CONFIG_SYS_OMAP24_I2C_SLAVE3,
721 3)
722#if (I2C_BUS_MAX > 4)
723#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
724#define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
725#endif
726#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
727#define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
728#endif
729
730U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
731 omap24_i2c_read, omap24_i2c_write, NULL,
732 CONFIG_SYS_OMAP24_I2C_SPEED4,
733 CONFIG_SYS_OMAP24_I2C_SLAVE4,
734 4)
735#endif
736#endif
737#endif