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 | 9f4322f | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 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 | |
Jagannadha Sutradharudu Teki | d08a1ba | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 75 | static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0) |
| 76 | { |
| 77 | switch (idcode0) { |
Jagannadha Sutradharudu Teki | 0679512 | 2013-12-26 14:13:36 +0530 | [diff] [blame] | 78 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
| 79 | case SPI_FLASH_CFI_MFR_MACRONIX: |
| 80 | return spi_flash_set_qeb_mxic(flash); |
| 81 | #endif |
Jagannadha Sutradharudu Teki | d08a1ba | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 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 | |
Jagannadha Sutradharudu Teki | ce22b92 | 2013-10-07 19:34:56 +0530 | [diff] [blame] | 98 | static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi, |
| 99 | u8 *idcode) |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 100 | { |
| 101 | const struct spi_flash_params *params; |
| 102 | struct spi_flash *flash; |
Jagannadha Sutradharudu Teki | 4e09cc1 | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 103 | u8 cmd; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 104 | u16 jedec = idcode[1] << 8 | idcode[2]; |
Jagannadha Sutradharudu Teki | 74bec16 | 2013-09-28 16:49:37 +0530 | [diff] [blame] | 105 | u16 ext_jedec = idcode[3] << 8 | idcode[4]; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 106 | |
Jagannadha Sutradharudu Teki | 33adfb5 | 2013-12-23 23:34:42 +0530 | [diff] [blame] | 107 | params = spi_flash_params_table; |
| 108 | for (; params->name != NULL; params++) { |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 109 | if ((params->jedec >> 16) == idcode[0]) { |
Jagannadha Sutradharudu Teki | 74bec16 | 2013-09-28 16:49:37 +0530 | [diff] [blame] | 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 | } |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Jagannadha Sutradharudu Teki | 33adfb5 | 2013-12-23 23:34:42 +0530 | [diff] [blame] | 119 | if (!params->name) { |
Jagannadha Sutradharudu Teki | 74bec16 | 2013-09-28 16:49:37 +0530 | [diff] [blame] | 120 | printf("SF: Unsupported flash IDs: "); |
| 121 | printf("manuf %02x, jedec %04x, ext_jedec %04x\n", |
| 122 | idcode[0], jedec, ext_jedec); |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 123 | return NULL; |
| 124 | } |
| 125 | |
Marek Vasut | cc56f13 | 2014-01-15 15:17:54 +0100 | [diff] [blame] | 126 | flash = calloc(1, sizeof(*flash)); |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 127 | if (!flash) { |
| 128 | debug("SF: Failed to allocate spi_flash\n"); |
| 129 | return NULL; |
| 130 | } |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 131 | |
Jagannadha Sutradharudu Teki | 469146c | 2013-10-10 22:14:09 +0530 | [diff] [blame] | 132 | /* Assign spi data */ |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 133 | flash->spi = spi; |
| 134 | flash->name = params->name; |
Jagannadha Sutradharudu Teki | ce22b92 | 2013-10-07 19:34:56 +0530 | [diff] [blame] | 135 | flash->memory_map = spi->memory_map; |
Jagannadha Sutradharudu Teki | f77f469 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 136 | flash->dual_flash = flash->spi->option; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 137 | |
| 138 | /* Assign spi_flash ops */ |
Jagannadha Sutradharudu Teki | a5e8199 | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 139 | flash->write = spi_flash_cmd_write_ops; |
Jagannadha Sutradharudu Teki | 10ca45d | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 140 | #ifdef CONFIG_SPI_FLASH_SST |
| 141 | if (params->flags & SST_WP) |
| 142 | flash->write = sst_write_wp; |
| 143 | #endif |
Jagannadha Sutradharudu Teki | a5e8199 | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 144 | flash->erase = spi_flash_cmd_erase_ops; |
| 145 | flash->read = spi_flash_cmd_read_ops; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 146 | |
| 147 | /* Compute the flash size */ |
Jagannadha Sutradharudu Teki | 056fbc7 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 148 | flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0; |
| 149 | flash->page_size = ((ext_jedec == 0x4d00) ? 512 : 256) << flash->shift; |
| 150 | flash->sector_size = params->sector_size << flash->shift; |
| 151 | flash->size = flash->sector_size * params->nr_sectors << flash->shift; |
Jagannadha Sutradharudu Teki | b902e07 | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 152 | #ifdef CONFIG_SF_DUAL_FLASH |
Jagannadha Sutradharudu Teki | f77f469 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 153 | if (flash->dual_flash & SF_DUAL_STACKED_FLASH) |
| 154 | flash->size <<= 1; |
Jagannadha Sutradharudu Teki | b902e07 | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 155 | #endif |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 156 | |
Jagannadha Sutradharudu Teki | f4f51a8 | 2013-10-02 19:36:58 +0530 | [diff] [blame] | 157 | /* Compute erase sector and command */ |
| 158 | if (params->flags & SECT_4K) { |
| 159 | flash->erase_cmd = CMD_ERASE_4K; |
Jagannadha Sutradharudu Teki | 056fbc7 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 160 | flash->erase_size = 4096 << flash->shift; |
Jagannadha Sutradharudu Teki | f4f51a8 | 2013-10-02 19:36:58 +0530 | [diff] [blame] | 161 | } else if (params->flags & SECT_32K) { |
| 162 | flash->erase_cmd = CMD_ERASE_32K; |
Jagannadha Sutradharudu Teki | 056fbc7 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 163 | flash->erase_size = 32768 << flash->shift; |
Jagannadha Sutradharudu Teki | f4f51a8 | 2013-10-02 19:36:58 +0530 | [diff] [blame] | 164 | } else { |
| 165 | flash->erase_cmd = CMD_ERASE_64K; |
| 166 | flash->erase_size = flash->sector_size; |
| 167 | } |
| 168 | |
Jagannadha Sutradharudu Teki | 4e09cc1 | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 169 | /* Look for the fastest read cmd */ |
| 170 | cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx); |
| 171 | if (cmd) { |
| 172 | cmd = spi_read_cmds_array[cmd - 1]; |
| 173 | flash->read_cmd = cmd; |
| 174 | } else { |
Jagannadha Sutradharudu Teki | 2ba863f | 2014-01-12 21:38:21 +0530 | [diff] [blame] | 175 | /* Go for default supported read cmd */ |
Jagannadha Sutradharudu Teki | 4e09cc1 | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 176 | flash->read_cmd = CMD_READ_ARRAY_FAST; |
| 177 | } |
| 178 | |
Jagannadha Sutradharudu Teki | 3163aaa | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 179 | /* Not require to look for fastest only two write cmds yet */ |
| 180 | if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP) |
| 181 | flash->write_cmd = CMD_QUAD_PAGE_PROGRAM; |
| 182 | else |
| 183 | /* Go for default supported write cmd */ |
| 184 | flash->write_cmd = CMD_PAGE_PROGRAM; |
| 185 | |
Jagannadha Sutradharudu Teki | d08a1ba | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 186 | /* Set the quad enable bit - only for quad commands */ |
| 187 | if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) || |
Jagannadha Sutradharudu Teki | c4ba0d8 | 2013-12-24 15:24:31 +0530 | [diff] [blame] | 188 | (flash->read_cmd == CMD_READ_QUAD_IO_FAST) || |
Jagannadha Sutradharudu Teki | d08a1ba | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 189 | (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) { |
| 190 | if (spi_flash_set_qeb(flash, idcode[0])) { |
| 191 | debug("SF: Fail to set QEB for %02x\n", idcode[0]); |
| 192 | return NULL; |
| 193 | } |
| 194 | } |
| 195 | |
Jagannadha Sutradharudu Teki | ff063ed | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 196 | /* Read dummy_byte: dummy byte is determined based on the |
| 197 | * dummy cycles of a particular command. |
| 198 | * Fast commands - dummy_byte = dummy_cycles/8 |
| 199 | * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8 |
| 200 | * For I/O commands except cmd[0] everything goes on no.of lines |
| 201 | * based on particular command but incase of fast commands except |
| 202 | * data all go on single line irrespective of command. |
| 203 | */ |
| 204 | switch (flash->read_cmd) { |
| 205 | case CMD_READ_QUAD_IO_FAST: |
| 206 | flash->dummy_byte = 2; |
| 207 | break; |
| 208 | case CMD_READ_ARRAY_SLOW: |
| 209 | flash->dummy_byte = 0; |
| 210 | break; |
| 211 | default: |
| 212 | flash->dummy_byte = 1; |
| 213 | } |
| 214 | |
Jagannadha Sutradharudu Teki | 2ba863f | 2014-01-12 21:38:21 +0530 | [diff] [blame] | 215 | /* Poll cmd selection */ |
Jagannadha Sutradharudu Teki | 0f62328 | 2013-10-02 19:37:43 +0530 | [diff] [blame] | 216 | flash->poll_cmd = CMD_READ_STATUS; |
| 217 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 218 | if (params->flags & E_FSR) |
| 219 | flash->poll_cmd = CMD_FLAG_STATUS; |
| 220 | #endif |
| 221 | |
Jagannadha Sutradharudu Teki | ce22b92 | 2013-10-07 19:34:56 +0530 | [diff] [blame] | 222 | /* Configure the BAR - discover bank cmds and read current bank */ |
Jagannadha Sutradharudu Teki | 32ebd1a | 2013-09-26 14:24:58 +0530 | [diff] [blame] | 223 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagannadha Sutradharudu Teki | 32ebd1a | 2013-09-26 14:24:58 +0530 | [diff] [blame] | 224 | u8 curr_bank = 0; |
| 225 | if (flash->size > SPI_FLASH_16MB_BOUN) { |
| 226 | flash->bank_read_cmd = (idcode[0] == 0x01) ? |
| 227 | CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR; |
| 228 | flash->bank_write_cmd = (idcode[0] == 0x01) ? |
| 229 | CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR; |
| 230 | |
| 231 | if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1, |
| 232 | &curr_bank, 1)) { |
| 233 | debug("SF: fail to read bank addr register\n"); |
| 234 | return NULL; |
| 235 | } |
| 236 | flash->bank_curr = curr_bank; |
| 237 | } else { |
| 238 | flash->bank_curr = curr_bank; |
| 239 | } |
| 240 | #endif |
| 241 | |
Jagannadha Sutradharudu Teki | 54024c1 | 2013-09-26 13:16:45 +0530 | [diff] [blame] | 242 | /* Flash powers up read-only, so clear BP# bits */ |
| 243 | #if defined(CONFIG_SPI_FLASH_ATMEL) || \ |
| 244 | defined(CONFIG_SPI_FLASH_MACRONIX) || \ |
| 245 | defined(CONFIG_SPI_FLASH_SST) |
| 246 | spi_flash_cmd_write_status(flash, 0); |
| 247 | #endif |
| 248 | |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 249 | return flash; |
| 250 | } |
| 251 | |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 252 | #ifdef CONFIG_OF_CONTROL |
| 253 | int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash) |
| 254 | { |
| 255 | fdt_addr_t addr; |
| 256 | fdt_size_t size; |
| 257 | int node; |
| 258 | |
| 259 | /* If there is no node, do nothing */ |
| 260 | node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH); |
| 261 | if (node < 0) |
| 262 | return 0; |
| 263 | |
| 264 | addr = fdtdec_get_addr_size(blob, node, "memory-map", &size); |
| 265 | if (addr == FDT_ADDR_T_NONE) { |
| 266 | debug("%s: Cannot decode address\n", __func__); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | if (flash->size != size) { |
| 271 | debug("%s: Memory map must cover entire device\n", __func__); |
| 272 | return -1; |
| 273 | } |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 274 | flash->memory_map = map_sysmem(addr, size); |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | #endif /* CONFIG_OF_CONTROL */ |
| 279 | |
Simon Glass | 0efc024 | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 280 | 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] | 281 | { |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 282 | struct spi_flash *flash = NULL; |
Jagannadha Sutradharudu Teki | 32ebd1a | 2013-09-26 14:24:58 +0530 | [diff] [blame] | 283 | u8 idcode[5]; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 284 | int ret; |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 285 | |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 286 | /* Setup spi_slave */ |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 287 | if (!spi) { |
| 288 | printf("SF: Failed to set up slave\n"); |
| 289 | return NULL; |
| 290 | } |
| 291 | |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 292 | /* Claim spi bus */ |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 293 | ret = spi_claim_bus(spi); |
| 294 | if (ret) { |
| 295 | debug("SF: Failed to claim SPI bus: %d\n", ret); |
| 296 | goto err_claim_bus; |
| 297 | } |
| 298 | |
| 299 | /* Read the ID codes */ |
| 300 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 301 | if (ret) { |
| 302 | printf("SF: Failed to get idcodes\n"); |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 303 | goto err_read_id; |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 304 | } |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 305 | |
| 306 | #ifdef DEBUG |
| 307 | printf("SF: Got idcodes\n"); |
| 308 | print_buffer(0, idcode, 1, sizeof(idcode), 0); |
| 309 | #endif |
| 310 | |
Jagannadha Sutradharudu Teki | a5e8199 | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 311 | /* Validate params from spi_flash_params table */ |
| 312 | flash = spi_flash_validate_params(spi, idcode); |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 313 | if (!flash) |
| 314 | goto err_read_id; |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 315 | |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 316 | #ifdef CONFIG_OF_CONTROL |
| 317 | if (spi_flash_decode_fdt(gd->fdt_blob, flash)) { |
| 318 | debug("SF: FDT decode error\n"); |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 319 | goto err_read_id; |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 320 | } |
| 321 | #endif |
| 322 | #ifndef CONFIG_SPL_BUILD |
| 323 | printf("SF: Detected %s with page size ", flash->name); |
Jagannadha Sutradharudu Teki | 3ea708f | 2013-09-15 23:06:12 +0530 | [diff] [blame] | 324 | print_size(flash->page_size, ", erase size "); |
| 325 | print_size(flash->erase_size, ", total "); |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 326 | print_size(flash->size, ""); |
| 327 | if (flash->memory_map) |
| 328 | printf(", mapped at %p", flash->memory_map); |
| 329 | puts("\n"); |
| 330 | #endif |
| 331 | #ifndef CONFIG_SPI_FLASH_BAR |
Jagannadha Sutradharudu Teki | f77f469 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 332 | if (((flash->dual_flash == SF_SINGLE_FLASH) && |
| 333 | (flash->size > SPI_FLASH_16MB_BOUN)) || |
| 334 | ((flash->dual_flash > SF_SINGLE_FLASH) && |
| 335 | (flash->size > SPI_FLASH_16MB_BOUN << 1))) { |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 336 | puts("SF: Warning - Only lower 16MiB accessible,"); |
| 337 | puts(" Full access #define CONFIG_SPI_FLASH_BAR\n"); |
| 338 | } |
| 339 | #endif |
| 340 | |
Jagannadha Sutradharudu Teki | 4d4ec99 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 341 | /* Release spi bus */ |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 342 | spi_release_bus(spi); |
| 343 | |
| 344 | return flash; |
| 345 | |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 346 | err_read_id: |
| 347 | spi_release_bus(spi); |
| 348 | err_claim_bus: |
| 349 | spi_free_slave(spi); |
| 350 | return NULL; |
| 351 | } |
| 352 | |
Simon Glass | 0efc024 | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 353 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 354 | unsigned int max_hz, unsigned int spi_mode) |
| 355 | { |
| 356 | struct spi_slave *spi; |
| 357 | |
| 358 | spi = spi_setup_slave(bus, cs, max_hz, spi_mode); |
| 359 | return spi_flash_probe_slave(spi); |
| 360 | } |
| 361 | |
| 362 | #ifdef CONFIG_OF_SPI_FLASH |
| 363 | struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node, |
| 364 | int spi_node) |
| 365 | { |
| 366 | struct spi_slave *spi; |
| 367 | |
| 368 | spi = spi_setup_slave_fdt(blob, slave_node, spi_node); |
| 369 | return spi_flash_probe_slave(spi); |
| 370 | } |
| 371 | #endif |
| 372 | |
Jagannadha Sutradharudu Teki | 4d5e29a | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 373 | void spi_flash_free(struct spi_flash *flash) |
| 374 | { |
| 375 | spi_free_slave(flash->spi); |
| 376 | free(flash); |
| 377 | } |