]> git.ipfire.org Git - people/ms/u-boot.git/blame - cmd/eeprom.c
cmd: eeprom: add support for layout aware commands
[people/ms/u-boot.git] / cmd / eeprom.c
CommitLineData
3863585b
WD
1/*
2 * (C) Copyright 2000, 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
3863585b
WD
6 */
7
d4f5c728 8/*
9 * Support for read and write access to EEPROM like memory devices. This
10 * includes regular EEPROM as well as FRAM (ferroelectic nonvolaile RAM).
11 * FRAM devices read and write data at bus speed. In particular, there is no
e506a006 12 * write delay. Also, there is no limit imposed on the number of bytes that can
d4f5c728 13 * be transferred with a single read or write.
6617aae9 14 *
d4f5c728 15 * Use the following configuration options to ensure no unneeded performance
16 * degradation (typical for EEPROM) is incured for FRAM memory:
6617aae9 17 *
6d0f6bcf
JCPV
18 * #define CONFIG_SYS_I2C_FRAM
19 * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
d4f5c728 20 *
21 */
22
3863585b
WD
23#include <common.h>
24#include <config.h>
25#include <command.h>
26#include <i2c.h>
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
52cd47c9
MV
62__weak int eeprom_write_enable(unsigned dev_addr, int state)
63{
64 return 0;
65}
4f296d09 66
354e3ed7 67void eeprom_init(int bus)
4f296d09
MV
68{
69 /* SPI EEPROM */
70#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
8eee40a6 71 spi_init_f();
4f296d09
MV
72#endif
73
74 /* I2C EEPROM */
2636ac65 75#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
354e3ed7
MV
76#if defined(CONFIG_SYS_I2C)
77 if (bus >= 0)
78 i2c_set_bus_num(bus);
79#endif
4f296d09
MV
80 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
81#endif
82}
83
02c321cf
MV
84static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
85{
86 unsigned blk_off;
87 int alen;
88
89 blk_off = offset & 0xff; /* block offset */
90#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
91 addr[0] = offset >> 8; /* block number */
92 addr[1] = blk_off; /* block offset */
93 alen = 2;
94#else
95 addr[0] = offset >> 16; /* block number */
96 addr[1] = offset >> 8; /* upper address octet */
97 addr[2] = blk_off; /* lower address octet */
98 alen = 3;
99#endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
100
101 addr[0] |= dev_addr; /* insert device address */
102
103 return alen;
104}
105
39b6f98b
MV
106static int eeprom_len(unsigned offset, unsigned end)
107{
108 unsigned len = end - offset;
109
110 /*
111 * For a FRAM device there is no limit on the number of the
112 * bytes that can be ccessed with the single read or write
113 * operation.
114 */
115#if !defined(CONFIG_SYS_I2C_FRAM)
116 unsigned blk_off = offset & 0xff;
117 unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
118
119 if (maxlen > I2C_RXTX_LEN)
120 maxlen = I2C_RXTX_LEN;
121
122 if (len > maxlen)
123 len = maxlen;
124#endif
125
126 return len;
127}
128
9132088b
MV
129static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
130 uchar *buffer, unsigned len, bool read)
131{
132 int ret = 0;
133
134 /* SPI */
135#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
136 if (read)
137 spi_read(addr, alen, buffer, len);
138 else
139 spi_write(addr, alen, buffer, len);
140#else /* I2C */
141
142#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
143 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
144#endif
145
146 if (read)
147 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
148 else
149 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
150
151 if (ret)
152 ret = 1;
153#endif
154 return ret;
155}
156
1a37889b
MV
157static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
158 unsigned cnt, bool read)
3863585b
WD
159{
160 unsigned end = offset + cnt;
1a37889b 161 unsigned alen, len;
3863585b 162 int rcode = 0;
02c321cf 163 uchar addr[3];
3863585b 164
3863585b 165 while (offset < end) {
02c321cf 166 alen = eeprom_addr(dev_addr, offset, addr);
3863585b 167
39b6f98b 168 len = eeprom_len(offset, end);
d4f5c728 169
1a37889b 170 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
9132088b 171
3863585b
WD
172 buffer += len;
173 offset += len;
1a37889b
MV
174
175 if (!read)
176 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
3863585b 177 }
d4f5c728 178
3863585b
WD
179 return rcode;
180}
181
1a37889b 182int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
3863585b 183{
1a37889b
MV
184 /*
185 * Read data until done or would cross a page boundary.
186 * We must write the address again when changing pages
187 * because the next page may be in a different device.
188 */
189 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
190}
191
d4e69e61
MV
192int eeprom_write(unsigned dev_addr, unsigned offset,
193 uchar *buffer, unsigned cnt)
1a37889b
MV
194{
195 int ret;
3863585b 196
52cd47c9
MV
197 eeprom_write_enable(dev_addr, 1);
198
4f296d09
MV
199 /*
200 * Write data until done or would cross a write page boundary.
3863585b
WD
201 * We must write the address again when changing pages
202 * because the address counter only increments within a page.
203 */
52bc7c7e 204 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
52cd47c9
MV
205
206 eeprom_write_enable(dev_addr, 0);
1a37889b 207 return ret;
3863585b
WD
208}
209
aa9e6044
NK
210#ifdef CONFIG_CMD_EEPROM_LAYOUT
211#include <eeprom_layout.h>
212
213__weak int eeprom_parse_layout_version(char *str)
214{
215 return LAYOUT_VERSION_UNRECOGNIZED;
216}
217
218static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
219
220#ifndef CONFIG_EEPROM_LAYOUT_HELP_STRING
221#define CONFIG_EEPROM_LAYOUT_HELP_STRING "<not defined>"
222#endif
223
224enum eeprom_action {
225 EEPROM_PRINT,
226 EEPROM_UPDATE,
227 EEPROM_ACTION_INVALID,
228};
229
230static enum eeprom_action parse_action(char *cmd)
231{
232 if (!strncmp(cmd, "print", 5))
233 return EEPROM_PRINT;
234 if (!strncmp(cmd, "update", 6))
235 return EEPROM_UPDATE;
236
237 return EEPROM_ACTION_INVALID;
238}
239
240static int parse_numeric_param(char *str)
241{
242 char *endptr;
243 int value = simple_strtol(str, &endptr, 16);
244
245 return (*endptr != '\0') ? -1 : value;
246}
247
248static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
249 int i2c_addr, int layout_ver, char *key,
250 char *value)
251{
252 int rcode;
253 struct eeprom_layout layout;
254
255 if (action == EEPROM_ACTION_INVALID)
256 return CMD_RET_USAGE;
257
258 eeprom_init(i2c_bus);
259 rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
260 if (rcode < 0)
261 return rcode;
262
263 eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
264 layout_ver);
265
266 if (action == EEPROM_PRINT) {
267 layout.print(&layout);
268 return 0;
269 }
270
271 layout.update(&layout, key, value);
272
273 rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
274
275 return rcode;
276}
277
278#define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
279static int do_eeprom_layout(cmd_tbl_t *cmdtp, int flag, int argc,
280 char * const argv[])
281{
282 int layout_ver = LAYOUT_VERSION_AUTODETECT;
283 enum eeprom_action action = EEPROM_ACTION_INVALID;
284 int i2c_bus = -1, i2c_addr = -1, index = 0;
285 char *field_name = "";
286 char *field_value = "";
287
288 if (argc <= 1)
289 return CMD_RET_USAGE;
290
291 NEXT_PARAM(argc, index); /* Skip program name */
292
293 action = parse_action(argv[index]);
294 NEXT_PARAM(argc, index);
295
296 if (argc <= 1)
297 return CMD_RET_USAGE;
298
299 if (!strcmp(argv[index], "-l")) {
300 NEXT_PARAM(argc, index);
301
302 layout_ver = eeprom_parse_layout_version(argv[index]);
303 NEXT_PARAM(argc, index);
304 }
305
306 if (argc <= 1)
307 return CMD_RET_USAGE;
308
309 i2c_bus = parse_numeric_param(argv[index]);
310 NEXT_PARAM(argc, index);
311
312 i2c_addr = parse_numeric_param(argv[index]);
313 NEXT_PARAM(argc, index);
314
315 if (action == EEPROM_PRINT)
316 goto done;
317
318 if (argc) {
319 field_name = argv[index];
320 NEXT_PARAM(argc, index);
321 }
322
323 if (argc) {
324 field_value = argv[index];
325 NEXT_PARAM(argc, index);
326 }
327
328done:
329 return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
330 field_name, field_value);
331}
332
333#endif
334
4f296d09 335static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b 336{
4f296d09
MV
337 const char *const fmt =
338 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
e4f65d00
MV
339 char * const *args = &argv[2];
340 int rcode;
341 ulong dev_addr, addr, off, cnt;
d4fec4e9 342 int bus_addr;
e4f65d00 343
aa9e6044
NK
344#ifdef CONFIG_CMD_EEPROM_LAYOUT
345 if (argc >= 2) {
346 if (!strcmp(argv[1], "update") || !strcmp(argv[1], "print"))
347 return do_eeprom_layout(cmdtp, flag, argc, argv);
348 }
349#endif
350
e4f65d00
MV
351 switch (argc) {
352#ifdef CONFIG_SYS_DEF_EEPROM_ADDR
353 case 5:
d4fec4e9 354 bus_addr = -1;
e4f65d00
MV
355 dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
356 break;
357#endif
358 case 6:
d4fec4e9
MV
359 bus_addr = -1;
360 dev_addr = simple_strtoul(*args++, NULL, 16);
361 break;
362 case 7:
363 bus_addr = simple_strtoul(*args++, NULL, 16);
e4f65d00
MV
364 dev_addr = simple_strtoul(*args++, NULL, 16);
365 break;
366 default:
367 return CMD_RET_USAGE;
368 }
548738b4 369
e4f65d00
MV
370 addr = simple_strtoul(*args++, NULL, 16);
371 off = simple_strtoul(*args++, NULL, 16);
372 cnt = simple_strtoul(*args++, NULL, 16);
548738b4 373
d4fec4e9 374 eeprom_init(bus_addr);
90253178 375
d4e69e61 376 if (strcmp(argv[1], "read") == 0) {
e4f65d00 377 printf(fmt, dev_addr, argv[1], addr, off, cnt);
4f296d09 378
d4e69e61 379 rcode = eeprom_read(dev_addr, off, (uchar *)addr, cnt);
4f296d09 380
d4e69e61 381 puts("done\n");
e4f65d00 382 return rcode;
d4e69e61 383 } else if (strcmp(argv[1], "write") == 0) {
e4f65d00 384 printf(fmt, dev_addr, argv[1], addr, off, cnt);
4f296d09 385
d4e69e61 386 rcode = eeprom_write(dev_addr, off, (uchar *)addr, cnt);
4f296d09 387
d4e69e61 388 puts("done\n");
e4f65d00 389 return rcode;
4f296d09
MV
390 }
391
392 return CMD_RET_USAGE;
393}
8bde7f77 394
0d498393 395U_BOOT_CMD(
aa9e6044 396 eeprom, 8, 1, do_eeprom,
2fb2604d 397 "EEPROM sub-system",
d4fec4e9
MV
398 "read <bus> <devaddr> addr off cnt\n"
399 "eeprom write <bus> <devaddr> addr off cnt\n"
a89c33db 400 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
aa9e6044
NK
401#ifdef CONFIG_CMD_EEPROM_LAYOUT
402 "\n"
403 "eeprom print [-l <layout_version>] bus devaddr\n"
404 " - Print layout fields and their data in human readable format\n"
405 "eeprom update [-l <layout_version>] bus devaddr <field_name> <field_value>\n"
406 " - Update a specific eeprom field with new data.\n"
407 " The new data must be written in the same human readable format as shown by the print command.\n"
408 "\n"
409 "LAYOUT VERSIONS\n"
410 "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
411 "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
412 "The values which can be provided with the -l option are:\n"
413 CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"
414#endif
0e350f81 415)