]>
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, [email protected]. | |
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> |
c05ed00a | 28 | #include <linux/delay.h> |
3863585b | 29 | |
4f296d09 MV |
30 | #ifndef CONFIG_SYS_I2C_SPEED |
31 | #define CONFIG_SYS_I2C_SPEED 50000 | |
98f4a3df | 32 | #endif |
3863585b | 33 | |
d738746c MV |
34 | #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS |
35 | #define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0 | |
36 | #endif | |
37 | ||
6717e3c8 MV |
38 | #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS |
39 | #define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8 | |
40 | #endif | |
41 | ||
a6e7b774 MS |
42 | #ifndef I2C_RXTX_LEN |
43 | #define I2C_RXTX_LEN 128 | |
44 | #endif | |
45 | ||
6717e3c8 MV |
46 | #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS) |
47 | #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1)) | |
48 | ||
4f296d09 | 49 | /* |
6d0f6bcf | 50 | * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is |
3863585b WD |
51 | * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM. |
52 | * | |
6d0f6bcf | 53 | * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is |
3863585b WD |
54 | * 0x00000nxx for EEPROM address selectors and page number at n. |
55 | */ | |
548738b4 | 56 | #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C) |
4f296d09 | 57 | #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \ |
d4e69e61 MV |
58 | (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \ |
59 | (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2) | |
6d0f6bcf | 60 | #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2 |
3863585b WD |
61 | #endif |
62 | #endif | |
63 | ||
4f256177 | 64 | #if defined(CONFIG_DM_I2C) |
ce976071 | 65 | static int eeprom_i2c_bus; |
4f256177 SG |
66 | #endif |
67 | ||
52cd47c9 MV |
68 | __weak int eeprom_write_enable(unsigned dev_addr, int state) |
69 | { | |
70 | return 0; | |
71 | } | |
4f296d09 | 72 | |
354e3ed7 | 73 | void eeprom_init(int bus) |
4f296d09 | 74 | { |
4f296d09 | 75 | /* I2C EEPROM */ |
4f256177 SG |
76 | #if defined(CONFIG_DM_I2C) |
77 | eeprom_i2c_bus = bus; | |
78 | #elif defined(CONFIG_SYS_I2C) | |
354e3ed7 MV |
79 | if (bus >= 0) |
80 | i2c_set_bus_num(bus); | |
4f296d09 MV |
81 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
82 | #endif | |
83 | } | |
84 | ||
02c321cf MV |
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 | ||
39b6f98b MV |
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 | |
35087fb4 | 113 | * bytes that can be accessed with the single read or write |
39b6f98b MV |
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 | ||
9132088b MV |
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 | ||
4f256177 | 135 | #if defined(CONFIG_DM_I2C) |
0c07a9b4 LM |
136 | struct udevice *dev; |
137 | ||
4f256177 | 138 | ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0], |
0c07a9b4 LM |
139 | alen - 1, &dev); |
140 | if (ret) { | |
141 | printf("%s: Cannot find udev for a bus %d\n", __func__, | |
4f256177 | 142 | eeprom_i2c_bus); |
0c07a9b4 LM |
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 */ | |
9132088b MV |
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); | |
4f256177 | 157 | #endif /* CONFIG_DM_I2C */ |
0c07a9b4 LM |
158 | if (ret) |
159 | ret = CMD_RET_FAILURE; | |
160 | ||
9132088b MV |
161 | return ret; |
162 | } | |
163 | ||
1a37889b MV |
164 | static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer, |
165 | unsigned cnt, bool read) | |
3863585b WD |
166 | { |
167 | unsigned end = offset + cnt; | |
1a37889b | 168 | unsigned alen, len; |
3863585b | 169 | int rcode = 0; |
02c321cf | 170 | uchar addr[3]; |
3863585b | 171 | |
4f256177 SG |
172 | #if defined(CONFIG_SYS_I2C_EEPROM_BUS) |
173 | eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS); | |
174 | #endif | |
175 | ||
3863585b | 176 | while (offset < end) { |
02c321cf | 177 | alen = eeprom_addr(dev_addr, offset, addr); |
3863585b | 178 | |
39b6f98b | 179 | len = eeprom_len(offset, end); |
d4f5c728 | 180 | |
1a37889b | 181 | rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read); |
9132088b | 182 | |
3863585b WD |
183 | buffer += len; |
184 | offset += len; | |
1a37889b MV |
185 | |
186 | if (!read) | |
187 | udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000); | |
3863585b | 188 | } |
d4f5c728 | 189 | |
3863585b WD |
190 | return rcode; |
191 | } | |
192 | ||
1a37889b | 193 | int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt) |
3863585b | 194 | { |
1a37889b MV |
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 | ||
d4e69e61 MV |
203 | int eeprom_write(unsigned dev_addr, unsigned offset, |
204 | uchar *buffer, unsigned cnt) | |
1a37889b MV |
205 | { |
206 | int ret; | |
3863585b | 207 | |
52cd47c9 MV |
208 | eeprom_write_enable(dev_addr, 1); |
209 | ||
4f296d09 MV |
210 | /* |
211 | * Write data until done or would cross a write page boundary. | |
3863585b WD |
212 | * We must write the address again when changing pages |
213 | * because the address counter only increments within a page. | |
214 | */ | |
52bc7c7e | 215 | ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0); |
52cd47c9 MV |
216 | |
217 | eeprom_write_enable(dev_addr, 0); | |
1a37889b | 218 | return ret; |
3863585b WD |
219 | } |
220 | ||
c40f0372 NK |
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 | */ | |
240 | static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc, | |
09140113 | 241 | char *const argv[], int argc_no_bus_addr) |
c40f0372 NK |
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 | ||
aa9e6044 | 271 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
aa9e6044 NK |
272 | |
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 | ||
e7c2729b NK |
280 | #endif |
281 | ||
aa9e6044 | 282 | enum eeprom_action { |
e7c2729b NK |
283 | EEPROM_READ, |
284 | EEPROM_WRITE, | |
aa9e6044 NK |
285 | EEPROM_PRINT, |
286 | EEPROM_UPDATE, | |
287 | EEPROM_ACTION_INVALID, | |
288 | }; | |
289 | ||
290 | static enum eeprom_action parse_action(char *cmd) | |
291 | { | |
3dc9be82 NK |
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 | |
aa9e6044 NK |
297 | if (!strncmp(cmd, "print", 5)) |
298 | return EEPROM_PRINT; | |
299 | if (!strncmp(cmd, "update", 6)) | |
300 | return EEPROM_UPDATE; | |
3dc9be82 | 301 | #endif |
aa9e6044 NK |
302 | |
303 | return EEPROM_ACTION_INVALID; | |
304 | } | |
305 | ||
aa9e6044 | 306 | static int eeprom_execute_command(enum eeprom_action action, int i2c_bus, |
e7c2729b NK |
307 | ulong i2c_addr, int layout_ver, char *key, |
308 | char *value, ulong addr, ulong off, ulong cnt) | |
aa9e6044 | 309 | { |
e7c2729b NK |
310 | int rcode = 0; |
311 | const char *const fmt = | |
6478a7ee | 312 | "\nEEPROM @0x%lX %s: addr 0x%08lx off 0x%04lx count %ld ... "; |
e7c2729b | 313 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
aa9e6044 | 314 | struct eeprom_layout layout; |
e7c2729b | 315 | #endif |
aa9e6044 NK |
316 | |
317 | if (action == EEPROM_ACTION_INVALID) | |
318 | return CMD_RET_USAGE; | |
319 | ||
320 | eeprom_init(i2c_bus); | |
e7c2729b NK |
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 | |
aa9e6044 NK |
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); | |
e7c2729b | 353 | #endif |
aa9e6044 NK |
354 | |
355 | return rcode; | |
356 | } | |
357 | ||
358 | #define NEXT_PARAM(argc, index) { (argc)--; (index)++; } | |
09140113 | 359 | int do_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
aa9e6044 NK |
360 | { |
361 | int layout_ver = LAYOUT_VERSION_AUTODETECT; | |
362 | enum eeprom_action action = EEPROM_ACTION_INVALID; | |
3dc9be82 NK |
363 | int i2c_bus = -1, index = 0; |
364 | ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0; | |
365 | int ret; | |
aa9e6044 NK |
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 | ||
3dc9be82 | 377 | if (action == EEPROM_ACTION_INVALID) |
aa9e6044 NK |
378 | return CMD_RET_USAGE; |
379 | ||
3dc9be82 NK |
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 | } | |
aa9e6044 | 387 | } |
3dc9be82 | 388 | #endif |
aa9e6044 | 389 | |
3dc9be82 NK |
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 */ | |
aa9e6044 | 406 | return CMD_RET_USAGE; |
3dc9be82 | 407 | } |
aa9e6044 | 408 | |
3dc9be82 NK |
409 | if (ret == CMD_RET_USAGE) |
410 | return ret; | |
aa9e6044 | 411 | |
3dc9be82 | 412 | while (ret--) |
aa9e6044 | 413 | NEXT_PARAM(argc, index); |
aa9e6044 | 414 | |
3dc9be82 NK |
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]); | |
aa9e6044 | 419 | NEXT_PARAM(argc, index); |
3dc9be82 | 420 | cnt = parse_numeric_param(argv[index]); |
aa9e6044 NK |
421 | } |
422 | ||
aa9e6044 | 423 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
3dc9be82 NK |
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); | |
aa9e6044 NK |
429 | } |
430 | #endif | |
431 | ||
3dc9be82 NK |
432 | return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver, |
433 | field_name, field_value, addr, off, cnt); | |
4f296d09 | 434 | } |
8bde7f77 | 435 | |
0d498393 | 436 | U_BOOT_CMD( |
aa9e6044 | 437 | eeprom, 8, 1, do_eeprom, |
2fb2604d | 438 | "EEPROM sub-system", |
d4fec4e9 MV |
439 | "read <bus> <devaddr> addr off cnt\n" |
440 | "eeprom write <bus> <devaddr> addr off cnt\n" | |
a89c33db | 441 | " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'" |
aa9e6044 NK |
442 | #ifdef CONFIG_CMD_EEPROM_LAYOUT |
443 | "\n" | |
3dc9be82 | 444 | "eeprom print [-l <layout_version>] <bus> <devaddr>\n" |
aa9e6044 | 445 | " - Print layout fields and their data in human readable format\n" |
3dc9be82 | 446 | "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n" |
aa9e6044 NK |
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 | |
0e350f81 | 456 | ) |