cmd_sf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * Command for accessing SPI flash.
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <div64.h>
  10. #include <dm.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. static struct spi_flash *flash;
  20. /*
  21. * This function computes the length argument for the erase command.
  22. * The length on which the command is to operate can be given in two forms:
  23. * 1. <cmd> offset len - operate on <'offset', 'len')
  24. * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)')
  25. * If the second form is used and the length doesn't fall on the
  26. * sector boundary, than it will be adjusted to the next sector boundary.
  27. * If it isn't in the flash, the function will fail (return -1).
  28. * Input:
  29. * arg: length specification (i.e. both command arguments)
  30. * Output:
  31. * len: computed length for operation
  32. * Return:
  33. * 1: success
  34. * -1: failure (bad format, bad address).
  35. */
  36. static int sf_parse_len_arg(char *arg, ulong *len)
  37. {
  38. char *ep;
  39. char round_up_len; /* indicates if the "+length" form used */
  40. ulong len_arg;
  41. round_up_len = 0;
  42. if (*arg == '+') {
  43. round_up_len = 1;
  44. ++arg;
  45. }
  46. len_arg = simple_strtoul(arg, &ep, 16);
  47. if (ep == arg || *ep != '\0')
  48. return -1;
  49. if (round_up_len && flash->sector_size > 0)
  50. *len = ROUND(len_arg, flash->sector_size);
  51. else
  52. *len = len_arg;
  53. return 1;
  54. }
  55. /**
  56. * This function takes a byte length and a delta unit of time to compute the
  57. * approximate bytes per second
  58. *
  59. * @param len amount of bytes currently processed
  60. * @param start_ms start time of processing in ms
  61. * @return bytes per second if OK, 0 on error
  62. */
  63. static ulong bytes_per_second(unsigned int len, ulong start_ms)
  64. {
  65. /* less accurate but avoids overflow */
  66. if (len >= ((unsigned int) -1) / 1024)
  67. return len / (max(get_timer(start_ms) / 1024, 1UL));
  68. else
  69. return 1024 * len / max(get_timer(start_ms), 1UL);
  70. }
  71. static int do_spi_flash_probe(int argc, char * const argv[])
  72. {
  73. unsigned int bus = CONFIG_SF_DEFAULT_BUS;
  74. unsigned int cs = CONFIG_SF_DEFAULT_CS;
  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);
  112. device_unbind(new);
  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) {
  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. /* Consistency checking */
  295. if (offset + size > flash->size) {
  296. printf("ERROR: attempting %s past flash size (%#x)\n",
  297. argv[0], flash->size);
  298. return 1;
  299. }
  300. ret = spi_flash_erase(flash, offset, size);
  301. printf("SF: %zu bytes @ %#x Erased: %s\n", (size_t)size, (u32)offset,
  302. ret ? "ERROR" : "OK");
  303. return ret == 0 ? 0 : 1;
  304. }
  305. #ifdef CONFIG_CMD_SF_TEST
  306. enum {
  307. STAGE_ERASE,
  308. STAGE_CHECK,
  309. STAGE_WRITE,
  310. STAGE_READ,
  311. STAGE_COUNT,
  312. };
  313. static char *stage_name[STAGE_COUNT] = {
  314. "erase",
  315. "check",
  316. "write",
  317. "read",
  318. };
  319. struct test_info {
  320. int stage;
  321. int bytes;
  322. unsigned base_ms;
  323. unsigned time_ms[STAGE_COUNT];
  324. };
  325. static void show_time(struct test_info *test, int stage)
  326. {
  327. uint64_t speed; /* KiB/s */
  328. int bps; /* Bits per second */
  329. speed = (long long)test->bytes * 1000;
  330. if (test->time_ms[stage])
  331. do_div(speed, test->time_ms[stage] * 1024);
  332. bps = speed * 8;
  333. printf("%d %s: %d ticks, %d KiB/s %d.%03d Mbps\n", stage,
  334. stage_name[stage], test->time_ms[stage],
  335. (int)speed, bps / 1000, bps % 1000);
  336. }
  337. static void spi_test_next_stage(struct test_info *test)
  338. {
  339. test->time_ms[test->stage] = get_timer(test->base_ms);
  340. show_time(test, test->stage);
  341. test->base_ms = get_timer(0);
  342. test->stage++;
  343. }
  344. /**
  345. * Run a test on the SPI flash
  346. *
  347. * @param flash SPI flash to use
  348. * @param buf Source buffer for data to write
  349. * @param len Size of data to read/write
  350. * @param offset Offset within flash to check
  351. * @param vbuf Verification buffer
  352. * @return 0 if ok, -1 on error
  353. */
  354. static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len,
  355. ulong offset, uint8_t *vbuf)
  356. {
  357. struct test_info test;
  358. int i;
  359. printf("SPI flash test:\n");
  360. memset(&test, '\0', sizeof(test));
  361. test.base_ms = get_timer(0);
  362. test.bytes = len;
  363. if (spi_flash_erase(flash, offset, len)) {
  364. printf("Erase failed\n");
  365. return -1;
  366. }
  367. spi_test_next_stage(&test);
  368. if (spi_flash_read(flash, offset, len, vbuf)) {
  369. printf("Check read failed\n");
  370. return -1;
  371. }
  372. for (i = 0; i < len; i++) {
  373. if (vbuf[i] != 0xff) {
  374. printf("Check failed at %d\n", i);
  375. print_buffer(i, vbuf + i, 1,
  376. min_t(uint, len - i, 0x40), 0);
  377. return -1;
  378. }
  379. }
  380. spi_test_next_stage(&test);
  381. if (spi_flash_write(flash, offset, len, buf)) {
  382. printf("Write failed\n");
  383. return -1;
  384. }
  385. memset(vbuf, '\0', len);
  386. spi_test_next_stage(&test);
  387. if (spi_flash_read(flash, offset, len, vbuf)) {
  388. printf("Read failed\n");
  389. return -1;
  390. }
  391. spi_test_next_stage(&test);
  392. for (i = 0; i < len; i++) {
  393. if (buf[i] != vbuf[i]) {
  394. printf("Verify failed at %d, good data:\n", i);
  395. print_buffer(i, buf + i, 1,
  396. min_t(uint, len - i, 0x40), 0);
  397. printf("Bad data:\n");
  398. print_buffer(i, vbuf + i, 1,
  399. min_t(uint, len - i, 0x40), 0);
  400. return -1;
  401. }
  402. }
  403. printf("Test passed\n");
  404. for (i = 0; i < STAGE_COUNT; i++)
  405. show_time(&test, i);
  406. return 0;
  407. }
  408. static int do_spi_flash_test(int argc, char * const argv[])
  409. {
  410. unsigned long offset;
  411. unsigned long len;
  412. uint8_t *buf, *from;
  413. char *endp;
  414. uint8_t *vbuf;
  415. int ret;
  416. if (argc < 3)
  417. return -1;
  418. offset = simple_strtoul(argv[1], &endp, 16);
  419. if (*argv[1] == 0 || *endp != 0)
  420. return -1;
  421. len = simple_strtoul(argv[2], &endp, 16);
  422. if (*argv[2] == 0 || *endp != 0)
  423. return -1;
  424. vbuf = memalign(ARCH_DMA_MINALIGN, len);
  425. if (!vbuf) {
  426. printf("Cannot allocate memory (%lu bytes)\n", len);
  427. return 1;
  428. }
  429. buf = memalign(ARCH_DMA_MINALIGN, len);
  430. if (!buf) {
  431. free(vbuf);
  432. printf("Cannot allocate memory (%lu bytes)\n", len);
  433. return 1;
  434. }
  435. from = map_sysmem(CONFIG_SYS_TEXT_BASE, 0);
  436. memcpy(buf, from, len);
  437. ret = spi_flash_test(flash, buf, len, offset, vbuf);
  438. free(vbuf);
  439. free(buf);
  440. if (ret) {
  441. printf("Test failed\n");
  442. return 1;
  443. }
  444. return 0;
  445. }
  446. #endif /* CONFIG_CMD_SF_TEST */
  447. static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc,
  448. char * const argv[])
  449. {
  450. const char *cmd;
  451. int ret;
  452. /* need at least two arguments */
  453. if (argc < 2)
  454. goto usage;
  455. cmd = argv[1];
  456. --argc;
  457. ++argv;
  458. if (strcmp(cmd, "probe") == 0) {
  459. ret = do_spi_flash_probe(argc, argv);
  460. goto done;
  461. }
  462. /* The remaining commands require a selected device */
  463. if (!flash) {
  464. puts("No SPI flash selected. Please run `sf probe'\n");
  465. return 1;
  466. }
  467. if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
  468. strcmp(cmd, "update") == 0)
  469. ret = do_spi_flash_read_write(argc, argv);
  470. else if (strcmp(cmd, "erase") == 0)
  471. ret = do_spi_flash_erase(argc, argv);
  472. #ifdef CONFIG_CMD_SF_TEST
  473. else if (!strcmp(cmd, "test"))
  474. ret = do_spi_flash_test(argc, argv);
  475. #endif
  476. else
  477. ret = -1;
  478. done:
  479. if (ret != -1)
  480. return ret;
  481. usage:
  482. return CMD_RET_USAGE;
  483. }
  484. #ifdef CONFIG_CMD_SF_TEST
  485. #define SF_TEST_HELP "\nsf test offset len " \
  486. "- run a very basic destructive test"
  487. #else
  488. #define SF_TEST_HELP
  489. #endif
  490. U_BOOT_CMD(
  491. sf, 5, 1, do_spi_flash,
  492. "SPI flash sub-system",
  493. "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
  494. " and chip select\n"
  495. "sf read addr offset|partition len - read `len' bytes starting at\n"
  496. " `offset' or from start of mtd\n"
  497. " `partition'to memory at `addr'\n"
  498. "sf write addr offset|partition len - write `len' bytes from memory\n"
  499. " at `addr' to flash at `offset'\n"
  500. " or to start of mtd `partition'\n"
  501. "sf erase offset|partition [+]len - erase `len' bytes from `offset'\n"
  502. " or from start of mtd `partition'\n"
  503. " `+len' round up `len' to block size\n"
  504. "sf update addr offset|partition len - erase and write `len' bytes from memory\n"
  505. " at `addr' to flash at `offset'\n"
  506. " or to start of mtd `partition'\n"
  507. SF_TEST_HELP
  508. );