spi_flash.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * SPI flash interface
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <common.h>
  10. #include <malloc.h>
  11. #include <spi.h>
  12. #include <spi_flash.h>
  13. #include "spi_flash_internal.h"
  14. static void spi_flash_addr(u32 addr, u8 *cmd)
  15. {
  16. /* cmd[0] is actual command */
  17. cmd[1] = addr >> 16;
  18. cmd[2] = addr >> 8;
  19. cmd[3] = addr >> 0;
  20. }
  21. static int spi_flash_read_write(struct spi_slave *spi,
  22. const u8 *cmd, size_t cmd_len,
  23. const u8 *data_out, u8 *data_in,
  24. size_t data_len)
  25. {
  26. unsigned long flags = SPI_XFER_BEGIN;
  27. int ret;
  28. if (data_len == 0)
  29. flags |= SPI_XFER_END;
  30. ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
  31. if (ret) {
  32. debug("SF: Failed to send command (%zu bytes): %d\n",
  33. cmd_len, ret);
  34. } else if (data_len != 0) {
  35. ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
  36. if (ret)
  37. debug("SF: Failed to transfer %zu bytes of data: %d\n",
  38. data_len, ret);
  39. }
  40. return ret;
  41. }
  42. int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
  43. {
  44. return spi_flash_cmd_read(spi, &cmd, 1, response, len);
  45. }
  46. int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
  47. size_t cmd_len, void *data, size_t data_len)
  48. {
  49. return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
  50. }
  51. int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
  52. const void *data, size_t data_len)
  53. {
  54. return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
  55. }
  56. int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
  57. size_t cmd_len, void *data, size_t data_len)
  58. {
  59. struct spi_slave *spi = flash->spi;
  60. int ret;
  61. spi_claim_bus(spi);
  62. ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
  63. spi_release_bus(spi);
  64. return ret;
  65. }
  66. int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
  67. size_t len, void *data)
  68. {
  69. u8 cmd[5];
  70. cmd[0] = CMD_READ_ARRAY_FAST;
  71. spi_flash_addr(offset, cmd);
  72. cmd[4] = 0x00;
  73. return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len);
  74. }
  75. int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
  76. u8 cmd, u8 poll_bit)
  77. {
  78. struct spi_slave *spi = flash->spi;
  79. unsigned long timebase;
  80. int ret;
  81. u8 status;
  82. ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
  83. if (ret) {
  84. debug("SF: Failed to send command %02x: %d\n", cmd, ret);
  85. return ret;
  86. }
  87. timebase = get_timer(0);
  88. do {
  89. ret = spi_xfer(spi, 8, NULL, &status, 0);
  90. if (ret)
  91. return -1;
  92. if ((status & poll_bit) == 0)
  93. break;
  94. } while (get_timer(timebase) < timeout);
  95. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
  96. if ((status & poll_bit) == 0)
  97. return 0;
  98. /* Timed out */
  99. debug("SF: time out!\n");
  100. return -1;
  101. }
  102. int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
  103. {
  104. return spi_flash_cmd_poll_bit(flash, timeout,
  105. CMD_READ_STATUS, STATUS_WIP);
  106. }
  107. int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
  108. u32 erase_size, u32 offset, size_t len)
  109. {
  110. u32 start, end;
  111. int ret;
  112. u8 cmd[4];
  113. if (offset % erase_size || len % erase_size) {
  114. debug("SF: Erase offset/length not multiple of erase size\n");
  115. return -1;
  116. }
  117. ret = spi_claim_bus(flash->spi);
  118. if (ret) {
  119. debug("SF: Unable to claim SPI bus\n");
  120. return ret;
  121. }
  122. cmd[0] = erase_cmd;
  123. start = offset;
  124. end = start + len;
  125. while (offset < end) {
  126. spi_flash_addr(offset, cmd);
  127. offset += erase_size;
  128. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  129. cmd[2], cmd[3], offset);
  130. ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
  131. if (ret)
  132. goto out;
  133. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
  134. if (ret)
  135. goto out;
  136. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  137. if (ret)
  138. goto out;
  139. }
  140. debug("SF: Successfully erased %lu bytes @ %#x\n",
  141. len * erase_size, start);
  142. out:
  143. spi_release_bus(flash->spi);
  144. return ret;
  145. }
  146. /*
  147. * The following table holds all device probe functions
  148. *
  149. * shift: number of continuation bytes before the ID
  150. * idcode: the expected IDCODE or 0xff for non JEDEC devices
  151. * probe: the function to call
  152. *
  153. * Non JEDEC devices should be ordered in the table such that
  154. * the probe functions with best detection algorithms come first.
  155. *
  156. * Several matching entries are permitted, they will be tried
  157. * in sequence until a probe function returns non NULL.
  158. *
  159. * IDCODE_CONT_LEN may be redefined if a device needs to declare a
  160. * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
  161. * changed. This is the max number of bytes probe functions may
  162. * examine when looking up part-specific identification info.
  163. *
  164. * Probe functions will be given the idcode buffer starting at their
  165. * manu id byte (the "idcode" in the table below). In other words,
  166. * all of the continuation bytes will be skipped (the "shift" below).
  167. */
  168. #define IDCODE_CONT_LEN 0
  169. #define IDCODE_PART_LEN 5
  170. static const struct {
  171. const u8 shift;
  172. const u8 idcode;
  173. struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
  174. } flashes[] = {
  175. /* Keep it sorted by define name */
  176. #ifdef CONFIG_SPI_FLASH_ATMEL
  177. { 0, 0x1f, spi_flash_probe_atmel, },
  178. #endif
  179. #ifdef CONFIG_SPI_FLASH_EON
  180. { 0, 0x1c, spi_flash_probe_eon, },
  181. #endif
  182. #ifdef CONFIG_SPI_FLASH_MACRONIX
  183. { 0, 0xc2, spi_flash_probe_macronix, },
  184. #endif
  185. #ifdef CONFIG_SPI_FLASH_SPANSION
  186. { 0, 0x01, spi_flash_probe_spansion, },
  187. #endif
  188. #ifdef CONFIG_SPI_FLASH_SST
  189. { 0, 0xbf, spi_flash_probe_sst, },
  190. #endif
  191. #ifdef CONFIG_SPI_FLASH_STMICRO
  192. { 0, 0x20, spi_flash_probe_stmicro, },
  193. #endif
  194. #ifdef CONFIG_SPI_FLASH_WINBOND
  195. { 0, 0xef, spi_flash_probe_winbond, },
  196. #endif
  197. #ifdef CONFIG_SPI_FRAM_RAMTRON
  198. { 6, 0xc2, spi_fram_probe_ramtron, },
  199. # undef IDCODE_CONT_LEN
  200. # define IDCODE_CONT_LEN 6
  201. #endif
  202. /* Keep it sorted by best detection */
  203. #ifdef CONFIG_SPI_FLASH_STMICRO
  204. { 0, 0xff, spi_flash_probe_stmicro, },
  205. #endif
  206. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  207. { 0, 0xff, spi_fram_probe_ramtron, },
  208. #endif
  209. };
  210. #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
  211. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  212. unsigned int max_hz, unsigned int spi_mode)
  213. {
  214. struct spi_slave *spi;
  215. struct spi_flash *flash = NULL;
  216. int ret, i, shift;
  217. u8 idcode[IDCODE_LEN], *idp;
  218. spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
  219. if (!spi) {
  220. printf("SF: Failed to set up slave\n");
  221. return NULL;
  222. }
  223. ret = spi_claim_bus(spi);
  224. if (ret) {
  225. debug("SF: Failed to claim SPI bus: %d\n", ret);
  226. goto err_claim_bus;
  227. }
  228. /* Read the ID codes */
  229. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  230. if (ret)
  231. goto err_read_id;
  232. #ifdef DEBUG
  233. printf("SF: Got idcodes\n");
  234. print_buffer(0, idcode, 1, sizeof(idcode), 0);
  235. #endif
  236. /* count the number of continuation bytes */
  237. for (shift = 0, idp = idcode;
  238. shift < IDCODE_CONT_LEN && *idp == 0x7f;
  239. ++shift, ++idp)
  240. continue;
  241. /* search the table for matches in shift and id */
  242. for (i = 0; i < ARRAY_SIZE(flashes); ++i)
  243. if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
  244. /* we have a match, call probe */
  245. flash = flashes[i].probe(spi, idp);
  246. if (flash)
  247. break;
  248. }
  249. if (!flash) {
  250. printf("SF: Unsupported manufacturer %02x\n", *idp);
  251. goto err_manufacturer_probe;
  252. }
  253. spi_release_bus(spi);
  254. return flash;
  255. err_manufacturer_probe:
  256. err_read_id:
  257. spi_release_bus(spi);
  258. err_claim_bus:
  259. spi_free_slave(spi);
  260. return NULL;
  261. }
  262. void spi_flash_free(struct spi_flash *flash)
  263. {
  264. spi_free_slave(flash->spi);
  265. free(flash);
  266. }