blob: 6fc9d1eba9d1be50621a963f285bbf9035ca2f3f [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>
Mugunthan V N560037b2016-07-18 15:11:01 +053042#include <dm.h>
Heiko Schocherf53f2b82013-10-22 11:03:18 +020043#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060044#include <log.h>
Simon Glassdbd79542020-05-10 11:40:11 -060045#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060046#include <linux/printk.h>
wdenkcb99da52005-01-12 00:15:14 +000047
wdenkf8062712005-01-09 23:16:25 +000048#include <asm/io.h>
Jean-Jacques Hiblot58994fc2018-12-07 14:50:42 +010049#include <asm/omap_i2c.h>
wdenkf8062712005-01-09 23:16:25 +000050
Vignesh R3f51de32018-12-07 14:50:41 +010051/*
52 * Provide access to architecture-specific I2C header files for platforms
53 * that are NOT yet solely relying on CONFIG_DM_I2C, CONFIG_OF_CONTROL, and
54 * the defaults provided in 'omap24xx_i2c.h' for all U-Boot stages where I2C
55 * access is desired.
56 */
57#ifndef CONFIG_ARCH_K3
58#include <asm/arch/i2c.h>
59#endif
60
Steve Sakoman10acc712010-06-12 06:42:57 -070061#include "omap24xx_i2c.h"
62
Tom Rini49fbf672012-02-20 18:49:16 +000063#define I2C_TIMEOUT 1000
Steve Sakomane2bdc132010-07-19 20:31:55 -070064
Lubomir Popov4d98efd2013-06-01 06:44:38 +000065/* Absolutely safe for status update at 100 kHz I2C: */
66#define I2C_WAIT 200
67
Vignesh R3f51de32018-12-07 14:50:41 +010068enum {
Vignesh R3f51de32018-12-07 14:50:41 +010069 OMAP_I2C_REV_REG = 0, /* Only on IP V1 (OMAP34XX) */
70 OMAP_I2C_IE_REG, /* Only on IP V1 (OMAP34XX) */
71 OMAP_I2C_STAT_REG,
72 OMAP_I2C_WE_REG,
73 OMAP_I2C_SYSS_REG,
74 OMAP_I2C_BUF_REG,
75 OMAP_I2C_CNT_REG,
76 OMAP_I2C_DATA_REG,
77 OMAP_I2C_SYSC_REG,
78 OMAP_I2C_CON_REG,
79 OMAP_I2C_OA_REG,
80 OMAP_I2C_SA_REG,
81 OMAP_I2C_PSC_REG,
82 OMAP_I2C_SCLL_REG,
83 OMAP_I2C_SCLH_REG,
84 OMAP_I2C_SYSTEST_REG,
85 OMAP_I2C_BUFSTAT_REG,
86 /* Only on IP V2 (OMAP4430, etc.) */
87 OMAP_I2C_IP_V2_REVNB_LO,
88 OMAP_I2C_IP_V2_REVNB_HI,
89 OMAP_I2C_IP_V2_IRQSTATUS_RAW,
90 OMAP_I2C_IP_V2_IRQENABLE_SET,
91 OMAP_I2C_IP_V2_IRQENABLE_CLR,
92};
93
94static const u8 __maybe_unused reg_map_ip_v1[] = {
95 [OMAP_I2C_REV_REG] = 0x00,
96 [OMAP_I2C_IE_REG] = 0x04,
97 [OMAP_I2C_STAT_REG] = 0x08,
98 [OMAP_I2C_WE_REG] = 0x0c,
99 [OMAP_I2C_SYSS_REG] = 0x10,
100 [OMAP_I2C_BUF_REG] = 0x14,
101 [OMAP_I2C_CNT_REG] = 0x18,
102 [OMAP_I2C_DATA_REG] = 0x1c,
103 [OMAP_I2C_SYSC_REG] = 0x20,
104 [OMAP_I2C_CON_REG] = 0x24,
105 [OMAP_I2C_OA_REG] = 0x28,
106 [OMAP_I2C_SA_REG] = 0x2c,
107 [OMAP_I2C_PSC_REG] = 0x30,
108 [OMAP_I2C_SCLL_REG] = 0x34,
109 [OMAP_I2C_SCLH_REG] = 0x38,
110 [OMAP_I2C_SYSTEST_REG] = 0x3c,
111 [OMAP_I2C_BUFSTAT_REG] = 0x40,
112};
113
114static const u8 __maybe_unused reg_map_ip_v2[] = {
115 [OMAP_I2C_STAT_REG] = 0x28,
116 [OMAP_I2C_WE_REG] = 0x34,
117 [OMAP_I2C_SYSS_REG] = 0x90,
118 [OMAP_I2C_BUF_REG] = 0x94,
119 [OMAP_I2C_CNT_REG] = 0x98,
120 [OMAP_I2C_DATA_REG] = 0x9c,
121 [OMAP_I2C_SYSC_REG] = 0x10,
122 [OMAP_I2C_CON_REG] = 0xa4,
123 [OMAP_I2C_OA_REG] = 0xa8,
124 [OMAP_I2C_SA_REG] = 0xac,
125 [OMAP_I2C_PSC_REG] = 0xb0,
126 [OMAP_I2C_SCLL_REG] = 0xb4,
127 [OMAP_I2C_SCLH_REG] = 0xb8,
128 [OMAP_I2C_SYSTEST_REG] = 0xbc,
129 [OMAP_I2C_BUFSTAT_REG] = 0xc0,
130 [OMAP_I2C_IP_V2_REVNB_LO] = 0x00,
131 [OMAP_I2C_IP_V2_REVNB_HI] = 0x04,
132 [OMAP_I2C_IP_V2_IRQSTATUS_RAW] = 0x24,
133 [OMAP_I2C_IP_V2_IRQENABLE_SET] = 0x2c,
134 [OMAP_I2C_IP_V2_IRQENABLE_CLR] = 0x30,
135};
136
Mugunthan V N560037b2016-07-18 15:11:01 +0530137struct omap_i2c {
138 struct udevice *clk;
Vignesh R3f51de32018-12-07 14:50:41 +0100139 int ip_rev;
Mugunthan V N560037b2016-07-18 15:11:01 +0530140 struct i2c *regs;
141 unsigned int speed;
142 int waitdelay;
143 int clk_id;
144};
145
Vignesh R3f51de32018-12-07 14:50:41 +0100146static inline const u8 *omap_i2c_get_ip_reg_map(int ip_rev)
147{
148 switch (ip_rev) {
149 case OMAP_I2C_REV_V1:
150 return reg_map_ip_v1;
151 case OMAP_I2C_REV_V2:
152 /* Fall through... */
153 default:
154 return reg_map_ip_v2;
155 }
156}
157
158static inline void omap_i2c_write_reg(void __iomem *base, int ip_rev,
159 u16 val, int reg)
160{
161 writew(val, base + omap_i2c_get_ip_reg_map(ip_rev)[reg]);
162}
163
164static inline u16 omap_i2c_read_reg(void __iomem *base, int ip_rev, int reg)
165{
166 return readw(base + omap_i2c_get_ip_reg_map(ip_rev)[reg]);
167}
168
Hannes Petermaierd5885052014-02-03 21:22:18 +0100169static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
wdenkf8062712005-01-09 23:16:25 +0000170{
Lukasz Majewski698a9ba2017-03-15 16:59:23 +0100171 unsigned long internal_clk = 0, fclk;
172 unsigned int prescaler;
Tom Rix03b2a742009-06-28 12:52:27 -0500173
Hannes Petermaierd5885052014-02-03 21:22:18 +0100174 /*
Lukasz Majewski698a9ba2017-03-15 16:59:23 +0100175 * This method is only called for Standard and Fast Mode speeds
176 *
177 * For some TI SoCs it is explicitly written in TRM (e,g, SPRUHZ6G,
178 * page 5685, Table 24-7)
179 * that the internal I2C clock (after prescaler) should be between
180 * 7-12 MHz (at least for Fast Mode (FS)).
181 *
182 * Such approach is used in v4.9 Linux kernel in:
183 * ./drivers/i2c/busses/i2c-omap.c (omap_i2c_init function).
Hannes Petermaierd5885052014-02-03 21:22:18 +0100184 */
Tom Rix03b2a742009-06-28 12:52:27 -0500185
Lukasz Majewski698a9ba2017-03-15 16:59:23 +0100186 speed /= 1000; /* convert speed to kHz */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100187
Lukasz Majewski698a9ba2017-03-15 16:59:23 +0100188 if (speed > 100)
189 internal_clk = 9600;
190 else
191 internal_clk = 4000;
Hannes Petermaierd5885052014-02-03 21:22:18 +0100192
Lukasz Majewski698a9ba2017-03-15 16:59:23 +0100193 fclk = I2C_IP_CLK / 1000;
194 prescaler = fclk / internal_clk;
195 prescaler = prescaler - 1;
196
197 if (speed > 100) {
198 unsigned long scl;
199
200 /* Fast mode */
201 scl = internal_clk / speed;
202 *pscl = scl - (scl / 3) - I2C_FASTSPEED_SCLL_TRIM;
203 *psch = (scl / 3) - I2C_FASTSPEED_SCLH_TRIM;
204 } else {
205 /* Standard mode */
206 *pscl = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLL_TRIM;
207 *psch = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLH_TRIM;
Tom Rix03b2a742009-06-28 12:52:27 -0500208 }
Lukasz Majewski698a9ba2017-03-15 16:59:23 +0100209
210 debug("%s: speed [kHz]: %d psc: 0x%x sscl: 0x%x ssch: 0x%x\n",
211 __func__, speed, prescaler, *pscl, *psch);
212
213 if (*pscl <= 0 || *psch <= 0 || prescaler <= 0)
214 return -EINVAL;
215
216 return prescaler;
Hannes Petermaierd5885052014-02-03 21:22:18 +0100217}
Mugunthan V N38d943a2016-07-18 15:11:00 +0530218
219/*
220 * Wait for the bus to be free by checking the Bus Busy (BB)
221 * bit to become clear
222 */
Vignesh R3f51de32018-12-07 14:50:41 +0100223static int wait_for_bb(void __iomem *i2c_base, int ip_rev, int waitdelay)
Hannes Petermaierd5885052014-02-03 21:22:18 +0100224{
Mugunthan V N38d943a2016-07-18 15:11:00 +0530225 int timeout = I2C_TIMEOUT;
Vignesh R3f51de32018-12-07 14:50:41 +0100226 int irq_stat_reg;
Mugunthan V N38d943a2016-07-18 15:11:00 +0530227 u16 stat;
228
Vignesh R3f51de32018-12-07 14:50:41 +0100229 irq_stat_reg = (ip_rev == OMAP_I2C_REV_V1) ?
230 OMAP_I2C_STAT_REG : OMAP_I2C_IP_V2_IRQSTATUS_RAW;
231
232 /* clear current interrupts */
233 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
234
235 while ((stat = omap_i2c_read_reg(i2c_base, ip_rev, irq_stat_reg) &
Mugunthan V N38d943a2016-07-18 15:11:00 +0530236 I2C_STAT_BB) && timeout--) {
Vignesh R3f51de32018-12-07 14:50:41 +0100237 omap_i2c_write_reg(i2c_base, ip_rev, stat, OMAP_I2C_STAT_REG);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530238 udelay(waitdelay);
239 }
240
241 if (timeout <= 0) {
Vignesh R3f51de32018-12-07 14:50:41 +0100242 printf("Timed out in %s: status=%04x\n", __func__, stat);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530243 return 1;
244 }
Vignesh R3f51de32018-12-07 14:50:41 +0100245
246 /* clear delayed stuff */
247 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530248 return 0;
249}
250
251/*
252 * Wait for the I2C controller to complete current action
253 * and update status
254 */
Vignesh R3f51de32018-12-07 14:50:41 +0100255static u16 wait_for_event(void __iomem *i2c_base, int ip_rev, int waitdelay)
Mugunthan V N38d943a2016-07-18 15:11:00 +0530256{
257 u16 status;
258 int timeout = I2C_TIMEOUT;
Vignesh R3f51de32018-12-07 14:50:41 +0100259 int irq_stat_reg;
Mugunthan V N38d943a2016-07-18 15:11:00 +0530260
Vignesh R3f51de32018-12-07 14:50:41 +0100261 irq_stat_reg = (ip_rev == OMAP_I2C_REV_V1) ?
262 OMAP_I2C_STAT_REG : OMAP_I2C_IP_V2_IRQSTATUS_RAW;
Mugunthan V N38d943a2016-07-18 15:11:00 +0530263 do {
264 udelay(waitdelay);
Vignesh R3f51de32018-12-07 14:50:41 +0100265 status = omap_i2c_read_reg(i2c_base, ip_rev, irq_stat_reg);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530266 } while (!(status &
267 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
268 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
269 I2C_STAT_AL)) && timeout--);
270
271 if (timeout <= 0) {
Vignesh R3f51de32018-12-07 14:50:41 +0100272 printf("Timed out in %s: status=%04x\n", __func__, status);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530273 /*
274 * If status is still 0 here, probably the bus pads have
275 * not been configured for I2C, and/or pull-ups are missing.
276 */
277 printf("Check if pads/pull-ups of bus are properly configured\n");
Vignesh R3f51de32018-12-07 14:50:41 +0100278 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530279 status = 0;
280 }
281
282 return status;
283}
284
Vignesh R3f51de32018-12-07 14:50:41 +0100285static void flush_fifo(void __iomem *i2c_base, int ip_rev)
Mugunthan V N38d943a2016-07-18 15:11:00 +0530286{
287 u16 stat;
288
289 /*
290 * note: if you try and read data when its not there or ready
291 * you get a bus error
292 */
293 while (1) {
Vignesh R3f51de32018-12-07 14:50:41 +0100294 stat = omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_STAT_REG);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530295 if (stat == I2C_STAT_RRDY) {
Vignesh R3f51de32018-12-07 14:50:41 +0100296 omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_DATA_REG);
297 omap_i2c_write_reg(i2c_base, ip_rev,
298 I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530299 udelay(1000);
300 } else
301 break;
302 }
303}
304
Vignesh R3f51de32018-12-07 14:50:41 +0100305static int __omap24_i2c_setspeed(void __iomem *i2c_base, int ip_rev, uint speed,
Mugunthan V N38d943a2016-07-18 15:11:00 +0530306 int *waitdelay)
307{
Hannes Petermaierd5885052014-02-03 21:22:18 +0100308 int psc, fsscll = 0, fssclh = 0;
309 int hsscll = 0, hssclh = 0;
310 u32 scll = 0, sclh = 0;
Tom Rix03b2a742009-06-28 12:52:27 -0500311
Simon Glassed0a60a2020-01-23 11:48:20 -0700312 if (speed >= I2C_SPEED_HIGH_RATE) {
Tom Rix03b2a742009-06-28 12:52:27 -0500313 /* High speed */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100314 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
315 psc -= 1;
316 if (psc < I2C_PSC_MIN) {
317 printf("Error : I2C unsupported prescaler %d\n", psc);
318 return -1;
319 }
Tom Rix03b2a742009-06-28 12:52:27 -0500320
321 /* For first phase of HS mode */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100322 fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
323
324 fssclh = fsscll;
Tom Rix03b2a742009-06-28 12:52:27 -0500325
326 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
327 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
328 if (((fsscll < 0) || (fssclh < 0)) ||
329 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000330 puts("Error : I2C initializing first phase clock\n");
Hannes Petermaierd5885052014-02-03 21:22:18 +0100331 return -1;
Tom Rix03b2a742009-06-28 12:52:27 -0500332 }
333
334 /* For second phase of HS mode */
335 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
336
337 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
338 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
339 if (((fsscll < 0) || (fssclh < 0)) ||
340 ((fsscll > 255) || (fssclh > 255))) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000341 puts("Error : I2C initializing second phase clock\n");
Hannes Petermaierd5885052014-02-03 21:22:18 +0100342 return -1;
Tom Rix03b2a742009-06-28 12:52:27 -0500343 }
344
345 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
346 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
347
348 } else {
349 /* Standard and fast speed */
Hannes Petermaierd5885052014-02-03 21:22:18 +0100350 psc = omap24_i2c_findpsc(&scll, &sclh, speed);
351 if (0 > psc) {
Andreas Müllera30293f2012-01-04 15:26:19 +0000352 puts("Error : I2C initializing clock\n");
Hannes Petermaierd5885052014-02-03 21:22:18 +0100353 return -1;
Tom Rix03b2a742009-06-28 12:52:27 -0500354 }
Tom Rix03b2a742009-06-28 12:52:27 -0500355 }
wdenkf8062712005-01-09 23:16:25 +0000356
Vignesh R3f51de32018-12-07 14:50:41 +0100357 /* wait for 20 clkperiods */
358 *waitdelay = (10000000 / speed) * 2;
359
360 omap_i2c_write_reg(i2c_base, ip_rev, 0, OMAP_I2C_CON_REG);
361 omap_i2c_write_reg(i2c_base, ip_rev, psc, OMAP_I2C_PSC_REG);
362 omap_i2c_write_reg(i2c_base, ip_rev, scll, OMAP_I2C_SCLL_REG);
363 omap_i2c_write_reg(i2c_base, ip_rev, sclh, OMAP_I2C_SCLH_REG);
364 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN, OMAP_I2C_CON_REG);
365
366 /* clear all pending status */
367 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Hannes Petermaierd5885052014-02-03 21:22:18 +0100368
369 return 0;
370}
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200371
Vignesh R3f51de32018-12-07 14:50:41 +0100372static void omap24_i2c_deblock(void __iomem *i2c_base, int ip_rev)
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200373{
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200374 int i;
375 u16 systest;
376 u16 orgsystest;
377
378 /* set test mode ST_EN = 1 */
Vignesh R3f51de32018-12-07 14:50:41 +0100379 orgsystest = omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_SYSTEST_REG);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200380 systest = orgsystest;
Vignesh R3f51de32018-12-07 14:50:41 +0100381
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200382 /* enable testmode */
383 systest |= I2C_SYSTEST_ST_EN;
Vignesh R3f51de32018-12-07 14:50:41 +0100384 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200385 systest &= ~I2C_SYSTEST_TMODE_MASK;
386 systest |= 3 << I2C_SYSTEST_TMODE_SHIFT;
Vignesh R3f51de32018-12-07 14:50:41 +0100387 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200388
389 /* set SCL, SDA = 1 */
390 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
Vignesh R3f51de32018-12-07 14:50:41 +0100391 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200392 udelay(10);
393
394 /* toggle scl 9 clocks */
395 for (i = 0; i < 9; i++) {
396 /* SCL = 0 */
397 systest &= ~I2C_SYSTEST_SCL_O;
Vignesh R3f51de32018-12-07 14:50:41 +0100398 omap_i2c_write_reg(i2c_base, ip_rev,
399 systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200400 udelay(10);
401 /* SCL = 1 */
402 systest |= I2C_SYSTEST_SCL_O;
Vignesh R3f51de32018-12-07 14:50:41 +0100403 omap_i2c_write_reg(i2c_base, ip_rev,
404 systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200405 udelay(10);
406 }
407
408 /* send stop */
409 systest &= ~I2C_SYSTEST_SDA_O;
Vignesh R3f51de32018-12-07 14:50:41 +0100410 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200411 udelay(10);
412 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
Vignesh R3f51de32018-12-07 14:50:41 +0100413 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200414 udelay(10);
415
416 /* restore original mode */
Vignesh R3f51de32018-12-07 14:50:41 +0100417 omap_i2c_write_reg(i2c_base, ip_rev, orgsystest, OMAP_I2C_SYSTEST_REG);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200418}
419
Vignesh R3f51de32018-12-07 14:50:41 +0100420static void __omap24_i2c_init(void __iomem *i2c_base, int ip_rev, int speed,
421 int slaveadd, int *waitdelay)
Hannes Petermaierd5885052014-02-03 21:22:18 +0100422{
Hannes Petermaierd5885052014-02-03 21:22:18 +0100423 int timeout = I2C_TIMEOUT;
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200424 int deblock = 1;
Hannes Petermaierd5885052014-02-03 21:22:18 +0100425
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200426retry:
Vignesh R3f51de32018-12-07 14:50:41 +0100427 if (omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_CON_REG) &
428 I2C_CON_EN) {
429 omap_i2c_write_reg(i2c_base, ip_rev, 0, OMAP_I2C_CON_REG);
Michael Jones4db67862011-07-27 14:01:55 -0400430 udelay(50000);
wdenkf8062712005-01-09 23:16:25 +0000431 }
432
Vignesh R3f51de32018-12-07 14:50:41 +0100433 /* for ES2 after soft reset */
434 omap_i2c_write_reg(i2c_base, ip_rev, 0x2, OMAP_I2C_SYSC_REG);
Tom Rini49fbf672012-02-20 18:49:16 +0000435 udelay(1000);
436
Vignesh R3f51de32018-12-07 14:50:41 +0100437 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN, OMAP_I2C_CON_REG);
438 while (!(omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_SYSS_REG) &
439 I2C_SYSS_RDONE) && timeout--) {
Tom Rini49fbf672012-02-20 18:49:16 +0000440 if (timeout <= 0) {
441 puts("ERROR: Timeout in soft-reset\n");
442 return;
443 }
444 udelay(1000);
445 }
446
Vignesh R3f51de32018-12-07 14:50:41 +0100447 if (__omap24_i2c_setspeed(i2c_base, ip_rev, speed, waitdelay)) {
Hannes Petermaierd5885052014-02-03 21:22:18 +0100448 printf("ERROR: failed to setup I2C bus-speed!\n");
449 return;
450 }
Tom Rix03b2a742009-06-28 12:52:27 -0500451
wdenkf8062712005-01-09 23:16:25 +0000452 /* own address */
Vignesh R3f51de32018-12-07 14:50:41 +0100453 omap_i2c_write_reg(i2c_base, ip_rev, slaveadd, OMAP_I2C_OA_REG);
Hannes Petermaierd5885052014-02-03 21:22:18 +0100454
Vignesh R3f51de32018-12-07 14:50:41 +0100455 if (ip_rev == OMAP_I2C_REV_V1) {
456 /*
457 * Have to enable interrupts for OMAP2/3, these IPs don't have
458 * an 'irqstatus_raw' register and we shall have to poll 'stat'
459 */
460 omap_i2c_write_reg(i2c_base, ip_rev, I2C_IE_XRDY_IE |
461 I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
462 I2C_IE_NACK_IE | I2C_IE_AL_IE,
463 OMAP_I2C_IE_REG);
464 }
465
Michael Jones4db67862011-07-27 14:01:55 -0400466 udelay(1000);
Vignesh R3f51de32018-12-07 14:50:41 +0100467 flush_fifo(i2c_base, ip_rev);
468 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200469
470 /* Handle possible failed I2C state */
Vignesh R3f51de32018-12-07 14:50:41 +0100471 if (wait_for_bb(i2c_base, ip_rev, *waitdelay))
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200472 if (deblock == 1) {
Vignesh R3f51de32018-12-07 14:50:41 +0100473 omap24_i2c_deblock(i2c_base, ip_rev);
Heiko Schocher0ecd6552014-06-30 09:12:09 +0200474 deblock = 0;
475 goto retry;
476 }
wdenkf8062712005-01-09 23:16:25 +0000477}
478
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000479/*
480 * i2c_probe: Use write access. Allows to identify addresses that are
481 * write-only (like the config register of dual-port EEPROMs)
482 */
Vignesh R3f51de32018-12-07 14:50:41 +0100483static int __omap24_i2c_probe(void __iomem *i2c_base, int ip_rev, int waitdelay,
484 uchar chip)
wdenkf8062712005-01-09 23:16:25 +0000485{
Tom Rini49fbf672012-02-20 18:49:16 +0000486 u16 status;
wdenkf8062712005-01-09 23:16:25 +0000487 int res = 1; /* default = fail */
488
Vignesh R3f51de32018-12-07 14:50:41 +0100489 if (chip == omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_OA_REG))
wdenkf8062712005-01-09 23:16:25 +0000490 return res;
wdenkf8062712005-01-09 23:16:25 +0000491
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000492 /* Wait until bus is free */
Vignesh R3f51de32018-12-07 14:50:41 +0100493 if (wait_for_bb(i2c_base, ip_rev, waitdelay))
Vincent Stehlé33205e32012-12-03 05:23:16 +0000494 return res;
wdenkf8062712005-01-09 23:16:25 +0000495
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000496 /* No data transfer, slave addr only */
Vignesh R3f51de32018-12-07 14:50:41 +0100497 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
498
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000499 /* Stop bit needed here */
Vignesh R3f51de32018-12-07 14:50:41 +0100500 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
501 I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP,
502 OMAP_I2C_CON_REG);
Nick Thompson48f7ae42011-04-11 22:37:41 +0000503
Vignesh R3f51de32018-12-07 14:50:41 +0100504 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000505
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000506 if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
507 /*
508 * With current high-level command implementation, notifying
509 * the user shall flood the console with 127 messages. If
510 * silent exit is desired upon unconfigured bus, remove the
511 * following 'if' section:
512 */
513 if (status == I2C_STAT_XRDY)
Mugunthan V N38d943a2016-07-18 15:11:00 +0530514 printf("i2c_probe: pads on bus probably not configured (status=0x%x)\n",
515 status);
Vincent Stehlé33205e32012-12-03 05:23:16 +0000516
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000517 goto pr_exit;
Tom Rini27eed8b2012-05-21 06:46:29 +0000518 }
Tom Rini49fbf672012-02-20 18:49:16 +0000519
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000520 /* Check for ACK (!NAK) */
521 if (!(status & I2C_STAT_NACK)) {
Hannes Petermaierd5885052014-02-03 21:22:18 +0100522 res = 0; /* Device found */
Mugunthan V N38d943a2016-07-18 15:11:00 +0530523 udelay(waitdelay);/* Required by AM335X in SPL */
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000524 /* Abort transfer (force idle state) */
Vignesh R3f51de32018-12-07 14:50:41 +0100525 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_MST | I2C_CON_TRX,
526 OMAP_I2C_CON_REG); /* Reset */
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000527 udelay(1000);
Vignesh R3f51de32018-12-07 14:50:41 +0100528 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
529 I2C_CON_TRX | I2C_CON_STP,
530 OMAP_I2C_CON_REG); /* STP */
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000531 }
Vignesh R3f51de32018-12-07 14:50:41 +0100532
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000533pr_exit:
Vignesh R3f51de32018-12-07 14:50:41 +0100534 flush_fifo(i2c_base, ip_rev);
535 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
wdenkf8062712005-01-09 23:16:25 +0000536 return res;
537}
538
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000539/*
540 * i2c_read: Function now uses a single I2C read transaction with bulk transfer
541 * of the requested number of bytes (note that the 'i2c md' command
542 * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
543 * defined in the board config header, this transaction shall be with
544 * Repeated Start (Sr) between the address and data phases; otherwise
545 * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
546 * The address (reg offset) may be 0, 1 or 2 bytes long.
547 * Function now reads correctly from chips that return more than one
548 * byte of data per addressed register (like TI temperature sensors),
549 * or that do not need a register address at all (such as some clock
550 * distributors).
551 */
Vignesh R3f51de32018-12-07 14:50:41 +0100552static int __omap24_i2c_read(void __iomem *i2c_base, int ip_rev, int waitdelay,
553 uchar chip, uint addr, int alen, uchar *buffer,
554 int len)
wdenkf8062712005-01-09 23:16:25 +0000555{
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000556 int i2c_error = 0;
557 u16 status;
558
559 if (alen < 0) {
560 puts("I2C read: addr len < 0\n");
561 return 1;
562 }
Vignesh R3f51de32018-12-07 14:50:41 +0100563
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000564 if (len < 0) {
565 puts("I2C read: data len < 0\n");
566 return 1;
567 }
Vignesh R3f51de32018-12-07 14:50:41 +0100568
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000569 if (buffer == NULL) {
570 puts("I2C read: NULL pointer passed\n");
571 return 1;
572 }
wdenkf8062712005-01-09 23:16:25 +0000573
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000574 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000575 printf("I2C read: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000576 return 1;
577 }
578
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000579 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000580 puts("I2C read: address out of range\n");
wdenkf8062712005-01-09 23:16:25 +0000581 return 1;
582 }
583
Guy Thouret51c27272016-03-11 16:23:41 +0000584#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
585 /*
586 * EEPROM chips that implement "address overflow" are ones
587 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
588 * address and the extra bits end up in the "chip address"
589 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
590 * four 256 byte chips.
591 *
592 * Note that we consider the length of the address field to
593 * still be one byte because the extra address bits are
594 * hidden in the chip address.
595 */
596 if (alen > 0)
597 chip |= ((addr >> (alen * 8)) &
598 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
599#endif
600
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000601 /* Wait until bus not busy */
Vignesh R3f51de32018-12-07 14:50:41 +0100602 if (wait_for_bb(i2c_base, ip_rev, waitdelay))
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000603 return 1;
604
605 /* Zero, one or two bytes reg address (offset) */
Vignesh R3f51de32018-12-07 14:50:41 +0100606 omap_i2c_write_reg(i2c_base, ip_rev, alen, OMAP_I2C_CNT_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000607 /* Set slave address */
Vignesh R3f51de32018-12-07 14:50:41 +0100608 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000609
610 if (alen) {
611 /* Must write reg offset first */
612#ifdef CONFIG_I2C_REPEATED_START
613 /* No stop bit, use Repeated Start (Sr) */
Vignesh R3f51de32018-12-07 14:50:41 +0100614 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
615 I2C_CON_STT | I2C_CON_TRX, OMAP_I2C_CON_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000616#else
617 /* Stop - Start (P-S) */
Vignesh R3f51de32018-12-07 14:50:41 +0100618 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
619 I2C_CON_STT | I2C_CON_STP | I2C_CON_TRX,
620 OMAP_I2C_CON_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000621#endif
622 /* Send register offset */
623 while (1) {
Vignesh R3f51de32018-12-07 14:50:41 +0100624 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000625 /* Try to identify bus that is not padconf'd for I2C */
626 if (status == I2C_STAT_XRDY) {
627 i2c_error = 2;
Mugunthan V N38d943a2016-07-18 15:11:00 +0530628 printf("i2c_read (addr phase): pads on bus probably not configured (status=0x%x)\n",
629 status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000630 goto rd_exit;
631 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100632 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000633 i2c_error = 1;
634 printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
635 status);
636 goto rd_exit;
637 }
638 if (alen) {
639 if (status & I2C_STAT_XRDY) {
Vignesh R3f51de32018-12-07 14:50:41 +0100640 u8 addr_byte;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000641 alen--;
Vignesh R3f51de32018-12-07 14:50:41 +0100642 addr_byte = (addr >> (8 * alen)) & 0xff;
643 omap_i2c_write_reg(i2c_base, ip_rev,
644 addr_byte,
645 OMAP_I2C_DATA_REG);
646 omap_i2c_write_reg(i2c_base, ip_rev,
647 I2C_STAT_XRDY,
648 OMAP_I2C_STAT_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000649 }
650 }
651 if (status & I2C_STAT_ARDY) {
Vignesh R3f51de32018-12-07 14:50:41 +0100652 omap_i2c_write_reg(i2c_base, ip_rev,
653 I2C_STAT_ARDY,
654 OMAP_I2C_STAT_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000655 break;
656 }
wdenkf8062712005-01-09 23:16:25 +0000657 }
658 }
Vignesh R3f51de32018-12-07 14:50:41 +0100659
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000660 /* Set slave address */
Vignesh R3f51de32018-12-07 14:50:41 +0100661 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000662 /* Read len bytes from slave */
Vignesh R3f51de32018-12-07 14:50:41 +0100663 omap_i2c_write_reg(i2c_base, ip_rev, len, OMAP_I2C_CNT_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000664 /* Need stop bit here */
Vignesh R3f51de32018-12-07 14:50:41 +0100665 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
666 I2C_CON_STT | I2C_CON_STP, OMAP_I2C_CON_REG);
wdenkf8062712005-01-09 23:16:25 +0000667
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000668 /* Receive data */
669 while (1) {
Vignesh R3f51de32018-12-07 14:50:41 +0100670 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000671 /*
672 * Try to identify bus that is not padconf'd for I2C. This
673 * state could be left over from previous transactions if
674 * the address phase is skipped due to alen=0.
675 */
676 if (status == I2C_STAT_XRDY) {
677 i2c_error = 2;
Mugunthan V N38d943a2016-07-18 15:11:00 +0530678 printf("i2c_read (data phase): pads on bus probably not configured (status=0x%x)\n",
679 status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000680 goto rd_exit;
681 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100682 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000683 i2c_error = 1;
684 goto rd_exit;
685 }
686 if (status & I2C_STAT_RRDY) {
Vignesh R3f51de32018-12-07 14:50:41 +0100687 *buffer++ = omap_i2c_read_reg(i2c_base, ip_rev,
688 OMAP_I2C_DATA_REG);
689 omap_i2c_write_reg(i2c_base, ip_rev,
690 I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000691 }
692 if (status & I2C_STAT_ARDY) {
Vignesh R3f51de32018-12-07 14:50:41 +0100693 omap_i2c_write_reg(i2c_base, ip_rev,
694 I2C_STAT_ARDY, OMAP_I2C_STAT_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000695 break;
696 }
697 }
698
699rd_exit:
Vignesh R3f51de32018-12-07 14:50:41 +0100700 flush_fifo(i2c_base, ip_rev);
701 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000702 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000703}
704
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000705/* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
Vignesh R3f51de32018-12-07 14:50:41 +0100706static int __omap24_i2c_write(void __iomem *i2c_base, int ip_rev, int waitdelay,
707 uchar chip, uint addr, int alen, uchar *buffer,
708 int len)
wdenkf8062712005-01-09 23:16:25 +0000709{
Tom Rini49fbf672012-02-20 18:49:16 +0000710 int i;
711 u16 status;
712 int i2c_error = 0;
Hannes Petermaierd5885052014-02-03 21:22:18 +0100713 int timeout = I2C_TIMEOUT;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000714
715 if (alen < 0) {
716 puts("I2C write: addr len < 0\n");
717 return 1;
718 }
719
720 if (len < 0) {
721 puts("I2C write: data len < 0\n");
722 return 1;
723 }
724
725 if (buffer == NULL) {
726 puts("I2C write: NULL pointer passed\n");
727 return 1;
728 }
wdenkf8062712005-01-09 23:16:25 +0000729
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000730 if (alen > 2) {
Tom Rini49fbf672012-02-20 18:49:16 +0000731 printf("I2C write: addr len %d not supported\n", alen);
wdenkf8062712005-01-09 23:16:25 +0000732 return 1;
Tom Rini49fbf672012-02-20 18:49:16 +0000733 }
wdenkf8062712005-01-09 23:16:25 +0000734
Ilya Yanokbe6c2e42012-06-08 03:12:09 +0000735 if (addr + len > (1 << 16)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000736 printf("I2C write: address 0x%x + 0x%x out of range\n",
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000737 addr, len);
wdenkf8062712005-01-09 23:16:25 +0000738 return 1;
739 }
740
Guy Thouret51c27272016-03-11 16:23:41 +0000741#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
742 /*
743 * EEPROM chips that implement "address overflow" are ones
744 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
745 * address and the extra bits end up in the "chip address"
746 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
747 * four 256 byte chips.
748 *
749 * Note that we consider the length of the address field to
750 * still be one byte because the extra address bits are
751 * hidden in the chip address.
752 */
753 if (alen > 0)
754 chip |= ((addr >> (alen * 8)) &
755 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
756#endif
757
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000758 /* Wait until bus not busy */
Vignesh R3f51de32018-12-07 14:50:41 +0100759 if (wait_for_bb(i2c_base, ip_rev, waitdelay))
Vincent Stehlé33205e32012-12-03 05:23:16 +0000760 return 1;
Michael Jonesbb54d572011-09-04 14:01:55 -0400761
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000762 /* Start address phase - will write regoffset + len bytes data */
Vignesh R3f51de32018-12-07 14:50:41 +0100763 omap_i2c_write_reg(i2c_base, ip_rev, alen + len, OMAP_I2C_CNT_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000764 /* Set slave address */
Vignesh R3f51de32018-12-07 14:50:41 +0100765 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000766 /* Stop bit needed here */
Vignesh R3f51de32018-12-07 14:50:41 +0100767 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
768 I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP,
769 OMAP_I2C_CON_REG);
Michael Jonesbb54d572011-09-04 14:01:55 -0400770
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000771 while (alen) {
772 /* Must write reg offset (one or two bytes) */
Vignesh R3f51de32018-12-07 14:50:41 +0100773 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000774 /* Try to identify bus that is not padconf'd for I2C */
775 if (status == I2C_STAT_XRDY) {
776 i2c_error = 2;
Mugunthan V N38d943a2016-07-18 15:11:00 +0530777 printf("i2c_write: pads on bus probably not configured (status=0x%x)\n",
778 status);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000779 goto wr_exit;
780 }
Hannes Petermaierd5885052014-02-03 21:22:18 +0100781 if (status == 0 || (status & I2C_STAT_NACK)) {
Tom Rini49fbf672012-02-20 18:49:16 +0000782 i2c_error = 1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000783 printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
784 status);
785 goto wr_exit;
Tom Rini49fbf672012-02-20 18:49:16 +0000786 }
Tom Rini49fbf672012-02-20 18:49:16 +0000787 if (status & I2C_STAT_XRDY) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000788 alen--;
Vignesh R3f51de32018-12-07 14:50:41 +0100789 omap_i2c_write_reg(i2c_base, ip_rev,
790 (addr >> (8 * alen)) & 0xff,
791 OMAP_I2C_DATA_REG);
792 omap_i2c_write_reg(i2c_base, ip_rev,
793 I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000794 } else {
795 i2c_error = 1;
796 printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
797 status);
798 goto wr_exit;
799 }
800 }
Vignesh R3f51de32018-12-07 14:50:41 +0100801
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000802 /* Address phase is over, now write data */
803 for (i = 0; i < len; i++) {
Vignesh R3f51de32018-12-07 14:50:41 +0100804 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Hannes Petermaierd5885052014-02-03 21:22:18 +0100805 if (status == 0 || (status & I2C_STAT_NACK)) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000806 i2c_error = 1;
807 printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
808 status);
809 goto wr_exit;
810 }
811 if (status & I2C_STAT_XRDY) {
Vignesh R3f51de32018-12-07 14:50:41 +0100812 omap_i2c_write_reg(i2c_base, ip_rev,
813 buffer[i], OMAP_I2C_DATA_REG);
814 omap_i2c_write_reg(i2c_base, ip_rev,
815 I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
Tom Rini49fbf672012-02-20 18:49:16 +0000816 } else {
817 i2c_error = 1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000818 printf("i2c_write: bus not ready for data Tx (i=%d)\n",
819 i);
820 goto wr_exit;
Patil, Rachnaa9e18c22012-01-22 23:44:12 +0000821 }
822 }
Vignesh R3f51de32018-12-07 14:50:41 +0100823
Hannes Petermaierd5885052014-02-03 21:22:18 +0100824 /*
825 * poll ARDY bit for making sure that last byte really has been
826 * transferred on the bus.
827 */
828 do {
Vignesh R3f51de32018-12-07 14:50:41 +0100829 status = wait_for_event(i2c_base, ip_rev, waitdelay);
Hannes Petermaierd5885052014-02-03 21:22:18 +0100830 } while (!(status & I2C_STAT_ARDY) && timeout--);
831 if (timeout <= 0)
832 printf("i2c_write: timed out writig last byte!\n");
Patil, Rachnaa9e18c22012-01-22 23:44:12 +0000833
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000834wr_exit:
Vignesh R3f51de32018-12-07 14:50:41 +0100835 flush_fifo(i2c_base, ip_rev);
836 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
Tom Rini49fbf672012-02-20 18:49:16 +0000837 return i2c_error;
wdenkf8062712005-01-09 23:16:25 +0000838}
839
Igor Opaniukf7c91762021-02-09 13:52:45 +0200840#if !CONFIG_IS_ENABLED(DM_I2C)
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000841/*
Mugunthan V N38d943a2016-07-18 15:11:00 +0530842 * The legacy I2C functions. These need to get removed once
843 * all users of this driver are converted to DM.
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000844 */
Vignesh R3f51de32018-12-07 14:50:41 +0100845static void __iomem *omap24_get_base(struct i2c_adapter *adap)
Dirk Behme7a8f6572009-11-02 20:36:26 +0100846{
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200847 switch (adap->hwadapnr) {
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000848 case 0:
Vignesh R3f51de32018-12-07 14:50:41 +0100849 return (void __iomem *)I2C_BASE1;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000850 break;
851 case 1:
Vignesh R3f51de32018-12-07 14:50:41 +0100852 return (void __iomem *)I2C_BASE2;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000853 break;
Adam Ford73010ab2017-08-11 06:39:13 -0500854#if (CONFIG_SYS_I2C_BUS_MAX > 2)
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000855 case 2:
Vignesh R3f51de32018-12-07 14:50:41 +0100856 return (void __iomem *)I2C_BASE3;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000857 break;
Adam Ford73010ab2017-08-11 06:39:13 -0500858#if (CONFIG_SYS_I2C_BUS_MAX > 3)
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000859 case 3:
Vignesh R3f51de32018-12-07 14:50:41 +0100860 return (void __iomem *)I2C_BASE4;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000861 break;
Adam Ford73010ab2017-08-11 06:39:13 -0500862#if (CONFIG_SYS_I2C_BUS_MAX > 4)
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000863 case 4:
Vignesh R3f51de32018-12-07 14:50:41 +0100864 return (void __iomem *)I2C_BASE5;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000865 break;
Koen Kooi584ff5f2012-08-08 00:57:35 +0000866#endif
Dirk Behme7a8f6572009-11-02 20:36:26 +0100867#endif
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000868#endif
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200869 default:
870 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
871 break;
Lubomir Popov4d98efd2013-06-01 06:44:38 +0000872 }
Vignesh R3f51de32018-12-07 14:50:41 +0100873
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200874 return NULL;
875}
Dirk Behme7a8f6572009-11-02 20:36:26 +0100876
Vignesh R3f51de32018-12-07 14:50:41 +0100877static int omap24_get_ip_rev(void)
878{
879#ifdef CONFIG_OMAP34XX
880 return OMAP_I2C_REV_V1;
881#else
882 return OMAP_I2C_REV_V2;
883#endif
884}
Mugunthan V N38d943a2016-07-18 15:11:00 +0530885
886static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
887 int alen, uchar *buffer, int len)
888{
Vignesh R3f51de32018-12-07 14:50:41 +0100889 void __iomem *i2c_base = omap24_get_base(adap);
890 int ip_rev = omap24_get_ip_rev();
Mugunthan V N38d943a2016-07-18 15:11:00 +0530891
Vignesh R3f51de32018-12-07 14:50:41 +0100892 return __omap24_i2c_read(i2c_base, ip_rev, adap->waitdelay, chip, addr,
Mugunthan V N38d943a2016-07-18 15:11:00 +0530893 alen, buffer, len);
894}
895
Mugunthan V N38d943a2016-07-18 15:11:00 +0530896static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
897 int alen, uchar *buffer, int len)
898{
Vignesh R3f51de32018-12-07 14:50:41 +0100899 void __iomem *i2c_base = omap24_get_base(adap);
900 int ip_rev = omap24_get_ip_rev();
Mugunthan V N38d943a2016-07-18 15:11:00 +0530901
Vignesh R3f51de32018-12-07 14:50:41 +0100902 return __omap24_i2c_write(i2c_base, ip_rev, adap->waitdelay, chip, addr,
Mugunthan V N38d943a2016-07-18 15:11:00 +0530903 alen, buffer, len);
904}
905
906static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
907{
Vignesh R3f51de32018-12-07 14:50:41 +0100908 void __iomem *i2c_base = omap24_get_base(adap);
909 int ip_rev = omap24_get_ip_rev();
Mugunthan V N38d943a2016-07-18 15:11:00 +0530910 int ret;
911
Vignesh R3f51de32018-12-07 14:50:41 +0100912 ret = __omap24_i2c_setspeed(i2c_base, ip_rev, speed, &adap->waitdelay);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530913 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900914 pr_err("%s: set i2c speed failed\n", __func__);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530915 return ret;
916 }
917
918 adap->speed = speed;
919
920 return 0;
921}
922
923static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
924{
Vignesh R3f51de32018-12-07 14:50:41 +0100925 void __iomem *i2c_base = omap24_get_base(adap);
926 int ip_rev = omap24_get_ip_rev();
Mugunthan V N38d943a2016-07-18 15:11:00 +0530927
Vignesh R3f51de32018-12-07 14:50:41 +0100928 return __omap24_i2c_init(i2c_base, ip_rev, speed, slaveadd,
929 &adap->waitdelay);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530930}
931
932static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
933{
Vignesh R3f51de32018-12-07 14:50:41 +0100934 void __iomem *i2c_base = omap24_get_base(adap);
935 int ip_rev = omap24_get_ip_rev();
Mugunthan V N38d943a2016-07-18 15:11:00 +0530936
Vignesh R3f51de32018-12-07 14:50:41 +0100937 return __omap24_i2c_probe(i2c_base, ip_rev, adap->waitdelay, chip);
Mugunthan V N38d943a2016-07-18 15:11:00 +0530938}
939
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200940U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5885052014-02-03 21:22:18 +0100941 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Tom Rinia7a9bc02021-08-18 23:12:29 -0400942 CONFIG_SYS_I2C_SPEED,
943 CONFIG_SYS_I2C_SLAVE,
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200944 0)
945U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
Hannes Petermaierd5885052014-02-03 21:22:18 +0100946 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
Tom Rinia7a9bc02021-08-18 23:12:29 -0400947 CONFIG_SYS_I2C_SPEED,
948 CONFIG_SYS_I2C_SLAVE,
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200949 1)
Vignesh R3f51de32018-12-07 14:50:41 +0100950
Adam Ford73010ab2017-08-11 06:39:13 -0500951#if (CONFIG_SYS_I2C_BUS_MAX > 2)
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200952U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
953 omap24_i2c_read, omap24_i2c_write, NULL,
Tom Rinia7a9bc02021-08-18 23:12:29 -0400954 CONFIG_SYS_I2C_SPEED,
955 CONFIG_SYS_I2C_SLAVE,
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200956 2)
Adam Ford73010ab2017-08-11 06:39:13 -0500957#if (CONFIG_SYS_I2C_BUS_MAX > 3)
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200958U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
959 omap24_i2c_read, omap24_i2c_write, NULL,
Tom Rinia7a9bc02021-08-18 23:12:29 -0400960 CONFIG_SYS_I2C_SPEED,
961 CONFIG_SYS_I2C_SLAVE,
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200962 3)
Adam Ford73010ab2017-08-11 06:39:13 -0500963#if (CONFIG_SYS_I2C_BUS_MAX > 4)
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200964U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
965 omap24_i2c_read, omap24_i2c_write, NULL,
Tom Rinia7a9bc02021-08-18 23:12:29 -0400966 CONFIG_SYS_I2C_SPEED,
967 CONFIG_SYS_I2C_SLAVE,
Heiko Schocherf53f2b82013-10-22 11:03:18 +0200968 4)
969#endif
970#endif
971#endif
Mugunthan V N560037b2016-07-18 15:11:01 +0530972
973#else /* CONFIG_DM_I2C */
974
975static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
976{
977 struct omap_i2c *priv = dev_get_priv(bus);
978 int ret;
979
980 debug("i2c_xfer: %d messages\n", nmsgs);
981 for (; nmsgs > 0; nmsgs--, msg++) {
982 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
983 if (msg->flags & I2C_M_RD) {
Vignesh R3f51de32018-12-07 14:50:41 +0100984 ret = __omap24_i2c_read(priv->regs, priv->ip_rev,
985 priv->waitdelay,
Mugunthan V N560037b2016-07-18 15:11:01 +0530986 msg->addr, 0, 0, msg->buf,
987 msg->len);
988 } else {
Vignesh R3f51de32018-12-07 14:50:41 +0100989 ret = __omap24_i2c_write(priv->regs, priv->ip_rev,
990 priv->waitdelay,
Mugunthan V N560037b2016-07-18 15:11:01 +0530991 msg->addr, 0, 0, msg->buf,
992 msg->len);
993 }
994 if (ret) {
995 debug("i2c_write: error sending\n");
996 return -EREMOTEIO;
997 }
998 }
999
1000 return 0;
1001}
1002
1003static int omap_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
1004{
1005 struct omap_i2c *priv = dev_get_priv(bus);
1006
1007 priv->speed = speed;
1008
Vignesh R3f51de32018-12-07 14:50:41 +01001009 return __omap24_i2c_setspeed(priv->regs, priv->ip_rev, speed,
1010 &priv->waitdelay);
Mugunthan V N560037b2016-07-18 15:11:01 +05301011}
1012
1013static int omap_i2c_probe_chip(struct udevice *bus, uint chip_addr,
1014 uint chip_flags)
1015{
1016 struct omap_i2c *priv = dev_get_priv(bus);
1017
Vignesh R3f51de32018-12-07 14:50:41 +01001018 return __omap24_i2c_probe(priv->regs, priv->ip_rev, priv->waitdelay,
Nikita Yushchenko83f3f8b2022-02-15 21:10:09 +03001019 chip_addr) ? -EREMOTEIO : 0;
Mugunthan V N560037b2016-07-18 15:11:01 +05301020}
1021
1022static int omap_i2c_probe(struct udevice *bus)
1023{
1024 struct omap_i2c *priv = dev_get_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -07001025 struct omap_i2c_plat *plat = dev_get_plat(bus);
Mugunthan V N560037b2016-07-18 15:11:01 +05301026
Jean-Jacques Hiblot58994fc2018-12-07 14:50:42 +01001027 priv->speed = plat->speed;
1028 priv->regs = map_physmem(plat->base, sizeof(void *),
1029 MAP_NOCACHE);
1030 priv->ip_rev = plat->ip_rev;
Vignesh R3f51de32018-12-07 14:50:41 +01001031
1032 __omap24_i2c_init(priv->regs, priv->ip_rev, priv->speed, 0,
1033 &priv->waitdelay);
Mugunthan V N560037b2016-07-18 15:11:01 +05301034
1035 return 0;
1036}
1037
Simon Glass3580f6d2021-08-07 07:24:03 -06001038#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassaad29ae2020-12-03 16:55:21 -07001039static int omap_i2c_of_to_plat(struct udevice *bus)
Mugunthan V N560037b2016-07-18 15:11:01 +05301040{
Simon Glassb75b15b2020-12-03 16:55:23 -07001041 struct omap_i2c_plat *plat = dev_get_plat(bus);
Mugunthan V N560037b2016-07-18 15:11:01 +05301042
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001043 plat->base = dev_read_addr(bus);
Simon Glassf0c99c52020-01-23 11:48:22 -07001044 plat->speed = dev_read_u32_default(bus, "clock-frequency",
1045 I2C_SPEED_STANDARD_RATE);
Jean-Jacques Hiblot58994fc2018-12-07 14:50:42 +01001046 plat->ip_rev = dev_get_driver_data(bus);
Mugunthan V N560037b2016-07-18 15:11:01 +05301047
1048 return 0;
1049}
1050
Mugunthan V N560037b2016-07-18 15:11:01 +05301051static const struct udevice_id omap_i2c_ids[] = {
Vignesh R3f51de32018-12-07 14:50:41 +01001052 { .compatible = "ti,omap3-i2c", .data = OMAP_I2C_REV_V1 },
1053 { .compatible = "ti,omap4-i2c", .data = OMAP_I2C_REV_V2 },
Mugunthan V N560037b2016-07-18 15:11:01 +05301054 { }
1055};
Adam Ford1f098462018-08-20 20:24:35 -05001056#endif
1057
1058static const struct dm_i2c_ops omap_i2c_ops = {
1059 .xfer = omap_i2c_xfer,
1060 .probe_chip = omap_i2c_probe_chip,
1061 .set_bus_speed = omap_i2c_set_bus_speed,
1062};
Mugunthan V N560037b2016-07-18 15:11:01 +05301063
1064U_BOOT_DRIVER(i2c_omap) = {
1065 .name = "i2c_omap",
1066 .id = UCLASS_I2C,
Simon Glass3580f6d2021-08-07 07:24:03 -06001067#if CONFIG_IS_ENABLED(OF_REAL)
Mugunthan V N560037b2016-07-18 15:11:01 +05301068 .of_match = omap_i2c_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001069 .of_to_plat = omap_i2c_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -07001070 .plat_auto = sizeof(struct omap_i2c_plat),
Adam Ford1f098462018-08-20 20:24:35 -05001071#endif
Mugunthan V N560037b2016-07-18 15:11:01 +05301072 .probe = omap_i2c_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001073 .priv_auto = sizeof(struct omap_i2c),
Mugunthan V N560037b2016-07-18 15:11:01 +05301074 .ops = &omap_i2c_ops,
Bin Menga61b9622018-10-24 06:36:31 -07001075#if !CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N560037b2016-07-18 15:11:01 +05301076 .flags = DM_FLAG_PRE_RELOC,
Bin Menga61b9622018-10-24 06:36:31 -07001077#endif
Mugunthan V N560037b2016-07-18 15:11:01 +05301078};
1079
1080#endif /* CONFIG_DM_I2C */