sf_probe.c 12 KB

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