cmd_sf.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. static int do_spi_flash_probe(int argc, char * const argv[])
  60. {
  61. unsigned int bus = CONFIG_SF_DEFAULT_BUS;
  62. unsigned int cs = CONFIG_SF_DEFAULT_CS;
  63. unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
  64. unsigned int mode = CONFIG_SF_DEFAULT_MODE;
  65. char *endp;
  66. struct spi_flash *new;
  67. if (argc >= 2) {
  68. cs = simple_strtoul(argv[1], &endp, 0);
  69. if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
  70. return -1;
  71. if (*endp == ':') {
  72. if (endp[1] == 0)
  73. return -1;
  74. bus = cs;
  75. cs = simple_strtoul(endp + 1, &endp, 0);
  76. if (*endp != 0)
  77. return -1;
  78. }
  79. }
  80. if (argc >= 3) {
  81. speed = simple_strtoul(argv[2], &endp, 0);
  82. if (*argv[2] == 0 || *endp != 0)
  83. return -1;
  84. }
  85. if (argc >= 4) {
  86. mode = simple_strtoul(argv[3], &endp, 16);
  87. if (*argv[3] == 0 || *endp != 0)
  88. return -1;
  89. }
  90. new = spi_flash_probe(bus, cs, speed, mode);
  91. if (!new) {
  92. printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
  93. return 1;
  94. }
  95. if (flash)
  96. spi_flash_free(flash);
  97. flash = new;
  98. return 0;
  99. }
  100. /**
  101. * Write a block of data to SPI flash, first checking if it is different from
  102. * what is already there.
  103. *
  104. * If the data being written is the same, then *skipped is incremented by len.
  105. *
  106. * @param flash flash context pointer
  107. * @param offset flash offset to write
  108. * @param len number of bytes to write
  109. * @param buf buffer to write from
  110. * @param cmp_buf read buffer to use to compare data
  111. * @param skipped Count of skipped data (incremented by this function)
  112. * @return NULL if OK, else a string containing the stage which failed
  113. */
  114. static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
  115. size_t len, const char *buf, char *cmp_buf, size_t *skipped)
  116. {
  117. debug("offset=%#x, sector_size=%#x, len=%#zx\n",
  118. offset, flash->sector_size, len);
  119. if (spi_flash_read(flash, offset, len, cmp_buf))
  120. return "read";
  121. if (memcmp(cmp_buf, buf, len) == 0) {
  122. debug("Skip region %x size %zx: no change\n",
  123. offset, len);
  124. *skipped += len;
  125. return NULL;
  126. }
  127. if (spi_flash_erase(flash, offset, len))
  128. return "erase";
  129. if (spi_flash_write(flash, offset, len, buf))
  130. return "write";
  131. return NULL;
  132. }
  133. /**
  134. * Update an area of SPI flash by erasing and writing any blocks which need
  135. * to change. Existing blocks with the correct data are left unchanged.
  136. *
  137. * @param flash flash context pointer
  138. * @param offset flash offset to write
  139. * @param len number of bytes to write
  140. * @param buf buffer to write from
  141. * @return 0 if ok, 1 on error
  142. */
  143. static int spi_flash_update(struct spi_flash *flash, u32 offset,
  144. size_t len, const char *buf)
  145. {
  146. const char *err_oper = NULL;
  147. char *cmp_buf;
  148. const char *end = buf + len;
  149. size_t todo; /* number of bytes to do in this pass */
  150. size_t skipped = 0; /* statistics */
  151. cmp_buf = malloc(flash->sector_size);
  152. if (cmp_buf) {
  153. for (; buf < end && !err_oper; buf += todo, offset += todo) {
  154. todo = min(end - buf, flash->sector_size);
  155. err_oper = spi_flash_update_block(flash, offset, todo,
  156. buf, cmp_buf, &skipped);
  157. }
  158. } else {
  159. err_oper = "malloc";
  160. }
  161. free(cmp_buf);
  162. if (err_oper) {
  163. printf("SPI flash failed in %s step\n", err_oper);
  164. return 1;
  165. }
  166. printf("%zu bytes written, %zu bytes skipped\n", len - skipped,
  167. skipped);
  168. return 0;
  169. }
  170. static int do_spi_flash_read_write(int argc, char * const argv[])
  171. {
  172. unsigned long addr;
  173. unsigned long offset;
  174. unsigned long len;
  175. void *buf;
  176. char *endp;
  177. int ret;
  178. if (argc < 4)
  179. return -1;
  180. addr = simple_strtoul(argv[1], &endp, 16);
  181. if (*argv[1] == 0 || *endp != 0)
  182. return -1;
  183. offset = simple_strtoul(argv[2], &endp, 16);
  184. if (*argv[2] == 0 || *endp != 0)
  185. return -1;
  186. len = simple_strtoul(argv[3], &endp, 16);
  187. if (*argv[3] == 0 || *endp != 0)
  188. return -1;
  189. /* Consistency checking */
  190. if (offset + len > flash->size) {
  191. printf("ERROR: attempting %s past flash size (%#x)\n",
  192. argv[0], flash->size);
  193. return 1;
  194. }
  195. buf = map_physmem(addr, len, MAP_WRBACK);
  196. if (!buf) {
  197. puts("Failed to map physical memory\n");
  198. return 1;
  199. }
  200. if (strcmp(argv[0], "update") == 0)
  201. ret = spi_flash_update(flash, offset, len, buf);
  202. else if (strcmp(argv[0], "read") == 0)
  203. ret = spi_flash_read(flash, offset, len, buf);
  204. else
  205. ret = spi_flash_write(flash, offset, len, buf);
  206. unmap_physmem(buf, len);
  207. if (ret) {
  208. printf("SPI flash %s failed\n", argv[0]);
  209. return 1;
  210. }
  211. return 0;
  212. }
  213. static int do_spi_flash_erase(int argc, char * const argv[])
  214. {
  215. unsigned long offset;
  216. unsigned long len;
  217. char *endp;
  218. int ret;
  219. if (argc < 3)
  220. return -1;
  221. offset = simple_strtoul(argv[1], &endp, 16);
  222. if (*argv[1] == 0 || *endp != 0)
  223. return -1;
  224. ret = sf_parse_len_arg(argv[2], &len);
  225. if (ret != 1)
  226. return -1;
  227. /* Consistency checking */
  228. if (offset + len > flash->size) {
  229. printf("ERROR: attempting %s past flash size (%#x)\n",
  230. argv[0], flash->size);
  231. return 1;
  232. }
  233. ret = spi_flash_erase(flash, offset, len);
  234. if (ret) {
  235. printf("SPI flash %s failed\n", argv[0]);
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  241. {
  242. const char *cmd;
  243. int ret;
  244. /* need at least two arguments */
  245. if (argc < 2)
  246. goto usage;
  247. cmd = argv[1];
  248. --argc;
  249. ++argv;
  250. if (strcmp(cmd, "probe") == 0) {
  251. ret = do_spi_flash_probe(argc, argv);
  252. goto done;
  253. }
  254. /* The remaining commands require a selected device */
  255. if (!flash) {
  256. puts("No SPI flash selected. Please run `sf probe'\n");
  257. return 1;
  258. }
  259. if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0 ||
  260. strcmp(cmd, "update") == 0)
  261. ret = do_spi_flash_read_write(argc, argv);
  262. else if (strcmp(cmd, "erase") == 0)
  263. ret = do_spi_flash_erase(argc, argv);
  264. else
  265. ret = -1;
  266. done:
  267. if (ret != -1)
  268. return ret;
  269. usage:
  270. return CMD_RET_USAGE;
  271. }
  272. U_BOOT_CMD(
  273. sf, 5, 1, do_spi_flash,
  274. "SPI flash sub-system",
  275. "probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus\n"
  276. " and chip select\n"
  277. "sf read addr offset len - read `len' bytes starting at\n"
  278. " `offset' to memory at `addr'\n"
  279. "sf write addr offset len - write `len' bytes from memory\n"
  280. " at `addr' to flash at `offset'\n"
  281. "sf erase offset [+]len - erase `len' bytes from `offset'\n"
  282. " `+len' round up `len' to block size\n"
  283. "sf update addr offset len - erase and write `len' bytes from memory\n"
  284. " at `addr' to flash at `offset'"
  285. );