cmd_sf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. new = spi_flash_probe(bus, cs, speed, mode);
  124. if (!new) {
  125. printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
  126. return 1;
  127. }
  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. void *buf;
  234. char *endp;
  235. int ret = 1;
  236. int dev = 0;
  237. loff_t offset, len, maxsize;
  238. if (argc < 3)
  239. return -1;
  240. addr = simple_strtoul(argv[1], &endp, 16);
  241. if (*argv[1] == 0 || *endp != 0)
  242. return -1;
  243. if (mtd_arg_off_size(argc - 2, &argv[2], &dev, &offset, &len,
  244. &maxsize, MTD_DEV_TYPE_NOR, flash->size))
  245. return -1;
  246. /* Consistency checking */
  247. if (offset + len > flash->size) {
  248. printf("ERROR: attempting %s past flash size (%#x)\n",
  249. argv[0], flash->size);
  250. return 1;
  251. }
  252. buf = map_physmem(addr, len, MAP_WRBACK);
  253. if (!buf) {
  254. puts("Failed to map physical memory\n");
  255. return 1;
  256. }
  257. if (strcmp(argv[0], "update") == 0) {
  258. ret = spi_flash_update(flash, offset, len, buf);
  259. } else if (strncmp(argv[0], "read", 4) == 0 ||
  260. strncmp(argv[0], "write", 5) == 0) {
  261. int read;
  262. read = strncmp(argv[0], "read", 4) == 0;
  263. if (read)
  264. ret = spi_flash_read(flash, offset, len, buf);
  265. else
  266. ret = spi_flash_write(flash, offset, len, buf);
  267. printf("SF: %zu bytes @ %#x %s: %s\n", (size_t)len, (u32)offset,
  268. read ? "Read" : "Written", ret ? "ERROR" : "OK");
  269. }
  270. unmap_physmem(buf, len);
  271. return ret == 0 ? 0 : 1;
  272. }
  273. static int do_spi_flash_erase(int argc, char * const argv[])
  274. {
  275. int ret;
  276. int dev = 0;
  277. loff_t offset, len, maxsize;
  278. ulong size;
  279. if (argc < 3)
  280. return -1;
  281. if (mtd_arg_off(argv[1], &dev, &offset, &len, &maxsize,
  282. MTD_DEV_TYPE_NOR, flash->size))
  283. return -1;
  284. ret = sf_parse_len_arg(argv[2], &size);
  285. if (ret != 1)
  286. return -1;
  287. /* Consistency checking */
  288. if (offset + size > flash->size) {
  289. printf("ERROR: attempting %s past flash size (%#x)\n",
  290. argv[0], flash->size);
  291. return 1;
  292. }
  293. ret = spi_flash_erase(flash, offset, size);
  294. printf("SF: %zu bytes @ %#x Erased: %s\n", (size_t)size, (u32)offset,
  295. ret ? "ERROR" : "OK");
  296. return ret == 0 ? 0 : 1;
  297. }
  298. #ifdef CONFIG_CMD_SF_TEST
  299. enum {
  300. STAGE_ERASE,
  301. STAGE_CHECK,
  302. STAGE_WRITE,
  303. STAGE_READ,
  304. STAGE_COUNT,
  305. };
  306. static char *stage_name[STAGE_COUNT] = {
  307. "erase",
  308. "check",
  309. "write",
  310. "read",
  311. };
  312. struct test_info {
  313. int stage;
  314. int bytes;
  315. unsigned base_ms;
  316. unsigned time_ms[STAGE_COUNT];
  317. };
  318. static void show_time(struct test_info *test, int stage)
  319. {
  320. uint64_t speed; /* KiB/s */
  321. int bps; /* Bits per second */
  322. speed = (long long)test->bytes * 1000;
  323. if (test->time_ms[stage])
  324. do_div(speed, test->time_ms[stage] * 1024);
  325. bps = speed * 8;
  326. printf("%d %s: %d ticks, %d KiB/s %d.%03d Mbps\n", stage,
  327. stage_name[stage], test->time_ms[stage],
  328. (int)speed, bps / 1000, bps % 1000);
  329. }
  330. static void spi_test_next_stage(struct test_info *test)
  331. {
  332. test->time_ms[test->stage] = get_timer(test->base_ms);
  333. show_time(test, test->stage);
  334. test->base_ms = get_timer(0);
  335. test->stage++;
  336. }
  337. /**
  338. * Run a test on the SPI flash
  339. *
  340. * @param flash SPI flash to use
  341. * @param buf Source buffer for data to write
  342. * @param len Size of data to read/write
  343. * @param offset Offset within flash to check
  344. * @param vbuf Verification buffer
  345. * @return 0 if ok, -1 on error
  346. */
  347. static int spi_flash_test(struct spi_flash *flash, uint8_t *buf, ulong len,
  348. ulong offset, uint8_t *vbuf)
  349. {
  350. struct test_info test;
  351. int i;
  352. printf("SPI flash test:\n");
  353. memset(&test, '\0', sizeof(test));
  354. test.base_ms = get_timer(0);
  355. test.bytes = len;
  356. if (spi_flash_erase(flash, offset, len)) {
  357. printf("Erase failed\n");
  358. return -1;
  359. }
  360. spi_test_next_stage(&test);
  361. if (spi_flash_read(flash, offset, len, vbuf)) {
  362. printf("Check read failed\n");
  363. return -1;
  364. }
  365. for (i = 0; i < len; i++) {
  366. if (vbuf[i] != 0xff) {
  367. printf("Check failed at %d\n", i);
  368. print_buffer(i, vbuf + i, 1,
  369. min_t(uint, len - i, 0x40), 0);
  370. return -1;
  371. }
  372. }
  373. spi_test_next_stage(&test);
  374. if (spi_flash_write(flash, offset, len, buf)) {
  375. printf("Write failed\n");
  376. return -1;
  377. }
  378. memset(vbuf, '\0', len);
  379. spi_test_next_stage(&test);
  380. if (spi_flash_read(flash, offset, len, vbuf)) {
  381. printf("Read failed\n");
  382. return -1;
  383. }
  384. spi_test_next_stage(&test);
  385. for (i = 0; i < len; i++) {
  386. if (buf[i] != vbuf[i]) {
  387. printf("Verify failed at %d, good data:\n", i);
  388. print_buffer(i, buf + i, 1,
  389. min_t(uint, len - i, 0x40), 0);
  390. printf("Bad data:\n");
  391. print_buffer(i, vbuf + i, 1,
  392. min_t(uint, len - i, 0x40), 0);
  393. return -1;
  394. }
  395. }
  396. printf("Test passed\n");
  397. for (i = 0; i < STAGE_COUNT; i++)
  398. show_time(&test, i);
  399. return 0;
  400. }
  401. static int do_spi_flash_test(int argc, char * const argv[])
  402. {
  403. unsigned long offset;
  404. unsigned long len;
  405. uint8_t *buf, *from;
  406. char *endp;
  407. uint8_t *vbuf;
  408. int ret;
  409. if (argc < 3)
  410. return -1;
  411. offset = simple_strtoul(argv[1], &endp, 16);
  412. if (*argv[1] == 0 || *endp != 0)
  413. return -1;
  414. len = simple_strtoul(argv[2], &endp, 16);
  415. if (*argv[2] == 0 || *endp != 0)
  416. return -1;
  417. vbuf = malloc(len);
  418. if (!vbuf) {
  419. printf("Cannot allocate memory (%lu bytes)\n", len);
  420. return 1;
  421. }
  422. buf = malloc(len);
  423. if (!buf) {
  424. free(vbuf);
  425. printf("Cannot allocate memory (%lu bytes)\n", len);
  426. return 1;
  427. }
  428. from = map_sysmem(CONFIG_SYS_TEXT_BASE, 0);
  429. memcpy(buf, from, len);
  430. ret = spi_flash_test(flash, buf, len, offset, vbuf);
  431. free(vbuf);
  432. free(buf);
  433. if (ret) {
  434. printf("Test failed\n");
  435. return 1;
  436. }
  437. return 0;
  438. }
  439. #endif /* CONFIG_CMD_SF_TEST */
  440. static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc,
  441. char * const argv[])
  442. {
  443. const char *cmd;
  444. int ret;
  445. /* need at least two arguments */
  446. if (argc < 2)
  447. goto usage;
  448. cmd = argv[1];
  449. --argc;
  450. ++argv;
  451. if (strcmp(cmd, "probe") == 0) {
  452. ret = do_spi_flash_probe(argc, argv);
  453. goto done;
  454. }
  455. /* The remaining commands require a selected device */
  456. if (!flash) {
  457. puts("No SPI flash selected. Please run `sf probe'\n");
  458. return 1;
  459. }
  460. if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
  461. strcmp(cmd, "update") == 0)
  462. ret = do_spi_flash_read_write(argc, argv);
  463. else if (strcmp(cmd, "erase") == 0)
  464. ret = do_spi_flash_erase(argc, argv);
  465. #ifdef CONFIG_CMD_SF_TEST
  466. else if (!strcmp(cmd, "test"))
  467. ret = do_spi_flash_test(argc, argv);
  468. #endif
  469. else
  470. ret = -1;
  471. done:
  472. if (ret != -1)
  473. return ret;
  474. usage:
  475. return CMD_RET_USAGE;
  476. }
  477. #ifdef CONFIG_CMD_SF_TEST
  478. #define SF_TEST_HELP "\nsf test offset len " \
  479. "- run a very basic destructive test"
  480. #else
  481. #define SF_TEST_HELP
  482. #endif
  483. U_BOOT_CMD(
  484. sf, 5, 1, do_spi_flash,
  485. "SPI flash sub-system",
  486. "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
  487. " and chip select\n"
  488. "sf read addr offset|partition len - read `len' bytes starting at\n"
  489. " `offset' or from start of mtd\n"
  490. " `partition'to memory at `addr'\n"
  491. "sf write addr offset|partition len - write `len' bytes from memory\n"
  492. " at `addr' to flash at `offset'\n"
  493. " or to start of mtd `partition'\n"
  494. "sf erase offset|partition [+]len - erase `len' bytes from `offset'\n"
  495. " or from start of mtd `partition'\n"
  496. " `+len' round up `len' to block size\n"
  497. "sf update addr offset|partition len - erase and write `len' bytes from memory\n"
  498. " at `addr' to flash at `offset'\n"
  499. " or to start of mtd `partition'\n"
  500. SF_TEST_HELP
  501. );