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 #ifdef CONFIG_SPI_FLASH_MACRONIX
32 static int spi_flash_set_qeb_mxic(struct spi_flash *flash)
37 ret = spi_flash_cmd_read_status(flash, &qeb_status);
41 if (qeb_status & STATUS_QEB_MXIC) {
42 debug("SF: mxic: QEB is already set\n");
44 ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC);
53 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
54 static int spi_flash_set_qeb_winspan(struct spi_flash *flash)
59 ret = spi_flash_cmd_read_config(flash, &qeb_status);
63 if (qeb_status & STATUS_QEB_WINSPAN) {
64 debug("SF: winspan: QEB is already set\n");
66 ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN);
75 static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
78 #ifdef CONFIG_SPI_FLASH_MACRONIX
79 case SPI_FLASH_CFI_MFR_MACRONIX:
80 return spi_flash_set_qeb_mxic(flash);
82 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
83 case SPI_FLASH_CFI_MFR_SPANSION:
84 case SPI_FLASH_CFI_MFR_WINBOND:
85 return spi_flash_set_qeb_winspan(flash);
87 #ifdef CONFIG_SPI_FLASH_STMICRO
88 case SPI_FLASH_CFI_MFR_STMICRO:
89 debug("SF: QEB is volatile for %02x flash\n", idcode0);
93 printf("SF: Need set QEB func for %02x flash\n", idcode0);
98 static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
101 const struct spi_flash_params *params;
102 struct spi_flash *flash;
104 u16 jedec = idcode[1] << 8 | idcode[2];
105 u16 ext_jedec = idcode[3] << 8 | idcode[4];
107 params = spi_flash_params_table;
108 for (; params->name != NULL; params++) {
109 if ((params->jedec >> 16) == idcode[0]) {
110 if ((params->jedec & 0xFFFF) == jedec) {
111 if (params->ext_jedec == 0)
113 else if (params->ext_jedec == ext_jedec)
120 printf("SF: Unsupported flash IDs: ");
121 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
122 idcode[0], jedec, ext_jedec);
126 flash = malloc(sizeof(*flash));
128 debug("SF: Failed to allocate spi_flash\n");
131 memset(flash, '\0', sizeof(*flash));
133 /* Assign spi data */
135 flash->name = params->name;
136 flash->memory_map = spi->memory_map;
138 /* Assign spi_flash ops */
139 flash->write = spi_flash_cmd_write_ops;
140 #ifdef CONFIG_SPI_FLASH_SST
141 if (params->flags & SST_WP)
142 flash->write = sst_write_wp;
144 flash->erase = spi_flash_cmd_erase_ops;
145 flash->read = spi_flash_cmd_read_ops;
147 /* Compute the flash size */
148 flash->page_size = (ext_jedec == 0x4d00) ? 512 : 256;
149 flash->sector_size = params->sector_size;
150 flash->size = flash->sector_size * params->nr_sectors;
152 /* Compute erase sector and command */
153 if (params->flags & SECT_4K) {
154 flash->erase_cmd = CMD_ERASE_4K;
155 flash->erase_size = 4096;
156 } else if (params->flags & SECT_32K) {
157 flash->erase_cmd = CMD_ERASE_32K;
158 flash->erase_size = 32768;
160 flash->erase_cmd = CMD_ERASE_64K;
161 flash->erase_size = flash->sector_size;
164 /* Look for the fastest read cmd */
165 cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
167 cmd = spi_read_cmds_array[cmd - 1];
168 flash->read_cmd = cmd;
170 /* Go for default supported read cmd */
171 flash->read_cmd = CMD_READ_ARRAY_FAST;
174 /* Not require to look for fastest only two write cmds yet */
175 if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
176 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
178 /* Go for default supported write cmd */
179 flash->write_cmd = CMD_PAGE_PROGRAM;
181 /* Set the quad enable bit - only for quad commands */
182 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
183 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
184 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
185 if (spi_flash_set_qeb(flash, idcode[0])) {
186 debug("SF: Fail to set QEB for %02x\n", idcode[0]);
191 /* Read dummy_byte: dummy byte is determined based on the
192 * dummy cycles of a particular command.
193 * Fast commands - dummy_byte = dummy_cycles/8
194 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
195 * For I/O commands except cmd[0] everything goes on no.of lines
196 * based on particular command but incase of fast commands except
197 * data all go on single line irrespective of command.
199 switch (flash->read_cmd) {
200 case CMD_READ_QUAD_IO_FAST:
201 flash->dummy_byte = 2;
203 case CMD_READ_ARRAY_SLOW:
204 flash->dummy_byte = 0;
207 flash->dummy_byte = 1;
210 /* Poll cmd selection */
211 flash->poll_cmd = CMD_READ_STATUS;
212 #ifdef CONFIG_SPI_FLASH_STMICRO
213 if (params->flags & E_FSR)
214 flash->poll_cmd = CMD_FLAG_STATUS;
217 /* Configure the BAR - discover bank cmds and read current bank */
218 #ifdef CONFIG_SPI_FLASH_BAR
220 if (flash->size > SPI_FLASH_16MB_BOUN) {
221 flash->bank_read_cmd = (idcode[0] == 0x01) ?
222 CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR;
223 flash->bank_write_cmd = (idcode[0] == 0x01) ?
224 CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR;
226 if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
228 debug("SF: fail to read bank addr register\n");
231 flash->bank_curr = curr_bank;
233 flash->bank_curr = curr_bank;
237 /* Flash powers up read-only, so clear BP# bits */
238 #if defined(CONFIG_SPI_FLASH_ATMEL) || \
239 defined(CONFIG_SPI_FLASH_MACRONIX) || \
240 defined(CONFIG_SPI_FLASH_SST)
241 spi_flash_cmd_write_status(flash, 0);
247 #ifdef CONFIG_OF_CONTROL
248 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
254 /* If there is no node, do nothing */
255 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
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__);
265 if (flash->size != size) {
266 debug("%s: Memory map must cover entire device\n", __func__);
269 flash->memory_map = map_sysmem(addr, size);
273 #endif /* CONFIG_OF_CONTROL */
275 static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi)
277 struct spi_flash *flash = NULL;
281 /* Setup spi_slave */
283 printf("SF: Failed to set up slave\n");
288 ret = spi_claim_bus(spi);
290 debug("SF: Failed to claim SPI bus: %d\n", ret);
294 /* Read the ID codes */
295 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
297 printf("SF: Failed to get idcodes\n");
302 printf("SF: Got idcodes\n");
303 print_buffer(0, idcode, 1, sizeof(idcode), 0);
306 /* Validate params from spi_flash_params table */
307 flash = spi_flash_validate_params(spi, idcode);
311 #ifdef CONFIG_OF_CONTROL
312 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
313 debug("SF: FDT decode error\n");
317 #ifndef CONFIG_SPL_BUILD
318 printf("SF: Detected %s with page size ", flash->name);
319 print_size(flash->page_size, ", erase size ");
320 print_size(flash->erase_size, ", total ");
321 print_size(flash->size, "");
322 if (flash->memory_map)
323 printf(", mapped at %p", flash->memory_map);
326 #ifndef CONFIG_SPI_FLASH_BAR
327 if (flash->size > SPI_FLASH_16MB_BOUN) {
328 puts("SF: Warning - Only lower 16MiB accessible,");
329 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
333 /* Release spi bus */
334 spi_release_bus(spi);
339 spi_release_bus(spi);
345 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
346 unsigned int max_hz, unsigned int spi_mode)
348 struct spi_slave *spi;
350 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
351 return spi_flash_probe_slave(spi);
354 #ifdef CONFIG_OF_SPI_FLASH
355 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
358 struct spi_slave *spi;
360 spi = spi_setup_slave_fdt(blob, slave_node, spi_node);
361 return spi_flash_probe_slave(spi);
365 void spi_flash_free(struct spi_flash *flash)
367 spi_free_slave(flash->spi);