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