spi_flash.c 14 KB

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