cmd_sf.c 7.3 KB

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