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