Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [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. |
| 18 | */ |
| 19 | |
| 20 | #include "flash.h" |
| 21 | #include "chipdrivers.h" |
| 22 | #include "spi.h" |
| 23 | |
| 24 | /* === Generic functions === */ |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 25 | static int spi_write_status_register_flag(const struct flashctx *flash, int status, const unsigned char enable_opcode) |
| 26 | { |
| 27 | int result; |
| 28 | int i = 0; |
| 29 | /* |
| 30 | * WRSR requires either EWSR or WREN depending on chip type. |
| 31 | * The code below relies on the fact hat EWSR and WREN have the same |
| 32 | * INSIZE and OUTSIZE. |
| 33 | */ |
| 34 | struct spi_command cmds[] = { |
| 35 | { |
| 36 | .writecnt = JEDEC_WREN_OUTSIZE, |
| 37 | .writearr = (const unsigned char[]){ enable_opcode }, |
| 38 | .readcnt = 0, |
| 39 | .readarr = NULL, |
| 40 | }, { |
| 41 | .writecnt = JEDEC_WRSR_OUTSIZE, |
| 42 | .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status }, |
| 43 | .readcnt = 0, |
| 44 | .readarr = NULL, |
| 45 | }, { |
| 46 | .writecnt = 0, |
| 47 | .writearr = NULL, |
| 48 | .readcnt = 0, |
| 49 | .readarr = NULL, |
| 50 | }}; |
| 51 | |
| 52 | result = spi_send_multicommand(flash, cmds); |
| 53 | if (result) { |
| 54 | msg_cerr("%s failed during command execution\n", __func__); |
| 55 | /* No point in waiting for the command to complete if execution |
| 56 | * failed. |
| 57 | */ |
| 58 | return result; |
| 59 | } |
| 60 | /* WRSR performs a self-timed erase before the changes take effect. |
| 61 | * This may take 50-85 ms in most cases, and some chips apparently |
| 62 | * allow running RDSR only once. Therefore pick an initial delay of |
| 63 | * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed. |
| 64 | */ |
| 65 | programmer_delay(100 * 1000); |
| 66 | while (spi_read_status_register(flash) & SPI_SR_WIP) { |
| 67 | if (++i > 490) { |
| 68 | msg_cerr("Error: WIP bit after WRSR never cleared\n"); |
| 69 | return TIMEOUT_ERROR; |
| 70 | } |
| 71 | programmer_delay(10 * 1000); |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int spi_write_status_register(const struct flashctx *flash, int status) |
| 77 | { |
| 78 | int feature_bits = flash->chip->feature_bits; |
| 79 | int ret = 1; |
| 80 | |
| 81 | if (!(feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) { |
| 82 | msg_cdbg("Missing status register write definition, assuming " |
| 83 | "EWSR is needed\n"); |
| 84 | feature_bits |= FEATURE_WRSR_EWSR; |
| 85 | } |
Edward O'Callaghan | 0bdade3 | 2021-01-22 00:33:20 +1100 | [diff] [blame] | 86 | if (flash->chip->write_status) |
| 87 | return flash->chip->write_status(flash, status); |
| 88 | |
| 89 | if (feature_bits & FEATURE_WRSR_WREN) |
| 90 | ret = spi_write_status_register_flag(flash, status, JEDEC_WREN); |
| 91 | if (ret && (feature_bits & FEATURE_WRSR_EWSR)) |
| 92 | ret = spi_write_status_register_flag(flash, status, JEDEC_EWSR); |
| 93 | return ret; |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | uint8_t spi_read_status_register(const struct flashctx *flash) |
| 97 | { |
| 98 | static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR }; |
| 99 | /* FIXME: No workarounds for driver/hardware bugs in generic code. */ |
| 100 | unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */ |
Nico Huber | 4c8a956 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 101 | int ret = 0; |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 102 | |
| 103 | /* Read Status Register */ |
Nico Huber | 4c8a956 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 104 | if (flash->chip->read_status) |
| 105 | readarr[0] = flash->chip->read_status(flash); |
| 106 | else |
| 107 | ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr); |
| 108 | if (ret) |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 109 | msg_cerr("RDSR failed!\n"); |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 110 | |
| 111 | return readarr[0]; |
| 112 | } |
| 113 | |
Chris Zhou | 15a3550 | 2019-05-02 15:55:00 +0800 | [diff] [blame] | 114 | static int spi_restore_status(struct flashctx *flash, uint8_t status) |
| 115 | { |
| 116 | msg_cdbg("restoring chip status (0x%02x)\n", status); |
| 117 | return spi_write_status_register(flash, status); |
| 118 | } |
| 119 | |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 120 | /* A generic block protection disable. |
| 121 | * Tests if a protection is enabled with the block protection mask (bp_mask) and returns success otherwise. |
| 122 | * Tests if the register bits are locked with the lock_mask (lock_mask). |
| 123 | * Tests if a hardware protection is active (i.e. low pin/high bit value) with the write protection mask |
| 124 | * (wp_mask) and bails out in that case. |
| 125 | * If there are register lock bits set we try to disable them by unsetting those bits of the previous register |
| 126 | * contents that are set in the lock_mask. We then check if removing the lock bits has worked and continue as if |
| 127 | * they never had been engaged: |
| 128 | * If the lock bits are out of the way try to disable engaged protections. |
| 129 | * To support uncommon global unprotects (e.g. on most AT2[56]xx1(A)) unprotect_mask can be used to force |
| 130 | * bits to 0 additionally to those set in bp_mask and lock_mask. Only bits set in unprotect_mask are potentially |
| 131 | * preserved when doing the final unprotect. |
| 132 | * |
| 133 | * To sum up: |
| 134 | * bp_mask: set those bits that correspond to the bits in the status register that indicate an active protection |
| 135 | * (which should be unset after this function returns). |
| 136 | * lock_mask: set the bits that correspond to the bits that lock changing the bits above. |
| 137 | * wp_mask: set the bits that correspond to bits indicating non-software revocable protections. |
| 138 | * unprotect_mask: set the bits that should be preserved if possible when unprotecting. |
| 139 | */ |
| 140 | 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) |
| 141 | { |
| 142 | uint8_t status; |
| 143 | int result; |
| 144 | |
| 145 | status = spi_read_status_register(flash); |
| 146 | if ((status & bp_mask) == 0) { |
| 147 | msg_cdbg2("Block protection is disabled.\n"); |
| 148 | return 0; |
| 149 | } |
| 150 | |
Nikolai Artemiev | b15d7cf | 2020-12-14 07:39:02 +1100 | [diff] [blame] | 151 | /* Restore status register content upon exit in finalize_flash_access(). */ |
Chris Zhou | 15a3550 | 2019-05-02 15:55:00 +0800 | [diff] [blame] | 152 | register_chip_restore(spi_restore_status, flash, status); |
| 153 | |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 154 | msg_cdbg("Some block protection in effect, disabling... "); |
| 155 | if ((status & lock_mask) != 0) { |
| 156 | msg_cdbg("\n\tNeed to disable the register lock first... "); |
| 157 | if (wp_mask != 0 && (status & wp_mask) == 0) { |
| 158 | msg_cerr("Hardware protection is active, disabling write protection is impossible.\n"); |
| 159 | return 1; |
| 160 | } |
| 161 | /* All bits except the register lock bit (often called SPRL, SRWD, WPEN) are readonly. */ |
| 162 | result = spi_write_status_register(flash, status & ~lock_mask); |
| 163 | if (result) { |
| 164 | msg_cerr("spi_write_status_register failed.\n"); |
| 165 | return result; |
| 166 | } |
| 167 | status = spi_read_status_register(flash); |
| 168 | if ((status & lock_mask) != 0) { |
| 169 | msg_cerr("Unsetting lock bit(s) failed.\n"); |
| 170 | return 1; |
| 171 | } |
| 172 | msg_cdbg("done.\n"); |
| 173 | } |
| 174 | /* Global unprotect. Make sure to mask the register lock bit as well. */ |
| 175 | result = spi_write_status_register(flash, status & ~(bp_mask | lock_mask) & unprotect_mask); |
| 176 | if (result) { |
| 177 | msg_cerr("spi_write_status_register failed.\n"); |
| 178 | return result; |
| 179 | } |
| 180 | status = spi_read_status_register(flash); |
| 181 | if ((status & bp_mask) != 0) { |
| 182 | msg_cerr("Block protection could not be disabled!\n"); |
Yuji Sasaki | 5855a1e | 2019-03-22 10:59:50 -0700 | [diff] [blame] | 183 | if (flash->chip->printlock) |
| 184 | flash->chip->printlock(flash); |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 185 | return 1; |
| 186 | } |
| 187 | msg_cdbg("disabled.\n"); |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* A common block protection disable that tries to unset the status register bits masked by 0x3C. */ |
| 192 | int spi_disable_blockprotect(struct flashctx *flash) |
| 193 | { |
| 194 | return spi_disable_blockprotect_generic(flash, 0x3C, 0, 0, 0xFF); |
| 195 | } |
| 196 | |
| 197 | int spi_disable_blockprotect_sst26_global_unprotect(struct flashctx *flash) |
| 198 | { |
| 199 | int result = spi_write_enable(flash); |
| 200 | if (result) |
| 201 | return result; |
| 202 | |
| 203 | static const unsigned char cmd[] = { 0x98 }; /* ULBPR */ |
| 204 | result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
| 205 | if (result) |
| 206 | msg_cerr("ULBPR failed\n"); |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | /* A common block protection disable that tries to unset the status register bits masked by 0x0C (BP0-1) and |
| 211 | * protected/locked by bit #7. Useful when bits 4-5 may be non-0). */ |
| 212 | int spi_disable_blockprotect_bp1_srwd(struct flashctx *flash) |
| 213 | { |
| 214 | return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF); |
| 215 | } |
| 216 | |
| 217 | /* A common block protection disable that tries to unset the status register bits masked by 0x1C (BP0-2) and |
| 218 | * protected/locked by bit #7. Useful when bit #5 is neither a protection bit nor reserved (and hence possibly |
| 219 | * non-0). */ |
| 220 | int spi_disable_blockprotect_bp2_srwd(struct flashctx *flash) |
| 221 | { |
| 222 | return spi_disable_blockprotect_generic(flash, 0x1C, 1 << 7, 0, 0xFF); |
| 223 | } |
| 224 | |
| 225 | /* A common block protection disable that tries to unset the status register bits masked by 0x3C (BP0-3) and |
| 226 | * protected/locked by bit #7. */ |
| 227 | int spi_disable_blockprotect_bp3_srwd(struct flashctx *flash) |
| 228 | { |
| 229 | return spi_disable_blockprotect_generic(flash, 0x3C, 1 << 7, 0, 0xFF); |
| 230 | } |
| 231 | |
| 232 | /* A common block protection disable that tries to unset the status register bits masked by 0x7C (BP0-4) and |
| 233 | * protected/locked by bit #7. */ |
| 234 | int spi_disable_blockprotect_bp4_srwd(struct flashctx *flash) |
| 235 | { |
| 236 | return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF); |
| 237 | } |
| 238 | |
| 239 | static void spi_prettyprint_status_register_hex(uint8_t status) |
| 240 | { |
| 241 | msg_cdbg("Chip status register is 0x%02x.\n", status); |
| 242 | } |
| 243 | |
| 244 | /* Common highest bit: Status Register Write Disable (SRWD) or Status Register Protect (SRP). */ |
| 245 | static void spi_prettyprint_status_register_srwd(uint8_t status) |
| 246 | { |
| 247 | msg_cdbg("Chip status register: Status Register Write Disable (SRWD, SRP, ...) is %sset\n", |
| 248 | (status & (1 << 7)) ? "" : "not "); |
| 249 | } |
| 250 | |
| 251 | /* Common highest bit: Block Protect Write Disable (BPL). */ |
| 252 | static void spi_prettyprint_status_register_bpl(uint8_t status) |
| 253 | { |
| 254 | msg_cdbg("Chip status register: Block Protect Write Disable (BPL) is %sset\n", |
| 255 | (status & (1 << 7)) ? "" : "not "); |
| 256 | } |
| 257 | |
| 258 | /* Common lowest 2 bits: WEL and WIP. */ |
| 259 | static void spi_prettyprint_status_register_welwip(uint8_t status) |
| 260 | { |
| 261 | msg_cdbg("Chip status register: Write Enable Latch (WEL) is %sset\n", |
| 262 | (status & (1 << 1)) ? "" : "not "); |
| 263 | msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is %sset\n", |
| 264 | (status & (1 << 0)) ? "" : "not "); |
| 265 | } |
| 266 | |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 267 | /* Common block protection (BP) bits. */ |
| 268 | static void spi_prettyprint_status_register_bp(uint8_t status, int bp) |
| 269 | { |
| 270 | switch (bp) { |
| 271 | case 4: |
| 272 | msg_cdbg("Chip status register: Block Protect 4 (BP4) is %sset\n", |
| 273 | (status & (1 << 6)) ? "" : "not "); |
| 274 | /* Fall through. */ |
| 275 | case 3: |
| 276 | msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n", |
| 277 | (status & (1 << 5)) ? "" : "not "); |
| 278 | /* Fall through. */ |
| 279 | case 2: |
| 280 | msg_cdbg("Chip status register: Block Protect 2 (BP2) is %sset\n", |
| 281 | (status & (1 << 4)) ? "" : "not "); |
| 282 | /* Fall through. */ |
| 283 | case 1: |
| 284 | msg_cdbg("Chip status register: Block Protect 1 (BP1) is %sset\n", |
| 285 | (status & (1 << 3)) ? "" : "not "); |
| 286 | /* Fall through. */ |
| 287 | case 0: |
| 288 | msg_cdbg("Chip status register: Block Protect 0 (BP0) is %sset\n", |
| 289 | (status & (1 << 2)) ? "" : "not "); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /* Unnamed bits. */ |
| 294 | void spi_prettyprint_status_register_bit(uint8_t status, int bit) |
| 295 | { |
| 296 | msg_cdbg("Chip status register: Bit %i is %sset\n", bit, (status & (1 << bit)) ? "" : "not "); |
| 297 | } |
| 298 | |
| 299 | int spi_prettyprint_status_register_plain(struct flashctx *flash) |
| 300 | { |
| 301 | uint8_t status = spi_read_status_register(flash); |
| 302 | spi_prettyprint_status_register_hex(status); |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* Print the plain hex value and the welwip bits only. */ |
| 307 | int spi_prettyprint_status_register_default_welwip(struct flashctx *flash) |
| 308 | { |
| 309 | uint8_t status = spi_read_status_register(flash); |
| 310 | spi_prettyprint_status_register_hex(status); |
| 311 | |
| 312 | spi_prettyprint_status_register_welwip(status); |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /* Works for many chips of the |
| 317 | * AMIC A25L series |
| 318 | * and MX MX25L512 |
| 319 | */ |
| 320 | int spi_prettyprint_status_register_bp1_srwd(struct flashctx *flash) |
| 321 | { |
| 322 | uint8_t status = spi_read_status_register(flash); |
| 323 | spi_prettyprint_status_register_hex(status); |
| 324 | |
| 325 | spi_prettyprint_status_register_srwd(status); |
| 326 | spi_prettyprint_status_register_bit(status, 6); |
| 327 | spi_prettyprint_status_register_bit(status, 5); |
| 328 | spi_prettyprint_status_register_bit(status, 4); |
| 329 | spi_prettyprint_status_register_bp(status, 1); |
| 330 | spi_prettyprint_status_register_welwip(status); |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | /* Works for many chips of the |
| 335 | * AMIC A25L series |
| 336 | * PMC Pm25LD series |
| 337 | */ |
| 338 | int spi_prettyprint_status_register_bp2_srwd(struct flashctx *flash) |
| 339 | { |
| 340 | uint8_t status = spi_read_status_register(flash); |
| 341 | spi_prettyprint_status_register_hex(status); |
| 342 | |
| 343 | spi_prettyprint_status_register_srwd(status); |
| 344 | spi_prettyprint_status_register_bit(status, 6); |
| 345 | spi_prettyprint_status_register_bit(status, 5); |
| 346 | spi_prettyprint_status_register_bp(status, 2); |
| 347 | spi_prettyprint_status_register_welwip(status); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | /* Works for many chips of the |
| 352 | * ST M25P series |
| 353 | * MX MX25L series |
| 354 | */ |
| 355 | int spi_prettyprint_status_register_bp3_srwd(struct flashctx *flash) |
| 356 | { |
| 357 | uint8_t status = spi_read_status_register(flash); |
| 358 | spi_prettyprint_status_register_hex(status); |
| 359 | |
| 360 | spi_prettyprint_status_register_srwd(status); |
| 361 | spi_prettyprint_status_register_bit(status, 6); |
| 362 | spi_prettyprint_status_register_bp(status, 3); |
| 363 | spi_prettyprint_status_register_welwip(status); |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | int spi_prettyprint_status_register_bp4_srwd(struct flashctx *flash) |
| 368 | { |
| 369 | uint8_t status = spi_read_status_register(flash); |
| 370 | spi_prettyprint_status_register_hex(status); |
| 371 | |
| 372 | spi_prettyprint_status_register_srwd(status); |
| 373 | spi_prettyprint_status_register_bp(status, 4); |
| 374 | spi_prettyprint_status_register_welwip(status); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | int spi_prettyprint_status_register_bp2_bpl(struct flashctx *flash) |
| 379 | { |
| 380 | uint8_t status = spi_read_status_register(flash); |
| 381 | spi_prettyprint_status_register_hex(status); |
| 382 | |
| 383 | spi_prettyprint_status_register_bpl(status); |
| 384 | spi_prettyprint_status_register_bit(status, 6); |
| 385 | spi_prettyprint_status_register_bit(status, 5); |
| 386 | spi_prettyprint_status_register_bp(status, 2); |
| 387 | spi_prettyprint_status_register_welwip(status); |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | int spi_prettyprint_status_register_bp2_tb_bpl(struct flashctx *flash) |
| 392 | { |
| 393 | uint8_t status = spi_read_status_register(flash); |
| 394 | spi_prettyprint_status_register_hex(status); |
| 395 | |
| 396 | spi_prettyprint_status_register_bpl(status); |
| 397 | spi_prettyprint_status_register_bit(status, 6); |
| 398 | msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top"); |
| 399 | spi_prettyprint_status_register_bp(status, 2); |
| 400 | spi_prettyprint_status_register_welwip(status); |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | /* === Amic === |
| 405 | * FIXME: spi_disable_blockprotect is incorrect but works fine for chips using |
| 406 | * spi_prettyprint_status_register_bp1_srwd or |
| 407 | * spi_prettyprint_status_register_bp2_srwd. |
| 408 | * FIXME: spi_disable_blockprotect is incorrect and will fail for chips using |
| 409 | * spi_prettyprint_status_register_amic_a25l032 if those have locks controlled |
| 410 | * by the second status register. |
| 411 | */ |
| 412 | |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 413 | int spi_prettyprint_status_register_amic_a25l032(struct flashctx *flash) |
| 414 | { |
| 415 | uint8_t status = spi_read_status_register(flash); |
| 416 | spi_prettyprint_status_register_hex(status); |
| 417 | |
| 418 | spi_prettyprint_status_register_srwd(status); |
| 419 | msg_cdbg("Chip status register: Sector Protect Size (SEC) is %i KB\n", (status & (1 << 6)) ? 4 : 64); |
| 420 | msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top"); |
| 421 | spi_prettyprint_status_register_bp(status, 2); |
| 422 | spi_prettyprint_status_register_welwip(status); |
| 423 | msg_cdbg("Chip status register 2 is NOT decoded!\n"); |
| 424 | return 0; |
| 425 | } |
| 426 | |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 427 | /* === Atmel === */ |
| 428 | |
| 429 | static void spi_prettyprint_status_register_atmel_at25_wpen(uint8_t status) |
| 430 | { |
| 431 | msg_cdbg("Chip status register: Write Protect Enable (WPEN) is %sset\n", |
| 432 | (status & (1 << 7)) ? "" : "not "); |
| 433 | } |
| 434 | |
| 435 | static void spi_prettyprint_status_register_atmel_at25_srpl(uint8_t status) |
| 436 | { |
| 437 | msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) is %sset\n", |
| 438 | (status & (1 << 7)) ? "" : "not "); |
| 439 | } |
| 440 | |
| 441 | static void spi_prettyprint_status_register_atmel_at25_epewpp(uint8_t status) |
| 442 | { |
| 443 | msg_cdbg("Chip status register: Erase/Program Error (EPE) is %sset\n", |
| 444 | (status & (1 << 5)) ? "" : "not "); |
| 445 | msg_cdbg("Chip status register: WP# pin (WPP) is %sasserted\n", |
| 446 | (status & (1 << 4)) ? "not " : ""); |
| 447 | } |
| 448 | |
| 449 | static void spi_prettyprint_status_register_atmel_at25_swp(uint8_t status) |
| 450 | { |
| 451 | msg_cdbg("Chip status register: Software Protection Status (SWP): "); |
| 452 | switch (status & (3 << 2)) { |
| 453 | case 0x0 << 2: |
| 454 | msg_cdbg("no sectors are protected\n"); |
| 455 | break; |
| 456 | case 0x1 << 2: |
| 457 | msg_cdbg("some sectors are protected\n"); |
| 458 | /* FIXME: Read individual Sector Protection Registers. */ |
| 459 | break; |
| 460 | case 0x3 << 2: |
| 461 | msg_cdbg("all sectors are protected\n"); |
| 462 | break; |
| 463 | default: |
| 464 | msg_cdbg("reserved for future use\n"); |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | int spi_prettyprint_status_register_at25df(struct flashctx *flash) |
| 470 | { |
| 471 | uint8_t status = spi_read_status_register(flash); |
| 472 | spi_prettyprint_status_register_hex(status); |
| 473 | |
| 474 | spi_prettyprint_status_register_atmel_at25_srpl(status); |
| 475 | spi_prettyprint_status_register_bit(status, 6); |
| 476 | spi_prettyprint_status_register_atmel_at25_epewpp(status); |
| 477 | spi_prettyprint_status_register_atmel_at25_swp(status); |
| 478 | spi_prettyprint_status_register_welwip(status); |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | int spi_prettyprint_status_register_at25df_sec(struct flashctx *flash) |
| 483 | { |
| 484 | /* FIXME: We should check the security lockdown. */ |
| 485 | msg_cdbg("Ignoring security lockdown (if present)\n"); |
| 486 | msg_cdbg("Ignoring status register byte 2\n"); |
| 487 | return spi_prettyprint_status_register_at25df(flash); |
| 488 | } |
| 489 | |
| 490 | /* used for AT25F512, AT25F1024(A), AT25F2048 */ |
| 491 | int spi_prettyprint_status_register_at25f(struct flashctx *flash) |
| 492 | { |
| 493 | uint8_t status; |
| 494 | |
| 495 | status = spi_read_status_register(flash); |
| 496 | spi_prettyprint_status_register_hex(status); |
| 497 | |
| 498 | spi_prettyprint_status_register_atmel_at25_wpen(status); |
| 499 | spi_prettyprint_status_register_bit(status, 6); |
| 500 | spi_prettyprint_status_register_bit(status, 5); |
| 501 | spi_prettyprint_status_register_bit(status, 4); |
| 502 | spi_prettyprint_status_register_bp(status, 1); |
| 503 | spi_prettyprint_status_register_welwip(status); |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | int spi_prettyprint_status_register_at25f512a(struct flashctx *flash) |
| 508 | { |
| 509 | uint8_t status; |
| 510 | |
| 511 | status = spi_read_status_register(flash); |
| 512 | spi_prettyprint_status_register_hex(status); |
| 513 | |
| 514 | spi_prettyprint_status_register_atmel_at25_wpen(status); |
| 515 | spi_prettyprint_status_register_bit(status, 6); |
| 516 | spi_prettyprint_status_register_bit(status, 5); |
| 517 | spi_prettyprint_status_register_bit(status, 4); |
| 518 | spi_prettyprint_status_register_bit(status, 3); |
| 519 | spi_prettyprint_status_register_bp(status, 0); |
| 520 | spi_prettyprint_status_register_welwip(status); |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | int spi_prettyprint_status_register_at25f512b(struct flashctx *flash) |
| 525 | { |
| 526 | uint8_t status = spi_read_status_register(flash); |
| 527 | spi_prettyprint_status_register_hex(status); |
| 528 | |
| 529 | spi_prettyprint_status_register_atmel_at25_srpl(status); |
| 530 | spi_prettyprint_status_register_bit(status, 6); |
| 531 | spi_prettyprint_status_register_atmel_at25_epewpp(status); |
| 532 | spi_prettyprint_status_register_bit(status, 3); |
| 533 | spi_prettyprint_status_register_bp(status, 0); |
| 534 | spi_prettyprint_status_register_welwip(status); |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | int spi_prettyprint_status_register_at25f4096(struct flashctx *flash) |
| 539 | { |
| 540 | uint8_t status; |
| 541 | |
| 542 | status = spi_read_status_register(flash); |
| 543 | spi_prettyprint_status_register_hex(status); |
| 544 | |
| 545 | spi_prettyprint_status_register_atmel_at25_wpen(status); |
| 546 | spi_prettyprint_status_register_bit(status, 6); |
| 547 | spi_prettyprint_status_register_bit(status, 5); |
| 548 | spi_prettyprint_status_register_bp(status, 2); |
| 549 | spi_prettyprint_status_register_welwip(status); |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | int spi_prettyprint_status_register_at25fs010(struct flashctx *flash) |
| 554 | { |
| 555 | uint8_t status = spi_read_status_register(flash); |
| 556 | spi_prettyprint_status_register_hex(status); |
| 557 | |
| 558 | spi_prettyprint_status_register_atmel_at25_wpen(status); |
| 559 | msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is " |
| 560 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
| 561 | msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
| 562 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
| 563 | spi_prettyprint_status_register_bit(status, 4); |
| 564 | msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
| 565 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
| 566 | msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
| 567 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
| 568 | /* FIXME: Pretty-print detailed sector protection status. */ |
| 569 | spi_prettyprint_status_register_welwip(status); |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | int spi_prettyprint_status_register_at25fs040(struct flashctx *flash) |
| 574 | { |
| 575 | uint8_t status = spi_read_status_register(flash); |
| 576 | spi_prettyprint_status_register_hex(status); |
| 577 | |
| 578 | spi_prettyprint_status_register_atmel_at25_wpen(status); |
| 579 | spi_prettyprint_status_register_bp(status, 4); |
| 580 | /* FIXME: Pretty-print detailed sector protection status. */ |
| 581 | spi_prettyprint_status_register_welwip(status); |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | int spi_prettyprint_status_register_at26df081a(struct flashctx *flash) |
| 586 | { |
| 587 | uint8_t status = spi_read_status_register(flash); |
| 588 | spi_prettyprint_status_register_hex(status); |
| 589 | |
| 590 | spi_prettyprint_status_register_atmel_at25_srpl(status); |
| 591 | msg_cdbg("Chip status register: Sequential Program Mode Status (SPM) is %sset\n", |
| 592 | (status & (1 << 6)) ? "" : "not "); |
| 593 | spi_prettyprint_status_register_atmel_at25_epewpp(status); |
| 594 | spi_prettyprint_status_register_atmel_at25_swp(status); |
| 595 | spi_prettyprint_status_register_welwip(status); |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | /* Some Atmel DataFlash chips support per sector protection bits and the write protection bits in the status |
| 600 | * register do indicate if none, some or all sectors are protected. It is possible to globally (un)lock all |
| 601 | * sectors at once by writing 0 not only the protection bits (2 and 3) but also completely unrelated bits (4 and |
| 602 | * 5) which normally are not touched. |
| 603 | * Affected are all known Atmel chips matched by AT2[56]D[FLQ]..1A? but the AT26DF041. */ |
| 604 | int spi_disable_blockprotect_at2x_global_unprotect(struct flashctx *flash) |
| 605 | { |
| 606 | return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 1 << 4, 0x00); |
| 607 | } |
| 608 | |
| 609 | int spi_disable_blockprotect_at2x_global_unprotect_sec(struct flashctx *flash) |
| 610 | { |
| 611 | /* FIXME: We should check the security lockdown. */ |
| 612 | msg_cinfo("Ignoring security lockdown (if present)\n"); |
| 613 | return spi_disable_blockprotect_at2x_global_unprotect(flash); |
| 614 | } |
| 615 | |
Edward O'Callaghan | 71e2314 | 2019-03-03 23:08:22 +1100 | [diff] [blame] | 616 | int spi_disable_blockprotect_at25f(struct flashctx *flash) |
| 617 | { |
| 618 | return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF); |
| 619 | } |
| 620 | |
| 621 | int spi_disable_blockprotect_at25f512a(struct flashctx *flash) |
| 622 | { |
| 623 | return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 0, 0xFF); |
| 624 | } |
| 625 | |
| 626 | int spi_disable_blockprotect_at25f512b(struct flashctx *flash) |
| 627 | { |
| 628 | return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 1 << 4, 0xFF); |
| 629 | } |
| 630 | |
| 631 | int spi_disable_blockprotect_at25fs010(struct flashctx *flash) |
| 632 | { |
| 633 | return spi_disable_blockprotect_generic(flash, 0x6C, 1 << 7, 0, 0xFF); |
| 634 | } |
| 635 | |
| 636 | int spi_disable_blockprotect_at25fs040(struct flashctx *flash) |
| 637 | { |
| 638 | return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF); |
| 639 | } |
| 640 | |
| 641 | /* === Eon === */ |
| 642 | |
| 643 | int spi_prettyprint_status_register_en25s_wp(struct flashctx *flash) |
| 644 | { |
| 645 | uint8_t status = spi_read_status_register(flash); |
| 646 | spi_prettyprint_status_register_hex(status); |
| 647 | |
| 648 | spi_prettyprint_status_register_srwd(status); |
| 649 | msg_cdbg("Chip status register: WP# disable (WPDIS) is %sabled\n", (status & (1 << 6)) ? "en " : "dis"); |
| 650 | spi_prettyprint_status_register_bp(status, 3); |
| 651 | spi_prettyprint_status_register_welwip(status); |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | /* === Intel/Numonyx/Micron - Spansion === */ |
| 656 | |
| 657 | int spi_disable_blockprotect_n25q(struct flashctx *flash) |
| 658 | { |
| 659 | return spi_disable_blockprotect_generic(flash, 0x5C, 1 << 7, 0, 0xFF); |
| 660 | } |
| 661 | |
| 662 | int spi_prettyprint_status_register_n25q(struct flashctx *flash) |
| 663 | { |
| 664 | uint8_t status = spi_read_status_register(flash); |
| 665 | spi_prettyprint_status_register_hex(status); |
| 666 | |
| 667 | spi_prettyprint_status_register_srwd(status); |
| 668 | if (flash->chip->total_size <= 32 / 8 * 1024) /* N25Q16 and N25Q32: reserved */ |
| 669 | spi_prettyprint_status_register_bit(status, 6); |
| 670 | else |
| 671 | msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n", |
| 672 | (status & (1 << 6)) ? "" : "not "); |
| 673 | msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top"); |
| 674 | spi_prettyprint_status_register_bp(status, 2); |
| 675 | spi_prettyprint_status_register_welwip(status); |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | /* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */ |
| 680 | /* TODO: Clear P_FAIL and E_FAIL with Clear SR Fail Flags Command (30h) here? */ |
| 681 | int spi_disable_blockprotect_bp2_ep_srwd(struct flashctx *flash) |
| 682 | { |
| 683 | return spi_disable_blockprotect_bp2_srwd(flash); |
| 684 | } |
| 685 | |
| 686 | /* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */ |
| 687 | int spi_prettyprint_status_register_bp2_ep_srwd(struct flashctx *flash) |
| 688 | { |
| 689 | uint8_t status = spi_read_status_register(flash); |
| 690 | spi_prettyprint_status_register_hex(status); |
| 691 | |
| 692 | spi_prettyprint_status_register_srwd(status); |
| 693 | msg_cdbg("Chip status register: Program Fail Flag (P_FAIL) is %sset\n", |
| 694 | (status & (1 << 6)) ? "" : "not "); |
| 695 | msg_cdbg("Chip status register: Erase Fail Flag (E_FAIL) is %sset\n", |
| 696 | (status & (1 << 5)) ? "" : "not "); |
| 697 | spi_prettyprint_status_register_bp(status, 2); |
| 698 | spi_prettyprint_status_register_welwip(status); |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | /* === SST === */ |
| 703 | |
| 704 | static void spi_prettyprint_status_register_sst25_common(uint8_t status) |
| 705 | { |
| 706 | spi_prettyprint_status_register_hex(status); |
| 707 | |
| 708 | spi_prettyprint_status_register_bpl(status); |
| 709 | msg_cdbg("Chip status register: Auto Address Increment Programming (AAI) is %sset\n", |
| 710 | (status & (1 << 6)) ? "" : "not "); |
| 711 | spi_prettyprint_status_register_bp(status, 3); |
| 712 | spi_prettyprint_status_register_welwip(status); |
| 713 | } |
| 714 | |
| 715 | int spi_prettyprint_status_register_sst25(struct flashctx *flash) |
| 716 | { |
| 717 | uint8_t status = spi_read_status_register(flash); |
| 718 | spi_prettyprint_status_register_sst25_common(status); |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | int spi_prettyprint_status_register_sst25vf016(struct flashctx *flash) |
| 723 | { |
| 724 | static const char *const bpt[] = { |
| 725 | "none", |
| 726 | "1F0000H-1FFFFFH", |
| 727 | "1E0000H-1FFFFFH", |
| 728 | "1C0000H-1FFFFFH", |
| 729 | "180000H-1FFFFFH", |
| 730 | "100000H-1FFFFFH", |
| 731 | "all", "all" |
| 732 | }; |
| 733 | uint8_t status = spi_read_status_register(flash); |
| 734 | spi_prettyprint_status_register_sst25_common(status); |
| 735 | msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]); |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | int spi_prettyprint_status_register_sst25vf040b(struct flashctx *flash) |
| 740 | { |
| 741 | static const char *const bpt[] = { |
| 742 | "none", |
| 743 | "0x70000-0x7ffff", |
| 744 | "0x60000-0x7ffff", |
| 745 | "0x40000-0x7ffff", |
| 746 | "all blocks", "all blocks", "all blocks", "all blocks" |
| 747 | }; |
| 748 | uint8_t status = spi_read_status_register(flash); |
| 749 | spi_prettyprint_status_register_sst25_common(status); |
| 750 | msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]); |
| 751 | return 0; |
| 752 | } |