spi_flash.c 13 KB

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