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