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