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