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