]> git.ipfire.org Git - people/ms/u-boot.git/blame - cmd/eeprom.c
Convert CONFIG_CMD_EEPROM et al to Kconfig
[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>
e7c2729b 27#include <eeprom_layout.h>
3863585b 28
4f296d09
MV
29#ifndef CONFIG_SYS_I2C_SPEED
30#define CONFIG_SYS_I2C_SPEED 50000
98f4a3df 31#endif
3863585b 32
d738746c
MV
33#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
34#define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0
35#endif
36
6717e3c8
MV
37#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
38#define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8
39#endif
40
a6e7b774
MS
41#ifndef I2C_RXTX_LEN
42#define I2C_RXTX_LEN 128
43#endif
44
6717e3c8
MV
45#define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
46#define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
47
4f296d09 48/*
6d0f6bcf 49 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
3863585b
WD
50 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
51 *
6d0f6bcf 52 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
3863585b
WD
53 * 0x00000nxx for EEPROM address selectors and page number at n.
54 */
548738b4 55#if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
4f296d09 56#if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
d4e69e61
MV
57 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
58 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
6d0f6bcf 59#error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
3863585b
WD
60#endif
61#endif
62
52cd47c9
MV
63__weak int eeprom_write_enable(unsigned dev_addr, int state)
64{
65 return 0;
66}
4f296d09 67
354e3ed7 68void eeprom_init(int bus)
4f296d09
MV
69{
70 /* SPI EEPROM */
71#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
8eee40a6 72 spi_init_f();
4f296d09
MV
73#endif
74
75 /* I2C EEPROM */
354e3ed7
MV
76#if defined(CONFIG_SYS_I2C)
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
111 * bytes that can be ccessed with the single read or write
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
133 /* SPI */
134#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
135 if (read)
136 spi_read(addr, alen, buffer, len);
137 else
138 spi_write(addr, alen, buffer, len);
139#else /* I2C */
140
141#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
142 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
143#endif
144
145 if (read)
146 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
147 else
148 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
149
150 if (ret)
151 ret = 1;
152#endif
153 return ret;
154}
155
1a37889b
MV
156static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
157 unsigned cnt, bool read)
3863585b
WD
158{
159 unsigned end = offset + cnt;
1a37889b 160 unsigned alen, len;
3863585b 161 int rcode = 0;
02c321cf 162 uchar addr[3];
3863585b 163
3863585b 164 while (offset < end) {
02c321cf 165 alen = eeprom_addr(dev_addr, offset, addr);
3863585b 166
39b6f98b 167 len = eeprom_len(offset, end);
d4f5c728 168
1a37889b 169 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
9132088b 170
3863585b
WD
171 buffer += len;
172 offset += len;
1a37889b
MV
173
174 if (!read)
175 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
3863585b 176 }
d4f5c728 177
3863585b
WD
178 return rcode;
179}
180
1a37889b 181int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
3863585b 182{
1a37889b
MV
183 /*
184 * Read data until done or would cross a page boundary.
185 * We must write the address again when changing pages
186 * because the next page may be in a different device.
187 */
188 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
189}
190
d4e69e61
MV
191int eeprom_write(unsigned dev_addr, unsigned offset,
192 uchar *buffer, unsigned cnt)
1a37889b
MV
193{
194 int ret;
3863585b 195
52cd47c9
MV
196 eeprom_write_enable(dev_addr, 1);
197
4f296d09
MV
198 /*
199 * Write data until done or would cross a write page boundary.
3863585b
WD
200 * We must write the address again when changing pages
201 * because the address counter only increments within a page.
202 */
52bc7c7e 203 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
52cd47c9
MV
204
205 eeprom_write_enable(dev_addr, 0);
1a37889b 206 return ret;
3863585b
WD
207}
208
c40f0372
NK
209static int parse_numeric_param(char *str)
210{
211 char *endptr;
212 int value = simple_strtol(str, &endptr, 16);
213
214 return (*endptr != '\0') ? -1 : value;
215}
216
217/**
218 * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
219 *
220 * @i2c_bus: address to store the i2c bus
221 * @i2c_addr: address to store the device i2c address
222 * @argc: count of command line arguments left to parse
223 * @argv: command line arguments left to parse
224 * @argc_no_bus_addr: argc value we expect to see when bus & addr aren't given
225 *
226 * @returns: number of arguments parsed or CMD_RET_USAGE if error
227 */
228static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
229 char * const argv[], int argc_no_bus_addr)
230{
231 int argc_no_bus = argc_no_bus_addr + 1;
232 int argc_bus_addr = argc_no_bus_addr + 2;
233
234#ifdef CONFIG_SYS_DEF_EEPROM_ADDR
235 if (argc == argc_no_bus_addr) {
236 *i2c_bus = -1;
237 *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
238
239 return 0;
240 }
241#endif
242 if (argc == argc_no_bus) {
243 *i2c_bus = -1;
244 *i2c_addr = parse_numeric_param(argv[0]);
245
246 return 1;
247 }
248
249 if (argc == argc_bus_addr) {
250 *i2c_bus = parse_numeric_param(argv[0]);
251 *i2c_addr = parse_numeric_param(argv[1]);
252
253 return 2;
254 }
255
256 return CMD_RET_USAGE;
257}
258
aa9e6044 259#ifdef CONFIG_CMD_EEPROM_LAYOUT
aa9e6044
NK
260
261__weak int eeprom_parse_layout_version(char *str)
262{
263 return LAYOUT_VERSION_UNRECOGNIZED;
264}
265
266static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
267
e7c2729b
NK
268#endif
269
aa9e6044 270enum eeprom_action {
e7c2729b
NK
271 EEPROM_READ,
272 EEPROM_WRITE,
aa9e6044
NK
273 EEPROM_PRINT,
274 EEPROM_UPDATE,
275 EEPROM_ACTION_INVALID,
276};
277
278static enum eeprom_action parse_action(char *cmd)
279{
3dc9be82
NK
280 if (!strncmp(cmd, "read", 4))
281 return EEPROM_READ;
282 if (!strncmp(cmd, "write", 5))
283 return EEPROM_WRITE;
284#ifdef CONFIG_CMD_EEPROM_LAYOUT
aa9e6044
NK
285 if (!strncmp(cmd, "print", 5))
286 return EEPROM_PRINT;
287 if (!strncmp(cmd, "update", 6))
288 return EEPROM_UPDATE;
3dc9be82 289#endif
aa9e6044
NK
290
291 return EEPROM_ACTION_INVALID;
292}
293
aa9e6044 294static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
e7c2729b
NK
295 ulong i2c_addr, int layout_ver, char *key,
296 char *value, ulong addr, ulong off, ulong cnt)
aa9e6044 297{
e7c2729b
NK
298 int rcode = 0;
299 const char *const fmt =
300 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
301#ifdef CONFIG_CMD_EEPROM_LAYOUT
aa9e6044 302 struct eeprom_layout layout;
e7c2729b 303#endif
aa9e6044
NK
304
305 if (action == EEPROM_ACTION_INVALID)
306 return CMD_RET_USAGE;
307
308 eeprom_init(i2c_bus);
e7c2729b
NK
309 if (action == EEPROM_READ) {
310 printf(fmt, i2c_addr, "read", addr, off, cnt);
311
312 rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
313
314 puts("done\n");
315 return rcode;
316 } else if (action == EEPROM_WRITE) {
317 printf(fmt, i2c_addr, "write", addr, off, cnt);
318
319 rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
320
321 puts("done\n");
322 return rcode;
323 }
324
325#ifdef CONFIG_CMD_EEPROM_LAYOUT
aa9e6044
NK
326 rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
327 if (rcode < 0)
328 return rcode;
329
330 eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
331 layout_ver);
332
333 if (action == EEPROM_PRINT) {
334 layout.print(&layout);
335 return 0;
336 }
337
338 layout.update(&layout, key, value);
339
340 rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
e7c2729b 341#endif
aa9e6044
NK
342
343 return rcode;
344}
345
346#define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
3dc9be82 347int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
aa9e6044
NK
348{
349 int layout_ver = LAYOUT_VERSION_AUTODETECT;
350 enum eeprom_action action = EEPROM_ACTION_INVALID;
3dc9be82
NK
351 int i2c_bus = -1, index = 0;
352 ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0;
353 int ret;
aa9e6044
NK
354 char *field_name = "";
355 char *field_value = "";
356
357 if (argc <= 1)
358 return CMD_RET_USAGE;
359
360 NEXT_PARAM(argc, index); /* Skip program name */
361
362 action = parse_action(argv[index]);
363 NEXT_PARAM(argc, index);
364
3dc9be82 365 if (action == EEPROM_ACTION_INVALID)
aa9e6044
NK
366 return CMD_RET_USAGE;
367
3dc9be82
NK
368#ifdef CONFIG_CMD_EEPROM_LAYOUT
369 if (action == EEPROM_PRINT || action == EEPROM_UPDATE) {
370 if (!strcmp(argv[index], "-l")) {
371 NEXT_PARAM(argc, index);
372 layout_ver = eeprom_parse_layout_version(argv[index]);
373 NEXT_PARAM(argc, index);
374 }
aa9e6044 375 }
3dc9be82 376#endif
aa9e6044 377
3dc9be82
NK
378 switch (action) {
379 case EEPROM_READ:
380 case EEPROM_WRITE:
381 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
382 argv + index, 3);
383 break;
384 case EEPROM_PRINT:
385 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
386 argv + index, 0);
387 break;
388 case EEPROM_UPDATE:
389 ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
390 argv + index, 2);
391 break;
392 default:
393 /* Get compiler to stop whining */
aa9e6044 394 return CMD_RET_USAGE;
3dc9be82 395 }
aa9e6044 396
3dc9be82
NK
397 if (ret == CMD_RET_USAGE)
398 return ret;
aa9e6044 399
3dc9be82 400 while (ret--)
aa9e6044 401 NEXT_PARAM(argc, index);
aa9e6044 402
3dc9be82
NK
403 if (action == EEPROM_READ || action == EEPROM_WRITE) {
404 addr = parse_numeric_param(argv[index]);
405 NEXT_PARAM(argc, index);
406 off = parse_numeric_param(argv[index]);
aa9e6044 407 NEXT_PARAM(argc, index);
3dc9be82 408 cnt = parse_numeric_param(argv[index]);
aa9e6044
NK
409 }
410
aa9e6044 411#ifdef CONFIG_CMD_EEPROM_LAYOUT
3dc9be82
NK
412 if (action == EEPROM_UPDATE) {
413 field_name = argv[index];
414 NEXT_PARAM(argc, index);
415 field_value = argv[index];
416 NEXT_PARAM(argc, index);
aa9e6044
NK
417 }
418#endif
419
3dc9be82
NK
420 return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
421 field_name, field_value, addr, off, cnt);
4f296d09 422}
8bde7f77 423
0d498393 424U_BOOT_CMD(
aa9e6044 425 eeprom, 8, 1, do_eeprom,
2fb2604d 426 "EEPROM sub-system",
d4fec4e9
MV
427 "read <bus> <devaddr> addr off cnt\n"
428 "eeprom write <bus> <devaddr> addr off cnt\n"
a89c33db 429 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
aa9e6044
NK
430#ifdef CONFIG_CMD_EEPROM_LAYOUT
431 "\n"
3dc9be82 432 "eeprom print [-l <layout_version>] <bus> <devaddr>\n"
aa9e6044 433 " - Print layout fields and their data in human readable format\n"
3dc9be82 434 "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n"
aa9e6044
NK
435 " - Update a specific eeprom field with new data.\n"
436 " The new data must be written in the same human readable format as shown by the print command.\n"
437 "\n"
438 "LAYOUT VERSIONS\n"
439 "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
440 "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
441 "The values which can be provided with the -l option are:\n"
442 CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"
443#endif
0e350f81 444)