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