2 * (C) Copyright 2000, 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * Support for read and write access to EEPROM like memory devices. This
10 * includes regular EEPROM as well as FRAM (ferroelectic nonvolaile RAM).
11 * FRAM devices read and write data at bus speed. In particular, there is no
12 * write delay. Also, there is no limit imposed on the number of bytes that can
13 * be transferred with a single read or write.
15 * Use the following configuration options to ensure no unneeded performance
16 * degradation (typical for EEPROM) is incured for FRAM memory:
18 * #define CONFIG_SYS_I2C_FRAM
19 * #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 __weak int eeprom_write_enable(unsigned dev_addr, int state)
68 void eeprom_init(int bus)
71 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
76 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
77 #if defined(CONFIG_SYS_I2C)
81 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
85 static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
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 */
96 addr[0] = offset >> 16; /* block number */
97 addr[1] = offset >> 8; /* upper address octet */
98 addr[2] = blk_off; /* lower address octet */
100 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
102 addr[0] |= dev_addr; /* insert device address */
107 static int eeprom_len(unsigned offset, unsigned end)
109 unsigned len = end - offset;
112 * For a FRAM device there is no limit on the number of the
113 * bytes that can be ccessed with the single read or write
116 #if !defined(CONFIG_SYS_I2C_FRAM)
117 unsigned blk_off = offset & 0xff;
118 unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
120 if (maxlen > I2C_RXTX_LEN)
121 maxlen = I2C_RXTX_LEN;
130 static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
131 uchar *buffer, unsigned len, bool read)
136 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
138 spi_read(addr, alen, buffer, len);
140 spi_write(addr, alen, buffer, len);
143 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
144 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
148 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
150 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
158 static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
159 unsigned cnt, bool read)
161 unsigned end = offset + cnt;
166 while (offset < end) {
167 alen = eeprom_addr(dev_addr, offset, addr);
169 len = eeprom_len(offset, end);
171 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
177 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
183 int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
186 * Read data until done or would cross a page boundary.
187 * We must write the address again when changing pages
188 * because the next page may be in a different device.
190 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
193 int eeprom_write(unsigned dev_addr, unsigned offset,
194 uchar *buffer, unsigned cnt)
198 eeprom_write_enable(dev_addr, 1);
201 * Write data until done or would cross a write page boundary.
202 * We must write the address again when changing pages
203 * because the address counter only increments within a page.
205 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
207 eeprom_write_enable(dev_addr, 0);
211 static int parse_numeric_param(char *str)
214 int value = simple_strtol(str, &endptr, 16);
216 return (*endptr != '\0') ? -1 : value;
220 * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
222 * @i2c_bus: address to store the i2c bus
223 * @i2c_addr: address to store the device i2c address
224 * @argc: count of command line arguments left to parse
225 * @argv: command line arguments left to parse
226 * @argc_no_bus_addr: argc value we expect to see when bus & addr aren't given
228 * @returns: number of arguments parsed or CMD_RET_USAGE if error
230 static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
231 char * const argv[], int argc_no_bus_addr)
233 int argc_no_bus = argc_no_bus_addr + 1;
234 int argc_bus_addr = argc_no_bus_addr + 2;
236 #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
237 if (argc == argc_no_bus_addr) {
239 *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
244 if (argc == argc_no_bus) {
246 *i2c_addr = parse_numeric_param(argv[0]);
251 if (argc == argc_bus_addr) {
252 *i2c_bus = parse_numeric_param(argv[0]);
253 *i2c_addr = parse_numeric_param(argv[1]);
258 return CMD_RET_USAGE;
261 #ifdef CONFIG_CMD_EEPROM_LAYOUT
263 __weak int eeprom_parse_layout_version(char *str)
265 return LAYOUT_VERSION_UNRECOGNIZED;
268 static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
270 #ifndef CONFIG_EEPROM_LAYOUT_HELP_STRING
271 #define CONFIG_EEPROM_LAYOUT_HELP_STRING "<not defined>"
281 EEPROM_ACTION_INVALID,
284 #ifdef CONFIG_CMD_EEPROM_LAYOUT
285 static enum eeprom_action parse_action(char *cmd)
287 if (!strncmp(cmd, "print", 5))
289 if (!strncmp(cmd, "update", 6))
290 return EEPROM_UPDATE;
292 return EEPROM_ACTION_INVALID;
296 static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
297 ulong i2c_addr, int layout_ver, char *key,
298 char *value, ulong addr, ulong off, ulong cnt)
301 const char *const fmt =
302 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
303 #ifdef CONFIG_CMD_EEPROM_LAYOUT
304 struct eeprom_layout layout;
307 if (action == EEPROM_ACTION_INVALID)
308 return CMD_RET_USAGE;
310 eeprom_init(i2c_bus);
311 if (action == EEPROM_READ) {
312 printf(fmt, i2c_addr, "read", addr, off, cnt);
314 rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
318 } else if (action == EEPROM_WRITE) {
319 printf(fmt, i2c_addr, "write", addr, off, cnt);
321 rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
327 #ifdef CONFIG_CMD_EEPROM_LAYOUT
328 rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
332 eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
335 if (action == EEPROM_PRINT) {
336 layout.print(&layout);
340 layout.update(&layout, key, value);
342 rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
348 #ifdef CONFIG_CMD_EEPROM_LAYOUT
349 #define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
350 static int do_eeprom_layout(cmd_tbl_t *cmdtp, int flag, int argc,
353 int layout_ver = LAYOUT_VERSION_AUTODETECT;
354 enum eeprom_action action = EEPROM_ACTION_INVALID;
355 int i2c_bus = -1, i2c_addr = -1, index = 0;
356 char *field_name = "";
357 char *field_value = "";
360 return CMD_RET_USAGE;
362 NEXT_PARAM(argc, index); /* Skip program name */
364 action = parse_action(argv[index]);
365 NEXT_PARAM(argc, index);
368 return CMD_RET_USAGE;
370 if (!strcmp(argv[index], "-l")) {
371 NEXT_PARAM(argc, index);
373 layout_ver = eeprom_parse_layout_version(argv[index]);
374 NEXT_PARAM(argc, index);
378 return CMD_RET_USAGE;
380 i2c_bus = parse_numeric_param(argv[index]);
381 NEXT_PARAM(argc, index);
383 i2c_addr = parse_numeric_param(argv[index]);
384 NEXT_PARAM(argc, index);
386 if (action == EEPROM_PRINT)
390 field_name = argv[index];
391 NEXT_PARAM(argc, index);
395 field_value = argv[index];
396 NEXT_PARAM(argc, index);
400 return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
401 field_name, field_value, 0, 0, 0);
406 static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
408 char * const *args = &argv[2];
410 ulong dev_addr, addr, off, cnt;
413 #ifdef CONFIG_CMD_EEPROM_LAYOUT
415 if (!strcmp(argv[1], "update") || !strcmp(argv[1], "print"))
416 return do_eeprom_layout(cmdtp, flag, argc, argv);
420 rcode = parse_i2c_bus_addr(&bus_addr, &dev_addr, argc - 2, argv + 2, 3);
421 if (rcode == CMD_RET_USAGE)
424 addr = simple_strtoul(*args++, NULL, 16);
425 off = simple_strtoul(*args++, NULL, 16);
426 cnt = simple_strtoul(*args++, NULL, 16);
428 if (strcmp(argv[1], "read") == 0) {
429 return eeprom_execute_command(EEPROM_READ, bus_addr, dev_addr,
430 LAYOUT_VERSION_UNRECOGNIZED,
431 NULL, NULL, addr, off, cnt);
432 } else if (strcmp(argv[1], "write") == 0) {
433 return eeprom_execute_command(EEPROM_WRITE, bus_addr, dev_addr,
434 LAYOUT_VERSION_UNRECOGNIZED,
435 NULL, NULL, addr, off, cnt);
438 return CMD_RET_USAGE;
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"