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