spi_flash.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 offset, size_t len)
  109. {
  110. u32 start, end, erase_size;
  111. int ret;
  112. u8 cmd[4];
  113. erase_size = flash->sector_size;
  114. if (offset % erase_size || len % erase_size) {
  115. debug("SF: Erase offset/length not multiple of erase size\n");
  116. return -1;
  117. }
  118. ret = spi_claim_bus(flash->spi);
  119. if (ret) {
  120. debug("SF: Unable to claim SPI bus\n");
  121. return ret;
  122. }
  123. cmd[0] = erase_cmd;
  124. start = offset;
  125. end = start + len;
  126. while (offset < end) {
  127. spi_flash_addr(offset, cmd);
  128. offset += erase_size;
  129. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  130. cmd[2], cmd[3], offset);
  131. ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
  132. if (ret)
  133. goto out;
  134. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
  135. if (ret)
  136. goto out;
  137. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  138. if (ret)
  139. goto out;
  140. }
  141. debug("SF: Successfully erased %lu bytes @ %#x\n",
  142. len * erase_size, start);
  143. out:
  144. spi_release_bus(flash->spi);
  145. return ret;
  146. }
  147. /*
  148. * The following table holds all device probe functions
  149. *
  150. * shift: number of continuation bytes before the ID
  151. * idcode: the expected IDCODE or 0xff for non JEDEC devices
  152. * probe: the function to call
  153. *
  154. * Non JEDEC devices should be ordered in the table such that
  155. * the probe functions with best detection algorithms come first.
  156. *
  157. * Several matching entries are permitted, they will be tried
  158. * in sequence until a probe function returns non NULL.
  159. *
  160. * IDCODE_CONT_LEN may be redefined if a device needs to declare a
  161. * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
  162. * changed. This is the max number of bytes probe functions may
  163. * examine when looking up part-specific identification info.
  164. *
  165. * Probe functions will be given the idcode buffer starting at their
  166. * manu id byte (the "idcode" in the table below). In other words,
  167. * all of the continuation bytes will be skipped (the "shift" below).
  168. */
  169. #define IDCODE_CONT_LEN 0
  170. #define IDCODE_PART_LEN 5
  171. static const struct {
  172. const u8 shift;
  173. const u8 idcode;
  174. struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
  175. } flashes[] = {
  176. /* Keep it sorted by define name */
  177. #ifdef CONFIG_SPI_FLASH_ATMEL
  178. { 0, 0x1f, spi_flash_probe_atmel, },
  179. #endif
  180. #ifdef CONFIG_SPI_FLASH_EON
  181. { 0, 0x1c, spi_flash_probe_eon, },
  182. #endif
  183. #ifdef CONFIG_SPI_FLASH_MACRONIX
  184. { 0, 0xc2, spi_flash_probe_macronix, },
  185. #endif
  186. #ifdef CONFIG_SPI_FLASH_SPANSION
  187. { 0, 0x01, spi_flash_probe_spansion, },
  188. #endif
  189. #ifdef CONFIG_SPI_FLASH_SST
  190. { 0, 0xbf, spi_flash_probe_sst, },
  191. #endif
  192. #ifdef CONFIG_SPI_FLASH_STMICRO
  193. { 0, 0x20, spi_flash_probe_stmicro, },
  194. #endif
  195. #ifdef CONFIG_SPI_FLASH_WINBOND
  196. { 0, 0xef, spi_flash_probe_winbond, },
  197. #endif
  198. #ifdef CONFIG_SPI_FRAM_RAMTRON
  199. { 6, 0xc2, spi_fram_probe_ramtron, },
  200. # undef IDCODE_CONT_LEN
  201. # define IDCODE_CONT_LEN 6
  202. #endif
  203. /* Keep it sorted by best detection */
  204. #ifdef CONFIG_SPI_FLASH_STMICRO
  205. { 0, 0xff, spi_flash_probe_stmicro, },
  206. #endif
  207. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  208. { 0, 0xff, spi_fram_probe_ramtron, },
  209. #endif
  210. };
  211. #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
  212. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  213. unsigned int max_hz, unsigned int spi_mode)
  214. {
  215. struct spi_slave *spi;
  216. struct spi_flash *flash = NULL;
  217. int ret, i, shift;
  218. u8 idcode[IDCODE_LEN], *idp;
  219. spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
  220. if (!spi) {
  221. printf("SF: Failed to set up slave\n");
  222. return NULL;
  223. }
  224. ret = spi_claim_bus(spi);
  225. if (ret) {
  226. debug("SF: Failed to claim SPI bus: %d\n", ret);
  227. goto err_claim_bus;
  228. }
  229. /* Read the ID codes */
  230. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  231. if (ret)
  232. goto err_read_id;
  233. #ifdef DEBUG
  234. printf("SF: Got idcodes\n");
  235. print_buffer(0, idcode, 1, sizeof(idcode), 0);
  236. #endif
  237. /* count the number of continuation bytes */
  238. for (shift = 0, idp = idcode;
  239. shift < IDCODE_CONT_LEN && *idp == 0x7f;
  240. ++shift, ++idp)
  241. continue;
  242. /* search the table for matches in shift and id */
  243. for (i = 0; i < ARRAY_SIZE(flashes); ++i)
  244. if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
  245. /* we have a match, call probe */
  246. flash = flashes[i].probe(spi, idp);
  247. if (flash)
  248. break;
  249. }
  250. if (!flash) {
  251. printf("SF: Unsupported manufacturer %02x\n", *idp);
  252. goto err_manufacturer_probe;
  253. }
  254. printf("SF: Detected %s with page size ", flash->name);
  255. print_size(flash->sector_size, ", total ");
  256. print_size(flash->size, "\n");
  257. spi_release_bus(spi);
  258. return flash;
  259. err_manufacturer_probe:
  260. err_read_id:
  261. spi_release_bus(spi);
  262. err_claim_bus:
  263. spi_free_slave(spi);
  264. return NULL;
  265. }
  266. void spi_flash_free(struct spi_flash *flash)
  267. {
  268. spi_free_slave(flash->spi);
  269. free(flash);
  270. }