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