Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 1 | /* |
Jagan Teki | fefdfd2 | 2015-12-06 23:29:02 +0530 | [diff] [blame] | 2 | * SPI Flash Core |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 3 | * |
Jagan Teki | fefdfd2 | 2015-12-06 23:29:02 +0530 | [diff] [blame] | 4 | * Copyright (C) 2015 Jagan Teki <jteki@openedev.com> |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 5 | * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. |
Jagan Teki | fefdfd2 | 2015-12-06 23:29:02 +0530 | [diff] [blame] | 6 | * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik |
| 7 | * Copyright (C) 2008 Atmel Corporation |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 8 | * |
Jagannadha Sutradharudu Teki | d145270 | 2013-10-10 22:32:55 +0530 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Jagannadha Sutradharudu Teki | 9ad0175 | 2014-02-04 21:36:13 +0530 | [diff] [blame] | 13 | #include <errno.h> |
Jagannadha Sutradharudu Teki | ca79986 | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 14 | #include <malloc.h> |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 15 | #include <mapmem.h> |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 16 | #include <spi.h> |
| 17 | #include <spi_flash.h> |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 18 | #include <linux/log2.h> |
Mugunthan V N | 4e4b58d | 2016-02-15 15:31:39 +0530 | [diff] [blame] | 19 | #include <dma.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 | |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 25 | static void spi_flash_addr(u32 addr, u8 *cmd) |
| 26 | { |
| 27 | /* cmd[0] is actual command */ |
| 28 | cmd[1] = addr >> 16; |
| 29 | cmd[2] = addr >> 8; |
| 30 | cmd[3] = addr >> 0; |
| 31 | } |
| 32 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 33 | static int read_sr(struct spi_flash *flash, u8 *rs) |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 34 | { |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 35 | int ret; |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 36 | u8 cmd; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 37 | |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 38 | cmd = CMD_READ_STATUS; |
| 39 | ret = spi_flash_read_common(flash, &cmd, 1, rs, 1); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 40 | if (ret < 0) { |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 41 | debug("SF: fail to read status register\n"); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Jagan Teki | 853ef3e | 2015-09-29 16:54:31 +0530 | [diff] [blame] | 48 | static int read_fsr(struct spi_flash *flash, u8 *fsr) |
| 49 | { |
| 50 | int ret; |
| 51 | const u8 cmd = CMD_FLAG_STATUS; |
| 52 | |
| 53 | ret = spi_flash_read_common(flash, &cmd, 1, fsr, 1); |
| 54 | if (ret < 0) { |
| 55 | debug("SF: fail to read flag status register\n"); |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 62 | static int write_sr(struct spi_flash *flash, u8 ws) |
Jagannadha Sutradharudu Teki | 754c73c | 2013-12-26 14:13:36 +0530 | [diff] [blame] | 63 | { |
Jagannadha Sutradharudu Teki | 754c73c | 2013-12-26 14:13:36 +0530 | [diff] [blame] | 64 | u8 cmd; |
| 65 | int ret; |
| 66 | |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 67 | cmd = CMD_WRITE_STATUS; |
Jagannadha Sutradharudu Teki | 243ced0 | 2014-01-12 21:38:21 +0530 | [diff] [blame] | 68 | ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1); |
Jagannadha Sutradharudu Teki | 754c73c | 2013-12-26 14:13:36 +0530 | [diff] [blame] | 69 | if (ret < 0) { |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 70 | debug("SF: fail to write status register\n"); |
Jagannadha Sutradharudu Teki | 754c73c | 2013-12-26 14:13:36 +0530 | [diff] [blame] | 71 | return ret; |
| 72 | } |
| 73 | |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 74 | return 0; |
Jagannadha Sutradharudu Teki | 754c73c | 2013-12-26 14:13:36 +0530 | [diff] [blame] | 75 | } |
Jagannadha Sutradharudu Teki | 754c73c | 2013-12-26 14:13:36 +0530 | [diff] [blame] | 76 | |
Jagannadha Sutradharudu Teki | 725c64e | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 77 | #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 78 | static int read_cr(struct spi_flash *flash, u8 *rc) |
Jagannadha Sutradharudu Teki | 8a9c968 | 2013-12-23 15:47:48 +0530 | [diff] [blame] | 79 | { |
Jagannadha Sutradharudu Teki | 8a9c968 | 2013-12-23 15:47:48 +0530 | [diff] [blame] | 80 | int ret; |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 81 | u8 cmd; |
Jagannadha Sutradharudu Teki | 8a9c968 | 2013-12-23 15:47:48 +0530 | [diff] [blame] | 82 | |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 83 | cmd = CMD_READ_CONFIG; |
| 84 | ret = spi_flash_read_common(flash, &cmd, 1, rc, 1); |
Jagannadha Sutradharudu Teki | 8a9c968 | 2013-12-23 15:47:48 +0530 | [diff] [blame] | 85 | if (ret < 0) { |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 86 | debug("SF: fail to read config register\n"); |
Jagannadha Sutradharudu Teki | 8a9c968 | 2013-12-23 15:47:48 +0530 | [diff] [blame] | 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 93 | static int write_cr(struct spi_flash *flash, u8 wc) |
Jagannadha Sutradharudu Teki | 725c64e | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 94 | { |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 95 | u8 data[2]; |
Jagannadha Sutradharudu Teki | 725c64e | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 96 | u8 cmd; |
| 97 | int ret; |
| 98 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 99 | ret = read_sr(flash, &data[0]); |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 100 | if (ret < 0) |
Jagannadha Sutradharudu Teki | 725c64e | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 101 | return ret; |
Jagannadha Sutradharudu Teki | 725c64e | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 102 | |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 103 | cmd = CMD_WRITE_STATUS; |
| 104 | data[1] = wc; |
| 105 | ret = spi_flash_write_common(flash, &cmd, 1, &data, 2); |
| 106 | if (ret) { |
| 107 | debug("SF: fail to write config register\n"); |
| 108 | return ret; |
Jagannadha Sutradharudu Teki | 725c64e | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 109 | } |
| 110 | |
Jagannadha Sutradharudu Teki | 564a126 | 2013-12-30 22:16:23 +0530 | [diff] [blame] | 111 | return 0; |
Jagannadha Sutradharudu Teki | 725c64e | 2013-12-26 13:54:57 +0530 | [diff] [blame] | 112 | } |
| 113 | #endif |
| 114 | |
Jagan Teki | c26abdb | 2015-12-14 18:15:39 +0530 | [diff] [blame] | 115 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 116 | static int read_evcr(struct spi_flash *flash, u8 *evcr) |
| 117 | { |
| 118 | int ret; |
| 119 | const u8 cmd = CMD_READ_EVCR; |
| 120 | |
| 121 | ret = spi_flash_read_common(flash, &cmd, 1, evcr, 1); |
| 122 | if (ret < 0) { |
| 123 | debug("SF: error reading EVCR\n"); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int write_evcr(struct spi_flash *flash, u8 evcr) |
| 131 | { |
| 132 | u8 cmd; |
| 133 | int ret; |
| 134 | |
| 135 | cmd = CMD_WRITE_EVCR; |
| 136 | ret = spi_flash_write_common(flash, &cmd, 1, &evcr, 1); |
| 137 | if (ret < 0) { |
| 138 | debug("SF: error while writing EVCR register\n"); |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | #endif |
| 145 | |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 146 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 147 | static int spi_flash_write_bar(struct spi_flash *flash, u32 offset) |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 148 | { |
Jagan Teki | 5088b07 | 2015-09-02 11:39:48 +0530 | [diff] [blame] | 149 | u8 cmd, bank_sel; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 150 | int ret; |
| 151 | |
Jagan Teki | 5088b07 | 2015-09-02 11:39:48 +0530 | [diff] [blame] | 152 | bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift); |
| 153 | if (bank_sel == flash->bank_curr) |
| 154 | goto bar_end; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 155 | |
| 156 | cmd = flash->bank_write_cmd; |
| 157 | ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1); |
| 158 | if (ret < 0) { |
| 159 | debug("SF: fail to write bank register\n"); |
| 160 | return ret; |
| 161 | } |
Jagannadha Sutradharudu Teki | e14596c | 2013-10-08 23:26:47 +0530 | [diff] [blame] | 162 | |
Jagan Teki | 5088b07 | 2015-09-02 11:39:48 +0530 | [diff] [blame] | 163 | bar_end: |
| 164 | flash->bank_curr = bank_sel; |
| 165 | return flash->bank_curr; |
Jagannadha Sutradharudu Teki | e14596c | 2013-10-08 23:26:47 +0530 | [diff] [blame] | 166 | } |
Jagan Teki | f9a026d | 2015-11-04 00:27:35 +0530 | [diff] [blame] | 167 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 168 | static int spi_flash_read_bar(struct spi_flash *flash, u8 idcode0) |
Jagan Teki | f9a026d | 2015-11-04 00:27:35 +0530 | [diff] [blame] | 169 | { |
| 170 | u8 curr_bank = 0; |
| 171 | int ret; |
| 172 | |
| 173 | if (flash->size <= SPI_FLASH_16MB_BOUN) |
Jagan Teki | da042a0 | 2015-12-13 23:10:33 +0530 | [diff] [blame] | 174 | goto bar_end; |
Jagan Teki | f9a026d | 2015-11-04 00:27:35 +0530 | [diff] [blame] | 175 | |
| 176 | switch (idcode0) { |
| 177 | case SPI_FLASH_CFI_MFR_SPANSION: |
| 178 | flash->bank_read_cmd = CMD_BANKADDR_BRRD; |
| 179 | flash->bank_write_cmd = CMD_BANKADDR_BRWR; |
Jagan Teki | e37a362 | 2015-11-20 13:00:15 +0530 | [diff] [blame] | 180 | break; |
Jagan Teki | f9a026d | 2015-11-04 00:27:35 +0530 | [diff] [blame] | 181 | default: |
| 182 | flash->bank_read_cmd = CMD_EXTNADDR_RDEAR; |
| 183 | flash->bank_write_cmd = CMD_EXTNADDR_WREAR; |
| 184 | } |
| 185 | |
| 186 | ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1, |
| 187 | &curr_bank, 1); |
| 188 | if (ret) { |
| 189 | debug("SF: fail to read bank addr register\n"); |
| 190 | return ret; |
| 191 | } |
| 192 | |
Jagan Teki | da042a0 | 2015-12-13 23:10:33 +0530 | [diff] [blame] | 193 | bar_end: |
Jagan Teki | f9a026d | 2015-11-04 00:27:35 +0530 | [diff] [blame] | 194 | flash->bank_curr = curr_bank; |
| 195 | return 0; |
| 196 | } |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 197 | #endif |
| 198 | |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 199 | #ifdef CONFIG_SF_DUAL_FLASH |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 200 | static void spi_flash_dual(struct spi_flash *flash, u32 *addr) |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 201 | { |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 202 | struct spi_slave *spi = flash->spi; |
| 203 | |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 204 | switch (flash->dual_flash) { |
| 205 | case SF_DUAL_STACKED_FLASH: |
| 206 | if (*addr >= (flash->size >> 1)) { |
| 207 | *addr -= flash->size >> 1; |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 208 | spi->flags |= SPI_XFER_U_PAGE; |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 209 | } else { |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 210 | spi->flags &= ~SPI_XFER_U_PAGE; |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 211 | } |
| 212 | break; |
Jagannadha Sutradharudu Teki | b47b741 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 213 | case SF_DUAL_PARALLEL_FLASH: |
| 214 | *addr >>= flash->shift; |
| 215 | break; |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 216 | default: |
| 217 | debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash); |
| 218 | break; |
| 219 | } |
| 220 | } |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 221 | #endif |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 222 | |
Jagan Teki | 853ef3e | 2015-09-29 16:54:31 +0530 | [diff] [blame] | 223 | static int spi_flash_sr_ready(struct spi_flash *flash) |
Siva Durga Prasad Paladugu | bf4e865 | 2015-03-11 14:47:57 +0530 | [diff] [blame] | 224 | { |
Jagan Teki | 5181dd9 | 2015-09-02 11:39:50 +0530 | [diff] [blame] | 225 | u8 sr; |
Jagan Teki | 853ef3e | 2015-09-29 16:54:31 +0530 | [diff] [blame] | 226 | int ret; |
| 227 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 228 | ret = read_sr(flash, &sr); |
Jagan Teki | 853ef3e | 2015-09-29 16:54:31 +0530 | [diff] [blame] | 229 | if (ret < 0) |
| 230 | return ret; |
| 231 | |
| 232 | return !(sr & STATUS_WIP); |
| 233 | } |
| 234 | |
| 235 | static int spi_flash_fsr_ready(struct spi_flash *flash) |
| 236 | { |
| 237 | u8 fsr; |
| 238 | int ret; |
| 239 | |
| 240 | ret = read_fsr(flash, &fsr); |
| 241 | if (ret < 0) |
| 242 | return ret; |
| 243 | |
| 244 | return fsr & STATUS_PEC; |
| 245 | } |
| 246 | |
| 247 | static int spi_flash_ready(struct spi_flash *flash) |
| 248 | { |
| 249 | int sr, fsr; |
| 250 | |
| 251 | sr = spi_flash_sr_ready(flash); |
| 252 | if (sr < 0) |
| 253 | return sr; |
| 254 | |
| 255 | fsr = 1; |
| 256 | if (flash->flags & SNOR_F_USE_FSR) { |
| 257 | fsr = spi_flash_fsr_ready(flash); |
| 258 | if (fsr < 0) |
| 259 | return fsr; |
| 260 | } |
| 261 | |
| 262 | return sr && fsr; |
| 263 | } |
| 264 | |
Jagan Teki | f347f75 | 2015-09-29 18:26:08 +0530 | [diff] [blame] | 265 | static int spi_flash_cmd_wait_ready(struct spi_flash *flash, |
| 266 | unsigned long timeout) |
Jagan Teki | 853ef3e | 2015-09-29 16:54:31 +0530 | [diff] [blame] | 267 | { |
Stephen Warren | 07a6e38 | 2016-04-04 11:03:52 -0600 | [diff] [blame] | 268 | unsigned long timebase; |
| 269 | int ret; |
Siva Durga Prasad Paladugu | bf4e865 | 2015-03-11 14:47:57 +0530 | [diff] [blame] | 270 | |
Jagan Teki | 5181dd9 | 2015-09-02 11:39:50 +0530 | [diff] [blame] | 271 | timebase = get_timer(0); |
Siva Durga Prasad Paladugu | bf4e865 | 2015-03-11 14:47:57 +0530 | [diff] [blame] | 272 | |
Jagan Teki | 5181dd9 | 2015-09-02 11:39:50 +0530 | [diff] [blame] | 273 | while (get_timer(timebase) < timeout) { |
Jagan Teki | 853ef3e | 2015-09-29 16:54:31 +0530 | [diff] [blame] | 274 | ret = spi_flash_ready(flash); |
Siva Durga Prasad Paladugu | bf4e865 | 2015-03-11 14:47:57 +0530 | [diff] [blame] | 275 | if (ret < 0) |
| 276 | return ret; |
Jagan Teki | 853ef3e | 2015-09-29 16:54:31 +0530 | [diff] [blame] | 277 | if (ret) |
Jagan Teki | 5181dd9 | 2015-09-02 11:39:50 +0530 | [diff] [blame] | 278 | return 0; |
Siva Durga Prasad Paladugu | bf4e865 | 2015-03-11 14:47:57 +0530 | [diff] [blame] | 279 | } |
| 280 | |
Jagan Teki | 5181dd9 | 2015-09-02 11:39:50 +0530 | [diff] [blame] | 281 | printf("SF: Timeout!\n"); |
| 282 | |
| 283 | return -ETIMEDOUT; |
Siva Durga Prasad Paladugu | bf4e865 | 2015-03-11 14:47:57 +0530 | [diff] [blame] | 284 | } |
| 285 | |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 286 | int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd, |
| 287 | size_t cmd_len, const void *buf, size_t buf_len) |
| 288 | { |
| 289 | struct spi_slave *spi = flash->spi; |
| 290 | unsigned long timeout = SPI_FLASH_PROG_TIMEOUT; |
| 291 | int ret; |
| 292 | |
| 293 | if (buf == NULL) |
| 294 | timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT; |
| 295 | |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 296 | ret = spi_claim_bus(spi); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 297 | if (ret) { |
| 298 | debug("SF: unable to claim SPI bus\n"); |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | ret = spi_flash_cmd_write_enable(flash); |
| 303 | if (ret < 0) { |
| 304 | debug("SF: enabling write failed\n"); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len); |
| 309 | if (ret < 0) { |
| 310 | debug("SF: write cmd failed\n"); |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | ret = spi_flash_cmd_wait_ready(flash, timeout); |
| 315 | if (ret < 0) { |
| 316 | debug("SF: write %s timed out\n", |
| 317 | timeout == SPI_FLASH_PROG_TIMEOUT ? |
| 318 | "program" : "page erase"); |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | spi_release_bus(spi); |
| 323 | |
| 324 | return ret; |
| 325 | } |
| 326 | |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 327 | int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len) |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 328 | { |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 329 | u32 erase_size, erase_addr; |
Jagannadha Sutradharudu Teki | ca79986 | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 330 | u8 cmd[SPI_FLASH_CMD_LEN]; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 331 | int ret = -1; |
| 332 | |
Jagannadha Sutradharudu Teki | bc0f879 | 2013-10-02 19:36:58 +0530 | [diff] [blame] | 333 | erase_size = flash->erase_size; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 334 | if (offset % erase_size || len % erase_size) { |
| 335 | debug("SF: Erase offset/length not multiple of erase size\n"); |
| 336 | return -1; |
| 337 | } |
| 338 | |
Bin Meng | 66528f6 | 2015-11-13 02:46:26 -0800 | [diff] [blame] | 339 | if (flash->flash_is_locked) { |
| 340 | if (flash->flash_is_locked(flash, offset, len) > 0) { |
| 341 | printf("offset 0x%x is protected and cannot be erased\n", |
| 342 | offset); |
| 343 | return -EINVAL; |
| 344 | } |
Fabio Estevam | 1cd8761 | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 345 | } |
| 346 | |
Jagannadha Sutradharudu Teki | bc0f879 | 2013-10-02 19:36:58 +0530 | [diff] [blame] | 347 | cmd[0] = flash->erase_cmd; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 348 | while (len) { |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 349 | erase_addr = offset; |
| 350 | |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 351 | #ifdef CONFIG_SF_DUAL_FLASH |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 352 | if (flash->dual_flash > SF_SINGLE_FLASH) |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 353 | spi_flash_dual(flash, &erase_addr); |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 354 | #endif |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 355 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 356 | ret = spi_flash_write_bar(flash, erase_addr); |
Jagannadha Sutradharudu Teki | e14596c | 2013-10-08 23:26:47 +0530 | [diff] [blame] | 357 | if (ret < 0) |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 358 | return ret; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 359 | #endif |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 360 | spi_flash_addr(erase_addr, cmd); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 361 | |
| 362 | debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1], |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 363 | cmd[2], cmd[3], erase_addr); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 364 | |
| 365 | ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0); |
| 366 | if (ret < 0) { |
| 367 | debug("SF: erase failed\n"); |
| 368 | break; |
| 369 | } |
| 370 | |
| 371 | offset += erase_size; |
| 372 | len -= erase_size; |
| 373 | } |
| 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 378 | int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset, |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 379 | size_t len, const void *buf) |
| 380 | { |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 381 | struct spi_slave *spi = flash->spi; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 382 | unsigned long byte_addr, page_size; |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 383 | u32 write_addr; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 384 | size_t chunk_len, actual; |
Jagannadha Sutradharudu Teki | ca79986 | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 385 | u8 cmd[SPI_FLASH_CMD_LEN]; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 386 | int ret = -1; |
| 387 | |
| 388 | page_size = flash->page_size; |
| 389 | |
Bin Meng | 66528f6 | 2015-11-13 02:46:26 -0800 | [diff] [blame] | 390 | if (flash->flash_is_locked) { |
| 391 | if (flash->flash_is_locked(flash, offset, len) > 0) { |
| 392 | printf("offset 0x%x is protected and cannot be written\n", |
| 393 | offset); |
| 394 | return -EINVAL; |
| 395 | } |
Fabio Estevam | 1cd8761 | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 396 | } |
| 397 | |
Jagannadha Sutradharudu Teki | e0ebabc | 2014-01-11 15:13:11 +0530 | [diff] [blame] | 398 | cmd[0] = flash->write_cmd; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 399 | for (actual = 0; actual < len; actual += chunk_len) { |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 400 | write_addr = offset; |
| 401 | |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 402 | #ifdef CONFIG_SF_DUAL_FLASH |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 403 | if (flash->dual_flash > SF_SINGLE_FLASH) |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 404 | spi_flash_dual(flash, &write_addr); |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 405 | #endif |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 406 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 407 | ret = spi_flash_write_bar(flash, write_addr); |
Jagannadha Sutradharudu Teki | e14596c | 2013-10-08 23:26:47 +0530 | [diff] [blame] | 408 | if (ret < 0) |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 409 | return ret; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 410 | #endif |
| 411 | byte_addr = offset % page_size; |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 412 | chunk_len = min(len - actual, (size_t)(page_size - byte_addr)); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 413 | |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 414 | if (spi->max_write_size) |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 415 | chunk_len = min(chunk_len, |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 416 | (size_t)spi->max_write_size); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 417 | |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 418 | spi_flash_addr(write_addr, cmd); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 419 | |
Jagannadha Sutradharudu Teki | 243ced0 | 2014-01-12 21:38:21 +0530 | [diff] [blame] | 420 | debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n", |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 421 | buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 422 | |
| 423 | ret = spi_flash_write_common(flash, cmd, sizeof(cmd), |
| 424 | buf + actual, chunk_len); |
| 425 | if (ret < 0) { |
| 426 | debug("SF: write failed\n"); |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | offset += chunk_len; |
| 431 | } |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd, |
| 437 | size_t cmd_len, void *data, size_t data_len) |
| 438 | { |
| 439 | struct spi_slave *spi = flash->spi; |
| 440 | int ret; |
| 441 | |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 442 | ret = spi_claim_bus(spi); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 443 | if (ret) { |
| 444 | debug("SF: unable to claim SPI bus\n"); |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len); |
| 449 | if (ret < 0) { |
| 450 | debug("SF: read cmd failed\n"); |
| 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | spi_release_bus(spi); |
| 455 | |
| 456 | return ret; |
| 457 | } |
| 458 | |
Mugunthan V N | 4e4b58d | 2016-02-15 15:31:39 +0530 | [diff] [blame] | 459 | /* |
| 460 | * TODO: remove the weak after all the other spi_flash_copy_mmap |
| 461 | * implementations removed from drivers |
| 462 | */ |
Tom Rini | 4fe4470 | 2015-08-17 13:29:54 +0530 | [diff] [blame] | 463 | void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len) |
| 464 | { |
Mugunthan V N | 4e4b58d | 2016-02-15 15:31:39 +0530 | [diff] [blame] | 465 | #ifdef CONFIG_DMA |
| 466 | if (!dma_memcpy(data, offset, len)) |
| 467 | return; |
| 468 | #endif |
Tom Rini | 4fe4470 | 2015-08-17 13:29:54 +0530 | [diff] [blame] | 469 | memcpy(data, offset, len); |
| 470 | } |
| 471 | |
Jagannadha Sutradharudu Teki | 25dc86a | 2013-10-02 19:38:49 +0530 | [diff] [blame] | 472 | int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset, |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 473 | size_t len, void *data) |
| 474 | { |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 475 | struct spi_slave *spi = flash->spi; |
Jagannadha Sutradharudu Teki | 3bd17ab | 2014-01-11 16:57:07 +0530 | [diff] [blame] | 476 | u8 *cmd, cmdsz; |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 477 | u32 remain_len, read_len, read_addr; |
Jagannadha Sutradharudu Teki | 3bd17ab | 2014-01-11 16:57:07 +0530 | [diff] [blame] | 478 | int bank_sel = 0; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 479 | int ret = -1; |
| 480 | |
| 481 | /* Handle memory-mapped SPI */ |
| 482 | if (flash->memory_map) { |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 483 | ret = spi_claim_bus(spi); |
Poddar, Sourav | 47e21e0 | 2013-11-14 21:01:15 +0530 | [diff] [blame] | 484 | if (ret) { |
| 485 | debug("SF: unable to claim SPI bus\n"); |
| 486 | return ret; |
| 487 | } |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 488 | spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP); |
Tom Rini | 4fe4470 | 2015-08-17 13:29:54 +0530 | [diff] [blame] | 489 | spi_flash_copy_mmap(data, flash->memory_map + offset, len); |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 490 | spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP_END); |
| 491 | spi_release_bus(spi); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
Jagannadha Sutradharudu Teki | ca79986 | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 495 | cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte; |
Jagannadha Sutradharudu Teki | 9ad0175 | 2014-02-04 21:36:13 +0530 | [diff] [blame] | 496 | cmd = calloc(1, cmdsz); |
| 497 | if (!cmd) { |
| 498 | debug("SF: Failed to allocate cmd\n"); |
| 499 | return -ENOMEM; |
| 500 | } |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 501 | |
Jagannadha Sutradharudu Teki | ca79986 | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 502 | cmd[0] = flash->read_cmd; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 503 | while (len) { |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 504 | read_addr = offset; |
| 505 | |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 506 | #ifdef CONFIG_SF_DUAL_FLASH |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 507 | if (flash->dual_flash > SF_SINGLE_FLASH) |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 508 | spi_flash_dual(flash, &read_addr); |
Jagannadha Sutradharudu Teki | d7f253b | 2014-01-11 15:25:04 +0530 | [diff] [blame] | 509 | #endif |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 510 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 511 | ret = spi_flash_write_bar(flash, read_addr); |
Jagan Teki | 5088b07 | 2015-09-02 11:39:48 +0530 | [diff] [blame] | 512 | if (ret < 0) |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 513 | return ret; |
Jagan Teki | 5088b07 | 2015-09-02 11:39:48 +0530 | [diff] [blame] | 514 | bank_sel = flash->bank_curr; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 515 | #endif |
Jagannadha Sutradharudu Teki | b47b741 | 2014-01-07 00:11:35 +0530 | [diff] [blame] | 516 | remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) * |
| 517 | (bank_sel + 1)) - offset; |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 518 | if (len < remain_len) |
| 519 | read_len = len; |
| 520 | else |
| 521 | read_len = remain_len; |
| 522 | |
Jagannadha Sutradharudu Teki | e84b4f6 | 2014-01-12 21:40:11 +0530 | [diff] [blame] | 523 | spi_flash_addr(read_addr, cmd); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 524 | |
Jagannadha Sutradharudu Teki | ca79986 | 2014-01-11 16:50:45 +0530 | [diff] [blame] | 525 | ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 526 | if (ret < 0) { |
| 527 | debug("SF: read failed\n"); |
| 528 | break; |
| 529 | } |
| 530 | |
| 531 | offset += read_len; |
| 532 | len -= read_len; |
| 533 | data += read_len; |
| 534 | } |
| 535 | |
Marek Vasut | d774c48 | 2014-07-12 18:11:31 +0530 | [diff] [blame] | 536 | free(cmd); |
Jagannadha Sutradharudu Teki | e9a8b82 | 2013-08-29 19:01:56 +0530 | [diff] [blame] | 537 | return ret; |
| 538 | } |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 539 | |
| 540 | #ifdef CONFIG_SPI_FLASH_SST |
| 541 | static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf) |
| 542 | { |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 543 | struct spi_slave *spi = flash->spi; |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 544 | int ret; |
| 545 | u8 cmd[4] = { |
| 546 | CMD_SST_BP, |
| 547 | offset >> 16, |
| 548 | offset >> 8, |
| 549 | offset, |
| 550 | }; |
| 551 | |
| 552 | debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n", |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 553 | spi_w8r8(spi, CMD_READ_STATUS), buf, cmd[0], offset); |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 554 | |
| 555 | ret = spi_flash_cmd_write_enable(flash); |
| 556 | if (ret) |
| 557 | return ret; |
| 558 | |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 559 | ret = spi_flash_cmd_write(spi, cmd, sizeof(cmd), buf, 1); |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 560 | if (ret) |
| 561 | return ret; |
| 562 | |
| 563 | return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 564 | } |
| 565 | |
| 566 | int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len, |
| 567 | const void *buf) |
| 568 | { |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 569 | struct spi_slave *spi = flash->spi; |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 570 | size_t actual, cmd_len; |
| 571 | int ret; |
| 572 | u8 cmd[4]; |
| 573 | |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 574 | ret = spi_claim_bus(spi); |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 575 | if (ret) { |
| 576 | debug("SF: Unable to claim SPI bus\n"); |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | /* If the data is not word aligned, write out leading single byte */ |
| 581 | actual = offset % 2; |
| 582 | if (actual) { |
| 583 | ret = sst_byte_write(flash, offset, buf); |
| 584 | if (ret) |
| 585 | goto done; |
| 586 | } |
| 587 | offset += actual; |
| 588 | |
| 589 | ret = spi_flash_cmd_write_enable(flash); |
| 590 | if (ret) |
| 591 | goto done; |
| 592 | |
| 593 | cmd_len = 4; |
| 594 | cmd[0] = CMD_SST_AAI_WP; |
| 595 | cmd[1] = offset >> 16; |
| 596 | cmd[2] = offset >> 8; |
| 597 | cmd[3] = offset; |
| 598 | |
| 599 | for (; actual < len - 1; actual += 2) { |
| 600 | debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n", |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 601 | spi_w8r8(spi, CMD_READ_STATUS), buf + actual, |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 602 | cmd[0], offset); |
| 603 | |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 604 | ret = spi_flash_cmd_write(spi, cmd, cmd_len, |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 605 | buf + actual, 2); |
| 606 | if (ret) { |
| 607 | debug("SF: sst word program failed\n"); |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 612 | if (ret) |
| 613 | break; |
| 614 | |
| 615 | cmd_len = 1; |
| 616 | offset += 2; |
| 617 | } |
| 618 | |
| 619 | if (!ret) |
| 620 | ret = spi_flash_cmd_write_disable(flash); |
| 621 | |
| 622 | /* If there is a single trailing byte, write it out */ |
| 623 | if (!ret && actual != len) |
| 624 | ret = sst_byte_write(flash, offset, buf + actual); |
| 625 | |
| 626 | done: |
| 627 | debug("SF: sst: program %s %zu bytes @ 0x%zx\n", |
| 628 | ret ? "failure" : "success", len, offset - actual); |
| 629 | |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 630 | spi_release_bus(spi); |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 631 | return ret; |
| 632 | } |
Bin Meng | fcbfc17 | 2014-12-12 19:36:13 +0530 | [diff] [blame] | 633 | |
| 634 | int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len, |
| 635 | const void *buf) |
| 636 | { |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 637 | struct spi_slave *spi = flash->spi; |
Bin Meng | fcbfc17 | 2014-12-12 19:36:13 +0530 | [diff] [blame] | 638 | size_t actual; |
| 639 | int ret; |
| 640 | |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 641 | ret = spi_claim_bus(spi); |
Bin Meng | fcbfc17 | 2014-12-12 19:36:13 +0530 | [diff] [blame] | 642 | if (ret) { |
| 643 | debug("SF: Unable to claim SPI bus\n"); |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | for (actual = 0; actual < len; actual++) { |
| 648 | ret = sst_byte_write(flash, offset, buf + actual); |
| 649 | if (ret) { |
| 650 | debug("SF: sst byte program failed\n"); |
| 651 | break; |
| 652 | } |
| 653 | offset++; |
| 654 | } |
| 655 | |
| 656 | if (!ret) |
| 657 | ret = spi_flash_cmd_write_disable(flash); |
| 658 | |
| 659 | debug("SF: sst: program %s %zu bytes @ 0x%zx\n", |
| 660 | ret ? "failure" : "success", len, offset - actual); |
| 661 | |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 662 | spi_release_bus(spi); |
Bin Meng | fcbfc17 | 2014-12-12 19:36:13 +0530 | [diff] [blame] | 663 | return ret; |
| 664 | } |
Jagannadha Sutradharudu Teki | 0803242 | 2013-10-02 19:34:53 +0530 | [diff] [blame] | 665 | #endif |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 666 | |
Fabio Estevam | 0a2bf5c | 2015-11-17 16:50:53 -0200 | [diff] [blame] | 667 | #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST) |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 668 | static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs, |
Marek Vasut | 405a3c6 | 2016-03-11 03:20:16 +0100 | [diff] [blame] | 669 | u64 *len) |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 670 | { |
| 671 | u8 mask = SR_BP2 | SR_BP1 | SR_BP0; |
| 672 | int shift = ffs(mask) - 1; |
| 673 | int pow; |
| 674 | |
| 675 | if (!(sr & mask)) { |
| 676 | /* No protection */ |
| 677 | *ofs = 0; |
| 678 | *len = 0; |
| 679 | } else { |
| 680 | pow = ((sr & mask) ^ mask) >> shift; |
| 681 | *len = flash->size >> pow; |
| 682 | *ofs = flash->size - *len; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Return 1 if the entire region is locked, 0 otherwise |
| 688 | */ |
Marek Vasut | 405a3c6 | 2016-03-11 03:20:16 +0100 | [diff] [blame] | 689 | static int stm_is_locked_sr(struct spi_flash *flash, loff_t ofs, u64 len, |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 690 | u8 sr) |
| 691 | { |
| 692 | loff_t lock_offs; |
Marek Vasut | 405a3c6 | 2016-03-11 03:20:16 +0100 | [diff] [blame] | 693 | u64 lock_len; |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 694 | |
| 695 | stm_get_locked_range(flash, sr, &lock_offs, &lock_len); |
| 696 | |
| 697 | return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs); |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Check if a region of the flash is (completely) locked. See stm_lock() for |
| 702 | * more info. |
| 703 | * |
| 704 | * Returns 1 if entire region is locked, 0 if any portion is unlocked, and |
| 705 | * negative on errors. |
| 706 | */ |
Fabio Estevam | 1cd8761 | 2015-11-05 12:43:42 -0200 | [diff] [blame] | 707 | int stm_is_locked(struct spi_flash *flash, u32 ofs, size_t len) |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 708 | { |
| 709 | int status; |
| 710 | u8 sr; |
| 711 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 712 | status = read_sr(flash, &sr); |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 713 | if (status < 0) |
| 714 | return status; |
| 715 | |
| 716 | return stm_is_locked_sr(flash, ofs, len, sr); |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Lock a region of the flash. Compatible with ST Micro and similar flash. |
| 721 | * Supports only the block protection bits BP{0,1,2} in the status register |
| 722 | * (SR). Does not support these features found in newer SR bitfields: |
| 723 | * - TB: top/bottom protect - only handle TB=0 (top protect) |
| 724 | * - SEC: sector/block protect - only handle SEC=0 (block protect) |
| 725 | * - CMP: complement protect - only support CMP=0 (range is not complemented) |
| 726 | * |
| 727 | * Sample table portion for 8MB flash (Winbond w25q64fw): |
| 728 | * |
| 729 | * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion |
| 730 | * -------------------------------------------------------------------------- |
| 731 | * X | X | 0 | 0 | 0 | NONE | NONE |
| 732 | * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64 |
| 733 | * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32 |
| 734 | * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16 |
| 735 | * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8 |
| 736 | * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4 |
| 737 | * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2 |
| 738 | * X | X | 1 | 1 | 1 | 8 MB | ALL |
| 739 | * |
| 740 | * Returns negative on errors, 0 on success. |
| 741 | */ |
| 742 | int stm_lock(struct spi_flash *flash, u32 ofs, size_t len) |
| 743 | { |
| 744 | u8 status_old, status_new; |
| 745 | u8 mask = SR_BP2 | SR_BP1 | SR_BP0; |
| 746 | u8 shift = ffs(mask) - 1, pow, val; |
Fabio Estevam | 38a7e9b | 2015-11-17 17:13:33 -0200 | [diff] [blame] | 747 | int ret; |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 748 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 749 | ret = read_sr(flash, &status_old); |
Fabio Estevam | 38a7e9b | 2015-11-17 17:13:33 -0200 | [diff] [blame] | 750 | if (ret < 0) |
| 751 | return ret; |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 752 | |
| 753 | /* SPI NOR always locks to the end */ |
| 754 | if (ofs + len != flash->size) { |
| 755 | /* Does combined region extend to end? */ |
| 756 | if (!stm_is_locked_sr(flash, ofs + len, flash->size - ofs - len, |
| 757 | status_old)) |
| 758 | return -EINVAL; |
| 759 | len = flash->size - ofs; |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * Need smallest pow such that: |
| 764 | * |
| 765 | * 1 / (2^pow) <= (len / size) |
| 766 | * |
| 767 | * so (assuming power-of-2 size) we do: |
| 768 | * |
| 769 | * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len)) |
| 770 | */ |
| 771 | pow = ilog2(flash->size) - ilog2(len); |
| 772 | val = mask - (pow << shift); |
| 773 | if (val & ~mask) |
| 774 | return -EINVAL; |
| 775 | |
| 776 | /* Don't "lock" with no region! */ |
| 777 | if (!(val & mask)) |
| 778 | return -EINVAL; |
| 779 | |
| 780 | status_new = (status_old & ~mask) | val; |
| 781 | |
| 782 | /* Only modify protection if it will not unlock other areas */ |
| 783 | if ((status_new & mask) <= (status_old & mask)) |
| 784 | return -EINVAL; |
| 785 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 786 | write_sr(flash, status_new); |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 787 | |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * Unlock a region of the flash. See stm_lock() for more info |
| 793 | * |
| 794 | * Returns negative on errors, 0 on success. |
| 795 | */ |
| 796 | int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len) |
| 797 | { |
| 798 | uint8_t status_old, status_new; |
| 799 | u8 mask = SR_BP2 | SR_BP1 | SR_BP0; |
| 800 | u8 shift = ffs(mask) - 1, pow, val; |
Fabio Estevam | 38a7e9b | 2015-11-17 17:13:33 -0200 | [diff] [blame] | 801 | int ret; |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 802 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 803 | ret = read_sr(flash, &status_old); |
Fabio Estevam | 38a7e9b | 2015-11-17 17:13:33 -0200 | [diff] [blame] | 804 | if (ret < 0) |
| 805 | return ret; |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 806 | |
| 807 | /* Cannot unlock; would unlock larger region than requested */ |
Fabio Estevam | 55f380c | 2016-01-05 22:24:39 -0200 | [diff] [blame] | 808 | if (stm_is_locked_sr(flash, ofs - flash->erase_size, flash->erase_size, |
| 809 | status_old)) |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 810 | return -EINVAL; |
| 811 | /* |
| 812 | * Need largest pow such that: |
| 813 | * |
| 814 | * 1 / (2^pow) >= (len / size) |
| 815 | * |
| 816 | * so (assuming power-of-2 size) we do: |
| 817 | * |
| 818 | * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len)) |
| 819 | */ |
| 820 | pow = ilog2(flash->size) - order_base_2(flash->size - (ofs + len)); |
| 821 | if (ofs + len == flash->size) { |
| 822 | val = 0; /* fully unlocked */ |
| 823 | } else { |
| 824 | val = mask - (pow << shift); |
| 825 | /* Some power-of-two sizes are not supported */ |
| 826 | if (val & ~mask) |
| 827 | return -EINVAL; |
| 828 | } |
| 829 | |
| 830 | status_new = (status_old & ~mask) | val; |
| 831 | |
| 832 | /* Only modify protection if it will not lock other areas */ |
| 833 | if ((status_new & mask) >= (status_old & mask)) |
| 834 | return -EINVAL; |
| 835 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 836 | write_sr(flash, status_new); |
Fabio Estevam | d970969 | 2015-11-05 12:43:41 -0200 | [diff] [blame] | 837 | |
| 838 | return 0; |
| 839 | } |
Fabio Estevam | 0a2bf5c | 2015-11-17 16:50:53 -0200 | [diff] [blame] | 840 | #endif |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 841 | |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 842 | |
| 843 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
Jagan Teki | 7fd4fd6 | 2015-12-13 23:04:46 +0530 | [diff] [blame] | 844 | static int macronix_quad_enable(struct spi_flash *flash) |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 845 | { |
| 846 | u8 qeb_status; |
| 847 | int ret; |
| 848 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 849 | ret = read_sr(flash, &qeb_status); |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 850 | if (ret < 0) |
| 851 | return ret; |
| 852 | |
Jagan Teki | f2db1bf | 2015-12-15 12:42:02 +0530 | [diff] [blame] | 853 | if (qeb_status & STATUS_QEB_MXIC) |
| 854 | return 0; |
| 855 | |
Jagan Teki | d723f34 | 2015-12-16 13:48:08 +0530 | [diff] [blame] | 856 | ret = write_sr(flash, qeb_status | STATUS_QEB_MXIC); |
Jagan Teki | f2db1bf | 2015-12-15 12:42:02 +0530 | [diff] [blame] | 857 | if (ret < 0) |
| 858 | return ret; |
| 859 | |
| 860 | /* read SR and check it */ |
| 861 | ret = read_sr(flash, &qeb_status); |
| 862 | if (!(ret >= 0 && (qeb_status & STATUS_QEB_MXIC))) { |
| 863 | printf("SF: Macronix SR Quad bit not clear\n"); |
| 864 | return -EINVAL; |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | return ret; |
| 868 | } |
| 869 | #endif |
| 870 | |
| 871 | #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) |
Jagan Teki | 7fd4fd6 | 2015-12-13 23:04:46 +0530 | [diff] [blame] | 872 | static int spansion_quad_enable(struct spi_flash *flash) |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 873 | { |
| 874 | u8 qeb_status; |
| 875 | int ret; |
| 876 | |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 877 | ret = read_cr(flash, &qeb_status); |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 878 | if (ret < 0) |
| 879 | return ret; |
| 880 | |
Jagan Teki | 294472b | 2015-12-15 12:28:39 +0530 | [diff] [blame] | 881 | if (qeb_status & STATUS_QEB_WINSPAN) |
| 882 | return 0; |
| 883 | |
Jagan Teki | d723f34 | 2015-12-16 13:48:08 +0530 | [diff] [blame] | 884 | ret = write_cr(flash, qeb_status | STATUS_QEB_WINSPAN); |
Jagan Teki | 294472b | 2015-12-15 12:28:39 +0530 | [diff] [blame] | 885 | if (ret < 0) |
| 886 | return ret; |
| 887 | |
| 888 | /* read CR and check it */ |
| 889 | ret = read_cr(flash, &qeb_status); |
| 890 | if (!(ret >= 0 && (qeb_status & STATUS_QEB_WINSPAN))) { |
| 891 | printf("SF: Spansion CR Quad bit not clear\n"); |
| 892 | return -EINVAL; |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | return ret; |
| 896 | } |
| 897 | #endif |
| 898 | |
Jagan Teki | c26abdb | 2015-12-14 18:15:39 +0530 | [diff] [blame] | 899 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 900 | static int micron_quad_enable(struct spi_flash *flash) |
| 901 | { |
| 902 | u8 qeb_status; |
| 903 | int ret; |
| 904 | |
| 905 | ret = read_evcr(flash, &qeb_status); |
| 906 | if (ret < 0) |
| 907 | return ret; |
| 908 | |
| 909 | if (!(qeb_status & STATUS_QEB_MICRON)) |
| 910 | return 0; |
| 911 | |
| 912 | ret = write_evcr(flash, qeb_status & ~STATUS_QEB_MICRON); |
| 913 | if (ret < 0) |
| 914 | return ret; |
| 915 | |
| 916 | /* read EVCR and check it */ |
| 917 | ret = read_evcr(flash, &qeb_status); |
| 918 | if (!(ret >= 0 && !(qeb_status & STATUS_QEB_MICRON))) { |
| 919 | printf("SF: Micron EVCR Quad bit not clear\n"); |
| 920 | return -EINVAL; |
| 921 | } |
| 922 | |
| 923 | return ret; |
| 924 | } |
| 925 | #endif |
| 926 | |
Jagan Teki | 7fd4fd6 | 2015-12-13 23:04:46 +0530 | [diff] [blame] | 927 | static int set_quad_mode(struct spi_flash *flash, u8 idcode0) |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 928 | { |
| 929 | switch (idcode0) { |
| 930 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
| 931 | case SPI_FLASH_CFI_MFR_MACRONIX: |
Jagan Teki | 7fd4fd6 | 2015-12-13 23:04:46 +0530 | [diff] [blame] | 932 | return macronix_quad_enable(flash); |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 933 | #endif |
| 934 | #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) |
| 935 | case SPI_FLASH_CFI_MFR_SPANSION: |
| 936 | case SPI_FLASH_CFI_MFR_WINBOND: |
Jagan Teki | 7fd4fd6 | 2015-12-13 23:04:46 +0530 | [diff] [blame] | 937 | return spansion_quad_enable(flash); |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 938 | #endif |
| 939 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 940 | case SPI_FLASH_CFI_MFR_STMICRO: |
Jagan Teki | c26abdb | 2015-12-14 18:15:39 +0530 | [diff] [blame] | 941 | return micron_quad_enable(flash); |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 942 | #endif |
| 943 | default: |
| 944 | printf("SF: Need set QEB func for %02x flash\n", idcode0); |
| 945 | return -1; |
| 946 | } |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 947 | } |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 948 | |
| 949 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 950 | int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash) |
| 951 | { |
Simon Glass | 0f5c269 | 2016-01-21 19:43:54 -0700 | [diff] [blame] | 952 | #ifdef CONFIG_DM_SPI_FLASH |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 953 | fdt_addr_t addr; |
| 954 | fdt_size_t size; |
Simon Glass | 0f5c269 | 2016-01-21 19:43:54 -0700 | [diff] [blame] | 955 | int node = flash->dev->of_offset; |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 956 | |
| 957 | addr = fdtdec_get_addr_size(blob, node, "memory-map", &size); |
| 958 | if (addr == FDT_ADDR_T_NONE) { |
| 959 | debug("%s: Cannot decode address\n", __func__); |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | if (flash->size != size) { |
| 964 | debug("%s: Memory map must cover entire device\n", __func__); |
| 965 | return -1; |
| 966 | } |
| 967 | flash->memory_map = map_sysmem(addr, size); |
Simon Glass | 0f5c269 | 2016-01-21 19:43:54 -0700 | [diff] [blame] | 968 | #endif |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 969 | |
| 970 | return 0; |
| 971 | } |
| 972 | #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ |
| 973 | |
Yuan Yao | de4b02f | 2016-03-15 14:36:43 +0800 | [diff] [blame] | 974 | #ifdef CONFIG_SPI_FLASH_SPANSION |
| 975 | static int spansion_s25fss_disable_4KB_erase(struct spi_slave *spi) |
| 976 | { |
| 977 | u8 cmd[4]; |
| 978 | u32 offset = 0x800004; /* CR3V register offset */ |
| 979 | u8 cr3v; |
| 980 | int ret; |
| 981 | |
| 982 | cmd[0] = CMD_SPANSION_RDAR; |
| 983 | cmd[1] = offset >> 16; |
| 984 | cmd[2] = offset >> 8; |
| 985 | cmd[3] = offset >> 0; |
| 986 | |
| 987 | ret = spi_flash_cmd_read(spi, cmd, 4, &cr3v, 1); |
| 988 | if (ret) |
| 989 | return -EIO; |
| 990 | /* CR3V bit3: 4-KB Erase */ |
| 991 | if (cr3v & 0x8) |
| 992 | return 0; |
| 993 | |
| 994 | cmd[0] = CMD_SPANSION_WRAR; |
| 995 | cr3v |= 0x8; |
| 996 | ret = spi_flash_cmd_write(spi, cmd, 4, &cr3v, 1); |
| 997 | if (ret) |
| 998 | return -EIO; |
| 999 | |
| 1000 | cmd[0] = CMD_SPANSION_RDAR; |
| 1001 | ret = spi_flash_cmd_read(spi, cmd, 4, &cr3v, 1); |
| 1002 | if (ret) |
| 1003 | return -EIO; |
| 1004 | if (!(cr3v & 0x8)) |
| 1005 | return -EFAULT; |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | #endif |
| 1010 | |
Jagan Teki | 4abfb98 | 2015-12-06 21:33:32 +0530 | [diff] [blame] | 1011 | int spi_flash_scan(struct spi_flash *flash) |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1012 | { |
Jagan Teki | 4abfb98 | 2015-12-06 21:33:32 +0530 | [diff] [blame] | 1013 | struct spi_slave *spi = flash->spi; |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1014 | const struct spi_flash_params *params; |
Jagan Teki | 0cd3726 | 2015-09-29 18:06:04 +0530 | [diff] [blame] | 1015 | u16 jedec, ext_jedec; |
Jagan Teki | b678539 | 2016-08-08 16:50:45 +0530 | [diff] [blame] | 1016 | u8 idcode[5]; |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1017 | int ret; |
| 1018 | |
Jagan Teki | 0cd3726 | 2015-09-29 18:06:04 +0530 | [diff] [blame] | 1019 | /* Read the ID codes */ |
| 1020 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
| 1021 | if (ret) { |
| 1022 | printf("SF: Failed to get idcodes\n"); |
Simon Glass | 07f6671 | 2016-02-24 09:14:56 -0700 | [diff] [blame] | 1023 | return ret; |
Jagan Teki | 0cd3726 | 2015-09-29 18:06:04 +0530 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | #ifdef DEBUG |
| 1027 | printf("SF: Got idcodes\n"); |
| 1028 | print_buffer(0, idcode, 1, sizeof(idcode), 0); |
| 1029 | #endif |
| 1030 | |
| 1031 | jedec = idcode[1] << 8 | idcode[2]; |
| 1032 | ext_jedec = idcode[3] << 8 | idcode[4]; |
| 1033 | |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1034 | /* Validate params from spi_flash_params table */ |
| 1035 | params = spi_flash_params_table; |
| 1036 | for (; params->name != NULL; params++) { |
| 1037 | if ((params->jedec >> 16) == idcode[0]) { |
| 1038 | if ((params->jedec & 0xFFFF) == jedec) { |
| 1039 | if (params->ext_jedec == 0) |
| 1040 | break; |
| 1041 | else if (params->ext_jedec == ext_jedec) |
| 1042 | break; |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | if (!params->name) { |
| 1048 | printf("SF: Unsupported flash IDs: "); |
| 1049 | printf("manuf %02x, jedec %04x, ext_jedec %04x\n", |
| 1050 | idcode[0], jedec, ext_jedec); |
| 1051 | return -EPROTONOSUPPORT; |
| 1052 | } |
| 1053 | |
Yuan Yao | de4b02f | 2016-03-15 14:36:43 +0800 | [diff] [blame] | 1054 | #ifdef CONFIG_SPI_FLASH_SPANSION |
| 1055 | /* |
| 1056 | * The S25FS-S family physical sectors may be configured as a |
| 1057 | * hybrid combination of eight 4-kB parameter sectors |
| 1058 | * at the top or bottom of the address space with all |
| 1059 | * but one of the remaining sectors being uniform size. |
| 1060 | * The Parameter Sector Erase commands (20h or 21h) must |
| 1061 | * be used to erase the 4-kB parameter sectors individually. |
| 1062 | * The Sector (uniform sector) Erase commands (D8h or DCh) |
| 1063 | * must be used to erase any of the remaining |
| 1064 | * sectors, including the portion of highest or lowest address |
| 1065 | * sector that is not overlaid by the parameter sectors. |
| 1066 | * The uniform sector erase command has no effect on parameter sectors. |
| 1067 | */ |
Prabhakar Kushwaha | 55f426f | 2016-06-03 18:41:28 +0530 | [diff] [blame] | 1068 | if ((jedec == 0x0219 || (jedec == 0x0220)) && |
| 1069 | (ext_jedec & 0xff00) == 0x4d00) { |
Yuan Yao | de4b02f | 2016-03-15 14:36:43 +0800 | [diff] [blame] | 1070 | int ret; |
| 1071 | u8 id[6]; |
| 1072 | |
| 1073 | /* Read the ID codes again, 6 bytes */ |
| 1074 | ret = spi_flash_cmd(flash->spi, CMD_READ_ID, id, sizeof(id)); |
| 1075 | if (ret) |
| 1076 | return -EIO; |
| 1077 | |
| 1078 | ret = memcmp(id, idcode, 5); |
| 1079 | if (ret) |
| 1080 | return -EIO; |
| 1081 | |
| 1082 | /* 0x81: S25FS-S family 0x80: S25FL-S family */ |
| 1083 | if (id[5] == 0x81) { |
| 1084 | ret = spansion_s25fss_disable_4KB_erase(spi); |
| 1085 | if (ret) |
| 1086 | return ret; |
| 1087 | } |
| 1088 | } |
| 1089 | #endif |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1090 | /* Flash powers up read-only, so clear BP# bits */ |
Jagan Teki | b723375 | 2015-09-30 02:01:23 +0530 | [diff] [blame] | 1091 | if (idcode[0] == SPI_FLASH_CFI_MFR_ATMEL || |
| 1092 | idcode[0] == SPI_FLASH_CFI_MFR_MACRONIX || |
| 1093 | idcode[0] == SPI_FLASH_CFI_MFR_SST) |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 1094 | write_sr(flash, 0); |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1095 | |
| 1096 | /* Assign spi data */ |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1097 | flash->name = params->name; |
| 1098 | flash->memory_map = spi->memory_map; |
Jagan Teki | 7d00599 | 2015-12-12 11:51:57 +0530 | [diff] [blame] | 1099 | flash->dual_flash = spi->option; |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1100 | |
| 1101 | /* Assign spi flash flags */ |
| 1102 | if (params->flags & SST_WR) |
| 1103 | flash->flags |= SNOR_F_SST_WR; |
| 1104 | |
| 1105 | /* Assign spi_flash ops */ |
| 1106 | #ifndef CONFIG_DM_SPI_FLASH |
| 1107 | flash->write = spi_flash_cmd_write_ops; |
| 1108 | #if defined(CONFIG_SPI_FLASH_SST) |
| 1109 | if (flash->flags & SNOR_F_SST_WR) { |
Jagan Teki | 71331b3 | 2015-12-13 20:12:45 +0530 | [diff] [blame] | 1110 | if (spi->mode & SPI_TX_BYTE) |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1111 | flash->write = sst_write_bp; |
| 1112 | else |
| 1113 | flash->write = sst_write_wp; |
| 1114 | } |
| 1115 | #endif |
| 1116 | flash->erase = spi_flash_cmd_erase_ops; |
| 1117 | flash->read = spi_flash_cmd_read_ops; |
| 1118 | #endif |
| 1119 | |
| 1120 | /* lock hooks are flash specific - assign them based on idcode0 */ |
| 1121 | switch (idcode[0]) { |
| 1122 | #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST) |
| 1123 | case SPI_FLASH_CFI_MFR_STMICRO: |
| 1124 | case SPI_FLASH_CFI_MFR_SST: |
| 1125 | flash->flash_lock = stm_lock; |
| 1126 | flash->flash_unlock = stm_unlock; |
| 1127 | flash->flash_is_locked = stm_is_locked; |
| 1128 | #endif |
| 1129 | break; |
| 1130 | default: |
| 1131 | debug("SF: Lock ops not supported for %02x flash\n", idcode[0]); |
| 1132 | } |
| 1133 | |
| 1134 | /* Compute the flash size */ |
| 1135 | flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0; |
| 1136 | /* |
| 1137 | * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the |
| 1138 | * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with |
| 1139 | * the 0x4d00 Extended JEDEC code have 512b pages. All of the others |
| 1140 | * have 256b pages. |
| 1141 | */ |
| 1142 | if (ext_jedec == 0x4d00) { |
Prabhakar Kushwaha | 55f426f | 2016-06-03 18:41:28 +0530 | [diff] [blame] | 1143 | if ((jedec == 0x0215) || (jedec == 0x216) || (jedec == 0x220)) |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1144 | flash->page_size = 256; |
| 1145 | else |
| 1146 | flash->page_size = 512; |
| 1147 | } else { |
| 1148 | flash->page_size = 256; |
| 1149 | } |
| 1150 | flash->page_size <<= flash->shift; |
| 1151 | flash->sector_size = params->sector_size << flash->shift; |
| 1152 | flash->size = flash->sector_size * params->nr_sectors << flash->shift; |
| 1153 | #ifdef CONFIG_SF_DUAL_FLASH |
| 1154 | if (flash->dual_flash & SF_DUAL_STACKED_FLASH) |
| 1155 | flash->size <<= 1; |
| 1156 | #endif |
| 1157 | |
Jagan Teki | ca40ea2 | 2016-08-08 17:23:56 +0530 | [diff] [blame^] | 1158 | #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1159 | /* Compute erase sector and command */ |
| 1160 | if (params->flags & SECT_4K) { |
| 1161 | flash->erase_cmd = CMD_ERASE_4K; |
| 1162 | flash->erase_size = 4096 << flash->shift; |
Jagan Teki | ca40ea2 | 2016-08-08 17:23:56 +0530 | [diff] [blame^] | 1163 | } else |
| 1164 | #endif |
| 1165 | { |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1166 | flash->erase_cmd = CMD_ERASE_64K; |
| 1167 | flash->erase_size = flash->sector_size; |
| 1168 | } |
| 1169 | |
| 1170 | /* Now erase size becomes valid sector size */ |
| 1171 | flash->sector_size = flash->erase_size; |
| 1172 | |
Jagan Teki | b678539 | 2016-08-08 16:50:45 +0530 | [diff] [blame] | 1173 | /* Look for read commands */ |
| 1174 | flash->read_cmd = CMD_READ_ARRAY_FAST; |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 1175 | if (spi->mode & SPI_RX_SLOW) |
Jagan Teki | b678539 | 2016-08-08 16:50:45 +0530 | [diff] [blame] | 1176 | flash->read_cmd = CMD_READ_ARRAY_SLOW; |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 1177 | else if (spi->mode & SPI_RX_QUAD && params->flags & RD_QUAD) |
Jagan Teki | b678539 | 2016-08-08 16:50:45 +0530 | [diff] [blame] | 1178 | flash->read_cmd = CMD_READ_QUAD_OUTPUT_FAST; |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 1179 | else if (spi->mode & SPI_RX_DUAL && params->flags & RD_DUAL) |
Jagan Teki | b678539 | 2016-08-08 16:50:45 +0530 | [diff] [blame] | 1180 | flash->read_cmd = CMD_READ_DUAL_OUTPUT_FAST; |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1181 | |
Jagan Teki | b678539 | 2016-08-08 16:50:45 +0530 | [diff] [blame] | 1182 | /* Look for write commands */ |
Jagan Teki | 71331b3 | 2015-12-13 20:12:45 +0530 | [diff] [blame] | 1183 | if (params->flags & WR_QPP && spi->mode & SPI_TX_QUAD) |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1184 | flash->write_cmd = CMD_QUAD_PAGE_PROGRAM; |
| 1185 | else |
| 1186 | /* Go for default supported write cmd */ |
| 1187 | flash->write_cmd = CMD_PAGE_PROGRAM; |
| 1188 | |
| 1189 | /* Set the quad enable bit - only for quad commands */ |
| 1190 | if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) || |
| 1191 | (flash->read_cmd == CMD_READ_QUAD_IO_FAST) || |
| 1192 | (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) { |
Jagan Teki | 7fd4fd6 | 2015-12-13 23:04:46 +0530 | [diff] [blame] | 1193 | ret = set_quad_mode(flash, idcode[0]); |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1194 | if (ret) { |
| 1195 | debug("SF: Fail to set QEB for %02x\n", idcode[0]); |
| 1196 | return -EINVAL; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | /* Read dummy_byte: dummy byte is determined based on the |
| 1201 | * dummy cycles of a particular command. |
| 1202 | * Fast commands - dummy_byte = dummy_cycles/8 |
| 1203 | * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8 |
| 1204 | * For I/O commands except cmd[0] everything goes on no.of lines |
| 1205 | * based on particular command but incase of fast commands except |
| 1206 | * data all go on single line irrespective of command. |
| 1207 | */ |
| 1208 | switch (flash->read_cmd) { |
| 1209 | case CMD_READ_QUAD_IO_FAST: |
| 1210 | flash->dummy_byte = 2; |
| 1211 | break; |
| 1212 | case CMD_READ_ARRAY_SLOW: |
| 1213 | flash->dummy_byte = 0; |
| 1214 | break; |
| 1215 | default: |
| 1216 | flash->dummy_byte = 1; |
| 1217 | } |
| 1218 | |
| 1219 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 1220 | if (params->flags & E_FSR) |
| 1221 | flash->flags |= SNOR_F_USE_FSR; |
| 1222 | #endif |
| 1223 | |
| 1224 | /* Configure the BAR - discover bank cmds and read current bank */ |
| 1225 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagan Teki | dc8ce0f | 2015-09-29 22:29:33 +0530 | [diff] [blame] | 1226 | ret = spi_flash_read_bar(flash, idcode[0]); |
Jagan Teki | e6401d8 | 2015-12-11 21:36:34 +0530 | [diff] [blame] | 1227 | if (ret < 0) |
| 1228 | return ret; |
| 1229 | #endif |
| 1230 | |
| 1231 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 1232 | ret = spi_flash_decode_fdt(gd->fdt_blob, flash); |
| 1233 | if (ret) { |
| 1234 | debug("SF: FDT decode error\n"); |
| 1235 | return -EINVAL; |
| 1236 | } |
| 1237 | #endif |
| 1238 | |
| 1239 | #ifndef CONFIG_SPL_BUILD |
| 1240 | printf("SF: Detected %s with page size ", flash->name); |
| 1241 | print_size(flash->page_size, ", erase size "); |
| 1242 | print_size(flash->erase_size, ", total "); |
| 1243 | print_size(flash->size, ""); |
| 1244 | if (flash->memory_map) |
| 1245 | printf(", mapped at %p", flash->memory_map); |
| 1246 | puts("\n"); |
| 1247 | #endif |
| 1248 | |
| 1249 | #ifndef CONFIG_SPI_FLASH_BAR |
| 1250 | if (((flash->dual_flash == SF_SINGLE_FLASH) && |
| 1251 | (flash->size > SPI_FLASH_16MB_BOUN)) || |
| 1252 | ((flash->dual_flash > SF_SINGLE_FLASH) && |
| 1253 | (flash->size > SPI_FLASH_16MB_BOUN << 1))) { |
| 1254 | puts("SF: Warning - Only lower 16MiB accessible,"); |
| 1255 | puts(" Full access #define CONFIG_SPI_FLASH_BAR\n"); |
| 1256 | } |
| 1257 | #endif |
| 1258 | |
| 1259 | return ret; |
| 1260 | } |