cmd_sf.c 13 KB

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