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