3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
5 * SPDX-License-Identifier: GPL-2.0+
7 * The original I2C interface was
8 * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
9 * AIRVENT SAM s.p.a - RIMINI(ITALY)
10 * but has been changed substantially.
17 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
19 * The implementation MUST NOT use static or global variables if the
20 * I2C routines are used to read SDRAM configuration information
21 * because this is done before the memories are initialized. Limited
22 * use of stack-based variables are OK (the initial stack size is
25 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
29 * Configuration items.
31 #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
33 #ifdef CONFIG_I2C_MULTI_BUS
35 #define I2C_MULTI_BUS 1
38 #define I2C_MULTI_BUS 0
41 #if !defined(CONFIG_SYS_MAX_I2C_BUS)
42 #define CONFIG_SYS_MAX_I2C_BUS MAX_I2C_BUS
45 /* define the I2C bus number for RTC and DTT if not already done */
46 #if !defined(CONFIG_SYS_RTC_BUS_NUM)
47 #define CONFIG_SYS_RTC_BUS_NUM 0
49 #if !defined(CONFIG_SYS_DTT_BUS_NUM)
50 #define CONFIG_SYS_DTT_BUS_NUM 0
52 #if !defined(CONFIG_SYS_SPD_BUS_NUM)
53 #define CONFIG_SYS_SPD_BUS_NUM 0
56 #ifndef I2C_SOFT_DECLARATIONS
57 # if defined(CONFIG_MPC8260)
58 # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
59 # elif defined(CONFIG_8xx)
60 # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
62 # elif (defined(CONFIG_AT91RM9200) || \
63 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
64 defined(CONFIG_AT91SAM9263)) && !defined(CONFIG_AT91_LEGACY)
65 # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
67 # define I2C_SOFT_DECLARATIONS
72 /* Set default value for the I2C bus speed on 8xx. In the
73 * future, we'll define these in all 8xx board config files.
75 #ifndef CONFIG_SYS_I2C_SPEED
76 #define CONFIG_SYS_I2C_SPEED 50000
81 * Many boards/controllers/drivers don't support an I2C slave interface so
82 * provide a default slave address for them for use in common code. A real
83 * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
84 * support a slave interface.
86 #ifndef CONFIG_SYS_I2C_SLAVE
87 #define CONFIG_SYS_I2C_SLAVE 0xfe
91 * Initialization, must be called once on start up, may be called
92 * repeatedly to change the speed and slave addresses.
94 void i2c_init(int speed, int slaveaddr);
95 void i2c_init_board(void);
96 #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
97 void i2c_board_late_init(void);
100 #if defined(CONFIG_I2C_MUX)
102 typedef struct _mux {
109 typedef struct _mux_device {
111 I2C_MUX *mux; /* List of muxes, to reach the device */
112 struct _mux_device *next;
115 I2C_MUX_DEVICE *i2c_mux_search_device(int id);
116 I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf);
117 int i2x_mux_select_mux(int bus);
118 int i2c_mux_ident_muxstring_f (uchar *buf);
122 * Probe the given I2C chip address. Returns 0 if a chip responded,
125 int i2c_probe(uchar chip);
128 * Read/Write interface:
129 * chip: I2C chip address, range 0..127
130 * addr: Memory (register) address within the chip
131 * alen: Number of bytes to use for addr (typically 1, 2 for larger
132 * memories, 0 for register type devices with only one
134 * buffer: Where to read/write the data
135 * len: How many bytes to read/write
137 * Returns: 0 on success, not 0 on failure
139 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
140 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
143 * Utility routines to read/write registers.
145 static inline u8 i2c_reg_read(u8 addr, u8 reg)
150 /* MPC8xx needs this. Maybe one day we can get rid of it. */
151 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
155 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
158 i2c_read(addr, reg, 1, &buf, 1);
163 static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
166 /* MPC8xx needs this. Maybe one day we can get rid of it. */
167 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
171 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
172 __func__, addr, reg, val);
175 i2c_write(addr, reg, 1, &val, 1);
179 * Functions for setting the current I2C bus and its speed
185 * Change the active I2C bus. Subsequent read/write calls will
188 * bus - bus index, zero based
190 * Returns: 0 on success, not 0 on failure
193 int i2c_set_bus_num(unsigned int bus);
198 * Returns index of currently active I2C bus. Zero-based.
201 unsigned int i2c_get_bus_num(void);
206 * Change the speed of the active I2C bus
208 * speed - bus speed in Hz
210 * Returns: 0 on success, not 0 on failure
213 int i2c_set_bus_speed(unsigned int);
218 * Returns speed of currently active I2C bus in Hz
221 unsigned int i2c_get_bus_speed(void);
223 /* NOTE: These two functions MUST be always_inline to avoid code growth! */
224 static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
225 static inline unsigned int I2C_GET_BUS(void)
227 return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
230 static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
231 static inline void I2C_SET_BUS(unsigned int bus)
234 i2c_set_bus_num(bus);
237 /* Multi I2C definitions */
239 I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
240 I2C_8, I2C_9, I2C_10,
243 /* Multi I2C busses handling */
244 #ifdef CONFIG_SOFT_I2C_MULTI_BUS
245 extern int get_multi_scl_pin(void);
246 extern int get_multi_sda_pin(void);
247 extern int multi_i2c_init(void);
251 * Get FDT values for i2c bus.
253 * @param blob Device tree blbo
254 * @return the number of I2C bus
256 void board_i2c_init(const void *blob);
259 * Find the I2C bus number by given a FDT I2C node.
261 * @param blob Device tree blbo
262 * @param node FDT I2C node to find
263 * @return the number of I2C bus (zero based), or -1 on error
265 int i2c_get_bus_num_fdt(int node);
268 * Reset the I2C bus represented by the given a FDT I2C node.
270 * @param blob Device tree blbo
271 * @param node FDT I2C node to find
272 * @return 0 if port was reset, -1 if not found
274 int i2c_reset_port_fdt(const void *blob, int node);