Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000, 2001 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 7 | /* |
| 8 | * Support for read and write access to EEPROM like memory devices. This |
| 9 | * includes regular EEPROM as well as FRAM (ferroelectic nonvolaile RAM). |
| 10 | * FRAM devices read and write data at bus speed. In particular, there is no |
Peter Meerwald | e506a00 | 2012-02-08 05:31:53 +0000 | [diff] [blame] | 11 | * write delay. Also, there is no limit imposed on the number of bytes that can |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 12 | * be transferred with a single read or write. |
Wolfgang Denk | 6617aae | 2005-08-19 00:46:54 +0200 | [diff] [blame] | 13 | * |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 14 | * Use the following configuration options to ensure no unneeded performance |
| 15 | * degradation (typical for EEPROM) is incured for FRAM memory: |
Wolfgang Denk | 6617aae | 2005-08-19 00:46:54 +0200 | [diff] [blame] | 16 | * |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 17 | * #define CONFIG_SYS_I2C_FRAM |
| 18 | * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 22 | #include <common.h> |
| 23 | #include <config.h> |
| 24 | #include <command.h> |
Simon Glass | cb3ef68 | 2019-11-14 12:57:50 -0700 | [diff] [blame] | 25 | #include <eeprom.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 26 | #include <i2c.h> |
Nikita Kiryanov | e7c2729 | 2016-04-16 17:55:12 +0300 | [diff] [blame] | 27 | #include <eeprom_layout.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 28 | |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 29 | #ifndef CONFIG_SYS_I2C_SPEED |
| 30 | #define CONFIG_SYS_I2C_SPEED 50000 |
Stefan Roese | 98f4a3d | 2005-09-22 09:04:17 +0200 | [diff] [blame] | 31 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 32 | |
Marek Vasut | d738746 | 2015-11-10 20:53:26 +0100 | [diff] [blame] | 33 | #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS |
| 34 | #define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0 |
| 35 | #endif |
| 36 | |
Marek Vasut | 6717e3c | 2015-11-10 20:53:28 +0100 | [diff] [blame] | 37 | #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS |
| 38 | #define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8 |
| 39 | #endif |
| 40 | |
Mario Six | a6e7b77 | 2016-04-20 10:44:52 +0200 | [diff] [blame] | 41 | #ifndef I2C_RXTX_LEN |
| 42 | #define I2C_RXTX_LEN 128 |
| 43 | #endif |
| 44 | |
Marek Vasut | 6717e3c | 2015-11-10 20:53:28 +0100 | [diff] [blame] | 45 | #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS) |
| 46 | #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1)) |
| 47 | |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 48 | /* |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 49 | * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 50 | * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM. |
| 51 | * |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 52 | * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 53 | * 0x00000nxx for EEPROM address selectors and page number at n. |
| 54 | */ |
Heiko Schocher | 548738b | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 55 | #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C) |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 56 | #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \ |
Marek Vasut | d4e69e6 | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 57 | (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \ |
| 58 | (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 59 | #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2 |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 60 | #endif |
| 61 | #endif |
| 62 | |
Simon Goldschmidt | 4f25617 | 2019-03-26 13:38:52 +0100 | [diff] [blame] | 63 | #if defined(CONFIG_DM_I2C) |
| 64 | int eeprom_i2c_bus; |
| 65 | #endif |
| 66 | |
Marek Vasut | 52cd47c | 2015-11-10 20:53:25 +0100 | [diff] [blame] | 67 | __weak int eeprom_write_enable(unsigned dev_addr, int state) |
| 68 | { |
| 69 | return 0; |
| 70 | } |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 71 | |
Marek Vasut | 354e3ed | 2015-11-10 20:53:31 +0100 | [diff] [blame] | 72 | void eeprom_init(int bus) |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 73 | { |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 74 | /* I2C EEPROM */ |
Simon Goldschmidt | 4f25617 | 2019-03-26 13:38:52 +0100 | [diff] [blame] | 75 | #if defined(CONFIG_DM_I2C) |
| 76 | eeprom_i2c_bus = bus; |
| 77 | #elif defined(CONFIG_SYS_I2C) |
Marek Vasut | 354e3ed | 2015-11-10 20:53:31 +0100 | [diff] [blame] | 78 | if (bus >= 0) |
| 79 | i2c_set_bus_num(bus); |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 80 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 81 | #endif |
| 82 | } |
| 83 | |
Marek Vasut | 02c321c | 2015-11-10 20:53:24 +0100 | [diff] [blame] | 84 | static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr) |
| 85 | { |
| 86 | unsigned blk_off; |
| 87 | int alen; |
| 88 | |
| 89 | blk_off = offset & 0xff; /* block offset */ |
| 90 | #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 |
| 91 | addr[0] = offset >> 8; /* block number */ |
| 92 | addr[1] = blk_off; /* block offset */ |
| 93 | alen = 2; |
| 94 | #else |
| 95 | addr[0] = offset >> 16; /* block number */ |
| 96 | addr[1] = offset >> 8; /* upper address octet */ |
| 97 | addr[2] = blk_off; /* lower address octet */ |
| 98 | alen = 3; |
| 99 | #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */ |
| 100 | |
| 101 | addr[0] |= dev_addr; /* insert device address */ |
| 102 | |
| 103 | return alen; |
| 104 | } |
| 105 | |
Marek Vasut | 39b6f98 | 2015-11-10 20:53:29 +0100 | [diff] [blame] | 106 | static int eeprom_len(unsigned offset, unsigned end) |
| 107 | { |
| 108 | unsigned len = end - offset; |
| 109 | |
| 110 | /* |
| 111 | * For a FRAM device there is no limit on the number of the |
Lukasz Majewski | 35087fb | 2019-06-09 22:54:38 +0200 | [diff] [blame] | 112 | * bytes that can be accessed with the single read or write |
Marek Vasut | 39b6f98 | 2015-11-10 20:53:29 +0100 | [diff] [blame] | 113 | * operation. |
| 114 | */ |
| 115 | #if !defined(CONFIG_SYS_I2C_FRAM) |
| 116 | unsigned blk_off = offset & 0xff; |
| 117 | unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off); |
| 118 | |
| 119 | if (maxlen > I2C_RXTX_LEN) |
| 120 | maxlen = I2C_RXTX_LEN; |
| 121 | |
| 122 | if (len > maxlen) |
| 123 | len = maxlen; |
| 124 | #endif |
| 125 | |
| 126 | return len; |
| 127 | } |
| 128 | |
Marek Vasut | 9132088 | 2015-11-10 20:53:23 +0100 | [diff] [blame] | 129 | static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen, |
| 130 | uchar *buffer, unsigned len, bool read) |
| 131 | { |
| 132 | int ret = 0; |
| 133 | |
Simon Goldschmidt | 4f25617 | 2019-03-26 13:38:52 +0100 | [diff] [blame] | 134 | #if defined(CONFIG_DM_I2C) |
Lukasz Majewski | 0c07a9b | 2018-11-21 23:40:43 +0100 | [diff] [blame] | 135 | struct udevice *dev; |
| 136 | |
Simon Goldschmidt | 4f25617 | 2019-03-26 13:38:52 +0100 | [diff] [blame] | 137 | ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0], |
Lukasz Majewski | 0c07a9b | 2018-11-21 23:40:43 +0100 | [diff] [blame] | 138 | alen - 1, &dev); |
| 139 | if (ret) { |
| 140 | printf("%s: Cannot find udev for a bus %d\n", __func__, |
Simon Goldschmidt | 4f25617 | 2019-03-26 13:38:52 +0100 | [diff] [blame] | 141 | eeprom_i2c_bus); |
Lukasz Majewski | 0c07a9b | 2018-11-21 23:40:43 +0100 | [diff] [blame] | 142 | return CMD_RET_FAILURE; |
| 143 | } |
| 144 | |
| 145 | if (read) |
| 146 | ret = dm_i2c_read(dev, offset, buffer, len); |
| 147 | else |
| 148 | ret = dm_i2c_write(dev, offset, buffer, len); |
| 149 | |
| 150 | #else /* Non DM I2C support - will be removed */ |
Marek Vasut | 9132088 | 2015-11-10 20:53:23 +0100 | [diff] [blame] | 151 | |
| 152 | if (read) |
| 153 | ret = i2c_read(addr[0], offset, alen - 1, buffer, len); |
| 154 | else |
| 155 | ret = i2c_write(addr[0], offset, alen - 1, buffer, len); |
Simon Goldschmidt | 4f25617 | 2019-03-26 13:38:52 +0100 | [diff] [blame] | 156 | #endif /* CONFIG_DM_I2C */ |
Lukasz Majewski | 0c07a9b | 2018-11-21 23:40:43 +0100 | [diff] [blame] | 157 | if (ret) |
| 158 | ret = CMD_RET_FAILURE; |
| 159 | |
Marek Vasut | 9132088 | 2015-11-10 20:53:23 +0100 | [diff] [blame] | 160 | return ret; |
| 161 | } |
| 162 | |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 163 | static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer, |
| 164 | unsigned cnt, bool read) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 165 | { |
| 166 | unsigned end = offset + cnt; |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 167 | unsigned alen, len; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 168 | int rcode = 0; |
Marek Vasut | 02c321c | 2015-11-10 20:53:24 +0100 | [diff] [blame] | 169 | uchar addr[3]; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 170 | |
Simon Goldschmidt | 4f25617 | 2019-03-26 13:38:52 +0100 | [diff] [blame] | 171 | #if defined(CONFIG_SYS_I2C_EEPROM_BUS) |
| 172 | eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS); |
| 173 | #endif |
| 174 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 175 | while (offset < end) { |
Marek Vasut | 02c321c | 2015-11-10 20:53:24 +0100 | [diff] [blame] | 176 | alen = eeprom_addr(dev_addr, offset, addr); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 177 | |
Marek Vasut | 39b6f98 | 2015-11-10 20:53:29 +0100 | [diff] [blame] | 178 | len = eeprom_len(offset, end); |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 179 | |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 180 | rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read); |
Marek Vasut | 9132088 | 2015-11-10 20:53:23 +0100 | [diff] [blame] | 181 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 182 | buffer += len; |
| 183 | offset += len; |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 184 | |
| 185 | if (!read) |
| 186 | udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 187 | } |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 188 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 189 | return rcode; |
| 190 | } |
| 191 | |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 192 | int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 193 | { |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Read data until done or would cross a page boundary. |
| 196 | * We must write the address again when changing pages |
| 197 | * because the next page may be in a different device. |
| 198 | */ |
| 199 | return eeprom_rw(dev_addr, offset, buffer, cnt, 1); |
| 200 | } |
| 201 | |
Marek Vasut | d4e69e6 | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 202 | int eeprom_write(unsigned dev_addr, unsigned offset, |
| 203 | uchar *buffer, unsigned cnt) |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 204 | { |
| 205 | int ret; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 206 | |
Marek Vasut | 52cd47c | 2015-11-10 20:53:25 +0100 | [diff] [blame] | 207 | eeprom_write_enable(dev_addr, 1); |
| 208 | |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 209 | /* |
| 210 | * Write data until done or would cross a write page boundary. |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 211 | * We must write the address again when changing pages |
| 212 | * because the address counter only increments within a page. |
| 213 | */ |
Alexey Brodkin | 52bc7c7 | 2015-12-14 18:45:34 +0300 | [diff] [blame] | 214 | ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0); |
Marek Vasut | 52cd47c | 2015-11-10 20:53:25 +0100 | [diff] [blame] | 215 | |
| 216 | eeprom_write_enable(dev_addr, 0); |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 217 | return ret; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Nikita Kiryanov | c40f037 | 2016-04-16 17:55:11 +0300 | [diff] [blame] | 220 | static int parse_numeric_param(char *str) |
| 221 | { |
| 222 | char *endptr; |
| 223 | int value = simple_strtol(str, &endptr, 16); |
| 224 | |
| 225 | return (*endptr != '\0') ? -1 : value; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters |
| 230 | * |
| 231 | * @i2c_bus: address to store the i2c bus |
| 232 | * @i2c_addr: address to store the device i2c address |
| 233 | * @argc: count of command line arguments left to parse |
| 234 | * @argv: command line arguments left to parse |
| 235 | * @argc_no_bus_addr: argc value we expect to see when bus & addr aren't given |
| 236 | * |
| 237 | * @returns: number of arguments parsed or CMD_RET_USAGE if error |
| 238 | */ |
| 239 | static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc, |
| 240 | char * const argv[], int argc_no_bus_addr) |
| 241 | { |
| 242 | int argc_no_bus = argc_no_bus_addr + 1; |
| 243 | int argc_bus_addr = argc_no_bus_addr + 2; |
| 244 | |
| 245 | #ifdef CONFIG_SYS_DEF_EEPROM_ADDR |
| 246 | if (argc == argc_no_bus_addr) { |
| 247 | *i2c_bus = -1; |
| 248 | *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR; |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | #endif |
| 253 | if (argc == argc_no_bus) { |
| 254 | *i2c_bus = -1; |
| 255 | *i2c_addr = parse_numeric_param(argv[0]); |
| 256 | |
| 257 | return 1; |
| 258 | } |
| 259 | |
| 260 | if (argc == argc_bus_addr) { |
| 261 | *i2c_bus = parse_numeric_param(argv[0]); |
| 262 | *i2c_addr = parse_numeric_param(argv[1]); |
| 263 | |
| 264 | return 2; |
| 265 | } |
| 266 | |
| 267 | return CMD_RET_USAGE; |
| 268 | } |
| 269 | |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 270 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 271 | |
| 272 | __weak int eeprom_parse_layout_version(char *str) |
| 273 | { |
| 274 | return LAYOUT_VERSION_UNRECOGNIZED; |
| 275 | } |
| 276 | |
| 277 | static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE]; |
| 278 | |
Nikita Kiryanov | e7c2729 | 2016-04-16 17:55:12 +0300 | [diff] [blame] | 279 | #endif |
| 280 | |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 281 | enum eeprom_action { |
Nikita Kiryanov | e7c2729 | 2016-04-16 17:55:12 +0300 | [diff] [blame] | 282 | EEPROM_READ, |
| 283 | EEPROM_WRITE, |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 284 | EEPROM_PRINT, |
| 285 | EEPROM_UPDATE, |
| 286 | EEPROM_ACTION_INVALID, |
| 287 | }; |
| 288 | |
| 289 | static enum eeprom_action parse_action(char *cmd) |
| 290 | { |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 291 | if (!strncmp(cmd, "read", 4)) |
| 292 | return EEPROM_READ; |
| 293 | if (!strncmp(cmd, "write", 5)) |
| 294 | return EEPROM_WRITE; |
| 295 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 296 | if (!strncmp(cmd, "print", 5)) |
| 297 | return EEPROM_PRINT; |
| 298 | if (!strncmp(cmd, "update", 6)) |
| 299 | return EEPROM_UPDATE; |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 300 | #endif |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 301 | |
| 302 | return EEPROM_ACTION_INVALID; |
| 303 | } |
| 304 | |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 305 | static int eeprom_execute_command(enum eeprom_action action, int i2c_bus, |
Nikita Kiryanov | e7c2729 | 2016-04-16 17:55:12 +0300 | [diff] [blame] | 306 | ulong i2c_addr, int layout_ver, char *key, |
| 307 | char *value, ulong addr, ulong off, ulong cnt) |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 308 | { |
Nikita Kiryanov | e7c2729 | 2016-04-16 17:55:12 +0300 | [diff] [blame] | 309 | int rcode = 0; |
| 310 | const char *const fmt = |
Klaus H. Sorensen | 6478a7e | 2019-12-11 11:18:29 +0000 | [diff] [blame] | 311 | "\nEEPROM @0x%lX %s: addr 0x%08lx off 0x%04lx count %ld ... "; |
Nikita Kiryanov | e7c2729 | 2016-04-16 17:55:12 +0300 | [diff] [blame] | 312 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 313 | struct eeprom_layout layout; |
Nikita Kiryanov | e7c2729 | 2016-04-16 17:55:12 +0300 | [diff] [blame] | 314 | #endif |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 315 | |
| 316 | if (action == EEPROM_ACTION_INVALID) |
| 317 | return CMD_RET_USAGE; |
| 318 | |
| 319 | eeprom_init(i2c_bus); |
Nikita Kiryanov | e7c2729 | 2016-04-16 17:55:12 +0300 | [diff] [blame] | 320 | if (action == EEPROM_READ) { |
| 321 | printf(fmt, i2c_addr, "read", addr, off, cnt); |
| 322 | |
| 323 | rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt); |
| 324 | |
| 325 | puts("done\n"); |
| 326 | return rcode; |
| 327 | } else if (action == EEPROM_WRITE) { |
| 328 | printf(fmt, i2c_addr, "write", addr, off, cnt); |
| 329 | |
| 330 | rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt); |
| 331 | |
| 332 | puts("done\n"); |
| 333 | return rcode; |
| 334 | } |
| 335 | |
| 336 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 337 | rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE); |
| 338 | if (rcode < 0) |
| 339 | return rcode; |
| 340 | |
| 341 | eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE, |
| 342 | layout_ver); |
| 343 | |
| 344 | if (action == EEPROM_PRINT) { |
| 345 | layout.print(&layout); |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | layout.update(&layout, key, value); |
| 350 | |
| 351 | rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE); |
Nikita Kiryanov | e7c2729 | 2016-04-16 17:55:12 +0300 | [diff] [blame] | 352 | #endif |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 353 | |
| 354 | return rcode; |
| 355 | } |
| 356 | |
| 357 | #define NEXT_PARAM(argc, index) { (argc)--; (index)++; } |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 358 | int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 359 | { |
| 360 | int layout_ver = LAYOUT_VERSION_AUTODETECT; |
| 361 | enum eeprom_action action = EEPROM_ACTION_INVALID; |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 362 | int i2c_bus = -1, index = 0; |
| 363 | ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0; |
| 364 | int ret; |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 365 | char *field_name = ""; |
| 366 | char *field_value = ""; |
| 367 | |
| 368 | if (argc <= 1) |
| 369 | return CMD_RET_USAGE; |
| 370 | |
| 371 | NEXT_PARAM(argc, index); /* Skip program name */ |
| 372 | |
| 373 | action = parse_action(argv[index]); |
| 374 | NEXT_PARAM(argc, index); |
| 375 | |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 376 | if (action == EEPROM_ACTION_INVALID) |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 377 | return CMD_RET_USAGE; |
| 378 | |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 379 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
| 380 | if (action == EEPROM_PRINT || action == EEPROM_UPDATE) { |
| 381 | if (!strcmp(argv[index], "-l")) { |
| 382 | NEXT_PARAM(argc, index); |
| 383 | layout_ver = eeprom_parse_layout_version(argv[index]); |
| 384 | NEXT_PARAM(argc, index); |
| 385 | } |
| 386 | } |
| 387 | #endif |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 388 | |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 389 | switch (action) { |
| 390 | case EEPROM_READ: |
| 391 | case EEPROM_WRITE: |
| 392 | ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc, |
| 393 | argv + index, 3); |
| 394 | break; |
| 395 | case EEPROM_PRINT: |
| 396 | ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc, |
| 397 | argv + index, 0); |
| 398 | break; |
| 399 | case EEPROM_UPDATE: |
| 400 | ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc, |
| 401 | argv + index, 2); |
| 402 | break; |
| 403 | default: |
| 404 | /* Get compiler to stop whining */ |
| 405 | return CMD_RET_USAGE; |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 406 | } |
| 407 | |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 408 | if (ret == CMD_RET_USAGE) |
| 409 | return ret; |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 410 | |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 411 | while (ret--) |
| 412 | NEXT_PARAM(argc, index); |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 413 | |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 414 | if (action == EEPROM_READ || action == EEPROM_WRITE) { |
| 415 | addr = parse_numeric_param(argv[index]); |
| 416 | NEXT_PARAM(argc, index); |
| 417 | off = parse_numeric_param(argv[index]); |
| 418 | NEXT_PARAM(argc, index); |
| 419 | cnt = parse_numeric_param(argv[index]); |
| 420 | } |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 421 | |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 422 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
| 423 | if (action == EEPROM_UPDATE) { |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 424 | field_name = argv[index]; |
| 425 | NEXT_PARAM(argc, index); |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 426 | field_value = argv[index]; |
| 427 | NEXT_PARAM(argc, index); |
| 428 | } |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 429 | #endif |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 430 | |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 431 | return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver, |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 432 | field_name, field_value, addr, off, cnt); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 433 | } |
Heiko Schocher | 548738b | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 434 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 435 | U_BOOT_CMD( |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 436 | eeprom, 8, 1, do_eeprom, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 437 | "EEPROM sub-system", |
Marek Vasut | d4fec4e | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 438 | "read <bus> <devaddr> addr off cnt\n" |
| 439 | "eeprom write <bus> <devaddr> addr off cnt\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 440 | " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'" |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 441 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
| 442 | "\n" |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 443 | "eeprom print [-l <layout_version>] <bus> <devaddr>\n" |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 444 | " - Print layout fields and their data in human readable format\n" |
Nikita Kiryanov | 3dc9be8 | 2016-04-16 17:55:13 +0300 | [diff] [blame] | 445 | "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n" |
Nikita Kiryanov | aa9e604 | 2016-04-16 17:55:03 +0300 | [diff] [blame] | 446 | " - Update a specific eeprom field with new data.\n" |
| 447 | " The new data must be written in the same human readable format as shown by the print command.\n" |
| 448 | "\n" |
| 449 | "LAYOUT VERSIONS\n" |
| 450 | "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n" |
| 451 | "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n" |
| 452 | "The values which can be provided with the -l option are:\n" |
| 453 | CONFIG_EEPROM_LAYOUT_HELP_STRING"\n" |
| 454 | #endif |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 455 | ) |