sf.c 15 KB

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