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