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 <malloc.h>
  11. #include <mapmem.h>
  12. #include <spi.h>
  13. #include <spi_flash.h>
  14. #include <jffs2/jffs2.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <asm/io.h>
  17. #include <dm/device-internal.h>
  18. static struct spi_flash *flash;
  19. /*
  20. * This function computes the length argument for the erase command.
  21. * The length on which the command is to operate can be given in two forms:
  22. * 1. <cmd> offset len - operate on <'offset', 'len')
  23. * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)')
  24. * If the second form is used and the length doesn't fall on the
  25. * sector boundary, than it will be adjusted to the next sector boundary.
  26. * If it isn't in the flash, the function will fail (return -1).
  27. * Input:
  28. * arg: length specification (i.e. both command arguments)
  29. * Output:
  30. * len: computed length for operation
  31. * Return:
  32. * 1: success
  33. * -1: failure (bad format, bad address).
  34. */
  35. static int sf_parse_len_arg(char *arg, ulong *len)
  36. {
  37. char *ep;
  38. char round_up_len; /* indicates if the "+length" form used */
  39. ulong len_arg;
  40. round_up_len = 0;
  41. if (*arg == '+') {
  42. round_up_len = 1;
  43. ++arg;
  44. }
  45. len_arg = simple_strtoul(arg, &ep, 16);
  46. if (ep == arg || *ep != '\0')
  47. return -1;
  48. if (round_up_len && flash->sector_size > 0)
  49. *len = ROUND(len_arg, flash->sector_size);
  50. else
  51. *len = len_arg;
  52. return 1;
  53. }
  54. /**
  55. * This function takes a byte length and a delta unit of time to compute the
  56. * approximate bytes per second
  57. *
  58. * @param len amount of bytes currently processed
  59. * @param start_ms start time of processing in ms
  60. * @return bytes per second if OK, 0 on error
  61. */
  62. static ulong bytes_per_second(unsigned int len, ulong start_ms)
  63. {
  64. /* less accurate but avoids overflow */
  65. if (len >= ((unsigned int) -1) / 1024)
  66. return len / (max(get_timer(start_ms) / 1024, 1UL));
  67. else
  68. return 1024 * len / max(get_timer(start_ms), 1UL);
  69. }
  70. static int do_spi_flash_probe(int argc, char * const argv[])
  71. {
  72. unsigned int bus = CONFIG_SF_DEFAULT_BUS;
  73. unsigned int cs = CONFIG_SF_DEFAULT_CS;
  74. unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
  75. unsigned int mode = CONFIG_SF_DEFAULT_MODE;
  76. char *endp;
  77. #ifdef CONFIG_DM_SPI_FLASH
  78. struct udevice *new, *bus_dev;
  79. int ret;
  80. /* In DM mode defaults will be taken from DT */
  81. speed = 0, mode = 0;
  82. #else
  83. struct spi_flash *new;
  84. #endif
  85. if (argc >= 2) {
  86. cs = simple_strtoul(argv[1], &endp, 0);
  87. if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
  88. return -1;
  89. if (*endp == ':') {
  90. if (endp[1] == 0)
  91. return -1;
  92. bus = cs;
  93. cs = simple_strtoul(endp + 1, &endp, 0);
  94. if (*endp != 0)
  95. return -1;
  96. }
  97. }
  98. if (argc >= 3) {
  99. speed = simple_strtoul(argv[2], &endp, 0);
  100. if (*argv[2] == 0 || *endp != 0)
  101. return -1;
  102. }
  103. if (argc >= 4) {
  104. mode = simple_strtoul(argv[3], &endp, 16);
  105. if (*argv[3] == 0 || *endp != 0)
  106. return -1;
  107. }
  108. #ifdef CONFIG_DM_SPI_FLASH
  109. /* Remove the old device, otherwise probe will just be a nop */
  110. ret = spi_find_bus_and_cs(bus, cs, &bus_dev, &new);
  111. if (!ret) {
  112. device_remove(new, DM_REMOVE_NORMAL);
  113. }
  114. flash = NULL;
  115. ret = spi_flash_probe_bus_cs(bus, cs, speed, mode, &new);
  116. if (ret) {
  117. printf("Failed to initialize SPI flash at %u:%u (error %d)\n",
  118. bus, cs, ret);
  119. return 1;
  120. }
  121. flash = dev_get_uclass_priv(new);
  122. #else
  123. if (flash)
  124. spi_flash_free(flash);
  125. new = spi_flash_probe(bus, cs, speed, mode);
  126. flash = new;
  127. if (!new) {
  128. printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
  129. return 1;
  130. }
  131. flash = new;
  132. #endif
  133. return 0;
  134. }
  135. /**
  136. * Write a block of data to SPI flash, first checking if it is different from
  137. * what is already there.
  138. *
  139. * If the data being written is the same, then *skipped is incremented by len.
  140. *
  141. * @param flash flash context pointer
  142. * @param offset flash offset to write
  143. * @param len number of bytes to write
  144. * @param buf buffer to write from
  145. * @param cmp_buf read buffer to use to compare data
  146. * @param skipped Count of skipped data (incremented by this function)
  147. * @return NULL if OK, else a string containing the stage which failed
  148. */
  149. static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
  150. size_t len, const char *buf, char *cmp_buf, size_t *skipped)
  151. {
  152. char *ptr = (char *)buf;
  153. debug("offset=%#x, sector_size=%#x, len=%#zx\n",
  154. offset, flash->sector_size, len);
  155. /* Read the entire sector so to allow for rewriting */
  156. if (spi_flash_read(flash, offset, flash->sector_size, cmp_buf))
  157. return "read";
  158. /* Compare only what is meaningful (len) */
  159. if (memcmp(cmp_buf, buf, len) == 0) {
  160. debug("Skip region %x size %zx: no change\n",
  161. offset, len);
  162. *skipped += len;
  163. return NULL;
  164. }
  165. /* Erase the entire sector */
  166. if (spi_flash_erase(flash, offset, flash->sector_size))
  167. return "erase";
  168. /* If it's a partial sector, copy the data into the temp-buffer */
  169. if (len != flash->sector_size) {
  170. memcpy(cmp_buf, buf, len);
  171. ptr = cmp_buf;
  172. }
  173. /* Write one complete sector */
  174. if (spi_flash_write(flash, offset, flash->sector_size, ptr))
  175. return "write";
  176. return NULL;
  177. }
  178. /**
  179. * Update an area of SPI flash by erasing and writing any blocks which need
  180. * to change. Existing blocks with the correct data are left unchanged.
  181. *
  182. * @param flash flash context pointer
  183. * @param offset flash offset to write
  184. * @param len number of bytes to write
  185. * @param buf buffer to write from
  186. * @return 0 if ok, 1 on error
  187. */
  188. static int spi_flash_update(struct spi_flash *flash, u32 offset,
  189. size_t len, const char *buf)
  190. {
  191. const char *err_oper = NULL;
  192. char *cmp_buf;
  193. const char *end = buf + len;
  194. size_t todo; /* number of bytes to do in this pass */
  195. size_t skipped = 0; /* statistics */
  196. const ulong start_time = get_timer(0);
  197. size_t scale = 1;
  198. const char *start_buf = buf;
  199. ulong delta;
  200. if (end - buf >= 200)
  201. scale = (end - buf) / 100;
  202. cmp_buf = memalign(ARCH_DMA_MINALIGN, flash->sector_size);
  203. if (cmp_buf) {
  204. ulong last_update = get_timer(0);
  205. for (; buf < end && !err_oper; buf += todo, offset += todo) {
  206. todo = min_t(size_t, end - buf, flash->sector_size);
  207. if (get_timer(last_update) > 100) {
  208. printf(" \rUpdating, %zu%% %lu B/s",
  209. 100 - (end - buf) / scale,
  210. bytes_per_second(buf - start_buf,
  211. start_time));
  212. last_update = get_timer(0);
  213. }
  214. err_oper = spi_flash_update_block(flash, offset, todo,
  215. buf, cmp_buf, &skipped);
  216. }
  217. } else {
  218. err_oper = "malloc";
  219. }
  220. free(cmp_buf);
  221. putc('\r');
  222. if (err_oper) {
  223. printf("SPI flash failed in %s step\n", err_oper);
  224. return 1;
  225. }
  226. delta = get_timer(start_time);
  227. printf("%zu bytes written, %zu bytes skipped", len - skipped,
  228. skipped);
  229. printf(" in %ld.%lds, speed %ld B/s\n",
  230. delta / 1000, delta % 1000, bytes_per_second(len, start_time));
  231. return 0;
  232. }
  233. static int do_spi_flash_read_write(int argc, char * const argv[])
  234. {
  235. unsigned long addr;
  236. void *buf;
  237. char *endp;
  238. int ret = 1;
  239. int dev = 0;
  240. loff_t offset, len, maxsize;
  241. if (argc < 3)
  242. return -1;
  243. addr = simple_strtoul(argv[1], &endp, 16);
  244. if (*argv[1] == 0 || *endp != 0)
  245. return -1;
  246. if (mtd_arg_off_size(argc - 2, &argv[2], &dev, &offset, &len,
  247. &maxsize, MTD_DEV_TYPE_NOR, flash->size))
  248. return -1;
  249. /* Consistency checking */
  250. if (offset + len > flash->size) {
  251. printf("ERROR: attempting %s past flash size (%#x)\n",
  252. argv[0], flash->size);
  253. return 1;
  254. }
  255. buf = map_physmem(addr, len, MAP_WRBACK);
  256. if (!buf && addr) {
  257. puts("Failed to map physical memory\n");
  258. return 1;
  259. }
  260. if (strcmp(argv[0], "update") == 0) {
  261. ret = spi_flash_update(flash, offset, len, buf);
  262. } else if (strncmp(argv[0], "read", 4) == 0 ||
  263. strncmp(argv[0], "write", 5) == 0) {
  264. int read;
  265. read = strncmp(argv[0], "read", 4) == 0;
  266. if (read)
  267. ret = spi_flash_read(flash, offset, len, buf);
  268. else
  269. ret = spi_flash_write(flash, offset, len, buf);
  270. printf("SF: %zu bytes @ %#x %s: ", (size_t)len, (u32)offset,
  271. read ? "Read" : "Written");
  272. if (ret)
  273. printf("ERROR %d\n", ret);
  274. else
  275. printf("OK\n");
  276. }
  277. unmap_physmem(buf, len);
  278. return ret == 0 ? 0 : 1;
  279. }
  280. static int do_spi_flash_erase(int argc, char * const argv[])
  281. {
  282. int ret;
  283. int dev = 0;
  284. loff_t offset, len, maxsize;
  285. ulong size;
  286. if (argc < 3)
  287. return -1;
  288. if (mtd_arg_off(argv[1], &dev, &offset, &len, &maxsize,
  289. MTD_DEV_TYPE_NOR, flash->size))
  290. return -1;
  291. ret = sf_parse_len_arg(argv[2], &size);
  292. if (ret != 1)
  293. return -1;
  294. if (size == 0)
  295. size = len;
  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: %d 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. );