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