4 * Copyright (c) 2004 Texas Instruments
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.
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.
14 * Author: Jian Zhang jzhang@ti.com, Texas Instruments
16 * Copyright (c) 2003 Wolfgang Denk, wd@denx.de
17 * Rewritten to fit into the current U-Boot framework
19 * Adapted for OMAP2420 I2C, r-woodruff2@ti.com
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).
36 * Copyright (c) 2014 Hannes Schmelzer <oe5hpm@oevsv.at>, B&R
37 * - Added support for set_speed
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
53 #ifndef CONFIG_ARCH_K3
54 #include <asm/arch/i2c.h>
57 #include "omap24xx_i2c.h"
59 #define I2C_TIMEOUT 1000
61 /* Absolutely safe for status update at 100 kHz I2C: */
70 OMAP_I2C_REV_REG = 0, /* Only on IP V1 (OMAP34XX) */
71 OMAP_I2C_IE_REG, /* Only on IP V1 (OMAP34XX) */
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,
95 static 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,
115 static 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,
147 static inline const u8 *omap_i2c_get_ip_reg_map(int ip_rev)
150 case OMAP_I2C_REV_V1:
151 return reg_map_ip_v1;
152 case OMAP_I2C_REV_V2:
153 /* Fall through... */
155 return reg_map_ip_v2;
159 static inline void omap_i2c_write_reg(void __iomem *base, int ip_rev,
162 writew(val, base + omap_i2c_get_ip_reg_map(ip_rev)[reg]);
165 static inline u16 omap_i2c_read_reg(void __iomem *base, int ip_rev, int reg)
167 return readw(base + omap_i2c_get_ip_reg_map(ip_rev)[reg]);
170 static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
172 unsigned long internal_clk = 0, fclk;
173 unsigned int prescaler;
176 * This method is only called for Standard and Fast Mode speeds
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)).
183 * Such approach is used in v4.9 Linux kernel in:
184 * ./drivers/i2c/busses/i2c-omap.c (omap_i2c_init function).
187 speed /= 1000; /* convert speed to kHz */
194 fclk = I2C_IP_CLK / 1000;
195 prescaler = fclk / internal_clk;
196 prescaler = prescaler - 1;
202 scl = internal_clk / speed;
203 *pscl = scl - (scl / 3) - I2C_FASTSPEED_SCLL_TRIM;
204 *psch = (scl / 3) - I2C_FASTSPEED_SCLH_TRIM;
207 *pscl = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLL_TRIM;
208 *psch = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLH_TRIM;
211 debug("%s: speed [kHz]: %d psc: 0x%x sscl: 0x%x ssch: 0x%x\n",
212 __func__, speed, prescaler, *pscl, *psch);
214 if (*pscl <= 0 || *psch <= 0 || prescaler <= 0)
221 * Wait for the bus to be free by checking the Bus Busy (BB)
222 * bit to become clear
224 static int wait_for_bb(void __iomem *i2c_base, int ip_rev, int waitdelay)
226 int timeout = I2C_TIMEOUT;
230 irq_stat_reg = (ip_rev == OMAP_I2C_REV_V1) ?
231 OMAP_I2C_STAT_REG : OMAP_I2C_IP_V2_IRQSTATUS_RAW;
233 /* clear current interrupts */
234 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
236 while ((stat = omap_i2c_read_reg(i2c_base, ip_rev, irq_stat_reg) &
237 I2C_STAT_BB) && timeout--) {
238 omap_i2c_write_reg(i2c_base, ip_rev, stat, OMAP_I2C_STAT_REG);
243 printf("Timed out in %s: status=%04x\n", __func__, stat);
247 /* clear delayed stuff */
248 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
253 * Wait for the I2C controller to complete current action
256 static u16 wait_for_event(void __iomem *i2c_base, int ip_rev, int waitdelay)
259 int timeout = I2C_TIMEOUT;
262 irq_stat_reg = (ip_rev == OMAP_I2C_REV_V1) ?
263 OMAP_I2C_STAT_REG : OMAP_I2C_IP_V2_IRQSTATUS_RAW;
266 status = omap_i2c_read_reg(i2c_base, ip_rev, irq_stat_reg);
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--);
273 printf("Timed out in %s: status=%04x\n", __func__, status);
275 * If status is still 0 here, probably the bus pads have
276 * not been configured for I2C, and/or pull-ups are missing.
278 printf("Check if pads/pull-ups of bus are properly configured\n");
279 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
286 static void flush_fifo(void __iomem *i2c_base, int ip_rev)
291 * note: if you try and read data when its not there or ready
292 * you get a bus error
295 stat = omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_STAT_REG);
296 if (stat == I2C_STAT_RRDY) {
297 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);
306 static int __omap24_i2c_setspeed(void __iomem *i2c_base, int ip_rev, uint speed,
309 int psc, fsscll = 0, fssclh = 0;
310 int hsscll = 0, hssclh = 0;
311 u32 scll = 0, sclh = 0;
313 if (speed >= OMAP_I2C_HIGH_SPEED) {
315 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
317 if (psc < I2C_PSC_MIN) {
318 printf("Error : I2C unsupported prescaler %d\n", psc);
322 /* For first phase of HS mode */
323 fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
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))) {
331 puts("Error : I2C initializing first phase clock\n");
335 /* For second phase of HS mode */
336 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
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))) {
342 puts("Error : I2C initializing second phase clock\n");
346 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
347 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
350 /* Standard and fast speed */
351 psc = omap24_i2c_findpsc(&scll, &sclh, speed);
353 puts("Error : I2C initializing clock\n");
358 /* wait for 20 clkperiods */
359 *waitdelay = (10000000 / speed) * 2;
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);
367 /* clear all pending status */
368 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
373 static void omap24_i2c_deblock(void __iomem *i2c_base, int ip_rev)
379 /* set test mode ST_EN = 1 */
380 orgsystest = omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_SYSTEST_REG);
381 systest = orgsystest;
383 /* enable testmode */
384 systest |= I2C_SYSTEST_ST_EN;
385 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
386 systest &= ~I2C_SYSTEST_TMODE_MASK;
387 systest |= 3 << I2C_SYSTEST_TMODE_SHIFT;
388 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
390 /* set SCL, SDA = 1 */
391 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
392 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
395 /* toggle scl 9 clocks */
396 for (i = 0; i < 9; i++) {
398 systest &= ~I2C_SYSTEST_SCL_O;
399 omap_i2c_write_reg(i2c_base, ip_rev,
400 systest, OMAP_I2C_SYSTEST_REG);
403 systest |= I2C_SYSTEST_SCL_O;
404 omap_i2c_write_reg(i2c_base, ip_rev,
405 systest, OMAP_I2C_SYSTEST_REG);
410 systest &= ~I2C_SYSTEST_SDA_O;
411 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
413 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
414 omap_i2c_write_reg(i2c_base, ip_rev, systest, OMAP_I2C_SYSTEST_REG);
417 /* restore original mode */
418 omap_i2c_write_reg(i2c_base, ip_rev, orgsystest, OMAP_I2C_SYSTEST_REG);
421 static void __omap24_i2c_init(void __iomem *i2c_base, int ip_rev, int speed,
422 int slaveadd, int *waitdelay)
424 int timeout = I2C_TIMEOUT;
428 if (omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_CON_REG) &
430 omap_i2c_write_reg(i2c_base, ip_rev, 0, OMAP_I2C_CON_REG);
434 /* for ES2 after soft reset */
435 omap_i2c_write_reg(i2c_base, ip_rev, 0x2, OMAP_I2C_SYSC_REG);
438 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--) {
442 puts("ERROR: Timeout in soft-reset\n");
448 if (__omap24_i2c_setspeed(i2c_base, ip_rev, speed, waitdelay)) {
449 printf("ERROR: failed to setup I2C bus-speed!\n");
454 omap_i2c_write_reg(i2c_base, ip_rev, slaveadd, OMAP_I2C_OA_REG);
456 if (ip_rev == OMAP_I2C_REV_V1) {
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'
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,
468 flush_fifo(i2c_base, ip_rev);
469 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
471 /* Handle possible failed I2C state */
472 if (wait_for_bb(i2c_base, ip_rev, *waitdelay))
474 omap24_i2c_deblock(i2c_base, ip_rev);
481 * i2c_probe: Use write access. Allows to identify addresses that are
482 * write-only (like the config register of dual-port EEPROMs)
484 static int __omap24_i2c_probe(void __iomem *i2c_base, int ip_rev, int waitdelay,
488 int res = 1; /* default = fail */
490 if (chip == omap_i2c_read_reg(i2c_base, ip_rev, OMAP_I2C_OA_REG))
493 /* Wait until bus is free */
494 if (wait_for_bb(i2c_base, ip_rev, waitdelay))
497 /* No data transfer, slave addr only */
498 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
500 /* 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,
505 status = wait_for_event(i2c_base, ip_rev, waitdelay);
507 if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
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:
514 if (status == I2C_STAT_XRDY)
515 printf("i2c_probe: pads on bus probably not configured (status=0x%x)\n",
521 /* Check for ACK (!NAK) */
522 if (!(status & I2C_STAT_NACK)) {
523 res = 0; /* Device found */
524 udelay(waitdelay);/* Required by AM335X in SPL */
525 /* Abort transfer (force idle state) */
526 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_MST | I2C_CON_TRX,
527 OMAP_I2C_CON_REG); /* Reset */
529 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 */
535 flush_fifo(i2c_base, ip_rev);
536 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
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
553 static int __omap24_i2c_read(void __iomem *i2c_base, int ip_rev, int waitdelay,
554 uchar chip, uint addr, int alen, uchar *buffer,
561 puts("I2C read: addr len < 0\n");
566 puts("I2C read: data len < 0\n");
570 if (buffer == NULL) {
571 puts("I2C read: NULL pointer passed\n");
576 printf("I2C read: addr len %d not supported\n", alen);
580 if (addr + len > (1 << 16)) {
581 puts("I2C read: address out of range\n");
585 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
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.
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.
598 chip |= ((addr >> (alen * 8)) &
599 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
602 /* Wait until bus not busy */
603 if (wait_for_bb(i2c_base, ip_rev, waitdelay))
606 /* Zero, one or two bytes reg address (offset) */
607 omap_i2c_write_reg(i2c_base, ip_rev, alen, OMAP_I2C_CNT_REG);
608 /* Set slave address */
609 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
612 /* Must write reg offset first */
613 #ifdef CONFIG_I2C_REPEATED_START
614 /* No stop bit, use Repeated Start (Sr) */
615 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);
618 /* Stop - Start (P-S) */
619 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
620 I2C_CON_STT | I2C_CON_STP | I2C_CON_TRX,
623 /* Send register offset */
625 status = wait_for_event(i2c_base, ip_rev, waitdelay);
626 /* Try to identify bus that is not padconf'd for I2C */
627 if (status == I2C_STAT_XRDY) {
629 printf("i2c_read (addr phase): pads on bus probably not configured (status=0x%x)\n",
633 if (status == 0 || (status & I2C_STAT_NACK)) {
635 printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
640 if (status & I2C_STAT_XRDY) {
643 addr_byte = (addr >> (8 * alen)) & 0xff;
644 omap_i2c_write_reg(i2c_base, ip_rev,
647 omap_i2c_write_reg(i2c_base, ip_rev,
652 if (status & I2C_STAT_ARDY) {
653 omap_i2c_write_reg(i2c_base, ip_rev,
661 /* Set slave address */
662 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
663 /* Read len bytes from slave */
664 omap_i2c_write_reg(i2c_base, ip_rev, len, OMAP_I2C_CNT_REG);
665 /* Need stop bit here */
666 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);
671 status = wait_for_event(i2c_base, ip_rev, waitdelay);
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.
677 if (status == I2C_STAT_XRDY) {
679 printf("i2c_read (data phase): pads on bus probably not configured (status=0x%x)\n",
683 if (status == 0 || (status & I2C_STAT_NACK)) {
687 if (status & I2C_STAT_RRDY) {
688 *buffer++ = omap_i2c_read_reg(i2c_base, ip_rev,
690 omap_i2c_write_reg(i2c_base, ip_rev,
691 I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
693 if (status & I2C_STAT_ARDY) {
694 omap_i2c_write_reg(i2c_base, ip_rev,
695 I2C_STAT_ARDY, OMAP_I2C_STAT_REG);
701 flush_fifo(i2c_base, ip_rev);
702 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
706 /* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
707 static int __omap24_i2c_write(void __iomem *i2c_base, int ip_rev, int waitdelay,
708 uchar chip, uint addr, int alen, uchar *buffer,
714 int timeout = I2C_TIMEOUT;
717 puts("I2C write: addr len < 0\n");
722 puts("I2C write: data len < 0\n");
726 if (buffer == NULL) {
727 puts("I2C write: NULL pointer passed\n");
732 printf("I2C write: addr len %d not supported\n", alen);
736 if (addr + len > (1 << 16)) {
737 printf("I2C write: address 0x%x + 0x%x out of range\n",
742 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
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.
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.
755 chip |= ((addr >> (alen * 8)) &
756 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
759 /* Wait until bus not busy */
760 if (wait_for_bb(i2c_base, ip_rev, waitdelay))
763 /* Start address phase - will write regoffset + len bytes data */
764 omap_i2c_write_reg(i2c_base, ip_rev, alen + len, OMAP_I2C_CNT_REG);
765 /* Set slave address */
766 omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
767 /* Stop bit needed here */
768 omap_i2c_write_reg(i2c_base, ip_rev, I2C_CON_EN | I2C_CON_MST |
769 I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP,
773 /* Must write reg offset (one or two bytes) */
774 status = wait_for_event(i2c_base, ip_rev, waitdelay);
775 /* Try to identify bus that is not padconf'd for I2C */
776 if (status == I2C_STAT_XRDY) {
778 printf("i2c_write: pads on bus probably not configured (status=0x%x)\n",
782 if (status == 0 || (status & I2C_STAT_NACK)) {
784 printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
788 if (status & I2C_STAT_XRDY) {
790 omap_i2c_write_reg(i2c_base, ip_rev,
791 (addr >> (8 * alen)) & 0xff,
793 omap_i2c_write_reg(i2c_base, ip_rev,
794 I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
797 printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
803 /* Address phase is over, now write data */
804 for (i = 0; i < len; i++) {
805 status = wait_for_event(i2c_base, ip_rev, waitdelay);
806 if (status == 0 || (status & I2C_STAT_NACK)) {
808 printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
812 if (status & I2C_STAT_XRDY) {
813 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);
819 printf("i2c_write: bus not ready for data Tx (i=%d)\n",
826 * poll ARDY bit for making sure that last byte really has been
827 * transferred on the bus.
830 status = wait_for_event(i2c_base, ip_rev, waitdelay);
831 } while (!(status & I2C_STAT_ARDY) && timeout--);
833 printf("i2c_write: timed out writig last byte!\n");
836 flush_fifo(i2c_base, ip_rev);
837 omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
841 #ifndef CONFIG_DM_I2C
843 * The legacy I2C functions. These need to get removed once
844 * all users of this driver are converted to DM.
846 static void __iomem *omap24_get_base(struct i2c_adapter *adap)
848 switch (adap->hwadapnr) {
850 return (void __iomem *)I2C_BASE1;
853 return (void __iomem *)I2C_BASE2;
855 #if (CONFIG_SYS_I2C_BUS_MAX > 2)
857 return (void __iomem *)I2C_BASE3;
859 #if (CONFIG_SYS_I2C_BUS_MAX > 3)
861 return (void __iomem *)I2C_BASE4;
863 #if (CONFIG_SYS_I2C_BUS_MAX > 4)
865 return (void __iomem *)I2C_BASE5;
871 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
878 static int omap24_get_ip_rev(void)
880 #ifdef CONFIG_OMAP34XX
881 return OMAP_I2C_REV_V1;
883 return OMAP_I2C_REV_V2;
887 static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
888 int alen, uchar *buffer, int len)
890 void __iomem *i2c_base = omap24_get_base(adap);
891 int ip_rev = omap24_get_ip_rev();
893 return __omap24_i2c_read(i2c_base, ip_rev, adap->waitdelay, chip, addr,
897 static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
898 int alen, uchar *buffer, int len)
900 void __iomem *i2c_base = omap24_get_base(adap);
901 int ip_rev = omap24_get_ip_rev();
903 return __omap24_i2c_write(i2c_base, ip_rev, adap->waitdelay, chip, addr,
907 static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
909 void __iomem *i2c_base = omap24_get_base(adap);
910 int ip_rev = omap24_get_ip_rev();
913 ret = __omap24_i2c_setspeed(i2c_base, ip_rev, speed, &adap->waitdelay);
915 pr_err("%s: set i2c speed failed\n", __func__);
924 static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
926 void __iomem *i2c_base = omap24_get_base(adap);
927 int ip_rev = omap24_get_ip_rev();
929 return __omap24_i2c_init(i2c_base, ip_rev, speed, slaveadd,
933 static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
935 void __iomem *i2c_base = omap24_get_base(adap);
936 int ip_rev = omap24_get_ip_rev();
938 return __omap24_i2c_probe(i2c_base, ip_rev, adap->waitdelay, chip);
941 #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
942 #define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
944 #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
945 #define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
948 U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
949 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
950 CONFIG_SYS_OMAP24_I2C_SPEED,
951 CONFIG_SYS_OMAP24_I2C_SLAVE,
953 U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
954 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
955 CONFIG_SYS_OMAP24_I2C_SPEED1,
956 CONFIG_SYS_OMAP24_I2C_SLAVE1,
959 #if (CONFIG_SYS_I2C_BUS_MAX > 2)
960 #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
961 #define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
963 #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
964 #define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
967 U_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,
972 #if (CONFIG_SYS_I2C_BUS_MAX > 3)
973 #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
974 #define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
976 #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
977 #define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
980 U_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,
985 #if (CONFIG_SYS_I2C_BUS_MAX > 4)
986 #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
987 #define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
989 #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
990 #define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
993 U_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,
1002 #else /* CONFIG_DM_I2C */
1004 static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
1006 struct omap_i2c *priv = dev_get_priv(bus);
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) {
1013 ret = __omap24_i2c_read(priv->regs, priv->ip_rev,
1015 msg->addr, 0, 0, msg->buf,
1018 ret = __omap24_i2c_write(priv->regs, priv->ip_rev,
1020 msg->addr, 0, 0, msg->buf,
1024 debug("i2c_write: error sending\n");
1032 static int omap_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
1034 struct omap_i2c *priv = dev_get_priv(bus);
1036 priv->speed = speed;
1038 return __omap24_i2c_setspeed(priv->regs, priv->ip_rev, speed,
1042 static int omap_i2c_probe_chip(struct udevice *bus, uint chip_addr,
1045 struct omap_i2c *priv = dev_get_priv(bus);
1047 return __omap24_i2c_probe(priv->regs, priv->ip_rev, priv->waitdelay,
1051 static int omap_i2c_probe(struct udevice *bus)
1053 struct omap_i2c *priv = dev_get_priv(bus);
1055 priv->ip_rev = dev_get_driver_data(bus);
1057 __omap24_i2c_init(priv->regs, priv->ip_rev, priv->speed, 0,
1063 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1064 static int omap_i2c_ofdata_to_platdata(struct udevice *bus)
1066 struct omap_i2c *priv = dev_get_priv(bus);
1068 priv->regs = map_physmem(devfdt_get_addr(bus), sizeof(void *),
1070 priv->speed = CONFIG_SYS_OMAP24_I2C_SPEED;
1075 static const struct udevice_id omap_i2c_ids[] = {
1076 { .compatible = "ti,omap3-i2c", .data = OMAP_I2C_REV_V1 },
1077 { .compatible = "ti,omap4-i2c", .data = OMAP_I2C_REV_V2 },
1082 static 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,
1088 U_BOOT_DRIVER(i2c_omap) = {
1091 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1092 .of_match = omap_i2c_ids,
1093 .ofdata_to_platdata = omap_i2c_ofdata_to_platdata,
1095 .probe = omap_i2c_probe,
1096 .priv_auto_alloc_size = sizeof(struct omap_i2c),
1097 .ops = &omap_i2c_ops,
1098 #if !CONFIG_IS_ENABLED(OF_CONTROL)
1099 .flags = DM_FLAG_PRE_RELOC,
1103 #endif /* CONFIG_DM_I2C */