blob: 9f716396dab4433c988c0137962eb32c94a8b267 [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/io.h>
46
Vignesh Rbca09ce2018-12-07 14:50:41 +010047/*
48 * Provide access to architecture-specific I2C header files for platforms
49 * that are NOT yet solely relying on CONFIG_DM_I2C, CONFIG_OF_CONTROL, and
50 * the defaults provided in 'omap24xx_i2c.h' for all U-Boot stages where I2C
51 * access is desired.
52 */
53#ifndef CONFIG_ARCH_K3
54#include <asm/arch/i2c.h>
55#endif
56
Steve Sakoman938717c2010-06-12 06:42:57 -070057#include "omap24xx_i2c.h"
58
Tom Rinicec487a2012-02-20 18:49:16 +000059#define I2C_TIMEOUT 1000
Steve Sakomand7083952010-07-19 20:31:55 -070060
Lubomir Popov960187f2013-06-01 06:44:38 +000061/* Absolutely safe for status update at 100 kHz I2C: */
62#define I2C_WAIT 200
63
Vignesh Rbca09ce2018-12-07 14:50:41 +010064enum {
65 OMAP_I2C_REV_V1 = 0,
66 OMAP_I2C_REV_V2 = 1,
67};
68
69enum {
70 OMAP_I2C_REV_REG = 0, /* Only on IP V1 (OMAP34XX) */
71 OMAP_I2C_IE_REG, /* Only on IP V1 (OMAP34XX) */
72 OMAP_I2C_STAT_REG,
73 OMAP_I2C_WE_REG,
74 OMAP_I2C_SYSS_REG,
75 OMAP_I2C_BUF_REG,
76 OMAP_I2C_CNT_REG,
77 OMAP_I2C_DATA_REG,
78 OMAP_I2C_SYSC_REG,
79 OMAP_I2C_CON_REG,
80 OMAP_I2C_OA_REG,
81 OMAP_I2C_SA_REG,
82 OMAP_I2C_PSC_REG,
83 OMAP_I2C_SCLL_REG,
84 OMAP_I2C_SCLH_REG,
85 OMAP_I2C_SYSTEST_REG,
86 OMAP_I2C_BUFSTAT_REG,
87 /* Only on IP V2 (OMAP4430, etc.) */
88 OMAP_I2C_IP_V2_REVNB_LO,
89 OMAP_I2C_IP_V2_REVNB_HI,
90 OMAP_I2C_IP_V2_IRQSTATUS_RAW,
91 OMAP_I2C_IP_V2_IRQENABLE_SET,
92 OMAP_I2C_IP_V2_IRQENABLE_CLR,
93};
94
95static const u8 __maybe_unused reg_map_ip_v1[] = {
96 [OMAP_I2C_REV_REG] = 0x00,
97 [OMAP_I2C_IE_REG] = 0x04,
98 [OMAP_I2C_STAT_REG] = 0x08,
99 [OMAP_I2C_WE_REG] = 0x0c,
100 [OMAP_I2C_SYSS_REG] = 0x10,
101 [OMAP_I2C_BUF_REG] = 0x14,
102 [OMAP_I2C_CNT_REG] = 0x18,
103 [OMAP_I2C_DATA_REG] = 0x1c,
104 [OMAP_I2C_SYSC_REG] = 0x20,
105 [OMAP_I2C_CON_REG] = 0x24,
106 [OMAP_I2C_OA_REG] = 0x28,
107 [OMAP_I2C_SA_REG] = 0x2c,
108 [OMAP_I2C_PSC_REG] = 0x30,
109 [OMAP_I2C_SCLL_REG] = 0x34,
110 [OMAP_I2C_SCLH_REG] = 0x38,
111 [OMAP_I2C_SYSTEST_REG] = 0x3c,
112 [OMAP_I2C_BUFSTAT_REG] = 0x40,
113};
114
115static const u8 __maybe_unused reg_map_ip_v2[] = {
116 [OMAP_I2C_STAT_REG] = 0x28,
117 [OMAP_I2C_WE_REG] = 0x34,
118 [OMAP_I2C_SYSS_REG] = 0x90,
119 [OMAP_I2C_BUF_REG] = 0x94,
120 [OMAP_I2C_CNT_REG] = 0x98,
121 [OMAP_I2C_DATA_REG] = 0x9c,
122 [OMAP_I2C_SYSC_REG] = 0x10,
123 [OMAP_I2C_CON_REG] = 0xa4,
124 [OMAP_I2C_OA_REG] = 0xa8,
125 [OMAP_I2C_SA_REG] = 0xac,
126 [OMAP_I2C_PSC_REG] = 0xb0,
127 [OMAP_I2C_SCLL_REG] = 0xb4,
128 [OMAP_I2C_SCLH_REG] = 0xb8,
129 [OMAP_I2C_SYSTEST_REG] = 0xbc,
130 [OMAP_I2C_BUFSTAT_REG] = 0xc0,
131 [OMAP_I2C_IP_V2_REVNB_LO] = 0x00,
132 [OMAP_I2C_IP_V2_REVNB_HI] = 0x04,
133 [OMAP_I2C_IP_V2_IRQSTATUS_RAW] = 0x24,
134 [OMAP_I2C_IP_V2_IRQENABLE_SET] = 0x2c,
135 [OMAP_I2C_IP_V2_IRQENABLE_CLR] = 0x30,
136};
137
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +0530138struct omap_i2c {
139 struct udevice *clk;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100140 int ip_rev;
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +0530141 struct i2c *regs;
142 unsigned int speed;
143 int waitdelay;
144 int clk_id;
145};
146
Vignesh Rbca09ce2018-12-07 14:50:41 +0100147static inline const u8 *omap_i2c_get_ip_reg_map(int ip_rev)
148{
149 switch (ip_rev) {
150 case OMAP_I2C_REV_V1:
151 return reg_map_ip_v1;
152 case OMAP_I2C_REV_V2:
153 /* Fall through... */
154 default:
155 return reg_map_ip_v2;
156 }
157}
158
159static inline void omap_i2c_write_reg(void __iomem *base, int ip_rev,
160 u16 val, int reg)
161{
162 writew(val, base + omap_i2c_get_ip_reg_map(ip_rev)[reg]);
163}
164
165static inline u16 omap_i2c_read_reg(void __iomem *base, int ip_rev, int reg)
166{
167 return readw(base + omap_i2c_get_ip_reg_map(ip_rev)[reg]);
168}
169
Hannes Petermaierd5243352014-02-03 21:22:18 +0100170static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
171{
Lukasz Majewskib52a3fa2017-03-15 16:59:23 +0100172 unsigned long internal_clk = 0, fclk;
173 unsigned int prescaler;
wdenk8ed96042005-01-09 23:16:25 +0000174
Hannes Petermaierd5243352014-02-03 21:22:18 +0100175 /*
Lukasz Majewskib52a3fa2017-03-15 16:59:23 +0100176 * This method is only called for Standard and Fast Mode speeds
177 *
178 * For some TI SoCs it is explicitly written in TRM (e,g, SPRUHZ6G,
179 * page 5685, Table 24-7)
180 * that the internal I2C clock (after prescaler) should be between
181 * 7-12 MHz (at least for Fast Mode (FS)).
182 *
183 * Such approach is used in v4.9 Linux kernel in:
184 * ./drivers/i2c/busses/i2c-omap.c (omap_i2c_init function).
Hannes Petermaierd5243352014-02-03 21:22:18 +0100185 */
Hannes Petermaierd5243352014-02-03 21:22:18 +0100186
Lukasz Majewskib52a3fa2017-03-15 16:59:23 +0100187 speed /= 1000; /* convert speed to kHz */
Hannes Petermaierd5243352014-02-03 21:22:18 +0100188
Lukasz Majewskib52a3fa2017-03-15 16:59:23 +0100189 if (speed > 100)
190 internal_clk = 9600;
191 else
192 internal_clk = 4000;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100193
Lukasz Majewskib52a3fa2017-03-15 16:59:23 +0100194 fclk = I2C_IP_CLK / 1000;
195 prescaler = fclk / internal_clk;
196 prescaler = prescaler - 1;
197
198 if (speed > 100) {
199 unsigned long scl;
200
201 /* Fast mode */
202 scl = internal_clk / speed;
203 *pscl = scl - (scl / 3) - I2C_FASTSPEED_SCLL_TRIM;
204 *psch = (scl / 3) - I2C_FASTSPEED_SCLH_TRIM;
205 } else {
206 /* Standard mode */
207 *pscl = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLL_TRIM;
208 *psch = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLH_TRIM;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100209 }
Lukasz Majewskib52a3fa2017-03-15 16:59:23 +0100210
211 debug("%s: speed [kHz]: %d psc: 0x%x sscl: 0x%x ssch: 0x%x\n",
212 __func__, speed, prescaler, *pscl, *psch);
213
214 if (*pscl <= 0 || *psch <= 0 || prescaler <= 0)
215 return -EINVAL;
216
217 return prescaler;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100218}
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530219
220/*
221 * Wait for the bus to be free by checking the Bus Busy (BB)
222 * bit to become clear
223 */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100224static int wait_for_bb(void __iomem *i2c_base, int ip_rev, int waitdelay)
wdenk8ed96042005-01-09 23:16:25 +0000225{
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530226 int timeout = I2C_TIMEOUT;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100227 int irq_stat_reg;
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530228 u16 stat;
229
Vignesh Rbca09ce2018-12-07 14:50:41 +0100230 irq_stat_reg = (ip_rev == OMAP_I2C_REV_V1) ?
231 OMAP_I2C_STAT_REG : OMAP_I2C_IP_V2_IRQSTATUS_RAW;
232
233 /* clear current interrupts */
234 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
235
236 while ((stat = omap_i2c_read_reg(i2c_base, ip_rev, irq_stat_reg) &
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530237 I2C_STAT_BB) && timeout--) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100238 omap_i2c_write_reg(i2c_base, ip_rev, stat, OMAP_I2C_STAT_REG);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530239 udelay(waitdelay);
240 }
241
242 if (timeout <= 0) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100243 printf("Timed out in %s: status=%04x\n", __func__, stat);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530244 return 1;
245 }
Vignesh Rbca09ce2018-12-07 14:50:41 +0100246
247 /* clear delayed stuff */
248 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530249 return 0;
250}
251
252/*
253 * Wait for the I2C controller to complete current action
254 * and update status
255 */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100256static u16 wait_for_event(void __iomem *i2c_base, int ip_rev, int waitdelay)
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530257{
258 u16 status;
259 int timeout = I2C_TIMEOUT;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100260 int irq_stat_reg;
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530261
Vignesh Rbca09ce2018-12-07 14:50:41 +0100262 irq_stat_reg = (ip_rev == OMAP_I2C_REV_V1) ?
263 OMAP_I2C_STAT_REG : OMAP_I2C_IP_V2_IRQSTATUS_RAW;
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530264 do {
265 udelay(waitdelay);
Vignesh Rbca09ce2018-12-07 14:50:41 +0100266 status = omap_i2c_read_reg(i2c_base, ip_rev, irq_stat_reg);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530267 } while (!(status &
268 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
269 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
270 I2C_STAT_AL)) && timeout--);
271
272 if (timeout <= 0) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100273 printf("Timed out in %s: status=%04x\n", __func__, status);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530274 /*
275 * If status is still 0 here, probably the bus pads have
276 * not been configured for I2C, and/or pull-ups are missing.
277 */
278 printf("Check if pads/pull-ups of bus are properly configured\n");
Vignesh Rbca09ce2018-12-07 14:50:41 +0100279 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530280 status = 0;
281 }
282
283 return status;
284}
285
Vignesh Rbca09ce2018-12-07 14:50:41 +0100286static void flush_fifo(void __iomem *i2c_base, int ip_rev)
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530287{
288 u16 stat;
289
290 /*
291 * note: if you try and read data when its not there or ready
292 * you get a bus error
293 */
294 while (1) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100295 stat = omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_STAT_REG);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530296 if (stat == I2C_STAT_RRDY) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100297 omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_DATA_REG);
298 omap_i2c_write_reg(i2c_base, ip_rev,
299 I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530300 udelay(1000);
301 } else
302 break;
303 }
304}
305
Vignesh Rbca09ce2018-12-07 14:50:41 +0100306static int __omap24_i2c_setspeed(void __iomem *i2c_base, int ip_rev, uint speed,
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530307 int *waitdelay)
308{
Hannes Petermaierd5243352014-02-03 21:22:18 +0100309 int psc, fsscll = 0, fssclh = 0;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500310 int hsscll = 0, hssclh = 0;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100311 u32 scll = 0, sclh = 0;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500312
Hannes Petermaierd5243352014-02-03 21:22:18 +0100313 if (speed >= OMAP_I2C_HIGH_SPEED) {
Tom Rix7f79dfb2009-06-28 12:52:27 -0500314 /* High speed */
Hannes Petermaierd5243352014-02-03 21:22:18 +0100315 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
316 psc -= 1;
317 if (psc < I2C_PSC_MIN) {
318 printf("Error : I2C unsupported prescaler %d\n", psc);
319 return -1;
320 }
Tom Rix7f79dfb2009-06-28 12:52:27 -0500321
322 /* For first phase of HS mode */
Hannes Petermaierd5243352014-02-03 21:22:18 +0100323 fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
324
325 fssclh = fsscll;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500326
327 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
328 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
329 if (((fsscll < 0) || (fssclh < 0)) ||
330 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müller49e9b4b2012-01-04 15:26:19 +0000331 puts("Error : I2C initializing first phase clock\n");
Hannes Petermaierd5243352014-02-03 21:22:18 +0100332 return -1;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500333 }
334
335 /* For second phase of HS mode */
336 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
337
338 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
339 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
340 if (((fsscll < 0) || (fssclh < 0)) ||
341 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müller49e9b4b2012-01-04 15:26:19 +0000342 puts("Error : I2C initializing second phase clock\n");
Hannes Petermaierd5243352014-02-03 21:22:18 +0100343 return -1;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500344 }
345
346 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
347 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
348
349 } else {
350 /* Standard and fast speed */
Hannes Petermaierd5243352014-02-03 21:22:18 +0100351 psc = omap24_i2c_findpsc(&scll, &sclh, speed);
352 if (0 > psc) {
Andreas Müller49e9b4b2012-01-04 15:26:19 +0000353 puts("Error : I2C initializing clock\n");
Hannes Petermaierd5243352014-02-03 21:22:18 +0100354 return -1;
Tom Rix7f79dfb2009-06-28 12:52:27 -0500355 }
Tom Rix7f79dfb2009-06-28 12:52:27 -0500356 }
wdenk8ed96042005-01-09 23:16:25 +0000357
Vignesh Rbca09ce2018-12-07 14:50:41 +0100358 /* wait for 20 clkperiods */
359 *waitdelay = (10000000 / speed) * 2;
360
361 omap_i2c_write_reg(i2c_base, ip_rev, 0, OMAP_I2C_CON_REG);
362 omap_i2c_write_reg(i2c_base, ip_rev, psc, OMAP_I2C_PSC_REG);
363 omap_i2c_write_reg(i2c_base, ip_rev, scll, OMAP_I2C_SCLL_REG);
364 omap_i2c_write_reg(i2c_base, ip_rev, sclh, OMAP_I2C_SCLH_REG);
365 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN, OMAP_I2C_CON_REG);
366
367 /* clear all pending status */
368 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Hannes Petermaierd5243352014-02-03 21:22:18 +0100369
370 return 0;
371}
Heiko Schocherf7c10532014-06-30 09:12:09 +0200372
Vignesh Rbca09ce2018-12-07 14:50:41 +0100373static void omap24_i2c_deblock(void __iomem *i2c_base, int ip_rev)
Heiko Schocherf7c10532014-06-30 09:12:09 +0200374{
Heiko Schocherf7c10532014-06-30 09:12:09 +0200375 int i;
376 u16 systest;
377 u16 orgsystest;
378
379 /* set test mode ST_EN = 1 */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100380 orgsystest = omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_SYSTEST_REG);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200381 systest = orgsystest;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100382
Heiko Schocherf7c10532014-06-30 09:12:09 +0200383 /* enable testmode */
384 systest |= I2C_SYSTEST_ST_EN;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100385 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200386 systest &= ~I2C_SYSTEST_TMODE_MASK;
387 systest |= 3 << I2C_SYSTEST_TMODE_SHIFT;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100388 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200389
390 /* set SCL, SDA = 1 */
391 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100392 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200393 udelay(10);
394
395 /* toggle scl 9 clocks */
396 for (i = 0; i < 9; i++) {
397 /* SCL = 0 */
398 systest &= ~I2C_SYSTEST_SCL_O;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100399 omap_i2c_write_reg(i2c_base, ip_rev,
400 systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200401 udelay(10);
402 /* SCL = 1 */
403 systest |= I2C_SYSTEST_SCL_O;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100404 omap_i2c_write_reg(i2c_base, ip_rev,
405 systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200406 udelay(10);
407 }
408
409 /* send stop */
410 systest &= ~I2C_SYSTEST_SDA_O;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100411 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200412 udelay(10);
413 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100414 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200415 udelay(10);
416
417 /* restore original mode */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100418 omap_i2c_write_reg(i2c_base, ip_rev, orgsystest, OMAP_I2C_SYSTEST_REG);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200419}
420
Vignesh Rbca09ce2018-12-07 14:50:41 +0100421static void __omap24_i2c_init(void __iomem *i2c_base, int ip_rev, int speed,
422 int slaveadd, int *waitdelay)
Hannes Petermaierd5243352014-02-03 21:22:18 +0100423{
Hannes Petermaierd5243352014-02-03 21:22:18 +0100424 int timeout = I2C_TIMEOUT;
Heiko Schocherf7c10532014-06-30 09:12:09 +0200425 int deblock = 1;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100426
Heiko Schocherf7c10532014-06-30 09:12:09 +0200427retry:
Vignesh Rbca09ce2018-12-07 14:50:41 +0100428 if (omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_CON_REG) &
429 I2C_CON_EN) {
430 omap_i2c_write_reg(i2c_base, ip_rev, 0, OMAP_I2C_CON_REG);
Michael Jones89677b22011-07-27 14:01:55 -0400431 udelay(50000);
wdenk8ed96042005-01-09 23:16:25 +0000432 }
433
Vignesh Rbca09ce2018-12-07 14:50:41 +0100434 /* for ES2 after soft reset */
435 omap_i2c_write_reg(i2c_base, ip_rev, 0x2, OMAP_I2C_SYSC_REG);
Tom Rinicec487a2012-02-20 18:49:16 +0000436 udelay(1000);
437
Vignesh Rbca09ce2018-12-07 14:50:41 +0100438 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN, OMAP_I2C_CON_REG);
439 while (!(omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_SYSS_REG) &
440 I2C_SYSS_RDONE) && timeout--) {
Tom Rinicec487a2012-02-20 18:49:16 +0000441 if (timeout <= 0) {
442 puts("ERROR: Timeout in soft-reset\n");
443 return;
444 }
445 udelay(1000);
446 }
447
Vignesh Rbca09ce2018-12-07 14:50:41 +0100448 if (__omap24_i2c_setspeed(i2c_base, ip_rev, speed, waitdelay)) {
Hannes Petermaierd5243352014-02-03 21:22:18 +0100449 printf("ERROR: failed to setup I2C bus-speed!\n");
450 return;
451 }
Tom Rix7f79dfb2009-06-28 12:52:27 -0500452
wdenk8ed96042005-01-09 23:16:25 +0000453 /* own address */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100454 omap_i2c_write_reg(i2c_base, ip_rev, slaveadd, OMAP_I2C_OA_REG);
Hannes Petermaierd5243352014-02-03 21:22:18 +0100455
Vignesh Rbca09ce2018-12-07 14:50:41 +0100456 if (ip_rev == OMAP_I2C_REV_V1) {
457 /*
458 * Have to enable interrupts for OMAP2/3, these IPs don't have
459 * an 'irqstatus_raw' register and we shall have to poll 'stat'
460 */
461 omap_i2c_write_reg(i2c_base, ip_rev, I2C_IE_XRDY_IE |
462 I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
463 I2C_IE_NACK_IE | I2C_IE_AL_IE,
464 OMAP_I2C_IE_REG);
465 }
466
Michael Jones89677b22011-07-27 14:01:55 -0400467 udelay(1000);
Vignesh Rbca09ce2018-12-07 14:50:41 +0100468 flush_fifo(i2c_base, ip_rev);
469 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200470
471 /* Handle possible failed I2C state */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100472 if (wait_for_bb(i2c_base, ip_rev, *waitdelay))
Heiko Schocherf7c10532014-06-30 09:12:09 +0200473 if (deblock == 1) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100474 omap24_i2c_deblock(i2c_base, ip_rev);
Heiko Schocherf7c10532014-06-30 09:12:09 +0200475 deblock = 0;
476 goto retry;
477 }
Tom Rinicec487a2012-02-20 18:49:16 +0000478}
479
Lubomir Popov960187f2013-06-01 06:44:38 +0000480/*
481 * i2c_probe: Use write access. Allows to identify addresses that are
482 * write-only (like the config register of dual-port EEPROMs)
483 */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100484static int __omap24_i2c_probe(void __iomem *i2c_base, int ip_rev, int waitdelay,
485 uchar chip)
wdenk8ed96042005-01-09 23:16:25 +0000486{
Tom Rinicec487a2012-02-20 18:49:16 +0000487 u16 status;
wdenk8ed96042005-01-09 23:16:25 +0000488 int res = 1; /* default = fail */
489
Vignesh Rbca09ce2018-12-07 14:50:41 +0100490 if (chip == omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_OA_REG))
wdenk8ed96042005-01-09 23:16:25 +0000491 return res;
wdenk8ed96042005-01-09 23:16:25 +0000492
Lubomir Popov960187f2013-06-01 06:44:38 +0000493 /* Wait until bus is free */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100494 if (wait_for_bb(i2c_base, ip_rev, waitdelay))
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000495 return res;
wdenk8ed96042005-01-09 23:16:25 +0000496
Lubomir Popov960187f2013-06-01 06:44:38 +0000497 /* No data transfer, slave addr only */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100498 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
wdenk8ed96042005-01-09 23:16:25 +0000499
Vignesh Rbca09ce2018-12-07 14:50:41 +0100500 /* Stop bit needed here */
501 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
502 I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP,
503 OMAP_I2C_CON_REG);
504
505 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000506
Lubomir Popov960187f2013-06-01 06:44:38 +0000507 if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
508 /*
509 * With current high-level command implementation, notifying
510 * the user shall flood the console with 127 messages. If
511 * silent exit is desired upon unconfigured bus, remove the
512 * following 'if' section:
513 */
514 if (status == I2C_STAT_XRDY)
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530515 printf("i2c_probe: pads on bus probably not configured (status=0x%x)\n",
516 status);
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000517
Lubomir Popov960187f2013-06-01 06:44:38 +0000518 goto pr_exit;
Tom Rini168a5ac2012-05-21 06:46:29 +0000519 }
Tom Rinicec487a2012-02-20 18:49:16 +0000520
Lubomir Popov960187f2013-06-01 06:44:38 +0000521 /* Check for ACK (!NAK) */
522 if (!(status & I2C_STAT_NACK)) {
Hannes Petermaierd5243352014-02-03 21:22:18 +0100523 res = 0; /* Device found */
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530524 udelay(waitdelay);/* Required by AM335X in SPL */
Lubomir Popov960187f2013-06-01 06:44:38 +0000525 /* Abort transfer (force idle state) */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100526 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_MST | I2C_CON_TRX,
527 OMAP_I2C_CON_REG); /* Reset */
Lubomir Popov960187f2013-06-01 06:44:38 +0000528 udelay(1000);
Vignesh Rbca09ce2018-12-07 14:50:41 +0100529 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
530 I2C_CON_TRX | I2C_CON_STP,
531 OMAP_I2C_CON_REG); /* STP */
Lubomir Popov960187f2013-06-01 06:44:38 +0000532 }
Vignesh Rbca09ce2018-12-07 14:50:41 +0100533
Lubomir Popov960187f2013-06-01 06:44:38 +0000534pr_exit:
Vignesh Rbca09ce2018-12-07 14:50:41 +0100535 flush_fifo(i2c_base, ip_rev);
536 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
wdenk8ed96042005-01-09 23:16:25 +0000537 return res;
538}
539
Lubomir Popov960187f2013-06-01 06:44:38 +0000540/*
541 * i2c_read: Function now uses a single I2C read transaction with bulk transfer
542 * of the requested number of bytes (note that the 'i2c md' command
543 * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
544 * defined in the board config header, this transaction shall be with
545 * Repeated Start (Sr) between the address and data phases; otherwise
546 * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
547 * The address (reg offset) may be 0, 1 or 2 bytes long.
548 * Function now reads correctly from chips that return more than one
549 * byte of data per addressed register (like TI temperature sensors),
550 * or that do not need a register address at all (such as some clock
551 * distributors).
552 */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100553static int __omap24_i2c_read(void __iomem *i2c_base, int ip_rev, int waitdelay,
554 uchar chip, uint addr, int alen, uchar *buffer,
555 int len)
wdenk8ed96042005-01-09 23:16:25 +0000556{
Lubomir Popov960187f2013-06-01 06:44:38 +0000557 int i2c_error = 0;
558 u16 status;
559
560 if (alen < 0) {
561 puts("I2C read: addr len < 0\n");
562 return 1;
563 }
Vignesh Rbca09ce2018-12-07 14:50:41 +0100564
Lubomir Popov960187f2013-06-01 06:44:38 +0000565 if (len < 0) {
566 puts("I2C read: data len < 0\n");
567 return 1;
568 }
Vignesh Rbca09ce2018-12-07 14:50:41 +0100569
Lubomir Popov960187f2013-06-01 06:44:38 +0000570 if (buffer == NULL) {
571 puts("I2C read: NULL pointer passed\n");
572 return 1;
573 }
wdenk8ed96042005-01-09 23:16:25 +0000574
Ilya Yanok55faa582012-06-08 03:12:09 +0000575 if (alen > 2) {
Tom Rinicec487a2012-02-20 18:49:16 +0000576 printf("I2C read: addr len %d not supported\n", alen);
wdenk8ed96042005-01-09 23:16:25 +0000577 return 1;
Tom Rinicec487a2012-02-20 18:49:16 +0000578 }
wdenk8ed96042005-01-09 23:16:25 +0000579
Ilya Yanok55faa582012-06-08 03:12:09 +0000580 if (addr + len > (1 << 16)) {
Tom Rinicec487a2012-02-20 18:49:16 +0000581 puts("I2C read: address out of range\n");
582 return 1;
583 }
584
Guy Thouret32b9b552016-03-11 16:23:41 +0000585#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
586 /*
587 * EEPROM chips that implement "address overflow" are ones
588 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
589 * address and the extra bits end up in the "chip address"
590 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
591 * four 256 byte chips.
592 *
593 * Note that we consider the length of the address field to
594 * still be one byte because the extra address bits are
595 * hidden in the chip address.
596 */
597 if (alen > 0)
598 chip |= ((addr >> (alen * 8)) &
599 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
600#endif
601
Lubomir Popov960187f2013-06-01 06:44:38 +0000602 /* Wait until bus not busy */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100603 if (wait_for_bb(i2c_base, ip_rev, waitdelay))
Lubomir Popov960187f2013-06-01 06:44:38 +0000604 return 1;
605
606 /* Zero, one or two bytes reg address (offset) */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100607 omap_i2c_write_reg(i2c_base, ip_rev, alen, OMAP_I2C_CNT_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000608 /* Set slave address */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100609 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000610
611 if (alen) {
612 /* Must write reg offset first */
613#ifdef CONFIG_I2C_REPEATED_START
614 /* No stop bit, use Repeated Start (Sr) */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100615 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
616 I2C_CON_STT | I2C_CON_TRX, OMAP_I2C_CON_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000617#else
618 /* Stop - Start (P-S) */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100619 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
620 I2C_CON_STT | I2C_CON_STP | I2C_CON_TRX,
621 OMAP_I2C_CON_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000622#endif
623 /* Send register offset */
624 while (1) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100625 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Lubomir Popov960187f2013-06-01 06:44:38 +0000626 /* Try to identify bus that is not padconf'd for I2C */
627 if (status == I2C_STAT_XRDY) {
628 i2c_error = 2;
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530629 printf("i2c_read (addr phase): pads on bus probably not configured (status=0x%x)\n",
630 status);
Lubomir Popov960187f2013-06-01 06:44:38 +0000631 goto rd_exit;
632 }
Hannes Petermaierd5243352014-02-03 21:22:18 +0100633 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000634 i2c_error = 1;
635 printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
636 status);
637 goto rd_exit;
638 }
639 if (alen) {
640 if (status & I2C_STAT_XRDY) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100641 u8 addr_byte;
Lubomir Popov960187f2013-06-01 06:44:38 +0000642 alen--;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100643 addr_byte = (addr >> (8 * alen)) & 0xff;
644 omap_i2c_write_reg(i2c_base, ip_rev,
645 addr_byte,
646 OMAP_I2C_DATA_REG);
647 omap_i2c_write_reg(i2c_base, ip_rev,
648 I2C_STAT_XRDY,
649 OMAP_I2C_STAT_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000650 }
651 }
652 if (status & I2C_STAT_ARDY) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100653 omap_i2c_write_reg(i2c_base, ip_rev,
654 I2C_STAT_ARDY,
655 OMAP_I2C_STAT_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000656 break;
657 }
658 }
659 }
Vignesh Rbca09ce2018-12-07 14:50:41 +0100660
Lubomir Popov960187f2013-06-01 06:44:38 +0000661 /* Set slave address */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100662 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000663 /* Read len bytes from slave */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100664 omap_i2c_write_reg(i2c_base, ip_rev, len, OMAP_I2C_CNT_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000665 /* Need stop bit here */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100666 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
667 I2C_CON_STT | I2C_CON_STP, OMAP_I2C_CON_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000668
669 /* Receive data */
670 while (1) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100671 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Lubomir Popov960187f2013-06-01 06:44:38 +0000672 /*
673 * Try to identify bus that is not padconf'd for I2C. This
674 * state could be left over from previous transactions if
675 * the address phase is skipped due to alen=0.
676 */
677 if (status == I2C_STAT_XRDY) {
678 i2c_error = 2;
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530679 printf("i2c_read (data phase): pads on bus probably not configured (status=0x%x)\n",
680 status);
Lubomir Popov960187f2013-06-01 06:44:38 +0000681 goto rd_exit;
682 }
Hannes Petermaierd5243352014-02-03 21:22:18 +0100683 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000684 i2c_error = 1;
685 goto rd_exit;
686 }
687 if (status & I2C_STAT_RRDY) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100688 *buffer++ = omap_i2c_read_reg(i2c_base, ip_rev,
689 OMAP_I2C_DATA_REG);
690 omap_i2c_write_reg(i2c_base, ip_rev,
691 I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000692 }
693 if (status & I2C_STAT_ARDY) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100694 omap_i2c_write_reg(i2c_base, ip_rev,
695 I2C_STAT_ARDY, OMAP_I2C_STAT_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000696 break;
wdenk8ed96042005-01-09 23:16:25 +0000697 }
698 }
699
Lubomir Popov960187f2013-06-01 06:44:38 +0000700rd_exit:
Vignesh Rbca09ce2018-12-07 14:50:41 +0100701 flush_fifo(i2c_base, ip_rev);
702 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000703 return i2c_error;
wdenk8ed96042005-01-09 23:16:25 +0000704}
705
Lubomir Popov960187f2013-06-01 06:44:38 +0000706/* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100707static int __omap24_i2c_write(void __iomem *i2c_base, int ip_rev, int waitdelay,
708 uchar chip, uint addr, int alen, uchar *buffer,
709 int len)
wdenk8ed96042005-01-09 23:16:25 +0000710{
Tom Rinicec487a2012-02-20 18:49:16 +0000711 int i;
712 u16 status;
713 int i2c_error = 0;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100714 int timeout = I2C_TIMEOUT;
Lubomir Popov960187f2013-06-01 06:44:38 +0000715
716 if (alen < 0) {
717 puts("I2C write: addr len < 0\n");
718 return 1;
719 }
720
721 if (len < 0) {
722 puts("I2C write: data len < 0\n");
723 return 1;
724 }
725
726 if (buffer == NULL) {
727 puts("I2C write: NULL pointer passed\n");
728 return 1;
729 }
wdenk8ed96042005-01-09 23:16:25 +0000730
Ilya Yanok55faa582012-06-08 03:12:09 +0000731 if (alen > 2) {
Tom Rinicec487a2012-02-20 18:49:16 +0000732 printf("I2C write: addr len %d not supported\n", alen);
wdenk8ed96042005-01-09 23:16:25 +0000733 return 1;
Tom Rinicec487a2012-02-20 18:49:16 +0000734 }
wdenk8ed96042005-01-09 23:16:25 +0000735
Ilya Yanok55faa582012-06-08 03:12:09 +0000736 if (addr + len > (1 << 16)) {
Tom Rinicec487a2012-02-20 18:49:16 +0000737 printf("I2C write: address 0x%x + 0x%x out of range\n",
Lubomir Popov960187f2013-06-01 06:44:38 +0000738 addr, len);
wdenk8ed96042005-01-09 23:16:25 +0000739 return 1;
740 }
741
Guy Thouret32b9b552016-03-11 16:23:41 +0000742#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
743 /*
744 * EEPROM chips that implement "address overflow" are ones
745 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
746 * address and the extra bits end up in the "chip address"
747 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
748 * four 256 byte chips.
749 *
750 * Note that we consider the length of the address field to
751 * still be one byte because the extra address bits are
752 * hidden in the chip address.
753 */
754 if (alen > 0)
755 chip |= ((addr >> (alen * 8)) &
756 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
757#endif
758
Lubomir Popov960187f2013-06-01 06:44:38 +0000759 /* Wait until bus not busy */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100760 if (wait_for_bb(i2c_base, ip_rev, waitdelay))
Vincent Stehléfebc4cd2012-12-03 05:23:16 +0000761 return 1;
Michael Jones0607e2b2011-09-04 14:01:55 -0400762
Lubomir Popov960187f2013-06-01 06:44:38 +0000763 /* Start address phase - will write regoffset + len bytes data */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100764 omap_i2c_write_reg(i2c_base, ip_rev, alen + len, OMAP_I2C_CNT_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000765 /* Set slave address */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100766 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000767 /* Stop bit needed here */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100768 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
769 I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP,
770 OMAP_I2C_CON_REG);
Michael Jones0607e2b2011-09-04 14:01:55 -0400771
Lubomir Popov960187f2013-06-01 06:44:38 +0000772 while (alen) {
773 /* Must write reg offset (one or two bytes) */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100774 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Lubomir Popov960187f2013-06-01 06:44:38 +0000775 /* Try to identify bus that is not padconf'd for I2C */
776 if (status == I2C_STAT_XRDY) {
777 i2c_error = 2;
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530778 printf("i2c_write: pads on bus probably not configured (status=0x%x)\n",
779 status);
Lubomir Popov960187f2013-06-01 06:44:38 +0000780 goto wr_exit;
781 }
Hannes Petermaierd5243352014-02-03 21:22:18 +0100782 if (status == 0 || (status & I2C_STAT_NACK)) {
Michael Jones0607e2b2011-09-04 14:01:55 -0400783 i2c_error = 1;
Lubomir Popov960187f2013-06-01 06:44:38 +0000784 printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
785 status);
786 goto wr_exit;
Tom Rinicec487a2012-02-20 18:49:16 +0000787 }
Tom Rinicec487a2012-02-20 18:49:16 +0000788 if (status & I2C_STAT_XRDY) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000789 alen--;
Vignesh Rbca09ce2018-12-07 14:50:41 +0100790 omap_i2c_write_reg(i2c_base, ip_rev,
791 (addr >> (8 * alen)) & 0xff,
792 OMAP_I2C_DATA_REG);
793 omap_i2c_write_reg(i2c_base, ip_rev,
794 I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
Tom Rinicec487a2012-02-20 18:49:16 +0000795 } else {
796 i2c_error = 1;
Lubomir Popov960187f2013-06-01 06:44:38 +0000797 printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
798 status);
799 goto wr_exit;
800 }
801 }
Vignesh Rbca09ce2018-12-07 14:50:41 +0100802
Lubomir Popov960187f2013-06-01 06:44:38 +0000803 /* Address phase is over, now write data */
804 for (i = 0; i < len; i++) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100805 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Hannes Petermaierd5243352014-02-03 21:22:18 +0100806 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000807 i2c_error = 1;
808 printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
809 status);
810 goto wr_exit;
811 }
812 if (status & I2C_STAT_XRDY) {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100813 omap_i2c_write_reg(i2c_base, ip_rev,
814 buffer[i], OMAP_I2C_DATA_REG);
815 omap_i2c_write_reg(i2c_base, ip_rev,
816 I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
Lubomir Popov960187f2013-06-01 06:44:38 +0000817 } else {
818 i2c_error = 1;
819 printf("i2c_write: bus not ready for data Tx (i=%d)\n",
820 i);
821 goto wr_exit;
wdenk8ed96042005-01-09 23:16:25 +0000822 }
823 }
Vignesh Rbca09ce2018-12-07 14:50:41 +0100824
Hannes Petermaierd5243352014-02-03 21:22:18 +0100825 /*
826 * poll ARDY bit for making sure that last byte really has been
827 * transferred on the bus.
828 */
829 do {
Vignesh Rbca09ce2018-12-07 14:50:41 +0100830 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Hannes Petermaierd5243352014-02-03 21:22:18 +0100831 } while (!(status & I2C_STAT_ARDY) && timeout--);
832 if (timeout <= 0)
833 printf("i2c_write: timed out writig last byte!\n");
wdenk8ed96042005-01-09 23:16:25 +0000834
Lubomir Popov960187f2013-06-01 06:44:38 +0000835wr_exit:
Vignesh Rbca09ce2018-12-07 14:50:41 +0100836 flush_fifo(i2c_base, ip_rev);
837 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Tom Rinicec487a2012-02-20 18:49:16 +0000838 return i2c_error;
wdenk8ed96042005-01-09 23:16:25 +0000839}
840
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +0530841#ifndef CONFIG_DM_I2C
Lubomir Popov960187f2013-06-01 06:44:38 +0000842/*
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530843 * The legacy I2C functions. These need to get removed once
844 * all users of this driver are converted to DM.
Lubomir Popov960187f2013-06-01 06:44:38 +0000845 */
Vignesh Rbca09ce2018-12-07 14:50:41 +0100846static void __iomem *omap24_get_base(struct i2c_adapter *adap)
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100847{
Heiko Schocher6789e842013-10-22 11:03:18 +0200848 switch (adap->hwadapnr) {
Lubomir Popov960187f2013-06-01 06:44:38 +0000849 case 0:
Vignesh Rbca09ce2018-12-07 14:50:41 +0100850 return (void __iomem *)I2C_BASE1;
Lubomir Popov960187f2013-06-01 06:44:38 +0000851 break;
852 case 1:
Vignesh Rbca09ce2018-12-07 14:50:41 +0100853 return (void __iomem *)I2C_BASE2;
Lubomir Popov960187f2013-06-01 06:44:38 +0000854 break;
Adam Fordac1d8ac2017-08-11 06:39:13 -0500855#if (CONFIG_SYS_I2C_BUS_MAX > 2)
Lubomir Popov960187f2013-06-01 06:44:38 +0000856 case 2:
Vignesh Rbca09ce2018-12-07 14:50:41 +0100857 return (void __iomem *)I2C_BASE3;
Lubomir Popov960187f2013-06-01 06:44:38 +0000858 break;
Adam Fordac1d8ac2017-08-11 06:39:13 -0500859#if (CONFIG_SYS_I2C_BUS_MAX > 3)
Lubomir Popov960187f2013-06-01 06:44:38 +0000860 case 3:
Vignesh Rbca09ce2018-12-07 14:50:41 +0100861 return (void __iomem *)I2C_BASE4;
Lubomir Popov960187f2013-06-01 06:44:38 +0000862 break;
Adam Fordac1d8ac2017-08-11 06:39:13 -0500863#if (CONFIG_SYS_I2C_BUS_MAX > 4)
Lubomir Popov960187f2013-06-01 06:44:38 +0000864 case 4:
Vignesh Rbca09ce2018-12-07 14:50:41 +0100865 return (void __iomem *)I2C_BASE5;
Lubomir Popov960187f2013-06-01 06:44:38 +0000866 break;
867#endif
868#endif
869#endif
Heiko Schocher6789e842013-10-22 11:03:18 +0200870 default:
871 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
872 break;
Lubomir Popov960187f2013-06-01 06:44:38 +0000873 }
Vignesh Rbca09ce2018-12-07 14:50:41 +0100874
Heiko Schocher6789e842013-10-22 11:03:18 +0200875 return NULL;
Dirk Behme1d2e96d2009-11-02 20:36:26 +0100876}
Steve Sakoman938717c2010-06-12 06:42:57 -0700877
Vignesh Rbca09ce2018-12-07 14:50:41 +0100878static int omap24_get_ip_rev(void)
879{
880#ifdef CONFIG_OMAP34XX
881 return OMAP_I2C_REV_V1;
882#else
883 return OMAP_I2C_REV_V2;
884#endif
885}
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530886
887static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
888 int alen, uchar *buffer, int len)
889{
Vignesh Rbca09ce2018-12-07 14:50:41 +0100890 void __iomem *i2c_base = omap24_get_base(adap);
891 int ip_rev = omap24_get_ip_rev();
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530892
Vignesh Rbca09ce2018-12-07 14:50:41 +0100893 return __omap24_i2c_read(i2c_base, ip_rev, adap->waitdelay, chip, addr,
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530894 alen, buffer, len);
895}
896
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530897static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
898 int alen, uchar *buffer, int len)
899{
Vignesh Rbca09ce2018-12-07 14:50:41 +0100900 void __iomem *i2c_base = omap24_get_base(adap);
901 int ip_rev = omap24_get_ip_rev();
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530902
Vignesh Rbca09ce2018-12-07 14:50:41 +0100903 return __omap24_i2c_write(i2c_base, ip_rev, adap->waitdelay, chip, addr,
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530904 alen, buffer, len);
905}
906
907static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
908{
Vignesh Rbca09ce2018-12-07 14:50:41 +0100909 void __iomem *i2c_base = omap24_get_base(adap);
910 int ip_rev = omap24_get_ip_rev();
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530911 int ret;
912
Vignesh Rbca09ce2018-12-07 14:50:41 +0100913 ret = __omap24_i2c_setspeed(i2c_base, ip_rev, speed, &adap->waitdelay);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530914 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900915 pr_err("%s: set i2c speed failed\n", __func__);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530916 return ret;
917 }
918
919 adap->speed = speed;
920
921 return 0;
922}
923
924static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
925{
Vignesh Rbca09ce2018-12-07 14:50:41 +0100926 void __iomem *i2c_base = omap24_get_base(adap);
927 int ip_rev = omap24_get_ip_rev();
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530928
Vignesh Rbca09ce2018-12-07 14:50:41 +0100929 return __omap24_i2c_init(i2c_base, ip_rev, speed, slaveadd,
930 &adap->waitdelay);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530931}
932
933static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
934{
Vignesh Rbca09ce2018-12-07 14:50:41 +0100935 void __iomem *i2c_base = omap24_get_base(adap);
936 int ip_rev = omap24_get_ip_rev();
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530937
Vignesh Rbca09ce2018-12-07 14:50:41 +0100938 return __omap24_i2c_probe(i2c_base, ip_rev, adap->waitdelay, chip);
Mugunthan V Nbe243e42016-07-18 15:11:00 +0530939}
940
Heiko Schocher6789e842013-10-22 11:03:18 +0200941#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
942#define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
943#endif
944#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
945#define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
946#endif
947
948U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5243352014-02-03 21:22:18 +0100949 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Heiko Schocher6789e842013-10-22 11:03:18 +0200950 CONFIG_SYS_OMAP24_I2C_SPEED,
951 CONFIG_SYS_OMAP24_I2C_SLAVE,
952 0)
953U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5243352014-02-03 21:22:18 +0100954 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Heiko Schocher6789e842013-10-22 11:03:18 +0200955 CONFIG_SYS_OMAP24_I2C_SPEED1,
956 CONFIG_SYS_OMAP24_I2C_SLAVE1,
957 1)
Vignesh Rbca09ce2018-12-07 14:50:41 +0100958
Adam Fordac1d8ac2017-08-11 06:39:13 -0500959#if (CONFIG_SYS_I2C_BUS_MAX > 2)
Heiko Schocher6789e842013-10-22 11:03:18 +0200960#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
961#define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
962#endif
963#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
964#define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
965#endif
966
967U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
968 omap24_i2c_read, omap24_i2c_write, NULL,
969 CONFIG_SYS_OMAP24_I2C_SPEED2,
970 CONFIG_SYS_OMAP24_I2C_SLAVE2,
971 2)
Adam Fordac1d8ac2017-08-11 06:39:13 -0500972#if (CONFIG_SYS_I2C_BUS_MAX > 3)
Heiko Schocher6789e842013-10-22 11:03:18 +0200973#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
974#define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
975#endif
976#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
977#define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
978#endif
979
980U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
981 omap24_i2c_read, omap24_i2c_write, NULL,
982 CONFIG_SYS_OMAP24_I2C_SPEED3,
983 CONFIG_SYS_OMAP24_I2C_SLAVE3,
984 3)
Adam Fordac1d8ac2017-08-11 06:39:13 -0500985#if (CONFIG_SYS_I2C_BUS_MAX > 4)
Heiko Schocher6789e842013-10-22 11:03:18 +0200986#if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
987#define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
988#endif
989#if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
990#define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
991#endif
992
993U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
994 omap24_i2c_read, omap24_i2c_write, NULL,
995 CONFIG_SYS_OMAP24_I2C_SPEED4,
996 CONFIG_SYS_OMAP24_I2C_SLAVE4,
997 4)
998#endif
999#endif
1000#endif
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301001
1002#else /* CONFIG_DM_I2C */
1003
1004static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
1005{
1006 struct omap_i2c *priv = dev_get_priv(bus);
1007 int ret;
1008
1009 debug("i2c_xfer: %d messages\n", nmsgs);
1010 for (; nmsgs > 0; nmsgs--, msg++) {
1011 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
1012 if (msg->flags & I2C_M_RD) {
Vignesh Rbca09ce2018-12-07 14:50:41 +01001013 ret = __omap24_i2c_read(priv->regs, priv->ip_rev,
1014 priv->waitdelay,
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301015 msg->addr, 0, 0, msg->buf,
1016 msg->len);
1017 } else {
Vignesh Rbca09ce2018-12-07 14:50:41 +01001018 ret = __omap24_i2c_write(priv->regs, priv->ip_rev,
1019 priv->waitdelay,
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301020 msg->addr, 0, 0, msg->buf,
1021 msg->len);
1022 }
1023 if (ret) {
1024 debug("i2c_write: error sending\n");
1025 return -EREMOTEIO;
1026 }
1027 }
1028
1029 return 0;
1030}
1031
1032static int omap_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
1033{
1034 struct omap_i2c *priv = dev_get_priv(bus);
1035
1036 priv->speed = speed;
1037
Vignesh Rbca09ce2018-12-07 14:50:41 +01001038 return __omap24_i2c_setspeed(priv->regs, priv->ip_rev, speed,
1039 &priv->waitdelay);
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301040}
1041
1042static int omap_i2c_probe_chip(struct udevice *bus, uint chip_addr,
1043 uint chip_flags)
1044{
1045 struct omap_i2c *priv = dev_get_priv(bus);
1046
Vignesh Rbca09ce2018-12-07 14:50:41 +01001047 return __omap24_i2c_probe(priv->regs, priv->ip_rev, priv->waitdelay,
1048 chip_addr);
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301049}
1050
1051static int omap_i2c_probe(struct udevice *bus)
1052{
1053 struct omap_i2c *priv = dev_get_priv(bus);
1054
Vignesh Rbca09ce2018-12-07 14:50:41 +01001055 priv->ip_rev = dev_get_driver_data(bus);
1056
1057 __omap24_i2c_init(priv->regs, priv->ip_rev, priv->speed, 0,
1058 &priv->waitdelay);
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301059
1060 return 0;
1061}
1062
Adam Ford410c5052018-08-20 20:24:35 -05001063#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301064static int omap_i2c_ofdata_to_platdata(struct udevice *bus)
1065{
1066 struct omap_i2c *priv = dev_get_priv(bus);
1067
Simon Glassa821c4a2017-05-17 17:18:05 -06001068 priv->regs = map_physmem(devfdt_get_addr(bus), sizeof(void *),
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301069 MAP_NOCACHE);
1070 priv->speed = CONFIG_SYS_OMAP24_I2C_SPEED;
1071
1072 return 0;
1073}
1074
Adam Ford410c5052018-08-20 20:24:35 -05001075static const struct udevice_id omap_i2c_ids[] = {
Vignesh Rbca09ce2018-12-07 14:50:41 +01001076 { .compatible = "ti,omap3-i2c", .data = OMAP_I2C_REV_V1 },
1077 { .compatible = "ti,omap4-i2c", .data = OMAP_I2C_REV_V2 },
Adam Ford410c5052018-08-20 20:24:35 -05001078 { }
1079};
1080#endif
1081
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301082static const struct dm_i2c_ops omap_i2c_ops = {
1083 .xfer = omap_i2c_xfer,
1084 .probe_chip = omap_i2c_probe_chip,
1085 .set_bus_speed = omap_i2c_set_bus_speed,
1086};
1087
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301088U_BOOT_DRIVER(i2c_omap) = {
1089 .name = "i2c_omap",
1090 .id = UCLASS_I2C,
Adam Ford410c5052018-08-20 20:24:35 -05001091#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301092 .of_match = omap_i2c_ids,
1093 .ofdata_to_platdata = omap_i2c_ofdata_to_platdata,
Adam Ford410c5052018-08-20 20:24:35 -05001094#endif
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301095 .probe = omap_i2c_probe,
1096 .priv_auto_alloc_size = sizeof(struct omap_i2c),
1097 .ops = &omap_i2c_ops,
Bin Menge0cfc202018-10-24 06:36:31 -07001098#if !CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301099 .flags = DM_FLAG_PRE_RELOC,
Bin Menge0cfc202018-10-24 06:36:31 -07001100#endif
Mugunthan V Ndaa69ff2016-07-18 15:11:01 +05301101};
1102
1103#endif /* CONFIG_DM_I2C */