1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000, 2001
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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
27 #include <eeprom_layout.h>
29 #ifndef CONFIG_SYS_I2C_SPEED
30 #define CONFIG_SYS_I2C_SPEED 50000
33 #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
34 #define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0
37 #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
38 #define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8
42 #define I2C_RXTX_LEN 128
45 #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
46 #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
49 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
50 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
52 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
53 * 0x00000nxx for EEPROM address selectors and page number at n.
55 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
56 #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
57 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
58 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
59 #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
63 #if defined(CONFIG_DM_I2C)
67 __weak int eeprom_write_enable(unsigned dev_addr, int state)
72 void eeprom_init(int bus)
75 #if defined(CONFIG_DM_I2C)
77 #elif defined(CONFIG_SYS_I2C)
80 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
84 static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
89 blk_off = offset & 0xff; /* block offset */
90 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
91 addr[0] = offset >> 8; /* block number */
92 addr[1] = blk_off; /* block offset */
95 addr[0] = offset >> 16; /* block number */
96 addr[1] = offset >> 8; /* upper address octet */
97 addr[2] = blk_off; /* lower address octet */
99 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
101 addr[0] |= dev_addr; /* insert device address */
106 static int eeprom_len(unsigned offset, unsigned end)
108 unsigned len = end - offset;
111 * For a FRAM device there is no limit on the number of the
112 * bytes that can be accessed with the single read or write
115 #if !defined(CONFIG_SYS_I2C_FRAM)
116 unsigned blk_off = offset & 0xff;
117 unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
119 if (maxlen > I2C_RXTX_LEN)
120 maxlen = I2C_RXTX_LEN;
129 static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
130 uchar *buffer, unsigned len, bool read)
134 #if defined(CONFIG_DM_I2C)
137 ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0],
140 printf("%s: Cannot find udev for a bus %d\n", __func__,
142 return CMD_RET_FAILURE;
146 ret = dm_i2c_read(dev, offset, buffer, len);
148 ret = dm_i2c_write(dev, offset, buffer, len);
150 #else /* Non DM I2C support - will be removed */
153 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
155 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
156 #endif /* CONFIG_DM_I2C */
158 ret = CMD_RET_FAILURE;
163 static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
164 unsigned cnt, bool read)
166 unsigned end = offset + cnt;
171 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
172 eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS);
175 while (offset < end) {
176 alen = eeprom_addr(dev_addr, offset, addr);
178 len = eeprom_len(offset, end);
180 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
186 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
192 int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
195 * Read data until done or would cross a page boundary.
196 * We must write the address again when changing pages
197 * because the next page may be in a different device.
199 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
202 int eeprom_write(unsigned dev_addr, unsigned offset,
203 uchar *buffer, unsigned cnt)
207 eeprom_write_enable(dev_addr, 1);
210 * Write data until done or would cross a write page boundary.
211 * We must write the address again when changing pages
212 * because the address counter only increments within a page.
214 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
216 eeprom_write_enable(dev_addr, 0);
220 static int parse_numeric_param(char *str)
223 int value = simple_strtol(str, &endptr, 16);
225 return (*endptr != '\0') ? -1 : value;
229 * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
231 * @i2c_bus: address to store the i2c bus
232 * @i2c_addr: address to store the device i2c address
233 * @argc: count of command line arguments left to parse
234 * @argv: command line arguments left to parse
235 * @argc_no_bus_addr: argc value we expect to see when bus & addr aren't given
237 * @returns: number of arguments parsed or CMD_RET_USAGE if error
239 static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
240 char * const argv[], int argc_no_bus_addr)
242 int argc_no_bus = argc_no_bus_addr + 1;
243 int argc_bus_addr = argc_no_bus_addr + 2;
245 #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
246 if (argc == argc_no_bus_addr) {
248 *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
253 if (argc == argc_no_bus) {
255 *i2c_addr = parse_numeric_param(argv[0]);
260 if (argc == argc_bus_addr) {
261 *i2c_bus = parse_numeric_param(argv[0]);
262 *i2c_addr = parse_numeric_param(argv[1]);
267 return CMD_RET_USAGE;
270 #ifdef CONFIG_CMD_EEPROM_LAYOUT
272 __weak int eeprom_parse_layout_version(char *str)
274 return LAYOUT_VERSION_UNRECOGNIZED;
277 static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
286 EEPROM_ACTION_INVALID,
289 static enum eeprom_action parse_action(char *cmd)
291 if (!strncmp(cmd, "read", 4))
293 if (!strncmp(cmd, "write", 5))
295 #ifdef CONFIG_CMD_EEPROM_LAYOUT
296 if (!strncmp(cmd, "print", 5))
298 if (!strncmp(cmd, "update", 6))
299 return EEPROM_UPDATE;
302 return EEPROM_ACTION_INVALID;
305 static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
306 ulong i2c_addr, int layout_ver, char *key,
307 char *value, ulong addr, ulong off, ulong cnt)
310 const char *const fmt =
311 "\nEEPROM @0x%lX %s: addr 0x%08lx off 0x%04lx count %ld ... ";
312 #ifdef CONFIG_CMD_EEPROM_LAYOUT
313 struct eeprom_layout layout;
316 if (action == EEPROM_ACTION_INVALID)
317 return CMD_RET_USAGE;
319 eeprom_init(i2c_bus);
320 if (action == EEPROM_READ) {
321 printf(fmt, i2c_addr, "read", addr, off, cnt);
323 rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
327 } else if (action == EEPROM_WRITE) {
328 printf(fmt, i2c_addr, "write", addr, off, cnt);
330 rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
336 #ifdef CONFIG_CMD_EEPROM_LAYOUT
337 rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
341 eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
344 if (action == EEPROM_PRINT) {
345 layout.print(&layout);
349 layout.update(&layout, key, value);
351 rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
357 #define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
358 int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
360 int layout_ver = LAYOUT_VERSION_AUTODETECT;
361 enum eeprom_action action = EEPROM_ACTION_INVALID;
362 int i2c_bus = -1, index = 0;
363 ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0;
365 char *field_name = "";
366 char *field_value = "";
369 return CMD_RET_USAGE;
371 NEXT_PARAM(argc, index); /* Skip program name */
373 action = parse_action(argv[index]);
374 NEXT_PARAM(argc, index);
376 if (action == EEPROM_ACTION_INVALID)
377 return CMD_RET_USAGE;
379 #ifdef CONFIG_CMD_EEPROM_LAYOUT
380 if (action == EEPROM_PRINT || action == EEPROM_UPDATE) {
381 if (!strcmp(argv[index], "-l")) {
382 NEXT_PARAM(argc, index);
383 layout_ver = eeprom_parse_layout_version(argv[index]);
384 NEXT_PARAM(argc, index);
392 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
396 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
400 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
404 /* Get compiler to stop whining */
405 return CMD_RET_USAGE;
408 if (ret == CMD_RET_USAGE)
412 NEXT_PARAM(argc, index);
414 if (action == EEPROM_READ || action == EEPROM_WRITE) {
415 addr = parse_numeric_param(argv[index]);
416 NEXT_PARAM(argc, index);
417 off = parse_numeric_param(argv[index]);
418 NEXT_PARAM(argc, index);
419 cnt = parse_numeric_param(argv[index]);
422 #ifdef CONFIG_CMD_EEPROM_LAYOUT
423 if (action == EEPROM_UPDATE) {
424 field_name = argv[index];
425 NEXT_PARAM(argc, index);
426 field_value = argv[index];
427 NEXT_PARAM(argc, index);
431 return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
432 field_name, field_value, addr, off, cnt);
436 eeprom, 8, 1, do_eeprom,
438 "read <bus> <devaddr> addr off cnt\n"
439 "eeprom write <bus> <devaddr> addr off cnt\n"
440 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
441 #ifdef CONFIG_CMD_EEPROM_LAYOUT
443 "eeprom print [-l <layout_version>] <bus> <devaddr>\n"
444 " - Print layout fields and their data in human readable format\n"
445 "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n"
446 " - Update a specific eeprom field with new data.\n"
447 " The new data must be written in the same human readable format as shown by the print command.\n"
450 "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
451 "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
452 "The values which can be provided with the -l option are:\n"
453 CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"