Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 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 | * |
Jagannadha Sutradharudu Teki | 0c88a84 | 2013-10-10 22:32:55 +0530 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <fdtdec.h> |
| 13 | #include <malloc.h> |
| 14 | #include <spi.h> |
| 15 | #include <spi_flash.h> |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 16 | #include <asm/io.h> |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 17 | |
Jagannadha Sutradharudu Teki | 898e76c | 2013-09-26 16:00:15 +0530 | [diff] [blame] | 18 | #include "sf_internal.h" |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Jagannadha Sutradharudu Teki | 4e09cc1 | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 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, |
Jagannadha Sutradharudu Teki | 3163aaa | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 27 | CMD_READ_QUAD_OUTPUT_FAST, |
Jagannadha Sutradharudu Teki | c4ba0d8 | 2013-12-24 15:24:31 +0530 | [diff] [blame] | 28 | CMD_READ_QUAD_IO_FAST, |
Jagannadha Sutradharudu Teki | 4e09cc1 | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 29 | }; |
| 30 | |
Jagannadha Sutradharudu Teki | d08a1ba | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 31 | static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0) |
| 32 | { |
| 33 | switch (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); |
| 38 | #endif |
| 39 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 40 | case SPI_FLASH_CFI_MFR_STMICRO: |
| 41 | debug("SF: QEB is volatile for %02x flash\n", idcode0); |
| 42 | return 0; |
| 43 | #endif |
| 44 | default: |
| 45 | printf("SF: Need set QEB func for %02x flash\n", idcode0); |
| 46 | return -1; |
| 47 | } |
| 48 | } |
| 49 | |
Jagannadha Sutradharudu Teki | ce22b92 | 2013-10-07 19:34:56 +0530 | [diff] [blame] | 50 | static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi, |
| 51 | u8 *idcode) |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 52 | { |
| 53 | const struct spi_flash_params *params; |
| 54 | struct spi_flash *flash; |
Jagannadha Sutradharudu Teki | 4e09cc1 | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 55 | u8 cmd; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 56 | u16 jedec = idcode[1] << 8 | idcode[2]; |
Jagannadha Sutradharudu Teki | 74bec16 | 2013-09-28 16:49:37 +0530 | [diff] [blame] | 57 | u16 ext_jedec = idcode[3] << 8 | idcode[4]; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 58 | |
Jagannadha Sutradharudu Teki | 33adfb5 | 2013-12-23 23:34:42 +0530 | [diff] [blame] | 59 | params = spi_flash_params_table; |
| 60 | for (; params->name != NULL; params++) { |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 61 | if ((params->jedec >> 16) == idcode[0]) { |
Jagannadha Sutradharudu Teki | 74bec16 | 2013-09-28 16:49:37 +0530 | [diff] [blame] | 62 | if ((params->jedec & 0xFFFF) == jedec) { |
| 63 | if (params->ext_jedec == 0) |
| 64 | break; |
| 65 | else if (params->ext_jedec == ext_jedec) |
| 66 | break; |
| 67 | } |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Jagannadha Sutradharudu Teki | 33adfb5 | 2013-12-23 23:34:42 +0530 | [diff] [blame] | 71 | if (!params->name) { |
Jagannadha Sutradharudu Teki | 74bec16 | 2013-09-28 16:49:37 +0530 | [diff] [blame] | 72 | printf("SF: Unsupported flash IDs: "); |
| 73 | printf("manuf %02x, jedec %04x, ext_jedec %04x\n", |
| 74 | idcode[0], jedec, ext_jedec); |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | flash = malloc(sizeof(*flash)); |
| 79 | if (!flash) { |
| 80 | debug("SF: Failed to allocate spi_flash\n"); |
| 81 | return NULL; |
| 82 | } |
| 83 | memset(flash, '\0', sizeof(*flash)); |
| 84 | |
Jagannadha Sutradharudu Teki | 469146c | 2013-10-10 22:14:09 +0530 | [diff] [blame] | 85 | /* Assign spi data */ |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 86 | flash->spi = spi; |
| 87 | flash->name = params->name; |
Jagannadha Sutradharudu Teki | ce22b92 | 2013-10-07 19:34:56 +0530 | [diff] [blame] | 88 | flash->memory_map = spi->memory_map; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 89 | |
| 90 | /* Assign spi_flash ops */ |
Jagannadha Sutradharudu Teki | a5e8199 | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 91 | flash->write = spi_flash_cmd_write_ops; |
Jagannadha Sutradharudu Teki | 10ca45d | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 92 | #ifdef CONFIG_SPI_FLASH_SST |
| 93 | if (params->flags & SST_WP) |
| 94 | flash->write = sst_write_wp; |
| 95 | #endif |
Jagannadha Sutradharudu Teki | a5e8199 | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 96 | flash->erase = spi_flash_cmd_erase_ops; |
| 97 | flash->read = spi_flash_cmd_read_ops; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 98 | |
| 99 | /* Compute the flash size */ |
Jagannadha Sutradharudu Teki | f0be6de | 2013-08-28 12:17:56 +0530 | [diff] [blame] | 100 | flash->page_size = (ext_jedec == 0x4d00) ? 512 : 256; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 101 | flash->sector_size = params->sector_size; |
| 102 | flash->size = flash->sector_size * params->nr_sectors; |
| 103 | |
Jagannadha Sutradharudu Teki | f4f51a8 | 2013-10-02 19:36:58 +0530 | [diff] [blame] | 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; |
| 111 | } else { |
| 112 | flash->erase_cmd = CMD_ERASE_64K; |
| 113 | flash->erase_size = flash->sector_size; |
| 114 | } |
| 115 | |
Jagannadha Sutradharudu Teki | 4e09cc1 | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 116 | /* Look for the fastest read cmd */ |
| 117 | cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx); |
| 118 | if (cmd) { |
| 119 | cmd = spi_read_cmds_array[cmd - 1]; |
| 120 | flash->read_cmd = cmd; |
| 121 | } else { |
| 122 | /* Go for for default supported read cmd */ |
| 123 | flash->read_cmd = CMD_READ_ARRAY_FAST; |
| 124 | } |
| 125 | |
Jagannadha Sutradharudu Teki | 3163aaa | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 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; |
| 129 | else |
| 130 | /* Go for default supported write cmd */ |
| 131 | flash->write_cmd = CMD_PAGE_PROGRAM; |
| 132 | |
Jagannadha Sutradharudu Teki | d08a1ba | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 133 | /* Set the quad enable bit - only for quad commands */ |
| 134 | if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) || |
Jagannadha Sutradharudu Teki | c4ba0d8 | 2013-12-24 15:24:31 +0530 | [diff] [blame] | 135 | (flash->read_cmd == CMD_READ_QUAD_IO_FAST) || |
Jagannadha Sutradharudu Teki | d08a1ba | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 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]); |
| 139 | return NULL; |
| 140 | } |
| 141 | } |
| 142 | |
Jagannadha Sutradharudu Teki | ff063ed | 2014-01-11 16:50:45 +0530 | [diff] [blame^] | 143 | /* Read dummy_byte: dummy byte is determined based on the |
| 144 | * dummy cycles of a particular command. |
| 145 | * Fast commands - dummy_byte = dummy_cycles/8 |
| 146 | * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8 |
| 147 | * For I/O commands except cmd[0] everything goes on no.of lines |
| 148 | * based on particular command but incase of fast commands except |
| 149 | * data all go on single line irrespective of command. |
| 150 | */ |
| 151 | switch (flash->read_cmd) { |
| 152 | case CMD_READ_QUAD_IO_FAST: |
| 153 | flash->dummy_byte = 2; |
| 154 | break; |
| 155 | case CMD_READ_ARRAY_SLOW: |
| 156 | flash->dummy_byte = 0; |
| 157 | break; |
| 158 | default: |
| 159 | flash->dummy_byte = 1; |
| 160 | } |
| 161 | |
Jagannadha Sutradharudu Teki | 0f62328 | 2013-10-02 19:37:43 +0530 | [diff] [blame] | 162 | /* Poll cmd seclection */ |
| 163 | flash->poll_cmd = CMD_READ_STATUS; |
| 164 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 165 | if (params->flags & E_FSR) |
| 166 | flash->poll_cmd = CMD_FLAG_STATUS; |
| 167 | #endif |
| 168 | |
Jagannadha Sutradharudu Teki | ce22b92 | 2013-10-07 19:34:56 +0530 | [diff] [blame] | 169 | /* Configure the BAR - discover bank cmds and read current bank */ |
Jagannadha Sutradharudu Teki | 32ebd1a | 2013-09-26 14:24:58 +0530 | [diff] [blame] | 170 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagannadha Sutradharudu Teki | 32ebd1a | 2013-09-26 14:24:58 +0530 | [diff] [blame] | 171 | u8 curr_bank = 0; |
| 172 | if (flash->size > SPI_FLASH_16MB_BOUN) { |
| 173 | flash->bank_read_cmd = (idcode[0] == 0x01) ? |
| 174 | CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR; |
| 175 | flash->bank_write_cmd = (idcode[0] == 0x01) ? |
| 176 | CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR; |
| 177 | |
| 178 | if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1, |
| 179 | &curr_bank, 1)) { |
| 180 | debug("SF: fail to read bank addr register\n"); |
| 181 | return NULL; |
| 182 | } |
| 183 | flash->bank_curr = curr_bank; |
| 184 | } else { |
| 185 | flash->bank_curr = curr_bank; |
| 186 | } |
| 187 | #endif |
| 188 | |
Jagannadha Sutradharudu Teki | 54024c1 | 2013-09-26 13:16:45 +0530 | [diff] [blame] | 189 | /* Flash powers up read-only, so clear BP# bits */ |
| 190 | #if defined(CONFIG_SPI_FLASH_ATMEL) || \ |
| 191 | defined(CONFIG_SPI_FLASH_MACRONIX) || \ |
| 192 | defined(CONFIG_SPI_FLASH_SST) |
| 193 | spi_flash_cmd_write_status(flash, 0); |
| 194 | #endif |
| 195 | |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 196 | return flash; |
| 197 | } |
| 198 | |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 199 | #ifdef CONFIG_OF_CONTROL |
| 200 | int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash) |
| 201 | { |
| 202 | fdt_addr_t addr; |
| 203 | fdt_size_t size; |
| 204 | int node; |
| 205 | |
| 206 | /* If there is no node, do nothing */ |
| 207 | node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH); |
| 208 | if (node < 0) |
| 209 | return 0; |
| 210 | |
| 211 | addr = fdtdec_get_addr_size(blob, node, "memory-map", &size); |
| 212 | if (addr == FDT_ADDR_T_NONE) { |
| 213 | debug("%s: Cannot decode address\n", __func__); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | if (flash->size != size) { |
| 218 | debug("%s: Memory map must cover entire device\n", __func__); |
| 219 | return -1; |
| 220 | } |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 221 | flash->memory_map = map_sysmem(addr, size); |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | #endif /* CONFIG_OF_CONTROL */ |
| 226 | |
Simon Glass | 0efc024 | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 227 | static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi) |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 228 | { |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 229 | struct spi_flash *flash = NULL; |
Jagannadha Sutradharudu Teki | 32ebd1a | 2013-09-26 14:24:58 +0530 | [diff] [blame] | 230 | u8 idcode[5]; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 231 | int ret; |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 232 | |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 233 | /* Setup spi_slave */ |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 234 | if (!spi) { |
| 235 | printf("SF: Failed to set up slave\n"); |
| 236 | return NULL; |
| 237 | } |
| 238 | |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 239 | /* Claim spi bus */ |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 240 | ret = spi_claim_bus(spi); |
| 241 | if (ret) { |
| 242 | debug("SF: Failed to claim SPI bus: %d\n", ret); |
| 243 | goto err_claim_bus; |
| 244 | } |
| 245 | |
| 246 | /* Read the ID codes */ |
| 247 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 248 | if (ret) { |
| 249 | printf("SF: Failed to get idcodes\n"); |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 250 | goto err_read_id; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 251 | } |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 252 | |
| 253 | #ifdef DEBUG |
| 254 | printf("SF: Got idcodes\n"); |
| 255 | print_buffer(0, idcode, 1, sizeof(idcode), 0); |
| 256 | #endif |
| 257 | |
Jagannadha Sutradharudu Teki | a5e8199 | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 258 | /* Validate params from spi_flash_params table */ |
| 259 | flash = spi_flash_validate_params(spi, idcode); |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 260 | if (!flash) |
| 261 | goto err_read_id; |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 262 | |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 263 | #ifdef CONFIG_OF_CONTROL |
| 264 | if (spi_flash_decode_fdt(gd->fdt_blob, flash)) { |
| 265 | debug("SF: FDT decode error\n"); |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 266 | goto err_read_id; |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 267 | } |
| 268 | #endif |
| 269 | #ifndef CONFIG_SPL_BUILD |
| 270 | printf("SF: Detected %s with page size ", flash->name); |
Jagannadha Sutradharudu Teki | 3ea708f | 2013-09-15 23:06:12 +0530 | [diff] [blame] | 271 | print_size(flash->page_size, ", erase size "); |
| 272 | print_size(flash->erase_size, ", total "); |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 273 | print_size(flash->size, ""); |
| 274 | if (flash->memory_map) |
| 275 | printf(", mapped at %p", flash->memory_map); |
| 276 | puts("\n"); |
| 277 | #endif |
| 278 | #ifndef CONFIG_SPI_FLASH_BAR |
| 279 | if (flash->size > SPI_FLASH_16MB_BOUN) { |
| 280 | puts("SF: Warning - Only lower 16MiB accessible,"); |
| 281 | puts(" Full access #define CONFIG_SPI_FLASH_BAR\n"); |
| 282 | } |
| 283 | #endif |
| 284 | |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 285 | /* Release spi bus */ |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 286 | spi_release_bus(spi); |
| 287 | |
| 288 | return flash; |
| 289 | |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 290 | err_read_id: |
| 291 | spi_release_bus(spi); |
| 292 | err_claim_bus: |
| 293 | spi_free_slave(spi); |
| 294 | return NULL; |
| 295 | } |
| 296 | |
Simon Glass | 0efc024 | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 297 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 298 | unsigned int max_hz, unsigned int spi_mode) |
| 299 | { |
| 300 | struct spi_slave *spi; |
| 301 | |
| 302 | spi = spi_setup_slave(bus, cs, max_hz, spi_mode); |
| 303 | return spi_flash_probe_slave(spi); |
| 304 | } |
| 305 | |
| 306 | #ifdef CONFIG_OF_SPI_FLASH |
| 307 | struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node, |
| 308 | int spi_node) |
| 309 | { |
| 310 | struct spi_slave *spi; |
| 311 | |
| 312 | spi = spi_setup_slave_fdt(blob, slave_node, spi_node); |
| 313 | return spi_flash_probe_slave(spi); |
| 314 | } |
| 315 | #endif |
| 316 | |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 317 | void spi_flash_free(struct spi_flash *flash) |
| 318 | { |
| 319 | spi_free_slave(flash->spi); |
| 320 | free(flash); |
| 321 | } |