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 11 #include <common.h> 12 #include <fdtdec.h> 13 #include <malloc.h> 14 #include <spi.h> 15 #include <spi_flash.h> 16 #include <asm/io.h> 17 18 #include "sf_internal.h" 19 20 DECLARE_GLOBAL_DATA_PTR; 21 22 /* Read commands array */ 23 static u8 spi_read_cmds_array[] = { 24 CMD_READ_ARRAY_SLOW, 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 31 #ifdef CONFIG_SPI_FLASH_MACRONIX 32 static int spi_flash_set_qeb_mxic(struct spi_flash *flash) 33 { 34 u8 qeb_status; 35 int ret; 36 37 ret = spi_flash_cmd_read_status(flash, &qeb_status); 38 if (ret < 0) 39 return ret; 40 41 if (qeb_status & STATUS_QEB_MXIC) { 42 debug("SF: mxic: QEB is already set\n"); 43 } else { 44 ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC); 45 if (ret < 0) 46 return ret; 47 } 48 49 return ret; 50 } 51 #endif 52 53 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) 54 static int spi_flash_set_qeb_winspan(struct spi_flash *flash) 55 { 56 u8 qeb_status; 57 int ret; 58 59 ret = spi_flash_cmd_read_config(flash, &qeb_status); 60 if (ret < 0) 61 return ret; 62 63 if (qeb_status & STATUS_QEB_WINSPAN) { 64 debug("SF: winspan: QEB is already set\n"); 65 } else { 66 ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN); 67 if (ret < 0) 68 return ret; 69 } 70 71 return ret; 72 } 73 #endif 74 75 static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0) 76 { 77 switch (idcode0) { 78 #ifdef CONFIG_SPI_FLASH_MACRONIX 79 case SPI_FLASH_CFI_MFR_MACRONIX: 80 return spi_flash_set_qeb_mxic(flash); 81 #endif 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); 86 #endif 87 #ifdef CONFIG_SPI_FLASH_STMICRO 88 case SPI_FLASH_CFI_MFR_STMICRO: 89 debug("SF: QEB is volatile for %02x flash\n", idcode0); 90 return 0; 91 #endif 92 default: 93 printf("SF: Need set QEB func for %02x flash\n", idcode0); 94 return -1; 95 } 96 } 97 98 static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi, 99 u8 *idcode) 100 { 101 const struct spi_flash_params *params; 102 struct spi_flash *flash; 103 u8 cmd; 104 u16 jedec = idcode[1] << 8 | idcode[2]; 105 u16 ext_jedec = idcode[3] << 8 | idcode[4]; 106 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) 112 break; 113 else if (params->ext_jedec == ext_jedec) 114 break; 115 } 116 } 117 } 118 119 if (!params->name) { 120 printf("SF: Unsupported flash IDs: "); 121 printf("manuf %02x, jedec %04x, ext_jedec %04x\n", 122 idcode[0], jedec, ext_jedec); 123 return NULL; 124 } 125 126 flash = malloc(sizeof(*flash)); 127 if (!flash) { 128 debug("SF: Failed to allocate spi_flash\n"); 129 return NULL; 130 } 131 memset(flash, '\0', sizeof(*flash)); 132 133 /* Assign spi data */ 134 flash->spi = spi; 135 flash->name = params->name; 136 flash->memory_map = spi->memory_map; 137 flash->dual_flash = flash->spi->option; 138 139 /* Assign spi_flash ops */ 140 flash->write = spi_flash_cmd_write_ops; 141 #ifdef CONFIG_SPI_FLASH_SST 142 if (params->flags & SST_WP) 143 flash->write = sst_write_wp; 144 #endif 145 flash->erase = spi_flash_cmd_erase_ops; 146 flash->read = spi_flash_cmd_read_ops; 147 148 /* Compute the flash size */ 149 flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0; 150 flash->page_size = ((ext_jedec == 0x4d00) ? 512 : 256) << flash->shift; 151 flash->sector_size = params->sector_size << flash->shift; 152 flash->size = flash->sector_size * params->nr_sectors << flash->shift; 153 #ifdef CONFIG_SF_DUAL_FLASH 154 if (flash->dual_flash & SF_DUAL_STACKED_FLASH) 155 flash->size <<= 1; 156 #endif 157 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 170 /* Look for the fastest read cmd */ 171 cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx); 172 if (cmd) { 173 cmd = spi_read_cmds_array[cmd - 1]; 174 flash->read_cmd = cmd; 175 } else { 176 /* Go for default supported read cmd */ 177 flash->read_cmd = CMD_READ_ARRAY_FAST; 178 } 179 180 /* Not require to look for fastest only two write cmds yet */ 181 if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP) 182 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM; 183 else 184 /* Go for default supported write cmd */ 185 flash->write_cmd = CMD_PAGE_PROGRAM; 186 187 /* Set the quad enable bit - only for quad commands */ 188 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) || 189 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) || 190 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) { 191 if (spi_flash_set_qeb(flash, idcode[0])) { 192 debug("SF: Fail to set QEB for %02x\n", idcode[0]); 193 return NULL; 194 } 195 } 196 197 /* Read dummy_byte: dummy byte is determined based on the 198 * dummy cycles of a particular command. 199 * Fast commands - dummy_byte = dummy_cycles/8 200 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8 201 * For I/O commands except cmd[0] everything goes on no.of lines 202 * based on particular command but incase of fast commands except 203 * data all go on single line irrespective of command. 204 */ 205 switch (flash->read_cmd) { 206 case CMD_READ_QUAD_IO_FAST: 207 flash->dummy_byte = 2; 208 break; 209 case CMD_READ_ARRAY_SLOW: 210 flash->dummy_byte = 0; 211 break; 212 default: 213 flash->dummy_byte = 1; 214 } 215 216 /* Poll cmd selection */ 217 flash->poll_cmd = CMD_READ_STATUS; 218 #ifdef CONFIG_SPI_FLASH_STMICRO 219 if (params->flags & E_FSR) 220 flash->poll_cmd = CMD_FLAG_STATUS; 221 #endif 222 223 /* Configure the BAR - discover bank cmds and read current bank */ 224 #ifdef CONFIG_SPI_FLASH_BAR 225 u8 curr_bank = 0; 226 if (flash->size > SPI_FLASH_16MB_BOUN) { 227 flash->bank_read_cmd = (idcode[0] == 0x01) ? 228 CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR; 229 flash->bank_write_cmd = (idcode[0] == 0x01) ? 230 CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR; 231 232 if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1, 233 &curr_bank, 1)) { 234 debug("SF: fail to read bank addr register\n"); 235 return NULL; 236 } 237 flash->bank_curr = curr_bank; 238 } else { 239 flash->bank_curr = curr_bank; 240 } 241 #endif 242 243 /* Flash powers up read-only, so clear BP# bits */ 244 #if defined(CONFIG_SPI_FLASH_ATMEL) || \ 245 defined(CONFIG_SPI_FLASH_MACRONIX) || \ 246 defined(CONFIG_SPI_FLASH_SST) 247 spi_flash_cmd_write_status(flash, 0); 248 #endif 249 250 return flash; 251 } 252 253 #ifdef CONFIG_OF_CONTROL 254 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash) 255 { 256 fdt_addr_t addr; 257 fdt_size_t size; 258 int node; 259 260 /* If there is no node, do nothing */ 261 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH); 262 if (node < 0) 263 return 0; 264 265 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size); 266 if (addr == FDT_ADDR_T_NONE) { 267 debug("%s: Cannot decode address\n", __func__); 268 return 0; 269 } 270 271 if (flash->size != size) { 272 debug("%s: Memory map must cover entire device\n", __func__); 273 return -1; 274 } 275 flash->memory_map = map_sysmem(addr, size); 276 277 return 0; 278 } 279 #endif /* CONFIG_OF_CONTROL */ 280 281 static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi) 282 { 283 struct spi_flash *flash = NULL; 284 u8 idcode[5]; 285 int ret; 286 287 /* Setup spi_slave */ 288 if (!spi) { 289 printf("SF: Failed to set up slave\n"); 290 return NULL; 291 } 292 293 /* Claim spi bus */ 294 ret = spi_claim_bus(spi); 295 if (ret) { 296 debug("SF: Failed to claim SPI bus: %d\n", ret); 297 goto err_claim_bus; 298 } 299 300 /* Read the ID codes */ 301 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); 302 if (ret) { 303 printf("SF: Failed to get idcodes\n"); 304 goto err_read_id; 305 } 306 307 #ifdef DEBUG 308 printf("SF: Got idcodes\n"); 309 print_buffer(0, idcode, 1, sizeof(idcode), 0); 310 #endif 311 312 /* Validate params from spi_flash_params table */ 313 flash = spi_flash_validate_params(spi, idcode); 314 if (!flash) 315 goto err_read_id; 316 317 #ifdef CONFIG_OF_CONTROL 318 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) { 319 debug("SF: FDT decode error\n"); 320 goto err_read_id; 321 } 322 #endif 323 #ifndef CONFIG_SPL_BUILD 324 printf("SF: Detected %s with page size ", flash->name); 325 print_size(flash->page_size, ", erase size "); 326 print_size(flash->erase_size, ", total "); 327 print_size(flash->size, ""); 328 if (flash->memory_map) 329 printf(", mapped at %p", flash->memory_map); 330 puts("\n"); 331 #endif 332 #ifndef CONFIG_SPI_FLASH_BAR 333 if (((flash->dual_flash == SF_SINGLE_FLASH) && 334 (flash->size > SPI_FLASH_16MB_BOUN)) || 335 ((flash->dual_flash > SF_SINGLE_FLASH) && 336 (flash->size > SPI_FLASH_16MB_BOUN << 1))) { 337 puts("SF: Warning - Only lower 16MiB accessible,"); 338 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n"); 339 } 340 #endif 341 342 /* Release spi bus */ 343 spi_release_bus(spi); 344 345 return flash; 346 347 err_read_id: 348 spi_release_bus(spi); 349 err_claim_bus: 350 spi_free_slave(spi); 351 return NULL; 352 } 353 354 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, 355 unsigned int max_hz, unsigned int spi_mode) 356 { 357 struct spi_slave *spi; 358 359 spi = spi_setup_slave(bus, cs, max_hz, spi_mode); 360 return spi_flash_probe_slave(spi); 361 } 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 *spi; 368 369 spi = spi_setup_slave_fdt(blob, slave_node, spi_node); 370 return spi_flash_probe_slave(spi); 371 } 372 #endif 373 374 void spi_flash_free(struct spi_flash *flash) 375 { 376 spi_free_slave(flash->spi); 377 free(flash); 378 } 379