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