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
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 #if defined(CONFIG_DM_I2C)
66 __weak int eeprom_write_enable(unsigned dev_addr, int state)
71 void eeprom_init(int bus)
74 #if defined(CONFIG_DM_I2C)
76 #elif defined(CONFIG_SYS_I2C)
79 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
83 static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
88 blk_off = offset & 0xff; /* block offset */
89 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
90 addr[0] = offset >> 8; /* block number */
91 addr[1] = blk_off; /* block offset */
94 addr[0] = offset >> 16; /* block number */
95 addr[1] = offset >> 8; /* upper address octet */
96 addr[2] = blk_off; /* lower address octet */
98 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
100 addr[0] |= dev_addr; /* insert device address */
105 static int eeprom_len(unsigned offset, unsigned end)
107 unsigned len = end - offset;
110 * For a FRAM device there is no limit on the number of the
111 * bytes that can be ccessed with the single read or write
114 #if !defined(CONFIG_SYS_I2C_FRAM)
115 unsigned blk_off = offset & 0xff;
116 unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
118 if (maxlen > I2C_RXTX_LEN)
119 maxlen = I2C_RXTX_LEN;
128 static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
129 uchar *buffer, unsigned len, bool read)
133 #if defined(CONFIG_DM_I2C)
136 ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0],
139 printf("%s: Cannot find udev for a bus %d\n", __func__,
141 return CMD_RET_FAILURE;
145 ret = dm_i2c_read(dev, offset, buffer, len);
147 ret = dm_i2c_write(dev, offset, buffer, len);
149 #else /* Non DM I2C support - will be removed */
152 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
154 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
155 #endif /* CONFIG_DM_I2C */
157 ret = CMD_RET_FAILURE;
162 static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
163 unsigned cnt, bool read)
165 unsigned end = offset + cnt;
170 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
171 eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS);
174 while (offset < end) {
175 alen = eeprom_addr(dev_addr, offset, addr);
177 len = eeprom_len(offset, end);
179 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
185 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
191 int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
194 * Read data until done or would cross a page boundary.
195 * We must write the address again when changing pages
196 * because the next page may be in a different device.
198 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
201 int eeprom_write(unsigned dev_addr, unsigned offset,
202 uchar *buffer, unsigned cnt)
206 eeprom_write_enable(dev_addr, 1);
209 * Write data until done or would cross a write page boundary.
210 * We must write the address again when changing pages
211 * because the address counter only increments within a page.
213 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
215 eeprom_write_enable(dev_addr, 0);
219 static int parse_numeric_param(char *str)
222 int value = simple_strtol(str, &endptr, 16);
224 return (*endptr != '\0') ? -1 : value;
228 * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
230 * @i2c_bus: address to store the i2c bus
231 * @i2c_addr: address to store the device i2c address
232 * @argc: count of command line arguments left to parse
233 * @argv: command line arguments left to parse
234 * @argc_no_bus_addr: argc value we expect to see when bus & addr aren't given
236 * @returns: number of arguments parsed or CMD_RET_USAGE if error
238 static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
239 char * const argv[], int argc_no_bus_addr)
241 int argc_no_bus = argc_no_bus_addr + 1;
242 int argc_bus_addr = argc_no_bus_addr + 2;
244 #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
245 if (argc == argc_no_bus_addr) {
247 *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
252 if (argc == argc_no_bus) {
254 *i2c_addr = parse_numeric_param(argv[0]);
259 if (argc == argc_bus_addr) {
260 *i2c_bus = parse_numeric_param(argv[0]);
261 *i2c_addr = parse_numeric_param(argv[1]);
266 return CMD_RET_USAGE;
269 #ifdef CONFIG_CMD_EEPROM_LAYOUT
271 __weak int eeprom_parse_layout_version(char *str)
273 return LAYOUT_VERSION_UNRECOGNIZED;
276 static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
285 EEPROM_ACTION_INVALID,
288 static enum eeprom_action parse_action(char *cmd)
290 if (!strncmp(cmd, "read", 4))
292 if (!strncmp(cmd, "write", 5))
294 #ifdef CONFIG_CMD_EEPROM_LAYOUT
295 if (!strncmp(cmd, "print", 5))
297 if (!strncmp(cmd, "update", 6))
298 return EEPROM_UPDATE;
301 return EEPROM_ACTION_INVALID;
304 static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
305 ulong i2c_addr, int layout_ver, char *key,
306 char *value, ulong addr, ulong off, ulong cnt)
309 const char *const fmt =
310 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
311 #ifdef CONFIG_CMD_EEPROM_LAYOUT
312 struct eeprom_layout layout;
315 if (action == EEPROM_ACTION_INVALID)
316 return CMD_RET_USAGE;
318 eeprom_init(i2c_bus);
319 if (action == EEPROM_READ) {
320 printf(fmt, i2c_addr, "read", addr, off, cnt);
322 rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
326 } else if (action == EEPROM_WRITE) {
327 printf(fmt, i2c_addr, "write", addr, off, cnt);
329 rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
335 #ifdef CONFIG_CMD_EEPROM_LAYOUT
336 rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
340 eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
343 if (action == EEPROM_PRINT) {
344 layout.print(&layout);
348 layout.update(&layout, key, value);
350 rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
356 #define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
357 int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
359 int layout_ver = LAYOUT_VERSION_AUTODETECT;
360 enum eeprom_action action = EEPROM_ACTION_INVALID;
361 int i2c_bus = -1, index = 0;
362 ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0;
364 char *field_name = "";
365 char *field_value = "";
368 return CMD_RET_USAGE;
370 NEXT_PARAM(argc, index); /* Skip program name */
372 action = parse_action(argv[index]);
373 NEXT_PARAM(argc, index);
375 if (action == EEPROM_ACTION_INVALID)
376 return CMD_RET_USAGE;
378 #ifdef CONFIG_CMD_EEPROM_LAYOUT
379 if (action == EEPROM_PRINT || action == EEPROM_UPDATE) {
380 if (!strcmp(argv[index], "-l")) {
381 NEXT_PARAM(argc, index);
382 layout_ver = eeprom_parse_layout_version(argv[index]);
383 NEXT_PARAM(argc, index);
391 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
395 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
399 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
403 /* Get compiler to stop whining */
404 return CMD_RET_USAGE;
407 if (ret == CMD_RET_USAGE)
411 NEXT_PARAM(argc, index);
413 if (action == EEPROM_READ || action == EEPROM_WRITE) {
414 addr = parse_numeric_param(argv[index]);
415 NEXT_PARAM(argc, index);
416 off = parse_numeric_param(argv[index]);
417 NEXT_PARAM(argc, index);
418 cnt = parse_numeric_param(argv[index]);
421 #ifdef CONFIG_CMD_EEPROM_LAYOUT
422 if (action == EEPROM_UPDATE) {
423 field_name = argv[index];
424 NEXT_PARAM(argc, index);
425 field_value = argv[index];
426 NEXT_PARAM(argc, index);
430 return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
431 field_name, field_value, addr, off, cnt);
435 eeprom, 8, 1, do_eeprom,
437 "read <bus> <devaddr> addr off cnt\n"
438 "eeprom write <bus> <devaddr> addr off cnt\n"
439 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
440 #ifdef CONFIG_CMD_EEPROM_LAYOUT
442 "eeprom print [-l <layout_version>] <bus> <devaddr>\n"
443 " - Print layout fields and their data in human readable format\n"
444 "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n"
445 " - Update a specific eeprom field with new data.\n"
446 " The new data must be written in the same human readable format as shown by the print command.\n"
449 "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
450 "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
451 "The values which can be provided with the -l option are:\n"
452 CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"