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