blob: a39b5917ecd89f0bb11f048e4cde9d721250e574 [file] [log] [blame]
wdenk8ed96042005-01-09 23:16:25 +00001/*
2 * Basic I2C functions
3 *
4 * Copyright (c) 2004 Texas Instruments
5 *
6 * This package is free software; you can redistribute it and/or
7 * modify it under the terms of the license found in the file
8 * named COPYING that should have accompanied this file.
9 *
10 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
11 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Author: Jian Zhang jzhang@ti.com, Texas Instruments
15 *
16 * Copyright (c) 2003 Wolfgang Denk, wd@denx.de
17 * Rewritten to fit into the current U-Boot framework
18 *
19 * Adapted for OMAP2420 I2C, r-woodruff2@ti.com
20 *
Lubomir Popov960187f2013-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 Petermaierd5243352014-02-03 21:22:18 +010035 *
36 * Copyright (c) 2014 Hannes Petermaier <oe5hpm@oevsv.at>, B&R
37 * - Added support for set_speed
38 *
wdenk8ed96042005-01-09 23:16:25 +000039 */
40
41#include <common.h>
Heiko Schocher6789e842013-10-22 11:03:18 +020042#include <i2c.h>
wdenk289f9322005-01-12 00:15:14 +000043
wdenk8ed96042005-01-09 23:16:25 +000044#include <asm/arch/i2c.h>
45#include <asm/io.h>
46
Steve Sakoman938717c2010-06-12 06:42:57 -070047#include "omap24xx_i2c.h"
48
John Rigby29565322010-12-20 18:27:51 -070049DECLARE_GLOBAL_DATA_PTR;
50
Tom Rinicec487a2012-02-20 18:49:16 +000051#define I2C_TIMEOUT 1000
Steve Sakomand7083952010-07-19 20:31:55 -070052
Lubomir Popov960187f2013-06-01 06:44:38 +000053/* Absolutely safe for status update at 100 kHz I2C: */
54#define I2C_WAIT 200
55
Heiko Schocher6789e842013-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 Petermaierd5243352014-02-03 21:22:18 +010060static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
61{
62 unsigned int sampleclk, prescaler;
63 int fsscll, fssclh;
wdenk8ed96042005-01-09 23:16:25 +000064
Hannes Petermaierd5243352014-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);
73
74 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++;
90 }
91 return -1;
92}
93static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
wdenk8ed96042005-01-09 23:16:25 +000094{
Heiko Schocher6789e842013-10-22 11:03:18 +020095 struct i2c *i2c_base = omap24_get_base(adap);
Hannes Petermaierd5243352014-02-03 21:22:18 +010096 int psc, fsscll = 0, fssclh = 0;
Tom Rix7f79dfb2009-06-28 12:52:27 -050097 int hsscll = 0, hssclh = 0;
Hannes Petermaierd5243352014-02-03 21:22:18 +010098 u32 scll = 0, sclh = 0;
Tom Rix7f79dfb2009-06-28 12:52:27 -050099
Hannes Petermaierd5243352014-02-03 21:22:18 +0100100 if (speed >= OMAP_I2C_HIGH_SPEED) {
Tom Rix7f79dfb2009-06-28 12:52:27 -0500101 /* High speed */
Hannes Petermaierd5243352014-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 Rix7f79dfb2009-06-28 12:52:27 -0500108
109 /* For first phase of HS mode */
Hannes Petermaierd5243352014-02-03 21:22:18 +0100110 fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
111
112 fssclh = fsscll;
Tom Rix7f79dfb2009-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üller49e9b4b2012-01-04 15:26:19 +0000118 puts("Error : I2C initializing first phase clock\n");
Hannes Petermaierd5243352014-02-03 21:22:18 +0100119 return -1;
Tom Rix7f79dfb2009-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üller49e9b4b2012-01-04 15:26:19 +0000129 puts("Error : I2C initializing second phase clock\n");
Hannes Petermaierd5243352014-02-03 21:22:18 +0100130 return -1;
Tom Rix7f79dfb2009-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 Petermaierd5243352014-02-03 21:22:18 +0100138 psc = omap24_i2c_findpsc(&scll, &sclh, speed);
139 if (0 > psc) {
Andreas Müller49e9b4b2012-01-04 15:26:19 +0000140 puts("Error : I2C initializing clock\n");
Hannes Petermaierd5243352014-02-03 21:22:18 +0100141 return -1;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500142 }
Tom Rix7f79dfb2009-06-28 12:52:27 -0500143 }
wdenk8ed96042005-01-09 23:16:25 +0000144
Hannes Petermaierd5243352014-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}
156static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
157{
158 struct i2c *i2c_base = omap24_get_base(adap);
159 int timeout = I2C_TIMEOUT;
160
Michael Jones89677b22011-07-27 14:01:55 -0400161 if (readw(&i2c_base->con) & I2C_CON_EN) {
162 writew(0, &i2c_base->con);
163 udelay(50000);
wdenk8ed96042005-01-09 23:16:25 +0000164 }
165
Tom Rinicec487a2012-02-20 18:49:16 +0000166 writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
167 udelay(1000);
168
169 writew(I2C_CON_EN, &i2c_base->con);
170 while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
171 if (timeout <= 0) {
172 puts("ERROR: Timeout in soft-reset\n");
173 return;
174 }
175 udelay(1000);
176 }
177
Hannes Petermaierd5243352014-02-03 21:22:18 +0100178 if (0 != omap24_i2c_setspeed(adap, speed)) {
179 printf("ERROR: failed to setup I2C bus-speed!\n");
180 return;
181 }
Tom Rix7f79dfb2009-06-28 12:52:27 -0500182
wdenk8ed96042005-01-09 23:16:25 +0000183 /* own address */
Michael Jones89677b22011-07-27 14:01:55 -0400184 writew(slaveadd, &i2c_base->oa);
Hannes Petermaierd5243352014-02-03 21:22:18 +0100185
Lubomir Popov960187f2013-06-01 06:44:38 +0000186#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
187 /*
188 * Have to enable interrupts for OMAP2/3, these IPs don't have
189 * an 'irqstatus_raw' register and we shall have to poll 'stat'
190 */
Michael Jones89677b22011-07-27 14:01:55 -0400191 writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
Lubomir Popov960187f2013-06-01 06:44:38 +0000192 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
193#endif
Michael Jones89677b22011-07-27 14:01:55 -0400194 udelay(1000);
Heiko Schocher6789e842013-10-22 11:03:18 +0200195 flush_fifo(adap);
Michael Jones89677b22011-07-27 14:01:55 -0400196 writew(0xFFFF, &i2c_base->stat);
Tom Rinicec487a2012-02-20 18:49:16 +0000197}
198
Heiko Schocher6789e842013-10-22 11:03:18 +0200199static void flush_fifo(struct i2c_adapter *adap)
200{
201 struct i2c *i2c_base = omap24_get_base(adap);
202 u16 stat;
wdenk082acfd2005-01-10 00:01:04 +0000203
Hannes Petermaierd5243352014-02-03 21:22:18 +0100204 /*
205 * note: if you try and read data when its not there or ready
wdenk082acfd2005-01-10 00:01:04 +0000206 * you get a bus error
207 */
Michael Jones89677b22011-07-27 14:01:55 -0400208 while (1) {
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100209 stat = readw(&i2c_base->stat);
Michael Jones89677b22011-07-27 14:01:55 -0400210 if (stat == I2C_STAT_RRDY) {
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100211 readb(&i2c_base->data);
Michael Jones89677b22011-07-27 14:01:55 -0400212 writew(I2C_STAT_RRDY, &i2c_base->stat);
wdenk8ed96042005-01-09 23:16:25 +0000213 udelay(1000);
Michael Jones89677b22011-07-27 14:01:55 -0400214 } else
wdenk8ed96042005-01-09 23:16:25 +0000215 break;
216 }
217}
218
Lubomir Popov960187f2013-06-01 06:44:38 +0000219/*
220 * i2c_probe: Use write access. Allows to identify addresses that are
221 * write-only (like the config register of dual-port EEPROMs)
222 */
Heiko Schocher6789e842013-10-22 11:03:18 +0200223static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
wdenk8ed96042005-01-09 23:16:25 +0000224{
Heiko Schocher6789e842013-10-22 11:03:18 +0200225 struct i2c *i2c_base = omap24_get_base(adap);
Tom Rinicec487a2012-02-20 18:49:16 +0000226 u16 status;
wdenk8ed96042005-01-09 23:16:25 +0000227 int res = 1; /* default = fail */
228
Michael Jones89677b22011-07-27 14:01:55 -0400229 if (chip == readw(&i2c_base->oa))
wdenk8ed96042005-01-09 23:16:25 +0000230 return res;
wdenk8ed96042005-01-09 23:16:25 +0000231
Lubomir Popov960187f2013-06-01 06:44:38 +0000232 /* Wait until bus is free */
Heiko Schocher6789e842013-10-22 11:03:18 +0200233 if (wait_for_bb(adap))
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000234 return res;
wdenk8ed96042005-01-09 23:16:25 +0000235
Lubomir Popov960187f2013-06-01 06:44:38 +0000236 /* No data transfer, slave addr only */
Michael Jones89677b22011-07-27 14:01:55 -0400237 writew(chip, &i2c_base->sa);
Lubomir Popov960187f2013-06-01 06:44:38 +0000238 /* Stop bit needed here */
239 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
240 I2C_CON_STP, &i2c_base->con);
wdenk8ed96042005-01-09 23:16:25 +0000241
Heiko Schocher6789e842013-10-22 11:03:18 +0200242 status = wait_for_event(adap);
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000243
Lubomir Popov960187f2013-06-01 06:44:38 +0000244 if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
245 /*
246 * With current high-level command implementation, notifying
247 * the user shall flood the console with 127 messages. If
248 * silent exit is desired upon unconfigured bus, remove the
249 * following 'if' section:
250 */
251 if (status == I2C_STAT_XRDY)
252 printf("i2c_probe: pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocher6789e842013-10-22 11:03:18 +0200253 adap->hwadapnr, status);
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000254
Lubomir Popov960187f2013-06-01 06:44:38 +0000255 goto pr_exit;
Tom Rini168a5ac2012-05-21 06:46:29 +0000256 }
Tom Rinicec487a2012-02-20 18:49:16 +0000257
Lubomir Popov960187f2013-06-01 06:44:38 +0000258 /* Check for ACK (!NAK) */
259 if (!(status & I2C_STAT_NACK)) {
Hannes Petermaierd5243352014-02-03 21:22:18 +0100260 res = 0; /* Device found */
261 udelay(adap->waitdelay);/* Required by AM335X in SPL */
Lubomir Popov960187f2013-06-01 06:44:38 +0000262 /* Abort transfer (force idle state) */
263 writew(I2C_CON_MST | I2C_CON_TRX, &i2c_base->con); /* Reset */
264 udelay(1000);
265 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX |
266 I2C_CON_STP, &i2c_base->con); /* STP */
267 }
268pr_exit:
Heiko Schocher6789e842013-10-22 11:03:18 +0200269 flush_fifo(adap);
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100270 writew(0xFFFF, &i2c_base->stat);
wdenk8ed96042005-01-09 23:16:25 +0000271 return res;
272}
273
Lubomir Popov960187f2013-06-01 06:44:38 +0000274/*
275 * i2c_read: Function now uses a single I2C read transaction with bulk transfer
276 * of the requested number of bytes (note that the 'i2c md' command
277 * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
278 * defined in the board config header, this transaction shall be with
279 * Repeated Start (Sr) between the address and data phases; otherwise
280 * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
281 * The address (reg offset) may be 0, 1 or 2 bytes long.
282 * Function now reads correctly from chips that return more than one
283 * byte of data per addressed register (like TI temperature sensors),
284 * or that do not need a register address at all (such as some clock
285 * distributors).
286 */
Heiko Schocher6789e842013-10-22 11:03:18 +0200287static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
288 int alen, uchar *buffer, int len)
wdenk8ed96042005-01-09 23:16:25 +0000289{
Heiko Schocher6789e842013-10-22 11:03:18 +0200290 struct i2c *i2c_base = omap24_get_base(adap);
Lubomir Popov960187f2013-06-01 06:44:38 +0000291 int i2c_error = 0;
292 u16 status;
293
294 if (alen < 0) {
295 puts("I2C read: addr len < 0\n");
296 return 1;
297 }
298 if (len < 0) {
299 puts("I2C read: data len < 0\n");
300 return 1;
301 }
302 if (buffer == NULL) {
303 puts("I2C read: NULL pointer passed\n");
304 return 1;
305 }
wdenk8ed96042005-01-09 23:16:25 +0000306
Ilya Yanok55faa582012-06-08 03:12:09 +0000307 if (alen > 2) {
Tom Rinicec487a2012-02-20 18:49:16 +0000308 printf("I2C read: addr len %d not supported\n", alen);
wdenk8ed96042005-01-09 23:16:25 +0000309 return 1;
Tom Rinicec487a2012-02-20 18:49:16 +0000310 }
wdenk8ed96042005-01-09 23:16:25 +0000311
Ilya Yanok55faa582012-06-08 03:12:09 +0000312 if (addr + len > (1 << 16)) {
Tom Rinicec487a2012-02-20 18:49:16 +0000313 puts("I2C read: address out of range\n");
314 return 1;
315 }
316
Lubomir Popov960187f2013-06-01 06:44:38 +0000317 /* Wait until bus not busy */
Heiko Schocher6789e842013-10-22 11:03:18 +0200318 if (wait_for_bb(adap))
Lubomir Popov960187f2013-06-01 06:44:38 +0000319 return 1;
320
321 /* Zero, one or two bytes reg address (offset) */
322 writew(alen, &i2c_base->cnt);
323 /* Set slave address */
324 writew(chip, &i2c_base->sa);
325
326 if (alen) {
327 /* Must write reg offset first */
328#ifdef CONFIG_I2C_REPEATED_START
329 /* No stop bit, use Repeated Start (Sr) */
330 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
331 I2C_CON_TRX, &i2c_base->con);
332#else
333 /* Stop - Start (P-S) */
334 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP |
335 I2C_CON_TRX, &i2c_base->con);
336#endif
337 /* Send register offset */
338 while (1) {
Heiko Schocher6789e842013-10-22 11:03:18 +0200339 status = wait_for_event(adap);
Lubomir Popov960187f2013-06-01 06:44:38 +0000340 /* Try to identify bus that is not padconf'd for I2C */
341 if (status == I2C_STAT_XRDY) {
342 i2c_error = 2;
343 printf("i2c_read (addr phase): pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocher6789e842013-10-22 11:03:18 +0200344 adap->hwadapnr, status);
Lubomir Popov960187f2013-06-01 06:44:38 +0000345 goto rd_exit;
346 }
Hannes Petermaierd5243352014-02-03 21:22:18 +0100347 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000348 i2c_error = 1;
349 printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
350 status);
351 goto rd_exit;
352 }
353 if (alen) {
354 if (status & I2C_STAT_XRDY) {
355 alen--;
356 /* Do we have to use byte access? */
357 writeb((addr >> (8 * alen)) & 0xff,
358 &i2c_base->data);
359 writew(I2C_STAT_XRDY, &i2c_base->stat);
360 }
361 }
362 if (status & I2C_STAT_ARDY) {
363 writew(I2C_STAT_ARDY, &i2c_base->stat);
364 break;
365 }
366 }
367 }
368 /* Set slave address */
369 writew(chip, &i2c_base->sa);
370 /* Read len bytes from slave */
371 writew(len, &i2c_base->cnt);
372 /* Need stop bit here */
373 writew(I2C_CON_EN | I2C_CON_MST |
374 I2C_CON_STT | I2C_CON_STP,
375 &i2c_base->con);
376
377 /* Receive data */
378 while (1) {
Heiko Schocher6789e842013-10-22 11:03:18 +0200379 status = wait_for_event(adap);
Lubomir Popov960187f2013-06-01 06:44:38 +0000380 /*
381 * Try to identify bus that is not padconf'd for I2C. This
382 * state could be left over from previous transactions if
383 * the address phase is skipped due to alen=0.
384 */
385 if (status == I2C_STAT_XRDY) {
386 i2c_error = 2;
387 printf("i2c_read (data phase): pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocher6789e842013-10-22 11:03:18 +0200388 adap->hwadapnr, status);
Lubomir Popov960187f2013-06-01 06:44:38 +0000389 goto rd_exit;
390 }
Hannes Petermaierd5243352014-02-03 21:22:18 +0100391 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000392 i2c_error = 1;
393 goto rd_exit;
394 }
395 if (status & I2C_STAT_RRDY) {
396 *buffer++ = readb(&i2c_base->data);
397 writew(I2C_STAT_RRDY, &i2c_base->stat);
398 }
399 if (status & I2C_STAT_ARDY) {
400 writew(I2C_STAT_ARDY, &i2c_base->stat);
401 break;
wdenk8ed96042005-01-09 23:16:25 +0000402 }
403 }
404
Lubomir Popov960187f2013-06-01 06:44:38 +0000405rd_exit:
Heiko Schocher6789e842013-10-22 11:03:18 +0200406 flush_fifo(adap);
Lubomir Popov960187f2013-06-01 06:44:38 +0000407 writew(0xFFFF, &i2c_base->stat);
Lubomir Popov960187f2013-06-01 06:44:38 +0000408 return i2c_error;
wdenk8ed96042005-01-09 23:16:25 +0000409}
410
Lubomir Popov960187f2013-06-01 06:44:38 +0000411/* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
Heiko Schocher6789e842013-10-22 11:03:18 +0200412static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
413 int alen, uchar *buffer, int len)
wdenk8ed96042005-01-09 23:16:25 +0000414{
Heiko Schocher6789e842013-10-22 11:03:18 +0200415 struct i2c *i2c_base = omap24_get_base(adap);
Tom Rinicec487a2012-02-20 18:49:16 +0000416 int i;
417 u16 status;
418 int i2c_error = 0;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100419 int timeout = I2C_TIMEOUT;
Lubomir Popov960187f2013-06-01 06:44:38 +0000420
421 if (alen < 0) {
422 puts("I2C write: addr len < 0\n");
423 return 1;
424 }
425
426 if (len < 0) {
427 puts("I2C write: data len < 0\n");
428 return 1;
429 }
430
431 if (buffer == NULL) {
432 puts("I2C write: NULL pointer passed\n");
433 return 1;
434 }
wdenk8ed96042005-01-09 23:16:25 +0000435
Ilya Yanok55faa582012-06-08 03:12:09 +0000436 if (alen > 2) {
Tom Rinicec487a2012-02-20 18:49:16 +0000437 printf("I2C write: addr len %d not supported\n", alen);
wdenk8ed96042005-01-09 23:16:25 +0000438 return 1;
Tom Rinicec487a2012-02-20 18:49:16 +0000439 }
wdenk8ed96042005-01-09 23:16:25 +0000440
Ilya Yanok55faa582012-06-08 03:12:09 +0000441 if (addr + len > (1 << 16)) {
Tom Rinicec487a2012-02-20 18:49:16 +0000442 printf("I2C write: address 0x%x + 0x%x out of range\n",
Lubomir Popov960187f2013-06-01 06:44:38 +0000443 addr, len);
wdenk8ed96042005-01-09 23:16:25 +0000444 return 1;
445 }
446
Lubomir Popov960187f2013-06-01 06:44:38 +0000447 /* Wait until bus not busy */
Heiko Schocher6789e842013-10-22 11:03:18 +0200448 if (wait_for_bb(adap))
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000449 return 1;
Michael Jones0607e2b2011-09-04 14:01:55 -0400450
Lubomir Popov960187f2013-06-01 06:44:38 +0000451 /* Start address phase - will write regoffset + len bytes data */
Tom Rinicec487a2012-02-20 18:49:16 +0000452 writew(alen + len, &i2c_base->cnt);
Lubomir Popov960187f2013-06-01 06:44:38 +0000453 /* Set slave address */
Michael Jones0607e2b2011-09-04 14:01:55 -0400454 writew(chip, &i2c_base->sa);
Lubomir Popov960187f2013-06-01 06:44:38 +0000455 /* Stop bit needed here */
Michael Jones0607e2b2011-09-04 14:01:55 -0400456 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
Lubomir Popov960187f2013-06-01 06:44:38 +0000457 I2C_CON_STP, &i2c_base->con);
Michael Jones0607e2b2011-09-04 14:01:55 -0400458
Lubomir Popov960187f2013-06-01 06:44:38 +0000459 while (alen) {
460 /* Must write reg offset (one or two bytes) */
Heiko Schocher6789e842013-10-22 11:03:18 +0200461 status = wait_for_event(adap);
Lubomir Popov960187f2013-06-01 06:44:38 +0000462 /* Try to identify bus that is not padconf'd for I2C */
463 if (status == I2C_STAT_XRDY) {
464 i2c_error = 2;
465 printf("i2c_write: pads on bus %d probably not configured (status=0x%x)\n",
Heiko Schocher6789e842013-10-22 11:03:18 +0200466 adap->hwadapnr, status);
Lubomir Popov960187f2013-06-01 06:44:38 +0000467 goto wr_exit;
468 }
Hannes Petermaierd5243352014-02-03 21:22:18 +0100469 if (status == 0 || (status & I2C_STAT_NACK)) {
Michael Jones0607e2b2011-09-04 14:01:55 -0400470 i2c_error = 1;
Lubomir Popov960187f2013-06-01 06:44:38 +0000471 printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
472 status);
473 goto wr_exit;
Tom Rinicec487a2012-02-20 18:49:16 +0000474 }
Tom Rinicec487a2012-02-20 18:49:16 +0000475 if (status & I2C_STAT_XRDY) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000476 alen--;
477 writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data);
Tom Rinicec487a2012-02-20 18:49:16 +0000478 writew(I2C_STAT_XRDY, &i2c_base->stat);
479 } else {
480 i2c_error = 1;
Lubomir Popov960187f2013-06-01 06:44:38 +0000481 printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
482 status);
483 goto wr_exit;
484 }
485 }
486 /* Address phase is over, now write data */
487 for (i = 0; i < len; i++) {
Heiko Schocher6789e842013-10-22 11:03:18 +0200488 status = wait_for_event(adap);
Hannes Petermaierd5243352014-02-03 21:22:18 +0100489 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000490 i2c_error = 1;
491 printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
492 status);
493 goto wr_exit;
494 }
495 if (status & I2C_STAT_XRDY) {
496 writeb(buffer[i], &i2c_base->data);
497 writew(I2C_STAT_XRDY, &i2c_base->stat);
498 } else {
499 i2c_error = 1;
500 printf("i2c_write: bus not ready for data Tx (i=%d)\n",
501 i);
502 goto wr_exit;
wdenk8ed96042005-01-09 23:16:25 +0000503 }
504 }
Hannes Petermaierd5243352014-02-03 21:22:18 +0100505 /*
506 * poll ARDY bit for making sure that last byte really has been
507 * transferred on the bus.
508 */
509 do {
510 status = wait_for_event(adap);
511 } while (!(status & I2C_STAT_ARDY) && timeout--);
512 if (timeout <= 0)
513 printf("i2c_write: timed out writig last byte!\n");
wdenk8ed96042005-01-09 23:16:25 +0000514
Lubomir Popov960187f2013-06-01 06:44:38 +0000515wr_exit:
Heiko Schocher6789e842013-10-22 11:03:18 +0200516 flush_fifo(adap);
Michael Jones0607e2b2011-09-04 14:01:55 -0400517 writew(0xFFFF, &i2c_base->stat);
Tom Rinicec487a2012-02-20 18:49:16 +0000518 return i2c_error;
wdenk8ed96042005-01-09 23:16:25 +0000519}
520
Lubomir Popov960187f2013-06-01 06:44:38 +0000521/*
522 * Wait for the bus to be free by checking the Bus Busy (BB)
523 * bit to become clear
524 */
Heiko Schocher6789e842013-10-22 11:03:18 +0200525static int wait_for_bb(struct i2c_adapter *adap)
wdenk8ed96042005-01-09 23:16:25 +0000526{
Heiko Schocher6789e842013-10-22 11:03:18 +0200527 struct i2c *i2c_base = omap24_get_base(adap);
Steve Sakoman73e87472010-10-20 06:07:44 -0700528 int timeout = I2C_TIMEOUT;
Tom Rinicec487a2012-02-20 18:49:16 +0000529 u16 stat;
wdenk8ed96042005-01-09 23:16:25 +0000530
Tom Rinicec487a2012-02-20 18:49:16 +0000531 writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
Lubomir Popov960187f2013-06-01 06:44:38 +0000532#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
Michael Jones89677b22011-07-27 14:01:55 -0400533 while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000534#else
535 /* Read RAW status */
536 while ((stat = readw(&i2c_base->irqstatus_raw) &
537 I2C_STAT_BB) && timeout--) {
538#endif
Michael Jones89677b22011-07-27 14:01:55 -0400539 writew(stat, &i2c_base->stat);
Hannes Petermaierd5243352014-02-03 21:22:18 +0100540 udelay(adap->waitdelay);
wdenk8ed96042005-01-09 23:16:25 +0000541 }
542
543 if (timeout <= 0) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000544 printf("Timed out in wait_for_bb: status=%04x\n",
545 stat);
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000546 return 1;
wdenk8ed96042005-01-09 23:16:25 +0000547 }
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100548 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000549 return 0;
wdenk8ed96042005-01-09 23:16:25 +0000550}
551
Lubomir Popov960187f2013-06-01 06:44:38 +0000552/*
553 * Wait for the I2C controller to complete current action
554 * and update status
555 */
Heiko Schocher6789e842013-10-22 11:03:18 +0200556static u16 wait_for_event(struct i2c_adapter *adap)
wdenk8ed96042005-01-09 23:16:25 +0000557{
Heiko Schocher6789e842013-10-22 11:03:18 +0200558 struct i2c *i2c_base = omap24_get_base(adap);
Tom Rinicec487a2012-02-20 18:49:16 +0000559 u16 status;
Steve Sakoman73e87472010-10-20 06:07:44 -0700560 int timeout = I2C_TIMEOUT;
wdenk8ed96042005-01-09 23:16:25 +0000561
562 do {
Hannes Petermaierd5243352014-02-03 21:22:18 +0100563 udelay(adap->waitdelay);
Lubomir Popov960187f2013-06-01 06:44:38 +0000564#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
Michael Jones89677b22011-07-27 14:01:55 -0400565 status = readw(&i2c_base->stat);
Lubomir Popov960187f2013-06-01 06:44:38 +0000566#else
567 /* Read RAW status */
568 status = readw(&i2c_base->irqstatus_raw);
569#endif
Tom Rinicec487a2012-02-20 18:49:16 +0000570 } while (!(status &
571 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
572 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
573 I2C_STAT_AL)) && timeout--);
wdenk8ed96042005-01-09 23:16:25 +0000574
575 if (timeout <= 0) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000576 printf("Timed out in wait_for_event: status=%04x\n",
577 status);
578 /*
579 * If status is still 0 here, probably the bus pads have
580 * not been configured for I2C, and/or pull-ups are missing.
581 */
582 printf("Check if pads/pull-ups of bus %d are properly configured\n",
Heiko Schocher6789e842013-10-22 11:03:18 +0200583 adap->hwadapnr);
Steve Sakoman73e87472010-10-20 06:07:44 -0700584 writew(0xFFFF, &i2c_base->stat);
Tom Rinicec487a2012-02-20 18:49:16 +0000585 status = 0;
Steve Sakoman73e87472010-10-20 06:07:44 -0700586 }
Tom Rinicec487a2012-02-20 18:49:16 +0000587
wdenk8ed96042005-01-09 23:16:25 +0000588 return status;
589}
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100590
Heiko Schocher6789e842013-10-22 11:03:18 +0200591static struct i2c *omap24_get_base(struct i2c_adapter *adap)
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100592{
Heiko Schocher6789e842013-10-22 11:03:18 +0200593 switch (adap->hwadapnr) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000594 case 0:
Heiko Schocher6789e842013-10-22 11:03:18 +0200595 return (struct i2c *)I2C_BASE1;
Lubomir Popov960187f2013-06-01 06:44:38 +0000596 break;
597 case 1:
Heiko Schocher6789e842013-10-22 11:03:18 +0200598 return (struct i2c *)I2C_BASE2;
Lubomir Popov960187f2013-06-01 06:44:38 +0000599 break;
600#if (I2C_BUS_MAX > 2)
601 case 2:
Heiko Schocher6789e842013-10-22 11:03:18 +0200602 return (struct i2c *)I2C_BASE3;
Lubomir Popov960187f2013-06-01 06:44:38 +0000603 break;
604#if (I2C_BUS_MAX > 3)
605 case 3:
Heiko Schocher6789e842013-10-22 11:03:18 +0200606 return (struct i2c *)I2C_BASE4;
Lubomir Popov960187f2013-06-01 06:44:38 +0000607 break;
608#if (I2C_BUS_MAX > 4)
609 case 4:
Heiko Schocher6789e842013-10-22 11:03:18 +0200610 return (struct i2c *)I2C_BASE5;
Lubomir Popov960187f2013-06-01 06:44:38 +0000611 break;
612#endif
613#endif
614#endif
Heiko Schocher6789e842013-10-22 11:03:18 +0200615 default:
616 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
617 break;
Lubomir Popov960187f2013-06-01 06:44:38 +0000618 }
Heiko Schocher6789e842013-10-22 11:03:18 +0200619 return NULL;
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100620}
Steve Sakoman938717c2010-06-12 06:42:57 -0700621
Heiko Schocher6789e842013-10-22 11:03:18 +0200622#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
623#define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
624#endif
625#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
626#define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
627#endif
628
629U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5243352014-02-03 21:22:18 +0100630 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Heiko Schocher6789e842013-10-22 11:03:18 +0200631 CONFIG_SYS_OMAP24_I2C_SPEED,
632 CONFIG_SYS_OMAP24_I2C_SLAVE,
633 0)
634U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5243352014-02-03 21:22:18 +0100635 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Heiko Schocher6789e842013-10-22 11:03:18 +0200636 CONFIG_SYS_OMAP24_I2C_SPEED1,
637 CONFIG_SYS_OMAP24_I2C_SLAVE1,
638 1)
639#if (I2C_BUS_MAX > 2)
640#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
641#define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
642#endif
643#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
644#define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
645#endif
646
647U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
648 omap24_i2c_read, omap24_i2c_write, NULL,
649 CONFIG_SYS_OMAP24_I2C_SPEED2,
650 CONFIG_SYS_OMAP24_I2C_SLAVE2,
651 2)
652#if (I2C_BUS_MAX > 3)
653#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
654#define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
655#endif
656#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
657#define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
658#endif
659
660U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
661 omap24_i2c_read, omap24_i2c_write, NULL,
662 CONFIG_SYS_OMAP24_I2C_SPEED3,
663 CONFIG_SYS_OMAP24_I2C_SLAVE3,
664 3)
665#if (I2C_BUS_MAX > 4)
666#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
667#define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
668#endif
669#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
670#define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
671#endif
672
673U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
674 omap24_i2c_read, omap24_i2c_write, NULL,
675 CONFIG_SYS_OMAP24_I2C_SPEED4,
676 CONFIG_SYS_OMAP24_I2C_SLAVE4,
677 4)
678#endif
679#endif
680#endif