wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 6 | * |
| 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. |
| 11 | */ |
| 12 | |
| 13 | #ifndef _I2C_H_ |
| 14 | #define _I2C_H_ |
| 15 | |
| 16 | /* |
| 17 | * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 18 | * |
| 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 |
| 23 | * limited). |
| 24 | * |
| 25 | * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 26 | */ |
| 27 | |
| 28 | /* |
| 29 | * Configuration items. |
| 30 | */ |
| 31 | #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */ |
| 32 | |
Marek Vasut | cd7b4e8 | 2011-10-25 11:40:57 +0200 | [diff] [blame] | 33 | #ifdef CONFIG_I2C_MULTI_BUS |
| 34 | #define MAX_I2C_BUS 2 |
| 35 | #define I2C_MULTI_BUS 1 |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 36 | #else |
Marek Vasut | cd7b4e8 | 2011-10-25 11:40:57 +0200 | [diff] [blame] | 37 | #define MAX_I2C_BUS 1 |
| 38 | #define I2C_MULTI_BUS 0 |
| 39 | #endif |
| 40 | |
| 41 | #if !defined(CONFIG_SYS_MAX_I2C_BUS) |
| 42 | #define CONFIG_SYS_MAX_I2C_BUS MAX_I2C_BUS |
Stefan Roese | 79b2d0b | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 43 | #endif |
| 44 | |
Stefan Roese | 8c12045 | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 45 | /* define the I2C bus number for RTC and DTT if not already done */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 46 | #if !defined(CONFIG_SYS_RTC_BUS_NUM) |
| 47 | #define CONFIG_SYS_RTC_BUS_NUM 0 |
Stefan Roese | 8c12045 | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 48 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 49 | #if !defined(CONFIG_SYS_DTT_BUS_NUM) |
| 50 | #define CONFIG_SYS_DTT_BUS_NUM 0 |
Stefan Roese | 8c12045 | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 51 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 52 | #if !defined(CONFIG_SYS_SPD_BUS_NUM) |
| 53 | #define CONFIG_SYS_SPD_BUS_NUM 0 |
Matthias Fuchs | d8a8ea5 | 2007-03-08 16:20:32 +0100 | [diff] [blame] | 54 | #endif |
Stefan Roese | 8c12045 | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 55 | |
Heiko Schocher | 98aed37 | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 56 | #ifndef I2C_SOFT_DECLARATIONS |
| 57 | # if defined(CONFIG_MPC8260) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 58 | # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT); |
Heiko Schocher | 98aed37 | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 59 | # elif defined(CONFIG_8xx) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 60 | # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
Jens Scharsig | 0cf0b93 | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 61 | |
| 62 | # elif (defined(CONFIG_AT91RM9200) || \ |
| 63 | defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \ |
| 64 | defined(CONFIG_AT91SAM9263)) && !defined(CONFIG_AT91_LEGACY) |
esw@bus-elektronik.de | 7813227 | 2011-12-20 06:05:30 +0000 | [diff] [blame] | 65 | # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA; |
Heiko Schocher | 98aed37 | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 66 | # else |
| 67 | # define I2C_SOFT_DECLARATIONS |
| 68 | # endif |
| 69 | #endif |
Timur Tabi | ecf5f07 | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 70 | |
| 71 | #ifdef CONFIG_8xx |
Peter Tyser | 9c90a2c | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 72 | /* Set default value for the I2C bus speed on 8xx. In the |
Timur Tabi | ecf5f07 | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 73 | * future, we'll define these in all 8xx board config files. |
| 74 | */ |
| 75 | #ifndef CONFIG_SYS_I2C_SPEED |
| 76 | #define CONFIG_SYS_I2C_SPEED 50000 |
| 77 | #endif |
Timur Tabi | ecf5f07 | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 78 | #endif |
Peter Tyser | 9c90a2c | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 79 | |
| 80 | /* |
| 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. |
| 85 | */ |
| 86 | #ifndef CONFIG_SYS_I2C_SLAVE |
| 87 | #define CONFIG_SYS_I2C_SLAVE 0xfe |
Timur Tabi | ecf5f07 | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 88 | #endif |
| 89 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 90 | /* |
| 91 | * Initialization, must be called once on start up, may be called |
| 92 | * repeatedly to change the speed and slave addresses. |
| 93 | */ |
| 94 | void i2c_init(int speed, int slaveaddr); |
wdenk | 06d01db | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 95 | void i2c_init_board(void); |
Richard Retanubun | 26a3350 | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 96 | #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT |
| 97 | void i2c_board_late_init(void); |
| 98 | #endif |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 99 | |
Heiko Schocher | 67b23a3 | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 100 | #if defined(CONFIG_I2C_MUX) |
| 101 | |
| 102 | typedef struct _mux { |
| 103 | uchar chip; |
| 104 | uchar channel; |
| 105 | char *name; |
| 106 | struct _mux *next; |
| 107 | } I2C_MUX; |
| 108 | |
| 109 | typedef struct _mux_device { |
| 110 | int busid; |
| 111 | I2C_MUX *mux; /* List of muxes, to reach the device */ |
| 112 | struct _mux_device *next; |
| 113 | } I2C_MUX_DEVICE; |
| 114 | |
Heiko Schocher | 67b23a3 | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 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); |
| 119 | #endif |
| 120 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 121 | /* |
| 122 | * Probe the given I2C chip address. Returns 0 if a chip responded, |
| 123 | * not 0 on failure. |
| 124 | */ |
| 125 | int i2c_probe(uchar chip); |
| 126 | |
| 127 | /* |
| 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 |
| 133 | * register) |
| 134 | * buffer: Where to read/write the data |
| 135 | * len: How many bytes to read/write |
| 136 | * |
| 137 | * Returns: 0 on success, not 0 on failure |
| 138 | */ |
| 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); |
| 141 | |
| 142 | /* |
| 143 | * Utility routines to read/write registers. |
| 144 | */ |
Timur Tabi | ecf5f07 | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 145 | static inline u8 i2c_reg_read(u8 addr, u8 reg) |
| 146 | { |
| 147 | u8 buf; |
| 148 | |
| 149 | #ifdef CONFIG_8xx |
| 150 | /* MPC8xx needs this. Maybe one day we can get rid of it. */ |
| 151 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 152 | #endif |
| 153 | |
| 154 | #ifdef DEBUG |
| 155 | printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg); |
| 156 | #endif |
| 157 | |
Timur Tabi | ecf5f07 | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 158 | i2c_read(addr, reg, 1, &buf, 1); |
Timur Tabi | ecf5f07 | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 159 | |
| 160 | return buf; |
| 161 | } |
| 162 | |
| 163 | static inline void i2c_reg_write(u8 addr, u8 reg, u8 val) |
| 164 | { |
| 165 | #ifdef CONFIG_8xx |
| 166 | /* MPC8xx needs this. Maybe one day we can get rid of it. */ |
| 167 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 168 | #endif |
| 169 | |
| 170 | #ifdef DEBUG |
| 171 | printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n", |
| 172 | __func__, addr, reg, val); |
| 173 | #endif |
| 174 | |
Timur Tabi | ecf5f07 | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 175 | i2c_write(addr, reg, 1, &val, 1); |
Timur Tabi | ecf5f07 | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 176 | } |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 177 | |
Ben Warren | bb99ad6 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 178 | /* |
| 179 | * Functions for setting the current I2C bus and its speed |
| 180 | */ |
| 181 | |
| 182 | /* |
| 183 | * i2c_set_bus_num: |
| 184 | * |
| 185 | * Change the active I2C bus. Subsequent read/write calls will |
| 186 | * go to this one. |
| 187 | * |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 188 | * bus - bus index, zero based |
Ben Warren | bb99ad6 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 189 | * |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 190 | * Returns: 0 on success, not 0 on failure |
Ben Warren | bb99ad6 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 191 | * |
| 192 | */ |
Timur Tabi | 9ca880a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 193 | int i2c_set_bus_num(unsigned int bus); |
Ben Warren | bb99ad6 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * i2c_get_bus_num: |
| 197 | * |
| 198 | * Returns index of currently active I2C bus. Zero-based. |
| 199 | */ |
| 200 | |
Timur Tabi | 9ca880a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 201 | unsigned int i2c_get_bus_num(void); |
Ben Warren | bb99ad6 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * i2c_set_bus_speed: |
| 205 | * |
| 206 | * Change the speed of the active I2C bus |
| 207 | * |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 208 | * speed - bus speed in Hz |
Ben Warren | bb99ad6 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 209 | * |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 210 | * Returns: 0 on success, not 0 on failure |
Ben Warren | bb99ad6 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 211 | * |
| 212 | */ |
Timur Tabi | 9ca880a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 213 | int i2c_set_bus_speed(unsigned int); |
Ben Warren | bb99ad6 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * i2c_get_bus_speed: |
| 217 | * |
| 218 | * Returns speed of currently active I2C bus in Hz |
| 219 | */ |
| 220 | |
Timur Tabi | 9ca880a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 221 | unsigned int i2c_get_bus_speed(void); |
Ben Warren | bb99ad6 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 222 | |
Marek Vasut | cd7b4e8 | 2011-10-25 11:40:57 +0200 | [diff] [blame] | 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) |
| 226 | { |
| 227 | return I2C_MULTI_BUS ? i2c_get_bus_num() : 0; |
| 228 | } |
| 229 | |
| 230 | static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline)); |
| 231 | static inline void I2C_SET_BUS(unsigned int bus) |
| 232 | { |
| 233 | if (I2C_MULTI_BUS) |
| 234 | i2c_set_bus_num(bus); |
| 235 | } |
| 236 | |
Ćukasz Majewski | 7ca8f73 | 2012-09-04 23:15:20 +0000 | [diff] [blame] | 237 | /* Multi I2C definitions */ |
| 238 | enum { |
| 239 | I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7, |
| 240 | I2C_8, I2C_9, I2C_10, |
| 241 | }; |
| 242 | |
| 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); |
| 248 | #endif |
Rajeshwari Shinde | a9d2ae7 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 249 | |
| 250 | /** |
| 251 | * Get FDT values for i2c bus. |
| 252 | * |
| 253 | * @param blob Device tree blbo |
| 254 | * @return the number of I2C bus |
| 255 | */ |
| 256 | void board_i2c_init(const void *blob); |
| 257 | |
| 258 | /** |
| 259 | * Find the I2C bus number by given a FDT I2C node. |
| 260 | * |
| 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 |
| 264 | */ |
| 265 | int i2c_get_bus_num_fdt(int node); |
| 266 | |
| 267 | /** |
| 268 | * Reset the I2C bus represented by the given a FDT I2C node. |
| 269 | * |
| 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 |
| 273 | */ |
| 274 | int i2c_reset_port_fdt(const void *blob, int node); |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 275 | #endif /* _I2C_H_ */ |