spi_flash.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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, u8 erase_cmd,
  155. u32 offset, size_t len)
  156. {
  157. u32 start, end, erase_size;
  158. int ret;
  159. u8 cmd[4];
  160. erase_size = flash->sector_size;
  161. if (offset % erase_size || len % erase_size) {
  162. debug("SF: Erase offset/length not multiple of erase size\n");
  163. return -1;
  164. }
  165. ret = spi_claim_bus(flash->spi);
  166. if (ret) {
  167. debug("SF: Unable to claim SPI bus\n");
  168. return ret;
  169. }
  170. cmd[0] = erase_cmd;
  171. start = offset;
  172. end = start + len;
  173. while (offset < end) {
  174. spi_flash_addr(offset, cmd);
  175. offset += erase_size;
  176. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  177. cmd[2], cmd[3], offset);
  178. ret = spi_flash_cmd_write_enable(flash);
  179. if (ret)
  180. goto out;
  181. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
  182. if (ret)
  183. goto out;
  184. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  185. if (ret)
  186. goto out;
  187. }
  188. debug("SF: Successfully erased %lu bytes @ %#x\n",
  189. len * erase_size, start);
  190. out:
  191. spi_release_bus(flash->spi);
  192. return ret;
  193. }
  194. /*
  195. * The following table holds all device probe functions
  196. *
  197. * shift: number of continuation bytes before the ID
  198. * idcode: the expected IDCODE or 0xff for non JEDEC devices
  199. * probe: the function to call
  200. *
  201. * Non JEDEC devices should be ordered in the table such that
  202. * the probe functions with best detection algorithms come first.
  203. *
  204. * Several matching entries are permitted, they will be tried
  205. * in sequence until a probe function returns non NULL.
  206. *
  207. * IDCODE_CONT_LEN may be redefined if a device needs to declare a
  208. * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
  209. * changed. This is the max number of bytes probe functions may
  210. * examine when looking up part-specific identification info.
  211. *
  212. * Probe functions will be given the idcode buffer starting at their
  213. * manu id byte (the "idcode" in the table below). In other words,
  214. * all of the continuation bytes will be skipped (the "shift" below).
  215. */
  216. #define IDCODE_CONT_LEN 0
  217. #define IDCODE_PART_LEN 5
  218. static const struct {
  219. const u8 shift;
  220. const u8 idcode;
  221. struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
  222. } flashes[] = {
  223. /* Keep it sorted by define name */
  224. #ifdef CONFIG_SPI_FLASH_ATMEL
  225. { 0, 0x1f, spi_flash_probe_atmel, },
  226. #endif
  227. #ifdef CONFIG_SPI_FLASH_EON
  228. { 0, 0x1c, spi_flash_probe_eon, },
  229. #endif
  230. #ifdef CONFIG_SPI_FLASH_MACRONIX
  231. { 0, 0xc2, spi_flash_probe_macronix, },
  232. #endif
  233. #ifdef CONFIG_SPI_FLASH_SPANSION
  234. { 0, 0x01, spi_flash_probe_spansion, },
  235. #endif
  236. #ifdef CONFIG_SPI_FLASH_SST
  237. { 0, 0xbf, spi_flash_probe_sst, },
  238. #endif
  239. #ifdef CONFIG_SPI_FLASH_STMICRO
  240. { 0, 0x20, spi_flash_probe_stmicro, },
  241. #endif
  242. #ifdef CONFIG_SPI_FLASH_WINBOND
  243. { 0, 0xef, spi_flash_probe_winbond, },
  244. #endif
  245. #ifdef CONFIG_SPI_FRAM_RAMTRON
  246. { 6, 0xc2, spi_fram_probe_ramtron, },
  247. # undef IDCODE_CONT_LEN
  248. # define IDCODE_CONT_LEN 6
  249. #endif
  250. /* Keep it sorted by best detection */
  251. #ifdef CONFIG_SPI_FLASH_STMICRO
  252. { 0, 0xff, spi_flash_probe_stmicro, },
  253. #endif
  254. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  255. { 0, 0xff, spi_fram_probe_ramtron, },
  256. #endif
  257. };
  258. #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
  259. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  260. unsigned int max_hz, unsigned int spi_mode)
  261. {
  262. struct spi_slave *spi;
  263. struct spi_flash *flash = NULL;
  264. int ret, i, shift;
  265. u8 idcode[IDCODE_LEN], *idp;
  266. spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
  267. if (!spi) {
  268. printf("SF: Failed to set up slave\n");
  269. return NULL;
  270. }
  271. ret = spi_claim_bus(spi);
  272. if (ret) {
  273. debug("SF: Failed to claim SPI bus: %d\n", ret);
  274. goto err_claim_bus;
  275. }
  276. /* Read the ID codes */
  277. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  278. if (ret)
  279. goto err_read_id;
  280. #ifdef DEBUG
  281. printf("SF: Got idcodes\n");
  282. print_buffer(0, idcode, 1, sizeof(idcode), 0);
  283. #endif
  284. /* count the number of continuation bytes */
  285. for (shift = 0, idp = idcode;
  286. shift < IDCODE_CONT_LEN && *idp == 0x7f;
  287. ++shift, ++idp)
  288. continue;
  289. /* search the table for matches in shift and id */
  290. for (i = 0; i < ARRAY_SIZE(flashes); ++i)
  291. if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
  292. /* we have a match, call probe */
  293. flash = flashes[i].probe(spi, idp);
  294. if (flash)
  295. break;
  296. }
  297. if (!flash) {
  298. printf("SF: Unsupported manufacturer %02x\n", *idp);
  299. goto err_manufacturer_probe;
  300. }
  301. printf("SF: Detected %s with page size ", flash->name);
  302. print_size(flash->sector_size, ", total ");
  303. print_size(flash->size, "\n");
  304. spi_release_bus(spi);
  305. return flash;
  306. err_manufacturer_probe:
  307. err_read_id:
  308. spi_release_bus(spi);
  309. err_claim_bus:
  310. spi_free_slave(spi);
  311. return NULL;
  312. }
  313. void spi_flash_free(struct spi_flash *flash)
  314. {
  315. spi_free_slave(flash->spi);
  316. free(flash);
  317. }