spi_flash.c 5.5 KB

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