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 #if defined(CONFIG_AVR32)
21 #include <asm/arch/portmux.h>
23 #if defined(CONFIG_AT91FAMILY)
25 #include <asm/arch/hardware.h>
26 #include <asm/arch/at91_pio.h>
27 #ifdef CONFIG_ATMEL_LEGACY
28 #include <asm/arch/gpio.h>
31 #if defined(CONFIG_MPC852T) || defined(CONFIG_MPC866)
36 #if defined(CONFIG_SOFT_I2C_GPIO_SCL)
37 # include <asm/gpio.h>
39 # ifndef I2C_GPIO_SYNC
40 # define I2C_GPIO_SYNC
46 gpio_request(CONFIG_SOFT_I2C_GPIO_SCL, "soft_i2c"); \
47 gpio_request(CONFIG_SOFT_I2C_GPIO_SDA, "soft_i2c"); \
52 # define I2C_ACTIVE do { } while (0)
56 # define I2C_TRISTATE do { } while (0)
60 # define I2C_READ gpio_get_value(CONFIG_SOFT_I2C_GPIO_SDA)
64 # define I2C_SDA(bit) \
67 gpio_direction_input(CONFIG_SOFT_I2C_GPIO_SDA); \
69 gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SDA, 0); \
75 # define I2C_SCL(bit) \
77 gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SCL, bit); \
83 # define I2C_DELAY udelay(5) /* 1/4 I2C clock duration */
88 /* #define DEBUG_I2C */
90 DECLARE_GLOBAL_DATA_PTR;
92 #ifndef I2C_SOFT_DECLARATIONS
93 # define I2C_SOFT_DECLARATIONS
96 #if !defined(CONFIG_SYS_I2C_SOFT_SPEED)
97 #define CONFIG_SYS_I2C_SOFT_SPEED CONFIG_SYS_I2C_SPEED
99 #if !defined(CONFIG_SYS_I2C_SOFT_SLAVE)
100 #define CONFIG_SYS_I2C_SOFT_SLAVE CONFIG_SYS_I2C_SLAVE
103 /*-----------------------------------------------------------------------
108 #define I2C_ACK 0 /* PD_SDA level to ack a byte */
109 #define I2C_NOACK 1 /* PD_SDA level to noack a byte */
113 #define PRINTD(fmt,args...) do { \
114 printf (fmt ,##args); \
117 #define PRINTD(fmt,args...)
120 /*-----------------------------------------------------------------------
123 #if !defined(CONFIG_SYS_I2C_INIT_BOARD)
124 static void send_reset (void);
126 static void send_start (void);
127 static void send_stop (void);
128 static void send_ack (int);
129 static int write_byte (uchar byte);
130 static uchar read_byte (int);
132 #if !defined(CONFIG_SYS_I2C_INIT_BOARD)
133 /*-----------------------------------------------------------------------
134 * Send a reset sequence consisting of 9 clocks with the data signal high
135 * to clock any confused device back into an idle state. Also send a
136 * <stop> at the end of the sequence for belts & suspenders.
138 static void send_reset(void)
140 I2C_SOFT_DECLARATIONS /* intentional without ';' */
149 for(j = 0; j < 9; j++) {
162 /*-----------------------------------------------------------------------
163 * START: High -> Low on SDA while SCL is High
165 static void send_start(void)
167 I2C_SOFT_DECLARATIONS /* intentional without ';' */
179 /*-----------------------------------------------------------------------
180 * STOP: Low -> High on SDA while SCL is High
182 static void send_stop(void)
184 I2C_SOFT_DECLARATIONS /* intentional without ';' */
198 /*-----------------------------------------------------------------------
199 * ack should be I2C_ACK or I2C_NOACK
201 static void send_ack(int ack)
203 I2C_SOFT_DECLARATIONS /* intentional without ';' */
217 /*-----------------------------------------------------------------------
218 * Send 8 bits and look for an acknowledgement.
220 static int write_byte(uchar data)
222 I2C_SOFT_DECLARATIONS /* intentional without ';' */
227 for(j = 0; j < 8; j++) {
230 I2C_SDA(data & 0x80);
240 * Look for an <ACK>(negative logic) and return it.
255 return(nack); /* not a nack is an ack */
258 /*-----------------------------------------------------------------------
259 * if ack == I2C_ACK, ACK the byte so can continue reading, else
260 * send I2C_NOACK to end the read.
262 static uchar read_byte(int ack)
264 I2C_SOFT_DECLARATIONS /* intentional without ';' */
269 * Read 8 bits, MSB first.
274 for(j = 0; j < 8; j++) {
288 /*-----------------------------------------------------------------------
291 static void soft_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
293 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
294 /* call board specific i2c bus reset routine before accessing the */
295 /* environment, which might be in a chip on that bus. For details */
296 /* about this problem see doc/I2C_Edge_Conditions. */
300 * WARNING: Do NOT save speed in a static variable: if the
301 * I2C routines are called before RAM is initialized (to read
302 * the DIMM SPD, for instance), RAM won't be usable and your
309 /*-----------------------------------------------------------------------
310 * Probe to see if a chip is present. Also good for checking for the
311 * completion of EEPROM writes since the chip stops responding until
312 * the write completes (typically 10mSec).
314 static int soft_i2c_probe(struct i2c_adapter *adap, uint8_t addr)
319 * perform 1 byte write transaction with just address byte
323 rc = write_byte ((addr << 1) | 0);
329 /*-----------------------------------------------------------------------
332 static int soft_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
333 int alen, uchar *buffer, int len)
336 PRINTD("i2c_read: chip %02X addr %02X alen %d buffer %p len %d\n",
337 chip, addr, alen, buffer, len);
339 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
341 * EEPROM chips that implement "address overflow" are ones
342 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
343 * address and the extra bits end up in the "chip address"
344 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
345 * four 256 byte chips.
347 * Note that we consider the length of the address field to
348 * still be one byte because the extra address bits are
349 * hidden in the chip address.
351 chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
353 PRINTD("i2c_read: fix addr_overflow: chip %02X addr %02X\n",
358 * Do the addressing portion of a write cycle to set the
359 * chip's address pointer. If the address length is zero,
360 * don't do the normal write cycle to set the address pointer,
361 * there is no address pointer in this chip.
365 if(write_byte(chip << 1)) { /* write cycle */
367 PRINTD("i2c_read, no chip responded %02X\n", chip);
370 shift = (alen-1) * 8;
372 if(write_byte(addr >> shift)) {
373 PRINTD("i2c_read, address not <ACK>ed\n");
379 /* Some I2C chips need a stop/start sequence here,
380 * other chips don't work with a full stop and need
381 * only a start. Default behaviour is to send the
382 * stop/start sequence.
384 #ifdef CONFIG_SOFT_I2C_READ_REPEATED_START
392 * Send the chip address again, this time for a read cycle.
393 * Then read the data. On the last byte, we do a NACK instead
394 * of an ACK(len == 0) to terminate the read.
396 write_byte((chip << 1) | 1); /* read cycle */
398 *buffer++ = read_byte(len == 0);
404 /*-----------------------------------------------------------------------
407 static int soft_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
408 int alen, uchar *buffer, int len)
410 int shift, failures = 0;
412 PRINTD("i2c_write: chip %02X addr %02X alen %d buffer %p len %d\n",
413 chip, addr, alen, buffer, len);
416 if(write_byte(chip << 1)) { /* write cycle */
418 PRINTD("i2c_write, no chip responded %02X\n", chip);
421 shift = (alen-1) * 8;
423 if(write_byte(addr >> shift)) {
424 PRINTD("i2c_write, address not <ACK>ed\n");
431 if(write_byte(*buffer++)) {
440 * Register soft i2c adapters
442 U_BOOT_I2C_ADAP_COMPLETE(soft00, soft_i2c_init, soft_i2c_probe,
443 soft_i2c_read, soft_i2c_write, NULL,
444 CONFIG_SYS_I2C_SOFT_SPEED, CONFIG_SYS_I2C_SOFT_SLAVE,
446 #if defined(I2C_SOFT_DECLARATIONS2)
447 U_BOOT_I2C_ADAP_COMPLETE(soft01, soft_i2c_init, soft_i2c_probe,
448 soft_i2c_read, soft_i2c_write, NULL,
449 CONFIG_SYS_I2C_SOFT_SPEED_2,
450 CONFIG_SYS_I2C_SOFT_SLAVE_2,
453 #if defined(I2C_SOFT_DECLARATIONS3)
454 U_BOOT_I2C_ADAP_COMPLETE(soft02, soft_i2c_init, soft_i2c_probe,
455 soft_i2c_read, soft_i2c_write, NULL,
456 CONFIG_SYS_I2C_SOFT_SPEED_3,
457 CONFIG_SYS_I2C_SOFT_SLAVE_3,
460 #if defined(I2C_SOFT_DECLARATIONS4)
461 U_BOOT_I2C_ADAP_COMPLETE(soft03, soft_i2c_init, soft_i2c_probe,
462 soft_i2c_read, soft_i2c_write, NULL,
463 CONFIG_SYS_I2C_SOFT_SPEED_4,
464 CONFIG_SYS_I2C_SOFT_SLAVE_4,
467 #if defined(I2C_SOFT_DECLARATIONS5)
468 U_BOOT_I2C_ADAP_COMPLETE(soft04, soft_i2c_init, soft_i2c_probe,
469 soft_i2c_read, soft_i2c_write, NULL,
470 CONFIG_SYS_I2C_SOFT_SPEED_5,
471 CONFIG_SYS_I2C_SOFT_SLAVE_5,
474 #if defined(I2C_SOFT_DECLARATIONS6)
475 U_BOOT_I2C_ADAP_COMPLETE(soft05, soft_i2c_init, soft_i2c_probe,
476 soft_i2c_read, soft_i2c_write, NULL,
477 CONFIG_SYS_I2C_SOFT_SPEED_6,
478 CONFIG_SYS_I2C_SOFT_SLAVE_6,
481 #if defined(I2C_SOFT_DECLARATIONS7)
482 U_BOOT_I2C_ADAP_COMPLETE(soft06, soft_i2c_init, soft_i2c_probe,
483 soft_i2c_read, soft_i2c_write, NULL,
484 CONFIG_SYS_I2C_SOFT_SPEED_7,
485 CONFIG_SYS_I2C_SOFT_SLAVE_7,
488 #if defined(I2C_SOFT_DECLARATIONS8)
489 U_BOOT_I2C_ADAP_COMPLETE(soft07, soft_i2c_init, soft_i2c_probe,
490 soft_i2c_read, soft_i2c_write, NULL,
491 CONFIG_SYS_I2C_SOFT_SPEED_8,
492 CONFIG_SYS_I2C_SOFT_SLAVE_8,
495 #if defined(I2C_SOFT_DECLARATIONS9)
496 U_BOOT_I2C_ADAP_COMPLETE(soft08, soft_i2c_init, soft_i2c_probe,
497 soft_i2c_read, soft_i2c_write, NULL,
498 CONFIG_SYS_I2C_SOFT_SPEED_9,
499 CONFIG_SYS_I2C_SOFT_SLAVE_9,
502 #if defined(I2C_SOFT_DECLARATIONS10)
503 U_BOOT_I2C_ADAP_COMPLETE(soft09, soft_i2c_init, soft_i2c_probe,
504 soft_i2c_read, soft_i2c_write, NULL,
505 CONFIG_SYS_I2C_SOFT_SPEED_10,
506 CONFIG_SYS_I2C_SOFT_SLAVE_10,
509 #if defined(I2C_SOFT_DECLARATIONS11)
510 U_BOOT_I2C_ADAP_COMPLETE(soft10, soft_i2c_init, soft_i2c_probe,
511 soft_i2c_read, soft_i2c_write, NULL,
512 CONFIG_SYS_I2C_SOFT_SPEED_11,
513 CONFIG_SYS_I2C_SOFT_SLAVE_11,
516 #if defined(I2C_SOFT_DECLARATIONS12)
517 U_BOOT_I2C_ADAP_COMPLETE(soft11, soft_i2c_init, soft_i2c_probe,
518 soft_i2c_read, soft_i2c_write, NULL,
519 CONFIG_SYS_I2C_SOFT_SPEED_12,
520 CONFIG_SYS_I2C_SOFT_SLAVE_12,