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