spi_flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. 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. #ifdef CONFIG_SPI_FLASH_BAR
  75. u8 bank_sel;
  76. bank_sel = offset / SPI_FLASH_16MB_BOUN;
  77. ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
  78. if (ret) {
  79. debug("SF: fail to set bank%d\n", bank_sel);
  80. return ret;
  81. }
  82. #endif
  83. page_addr = offset / page_size;
  84. byte_addr = offset % page_size;
  85. chunk_len = min(len - actual, page_size - byte_addr);
  86. if (flash->spi->max_write_size)
  87. chunk_len = min(chunk_len, flash->spi->max_write_size);
  88. cmd[1] = page_addr >> 8;
  89. cmd[2] = page_addr;
  90. cmd[3] = byte_addr;
  91. debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
  92. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  93. ret = spi_flash_cmd_write_enable(flash);
  94. if (ret < 0) {
  95. debug("SF: enabling write failed\n");
  96. break;
  97. }
  98. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  99. buf + actual, chunk_len);
  100. if (ret < 0) {
  101. debug("SF: write failed\n");
  102. break;
  103. }
  104. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  105. if (ret)
  106. break;
  107. offset += chunk_len;
  108. }
  109. spi_release_bus(flash->spi);
  110. return ret;
  111. }
  112. int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
  113. size_t cmd_len, void *data, size_t data_len)
  114. {
  115. struct spi_slave *spi = flash->spi;
  116. int ret;
  117. spi_claim_bus(spi);
  118. ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
  119. spi_release_bus(spi);
  120. return ret;
  121. }
  122. int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
  123. size_t len, void *data)
  124. {
  125. u8 cmd[5], bank_sel = 0;
  126. u32 remain_len, read_len;
  127. int ret = -1;
  128. /* Handle memory-mapped SPI */
  129. if (flash->memory_map) {
  130. memcpy(data, flash->memory_map + offset, len);
  131. return 0;
  132. }
  133. cmd[0] = CMD_READ_ARRAY_FAST;
  134. cmd[4] = 0x00;
  135. while (len) {
  136. #ifdef CONFIG_SPI_FLASH_BAR
  137. bank_sel = offset / SPI_FLASH_16MB_BOUN;
  138. ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
  139. if (ret) {
  140. debug("SF: fail to set bank%d\n", bank_sel);
  141. return ret;
  142. }
  143. #endif
  144. remain_len = (SPI_FLASH_16MB_BOUN * (bank_sel + 1) - offset);
  145. if (len < remain_len)
  146. read_len = len;
  147. else
  148. read_len = remain_len;
  149. spi_flash_addr(offset, cmd);
  150. ret = spi_flash_read_common(flash, cmd, sizeof(cmd),
  151. data, read_len);
  152. if (ret < 0) {
  153. debug("SF: read failed\n");
  154. break;
  155. }
  156. offset += read_len;
  157. len -= read_len;
  158. data += read_len;
  159. }
  160. return ret;
  161. }
  162. int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
  163. u8 cmd, u8 poll_bit)
  164. {
  165. struct spi_slave *spi = flash->spi;
  166. unsigned long timebase;
  167. int ret;
  168. u8 status;
  169. ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
  170. if (ret) {
  171. debug("SF: Failed to send command %02x: %d\n", cmd, ret);
  172. return ret;
  173. }
  174. timebase = get_timer(0);
  175. do {
  176. WATCHDOG_RESET();
  177. ret = spi_xfer(spi, 8, NULL, &status, 0);
  178. if (ret)
  179. return -1;
  180. if ((status & poll_bit) == 0)
  181. break;
  182. } while (get_timer(timebase) < timeout);
  183. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
  184. if ((status & poll_bit) == 0)
  185. return 0;
  186. /* Timed out */
  187. debug("SF: time out!\n");
  188. return -1;
  189. }
  190. int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
  191. {
  192. return spi_flash_cmd_poll_bit(flash, timeout,
  193. CMD_READ_STATUS, STATUS_WIP);
  194. }
  195. int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
  196. {
  197. u32 erase_size;
  198. int ret;
  199. u8 cmd[4];
  200. erase_size = flash->sector_size;
  201. if (offset % erase_size || len % erase_size) {
  202. debug("SF: Erase offset/length not multiple of erase size\n");
  203. return -1;
  204. }
  205. ret = spi_claim_bus(flash->spi);
  206. if (ret) {
  207. debug("SF: Unable to claim SPI bus\n");
  208. return ret;
  209. }
  210. if (erase_size == 4096)
  211. cmd[0] = CMD_ERASE_4K;
  212. else
  213. cmd[0] = CMD_ERASE_64K;
  214. while (len) {
  215. #ifdef CONFIG_SPI_FLASH_BAR
  216. u8 bank_sel;
  217. bank_sel = offset / SPI_FLASH_16MB_BOUN;
  218. ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
  219. if (ret) {
  220. debug("SF: fail to set bank%d\n", bank_sel);
  221. return ret;
  222. }
  223. #endif
  224. spi_flash_addr(offset, cmd);
  225. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  226. cmd[2], cmd[3], offset);
  227. ret = spi_flash_cmd_write_enable(flash);
  228. if (ret)
  229. goto out;
  230. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
  231. if (ret)
  232. goto out;
  233. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  234. if (ret)
  235. goto out;
  236. offset += erase_size;
  237. len -= erase_size;
  238. }
  239. out:
  240. spi_release_bus(flash->spi);
  241. return ret;
  242. }
  243. int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
  244. {
  245. u8 cmd;
  246. int ret;
  247. ret = spi_flash_cmd_write_enable(flash);
  248. if (ret < 0) {
  249. debug("SF: enabling write failed\n");
  250. return ret;
  251. }
  252. cmd = CMD_WRITE_STATUS;
  253. ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
  254. if (ret) {
  255. debug("SF: fail to write status register\n");
  256. return ret;
  257. }
  258. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  259. if (ret < 0) {
  260. debug("SF: write status register timed out\n");
  261. return ret;
  262. }
  263. return 0;
  264. }
  265. #ifdef CONFIG_SPI_FLASH_BAR
  266. int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
  267. {
  268. u8 cmd;
  269. int ret;
  270. if (flash->bank_curr == bank_sel) {
  271. debug("SF: not require to enable bank%d\n", bank_sel);
  272. return 0;
  273. }
  274. cmd = flash->bank_write_cmd;
  275. ret = spi_flash_cmd_write_enable(flash);
  276. if (ret < 0) {
  277. debug("SF: enabling write failed\n");
  278. return ret;
  279. }
  280. ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &bank_sel, 1);
  281. if (ret) {
  282. debug("SF: fail to write bank addr register\n");
  283. return ret;
  284. }
  285. flash->bank_curr = bank_sel;
  286. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  287. if (ret < 0) {
  288. debug("SF: write bank addr register timed out\n");
  289. return ret;
  290. }
  291. return 0;
  292. }
  293. int spi_flash_bank_config(struct spi_flash *flash, u8 idcode0)
  294. {
  295. u8 cmd;
  296. u8 curr_bank = 0;
  297. /* discover bank cmds */
  298. switch (idcode0) {
  299. case SPI_FLASH_SPANSION_IDCODE0:
  300. flash->bank_read_cmd = CMD_BANKADDR_BRRD;
  301. flash->bank_write_cmd = CMD_BANKADDR_BRWR;
  302. break;
  303. case SPI_FLASH_STMICRO_IDCODE0:
  304. case SPI_FLASH_WINBOND_IDCODE0:
  305. flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
  306. flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
  307. break;
  308. default:
  309. printf("SF: Unsupported bank commands %02x\n", idcode0);
  310. return -1;
  311. }
  312. /* read the bank reg - on which bank the flash is in currently */
  313. cmd = flash->bank_read_cmd;
  314. if (flash->size > SPI_FLASH_16MB_BOUN) {
  315. if (spi_flash_read_common(flash, &cmd, 1, &curr_bank, 1)) {
  316. debug("SF: fail to read bank addr register\n");
  317. return -1;
  318. }
  319. flash->bank_curr = curr_bank;
  320. } else {
  321. flash->bank_curr = curr_bank;
  322. }
  323. return 0;
  324. }
  325. #endif
  326. #ifdef CONFIG_OF_CONTROL
  327. int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
  328. {
  329. fdt_addr_t addr;
  330. fdt_size_t size;
  331. int node;
  332. /* If there is no node, do nothing */
  333. node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
  334. if (node < 0)
  335. return 0;
  336. addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
  337. if (addr == FDT_ADDR_T_NONE) {
  338. debug("%s: Cannot decode address\n", __func__);
  339. return 0;
  340. }
  341. if (flash->size != size) {
  342. debug("%s: Memory map must cover entire device\n", __func__);
  343. return -1;
  344. }
  345. flash->memory_map = (void *)addr;
  346. return 0;
  347. }
  348. #endif /* CONFIG_OF_CONTROL */
  349. /*
  350. * The following table holds all device probe functions
  351. *
  352. * shift: number of continuation bytes before the ID
  353. * idcode: the expected IDCODE or 0xff for non JEDEC devices
  354. * probe: the function to call
  355. *
  356. * Non JEDEC devices should be ordered in the table such that
  357. * the probe functions with best detection algorithms come first.
  358. *
  359. * Several matching entries are permitted, they will be tried
  360. * in sequence until a probe function returns non NULL.
  361. *
  362. * IDCODE_CONT_LEN may be redefined if a device needs to declare a
  363. * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
  364. * changed. This is the max number of bytes probe functions may
  365. * examine when looking up part-specific identification info.
  366. *
  367. * Probe functions will be given the idcode buffer starting at their
  368. * manu id byte (the "idcode" in the table below). In other words,
  369. * all of the continuation bytes will be skipped (the "shift" below).
  370. */
  371. #define IDCODE_CONT_LEN 0
  372. #define IDCODE_PART_LEN 5
  373. static const struct {
  374. const u8 shift;
  375. const u8 idcode;
  376. struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
  377. } flashes[] = {
  378. /* Keep it sorted by define name */
  379. #ifdef CONFIG_SPI_FLASH_ATMEL
  380. { 0, 0x1f, spi_flash_probe_atmel, },
  381. #endif
  382. #ifdef CONFIG_SPI_FLASH_EON
  383. { 0, 0x1c, spi_flash_probe_eon, },
  384. #endif
  385. #ifdef CONFIG_SPI_FLASH_MACRONIX
  386. { 0, 0xc2, spi_flash_probe_macronix, },
  387. #endif
  388. #ifdef CONFIG_SPI_FLASH_SPANSION
  389. { 0, 0x01, spi_flash_probe_spansion, },
  390. #endif
  391. #ifdef CONFIG_SPI_FLASH_SST
  392. { 0, 0xbf, spi_flash_probe_sst, },
  393. #endif
  394. #ifdef CONFIG_SPI_FLASH_STMICRO
  395. { 0, 0x20, spi_flash_probe_stmicro, },
  396. #endif
  397. #ifdef CONFIG_SPI_FLASH_WINBOND
  398. { 0, 0xef, spi_flash_probe_winbond, },
  399. #endif
  400. #ifdef CONFIG_SPI_FRAM_RAMTRON
  401. { 6, 0xc2, spi_fram_probe_ramtron, },
  402. # undef IDCODE_CONT_LEN
  403. # define IDCODE_CONT_LEN 6
  404. #endif
  405. /* Keep it sorted by best detection */
  406. #ifdef CONFIG_SPI_FLASH_STMICRO
  407. { 0, 0xff, spi_flash_probe_stmicro, },
  408. #endif
  409. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  410. { 0, 0xff, spi_fram_probe_ramtron, },
  411. #endif
  412. };
  413. #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
  414. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  415. unsigned int max_hz, unsigned int spi_mode)
  416. {
  417. struct spi_slave *spi;
  418. struct spi_flash *flash = NULL;
  419. int ret, i, shift;
  420. u8 idcode[IDCODE_LEN], *idp;
  421. spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
  422. if (!spi) {
  423. printf("SF: Failed to set up slave\n");
  424. return NULL;
  425. }
  426. ret = spi_claim_bus(spi);
  427. if (ret) {
  428. debug("SF: Failed to claim SPI bus: %d\n", ret);
  429. goto err_claim_bus;
  430. }
  431. /* Read the ID codes */
  432. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  433. if (ret)
  434. goto err_read_id;
  435. #ifdef DEBUG
  436. printf("SF: Got idcodes\n");
  437. print_buffer(0, idcode, 1, sizeof(idcode), 0);
  438. #endif
  439. /* count the number of continuation bytes */
  440. for (shift = 0, idp = idcode;
  441. shift < IDCODE_CONT_LEN && *idp == 0x7f;
  442. ++shift, ++idp)
  443. continue;
  444. /* search the table for matches in shift and id */
  445. for (i = 0; i < ARRAY_SIZE(flashes); ++i)
  446. if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
  447. /* we have a match, call probe */
  448. flash = flashes[i].probe(spi, idp);
  449. if (flash)
  450. break;
  451. }
  452. if (!flash) {
  453. printf("SF: Unsupported manufacturer %02x\n", *idp);
  454. goto err_manufacturer_probe;
  455. }
  456. #ifdef CONFIG_SPI_FLASH_BAR
  457. /* Configure the BAR - disover bank cmds and read current bank */
  458. ret = spi_flash_bank_config(flash, *idp);
  459. if (ret < 0)
  460. goto err_manufacturer_probe;
  461. #endif
  462. #ifdef CONFIG_OF_CONTROL
  463. if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
  464. debug("SF: FDT decode error\n");
  465. goto err_manufacturer_probe;
  466. }
  467. #endif
  468. printf("SF: Detected %s with page size ", flash->name);
  469. print_size(flash->sector_size, ", total ");
  470. print_size(flash->size, "");
  471. if (flash->memory_map)
  472. printf(", mapped at %p", flash->memory_map);
  473. puts("\n");
  474. spi_release_bus(spi);
  475. return flash;
  476. err_manufacturer_probe:
  477. err_read_id:
  478. spi_release_bus(spi);
  479. err_claim_bus:
  480. spi_free_slave(spi);
  481. return NULL;
  482. }
  483. void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
  484. const char *name)
  485. {
  486. struct spi_flash *flash;
  487. void *ptr;
  488. ptr = malloc(size);
  489. if (!ptr) {
  490. debug("SF: Failed to allocate memory\n");
  491. return NULL;
  492. }
  493. memset(ptr, '\0', size);
  494. flash = (struct spi_flash *)(ptr + offset);
  495. /* Set up some basic fields - caller will sort out sizes */
  496. flash->spi = spi;
  497. flash->name = name;
  498. flash->read = spi_flash_cmd_read_fast;
  499. flash->write = spi_flash_cmd_write_multi;
  500. flash->erase = spi_flash_cmd_erase;
  501. return flash;
  502. }
  503. void spi_flash_free(struct spi_flash *flash)
  504. {
  505. spi_free_slave(flash->spi);
  506. free(flash);
  507. }