]> Git Repo - J-u-boot.git/blame - cmd/eeprom.c
cmd: avb: Support A/B slots
[J-u-boot.git] / cmd / eeprom.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
3863585b
WD
2/*
3 * (C) Copyright 2000, 2001
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
3863585b
WD
5 */
6
d4f5c728 7/*
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
e506a006 11 * write delay. Also, there is no limit imposed on the number of bytes that can
d4f5c728 12 * be transferred with a single read or write.
6617aae9 13 *
d4f5c728 14 * Use the following configuration options to ensure no unneeded performance
15 * degradation (typical for EEPROM) is incured for FRAM memory:
6617aae9 16 *
6d0f6bcf
JCPV
17 * #define CONFIG_SYS_I2C_FRAM
18 * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
d4f5c728 19 *
20 */
21
3863585b
WD
22#include <common.h>
23#include <config.h>
24#include <command.h>
25#include <i2c.h>
e7c2729b 26#include <eeprom_layout.h>
3863585b 27
4f296d09
MV
28#ifndef CONFIG_SYS_I2C_SPEED
29#define CONFIG_SYS_I2C_SPEED 50000
98f4a3df 30#endif
3863585b 31
d738746c
MV
32#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
33#define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0
34#endif
35
6717e3c8
MV
36#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
37#define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8
38#endif
39
a6e7b774
MS
40#ifndef I2C_RXTX_LEN
41#define I2C_RXTX_LEN 128
42#endif
43
6717e3c8
MV
44#define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
45#define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
46
4f296d09 47/*
6d0f6bcf 48 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
3863585b
WD
49 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
50 *
6d0f6bcf 51 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
3863585b
WD
52 * 0x00000nxx for EEPROM address selectors and page number at n.
53 */
548738b4 54#if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
4f296d09 55#if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
d4e69e61
MV
56 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
57 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
6d0f6bcf 58#error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
3863585b
WD
59#endif
60#endif
61
4f256177
SG
62#if defined(CONFIG_DM_I2C)
63int eeprom_i2c_bus;
64#endif
65
52cd47c9
MV
66__weak int eeprom_write_enable(unsigned dev_addr, int state)
67{
68 return 0;
69}
4f296d09 70
354e3ed7 71void eeprom_init(int bus)
4f296d09 72{
4f296d09 73 /* I2C EEPROM */
4f256177
SG
74#if defined(CONFIG_DM_I2C)
75 eeprom_i2c_bus = bus;
76#elif defined(CONFIG_SYS_I2C)
354e3ed7
MV
77 if (bus >= 0)
78 i2c_set_bus_num(bus);
4f296d09
MV
79 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
80#endif
81}
82
02c321cf
MV
83static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
84{
85 unsigned blk_off;
86 int alen;
87
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 */
92 alen = 2;
93#else
94 addr[0] = offset >> 16; /* block number */
95 addr[1] = offset >> 8; /* upper address octet */
96 addr[2] = blk_off; /* lower address octet */
97 alen = 3;
98#endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
99
100 addr[0] |= dev_addr; /* insert device address */
101
102 return alen;
103}
104
39b6f98b
MV
105static int eeprom_len(unsigned offset, unsigned end)
106{
107 unsigned len = end - offset;
108
109 /*
110 * For a FRAM device there is no limit on the number of the
35087fb4 111 * bytes that can be accessed with the single read or write
39b6f98b
MV
112 * operation.
113 */
114#if !defined(CONFIG_SYS_I2C_FRAM)
115 unsigned blk_off = offset & 0xff;
116 unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
117
118 if (maxlen > I2C_RXTX_LEN)
119 maxlen = I2C_RXTX_LEN;
120
121 if (len > maxlen)
122 len = maxlen;
123#endif
124
125 return len;
126}
127
9132088b
MV
128static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
129 uchar *buffer, unsigned len, bool read)
130{
131 int ret = 0;
132
4f256177 133#if defined(CONFIG_DM_I2C)
0c07a9b4
LM
134 struct udevice *dev;
135
4f256177 136 ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0],
0c07a9b4
LM
137 alen - 1, &dev);
138 if (ret) {
139 printf("%s: Cannot find udev for a bus %d\n", __func__,
4f256177 140 eeprom_i2c_bus);
0c07a9b4
LM
141 return CMD_RET_FAILURE;
142 }
143
144 if (read)
145 ret = dm_i2c_read(dev, offset, buffer, len);
146 else
147 ret = dm_i2c_write(dev, offset, buffer, len);
148
149#else /* Non DM I2C support - will be removed */
9132088b
MV
150
151 if (read)
152 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
153 else
154 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
4f256177 155#endif /* CONFIG_DM_I2C */
0c07a9b4
LM
156 if (ret)
157 ret = CMD_RET_FAILURE;
158
9132088b
MV
159 return ret;
160}
161
1a37889b
MV
162static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
163 unsigned cnt, bool read)
3863585b
WD
164{
165 unsigned end = offset + cnt;
1a37889b 166 unsigned alen, len;
3863585b 167 int rcode = 0;
02c321cf 168 uchar addr[3];
3863585b 169
4f256177
SG
170#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
171 eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS);
172#endif
173
3863585b 174 while (offset < end) {
02c321cf 175 alen = eeprom_addr(dev_addr, offset, addr);
3863585b 176
39b6f98b 177 len = eeprom_len(offset, end);
d4f5c728 178
1a37889b 179 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
9132088b 180
3863585b
WD
181 buffer += len;
182 offset += len;
1a37889b
MV
183
184 if (!read)
185 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
3863585b 186 }
d4f5c728 187
3863585b
WD
188 return rcode;
189}
190
1a37889b 191int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
3863585b 192{
1a37889b
MV
193 /*
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.
197 */
198 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
199}
200
d4e69e61
MV
201int eeprom_write(unsigned dev_addr, unsigned offset,
202 uchar *buffer, unsigned cnt)
1a37889b
MV
203{
204 int ret;
3863585b 205
52cd47c9
MV
206 eeprom_write_enable(dev_addr, 1);
207
4f296d09
MV
208 /*
209 * Write data until done or would cross a write page boundary.
3863585b
WD
210 * We must write the address again when changing pages
211 * because the address counter only increments within a page.
212 */
52bc7c7e 213 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
52cd47c9
MV
214
215 eeprom_write_enable(dev_addr, 0);
1a37889b 216 return ret;
3863585b
WD
217}
218
c40f0372
NK
219static int parse_numeric_param(char *str)
220{
221 char *endptr;
222 int value = simple_strtol(str, &endptr, 16);
223
224 return (*endptr != '\0') ? -1 : value;
225}
226
227/**
228 * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
229 *
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
235 *
236 * @returns: number of arguments parsed or CMD_RET_USAGE if error
237 */
238static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
239 char * const argv[], int argc_no_bus_addr)
240{
241 int argc_no_bus = argc_no_bus_addr + 1;
242 int argc_bus_addr = argc_no_bus_addr + 2;
243
244#ifdef CONFIG_SYS_DEF_EEPROM_ADDR
245 if (argc == argc_no_bus_addr) {
246 *i2c_bus = -1;
247 *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
248
249 return 0;
250 }
251#endif
252 if (argc == argc_no_bus) {
253 *i2c_bus = -1;
254 *i2c_addr = parse_numeric_param(argv[0]);
255
256 return 1;
257 }
258
259 if (argc == argc_bus_addr) {
260 *i2c_bus = parse_numeric_param(argv[0]);
261 *i2c_addr = parse_numeric_param(argv[1]);
262
263 return 2;
264 }
265
266 return CMD_RET_USAGE;
267}
268
aa9e6044 269#ifdef CONFIG_CMD_EEPROM_LAYOUT
aa9e6044
NK
270
271__weak int eeprom_parse_layout_version(char *str)
272{
273 return LAYOUT_VERSION_UNRECOGNIZED;
274}
275
276static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
277
e7c2729b
NK
278#endif
279
aa9e6044 280enum eeprom_action {
e7c2729b
NK
281 EEPROM_READ,
282 EEPROM_WRITE,
aa9e6044
NK
283 EEPROM_PRINT,
284 EEPROM_UPDATE,
285 EEPROM_ACTION_INVALID,
286};
287
288static enum eeprom_action parse_action(char *cmd)
289{
3dc9be82
NK
290 if (!strncmp(cmd, "read", 4))
291 return EEPROM_READ;
292 if (!strncmp(cmd, "write", 5))
293 return EEPROM_WRITE;
294#ifdef CONFIG_CMD_EEPROM_LAYOUT
aa9e6044
NK
295 if (!strncmp(cmd, "print", 5))
296 return EEPROM_PRINT;
297 if (!strncmp(cmd, "update", 6))
298 return EEPROM_UPDATE;
3dc9be82 299#endif
aa9e6044
NK
300
301 return EEPROM_ACTION_INVALID;
302}
303
aa9e6044 304static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
e7c2729b
NK
305 ulong i2c_addr, int layout_ver, char *key,
306 char *value, ulong addr, ulong off, ulong cnt)
aa9e6044 307{
e7c2729b
NK
308 int rcode = 0;
309 const char *const fmt =
310 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
311#ifdef CONFIG_CMD_EEPROM_LAYOUT
aa9e6044 312 struct eeprom_layout layout;
e7c2729b 313#endif
aa9e6044
NK
314
315 if (action == EEPROM_ACTION_INVALID)
316 return CMD_RET_USAGE;
317
318 eeprom_init(i2c_bus);
e7c2729b
NK
319 if (action == EEPROM_READ) {
320 printf(fmt, i2c_addr, "read", addr, off, cnt);
321
322 rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
323
324 puts("done\n");
325 return rcode;
326 } else if (action == EEPROM_WRITE) {
327 printf(fmt, i2c_addr, "write", addr, off, cnt);
328
329 rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
330
331 puts("done\n");
332 return rcode;
333 }
334
335#ifdef CONFIG_CMD_EEPROM_LAYOUT
aa9e6044
NK
336 rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
337 if (rcode < 0)
338 return rcode;
339
340 eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
341 layout_ver);
342
343 if (action == EEPROM_PRINT) {
344 layout.print(&layout);
345 return 0;
346 }
347
348 layout.update(&layout, key, value);
349
350 rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
e7c2729b 351#endif
aa9e6044
NK
352
353 return rcode;
354}
355
356#define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
3dc9be82 357int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
aa9e6044
NK
358{
359 int layout_ver = LAYOUT_VERSION_AUTODETECT;
360 enum eeprom_action action = EEPROM_ACTION_INVALID;
3dc9be82
NK
361 int i2c_bus = -1, index = 0;
362 ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0;
363 int ret;
aa9e6044
NK
364 char *field_name = "";
365 char *field_value = "";
366
367 if (argc <= 1)
368 return CMD_RET_USAGE;
369
370 NEXT_PARAM(argc, index); /* Skip program name */
371
372 action = parse_action(argv[index]);
373 NEXT_PARAM(argc, index);
374
3dc9be82 375 if (action == EEPROM_ACTION_INVALID)
aa9e6044
NK
376 return CMD_RET_USAGE;
377
3dc9be82
NK
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);
384 }
aa9e6044 385 }
3dc9be82 386#endif
aa9e6044 387
3dc9be82
NK
388 switch (action) {
389 case EEPROM_READ:
390 case EEPROM_WRITE:
391 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
392 argv + index, 3);
393 break;
394 case EEPROM_PRINT:
395 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
396 argv + index, 0);
397 break;
398 case EEPROM_UPDATE:
399 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
400 argv + index, 2);
401 break;
402 default:
403 /* Get compiler to stop whining */
aa9e6044 404 return CMD_RET_USAGE;
3dc9be82 405 }
aa9e6044 406
3dc9be82
NK
407 if (ret == CMD_RET_USAGE)
408 return ret;
aa9e6044 409
3dc9be82 410 while (ret--)
aa9e6044 411 NEXT_PARAM(argc, index);
aa9e6044 412
3dc9be82
NK
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]);
aa9e6044 417 NEXT_PARAM(argc, index);
3dc9be82 418 cnt = parse_numeric_param(argv[index]);
aa9e6044
NK
419 }
420
aa9e6044 421#ifdef CONFIG_CMD_EEPROM_LAYOUT
3dc9be82
NK
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);
aa9e6044
NK
427 }
428#endif
429
3dc9be82
NK
430 return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
431 field_name, field_value, addr, off, cnt);
4f296d09 432}
8bde7f77 433
0d498393 434U_BOOT_CMD(
aa9e6044 435 eeprom, 8, 1, do_eeprom,
2fb2604d 436 "EEPROM sub-system",
d4fec4e9
MV
437 "read <bus> <devaddr> addr off cnt\n"
438 "eeprom write <bus> <devaddr> addr off cnt\n"
a89c33db 439 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
aa9e6044
NK
440#ifdef CONFIG_CMD_EEPROM_LAYOUT
441 "\n"
3dc9be82 442 "eeprom print [-l <layout_version>] <bus> <devaddr>\n"
aa9e6044 443 " - Print layout fields and their data in human readable format\n"
3dc9be82 444 "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n"
aa9e6044
NK
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"
447 "\n"
448 "LAYOUT VERSIONS\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"
453#endif
0e350f81 454)
This page took 0.508207 seconds and 4 git commands to generate.