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 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | * |
| 23 | * The original I2C interface was |
| 24 | * (C) 2000 by Paolo Scaffardi (arsenio@tin.it) |
| 25 | * AIRVENT SAM s.p.a - RIMINI(ITALY) |
| 26 | * but has been changed substantially. |
| 27 | */ |
| 28 | |
| 29 | #ifndef _I2C_H_ |
| 30 | #define _I2C_H_ |
| 31 | |
| 32 | /* |
| 33 | * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 34 | * |
| 35 | * The implementation MUST NOT use static or global variables if the |
| 36 | * I2C routines are used to read SDRAM configuration information |
| 37 | * because this is done before the memories are initialized. Limited |
| 38 | * use of stack-based variables are OK (the initial stack size is |
| 39 | * limited). |
| 40 | * |
| 41 | * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 42 | */ |
| 43 | |
| 44 | /* |
| 45 | * Configuration items. |
| 46 | */ |
| 47 | #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */ |
| 48 | |
Marek Vasut | 513b820 | 2011-10-25 11:40:57 +0200 | [diff] [blame^] | 49 | #ifdef CONFIG_I2C_MULTI_BUS |
| 50 | #define MAX_I2C_BUS 2 |
| 51 | #define I2C_MULTI_BUS 1 |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 52 | #else |
Marek Vasut | 513b820 | 2011-10-25 11:40:57 +0200 | [diff] [blame^] | 53 | #define MAX_I2C_BUS 1 |
| 54 | #define I2C_MULTI_BUS 0 |
| 55 | #endif |
| 56 | |
| 57 | #if !defined(CONFIG_SYS_MAX_I2C_BUS) |
| 58 | #define CONFIG_SYS_MAX_I2C_BUS MAX_I2C_BUS |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 59 | #endif |
| 60 | |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 61 | /* define the I2C bus number for RTC and DTT if not already done */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | #if !defined(CONFIG_SYS_RTC_BUS_NUM) |
| 63 | #define CONFIG_SYS_RTC_BUS_NUM 0 |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 64 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 65 | #if !defined(CONFIG_SYS_DTT_BUS_NUM) |
| 66 | #define CONFIG_SYS_DTT_BUS_NUM 0 |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 67 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 68 | #if !defined(CONFIG_SYS_SPD_BUS_NUM) |
| 69 | #define CONFIG_SYS_SPD_BUS_NUM 0 |
Matthias Fuchs | 66067c3 | 2007-03-08 16:20:32 +0100 | [diff] [blame] | 70 | #endif |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 71 | |
Heiko Schocher | dc7d22a | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 72 | #ifndef I2C_SOFT_DECLARATIONS |
| 73 | # if defined(CONFIG_MPC8260) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 74 | # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT); |
Heiko Schocher | dc7d22a | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 75 | # elif defined(CONFIG_8xx) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 76 | # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 77 | |
| 78 | # elif (defined(CONFIG_AT91RM9200) || \ |
| 79 | defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \ |
| 80 | defined(CONFIG_AT91SAM9263)) && !defined(CONFIG_AT91_LEGACY) |
| 81 | # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE; |
Heiko Schocher | dc7d22a | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 82 | # else |
| 83 | # define I2C_SOFT_DECLARATIONS |
| 84 | # endif |
| 85 | #endif |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 86 | |
| 87 | #ifdef CONFIG_8xx |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 88 | /* Set default value for the I2C bus speed on 8xx. In the |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 89 | * future, we'll define these in all 8xx board config files. |
| 90 | */ |
| 91 | #ifndef CONFIG_SYS_I2C_SPEED |
| 92 | #define CONFIG_SYS_I2C_SPEED 50000 |
| 93 | #endif |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 94 | #endif |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 95 | |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 96 | /* |
| 97 | * Many boards/controllers/drivers don't support an I2C slave interface so |
| 98 | * provide a default slave address for them for use in common code. A real |
| 99 | * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does |
| 100 | * support a slave interface. |
| 101 | */ |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 102 | #ifndef CONFIG_SYS_I2C_SLAVE |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 103 | #define CONFIG_SYS_I2C_SLAVE 0xfe |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 104 | #endif |
| 105 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 106 | /* |
| 107 | * Initialization, must be called once on start up, may be called |
| 108 | * repeatedly to change the speed and slave addresses. |
| 109 | */ |
| 110 | void i2c_init(int speed, int slaveaddr); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 111 | void i2c_init_board(void); |
Richard Retanubun | df0149c | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 112 | #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT |
| 113 | void i2c_board_late_init(void); |
| 114 | #endif |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 115 | |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 116 | #if defined(CONFIG_I2C_MUX) |
| 117 | |
| 118 | typedef struct _mux { |
| 119 | uchar chip; |
| 120 | uchar channel; |
| 121 | char *name; |
| 122 | struct _mux *next; |
| 123 | } I2C_MUX; |
| 124 | |
| 125 | typedef struct _mux_device { |
| 126 | int busid; |
| 127 | I2C_MUX *mux; /* List of muxes, to reach the device */ |
| 128 | struct _mux_device *next; |
| 129 | } I2C_MUX_DEVICE; |
| 130 | |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 131 | I2C_MUX_DEVICE *i2c_mux_search_device(int id); |
| 132 | I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf); |
| 133 | int i2x_mux_select_mux(int bus); |
| 134 | int i2c_mux_ident_muxstring_f (uchar *buf); |
| 135 | #endif |
| 136 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 137 | /* |
| 138 | * Probe the given I2C chip address. Returns 0 if a chip responded, |
| 139 | * not 0 on failure. |
| 140 | */ |
| 141 | int i2c_probe(uchar chip); |
| 142 | |
| 143 | /* |
| 144 | * Read/Write interface: |
| 145 | * chip: I2C chip address, range 0..127 |
| 146 | * addr: Memory (register) address within the chip |
| 147 | * alen: Number of bytes to use for addr (typically 1, 2 for larger |
| 148 | * memories, 0 for register type devices with only one |
| 149 | * register) |
| 150 | * buffer: Where to read/write the data |
| 151 | * len: How many bytes to read/write |
| 152 | * |
| 153 | * Returns: 0 on success, not 0 on failure |
| 154 | */ |
| 155 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len); |
| 156 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len); |
| 157 | |
| 158 | /* |
| 159 | * Utility routines to read/write registers. |
| 160 | */ |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 161 | static inline u8 i2c_reg_read(u8 addr, u8 reg) |
| 162 | { |
| 163 | u8 buf; |
| 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\n", __func__, addr, reg); |
| 172 | #endif |
| 173 | |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 174 | i2c_read(addr, reg, 1, &buf, 1); |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 175 | |
| 176 | return buf; |
| 177 | } |
| 178 | |
| 179 | static inline void i2c_reg_write(u8 addr, u8 reg, u8 val) |
| 180 | { |
| 181 | #ifdef CONFIG_8xx |
| 182 | /* MPC8xx needs this. Maybe one day we can get rid of it. */ |
| 183 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 184 | #endif |
| 185 | |
| 186 | #ifdef DEBUG |
| 187 | printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n", |
| 188 | __func__, addr, reg, val); |
| 189 | #endif |
| 190 | |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 191 | i2c_write(addr, reg, 1, &val, 1); |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 192 | } |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 193 | |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 194 | /* |
| 195 | * Functions for setting the current I2C bus and its speed |
| 196 | */ |
| 197 | |
| 198 | /* |
| 199 | * i2c_set_bus_num: |
| 200 | * |
| 201 | * Change the active I2C bus. Subsequent read/write calls will |
| 202 | * go to this one. |
| 203 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 204 | * bus - bus index, zero based |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 205 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 206 | * Returns: 0 on success, not 0 on failure |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 207 | * |
| 208 | */ |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 209 | int i2c_set_bus_num(unsigned int bus); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * i2c_get_bus_num: |
| 213 | * |
| 214 | * Returns index of currently active I2C bus. Zero-based. |
| 215 | */ |
| 216 | |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 217 | unsigned int i2c_get_bus_num(void); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 218 | |
| 219 | /* |
| 220 | * i2c_set_bus_speed: |
| 221 | * |
| 222 | * Change the speed of the active I2C bus |
| 223 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 224 | * speed - bus speed in Hz |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 225 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 226 | * Returns: 0 on success, not 0 on failure |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 227 | * |
| 228 | */ |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 229 | int i2c_set_bus_speed(unsigned int); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * i2c_get_bus_speed: |
| 233 | * |
| 234 | * Returns speed of currently active I2C bus in Hz |
| 235 | */ |
| 236 | |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 237 | unsigned int i2c_get_bus_speed(void); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 238 | |
Marek Vasut | 513b820 | 2011-10-25 11:40:57 +0200 | [diff] [blame^] | 239 | /* NOTE: These two functions MUST be always_inline to avoid code growth! */ |
| 240 | static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline)); |
| 241 | static inline unsigned int I2C_GET_BUS(void) |
| 242 | { |
| 243 | return I2C_MULTI_BUS ? i2c_get_bus_num() : 0; |
| 244 | } |
| 245 | |
| 246 | static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline)); |
| 247 | static inline void I2C_SET_BUS(unsigned int bus) |
| 248 | { |
| 249 | if (I2C_MULTI_BUS) |
| 250 | i2c_set_bus_num(bus); |
| 251 | } |
| 252 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 253 | #endif /* _I2C_H_ */ |