spi_flash.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 int spi_flash_read_write(struct spi_slave *spi,
  15. const u8 *cmd, size_t cmd_len,
  16. const u8 *data_out, u8 *data_in,
  17. size_t data_len)
  18. {
  19. unsigned long flags = SPI_XFER_BEGIN;
  20. int ret;
  21. if (data_len == 0)
  22. flags |= SPI_XFER_END;
  23. ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
  24. if (ret) {
  25. debug("SF: Failed to send command (%zu bytes): %d\n",
  26. cmd_len, ret);
  27. } else if (data_len != 0) {
  28. ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
  29. if (ret)
  30. debug("SF: Failed to transfer %zu bytes of data: %d\n",
  31. data_len, ret);
  32. }
  33. return ret;
  34. }
  35. int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
  36. {
  37. return spi_flash_cmd_read(spi, &cmd, 1, response, len);
  38. }
  39. int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
  40. size_t cmd_len, void *data, size_t data_len)
  41. {
  42. return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
  43. }
  44. int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
  45. const void *data, size_t data_len)
  46. {
  47. return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
  48. }
  49. int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
  50. size_t cmd_len, void *data, size_t data_len)
  51. {
  52. struct spi_slave *spi = flash->spi;
  53. int ret;
  54. spi_claim_bus(spi);
  55. ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
  56. spi_release_bus(spi);
  57. return ret;
  58. }
  59. int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
  60. u8 cmd, u8 poll_bit)
  61. {
  62. struct spi_slave *spi = flash->spi;
  63. unsigned long timebase;
  64. int ret;
  65. u8 status;
  66. ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
  67. if (ret) {
  68. debug("SF: Failed to send command %02x: %d\n", cmd, ret);
  69. return ret;
  70. }
  71. timebase = get_timer(0);
  72. do {
  73. ret = spi_xfer(spi, 8, NULL, &status, 0);
  74. if (ret)
  75. return -1;
  76. if ((status & poll_bit) == 0)
  77. break;
  78. } while (get_timer(timebase) < timeout);
  79. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
  80. if ((status & poll_bit) == 0)
  81. return 0;
  82. /* Timed out */
  83. debug("SF: time out!\n");
  84. return -1;
  85. }
  86. int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
  87. {
  88. return spi_flash_cmd_poll_bit(flash, timeout,
  89. CMD_READ_STATUS, STATUS_WIP);
  90. }
  91. /*
  92. * The following table holds all device probe functions
  93. *
  94. * shift: number of continuation bytes before the ID
  95. * idcode: the expected IDCODE or 0xff for non JEDEC devices
  96. * probe: the function to call
  97. *
  98. * Non JEDEC devices should be ordered in the table such that
  99. * the probe functions with best detection algorithms come first.
  100. *
  101. * Several matching entries are permitted, they will be tried
  102. * in sequence until a probe function returns non NULL.
  103. *
  104. * IDCODE_CONT_LEN may be redefined if a device needs to declare a
  105. * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
  106. * changed. This is the max number of bytes probe functions may
  107. * examine when looking up part-specific identification info.
  108. *
  109. * Probe functions will be given the idcode buffer starting at their
  110. * manu id byte (the "idcode" in the table below). In other words,
  111. * all of the continuation bytes will be skipped (the "shift" below).
  112. */
  113. #define IDCODE_CONT_LEN 0
  114. #define IDCODE_PART_LEN 5
  115. static const struct {
  116. const u8 shift;
  117. const u8 idcode;
  118. struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
  119. } flashes[] = {
  120. /* Keep it sorted by define name */
  121. #ifdef CONFIG_SPI_FLASH_ATMEL
  122. { 0, 0x1f, spi_flash_probe_atmel, },
  123. #endif
  124. #ifdef CONFIG_SPI_FLASH_EON
  125. { 0, 0x1c, spi_flash_probe_eon, },
  126. #endif
  127. #ifdef CONFIG_SPI_FLASH_MACRONIX
  128. { 0, 0xc2, spi_flash_probe_macronix, },
  129. #endif
  130. #ifdef CONFIG_SPI_FLASH_SPANSION
  131. { 0, 0x01, spi_flash_probe_spansion, },
  132. #endif
  133. #ifdef CONFIG_SPI_FLASH_SST
  134. { 0, 0xbf, spi_flash_probe_sst, },
  135. #endif
  136. #ifdef CONFIG_SPI_FLASH_STMICRO
  137. { 0, 0x20, spi_flash_probe_stmicro, },
  138. #endif
  139. #ifdef CONFIG_SPI_FLASH_WINBOND
  140. { 0, 0xef, spi_flash_probe_winbond, },
  141. #endif
  142. #ifdef CONFIG_SPI_FRAM_RAMTRON
  143. { 6, 0xc2, spi_fram_probe_ramtron, },
  144. # undef IDCODE_CONT_LEN
  145. # define IDCODE_CONT_LEN 6
  146. #endif
  147. /* Keep it sorted by best detection */
  148. #ifdef CONFIG_SPI_FLASH_STMICRO
  149. { 0, 0xff, spi_flash_probe_stmicro, },
  150. #endif
  151. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  152. { 0, 0xff, spi_fram_probe_ramtron, },
  153. #endif
  154. };
  155. #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
  156. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  157. unsigned int max_hz, unsigned int spi_mode)
  158. {
  159. struct spi_slave *spi;
  160. struct spi_flash *flash = NULL;
  161. int ret, i, shift;
  162. u8 idcode[IDCODE_LEN], *idp;
  163. spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
  164. if (!spi) {
  165. printf("SF: Failed to set up slave\n");
  166. return NULL;
  167. }
  168. ret = spi_claim_bus(spi);
  169. if (ret) {
  170. debug("SF: Failed to claim SPI bus: %d\n", ret);
  171. goto err_claim_bus;
  172. }
  173. /* Read the ID codes */
  174. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  175. if (ret)
  176. goto err_read_id;
  177. #ifdef DEBUG
  178. printf("SF: Got idcodes\n");
  179. print_buffer(0, idcode, 1, sizeof(idcode), 0);
  180. #endif
  181. /* count the number of continuation bytes */
  182. for (shift = 0, idp = idcode;
  183. shift < IDCODE_CONT_LEN && *idp == 0x7f;
  184. ++shift, ++idp)
  185. continue;
  186. /* search the table for matches in shift and id */
  187. for (i = 0; i < ARRAY_SIZE(flashes); ++i)
  188. if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
  189. /* we have a match, call probe */
  190. flash = flashes[i].probe(spi, idp);
  191. if (flash)
  192. break;
  193. }
  194. if (!flash) {
  195. printf("SF: Unsupported manufacturer %02x\n", *idp);
  196. goto err_manufacturer_probe;
  197. }
  198. spi_release_bus(spi);
  199. return flash;
  200. err_manufacturer_probe:
  201. err_read_id:
  202. spi_release_bus(spi);
  203. err_claim_bus:
  204. spi_free_slave(spi);
  205. return NULL;
  206. }
  207. void spi_flash_free(struct spi_flash *flash)
  208. {
  209. spi_free_slave(flash->spi);
  210. free(flash);
  211. }