1 /*
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  *
21  * 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).
35  *
36  * Copyright (c) 2014 Hannes Schmelzer <oe5hpm@oevsv.at>, B&R
37  * - Added support for set_speed
38  *
39  */
40 
41 #include <dm.h>
42 #include <i2c.h>
43 #include <log.h>
44 #include <linux/delay.h>
45 #include <linux/printk.h>
46 
47 #include <asm/io.h>
48 #include <asm/omap_i2c.h>
49 
50 /*
51  * Provide access to architecture-specific I2C header files for platforms
52  * that are NOT yet solely relying on CONFIG_DM_I2C, CONFIG_OF_CONTROL, and
53  * the defaults provided in 'omap24xx_i2c.h' for all U-Boot stages where I2C
54  * access is desired.
55  */
56 #ifndef CONFIG_ARCH_K3
57 #include <asm/arch/i2c.h>
58 #endif
59 
60 #include "omap24xx_i2c.h"
61 
62 #define I2C_TIMEOUT	1000
63 
64 /* Absolutely safe for status update at 100 kHz I2C: */
65 #define I2C_WAIT	200
66 
67 enum {
68 	OMAP_I2C_REV_REG = 0,		/* Only on IP V1 (OMAP34XX) */
69 	OMAP_I2C_IE_REG,		/* Only on IP V1 (OMAP34XX) */
70 	OMAP_I2C_STAT_REG,
71 	OMAP_I2C_WE_REG,
72 	OMAP_I2C_SYSS_REG,
73 	OMAP_I2C_BUF_REG,
74 	OMAP_I2C_CNT_REG,
75 	OMAP_I2C_DATA_REG,
76 	OMAP_I2C_SYSC_REG,
77 	OMAP_I2C_CON_REG,
78 	OMAP_I2C_OA_REG,
79 	OMAP_I2C_SA_REG,
80 	OMAP_I2C_PSC_REG,
81 	OMAP_I2C_SCLL_REG,
82 	OMAP_I2C_SCLH_REG,
83 	OMAP_I2C_SYSTEST_REG,
84 	OMAP_I2C_BUFSTAT_REG,
85 	/* Only on IP V2 (OMAP4430, etc.) */
86 	OMAP_I2C_IP_V2_REVNB_LO,
87 	OMAP_I2C_IP_V2_REVNB_HI,
88 	OMAP_I2C_IP_V2_IRQSTATUS_RAW,
89 	OMAP_I2C_IP_V2_IRQENABLE_SET,
90 	OMAP_I2C_IP_V2_IRQENABLE_CLR,
91 };
92 
93 static const u8 __maybe_unused reg_map_ip_v1[] = {
94 	[OMAP_I2C_REV_REG] = 0x00,
95 	[OMAP_I2C_IE_REG] = 0x04,
96 	[OMAP_I2C_STAT_REG] = 0x08,
97 	[OMAP_I2C_WE_REG] = 0x0c,
98 	[OMAP_I2C_SYSS_REG] = 0x10,
99 	[OMAP_I2C_BUF_REG] = 0x14,
100 	[OMAP_I2C_CNT_REG] = 0x18,
101 	[OMAP_I2C_DATA_REG] = 0x1c,
102 	[OMAP_I2C_SYSC_REG] = 0x20,
103 	[OMAP_I2C_CON_REG] = 0x24,
104 	[OMAP_I2C_OA_REG] = 0x28,
105 	[OMAP_I2C_SA_REG] = 0x2c,
106 	[OMAP_I2C_PSC_REG] = 0x30,
107 	[OMAP_I2C_SCLL_REG] = 0x34,
108 	[OMAP_I2C_SCLH_REG] = 0x38,
109 	[OMAP_I2C_SYSTEST_REG] = 0x3c,
110 	[OMAP_I2C_BUFSTAT_REG] = 0x40,
111 };
112 
113 static const u8 __maybe_unused reg_map_ip_v2[] = {
114 	[OMAP_I2C_STAT_REG] = 0x28,
115 	[OMAP_I2C_WE_REG] = 0x34,
116 	[OMAP_I2C_SYSS_REG] = 0x90,
117 	[OMAP_I2C_BUF_REG] = 0x94,
118 	[OMAP_I2C_CNT_REG] = 0x98,
119 	[OMAP_I2C_DATA_REG] = 0x9c,
120 	[OMAP_I2C_SYSC_REG] = 0x10,
121 	[OMAP_I2C_CON_REG] = 0xa4,
122 	[OMAP_I2C_OA_REG] = 0xa8,
123 	[OMAP_I2C_SA_REG] = 0xac,
124 	[OMAP_I2C_PSC_REG] = 0xb0,
125 	[OMAP_I2C_SCLL_REG] = 0xb4,
126 	[OMAP_I2C_SCLH_REG] = 0xb8,
127 	[OMAP_I2C_SYSTEST_REG] = 0xbc,
128 	[OMAP_I2C_BUFSTAT_REG] = 0xc0,
129 	[OMAP_I2C_IP_V2_REVNB_LO] = 0x00,
130 	[OMAP_I2C_IP_V2_REVNB_HI] = 0x04,
131 	[OMAP_I2C_IP_V2_IRQSTATUS_RAW] = 0x24,
132 	[OMAP_I2C_IP_V2_IRQENABLE_SET] = 0x2c,
133 	[OMAP_I2C_IP_V2_IRQENABLE_CLR] = 0x30,
134 };
135 
136 struct omap_i2c {
137 	struct udevice *clk;
138 	int ip_rev;
139 	struct i2c *regs;
140 	unsigned int speed;
141 	int waitdelay;
142 	int clk_id;
143 };
144 
omap_i2c_get_ip_reg_map(int ip_rev)145 static inline const u8 *omap_i2c_get_ip_reg_map(int ip_rev)
146 {
147 	switch (ip_rev) {
148 	case OMAP_I2C_REV_V1:
149 		return reg_map_ip_v1;
150 	case OMAP_I2C_REV_V2:
151 		/* Fall through... */
152 	default:
153 		return reg_map_ip_v2;
154 	}
155 }
156 
omap_i2c_write_reg(void __iomem * base,int ip_rev,u16 val,int reg)157 static inline void omap_i2c_write_reg(void __iomem *base, int ip_rev,
158 				      u16 val, int reg)
159 {
160 	writew(val, base + omap_i2c_get_ip_reg_map(ip_rev)[reg]);
161 }
162 
omap_i2c_read_reg(void __iomem * base,int ip_rev,int reg)163 static inline u16 omap_i2c_read_reg(void __iomem *base, int ip_rev, int reg)
164 {
165 	return readw(base + omap_i2c_get_ip_reg_map(ip_rev)[reg]);
166 }
167 
omap24_i2c_findpsc(u32 * pscl,u32 * psch,uint speed)168 static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
169 {
170 	unsigned long internal_clk = 0, fclk;
171 	unsigned int prescaler;
172 
173 	/*
174 	 * This method is only called for Standard and Fast Mode speeds
175 	 *
176 	 * For some TI SoCs it is explicitly written in TRM (e,g, SPRUHZ6G,
177 	 * page 5685, Table 24-7)
178 	 * that the internal I2C clock (after prescaler) should be between
179 	 * 7-12 MHz (at least for Fast Mode (FS)).
180 	 *
181 	 * Such approach is used in v4.9 Linux kernel in:
182 	 * ./drivers/i2c/busses/i2c-omap.c (omap_i2c_init function).
183 	 */
184 
185 	speed /= 1000; /* convert speed to kHz */
186 
187 	if (speed > 100)
188 		internal_clk = 9600;
189 	else
190 		internal_clk = 4000;
191 
192 	fclk = I2C_IP_CLK / 1000;
193 	prescaler = fclk / internal_clk;
194 	prescaler = prescaler - 1;
195 
196 	if (speed > 100) {
197 		unsigned long scl;
198 
199 		/* Fast mode */
200 		scl = internal_clk / speed;
201 		*pscl = scl - (scl / 3) - I2C_FASTSPEED_SCLL_TRIM;
202 		*psch = (scl / 3) - I2C_FASTSPEED_SCLH_TRIM;
203 	} else {
204 		/* Standard mode */
205 		*pscl = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLL_TRIM;
206 		*psch = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLH_TRIM;
207 	}
208 
209 	debug("%s: speed [kHz]: %d psc: 0x%x sscl: 0x%x ssch: 0x%x\n",
210 	      __func__, speed, prescaler, *pscl, *psch);
211 
212 	if (*pscl <= 0 || *psch <= 0 || prescaler <= 0)
213 		return -EINVAL;
214 
215 	return prescaler;
216 }
217 
218 /*
219  * Wait for the bus to be free by checking the Bus Busy (BB)
220  * bit to become clear
221  */
wait_for_bb(void __iomem * i2c_base,int ip_rev,int waitdelay)222 static int wait_for_bb(void __iomem *i2c_base, int ip_rev, int waitdelay)
223 {
224 	int timeout = I2C_TIMEOUT;
225 	int irq_stat_reg;
226 	u16 stat;
227 
228 	irq_stat_reg = (ip_rev == OMAP_I2C_REV_V1) ?
229 		       OMAP_I2C_STAT_REG : OMAP_I2C_IP_V2_IRQSTATUS_RAW;
230 
231 	/* clear current interrupts */
232 	omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
233 
234 	while ((stat = omap_i2c_read_reg(i2c_base, ip_rev, irq_stat_reg) &
235 		I2C_STAT_BB) && timeout--) {
236 		omap_i2c_write_reg(i2c_base, ip_rev, stat, OMAP_I2C_STAT_REG);
237 		udelay(waitdelay);
238 	}
239 
240 	if (timeout <= 0) {
241 		printf("Timed out in %s: status=%04x\n", __func__, stat);
242 		return 1;
243 	}
244 
245 	/* clear delayed stuff */
246 	omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
247 	return 0;
248 }
249 
250 /*
251  * Wait for the I2C controller to complete current action
252  * and update status
253  */
wait_for_event(void __iomem * i2c_base,int ip_rev,int waitdelay)254 static u16 wait_for_event(void __iomem *i2c_base, int ip_rev, int waitdelay)
255 {
256 	u16 status;
257 	int timeout = I2C_TIMEOUT;
258 	int irq_stat_reg;
259 
260 	irq_stat_reg = (ip_rev == OMAP_I2C_REV_V1) ?
261 		       OMAP_I2C_STAT_REG : OMAP_I2C_IP_V2_IRQSTATUS_RAW;
262 	do {
263 		udelay(waitdelay);
264 		status = omap_i2c_read_reg(i2c_base, ip_rev, irq_stat_reg);
265 	} while (!(status &
266 		   (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
267 		    I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
268 		    I2C_STAT_AL)) && timeout--);
269 
270 	if (timeout <= 0) {
271 		printf("Timed out in %s: status=%04x\n", __func__, status);
272 		/*
273 		 * If status is still 0 here, probably the bus pads have
274 		 * not been configured for I2C, and/or pull-ups are missing.
275 		 */
276 		printf("Check if pads/pull-ups of bus are properly configured\n");
277 		omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
278 		status = 0;
279 	}
280 
281 	return status;
282 }
283 
flush_fifo(void __iomem * i2c_base,int ip_rev)284 static void flush_fifo(void __iomem *i2c_base, int ip_rev)
285 {
286 	u16 stat;
287 
288 	/*
289 	 * note: if you try and read data when its not there or ready
290 	 * you get a bus error
291 	 */
292 	while (1) {
293 		stat = omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_STAT_REG);
294 		if (stat == I2C_STAT_RRDY) {
295 			omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_DATA_REG);
296 			omap_i2c_write_reg(i2c_base, ip_rev,
297 					   I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
298 			udelay(1000);
299 		} else
300 			break;
301 	}
302 }
303 
__omap24_i2c_setspeed(void __iomem * i2c_base,int ip_rev,uint speed,int * waitdelay)304 static int __omap24_i2c_setspeed(void __iomem *i2c_base, int ip_rev, uint speed,
305 				 int *waitdelay)
306 {
307 	int psc, fsscll = 0, fssclh = 0;
308 	int hsscll = 0, hssclh = 0;
309 	u32 scll = 0, sclh = 0;
310 
311 	if (speed >= I2C_SPEED_HIGH_RATE) {
312 		/* High speed */
313 		psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
314 		psc -= 1;
315 		if (psc < I2C_PSC_MIN) {
316 			printf("Error : I2C unsupported prescaler %d\n", psc);
317 			return -1;
318 		}
319 
320 		/* For first phase of HS mode */
321 		fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
322 
323 		fssclh = fsscll;
324 
325 		fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
326 		fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
327 		if (((fsscll < 0) || (fssclh < 0)) ||
328 		    ((fsscll > 255) || (fssclh > 255))) {
329 			puts("Error : I2C initializing first phase clock\n");
330 			return -1;
331 		}
332 
333 		/* For second phase of HS mode */
334 		hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
335 
336 		hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
337 		hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
338 		if (((fsscll < 0) || (fssclh < 0)) ||
339 		    ((fsscll > 255) || (fssclh > 255))) {
340 			puts("Error : I2C initializing second phase clock\n");
341 			return -1;
342 		}
343 
344 		scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
345 		sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
346 
347 	} else {
348 		/* Standard and fast speed */
349 		psc = omap24_i2c_findpsc(&scll, &sclh, speed);
350 		if (0 > psc) {
351 			puts("Error : I2C initializing clock\n");
352 			return -1;
353 		}
354 	}
355 
356 	/* wait for 20 clkperiods */
357 	*waitdelay = (10000000 / speed) * 2;
358 
359 	omap_i2c_write_reg(i2c_base, ip_rev, 0,  OMAP_I2C_CON_REG);
360 	omap_i2c_write_reg(i2c_base, ip_rev, psc, OMAP_I2C_PSC_REG);
361 	omap_i2c_write_reg(i2c_base, ip_rev, scll, OMAP_I2C_SCLL_REG);
362 	omap_i2c_write_reg(i2c_base, ip_rev, sclh, OMAP_I2C_SCLH_REG);
363 	omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN, OMAP_I2C_CON_REG);
364 
365 	/* clear all pending status */
366 	omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
367 
368 	return 0;
369 }
370 
omap24_i2c_deblock(void __iomem * i2c_base,int ip_rev)371 static void omap24_i2c_deblock(void __iomem *i2c_base, int ip_rev)
372 {
373 	int i;
374 	u16 systest;
375 	u16 orgsystest;
376 
377 	/* set test mode ST_EN = 1 */
378 	orgsystest = omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_SYSTEST_REG);
379 	systest = orgsystest;
380 
381 	/* enable testmode */
382 	systest |= I2C_SYSTEST_ST_EN;
383 	omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
384 	systest &= ~I2C_SYSTEST_TMODE_MASK;
385 	systest |= 3 << I2C_SYSTEST_TMODE_SHIFT;
386 	omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
387 
388 	/* set SCL, SDA  = 1 */
389 	systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
390 	omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
391 	udelay(10);
392 
393 	/* toggle scl 9 clocks */
394 	for (i = 0; i < 9; i++) {
395 		/* SCL = 0 */
396 		systest &= ~I2C_SYSTEST_SCL_O;
397 		omap_i2c_write_reg(i2c_base, ip_rev,
398 				   systest, OMAP_I2C_SYSTEST_REG);
399 		udelay(10);
400 		/* SCL = 1 */
401 		systest |= I2C_SYSTEST_SCL_O;
402 		omap_i2c_write_reg(i2c_base, ip_rev,
403 				   systest, OMAP_I2C_SYSTEST_REG);
404 		udelay(10);
405 	}
406 
407 	/* send stop */
408 	systest &= ~I2C_SYSTEST_SDA_O;
409 	omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
410 	udelay(10);
411 	systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
412 	omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
413 	udelay(10);
414 
415 	/* restore original mode */
416 	omap_i2c_write_reg(i2c_base, ip_rev, orgsystest, OMAP_I2C_SYSTEST_REG);
417 }
418 
__omap24_i2c_init(void __iomem * i2c_base,int ip_rev,int speed,int slaveadd,int * waitdelay)419 static void __omap24_i2c_init(void __iomem *i2c_base, int ip_rev, int speed,
420 			      int slaveadd, int *waitdelay)
421 {
422 	int timeout = I2C_TIMEOUT;
423 	int deblock = 1;
424 
425 retry:
426 	if (omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_CON_REG) &
427 	    I2C_CON_EN) {
428 		omap_i2c_write_reg(i2c_base, ip_rev, 0, OMAP_I2C_CON_REG);
429 		udelay(50000);
430 	}
431 
432 	/* for ES2 after soft reset */
433 	omap_i2c_write_reg(i2c_base, ip_rev, 0x2, OMAP_I2C_SYSC_REG);
434 	udelay(1000);
435 
436 	omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN, OMAP_I2C_CON_REG);
437 	while (!(omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_SYSS_REG) &
438 		 I2C_SYSS_RDONE) && timeout--) {
439 		if (timeout <= 0) {
440 			puts("ERROR: Timeout in soft-reset\n");
441 			return;
442 		}
443 		udelay(1000);
444 	}
445 
446 	if (__omap24_i2c_setspeed(i2c_base, ip_rev, speed, waitdelay)) {
447 		printf("ERROR: failed to setup I2C bus-speed!\n");
448 		return;
449 	}
450 
451 	/* own address */
452 	omap_i2c_write_reg(i2c_base, ip_rev, slaveadd, OMAP_I2C_OA_REG);
453 
454 	if (ip_rev == OMAP_I2C_REV_V1) {
455 		/*
456 		 * Have to enable interrupts for OMAP2/3, these IPs don't have
457 		 * an 'irqstatus_raw' register and we shall have to poll 'stat'
458 		 */
459 		omap_i2c_write_reg(i2c_base, ip_rev, I2C_IE_XRDY_IE |
460 				   I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
461 				   I2C_IE_NACK_IE | I2C_IE_AL_IE,
462 				   OMAP_I2C_IE_REG);
463 	}
464 
465 	udelay(1000);
466 	flush_fifo(i2c_base, ip_rev);
467 	omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
468 
469 	/* Handle possible failed I2C state */
470 	if (wait_for_bb(i2c_base, ip_rev, *waitdelay))
471 		if (deblock == 1) {
472 			omap24_i2c_deblock(i2c_base, ip_rev);
473 			deblock = 0;
474 			goto retry;
475 		}
476 }
477 
478 /*
479  * i2c_probe: Use write access. Allows to identify addresses that are
480  *            write-only (like the config register of dual-port EEPROMs)
481  */
__omap24_i2c_probe(void __iomem * i2c_base,int ip_rev,int waitdelay,uchar chip)482 static int __omap24_i2c_probe(void __iomem *i2c_base, int ip_rev, int waitdelay,
483 			      uchar chip)
484 {
485 	u16 status;
486 	int res = 1; /* default = fail */
487 
488 	if (chip == omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_OA_REG))
489 		return res;
490 
491 	/* Wait until bus is free */
492 	if (wait_for_bb(i2c_base, ip_rev, waitdelay))
493 		return res;
494 
495 	/* No data transfer, slave addr only */
496 	omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
497 
498 	/* Stop bit needed here */
499 	omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
500 			   I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP,
501 			   OMAP_I2C_CON_REG);
502 
503 	status = wait_for_event(i2c_base, ip_rev, waitdelay);
504 
505 	if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
506 		/*
507 		 * With current high-level command implementation, notifying
508 		 * the user shall flood the console with 127 messages. If
509 		 * silent exit is desired upon unconfigured bus, remove the
510 		 * following 'if' section:
511 		 */
512 		if (status == I2C_STAT_XRDY)
513 			printf("i2c_probe: pads on bus probably not configured (status=0x%x)\n",
514 			       status);
515 
516 		goto pr_exit;
517 	}
518 
519 	/* Check for ACK (!NAK) */
520 	if (!(status & I2C_STAT_NACK)) {
521 		res = 0;				/* Device found */
522 		udelay(waitdelay);/* Required by AM335X in SPL */
523 		/* Abort transfer (force idle state) */
524 		omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_MST | I2C_CON_TRX,
525 				   OMAP_I2C_CON_REG);	/* Reset */
526 		udelay(1000);
527 		omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
528 				   I2C_CON_TRX | I2C_CON_STP,
529 				   OMAP_I2C_CON_REG);	/* STP */
530 	}
531 
532 pr_exit:
533 	flush_fifo(i2c_base, ip_rev);
534 	omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
535 	return res;
536 }
537 
538 #if !CONFIG_IS_ENABLED(DM_I2C)
539 /*
540  * The legacy I2C functions. These need to get removed once
541  * all users of this driver are converted to DM.
542  */
543 
544 /*
545  * i2c_read: Function now uses a single I2C read transaction with bulk transfer
546  *           of the requested number of bytes (note that the 'i2c md' command
547  *           limits this to 16 bytes anyway).
548  *           Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
549  *           The address (reg offset) may be 0, 1 or 2 bytes long.
550  *           Function now reads correctly from chips that return more than one
551  *           byte of data per addressed register (like TI temperature sensors),
552  *           or that do not need a register address at all (such as some clock
553  *           distributors).
554  */
__omap24_i2c_read(void __iomem * i2c_base,int ip_rev,int waitdelay,uchar chip,uint addr,int alen,uchar * buffer,int len)555 static int __omap24_i2c_read(void __iomem *i2c_base, int ip_rev, int waitdelay,
556 			     uchar chip, uint addr, int alen, uchar *buffer,
557 			     int len)
558 {
559 	int i2c_error = 0;
560 	u16 status;
561 
562 	if (alen < 0) {
563 		puts("I2C read: addr len < 0\n");
564 		return 1;
565 	}
566 
567 	if (len < 0) {
568 		puts("I2C read: data len < 0\n");
569 		return 1;
570 	}
571 
572 	if (buffer == NULL) {
573 		puts("I2C read: NULL pointer passed\n");
574 		return 1;
575 	}
576 
577 	if (alen > 2) {
578 		printf("I2C read: addr len %d not supported\n", alen);
579 		return 1;
580 	}
581 
582 	if (addr + len > (1 << 16)) {
583 		puts("I2C read: address out of range\n");
584 		return 1;
585 	}
586 
587 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
588 	/*
589 	 * EEPROM chips that implement "address overflow" are ones
590 	 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
591 	 * address and the extra bits end up in the "chip address"
592 	 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
593 	 * four 256 byte chips.
594 	 *
595 	 * Note that we consider the length of the address field to
596 	 * still be one byte because the extra address bits are
597 	 * hidden in the chip address.
598 	 */
599 	if (alen > 0)
600 		chip |= ((addr >> (alen * 8)) &
601 			 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
602 #endif
603 
604 	/* Wait until bus not busy */
605 	if (wait_for_bb(i2c_base, ip_rev, waitdelay))
606 		return 1;
607 
608 	/* Zero, one or two bytes reg address (offset) */
609 	omap_i2c_write_reg(i2c_base, ip_rev, alen, OMAP_I2C_CNT_REG);
610 	/* Set slave address */
611 	omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
612 
613 	if (alen) {
614 		/* Must write reg offset first */
615 		/* Stop - Start (P-S) */
616 		omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
617 				   I2C_CON_STT | I2C_CON_STP | I2C_CON_TRX,
618 				   OMAP_I2C_CON_REG);
619 		/* Send register offset */
620 		while (1) {
621 			status = wait_for_event(i2c_base, ip_rev, waitdelay);
622 			/* Try to identify bus that is not padconf'd for I2C */
623 			if (status == I2C_STAT_XRDY) {
624 				i2c_error = 2;
625 				printf("i2c_read (addr phase): pads on bus probably not configured (status=0x%x)\n",
626 				       status);
627 				goto rd_exit;
628 			}
629 			if (status == 0 || (status & I2C_STAT_NACK)) {
630 				i2c_error = 1;
631 				printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
632 				       status);
633 				goto rd_exit;
634 			}
635 			if (alen) {
636 				if (status & I2C_STAT_XRDY) {
637 					u8 addr_byte;
638 					alen--;
639 					addr_byte = (addr >> (8 * alen)) & 0xff;
640 					omap_i2c_write_reg(i2c_base, ip_rev,
641 							   addr_byte,
642 							   OMAP_I2C_DATA_REG);
643 					omap_i2c_write_reg(i2c_base, ip_rev,
644 							   I2C_STAT_XRDY,
645 							   OMAP_I2C_STAT_REG);
646 				}
647 			}
648 			if (status & I2C_STAT_ARDY) {
649 				omap_i2c_write_reg(i2c_base, ip_rev,
650 						   I2C_STAT_ARDY,
651 						   OMAP_I2C_STAT_REG);
652 				break;
653 			}
654 		}
655 	}
656 
657 	/* Set slave address */
658 	omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
659 	/* Read len bytes from slave */
660 	omap_i2c_write_reg(i2c_base, ip_rev, len, OMAP_I2C_CNT_REG);
661 	/* Need stop bit here */
662 	omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
663 			   I2C_CON_STT | I2C_CON_STP, OMAP_I2C_CON_REG);
664 
665 	/* Receive data */
666 	while (1) {
667 		status = wait_for_event(i2c_base, ip_rev, waitdelay);
668 		/*
669 		 * Try to identify bus that is not padconf'd for I2C. This
670 		 * state could be left over from previous transactions if
671 		 * the address phase is skipped due to alen=0.
672 		 */
673 		if (status == I2C_STAT_XRDY) {
674 			i2c_error = 2;
675 			printf("i2c_read (data phase): pads on bus probably not configured (status=0x%x)\n",
676 			       status);
677 			goto rd_exit;
678 		}
679 		if (status == 0 || (status & I2C_STAT_NACK)) {
680 			i2c_error = 1;
681 			goto rd_exit;
682 		}
683 		if (status & I2C_STAT_RRDY) {
684 			*buffer++ = omap_i2c_read_reg(i2c_base, ip_rev,
685 						      OMAP_I2C_DATA_REG);
686 			omap_i2c_write_reg(i2c_base, ip_rev,
687 					   I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
688 		}
689 		if (status & I2C_STAT_ARDY) {
690 			omap_i2c_write_reg(i2c_base, ip_rev,
691 					   I2C_STAT_ARDY, OMAP_I2C_STAT_REG);
692 			break;
693 		}
694 	}
695 
696 rd_exit:
697 	flush_fifo(i2c_base, ip_rev);
698 	omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
699 	return i2c_error;
700 }
701 
702 /* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
__omap24_i2c_write(void __iomem * i2c_base,int ip_rev,int waitdelay,uchar chip,uint addr,int alen,uchar * buffer,int len)703 static int __omap24_i2c_write(void __iomem *i2c_base, int ip_rev, int waitdelay,
704 			      uchar chip, uint addr, int alen, uchar *buffer,
705 			      int len)
706 {
707 	int i;
708 	u16 status;
709 	int i2c_error = 0;
710 	int timeout = I2C_TIMEOUT;
711 
712 	if (alen < 0) {
713 		puts("I2C write: addr len < 0\n");
714 		return 1;
715 	}
716 
717 	if (len < 0) {
718 		puts("I2C write: data len < 0\n");
719 		return 1;
720 	}
721 
722 	if (buffer == NULL) {
723 		puts("I2C write: NULL pointer passed\n");
724 		return 1;
725 	}
726 
727 	if (alen > 2) {
728 		printf("I2C write: addr len %d not supported\n", alen);
729 		return 1;
730 	}
731 
732 	if (addr + len > (1 << 16)) {
733 		printf("I2C write: address 0x%x + 0x%x out of range\n",
734 		       addr, len);
735 		return 1;
736 	}
737 
738 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
739 	/*
740 	 * EEPROM chips that implement "address overflow" are ones
741 	 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
742 	 * address and the extra bits end up in the "chip address"
743 	 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
744 	 * four 256 byte chips.
745 	 *
746 	 * Note that we consider the length of the address field to
747 	 * still be one byte because the extra address bits are
748 	 * hidden in the chip address.
749 	 */
750 	if (alen > 0)
751 		chip |= ((addr >> (alen * 8)) &
752 			 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
753 #endif
754 
755 	/* Wait until bus not busy */
756 	if (wait_for_bb(i2c_base, ip_rev, waitdelay))
757 		return 1;
758 
759 	/* Start address phase - will write regoffset + len bytes data */
760 	omap_i2c_write_reg(i2c_base, ip_rev, alen + len, OMAP_I2C_CNT_REG);
761 	/* Set slave address */
762 	omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
763 	/* Stop bit needed here */
764 	omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
765 			   I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP,
766 			   OMAP_I2C_CON_REG);
767 
768 	while (alen) {
769 		/* Must write reg offset (one or two bytes) */
770 		status = wait_for_event(i2c_base, ip_rev, waitdelay);
771 		/* Try to identify bus that is not padconf'd for I2C */
772 		if (status == I2C_STAT_XRDY) {
773 			i2c_error = 2;
774 			printf("i2c_write: pads on bus probably not configured (status=0x%x)\n",
775 			       status);
776 			goto wr_exit;
777 		}
778 		if (status == 0 || (status & I2C_STAT_NACK)) {
779 			i2c_error = 1;
780 			printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
781 			       status);
782 			goto wr_exit;
783 		}
784 		if (status & I2C_STAT_XRDY) {
785 			alen--;
786 			omap_i2c_write_reg(i2c_base, ip_rev,
787 					   (addr >> (8 * alen)) & 0xff,
788 					   OMAP_I2C_DATA_REG);
789 			omap_i2c_write_reg(i2c_base, ip_rev,
790 					   I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
791 		} else {
792 			i2c_error = 1;
793 			printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
794 			       status);
795 			goto wr_exit;
796 		}
797 	}
798 
799 	/* Address phase is over, now write data */
800 	for (i = 0; i < len; i++) {
801 		status = wait_for_event(i2c_base, ip_rev, waitdelay);
802 		if (status == 0 || (status & I2C_STAT_NACK)) {
803 			i2c_error = 1;
804 			printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
805 			       status);
806 			goto wr_exit;
807 		}
808 		if (status & I2C_STAT_XRDY) {
809 			omap_i2c_write_reg(i2c_base, ip_rev,
810 					   buffer[i], OMAP_I2C_DATA_REG);
811 			omap_i2c_write_reg(i2c_base, ip_rev,
812 					   I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
813 		} else {
814 			i2c_error = 1;
815 			printf("i2c_write: bus not ready for data Tx (i=%d)\n",
816 			       i);
817 			goto wr_exit;
818 		}
819 	}
820 
821 	/*
822 	 * poll ARDY bit for making sure that last byte really has been
823 	 * transferred on the bus.
824 	 */
825 	do {
826 		status = wait_for_event(i2c_base, ip_rev, waitdelay);
827 	} while (!(status & I2C_STAT_ARDY) && timeout--);
828 	if (timeout <= 0)
829 		printf("i2c_write: timed out writig last byte!\n");
830 
831 wr_exit:
832 	flush_fifo(i2c_base, ip_rev);
833 	omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
834 	return i2c_error;
835 }
836 
omap24_get_base(struct i2c_adapter * adap)837 static void __iomem *omap24_get_base(struct i2c_adapter *adap)
838 {
839 	switch (adap->hwadapnr) {
840 	case 0:
841 		return (void __iomem *)I2C_BASE1;
842 		break;
843 	case 1:
844 		return (void __iomem *)I2C_BASE2;
845 		break;
846 #if (CONFIG_SYS_I2C_BUS_MAX > 2)
847 	case 2:
848 		return (void __iomem *)I2C_BASE3;
849 		break;
850 #if (CONFIG_SYS_I2C_BUS_MAX > 3)
851 	case 3:
852 		return (void __iomem *)I2C_BASE4;
853 		break;
854 #if (CONFIG_SYS_I2C_BUS_MAX > 4)
855 	case 4:
856 		return (void __iomem *)I2C_BASE5;
857 		break;
858 #endif
859 #endif
860 #endif
861 	default:
862 		printf("wrong hwadapnr: %d\n", adap->hwadapnr);
863 		break;
864 	}
865 
866 	return NULL;
867 }
868 
omap24_get_ip_rev(void)869 static int omap24_get_ip_rev(void)
870 {
871 #ifdef CONFIG_OMAP34XX
872 	return OMAP_I2C_REV_V1;
873 #else
874 	return OMAP_I2C_REV_V2;
875 #endif
876 }
877 
omap24_i2c_read(struct i2c_adapter * adap,uchar chip,uint addr,int alen,uchar * buffer,int len)878 static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
879 			   int alen, uchar *buffer, int len)
880 {
881 	void __iomem *i2c_base = omap24_get_base(adap);
882 	int ip_rev = omap24_get_ip_rev();
883 
884 	return __omap24_i2c_read(i2c_base, ip_rev, adap->waitdelay, chip, addr,
885 				 alen, buffer, len);
886 }
887 
omap24_i2c_write(struct i2c_adapter * adap,uchar chip,uint addr,int alen,uchar * buffer,int len)888 static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
889 			    int alen, uchar *buffer, int len)
890 {
891 	void __iomem *i2c_base = omap24_get_base(adap);
892 	int ip_rev = omap24_get_ip_rev();
893 
894 	return __omap24_i2c_write(i2c_base, ip_rev, adap->waitdelay, chip, addr,
895 				  alen, buffer, len);
896 }
897 
omap24_i2c_setspeed(struct i2c_adapter * adap,uint speed)898 static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
899 {
900 	void __iomem *i2c_base = omap24_get_base(adap);
901 	int ip_rev = omap24_get_ip_rev();
902 	int ret;
903 
904 	ret = __omap24_i2c_setspeed(i2c_base, ip_rev, speed, &adap->waitdelay);
905 	if (ret) {
906 		pr_err("%s: set i2c speed failed\n", __func__);
907 		return ret;
908 	}
909 
910 	adap->speed = speed;
911 
912 	return 0;
913 }
914 
omap24_i2c_init(struct i2c_adapter * adap,int speed,int slaveadd)915 static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
916 {
917 	void __iomem *i2c_base = omap24_get_base(adap);
918 	int ip_rev = omap24_get_ip_rev();
919 
920 	return __omap24_i2c_init(i2c_base, ip_rev, speed, slaveadd,
921 				 &adap->waitdelay);
922 }
923 
omap24_i2c_probe(struct i2c_adapter * adap,uchar chip)924 static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
925 {
926 	void __iomem *i2c_base = omap24_get_base(adap);
927 	int ip_rev = omap24_get_ip_rev();
928 
929 	return __omap24_i2c_probe(i2c_base, ip_rev, adap->waitdelay, chip);
930 }
931 
932 U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
933 			 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
934 			 CONFIG_SYS_I2C_SPEED,
935 			 CONFIG_SYS_I2C_SLAVE,
936 			 0)
937 U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
938 			 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
939 			 CONFIG_SYS_I2C_SPEED,
940 			 CONFIG_SYS_I2C_SLAVE,
941 			 1)
942 
943 #if (CONFIG_SYS_I2C_BUS_MAX > 2)
944 U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
945 			 omap24_i2c_read, omap24_i2c_write, NULL,
946 			 CONFIG_SYS_I2C_SPEED,
947 			 CONFIG_SYS_I2C_SLAVE,
948 			 2)
949 #if (CONFIG_SYS_I2C_BUS_MAX > 3)
950 U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
951 			 omap24_i2c_read, omap24_i2c_write, NULL,
952 			 CONFIG_SYS_I2C_SPEED,
953 			 CONFIG_SYS_I2C_SLAVE,
954 			 3)
955 #if (CONFIG_SYS_I2C_BUS_MAX > 4)
956 U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
957 			 omap24_i2c_read, omap24_i2c_write, NULL,
958 			 CONFIG_SYS_I2C_SPEED,
959 			 CONFIG_SYS_I2C_SLAVE,
960 			 4)
961 #endif
962 #endif
963 #endif
964 
965 #else /* CONFIG_DM_I2C */
966 
967 static int __omap24_i2c_xfer_msg(void __iomem *i2c_base, int ip_rev, int waitdelay,
968 				 uchar chip, uchar *buffer, int len, u16 i2c_con_reg)
969 {
970 	int i;
971 	u16 status;
972 	int i2c_error = 0;
973 	int timeout = I2C_TIMEOUT;
974 
975 	if (len < 0) {
976 		printf("%s: data len < 0\n", __func__);
977 		return -EINVAL;
978 	}
979 
980 	if (!buffer) {
981 		printf("%s: NULL pointer passed\n", __func__);
982 		return -EINVAL;
983 	}
984 
985 	if (!(i2c_con_reg & I2C_CON_EN)) {
986 		printf("%s: I2C_CON_EN not set\n", __func__);
987 		return -EINVAL;
988 	}
989 
990 	/* Set slave address */
991 	omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
992 	/* Read/Write len bytes data */
993 	omap_i2c_write_reg(i2c_base, ip_rev, len, OMAP_I2C_CNT_REG);
994 	/* Configure the I2C_CON register */
995 	omap_i2c_write_reg(i2c_base, ip_rev, i2c_con_reg, OMAP_I2C_CON_REG);
996 
997 	/* read/write data bytewise */
998 	for (i = 0; i < len; i++) {
999 		status = wait_for_event(i2c_base, ip_rev, waitdelay);
1000 		/* Ignore I2C_STAT_RRDY in transmitter mode */
1001 		if (i2c_con_reg & I2C_CON_TRX)
1002 			status &= ~I2C_STAT_RRDY;
1003 		else
1004 			status &= ~I2C_STAT_XRDY;
1005 
1006 		/* Try to identify bus that is not padconf'd for I2C */
1007 		if (status == I2C_STAT_XRDY) {
1008 			i2c_error = -EREMOTEIO;
1009 			printf("%s: pads on bus probably not configured (status=0x%x)\n",
1010 			       __func__, status);
1011 			goto xfer_exit;
1012 		}
1013 		if (status == 0 || (status & I2C_STAT_NACK)) {
1014 			i2c_error = -EREMOTEIO;
1015 			printf("%s: error waiting for ACK (status=0x%x)\n",
1016 			       __func__, status);
1017 			goto xfer_exit;
1018 		}
1019 		if (status & I2C_STAT_XRDY) {
1020 			/* Transmit data */
1021 			omap_i2c_write_reg(i2c_base, ip_rev,
1022 					   buffer[i], OMAP_I2C_DATA_REG);
1023 			omap_i2c_write_reg(i2c_base, ip_rev,
1024 					   I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
1025 		}
1026 		if (status & I2C_STAT_RRDY) {
1027 			/* Receive data */
1028 			*buffer++ = omap_i2c_read_reg(i2c_base, ip_rev,
1029 							OMAP_I2C_DATA_REG);
1030 			omap_i2c_write_reg(i2c_base, ip_rev,
1031 					   I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
1032 		}
1033 	}
1034 
1035 	/*
1036 	 * poll ARDY bit for making sure that last byte really has been
1037 	 * transferred on the bus.
1038 	 */
1039 	do {
1040 		status = wait_for_event(i2c_base, ip_rev, waitdelay);
1041 	} while (!(status & I2C_STAT_ARDY) && timeout--);
1042 	if (timeout <= 0) {
1043 		printf("%s: timed out on last byte!\n", __func__);
1044 		i2c_error = -EREMOTEIO;
1045 		goto xfer_exit;
1046 	} else {
1047 		omap_i2c_write_reg(i2c_base, ip_rev, I2C_STAT_ARDY, OMAP_I2C_STAT_REG);
1048 	}
1049 
1050 	/* If Stop bit set, flush FIFO. */
1051 	if (i2c_con_reg & I2C_CON_STP)
1052 		goto xfer_exit;
1053 
1054 	return 0;
1055 
1056 xfer_exit:
1057 	flush_fifo(i2c_base, ip_rev);
1058 	omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
1059 	return i2c_error;
1060 }
1061 
1062 static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
1063 {
1064 	struct omap_i2c *priv = dev_get_priv(bus);
1065 	int ret;
1066 	u16 i2c_con_reg = 0;
1067 
1068 	debug("%s: %d messages\n", __func__, nmsgs);
1069 	for (int i = 0; i < nmsgs; i++, msg++) {
1070 		/*
1071 		 * If previous msg sent a Stop or if this is the first msg
1072 		 * Wait until bus not busy
1073 		 */
1074 		if ((i2c_con_reg & I2C_CON_STP) || (i == 0))
1075 			if (wait_for_bb(priv->regs, priv->ip_rev, priv->waitdelay))
1076 				return -EREMOTEIO;
1077 
1078 		/* Set Controller mode with Start bit */
1079 		i2c_con_reg = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT;
1080 		/* Set Transmitter/Receiver mode if it is a write/read msg */
1081 		if (msg->flags & I2C_M_RD)
1082 			i2c_con_reg &= ~I2C_CON_TRX;
1083 		else
1084 			i2c_con_reg |= I2C_CON_TRX;
1085 		/* Send Stop condition (P) by default */
1086 		if (!IS_ENABLED(CONFIG_SYS_I2C_OMAP24XX_REPEATED_START))
1087 			i2c_con_reg |= I2C_CON_STP;
1088 		/* Send Stop if explicitly requested or if this is the last msg */
1089 		if ((msg->flags & I2C_M_STOP) || (i == nmsgs - 1))
1090 			i2c_con_reg |= I2C_CON_STP;
1091 
1092 		debug("%s: chip=0x%x, len=0x%x, i2c_con_reg=0x%x\n",
1093 		      __func__, msg->addr, msg->len, i2c_con_reg);
1094 
1095 		ret = __omap24_i2c_xfer_msg(priv->regs, priv->ip_rev, priv->waitdelay,
1096 					    msg->addr, msg->buf, msg->len,
1097 					    i2c_con_reg);
1098 		if (ret) {
1099 			printf("%s: errored out at msg %d: %d\n", __func__, i, ret);
1100 			return ret;
1101 		}
1102 	}
1103 
1104 	return 0;
1105 }
1106 
1107 static int omap_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
1108 {
1109 	struct omap_i2c *priv = dev_get_priv(bus);
1110 
1111 	priv->speed = speed;
1112 
1113 	return __omap24_i2c_setspeed(priv->regs, priv->ip_rev, speed,
1114 				     &priv->waitdelay);
1115 }
1116 
1117 static int omap_i2c_probe_chip(struct udevice *bus, uint chip_addr,
1118 				     uint chip_flags)
1119 {
1120 	struct omap_i2c *priv = dev_get_priv(bus);
1121 
1122 	return __omap24_i2c_probe(priv->regs, priv->ip_rev, priv->waitdelay,
1123 				  chip_addr) ? -EREMOTEIO : 0;
1124 }
1125 
1126 static int omap_i2c_probe(struct udevice *bus)
1127 {
1128 	struct omap_i2c *priv = dev_get_priv(bus);
1129 	struct omap_i2c_plat *plat = dev_get_plat(bus);
1130 
1131 	priv->speed = plat->speed;
1132 	priv->regs = map_physmem(plat->base, sizeof(void *),
1133 				 MAP_NOCACHE);
1134 	priv->ip_rev = plat->ip_rev;
1135 
1136 	__omap24_i2c_init(priv->regs, priv->ip_rev, priv->speed, 0,
1137 			  &priv->waitdelay);
1138 
1139 	return 0;
1140 }
1141 
1142 #if CONFIG_IS_ENABLED(OF_REAL)
1143 static int omap_i2c_of_to_plat(struct udevice *bus)
1144 {
1145 	struct omap_i2c_plat *plat = dev_get_plat(bus);
1146 
1147 	plat->base = dev_read_addr(bus);
1148 	plat->speed = dev_read_u32_default(bus, "clock-frequency",
1149 					   I2C_SPEED_STANDARD_RATE);
1150 	plat->ip_rev = dev_get_driver_data(bus);
1151 
1152 	return 0;
1153 }
1154 
1155 static const struct udevice_id omap_i2c_ids[] = {
1156 	{ .compatible = "ti,omap3-i2c", .data = OMAP_I2C_REV_V1 },
1157 	{ .compatible = "ti,omap4-i2c", .data = OMAP_I2C_REV_V2 },
1158 	{ }
1159 };
1160 #endif
1161 
1162 static const struct dm_i2c_ops omap_i2c_ops = {
1163 	.xfer		= omap_i2c_xfer,
1164 	.probe_chip	= omap_i2c_probe_chip,
1165 	.set_bus_speed	= omap_i2c_set_bus_speed,
1166 };
1167 
1168 U_BOOT_DRIVER(i2c_omap) = {
1169 	.name	= "i2c_omap",
1170 	.id	= UCLASS_I2C,
1171 #if CONFIG_IS_ENABLED(OF_REAL)
1172 	.of_match = omap_i2c_ids,
1173 	.of_to_plat = omap_i2c_of_to_plat,
1174 	.plat_auto	= sizeof(struct omap_i2c_plat),
1175 #endif
1176 	.probe	= omap_i2c_probe,
1177 	.priv_auto	= sizeof(struct omap_i2c),
1178 	.ops	= &omap_i2c_ops,
1179 #if !CONFIG_IS_ENABLED(OF_CONTROL)
1180 	.flags  = DM_FLAG_PRE_RELOC,
1181 #endif
1182 };
1183 
1184 #endif /* CONFIG_DM_I2C */
1185