blob: 00063431040313e5240c096e9f4fb7349acd2cf9 [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 *
Hannes Schmelzer4c302b92015-05-28 15:41:12 +020036 * Copyright (c) 2014 Hannes Schmelzer <oe5hpm@oevsv.at>, B&R
Hannes Petermaierd5243352014-02-03 21:22:18 +010037 * - Added support for set_speed
38 *
wdenk8ed96042005-01-09 23:16:25 +000039 */
40
41#include <common.h>
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +053042#include <dm.h>
Heiko Schocher6789e842013-10-22 11:03:18 +020043#include <i2c.h>
wdenk289f9322005-01-12 00:15:14 +000044
wdenk8ed96042005-01-09 23:16:25 +000045#include <asm/arch/i2c.h>
46#include <asm/io.h>
47
Steve Sakoman938717c2010-06-12 06:42:57 -070048#include "omap24xx_i2c.h"
49
John Rigby29565322010-12-20 18:27:51 -070050DECLARE_GLOBAL_DATA_PTR;
51
Tom Rinicec487a2012-02-20 18:49:16 +000052#define I2C_TIMEOUT 1000
Steve Sakomand7083952010-07-19 20:31:55 -070053
Lubomir Popov960187f2013-06-01 06:44:38 +000054/* Absolutely safe for status update at 100 kHz I2C: */
55#define I2C_WAIT 200
56
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +053057struct omap_i2c {
58 struct udevice *clk;
59 struct i2c *regs;
60 unsigned int speed;
61 int waitdelay;
62 int clk_id;
63};
64
Hannes Petermaierd5243352014-02-03 21:22:18 +010065static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
66{
67 unsigned int sampleclk, prescaler;
68 int fsscll, fssclh;
wdenk8ed96042005-01-09 23:16:25 +000069
Hannes Petermaierd5243352014-02-03 21:22:18 +010070 speed <<= 1;
71 prescaler = 0;
72 /*
73 * some divisors may cause a precission loss, but shouldn't
74 * be a big thing, because i2c_clk is then allready very slow.
75 */
76 while (prescaler <= 0xFF) {
77 sampleclk = I2C_IP_CLK / (prescaler+1);
78
79 fsscll = sampleclk / speed;
80 fssclh = fsscll;
81 fsscll -= I2C_FASTSPEED_SCLL_TRIM;
82 fssclh -= I2C_FASTSPEED_SCLH_TRIM;
83
84 if (((fsscll > 0) && (fssclh > 0)) &&
85 ((fsscll <= (255-I2C_FASTSPEED_SCLL_TRIM)) &&
86 (fssclh <= (255-I2C_FASTSPEED_SCLH_TRIM)))) {
87 if (pscl)
88 *pscl = fsscll;
89 if (psch)
90 *psch = fssclh;
91
92 return prescaler;
93 }
94 prescaler++;
95 }
96 return -1;
97}
Mugunthan V Nbe243e42016-07-18 15:11:00 +053098
99/*
100 * Wait for the bus to be free by checking the Bus Busy (BB)
101 * bit to become clear
102 */
103static int wait_for_bb(struct i2c *i2c_base, int waitdelay)
wdenk8ed96042005-01-09 23:16:25 +0000104{
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530105 int timeout = I2C_TIMEOUT;
106 u16 stat;
107
108 writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
109#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
110 while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
111#else
112 /* Read RAW status */
113 while ((stat = readw(&i2c_base->irqstatus_raw) &
114 I2C_STAT_BB) && timeout--) {
115#endif
116 writew(stat, &i2c_base->stat);
117 udelay(waitdelay);
118 }
119
120 if (timeout <= 0) {
121 printf("Timed out in wait_for_bb: status=%04x\n",
122 stat);
123 return 1;
124 }
125 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
126 return 0;
127}
128
129/*
130 * Wait for the I2C controller to complete current action
131 * and update status
132 */
133static u16 wait_for_event(struct i2c *i2c_base, int waitdelay)
134{
135 u16 status;
136 int timeout = I2C_TIMEOUT;
137
138 do {
139 udelay(waitdelay);
140#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
141 status = readw(&i2c_base->stat);
142#else
143 /* Read RAW status */
144 status = readw(&i2c_base->irqstatus_raw);
145#endif
146 } while (!(status &
147 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
148 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
149 I2C_STAT_AL)) && timeout--);
150
151 if (timeout <= 0) {
152 printf("Timed out in wait_for_event: status=%04x\n",
153 status);
154 /*
155 * If status is still 0 here, probably the bus pads have
156 * not been configured for I2C, and/or pull-ups are missing.
157 */
158 printf("Check if pads/pull-ups of bus are properly configured\n");
159 writew(0xFFFF, &i2c_base->stat);
160 status = 0;
161 }
162
163 return status;
164}
165
166static void flush_fifo(struct i2c *i2c_base)
167{
168 u16 stat;
169
170 /*
171 * note: if you try and read data when its not there or ready
172 * you get a bus error
173 */
174 while (1) {
175 stat = readw(&i2c_base->stat);
176 if (stat == I2C_STAT_RRDY) {
177 readb(&i2c_base->data);
178 writew(I2C_STAT_RRDY, &i2c_base->stat);
179 udelay(1000);
180 } else
181 break;
182 }
183}
184
185static int __omap24_i2c_setspeed(struct i2c *i2c_base, uint speed,
186 int *waitdelay)
187{
Hannes Petermaierd5243352014-02-03 21:22:18 +0100188 int psc, fsscll = 0, fssclh = 0;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500189 int hsscll = 0, hssclh = 0;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100190 u32 scll = 0, sclh = 0;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500191
Hannes Petermaierd5243352014-02-03 21:22:18 +0100192 if (speed >= OMAP_I2C_HIGH_SPEED) {
Tom Rix7f79dfb2009-06-28 12:52:27 -0500193 /* High speed */
Hannes Petermaierd5243352014-02-03 21:22:18 +0100194 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
195 psc -= 1;
196 if (psc < I2C_PSC_MIN) {
197 printf("Error : I2C unsupported prescaler %d\n", psc);
198 return -1;
199 }
Tom Rix7f79dfb2009-06-28 12:52:27 -0500200
201 /* For first phase of HS mode */
Hannes Petermaierd5243352014-02-03 21:22:18 +0100202 fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
203
204 fssclh = fsscll;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500205
206 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
207 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
208 if (((fsscll < 0) || (fssclh < 0)) ||
209 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müller49e9b4b2012-01-04 15:26:19 +0000210 puts("Error : I2C initializing first phase clock\n");
Hannes Petermaierd5243352014-02-03 21:22:18 +0100211 return -1;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500212 }
213
214 /* For second phase of HS mode */
215 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
216
217 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
218 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
219 if (((fsscll < 0) || (fssclh < 0)) ||
220 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müller49e9b4b2012-01-04 15:26:19 +0000221 puts("Error : I2C initializing second phase clock\n");
Hannes Petermaierd5243352014-02-03 21:22:18 +0100222 return -1;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500223 }
224
225 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
226 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
227
228 } else {
229 /* Standard and fast speed */
Hannes Petermaierd5243352014-02-03 21:22:18 +0100230 psc = omap24_i2c_findpsc(&scll, &sclh, speed);
231 if (0 > psc) {
Andreas Müller49e9b4b2012-01-04 15:26:19 +0000232 puts("Error : I2C initializing clock\n");
Hannes Petermaierd5243352014-02-03 21:22:18 +0100233 return -1;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500234 }
Tom Rix7f79dfb2009-06-28 12:52:27 -0500235 }
wdenk8ed96042005-01-09 23:16:25 +0000236
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530237 *waitdelay = (10000000 / speed) * 2; /* wait for 20 clkperiods */
Hannes Petermaierd5243352014-02-03 21:22:18 +0100238 writew(0, &i2c_base->con);
239 writew(psc, &i2c_base->psc);
240 writew(scll, &i2c_base->scll);
241 writew(sclh, &i2c_base->sclh);
242 writew(I2C_CON_EN, &i2c_base->con);
243 writew(0xFFFF, &i2c_base->stat); /* clear all pending status */
244
245 return 0;
246}
Heiko Schocherf7c10532014-06-30 09:12:09 +0200247
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530248static void omap24_i2c_deblock(struct i2c *i2c_base)
Heiko Schocherf7c10532014-06-30 09:12:09 +0200249{
Heiko Schocherf7c10532014-06-30 09:12:09 +0200250 int i;
251 u16 systest;
252 u16 orgsystest;
253
254 /* set test mode ST_EN = 1 */
255 orgsystest = readw(&i2c_base->systest);
256 systest = orgsystest;
257 /* enable testmode */
258 systest |= I2C_SYSTEST_ST_EN;
259 writew(systest, &i2c_base->systest);
260 systest &= ~I2C_SYSTEST_TMODE_MASK;
261 systest |= 3 << I2C_SYSTEST_TMODE_SHIFT;
262 writew(systest, &i2c_base->systest);
263
264 /* set SCL, SDA = 1 */
265 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
266 writew(systest, &i2c_base->systest);
267 udelay(10);
268
269 /* toggle scl 9 clocks */
270 for (i = 0; i < 9; i++) {
271 /* SCL = 0 */
272 systest &= ~I2C_SYSTEST_SCL_O;
273 writew(systest, &i2c_base->systest);
274 udelay(10);
275 /* SCL = 1 */
276 systest |= I2C_SYSTEST_SCL_O;
277 writew(systest, &i2c_base->systest);
278 udelay(10);
279 }
280
281 /* send stop */
282 systest &= ~I2C_SYSTEST_SDA_O;
283 writew(systest, &i2c_base->systest);
284 udelay(10);
285 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
286 writew(systest, &i2c_base->systest);
287 udelay(10);
288
289 /* restore original mode */
290 writew(orgsystest, &i2c_base->systest);
291}
292
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530293static void __omap24_i2c_init(struct i2c *i2c_base, int speed, int slaveadd,
294 int *waitdelay)
Hannes Petermaierd5243352014-02-03 21:22:18 +0100295{
Hannes Petermaierd5243352014-02-03 21:22:18 +0100296 int timeout = I2C_TIMEOUT;
Heiko Schocherf7c10532014-06-30 09:12:09 +0200297 int deblock = 1;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100298
Heiko Schocherf7c10532014-06-30 09:12:09 +0200299retry:
Michael Jones89677b22011-07-27 14:01:55 -0400300 if (readw(&i2c_base->con) & I2C_CON_EN) {
301 writew(0, &i2c_base->con);
302 udelay(50000);
wdenk8ed96042005-01-09 23:16:25 +0000303 }
304
Tom Rinicec487a2012-02-20 18:49:16 +0000305 writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
306 udelay(1000);
307
308 writew(I2C_CON_EN, &i2c_base->con);
309 while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
310 if (timeout <= 0) {
311 puts("ERROR: Timeout in soft-reset\n");
312 return;
313 }
314 udelay(1000);
315 }
316
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530317 if (0 != __omap24_i2c_setspeed(i2c_base, speed, waitdelay)) {
Hannes Petermaierd5243352014-02-03 21:22:18 +0100318 printf("ERROR: failed to setup I2C bus-speed!\n");
319 return;
320 }
Tom Rix7f79dfb2009-06-28 12:52:27 -0500321
wdenk8ed96042005-01-09 23:16:25 +0000322 /* own address */
Michael Jones89677b22011-07-27 14:01:55 -0400323 writew(slaveadd, &i2c_base->oa);
Hannes Petermaierd5243352014-02-03 21:22:18 +0100324
Lubomir Popov960187f2013-06-01 06:44:38 +0000325#if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
326 /*
327 * Have to enable interrupts for OMAP2/3, these IPs don't have
328 * an 'irqstatus_raw' register and we shall have to poll 'stat'
329 */
Michael Jones89677b22011-07-27 14:01:55 -0400330 writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
Lubomir Popov960187f2013-06-01 06:44:38 +0000331 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
332#endif
Michael Jones89677b22011-07-27 14:01:55 -0400333 udelay(1000);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530334 flush_fifo(i2c_base);
Michael Jones89677b22011-07-27 14:01:55 -0400335 writew(0xFFFF, &i2c_base->stat);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200336
337 /* Handle possible failed I2C state */
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530338 if (wait_for_bb(i2c_base, *waitdelay))
Heiko Schocherf7c10532014-06-30 09:12:09 +0200339 if (deblock == 1) {
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530340 omap24_i2c_deblock(i2c_base);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200341 deblock = 0;
342 goto retry;
343 }
Tom Rinicec487a2012-02-20 18:49:16 +0000344}
345
Lubomir Popov960187f2013-06-01 06:44:38 +0000346/*
347 * i2c_probe: Use write access. Allows to identify addresses that are
348 * write-only (like the config register of dual-port EEPROMs)
349 */
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530350static int __omap24_i2c_probe(struct i2c *i2c_base, int waitdelay, uchar chip)
wdenk8ed96042005-01-09 23:16:25 +0000351{
Tom Rinicec487a2012-02-20 18:49:16 +0000352 u16 status;
wdenk8ed96042005-01-09 23:16:25 +0000353 int res = 1; /* default = fail */
354
Michael Jones89677b22011-07-27 14:01:55 -0400355 if (chip == readw(&i2c_base->oa))
wdenk8ed96042005-01-09 23:16:25 +0000356 return res;
wdenk8ed96042005-01-09 23:16:25 +0000357
Lubomir Popov960187f2013-06-01 06:44:38 +0000358 /* Wait until bus is free */
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530359 if (wait_for_bb(i2c_base, waitdelay))
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000360 return res;
wdenk8ed96042005-01-09 23:16:25 +0000361
Lubomir Popov960187f2013-06-01 06:44:38 +0000362 /* No data transfer, slave addr only */
Michael Jones89677b22011-07-27 14:01:55 -0400363 writew(chip, &i2c_base->sa);
Lubomir Popov960187f2013-06-01 06:44:38 +0000364 /* Stop bit needed here */
365 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
366 I2C_CON_STP, &i2c_base->con);
wdenk8ed96042005-01-09 23:16:25 +0000367
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530368 status = wait_for_event(i2c_base, waitdelay);
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000369
Lubomir Popov960187f2013-06-01 06:44:38 +0000370 if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
371 /*
372 * With current high-level command implementation, notifying
373 * the user shall flood the console with 127 messages. If
374 * silent exit is desired upon unconfigured bus, remove the
375 * following 'if' section:
376 */
377 if (status == I2C_STAT_XRDY)
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530378 printf("i2c_probe: pads on bus probably not configured (status=0x%x)\n",
379 status);
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000380
Lubomir Popov960187f2013-06-01 06:44:38 +0000381 goto pr_exit;
Tom Rini168a5ac2012-05-21 06:46:29 +0000382 }
Tom Rinicec487a2012-02-20 18:49:16 +0000383
Lubomir Popov960187f2013-06-01 06:44:38 +0000384 /* Check for ACK (!NAK) */
385 if (!(status & I2C_STAT_NACK)) {
Hannes Petermaierd5243352014-02-03 21:22:18 +0100386 res = 0; /* Device found */
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530387 udelay(waitdelay);/* Required by AM335X in SPL */
Lubomir Popov960187f2013-06-01 06:44:38 +0000388 /* Abort transfer (force idle state) */
389 writew(I2C_CON_MST | I2C_CON_TRX, &i2c_base->con); /* Reset */
390 udelay(1000);
391 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX |
392 I2C_CON_STP, &i2c_base->con); /* STP */
393 }
394pr_exit:
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530395 flush_fifo(i2c_base);
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100396 writew(0xFFFF, &i2c_base->stat);
wdenk8ed96042005-01-09 23:16:25 +0000397 return res;
398}
399
Lubomir Popov960187f2013-06-01 06:44:38 +0000400/*
401 * i2c_read: Function now uses a single I2C read transaction with bulk transfer
402 * of the requested number of bytes (note that the 'i2c md' command
403 * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
404 * defined in the board config header, this transaction shall be with
405 * Repeated Start (Sr) between the address and data phases; otherwise
406 * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
407 * The address (reg offset) may be 0, 1 or 2 bytes long.
408 * Function now reads correctly from chips that return more than one
409 * byte of data per addressed register (like TI temperature sensors),
410 * or that do not need a register address at all (such as some clock
411 * distributors).
412 */
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530413static int __omap24_i2c_read(struct i2c *i2c_base, int waitdelay, uchar chip,
414 uint addr, int alen, uchar *buffer, int len)
wdenk8ed96042005-01-09 23:16:25 +0000415{
Lubomir Popov960187f2013-06-01 06:44:38 +0000416 int i2c_error = 0;
417 u16 status;
418
419 if (alen < 0) {
420 puts("I2C read: addr len < 0\n");
421 return 1;
422 }
423 if (len < 0) {
424 puts("I2C read: data len < 0\n");
425 return 1;
426 }
427 if (buffer == NULL) {
428 puts("I2C read: NULL pointer passed\n");
429 return 1;
430 }
wdenk8ed96042005-01-09 23:16:25 +0000431
Ilya Yanok55faa582012-06-08 03:12:09 +0000432 if (alen > 2) {
Tom Rinicec487a2012-02-20 18:49:16 +0000433 printf("I2C read: addr len %d not supported\n", alen);
wdenk8ed96042005-01-09 23:16:25 +0000434 return 1;
Tom Rinicec487a2012-02-20 18:49:16 +0000435 }
wdenk8ed96042005-01-09 23:16:25 +0000436
Ilya Yanok55faa582012-06-08 03:12:09 +0000437 if (addr + len > (1 << 16)) {
Tom Rinicec487a2012-02-20 18:49:16 +0000438 puts("I2C read: address out of range\n");
439 return 1;
440 }
441
Guy Thouret32b9b552016-03-11 16:23:41 +0000442#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
443 /*
444 * EEPROM chips that implement "address overflow" are ones
445 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
446 * address and the extra bits end up in the "chip address"
447 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
448 * four 256 byte chips.
449 *
450 * Note that we consider the length of the address field to
451 * still be one byte because the extra address bits are
452 * hidden in the chip address.
453 */
454 if (alen > 0)
455 chip |= ((addr >> (alen * 8)) &
456 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
457#endif
458
Lubomir Popov960187f2013-06-01 06:44:38 +0000459 /* Wait until bus not busy */
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530460 if (wait_for_bb(i2c_base, waitdelay))
Lubomir Popov960187f2013-06-01 06:44:38 +0000461 return 1;
462
463 /* Zero, one or two bytes reg address (offset) */
464 writew(alen, &i2c_base->cnt);
465 /* Set slave address */
466 writew(chip, &i2c_base->sa);
467
468 if (alen) {
469 /* Must write reg offset first */
470#ifdef CONFIG_I2C_REPEATED_START
471 /* No stop bit, use Repeated Start (Sr) */
472 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
473 I2C_CON_TRX, &i2c_base->con);
474#else
475 /* Stop - Start (P-S) */
476 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP |
477 I2C_CON_TRX, &i2c_base->con);
478#endif
479 /* Send register offset */
480 while (1) {
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530481 status = wait_for_event(i2c_base, waitdelay);
Lubomir Popov960187f2013-06-01 06:44:38 +0000482 /* Try to identify bus that is not padconf'd for I2C */
483 if (status == I2C_STAT_XRDY) {
484 i2c_error = 2;
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530485 printf("i2c_read (addr phase): pads on bus probably not configured (status=0x%x)\n",
486 status);
Lubomir Popov960187f2013-06-01 06:44:38 +0000487 goto rd_exit;
488 }
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_read: error waiting for addr ACK (status=0x%x)\n",
492 status);
493 goto rd_exit;
494 }
495 if (alen) {
496 if (status & I2C_STAT_XRDY) {
497 alen--;
498 /* Do we have to use byte access? */
499 writeb((addr >> (8 * alen)) & 0xff,
500 &i2c_base->data);
501 writew(I2C_STAT_XRDY, &i2c_base->stat);
502 }
503 }
504 if (status & I2C_STAT_ARDY) {
505 writew(I2C_STAT_ARDY, &i2c_base->stat);
506 break;
507 }
508 }
509 }
510 /* Set slave address */
511 writew(chip, &i2c_base->sa);
512 /* Read len bytes from slave */
513 writew(len, &i2c_base->cnt);
514 /* Need stop bit here */
515 writew(I2C_CON_EN | I2C_CON_MST |
516 I2C_CON_STT | I2C_CON_STP,
517 &i2c_base->con);
518
519 /* Receive data */
520 while (1) {
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530521 status = wait_for_event(i2c_base, waitdelay);
Lubomir Popov960187f2013-06-01 06:44:38 +0000522 /*
523 * Try to identify bus that is not padconf'd for I2C. This
524 * state could be left over from previous transactions if
525 * the address phase is skipped due to alen=0.
526 */
527 if (status == I2C_STAT_XRDY) {
528 i2c_error = 2;
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530529 printf("i2c_read (data phase): pads on bus probably not configured (status=0x%x)\n",
530 status);
Lubomir Popov960187f2013-06-01 06:44:38 +0000531 goto rd_exit;
532 }
Hannes Petermaierd5243352014-02-03 21:22:18 +0100533 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000534 i2c_error = 1;
535 goto rd_exit;
536 }
537 if (status & I2C_STAT_RRDY) {
538 *buffer++ = readb(&i2c_base->data);
539 writew(I2C_STAT_RRDY, &i2c_base->stat);
540 }
541 if (status & I2C_STAT_ARDY) {
542 writew(I2C_STAT_ARDY, &i2c_base->stat);
543 break;
wdenk8ed96042005-01-09 23:16:25 +0000544 }
545 }
546
Lubomir Popov960187f2013-06-01 06:44:38 +0000547rd_exit:
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530548 flush_fifo(i2c_base);
Lubomir Popov960187f2013-06-01 06:44:38 +0000549 writew(0xFFFF, &i2c_base->stat);
Lubomir Popov960187f2013-06-01 06:44:38 +0000550 return i2c_error;
wdenk8ed96042005-01-09 23:16:25 +0000551}
552
Lubomir Popov960187f2013-06-01 06:44:38 +0000553/* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530554static int __omap24_i2c_write(struct i2c *i2c_base, int waitdelay, uchar chip,
555 uint addr, int alen, uchar *buffer, int len)
wdenk8ed96042005-01-09 23:16:25 +0000556{
Tom Rinicec487a2012-02-20 18:49:16 +0000557 int i;
558 u16 status;
559 int i2c_error = 0;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100560 int timeout = I2C_TIMEOUT;
Lubomir Popov960187f2013-06-01 06:44:38 +0000561
562 if (alen < 0) {
563 puts("I2C write: addr len < 0\n");
564 return 1;
565 }
566
567 if (len < 0) {
568 puts("I2C write: data len < 0\n");
569 return 1;
570 }
571
572 if (buffer == NULL) {
573 puts("I2C write: NULL pointer passed\n");
574 return 1;
575 }
wdenk8ed96042005-01-09 23:16:25 +0000576
Ilya Yanok55faa582012-06-08 03:12:09 +0000577 if (alen > 2) {
Tom Rinicec487a2012-02-20 18:49:16 +0000578 printf("I2C write: addr len %d not supported\n", alen);
wdenk8ed96042005-01-09 23:16:25 +0000579 return 1;
Tom Rinicec487a2012-02-20 18:49:16 +0000580 }
wdenk8ed96042005-01-09 23:16:25 +0000581
Ilya Yanok55faa582012-06-08 03:12:09 +0000582 if (addr + len > (1 << 16)) {
Tom Rinicec487a2012-02-20 18:49:16 +0000583 printf("I2C write: address 0x%x + 0x%x out of range\n",
Lubomir Popov960187f2013-06-01 06:44:38 +0000584 addr, len);
wdenk8ed96042005-01-09 23:16:25 +0000585 return 1;
586 }
587
Guy Thouret32b9b552016-03-11 16:23:41 +0000588#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
589 /*
590 * EEPROM chips that implement "address overflow" are ones
591 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
592 * address and the extra bits end up in the "chip address"
593 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
594 * four 256 byte chips.
595 *
596 * Note that we consider the length of the address field to
597 * still be one byte because the extra address bits are
598 * hidden in the chip address.
599 */
600 if (alen > 0)
601 chip |= ((addr >> (alen * 8)) &
602 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
603#endif
604
Lubomir Popov960187f2013-06-01 06:44:38 +0000605 /* Wait until bus not busy */
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530606 if (wait_for_bb(i2c_base, waitdelay))
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000607 return 1;
Michael Jones0607e2b2011-09-04 14:01:55 -0400608
Lubomir Popov960187f2013-06-01 06:44:38 +0000609 /* Start address phase - will write regoffset + len bytes data */
Tom Rinicec487a2012-02-20 18:49:16 +0000610 writew(alen + len, &i2c_base->cnt);
Lubomir Popov960187f2013-06-01 06:44:38 +0000611 /* Set slave address */
Michael Jones0607e2b2011-09-04 14:01:55 -0400612 writew(chip, &i2c_base->sa);
Lubomir Popov960187f2013-06-01 06:44:38 +0000613 /* Stop bit needed here */
Michael Jones0607e2b2011-09-04 14:01:55 -0400614 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
Lubomir Popov960187f2013-06-01 06:44:38 +0000615 I2C_CON_STP, &i2c_base->con);
Michael Jones0607e2b2011-09-04 14:01:55 -0400616
Lubomir Popov960187f2013-06-01 06:44:38 +0000617 while (alen) {
618 /* Must write reg offset (one or two bytes) */
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530619 status = wait_for_event(i2c_base, waitdelay);
Lubomir Popov960187f2013-06-01 06:44:38 +0000620 /* Try to identify bus that is not padconf'd for I2C */
621 if (status == I2C_STAT_XRDY) {
622 i2c_error = 2;
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530623 printf("i2c_write: pads on bus probably not configured (status=0x%x)\n",
624 status);
Lubomir Popov960187f2013-06-01 06:44:38 +0000625 goto wr_exit;
626 }
Hannes Petermaierd5243352014-02-03 21:22:18 +0100627 if (status == 0 || (status & I2C_STAT_NACK)) {
Michael Jones0607e2b2011-09-04 14:01:55 -0400628 i2c_error = 1;
Lubomir Popov960187f2013-06-01 06:44:38 +0000629 printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
630 status);
631 goto wr_exit;
Tom Rinicec487a2012-02-20 18:49:16 +0000632 }
Tom Rinicec487a2012-02-20 18:49:16 +0000633 if (status & I2C_STAT_XRDY) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000634 alen--;
635 writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data);
Tom Rinicec487a2012-02-20 18:49:16 +0000636 writew(I2C_STAT_XRDY, &i2c_base->stat);
637 } else {
638 i2c_error = 1;
Lubomir Popov960187f2013-06-01 06:44:38 +0000639 printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
640 status);
641 goto wr_exit;
642 }
643 }
644 /* Address phase is over, now write data */
645 for (i = 0; i < len; i++) {
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530646 status = wait_for_event(i2c_base, waitdelay);
Hannes Petermaierd5243352014-02-03 21:22:18 +0100647 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000648 i2c_error = 1;
649 printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
650 status);
651 goto wr_exit;
652 }
653 if (status & I2C_STAT_XRDY) {
654 writeb(buffer[i], &i2c_base->data);
655 writew(I2C_STAT_XRDY, &i2c_base->stat);
656 } else {
657 i2c_error = 1;
658 printf("i2c_write: bus not ready for data Tx (i=%d)\n",
659 i);
660 goto wr_exit;
wdenk8ed96042005-01-09 23:16:25 +0000661 }
662 }
Hannes Petermaierd5243352014-02-03 21:22:18 +0100663 /*
664 * poll ARDY bit for making sure that last byte really has been
665 * transferred on the bus.
666 */
667 do {
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530668 status = wait_for_event(i2c_base, waitdelay);
Hannes Petermaierd5243352014-02-03 21:22:18 +0100669 } while (!(status & I2C_STAT_ARDY) && timeout--);
670 if (timeout <= 0)
671 printf("i2c_write: timed out writig last byte!\n");
wdenk8ed96042005-01-09 23:16:25 +0000672
Lubomir Popov960187f2013-06-01 06:44:38 +0000673wr_exit:
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530674 flush_fifo(i2c_base);
Michael Jones0607e2b2011-09-04 14:01:55 -0400675 writew(0xFFFF, &i2c_base->stat);
Tom Rinicec487a2012-02-20 18:49:16 +0000676 return i2c_error;
wdenk8ed96042005-01-09 23:16:25 +0000677}
678
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +0530679#ifndef CONFIG_DM_I2C
Lubomir Popov960187f2013-06-01 06:44:38 +0000680/*
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530681 * The legacy I2C functions. These need to get removed once
682 * all users of this driver are converted to DM.
Lubomir Popov960187f2013-06-01 06:44:38 +0000683 */
Heiko Schocher6789e842013-10-22 11:03:18 +0200684static struct i2c *omap24_get_base(struct i2c_adapter *adap)
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100685{
Heiko Schocher6789e842013-10-22 11:03:18 +0200686 switch (adap->hwadapnr) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000687 case 0:
Heiko Schocher6789e842013-10-22 11:03:18 +0200688 return (struct i2c *)I2C_BASE1;
Lubomir Popov960187f2013-06-01 06:44:38 +0000689 break;
690 case 1:
Heiko Schocher6789e842013-10-22 11:03:18 +0200691 return (struct i2c *)I2C_BASE2;
Lubomir Popov960187f2013-06-01 06:44:38 +0000692 break;
693#if (I2C_BUS_MAX > 2)
694 case 2:
Heiko Schocher6789e842013-10-22 11:03:18 +0200695 return (struct i2c *)I2C_BASE3;
Lubomir Popov960187f2013-06-01 06:44:38 +0000696 break;
697#if (I2C_BUS_MAX > 3)
698 case 3:
Heiko Schocher6789e842013-10-22 11:03:18 +0200699 return (struct i2c *)I2C_BASE4;
Lubomir Popov960187f2013-06-01 06:44:38 +0000700 break;
701#if (I2C_BUS_MAX > 4)
702 case 4:
Heiko Schocher6789e842013-10-22 11:03:18 +0200703 return (struct i2c *)I2C_BASE5;
Lubomir Popov960187f2013-06-01 06:44:38 +0000704 break;
705#endif
706#endif
707#endif
Heiko Schocher6789e842013-10-22 11:03:18 +0200708 default:
709 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
710 break;
Lubomir Popov960187f2013-06-01 06:44:38 +0000711 }
Heiko Schocher6789e842013-10-22 11:03:18 +0200712 return NULL;
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100713}
Steve Sakoman938717c2010-06-12 06:42:57 -0700714
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530715
716static int omap24_i2c_read(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_read(i2c_base, adap->waitdelay, chip, addr,
722 alen, buffer, len);
723}
724
725
726static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
727 int alen, uchar *buffer, int len)
728{
729 struct i2c *i2c_base = omap24_get_base(adap);
730
731 return __omap24_i2c_write(i2c_base, adap->waitdelay, chip, addr,
732 alen, buffer, len);
733}
734
735static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
736{
737 struct i2c *i2c_base = omap24_get_base(adap);
738 int ret;
739
740 ret = __omap24_i2c_setspeed(i2c_base, speed, &adap->waitdelay);
741 if (ret) {
742 error("%s: set i2c speed failed\n", __func__);
743 return ret;
744 }
745
746 adap->speed = speed;
747
748 return 0;
749}
750
751static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
752{
753 struct i2c *i2c_base = omap24_get_base(adap);
754
755 return __omap24_i2c_init(i2c_base, speed, slaveadd, &adap->waitdelay);
756}
757
758static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
759{
760 struct i2c *i2c_base = omap24_get_base(adap);
761
762 return __omap24_i2c_probe(i2c_base, adap->waitdelay, chip);
763}
764
Heiko Schocher6789e842013-10-22 11:03:18 +0200765#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
766#define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
767#endif
768#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
769#define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
770#endif
771
772U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5243352014-02-03 21:22:18 +0100773 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Heiko Schocher6789e842013-10-22 11:03:18 +0200774 CONFIG_SYS_OMAP24_I2C_SPEED,
775 CONFIG_SYS_OMAP24_I2C_SLAVE,
776 0)
777U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5243352014-02-03 21:22:18 +0100778 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Heiko Schocher6789e842013-10-22 11:03:18 +0200779 CONFIG_SYS_OMAP24_I2C_SPEED1,
780 CONFIG_SYS_OMAP24_I2C_SLAVE1,
781 1)
782#if (I2C_BUS_MAX > 2)
783#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
784#define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
785#endif
786#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
787#define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
788#endif
789
790U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
791 omap24_i2c_read, omap24_i2c_write, NULL,
792 CONFIG_SYS_OMAP24_I2C_SPEED2,
793 CONFIG_SYS_OMAP24_I2C_SLAVE2,
794 2)
795#if (I2C_BUS_MAX > 3)
796#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
797#define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
798#endif
799#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
800#define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
801#endif
802
803U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
804 omap24_i2c_read, omap24_i2c_write, NULL,
805 CONFIG_SYS_OMAP24_I2C_SPEED3,
806 CONFIG_SYS_OMAP24_I2C_SLAVE3,
807 3)
808#if (I2C_BUS_MAX > 4)
809#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
810#define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
811#endif
812#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
813#define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
814#endif
815
816U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
817 omap24_i2c_read, omap24_i2c_write, NULL,
818 CONFIG_SYS_OMAP24_I2C_SPEED4,
819 CONFIG_SYS_OMAP24_I2C_SLAVE4,
820 4)
821#endif
822#endif
823#endif
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +0530824
825#else /* CONFIG_DM_I2C */
826
827static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
828{
829 struct omap_i2c *priv = dev_get_priv(bus);
830 int ret;
831
832 debug("i2c_xfer: %d messages\n", nmsgs);
833 for (; nmsgs > 0; nmsgs--, msg++) {
834 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
835 if (msg->flags & I2C_M_RD) {
836 ret = __omap24_i2c_read(priv->regs, priv->waitdelay,
837 msg->addr, 0, 0, msg->buf,
838 msg->len);
839 } else {
840 ret = __omap24_i2c_write(priv->regs, priv->waitdelay,
841 msg->addr, 0, 0, msg->buf,
842 msg->len);
843 }
844 if (ret) {
845 debug("i2c_write: error sending\n");
846 return -EREMOTEIO;
847 }
848 }
849
850 return 0;
851}
852
853static int omap_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
854{
855 struct omap_i2c *priv = dev_get_priv(bus);
856
857 priv->speed = speed;
858
859 return __omap24_i2c_setspeed(priv->regs, speed, &priv->waitdelay);
860}
861
862static int omap_i2c_probe_chip(struct udevice *bus, uint chip_addr,
863 uint chip_flags)
864{
865 struct omap_i2c *priv = dev_get_priv(bus);
866
867 return __omap24_i2c_probe(priv->regs, priv->waitdelay, chip_addr);
868}
869
870static int omap_i2c_probe(struct udevice *bus)
871{
872 struct omap_i2c *priv = dev_get_priv(bus);
873
874 __omap24_i2c_init(priv->regs, priv->speed, 0, &priv->waitdelay);
875
876 return 0;
877}
878
879static int omap_i2c_ofdata_to_platdata(struct udevice *bus)
880{
881 struct omap_i2c *priv = dev_get_priv(bus);
882
883 priv->regs = map_physmem(dev_get_addr(bus), sizeof(void *),
884 MAP_NOCACHE);
885 priv->speed = CONFIG_SYS_OMAP24_I2C_SPEED;
886
887 return 0;
888}
889
890static const struct dm_i2c_ops omap_i2c_ops = {
891 .xfer = omap_i2c_xfer,
892 .probe_chip = omap_i2c_probe_chip,
893 .set_bus_speed = omap_i2c_set_bus_speed,
894};
895
896static const struct udevice_id omap_i2c_ids[] = {
897 { .compatible = "ti,omap4-i2c" },
898 { }
899};
900
901U_BOOT_DRIVER(i2c_omap) = {
902 .name = "i2c_omap",
903 .id = UCLASS_I2C,
904 .of_match = omap_i2c_ids,
905 .ofdata_to_platdata = omap_i2c_ofdata_to_platdata,
906 .probe = omap_i2c_probe,
907 .priv_auto_alloc_size = sizeof(struct omap_i2c),
908 .ops = &omap_i2c_ops,
909 .flags = DM_FLAG_PRE_RELOC,
910};
911
912#endif /* CONFIG_DM_I2C */