spi_flash.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
  196. {
  197. u8 cmd;
  198. int ret;
  199. ret = spi_flash_cmd_write_enable(flash);
  200. if (ret < 0) {
  201. debug("SF: enabling write failed\n");
  202. return ret;
  203. }
  204. cmd = CMD_WRITE_STATUS;
  205. ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
  206. if (ret) {
  207. debug("SF: fail to write status register\n");
  208. return ret;
  209. }
  210. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  211. if (ret < 0) {
  212. debug("SF: write status register timed out\n");
  213. return ret;
  214. }
  215. return 0;
  216. }
  217. /*
  218. * The following table holds all device probe functions
  219. *
  220. * shift: number of continuation bytes before the ID
  221. * idcode: the expected IDCODE or 0xff for non JEDEC devices
  222. * probe: the function to call
  223. *
  224. * Non JEDEC devices should be ordered in the table such that
  225. * the probe functions with best detection algorithms come first.
  226. *
  227. * Several matching entries are permitted, they will be tried
  228. * in sequence until a probe function returns non NULL.
  229. *
  230. * IDCODE_CONT_LEN may be redefined if a device needs to declare a
  231. * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
  232. * changed. This is the max number of bytes probe functions may
  233. * examine when looking up part-specific identification info.
  234. *
  235. * Probe functions will be given the idcode buffer starting at their
  236. * manu id byte (the "idcode" in the table below). In other words,
  237. * all of the continuation bytes will be skipped (the "shift" below).
  238. */
  239. #define IDCODE_CONT_LEN 0
  240. #define IDCODE_PART_LEN 5
  241. static const struct {
  242. const u8 shift;
  243. const u8 idcode;
  244. struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
  245. } flashes[] = {
  246. /* Keep it sorted by define name */
  247. #ifdef CONFIG_SPI_FLASH_ATMEL
  248. { 0, 0x1f, spi_flash_probe_atmel, },
  249. #endif
  250. #ifdef CONFIG_SPI_FLASH_EON
  251. { 0, 0x1c, spi_flash_probe_eon, },
  252. #endif
  253. #ifdef CONFIG_SPI_FLASH_MACRONIX
  254. { 0, 0xc2, spi_flash_probe_macronix, },
  255. #endif
  256. #ifdef CONFIG_SPI_FLASH_SPANSION
  257. { 0, 0x01, spi_flash_probe_spansion, },
  258. #endif
  259. #ifdef CONFIG_SPI_FLASH_SST
  260. { 0, 0xbf, spi_flash_probe_sst, },
  261. #endif
  262. #ifdef CONFIG_SPI_FLASH_STMICRO
  263. { 0, 0x20, spi_flash_probe_stmicro, },
  264. #endif
  265. #ifdef CONFIG_SPI_FLASH_WINBOND
  266. { 0, 0xef, spi_flash_probe_winbond, },
  267. #endif
  268. #ifdef CONFIG_SPI_FRAM_RAMTRON
  269. { 6, 0xc2, spi_fram_probe_ramtron, },
  270. # undef IDCODE_CONT_LEN
  271. # define IDCODE_CONT_LEN 6
  272. #endif
  273. /* Keep it sorted by best detection */
  274. #ifdef CONFIG_SPI_FLASH_STMICRO
  275. { 0, 0xff, spi_flash_probe_stmicro, },
  276. #endif
  277. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  278. { 0, 0xff, spi_fram_probe_ramtron, },
  279. #endif
  280. };
  281. #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
  282. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  283. unsigned int max_hz, unsigned int spi_mode)
  284. {
  285. struct spi_slave *spi;
  286. struct spi_flash *flash = NULL;
  287. int ret, i, shift;
  288. u8 idcode[IDCODE_LEN], *idp;
  289. spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
  290. if (!spi) {
  291. printf("SF: Failed to set up slave\n");
  292. return NULL;
  293. }
  294. ret = spi_claim_bus(spi);
  295. if (ret) {
  296. debug("SF: Failed to claim SPI bus: %d\n", ret);
  297. goto err_claim_bus;
  298. }
  299. /* Read the ID codes */
  300. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  301. if (ret)
  302. goto err_read_id;
  303. #ifdef DEBUG
  304. printf("SF: Got idcodes\n");
  305. print_buffer(0, idcode, 1, sizeof(idcode), 0);
  306. #endif
  307. /* count the number of continuation bytes */
  308. for (shift = 0, idp = idcode;
  309. shift < IDCODE_CONT_LEN && *idp == 0x7f;
  310. ++shift, ++idp)
  311. continue;
  312. /* search the table for matches in shift and id */
  313. for (i = 0; i < ARRAY_SIZE(flashes); ++i)
  314. if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
  315. /* we have a match, call probe */
  316. flash = flashes[i].probe(spi, idp);
  317. if (flash)
  318. break;
  319. }
  320. if (!flash) {
  321. printf("SF: Unsupported manufacturer %02x\n", *idp);
  322. goto err_manufacturer_probe;
  323. }
  324. printf("SF: Detected %s with page size ", flash->name);
  325. print_size(flash->sector_size, ", total ");
  326. print_size(flash->size, "\n");
  327. spi_release_bus(spi);
  328. return flash;
  329. err_manufacturer_probe:
  330. err_read_id:
  331. spi_release_bus(spi);
  332. err_claim_bus:
  333. spi_free_slave(spi);
  334. return NULL;
  335. }
  336. void spi_flash_free(struct spi_flash *flash)
  337. {
  338. spi_free_slave(flash->spi);
  339. free(flash);
  340. }