cmd_sf.c 13 KB

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