cmd_sf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <spi_flash.h>
  9. #include <asm/io.h>
  10. #ifndef CONFIG_SF_DEFAULT_SPEED
  11. # define CONFIG_SF_DEFAULT_SPEED 1000000
  12. #endif
  13. #ifndef CONFIG_SF_DEFAULT_MODE
  14. # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
  15. #endif
  16. static struct spi_flash *flash;
  17. /*
  18. * This function computes the length argument for the erase command.
  19. * The length on which the command is to operate can be given in two forms:
  20. * 1. <cmd> offset len - operate on <'offset', 'len')
  21. * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)')
  22. * If the second form is used and the length doesn't fall on the
  23. * sector boundary, than it will be adjusted to the next sector boundary.
  24. * If it isn't in the flash, the function will fail (return -1).
  25. * Input:
  26. * arg: length specification (i.e. both command arguments)
  27. * Output:
  28. * len: computed length for operation
  29. * Return:
  30. * 1: success
  31. * -1: failure (bad format, bad address).
  32. */
  33. static int sf_parse_len_arg(char *arg, ulong *len)
  34. {
  35. char *ep;
  36. char round_up_len; /* indicates if the "+length" form used */
  37. ulong len_arg;
  38. round_up_len = 0;
  39. if (*arg == '+') {
  40. round_up_len = 1;
  41. ++arg;
  42. }
  43. len_arg = simple_strtoul(arg, &ep, 16);
  44. if (ep == arg || *ep != '\0')
  45. return -1;
  46. if (round_up_len && flash->sector_size > 0)
  47. *len = ROUND(len_arg - 1, flash->sector_size);
  48. else
  49. *len = len_arg;
  50. return 1;
  51. }
  52. static int do_spi_flash_probe(int argc, char * const argv[])
  53. {
  54. unsigned int bus = 0;
  55. unsigned int cs;
  56. unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
  57. unsigned int mode = CONFIG_SF_DEFAULT_MODE;
  58. char *endp;
  59. struct spi_flash *new;
  60. if (argc < 2)
  61. return -1;
  62. cs = simple_strtoul(argv[1], &endp, 0);
  63. if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
  64. return -1;
  65. if (*endp == ':') {
  66. if (endp[1] == 0)
  67. return -1;
  68. bus = cs;
  69. cs = simple_strtoul(endp + 1, &endp, 0);
  70. if (*endp != 0)
  71. return -1;
  72. }
  73. if (argc >= 3) {
  74. speed = simple_strtoul(argv[2], &endp, 0);
  75. if (*argv[2] == 0 || *endp != 0)
  76. return -1;
  77. }
  78. if (argc >= 4) {
  79. mode = simple_strtoul(argv[3], &endp, 16);
  80. if (*argv[3] == 0 || *endp != 0)
  81. return -1;
  82. }
  83. new = spi_flash_probe(bus, cs, speed, mode);
  84. if (!new) {
  85. printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
  86. return 1;
  87. }
  88. if (flash)
  89. spi_flash_free(flash);
  90. flash = new;
  91. return 0;
  92. }
  93. static int do_spi_flash_read_write(int argc, char * const argv[])
  94. {
  95. unsigned long addr;
  96. unsigned long offset;
  97. unsigned long len;
  98. void *buf;
  99. char *endp;
  100. int ret;
  101. if (argc < 4)
  102. return -1;
  103. addr = simple_strtoul(argv[1], &endp, 16);
  104. if (*argv[1] == 0 || *endp != 0)
  105. return -1;
  106. offset = simple_strtoul(argv[2], &endp, 16);
  107. if (*argv[2] == 0 || *endp != 0)
  108. return -1;
  109. len = simple_strtoul(argv[3], &endp, 16);
  110. if (*argv[3] == 0 || *endp != 0)
  111. return -1;
  112. buf = map_physmem(addr, len, MAP_WRBACK);
  113. if (!buf) {
  114. puts("Failed to map physical memory\n");
  115. return 1;
  116. }
  117. if (strcmp(argv[0], "read") == 0)
  118. ret = spi_flash_read(flash, offset, len, buf);
  119. else
  120. ret = spi_flash_write(flash, offset, len, buf);
  121. unmap_physmem(buf, len);
  122. if (ret) {
  123. printf("SPI flash %s failed\n", argv[0]);
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. static int do_spi_flash_erase(int argc, char * const argv[])
  129. {
  130. unsigned long offset;
  131. unsigned long len;
  132. char *endp;
  133. int ret;
  134. if (argc < 3)
  135. return -1;
  136. offset = simple_strtoul(argv[1], &endp, 16);
  137. if (*argv[1] == 0 || *endp != 0)
  138. return -1;
  139. ret = sf_parse_len_arg(argv[2], &len);
  140. if (ret != 1)
  141. return -1;
  142. ret = spi_flash_erase(flash, offset, len);
  143. if (ret) {
  144. printf("SPI flash %s failed\n", argv[0]);
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  150. {
  151. const char *cmd;
  152. int ret;
  153. /* need at least two arguments */
  154. if (argc < 2)
  155. goto usage;
  156. cmd = argv[1];
  157. --argc;
  158. ++argv;
  159. if (strcmp(cmd, "probe") == 0) {
  160. ret = do_spi_flash_probe(argc, argv);
  161. goto done;
  162. }
  163. /* The remaining commands require a selected device */
  164. if (!flash) {
  165. puts("No SPI flash selected. Please run `sf probe'\n");
  166. return 1;
  167. }
  168. if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0)
  169. ret = do_spi_flash_read_write(argc, argv);
  170. else if (strcmp(cmd, "erase") == 0)
  171. ret = do_spi_flash_erase(argc, argv);
  172. else
  173. ret = -1;
  174. done:
  175. if (ret != -1)
  176. return ret;
  177. usage:
  178. return cmd_usage(cmdtp);
  179. }
  180. U_BOOT_CMD(
  181. sf, 5, 1, do_spi_flash,
  182. "SPI flash sub-system",
  183. "probe [bus:]cs [hz] [mode] - init flash device on given SPI bus\n"
  184. " and chip select\n"
  185. "sf read addr offset len - read `len' bytes starting at\n"
  186. " `offset' to memory at `addr'\n"
  187. "sf write addr offset len - write `len' bytes from memory\n"
  188. " at `addr' to flash at `offset'\n"
  189. "sf erase offset [+]len - erase `len' bytes from `offset'\n"
  190. " `+len' round up `len' to block size"
  191. );