sf_probe.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * SPI flash probing
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
  6. * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <fdtdec.h>
  14. #include <malloc.h>
  15. #include <mapmem.h>
  16. #include <spi.h>
  17. #include <spi_flash.h>
  18. #include <asm/io.h>
  19. #include "sf_internal.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. /* Read commands array */
  22. static u8 spi_read_cmds_array[] = {
  23. CMD_READ_ARRAY_SLOW,
  24. CMD_READ_ARRAY_FAST,
  25. CMD_READ_DUAL_OUTPUT_FAST,
  26. CMD_READ_DUAL_IO_FAST,
  27. CMD_READ_QUAD_OUTPUT_FAST,
  28. CMD_READ_QUAD_IO_FAST,
  29. };
  30. #ifdef CONFIG_SPI_FLASH_MACRONIX
  31. static int spi_flash_set_qeb_mxic(struct spi_flash *flash)
  32. {
  33. u8 qeb_status;
  34. int ret;
  35. ret = spi_flash_cmd_read_status(flash, &qeb_status);
  36. if (ret < 0)
  37. return ret;
  38. if (qeb_status & STATUS_QEB_MXIC) {
  39. debug("SF: mxic: QEB is already set\n");
  40. } else {
  41. ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC);
  42. if (ret < 0)
  43. return ret;
  44. }
  45. return ret;
  46. }
  47. #endif
  48. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  49. static int spi_flash_set_qeb_winspan(struct spi_flash *flash)
  50. {
  51. u8 qeb_status;
  52. int ret;
  53. ret = spi_flash_cmd_read_config(flash, &qeb_status);
  54. if (ret < 0)
  55. return ret;
  56. if (qeb_status & STATUS_QEB_WINSPAN) {
  57. debug("SF: winspan: QEB is already set\n");
  58. } else {
  59. ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN);
  60. if (ret < 0)
  61. return ret;
  62. }
  63. return ret;
  64. }
  65. #endif
  66. static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
  67. {
  68. switch (idcode0) {
  69. #ifdef CONFIG_SPI_FLASH_MACRONIX
  70. case SPI_FLASH_CFI_MFR_MACRONIX:
  71. return spi_flash_set_qeb_mxic(flash);
  72. #endif
  73. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  74. case SPI_FLASH_CFI_MFR_SPANSION:
  75. case SPI_FLASH_CFI_MFR_WINBOND:
  76. return spi_flash_set_qeb_winspan(flash);
  77. #endif
  78. #ifdef CONFIG_SPI_FLASH_STMICRO
  79. case SPI_FLASH_CFI_MFR_STMICRO:
  80. debug("SF: QEB is volatile for %02x flash\n", idcode0);
  81. return 0;
  82. #endif
  83. default:
  84. printf("SF: Need set QEB func for %02x flash\n", idcode0);
  85. return -1;
  86. }
  87. }
  88. #ifdef CONFIG_SPI_FLASH_BAR
  89. static int spi_flash_read_bank(struct spi_flash *flash, u8 idcode0)
  90. {
  91. u8 curr_bank = 0;
  92. int ret;
  93. if (flash->size <= SPI_FLASH_16MB_BOUN)
  94. goto bank_end;
  95. switch (idcode0) {
  96. case SPI_FLASH_CFI_MFR_SPANSION:
  97. flash->bank_read_cmd = CMD_BANKADDR_BRRD;
  98. flash->bank_write_cmd = CMD_BANKADDR_BRWR;
  99. default:
  100. flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
  101. flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
  102. }
  103. ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
  104. &curr_bank, 1);
  105. if (ret) {
  106. debug("SF: fail to read bank addr register\n");
  107. return ret;
  108. }
  109. bank_end:
  110. flash->bank_curr = curr_bank;
  111. return 0;
  112. }
  113. #endif
  114. static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
  115. struct spi_flash *flash)
  116. {
  117. const struct spi_flash_params *params;
  118. u8 cmd;
  119. u16 jedec = idcode[1] << 8 | idcode[2];
  120. u16 ext_jedec = idcode[3] << 8 | idcode[4];
  121. /* Validate params from spi_flash_params table */
  122. params = spi_flash_params_table;
  123. for (; params->name != NULL; params++) {
  124. if ((params->jedec >> 16) == idcode[0]) {
  125. if ((params->jedec & 0xFFFF) == jedec) {
  126. if (params->ext_jedec == 0)
  127. break;
  128. else if (params->ext_jedec == ext_jedec)
  129. break;
  130. }
  131. }
  132. }
  133. if (!params->name) {
  134. printf("SF: Unsupported flash IDs: ");
  135. printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
  136. idcode[0], jedec, ext_jedec);
  137. return -EPROTONOSUPPORT;
  138. }
  139. /* Assign spi data */
  140. flash->spi = spi;
  141. flash->name = params->name;
  142. flash->memory_map = spi->memory_map;
  143. flash->dual_flash = flash->spi->option;
  144. /* Assign spi_flash ops */
  145. #ifndef CONFIG_DM_SPI_FLASH
  146. flash->write = spi_flash_cmd_write_ops;
  147. #if defined(CONFIG_SPI_FLASH_SST)
  148. if (params->flags & SST_WR)
  149. flash->flags |= SNOR_F_SST_WR;
  150. if (params->flags & SNOR_F_SST_WR) {
  151. if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
  152. flash->write = sst_write_bp;
  153. else
  154. flash->write = sst_write_wp;
  155. }
  156. #endif
  157. flash->erase = spi_flash_cmd_erase_ops;
  158. flash->read = spi_flash_cmd_read_ops;
  159. #endif
  160. /* Compute the flash size */
  161. flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
  162. /*
  163. * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
  164. * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
  165. * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
  166. * have 256b pages.
  167. */
  168. if (ext_jedec == 0x4d00) {
  169. if ((jedec == 0x0215) || (jedec == 0x216))
  170. flash->page_size = 256;
  171. else
  172. flash->page_size = 512;
  173. } else {
  174. flash->page_size = 256;
  175. }
  176. flash->page_size <<= flash->shift;
  177. flash->sector_size = params->sector_size << flash->shift;
  178. flash->size = flash->sector_size * params->nr_sectors << flash->shift;
  179. #ifdef CONFIG_SF_DUAL_FLASH
  180. if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
  181. flash->size <<= 1;
  182. #endif
  183. /* Compute erase sector and command */
  184. if (params->flags & SECT_4K) {
  185. flash->erase_cmd = CMD_ERASE_4K;
  186. flash->erase_size = 4096 << flash->shift;
  187. } else if (params->flags & SECT_32K) {
  188. flash->erase_cmd = CMD_ERASE_32K;
  189. flash->erase_size = 32768 << flash->shift;
  190. } else {
  191. flash->erase_cmd = CMD_ERASE_64K;
  192. flash->erase_size = flash->sector_size;
  193. }
  194. /* Now erase size becomes valid sector size */
  195. flash->sector_size = flash->erase_size;
  196. /* Look for the fastest read cmd */
  197. cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
  198. if (cmd) {
  199. cmd = spi_read_cmds_array[cmd - 1];
  200. flash->read_cmd = cmd;
  201. } else {
  202. /* Go for default supported read cmd */
  203. flash->read_cmd = CMD_READ_ARRAY_FAST;
  204. }
  205. /* Not require to look for fastest only two write cmds yet */
  206. if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
  207. flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
  208. else
  209. /* Go for default supported write cmd */
  210. flash->write_cmd = CMD_PAGE_PROGRAM;
  211. /* Read dummy_byte: dummy byte is determined based on the
  212. * dummy cycles of a particular command.
  213. * Fast commands - dummy_byte = dummy_cycles/8
  214. * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
  215. * For I/O commands except cmd[0] everything goes on no.of lines
  216. * based on particular command but incase of fast commands except
  217. * data all go on single line irrespective of command.
  218. */
  219. switch (flash->read_cmd) {
  220. case CMD_READ_QUAD_IO_FAST:
  221. flash->dummy_byte = 2;
  222. break;
  223. case CMD_READ_ARRAY_SLOW:
  224. flash->dummy_byte = 0;
  225. break;
  226. default:
  227. flash->dummy_byte = 1;
  228. }
  229. /* Poll cmd selection */
  230. flash->poll_cmd = CMD_READ_STATUS;
  231. #ifdef CONFIG_SPI_FLASH_STMICRO
  232. if (params->flags & E_FSR)
  233. flash->poll_cmd = CMD_FLAG_STATUS;
  234. #endif
  235. /* Configure the BAR - discover bank cmds and read current bank */
  236. #ifdef CONFIG_SPI_FLASH_BAR
  237. int ret = spi_flash_read_bank(flash, idcode[0]);
  238. if (ret < 0)
  239. return ret;
  240. #endif
  241. /* Flash powers up read-only, so clear BP# bits */
  242. #if defined(CONFIG_SPI_FLASH_ATMEL) || \
  243. defined(CONFIG_SPI_FLASH_MACRONIX) || \
  244. defined(CONFIG_SPI_FLASH_SST)
  245. spi_flash_cmd_write_status(flash, 0);
  246. #endif
  247. return 0;
  248. }
  249. #if CONFIG_IS_ENABLED(OF_CONTROL)
  250. int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
  251. {
  252. fdt_addr_t addr;
  253. fdt_size_t size;
  254. int node;
  255. /* If there is no node, do nothing */
  256. node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
  257. if (node < 0)
  258. return 0;
  259. addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
  260. if (addr == FDT_ADDR_T_NONE) {
  261. debug("%s: Cannot decode address\n", __func__);
  262. return 0;
  263. }
  264. if (flash->size != size) {
  265. debug("%s: Memory map must cover entire device\n", __func__);
  266. return -1;
  267. }
  268. flash->memory_map = map_sysmem(addr, size);
  269. return 0;
  270. }
  271. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  272. /**
  273. * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
  274. *
  275. * @spi: Bus to probe
  276. * @flashp: Pointer to place to put flash info, which may be NULL if the
  277. * space should be allocated
  278. */
  279. int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash)
  280. {
  281. u8 idcode[5];
  282. int ret;
  283. /* Setup spi_slave */
  284. if (!spi) {
  285. printf("SF: Failed to set up slave\n");
  286. return -ENODEV;
  287. }
  288. /* Claim spi bus */
  289. ret = spi_claim_bus(spi);
  290. if (ret) {
  291. debug("SF: Failed to claim SPI bus: %d\n", ret);
  292. return ret;
  293. }
  294. /* Read the ID codes */
  295. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  296. if (ret) {
  297. printf("SF: Failed to get idcodes\n");
  298. goto err_read_id;
  299. }
  300. #ifdef DEBUG
  301. printf("SF: Got idcodes\n");
  302. print_buffer(0, idcode, 1, sizeof(idcode), 0);
  303. #endif
  304. if (spi_flash_validate_params(spi, idcode, flash)) {
  305. ret = -EINVAL;
  306. goto err_read_id;
  307. }
  308. /* Set the quad enable bit - only for quad commands */
  309. if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
  310. (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
  311. (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
  312. if (spi_flash_set_qeb(flash, idcode[0])) {
  313. debug("SF: Fail to set QEB for %02x\n", idcode[0]);
  314. ret = -EINVAL;
  315. goto err_read_id;
  316. }
  317. }
  318. #if CONFIG_IS_ENABLED(OF_CONTROL)
  319. if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
  320. debug("SF: FDT decode error\n");
  321. ret = -EINVAL;
  322. goto err_read_id;
  323. }
  324. #endif
  325. #ifndef CONFIG_SPL_BUILD
  326. printf("SF: Detected %s with page size ", flash->name);
  327. print_size(flash->page_size, ", erase size ");
  328. print_size(flash->erase_size, ", total ");
  329. print_size(flash->size, "");
  330. if (flash->memory_map)
  331. printf(", mapped at %p", flash->memory_map);
  332. puts("\n");
  333. #endif
  334. #ifndef CONFIG_SPI_FLASH_BAR
  335. if (((flash->dual_flash == SF_SINGLE_FLASH) &&
  336. (flash->size > SPI_FLASH_16MB_BOUN)) ||
  337. ((flash->dual_flash > SF_SINGLE_FLASH) &&
  338. (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
  339. puts("SF: Warning - Only lower 16MiB accessible,");
  340. puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
  341. }
  342. #endif
  343. #ifdef CONFIG_SPI_FLASH_MTD
  344. ret = spi_flash_mtd_register(flash);
  345. #endif
  346. err_read_id:
  347. spi_release_bus(spi);
  348. return ret;
  349. }
  350. #ifndef CONFIG_DM_SPI_FLASH
  351. struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
  352. {
  353. struct spi_flash *flash;
  354. /* Allocate space if needed (not used by sf-uclass */
  355. flash = calloc(1, sizeof(*flash));
  356. if (!flash) {
  357. debug("SF: Failed to allocate spi_flash\n");
  358. return NULL;
  359. }
  360. if (spi_flash_probe_slave(bus, flash)) {
  361. spi_free_slave(bus);
  362. free(flash);
  363. return NULL;
  364. }
  365. return flash;
  366. }
  367. struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
  368. unsigned int max_hz, unsigned int spi_mode)
  369. {
  370. struct spi_slave *bus;
  371. bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
  372. if (!bus)
  373. return NULL;
  374. return spi_flash_probe_tail(bus);
  375. }
  376. #ifdef CONFIG_OF_SPI_FLASH
  377. struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
  378. int spi_node)
  379. {
  380. struct spi_slave *bus;
  381. bus = spi_setup_slave_fdt(blob, slave_node, spi_node);
  382. if (!bus)
  383. return NULL;
  384. return spi_flash_probe_tail(bus);
  385. }
  386. #endif
  387. void spi_flash_free(struct spi_flash *flash)
  388. {
  389. #ifdef CONFIG_SPI_FLASH_MTD
  390. spi_flash_mtd_unregister();
  391. #endif
  392. spi_free_slave(flash->spi);
  393. free(flash);
  394. }
  395. #else /* defined CONFIG_DM_SPI_FLASH */
  396. static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
  397. void *buf)
  398. {
  399. struct spi_flash *flash = dev_get_uclass_priv(dev);
  400. return spi_flash_cmd_read_ops(flash, offset, len, buf);
  401. }
  402. int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
  403. const void *buf)
  404. {
  405. struct spi_flash *flash = dev_get_uclass_priv(dev);
  406. #if defined(CONFIG_SPI_FLASH_SST)
  407. if (flash->flags & SNOR_F_SST_WR) {
  408. if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
  409. return sst_write_bp(flash, offset, len, buf);
  410. else
  411. return sst_write_wp(flash, offset, len, buf);
  412. }
  413. #endif
  414. return spi_flash_cmd_write_ops(flash, offset, len, buf);
  415. }
  416. int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
  417. {
  418. struct spi_flash *flash = dev_get_uclass_priv(dev);
  419. return spi_flash_cmd_erase_ops(flash, offset, len);
  420. }
  421. int spi_flash_std_probe(struct udevice *dev)
  422. {
  423. struct spi_slave *slave = dev_get_parentdata(dev);
  424. struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
  425. struct spi_flash *flash;
  426. flash = dev_get_uclass_priv(dev);
  427. flash->dev = dev;
  428. debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs);
  429. return spi_flash_probe_slave(slave, flash);
  430. }
  431. static const struct dm_spi_flash_ops spi_flash_std_ops = {
  432. .read = spi_flash_std_read,
  433. .write = spi_flash_std_write,
  434. .erase = spi_flash_std_erase,
  435. };
  436. static const struct udevice_id spi_flash_std_ids[] = {
  437. { .compatible = "spi-flash" },
  438. { }
  439. };
  440. U_BOOT_DRIVER(spi_flash_std) = {
  441. .name = "spi_flash_std",
  442. .id = UCLASS_SPI_FLASH,
  443. .of_match = spi_flash_std_ids,
  444. .probe = spi_flash_std_probe,
  445. .priv_auto_alloc_size = sizeof(struct spi_flash),
  446. .ops = &spi_flash_std_ops,
  447. };
  448. #endif /* CONFIG_DM_SPI_FLASH */