cmd_sf.c 13 KB

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