David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2014 Google Inc. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * |
| 13 | * Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * |
| 17 | * Neither the name of Google or the names of contributors or |
| 18 | * licensors may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | * This software is provided "AS IS," without a warranty of any kind. |
| 22 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, |
| 23 | * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
| 24 | * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. |
| 25 | * GOOGLE INC AND ITS LICENSORS SHALL NOT BE LIABLE |
| 26 | * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING |
| 27 | * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL |
| 28 | * GOOGLE OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, |
| 29 | * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR |
| 30 | * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF |
| 31 | * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, |
| 32 | * EVEN IF GOOGLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
| 33 | * |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 34 | * s25f.c - Helper functions for Spansion S25FL and S25FS SPI flash chips. |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 35 | * Uses 24 bit addressing for the FS chips and 32 bit addressing for the FL |
| 36 | * chips (which is required by the overlayed sector size devices). |
| 37 | * TODO: Implement fancy hybrid sector architecture helpers. |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 38 | */ |
| 39 | |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 40 | #include <string.h> |
| 41 | |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 42 | #include "chipdrivers.h" |
Mayur Panchal | f479686 | 2019-08-05 15:46:12 +1000 | [diff] [blame] | 43 | #include "hwaccess.h" |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 44 | #include "spi.h" |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 45 | #include "writeprotect.h" |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 46 | |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 47 | /* |
| 48 | * RDAR and WRAR are supported on chips which have more than one set of status |
| 49 | * and control registers and take an address of the register to read/write. |
| 50 | * WRR, RDSR2, and RDCR are used on chips with a more limited set of control/ |
| 51 | * status registers. |
| 52 | * |
| 53 | * WRR is somewhat peculiar. It shares the same opcode as JEDEC_WRSR, and if |
| 54 | * given one data byte (following the opcode) it acts the same way. If it's |
| 55 | * given two data bytes, the first data byte overwrites status register 1 |
| 56 | * and the second data byte overwrites config register 1. |
| 57 | */ |
| 58 | #define CMD_WRR 0x01 |
| 59 | #define CMD_WRDI 0x04 |
| 60 | #define CMD_RDSR2 0x07 /* note: read SR1 with JEDEC RDSR opcode */ |
| 61 | #define CMD_RDCR 0x35 |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 62 | #define CMD_RDAR 0x65 |
| 63 | #define CMD_WRAR 0x71 |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 64 | |
| 65 | /* TODO: For now, commands which use an address assume 24-bit addressing */ |
| 66 | #define CMD_WRR_LEN 3 |
| 67 | #define CMD_WRDI_LEN 1 |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 68 | #define CMD_RDAR_LEN 4 |
| 69 | #define CMD_WRAR_LEN 5 |
| 70 | |
| 71 | #define CMD_RSTEN 0x66 |
| 72 | #define CMD_RST 0x99 |
| 73 | |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 74 | #define CR1NV_ADDR 0x000002 |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 75 | #define CR1_BPNV_O (1 << 3) |
| 76 | #define CR1_TBPROT_O (1 << 5) |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 77 | #define CR3NV_ADDR 0x000004 |
| 78 | #define CR3NV_20H_NV (1 << 3) |
| 79 | |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 80 | /* See "Embedded Algorithm Performance Tables for additional timing specs. */ |
| 81 | #define T_W 145 * 1000 /* NV register write time (145ms) */ |
| 82 | #define T_RPH 35 /* Reset pulse hold time (35us) */ |
| 83 | #define S25FS_T_SE 145 * 1000 /* Sector Erase Time (145ms) */ |
| 84 | #define S25FL_T_SE 130 * 1000 /* Sector Erase Time (130ms) */ |
| 85 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 86 | static int s25f_legacy_software_reset(const struct flashctx *flash) |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 87 | { |
| 88 | int result; |
| 89 | struct spi_command cmds[] = { |
| 90 | { |
| 91 | .writecnt = 1, |
| 92 | .writearr = (const unsigned char[]){ CMD_RSTEN }, |
| 93 | .readcnt = 0, |
| 94 | .readarr = NULL, |
| 95 | }, { |
| 96 | .writecnt = 1, |
| 97 | .writearr = (const unsigned char[]){ 0xf0 }, |
| 98 | .readcnt = 0, |
| 99 | .readarr = NULL, |
| 100 | }, { |
| 101 | .writecnt = 0, |
| 102 | .writearr = NULL, |
| 103 | .readcnt = 0, |
| 104 | .readarr = NULL, |
| 105 | }}; |
| 106 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 107 | result = spi_send_multicommand(flash, cmds); |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 108 | if (result) { |
| 109 | msg_cerr("%s failed during command execution\n", __func__); |
| 110 | return result; |
| 111 | } |
| 112 | |
| 113 | /* Allow time for reset command to execute. The datasheet specifies |
| 114 | * Trph = 35us, double that to be safe. */ |
| 115 | programmer_delay(T_RPH * 2); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /* "Legacy software reset" is disabled by default on S25FS, use this instead. */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 121 | static int s25fs_software_reset(struct flashctx *flash) |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 122 | { |
| 123 | int result; |
| 124 | struct spi_command cmds[] = { |
| 125 | { |
| 126 | .writecnt = 1, |
| 127 | .writearr = (const unsigned char[]){ CMD_RSTEN }, |
| 128 | .readcnt = 0, |
| 129 | .readarr = NULL, |
| 130 | }, { |
| 131 | .writecnt = 1, |
| 132 | .writearr = (const unsigned char[]){ CMD_RST }, |
| 133 | .readcnt = 0, |
| 134 | .readarr = NULL, |
| 135 | }, { |
| 136 | .writecnt = 0, |
| 137 | .writearr = NULL, |
| 138 | .readcnt = 0, |
| 139 | .readarr = NULL, |
| 140 | }}; |
| 141 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 142 | result = spi_send_multicommand(flash, cmds); |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 143 | if (result) { |
| 144 | msg_cerr("%s failed during command execution\n", __func__); |
| 145 | return result; |
| 146 | } |
| 147 | |
| 148 | /* Allow time for reset command to execute. Double tRPH to be safe. */ |
| 149 | programmer_delay(T_RPH * 2); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 154 | static int s25f_poll_status(const struct flashctx *flash) |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 155 | { |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 156 | uint8_t tmp = spi_read_status_register(flash); |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 157 | |
Edward O'Callaghan | 8b5e473 | 2019-03-05 15:27:53 +1100 | [diff] [blame] | 158 | while (tmp & SPI_SR_WIP) { |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 159 | /* |
| 160 | * The WIP bit on S25F chips remains set to 1 if erase or |
| 161 | * programming errors occur, so we must check for those |
| 162 | * errors here. If an error is encountered, do a software |
| 163 | * reset to clear WIP and other volatile bits, otherwise |
| 164 | * the chip will be unresponsive to further commands. |
| 165 | */ |
Edward O'Callaghan | 1945f1e | 2019-03-18 13:12:51 +1100 | [diff] [blame] | 166 | if (tmp & SPI_SR_ERA_ERR) { |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 167 | msg_cerr("Erase error occurred\n"); |
| 168 | s25f_legacy_software_reset(flash); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | if (tmp & (1 << 6)) { |
| 173 | msg_cerr("Programming error occurred\n"); |
| 174 | s25f_legacy_software_reset(flash); |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | programmer_delay(1000 * 10); |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 179 | tmp = spi_read_status_register(flash); |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /* "Read Any Register" instruction only supported on S25FS */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 186 | static int s25fs_read_cr(const struct flashctx *flash, uint32_t addr) |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 187 | { |
| 188 | int result; |
| 189 | uint8_t cfg; |
| 190 | /* By default, 8 dummy cycles are necessary for variable-latency |
| 191 | commands such as RDAR (see CR2NV[3:0]). */ |
| 192 | unsigned char read_cr_cmd[] = { |
| 193 | CMD_RDAR, |
| 194 | (addr >> 16) & 0xff, |
| 195 | (addr >> 8) & 0xff, |
| 196 | (addr & 0xff), |
| 197 | 0x00, 0x00, 0x00, 0x00, |
| 198 | 0x00, 0x00, 0x00, 0x00, |
| 199 | }; |
| 200 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 201 | result = spi_send_command(flash, sizeof(read_cr_cmd), 1, read_cr_cmd, &cfg); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 202 | if (result) { |
| 203 | msg_cerr("%s failed during command execution at address 0x%x\n", |
| 204 | __func__, addr); |
David Hendricks | 688b5e2 | 2014-12-12 11:15:44 -0800 | [diff] [blame] | 205 | return -1; |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return cfg; |
| 209 | } |
| 210 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 211 | static int s25f_read_cr1(const struct flashctx *flash) |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 212 | { |
| 213 | int result; |
| 214 | uint8_t cfg; |
| 215 | unsigned char read_cr_cmd[] = { CMD_RDCR }; |
| 216 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 217 | result = spi_send_command(flash, sizeof(read_cr_cmd), 1, read_cr_cmd, &cfg); |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 218 | if (result) { |
| 219 | msg_cerr("%s failed during command execution\n", __func__); |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | return cfg; |
| 224 | } |
| 225 | |
| 226 | /* "Write Any Register" instruction only supported on S25FS */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 227 | static int s25fs_write_cr(const struct flashctx *flash, |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 228 | uint32_t addr, uint8_t data) |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 229 | { |
| 230 | int result; |
| 231 | struct spi_command cmds[] = { |
| 232 | { |
| 233 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 234 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 235 | .readcnt = 0, |
| 236 | .readarr = NULL, |
| 237 | }, { |
| 238 | .writecnt = CMD_WRAR_LEN, |
| 239 | .writearr = (const unsigned char[]){ |
| 240 | CMD_WRAR, |
| 241 | (addr >> 16) & 0xff, |
| 242 | (addr >> 8) & 0xff, |
| 243 | (addr & 0xff), |
| 244 | data |
| 245 | }, |
| 246 | .readcnt = 0, |
| 247 | .readarr = NULL, |
| 248 | }, { |
| 249 | .writecnt = 0, |
| 250 | .writearr = NULL, |
| 251 | .readcnt = 0, |
| 252 | .readarr = NULL, |
| 253 | }}; |
| 254 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 255 | result = spi_send_multicommand(flash, cmds); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 256 | if (result) { |
| 257 | msg_cerr("%s failed during command execution at address 0x%x\n", |
| 258 | __func__, addr); |
David Hendricks | 688b5e2 | 2014-12-12 11:15:44 -0800 | [diff] [blame] | 259 | return -1; |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 260 | } |
| 261 | |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 262 | programmer_delay(T_W); |
| 263 | return s25f_poll_status(flash); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 266 | static int s25f_write_cr1(const struct flashctx *flash, uint8_t data) |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 267 | { |
| 268 | int result; |
| 269 | struct spi_command cmds[] = { |
| 270 | { |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 271 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 272 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 273 | .readcnt = 0, |
| 274 | .readarr = NULL, |
| 275 | }, { |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 276 | .writecnt = CMD_WRR_LEN, |
| 277 | .writearr = (const unsigned char[]){ |
| 278 | CMD_WRR, |
Ramya Vijaykumar | 4af3f82 | 2016-01-27 11:51:27 +0530 | [diff] [blame] | 279 | spi_read_status_register(flash), |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 280 | data, |
| 281 | }, |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 282 | .readcnt = 0, |
| 283 | .readarr = NULL, |
| 284 | }, { |
| 285 | .writecnt = 0, |
| 286 | .writearr = NULL, |
| 287 | .readcnt = 0, |
| 288 | .readarr = NULL, |
| 289 | }}; |
| 290 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 291 | result = spi_send_multicommand(flash, cmds); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 292 | if (result) { |
| 293 | msg_cerr("%s failed during command execution\n", __func__); |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 294 | return -1; |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 295 | } |
| 296 | |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 297 | programmer_delay(T_W); |
| 298 | return s25f_poll_status(flash); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 301 | static int s25fs_restore_cr3nv(struct flashctx *flash, uint8_t cfg) |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 302 | { |
| 303 | int ret = 0; |
| 304 | |
| 305 | msg_cdbg("Restoring CR3NV value to 0x%02x\n", cfg); |
David Hendricks | 636c74a | 2014-12-12 11:30:00 -0800 | [diff] [blame] | 306 | ret |= s25fs_write_cr(flash, CR3NV_ADDR, cfg); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 307 | ret |= s25fs_software_reset(flash); |
| 308 | return ret; |
| 309 | } |
| 310 | |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 311 | /* returns state of top/bottom block protection, or <0 to indicate error */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 312 | static int s25f_get_tbprot_o(const struct flashctx *flash) |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 313 | { |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 314 | int cr1 = s25f_read_cr1(flash); |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 315 | |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 316 | if (cr1 < 0) |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 317 | return -1; |
| 318 | |
| 319 | /* |
| 320 | * 1 = BP starts at bottom (low address) |
| 321 | * 0 = BP start at top (high address) |
| 322 | */ |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 323 | return cr1 & CR1_TBPROT_O ? 1 : 0; |
David Hendricks | a988485 | 2014-12-11 15:31:12 -0800 | [diff] [blame] | 324 | } |
| 325 | |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 326 | /* fills modifier_bits struct, returns 0 to indicate success */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 327 | int s25f_get_modifier_bits(const struct flashctx *flash, |
Edward O'Callaghan | 9c4c9a5 | 2019-12-04 18:18:01 +1100 | [diff] [blame^] | 328 | struct modifier_bits *m) |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 329 | { |
| 330 | int tmp; |
| 331 | |
| 332 | memset(m, 0, sizeof(*m)); |
| 333 | |
| 334 | tmp = s25f_get_tbprot_o(flash); |
| 335 | if (tmp < 0) |
| 336 | return -1; |
| 337 | m->tb = tmp; |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 342 | int s25f_set_modifier_bits(const struct flashctx *flash, |
Edward O'Callaghan | 9c4c9a5 | 2019-12-04 18:18:01 +1100 | [diff] [blame^] | 343 | struct modifier_bits *m) |
David Hendricks | 148a4bf | 2015-03-13 21:02:42 -0700 | [diff] [blame] | 344 | { |
| 345 | int cr1, cr1_orig; |
| 346 | |
| 347 | cr1 = cr1_orig = s25f_read_cr1(flash); |
| 348 | if (cr1 < 0) |
| 349 | return -1; |
| 350 | |
| 351 | /* |
| 352 | * Clear BPNV so that setting BP2-0 in status register gets |
| 353 | * written to non-volatile memory. |
| 354 | * |
| 355 | * For TBPROT: |
| 356 | * 1 = BP starts at bottom (low address) |
| 357 | * 0 = BP start at top (high address) |
| 358 | */ |
| 359 | cr1 &= ~(CR1_BPNV_O | CR1_TBPROT_O); |
| 360 | cr1 |= m->tb ? CR1_TBPROT_O : 0; |
| 361 | |
| 362 | if (cr1 != cr1_orig) { |
| 363 | msg_cdbg("%s: setting cr1 bits to 0x%02x\n", __func__, cr1); |
| 364 | if (s25f_write_cr1(flash, cr1) < 0) |
| 365 | return -1; |
| 366 | if (s25f_read_cr1(flash) != cr1) { |
| 367 | msg_cerr("%s: failed to set CR1 value\n", __func__); |
| 368 | return -1; |
| 369 | } |
| 370 | } else { |
| 371 | msg_cdbg("%s: cr1 bits already match desired value: " |
| 372 | "0x%02x\n", __func__, cr1); |
| 373 | } |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 378 | int s25fs_block_erase_d8(struct flashctx *flash, |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 379 | unsigned int addr, unsigned int blocklen) |
| 380 | { |
| 381 | unsigned char cfg; |
| 382 | int result; |
| 383 | static int cr3nv_checked = 0; |
| 384 | |
| 385 | struct spi_command erase_cmds[] = { |
| 386 | { |
| 387 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 388 | .writearr = (const unsigned char[]){ JEDEC_WREN }, |
| 389 | .readcnt = 0, |
| 390 | .readarr = NULL, |
| 391 | }, { |
| 392 | .writecnt = JEDEC_BE_D8_OUTSIZE, |
| 393 | .writearr = (const unsigned char[]){ |
| 394 | JEDEC_BE_D8, |
| 395 | (addr >> 16) & 0xff, |
| 396 | (addr >> 8) & 0xff, |
| 397 | (addr & 0xff) |
| 398 | }, |
| 399 | .readcnt = 0, |
| 400 | .readarr = NULL, |
| 401 | }, { |
| 402 | .writecnt = 0, |
| 403 | .writearr = NULL, |
| 404 | .readcnt = 0, |
| 405 | .readarr = NULL, |
| 406 | }}; |
| 407 | |
| 408 | /* Check if hybrid sector architecture is in use and, if so, |
| 409 | * switch to uniform sectors. */ |
| 410 | if (!cr3nv_checked) { |
David Hendricks | 636c74a | 2014-12-12 11:30:00 -0800 | [diff] [blame] | 411 | cfg = s25fs_read_cr(flash, CR3NV_ADDR); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 412 | if (!(cfg & CR3NV_20H_NV)) { |
David Hendricks | 636c74a | 2014-12-12 11:30:00 -0800 | [diff] [blame] | 413 | s25fs_write_cr(flash, CR3NV_ADDR, cfg | CR3NV_20H_NV); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 414 | s25fs_software_reset(flash); |
| 415 | |
David Hendricks | 636c74a | 2014-12-12 11:30:00 -0800 | [diff] [blame] | 416 | cfg = s25fs_read_cr(flash, CR3NV_ADDR); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 417 | if (!(cfg & CR3NV_20H_NV)) { |
| 418 | msg_cerr("%s: Unable to enable uniform " |
| 419 | "block sizes.\n", __func__); |
| 420 | return 1; |
| 421 | } |
| 422 | |
| 423 | msg_cdbg("\n%s: CR3NV updated (0x%02x -> 0x%02x)\n", |
| 424 | __func__, cfg, |
David Hendricks | 636c74a | 2014-12-12 11:30:00 -0800 | [diff] [blame] | 425 | s25fs_read_cr(flash, CR3NV_ADDR)); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 426 | /* Restore CR3V when flashrom exits */ |
| 427 | register_chip_restore(s25fs_restore_cr3nv, flash, cfg); |
| 428 | } |
| 429 | |
| 430 | cr3nv_checked = 1; |
| 431 | } |
| 432 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 433 | result = spi_send_multicommand(flash, erase_cmds); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 434 | if (result) { |
| 435 | msg_cerr("%s failed during command execution at address 0x%x\n", |
| 436 | __func__, addr); |
| 437 | return result; |
| 438 | } |
| 439 | |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 440 | programmer_delay(S25FS_T_SE); |
| 441 | return s25f_poll_status(flash); |
David Hendricks | 398714f | 2014-07-03 17:49:41 -0700 | [diff] [blame] | 442 | } |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 443 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 444 | int s25fl_block_erase(struct flashctx *flash, |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 445 | unsigned int addr, unsigned int blocklen) |
| 446 | { |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 447 | int result; |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 448 | |
| 449 | struct spi_command erase_cmds[] = { |
| 450 | { |
| 451 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 452 | .writearr = (const unsigned char[]){ |
| 453 | JEDEC_WREN |
| 454 | }, |
| 455 | .readcnt = 0, |
| 456 | .readarr = NULL, |
| 457 | }, { |
| 458 | .writecnt = JEDEC_BE_DC_OUTSIZE, |
| 459 | .writearr = (const unsigned char[]){ |
| 460 | JEDEC_BE_DC, |
| 461 | (addr >> 24) & 0xff, |
| 462 | (addr >> 16) & 0xff, |
| 463 | (addr >> 8) & 0xff, |
| 464 | (addr & 0xff) |
| 465 | }, |
| 466 | .readcnt = 0, |
| 467 | .readarr = NULL, |
| 468 | }, { |
| 469 | .writecnt = 0, |
| 470 | .readcnt = 0, |
| 471 | } |
| 472 | }; |
| 473 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 474 | result = spi_send_multicommand(flash, erase_cmds); |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 475 | if (result) { |
| 476 | msg_cerr("%s failed during command execution at address 0x%x\n", |
| 477 | __func__, addr); |
| 478 | return result; |
| 479 | } |
| 480 | |
David Hendricks | 43d9afd | 2015-03-13 20:54:23 -0700 | [diff] [blame] | 481 | programmer_delay(S25FL_T_SE); |
| 482 | return s25f_poll_status(flash); |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 486 | int probe_spi_big_spansion(struct flashctx *flash) |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 487 | { |
| 488 | static const unsigned char cmd = JEDEC_RDID; |
| 489 | int ret; |
| 490 | unsigned char dev_id[6]; /* We care only about 6 first bytes */ |
| 491 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 492 | ret = spi_send_command(flash, sizeof(cmd), sizeof(dev_id), &cmd, dev_id); |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 493 | |
| 494 | if (!ret) { |
| 495 | int i; |
| 496 | |
| 497 | for (i = 0; i < sizeof(dev_id); i++) |
| 498 | msg_gdbg(" 0x%02x", dev_id[i]); |
| 499 | msg_gdbg(".\n"); |
| 500 | |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 501 | if (dev_id[0] == flash->chip->manufacture_id) { |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 502 | union { |
| 503 | uint8_t array[4]; |
| 504 | uint32_t whole; |
| 505 | } model_id; |
| 506 | |
| 507 | /* |
| 508 | * The structure of the RDID output is as follows: |
| 509 | * |
| 510 | * offset value meaning |
| 511 | * 00h 01h Manufacturer ID for Spansion |
| 512 | * 01h 20h 128 Mb capacity |
| 513 | * 01h 02h 256 Mb capacity |
| 514 | * 02h 18h 128 Mb capacity |
| 515 | * 02h 19h 256 Mb capacity |
| 516 | * 03h 4Dh Full size of the RDID output (ignored) |
| 517 | * 04h 00h FS: 256-kB physical sectors |
| 518 | * 04h 01h FS: 64-kB physical sectors |
| 519 | * 04h 00h FL: 256-kB physical sectors |
| 520 | * 04h 01h FL: Mix of 64-kB and 4KB overlayed sectors |
| 521 | * 05h 80h FL family |
| 522 | * 05h 81h FS family |
| 523 | * |
| 524 | * Need to use bytes 1, 2, 4, and 5 to properly identify one of eight |
| 525 | * possible chips: |
| 526 | * |
| 527 | * 2 types * 2 possible sizes * 2 possible sector layouts |
| 528 | * |
| 529 | */ |
| 530 | memcpy(model_id.array, dev_id + 1, 2); |
| 531 | memcpy(model_id.array + 2, dev_id + 4, 2); |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 532 | if (be_to_cpu32(model_id.whole) == flash->chip->model_id) |
Vadim Bendebury | 3a50116 | 2014-10-21 20:38:13 -0700 | [diff] [blame] | 533 | return 1; |
| 534 | } |
| 535 | } |
| 536 | return 0; |
| 537 | } |