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