sf_probe.c 12 KB

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