blob: 8dea5fa55661dca962f7e94e0da26c0073f9e816 [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 *
Hannes Schmelzer7935f032015-05-28 15:41:12 +020036 * Copyright (c) 2014 Hannes Schmelzer <oe5hpm@oevsv.at>, B&R
Hannes Petermaierd5885052014-02-03 21:22:18 +010037 * - 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
Hannes Petermaierd5885052014-02-03 21:22:18 +010056static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
wdenkf8062712005-01-09 23:16:25 +000057{
Hannes Petermaierd5885052014-02-03 21:22:18 +010058 unsigned int sampleclk, prescaler;
59 int fsscll, fssclh;
Tom Rix03b2a742009-06-28 12:52:27 -050060
Hannes Petermaierd5885052014-02-03 21:22:18 +010061 speed <<= 1;
62 prescaler = 0;
63 /*
64 * some divisors may cause a precission loss, but shouldn't
65 * be a big thing, because i2c_clk is then allready very slow.
66 */
67 while (prescaler <= 0xFF) {
68 sampleclk = I2C_IP_CLK / (prescaler+1);
Tom Rix03b2a742009-06-28 12:52:27 -050069
Hannes Petermaierd5885052014-02-03 21:22:18 +010070 fsscll = sampleclk / speed;
71 fssclh = fsscll;
72 fsscll -= I2C_FASTSPEED_SCLL_TRIM;
73 fssclh -= I2C_FASTSPEED_SCLH_TRIM;
74
75 if (((fsscll > 0) && (fssclh > 0)) &&
76 ((fsscll <= (255-I2C_FASTSPEED_SCLL_TRIM)) &&
77 (fssclh <= (255-I2C_FASTSPEED_SCLH_TRIM)))) {
78 if (pscl)
79 *pscl = fsscll;
80 if (psch)
81 *psch = fssclh;
82
83 return prescaler;
84 }
85 prescaler++;
Tom Rix03b2a742009-06-28 12:52:27 -050086 }
Hannes Petermaierd5885052014-02-03 21:22:18 +010087 return -1;
88}
Mugunthan V N38d943a2016-07-18 15:11:00 +053089
90/*
91 * Wait for the bus to be free by checking the Bus Busy (BB)
92 * bit to become clear
93 */
94static int wait_for_bb(struct i2c *i2c_base, int waitdelay)
Hannes Petermaierd5885052014-02-03 21:22:18 +010095{
Mugunthan V N38d943a2016-07-18 15:11:00 +053096 int timeout = I2C_TIMEOUT;
97 u16 stat;
98
99 writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
100#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
101 while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
102#else
103 /* Read RAW status */
104 while ((stat = readw(&i2c_base->irqstatus_raw) &
105 I2C_STAT_BB) && timeout--) {
106#endif
107 writew(stat, &i2c_base->stat);
108 udelay(waitdelay);
109 }
110
111 if (timeout <= 0) {
112 printf("Timed out in wait_for_bb: status=%04x\n",
113 stat);
114 return 1;
115 }
116 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
117 return 0;
118}
119
120/*
121 * Wait for the I2C controller to complete current action
122 * and update status
123 */
124static u16 wait_for_event(struct i2c *i2c_base, int waitdelay)
125{
126 u16 status;
127 int timeout = I2C_TIMEOUT;
128
129 do {
130 udelay(waitdelay);
131#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
132 status = readw(&i2c_base->stat);
133#else
134 /* Read RAW status */
135 status = readw(&i2c_base->irqstatus_raw);
136#endif
137 } while (!(status &
138 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
139 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
140 I2C_STAT_AL)) && timeout--);
141
142 if (timeout <= 0) {
143 printf("Timed out in wait_for_event: status=%04x\n",
144 status);
145 /*
146 * If status is still 0 here, probably the bus pads have
147 * not been configured for I2C, and/or pull-ups are missing.
148 */
149 printf("Check if pads/pull-ups of bus are properly configured\n");
150 writew(0xFFFF, &i2c_base->stat);
151 status = 0;
152 }
153
154 return status;
155}
156
157static void flush_fifo(struct i2c *i2c_base)
158{
159 u16 stat;
160
161 /*
162 * note: if you try and read data when its not there or ready
163 * you get a bus error
164 */
165 while (1) {
166 stat = readw(&i2c_base->stat);
167 if (stat == I2C_STAT_RRDY) {
168 readb(&i2c_base->data);
169 writew(I2C_STAT_RRDY, &i2c_base->stat);
170 udelay(1000);
171 } else
172 break;
173 }
174}
175
176static int __omap24_i2c_setspeed(struct i2c *i2c_base, uint speed,
177 int *waitdelay)
178{
Hannes Petermaierd5885052014-02-03 21:22:18 +0100179 int psc, fsscll = 0, fssclh = 0;
180 int hsscll = 0, hssclh = 0;
181 u32 scll = 0, sclh = 0;
Tom Rix03b2a742009-06-28 12:52:27 -0500182
Hannes Petermaierd5885052014-02-03 21:22:18 +0100183 if (speed >= OMAP_I2C_HIGH_SPEED) {
Tom Rix03b2a742009-06-28 12:52:27 -0500184 /* High speed */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100185 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
186 psc -= 1;
187 if (psc < I2C_PSC_MIN) {
188 printf("Error : I2C unsupported prescaler %d\n", psc);
189 return -1;
190 }
Tom Rix03b2a742009-06-28 12:52:27 -0500191
192 /* For first phase of HS mode */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100193 fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
194
195 fssclh = fsscll;
Tom Rix03b2a742009-06-28 12:52:27 -0500196
197 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
198 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
199 if (((fsscll < 0) || (fssclh < 0)) ||
200 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000201 puts("Error : I2C initializing first phase clock\n");
Hannes Petermaierd5885052014-02-03 21:22:18 +0100202 return -1;
Tom Rix03b2a742009-06-28 12:52:27 -0500203 }
204
205 /* For second phase of HS mode */
206 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
207
208 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
209 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
210 if (((fsscll < 0) || (fssclh < 0)) ||
211 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000212 puts("Error : I2C initializing second phase clock\n");
Hannes Petermaierd5885052014-02-03 21:22:18 +0100213 return -1;
Tom Rix03b2a742009-06-28 12:52:27 -0500214 }
215
216 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
217 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
218
219 } else {
220 /* Standard and fast speed */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100221 psc = omap24_i2c_findpsc(&scll, &sclh, speed);
222 if (0 > psc) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000223 puts("Error : I2C initializing clock\n");
Hannes Petermaierd5885052014-02-03 21:22:18 +0100224 return -1;
Tom Rix03b2a742009-06-28 12:52:27 -0500225 }
Tom Rix03b2a742009-06-28 12:52:27 -0500226 }
wdenkf8062712005-01-09 23:16:25 +0000227
Mugunthan V N38d943a2016-07-18 15:11:00 +0530228 *waitdelay = (10000000 / speed) * 2; /* wait for 20 clkperiods */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100229 writew(0, &i2c_base->con);
230 writew(psc, &i2c_base->psc);
231 writew(scll, &i2c_base->scll);
232 writew(sclh, &i2c_base->sclh);
233 writew(I2C_CON_EN, &i2c_base->con);
234 writew(0xFFFF, &i2c_base->stat); /* clear all pending status */
235
236 return 0;
237}
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200238
Mugunthan V N38d943a2016-07-18 15:11:00 +0530239static void omap24_i2c_deblock(struct i2c *i2c_base)
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200240{
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200241 int i;
242 u16 systest;
243 u16 orgsystest;
244
245 /* set test mode ST_EN = 1 */
246 orgsystest = readw(&i2c_base->systest);
247 systest = orgsystest;
248 /* enable testmode */
249 systest |= I2C_SYSTEST_ST_EN;
250 writew(systest, &i2c_base->systest);
251 systest &= ~I2C_SYSTEST_TMODE_MASK;
252 systest |= 3 << I2C_SYSTEST_TMODE_SHIFT;
253 writew(systest, &i2c_base->systest);
254
255 /* set SCL, SDA = 1 */
256 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
257 writew(systest, &i2c_base->systest);
258 udelay(10);
259
260 /* toggle scl 9 clocks */
261 for (i = 0; i < 9; i++) {
262 /* SCL = 0 */
263 systest &= ~I2C_SYSTEST_SCL_O;
264 writew(systest, &i2c_base->systest);
265 udelay(10);
266 /* SCL = 1 */
267 systest |= I2C_SYSTEST_SCL_O;
268 writew(systest, &i2c_base->systest);
269 udelay(10);
270 }
271
272 /* send stop */
273 systest &= ~I2C_SYSTEST_SDA_O;
274 writew(systest, &i2c_base->systest);
275 udelay(10);
276 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
277 writew(systest, &i2c_base->systest);
278 udelay(10);
279
280 /* restore original mode */
281 writew(orgsystest, &i2c_base->systest);
282}
283
Mugunthan V N38d943a2016-07-18 15:11:00 +0530284static void __omap24_i2c_init(struct i2c *i2c_base, int speed, int slaveadd,
285 int *waitdelay)
Hannes Petermaierd5885052014-02-03 21:22:18 +0100286{
Hannes Petermaierd5885052014-02-03 21:22:18 +0100287 int timeout = I2C_TIMEOUT;
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200288 int deblock = 1;
Hannes Petermaierd5885052014-02-03 21:22:18 +0100289
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200290retry:
Michael Jones4db67862011-07-27 14:01:55 -0400291 if (readw(&i2c_base->con) & I2C_CON_EN) {
292 writew(0, &i2c_base->con);
293 udelay(50000);
wdenkf8062712005-01-09 23:16:25 +0000294 }
295
Tom Rini49fbf672012-02-20 18:49:16 +0000296 writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
297 udelay(1000);
298
299 writew(I2C_CON_EN, &i2c_base->con);
300 while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
301 if (timeout <= 0) {
302 puts("ERROR: Timeout in soft-reset\n");
303 return;
304 }
305 udelay(1000);
306 }
307
Mugunthan V N38d943a2016-07-18 15:11:00 +0530308 if (0 != __omap24_i2c_setspeed(i2c_base, speed, waitdelay)) {
Hannes Petermaierd5885052014-02-03 21:22:18 +0100309 printf("ERROR: failed to setup I2C bus-speed!\n");
310 return;
311 }
Tom Rix03b2a742009-06-28 12:52:27 -0500312
wdenkf8062712005-01-09 23:16:25 +0000313 /* own address */
Michael Jones4db67862011-07-27 14:01:55 -0400314 writew(slaveadd, &i2c_base->oa);
Hannes Petermaierd5885052014-02-03 21:22:18 +0100315
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000316#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
317 /*
318 * Have to enable interrupts for OMAP2/3, these IPs don't have
319 * an 'irqstatus_raw' register and we shall have to poll 'stat'
320 */
Michael Jones4db67862011-07-27 14:01:55 -0400321 writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000322 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
323#endif
Michael Jones4db67862011-07-27 14:01:55 -0400324 udelay(1000);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530325 flush_fifo(i2c_base);
Michael Jones4db67862011-07-27 14:01:55 -0400326 writew(0xFFFF, &i2c_base->stat);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200327
328 /* Handle possible failed I2C state */
Mugunthan V N38d943a2016-07-18 15:11:00 +0530329 if (wait_for_bb(i2c_base, *waitdelay))
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200330 if (deblock == 1) {
Mugunthan V N38d943a2016-07-18 15:11:00 +0530331 omap24_i2c_deblock(i2c_base);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200332 deblock = 0;
333 goto retry;
334 }
wdenkf8062712005-01-09 23:16:25 +0000335}
336
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000337/*
338 * i2c_probe: Use write access. Allows to identify addresses that are
339 * write-only (like the config register of dual-port EEPROMs)
340 */
Mugunthan V N38d943a2016-07-18 15:11:00 +0530341static int __omap24_i2c_probe(struct i2c *i2c_base, int waitdelay, uchar chip)
wdenkf8062712005-01-09 23:16:25 +0000342{
Tom Rini49fbf672012-02-20 18:49:16 +0000343 u16 status;
wdenkf8062712005-01-09 23:16:25 +0000344 int res = 1; /* default = fail */
345
Michael Jones4db67862011-07-27 14:01:55 -0400346 if (chip == readw(&i2c_base->oa))
wdenkf8062712005-01-09 23:16:25 +0000347 return res;
wdenkf8062712005-01-09 23:16:25 +0000348
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000349 /* Wait until bus is free */
Mugunthan V N38d943a2016-07-18 15:11:00 +0530350 if (wait_for_bb(i2c_base, waitdelay))
Vincent Stehlé33205e32012-12-03 05:23:16 +0000351 return res;
wdenkf8062712005-01-09 23:16:25 +0000352
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000353 /* No data transfer, slave addr only */
Michael Jones4db67862011-07-27 14:01:55 -0400354 writew(chip, &i2c_base->sa);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000355 /* Stop bit needed here */
356 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
357 I2C_CON_STP, &i2c_base->con);
Nick Thompson48f7ae42011-04-11 22:37:41 +0000358
Mugunthan V N38d943a2016-07-18 15:11:00 +0530359 status = wait_for_event(i2c_base, waitdelay);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000360
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000361 if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
362 /*
363 * With current high-level command implementation, notifying
364 * the user shall flood the console with 127 messages. If
365 * silent exit is desired upon unconfigured bus, remove the
366 * following 'if' section:
367 */
368 if (status == I2C_STAT_XRDY)
Mugunthan V N38d943a2016-07-18 15:11:00 +0530369 printf("i2c_probe: pads on bus probably not configured (status=0x%x)\n",
370 status);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000371
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000372 goto pr_exit;
Tom Rini27eed8b2012-05-21 06:46:29 +0000373 }
Tom Rini49fbf672012-02-20 18:49:16 +0000374
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000375 /* Check for ACK (!NAK) */
376 if (!(status & I2C_STAT_NACK)) {
Hannes Petermaierd5885052014-02-03 21:22:18 +0100377 res = 0; /* Device found */
Mugunthan V N38d943a2016-07-18 15:11:00 +0530378 udelay(waitdelay);/* Required by AM335X in SPL */
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000379 /* Abort transfer (force idle state) */
380 writew(I2C_CON_MST | I2C_CON_TRX, &i2c_base->con); /* Reset */
381 udelay(1000);
382 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX |
383 I2C_CON_STP, &i2c_base->con); /* STP */
384 }
385pr_exit:
Mugunthan V N38d943a2016-07-18 15:11:00 +0530386 flush_fifo(i2c_base);
Dirk Behme7a8f6572009-11-02 20:36:26 +0100387 writew(0xFFFF, &i2c_base->stat);
wdenkf8062712005-01-09 23:16:25 +0000388 return res;
389}
390
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000391/*
392 * i2c_read: Function now uses a single I2C read transaction with bulk transfer
393 * of the requested number of bytes (note that the 'i2c md' command
394 * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
395 * defined in the board config header, this transaction shall be with
396 * Repeated Start (Sr) between the address and data phases; otherwise
397 * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
398 * The address (reg offset) may be 0, 1 or 2 bytes long.
399 * Function now reads correctly from chips that return more than one
400 * byte of data per addressed register (like TI temperature sensors),
401 * or that do not need a register address at all (such as some clock
402 * distributors).
403 */
Mugunthan V N38d943a2016-07-18 15:11:00 +0530404static int __omap24_i2c_read(struct i2c *i2c_base, int waitdelay, uchar chip,
405 uint addr, int alen, uchar *buffer, int len)
wdenkf8062712005-01-09 23:16:25 +0000406{
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000407 int i2c_error = 0;
408 u16 status;
409
410 if (alen < 0) {
411 puts("I2C read: addr len < 0\n");
412 return 1;
413 }
414 if (len < 0) {
415 puts("I2C read: data len < 0\n");
416 return 1;
417 }
418 if (buffer == NULL) {
419 puts("I2C read: NULL pointer passed\n");
420 return 1;
421 }
wdenkf8062712005-01-09 23:16:25 +0000422
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000423 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000424 printf("I2C read: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000425 return 1;
426 }
427
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000428 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000429 puts("I2C read: address out of range\n");
wdenkf8062712005-01-09 23:16:25 +0000430 return 1;
431 }
432
Guy Thouret51c27272016-03-11 16:23:41 +0000433#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
434 /*
435 * EEPROM chips that implement "address overflow" are ones
436 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
437 * address and the extra bits end up in the "chip address"
438 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
439 * four 256 byte chips.
440 *
441 * Note that we consider the length of the address field to
442 * still be one byte because the extra address bits are
443 * hidden in the chip address.
444 */
445 if (alen > 0)
446 chip |= ((addr >> (alen * 8)) &
447 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
448#endif
449
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000450 /* Wait until bus not busy */
Mugunthan V N38d943a2016-07-18 15:11:00 +0530451 if (wait_for_bb(i2c_base, waitdelay))
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000452 return 1;
453
454 /* Zero, one or two bytes reg address (offset) */
455 writew(alen, &i2c_base->cnt);
456 /* Set slave address */
457 writew(chip, &i2c_base->sa);
458
459 if (alen) {
460 /* Must write reg offset first */
461#ifdef CONFIG_I2C_REPEATED_START
462 /* No stop bit, use Repeated Start (Sr) */
463 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
464 I2C_CON_TRX, &i2c_base->con);
465#else
466 /* Stop - Start (P-S) */
467 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP |
468 I2C_CON_TRX, &i2c_base->con);
469#endif
470 /* Send register offset */
471 while (1) {
Mugunthan V N38d943a2016-07-18 15:11:00 +0530472 status = wait_for_event(i2c_base, waitdelay);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000473 /* Try to identify bus that is not padconf'd for I2C */
474 if (status == I2C_STAT_XRDY) {
475 i2c_error = 2;
Mugunthan V N38d943a2016-07-18 15:11:00 +0530476 printf("i2c_read (addr phase): pads on bus probably not configured (status=0x%x)\n",
477 status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000478 goto rd_exit;
479 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100480 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000481 i2c_error = 1;
482 printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
483 status);
484 goto rd_exit;
485 }
486 if (alen) {
487 if (status & I2C_STAT_XRDY) {
488 alen--;
489 /* Do we have to use byte access? */
490 writeb((addr >> (8 * alen)) & 0xff,
491 &i2c_base->data);
492 writew(I2C_STAT_XRDY, &i2c_base->stat);
493 }
494 }
495 if (status & I2C_STAT_ARDY) {
496 writew(I2C_STAT_ARDY, &i2c_base->stat);
497 break;
498 }
wdenkf8062712005-01-09 23:16:25 +0000499 }
500 }
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000501 /* Set slave address */
502 writew(chip, &i2c_base->sa);
503 /* Read len bytes from slave */
504 writew(len, &i2c_base->cnt);
505 /* Need stop bit here */
506 writew(I2C_CON_EN | I2C_CON_MST |
507 I2C_CON_STT | I2C_CON_STP,
508 &i2c_base->con);
wdenkf8062712005-01-09 23:16:25 +0000509
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000510 /* Receive data */
511 while (1) {
Mugunthan V N38d943a2016-07-18 15:11:00 +0530512 status = wait_for_event(i2c_base, waitdelay);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000513 /*
514 * Try to identify bus that is not padconf'd for I2C. This
515 * state could be left over from previous transactions if
516 * the address phase is skipped due to alen=0.
517 */
518 if (status == I2C_STAT_XRDY) {
519 i2c_error = 2;
Mugunthan V N38d943a2016-07-18 15:11:00 +0530520 printf("i2c_read (data phase): pads on bus probably not configured (status=0x%x)\n",
521 status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000522 goto rd_exit;
523 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100524 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000525 i2c_error = 1;
526 goto rd_exit;
527 }
528 if (status & I2C_STAT_RRDY) {
529 *buffer++ = readb(&i2c_base->data);
530 writew(I2C_STAT_RRDY, &i2c_base->stat);
531 }
532 if (status & I2C_STAT_ARDY) {
533 writew(I2C_STAT_ARDY, &i2c_base->stat);
534 break;
535 }
536 }
537
538rd_exit:
Mugunthan V N38d943a2016-07-18 15:11:00 +0530539 flush_fifo(i2c_base);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000540 writew(0xFFFF, &i2c_base->stat);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000541 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000542}
543
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000544/* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
Mugunthan V N38d943a2016-07-18 15:11:00 +0530545static int __omap24_i2c_write(struct i2c *i2c_base, int waitdelay, uchar chip,
546 uint addr, int alen, uchar *buffer, int len)
wdenkf8062712005-01-09 23:16:25 +0000547{
Tom Rini49fbf672012-02-20 18:49:16 +0000548 int i;
549 u16 status;
550 int i2c_error = 0;
Hannes Petermaierd5885052014-02-03 21:22:18 +0100551 int timeout = I2C_TIMEOUT;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000552
553 if (alen < 0) {
554 puts("I2C write: addr len < 0\n");
555 return 1;
556 }
557
558 if (len < 0) {
559 puts("I2C write: data len < 0\n");
560 return 1;
561 }
562
563 if (buffer == NULL) {
564 puts("I2C write: NULL pointer passed\n");
565 return 1;
566 }
wdenkf8062712005-01-09 23:16:25 +0000567
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000568 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000569 printf("I2C write: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000570 return 1;
Tom Rini49fbf672012-02-20 18:49:16 +0000571 }
wdenkf8062712005-01-09 23:16:25 +0000572
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000573 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000574 printf("I2C write: address 0x%x + 0x%x out of range\n",
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000575 addr, len);
wdenkf8062712005-01-09 23:16:25 +0000576 return 1;
577 }
578
Guy Thouret51c27272016-03-11 16:23:41 +0000579#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
580 /*
581 * EEPROM chips that implement "address overflow" are ones
582 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
583 * address and the extra bits end up in the "chip address"
584 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
585 * four 256 byte chips.
586 *
587 * Note that we consider the length of the address field to
588 * still be one byte because the extra address bits are
589 * hidden in the chip address.
590 */
591 if (alen > 0)
592 chip |= ((addr >> (alen * 8)) &
593 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
594#endif
595
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000596 /* Wait until bus not busy */
Mugunthan V N38d943a2016-07-18 15:11:00 +0530597 if (wait_for_bb(i2c_base, waitdelay))
Vincent Stehlé33205e32012-12-03 05:23:16 +0000598 return 1;
Michael Jonesbb54d572011-09-04 14:01:55 -0400599
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000600 /* Start address phase - will write regoffset + len bytes data */
Tom Rini49fbf672012-02-20 18:49:16 +0000601 writew(alen + len, &i2c_base->cnt);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000602 /* Set slave address */
Michael Jonesbb54d572011-09-04 14:01:55 -0400603 writew(chip, &i2c_base->sa);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000604 /* Stop bit needed here */
Michael Jonesbb54d572011-09-04 14:01:55 -0400605 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000606 I2C_CON_STP, &i2c_base->con);
Michael Jonesbb54d572011-09-04 14:01:55 -0400607
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000608 while (alen) {
609 /* Must write reg offset (one or two bytes) */
Mugunthan V N38d943a2016-07-18 15:11:00 +0530610 status = wait_for_event(i2c_base, waitdelay);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000611 /* Try to identify bus that is not padconf'd for I2C */
612 if (status == I2C_STAT_XRDY) {
613 i2c_error = 2;
Mugunthan V N38d943a2016-07-18 15:11:00 +0530614 printf("i2c_write: pads on bus probably not configured (status=0x%x)\n",
615 status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000616 goto wr_exit;
617 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100618 if (status == 0 || (status & I2C_STAT_NACK)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000619 i2c_error = 1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000620 printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
621 status);
622 goto wr_exit;
Tom Rini49fbf672012-02-20 18:49:16 +0000623 }
Tom Rini49fbf672012-02-20 18:49:16 +0000624 if (status & I2C_STAT_XRDY) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000625 alen--;
626 writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data);
627 writew(I2C_STAT_XRDY, &i2c_base->stat);
628 } else {
629 i2c_error = 1;
630 printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
631 status);
632 goto wr_exit;
633 }
634 }
635 /* Address phase is over, now write data */
636 for (i = 0; i < len; i++) {
Mugunthan V N38d943a2016-07-18 15:11:00 +0530637 status = wait_for_event(i2c_base, waitdelay);
Hannes Petermaierd5885052014-02-03 21:22:18 +0100638 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000639 i2c_error = 1;
640 printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
641 status);
642 goto wr_exit;
643 }
644 if (status & I2C_STAT_XRDY) {
645 writeb(buffer[i], &i2c_base->data);
Tom Rini49fbf672012-02-20 18:49:16 +0000646 writew(I2C_STAT_XRDY, &i2c_base->stat);
647 } else {
648 i2c_error = 1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000649 printf("i2c_write: bus not ready for data Tx (i=%d)\n",
650 i);
651 goto wr_exit;
Patil, Rachnaa9e18c22012-01-22 23:44:12 +0000652 }
653 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100654 /*
655 * poll ARDY bit for making sure that last byte really has been
656 * transferred on the bus.
657 */
658 do {
Mugunthan V N38d943a2016-07-18 15:11:00 +0530659 status = wait_for_event(i2c_base, waitdelay);
Hannes Petermaierd5885052014-02-03 21:22:18 +0100660 } while (!(status & I2C_STAT_ARDY) && timeout--);
661 if (timeout <= 0)
662 printf("i2c_write: timed out writig last byte!\n");
Patil, Rachnaa9e18c22012-01-22 23:44:12 +0000663
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000664wr_exit:
Mugunthan V N38d943a2016-07-18 15:11:00 +0530665 flush_fifo(i2c_base);
Michael Jonesbb54d572011-09-04 14:01:55 -0400666 writew(0xFFFF, &i2c_base->stat);
Tom Rini49fbf672012-02-20 18:49:16 +0000667 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000668}
669
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000670/*
Mugunthan V N38d943a2016-07-18 15:11:00 +0530671 * The legacy I2C functions. These need to get removed once
672 * all users of this driver are converted to DM.
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000673 */
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200674static struct i2c *omap24_get_base(struct i2c_adapter *adap)
Dirk Behme7a8f6572009-11-02 20:36:26 +0100675{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200676 switch (adap->hwadapnr) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000677 case 0:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200678 return (struct i2c *)I2C_BASE1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000679 break;
680 case 1:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200681 return (struct i2c *)I2C_BASE2;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000682 break;
683#if (I2C_BUS_MAX > 2)
684 case 2:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200685 return (struct i2c *)I2C_BASE3;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000686 break;
687#if (I2C_BUS_MAX > 3)
688 case 3:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200689 return (struct i2c *)I2C_BASE4;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000690 break;
691#if (I2C_BUS_MAX > 4)
692 case 4:
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200693 return (struct i2c *)I2C_BASE5;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000694 break;
Koen Kooi584ff5f2012-08-08 00:57:35 +0000695#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100696#endif
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000697#endif
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200698 default:
699 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
700 break;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000701 }
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200702 return NULL;
703}
Dirk Behme7a8f6572009-11-02 20:36:26 +0100704
Mugunthan V N38d943a2016-07-18 15:11:00 +0530705
706static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
707 int alen, uchar *buffer, int len)
708{
709 struct i2c *i2c_base = omap24_get_base(adap);
710
711 return __omap24_i2c_read(i2c_base, adap->waitdelay, chip, addr,
712 alen, buffer, len);
713}
714
715
716static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
717 int alen, uchar *buffer, int len)
718{
719 struct i2c *i2c_base = omap24_get_base(adap);
720
721 return __omap24_i2c_write(i2c_base, adap->waitdelay, chip, addr,
722 alen, buffer, len);
723}
724
725static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
726{
727 struct i2c *i2c_base = omap24_get_base(adap);
728 int ret;
729
730 ret = __omap24_i2c_setspeed(i2c_base, speed, &adap->waitdelay);
731 if (ret) {
732 error("%s: set i2c speed failed\n", __func__);
733 return ret;
734 }
735
736 adap->speed = speed;
737
738 return 0;
739}
740
741static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
742{
743 struct i2c *i2c_base = omap24_get_base(adap);
744
745 return __omap24_i2c_init(i2c_base, speed, slaveadd, &adap->waitdelay);
746}
747
748static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
749{
750 struct i2c *i2c_base = omap24_get_base(adap);
751
752 return __omap24_i2c_probe(i2c_base, adap->waitdelay, chip);
753}
754
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200755#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
756#define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
757#endif
758#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
759#define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
760#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100761
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200762U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5885052014-02-03 21:22:18 +0100763 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200764 CONFIG_SYS_OMAP24_I2C_SPEED,
765 CONFIG_SYS_OMAP24_I2C_SLAVE,
766 0)
767U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5885052014-02-03 21:22:18 +0100768 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200769 CONFIG_SYS_OMAP24_I2C_SPEED1,
770 CONFIG_SYS_OMAP24_I2C_SLAVE1,
771 1)
772#if (I2C_BUS_MAX > 2)
773#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
774#define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
775#endif
776#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
777#define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
778#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100779
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200780U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
781 omap24_i2c_read, omap24_i2c_write, NULL,
782 CONFIG_SYS_OMAP24_I2C_SPEED2,
783 CONFIG_SYS_OMAP24_I2C_SLAVE2,
784 2)
785#if (I2C_BUS_MAX > 3)
786#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
787#define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
788#endif
789#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
790#define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
791#endif
Steve Sakoman10acc712010-06-12 06:42:57 -0700792
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200793U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
794 omap24_i2c_read, omap24_i2c_write, NULL,
795 CONFIG_SYS_OMAP24_I2C_SPEED3,
796 CONFIG_SYS_OMAP24_I2C_SLAVE3,
797 3)
798#if (I2C_BUS_MAX > 4)
799#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
800#define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
801#endif
802#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
803#define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
804#endif
805
806U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
807 omap24_i2c_read, omap24_i2c_write, NULL,
808 CONFIG_SYS_OMAP24_I2C_SPEED4,
809 CONFIG_SYS_OMAP24_I2C_SLAVE4,
810 4)
811#endif
812#endif
813#endif