]>
Commit | Line | Data |
---|---|---|
b6368467 HS |
1 | /* |
2 | * Command for accessing SPI flash. | |
3 | * | |
4 | * Copyright (C) 2008 Atmel Corporation | |
0c88a84a JT |
5 | * |
6 | * SPDX-License-Identifier: GPL-2.0+ | |
b6368467 | 7 | */ |
4166ee58 | 8 | |
b6368467 | 9 | #include <common.h> |
381c6e2c | 10 | #include <div64.h> |
8d1af942 | 11 | #include <malloc.h> |
ff0960f9 | 12 | #include <spi.h> |
b6368467 HS |
13 | #include <spi_flash.h> |
14 | ||
15 | #include <asm/io.h> | |
16 | ||
b6368467 HS |
17 | static struct spi_flash *flash; |
18 | ||
334eb4b0 RR |
19 | |
20 | /* | |
21 | * This function computes the length argument for the erase command. | |
22 | * The length on which the command is to operate can be given in two forms: | |
23 | * 1. <cmd> offset len - operate on <'offset', 'len') | |
24 | * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)') | |
25 | * If the second form is used and the length doesn't fall on the | |
26 | * sector boundary, than it will be adjusted to the next sector boundary. | |
27 | * If it isn't in the flash, the function will fail (return -1). | |
28 | * Input: | |
29 | * arg: length specification (i.e. both command arguments) | |
30 | * Output: | |
31 | * len: computed length for operation | |
32 | * Return: | |
33 | * 1: success | |
34 | * -1: failure (bad format, bad address). | |
35 | */ | |
36 | static int sf_parse_len_arg(char *arg, ulong *len) | |
37 | { | |
38 | char *ep; | |
39 | char round_up_len; /* indicates if the "+length" form used */ | |
40 | ulong len_arg; | |
41 | ||
42 | round_up_len = 0; | |
43 | if (*arg == '+') { | |
44 | round_up_len = 1; | |
45 | ++arg; | |
46 | } | |
47 | ||
48 | len_arg = simple_strtoul(arg, &ep, 16); | |
49 | if (ep == arg || *ep != '\0') | |
50 | return -1; | |
51 | ||
52 | if (round_up_len && flash->sector_size > 0) | |
155cfb5e | 53 | *len = ROUND(len_arg, flash->sector_size); |
334eb4b0 RR |
54 | else |
55 | *len = len_arg; | |
56 | ||
57 | return 1; | |
58 | } | |
59 | ||
a683c288 JM |
60 | /** |
61 | * This function takes a byte length and a delta unit of time to compute the | |
62 | * approximate bytes per second | |
63 | * | |
64 | * @param len amount of bytes currently processed | |
65 | * @param start_ms start time of processing in ms | |
66 | * @return bytes per second if OK, 0 on error | |
67 | */ | |
68 | static ulong bytes_per_second(unsigned int len, ulong start_ms) | |
69 | { | |
70 | /* less accurate but avoids overflow */ | |
71 | if (len >= ((unsigned int) -1) / 1024) | |
72 | return len / (max(get_timer(start_ms) / 1024, 1)); | |
73 | else | |
74 | return 1024 * len / max(get_timer(start_ms), 1); | |
75 | } | |
76 | ||
54841ab5 | 77 | static int do_spi_flash_probe(int argc, char * const argv[]) |
b6368467 | 78 | { |
c1173bd0 EN |
79 | unsigned int bus = CONFIG_SF_DEFAULT_BUS; |
80 | unsigned int cs = CONFIG_SF_DEFAULT_CS; | |
b6368467 HS |
81 | unsigned int speed = CONFIG_SF_DEFAULT_SPEED; |
82 | unsigned int mode = CONFIG_SF_DEFAULT_MODE; | |
83 | char *endp; | |
84 | struct spi_flash *new; | |
85 | ||
c1173bd0 EN |
86 | if (argc >= 2) { |
87 | cs = simple_strtoul(argv[1], &endp, 0); | |
88 | if (*argv[1] == 0 || (*endp != 0 && *endp != ':')) | |
8a07de03 | 89 | return -1; |
c1173bd0 EN |
90 | if (*endp == ':') { |
91 | if (endp[1] == 0) | |
92 | return -1; | |
93 | ||
94 | bus = cs; | |
95 | cs = simple_strtoul(endp + 1, &endp, 0); | |
96 | if (*endp != 0) | |
97 | return -1; | |
98 | } | |
b6368467 HS |
99 | } |
100 | ||
101 | if (argc >= 3) { | |
102 | speed = simple_strtoul(argv[2], &endp, 0); | |
103 | if (*argv[2] == 0 || *endp != 0) | |
8a07de03 | 104 | return -1; |
b6368467 HS |
105 | } |
106 | if (argc >= 4) { | |
6e8d58d3 | 107 | mode = simple_strtoul(argv[3], &endp, 16); |
b6368467 | 108 | if (*argv[3] == 0 || *endp != 0) |
8a07de03 | 109 | return -1; |
b6368467 HS |
110 | } |
111 | ||
112 | new = spi_flash_probe(bus, cs, speed, mode); | |
113 | if (!new) { | |
114 | printf("Failed to initialize SPI flash at %u:%u\n", bus, cs); | |
115 | return 1; | |
116 | } | |
117 | ||
118 | if (flash) | |
119 | spi_flash_free(flash); | |
120 | flash = new; | |
121 | ||
b6368467 | 122 | return 0; |
b6368467 HS |
123 | } |
124 | ||
8d1af942 SG |
125 | /** |
126 | * Write a block of data to SPI flash, first checking if it is different from | |
127 | * what is already there. | |
128 | * | |
129 | * If the data being written is the same, then *skipped is incremented by len. | |
130 | * | |
131 | * @param flash flash context pointer | |
132 | * @param offset flash offset to write | |
133 | * @param len number of bytes to write | |
134 | * @param buf buffer to write from | |
135 | * @param cmp_buf read buffer to use to compare data | |
136 | * @param skipped Count of skipped data (incremented by this function) | |
137 | * @return NULL if OK, else a string containing the stage which failed | |
138 | */ | |
139 | static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset, | |
140 | size_t len, const char *buf, char *cmp_buf, size_t *skipped) | |
141 | { | |
a97f6efd | 142 | debug("offset=%#x, sector_size=%#x, len=%#zx\n", |
402ba1e3 | 143 | offset, flash->sector_size, len); |
b95f958d GF |
144 | /* Read the entire sector so to allow for rewriting */ |
145 | if (spi_flash_read(flash, offset, flash->sector_size, cmp_buf)) | |
8d1af942 | 146 | return "read"; |
b95f958d | 147 | /* Compare only what is meaningful (len) */ |
8d1af942 | 148 | if (memcmp(cmp_buf, buf, len) == 0) { |
a97f6efd | 149 | debug("Skip region %x size %zx: no change\n", |
402ba1e3 | 150 | offset, len); |
8d1af942 SG |
151 | *skipped += len; |
152 | return NULL; | |
153 | } | |
34888506 GF |
154 | /* Erase the entire sector */ |
155 | if (spi_flash_erase(flash, offset, flash->sector_size)) | |
8d1af942 | 156 | return "erase"; |
b95f958d | 157 | /* Write the initial part of the block from the source */ |
8d1af942 SG |
158 | if (spi_flash_write(flash, offset, len, buf)) |
159 | return "write"; | |
b95f958d GF |
160 | /* If it's a partial sector, rewrite the existing part */ |
161 | if (len != flash->sector_size) { | |
162 | /* Rewrite the original data to the end of the sector */ | |
163 | if (spi_flash_write(flash, offset + len, | |
164 | flash->sector_size - len, &cmp_buf[len])) | |
165 | return "write"; | |
166 | } | |
167 | ||
8d1af942 SG |
168 | return NULL; |
169 | } | |
170 | ||
171 | /** | |
172 | * Update an area of SPI flash by erasing and writing any blocks which need | |
173 | * to change. Existing blocks with the correct data are left unchanged. | |
174 | * | |
175 | * @param flash flash context pointer | |
176 | * @param offset flash offset to write | |
177 | * @param len number of bytes to write | |
178 | * @param buf buffer to write from | |
179 | * @return 0 if ok, 1 on error | |
180 | */ | |
181 | static int spi_flash_update(struct spi_flash *flash, u32 offset, | |
182 | size_t len, const char *buf) | |
183 | { | |
184 | const char *err_oper = NULL; | |
185 | char *cmp_buf; | |
186 | const char *end = buf + len; | |
187 | size_t todo; /* number of bytes to do in this pass */ | |
8e8a4bc2 | 188 | size_t skipped = 0; /* statistics */ |
a683c288 JM |
189 | const ulong start_time = get_timer(0); |
190 | size_t scale = 1; | |
191 | const char *start_buf = buf; | |
192 | ulong delta; | |
8d1af942 | 193 | |
a683c288 JM |
194 | if (end - buf >= 200) |
195 | scale = (end - buf) / 100; | |
8d1af942 SG |
196 | cmp_buf = malloc(flash->sector_size); |
197 | if (cmp_buf) { | |
a683c288 JM |
198 | ulong last_update = get_timer(0); |
199 | ||
8e8a4bc2 | 200 | for (; buf < end && !err_oper; buf += todo, offset += todo) { |
8d1af942 | 201 | todo = min(end - buf, flash->sector_size); |
a683c288 JM |
202 | if (get_timer(last_update) > 100) { |
203 | printf(" \rUpdating, %zu%% %lu B/s", | |
402ba1e3 | 204 | 100 - (end - buf) / scale, |
a683c288 JM |
205 | bytes_per_second(buf - start_buf, |
206 | start_time)); | |
207 | last_update = get_timer(0); | |
208 | } | |
8d1af942 SG |
209 | err_oper = spi_flash_update_block(flash, offset, todo, |
210 | buf, cmp_buf, &skipped); | |
211 | } | |
212 | } else { | |
213 | err_oper = "malloc"; | |
214 | } | |
215 | free(cmp_buf); | |
a683c288 | 216 | putc('\r'); |
8d1af942 SG |
217 | if (err_oper) { |
218 | printf("SPI flash failed in %s step\n", err_oper); | |
219 | return 1; | |
220 | } | |
a683c288 JM |
221 | |
222 | delta = get_timer(start_time); | |
223 | printf("%zu bytes written, %zu bytes skipped", len - skipped, | |
402ba1e3 | 224 | skipped); |
a683c288 | 225 | printf(" in %ld.%lds, speed %ld B/s\n", |
402ba1e3 | 226 | delta / 1000, delta % 1000, bytes_per_second(len, start_time)); |
8e8a4bc2 | 227 | |
8d1af942 SG |
228 | return 0; |
229 | } | |
230 | ||
54841ab5 | 231 | static int do_spi_flash_read_write(int argc, char * const argv[]) |
b6368467 HS |
232 | { |
233 | unsigned long addr; | |
234 | unsigned long offset; | |
235 | unsigned long len; | |
236 | void *buf; | |
237 | char *endp; | |
60b6614a | 238 | int ret = 1; |
b6368467 HS |
239 | |
240 | if (argc < 4) | |
8a07de03 | 241 | return -1; |
b6368467 HS |
242 | |
243 | addr = simple_strtoul(argv[1], &endp, 16); | |
244 | if (*argv[1] == 0 || *endp != 0) | |
8a07de03 | 245 | return -1; |
b6368467 HS |
246 | offset = simple_strtoul(argv[2], &endp, 16); |
247 | if (*argv[2] == 0 || *endp != 0) | |
8a07de03 | 248 | return -1; |
b6368467 HS |
249 | len = simple_strtoul(argv[3], &endp, 16); |
250 | if (*argv[3] == 0 || *endp != 0) | |
8a07de03 | 251 | return -1; |
b6368467 | 252 | |
86493994 GF |
253 | /* Consistency checking */ |
254 | if (offset + len > flash->size) { | |
255 | printf("ERROR: attempting %s past flash size (%#x)\n", | |
402ba1e3 | 256 | argv[0], flash->size); |
86493994 GF |
257 | return 1; |
258 | } | |
259 | ||
b6368467 HS |
260 | buf = map_physmem(addr, len, MAP_WRBACK); |
261 | if (!buf) { | |
262 | puts("Failed to map physical memory\n"); | |
263 | return 1; | |
264 | } | |
265 | ||
402ba1e3 | 266 | if (strcmp(argv[0], "update") == 0) { |
8d1af942 | 267 | ret = spi_flash_update(flash, offset, len, buf); |
402ba1e3 | 268 | } else if (strncmp(argv[0], "read", 4) == 0 || |
60b6614a JT |
269 | strncmp(argv[0], "write", 5) == 0) { |
270 | int read; | |
271 | ||
272 | read = strncmp(argv[0], "read", 4) == 0; | |
273 | if (read) | |
274 | ret = spi_flash_read(flash, offset, len, buf); | |
275 | else | |
276 | ret = spi_flash_write(flash, offset, len, buf); | |
277 | ||
278 | printf("SF: %zu bytes @ %#x %s: %s\n", (size_t)len, (u32)offset, | |
402ba1e3 | 279 | read ? "Read" : "Written", ret ? "ERROR" : "OK"); |
60b6614a | 280 | } |
b6368467 HS |
281 | |
282 | unmap_physmem(buf, len); | |
283 | ||
60b6614a | 284 | return ret == 0 ? 0 : 1; |
b6368467 HS |
285 | } |
286 | ||
54841ab5 | 287 | static int do_spi_flash_erase(int argc, char * const argv[]) |
b6368467 HS |
288 | { |
289 | unsigned long offset; | |
290 | unsigned long len; | |
291 | char *endp; | |
292 | int ret; | |
293 | ||
294 | if (argc < 3) | |
8a07de03 | 295 | return -1; |
b6368467 HS |
296 | |
297 | offset = simple_strtoul(argv[1], &endp, 16); | |
298 | if (*argv[1] == 0 || *endp != 0) | |
8a07de03 | 299 | return -1; |
334eb4b0 RR |
300 | |
301 | ret = sf_parse_len_arg(argv[2], &len); | |
302 | if (ret != 1) | |
8a07de03 | 303 | return -1; |
b6368467 | 304 | |
86493994 GF |
305 | /* Consistency checking */ |
306 | if (offset + len > flash->size) { | |
307 | printf("ERROR: attempting %s past flash size (%#x)\n", | |
402ba1e3 | 308 | argv[0], flash->size); |
86493994 GF |
309 | return 1; |
310 | } | |
311 | ||
b6368467 | 312 | ret = spi_flash_erase(flash, offset, len); |
96bbf556 | 313 | printf("SF: %zu bytes @ %#x Erased: %s\n", (size_t)len, (u32)offset, |
402ba1e3 | 314 | ret ? "ERROR" : "OK"); |
b6368467 | 315 | |
96bbf556 | 316 | return ret == 0 ? 0 : 1; |
b6368467 HS |
317 | } |
318 | ||
24007273 SG |
319 | #ifdef CONFIG_CMD_SF_TEST |
320 | enum { | |
321 | STAGE_ERASE, | |
322 | STAGE_CHECK, | |
323 | STAGE_WRITE, | |
324 | STAGE_READ, | |
325 | ||
326 | STAGE_COUNT, | |
327 | }; | |
328 | ||
329 | static char *stage_name[STAGE_COUNT] = { | |
330 | "erase", | |
331 | "check", | |
332 | "write", | |
333 | "read", | |
334 | }; | |
335 | ||
336 | struct test_info { | |
337 | int stage; | |
338 | int bytes; | |
339 | unsigned base_ms; | |
340 | unsigned time_ms[STAGE_COUNT]; | |
341 | }; | |
342 | ||
343 | static void show_time(struct test_info *test, int stage) | |
344 | { | |
345 | uint64_t speed; /* KiB/s */ | |
346 | int bps; /* Bits per second */ | |
347 | ||
348 | speed = (long long)test->bytes * 1000; | |
d1f22d4b SG |
349 | if (test->time_ms[stage]) |
350 | do_div(speed, test->time_ms[stage] * 1024); | |
24007273 SG |
351 | bps = speed * 8; |
352 | ||
353 | printf("%d %s: %d ticks, %d KiB/s %d.%03d Mbps\n", stage, | |
354 | stage_name[stage], test->time_ms[stage], | |
355 | (int)speed, bps / 1000, bps % 1000); | |
356 | } | |
357 | ||
358 | static void spi_test_next_stage(struct test_info *test) | |
359 | { | |
360 | test->time_ms[test->stage] = get_timer(test->base_ms); | |
361 | show_time(test, test->stage); | |
362 | test->base_ms = get_timer(0); | |
363 | test->stage++; | |
364 | } | |
365 | ||
366 | /** | |
367 | * Run a test on the SPI flash | |
368 | * | |
369 | * @param flash SPI flash to use | |
370 | * @param buf Source buffer for data to write | |
371 | * @param len Size of data to read/write | |
372 | * @param offset Offset within flash to check | |
373 | * @param vbuf Verification buffer | |
374 | * @return 0 if ok, -1 on error | |
375 | */ | |
1e7133e9 SG |
376 | static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len, |
377 | ulong offset, uint8_t *vbuf) | |
24007273 SG |
378 | { |
379 | struct test_info test; | |
380 | int i; | |
381 | ||
382 | printf("SPI flash test:\n"); | |
383 | memset(&test, '\0', sizeof(test)); | |
384 | test.base_ms = get_timer(0); | |
385 | test.bytes = len; | |
386 | if (spi_flash_erase(flash, offset, len)) { | |
387 | printf("Erase failed\n"); | |
388 | return -1; | |
389 | } | |
390 | spi_test_next_stage(&test); | |
391 | ||
392 | if (spi_flash_read(flash, offset, len, vbuf)) { | |
393 | printf("Check read failed\n"); | |
394 | return -1; | |
395 | } | |
396 | for (i = 0; i < len; i++) { | |
397 | if (vbuf[i] != 0xff) { | |
398 | printf("Check failed at %d\n", i); | |
399 | print_buffer(i, vbuf + i, 1, min(len - i, 0x40), 0); | |
400 | return -1; | |
401 | } | |
402 | } | |
403 | spi_test_next_stage(&test); | |
404 | ||
405 | if (spi_flash_write(flash, offset, len, buf)) { | |
406 | printf("Write failed\n"); | |
407 | return -1; | |
408 | } | |
409 | memset(vbuf, '\0', len); | |
410 | spi_test_next_stage(&test); | |
411 | ||
412 | if (spi_flash_read(flash, offset, len, vbuf)) { | |
413 | printf("Read failed\n"); | |
414 | return -1; | |
415 | } | |
416 | spi_test_next_stage(&test); | |
417 | ||
418 | for (i = 0; i < len; i++) { | |
419 | if (buf[i] != vbuf[i]) { | |
420 | printf("Verify failed at %d, good data:\n", i); | |
421 | print_buffer(i, buf + i, 1, min(len - i, 0x40), 0); | |
422 | printf("Bad data:\n"); | |
423 | print_buffer(i, vbuf + i, 1, min(len - i, 0x40), 0); | |
424 | return -1; | |
425 | } | |
426 | } | |
427 | printf("Test passed\n"); | |
428 | for (i = 0; i < STAGE_COUNT; i++) | |
429 | show_time(&test, i); | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | static int do_spi_flash_test(int argc, char * const argv[]) | |
435 | { | |
436 | unsigned long offset; | |
437 | unsigned long len; | |
d1f22d4b | 438 | uint8_t *buf, *from; |
24007273 | 439 | char *endp; |
1e7133e9 | 440 | uint8_t *vbuf; |
24007273 SG |
441 | int ret; |
442 | ||
d1f22d4b SG |
443 | if (argc < 3) |
444 | return -1; | |
24007273 SG |
445 | offset = simple_strtoul(argv[1], &endp, 16); |
446 | if (*argv[1] == 0 || *endp != 0) | |
447 | return -1; | |
448 | len = simple_strtoul(argv[2], &endp, 16); | |
449 | if (*argv[2] == 0 || *endp != 0) | |
450 | return -1; | |
451 | ||
452 | vbuf = malloc(len); | |
453 | if (!vbuf) { | |
d1f22d4b | 454 | printf("Cannot allocate memory (%lu bytes)\n", len); |
24007273 SG |
455 | return 1; |
456 | } | |
457 | buf = malloc(len); | |
458 | if (!buf) { | |
459 | free(vbuf); | |
d1f22d4b | 460 | printf("Cannot allocate memory (%lu bytes)\n", len); |
24007273 SG |
461 | return 1; |
462 | } | |
463 | ||
d1f22d4b SG |
464 | from = map_sysmem(CONFIG_SYS_TEXT_BASE, 0); |
465 | memcpy(buf, from, len); | |
24007273 SG |
466 | ret = spi_flash_test(flash, buf, len, offset, vbuf); |
467 | free(vbuf); | |
468 | free(buf); | |
469 | if (ret) { | |
470 | printf("Test failed\n"); | |
471 | return 1; | |
472 | } | |
473 | ||
474 | return 0; | |
475 | } | |
476 | #endif /* CONFIG_CMD_SF_TEST */ | |
477 | ||
402ba1e3 JT |
478 | static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, |
479 | char * const argv[]) | |
b6368467 HS |
480 | { |
481 | const char *cmd; | |
8a07de03 | 482 | int ret; |
b6368467 HS |
483 | |
484 | /* need at least two arguments */ | |
485 | if (argc < 2) | |
486 | goto usage; | |
487 | ||
488 | cmd = argv[1]; | |
8a07de03 MF |
489 | --argc; |
490 | ++argv; | |
b6368467 | 491 | |
8a07de03 MF |
492 | if (strcmp(cmd, "probe") == 0) { |
493 | ret = do_spi_flash_probe(argc, argv); | |
494 | goto done; | |
495 | } | |
b6368467 HS |
496 | |
497 | /* The remaining commands require a selected device */ | |
498 | if (!flash) { | |
499 | puts("No SPI flash selected. Please run `sf probe'\n"); | |
500 | return 1; | |
501 | } | |
502 | ||
8d1af942 SG |
503 | if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 || |
504 | strcmp(cmd, "update") == 0) | |
8a07de03 MF |
505 | ret = do_spi_flash_read_write(argc, argv); |
506 | else if (strcmp(cmd, "erase") == 0) | |
507 | ret = do_spi_flash_erase(argc, argv); | |
24007273 SG |
508 | #ifdef CONFIG_CMD_SF_TEST |
509 | else if (!strcmp(cmd, "test")) | |
510 | ret = do_spi_flash_test(argc, argv); | |
511 | #endif | |
8a07de03 MF |
512 | else |
513 | ret = -1; | |
514 | ||
515 | done: | |
516 | if (ret != -1) | |
517 | return ret; | |
b6368467 HS |
518 | |
519 | usage: | |
4c12eeb8 | 520 | return CMD_RET_USAGE; |
b6368467 HS |
521 | } |
522 | ||
24007273 SG |
523 | #ifdef CONFIG_CMD_SF_TEST |
524 | #define SF_TEST_HELP "\nsf test offset len " \ | |
525 | "- run a very basic destructive test" | |
526 | #else | |
527 | #define SF_TEST_HELP | |
528 | #endif | |
529 | ||
b6368467 HS |
530 | U_BOOT_CMD( |
531 | sf, 5, 1, do_spi_flash, | |
2fb2604d | 532 | "SPI flash sub-system", |
c1173bd0 | 533 | "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n" |
b6368467 | 534 | " and chip select\n" |
402ba1e3 | 535 | "sf read addr offset len - read `len' bytes starting at\n" |
b6368467 HS |
536 | " `offset' to memory at `addr'\n" |
537 | "sf write addr offset len - write `len' bytes from memory\n" | |
538 | " at `addr' to flash at `offset'\n" | |
334eb4b0 | 539 | "sf erase offset [+]len - erase `len' bytes from `offset'\n" |
8d1af942 SG |
540 | " `+len' round up `len' to block size\n" |
541 | "sf update addr offset len - erase and write `len' bytes from memory\n" | |
542 | " at `addr' to flash at `offset'" | |
24007273 | 543 | SF_TEST_HELP |
a89c33db | 544 | ); |