cmd_sf.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 <malloc.h>
  9. #include <spi_flash.h>
  10. #include <asm/io.h>
  11. #ifndef CONFIG_SF_DEFAULT_SPEED
  12. # define CONFIG_SF_DEFAULT_SPEED 1000000
  13. #endif
  14. #ifndef CONFIG_SF_DEFAULT_MODE
  15. # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
  16. #endif
  17. #ifndef CONFIG_SF_DEFAULT_CS
  18. # define CONFIG_SF_DEFAULT_CS 0
  19. #endif
  20. #ifndef CONFIG_SF_DEFAULT_BUS
  21. # define CONFIG_SF_DEFAULT_BUS 0
  22. #endif
  23. static struct spi_flash *flash;
  24. /*
  25. * This function computes the length argument for the erase command.
  26. * The length on which the command is to operate can be given in two forms:
  27. * 1. <cmd> offset len - operate on <'offset', 'len')
  28. * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)')
  29. * If the second form is used and the length doesn't fall on the
  30. * sector boundary, than it will be adjusted to the next sector boundary.
  31. * If it isn't in the flash, the function will fail (return -1).
  32. * Input:
  33. * arg: length specification (i.e. both command arguments)
  34. * Output:
  35. * len: computed length for operation
  36. * Return:
  37. * 1: success
  38. * -1: failure (bad format, bad address).
  39. */
  40. static int sf_parse_len_arg(char *arg, ulong *len)
  41. {
  42. char *ep;
  43. char round_up_len; /* indicates if the "+length" form used */
  44. ulong len_arg;
  45. round_up_len = 0;
  46. if (*arg == '+') {
  47. round_up_len = 1;
  48. ++arg;
  49. }
  50. len_arg = simple_strtoul(arg, &ep, 16);
  51. if (ep == arg || *ep != '\0')
  52. return -1;
  53. if (round_up_len && flash->sector_size > 0)
  54. *len = ROUND(len_arg, flash->sector_size);
  55. else
  56. *len = len_arg;
  57. return 1;
  58. }
  59. /**
  60. * This function takes a byte length and a delta unit of time to compute the
  61. * approximate bytes per second
  62. *
  63. * @param len amount of bytes currently processed
  64. * @param start_ms start time of processing in ms
  65. * @return bytes per second if OK, 0 on error
  66. */
  67. static ulong bytes_per_second(unsigned int len, ulong start_ms)
  68. {
  69. /* less accurate but avoids overflow */
  70. if (len >= ((unsigned int) -1) / 1024)
  71. return len / (max(get_timer(start_ms) / 1024, 1));
  72. else
  73. return 1024 * len / max(get_timer(start_ms), 1);
  74. }
  75. static int do_spi_flash_probe(int argc, char * const argv[])
  76. {
  77. unsigned int bus = CONFIG_SF_DEFAULT_BUS;
  78. unsigned int cs = CONFIG_SF_DEFAULT_CS;
  79. unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
  80. unsigned int mode = CONFIG_SF_DEFAULT_MODE;
  81. char *endp;
  82. struct spi_flash *new;
  83. if (argc >= 2) {
  84. cs = simple_strtoul(argv[1], &endp, 0);
  85. if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
  86. return -1;
  87. if (*endp == ':') {
  88. if (endp[1] == 0)
  89. return -1;
  90. bus = cs;
  91. cs = simple_strtoul(endp + 1, &endp, 0);
  92. if (*endp != 0)
  93. return -1;
  94. }
  95. }
  96. if (argc >= 3) {
  97. speed = simple_strtoul(argv[2], &endp, 0);
  98. if (*argv[2] == 0 || *endp != 0)
  99. return -1;
  100. }
  101. if (argc >= 4) {
  102. mode = simple_strtoul(argv[3], &endp, 16);
  103. if (*argv[3] == 0 || *endp != 0)
  104. return -1;
  105. }
  106. new = spi_flash_probe(bus, cs, speed, mode);
  107. if (!new) {
  108. printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
  109. return 1;
  110. }
  111. if (flash)
  112. spi_flash_free(flash);
  113. flash = new;
  114. return 0;
  115. }
  116. /**
  117. * Write a block of data to SPI flash, first checking if it is different from
  118. * what is already there.
  119. *
  120. * If the data being written is the same, then *skipped is incremented by len.
  121. *
  122. * @param flash flash context pointer
  123. * @param offset flash offset to write
  124. * @param len number of bytes to write
  125. * @param buf buffer to write from
  126. * @param cmp_buf read buffer to use to compare data
  127. * @param skipped Count of skipped data (incremented by this function)
  128. * @return NULL if OK, else a string containing the stage which failed
  129. */
  130. static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
  131. size_t len, const char *buf, char *cmp_buf, size_t *skipped)
  132. {
  133. debug("offset=%#x, sector_size=%#x, len=%#zx\n",
  134. offset, flash->sector_size, len);
  135. if (spi_flash_read(flash, offset, len, cmp_buf))
  136. return "read";
  137. if (memcmp(cmp_buf, buf, len) == 0) {
  138. debug("Skip region %x size %zx: no change\n",
  139. offset, len);
  140. *skipped += len;
  141. return NULL;
  142. }
  143. if (spi_flash_erase(flash, offset, len))
  144. return "erase";
  145. if (spi_flash_write(flash, offset, len, buf))
  146. return "write";
  147. return NULL;
  148. }
  149. /**
  150. * Update an area of SPI flash by erasing and writing any blocks which need
  151. * to change. Existing blocks with the correct data are left unchanged.
  152. *
  153. * @param flash flash context pointer
  154. * @param offset flash offset to write
  155. * @param len number of bytes to write
  156. * @param buf buffer to write from
  157. * @return 0 if ok, 1 on error
  158. */
  159. static int spi_flash_update(struct spi_flash *flash, u32 offset,
  160. size_t len, const char *buf)
  161. {
  162. const char *err_oper = NULL;
  163. char *cmp_buf;
  164. const char *end = buf + len;
  165. size_t todo; /* number of bytes to do in this pass */
  166. size_t skipped = 0; /* statistics */
  167. const ulong start_time = get_timer(0);
  168. size_t scale = 1;
  169. const char *start_buf = buf;
  170. ulong delta;
  171. if (end - buf >= 200)
  172. scale = (end - buf) / 100;
  173. cmp_buf = malloc(flash->sector_size);
  174. if (cmp_buf) {
  175. ulong last_update = get_timer(0);
  176. for (; buf < end && !err_oper; buf += todo, offset += todo) {
  177. todo = min(end - buf, flash->sector_size);
  178. if (get_timer(last_update) > 100) {
  179. printf(" \rUpdating, %zu%% %lu B/s",
  180. 100 - (end - buf) / scale,
  181. bytes_per_second(buf - start_buf,
  182. start_time));
  183. last_update = get_timer(0);
  184. }
  185. err_oper = spi_flash_update_block(flash, offset, todo,
  186. buf, cmp_buf, &skipped);
  187. }
  188. } else {
  189. err_oper = "malloc";
  190. }
  191. free(cmp_buf);
  192. putc('\r');
  193. if (err_oper) {
  194. printf("SPI flash failed in %s step\n", err_oper);
  195. return 1;
  196. }
  197. delta = get_timer(start_time);
  198. printf("%zu bytes written, %zu bytes skipped", len - skipped,
  199. skipped);
  200. printf(" in %ld.%lds, speed %ld B/s\n",
  201. delta / 1000, delta % 1000, bytes_per_second(len, start_time));
  202. return 0;
  203. }
  204. static int do_spi_flash_read_write(int argc, char * const argv[])
  205. {
  206. unsigned long addr;
  207. unsigned long offset;
  208. unsigned long len;
  209. void *buf;
  210. char *endp;
  211. int ret;
  212. if (argc < 4)
  213. return -1;
  214. addr = simple_strtoul(argv[1], &endp, 16);
  215. if (*argv[1] == 0 || *endp != 0)
  216. return -1;
  217. offset = simple_strtoul(argv[2], &endp, 16);
  218. if (*argv[2] == 0 || *endp != 0)
  219. return -1;
  220. len = simple_strtoul(argv[3], &endp, 16);
  221. if (*argv[3] == 0 || *endp != 0)
  222. return -1;
  223. /* Consistency checking */
  224. if (offset + len > flash->size) {
  225. printf("ERROR: attempting %s past flash size (%#x)\n",
  226. argv[0], flash->size);
  227. return 1;
  228. }
  229. buf = map_physmem(addr, len, MAP_WRBACK);
  230. if (!buf) {
  231. puts("Failed to map physical memory\n");
  232. return 1;
  233. }
  234. if (strcmp(argv[0], "update") == 0)
  235. ret = spi_flash_update(flash, offset, len, buf);
  236. else if (strcmp(argv[0], "read") == 0)
  237. ret = spi_flash_read(flash, offset, len, buf);
  238. else
  239. ret = spi_flash_write(flash, offset, len, buf);
  240. unmap_physmem(buf, len);
  241. if (ret) {
  242. printf("SPI flash %s failed\n", argv[0]);
  243. return 1;
  244. }
  245. return 0;
  246. }
  247. static int do_spi_flash_erase(int argc, char * const argv[])
  248. {
  249. unsigned long offset;
  250. unsigned long len;
  251. char *endp;
  252. int ret;
  253. if (argc < 3)
  254. return -1;
  255. offset = simple_strtoul(argv[1], &endp, 16);
  256. if (*argv[1] == 0 || *endp != 0)
  257. return -1;
  258. ret = sf_parse_len_arg(argv[2], &len);
  259. if (ret != 1)
  260. return -1;
  261. /* Consistency checking */
  262. if (offset + len > flash->size) {
  263. printf("ERROR: attempting %s past flash size (%#x)\n",
  264. argv[0], flash->size);
  265. return 1;
  266. }
  267. ret = spi_flash_erase(flash, offset, len);
  268. if (ret) {
  269. printf("SPI flash %s failed\n", argv[0]);
  270. return 1;
  271. }
  272. return 0;
  273. }
  274. static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  275. {
  276. const char *cmd;
  277. int ret;
  278. /* need at least two arguments */
  279. if (argc < 2)
  280. goto usage;
  281. cmd = argv[1];
  282. --argc;
  283. ++argv;
  284. if (strcmp(cmd, "probe") == 0) {
  285. ret = do_spi_flash_probe(argc, argv);
  286. goto done;
  287. }
  288. /* The remaining commands require a selected device */
  289. if (!flash) {
  290. puts("No SPI flash selected. Please run `sf probe'\n");
  291. return 1;
  292. }
  293. if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
  294. strcmp(cmd, "update") == 0)
  295. ret = do_spi_flash_read_write(argc, argv);
  296. else if (strcmp(cmd, "erase") == 0)
  297. ret = do_spi_flash_erase(argc, argv);
  298. else
  299. ret = -1;
  300. done:
  301. if (ret != -1)
  302. return ret;
  303. usage:
  304. return CMD_RET_USAGE;
  305. }
  306. U_BOOT_CMD(
  307. sf, 5, 1, do_spi_flash,
  308. "SPI flash sub-system",
  309. "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
  310. " and chip select\n"
  311. "sf read addr offset len - read `len' bytes starting at\n"
  312. " `offset' to memory at `addr'\n"
  313. "sf write addr offset len - write `len' bytes from memory\n"
  314. " at `addr' to flash at `offset'\n"
  315. "sf erase offset [+]len - erase `len' bytes from `offset'\n"
  316. " `+len' round up `len' to block size\n"
  317. "sf update addr offset len - erase and write `len' bytes from memory\n"
  318. " at `addr' to flash at `offset'"
  319. );