spi_flash.c 10.0 KB

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