Jagannadha Sutradharudu Teki | e9a8b82 | 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 | d145270 | 2013-10-10 22:32:55 +0530 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 13 | #include <errno.h> |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 14 | #include <fdtdec.h> |
| 15 | #include <malloc.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 16 | #include <mapmem.h> |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 17 | #include <spi.h> |
| 18 | #include <spi_flash.h> |
Mike Frysinger | b375ad9 | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 19 | #include <asm/io.h> |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 20 | |
Jagannadha Sutradharudu Teki | ac8242d | 2013-09-26 16:00:15 +0530 | [diff] [blame] | 21 | #include "sf_internal.h" |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Jagannadha Sutradharudu Teki | 02eee9a | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 25 | /* Read commands array */ |
| 26 | static u8 spi_read_cmds_array[] = { |
| 27 | CMD_READ_ARRAY_SLOW, |
Jagannadha Sutradharudu Teki | 29e6391 | 2014-12-12 19:36:11 +0530 | [diff] [blame] | 28 | CMD_READ_ARRAY_FAST, |
Jagannadha Sutradharudu Teki | 02eee9a | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 29 | CMD_READ_DUAL_OUTPUT_FAST, |
| 30 | CMD_READ_DUAL_IO_FAST, |
Jagannadha Sutradharudu Teki | e0ebabc | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 31 | CMD_READ_QUAD_OUTPUT_FAST, |
Jagannadha Sutradharudu Teki | 4546230 | 2013-12-24 15:24:31 +0530 | [diff] [blame] | 32 | CMD_READ_QUAD_IO_FAST, |
Jagannadha Sutradharudu Teki | 02eee9a | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 33 | }; |
| 34 | |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 35 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
| 36 | static int spi_flash_set_qeb_mxic(struct spi_flash *flash) |
| 37 | { |
| 38 | u8 qeb_status; |
| 39 | int ret; |
| 40 | |
| 41 | ret = spi_flash_cmd_read_status(flash, &qeb_status); |
| 42 | if (ret < 0) |
| 43 | return ret; |
| 44 | |
| 45 | if (qeb_status & STATUS_QEB_MXIC) { |
| 46 | debug("SF: mxic: QEB is already set\n"); |
| 47 | } else { |
| 48 | ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC); |
| 49 | if (ret < 0) |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | return ret; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) |
| 58 | static int spi_flash_set_qeb_winspan(struct spi_flash *flash) |
| 59 | { |
| 60 | u8 qeb_status; |
| 61 | int ret; |
| 62 | |
| 63 | ret = spi_flash_cmd_read_config(flash, &qeb_status); |
| 64 | if (ret < 0) |
| 65 | return ret; |
| 66 | |
| 67 | if (qeb_status & STATUS_QEB_WINSPAN) { |
| 68 | debug("SF: winspan: QEB is already set\n"); |
| 69 | } else { |
| 70 | ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN); |
| 71 | if (ret < 0) |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | return ret; |
| 76 | } |
| 77 | #endif |
| 78 | |
Jagannadha Sutradharudu Teki | 725c64e | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 79 | static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0) |
| 80 | { |
| 81 | switch (idcode0) { |
Jagannadha Sutradharudu Teki | 754c73c | 2013-12-26 14:13:36 +0530 | [diff] [blame] | 82 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
| 83 | case SPI_FLASH_CFI_MFR_MACRONIX: |
| 84 | return spi_flash_set_qeb_mxic(flash); |
| 85 | #endif |
Jagannadha Sutradharudu Teki | 725c64e | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 86 | #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) |
| 87 | case SPI_FLASH_CFI_MFR_SPANSION: |
| 88 | case SPI_FLASH_CFI_MFR_WINBOND: |
| 89 | return spi_flash_set_qeb_winspan(flash); |
| 90 | #endif |
| 91 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 92 | case SPI_FLASH_CFI_MFR_STMICRO: |
| 93 | debug("SF: QEB is volatile for %02x flash\n", idcode0); |
| 94 | return 0; |
| 95 | #endif |
| 96 | default: |
| 97 | printf("SF: Need set QEB func for %02x flash\n", idcode0); |
| 98 | return -1; |
| 99 | } |
| 100 | } |
| 101 | |
Jagan Teki | 88e7a7c | 2015-09-02 11:39:47 +0530 | [diff] [blame] | 102 | #ifdef CONFIG_SPI_FLASH_BAR |
| 103 | static int spi_flash_read_bank(struct spi_flash *flash, u8 idcode0) |
| 104 | { |
| 105 | u8 curr_bank = 0; |
| 106 | int ret; |
| 107 | |
| 108 | if (flash->size <= SPI_FLASH_16MB_BOUN) |
| 109 | goto bank_end; |
| 110 | |
| 111 | switch (idcode0) { |
| 112 | case SPI_FLASH_CFI_MFR_SPANSION: |
| 113 | flash->bank_read_cmd = CMD_BANKADDR_BRRD; |
| 114 | flash->bank_write_cmd = CMD_BANKADDR_BRWR; |
| 115 | default: |
| 116 | flash->bank_read_cmd = CMD_EXTNADDR_RDEAR; |
| 117 | flash->bank_write_cmd = CMD_EXTNADDR_WREAR; |
| 118 | } |
| 119 | |
| 120 | ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1, |
| 121 | &curr_bank, 1); |
| 122 | if (ret) { |
| 123 | debug("SF: fail to read bank addr register\n"); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | bank_end: |
| 128 | flash->bank_curr = curr_bank; |
| 129 | return 0; |
| 130 | } |
| 131 | #endif |
| 132 | |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 133 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 134 | int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash) |
| 135 | { |
| 136 | fdt_addr_t addr; |
| 137 | fdt_size_t size; |
| 138 | int node; |
| 139 | |
| 140 | /* If there is no node, do nothing */ |
| 141 | node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH); |
| 142 | if (node < 0) |
| 143 | return 0; |
| 144 | |
| 145 | addr = fdtdec_get_addr_size(blob, node, "memory-map", &size); |
| 146 | if (addr == FDT_ADDR_T_NONE) { |
| 147 | debug("%s: Cannot decode address\n", __func__); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | if (flash->size != size) { |
| 152 | debug("%s: Memory map must cover entire device\n", __func__); |
| 153 | return -1; |
| 154 | } |
| 155 | flash->memory_map = map_sysmem(addr, size); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ |
| 160 | |
| 161 | static int spi_flash_scan(struct spi_slave *spi, u8 *idcode, |
| 162 | struct spi_flash *flash) |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 163 | { |
| 164 | const struct spi_flash_params *params; |
Jagannadha Sutradharudu Teki | 02eee9a | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 165 | u8 cmd; |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 166 | u16 jedec = idcode[1] << 8 | idcode[2]; |
Jagannadha Sutradharudu Teki | 2531553 | 2013-09-28 16:49:37 +0530 | [diff] [blame] | 167 | u16 ext_jedec = idcode[3] << 8 | idcode[4]; |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 168 | int ret; |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 169 | |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 170 | /* Validate params from spi_flash_params table */ |
Jagannadha Sutradharudu Teki | 20e45bc | 2013-12-23 23:34:42 +0530 | [diff] [blame] | 171 | params = spi_flash_params_table; |
| 172 | for (; params->name != NULL; params++) { |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 173 | if ((params->jedec >> 16) == idcode[0]) { |
Jagannadha Sutradharudu Teki | 2531553 | 2013-09-28 16:49:37 +0530 | [diff] [blame] | 174 | if ((params->jedec & 0xFFFF) == jedec) { |
| 175 | if (params->ext_jedec == 0) |
| 176 | break; |
| 177 | else if (params->ext_jedec == ext_jedec) |
| 178 | break; |
| 179 | } |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Jagannadha Sutradharudu Teki | 20e45bc | 2013-12-23 23:34:42 +0530 | [diff] [blame] | 183 | if (!params->name) { |
Jagannadha Sutradharudu Teki | 2531553 | 2013-09-28 16:49:37 +0530 | [diff] [blame] | 184 | printf("SF: Unsupported flash IDs: "); |
| 185 | printf("manuf %02x, jedec %04x, ext_jedec %04x\n", |
| 186 | idcode[0], jedec, ext_jedec); |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 187 | return -EPROTONOSUPPORT; |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 188 | } |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 189 | |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 190 | /* Flash powers up read-only, so clear BP# bits */ |
| 191 | #if defined(CONFIG_SPI_FLASH_ATMEL) || \ |
| 192 | defined(CONFIG_SPI_FLASH_MACRONIX) || \ |
| 193 | defined(CONFIG_SPI_FLASH_SST) |
| 194 | spi_flash_cmd_write_status(flash, 0); |
| 195 | #endif |
| 196 | |
Jagannadha Sutradharudu Teki | 84fb863 | 2013-10-10 22:14:09 +0530 | [diff] [blame] | 197 | /* Assign spi data */ |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 198 | flash->spi = spi; |
| 199 | flash->name = params->name; |
Jagannadha Sutradharudu Teki | f3b2dd8 | 2013-10-07 19:34:56 +0530 | [diff] [blame] | 200 | flash->memory_map = spi->memory_map; |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 201 | flash->dual_flash = flash->spi->option; |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 202 | |
Bin Meng | c78a152 | 2015-11-15 19:31:37 -0800 | [diff] [blame] | 203 | /* Assign spi flash flags */ |
| 204 | if (params->flags & SST_WR) |
| 205 | flash->flags |= SNOR_F_SST_WR; |
| 206 | |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 207 | /* Assign spi_flash ops */ |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 208 | #ifndef CONFIG_DM_SPI_FLASH |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 209 | flash->write = spi_flash_cmd_write_ops; |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 210 | #if defined(CONFIG_SPI_FLASH_SST) |
Bin Meng | c78a152 | 2015-11-15 19:31:37 -0800 | [diff] [blame] | 211 | if (flash->flags & SNOR_F_SST_WR) { |
Jagannadha Sutradharudu Teki | 7f0fd70 | 2014-12-12 19:36:14 +0530 | [diff] [blame] | 212 | if (flash->spi->op_mode_tx & SPI_OPM_TX_BP) |
| 213 | flash->write = sst_write_bp; |
| 214 | else |
| 215 | flash->write = sst_write_wp; |
| 216 | } |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 217 | #endif |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 218 | flash->erase = spi_flash_cmd_erase_ops; |
| 219 | flash->read = spi_flash_cmd_read_ops; |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 220 | #endif |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 221 | |
Fabio Estevam | 1cd8761 | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 222 | /* lock hooks are flash specific - assign them based on idcode0 */ |
| 223 | switch (idcode[0]) { |
Fabio Estevam | 0a2bf5c | 2015-11-17 16:50:53 -0200 | [diff] [blame] | 224 | #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST) |
Fabio Estevam | 1cd8761 | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 225 | case SPI_FLASH_CFI_MFR_STMICRO: |
Fabio Estevam | 0a2bf5c | 2015-11-17 16:50:53 -0200 | [diff] [blame] | 226 | case SPI_FLASH_CFI_MFR_SST: |
Fabio Estevam | 1cd8761 | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 227 | flash->flash_lock = stm_lock; |
| 228 | flash->flash_unlock = stm_unlock; |
| 229 | flash->flash_is_locked = stm_is_locked; |
| 230 | #endif |
| 231 | break; |
| 232 | default: |
| 233 | debug("SF: Lock ops not supported for %02x flash\n", idcode[0]); |
| 234 | } |
| 235 | |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 236 | /* Compute the flash size */ |
Jagannadha Sutradharudu Teki | b47b741 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 237 | flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0; |
Marek Vasut | 52fa1ff | 2014-02-21 18:13:26 +0100 | [diff] [blame] | 238 | /* |
| 239 | * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the |
| 240 | * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with |
| 241 | * the 0x4d00 Extended JEDEC code have 512b pages. All of the others |
| 242 | * have 256b pages. |
| 243 | */ |
| 244 | if (ext_jedec == 0x4d00) { |
| 245 | if ((jedec == 0x0215) || (jedec == 0x216)) |
| 246 | flash->page_size = 256; |
| 247 | else |
| 248 | flash->page_size = 512; |
| 249 | } else { |
| 250 | flash->page_size = 256; |
| 251 | } |
| 252 | flash->page_size <<= flash->shift; |
Jagannadha Sutradharudu Teki | b47b741 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 253 | flash->sector_size = params->sector_size << flash->shift; |
| 254 | flash->size = flash->sector_size * params->nr_sectors << flash->shift; |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 255 | #ifdef CONFIG_SF_DUAL_FLASH |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 256 | if (flash->dual_flash & SF_DUAL_STACKED_FLASH) |
| 257 | flash->size <<= 1; |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 258 | #endif |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 259 | |
Jagannadha Sutradharudu Teki | bc0f879 | 2013-10-02 19:36:58 +0530 | [diff] [blame] | 260 | /* Compute erase sector and command */ |
| 261 | if (params->flags & SECT_4K) { |
| 262 | flash->erase_cmd = CMD_ERASE_4K; |
Jagannadha Sutradharudu Teki | b47b741 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 263 | flash->erase_size = 4096 << flash->shift; |
Jagannadha Sutradharudu Teki | bc0f879 | 2013-10-02 19:36:58 +0530 | [diff] [blame] | 264 | } else if (params->flags & SECT_32K) { |
| 265 | flash->erase_cmd = CMD_ERASE_32K; |
Jagannadha Sutradharudu Teki | b47b741 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 266 | flash->erase_size = 32768 << flash->shift; |
Jagannadha Sutradharudu Teki | bc0f879 | 2013-10-02 19:36:58 +0530 | [diff] [blame] | 267 | } else { |
| 268 | flash->erase_cmd = CMD_ERASE_64K; |
| 269 | flash->erase_size = flash->sector_size; |
| 270 | } |
| 271 | |
Jagannadha Sutradharudu Teki | dc1e3ae | 2015-04-27 21:04:15 +0530 | [diff] [blame] | 272 | /* Now erase size becomes valid sector size */ |
| 273 | flash->sector_size = flash->erase_size; |
| 274 | |
Jagannadha Sutradharudu Teki | 02eee9a | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 275 | /* Look for the fastest read cmd */ |
| 276 | cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx); |
| 277 | if (cmd) { |
| 278 | cmd = spi_read_cmds_array[cmd - 1]; |
| 279 | flash->read_cmd = cmd; |
| 280 | } else { |
Jagannadha Sutradharudu Teki | 243ced0 | 2014-01-12 21:38:21 +0530 | [diff] [blame] | 281 | /* Go for default supported read cmd */ |
Jagannadha Sutradharudu Teki | 02eee9a | 2014-01-11 15:10:28 +0530 | [diff] [blame] | 282 | flash->read_cmd = CMD_READ_ARRAY_FAST; |
| 283 | } |
| 284 | |
Jagannadha Sutradharudu Teki | e0ebabc | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 285 | /* Not require to look for fastest only two write cmds yet */ |
| 286 | if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP) |
| 287 | flash->write_cmd = CMD_QUAD_PAGE_PROGRAM; |
| 288 | else |
| 289 | /* Go for default supported write cmd */ |
| 290 | flash->write_cmd = CMD_PAGE_PROGRAM; |
| 291 | |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 292 | /* Set the quad enable bit - only for quad commands */ |
| 293 | if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) || |
| 294 | (flash->read_cmd == CMD_READ_QUAD_IO_FAST) || |
| 295 | (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) { |
| 296 | ret = spi_flash_set_qeb(flash, idcode[0]); |
| 297 | if (ret) { |
| 298 | debug("SF: Fail to set QEB for %02x\n", idcode[0]); |
| 299 | return -EINVAL; |
| 300 | } |
| 301 | } |
| 302 | |
Jagannadha Sutradharudu Teki | ca79986 | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 303 | /* Read dummy_byte: dummy byte is determined based on the |
| 304 | * dummy cycles of a particular command. |
| 305 | * Fast commands - dummy_byte = dummy_cycles/8 |
| 306 | * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8 |
| 307 | * For I/O commands except cmd[0] everything goes on no.of lines |
| 308 | * based on particular command but incase of fast commands except |
| 309 | * data all go on single line irrespective of command. |
| 310 | */ |
| 311 | switch (flash->read_cmd) { |
| 312 | case CMD_READ_QUAD_IO_FAST: |
| 313 | flash->dummy_byte = 2; |
| 314 | break; |
| 315 | case CMD_READ_ARRAY_SLOW: |
| 316 | flash->dummy_byte = 0; |
| 317 | break; |
| 318 | default: |
| 319 | flash->dummy_byte = 1; |
| 320 | } |
| 321 | |
Jagannadha Sutradharudu Teki | f41fcca | 2013-10-02 19:37:43 +0530 | [diff] [blame] | 322 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 323 | if (params->flags & E_FSR) |
Jagan Teki | 853ef3e | 2015-09-29 16:54:31 +0530 | [diff] [blame] | 324 | flash->flags |= SNOR_F_USE_FSR; |
Jagannadha Sutradharudu Teki | f41fcca | 2013-10-02 19:37:43 +0530 | [diff] [blame] | 325 | #endif |
| 326 | |
Jagannadha Sutradharudu Teki | f3b2dd8 | 2013-10-07 19:34:56 +0530 | [diff] [blame] | 327 | /* Configure the BAR - discover bank cmds and read current bank */ |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 328 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 329 | ret = spi_flash_read_bank(flash, idcode[0]); |
Jagan Teki | 88e7a7c | 2015-09-02 11:39:47 +0530 | [diff] [blame] | 330 | if (ret < 0) |
| 331 | return ret; |
Jagannadha Sutradharudu Teki | 748bd4d | 2013-09-26 14:24:58 +0530 | [diff] [blame] | 332 | #endif |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 333 | |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 334 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 335 | ret = spi_flash_decode_fdt(gd->fdt_blob, flash); |
| 336 | if (ret) { |
| 337 | debug("SF: FDT decode error\n"); |
| 338 | return -EINVAL; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 339 | } |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 340 | #endif |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 341 | |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 342 | #ifndef CONFIG_SPL_BUILD |
| 343 | printf("SF: Detected %s with page size ", flash->name); |
| 344 | print_size(flash->page_size, ", erase size "); |
| 345 | print_size(flash->erase_size, ", total "); |
| 346 | print_size(flash->size, ""); |
| 347 | if (flash->memory_map) |
| 348 | printf(", mapped at %p", flash->memory_map); |
| 349 | puts("\n"); |
| 350 | #endif |
| 351 | |
| 352 | #ifndef CONFIG_SPI_FLASH_BAR |
| 353 | if (((flash->dual_flash == SF_SINGLE_FLASH) && |
| 354 | (flash->size > SPI_FLASH_16MB_BOUN)) || |
| 355 | ((flash->dual_flash > SF_SINGLE_FLASH) && |
| 356 | (flash->size > SPI_FLASH_16MB_BOUN << 1))) { |
| 357 | puts("SF: Warning - Only lower 16MiB accessible,"); |
| 358 | puts(" Full access #define CONFIG_SPI_FLASH_BAR\n"); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 359 | } |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 360 | #endif |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 361 | |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 362 | return ret; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 363 | } |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 364 | |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 365 | /** |
| 366 | * spi_flash_probe_slave() - Probe for a SPI flash device on a bus |
| 367 | * |
| 368 | * @spi: Bus to probe |
| 369 | * @flashp: Pointer to place to put flash info, which may be NULL if the |
| 370 | * space should be allocated |
| 371 | */ |
| 372 | int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash) |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 373 | { |
Jagannadha Sutradharudu Teki | 748bd4d | 2013-09-26 14:24:58 +0530 | [diff] [blame] | 374 | u8 idcode[5]; |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 375 | int ret; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 376 | |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 377 | /* Setup spi_slave */ |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 378 | if (!spi) { |
| 379 | printf("SF: Failed to set up slave\n"); |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 380 | return -ENODEV; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 381 | } |
| 382 | |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 383 | /* Claim spi bus */ |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 384 | ret = spi_claim_bus(spi); |
| 385 | if (ret) { |
| 386 | debug("SF: Failed to claim SPI bus: %d\n", ret); |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 387 | return ret; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* Read the ID codes */ |
| 391 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 392 | if (ret) { |
| 393 | printf("SF: Failed to get idcodes\n"); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 394 | goto err_read_id; |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 395 | } |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 396 | |
| 397 | #ifdef DEBUG |
| 398 | printf("SF: Got idcodes\n"); |
| 399 | print_buffer(0, idcode, 1, sizeof(idcode), 0); |
| 400 | #endif |
| 401 | |
Jagan Teki | 0d66a18 | 2015-09-29 17:28:20 +0530 | [diff] [blame^] | 402 | ret = spi_flash_scan(spi, idcode, flash); |
| 403 | if (ret) { |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 404 | ret = -EINVAL; |
Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 405 | goto err_read_id; |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 406 | } |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 407 | |
Daniel Schwierzeck | 06cfc03 | 2015-04-27 07:42:04 +0200 | [diff] [blame] | 408 | #ifdef CONFIG_SPI_FLASH_MTD |
| 409 | ret = spi_flash_mtd_register(flash); |
| 410 | #endif |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 411 | |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 412 | err_read_id: |
| 413 | spi_release_bus(spi); |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 414 | return ret; |
| 415 | } |
| 416 | |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 417 | #ifndef CONFIG_DM_SPI_FLASH |
| 418 | struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus) |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 419 | { |
| 420 | struct spi_flash *flash; |
| 421 | |
| 422 | /* Allocate space if needed (not used by sf-uclass */ |
| 423 | flash = calloc(1, sizeof(*flash)); |
| 424 | if (!flash) { |
| 425 | debug("SF: Failed to allocate spi_flash\n"); |
| 426 | return NULL; |
| 427 | } |
| 428 | |
| 429 | if (spi_flash_probe_slave(bus, flash)) { |
| 430 | spi_free_slave(bus); |
| 431 | free(flash); |
| 432 | return NULL; |
| 433 | } |
| 434 | |
| 435 | return flash; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 436 | } |
| 437 | |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 438 | struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs, |
Simon Glass | 49e9d2c | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 439 | unsigned int max_hz, unsigned int spi_mode) |
| 440 | { |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 441 | struct spi_slave *bus; |
Simon Glass | 49e9d2c | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 442 | |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 443 | bus = spi_setup_slave(busnum, cs, max_hz, spi_mode); |
Peng Fan | 54473a4 | 2015-03-20 13:19:16 +0800 | [diff] [blame] | 444 | if (!bus) |
| 445 | return NULL; |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 446 | return spi_flash_probe_tail(bus); |
Simon Glass | 49e9d2c | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | #ifdef CONFIG_OF_SPI_FLASH |
| 450 | struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node, |
| 451 | int spi_node) |
| 452 | { |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 453 | struct spi_slave *bus; |
Simon Glass | 49e9d2c | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 454 | |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 455 | bus = spi_setup_slave_fdt(blob, slave_node, spi_node); |
Peng Fan | 54473a4 | 2015-03-20 13:19:16 +0800 | [diff] [blame] | 456 | if (!bus) |
| 457 | return NULL; |
Simon Glass | c10c3d4 | 2014-10-13 23:42:05 -0600 | [diff] [blame] | 458 | return spi_flash_probe_tail(bus); |
Simon Glass | 49e9d2c | 2013-12-03 16:43:24 -0700 | [diff] [blame] | 459 | } |
| 460 | #endif |
| 461 | |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 462 | void spi_flash_free(struct spi_flash *flash) |
| 463 | { |
Daniel Schwierzeck | 06cfc03 | 2015-04-27 07:42:04 +0200 | [diff] [blame] | 464 | #ifdef CONFIG_SPI_FLASH_MTD |
| 465 | spi_flash_mtd_unregister(); |
| 466 | #endif |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 467 | spi_free_slave(flash->spi); |
| 468 | free(flash); |
| 469 | } |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 470 | |
| 471 | #else /* defined CONFIG_DM_SPI_FLASH */ |
| 472 | |
| 473 | static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len, |
| 474 | void *buf) |
| 475 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 476 | struct spi_flash *flash = dev_get_uclass_priv(dev); |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 477 | |
| 478 | return spi_flash_cmd_read_ops(flash, offset, len, buf); |
| 479 | } |
| 480 | |
| 481 | int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len, |
| 482 | const void *buf) |
| 483 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 484 | struct spi_flash *flash = dev_get_uclass_priv(dev); |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 485 | |
Bin Meng | fe0d080 | 2015-04-24 19:51:10 +0800 | [diff] [blame] | 486 | #if defined(CONFIG_SPI_FLASH_SST) |
Jagan Teki | 4537cec | 2015-09-29 11:17:02 +0530 | [diff] [blame] | 487 | if (flash->flags & SNOR_F_SST_WR) { |
Bin Meng | fe0d080 | 2015-04-24 19:51:10 +0800 | [diff] [blame] | 488 | if (flash->spi->op_mode_tx & SPI_OPM_TX_BP) |
| 489 | return sst_write_bp(flash, offset, len, buf); |
| 490 | else |
| 491 | return sst_write_wp(flash, offset, len, buf); |
| 492 | } |
| 493 | #endif |
| 494 | |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 495 | return spi_flash_cmd_write_ops(flash, offset, len, buf); |
| 496 | } |
| 497 | |
| 498 | int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len) |
| 499 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 500 | struct spi_flash *flash = dev_get_uclass_priv(dev); |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 501 | |
| 502 | return spi_flash_cmd_erase_ops(flash, offset, len); |
| 503 | } |
| 504 | |
| 505 | int spi_flash_std_probe(struct udevice *dev) |
| 506 | { |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 507 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 508 | struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev); |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 509 | struct spi_flash *flash; |
| 510 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 511 | flash = dev_get_uclass_priv(dev); |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 512 | flash->dev = dev; |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 513 | debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs); |
Simon Glass | 6f7d259 | 2014-10-13 23:42:07 -0600 | [diff] [blame] | 514 | return spi_flash_probe_slave(slave, flash); |
| 515 | } |
| 516 | |
| 517 | static const struct dm_spi_flash_ops spi_flash_std_ops = { |
| 518 | .read = spi_flash_std_read, |
| 519 | .write = spi_flash_std_write, |
| 520 | .erase = spi_flash_std_erase, |
| 521 | }; |
| 522 | |
| 523 | static const struct udevice_id spi_flash_std_ids[] = { |
| 524 | { .compatible = "spi-flash" }, |
| 525 | { } |
| 526 | }; |
| 527 | |
| 528 | U_BOOT_DRIVER(spi_flash_std) = { |
| 529 | .name = "spi_flash_std", |
| 530 | .id = UCLASS_SPI_FLASH, |
| 531 | .of_match = spi_flash_std_ids, |
| 532 | .probe = spi_flash_std_probe, |
| 533 | .priv_auto_alloc_size = sizeof(struct spi_flash), |
| 534 | .ops = &spi_flash_std_ops, |
| 535 | }; |
| 536 | |
| 537 | #endif /* CONFIG_DM_SPI_FLASH */ |