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