2 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
3 * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de>
4 * Changes for multibus/multiadapter I2C support.
7 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
9 * SPDX-License-Identifier: GPL-2.0+
11 * The original I2C interface was
12 * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
13 * AIRVENT SAM s.p.a - RIMINI(ITALY)
14 * but has been changed substantially.
21 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
23 * The implementation MUST NOT use static or global variables if the
24 * I2C routines are used to read SDRAM configuration information
25 * because this is done before the memories are initialized. Limited
26 * use of stack-based variables are OK (the initial stack size is
29 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
33 * Configuration items.
35 #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
37 #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
38 /* no muxes used bus = i2c adapters */
39 #define CONFIG_SYS_I2C_DIRECT_BUS 1
40 #define CONFIG_SYS_I2C_MAX_HOPS 0
41 #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
43 /* we use i2c muxes */
44 #undef CONFIG_SYS_I2C_DIRECT_BUS
47 /* define the I2C bus number for RTC and DTT if not already done */
48 #if !defined(CONFIG_SYS_RTC_BUS_NUM)
49 #define CONFIG_SYS_RTC_BUS_NUM 0
51 #if !defined(CONFIG_SYS_DTT_BUS_NUM)
52 #define CONFIG_SYS_DTT_BUS_NUM 0
54 #if !defined(CONFIG_SYS_SPD_BUS_NUM)
55 #define CONFIG_SYS_SPD_BUS_NUM 0
59 void (*init)(struct i2c_adapter *adap, int speed,
61 int (*probe)(struct i2c_adapter *adap, uint8_t chip);
62 int (*read)(struct i2c_adapter *adap, uint8_t chip,
63 uint addr, int alen, uint8_t *buffer,
65 int (*write)(struct i2c_adapter *adap, uint8_t chip,
66 uint addr, int alen, uint8_t *buffer,
68 uint (*set_bus_speed)(struct i2c_adapter *adap,
78 #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
79 _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
85 .set_bus_speed = _set_speed, \
87 .slaveaddr = _slaveaddr, \
89 .hwadapnr = _hwadapnr, \
93 #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
94 _set_speed, _speed, _slaveaddr, _hwadapnr) \
95 ll_entry_declare(struct i2c_adapter, _name, i2c) = \
96 U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
97 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
99 struct i2c_adapter *i2c_get_adapter(int index);
101 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
107 struct i2c_next_hop {
113 struct i2c_bus_hose {
115 struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
117 #define I2C_NULL_HOP {{-1, ""}, 0, 0}
118 extern struct i2c_bus_hose i2c_bus[];
120 #define I2C_ADAPTER(bus) i2c_bus[bus].adapter
122 #define I2C_ADAPTER(bus) bus
124 #define I2C_BUS gd->cur_i2c_bus
126 #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
127 #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
128 #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
130 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
131 #define I2C_MUX_PCA9540_ID 1
132 #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
133 #define I2C_MUX_PCA9542_ID 2
134 #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
135 #define I2C_MUX_PCA9544_ID 3
136 #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
137 #define I2C_MUX_PCA9547_ID 4
138 #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
139 #define I2C_MUX_PCA9548_ID 5
140 #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
143 #ifndef I2C_SOFT_DECLARATIONS
144 # if defined(CONFIG_MPC8260)
145 # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
146 # elif defined(CONFIG_8xx)
147 # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
149 # elif (defined(CONFIG_AT91RM9200) || \
150 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
151 defined(CONFIG_AT91SAM9263))
152 # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
154 # define I2C_SOFT_DECLARATIONS
159 /* Set default value for the I2C bus speed on 8xx. In the
160 * future, we'll define these in all 8xx board config files.
162 #ifndef CONFIG_SYS_I2C_SPEED
163 #define CONFIG_SYS_I2C_SPEED 50000
168 * Many boards/controllers/drivers don't support an I2C slave interface so
169 * provide a default slave address for them for use in common code. A real
170 * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
171 * support a slave interface.
173 #ifndef CONFIG_SYS_I2C_SLAVE
174 #define CONFIG_SYS_I2C_SLAVE 0xfe
178 * Initialization, must be called once on start up, may be called
179 * repeatedly to change the speed and slave addresses.
181 void i2c_init(int speed, int slaveaddr);
182 void i2c_init_board(void);
183 #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
184 void i2c_board_late_init(void);
187 #ifdef CONFIG_SYS_I2C
191 * Returns index of currently active I2C bus. Zero-based.
193 unsigned int i2c_get_bus_num(void);
198 * Change the active I2C bus. Subsequent read/write calls will
201 * bus - bus index, zero based
203 * Returns: 0 on success, not 0 on failure
206 int i2c_set_bus_num(unsigned int bus);
211 * Initializes all I2C adapters in the system. All i2c_adap structures must
212 * be initialized beforehead with function pointers and data, including
213 * speed and slaveaddr. Returns 0 on success, non-0 on failure.
215 void i2c_init_all(void);
218 * Probe the given I2C chip address. Returns 0 if a chip responded,
221 int i2c_probe(uint8_t chip);
224 * Read/Write interface:
225 * chip: I2C chip address, range 0..127
226 * addr: Memory (register) address within the chip
227 * alen: Number of bytes to use for addr (typically 1, 2 for larger
228 * memories, 0 for register type devices with only one
230 * buffer: Where to read/write the data
231 * len: How many bytes to read/write
233 * Returns: 0 on success, not 0 on failure
235 int i2c_read(uint8_t chip, unsigned int addr, int alen,
236 uint8_t *buffer, int len);
238 int i2c_write(uint8_t chip, unsigned int addr, int alen,
239 uint8_t *buffer, int len);
242 * Utility routines to read/write registers.
244 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
246 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
251 * Change the speed of the active I2C bus
253 * speed - bus speed in Hz
255 * Returns: new bus speed
258 unsigned int i2c_set_bus_speed(unsigned int speed);
263 * Returns speed of currently active I2C bus in Hz
266 unsigned int i2c_get_bus_speed(void);
271 * Adjusts I2C pointers after U-Boot is relocated to DRAM
273 void i2c_reloc_fixup(void);
274 #if defined(CONFIG_SYS_I2C_SOFT)
275 void i2c_soft_init(void);
276 void i2c_soft_active(void);
277 void i2c_soft_tristate(void);
278 int i2c_soft_read(void);
279 void i2c_soft_sda(int bit);
280 void i2c_soft_scl(int bit);
281 void i2c_soft_delay(void);
286 * Probe the given I2C chip address. Returns 0 if a chip responded,
289 int i2c_probe(uchar chip);
292 * Read/Write interface:
293 * chip: I2C chip address, range 0..127
294 * addr: Memory (register) address within the chip
295 * alen: Number of bytes to use for addr (typically 1, 2 for larger
296 * memories, 0 for register type devices with only one
298 * buffer: Where to read/write the data
299 * len: How many bytes to read/write
301 * Returns: 0 on success, not 0 on failure
303 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
304 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
307 * Utility routines to read/write registers.
309 static inline u8 i2c_reg_read(u8 addr, u8 reg)
314 /* MPC8xx needs this. Maybe one day we can get rid of it. */
315 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
319 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
322 i2c_read(addr, reg, 1, &buf, 1);
327 static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
330 /* MPC8xx needs this. Maybe one day we can get rid of it. */
331 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
335 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
336 __func__, addr, reg, val);
339 i2c_write(addr, reg, 1, &val, 1);
343 * Functions for setting the current I2C bus and its speed
349 * Change the active I2C bus. Subsequent read/write calls will
352 * bus - bus index, zero based
354 * Returns: 0 on success, not 0 on failure
357 int i2c_set_bus_num(unsigned int bus);
362 * Returns index of currently active I2C bus. Zero-based.
365 unsigned int i2c_get_bus_num(void);
370 * Change the speed of the active I2C bus
372 * speed - bus speed in Hz
374 * Returns: 0 on success, not 0 on failure
377 int i2c_set_bus_speed(unsigned int);
382 * Returns speed of currently active I2C bus in Hz
385 unsigned int i2c_get_bus_speed(void);
386 #endif /* CONFIG_SYS_I2C */
389 * only for backwardcompatibility, should go away if we switched
390 * completely to new multibus support.
392 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
393 # if !defined(CONFIG_SYS_MAX_I2C_BUS)
394 # define CONFIG_SYS_MAX_I2C_BUS 2
396 # define I2C_MULTI_BUS 1
398 # define CONFIG_SYS_MAX_I2C_BUS 1
399 # define I2C_MULTI_BUS 0
402 /* NOTE: These two functions MUST be always_inline to avoid code growth! */
403 static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
404 static inline unsigned int I2C_GET_BUS(void)
406 return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
409 static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
410 static inline void I2C_SET_BUS(unsigned int bus)
413 i2c_set_bus_num(bus);
416 /* Multi I2C definitions */
418 I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
419 I2C_8, I2C_9, I2C_10,
422 /* Multi I2C busses handling */
423 #ifdef CONFIG_SOFT_I2C_MULTI_BUS
424 extern int get_multi_scl_pin(void);
425 extern int get_multi_sda_pin(void);
426 extern int multi_i2c_init(void);
430 * Get FDT values for i2c bus.
432 * @param blob Device tree blbo
433 * @return the number of I2C bus
435 void board_i2c_init(const void *blob);
438 * Find the I2C bus number by given a FDT I2C node.
440 * @param blob Device tree blbo
441 * @param node FDT I2C node to find
442 * @return the number of I2C bus (zero based), or -1 on error
444 int i2c_get_bus_num_fdt(int node);
447 * Reset the I2C bus represented by the given a FDT I2C node.
449 * @param blob Device tree blbo
450 * @param node FDT I2C node to find
451 * @return 0 if port was reset, -1 if not found
453 int i2c_reset_port_fdt(const void *blob, int node);