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