wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 2 | * (C) Copyright 2009 |
| 3 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 4 | * Changes for multibus/multiadapter I2C support. |
| 5 | * |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 6 | * (C) Copyright 2001, 2002 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 8 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 10 | * |
| 11 | * This has been changed substantially by Gerald Van Baren, Custom IDEAS, |
| 12 | * vanbaren@cideas.com. It was heavily influenced by LiMon, written by |
| 13 | * Neil Russell. |
Simon Glass | 2852709 | 2016-11-23 06:34:44 -0700 | [diff] [blame] | 14 | * |
| 15 | * NOTE: This driver should be converted to driver model before June 2017. |
| 16 | * Please see doc/driver-model/i2c-howto.txt for instructions. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <common.h> |
Ryan Mallon | f3100ff | 2011-01-27 08:54:15 +1300 | [diff] [blame] | 20 | #if defined(CONFIG_AT91FAMILY) |
wdenk | 9d5028c | 2004-11-21 00:06:33 +0000 | [diff] [blame] | 21 | #include <asm/io.h> |
| 22 | #include <asm/arch/hardware.h> |
Jens Scharsig | 0cf0b93 | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 23 | #include <asm/arch/at91_pio.h> |
Andreas Bießmann | cb96a0a | 2013-10-30 15:18:18 +0100 | [diff] [blame] | 24 | #ifdef CONFIG_ATMEL_LEGACY |
Daniel Gorsulowski | 4e574c4 | 2009-05-18 13:20:54 +0200 | [diff] [blame] | 25 | #include <asm/arch/gpio.h> |
Jens Scharsig | 0cf0b93 | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 26 | #endif |
Daniel Gorsulowski | 4e574c4 | 2009-05-18 13:20:54 +0200 | [diff] [blame] | 27 | #endif |
Christophe Leroy | b1e41d1 | 2017-07-06 10:33:21 +0200 | [diff] [blame] | 28 | #if defined(CONFIG_8xx) |
Heiko Schocher | a21ca95 | 2008-10-17 13:52:51 +0200 | [diff] [blame] | 29 | #include <asm/io.h> |
| 30 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 31 | #include <i2c.h> |
| 32 | |
Mike Frysinger | 793b572 | 2010-07-21 13:38:02 -0400 | [diff] [blame] | 33 | #if defined(CONFIG_SOFT_I2C_GPIO_SCL) |
| 34 | # include <asm/gpio.h> |
| 35 | |
| 36 | # ifndef I2C_GPIO_SYNC |
| 37 | # define I2C_GPIO_SYNC |
| 38 | # endif |
| 39 | |
| 40 | # ifndef I2C_INIT |
| 41 | # define I2C_INIT \ |
| 42 | do { \ |
| 43 | gpio_request(CONFIG_SOFT_I2C_GPIO_SCL, "soft_i2c"); \ |
| 44 | gpio_request(CONFIG_SOFT_I2C_GPIO_SDA, "soft_i2c"); \ |
| 45 | } while (0) |
| 46 | # endif |
| 47 | |
| 48 | # ifndef I2C_ACTIVE |
| 49 | # define I2C_ACTIVE do { } while (0) |
| 50 | # endif |
| 51 | |
| 52 | # ifndef I2C_TRISTATE |
| 53 | # define I2C_TRISTATE do { } while (0) |
| 54 | # endif |
| 55 | |
| 56 | # ifndef I2C_READ |
| 57 | # define I2C_READ gpio_get_value(CONFIG_SOFT_I2C_GPIO_SDA) |
| 58 | # endif |
| 59 | |
| 60 | # ifndef I2C_SDA |
| 61 | # define I2C_SDA(bit) \ |
| 62 | do { \ |
| 63 | if (bit) \ |
| 64 | gpio_direction_input(CONFIG_SOFT_I2C_GPIO_SDA); \ |
| 65 | else \ |
| 66 | gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SDA, 0); \ |
| 67 | I2C_GPIO_SYNC; \ |
| 68 | } while (0) |
| 69 | # endif |
| 70 | |
| 71 | # ifndef I2C_SCL |
| 72 | # define I2C_SCL(bit) \ |
| 73 | do { \ |
| 74 | gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SCL, bit); \ |
| 75 | I2C_GPIO_SYNC; \ |
| 76 | } while (0) |
| 77 | # endif |
| 78 | |
| 79 | # ifndef I2C_DELAY |
| 80 | # define I2C_DELAY udelay(5) /* 1/4 I2C clock duration */ |
| 81 | # endif |
| 82 | |
| 83 | #endif |
| 84 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 85 | /* #define DEBUG_I2C */ |
| 86 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 87 | DECLARE_GLOBAL_DATA_PTR; |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 88 | |
| 89 | #ifndef I2C_SOFT_DECLARATIONS |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 90 | # define I2C_SOFT_DECLARATIONS |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 91 | #endif |
| 92 | |
Marek Vasut | 90f002a | 2013-08-01 12:32:20 +0200 | [diff] [blame] | 93 | #if !defined(CONFIG_SYS_I2C_SOFT_SPEED) |
| 94 | #define CONFIG_SYS_I2C_SOFT_SPEED CONFIG_SYS_I2C_SPEED |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 95 | #endif |
Marek Vasut | 90f002a | 2013-08-01 12:32:20 +0200 | [diff] [blame] | 96 | #if !defined(CONFIG_SYS_I2C_SOFT_SLAVE) |
| 97 | #define CONFIG_SYS_I2C_SOFT_SLAVE CONFIG_SYS_I2C_SLAVE |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 98 | #endif |
| 99 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | /*----------------------------------------------------------------------- |
| 101 | * Definitions |
| 102 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | #define RETRIES 0 |
| 104 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 105 | #define I2C_ACK 0 /* PD_SDA level to ack a byte */ |
| 106 | #define I2C_NOACK 1 /* PD_SDA level to noack a byte */ |
| 107 | |
| 108 | |
| 109 | #ifdef DEBUG_I2C |
| 110 | #define PRINTD(fmt,args...) do { \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | printf (fmt ,##args); \ |
| 112 | } while (0) |
| 113 | #else |
| 114 | #define PRINTD(fmt,args...) |
| 115 | #endif |
| 116 | |
| 117 | /*----------------------------------------------------------------------- |
| 118 | * Local functions |
| 119 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 120 | #if !defined(CONFIG_SYS_I2C_INIT_BOARD) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 121 | static void send_reset (void); |
Heiko Schocher | 4ca107e | 2008-10-15 09:38:38 +0200 | [diff] [blame] | 122 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 123 | static void send_start (void); |
| 124 | static void send_stop (void); |
| 125 | static void send_ack (int); |
| 126 | static int write_byte (uchar byte); |
| 127 | static uchar read_byte (int); |
| 128 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 129 | #if !defined(CONFIG_SYS_I2C_INIT_BOARD) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 130 | /*----------------------------------------------------------------------- |
| 131 | * Send a reset sequence consisting of 9 clocks with the data signal high |
| 132 | * to clock any confused device back into an idle state. Also send a |
| 133 | * <stop> at the end of the sequence for belts & suspenders. |
| 134 | */ |
| 135 | static void send_reset(void) |
| 136 | { |
Heiko Schocher | 98aed37 | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 137 | I2C_SOFT_DECLARATIONS /* intentional without ';' */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 138 | int j; |
| 139 | |
wdenk | 60fbe25 | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 140 | I2C_SCL(1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 141 | I2C_SDA(1); |
wdenk | 60fbe25 | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 142 | #ifdef I2C_INIT |
| 143 | I2C_INIT; |
| 144 | #endif |
| 145 | I2C_TRISTATE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 146 | for(j = 0; j < 9; j++) { |
| 147 | I2C_SCL(0); |
| 148 | I2C_DELAY; |
| 149 | I2C_DELAY; |
| 150 | I2C_SCL(1); |
| 151 | I2C_DELAY; |
| 152 | I2C_DELAY; |
| 153 | } |
| 154 | send_stop(); |
| 155 | I2C_TRISTATE; |
| 156 | } |
Heiko Schocher | 4ca107e | 2008-10-15 09:38:38 +0200 | [diff] [blame] | 157 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 158 | |
| 159 | /*----------------------------------------------------------------------- |
| 160 | * START: High -> Low on SDA while SCL is High |
| 161 | */ |
| 162 | static void send_start(void) |
| 163 | { |
Heiko Schocher | 98aed37 | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 164 | I2C_SOFT_DECLARATIONS /* intentional without ';' */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 165 | |
| 166 | I2C_DELAY; |
| 167 | I2C_SDA(1); |
| 168 | I2C_ACTIVE; |
| 169 | I2C_DELAY; |
| 170 | I2C_SCL(1); |
| 171 | I2C_DELAY; |
| 172 | I2C_SDA(0); |
| 173 | I2C_DELAY; |
| 174 | } |
| 175 | |
| 176 | /*----------------------------------------------------------------------- |
| 177 | * STOP: Low -> High on SDA while SCL is High |
| 178 | */ |
| 179 | static void send_stop(void) |
| 180 | { |
Heiko Schocher | 98aed37 | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 181 | I2C_SOFT_DECLARATIONS /* intentional without ';' */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 182 | |
| 183 | I2C_SCL(0); |
| 184 | I2C_DELAY; |
| 185 | I2C_SDA(0); |
| 186 | I2C_ACTIVE; |
| 187 | I2C_DELAY; |
| 188 | I2C_SCL(1); |
| 189 | I2C_DELAY; |
| 190 | I2C_SDA(1); |
| 191 | I2C_DELAY; |
| 192 | I2C_TRISTATE; |
| 193 | } |
| 194 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 195 | /*----------------------------------------------------------------------- |
| 196 | * ack should be I2C_ACK or I2C_NOACK |
| 197 | */ |
| 198 | static void send_ack(int ack) |
| 199 | { |
Heiko Schocher | 98aed37 | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 200 | I2C_SOFT_DECLARATIONS /* intentional without ';' */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 201 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 202 | I2C_SCL(0); |
| 203 | I2C_DELAY; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 204 | I2C_ACTIVE; |
Wolfgang Denk | c15f80e | 2006-03-13 00:50:48 +0100 | [diff] [blame] | 205 | I2C_SDA(ack); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 206 | I2C_DELAY; |
| 207 | I2C_SCL(1); |
| 208 | I2C_DELAY; |
| 209 | I2C_DELAY; |
| 210 | I2C_SCL(0); |
| 211 | I2C_DELAY; |
| 212 | } |
| 213 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 214 | /*----------------------------------------------------------------------- |
| 215 | * Send 8 bits and look for an acknowledgement. |
| 216 | */ |
| 217 | static int write_byte(uchar data) |
| 218 | { |
Heiko Schocher | 98aed37 | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 219 | I2C_SOFT_DECLARATIONS /* intentional without ';' */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 220 | int j; |
| 221 | int nack; |
| 222 | |
| 223 | I2C_ACTIVE; |
| 224 | for(j = 0; j < 8; j++) { |
| 225 | I2C_SCL(0); |
| 226 | I2C_DELAY; |
| 227 | I2C_SDA(data & 0x80); |
| 228 | I2C_DELAY; |
| 229 | I2C_SCL(1); |
| 230 | I2C_DELAY; |
| 231 | I2C_DELAY; |
| 232 | |
| 233 | data <<= 1; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Look for an <ACK>(negative logic) and return it. |
| 238 | */ |
| 239 | I2C_SCL(0); |
| 240 | I2C_DELAY; |
| 241 | I2C_SDA(1); |
| 242 | I2C_TRISTATE; |
| 243 | I2C_DELAY; |
| 244 | I2C_SCL(1); |
| 245 | I2C_DELAY; |
| 246 | I2C_DELAY; |
| 247 | nack = I2C_READ; |
| 248 | I2C_SCL(0); |
| 249 | I2C_DELAY; |
| 250 | I2C_ACTIVE; |
| 251 | |
| 252 | return(nack); /* not a nack is an ack */ |
| 253 | } |
| 254 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 255 | /*----------------------------------------------------------------------- |
| 256 | * if ack == I2C_ACK, ACK the byte so can continue reading, else |
| 257 | * send I2C_NOACK to end the read. |
| 258 | */ |
| 259 | static uchar read_byte(int ack) |
| 260 | { |
Heiko Schocher | 98aed37 | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 261 | I2C_SOFT_DECLARATIONS /* intentional without ';' */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 262 | int data; |
| 263 | int j; |
| 264 | |
| 265 | /* |
| 266 | * Read 8 bits, MSB first. |
| 267 | */ |
| 268 | I2C_TRISTATE; |
Haavard Skinnemoen | 110e006 | 2008-05-16 11:08:11 +0200 | [diff] [blame] | 269 | I2C_SDA(1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 270 | data = 0; |
| 271 | for(j = 0; j < 8; j++) { |
| 272 | I2C_SCL(0); |
| 273 | I2C_DELAY; |
| 274 | I2C_SCL(1); |
| 275 | I2C_DELAY; |
| 276 | data <<= 1; |
| 277 | data |= I2C_READ; |
| 278 | I2C_DELAY; |
| 279 | } |
| 280 | send_ack(ack); |
| 281 | |
| 282 | return(data); |
| 283 | } |
| 284 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 285 | /*----------------------------------------------------------------------- |
| 286 | * Initialization |
| 287 | */ |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 288 | static void soft_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 289 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 290 | #if defined(CONFIG_SYS_I2C_INIT_BOARD) |
Heiko Schocher | 4ca107e | 2008-10-15 09:38:38 +0200 | [diff] [blame] | 291 | /* call board specific i2c bus reset routine before accessing the */ |
| 292 | /* environment, which might be in a chip on that bus. For details */ |
| 293 | /* about this problem see doc/I2C_Edge_Conditions. */ |
| 294 | i2c_init_board(); |
| 295 | #else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 296 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 297 | * WARNING: Do NOT save speed in a static variable: if the |
| 298 | * I2C routines are called before RAM is initialized (to read |
| 299 | * the DIMM SPD, for instance), RAM won't be usable and your |
| 300 | * system will crash. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 301 | */ |
| 302 | send_reset (); |
Heiko Schocher | 4ca107e | 2008-10-15 09:38:38 +0200 | [diff] [blame] | 303 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /*----------------------------------------------------------------------- |
| 307 | * Probe to see if a chip is present. Also good for checking for the |
| 308 | * completion of EEPROM writes since the chip stops responding until |
| 309 | * the write completes (typically 10mSec). |
| 310 | */ |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 311 | static int soft_i2c_probe(struct i2c_adapter *adap, uint8_t addr) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 312 | { |
| 313 | int rc; |
| 314 | |
Wolfgang Denk | 82d716f | 2006-03-12 01:30:45 +0100 | [diff] [blame] | 315 | /* |
Wolfgang Denk | 8e7b703 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 316 | * perform 1 byte write transaction with just address byte |
Wolfgang Denk | 82d716f | 2006-03-12 01:30:45 +0100 | [diff] [blame] | 317 | * (fake write) |
| 318 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 319 | send_start(); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 320 | rc = write_byte ((addr << 1) | 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 321 | send_stop(); |
| 322 | |
| 323 | return (rc ? 1 : 0); |
| 324 | } |
| 325 | |
| 326 | /*----------------------------------------------------------------------- |
| 327 | * Read bytes |
| 328 | */ |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 329 | static int soft_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr, |
| 330 | int alen, uchar *buffer, int len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 331 | { |
| 332 | int shift; |
| 333 | PRINTD("i2c_read: chip %02X addr %02X alen %d buffer %p len %d\n", |
| 334 | chip, addr, alen, buffer, len); |
| 335 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 336 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 337 | /* |
| 338 | * EEPROM chips that implement "address overflow" are ones |
| 339 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 340 | * address and the extra bits end up in the "chip address" |
| 341 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 342 | * four 256 byte chips. |
| 343 | * |
| 344 | * Note that we consider the length of the address field to |
| 345 | * still be one byte because the extra address bits are |
| 346 | * hidden in the chip address. |
| 347 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 348 | chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 349 | |
| 350 | PRINTD("i2c_read: fix addr_overflow: chip %02X addr %02X\n", |
| 351 | chip, addr); |
| 352 | #endif |
| 353 | |
| 354 | /* |
| 355 | * Do the addressing portion of a write cycle to set the |
| 356 | * chip's address pointer. If the address length is zero, |
| 357 | * don't do the normal write cycle to set the address pointer, |
| 358 | * there is no address pointer in this chip. |
| 359 | */ |
| 360 | send_start(); |
| 361 | if(alen > 0) { |
| 362 | if(write_byte(chip << 1)) { /* write cycle */ |
| 363 | send_stop(); |
| 364 | PRINTD("i2c_read, no chip responded %02X\n", chip); |
| 365 | return(1); |
| 366 | } |
| 367 | shift = (alen-1) * 8; |
| 368 | while(alen-- > 0) { |
| 369 | if(write_byte(addr >> shift)) { |
| 370 | PRINTD("i2c_read, address not <ACK>ed\n"); |
| 371 | return(1); |
| 372 | } |
| 373 | shift -= 8; |
| 374 | } |
Andrew Dyer | 2ac6985 | 2008-12-29 17:36:01 -0600 | [diff] [blame] | 375 | |
| 376 | /* Some I2C chips need a stop/start sequence here, |
| 377 | * other chips don't work with a full stop and need |
| 378 | * only a start. Default behaviour is to send the |
| 379 | * stop/start sequence. |
| 380 | */ |
| 381 | #ifdef CONFIG_SOFT_I2C_READ_REPEATED_START |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 382 | send_start(); |
Andrew Dyer | 2ac6985 | 2008-12-29 17:36:01 -0600 | [diff] [blame] | 383 | #else |
| 384 | send_stop(); |
| 385 | send_start(); |
| 386 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 387 | } |
| 388 | /* |
| 389 | * Send the chip address again, this time for a read cycle. |
| 390 | * Then read the data. On the last byte, we do a NACK instead |
| 391 | * of an ACK(len == 0) to terminate the read. |
| 392 | */ |
| 393 | write_byte((chip << 1) | 1); /* read cycle */ |
| 394 | while(len-- > 0) { |
| 395 | *buffer++ = read_byte(len == 0); |
| 396 | } |
| 397 | send_stop(); |
| 398 | return(0); |
| 399 | } |
| 400 | |
| 401 | /*----------------------------------------------------------------------- |
| 402 | * Write bytes |
| 403 | */ |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 404 | static int soft_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr, |
| 405 | int alen, uchar *buffer, int len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 406 | { |
| 407 | int shift, failures = 0; |
| 408 | |
| 409 | PRINTD("i2c_write: chip %02X addr %02X alen %d buffer %p len %d\n", |
| 410 | chip, addr, alen, buffer, len); |
| 411 | |
| 412 | send_start(); |
| 413 | if(write_byte(chip << 1)) { /* write cycle */ |
| 414 | send_stop(); |
| 415 | PRINTD("i2c_write, no chip responded %02X\n", chip); |
| 416 | return(1); |
| 417 | } |
| 418 | shift = (alen-1) * 8; |
| 419 | while(alen-- > 0) { |
| 420 | if(write_byte(addr >> shift)) { |
| 421 | PRINTD("i2c_write, address not <ACK>ed\n"); |
| 422 | return(1); |
| 423 | } |
| 424 | shift -= 8; |
| 425 | } |
| 426 | |
| 427 | while(len-- > 0) { |
| 428 | if(write_byte(*buffer++)) { |
| 429 | failures++; |
| 430 | } |
| 431 | } |
| 432 | send_stop(); |
| 433 | return(failures); |
| 434 | } |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * Register soft i2c adapters |
| 438 | */ |
Dirk Eibach | 37b3325 | 2015-10-28 11:46:39 +0100 | [diff] [blame] | 439 | U_BOOT_I2C_ADAP_COMPLETE(soft00, soft_i2c_init, soft_i2c_probe, |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 440 | soft_i2c_read, soft_i2c_write, NULL, |
| 441 | CONFIG_SYS_I2C_SOFT_SPEED, CONFIG_SYS_I2C_SOFT_SLAVE, |
| 442 | 0) |
| 443 | #if defined(I2C_SOFT_DECLARATIONS2) |
Dirk Eibach | 37b3325 | 2015-10-28 11:46:39 +0100 | [diff] [blame] | 444 | U_BOOT_I2C_ADAP_COMPLETE(soft01, soft_i2c_init, soft_i2c_probe, |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 445 | soft_i2c_read, soft_i2c_write, NULL, |
| 446 | CONFIG_SYS_I2C_SOFT_SPEED_2, |
| 447 | CONFIG_SYS_I2C_SOFT_SLAVE_2, |
| 448 | 1) |
| 449 | #endif |
| 450 | #if defined(I2C_SOFT_DECLARATIONS3) |
Dirk Eibach | 37b3325 | 2015-10-28 11:46:39 +0100 | [diff] [blame] | 451 | U_BOOT_I2C_ADAP_COMPLETE(soft02, soft_i2c_init, soft_i2c_probe, |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 452 | soft_i2c_read, soft_i2c_write, NULL, |
| 453 | CONFIG_SYS_I2C_SOFT_SPEED_3, |
| 454 | CONFIG_SYS_I2C_SOFT_SLAVE_3, |
| 455 | 2) |
| 456 | #endif |
| 457 | #if defined(I2C_SOFT_DECLARATIONS4) |
Dirk Eibach | 37b3325 | 2015-10-28 11:46:39 +0100 | [diff] [blame] | 458 | U_BOOT_I2C_ADAP_COMPLETE(soft03, soft_i2c_init, soft_i2c_probe, |
Heiko Schocher | ea818db | 2013-01-29 08:53:15 +0100 | [diff] [blame] | 459 | soft_i2c_read, soft_i2c_write, NULL, |
| 460 | CONFIG_SYS_I2C_SOFT_SPEED_4, |
| 461 | CONFIG_SYS_I2C_SOFT_SLAVE_4, |
| 462 | 3) |
| 463 | #endif |
Dirk Eibach | 7ed45d3 | 2015-10-28 11:46:35 +0100 | [diff] [blame] | 464 | #if defined(I2C_SOFT_DECLARATIONS5) |
Dirk Eibach | 37b3325 | 2015-10-28 11:46:39 +0100 | [diff] [blame] | 465 | U_BOOT_I2C_ADAP_COMPLETE(soft04, soft_i2c_init, soft_i2c_probe, |
Dirk Eibach | 7ed45d3 | 2015-10-28 11:46:35 +0100 | [diff] [blame] | 466 | soft_i2c_read, soft_i2c_write, NULL, |
| 467 | CONFIG_SYS_I2C_SOFT_SPEED_5, |
| 468 | CONFIG_SYS_I2C_SOFT_SLAVE_5, |
| 469 | 4) |
| 470 | #endif |
| 471 | #if defined(I2C_SOFT_DECLARATIONS6) |
Dirk Eibach | 37b3325 | 2015-10-28 11:46:39 +0100 | [diff] [blame] | 472 | U_BOOT_I2C_ADAP_COMPLETE(soft05, soft_i2c_init, soft_i2c_probe, |
Dirk Eibach | 7ed45d3 | 2015-10-28 11:46:35 +0100 | [diff] [blame] | 473 | soft_i2c_read, soft_i2c_write, NULL, |
| 474 | CONFIG_SYS_I2C_SOFT_SPEED_6, |
| 475 | CONFIG_SYS_I2C_SOFT_SLAVE_6, |
| 476 | 5) |
| 477 | #endif |
| 478 | #if defined(I2C_SOFT_DECLARATIONS7) |
Dirk Eibach | 37b3325 | 2015-10-28 11:46:39 +0100 | [diff] [blame] | 479 | U_BOOT_I2C_ADAP_COMPLETE(soft06, soft_i2c_init, soft_i2c_probe, |
Dirk Eibach | 7ed45d3 | 2015-10-28 11:46:35 +0100 | [diff] [blame] | 480 | soft_i2c_read, soft_i2c_write, NULL, |
| 481 | CONFIG_SYS_I2C_SOFT_SPEED_7, |
| 482 | CONFIG_SYS_I2C_SOFT_SLAVE_7, |
| 483 | 6) |
| 484 | #endif |
| 485 | #if defined(I2C_SOFT_DECLARATIONS8) |
Dirk Eibach | 37b3325 | 2015-10-28 11:46:39 +0100 | [diff] [blame] | 486 | U_BOOT_I2C_ADAP_COMPLETE(soft07, soft_i2c_init, soft_i2c_probe, |
Dirk Eibach | 7ed45d3 | 2015-10-28 11:46:35 +0100 | [diff] [blame] | 487 | soft_i2c_read, soft_i2c_write, NULL, |
| 488 | CONFIG_SYS_I2C_SOFT_SPEED_8, |
| 489 | CONFIG_SYS_I2C_SOFT_SLAVE_8, |
| 490 | 7) |
| 491 | #endif |
Dirk Eibach | 5c3b6dc | 2015-10-28 11:46:36 +0100 | [diff] [blame] | 492 | #if defined(I2C_SOFT_DECLARATIONS9) |
Dirk Eibach | 37b3325 | 2015-10-28 11:46:39 +0100 | [diff] [blame] | 493 | U_BOOT_I2C_ADAP_COMPLETE(soft08, soft_i2c_init, soft_i2c_probe, |
Dirk Eibach | 5c3b6dc | 2015-10-28 11:46:36 +0100 | [diff] [blame] | 494 | soft_i2c_read, soft_i2c_write, NULL, |
| 495 | CONFIG_SYS_I2C_SOFT_SPEED_9, |
| 496 | CONFIG_SYS_I2C_SOFT_SLAVE_9, |
| 497 | 8) |
| 498 | #endif |
| 499 | #if defined(I2C_SOFT_DECLARATIONS10) |
Dirk Eibach | 37b3325 | 2015-10-28 11:46:39 +0100 | [diff] [blame] | 500 | U_BOOT_I2C_ADAP_COMPLETE(soft09, soft_i2c_init, soft_i2c_probe, |
Dirk Eibach | 5c3b6dc | 2015-10-28 11:46:36 +0100 | [diff] [blame] | 501 | soft_i2c_read, soft_i2c_write, NULL, |
| 502 | CONFIG_SYS_I2C_SOFT_SPEED_10, |
| 503 | CONFIG_SYS_I2C_SOFT_SLAVE_10, |
| 504 | 9) |
| 505 | #endif |
| 506 | #if defined(I2C_SOFT_DECLARATIONS11) |
| 507 | U_BOOT_I2C_ADAP_COMPLETE(soft10, soft_i2c_init, soft_i2c_probe, |
| 508 | soft_i2c_read, soft_i2c_write, NULL, |
| 509 | CONFIG_SYS_I2C_SOFT_SPEED_11, |
| 510 | CONFIG_SYS_I2C_SOFT_SLAVE_11, |
| 511 | 10) |
| 512 | #endif |
| 513 | #if defined(I2C_SOFT_DECLARATIONS12) |
| 514 | U_BOOT_I2C_ADAP_COMPLETE(soft11, soft_i2c_init, soft_i2c_probe, |
| 515 | soft_i2c_read, soft_i2c_write, NULL, |
| 516 | CONFIG_SYS_I2C_SOFT_SPEED_12, |
| 517 | CONFIG_SYS_I2C_SOFT_SLAVE_12, |
| 518 | 11) |
| 519 | #endif |