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