spi_flash.c 7.5 KB

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