]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
3863585b WD |
2 | /* |
3 | * (C) Copyright 2000, 2001 | |
4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
3863585b WD |
5 | */ |
6 | ||
d4f5c728 | 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 | |
e506a006 | 11 | * write delay. Also, there is no limit imposed on the number of bytes that can |
d4f5c728 | 12 | * be transferred with a single read or write. |
6617aae9 | 13 | * |
d4f5c728 | 14 | * Use the following configuration options to ensure no unneeded performance |
15 | * degradation (typical for EEPROM) is incured for FRAM memory: | |
6617aae9 | 16 | * |
6d0f6bcf JCPV |
17 | * #define CONFIG_SYS_I2C_FRAM |
18 | * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS | |
d4f5c728 | 19 | * |
20 | */ | |
21 | ||
3863585b WD |
22 | #include <common.h> |
23 | #include <config.h> | |
24 | #include <command.h> | |
cb3ef681 | 25 | #include <eeprom.h> |
3863585b | 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 | ||
4f256177 | 63 | #if defined(CONFIG_DM_I2C) |
ce976071 | 64 | static int eeprom_i2c_bus; |
4f256177 SG |
65 | #endif |
66 | ||
52cd47c9 MV |
67 | __weak int eeprom_write_enable(unsigned dev_addr, int state) |
68 | { | |
69 | return 0; | |
70 | } | |
4f296d09 | 71 | |
354e3ed7 | 72 | void eeprom_init(int bus) |
4f296d09 | 73 | { |
4f296d09 | 74 | /* I2C EEPROM */ |
4f256177 SG |
75 | #if defined(CONFIG_DM_I2C) |
76 | eeprom_i2c_bus = bus; | |
77 | #elif defined(CONFIG_SYS_I2C) | |
354e3ed7 MV |
78 | if (bus >= 0) |
79 | i2c_set_bus_num(bus); | |
4f296d09 MV |
80 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
81 | #endif | |
82 | } | |
83 | ||
02c321cf MV |
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 | ||
39b6f98b MV |
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 | |
35087fb4 | 112 | * bytes that can be accessed with the single read or write |
39b6f98b MV |
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 |
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 | ||
4f256177 | 134 | #if defined(CONFIG_DM_I2C) |
0c07a9b4 LM |
135 | struct udevice *dev; |
136 | ||
4f256177 | 137 | ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0], |
0c07a9b4 LM |
138 | alen - 1, &dev); |
139 | if (ret) { | |
140 | printf("%s: Cannot find udev for a bus %d\n", __func__, | |
4f256177 | 141 | eeprom_i2c_bus); |
0c07a9b4 LM |
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 */ | |
9132088b MV |
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); | |
4f256177 | 156 | #endif /* CONFIG_DM_I2C */ |
0c07a9b4 LM |
157 | if (ret) |
158 | ret = CMD_RET_FAILURE; | |
159 | ||
9132088b MV |
160 | return ret; |
161 | } | |
162 | ||
1a37889b MV |
163 | static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer, |
164 | unsigned cnt, bool read) | |
3863585b WD |
165 | { |
166 | unsigned end = offset + cnt; | |
1a37889b | 167 | unsigned alen, len; |
3863585b | 168 | int rcode = 0; |
02c321cf | 169 | uchar addr[3]; |
3863585b | 170 | |
4f256177 SG |
171 | #if defined(CONFIG_SYS_I2C_EEPROM_BUS) |
172 | eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS); | |
173 | #endif | |
174 | ||
3863585b | 175 | while (offset < end) { |
02c321cf | 176 | alen = eeprom_addr(dev_addr, offset, addr); |
3863585b | 177 | |
39b6f98b | 178 | len = eeprom_len(offset, end); |
d4f5c728 | 179 | |
1a37889b | 180 | rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read); |
9132088b | 181 | |
3863585b WD |
182 | buffer += len; |
183 | offset += len; | |
1a37889b MV |
184 | |
185 | if (!read) | |
186 | udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000); | |
3863585b | 187 | } |
d4f5c728 | 188 | |
3863585b WD |
189 | return rcode; |
190 | } | |
191 | ||
1a37889b | 192 | int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt) |
3863585b | 193 | { |
1a37889b MV |
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 | ||
d4e69e61 MV |
202 | int eeprom_write(unsigned dev_addr, unsigned offset, |
203 | uchar *buffer, unsigned cnt) | |
1a37889b MV |
204 | { |
205 | int ret; | |
3863585b | 206 | |
52cd47c9 MV |
207 | eeprom_write_enable(dev_addr, 1); |
208 | ||
4f296d09 MV |
209 | /* |
210 | * Write data until done or would cross a write page boundary. | |
3863585b WD |
211 | * We must write the address again when changing pages |
212 | * because the address counter only increments within a page. | |
213 | */ | |
52bc7c7e | 214 | ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0); |
52cd47c9 MV |
215 | |
216 | eeprom_write_enable(dev_addr, 0); | |
1a37889b | 217 | return ret; |
3863585b WD |
218 | } |
219 | ||
c40f0372 NK |
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 | ||
aa9e6044 | 270 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
aa9e6044 NK |
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 | ||
e7c2729b NK |
279 | #endif |
280 | ||
aa9e6044 | 281 | enum eeprom_action { |
e7c2729b NK |
282 | EEPROM_READ, |
283 | EEPROM_WRITE, | |
aa9e6044 NK |
284 | EEPROM_PRINT, |
285 | EEPROM_UPDATE, | |
286 | EEPROM_ACTION_INVALID, | |
287 | }; | |
288 | ||
289 | static enum eeprom_action parse_action(char *cmd) | |
290 | { | |
3dc9be82 NK |
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 | |
aa9e6044 NK |
296 | if (!strncmp(cmd, "print", 5)) |
297 | return EEPROM_PRINT; | |
298 | if (!strncmp(cmd, "update", 6)) | |
299 | return EEPROM_UPDATE; | |
3dc9be82 | 300 | #endif |
aa9e6044 NK |
301 | |
302 | return EEPROM_ACTION_INVALID; | |
303 | } | |
304 | ||
aa9e6044 | 305 | static int eeprom_execute_command(enum eeprom_action action, int i2c_bus, |
e7c2729b NK |
306 | ulong i2c_addr, int layout_ver, char *key, |
307 | char *value, ulong addr, ulong off, ulong cnt) | |
aa9e6044 | 308 | { |
e7c2729b NK |
309 | int rcode = 0; |
310 | const char *const fmt = | |
6478a7ee | 311 | "\nEEPROM @0x%lX %s: addr 0x%08lx off 0x%04lx count %ld ... "; |
e7c2729b | 312 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
aa9e6044 | 313 | struct eeprom_layout layout; |
e7c2729b | 314 | #endif |
aa9e6044 NK |
315 | |
316 | if (action == EEPROM_ACTION_INVALID) | |
317 | return CMD_RET_USAGE; | |
318 | ||
319 | eeprom_init(i2c_bus); | |
e7c2729b NK |
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 | |
aa9e6044 NK |
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); | |
e7c2729b | 352 | #endif |
aa9e6044 NK |
353 | |
354 | return rcode; | |
355 | } | |
356 | ||
357 | #define NEXT_PARAM(argc, index) { (argc)--; (index)++; } | |
3dc9be82 | 358 | int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
aa9e6044 NK |
359 | { |
360 | int layout_ver = LAYOUT_VERSION_AUTODETECT; | |
361 | enum eeprom_action action = EEPROM_ACTION_INVALID; | |
3dc9be82 NK |
362 | int i2c_bus = -1, index = 0; |
363 | ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0; | |
364 | int ret; | |
aa9e6044 NK |
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 | ||
3dc9be82 | 376 | if (action == EEPROM_ACTION_INVALID) |
aa9e6044 NK |
377 | return CMD_RET_USAGE; |
378 | ||
3dc9be82 NK |
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 | } | |
aa9e6044 | 386 | } |
3dc9be82 | 387 | #endif |
aa9e6044 | 388 | |
3dc9be82 NK |
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 */ | |
aa9e6044 | 405 | return CMD_RET_USAGE; |
3dc9be82 | 406 | } |
aa9e6044 | 407 | |
3dc9be82 NK |
408 | if (ret == CMD_RET_USAGE) |
409 | return ret; | |
aa9e6044 | 410 | |
3dc9be82 | 411 | while (ret--) |
aa9e6044 | 412 | NEXT_PARAM(argc, index); |
aa9e6044 | 413 | |
3dc9be82 NK |
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]); | |
aa9e6044 | 418 | NEXT_PARAM(argc, index); |
3dc9be82 | 419 | cnt = parse_numeric_param(argv[index]); |
aa9e6044 NK |
420 | } |
421 | ||
aa9e6044 | 422 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
3dc9be82 NK |
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); | |
aa9e6044 NK |
428 | } |
429 | #endif | |
430 | ||
3dc9be82 NK |
431 | return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver, |
432 | field_name, field_value, addr, off, cnt); | |
4f296d09 | 433 | } |
8bde7f77 | 434 | |
0d498393 | 435 | U_BOOT_CMD( |
aa9e6044 | 436 | eeprom, 8, 1, do_eeprom, |
2fb2604d | 437 | "EEPROM sub-system", |
d4fec4e9 MV |
438 | "read <bus> <devaddr> addr off cnt\n" |
439 | "eeprom write <bus> <devaddr> addr off cnt\n" | |
a89c33db | 440 | " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'" |
aa9e6044 NK |
441 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
442 | "\n" | |
3dc9be82 | 443 | "eeprom print [-l <layout_version>] <bus> <devaddr>\n" |
aa9e6044 | 444 | " - Print layout fields and their data in human readable format\n" |
3dc9be82 | 445 | "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n" |
aa9e6044 NK |
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 | |
0e350f81 | 455 | ) |