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