2 * Command for accessing SPI flash.
4 * Copyright (C) 2008 Atmel Corporation
5 * Licensed under the GPL-2 or later.
11 #include <spi_flash.h>
15 #ifndef CONFIG_SF_DEFAULT_SPEED
16 # define CONFIG_SF_DEFAULT_SPEED 1000000
18 #ifndef CONFIG_SF_DEFAULT_MODE
19 # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
21 #ifndef CONFIG_SF_DEFAULT_CS
22 # define CONFIG_SF_DEFAULT_CS 0
24 #ifndef CONFIG_SF_DEFAULT_BUS
25 # define CONFIG_SF_DEFAULT_BUS 0
28 static struct spi_flash *flash;
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).
40 * arg: length specification (i.e. both command arguments)
42 * len: computed length for operation
45 * -1: failure (bad format, bad address).
47 static int sf_parse_len_arg(char *arg, ulong *len)
50 char round_up_len; /* indicates if the "+length" form used */
59 len_arg = simple_strtoul(arg, &ep, 16);
60 if (ep == arg || *ep != '\0')
63 if (round_up_len && flash->sector_size > 0)
64 *len = ROUND(len_arg, flash->sector_size);
72 * This function takes a byte length and a delta unit of time to compute the
73 * approximate bytes per second
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
79 static ulong bytes_per_second(unsigned int len, ulong start_ms)
81 /* less accurate but avoids overflow */
82 if (len >= ((unsigned int) -1) / 1024)
83 return len / (max(get_timer(start_ms) / 1024, 1));
85 return 1024 * len / max(get_timer(start_ms), 1);
88 static int do_spi_flash_probe(int argc, char * const argv[])
90 unsigned int bus = CONFIG_SF_DEFAULT_BUS;
91 unsigned int cs = CONFIG_SF_DEFAULT_CS;
92 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
93 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
95 struct spi_flash *new;
98 cs = simple_strtoul(argv[1], &endp, 0);
99 if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
106 cs = simple_strtoul(endp + 1, &endp, 0);
113 speed = simple_strtoul(argv[2], &endp, 0);
114 if (*argv[2] == 0 || *endp != 0)
118 mode = simple_strtoul(argv[3], &endp, 16);
119 if (*argv[3] == 0 || *endp != 0)
123 new = spi_flash_probe(bus, cs, speed, mode);
125 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
130 spi_flash_free(flash);
137 * Write a block of data to SPI flash, first checking if it is different from
138 * what is already there.
140 * If the data being written is the same, then *skipped is incremented by len.
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
150 static 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)
153 debug("offset=%#x, sector_size=%#x, len=%#zx\n",
154 offset, flash->sector_size, len);
155 if (spi_flash_read(flash, offset, len, cmp_buf))
157 if (memcmp(cmp_buf, buf, len) == 0) {
158 debug("Skip region %x size %zx: no change\n",
163 if (spi_flash_erase(flash, offset, len))
165 if (spi_flash_write(flash, offset, len, buf))
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.
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
180 static int spi_flash_update(struct spi_flash *flash, u32 offset,
181 size_t len, const char *buf)
183 const char *err_oper = NULL;
185 const char *end = buf + len;
186 size_t todo; /* number of bytes to do in this pass */
187 size_t skipped = 0; /* statistics */
188 const ulong start_time = get_timer(0);
190 const char *start_buf = buf;
193 if (end - buf >= 200)
194 scale = (end - buf) / 100;
195 cmp_buf = malloc(flash->sector_size);
197 ulong last_update = get_timer(0);
199 for (; buf < end && !err_oper; buf += todo, offset += todo) {
200 todo = min(end - buf, flash->sector_size);
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,
206 last_update = get_timer(0);
208 err_oper = spi_flash_update_block(flash, offset, todo,
209 buf, cmp_buf, &skipped);
217 printf("SPI flash failed in %s step\n", err_oper);
221 delta = get_timer(start_time);
222 printf("%zu bytes written, %zu bytes skipped", len - skipped,
224 printf(" in %ld.%lds, speed %ld B/s\n",
225 delta / 1000, delta % 1000, bytes_per_second(len, start_time));
230 static int do_spi_flash_read_write(int argc, char * const argv[])
233 unsigned long offset;
242 addr = simple_strtoul(argv[1], &endp, 16);
243 if (*argv[1] == 0 || *endp != 0)
245 offset = simple_strtoul(argv[2], &endp, 16);
246 if (*argv[2] == 0 || *endp != 0)
248 len = simple_strtoul(argv[3], &endp, 16);
249 if (*argv[3] == 0 || *endp != 0)
252 /* Consistency checking */
253 if (offset + len > flash->size) {
254 printf("ERROR: attempting %s past flash size (%#x)\n",
255 argv[0], flash->size);
259 buf = map_physmem(addr, len, MAP_WRBACK);
261 puts("Failed to map physical memory\n");
265 if (strcmp(argv[0], "update") == 0)
266 ret = spi_flash_update(flash, offset, len, buf);
267 else if (strcmp(argv[0], "read") == 0)
268 ret = spi_flash_read(flash, offset, len, buf);
270 ret = spi_flash_write(flash, offset, len, buf);
272 unmap_physmem(buf, len);
275 printf("SPI flash %s failed\n", argv[0]);
282 static int do_spi_flash_erase(int argc, char * const argv[])
284 unsigned long offset;
292 offset = simple_strtoul(argv[1], &endp, 16);
293 if (*argv[1] == 0 || *endp != 0)
296 ret = sf_parse_len_arg(argv[2], &len);
300 /* Consistency checking */
301 if (offset + len > flash->size) {
302 printf("ERROR: attempting %s past flash size (%#x)\n",
303 argv[0], flash->size);
307 ret = spi_flash_erase(flash, offset, len);
308 printf("SF: %zu bytes @ %#x Erased: %s\n", (size_t)len, (u32)offset,
309 ret ? "ERROR" : "OK");
311 return ret == 0 ? 0 : 1;
314 #ifdef CONFIG_CMD_SF_TEST
324 static char *stage_name[STAGE_COUNT] = {
335 unsigned time_ms[STAGE_COUNT];
338 static void show_time(struct test_info *test, int stage)
340 uint64_t speed; /* KiB/s */
341 int bps; /* Bits per second */
343 speed = (long long)test->bytes * 1000;
344 do_div(speed, test->time_ms[stage] * 1024);
347 printf("%d %s: %d ticks, %d KiB/s %d.%03d Mbps\n", stage,
348 stage_name[stage], test->time_ms[stage],
349 (int)speed, bps / 1000, bps % 1000);
352 static void spi_test_next_stage(struct test_info *test)
354 test->time_ms[test->stage] = get_timer(test->base_ms);
355 show_time(test, test->stage);
356 test->base_ms = get_timer(0);
361 * Run a test on the SPI flash
363 * @param flash SPI flash to use
364 * @param buf Source buffer for data to write
365 * @param len Size of data to read/write
366 * @param offset Offset within flash to check
367 * @param vbuf Verification buffer
368 * @return 0 if ok, -1 on error
370 static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len,
371 ulong offset, uint8_t *vbuf)
373 struct test_info test;
376 printf("SPI flash test:\n");
377 memset(&test, '\0', sizeof(test));
378 test.base_ms = get_timer(0);
380 if (spi_flash_erase(flash, offset, len)) {
381 printf("Erase failed\n");
384 spi_test_next_stage(&test);
386 if (spi_flash_read(flash, offset, len, vbuf)) {
387 printf("Check read failed\n");
390 for (i = 0; i < len; i++) {
391 if (vbuf[i] != 0xff) {
392 printf("Check failed at %d\n", i);
393 print_buffer(i, vbuf + i, 1, min(len - i, 0x40), 0);
397 spi_test_next_stage(&test);
399 if (spi_flash_write(flash, offset, len, buf)) {
400 printf("Write failed\n");
403 memset(vbuf, '\0', len);
404 spi_test_next_stage(&test);
406 if (spi_flash_read(flash, offset, len, vbuf)) {
407 printf("Read failed\n");
410 spi_test_next_stage(&test);
412 for (i = 0; i < len; i++) {
413 if (buf[i] != vbuf[i]) {
414 printf("Verify failed at %d, good data:\n", i);
415 print_buffer(i, buf + i, 1, min(len - i, 0x40), 0);
416 printf("Bad data:\n");
417 print_buffer(i, vbuf + i, 1, min(len - i, 0x40), 0);
421 printf("Test passed\n");
422 for (i = 0; i < STAGE_COUNT; i++)
428 static int do_spi_flash_test(int argc, char * const argv[])
430 unsigned long offset;
432 uint8_t *buf = (uint8_t *)CONFIG_SYS_TEXT_BASE;
437 offset = simple_strtoul(argv[1], &endp, 16);
438 if (*argv[1] == 0 || *endp != 0)
440 len = simple_strtoul(argv[2], &endp, 16);
441 if (*argv[2] == 0 || *endp != 0)
446 printf("Cannot allocate memory\n");
452 printf("Cannot allocate memory\n");
456 memcpy(buf, (char *)CONFIG_SYS_TEXT_BASE, len);
457 ret = spi_flash_test(flash, buf, len, offset, vbuf);
461 printf("Test failed\n");
467 #endif /* CONFIG_CMD_SF_TEST */
469 static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
474 /* need at least two arguments */
482 if (strcmp(cmd, "probe") == 0) {
483 ret = do_spi_flash_probe(argc, argv);
487 /* The remaining commands require a selected device */
489 puts("No SPI flash selected. Please run `sf probe'\n");
493 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
494 strcmp(cmd, "update") == 0)
495 ret = do_spi_flash_read_write(argc, argv);
496 else if (strcmp(cmd, "erase") == 0)
497 ret = do_spi_flash_erase(argc, argv);
498 #ifdef CONFIG_CMD_SF_TEST
499 else if (!strcmp(cmd, "test"))
500 ret = do_spi_flash_test(argc, argv);
510 return CMD_RET_USAGE;
513 #ifdef CONFIG_CMD_SF_TEST
514 #define SF_TEST_HELP "\nsf test offset len " \
515 "- run a very basic destructive test"
521 sf, 5, 1, do_spi_flash,
522 "SPI flash sub-system",
523 "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
525 "sf read addr offset len - read `len' bytes starting at\n"
526 " `offset' to memory at `addr'\n"
527 "sf write addr offset len - write `len' bytes from memory\n"
528 " at `addr' to flash at `offset'\n"
529 "sf erase offset [+]len - erase `len' bytes from `offset'\n"
530 " `+len' round up `len' to block size\n"
531 "sf update addr offset len - erase and write `len' bytes from memory\n"
532 " at `addr' to flash at `offset'"