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 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * The original I2C interface was
28 * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
29 * AIRVENT SAM s.p.a - RIMINI(ITALY)
30 * but has been changed substantially.
37 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
39 * The implementation MUST NOT use static or global variables if the
40 * I2C routines are used to read SDRAM configuration information
41 * because this is done before the memories are initialized. Limited
42 * use of stack-based variables are OK (the initial stack size is
45 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
49 * Configuration items.
51 #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
53 #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
54 /* no muxes used bus = i2c adapters */
55 #define CONFIG_SYS_I2C_DIRECT_BUS 1
56 #define CONFIG_SYS_I2C_MAX_HOPS 0
57 #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
59 /* we use i2c muxes */
60 #undef CONFIG_SYS_I2C_DIRECT_BUS
63 /* define the I2C bus number for RTC and DTT if not already done */
64 #if !defined(CONFIG_SYS_RTC_BUS_NUM)
65 #define CONFIG_SYS_RTC_BUS_NUM 0
67 #if !defined(CONFIG_SYS_DTT_BUS_NUM)
68 #define CONFIG_SYS_DTT_BUS_NUM 0
70 #if !defined(CONFIG_SYS_SPD_BUS_NUM)
71 #define CONFIG_SYS_SPD_BUS_NUM 0
75 void (*init)(struct i2c_adapter *adap, int speed,
77 int (*probe)(struct i2c_adapter *adap, uint8_t chip);
78 int (*read)(struct i2c_adapter *adap, uint8_t chip,
79 uint addr, int alen, uint8_t *buffer,
81 int (*write)(struct i2c_adapter *adap, uint8_t chip,
82 uint addr, int alen, uint8_t *buffer,
84 uint (*set_bus_speed)(struct i2c_adapter *adap,
93 #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
94 _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
100 .set_bus_speed = _set_speed, \
102 .slaveaddr = _slaveaddr, \
104 .hwadapnr = _hwadapnr, \
108 #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
109 _set_speed, _speed, _slaveaddr, _hwadapnr) \
110 ll_entry_declare(struct i2c_adapter, _name, i2c) = \
111 U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
112 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
114 struct i2c_adapter *i2c_get_adapter(int index);
116 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
122 struct i2c_next_hop {
128 struct i2c_bus_hose {
130 struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
132 #define I2C_NULL_HOP {{-1, ""}, 0, 0}
133 extern struct i2c_bus_hose i2c_bus[];
135 #define I2C_ADAPTER(bus) i2c_bus[bus].adapter
137 #define I2C_ADAPTER(bus) bus
139 #define I2C_BUS gd->cur_i2c_bus
141 #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
142 #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
143 #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
145 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
146 #define I2C_MUX_PCA9540_ID 1
147 #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
148 #define I2C_MUX_PCA9542_ID 2
149 #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
150 #define I2C_MUX_PCA9544_ID 3
151 #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
152 #define I2C_MUX_PCA9547_ID 4
153 #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
156 #ifndef I2C_SOFT_DECLARATIONS
157 # if defined(CONFIG_MPC8260)
158 # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
159 # elif defined(CONFIG_8xx)
160 # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
162 # elif (defined(CONFIG_AT91RM9200) || \
163 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
164 defined(CONFIG_AT91SAM9263)) && !defined(CONFIG_AT91_LEGACY)
165 # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
167 # define I2C_SOFT_DECLARATIONS
172 /* Set default value for the I2C bus speed on 8xx. In the
173 * future, we'll define these in all 8xx board config files.
175 #ifndef CONFIG_SYS_I2C_SPEED
176 #define CONFIG_SYS_I2C_SPEED 50000
181 * Many boards/controllers/drivers don't support an I2C slave interface so
182 * provide a default slave address for them for use in common code. A real
183 * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
184 * support a slave interface.
186 #ifndef CONFIG_SYS_I2C_SLAVE
187 #define CONFIG_SYS_I2C_SLAVE 0xfe
191 * Initialization, must be called once on start up, may be called
192 * repeatedly to change the speed and slave addresses.
194 void i2c_init(int speed, int slaveaddr);
195 void i2c_init_board(void);
196 #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
197 void i2c_board_late_init(void);
200 #ifdef CONFIG_SYS_I2C
204 * Returns index of currently active I2C bus. Zero-based.
206 unsigned int i2c_get_bus_num(void);
211 * Change the active I2C bus. Subsequent read/write calls will
214 * bus - bus index, zero based
216 * Returns: 0 on success, not 0 on failure
219 int i2c_set_bus_num(unsigned int bus);
224 * Initializes all I2C adapters in the system. All i2c_adap structures must
225 * be initialized beforehead with function pointers and data, including
226 * speed and slaveaddr. Returns 0 on success, non-0 on failure.
228 void i2c_init_all(void);
231 * Probe the given I2C chip address. Returns 0 if a chip responded,
234 int i2c_probe(uint8_t chip);
237 * Read/Write interface:
238 * chip: I2C chip address, range 0..127
239 * addr: Memory (register) address within the chip
240 * alen: Number of bytes to use for addr (typically 1, 2 for larger
241 * memories, 0 for register type devices with only one
243 * buffer: Where to read/write the data
244 * len: How many bytes to read/write
246 * Returns: 0 on success, not 0 on failure
248 int i2c_read(uint8_t chip, unsigned int addr, int alen,
249 uint8_t *buffer, int len);
251 int i2c_write(uint8_t chip, unsigned int addr, int alen,
252 uint8_t *buffer, int len);
255 * Utility routines to read/write registers.
257 uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
259 void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
264 * Change the speed of the active I2C bus
266 * speed - bus speed in Hz
268 * Returns: new bus speed
271 unsigned int i2c_set_bus_speed(unsigned int speed);
276 * Returns speed of currently active I2C bus in Hz
279 unsigned int i2c_get_bus_speed(void);
284 * Adjusts I2C pointers after U-Boot is relocated to DRAM
286 void i2c_reloc_fixup(void);
287 #if defined(CONFIG_SYS_I2C_SOFT)
288 void i2c_soft_init(void);
289 void i2c_soft_active(void);
290 void i2c_soft_tristate(void);
291 int i2c_soft_read(void);
292 void i2c_soft_sda(int bit);
293 void i2c_soft_scl(int bit);
294 void i2c_soft_delay(void);
299 * Probe the given I2C chip address. Returns 0 if a chip responded,
302 int i2c_probe(uchar chip);
305 * Read/Write interface:
306 * chip: I2C chip address, range 0..127
307 * addr: Memory (register) address within the chip
308 * alen: Number of bytes to use for addr (typically 1, 2 for larger
309 * memories, 0 for register type devices with only one
311 * buffer: Where to read/write the data
312 * len: How many bytes to read/write
314 * Returns: 0 on success, not 0 on failure
316 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
317 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
320 * Utility routines to read/write registers.
322 static inline u8 i2c_reg_read(u8 addr, u8 reg)
327 /* MPC8xx needs this. Maybe one day we can get rid of it. */
328 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
332 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
335 i2c_read(addr, reg, 1, &buf, 1);
340 static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
343 /* MPC8xx needs this. Maybe one day we can get rid of it. */
344 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
348 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
349 __func__, addr, reg, val);
352 i2c_write(addr, reg, 1, &val, 1);
356 * Functions for setting the current I2C bus and its speed
362 * Change the active I2C bus. Subsequent read/write calls will
365 * bus - bus index, zero based
367 * Returns: 0 on success, not 0 on failure
370 int i2c_set_bus_num(unsigned int bus);
375 * Returns index of currently active I2C bus. Zero-based.
378 unsigned int i2c_get_bus_num(void);
383 * Change the speed of the active I2C bus
385 * speed - bus speed in Hz
387 * Returns: 0 on success, not 0 on failure
390 int i2c_set_bus_speed(unsigned int);
395 * Returns speed of currently active I2C bus in Hz
398 unsigned int i2c_get_bus_speed(void);
399 #endif /* CONFIG_SYS_I2C */
402 * only for backwardcompatibility, should go away if we switched
403 * completely to new multibus support.
405 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
406 # if !defined(CONFIG_SYS_MAX_I2C_BUS)
407 # define CONFIG_SYS_MAX_I2C_BUS 2
409 # define I2C_MULTI_BUS 0
411 # define CONFIG_SYS_MAX_I2C_BUS 1
412 # define I2C_MULTI_BUS 0
415 /* NOTE: These two functions MUST be always_inline to avoid code growth! */
416 static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
417 static inline unsigned int I2C_GET_BUS(void)
419 return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
422 static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
423 static inline void I2C_SET_BUS(unsigned int bus)
426 i2c_set_bus_num(bus);
429 /* Multi I2C definitions */
431 I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
432 I2C_8, I2C_9, I2C_10,
435 /* Multi I2C busses handling */
436 #ifdef CONFIG_SOFT_I2C_MULTI_BUS
437 extern int get_multi_scl_pin(void);
438 extern int get_multi_sda_pin(void);
439 extern int multi_i2c_init(void);
443 * Get FDT values for i2c bus.
445 * @param blob Device tree blbo
446 * @return the number of I2C bus
448 void board_i2c_init(const void *blob);
451 * Find the I2C bus number by given a FDT I2C node.
453 * @param blob Device tree blbo
454 * @param node FDT I2C node to find
455 * @return the number of I2C bus (zero based), or -1 on error
457 int i2c_get_bus_num_fdt(int node);
460 * Reset the I2C bus represented by the given a FDT I2C node.
462 * @param blob Device tree blbo
463 * @param node FDT I2C node to find
464 * @return 0 if port was reset, -1 if not found
466 int i2c_reset_port_fdt(const void *blob, int node);