blob: cf89cfce3e4d1e71b9b2d066b2644871c6a38ca6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00002/*
3 * (C) Copyright 2000, 2001
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38635852002-08-27 05:55:31 +00005 */
6
m8a484c602005-08-12 21:16:13 +02007/*
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
Peter Meerwaldc48ae8c2012-02-08 05:31:53 +000011 * write delay. Also, there is no limit imposed on the number of bytes that can
m8a484c602005-08-12 21:16:13 +020012 * be transferred with a single read or write.
Wolfgang Denke4e5e4e2005-08-19 00:46:54 +020013 *
m8a484c602005-08-12 21:16:13 +020014 * Use the following configuration options to ensure no unneeded performance
15 * degradation (typical for EEPROM) is incured for FRAM memory:
Wolfgang Denke4e5e4e2005-08-19 00:46:54 +020016 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020017 * #define CONFIG_SYS_I2C_FRAM
Tom Rinifaed5672021-08-17 17:59:45 -040018 * Set CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS to 0
m8a484c602005-08-12 21:16:13 +020019 *
20 */
21
wdenk38635852002-08-27 05:55:31 +000022#include <config.h>
23#include <command.h>
Marek Behúne5545062024-05-21 09:13:35 +020024#include <dm.h>
Simon Glasseba6b8d2019-11-14 12:57:50 -070025#include <eeprom.h>
wdenk38635852002-08-27 05:55:31 +000026#include <i2c.h>
Marek Behúne5545062024-05-21 09:13:35 +020027#include <i2c_eeprom.h>
Nikita Kiryanov91f226a2016-04-16 17:55:12 +030028#include <eeprom_layout.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060029#include <vsprintf.h>
Simon Glassdbd79542020-05-10 11:40:11 -060030#include <linux/delay.h>
wdenk38635852002-08-27 05:55:31 +000031
Mario Sixdf0a9142016-04-20 10:44:52 +020032#ifndef I2C_RXTX_LEN
33#define I2C_RXTX_LEN 128
34#endif
35
Marek Vasut42086792015-11-10 20:53:28 +010036#define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
37#define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
38
Igor Opaniukf7c91762021-02-09 13:52:45 +020039#if CONFIG_IS_ENABLED(DM_I2C)
Marek Vasut75f3c192020-04-07 13:29:04 +020040static int eeprom_i2c_bus;
Simon Goldschmidt30946822019-03-26 13:38:52 +010041#endif
42
Marek Vasut2a632ffc2015-11-10 20:53:25 +010043__weak int eeprom_write_enable(unsigned dev_addr, int state)
44{
45 return 0;
46}
Marek Vasut704a0962015-11-10 20:53:18 +010047
Marek Vasutf422ae72015-11-10 20:53:31 +010048void eeprom_init(int bus)
Marek Vasut704a0962015-11-10 20:53:18 +010049{
Marek Vasut704a0962015-11-10 20:53:18 +010050 /* I2C EEPROM */
Igor Opaniukf7c91762021-02-09 13:52:45 +020051#if CONFIG_IS_ENABLED(DM_I2C)
Simon Goldschmidt30946822019-03-26 13:38:52 +010052 eeprom_i2c_bus = bus;
Tom Rini52b2e262021-08-18 23:12:24 -040053#elif CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
Marek Vasutf422ae72015-11-10 20:53:31 +010054 if (bus >= 0)
55 i2c_set_bus_num(bus);
Marek Vasut704a0962015-11-10 20:53:18 +010056 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenk38635852002-08-27 05:55:31 +000057#endif
Marek Vasut704a0962015-11-10 20:53:18 +010058}
wdenk38635852002-08-27 05:55:31 +000059
Tom Rinifaed5672021-08-17 17:59:45 -040060/*
61 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
62 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
63 *
64 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
65 * 0x00000nxx for EEPROM address selectors and page number at n.
66 */
Marek Vasut6ae59572015-11-10 20:53:24 +010067static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
68{
69 unsigned blk_off;
70 int alen;
71
72 blk_off = offset & 0xff; /* block offset */
73#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
74 addr[0] = offset >> 8; /* block number */
75 addr[1] = blk_off; /* block offset */
76 alen = 2;
77#else
78 addr[0] = offset >> 16; /* block number */
79 addr[1] = offset >> 8; /* upper address octet */
80 addr[2] = blk_off; /* lower address octet */
81 alen = 3;
82#endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
83
84 addr[0] |= dev_addr; /* insert device address */
85
86 return alen;
87}
88
Marek Vasutd062c482015-11-10 20:53:29 +010089static int eeprom_len(unsigned offset, unsigned end)
90{
91 unsigned len = end - offset;
92
93 /*
94 * For a FRAM device there is no limit on the number of the
Lukasz Majewski20bfc572019-06-09 22:54:38 +020095 * bytes that can be accessed with the single read or write
Marek Vasutd062c482015-11-10 20:53:29 +010096 * operation.
97 */
98#if !defined(CONFIG_SYS_I2C_FRAM)
99 unsigned blk_off = offset & 0xff;
100 unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
101
102 if (maxlen > I2C_RXTX_LEN)
103 maxlen = I2C_RXTX_LEN;
104
105 if (len > maxlen)
106 len = maxlen;
107#endif
108
109 return len;
110}
111
Marek Vasut78990c72015-11-10 20:53:23 +0100112static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
113 uchar *buffer, unsigned len, bool read)
114{
115 int ret = 0;
116
Igor Opaniukf7c91762021-02-09 13:52:45 +0200117#if CONFIG_IS_ENABLED(DM_I2C)
Lukasz Majewskic71c2692018-11-21 23:40:43 +0100118 struct udevice *dev;
119
Simon Goldschmidt30946822019-03-26 13:38:52 +0100120 ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0],
Lukasz Majewskic71c2692018-11-21 23:40:43 +0100121 alen - 1, &dev);
122 if (ret) {
123 printf("%s: Cannot find udev for a bus %d\n", __func__,
Simon Goldschmidt30946822019-03-26 13:38:52 +0100124 eeprom_i2c_bus);
Lukasz Majewskic71c2692018-11-21 23:40:43 +0100125 return CMD_RET_FAILURE;
126 }
127
128 if (read)
129 ret = dm_i2c_read(dev, offset, buffer, len);
130 else
131 ret = dm_i2c_write(dev, offset, buffer, len);
132
133#else /* Non DM I2C support - will be removed */
Marek Vasut78990c72015-11-10 20:53:23 +0100134
135 if (read)
136 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
137 else
138 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
Simon Goldschmidt30946822019-03-26 13:38:52 +0100139#endif /* CONFIG_DM_I2C */
Lukasz Majewskic71c2692018-11-21 23:40:43 +0100140 if (ret)
141 ret = CMD_RET_FAILURE;
142
Marek Vasut78990c72015-11-10 20:53:23 +0100143 return ret;
144}
145
Marek Vasut7a237212015-11-10 20:53:30 +0100146static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
147 unsigned cnt, bool read)
wdenk38635852002-08-27 05:55:31 +0000148{
149 unsigned end = offset + cnt;
Marek Vasut7a237212015-11-10 20:53:30 +0100150 unsigned alen, len;
wdenk38635852002-08-27 05:55:31 +0000151 int rcode = 0;
Marek Vasut6ae59572015-11-10 20:53:24 +0100152 uchar addr[3];
wdenk38635852002-08-27 05:55:31 +0000153
Marek Vasut99d1b102022-02-16 15:27:59 +0100154#if !CONFIG_IS_ENABLED(DM_I2C) && defined(CONFIG_SYS_I2C_EEPROM_BUS)
Simon Goldschmidt30946822019-03-26 13:38:52 +0100155 eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS);
156#endif
157
wdenk38635852002-08-27 05:55:31 +0000158 while (offset < end) {
Marek Vasut6ae59572015-11-10 20:53:24 +0100159 alen = eeprom_addr(dev_addr, offset, addr);
wdenk38635852002-08-27 05:55:31 +0000160
Marek Vasutd062c482015-11-10 20:53:29 +0100161 len = eeprom_len(offset, end);
m8a484c602005-08-12 21:16:13 +0200162
Marek Vasut7a237212015-11-10 20:53:30 +0100163 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
Marek Vasut78990c72015-11-10 20:53:23 +0100164
wdenk38635852002-08-27 05:55:31 +0000165 buffer += len;
166 offset += len;
Marek Vasut7a237212015-11-10 20:53:30 +0100167
Tom Rinifaed5672021-08-17 17:59:45 -0400168#if CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS > 0
Marek Vasut7a237212015-11-10 20:53:30 +0100169 if (!read)
170 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
Tom Rinifaed5672021-08-17 17:59:45 -0400171#endif
wdenk38635852002-08-27 05:55:31 +0000172 }
m8a484c602005-08-12 21:16:13 +0200173
wdenk38635852002-08-27 05:55:31 +0000174 return rcode;
175}
176
Marek Vasut7a237212015-11-10 20:53:30 +0100177int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
wdenk38635852002-08-27 05:55:31 +0000178{
Marek Vasut7a237212015-11-10 20:53:30 +0100179 /*
180 * Read data until done or would cross a page boundary.
181 * We must write the address again when changing pages
182 * because the next page may be in a different device.
183 */
184 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
185}
186
Marek Vasuta24ea7a2015-11-10 20:53:33 +0100187int eeprom_write(unsigned dev_addr, unsigned offset,
188 uchar *buffer, unsigned cnt)
Marek Vasut7a237212015-11-10 20:53:30 +0100189{
190 int ret;
wdenk38635852002-08-27 05:55:31 +0000191
Marek Vasut2a632ffc2015-11-10 20:53:25 +0100192 eeprom_write_enable(dev_addr, 1);
193
Marek Vasut704a0962015-11-10 20:53:18 +0100194 /*
195 * Write data until done or would cross a write page boundary.
wdenk38635852002-08-27 05:55:31 +0000196 * We must write the address again when changing pages
197 * because the address counter only increments within a page.
198 */
Alexey Brodkin90eed7f2015-12-14 18:45:34 +0300199 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
Marek Vasut2a632ffc2015-11-10 20:53:25 +0100200
201 eeprom_write_enable(dev_addr, 0);
Marek Vasut7a237212015-11-10 20:53:30 +0100202 return ret;
wdenk38635852002-08-27 05:55:31 +0000203}
204
Baruch Siach76afe2f2022-10-23 12:28:12 +0300205static long parse_numeric_param(char *str)
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300206{
207 char *endptr;
Baruch Siach76afe2f2022-10-23 12:28:12 +0300208 long value = simple_strtol(str, &endptr, 16);
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300209
210 return (*endptr != '\0') ? -1 : value;
211}
212
Marek Behún246d1a52024-05-21 09:13:32 +0200213struct eeprom_dev_spec {
Marek Behúne5545062024-05-21 09:13:35 +0200214#if CONFIG_IS_ENABLED(I2C_EEPROM)
215 struct udevice *dev;
216#endif
Marek Behún246d1a52024-05-21 09:13:32 +0200217 int i2c_bus;
218 ulong i2c_addr;
219};
220
Marek Behúne5545062024-05-21 09:13:35 +0200221static void eeprom_dev_spec_init(struct eeprom_dev_spec *dev)
222{
223#if CONFIG_IS_ENABLED(I2C_EEPROM)
224 if (!dev->dev)
225#endif
226 eeprom_init(dev->i2c_bus);
227}
228
229static int eeprom_dev_spec_read(struct eeprom_dev_spec *dev,
230 unsigned offset, uchar *buffer, unsigned cnt)
231{
232#if CONFIG_IS_ENABLED(I2C_EEPROM)
233 if (dev->dev)
234 return i2c_eeprom_read(dev->dev, offset, buffer, cnt);
235#endif
236 return eeprom_read(dev->i2c_addr, offset, buffer, cnt);
237}
238
239static int eeprom_dev_spec_write(struct eeprom_dev_spec *dev,
240 unsigned offset, uchar *buffer, unsigned cnt)
241{
242#if CONFIG_IS_ENABLED(I2C_EEPROM)
243 if (dev->dev)
244 return i2c_eeprom_write(dev->dev, offset, buffer, cnt);
245#endif
246 return eeprom_write(dev->i2c_addr, offset, buffer, cnt);
247}
248
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300249/**
Marek Behún246d1a52024-05-21 09:13:32 +0200250 * parse_eeprom_dev_spec - parse the eeprom device specifier
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300251 *
Marek Behún246d1a52024-05-21 09:13:32 +0200252 * @dev: pointer to eeprom device specifier
253 * @argc: count of command line arguments that can be used to parse
254 * the device specifier
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300255 * @argv: command line arguments left to parse
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300256 *
257 * @returns: number of arguments parsed or CMD_RET_USAGE if error
258 */
Marek Behún246d1a52024-05-21 09:13:32 +0200259static int parse_eeprom_dev_spec(struct eeprom_dev_spec *dev, int argc,
260 char *const argv[])
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300261{
Marek Behúne5545062024-05-21 09:13:35 +0200262#if CONFIG_IS_ENABLED(I2C_EEPROM)
263 if (argc == 0) {
264 if (!uclass_first_device_err(UCLASS_I2C_EEPROM, &dev->dev))
265 return 0;
266 }
267
268 if (argc == 1) {
269 if (!uclass_get_device_by_name(UCLASS_I2C_EEPROM, argv[0],
270 &dev->dev))
271 return 1;
272
273 /*
274 * If we could not find the device by name and the parameter is
275 * not numeric (and so won't be handled later), fail.
276 */
277 if (parse_numeric_param(argv[0]) == -1) {
278 printf("Can't get eeprom device: %s\n", argv[0]);
279 return CMD_RET_USAGE;
280 }
281 }
282#endif
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300283
Tom Rinifaed5672021-08-17 17:59:45 -0400284#ifdef CONFIG_SYS_I2C_EEPROM_ADDR
Marek Behún246d1a52024-05-21 09:13:32 +0200285 if (argc == 0) {
286 dev->i2c_bus = -1;
287 dev->i2c_addr = CONFIG_SYS_I2C_EEPROM_ADDR;
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300288
289 return 0;
290 }
291#endif
Marek Behún246d1a52024-05-21 09:13:32 +0200292 if (argc == 1) {
293 dev->i2c_bus = -1;
294 dev->i2c_addr = parse_numeric_param(argv[0]);
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300295
296 return 1;
297 }
298
Marek Behún246d1a52024-05-21 09:13:32 +0200299 if (argc == 2) {
300 dev->i2c_bus = parse_numeric_param(argv[0]);
301 dev->i2c_addr = parse_numeric_param(argv[1]);
Nikita Kiryanovadb62f02016-04-16 17:55:11 +0300302
303 return 2;
304 }
305
306 return CMD_RET_USAGE;
307}
308
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300309#ifdef CONFIG_CMD_EEPROM_LAYOUT
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300310
Marek Behúnbff98ba2024-05-21 09:13:30 +0200311#ifdef CONFIG_EEPROM_LAYOUT_VERSIONS
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300312__weak int eeprom_parse_layout_version(char *str)
313{
314 return LAYOUT_VERSION_UNRECOGNIZED;
315}
Marek Behúnbff98ba2024-05-21 09:13:30 +0200316#endif
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300317
318static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
319
Nikita Kiryanov91f226a2016-04-16 17:55:12 +0300320#endif
321
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300322enum eeprom_action {
Marek Behúne5545062024-05-21 09:13:35 +0200323 EEPROM_LIST,
Nikita Kiryanov91f226a2016-04-16 17:55:12 +0300324 EEPROM_READ,
325 EEPROM_WRITE,
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300326 EEPROM_PRINT,
327 EEPROM_UPDATE,
328 EEPROM_ACTION_INVALID,
329};
330
331static enum eeprom_action parse_action(char *cmd)
332{
Marek Behúne5545062024-05-21 09:13:35 +0200333#if CONFIG_IS_ENABLED(I2C_EEPROM)
334 if (!strncmp(cmd, "list", 4))
335 return EEPROM_LIST;
336#endif
Nikita Kiryanov9e559fd2016-04-16 17:55:13 +0300337 if (!strncmp(cmd, "read", 4))
338 return EEPROM_READ;
339 if (!strncmp(cmd, "write", 5))
340 return EEPROM_WRITE;
341#ifdef CONFIG_CMD_EEPROM_LAYOUT
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300342 if (!strncmp(cmd, "print", 5))
343 return EEPROM_PRINT;
344 if (!strncmp(cmd, "update", 6))
345 return EEPROM_UPDATE;
Nikita Kiryanov9e559fd2016-04-16 17:55:13 +0300346#endif
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300347
348 return EEPROM_ACTION_INVALID;
349}
350
Marek Behúne5545062024-05-21 09:13:35 +0200351#if CONFIG_IS_ENABLED(I2C_EEPROM)
352static int do_eeprom_list(void)
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300353{
Marek Behúne5545062024-05-21 09:13:35 +0200354 struct udevice *dev;
355 struct uclass *uc;
356 int err;
357
358 err = uclass_get(UCLASS_I2C_EEPROM, &uc);
359 if (err)
360 return CMD_RET_FAILURE;
361
362 uclass_foreach_dev(dev, uc)
363 printf("%s (%s)\n", dev->name, dev->driver->name);
364
365 return CMD_RET_SUCCESS;
366}
367#endif
368
Marek Behún533d3382024-05-21 09:13:33 +0200369static int do_eeprom_rw(struct eeprom_dev_spec *dev, bool read,
370 ulong addr, ulong off, ulong cnt)
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300371{
Nikita Kiryanov91f226a2016-04-16 17:55:12 +0300372 const char *const fmt =
Klaus H. Sorensen0c1ca6d2019-12-11 11:18:29 +0000373 "\nEEPROM @0x%lX %s: addr 0x%08lx off 0x%04lx count %ld ... ";
Marek Behún533d3382024-05-21 09:13:33 +0200374 uchar *memloc = (uchar *)addr;
375 int ret;
376
377 printf(fmt, dev->i2c_addr, read ? "read" : "write", addr, off, cnt);
378 if (read)
Marek Behúne5545062024-05-21 09:13:35 +0200379 ret = eeprom_dev_spec_read(dev, off, memloc, cnt);
Marek Behún533d3382024-05-21 09:13:33 +0200380 else
Marek Behúne5545062024-05-21 09:13:35 +0200381 ret = eeprom_dev_spec_write(dev, off, memloc, cnt);
Marek Behún533d3382024-05-21 09:13:33 +0200382 puts("done\n");
383
384 return ret;
385}
386
Nikita Kiryanov91f226a2016-04-16 17:55:12 +0300387#ifdef CONFIG_CMD_EEPROM_LAYOUT
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300388
Marek Behún533d3382024-05-21 09:13:33 +0200389static int do_eeprom_layout(struct eeprom_dev_spec *dev, int layout_ver,
390 struct eeprom_layout *layout)
391{
Marek Behún533d3382024-05-21 09:13:33 +0200392 eeprom_layout_setup(layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
393 layout_ver);
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300394
Marek Behúne5545062024-05-21 09:13:35 +0200395 return eeprom_dev_spec_read(dev, 0, eeprom_buf, layout->data_size);
Marek Behún533d3382024-05-21 09:13:33 +0200396}
Nikita Kiryanov91f226a2016-04-16 17:55:12 +0300397
Marek Behún533d3382024-05-21 09:13:33 +0200398static int do_eeprom_print(struct eeprom_dev_spec *dev, int layout_ver)
399{
400 struct eeprom_layout layout;
401 int ret;
Nikita Kiryanov91f226a2016-04-16 17:55:12 +0300402
Marek Behún533d3382024-05-21 09:13:33 +0200403 ret = do_eeprom_layout(dev, layout_ver, &layout);
404 if (ret)
405 return ret;
Nikita Kiryanov91f226a2016-04-16 17:55:12 +0300406
Marek Behún533d3382024-05-21 09:13:33 +0200407 layout.print(&layout);
Nikita Kiryanov91f226a2016-04-16 17:55:12 +0300408
Marek Behún533d3382024-05-21 09:13:33 +0200409 return 0;
410}
Nikita Kiryanov91f226a2016-04-16 17:55:12 +0300411
Marek Behún533d3382024-05-21 09:13:33 +0200412static int do_eeprom_update(struct eeprom_dev_spec *dev, int layout_ver,
413 char *key, char *value)
414{
415 struct eeprom_layout layout;
416 int ret;
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300417
Marek Behún533d3382024-05-21 09:13:33 +0200418 ret = do_eeprom_layout(dev, layout_ver, &layout);
419 if (ret)
420 return ret;
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300421
Marek Behún533d3382024-05-21 09:13:33 +0200422 ret = layout.update(&layout, key, value);
423 if (ret)
424 return CMD_RET_FAILURE;
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300425
Marek Behúne5545062024-05-21 09:13:35 +0200426 return eeprom_dev_spec_write(dev, 0, layout.data, layout.data_size);
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300427}
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300428
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300429#endif
430
Marek Behúna56f0ca2024-05-21 09:13:31 +0200431static int eeprom_action_expected_argc(enum eeprom_action action)
432{
433 switch (action) {
Marek Behúne5545062024-05-21 09:13:35 +0200434 case EEPROM_LIST:
435 return 0;
Marek Behúna56f0ca2024-05-21 09:13:31 +0200436 case EEPROM_READ:
437 case EEPROM_WRITE:
438 return 3;
439 case EEPROM_PRINT:
440 return 0;
441 case EEPROM_UPDATE:
442 return 2;
443 default:
444 return CMD_RET_USAGE;
445 }
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300446}
447
448#define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
Simon Glassed38aef2020-05-10 11:40:03 -0600449int do_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300450{
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300451 enum eeprom_action action = EEPROM_ACTION_INVALID;
Marek Behún246d1a52024-05-21 09:13:32 +0200452 struct eeprom_dev_spec dev;
453 ulong addr = 0, cnt = 0, off = 0;
454 int ret, index = 0;
Marek Behún533d3382024-05-21 09:13:33 +0200455#ifdef CONFIG_CMD_EEPROM_LAYOUT
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300456 char *field_name = "";
457 char *field_value = "";
Marek Behún533d3382024-05-21 09:13:33 +0200458 int layout_ver = LAYOUT_VERSION_AUTODETECT;
459#endif
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300460
461 if (argc <= 1)
462 return CMD_RET_USAGE;
463
464 NEXT_PARAM(argc, index); /* Skip program name */
465
466 action = parse_action(argv[index]);
467 NEXT_PARAM(argc, index);
468
Nikita Kiryanov9e559fd2016-04-16 17:55:13 +0300469 if (action == EEPROM_ACTION_INVALID)
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300470 return CMD_RET_USAGE;
471
Marek Behúne5545062024-05-21 09:13:35 +0200472#if CONFIG_IS_ENABLED(I2C_EEPROM)
473 if (action == EEPROM_LIST)
474 return do_eeprom_list();
475#endif
476
Marek Behúnbff98ba2024-05-21 09:13:30 +0200477#ifdef CONFIG_EEPROM_LAYOUT_VERSIONS
Nikita Kiryanov9e559fd2016-04-16 17:55:13 +0300478 if (action == EEPROM_PRINT || action == EEPROM_UPDATE) {
479 if (!strcmp(argv[index], "-l")) {
480 NEXT_PARAM(argc, index);
481 layout_ver = eeprom_parse_layout_version(argv[index]);
482 NEXT_PARAM(argc, index);
483 }
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300484 }
Nikita Kiryanov9e559fd2016-04-16 17:55:13 +0300485#endif
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300486
Marek Behún246d1a52024-05-21 09:13:32 +0200487 ret = parse_eeprom_dev_spec(&dev,
488 argc - eeprom_action_expected_argc(action),
489 argv + index);
Nikita Kiryanov9e559fd2016-04-16 17:55:13 +0300490 if (ret == CMD_RET_USAGE)
491 return ret;
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300492
Nikita Kiryanov9e559fd2016-04-16 17:55:13 +0300493 while (ret--)
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300494 NEXT_PARAM(argc, index);
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300495
Nikita Kiryanov9e559fd2016-04-16 17:55:13 +0300496 if (action == EEPROM_READ || action == EEPROM_WRITE) {
497 addr = parse_numeric_param(argv[index]);
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300498 NEXT_PARAM(argc, index);
Nikita Kiryanov9e559fd2016-04-16 17:55:13 +0300499 off = parse_numeric_param(argv[index]);
500 NEXT_PARAM(argc, index);
501 cnt = parse_numeric_param(argv[index]);
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300502 }
503
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300504#ifdef CONFIG_CMD_EEPROM_LAYOUT
Nikita Kiryanov9e559fd2016-04-16 17:55:13 +0300505 if (action == EEPROM_UPDATE) {
506 field_name = argv[index];
507 NEXT_PARAM(argc, index);
508 field_value = argv[index];
509 NEXT_PARAM(argc, index);
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300510 }
511#endif
512
Marek Behúne5545062024-05-21 09:13:35 +0200513 eeprom_dev_spec_init(&dev);
Marek Behún533d3382024-05-21 09:13:33 +0200514
515 switch (action) {
516 case EEPROM_READ:
517 case EEPROM_WRITE:
518 return do_eeprom_rw(&dev, action == EEPROM_READ,
519 addr, off, cnt);
520#ifdef CONFIG_CMD_EEPROM_LAYOUT
521 case EEPROM_PRINT:
522 return do_eeprom_print(&dev, layout_ver);
523 case EEPROM_UPDATE:
524 return do_eeprom_update(&dev, layout_ver,
525 field_name, field_value);
526#endif
527 default:
528 return CMD_RET_USAGE;
529 }
Marek Vasut704a0962015-11-10 20:53:18 +0100530}
wdenk57b2d802003-06-27 21:31:46 +0000531
Marek Behúnbff98ba2024-05-21 09:13:30 +0200532#ifdef CONFIG_EEPROM_LAYOUT_VERSIONS
533#define EEPROM_LAYOUT_SPEC "[-l <layout_version>] "
534#else
535#define EEPROM_LAYOUT_SPEC ""
536#endif
537
Marek Behúne5545062024-05-21 09:13:35 +0200538#if CONFIG_IS_ENABLED(I2C_EEPROM)
539# define EEPROM_DEV_SPEC "[device_specifier]"
540#else
541# define EEPROM_DEV_SPEC "[[bus] devaddr]"
542#endif
543
wdenkf287a242003-07-01 21:06:45 +0000544U_BOOT_CMD(
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300545 eeprom, 8, 1, do_eeprom,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600546 "EEPROM sub-system",
Marek Behúne5545062024-05-21 09:13:35 +0200547#if CONFIG_IS_ENABLED(I2C_EEPROM)
548 "list\n"
549 "eeprom "
550#endif
551 "read " EEPROM_DEV_SPEC " addr off cnt\n"
552 "eeprom write " EEPROM_DEV_SPEC " addr off cnt\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200553 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300554#ifdef CONFIG_CMD_EEPROM_LAYOUT
555 "\n"
Marek Behúne5545062024-05-21 09:13:35 +0200556 "eeprom print " EEPROM_LAYOUT_SPEC EEPROM_DEV_SPEC "\n"
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300557 " - Print layout fields and their data in human readable format\n"
Marek Behúne5545062024-05-21 09:13:35 +0200558 "eeprom update " EEPROM_LAYOUT_SPEC EEPROM_DEV_SPEC " field_name field_value\n"
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300559 " - Update a specific eeprom field with new data.\n"
Marek Behúnbff98ba2024-05-21 09:13:30 +0200560 " The new data must be written in the same human readable format as shown by the print command."
Marek Behúne5545062024-05-21 09:13:35 +0200561#endif
562#if CONFIG_IS_ENABLED(I2C_EEPROM)
563 "\n\n"
564 "DEVICE SPECIFIER - the eeprom device can be specified\n"
565 " [dev_name] - by device name (devices can listed with the eeprom list command)\n"
566 " [[bus] devaddr] - or by I2C bus and I2C device address\n"
567 "If no device specifier is given, the first driver-model found device is used."
568#endif
Marek Behúnbff98ba2024-05-21 09:13:30 +0200569#ifdef CONFIG_EEPROM_LAYOUT_VERSIONS
570 "\n\n"
Nikita Kiryanov37dda1c2016-04-16 17:55:03 +0300571 "LAYOUT VERSIONS\n"
572 "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
573 "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
574 "The values which can be provided with the -l option are:\n"
575 CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"
576#endif
Simon Glass761d6022023-11-18 14:04:52 -0700577);