cmd_sf.c 13 KB

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