3 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
4 * Changes for multibus/multiadapter I2C support.
6 * (C) Copyright 2001, 2002
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * SPDX-License-Identifier: GPL-2.0+
11 * This has been changed substantially by Gerald Van Baren, Custom IDEAS,
12 * vanbaren@cideas.com. It was heavily influenced by LiMon, written by
15 * NOTE: This driver should be converted to driver model before June 2017.
16 * Please see doc/driver-model/i2c-howto.txt for instructions.
20 #ifdef CONFIG_MPC8260 /* only valid for MPC8260 */
24 #if defined(CONFIG_AT91FAMILY)
26 #include <asm/arch/hardware.h>
27 #include <asm/arch/at91_pio.h>
28 #ifdef CONFIG_ATMEL_LEGACY
29 #include <asm/arch/gpio.h>
32 #if defined(CONFIG_MPC852T) || defined(CONFIG_MPC866)
37 #if defined(CONFIG_SOFT_I2C_GPIO_SCL)
38 # include <asm/gpio.h>
40 # ifndef I2C_GPIO_SYNC
41 # define I2C_GPIO_SYNC
47 gpio_request(CONFIG_SOFT_I2C_GPIO_SCL, "soft_i2c"); \
48 gpio_request(CONFIG_SOFT_I2C_GPIO_SDA, "soft_i2c"); \
53 # define I2C_ACTIVE do { } while (0)
57 # define I2C_TRISTATE do { } while (0)
61 # define I2C_READ gpio_get_value(CONFIG_SOFT_I2C_GPIO_SDA)
65 # define I2C_SDA(bit) \
68 gpio_direction_input(CONFIG_SOFT_I2C_GPIO_SDA); \
70 gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SDA, 0); \
76 # define I2C_SCL(bit) \
78 gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SCL, bit); \
84 # define I2C_DELAY udelay(5) /* 1/4 I2C clock duration */
89 /* #define DEBUG_I2C */
91 DECLARE_GLOBAL_DATA_PTR;
93 #ifndef I2C_SOFT_DECLARATIONS
94 # define I2C_SOFT_DECLARATIONS
97 #if !defined(CONFIG_SYS_I2C_SOFT_SPEED)
98 #define CONFIG_SYS_I2C_SOFT_SPEED CONFIG_SYS_I2C_SPEED
100 #if !defined(CONFIG_SYS_I2C_SOFT_SLAVE)
101 #define CONFIG_SYS_I2C_SOFT_SLAVE CONFIG_SYS_I2C_SLAVE
104 /*-----------------------------------------------------------------------
109 #define I2C_ACK 0 /* PD_SDA level to ack a byte */
110 #define I2C_NOACK 1 /* PD_SDA level to noack a byte */
114 #define PRINTD(fmt,args...) do { \
115 printf (fmt ,##args); \
118 #define PRINTD(fmt,args...)
121 /*-----------------------------------------------------------------------
124 #if !defined(CONFIG_SYS_I2C_INIT_BOARD)
125 static void send_reset (void);
127 static void send_start (void);
128 static void send_stop (void);
129 static void send_ack (int);
130 static int write_byte (uchar byte);
131 static uchar read_byte (int);
133 #if !defined(CONFIG_SYS_I2C_INIT_BOARD)
134 /*-----------------------------------------------------------------------
135 * Send a reset sequence consisting of 9 clocks with the data signal high
136 * to clock any confused device back into an idle state. Also send a
137 * <stop> at the end of the sequence for belts & suspenders.
139 static void send_reset(void)
141 I2C_SOFT_DECLARATIONS /* intentional without ';' */
150 for(j = 0; j < 9; j++) {
163 /*-----------------------------------------------------------------------
164 * START: High -> Low on SDA while SCL is High
166 static void send_start(void)
168 I2C_SOFT_DECLARATIONS /* intentional without ';' */
180 /*-----------------------------------------------------------------------
181 * STOP: Low -> High on SDA while SCL is High
183 static void send_stop(void)
185 I2C_SOFT_DECLARATIONS /* intentional without ';' */
199 /*-----------------------------------------------------------------------
200 * ack should be I2C_ACK or I2C_NOACK
202 static void send_ack(int ack)
204 I2C_SOFT_DECLARATIONS /* intentional without ';' */
218 /*-----------------------------------------------------------------------
219 * Send 8 bits and look for an acknowledgement.
221 static int write_byte(uchar data)
223 I2C_SOFT_DECLARATIONS /* intentional without ';' */
228 for(j = 0; j < 8; j++) {
231 I2C_SDA(data & 0x80);
241 * Look for an <ACK>(negative logic) and return it.
256 return(nack); /* not a nack is an ack */
259 /*-----------------------------------------------------------------------
260 * if ack == I2C_ACK, ACK the byte so can continue reading, else
261 * send I2C_NOACK to end the read.
263 static uchar read_byte(int ack)
265 I2C_SOFT_DECLARATIONS /* intentional without ';' */
270 * Read 8 bits, MSB first.
275 for(j = 0; j < 8; j++) {
289 /*-----------------------------------------------------------------------
292 static void soft_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
294 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
295 /* call board specific i2c bus reset routine before accessing the */
296 /* environment, which might be in a chip on that bus. For details */
297 /* about this problem see doc/I2C_Edge_Conditions. */
301 * WARNING: Do NOT save speed in a static variable: if the
302 * I2C routines are called before RAM is initialized (to read
303 * the DIMM SPD, for instance), RAM won't be usable and your
310 /*-----------------------------------------------------------------------
311 * Probe to see if a chip is present. Also good for checking for the
312 * completion of EEPROM writes since the chip stops responding until
313 * the write completes (typically 10mSec).
315 static int soft_i2c_probe(struct i2c_adapter *adap, uint8_t addr)
320 * perform 1 byte write transaction with just address byte
324 rc = write_byte ((addr << 1) | 0);
330 /*-----------------------------------------------------------------------
333 static int soft_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
334 int alen, uchar *buffer, int len)
337 PRINTD("i2c_read: chip %02X addr %02X alen %d buffer %p len %d\n",
338 chip, addr, alen, buffer, len);
340 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
342 * EEPROM chips that implement "address overflow" are ones
343 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
344 * address and the extra bits end up in the "chip address"
345 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
346 * four 256 byte chips.
348 * Note that we consider the length of the address field to
349 * still be one byte because the extra address bits are
350 * hidden in the chip address.
352 chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
354 PRINTD("i2c_read: fix addr_overflow: chip %02X addr %02X\n",
359 * Do the addressing portion of a write cycle to set the
360 * chip's address pointer. If the address length is zero,
361 * don't do the normal write cycle to set the address pointer,
362 * there is no address pointer in this chip.
366 if(write_byte(chip << 1)) { /* write cycle */
368 PRINTD("i2c_read, no chip responded %02X\n", chip);
371 shift = (alen-1) * 8;
373 if(write_byte(addr >> shift)) {
374 PRINTD("i2c_read, address not <ACK>ed\n");
380 /* Some I2C chips need a stop/start sequence here,
381 * other chips don't work with a full stop and need
382 * only a start. Default behaviour is to send the
383 * stop/start sequence.
385 #ifdef CONFIG_SOFT_I2C_READ_REPEATED_START
393 * Send the chip address again, this time for a read cycle.
394 * Then read the data. On the last byte, we do a NACK instead
395 * of an ACK(len == 0) to terminate the read.
397 write_byte((chip << 1) | 1); /* read cycle */
399 *buffer++ = read_byte(len == 0);
405 /*-----------------------------------------------------------------------
408 static int soft_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
409 int alen, uchar *buffer, int len)
411 int shift, failures = 0;
413 PRINTD("i2c_write: chip %02X addr %02X alen %d buffer %p len %d\n",
414 chip, addr, alen, buffer, len);
417 if(write_byte(chip << 1)) { /* write cycle */
419 PRINTD("i2c_write, no chip responded %02X\n", chip);
422 shift = (alen-1) * 8;
424 if(write_byte(addr >> shift)) {
425 PRINTD("i2c_write, address not <ACK>ed\n");
432 if(write_byte(*buffer++)) {
441 * Register soft i2c adapters
443 U_BOOT_I2C_ADAP_COMPLETE(soft00, soft_i2c_init, soft_i2c_probe,
444 soft_i2c_read, soft_i2c_write, NULL,
445 CONFIG_SYS_I2C_SOFT_SPEED, CONFIG_SYS_I2C_SOFT_SLAVE,
447 #if defined(I2C_SOFT_DECLARATIONS2)
448 U_BOOT_I2C_ADAP_COMPLETE(soft01, soft_i2c_init, soft_i2c_probe,
449 soft_i2c_read, soft_i2c_write, NULL,
450 CONFIG_SYS_I2C_SOFT_SPEED_2,
451 CONFIG_SYS_I2C_SOFT_SLAVE_2,
454 #if defined(I2C_SOFT_DECLARATIONS3)
455 U_BOOT_I2C_ADAP_COMPLETE(soft02, soft_i2c_init, soft_i2c_probe,
456 soft_i2c_read, soft_i2c_write, NULL,
457 CONFIG_SYS_I2C_SOFT_SPEED_3,
458 CONFIG_SYS_I2C_SOFT_SLAVE_3,
461 #if defined(I2C_SOFT_DECLARATIONS4)
462 U_BOOT_I2C_ADAP_COMPLETE(soft03, soft_i2c_init, soft_i2c_probe,
463 soft_i2c_read, soft_i2c_write, NULL,
464 CONFIG_SYS_I2C_SOFT_SPEED_4,
465 CONFIG_SYS_I2C_SOFT_SLAVE_4,
468 #if defined(I2C_SOFT_DECLARATIONS5)
469 U_BOOT_I2C_ADAP_COMPLETE(soft04, soft_i2c_init, soft_i2c_probe,
470 soft_i2c_read, soft_i2c_write, NULL,
471 CONFIG_SYS_I2C_SOFT_SPEED_5,
472 CONFIG_SYS_I2C_SOFT_SLAVE_5,
475 #if defined(I2C_SOFT_DECLARATIONS6)
476 U_BOOT_I2C_ADAP_COMPLETE(soft05, soft_i2c_init, soft_i2c_probe,
477 soft_i2c_read, soft_i2c_write, NULL,
478 CONFIG_SYS_I2C_SOFT_SPEED_6,
479 CONFIG_SYS_I2C_SOFT_SLAVE_6,
482 #if defined(I2C_SOFT_DECLARATIONS7)
483 U_BOOT_I2C_ADAP_COMPLETE(soft06, soft_i2c_init, soft_i2c_probe,
484 soft_i2c_read, soft_i2c_write, NULL,
485 CONFIG_SYS_I2C_SOFT_SPEED_7,
486 CONFIG_SYS_I2C_SOFT_SLAVE_7,
489 #if defined(I2C_SOFT_DECLARATIONS8)
490 U_BOOT_I2C_ADAP_COMPLETE(soft07, soft_i2c_init, soft_i2c_probe,
491 soft_i2c_read, soft_i2c_write, NULL,
492 CONFIG_SYS_I2C_SOFT_SPEED_8,
493 CONFIG_SYS_I2C_SOFT_SLAVE_8,
496 #if defined(I2C_SOFT_DECLARATIONS9)
497 U_BOOT_I2C_ADAP_COMPLETE(soft08, soft_i2c_init, soft_i2c_probe,
498 soft_i2c_read, soft_i2c_write, NULL,
499 CONFIG_SYS_I2C_SOFT_SPEED_9,
500 CONFIG_SYS_I2C_SOFT_SLAVE_9,
503 #if defined(I2C_SOFT_DECLARATIONS10)
504 U_BOOT_I2C_ADAP_COMPLETE(soft09, soft_i2c_init, soft_i2c_probe,
505 soft_i2c_read, soft_i2c_write, NULL,
506 CONFIG_SYS_I2C_SOFT_SPEED_10,
507 CONFIG_SYS_I2C_SOFT_SLAVE_10,
510 #if defined(I2C_SOFT_DECLARATIONS11)
511 U_BOOT_I2C_ADAP_COMPLETE(soft10, soft_i2c_init, soft_i2c_probe,
512 soft_i2c_read, soft_i2c_write, NULL,
513 CONFIG_SYS_I2C_SOFT_SPEED_11,
514 CONFIG_SYS_I2C_SOFT_SLAVE_11,
517 #if defined(I2C_SOFT_DECLARATIONS12)
518 U_BOOT_I2C_ADAP_COMPLETE(soft11, soft_i2c_init, soft_i2c_probe,
519 soft_i2c_read, soft_i2c_write, NULL,
520 CONFIG_SYS_I2C_SOFT_SPEED_12,
521 CONFIG_SYS_I2C_SOFT_SLAVE_12,