spi_flash.c 8.8 KB

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