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