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