4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
8 * SPDX-License-Identifier: GPL-2.0+
15 #include <spi_flash.h>
18 #include "sf_internal.h"
20 DECLARE_GLOBAL_DATA_PTR;
22 /* Read commands array */
23 static u8 spi_read_cmds_array[] = {
25 CMD_READ_DUAL_OUTPUT_FAST,
26 CMD_READ_DUAL_IO_FAST,
27 CMD_READ_QUAD_OUTPUT_FAST,
28 CMD_READ_QUAD_IO_FAST,
31 static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
34 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
35 case SPI_FLASH_CFI_MFR_SPANSION:
36 case SPI_FLASH_CFI_MFR_WINBOND:
37 return spi_flash_set_qeb_winspan(flash);
39 #ifdef CONFIG_SPI_FLASH_STMICRO
40 case SPI_FLASH_CFI_MFR_STMICRO:
41 debug("SF: QEB is volatile for %02x flash\n", idcode0);
45 printf("SF: Need set QEB func for %02x flash\n", idcode0);
50 static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
53 const struct spi_flash_params *params;
54 struct spi_flash *flash;
56 u16 jedec = idcode[1] << 8 | idcode[2];
57 u16 ext_jedec = idcode[3] << 8 | idcode[4];
59 params = spi_flash_params_table;
60 for (; params->name != NULL; params++) {
61 if ((params->jedec >> 16) == idcode[0]) {
62 if ((params->jedec & 0xFFFF) == jedec) {
63 if (params->ext_jedec == 0)
65 else if (params->ext_jedec == ext_jedec)
72 printf("SF: Unsupported flash IDs: ");
73 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
74 idcode[0], jedec, ext_jedec);
78 flash = malloc(sizeof(*flash));
80 debug("SF: Failed to allocate spi_flash\n");
83 memset(flash, '\0', sizeof(*flash));
87 flash->name = params->name;
88 flash->memory_map = spi->memory_map;
90 /* Assign spi_flash ops */
91 flash->write = spi_flash_cmd_write_ops;
92 #ifdef CONFIG_SPI_FLASH_SST
93 if (params->flags & SST_WP)
94 flash->write = sst_write_wp;
96 flash->erase = spi_flash_cmd_erase_ops;
97 flash->read = spi_flash_cmd_read_ops;
99 /* Compute the flash size */
100 flash->page_size = (ext_jedec == 0x4d00) ? 512 : 256;
101 flash->sector_size = params->sector_size;
102 flash->size = flash->sector_size * params->nr_sectors;
104 /* Compute erase sector and command */
105 if (params->flags & SECT_4K) {
106 flash->erase_cmd = CMD_ERASE_4K;
107 flash->erase_size = 4096;
108 } else if (params->flags & SECT_32K) {
109 flash->erase_cmd = CMD_ERASE_32K;
110 flash->erase_size = 32768;
112 flash->erase_cmd = CMD_ERASE_64K;
113 flash->erase_size = flash->sector_size;
116 /* Look for the fastest read cmd */
117 cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
119 cmd = spi_read_cmds_array[cmd - 1];
120 flash->read_cmd = cmd;
122 /* Go for for default supported read cmd */
123 flash->read_cmd = CMD_READ_ARRAY_FAST;
126 /* Not require to look for fastest only two write cmds yet */
127 if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
128 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
130 /* Go for default supported write cmd */
131 flash->write_cmd = CMD_PAGE_PROGRAM;
133 /* Set the quad enable bit - only for quad commands */
134 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
135 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
136 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
137 if (spi_flash_set_qeb(flash, idcode[0])) {
138 debug("SF: Fail to set QEB for %02x\n", idcode[0]);
143 /* Poll cmd seclection */
144 flash->poll_cmd = CMD_READ_STATUS;
145 #ifdef CONFIG_SPI_FLASH_STMICRO
146 if (params->flags & E_FSR)
147 flash->poll_cmd = CMD_FLAG_STATUS;
150 /* Configure the BAR - discover bank cmds and read current bank */
151 #ifdef CONFIG_SPI_FLASH_BAR
153 if (flash->size > SPI_FLASH_16MB_BOUN) {
154 flash->bank_read_cmd = (idcode[0] == 0x01) ?
155 CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR;
156 flash->bank_write_cmd = (idcode[0] == 0x01) ?
157 CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR;
159 if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
161 debug("SF: fail to read bank addr register\n");
164 flash->bank_curr = curr_bank;
166 flash->bank_curr = curr_bank;
170 /* Flash powers up read-only, so clear BP# bits */
171 #if defined(CONFIG_SPI_FLASH_ATMEL) || \
172 defined(CONFIG_SPI_FLASH_MACRONIX) || \
173 defined(CONFIG_SPI_FLASH_SST)
174 spi_flash_cmd_write_status(flash, 0);
180 #ifdef CONFIG_OF_CONTROL
181 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
187 /* If there is no node, do nothing */
188 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
192 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
193 if (addr == FDT_ADDR_T_NONE) {
194 debug("%s: Cannot decode address\n", __func__);
198 if (flash->size != size) {
199 debug("%s: Memory map must cover entire device\n", __func__);
202 flash->memory_map = map_sysmem(addr, size);
206 #endif /* CONFIG_OF_CONTROL */
208 static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi)
210 struct spi_flash *flash = NULL;
214 /* Setup spi_slave */
216 printf("SF: Failed to set up slave\n");
221 ret = spi_claim_bus(spi);
223 debug("SF: Failed to claim SPI bus: %d\n", ret);
227 /* Read the ID codes */
228 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
230 printf("SF: Failed to get idcodes\n");
235 printf("SF: Got idcodes\n");
236 print_buffer(0, idcode, 1, sizeof(idcode), 0);
239 /* Validate params from spi_flash_params table */
240 flash = spi_flash_validate_params(spi, idcode);
244 #ifdef CONFIG_OF_CONTROL
245 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
246 debug("SF: FDT decode error\n");
250 #ifndef CONFIG_SPL_BUILD
251 printf("SF: Detected %s with page size ", flash->name);
252 print_size(flash->page_size, ", erase size ");
253 print_size(flash->erase_size, ", total ");
254 print_size(flash->size, "");
255 if (flash->memory_map)
256 printf(", mapped at %p", flash->memory_map);
259 #ifndef CONFIG_SPI_FLASH_BAR
260 if (flash->size > SPI_FLASH_16MB_BOUN) {
261 puts("SF: Warning - Only lower 16MiB accessible,");
262 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
266 /* Release spi bus */
267 spi_release_bus(spi);
272 spi_release_bus(spi);
278 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
279 unsigned int max_hz, unsigned int spi_mode)
281 struct spi_slave *spi;
283 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
284 return spi_flash_probe_slave(spi);
287 #ifdef CONFIG_OF_SPI_FLASH
288 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
291 struct spi_slave *spi;
293 spi = spi_setup_slave_fdt(blob, slave_node, spi_node);
294 return spi_flash_probe_slave(spi);
298 void spi_flash_free(struct spi_flash *flash)
300 spi_free_slave(flash->spi);