spi_flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 <fdtdec.h>
  11. #include <malloc.h>
  12. #include <spi.h>
  13. #include <spi_flash.h>
  14. #include <watchdog.h>
  15. #include "spi_flash_internal.h"
  16. DECLARE_GLOBAL_DATA_PTR;
  17. static void spi_flash_addr(u32 addr, u8 *cmd)
  18. {
  19. /* cmd[0] is actual command */
  20. cmd[1] = addr >> 16;
  21. cmd[2] = addr >> 8;
  22. cmd[3] = addr >> 0;
  23. }
  24. static int spi_flash_read_write(struct spi_slave *spi,
  25. const u8 *cmd, size_t cmd_len,
  26. const u8 *data_out, u8 *data_in,
  27. size_t data_len)
  28. {
  29. unsigned long flags = SPI_XFER_BEGIN;
  30. int ret;
  31. if (data_len == 0)
  32. flags |= SPI_XFER_END;
  33. ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
  34. if (ret) {
  35. debug("SF: Failed to send command (%zu bytes): %d\n",
  36. cmd_len, ret);
  37. } else if (data_len != 0) {
  38. ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
  39. if (ret)
  40. debug("SF: Failed to transfer %zu bytes of data: %d\n",
  41. data_len, ret);
  42. }
  43. return ret;
  44. }
  45. int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
  46. {
  47. return spi_flash_cmd_read(spi, &cmd, 1, response, len);
  48. }
  49. int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
  50. size_t cmd_len, void *data, size_t data_len)
  51. {
  52. return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
  53. }
  54. int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
  55. const void *data, size_t data_len)
  56. {
  57. return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
  58. }
  59. int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset,
  60. size_t len, const void *buf)
  61. {
  62. unsigned long page_addr, byte_addr, page_size;
  63. size_t chunk_len, actual;
  64. int ret;
  65. u8 cmd[4];
  66. page_size = flash->page_size;
  67. page_addr = offset / page_size;
  68. byte_addr = offset % page_size;
  69. ret = spi_claim_bus(flash->spi);
  70. if (ret) {
  71. debug("SF: unable to claim SPI bus\n");
  72. return ret;
  73. }
  74. cmd[0] = CMD_PAGE_PROGRAM;
  75. for (actual = 0; actual < len; actual += chunk_len) {
  76. chunk_len = min(len - actual, page_size - byte_addr);
  77. if (flash->spi->max_write_size)
  78. chunk_len = min(chunk_len, flash->spi->max_write_size);
  79. cmd[1] = page_addr >> 8;
  80. cmd[2] = page_addr;
  81. cmd[3] = byte_addr;
  82. debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
  83. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  84. ret = spi_flash_cmd_write_enable(flash);
  85. if (ret < 0) {
  86. debug("SF: enabling write failed\n");
  87. break;
  88. }
  89. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  90. buf + actual, chunk_len);
  91. if (ret < 0) {
  92. debug("SF: write failed\n");
  93. break;
  94. }
  95. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  96. if (ret)
  97. break;
  98. byte_addr += chunk_len;
  99. if (byte_addr == page_size) {
  100. page_addr++;
  101. byte_addr = 0;
  102. }
  103. }
  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. /* Handle memory-mapped SPI */
  122. if (flash->memory_map) {
  123. memcpy(data, flash->memory_map + offset, len);
  124. return 0;
  125. }
  126. cmd[0] = CMD_READ_ARRAY_FAST;
  127. spi_flash_addr(offset, cmd);
  128. cmd[4] = 0x00;
  129. return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len);
  130. }
  131. int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
  132. u8 cmd, u8 poll_bit)
  133. {
  134. struct spi_slave *spi = flash->spi;
  135. unsigned long timebase;
  136. int ret;
  137. u8 status;
  138. ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
  139. if (ret) {
  140. debug("SF: Failed to send command %02x: %d\n", cmd, ret);
  141. return ret;
  142. }
  143. timebase = get_timer(0);
  144. do {
  145. WATCHDOG_RESET();
  146. ret = spi_xfer(spi, 8, NULL, &status, 0);
  147. if (ret)
  148. return -1;
  149. if ((status & poll_bit) == 0)
  150. break;
  151. } while (get_timer(timebase) < timeout);
  152. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
  153. if ((status & poll_bit) == 0)
  154. return 0;
  155. /* Timed out */
  156. debug("SF: time out!\n");
  157. return -1;
  158. }
  159. int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
  160. {
  161. return spi_flash_cmd_poll_bit(flash, timeout,
  162. CMD_READ_STATUS, STATUS_WIP);
  163. }
  164. int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
  165. {
  166. u32 end, erase_size;
  167. int ret;
  168. u8 cmd[4];
  169. erase_size = flash->sector_size;
  170. if (offset % erase_size || len % erase_size) {
  171. debug("SF: Erase offset/length not multiple of erase size\n");
  172. return -1;
  173. }
  174. ret = spi_claim_bus(flash->spi);
  175. if (ret) {
  176. debug("SF: Unable to claim SPI bus\n");
  177. return ret;
  178. }
  179. if (erase_size == 4096)
  180. cmd[0] = CMD_ERASE_4K;
  181. else
  182. cmd[0] = CMD_ERASE_64K;
  183. end = offset + len;
  184. while (offset < end) {
  185. spi_flash_addr(offset, cmd);
  186. offset += erase_size;
  187. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  188. cmd[2], cmd[3], offset);
  189. ret = spi_flash_cmd_write_enable(flash);
  190. if (ret)
  191. goto out;
  192. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
  193. if (ret)
  194. goto out;
  195. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  196. if (ret)
  197. goto out;
  198. }
  199. out:
  200. spi_release_bus(flash->spi);
  201. return ret;
  202. }
  203. int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
  204. {
  205. u8 cmd;
  206. int ret;
  207. ret = spi_flash_cmd_write_enable(flash);
  208. if (ret < 0) {
  209. debug("SF: enabling write failed\n");
  210. return ret;
  211. }
  212. cmd = CMD_WRITE_STATUS;
  213. ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
  214. if (ret) {
  215. debug("SF: fail to write status register\n");
  216. return ret;
  217. }
  218. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  219. if (ret < 0) {
  220. debug("SF: write status register timed out\n");
  221. return ret;
  222. }
  223. return 0;
  224. }
  225. #ifdef CONFIG_OF_CONTROL
  226. int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
  227. {
  228. fdt_addr_t addr;
  229. fdt_size_t size;
  230. int node;
  231. /* If there is no node, do nothing */
  232. node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
  233. if (node < 0)
  234. return 0;
  235. addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
  236. if (addr == FDT_ADDR_T_NONE) {
  237. debug("%s: Cannot decode address\n", __func__);
  238. return 0;
  239. }
  240. if (flash->size != size) {
  241. debug("%s: Memory map must cover entire device\n", __func__);
  242. return -1;
  243. }
  244. flash->memory_map = (void *)addr;
  245. return 0;
  246. }
  247. #endif /* CONFIG_OF_CONTROL */
  248. /*
  249. * The following table holds all device probe functions
  250. *
  251. * shift: number of continuation bytes before the ID
  252. * idcode: the expected IDCODE or 0xff for non JEDEC devices
  253. * probe: the function to call
  254. *
  255. * Non JEDEC devices should be ordered in the table such that
  256. * the probe functions with best detection algorithms come first.
  257. *
  258. * Several matching entries are permitted, they will be tried
  259. * in sequence until a probe function returns non NULL.
  260. *
  261. * IDCODE_CONT_LEN may be redefined if a device needs to declare a
  262. * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
  263. * changed. This is the max number of bytes probe functions may
  264. * examine when looking up part-specific identification info.
  265. *
  266. * Probe functions will be given the idcode buffer starting at their
  267. * manu id byte (the "idcode" in the table below). In other words,
  268. * all of the continuation bytes will be skipped (the "shift" below).
  269. */
  270. #define IDCODE_CONT_LEN 0
  271. #define IDCODE_PART_LEN 5
  272. static const struct {
  273. const u8 shift;
  274. const u8 idcode;
  275. struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
  276. } flashes[] = {
  277. /* Keep it sorted by define name */
  278. #ifdef CONFIG_SPI_FLASH_ATMEL
  279. { 0, 0x1f, spi_flash_probe_atmel, },
  280. #endif
  281. #ifdef CONFIG_SPI_FLASH_EON
  282. { 0, 0x1c, spi_flash_probe_eon, },
  283. #endif
  284. #ifdef CONFIG_SPI_FLASH_GIGADEVICE
  285. { 0, 0xc8, spi_flash_probe_gigadevice, },
  286. #endif
  287. #ifdef CONFIG_SPI_FLASH_MACRONIX
  288. { 0, 0xc2, spi_flash_probe_macronix, },
  289. #endif
  290. #ifdef CONFIG_SPI_FLASH_SPANSION
  291. { 0, 0x01, spi_flash_probe_spansion, },
  292. #endif
  293. #ifdef CONFIG_SPI_FLASH_SST
  294. { 0, 0xbf, spi_flash_probe_sst, },
  295. #endif
  296. #ifdef CONFIG_SPI_FLASH_STMICRO
  297. { 0, 0x20, spi_flash_probe_stmicro, },
  298. #endif
  299. #ifdef CONFIG_SPI_FLASH_WINBOND
  300. { 0, 0xef, spi_flash_probe_winbond, },
  301. #endif
  302. #ifdef CONFIG_SPI_FRAM_RAMTRON
  303. { 6, 0xc2, spi_fram_probe_ramtron, },
  304. # undef IDCODE_CONT_LEN
  305. # define IDCODE_CONT_LEN 6
  306. #endif
  307. /* Keep it sorted by best detection */
  308. #ifdef CONFIG_SPI_FLASH_STMICRO
  309. { 0, 0xff, spi_flash_probe_stmicro, },
  310. #endif
  311. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  312. { 0, 0xff, spi_fram_probe_ramtron, },
  313. #endif
  314. };
  315. #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
  316. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  317. unsigned int max_hz, unsigned int spi_mode)
  318. {
  319. struct spi_slave *spi;
  320. struct spi_flash *flash = NULL;
  321. int ret, i, shift;
  322. u8 idcode[IDCODE_LEN], *idp;
  323. spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
  324. if (!spi) {
  325. printf("SF: Failed to set up slave\n");
  326. return NULL;
  327. }
  328. ret = spi_claim_bus(spi);
  329. if (ret) {
  330. debug("SF: Failed to claim SPI bus: %d\n", ret);
  331. goto err_claim_bus;
  332. }
  333. /* Read the ID codes */
  334. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  335. if (ret)
  336. goto err_read_id;
  337. #ifdef DEBUG
  338. printf("SF: Got idcodes\n");
  339. print_buffer(0, idcode, 1, sizeof(idcode), 0);
  340. #endif
  341. /* count the number of continuation bytes */
  342. for (shift = 0, idp = idcode;
  343. shift < IDCODE_CONT_LEN && *idp == 0x7f;
  344. ++shift, ++idp)
  345. continue;
  346. /* search the table for matches in shift and id */
  347. for (i = 0; i < ARRAY_SIZE(flashes); ++i)
  348. if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
  349. /* we have a match, call probe */
  350. flash = flashes[i].probe(spi, idp);
  351. if (flash)
  352. break;
  353. }
  354. if (!flash) {
  355. printf("SF: Unsupported manufacturer %02x\n", *idp);
  356. goto err_manufacturer_probe;
  357. }
  358. #ifdef CONFIG_OF_CONTROL
  359. if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
  360. debug("SF: FDT decode error\n");
  361. goto err_manufacturer_probe;
  362. }
  363. #endif
  364. printf("SF: Detected %s with page size ", flash->name);
  365. print_size(flash->sector_size, ", total ");
  366. print_size(flash->size, "");
  367. if (flash->memory_map)
  368. printf(", mapped at %p", flash->memory_map);
  369. puts("\n");
  370. spi_release_bus(spi);
  371. return flash;
  372. err_manufacturer_probe:
  373. err_read_id:
  374. spi_release_bus(spi);
  375. err_claim_bus:
  376. spi_free_slave(spi);
  377. return NULL;
  378. }
  379. void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
  380. const char *name)
  381. {
  382. struct spi_flash *flash;
  383. void *ptr;
  384. ptr = malloc(size);
  385. if (!ptr) {
  386. debug("SF: Failed to allocate memory\n");
  387. return NULL;
  388. }
  389. memset(ptr, '\0', size);
  390. flash = (struct spi_flash *)(ptr + offset);
  391. /* Set up some basic fields - caller will sort out sizes */
  392. flash->spi = spi;
  393. flash->name = name;
  394. flash->read = spi_flash_cmd_read_fast;
  395. flash->write = spi_flash_cmd_write_multi;
  396. flash->erase = spi_flash_cmd_erase;
  397. return flash;
  398. }
  399. void spi_flash_free(struct spi_flash *flash)
  400. {
  401. spi_free_slave(flash->spi);
  402. free(flash);
  403. }