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