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>
28 #include <linux/delay.h>
30 #ifndef CONFIG_SYS_I2C_SPEED
31 #define CONFIG_SYS_I2C_SPEED 50000
34 #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
35 #define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0
38 #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
39 #define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8
43 #define I2C_RXTX_LEN 128
46 #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
47 #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
50 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
51 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
53 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
54 * 0x00000nxx for EEPROM address selectors and page number at n.
56 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
57 #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
58 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
59 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
60 #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
64 #if CONFIG_IS_ENABLED(DM_I2C)
65 static int eeprom_i2c_bus;
68 __weak int eeprom_write_enable(unsigned dev_addr, int state)
73 void eeprom_init(int bus)
76 #if CONFIG_IS_ENABLED(DM_I2C)
78 #elif defined(CONFIG_SYS_I2C_LEGACY)
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 accessed 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)
135 #if CONFIG_IS_ENABLED(DM_I2C)
138 ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0],
141 printf("%s: Cannot find udev for a bus %d\n", __func__,
143 return CMD_RET_FAILURE;
147 ret = dm_i2c_read(dev, offset, buffer, len);
149 ret = dm_i2c_write(dev, offset, buffer, len);
151 #else /* Non DM I2C support - will be removed */
154 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
156 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
157 #endif /* CONFIG_DM_I2C */
159 ret = CMD_RET_FAILURE;
164 static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
165 unsigned cnt, bool read)
167 unsigned end = offset + cnt;
172 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
173 eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS);
176 while (offset < end) {
177 alen = eeprom_addr(dev_addr, offset, addr);
179 len = eeprom_len(offset, end);
181 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
187 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
193 int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
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.
200 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
203 int eeprom_write(unsigned dev_addr, unsigned offset,
204 uchar *buffer, unsigned cnt)
208 eeprom_write_enable(dev_addr, 1);
211 * Write data until done or would cross a write page boundary.
212 * We must write the address again when changing pages
213 * because the address counter only increments within a page.
215 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
217 eeprom_write_enable(dev_addr, 0);
221 static int parse_numeric_param(char *str)
224 int value = simple_strtol(str, &endptr, 16);
226 return (*endptr != '\0') ? -1 : value;
230 * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
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
238 * @returns: number of arguments parsed or CMD_RET_USAGE if error
240 static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
241 char *const argv[], int argc_no_bus_addr)
243 int argc_no_bus = argc_no_bus_addr + 1;
244 int argc_bus_addr = argc_no_bus_addr + 2;
246 #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
247 if (argc == argc_no_bus_addr) {
249 *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
254 if (argc == argc_no_bus) {
256 *i2c_addr = parse_numeric_param(argv[0]);
261 if (argc == argc_bus_addr) {
262 *i2c_bus = parse_numeric_param(argv[0]);
263 *i2c_addr = parse_numeric_param(argv[1]);
268 return CMD_RET_USAGE;
271 #ifdef CONFIG_CMD_EEPROM_LAYOUT
273 __weak int eeprom_parse_layout_version(char *str)
275 return LAYOUT_VERSION_UNRECOGNIZED;
278 static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
287 EEPROM_ACTION_INVALID,
290 static enum eeprom_action parse_action(char *cmd)
292 if (!strncmp(cmd, "read", 4))
294 if (!strncmp(cmd, "write", 5))
296 #ifdef CONFIG_CMD_EEPROM_LAYOUT
297 if (!strncmp(cmd, "print", 5))
299 if (!strncmp(cmd, "update", 6))
300 return EEPROM_UPDATE;
303 return EEPROM_ACTION_INVALID;
306 static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
307 ulong i2c_addr, int layout_ver, char *key,
308 char *value, ulong addr, ulong off, ulong cnt)
311 const char *const fmt =
312 "\nEEPROM @0x%lX %s: addr 0x%08lx off 0x%04lx count %ld ... ";
313 #ifdef CONFIG_CMD_EEPROM_LAYOUT
314 struct eeprom_layout layout;
317 if (action == EEPROM_ACTION_INVALID)
318 return CMD_RET_USAGE;
320 eeprom_init(i2c_bus);
321 if (action == EEPROM_READ) {
322 printf(fmt, i2c_addr, "read", addr, off, cnt);
324 rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
328 } else if (action == EEPROM_WRITE) {
329 printf(fmt, i2c_addr, "write", addr, off, cnt);
331 rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
337 #ifdef CONFIG_CMD_EEPROM_LAYOUT
338 rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
342 eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
345 if (action == EEPROM_PRINT) {
346 layout.print(&layout);
350 layout.update(&layout, key, value);
352 rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
358 #define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
359 int do_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
361 int layout_ver = LAYOUT_VERSION_AUTODETECT;
362 enum eeprom_action action = EEPROM_ACTION_INVALID;
363 int i2c_bus = -1, index = 0;
364 ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0;
366 char *field_name = "";
367 char *field_value = "";
370 return CMD_RET_USAGE;
372 NEXT_PARAM(argc, index); /* Skip program name */
374 action = parse_action(argv[index]);
375 NEXT_PARAM(argc, index);
377 if (action == EEPROM_ACTION_INVALID)
378 return CMD_RET_USAGE;
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);
393 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
397 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
401 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
405 /* Get compiler to stop whining */
406 return CMD_RET_USAGE;
409 if (ret == CMD_RET_USAGE)
413 NEXT_PARAM(argc, index);
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]);
423 #ifdef CONFIG_CMD_EEPROM_LAYOUT
424 if (action == EEPROM_UPDATE) {
425 field_name = argv[index];
426 NEXT_PARAM(argc, index);
427 field_value = argv[index];
428 NEXT_PARAM(argc, index);
432 return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
433 field_name, field_value, addr, off, cnt);
437 eeprom, 8, 1, do_eeprom,
439 "read <bus> <devaddr> addr off cnt\n"
440 "eeprom write <bus> <devaddr> addr off cnt\n"
441 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
442 #ifdef CONFIG_CMD_EEPROM_LAYOUT
444 "eeprom print [-l <layout_version>] <bus> <devaddr>\n"
445 " - Print layout fields and their data in human readable format\n"
446 "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n"
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"
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"