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 * Set CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS to 0
27 #include <eeprom_layout.h>
28 #include <linux/delay.h>
31 #define I2C_RXTX_LEN 128
34 #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
35 #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
37 #if CONFIG_IS_ENABLED(DM_I2C)
38 static int eeprom_i2c_bus;
41 __weak int eeprom_write_enable(unsigned dev_addr, int state)
46 void eeprom_init(int bus)
49 #if CONFIG_IS_ENABLED(DM_I2C)
51 #elif CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
54 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
59 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
60 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
62 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
63 * 0x00000nxx for EEPROM address selectors and page number at n.
65 static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
70 blk_off = offset & 0xff; /* block offset */
71 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
72 addr[0] = offset >> 8; /* block number */
73 addr[1] = blk_off; /* block offset */
76 addr[0] = offset >> 16; /* block number */
77 addr[1] = offset >> 8; /* upper address octet */
78 addr[2] = blk_off; /* lower address octet */
80 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
82 addr[0] |= dev_addr; /* insert device address */
87 static int eeprom_len(unsigned offset, unsigned end)
89 unsigned len = end - offset;
92 * For a FRAM device there is no limit on the number of the
93 * bytes that can be accessed with the single read or write
96 #if !defined(CONFIG_SYS_I2C_FRAM)
97 unsigned blk_off = offset & 0xff;
98 unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
100 if (maxlen > I2C_RXTX_LEN)
101 maxlen = I2C_RXTX_LEN;
110 static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
111 uchar *buffer, unsigned len, bool read)
115 #if CONFIG_IS_ENABLED(DM_I2C)
118 ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0],
121 printf("%s: Cannot find udev for a bus %d\n", __func__,
123 return CMD_RET_FAILURE;
127 ret = dm_i2c_read(dev, offset, buffer, len);
129 ret = dm_i2c_write(dev, offset, buffer, len);
131 #else /* Non DM I2C support - will be removed */
134 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
136 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
137 #endif /* CONFIG_DM_I2C */
139 ret = CMD_RET_FAILURE;
144 static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
145 unsigned cnt, bool read)
147 unsigned end = offset + cnt;
152 #if !CONFIG_IS_ENABLED(DM_I2C) && defined(CONFIG_SYS_I2C_EEPROM_BUS)
153 eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS);
156 while (offset < end) {
157 alen = eeprom_addr(dev_addr, offset, addr);
159 len = eeprom_len(offset, end);
161 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
166 #if CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS > 0
168 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
175 int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
178 * Read data until done or would cross a page boundary.
179 * We must write the address again when changing pages
180 * because the next page may be in a different device.
182 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
185 int eeprom_write(unsigned dev_addr, unsigned offset,
186 uchar *buffer, unsigned cnt)
190 eeprom_write_enable(dev_addr, 1);
193 * Write data until done or would cross a write page boundary.
194 * We must write the address again when changing pages
195 * because the address counter only increments within a page.
197 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
199 eeprom_write_enable(dev_addr, 0);
203 static int parse_numeric_param(char *str)
206 int value = simple_strtol(str, &endptr, 16);
208 return (*endptr != '\0') ? -1 : value;
212 * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
214 * @i2c_bus: address to store the i2c bus
215 * @i2c_addr: address to store the device i2c address
216 * @argc: count of command line arguments left to parse
217 * @argv: command line arguments left to parse
218 * @argc_no_bus_addr: argc value we expect to see when bus & addr aren't given
220 * @returns: number of arguments parsed or CMD_RET_USAGE if error
222 static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
223 char *const argv[], int argc_no_bus_addr)
225 int argc_no_bus = argc_no_bus_addr + 1;
226 int argc_bus_addr = argc_no_bus_addr + 2;
228 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR
229 if (argc == argc_no_bus_addr) {
231 *i2c_addr = CONFIG_SYS_I2C_EEPROM_ADDR;
236 if (argc == argc_no_bus) {
238 *i2c_addr = parse_numeric_param(argv[0]);
243 if (argc == argc_bus_addr) {
244 *i2c_bus = parse_numeric_param(argv[0]);
245 *i2c_addr = parse_numeric_param(argv[1]);
250 return CMD_RET_USAGE;
253 #ifdef CONFIG_CMD_EEPROM_LAYOUT
255 __weak int eeprom_parse_layout_version(char *str)
257 return LAYOUT_VERSION_UNRECOGNIZED;
260 static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
269 EEPROM_ACTION_INVALID,
272 static enum eeprom_action parse_action(char *cmd)
274 if (!strncmp(cmd, "read", 4))
276 if (!strncmp(cmd, "write", 5))
278 #ifdef CONFIG_CMD_EEPROM_LAYOUT
279 if (!strncmp(cmd, "print", 5))
281 if (!strncmp(cmd, "update", 6))
282 return EEPROM_UPDATE;
285 return EEPROM_ACTION_INVALID;
288 static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
289 ulong i2c_addr, int layout_ver, char *key,
290 char *value, ulong addr, ulong off, ulong cnt)
293 const char *const fmt =
294 "\nEEPROM @0x%lX %s: addr 0x%08lx off 0x%04lx count %ld ... ";
295 #ifdef CONFIG_CMD_EEPROM_LAYOUT
296 struct eeprom_layout layout;
299 if (action == EEPROM_ACTION_INVALID)
300 return CMD_RET_USAGE;
302 eeprom_init(i2c_bus);
303 if (action == EEPROM_READ) {
304 printf(fmt, i2c_addr, "read", addr, off, cnt);
306 rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
310 } else if (action == EEPROM_WRITE) {
311 printf(fmt, i2c_addr, "write", addr, off, cnt);
313 rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
319 #ifdef CONFIG_CMD_EEPROM_LAYOUT
320 rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
324 eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
327 if (action == EEPROM_PRINT) {
328 layout.print(&layout);
332 layout.update(&layout, key, value);
334 rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
340 #define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
341 int do_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
343 int layout_ver = LAYOUT_VERSION_AUTODETECT;
344 enum eeprom_action action = EEPROM_ACTION_INVALID;
345 int i2c_bus = -1, index = 0;
346 ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0;
348 char *field_name = "";
349 char *field_value = "";
352 return CMD_RET_USAGE;
354 NEXT_PARAM(argc, index); /* Skip program name */
356 action = parse_action(argv[index]);
357 NEXT_PARAM(argc, index);
359 if (action == EEPROM_ACTION_INVALID)
360 return CMD_RET_USAGE;
362 #ifdef CONFIG_CMD_EEPROM_LAYOUT
363 if (action == EEPROM_PRINT || action == EEPROM_UPDATE) {
364 if (!strcmp(argv[index], "-l")) {
365 NEXT_PARAM(argc, index);
366 layout_ver = eeprom_parse_layout_version(argv[index]);
367 NEXT_PARAM(argc, index);
375 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
379 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
383 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
387 /* Get compiler to stop whining */
388 return CMD_RET_USAGE;
391 if (ret == CMD_RET_USAGE)
395 NEXT_PARAM(argc, index);
397 if (action == EEPROM_READ || action == EEPROM_WRITE) {
398 addr = parse_numeric_param(argv[index]);
399 NEXT_PARAM(argc, index);
400 off = parse_numeric_param(argv[index]);
401 NEXT_PARAM(argc, index);
402 cnt = parse_numeric_param(argv[index]);
405 #ifdef CONFIG_CMD_EEPROM_LAYOUT
406 if (action == EEPROM_UPDATE) {
407 field_name = argv[index];
408 NEXT_PARAM(argc, index);
409 field_value = argv[index];
410 NEXT_PARAM(argc, index);
414 return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
415 field_name, field_value, addr, off, cnt);
419 eeprom, 8, 1, do_eeprom,
421 "read <bus> <devaddr> addr off cnt\n"
422 "eeprom write <bus> <devaddr> addr off cnt\n"
423 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
424 #ifdef CONFIG_CMD_EEPROM_LAYOUT
426 "eeprom print [-l <layout_version>] <bus> <devaddr>\n"
427 " - Print layout fields and their data in human readable format\n"
428 "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n"
429 " - Update a specific eeprom field with new data.\n"
430 " The new data must be written in the same human readable format as shown by the print command.\n"
433 "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
434 "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
435 "The values which can be provided with the -l option are:\n"
436 CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"