cmd_sf.c 13 KB

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