Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * It handles everything related to status registers of the JEDEC family 25. |
| 4 | * |
| 5 | * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger |
| 6 | * Copyright (C) 2008 coresystems GmbH |
| 7 | * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl> |
| 8 | * Copyright (C) 2012 Stefan Tauner |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; version 2 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "flash.h" |
| 21 | #include "chipdrivers.h" |
| 22 | #include "spi.h" |
| 23 | |
| 24 | /* === Generic functions === */ |
Nico Huber | 3f3c1f3 | 2022-05-28 16:48:26 +0200 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Writing SR2 or higher with an extended WRSR command requires |
| 28 | * writing all lower SRx along with it, so just read the lower |
| 29 | * SRx and write them back. |
| 30 | */ |
| 31 | static int spi_prepare_wrsr_ext( |
| 32 | uint8_t write_cmd[4], size_t *const write_cmd_len, |
| 33 | const struct flashctx *const flash, |
| 34 | const enum flash_reg reg, const uint8_t value) |
| 35 | { |
| 36 | enum flash_reg reg_it; |
| 37 | size_t i = 0; |
| 38 | |
| 39 | write_cmd[i++] = JEDEC_WRSR; |
| 40 | |
| 41 | for (reg_it = STATUS1; reg_it < reg; ++reg_it) { |
| 42 | uint8_t sr; |
| 43 | |
| 44 | if (spi_read_register(flash, reg_it, &sr)) { |
| 45 | msg_cerr("Writing SR%d failed: failed to read SR%d for writeback.\n", |
| 46 | reg - STATUS1 + 1, reg_it - STATUS1 + 1); |
| 47 | return 1; |
| 48 | } |
| 49 | write_cmd[i++] = sr; |
| 50 | } |
| 51 | |
| 52 | write_cmd[i++] = value; |
| 53 | *write_cmd_len = i; |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 58 | int spi_write_register(const struct flashctx *flash, enum flash_reg reg, uint8_t value) |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 59 | { |
| 60 | int feature_bits = flash->chip->feature_bits; |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 61 | |
Nico Huber | 3f3c1f3 | 2022-05-28 16:48:26 +0200 | [diff] [blame] | 62 | uint8_t write_cmd[4]; |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 63 | size_t write_cmd_len = 0; |
| 64 | |
| 65 | /* |
| 66 | * Create SPI write command sequence based on the destination register |
| 67 | * and the chip's supported command set. |
| 68 | */ |
| 69 | switch (reg) { |
| 70 | case STATUS1: |
| 71 | write_cmd[0] = JEDEC_WRSR; |
| 72 | write_cmd[1] = value; |
| 73 | write_cmd_len = JEDEC_WRSR_OUTSIZE; |
| 74 | break; |
Nikolai Artemiev | 9de3f87 | 2021-10-20 22:32:25 +1100 | [diff] [blame] | 75 | case STATUS2: |
| 76 | if (feature_bits & FEATURE_WRSR2) { |
| 77 | write_cmd[0] = JEDEC_WRSR2; |
| 78 | write_cmd[1] = value; |
| 79 | write_cmd_len = JEDEC_WRSR2_OUTSIZE; |
| 80 | break; |
| 81 | } |
Nico Huber | 3f3c1f3 | 2022-05-28 16:48:26 +0200 | [diff] [blame] | 82 | if (feature_bits & FEATURE_WRSR_EXT2) { |
| 83 | if (spi_prepare_wrsr_ext(write_cmd, &write_cmd_len, flash, reg, value)) |
Nikolai Artemiev | 9de3f87 | 2021-10-20 22:32:25 +1100 | [diff] [blame] | 84 | return 1; |
Nikolai Artemiev | 9de3f87 | 2021-10-20 22:32:25 +1100 | [diff] [blame] | 85 | break; |
| 86 | } |
| 87 | msg_cerr("Cannot write SR2: unsupported by chip\n"); |
| 88 | return 1; |
Sergii Dmytruk | 0b2e7dd | 2021-12-19 18:37:51 +0200 | [diff] [blame] | 89 | case STATUS3: |
Nico Huber | 3f3c1f3 | 2022-05-28 16:48:26 +0200 | [diff] [blame] | 90 | if (feature_bits & FEATURE_WRSR3) { |
| 91 | write_cmd[0] = JEDEC_WRSR3; |
| 92 | write_cmd[1] = value; |
| 93 | write_cmd_len = JEDEC_WRSR3_OUTSIZE; |
| 94 | break; |
| 95 | } |
| 96 | if ((feature_bits & FEATURE_WRSR_EXT3) == FEATURE_WRSR_EXT3) { |
| 97 | if (spi_prepare_wrsr_ext(write_cmd, &write_cmd_len, flash, reg, value)) |
| 98 | return 1; |
| 99 | break; |
| 100 | } |
| 101 | msg_cerr("Cannot write SR3: unsupported by chip\n"); |
| 102 | return 1; |
Sergii Dmytruk | 3d728e7 | 2021-11-27 15:14:27 +0200 | [diff] [blame] | 103 | case SECURITY: |
| 104 | /* |
| 105 | * Security register doesn't have a normal write operation. Instead, |
| 106 | * there are separate commands that set individual OTP bits. |
| 107 | */ |
| 108 | msg_cerr("Cannot write SECURITY: unsupported by design\n"); |
| 109 | return 1; |
Sergii Dmytruk | bd72a47 | 2022-07-24 17:11:05 +0300 | [diff] [blame^] | 110 | case CONFIG: |
| 111 | /* |
| 112 | * This one is read via a separate command, but written as if it's SR2 |
| 113 | * in FEATURE_WRSR_EXT2 case of WRSR command. |
| 114 | */ |
| 115 | write_cmd[0] = JEDEC_WRSR; |
| 116 | if (spi_read_register(flash, STATUS1, &write_cmd[1])) { |
| 117 | msg_cerr("Writing CONFIG failed: failed to read SR1 for writeback.\n"); |
| 118 | return 1; |
| 119 | } |
| 120 | write_cmd[2] = value; |
| 121 | write_cmd_len = 3; |
| 122 | break; |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 123 | default: |
| 124 | msg_cerr("Cannot write register: unknown register\n"); |
| 125 | return 1; |
| 126 | } |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 127 | |
Nikolai Artemiev | a1d6865 | 2021-11-22 13:18:49 +1100 | [diff] [blame] | 128 | uint8_t enable_cmd; |
| 129 | if (feature_bits & FEATURE_WRSR_WREN) { |
| 130 | enable_cmd = JEDEC_WREN; |
| 131 | } else if (feature_bits & FEATURE_WRSR_EWSR) { |
| 132 | enable_cmd = JEDEC_EWSR; |
| 133 | } else { |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 134 | msg_cdbg("Missing status register write definition, assuming " |
| 135 | "EWSR is needed\n"); |
Nikolai Artemiev | a1d6865 | 2021-11-22 13:18:49 +1100 | [diff] [blame] | 136 | enable_cmd = JEDEC_EWSR; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 137 | } |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 138 | |
Nikolai Artemiev | a1d6865 | 2021-11-22 13:18:49 +1100 | [diff] [blame] | 139 | struct spi_command cmds[] = { |
| 140 | { |
| 141 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 142 | .writearr = &enable_cmd, |
| 143 | .readcnt = 0, |
| 144 | .readarr = NULL, |
| 145 | }, { |
| 146 | .writecnt = write_cmd_len, |
| 147 | .writearr = write_cmd, |
| 148 | .readcnt = 0, |
| 149 | .readarr = NULL, |
| 150 | }, { |
| 151 | .writecnt = 0, |
| 152 | .writearr = NULL, |
| 153 | .readcnt = 0, |
| 154 | .readarr = NULL, |
| 155 | }}; |
| 156 | |
| 157 | int result = spi_send_multicommand(flash, cmds); |
| 158 | if (result) { |
| 159 | msg_cerr("%s failed during command execution\n", __func__); |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * WRSR performs a self-timed erase before the changes take effect. |
| 165 | * This may take 50-85 ms in most cases, and some chips apparently |
| 166 | * allow running RDSR only once. Therefore pick an initial delay of |
| 167 | * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed. |
| 168 | * |
| 169 | * Newer chips with multiple status registers (SR2 etc.) are unlikely |
| 170 | * to have problems with multiple RDSR commands, so only wait for the |
| 171 | * initial 100 ms if the register we wrote to was SR1. |
| 172 | */ |
| 173 | int delay_ms = 5000; |
| 174 | if (reg == STATUS1) { |
| 175 | programmer_delay(100 * 1000); |
| 176 | delay_ms -= 100; |
| 177 | } |
| 178 | |
| 179 | for (; delay_ms > 0; delay_ms -= 10) { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 180 | uint8_t status; |
| 181 | result = spi_read_register(flash, STATUS1, &status); |
| 182 | if (result) |
| 183 | return result; |
| 184 | if ((status & SPI_SR_WIP) == 0) |
Nikolai Artemiev | a1d6865 | 2021-11-22 13:18:49 +1100 | [diff] [blame] | 185 | return 0; |
| 186 | programmer_delay(10 * 1000); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | msg_cerr("Error: WIP bit after WRSR never cleared\n"); |
| 191 | return TIMEOUT_ERROR; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 194 | int spi_read_register(const struct flashctx *flash, enum flash_reg reg, uint8_t *value) |
| 195 | { |
Nikolai Artemiev | 9de3f87 | 2021-10-20 22:32:25 +1100 | [diff] [blame] | 196 | int feature_bits = flash->chip->feature_bits; |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 197 | uint8_t read_cmd; |
| 198 | |
| 199 | switch (reg) { |
| 200 | case STATUS1: |
| 201 | read_cmd = JEDEC_RDSR; |
| 202 | break; |
Nikolai Artemiev | 9de3f87 | 2021-10-20 22:32:25 +1100 | [diff] [blame] | 203 | case STATUS2: |
Nico Huber | 3f3c1f3 | 2022-05-28 16:48:26 +0200 | [diff] [blame] | 204 | if (feature_bits & (FEATURE_WRSR_EXT2 | FEATURE_WRSR2)) { |
Nikolai Artemiev | 9de3f87 | 2021-10-20 22:32:25 +1100 | [diff] [blame] | 205 | read_cmd = JEDEC_RDSR2; |
| 206 | break; |
| 207 | } |
| 208 | msg_cerr("Cannot read SR2: unsupported by chip\n"); |
| 209 | return 1; |
Sergii Dmytruk | 0b2e7dd | 2021-12-19 18:37:51 +0200 | [diff] [blame] | 210 | case STATUS3: |
Nico Huber | 3f3c1f3 | 2022-05-28 16:48:26 +0200 | [diff] [blame] | 211 | if ((feature_bits & FEATURE_WRSR_EXT3) == FEATURE_WRSR_EXT3 |
| 212 | || (feature_bits & FEATURE_WRSR3)) { |
| 213 | read_cmd = JEDEC_RDSR3; |
| 214 | break; |
| 215 | } |
| 216 | msg_cerr("Cannot read SR3: unsupported by chip\n"); |
| 217 | return 1; |
Sergii Dmytruk | 3d728e7 | 2021-11-27 15:14:27 +0200 | [diff] [blame] | 218 | case SECURITY: |
| 219 | read_cmd = JEDEC_RDSCUR; |
| 220 | break; |
Sergii Dmytruk | bd72a47 | 2022-07-24 17:11:05 +0300 | [diff] [blame^] | 221 | case CONFIG: |
| 222 | read_cmd = JEDEC_RDCR; |
| 223 | break; |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 224 | default: |
| 225 | msg_cerr("Cannot read register: unknown register\n"); |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | /* FIXME: No workarounds for driver/hardware bugs in generic code. */ |
| 230 | /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */ |
| 231 | uint8_t readarr[2]; |
| 232 | |
| 233 | int ret = spi_send_command(flash, sizeof(read_cmd), sizeof(readarr), &read_cmd, readarr); |
| 234 | if (ret) { |
| 235 | msg_cerr("Register read failed!\n"); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | *value = readarr[0]; |
| 240 | return 0; |
| 241 | } |
| 242 | |
Nikolai Artemiev | 721a4f3 | 2020-12-14 07:39:02 +1100 | [diff] [blame] | 243 | static int spi_restore_status(struct flashctx *flash, uint8_t status) |
| 244 | { |
| 245 | msg_cdbg("restoring chip status (0x%02x)\n", status); |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 246 | return spi_write_register(flash, STATUS1, status); |
Nikolai Artemiev | 721a4f3 | 2020-12-14 07:39:02 +1100 | [diff] [blame] | 247 | } |
| 248 | |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 249 | /* A generic block protection disable. |
| 250 | * Tests if a protection is enabled with the block protection mask (bp_mask) and returns success otherwise. |
| 251 | * Tests if the register bits are locked with the lock_mask (lock_mask). |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 252 | * Tests if a hardware protection is active (i.e. low pin/high bit value) with the write protection mask |
| 253 | * (wp_mask) and bails out in that case. |
| 254 | * If there are register lock bits set we try to disable them by unsetting those bits of the previous register |
| 255 | * contents that are set in the lock_mask. We then check if removing the lock bits has worked and continue as if |
| 256 | * they never had been engaged: |
| 257 | * If the lock bits are out of the way try to disable engaged protections. |
| 258 | * To support uncommon global unprotects (e.g. on most AT2[56]xx1(A)) unprotect_mask can be used to force |
| 259 | * bits to 0 additionally to those set in bp_mask and lock_mask. Only bits set in unprotect_mask are potentially |
| 260 | * preserved when doing the final unprotect. |
| 261 | * |
| 262 | * To sum up: |
| 263 | * bp_mask: set those bits that correspond to the bits in the status register that indicate an active protection |
| 264 | * (which should be unset after this function returns). |
| 265 | * lock_mask: set the bits that correspond to the bits that lock changing the bits above. |
| 266 | * wp_mask: set the bits that correspond to bits indicating non-software revocable protections. |
| 267 | * unprotect_mask: set the bits that should be preserved if possible when unprotecting. |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 268 | */ |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 269 | static int spi_disable_blockprotect_generic(struct flashctx *flash, uint8_t bp_mask, uint8_t lock_mask, uint8_t wp_mask, uint8_t unprotect_mask) |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 270 | { |
| 271 | uint8_t status; |
| 272 | int result; |
| 273 | |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 274 | int ret = spi_read_register(flash, STATUS1, &status); |
| 275 | if (ret) |
| 276 | return ret; |
| 277 | |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 278 | if ((status & bp_mask) == 0) { |
| 279 | msg_cdbg2("Block protection is disabled.\n"); |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 280 | return 0; |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 281 | } |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 282 | |
Nikolai Artemiev | 721a4f3 | 2020-12-14 07:39:02 +1100 | [diff] [blame] | 283 | /* Restore status register content upon exit in finalize_flash_access(). */ |
| 284 | register_chip_restore(spi_restore_status, flash, status); |
| 285 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 286 | msg_cdbg("Some block protection in effect, disabling... "); |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 287 | if ((status & lock_mask) != 0) { |
| 288 | msg_cdbg("\n\tNeed to disable the register lock first... "); |
| 289 | if (wp_mask != 0 && (status & wp_mask) == 0) { |
| 290 | msg_cerr("Hardware protection is active, disabling write protection is impossible.\n"); |
| 291 | return 1; |
| 292 | } |
| 293 | /* All bits except the register lock bit (often called SPRL, SRWD, WPEN) are readonly. */ |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 294 | result = spi_write_register(flash, STATUS1, status & ~lock_mask); |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 295 | if (result) { |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 296 | msg_cerr("Could not write status register 1.\n"); |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 297 | return result; |
| 298 | } |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 299 | |
| 300 | ret = spi_read_register(flash, STATUS1, &status); |
| 301 | if (ret) |
| 302 | return ret; |
| 303 | |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 304 | if ((status & lock_mask) != 0) { |
| 305 | msg_cerr("Unsetting lock bit(s) failed.\n"); |
| 306 | return 1; |
| 307 | } |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 308 | msg_cdbg("done.\n"); |
| 309 | } |
| 310 | /* Global unprotect. Make sure to mask the register lock bit as well. */ |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 311 | result = spi_write_register(flash, STATUS1, status & ~(bp_mask | lock_mask) & unprotect_mask); |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 312 | if (result) { |
Nikolai Artemiev | 0167522 | 2021-10-20 22:30:41 +1100 | [diff] [blame] | 313 | msg_cerr("Could not write status register 1.\n"); |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 314 | return result; |
| 315 | } |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 316 | |
| 317 | ret = spi_read_register(flash, STATUS1, &status); |
| 318 | if (ret) |
| 319 | return ret; |
| 320 | |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 321 | if ((status & bp_mask) != 0) { |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 322 | msg_cerr("Block protection could not be disabled!\n"); |
Yuji Sasaki | 4af3609 | 2019-03-22 10:59:50 -0700 | [diff] [blame] | 323 | if (flash->chip->printlock) |
| 324 | flash->chip->printlock(flash); |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 325 | return 1; |
| 326 | } |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 327 | msg_cdbg("disabled.\n"); |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 331 | /* A common block protection disable that tries to unset the status register bits masked by 0x3C. */ |
| 332 | int spi_disable_blockprotect(struct flashctx *flash) |
| 333 | { |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 334 | return spi_disable_blockprotect_generic(flash, 0x3C, 0, 0, 0xFF); |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Wei Hu | 25584de | 2018-04-30 14:02:08 -0700 | [diff] [blame] | 337 | int spi_disable_blockprotect_sst26_global_unprotect(struct flashctx *flash) |
| 338 | { |
| 339 | int result = spi_write_enable(flash); |
| 340 | if (result) |
| 341 | return result; |
| 342 | |
| 343 | static const unsigned char cmd[] = { 0x98 }; /* ULBPR */ |
| 344 | result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
| 345 | if (result) |
| 346 | msg_cerr("ULBPR failed\n"); |
| 347 | return result; |
| 348 | } |
| 349 | |
Stefan Tauner | a60d408 | 2014-06-04 16:17:03 +0000 | [diff] [blame] | 350 | /* A common block protection disable that tries to unset the status register bits masked by 0x0C (BP0-1) and |
| 351 | * protected/locked by bit #7. Useful when bits 4-5 may be non-0). */ |
| 352 | int spi_disable_blockprotect_bp1_srwd(struct flashctx *flash) |
| 353 | { |
| 354 | return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF); |
| 355 | } |
| 356 | |
Stefan Tauner | 278ba6e | 2013-06-28 21:28:27 +0000 | [diff] [blame] | 357 | /* A common block protection disable that tries to unset the status register bits masked by 0x1C (BP0-2) and |
| 358 | * protected/locked by bit #7. Useful when bit #5 is neither a protection bit nor reserved (and hence possibly |
| 359 | * non-0). */ |
| 360 | int spi_disable_blockprotect_bp2_srwd(struct flashctx *flash) |
| 361 | { |
| 362 | return spi_disable_blockprotect_generic(flash, 0x1C, 1 << 7, 0, 0xFF); |
| 363 | } |
| 364 | |
| 365 | /* A common block protection disable that tries to unset the status register bits masked by 0x3C (BP0-3) and |
| 366 | * protected/locked by bit #7. */ |
| 367 | int spi_disable_blockprotect_bp3_srwd(struct flashctx *flash) |
| 368 | { |
| 369 | return spi_disable_blockprotect_generic(flash, 0x3C, 1 << 7, 0, 0xFF); |
| 370 | } |
| 371 | |
| 372 | /* A common block protection disable that tries to unset the status register bits masked by 0x7C (BP0-4) and |
| 373 | * protected/locked by bit #7. */ |
| 374 | int spi_disable_blockprotect_bp4_srwd(struct flashctx *flash) |
| 375 | { |
| 376 | return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF); |
| 377 | } |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 378 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 379 | static void spi_prettyprint_status_register_hex(uint8_t status) |
| 380 | { |
| 381 | msg_cdbg("Chip status register is 0x%02x.\n", status); |
| 382 | } |
| 383 | |
Stefan Tauner | b6b00e9 | 2013-06-28 21:28:43 +0000 | [diff] [blame] | 384 | /* Common highest bit: Status Register Write Disable (SRWD) or Status Register Protect (SRP). */ |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 385 | static void spi_prettyprint_status_register_srwd(uint8_t status) |
| 386 | { |
Stefan Tauner | b6b00e9 | 2013-06-28 21:28:43 +0000 | [diff] [blame] | 387 | msg_cdbg("Chip status register: Status Register Write Disable (SRWD, SRP, ...) is %sset\n", |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 388 | (status & (1 << 7)) ? "" : "not "); |
| 389 | } |
| 390 | |
| 391 | /* Common highest bit: Block Protect Write Disable (BPL). */ |
| 392 | static void spi_prettyprint_status_register_bpl(uint8_t status) |
| 393 | { |
| 394 | msg_cdbg("Chip status register: Block Protect Write Disable (BPL) is %sset\n", |
| 395 | (status & (1 << 7)) ? "" : "not "); |
| 396 | } |
| 397 | |
| 398 | /* Common lowest 2 bits: WEL and WIP. */ |
| 399 | static void spi_prettyprint_status_register_welwip(uint8_t status) |
| 400 | { |
| 401 | msg_cdbg("Chip status register: Write Enable Latch (WEL) is %sset\n", |
| 402 | (status & (1 << 1)) ? "" : "not "); |
| 403 | msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is %sset\n", |
| 404 | (status & (1 << 0)) ? "" : "not "); |
| 405 | } |
| 406 | |
| 407 | /* Common block protection (BP) bits. */ |
| 408 | static void spi_prettyprint_status_register_bp(uint8_t status, int bp) |
| 409 | { |
| 410 | switch (bp) { |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 411 | case 4: |
| 412 | msg_cdbg("Chip status register: Block Protect 4 (BP4) is %sset\n", |
Stefan Tauner | 5c316f9 | 2015-02-08 21:57:52 +0000 | [diff] [blame] | 413 | (status & (1 << 6)) ? "" : "not "); |
Richard Hughes | db7482b | 2018-12-19 12:04:30 +0000 | [diff] [blame] | 414 | /* Fall through. */ |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 415 | case 3: |
| 416 | msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n", |
| 417 | (status & (1 << 5)) ? "" : "not "); |
Richard Hughes | db7482b | 2018-12-19 12:04:30 +0000 | [diff] [blame] | 418 | /* Fall through. */ |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 419 | case 2: |
| 420 | msg_cdbg("Chip status register: Block Protect 2 (BP2) is %sset\n", |
| 421 | (status & (1 << 4)) ? "" : "not "); |
Richard Hughes | db7482b | 2018-12-19 12:04:30 +0000 | [diff] [blame] | 422 | /* Fall through. */ |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 423 | case 1: |
| 424 | msg_cdbg("Chip status register: Block Protect 1 (BP1) is %sset\n", |
| 425 | (status & (1 << 3)) ? "" : "not "); |
Richard Hughes | db7482b | 2018-12-19 12:04:30 +0000 | [diff] [blame] | 426 | /* Fall through. */ |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 427 | case 0: |
| 428 | msg_cdbg("Chip status register: Block Protect 0 (BP0) is %sset\n", |
| 429 | (status & (1 << 2)) ? "" : "not "); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* Unnamed bits. */ |
Aidan Thornton | db4e87d | 2013-08-27 18:01:53 +0000 | [diff] [blame] | 434 | void spi_prettyprint_status_register_bit(uint8_t status, int bit) |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 435 | { |
| 436 | msg_cdbg("Chip status register: Bit %i is %sset\n", bit, (status & (1 << bit)) ? "" : "not "); |
| 437 | } |
| 438 | |
| 439 | int spi_prettyprint_status_register_plain(struct flashctx *flash) |
| 440 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 441 | uint8_t status; |
| 442 | int ret = spi_read_register(flash, STATUS1, &status); |
| 443 | if (ret) |
| 444 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 445 | spi_prettyprint_status_register_hex(status); |
| 446 | return 0; |
| 447 | } |
| 448 | |
Stefan Tauner | 278ba6e | 2013-06-28 21:28:27 +0000 | [diff] [blame] | 449 | /* Print the plain hex value and the welwip bits only. */ |
| 450 | int spi_prettyprint_status_register_default_welwip(struct flashctx *flash) |
| 451 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 452 | uint8_t status; |
| 453 | int ret = spi_read_register(flash, STATUS1, &status); |
| 454 | if (ret) |
| 455 | return ret; |
Stefan Tauner | 278ba6e | 2013-06-28 21:28:27 +0000 | [diff] [blame] | 456 | spi_prettyprint_status_register_hex(status); |
| 457 | |
| 458 | spi_prettyprint_status_register_welwip(status); |
| 459 | return 0; |
| 460 | } |
| 461 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 462 | /* Works for many chips of the |
| 463 | * AMIC A25L series |
| 464 | * and MX MX25L512 |
| 465 | */ |
Stefan Tauner | 12f3d51 | 2014-05-27 21:27:27 +0000 | [diff] [blame] | 466 | int spi_prettyprint_status_register_bp1_srwd(struct flashctx *flash) |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 467 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 468 | uint8_t status; |
| 469 | int ret = spi_read_register(flash, STATUS1, &status); |
| 470 | if (ret) |
| 471 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 472 | spi_prettyprint_status_register_hex(status); |
| 473 | |
| 474 | spi_prettyprint_status_register_srwd(status); |
| 475 | spi_prettyprint_status_register_bit(status, 6); |
| 476 | spi_prettyprint_status_register_bit(status, 5); |
| 477 | spi_prettyprint_status_register_bit(status, 4); |
| 478 | spi_prettyprint_status_register_bp(status, 1); |
| 479 | spi_prettyprint_status_register_welwip(status); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* Works for many chips of the |
| 484 | * AMIC A25L series |
Stefan Tauner | f445161 | 2013-04-19 01:59:15 +0000 | [diff] [blame] | 485 | * PMC Pm25LD series |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 486 | */ |
Stefan Tauner | 12f3d51 | 2014-05-27 21:27:27 +0000 | [diff] [blame] | 487 | int spi_prettyprint_status_register_bp2_srwd(struct flashctx *flash) |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 488 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 489 | uint8_t status; |
| 490 | int ret = spi_read_register(flash, STATUS1, &status); |
| 491 | if (ret) |
| 492 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 493 | spi_prettyprint_status_register_hex(status); |
| 494 | |
| 495 | spi_prettyprint_status_register_srwd(status); |
| 496 | spi_prettyprint_status_register_bit(status, 6); |
| 497 | spi_prettyprint_status_register_bit(status, 5); |
| 498 | spi_prettyprint_status_register_bp(status, 2); |
| 499 | spi_prettyprint_status_register_welwip(status); |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | /* Works for many chips of the |
| 504 | * ST M25P series |
| 505 | * MX MX25L series |
| 506 | */ |
Stefan Tauner | 12f3d51 | 2014-05-27 21:27:27 +0000 | [diff] [blame] | 507 | int spi_prettyprint_status_register_bp3_srwd(struct flashctx *flash) |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 508 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 509 | uint8_t status; |
| 510 | int ret = spi_read_register(flash, STATUS1, &status); |
| 511 | if (ret) |
| 512 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 513 | spi_prettyprint_status_register_hex(status); |
| 514 | |
| 515 | spi_prettyprint_status_register_srwd(status); |
| 516 | spi_prettyprint_status_register_bit(status, 6); |
| 517 | spi_prettyprint_status_register_bp(status, 3); |
| 518 | spi_prettyprint_status_register_welwip(status); |
| 519 | return 0; |
| 520 | } |
| 521 | |
Stefan Tauner | 12f3d51 | 2014-05-27 21:27:27 +0000 | [diff] [blame] | 522 | int spi_prettyprint_status_register_bp4_srwd(struct flashctx *flash) |
Stefan Tauner | 278ba6e | 2013-06-28 21:28:27 +0000 | [diff] [blame] | 523 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 524 | uint8_t status; |
| 525 | int ret = spi_read_register(flash, STATUS1, &status); |
| 526 | if (ret) |
| 527 | return ret; |
Stefan Tauner | 278ba6e | 2013-06-28 21:28:27 +0000 | [diff] [blame] | 528 | spi_prettyprint_status_register_hex(status); |
| 529 | |
| 530 | spi_prettyprint_status_register_srwd(status); |
| 531 | spi_prettyprint_status_register_bp(status, 4); |
| 532 | spi_prettyprint_status_register_welwip(status); |
| 533 | return 0; |
| 534 | } |
| 535 | |
Stefan Tauner | 85f09f7 | 2014-05-27 21:27:14 +0000 | [diff] [blame] | 536 | int spi_prettyprint_status_register_bp2_bpl(struct flashctx *flash) |
| 537 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 538 | uint8_t status; |
| 539 | int ret = spi_read_register(flash, STATUS1, &status); |
| 540 | if (ret) |
| 541 | return ret; |
Stefan Tauner | 85f09f7 | 2014-05-27 21:27:14 +0000 | [diff] [blame] | 542 | spi_prettyprint_status_register_hex(status); |
| 543 | |
| 544 | spi_prettyprint_status_register_bpl(status); |
| 545 | spi_prettyprint_status_register_bit(status, 6); |
| 546 | spi_prettyprint_status_register_bit(status, 5); |
| 547 | spi_prettyprint_status_register_bp(status, 2); |
| 548 | spi_prettyprint_status_register_welwip(status); |
| 549 | return 0; |
| 550 | } |
| 551 | |
Ben Gardner | bcf6109 | 2015-11-22 02:23:31 +0000 | [diff] [blame] | 552 | int spi_prettyprint_status_register_bp2_tb_bpl(struct flashctx *flash) |
| 553 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 554 | uint8_t status; |
| 555 | int ret = spi_read_register(flash, STATUS1, &status); |
| 556 | if (ret) |
| 557 | return ret; |
Ben Gardner | bcf6109 | 2015-11-22 02:23:31 +0000 | [diff] [blame] | 558 | spi_prettyprint_status_register_hex(status); |
| 559 | |
| 560 | spi_prettyprint_status_register_bpl(status); |
| 561 | spi_prettyprint_status_register_bit(status, 6); |
| 562 | msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top"); |
| 563 | spi_prettyprint_status_register_bp(status, 2); |
| 564 | spi_prettyprint_status_register_welwip(status); |
| 565 | return 0; |
| 566 | } |
| 567 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 568 | /* === Amic === |
| 569 | * FIXME: spi_disable_blockprotect is incorrect but works fine for chips using |
Stefan Tauner | 12f3d51 | 2014-05-27 21:27:27 +0000 | [diff] [blame] | 570 | * spi_prettyprint_status_register_bp1_srwd or |
| 571 | * spi_prettyprint_status_register_bp2_srwd. |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 572 | * FIXME: spi_disable_blockprotect is incorrect and will fail for chips using |
| 573 | * spi_prettyprint_status_register_amic_a25l032 if those have locks controlled |
| 574 | * by the second status register. |
| 575 | */ |
| 576 | |
| 577 | int spi_prettyprint_status_register_amic_a25l032(struct flashctx *flash) |
| 578 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 579 | uint8_t status; |
| 580 | int ret = spi_read_register(flash, STATUS1, &status); |
| 581 | if (ret) |
| 582 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 583 | spi_prettyprint_status_register_hex(status); |
| 584 | |
| 585 | spi_prettyprint_status_register_srwd(status); |
| 586 | msg_cdbg("Chip status register: Sector Protect Size (SEC) is %i KB\n", (status & (1 << 6)) ? 4 : 64); |
| 587 | msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top"); |
| 588 | spi_prettyprint_status_register_bp(status, 2); |
| 589 | spi_prettyprint_status_register_welwip(status); |
| 590 | msg_cdbg("Chip status register 2 is NOT decoded!\n"); |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | /* === Atmel === */ |
| 595 | |
| 596 | static void spi_prettyprint_status_register_atmel_at25_wpen(uint8_t status) |
| 597 | { |
| 598 | msg_cdbg("Chip status register: Write Protect Enable (WPEN) is %sset\n", |
| 599 | (status & (1 << 7)) ? "" : "not "); |
| 600 | } |
| 601 | |
| 602 | static void spi_prettyprint_status_register_atmel_at25_srpl(uint8_t status) |
| 603 | { |
| 604 | msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) is %sset\n", |
| 605 | (status & (1 << 7)) ? "" : "not "); |
| 606 | } |
| 607 | |
| 608 | static void spi_prettyprint_status_register_atmel_at25_epewpp(uint8_t status) |
| 609 | { |
| 610 | msg_cdbg("Chip status register: Erase/Program Error (EPE) is %sset\n", |
| 611 | (status & (1 << 5)) ? "" : "not "); |
| 612 | msg_cdbg("Chip status register: WP# pin (WPP) is %sasserted\n", |
| 613 | (status & (1 << 4)) ? "not " : ""); |
| 614 | } |
| 615 | |
| 616 | static void spi_prettyprint_status_register_atmel_at25_swp(uint8_t status) |
| 617 | { |
| 618 | msg_cdbg("Chip status register: Software Protection Status (SWP): "); |
| 619 | switch (status & (3 << 2)) { |
| 620 | case 0x0 << 2: |
| 621 | msg_cdbg("no sectors are protected\n"); |
| 622 | break; |
| 623 | case 0x1 << 2: |
| 624 | msg_cdbg("some sectors are protected\n"); |
| 625 | /* FIXME: Read individual Sector Protection Registers. */ |
| 626 | break; |
| 627 | case 0x3 << 2: |
| 628 | msg_cdbg("all sectors are protected\n"); |
| 629 | break; |
| 630 | default: |
| 631 | msg_cdbg("reserved for future use\n"); |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | int spi_prettyprint_status_register_at25df(struct flashctx *flash) |
| 637 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 638 | uint8_t status; |
| 639 | int ret = spi_read_register(flash, STATUS1, &status); |
| 640 | if (ret) |
| 641 | return ret; |
| 642 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 643 | spi_prettyprint_status_register_hex(status); |
| 644 | |
| 645 | spi_prettyprint_status_register_atmel_at25_srpl(status); |
| 646 | spi_prettyprint_status_register_bit(status, 6); |
| 647 | spi_prettyprint_status_register_atmel_at25_epewpp(status); |
| 648 | spi_prettyprint_status_register_atmel_at25_swp(status); |
| 649 | spi_prettyprint_status_register_welwip(status); |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | int spi_prettyprint_status_register_at25df_sec(struct flashctx *flash) |
| 654 | { |
| 655 | /* FIXME: We should check the security lockdown. */ |
| 656 | msg_cdbg("Ignoring security lockdown (if present)\n"); |
| 657 | msg_cdbg("Ignoring status register byte 2\n"); |
| 658 | return spi_prettyprint_status_register_at25df(flash); |
| 659 | } |
| 660 | |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 661 | /* used for AT25F512, AT25F1024(A), AT25F2048 */ |
| 662 | int spi_prettyprint_status_register_at25f(struct flashctx *flash) |
| 663 | { |
| 664 | uint8_t status; |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 665 | int ret = spi_read_register(flash, STATUS1, &status); |
| 666 | if (ret) |
| 667 | return ret; |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 668 | |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 669 | spi_prettyprint_status_register_hex(status); |
| 670 | |
| 671 | spi_prettyprint_status_register_atmel_at25_wpen(status); |
| 672 | spi_prettyprint_status_register_bit(status, 6); |
| 673 | spi_prettyprint_status_register_bit(status, 5); |
| 674 | spi_prettyprint_status_register_bit(status, 4); |
| 675 | spi_prettyprint_status_register_bp(status, 1); |
| 676 | spi_prettyprint_status_register_welwip(status); |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | int spi_prettyprint_status_register_at25f512a(struct flashctx *flash) |
| 681 | { |
| 682 | uint8_t status; |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 683 | int ret = spi_read_register(flash, STATUS1, &status); |
| 684 | if (ret) |
| 685 | return ret; |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 686 | |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 687 | spi_prettyprint_status_register_hex(status); |
| 688 | |
| 689 | spi_prettyprint_status_register_atmel_at25_wpen(status); |
| 690 | spi_prettyprint_status_register_bit(status, 6); |
| 691 | spi_prettyprint_status_register_bit(status, 5); |
| 692 | spi_prettyprint_status_register_bit(status, 4); |
| 693 | spi_prettyprint_status_register_bit(status, 3); |
| 694 | spi_prettyprint_status_register_bp(status, 0); |
| 695 | spi_prettyprint_status_register_welwip(status); |
| 696 | return 0; |
| 697 | } |
| 698 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 699 | int spi_prettyprint_status_register_at25f512b(struct flashctx *flash) |
| 700 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 701 | uint8_t status; |
| 702 | int ret = spi_read_register(flash, STATUS1, &status); |
| 703 | if (ret) |
| 704 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 705 | spi_prettyprint_status_register_hex(status); |
| 706 | |
| 707 | spi_prettyprint_status_register_atmel_at25_srpl(status); |
| 708 | spi_prettyprint_status_register_bit(status, 6); |
| 709 | spi_prettyprint_status_register_atmel_at25_epewpp(status); |
| 710 | spi_prettyprint_status_register_bit(status, 3); |
| 711 | spi_prettyprint_status_register_bp(status, 0); |
| 712 | spi_prettyprint_status_register_welwip(status); |
| 713 | return 0; |
| 714 | } |
| 715 | |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 716 | int spi_prettyprint_status_register_at25f4096(struct flashctx *flash) |
| 717 | { |
| 718 | uint8_t status; |
| 719 | |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 720 | int ret = spi_read_register(flash, STATUS1, &status); |
| 721 | if (ret) |
| 722 | return ret; |
| 723 | |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 724 | spi_prettyprint_status_register_hex(status); |
| 725 | |
| 726 | spi_prettyprint_status_register_atmel_at25_wpen(status); |
| 727 | spi_prettyprint_status_register_bit(status, 6); |
| 728 | spi_prettyprint_status_register_bit(status, 5); |
| 729 | spi_prettyprint_status_register_bp(status, 2); |
| 730 | spi_prettyprint_status_register_welwip(status); |
| 731 | return 0; |
| 732 | } |
| 733 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 734 | int spi_prettyprint_status_register_at25fs010(struct flashctx *flash) |
| 735 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 736 | uint8_t status; |
| 737 | int ret = spi_read_register(flash, STATUS1, &status); |
| 738 | if (ret) |
| 739 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 740 | spi_prettyprint_status_register_hex(status); |
| 741 | |
| 742 | spi_prettyprint_status_register_atmel_at25_wpen(status); |
| 743 | msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is " |
| 744 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
| 745 | msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
| 746 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
| 747 | spi_prettyprint_status_register_bit(status, 4); |
| 748 | msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
| 749 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
| 750 | msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
| 751 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
| 752 | /* FIXME: Pretty-print detailed sector protection status. */ |
| 753 | spi_prettyprint_status_register_welwip(status); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | int spi_prettyprint_status_register_at25fs040(struct flashctx *flash) |
| 758 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 759 | uint8_t status; |
| 760 | int ret = spi_read_register(flash, STATUS1, &status); |
| 761 | if (ret) |
| 762 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 763 | spi_prettyprint_status_register_hex(status); |
| 764 | |
| 765 | spi_prettyprint_status_register_atmel_at25_wpen(status); |
| 766 | spi_prettyprint_status_register_bp(status, 4); |
| 767 | /* FIXME: Pretty-print detailed sector protection status. */ |
| 768 | spi_prettyprint_status_register_welwip(status); |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | int spi_prettyprint_status_register_at26df081a(struct flashctx *flash) |
| 773 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 774 | uint8_t status; |
| 775 | int ret = spi_read_register(flash, STATUS1, &status); |
| 776 | if (ret) |
| 777 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 778 | spi_prettyprint_status_register_hex(status); |
| 779 | |
| 780 | spi_prettyprint_status_register_atmel_at25_srpl(status); |
| 781 | msg_cdbg("Chip status register: Sequential Program Mode Status (SPM) is %sset\n", |
| 782 | (status & (1 << 6)) ? "" : "not "); |
| 783 | spi_prettyprint_status_register_atmel_at25_epewpp(status); |
| 784 | spi_prettyprint_status_register_atmel_at25_swp(status); |
| 785 | spi_prettyprint_status_register_welwip(status); |
| 786 | return 0; |
| 787 | } |
| 788 | |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 789 | /* Some Atmel DataFlash chips support per sector protection bits and the write protection bits in the status |
| 790 | * register do indicate if none, some or all sectors are protected. It is possible to globally (un)lock all |
| 791 | * sectors at once by writing 0 not only the protection bits (2 and 3) but also completely unrelated bits (4 and |
| 792 | * 5) which normally are not touched. |
| 793 | * Affected are all known Atmel chips matched by AT2[56]D[FLQ]..1A? but the AT26DF041. */ |
| 794 | int spi_disable_blockprotect_at2x_global_unprotect(struct flashctx *flash) |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 795 | { |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 796 | return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 1 << 4, 0x00); |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 799 | int spi_disable_blockprotect_at2x_global_unprotect_sec(struct flashctx *flash) |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 800 | { |
| 801 | /* FIXME: We should check the security lockdown. */ |
| 802 | msg_cinfo("Ignoring security lockdown (if present)\n"); |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 803 | return spi_disable_blockprotect_at2x_global_unprotect(flash); |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 806 | int spi_disable_blockprotect_at25f(struct flashctx *flash) |
| 807 | { |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 808 | return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF); |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | int spi_disable_blockprotect_at25f512a(struct flashctx *flash) |
| 812 | { |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 813 | return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 0, 0xFF); |
Stefan Tauner | 57794ac | 2012-12-29 15:04:20 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 816 | int spi_disable_blockprotect_at25f512b(struct flashctx *flash) |
| 817 | { |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 818 | return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 1 << 4, 0xFF); |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | int spi_disable_blockprotect_at25fs010(struct flashctx *flash) |
| 822 | { |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 823 | return spi_disable_blockprotect_generic(flash, 0x6C, 1 << 7, 0, 0xFF); |
Stefan Tauner | 9530a02 | 2012-12-29 15:04:05 +0000 | [diff] [blame] | 824 | } |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 825 | |
| 826 | int spi_disable_blockprotect_at25fs040(struct flashctx *flash) |
| 827 | { |
Stefan Tauner | cecb2c5 | 2013-06-20 22:55:41 +0000 | [diff] [blame] | 828 | return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF); |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Nikolay Nikolaev | d0e3ea1 | 2013-06-28 21:29:08 +0000 | [diff] [blame] | 831 | /* === Eon === */ |
| 832 | |
| 833 | int spi_prettyprint_status_register_en25s_wp(struct flashctx *flash) |
| 834 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 835 | uint8_t status; |
| 836 | int ret = spi_read_register(flash, STATUS1, &status); |
| 837 | if (ret) |
| 838 | return ret; |
Nikolay Nikolaev | d0e3ea1 | 2013-06-28 21:29:08 +0000 | [diff] [blame] | 839 | spi_prettyprint_status_register_hex(status); |
| 840 | |
| 841 | spi_prettyprint_status_register_srwd(status); |
| 842 | msg_cdbg("Chip status register: WP# disable (WPDIS) is %sabled\n", (status & (1 << 6)) ? "en " : "dis"); |
| 843 | spi_prettyprint_status_register_bp(status, 3); |
| 844 | spi_prettyprint_status_register_welwip(status); |
| 845 | return 0; |
| 846 | } |
| 847 | |
Nikolay Nikolaev | c80c4a3 | 2013-06-28 21:29:44 +0000 | [diff] [blame] | 848 | /* === Intel/Numonyx/Micron - Spansion === */ |
Stefan Tauner | 54aaa4a | 2012-12-29 15:04:12 +0000 | [diff] [blame] | 849 | |
Nikolay Nikolaev | 6f59b0b | 2013-06-28 21:29:51 +0000 | [diff] [blame] | 850 | int spi_disable_blockprotect_n25q(struct flashctx *flash) |
| 851 | { |
| 852 | return spi_disable_blockprotect_generic(flash, 0x5C, 1 << 7, 0, 0xFF); |
| 853 | } |
| 854 | |
| 855 | int spi_prettyprint_status_register_n25q(struct flashctx *flash) |
| 856 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 857 | uint8_t status; |
| 858 | int ret = spi_read_register(flash, STATUS1, &status); |
| 859 | if (ret) |
| 860 | return ret; |
Nikolay Nikolaev | 6f59b0b | 2013-06-28 21:29:51 +0000 | [diff] [blame] | 861 | spi_prettyprint_status_register_hex(status); |
| 862 | |
| 863 | spi_prettyprint_status_register_srwd(status); |
| 864 | if (flash->chip->total_size <= 32 / 8 * 1024) /* N25Q16 and N25Q32: reserved */ |
| 865 | spi_prettyprint_status_register_bit(status, 6); |
| 866 | else |
| 867 | msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n", |
| 868 | (status & (1 << 6)) ? "" : "not "); |
| 869 | msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top"); |
| 870 | spi_prettyprint_status_register_bp(status, 2); |
| 871 | spi_prettyprint_status_register_welwip(status); |
| 872 | return 0; |
| 873 | } |
| 874 | |
Nikolay Nikolaev | c80c4a3 | 2013-06-28 21:29:44 +0000 | [diff] [blame] | 875 | /* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */ |
Stefan Tauner | 54aaa4a | 2012-12-29 15:04:12 +0000 | [diff] [blame] | 876 | /* TODO: Clear P_FAIL and E_FAIL with Clear SR Fail Flags Command (30h) here? */ |
Nikolay Nikolaev | c80c4a3 | 2013-06-28 21:29:44 +0000 | [diff] [blame] | 877 | int spi_disable_blockprotect_bp2_ep_srwd(struct flashctx *flash) |
Stefan Tauner | 54aaa4a | 2012-12-29 15:04:12 +0000 | [diff] [blame] | 878 | { |
Stefan Tauner | 278ba6e | 2013-06-28 21:28:27 +0000 | [diff] [blame] | 879 | return spi_disable_blockprotect_bp2_srwd(flash); |
Stefan Tauner | 54aaa4a | 2012-12-29 15:04:12 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Nikolay Nikolaev | c80c4a3 | 2013-06-28 21:29:44 +0000 | [diff] [blame] | 882 | /* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */ |
| 883 | int spi_prettyprint_status_register_bp2_ep_srwd(struct flashctx *flash) |
Stefan Tauner | 54aaa4a | 2012-12-29 15:04:12 +0000 | [diff] [blame] | 884 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 885 | uint8_t status; |
| 886 | int ret = spi_read_register(flash, STATUS1, &status); |
| 887 | if (ret) |
| 888 | return ret; |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 889 | spi_prettyprint_status_register_hex(status); |
Stefan Tauner | 54aaa4a | 2012-12-29 15:04:12 +0000 | [diff] [blame] | 890 | |
| 891 | spi_prettyprint_status_register_srwd(status); |
| 892 | msg_cdbg("Chip status register: Program Fail Flag (P_FAIL) is %sset\n", |
| 893 | (status & (1 << 6)) ? "" : "not "); |
| 894 | msg_cdbg("Chip status register: Erase Fail Flag (E_FAIL) is %sset\n", |
| 895 | (status & (1 << 5)) ? "" : "not "); |
| 896 | spi_prettyprint_status_register_bp(status, 2); |
| 897 | spi_prettyprint_status_register_welwip(status); |
| 898 | return 0; |
| 899 | } |
| 900 | |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 901 | /* === SST === */ |
| 902 | |
| 903 | static void spi_prettyprint_status_register_sst25_common(uint8_t status) |
| 904 | { |
| 905 | spi_prettyprint_status_register_hex(status); |
| 906 | |
| 907 | spi_prettyprint_status_register_bpl(status); |
| 908 | msg_cdbg("Chip status register: Auto Address Increment Programming (AAI) is %sset\n", |
| 909 | (status & (1 << 6)) ? "" : "not "); |
| 910 | spi_prettyprint_status_register_bp(status, 3); |
| 911 | spi_prettyprint_status_register_welwip(status); |
| 912 | } |
| 913 | |
| 914 | int spi_prettyprint_status_register_sst25(struct flashctx *flash) |
| 915 | { |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 916 | uint8_t status; |
| 917 | int ret = spi_read_register(flash, STATUS1, &status); |
| 918 | if (ret) |
| 919 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 920 | spi_prettyprint_status_register_sst25_common(status); |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | int spi_prettyprint_status_register_sst25vf016(struct flashctx *flash) |
| 925 | { |
| 926 | static const char *const bpt[] = { |
| 927 | "none", |
| 928 | "1F0000H-1FFFFFH", |
| 929 | "1E0000H-1FFFFFH", |
| 930 | "1C0000H-1FFFFFH", |
| 931 | "180000H-1FFFFFH", |
| 932 | "100000H-1FFFFFH", |
| 933 | "all", "all" |
| 934 | }; |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 935 | uint8_t status; |
| 936 | int ret = spi_read_register(flash, STATUS1, &status); |
| 937 | if (ret) |
| 938 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 939 | spi_prettyprint_status_register_sst25_common(status); |
| 940 | msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]); |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | int spi_prettyprint_status_register_sst25vf040b(struct flashctx *flash) |
| 945 | { |
| 946 | static const char *const bpt[] = { |
| 947 | "none", |
| 948 | "0x70000-0x7ffff", |
| 949 | "0x60000-0x7ffff", |
| 950 | "0x40000-0x7ffff", |
| 951 | "all blocks", "all blocks", "all blocks", "all blocks" |
| 952 | }; |
Nikolai Artemiev | b8a90d0 | 2021-10-28 16:18:28 +1100 | [diff] [blame] | 953 | uint8_t status; |
| 954 | int ret = spi_read_register(flash, STATUS1, &status); |
| 955 | if (ret) |
| 956 | return ret; |
Stefan Tauner | 6ee37e2 | 2012-12-29 15:03:51 +0000 | [diff] [blame] | 957 | spi_prettyprint_status_register_sst25_common(status); |
| 958 | msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]); |
| 959 | return 0; |
| 960 | } |