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 = 0; /* statistics */
  145. cmp_buf = malloc(flash->sector_size);
  146. if (cmp_buf) {
  147. for (; buf < end && !err_oper; buf += todo, offset += todo) {
  148. todo = min(end - buf, flash->sector_size);
  149. err_oper = spi_flash_update_block(flash, offset, todo,
  150. buf, cmp_buf, &skipped);
  151. }
  152. } else {
  153. err_oper = "malloc";
  154. }
  155. free(cmp_buf);
  156. if (err_oper) {
  157. printf("SPI flash failed in %s step\n", err_oper);
  158. return 1;
  159. }
  160. printf("%zu bytes written, %zu bytes skipped\n", len - skipped,
  161. skipped);
  162. return 0;
  163. }
  164. static int do_spi_flash_read_write(int argc, char * const argv[])
  165. {
  166. unsigned long addr;
  167. unsigned long offset;
  168. unsigned long len;
  169. void *buf;
  170. char *endp;
  171. int ret;
  172. if (argc < 4)
  173. return -1;
  174. addr = simple_strtoul(argv[1], &endp, 16);
  175. if (*argv[1] == 0 || *endp != 0)
  176. return -1;
  177. offset = simple_strtoul(argv[2], &endp, 16);
  178. if (*argv[2] == 0 || *endp != 0)
  179. return -1;
  180. len = simple_strtoul(argv[3], &endp, 16);
  181. if (*argv[3] == 0 || *endp != 0)
  182. return -1;
  183. buf = map_physmem(addr, len, MAP_WRBACK);
  184. if (!buf) {
  185. puts("Failed to map physical memory\n");
  186. return 1;
  187. }
  188. if (strcmp(argv[0], "update") == 0)
  189. ret = spi_flash_update(flash, offset, len, buf);
  190. else if (strcmp(argv[0], "read") == 0)
  191. ret = spi_flash_read(flash, offset, len, buf);
  192. else
  193. ret = spi_flash_write(flash, offset, len, buf);
  194. unmap_physmem(buf, len);
  195. if (ret) {
  196. printf("SPI flash %s failed\n", argv[0]);
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. static int do_spi_flash_erase(int argc, char * const argv[])
  202. {
  203. unsigned long offset;
  204. unsigned long len;
  205. char *endp;
  206. int ret;
  207. if (argc < 3)
  208. return -1;
  209. offset = simple_strtoul(argv[1], &endp, 16);
  210. if (*argv[1] == 0 || *endp != 0)
  211. return -1;
  212. ret = sf_parse_len_arg(argv[2], &len);
  213. if (ret != 1)
  214. return -1;
  215. ret = spi_flash_erase(flash, offset, len);
  216. if (ret) {
  217. printf("SPI flash %s failed\n", argv[0]);
  218. return 1;
  219. }
  220. return 0;
  221. }
  222. static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  223. {
  224. const char *cmd;
  225. int ret;
  226. /* need at least two arguments */
  227. if (argc < 2)
  228. goto usage;
  229. cmd = argv[1];
  230. --argc;
  231. ++argv;
  232. if (strcmp(cmd, "probe") == 0) {
  233. ret = do_spi_flash_probe(argc, argv);
  234. goto done;
  235. }
  236. /* The remaining commands require a selected device */
  237. if (!flash) {
  238. puts("No SPI flash selected. Please run `sf probe'\n");
  239. return 1;
  240. }
  241. if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
  242. strcmp(cmd, "update") == 0)
  243. ret = do_spi_flash_read_write(argc, argv);
  244. else if (strcmp(cmd, "erase") == 0)
  245. ret = do_spi_flash_erase(argc, argv);
  246. else
  247. ret = -1;
  248. done:
  249. if (ret != -1)
  250. return ret;
  251. usage:
  252. return cmd_usage(cmdtp);
  253. }
  254. U_BOOT_CMD(
  255. sf, 5, 1, do_spi_flash,
  256. "SPI flash sub-system",
  257. "probe [bus:]cs [hz] [mode] - init flash device on given SPI bus\n"
  258. " and chip select\n"
  259. "sf read addr offset len - read `len' bytes starting at\n"
  260. " `offset' to memory at `addr'\n"
  261. "sf write addr offset len - write `len' bytes from memory\n"
  262. " at `addr' to flash at `offset'\n"
  263. "sf erase offset [+]len - erase `len' bytes from `offset'\n"
  264. " `+len' round up `len' to block size\n"
  265. "sf update addr offset len - erase and write `len' bytes from memory\n"
  266. " at `addr' to flash at `offset'"
  267. );