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