sf.c 15 KB

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