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 * Set CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS to 0
28 #include <i2c_eeprom.h>
29 #include <eeprom_layout.h>
30 #include <linux/delay.h>
33 #define I2C_RXTX_LEN 128
36 #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
37 #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
39 #if CONFIG_IS_ENABLED(DM_I2C)
40 static int eeprom_i2c_bus;
43 __weak int eeprom_write_enable(unsigned dev_addr, int state)
48 void eeprom_init(int bus)
51 #if CONFIG_IS_ENABLED(DM_I2C)
53 #elif CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
56 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
61 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
62 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
64 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
65 * 0x00000nxx for EEPROM address selectors and page number at n.
67 static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
72 blk_off = offset & 0xff; /* block offset */
73 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
74 addr[0] = offset >> 8; /* block number */
75 addr[1] = blk_off; /* block offset */
78 addr[0] = offset >> 16; /* block number */
79 addr[1] = offset >> 8; /* upper address octet */
80 addr[2] = blk_off; /* lower address octet */
82 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
84 addr[0] |= dev_addr; /* insert device address */
89 static int eeprom_len(unsigned offset, unsigned end)
91 unsigned len = end - offset;
94 * For a FRAM device there is no limit on the number of the
95 * bytes that can be accessed with the single read or write
98 #if !defined(CONFIG_SYS_I2C_FRAM)
99 unsigned blk_off = offset & 0xff;
100 unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
102 if (maxlen > I2C_RXTX_LEN)
103 maxlen = I2C_RXTX_LEN;
112 static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
113 uchar *buffer, unsigned len, bool read)
117 #if CONFIG_IS_ENABLED(DM_I2C)
120 ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0],
123 printf("%s: Cannot find udev for a bus %d\n", __func__,
125 return CMD_RET_FAILURE;
129 ret = dm_i2c_read(dev, offset, buffer, len);
131 ret = dm_i2c_write(dev, offset, buffer, len);
133 #else /* Non DM I2C support - will be removed */
136 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
138 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
139 #endif /* CONFIG_DM_I2C */
141 ret = CMD_RET_FAILURE;
146 static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
147 unsigned cnt, bool read)
149 unsigned end = offset + cnt;
154 #if !CONFIG_IS_ENABLED(DM_I2C) && defined(CONFIG_SYS_I2C_EEPROM_BUS)
155 eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS);
158 while (offset < end) {
159 alen = eeprom_addr(dev_addr, offset, addr);
161 len = eeprom_len(offset, end);
163 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
168 #if CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS > 0
170 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
177 int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
180 * Read data until done or would cross a page boundary.
181 * We must write the address again when changing pages
182 * because the next page may be in a different device.
184 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
187 int eeprom_write(unsigned dev_addr, unsigned offset,
188 uchar *buffer, unsigned cnt)
192 eeprom_write_enable(dev_addr, 1);
195 * Write data until done or would cross a write page boundary.
196 * We must write the address again when changing pages
197 * because the address counter only increments within a page.
199 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
201 eeprom_write_enable(dev_addr, 0);
205 static long parse_numeric_param(char *str)
208 long value = simple_strtol(str, &endptr, 16);
210 return (*endptr != '\0') ? -1 : value;
213 struct eeprom_dev_spec {
214 #if CONFIG_IS_ENABLED(I2C_EEPROM)
221 static void eeprom_dev_spec_init(struct eeprom_dev_spec *dev)
223 #if CONFIG_IS_ENABLED(I2C_EEPROM)
226 eeprom_init(dev->i2c_bus);
229 static int eeprom_dev_spec_read(struct eeprom_dev_spec *dev,
230 unsigned offset, uchar *buffer, unsigned cnt)
232 #if CONFIG_IS_ENABLED(I2C_EEPROM)
234 return i2c_eeprom_read(dev->dev, offset, buffer, cnt);
236 return eeprom_read(dev->i2c_addr, offset, buffer, cnt);
239 static int eeprom_dev_spec_write(struct eeprom_dev_spec *dev,
240 unsigned offset, uchar *buffer, unsigned cnt)
242 #if CONFIG_IS_ENABLED(I2C_EEPROM)
244 return i2c_eeprom_write(dev->dev, offset, buffer, cnt);
246 return eeprom_write(dev->i2c_addr, offset, buffer, cnt);
250 * parse_eeprom_dev_spec - parse the eeprom device specifier
252 * @dev: pointer to eeprom device specifier
253 * @argc: count of command line arguments that can be used to parse
254 * the device specifier
255 * @argv: command line arguments left to parse
257 * @returns: number of arguments parsed or CMD_RET_USAGE if error
259 static int parse_eeprom_dev_spec(struct eeprom_dev_spec *dev, int argc,
262 #if CONFIG_IS_ENABLED(I2C_EEPROM)
264 if (!uclass_first_device_err(UCLASS_I2C_EEPROM, &dev->dev))
269 if (!uclass_get_device_by_name(UCLASS_I2C_EEPROM, argv[0],
274 * If we could not find the device by name and the parameter is
275 * not numeric (and so won't be handled later), fail.
277 if (parse_numeric_param(argv[0]) == -1) {
278 printf("Can't get eeprom device: %s\n", argv[0]);
279 return CMD_RET_USAGE;
284 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR
287 dev->i2c_addr = CONFIG_SYS_I2C_EEPROM_ADDR;
294 dev->i2c_addr = parse_numeric_param(argv[0]);
300 dev->i2c_bus = parse_numeric_param(argv[0]);
301 dev->i2c_addr = parse_numeric_param(argv[1]);
306 return CMD_RET_USAGE;
309 #ifdef CONFIG_CMD_EEPROM_LAYOUT
311 #ifdef CONFIG_EEPROM_LAYOUT_VERSIONS
312 __weak int eeprom_parse_layout_version(char *str)
314 return LAYOUT_VERSION_UNRECOGNIZED;
318 static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
328 EEPROM_ACTION_INVALID,
331 static enum eeprom_action parse_action(char *cmd)
333 #if CONFIG_IS_ENABLED(I2C_EEPROM)
334 if (!strncmp(cmd, "list", 4))
337 if (!strncmp(cmd, "read", 4))
339 if (!strncmp(cmd, "write", 5))
341 #ifdef CONFIG_CMD_EEPROM_LAYOUT
342 if (!strncmp(cmd, "print", 5))
344 if (!strncmp(cmd, "update", 6))
345 return EEPROM_UPDATE;
348 return EEPROM_ACTION_INVALID;
351 #if CONFIG_IS_ENABLED(I2C_EEPROM)
352 static int do_eeprom_list(void)
358 err = uclass_get(UCLASS_I2C_EEPROM, &uc);
360 return CMD_RET_FAILURE;
362 uclass_foreach_dev(dev, uc)
363 printf("%s (%s)\n", dev->name, dev->driver->name);
365 return CMD_RET_SUCCESS;
369 static int do_eeprom_rw(struct eeprom_dev_spec *dev, bool read,
370 ulong addr, ulong off, ulong cnt)
372 const char *const fmt =
373 "\nEEPROM @0x%lX %s: addr 0x%08lx off 0x%04lx count %ld ... ";
374 uchar *memloc = (uchar *)addr;
377 printf(fmt, dev->i2c_addr, read ? "read" : "write", addr, off, cnt);
379 ret = eeprom_dev_spec_read(dev, off, memloc, cnt);
381 ret = eeprom_dev_spec_write(dev, off, memloc, cnt);
387 #ifdef CONFIG_CMD_EEPROM_LAYOUT
389 static int do_eeprom_layout(struct eeprom_dev_spec *dev, int layout_ver,
390 struct eeprom_layout *layout)
392 eeprom_layout_setup(layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
395 return eeprom_dev_spec_read(dev, 0, eeprom_buf, layout->data_size);
398 static int do_eeprom_print(struct eeprom_dev_spec *dev, int layout_ver)
400 struct eeprom_layout layout;
403 ret = do_eeprom_layout(dev, layout_ver, &layout);
407 layout.print(&layout);
412 static int do_eeprom_update(struct eeprom_dev_spec *dev, int layout_ver,
413 char *key, char *value)
415 struct eeprom_layout layout;
418 ret = do_eeprom_layout(dev, layout_ver, &layout);
422 ret = layout.update(&layout, key, value);
424 return CMD_RET_FAILURE;
426 return eeprom_dev_spec_write(dev, 0, layout.data, layout.data_size);
431 static int eeprom_action_expected_argc(enum eeprom_action action)
444 return CMD_RET_USAGE;
448 #define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
449 int do_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
451 enum eeprom_action action = EEPROM_ACTION_INVALID;
452 struct eeprom_dev_spec dev;
453 ulong addr = 0, cnt = 0, off = 0;
455 #ifdef CONFIG_CMD_EEPROM_LAYOUT
456 char *field_name = "";
457 char *field_value = "";
458 int layout_ver = LAYOUT_VERSION_AUTODETECT;
462 return CMD_RET_USAGE;
464 NEXT_PARAM(argc, index); /* Skip program name */
466 action = parse_action(argv[index]);
467 NEXT_PARAM(argc, index);
469 if (action == EEPROM_ACTION_INVALID)
470 return CMD_RET_USAGE;
472 #if CONFIG_IS_ENABLED(I2C_EEPROM)
473 if (action == EEPROM_LIST)
474 return do_eeprom_list();
477 #ifdef CONFIG_EEPROM_LAYOUT_VERSIONS
478 if (action == EEPROM_PRINT || action == EEPROM_UPDATE) {
479 if (!strcmp(argv[index], "-l")) {
480 NEXT_PARAM(argc, index);
481 layout_ver = eeprom_parse_layout_version(argv[index]);
482 NEXT_PARAM(argc, index);
487 ret = parse_eeprom_dev_spec(&dev,
488 argc - eeprom_action_expected_argc(action),
490 if (ret == CMD_RET_USAGE)
494 NEXT_PARAM(argc, index);
496 if (action == EEPROM_READ || action == EEPROM_WRITE) {
497 addr = parse_numeric_param(argv[index]);
498 NEXT_PARAM(argc, index);
499 off = parse_numeric_param(argv[index]);
500 NEXT_PARAM(argc, index);
501 cnt = parse_numeric_param(argv[index]);
504 #ifdef CONFIG_CMD_EEPROM_LAYOUT
505 if (action == EEPROM_UPDATE) {
506 field_name = argv[index];
507 NEXT_PARAM(argc, index);
508 field_value = argv[index];
509 NEXT_PARAM(argc, index);
513 eeprom_dev_spec_init(&dev);
518 return do_eeprom_rw(&dev, action == EEPROM_READ,
520 #ifdef CONFIG_CMD_EEPROM_LAYOUT
522 return do_eeprom_print(&dev, layout_ver);
524 return do_eeprom_update(&dev, layout_ver,
525 field_name, field_value);
528 return CMD_RET_USAGE;
532 #ifdef CONFIG_EEPROM_LAYOUT_VERSIONS
533 #define EEPROM_LAYOUT_SPEC "[-l <layout_version>] "
535 #define EEPROM_LAYOUT_SPEC ""
538 #if CONFIG_IS_ENABLED(I2C_EEPROM)
539 # define EEPROM_DEV_SPEC "[device_specifier]"
541 # define EEPROM_DEV_SPEC "[[bus] devaddr]"
545 eeprom, 8, 1, do_eeprom,
547 #if CONFIG_IS_ENABLED(I2C_EEPROM)
551 "read " EEPROM_DEV_SPEC " addr off cnt\n"
552 "eeprom write " EEPROM_DEV_SPEC " addr off cnt\n"
553 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
554 #ifdef CONFIG_CMD_EEPROM_LAYOUT
556 "eeprom print " EEPROM_LAYOUT_SPEC EEPROM_DEV_SPEC "\n"
557 " - Print layout fields and their data in human readable format\n"
558 "eeprom update " EEPROM_LAYOUT_SPEC EEPROM_DEV_SPEC " field_name field_value\n"
559 " - Update a specific eeprom field with new data.\n"
560 " The new data must be written in the same human readable format as shown by the print command."
562 #if CONFIG_IS_ENABLED(I2C_EEPROM)
564 "DEVICE SPECIFIER - the eeprom device can be specified\n"
565 " [dev_name] - by device name (devices can listed with the eeprom list command)\n"
566 " [[bus] devaddr] - or by I2C bus and I2C device address\n"
567 "If no device specifier is given, the first driver-model found device is used."
569 #ifdef CONFIG_EEPROM_LAYOUT_VERSIONS
572 "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
573 "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
574 "The values which can be provided with the -l option are:\n"
575 CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"