sf_probe.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 <fdtdec.h>
  12. #include <malloc.h>
  13. #include <spi.h>
  14. #include <spi_flash.h>
  15. #include <asm/io.h>
  16. #include "sf_internal.h"
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /* Read commands array */
  19. static u8 spi_read_cmds_array[] = {
  20. CMD_READ_ARRAY_SLOW,
  21. CMD_READ_DUAL_OUTPUT_FAST,
  22. CMD_READ_DUAL_IO_FAST,
  23. CMD_READ_QUAD_OUTPUT_FAST,
  24. CMD_READ_QUAD_IO_FAST,
  25. };
  26. static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
  27. {
  28. switch (idcode0) {
  29. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  30. case SPI_FLASH_CFI_MFR_SPANSION:
  31. case SPI_FLASH_CFI_MFR_WINBOND:
  32. return spi_flash_set_qeb_winspan(flash);
  33. #endif
  34. #ifdef CONFIG_SPI_FLASH_STMICRO
  35. case SPI_FLASH_CFI_MFR_STMICRO:
  36. debug("SF: QEB is volatile for %02x flash\n", idcode0);
  37. return 0;
  38. #endif
  39. default:
  40. printf("SF: Need set QEB func for %02x flash\n", idcode0);
  41. return -1;
  42. }
  43. }
  44. static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
  45. u8 *idcode)
  46. {
  47. const struct spi_flash_params *params;
  48. struct spi_flash *flash;
  49. u8 cmd;
  50. u16 jedec = idcode[1] << 8 | idcode[2];
  51. u16 ext_jedec = idcode[3] << 8 | idcode[4];
  52. params = spi_flash_params_table;
  53. for (; params->name != NULL; params++) {
  54. if ((params->jedec >> 16) == idcode[0]) {
  55. if ((params->jedec & 0xFFFF) == jedec) {
  56. if (params->ext_jedec == 0)
  57. break;
  58. else if (params->ext_jedec == ext_jedec)
  59. break;
  60. }
  61. }
  62. }
  63. if (!params->name) {
  64. printf("SF: Unsupported flash IDs: ");
  65. printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
  66. idcode[0], jedec, ext_jedec);
  67. return NULL;
  68. }
  69. flash = malloc(sizeof(*flash));
  70. if (!flash) {
  71. debug("SF: Failed to allocate spi_flash\n");
  72. return NULL;
  73. }
  74. memset(flash, '\0', sizeof(*flash));
  75. /* Assign spi data */
  76. flash->spi = spi;
  77. flash->name = params->name;
  78. flash->memory_map = spi->memory_map;
  79. /* Assign spi_flash ops */
  80. flash->write = spi_flash_cmd_write_ops;
  81. #ifdef CONFIG_SPI_FLASH_SST
  82. if (params->flags & SST_WP)
  83. flash->write = sst_write_wp;
  84. #endif
  85. flash->erase = spi_flash_cmd_erase_ops;
  86. flash->read = spi_flash_cmd_read_ops;
  87. /* Compute the flash size */
  88. flash->page_size = (ext_jedec == 0x4d00) ? 512 : 256;
  89. flash->sector_size = params->sector_size;
  90. flash->size = flash->sector_size * params->nr_sectors;
  91. /* Compute erase sector and command */
  92. if (params->flags & SECT_4K) {
  93. flash->erase_cmd = CMD_ERASE_4K;
  94. flash->erase_size = 4096;
  95. } else if (params->flags & SECT_32K) {
  96. flash->erase_cmd = CMD_ERASE_32K;
  97. flash->erase_size = 32768;
  98. } else {
  99. flash->erase_cmd = CMD_ERASE_64K;
  100. flash->erase_size = flash->sector_size;
  101. }
  102. /* Look for the fastest read cmd */
  103. cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
  104. if (cmd) {
  105. cmd = spi_read_cmds_array[cmd - 1];
  106. flash->read_cmd = cmd;
  107. } else {
  108. /* Go for for default supported read cmd */
  109. flash->read_cmd = CMD_READ_ARRAY_FAST;
  110. }
  111. /* Not require to look for fastest only two write cmds yet */
  112. if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
  113. flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
  114. else
  115. /* Go for default supported write cmd */
  116. flash->write_cmd = CMD_PAGE_PROGRAM;
  117. /* Set the quad enable bit - only for quad commands */
  118. if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
  119. (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
  120. (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
  121. if (spi_flash_set_qeb(flash, idcode[0])) {
  122. debug("SF: Fail to set QEB for %02x\n", idcode[0]);
  123. return NULL;
  124. }
  125. }
  126. /* Poll cmd seclection */
  127. flash->poll_cmd = CMD_READ_STATUS;
  128. #ifdef CONFIG_SPI_FLASH_STMICRO
  129. if (params->flags & E_FSR)
  130. flash->poll_cmd = CMD_FLAG_STATUS;
  131. #endif
  132. /* Configure the BAR - discover bank cmds and read current bank */
  133. #ifdef CONFIG_SPI_FLASH_BAR
  134. u8 curr_bank = 0;
  135. if (flash->size > SPI_FLASH_16MB_BOUN) {
  136. flash->bank_read_cmd = (idcode[0] == 0x01) ?
  137. CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR;
  138. flash->bank_write_cmd = (idcode[0] == 0x01) ?
  139. CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR;
  140. if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
  141. &curr_bank, 1)) {
  142. debug("SF: fail to read bank addr register\n");
  143. return NULL;
  144. }
  145. flash->bank_curr = curr_bank;
  146. } else {
  147. flash->bank_curr = curr_bank;
  148. }
  149. #endif
  150. /* Flash powers up read-only, so clear BP# bits */
  151. #if defined(CONFIG_SPI_FLASH_ATMEL) || \
  152. defined(CONFIG_SPI_FLASH_MACRONIX) || \
  153. defined(CONFIG_SPI_FLASH_SST)
  154. spi_flash_cmd_write_status(flash, 0);
  155. #endif
  156. return flash;
  157. }
  158. #ifdef CONFIG_OF_CONTROL
  159. int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
  160. {
  161. fdt_addr_t addr;
  162. fdt_size_t size;
  163. int node;
  164. /* If there is no node, do nothing */
  165. node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
  166. if (node < 0)
  167. return 0;
  168. addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
  169. if (addr == FDT_ADDR_T_NONE) {
  170. debug("%s: Cannot decode address\n", __func__);
  171. return 0;
  172. }
  173. if (flash->size != size) {
  174. debug("%s: Memory map must cover entire device\n", __func__);
  175. return -1;
  176. }
  177. flash->memory_map = map_sysmem(addr, size);
  178. return 0;
  179. }
  180. #endif /* CONFIG_OF_CONTROL */
  181. static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi)
  182. {
  183. struct spi_flash *flash = NULL;
  184. u8 idcode[5];
  185. int ret;
  186. /* Setup spi_slave */
  187. if (!spi) {
  188. printf("SF: Failed to set up slave\n");
  189. return NULL;
  190. }
  191. /* Claim spi bus */
  192. ret = spi_claim_bus(spi);
  193. if (ret) {
  194. debug("SF: Failed to claim SPI bus: %d\n", ret);
  195. goto err_claim_bus;
  196. }
  197. /* Read the ID codes */
  198. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  199. if (ret) {
  200. printf("SF: Failed to get idcodes\n");
  201. goto err_read_id;
  202. }
  203. #ifdef DEBUG
  204. printf("SF: Got idcodes\n");
  205. print_buffer(0, idcode, 1, sizeof(idcode), 0);
  206. #endif
  207. /* Validate params from spi_flash_params table */
  208. flash = spi_flash_validate_params(spi, idcode);
  209. if (!flash)
  210. goto err_read_id;
  211. #ifdef CONFIG_OF_CONTROL
  212. if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
  213. debug("SF: FDT decode error\n");
  214. goto err_read_id;
  215. }
  216. #endif
  217. #ifndef CONFIG_SPL_BUILD
  218. printf("SF: Detected %s with page size ", flash->name);
  219. print_size(flash->page_size, ", erase size ");
  220. print_size(flash->erase_size, ", total ");
  221. print_size(flash->size, "");
  222. if (flash->memory_map)
  223. printf(", mapped at %p", flash->memory_map);
  224. puts("\n");
  225. #endif
  226. #ifndef CONFIG_SPI_FLASH_BAR
  227. if (flash->size > SPI_FLASH_16MB_BOUN) {
  228. puts("SF: Warning - Only lower 16MiB accessible,");
  229. puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
  230. }
  231. #endif
  232. /* Release spi bus */
  233. spi_release_bus(spi);
  234. return flash;
  235. err_read_id:
  236. spi_release_bus(spi);
  237. err_claim_bus:
  238. spi_free_slave(spi);
  239. return NULL;
  240. }
  241. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  242. unsigned int max_hz, unsigned int spi_mode)
  243. {
  244. struct spi_slave *spi;
  245. spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
  246. return spi_flash_probe_slave(spi);
  247. }
  248. #ifdef CONFIG_OF_SPI_FLASH
  249. struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
  250. int spi_node)
  251. {
  252. struct spi_slave *spi;
  253. spi = spi_setup_slave_fdt(blob, slave_node, spi_node);
  254. return spi_flash_probe_slave(spi);
  255. }
  256. #endif
  257. void spi_flash_free(struct spi_flash *flash)
  258. {
  259. spi_free_slave(flash->spi);
  260. free(flash);
  261. }