]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_sf.c
2 * Command for accessing SPI flash.
4 * Copyright (C) 2008 Atmel Corporation
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <spi_flash.h>
16 #ifndef CONFIG_SF_DEFAULT_SPEED
17 # define CONFIG_SF_DEFAULT_SPEED 1000000
19 #ifndef CONFIG_SF_DEFAULT_MODE
20 # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
22 #ifndef CONFIG_SF_DEFAULT_CS
23 # define CONFIG_SF_DEFAULT_CS 0
25 #ifndef CONFIG_SF_DEFAULT_BUS
26 # define CONFIG_SF_DEFAULT_BUS 0
29 static struct spi_flash
*flash
;
33 * This function computes the length argument for the erase command.
34 * The length on which the command is to operate can be given in two forms:
35 * 1. <cmd> offset len - operate on <'offset', 'len')
36 * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)')
37 * If the second form is used and the length doesn't fall on the
38 * sector boundary, than it will be adjusted to the next sector boundary.
39 * If it isn't in the flash, the function will fail (return -1).
41 * arg: length specification (i.e. both command arguments)
43 * len: computed length for operation
46 * -1: failure (bad format, bad address).
48 static int sf_parse_len_arg(char *arg
, ulong
*len
)
51 char round_up_len
; /* indicates if the "+length" form used */
60 len_arg
= simple_strtoul(arg
, &ep
, 16);
61 if (ep
== arg
|| *ep
!= '\0')
64 if (round_up_len
&& flash
->sector_size
> 0)
65 *len
= ROUND(len_arg
, flash
->sector_size
);
73 * This function takes a byte length and a delta unit of time to compute the
74 * approximate bytes per second
76 * @param len amount of bytes currently processed
77 * @param start_ms start time of processing in ms
78 * @return bytes per second if OK, 0 on error
80 static ulong
bytes_per_second(unsigned int len
, ulong start_ms
)
82 /* less accurate but avoids overflow */
83 if (len
>= ((unsigned int) -1) / 1024)
84 return len
/ (max(get_timer(start_ms
) / 1024, 1));
86 return 1024 * len
/ max(get_timer(start_ms
), 1);
89 static int do_spi_flash_probe(int argc
, char * const argv
[])
91 unsigned int bus
= CONFIG_SF_DEFAULT_BUS
;
92 unsigned int cs
= CONFIG_SF_DEFAULT_CS
;
93 unsigned int speed
= CONFIG_SF_DEFAULT_SPEED
;
94 unsigned int mode
= CONFIG_SF_DEFAULT_MODE
;
96 struct spi_flash
*new;
99 cs
= simple_strtoul(argv
[1], &endp
, 0);
100 if (*argv
[1] == 0 || (*endp
!= 0 && *endp
!= ':'))
107 cs
= simple_strtoul(endp
+ 1, &endp
, 0);
114 speed
= simple_strtoul(argv
[2], &endp
, 0);
115 if (*argv
[2] == 0 || *endp
!= 0)
119 mode
= simple_strtoul(argv
[3], &endp
, 16);
120 if (*argv
[3] == 0 || *endp
!= 0)
124 new = spi_flash_probe(bus
, cs
, speed
, mode
);
126 printf("Failed to initialize SPI flash at %u:%u\n", bus
, cs
);
131 spi_flash_free(flash
);
138 * Write a block of data to SPI flash, first checking if it is different from
139 * what is already there.
141 * If the data being written is the same, then *skipped is incremented by len.
143 * @param flash flash context pointer
144 * @param offset flash offset to write
145 * @param len number of bytes to write
146 * @param buf buffer to write from
147 * @param cmp_buf read buffer to use to compare data
148 * @param skipped Count of skipped data (incremented by this function)
149 * @return NULL if OK, else a string containing the stage which failed
151 static const char *spi_flash_update_block(struct spi_flash
*flash
, u32 offset
,
152 size_t len
, const char *buf
, char *cmp_buf
, size_t *skipped
)
154 debug("offset=%#x, sector_size=%#x, len=%#zx\n",
155 offset
, flash
->sector_size
, len
);
156 /* Read the entire sector so to allow for rewriting */
157 if (spi_flash_read(flash
, offset
, flash
->sector_size
, cmp_buf
))
159 /* Compare only what is meaningful (len) */
160 if (memcmp(cmp_buf
, buf
, len
) == 0) {
161 debug("Skip region %x size %zx: no change\n",
166 /* Erase the entire sector */
167 if (spi_flash_erase(flash
, offset
, flash
->sector_size
))
169 /* Write the initial part of the block from the source */
170 if (spi_flash_write(flash
, offset
, len
, buf
))
172 /* If it's a partial sector, rewrite the existing part */
173 if (len
!= flash
->sector_size
) {
174 /* Rewrite the original data to the end of the sector */
175 if (spi_flash_write(flash
, offset
+ len
,
176 flash
->sector_size
- len
, &cmp_buf
[len
]))
184 * Update an area of SPI flash by erasing and writing any blocks which need
185 * to change. Existing blocks with the correct data are left unchanged.
187 * @param flash flash context pointer
188 * @param offset flash offset to write
189 * @param len number of bytes to write
190 * @param buf buffer to write from
191 * @return 0 if ok, 1 on error
193 static int spi_flash_update(struct spi_flash
*flash
, u32 offset
,
194 size_t len
, const char *buf
)
196 const char *err_oper
= NULL
;
198 const char *end
= buf
+ len
;
199 size_t todo
; /* number of bytes to do in this pass */
200 size_t skipped
= 0; /* statistics */
201 const ulong start_time
= get_timer(0);
203 const char *start_buf
= buf
;
206 if (end
- buf
>= 200)
207 scale
= (end
- buf
) / 100;
208 cmp_buf
= malloc(flash
->sector_size
);
210 ulong last_update
= get_timer(0);
212 for (; buf
< end
&& !err_oper
; buf
+= todo
, offset
+= todo
) {
213 todo
= min(end
- buf
, flash
->sector_size
);
214 if (get_timer(last_update
) > 100) {
215 printf(" \rUpdating, %zu%% %lu B/s",
216 100 - (end
- buf
) / scale
,
217 bytes_per_second(buf
- start_buf
,
219 last_update
= get_timer(0);
221 err_oper
= spi_flash_update_block(flash
, offset
, todo
,
222 buf
, cmp_buf
, &skipped
);
230 printf("SPI flash failed in %s step\n", err_oper
);
234 delta
= get_timer(start_time
);
235 printf("%zu bytes written, %zu bytes skipped", len
- skipped
,
237 printf(" in %ld.%lds, speed %ld B/s\n",
238 delta
/ 1000, delta
% 1000, bytes_per_second(len
, start_time
));
243 static int do_spi_flash_read_write(int argc
, char * const argv
[])
246 unsigned long offset
;
255 addr
= simple_strtoul(argv
[1], &endp
, 16);
256 if (*argv
[1] == 0 || *endp
!= 0)
258 offset
= simple_strtoul(argv
[2], &endp
, 16);
259 if (*argv
[2] == 0 || *endp
!= 0)
261 len
= simple_strtoul(argv
[3], &endp
, 16);
262 if (*argv
[3] == 0 || *endp
!= 0)
265 /* Consistency checking */
266 if (offset
+ len
> flash
->size
) {
267 printf("ERROR: attempting %s past flash size (%#x)\n",
268 argv
[0], flash
->size
);
272 buf
= map_physmem(addr
, len
, MAP_WRBACK
);
274 puts("Failed to map physical memory\n");
278 if (strcmp(argv
[0], "update") == 0) {
279 ret
= spi_flash_update(flash
, offset
, len
, buf
);
280 } else if (strncmp(argv
[0], "read", 4) == 0 ||
281 strncmp(argv
[0], "write", 5) == 0) {
284 read
= strncmp(argv
[0], "read", 4) == 0;
286 ret
= spi_flash_read(flash
, offset
, len
, buf
);
288 ret
= spi_flash_write(flash
, offset
, len
, buf
);
290 printf("SF: %zu bytes @ %#x %s: %s\n", (size_t)len
, (u32
)offset
,
291 read
? "Read" : "Written", ret
? "ERROR" : "OK");
294 unmap_physmem(buf
, len
);
296 return ret
== 0 ? 0 : 1;
299 static int do_spi_flash_erase(int argc
, char * const argv
[])
301 unsigned long offset
;
309 offset
= simple_strtoul(argv
[1], &endp
, 16);
310 if (*argv
[1] == 0 || *endp
!= 0)
313 ret
= sf_parse_len_arg(argv
[2], &len
);
317 /* Consistency checking */
318 if (offset
+ len
> flash
->size
) {
319 printf("ERROR: attempting %s past flash size (%#x)\n",
320 argv
[0], flash
->size
);
324 ret
= spi_flash_erase(flash
, offset
, len
);
325 printf("SF: %zu bytes @ %#x Erased: %s\n", (size_t)len
, (u32
)offset
,
326 ret
? "ERROR" : "OK");
328 return ret
== 0 ? 0 : 1;
331 #ifdef CONFIG_CMD_SF_TEST
341 static char *stage_name
[STAGE_COUNT
] = {
352 unsigned time_ms
[STAGE_COUNT
];
355 static void show_time(struct test_info
*test
, int stage
)
357 uint64_t speed
; /* KiB/s */
358 int bps
; /* Bits per second */
360 speed
= (long long)test
->bytes
* 1000;
361 if (test
->time_ms
[stage
])
362 do_div(speed
, test
->time_ms
[stage
] * 1024);
365 printf("%d %s: %d ticks, %d KiB/s %d.%03d Mbps\n", stage
,
366 stage_name
[stage
], test
->time_ms
[stage
],
367 (int)speed
, bps
/ 1000, bps
% 1000);
370 static void spi_test_next_stage(struct test_info
*test
)
372 test
->time_ms
[test
->stage
] = get_timer(test
->base_ms
);
373 show_time(test
, test
->stage
);
374 test
->base_ms
= get_timer(0);
379 * Run a test on the SPI flash
381 * @param flash SPI flash to use
382 * @param buf Source buffer for data to write
383 * @param len Size of data to read/write
384 * @param offset Offset within flash to check
385 * @param vbuf Verification buffer
386 * @return 0 if ok, -1 on error
388 static int spi_flash_test(struct spi_flash
*flash
, uint8_t *buf
, ulong len
,
389 ulong offset
, uint8_t *vbuf
)
391 struct test_info test
;
394 printf("SPI flash test:\n");
395 memset(&test
, '\0', sizeof(test
));
396 test
.base_ms
= get_timer(0);
398 if (spi_flash_erase(flash
, offset
, len
)) {
399 printf("Erase failed\n");
402 spi_test_next_stage(&test
);
404 if (spi_flash_read(flash
, offset
, len
, vbuf
)) {
405 printf("Check read failed\n");
408 for (i
= 0; i
< len
; i
++) {
409 if (vbuf
[i
] != 0xff) {
410 printf("Check failed at %d\n", i
);
411 print_buffer(i
, vbuf
+ i
, 1, min(len
- i
, 0x40), 0);
415 spi_test_next_stage(&test
);
417 if (spi_flash_write(flash
, offset
, len
, buf
)) {
418 printf("Write failed\n");
421 memset(vbuf
, '\0', len
);
422 spi_test_next_stage(&test
);
424 if (spi_flash_read(flash
, offset
, len
, vbuf
)) {
425 printf("Read failed\n");
428 spi_test_next_stage(&test
);
430 for (i
= 0; i
< len
; i
++) {
431 if (buf
[i
] != vbuf
[i
]) {
432 printf("Verify failed at %d, good data:\n", i
);
433 print_buffer(i
, buf
+ i
, 1, min(len
- i
, 0x40), 0);
434 printf("Bad data:\n");
435 print_buffer(i
, vbuf
+ i
, 1, min(len
- i
, 0x40), 0);
439 printf("Test passed\n");
440 for (i
= 0; i
< STAGE_COUNT
; i
++)
446 static int do_spi_flash_test(int argc
, char * const argv
[])
448 unsigned long offset
;
457 offset
= simple_strtoul(argv
[1], &endp
, 16);
458 if (*argv
[1] == 0 || *endp
!= 0)
460 len
= simple_strtoul(argv
[2], &endp
, 16);
461 if (*argv
[2] == 0 || *endp
!= 0)
466 printf("Cannot allocate memory (%lu bytes)\n", len
);
472 printf("Cannot allocate memory (%lu bytes)\n", len
);
476 from
= map_sysmem(CONFIG_SYS_TEXT_BASE
, 0);
477 memcpy(buf
, from
, len
);
478 ret
= spi_flash_test(flash
, buf
, len
, offset
, vbuf
);
482 printf("Test failed\n");
488 #endif /* CONFIG_CMD_SF_TEST */
490 static int do_spi_flash(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
496 /* need at least two arguments */
504 if (strcmp(cmd
, "probe") == 0) {
505 ret
= do_spi_flash_probe(argc
, argv
);
509 /* The remaining commands require a selected device */
511 puts("No SPI flash selected. Please run `sf probe'\n");
515 if (strcmp(cmd
, "read") == 0 || strcmp(cmd
, "write") == 0 ||
516 strcmp(cmd
, "update") == 0)
517 ret
= do_spi_flash_read_write(argc
, argv
);
518 else if (strcmp(cmd
, "erase") == 0)
519 ret
= do_spi_flash_erase(argc
, argv
);
520 #ifdef CONFIG_CMD_SF_TEST
521 else if (!strcmp(cmd
, "test"))
522 ret
= do_spi_flash_test(argc
, argv
);
532 return CMD_RET_USAGE
;
535 #ifdef CONFIG_CMD_SF_TEST
536 #define SF_TEST_HELP "\nsf test offset len " \
537 "- run a very basic destructive test"
543 sf
, 5, 1, do_spi_flash
,
544 "SPI flash sub-system",
545 "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
547 "sf read addr offset len - read `len' bytes starting at\n"
548 " `offset' to memory at `addr'\n"
549 "sf write addr offset len - write `len' bytes from memory\n"
550 " at `addr' to flash at `offset'\n"
551 "sf erase offset [+]len - erase `len' bytes from `offset'\n"
552 " `+len' round up `len' to block size\n"
553 "sf update addr offset len - erase and write `len' bytes from memory\n"
554 " at `addr' to flash at `offset'"