1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000, 2001
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
11 * write delay. Also, there is no limit imposed on the number of bytes that can
12 * be transferred with a single read or write.
14 * Use the following configuration options to ensure no unneeded performance
15 * degradation (typical for EEPROM) is incured for FRAM memory:
17 * #define CONFIG_SYS_I2C_FRAM
18 * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
26 #include <eeprom_layout.h>
28 #ifndef CONFIG_SYS_I2C_SPEED
29 #define CONFIG_SYS_I2C_SPEED 50000
32 #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
33 #define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0
36 #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
37 #define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8
41 #define I2C_RXTX_LEN 128
44 #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
45 #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
48 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
49 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
51 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
52 * 0x00000nxx for EEPROM address selectors and page number at n.
54 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
55 #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
56 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
57 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
58 #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
62 __weak int eeprom_write_enable(unsigned dev_addr, int state)
67 void eeprom_init(int bus)
70 #if defined(CONFIG_MPC8XX_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
75 #if defined(CONFIG_SYS_I2C)
78 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
82 static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
87 blk_off = offset & 0xff; /* block offset */
88 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
89 addr[0] = offset >> 8; /* block number */
90 addr[1] = blk_off; /* block offset */
93 addr[0] = offset >> 16; /* block number */
94 addr[1] = offset >> 8; /* upper address octet */
95 addr[2] = blk_off; /* lower address octet */
97 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
99 addr[0] |= dev_addr; /* insert device address */
104 static int eeprom_len(unsigned offset, unsigned end)
106 unsigned len = end - offset;
109 * For a FRAM device there is no limit on the number of the
110 * bytes that can be ccessed with the single read or write
113 #if !defined(CONFIG_SYS_I2C_FRAM)
114 unsigned blk_off = offset & 0xff;
115 unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
117 if (maxlen > I2C_RXTX_LEN)
118 maxlen = I2C_RXTX_LEN;
127 static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
128 uchar *buffer, unsigned len, bool read)
133 #if defined(CONFIG_MPC8XX_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
135 spi_read(addr, alen, buffer, len);
137 spi_write(addr, alen, buffer, len);
140 #if defined(CONFIG_DM_I2C) && defined(CONFIG_SYS_I2C_EEPROM_BUS)
143 ret = i2c_get_chip_for_busnum(CONFIG_SYS_I2C_EEPROM_BUS, addr[0],
146 printf("%s: Cannot find udev for a bus %d\n", __func__,
147 CONFIG_SYS_I2C_EEPROM_BUS);
148 return CMD_RET_FAILURE;
152 ret = dm_i2c_read(dev, offset, buffer, len);
154 ret = dm_i2c_write(dev, offset, buffer, len);
156 #else /* Non DM I2C support - will be removed */
157 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
158 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
162 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
164 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
166 #endif /* CONFIG_DM_I2C && CONFIG_SYS_I2C_EEPROM_BUS */
168 ret = CMD_RET_FAILURE;
173 static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
174 unsigned cnt, bool read)
176 unsigned end = offset + cnt;
181 while (offset < end) {
182 alen = eeprom_addr(dev_addr, offset, addr);
184 len = eeprom_len(offset, end);
186 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
192 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
198 int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
201 * Read data until done or would cross a page boundary.
202 * We must write the address again when changing pages
203 * because the next page may be in a different device.
205 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
208 int eeprom_write(unsigned dev_addr, unsigned offset,
209 uchar *buffer, unsigned cnt)
213 eeprom_write_enable(dev_addr, 1);
216 * Write data until done or would cross a write page boundary.
217 * We must write the address again when changing pages
218 * because the address counter only increments within a page.
220 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
222 eeprom_write_enable(dev_addr, 0);
226 static int parse_numeric_param(char *str)
229 int value = simple_strtol(str, &endptr, 16);
231 return (*endptr != '\0') ? -1 : value;
235 * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
237 * @i2c_bus: address to store the i2c bus
238 * @i2c_addr: address to store the device i2c address
239 * @argc: count of command line arguments left to parse
240 * @argv: command line arguments left to parse
241 * @argc_no_bus_addr: argc value we expect to see when bus & addr aren't given
243 * @returns: number of arguments parsed or CMD_RET_USAGE if error
245 static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
246 char * const argv[], int argc_no_bus_addr)
248 int argc_no_bus = argc_no_bus_addr + 1;
249 int argc_bus_addr = argc_no_bus_addr + 2;
251 #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
252 if (argc == argc_no_bus_addr) {
254 *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
259 if (argc == argc_no_bus) {
261 *i2c_addr = parse_numeric_param(argv[0]);
266 if (argc == argc_bus_addr) {
267 *i2c_bus = parse_numeric_param(argv[0]);
268 *i2c_addr = parse_numeric_param(argv[1]);
273 return CMD_RET_USAGE;
276 #ifdef CONFIG_CMD_EEPROM_LAYOUT
278 __weak int eeprom_parse_layout_version(char *str)
280 return LAYOUT_VERSION_UNRECOGNIZED;
283 static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
292 EEPROM_ACTION_INVALID,
295 static enum eeprom_action parse_action(char *cmd)
297 if (!strncmp(cmd, "read", 4))
299 if (!strncmp(cmd, "write", 5))
301 #ifdef CONFIG_CMD_EEPROM_LAYOUT
302 if (!strncmp(cmd, "print", 5))
304 if (!strncmp(cmd, "update", 6))
305 return EEPROM_UPDATE;
308 return EEPROM_ACTION_INVALID;
311 static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
312 ulong i2c_addr, int layout_ver, char *key,
313 char *value, ulong addr, ulong off, ulong cnt)
316 const char *const fmt =
317 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
318 #ifdef CONFIG_CMD_EEPROM_LAYOUT
319 struct eeprom_layout layout;
322 if (action == EEPROM_ACTION_INVALID)
323 return CMD_RET_USAGE;
325 eeprom_init(i2c_bus);
326 if (action == EEPROM_READ) {
327 printf(fmt, i2c_addr, "read", addr, off, cnt);
329 rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
333 } else if (action == EEPROM_WRITE) {
334 printf(fmt, i2c_addr, "write", addr, off, cnt);
336 rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
342 #ifdef CONFIG_CMD_EEPROM_LAYOUT
343 rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
347 eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
350 if (action == EEPROM_PRINT) {
351 layout.print(&layout);
355 layout.update(&layout, key, value);
357 rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
363 #define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
364 int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
366 int layout_ver = LAYOUT_VERSION_AUTODETECT;
367 enum eeprom_action action = EEPROM_ACTION_INVALID;
368 int i2c_bus = -1, index = 0;
369 ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0;
371 char *field_name = "";
372 char *field_value = "";
375 return CMD_RET_USAGE;
377 NEXT_PARAM(argc, index); /* Skip program name */
379 action = parse_action(argv[index]);
380 NEXT_PARAM(argc, index);
382 if (action == EEPROM_ACTION_INVALID)
383 return CMD_RET_USAGE;
385 #ifdef CONFIG_CMD_EEPROM_LAYOUT
386 if (action == EEPROM_PRINT || action == EEPROM_UPDATE) {
387 if (!strcmp(argv[index], "-l")) {
388 NEXT_PARAM(argc, index);
389 layout_ver = eeprom_parse_layout_version(argv[index]);
390 NEXT_PARAM(argc, index);
398 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
402 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
406 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
410 /* Get compiler to stop whining */
411 return CMD_RET_USAGE;
414 if (ret == CMD_RET_USAGE)
418 NEXT_PARAM(argc, index);
420 if (action == EEPROM_READ || action == EEPROM_WRITE) {
421 addr = parse_numeric_param(argv[index]);
422 NEXT_PARAM(argc, index);
423 off = parse_numeric_param(argv[index]);
424 NEXT_PARAM(argc, index);
425 cnt = parse_numeric_param(argv[index]);
428 #ifdef CONFIG_CMD_EEPROM_LAYOUT
429 if (action == EEPROM_UPDATE) {
430 field_name = argv[index];
431 NEXT_PARAM(argc, index);
432 field_value = argv[index];
433 NEXT_PARAM(argc, index);
437 return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
438 field_name, field_value, addr, off, cnt);
442 eeprom, 8, 1, do_eeprom,
444 "read <bus> <devaddr> addr off cnt\n"
445 "eeprom write <bus> <devaddr> addr off cnt\n"
446 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
447 #ifdef CONFIG_CMD_EEPROM_LAYOUT
449 "eeprom print [-l <layout_version>] <bus> <devaddr>\n"
450 " - Print layout fields and their data in human readable format\n"
451 "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n"
452 " - Update a specific eeprom field with new data.\n"
453 " The new data must be written in the same human readable format as shown by the print command.\n"
456 "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
457 "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
458 "The values which can be provided with the -l option are:\n"
459 CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"