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