sf.c 15 KB

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