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