blob: 793290a66f73a6a5a04c26f7b3731649ce41bd3a [file] [log] [blame]
Stefan Tauner6ee37e22012-12-29 15:03:51 +00001/*
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 === */
29int 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
43static 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
94int 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
111uint8_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 Tauner9530a022012-12-29 15:04:05 +0000126/* 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 Taunercecb2c52013-06-20 22:55:41 +0000129 * 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 Tauner6ee37e22012-12-29 15:03:51 +0000145 */
Stefan Taunercecb2c52013-06-20 22:55:41 +0000146static 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 Tauner6ee37e22012-12-29 15:03:51 +0000147{
148 uint8_t status;
149 int result;
150
151 status = spi_read_status_register(flash);
Stefan Tauner9530a022012-12-29 15:04:05 +0000152 if ((status & bp_mask) == 0) {
153 msg_cdbg2("Block protection is disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000154 return 0;
Stefan Tauner9530a022012-12-29 15:04:05 +0000155 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000156
157 msg_cdbg("Some block protection in effect, disabling... ");
Stefan Tauner9530a022012-12-29 15:04:05 +0000158 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 Taunercecb2c52013-06-20 22:55:41 +0000170 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 Tauner9530a022012-12-29 15:04:05 +0000175 msg_cdbg("done.\n");
176 }
177 /* Global unprotect. Make sure to mask the register lock bit as well. */
Stefan Taunercecb2c52013-06-20 22:55:41 +0000178 result = spi_write_status_register(flash, status & ~(bp_mask | lock_mask) & unprotect_mask);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000179 if (result) {
180 msg_cerr("spi_write_status_register failed.\n");
181 return result;
182 }
183 status = spi_read_status_register(flash);
Stefan Tauner9530a022012-12-29 15:04:05 +0000184 if ((status & bp_mask) != 0) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000185 msg_cerr("Block protection could not be disabled!\n");
Stefan Taunercecb2c52013-06-20 22:55:41 +0000186 flash->chip->printlock(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000187 return 1;
188 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000189 msg_cdbg("disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000190 return 0;
191}
192
Stefan Tauner9530a022012-12-29 15:04:05 +0000193/* A common block protection disable that tries to unset the status register bits masked by 0x3C. */
194int spi_disable_blockprotect(struct flashctx *flash)
195{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000196 return spi_disable_blockprotect_generic(flash, 0x3C, 0, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000197}
198
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000199/* A common block protection disable that tries to unset the status register bits masked by 0x1C (BP0-2) and
200 * protected/locked by bit #7. Useful when bit #5 is neither a protection bit nor reserved (and hence possibly
201 * non-0). */
202int spi_disable_blockprotect_bp2_srwd(struct flashctx *flash)
203{
204 return spi_disable_blockprotect_generic(flash, 0x1C, 1 << 7, 0, 0xFF);
205}
206
207/* A common block protection disable that tries to unset the status register bits masked by 0x3C (BP0-3) and
208 * protected/locked by bit #7. */
209int spi_disable_blockprotect_bp3_srwd(struct flashctx *flash)
210{
211 return spi_disable_blockprotect_generic(flash, 0x3C, 1 << 7, 0, 0xFF);
212}
213
214/* A common block protection disable that tries to unset the status register bits masked by 0x7C (BP0-4) and
215 * protected/locked by bit #7. */
216int spi_disable_blockprotect_bp4_srwd(struct flashctx *flash)
217{
218 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
219}
Stefan Tauner9530a022012-12-29 15:04:05 +0000220
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000221static void spi_prettyprint_status_register_hex(uint8_t status)
222{
223 msg_cdbg("Chip status register is 0x%02x.\n", status);
224}
225
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000226/* Common highest bit: Status Register Write Disable (SRWD) or Status Register Protect (SRP). */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000227static void spi_prettyprint_status_register_srwd(uint8_t status)
228{
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000229 msg_cdbg("Chip status register: Status Register Write Disable (SRWD, SRP, ...) is %sset\n",
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000230 (status & (1 << 7)) ? "" : "not ");
231}
232
233/* Common highest bit: Block Protect Write Disable (BPL). */
234static void spi_prettyprint_status_register_bpl(uint8_t status)
235{
236 msg_cdbg("Chip status register: Block Protect Write Disable (BPL) is %sset\n",
237 (status & (1 << 7)) ? "" : "not ");
238}
239
240/* Common lowest 2 bits: WEL and WIP. */
241static void spi_prettyprint_status_register_welwip(uint8_t status)
242{
243 msg_cdbg("Chip status register: Write Enable Latch (WEL) is %sset\n",
244 (status & (1 << 1)) ? "" : "not ");
245 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is %sset\n",
246 (status & (1 << 0)) ? "" : "not ");
247}
248
249/* Common block protection (BP) bits. */
250static void spi_prettyprint_status_register_bp(uint8_t status, int bp)
251{
252 switch (bp) {
253 /* Fall through. */
254 case 4:
255 msg_cdbg("Chip status register: Block Protect 4 (BP4) is %sset\n",
256 (status & (1 << 5)) ? "" : "not ");
257 case 3:
258 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
259 (status & (1 << 5)) ? "" : "not ");
260 case 2:
261 msg_cdbg("Chip status register: Block Protect 2 (BP2) is %sset\n",
262 (status & (1 << 4)) ? "" : "not ");
263 case 1:
264 msg_cdbg("Chip status register: Block Protect 1 (BP1) is %sset\n",
265 (status & (1 << 3)) ? "" : "not ");
266 case 0:
267 msg_cdbg("Chip status register: Block Protect 0 (BP0) is %sset\n",
268 (status & (1 << 2)) ? "" : "not ");
269 }
270}
271
272/* Unnamed bits. */
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000273void spi_prettyprint_status_register_bit(uint8_t status, int bit)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000274{
275 msg_cdbg("Chip status register: Bit %i is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
276}
277
278int spi_prettyprint_status_register_plain(struct flashctx *flash)
279{
280 uint8_t status = spi_read_status_register(flash);
281 spi_prettyprint_status_register_hex(status);
282 return 0;
283}
284
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000285/* Print the plain hex value and the welwip bits only. */
286int spi_prettyprint_status_register_default_welwip(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_welwip(status);
292 return 0;
293}
294
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000295/* Works for many chips of the
296 * AMIC A25L series
297 * and MX MX25L512
298 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000299int spi_prettyprint_status_register_bp1_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000300{
301 uint8_t status = spi_read_status_register(flash);
302 spi_prettyprint_status_register_hex(status);
303
304 spi_prettyprint_status_register_srwd(status);
305 spi_prettyprint_status_register_bit(status, 6);
306 spi_prettyprint_status_register_bit(status, 5);
307 spi_prettyprint_status_register_bit(status, 4);
308 spi_prettyprint_status_register_bp(status, 1);
309 spi_prettyprint_status_register_welwip(status);
310 return 0;
311}
312
313/* Works for many chips of the
314 * AMIC A25L series
Stefan Taunerf4451612013-04-19 01:59:15 +0000315 * PMC Pm25LD series
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000316 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000317int spi_prettyprint_status_register_bp2_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000318{
319 uint8_t status = spi_read_status_register(flash);
320 spi_prettyprint_status_register_hex(status);
321
322 spi_prettyprint_status_register_srwd(status);
323 spi_prettyprint_status_register_bit(status, 6);
324 spi_prettyprint_status_register_bit(status, 5);
325 spi_prettyprint_status_register_bp(status, 2);
326 spi_prettyprint_status_register_welwip(status);
327 return 0;
328}
329
330/* Works for many chips of the
331 * ST M25P series
332 * MX MX25L series
333 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000334int spi_prettyprint_status_register_bp3_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000335{
336 uint8_t status = spi_read_status_register(flash);
337 spi_prettyprint_status_register_hex(status);
338
339 spi_prettyprint_status_register_srwd(status);
340 spi_prettyprint_status_register_bit(status, 6);
341 spi_prettyprint_status_register_bp(status, 3);
342 spi_prettyprint_status_register_welwip(status);
343 return 0;
344}
345
Stefan Tauner12f3d512014-05-27 21:27:27 +0000346int spi_prettyprint_status_register_bp4_srwd(struct flashctx *flash)
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000347{
348 uint8_t status = spi_read_status_register(flash);
349 spi_prettyprint_status_register_hex(status);
350
351 spi_prettyprint_status_register_srwd(status);
352 spi_prettyprint_status_register_bp(status, 4);
353 spi_prettyprint_status_register_welwip(status);
354 return 0;
355}
356
Stefan Tauner85f09f72014-05-27 21:27:14 +0000357int spi_prettyprint_status_register_bp2_bpl(struct flashctx *flash)
358{
359 uint8_t status = spi_read_status_register(flash);
360 spi_prettyprint_status_register_hex(status);
361
362 spi_prettyprint_status_register_bpl(status);
363 spi_prettyprint_status_register_bit(status, 6);
364 spi_prettyprint_status_register_bit(status, 5);
365 spi_prettyprint_status_register_bp(status, 2);
366 spi_prettyprint_status_register_welwip(status);
367 return 0;
368}
369
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000370/* === Amic ===
371 * FIXME: spi_disable_blockprotect is incorrect but works fine for chips using
Stefan Tauner12f3d512014-05-27 21:27:27 +0000372 * spi_prettyprint_status_register_bp1_srwd or
373 * spi_prettyprint_status_register_bp2_srwd.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000374 * FIXME: spi_disable_blockprotect is incorrect and will fail for chips using
375 * spi_prettyprint_status_register_amic_a25l032 if those have locks controlled
376 * by the second status register.
377 */
378
379int spi_prettyprint_status_register_amic_a25l032(struct flashctx *flash)
380{
381 uint8_t status = spi_read_status_register(flash);
382 spi_prettyprint_status_register_hex(status);
383
384 spi_prettyprint_status_register_srwd(status);
385 msg_cdbg("Chip status register: Sector Protect Size (SEC) is %i KB\n", (status & (1 << 6)) ? 4 : 64);
386 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
387 spi_prettyprint_status_register_bp(status, 2);
388 spi_prettyprint_status_register_welwip(status);
389 msg_cdbg("Chip status register 2 is NOT decoded!\n");
390 return 0;
391}
392
393/* === Atmel === */
394
395static void spi_prettyprint_status_register_atmel_at25_wpen(uint8_t status)
396{
397 msg_cdbg("Chip status register: Write Protect Enable (WPEN) is %sset\n",
398 (status & (1 << 7)) ? "" : "not ");
399}
400
401static void spi_prettyprint_status_register_atmel_at25_srpl(uint8_t status)
402{
403 msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) is %sset\n",
404 (status & (1 << 7)) ? "" : "not ");
405}
406
407static void spi_prettyprint_status_register_atmel_at25_epewpp(uint8_t status)
408{
409 msg_cdbg("Chip status register: Erase/Program Error (EPE) is %sset\n",
410 (status & (1 << 5)) ? "" : "not ");
411 msg_cdbg("Chip status register: WP# pin (WPP) is %sasserted\n",
412 (status & (1 << 4)) ? "not " : "");
413}
414
415static void spi_prettyprint_status_register_atmel_at25_swp(uint8_t status)
416{
417 msg_cdbg("Chip status register: Software Protection Status (SWP): ");
418 switch (status & (3 << 2)) {
419 case 0x0 << 2:
420 msg_cdbg("no sectors are protected\n");
421 break;
422 case 0x1 << 2:
423 msg_cdbg("some sectors are protected\n");
424 /* FIXME: Read individual Sector Protection Registers. */
425 break;
426 case 0x3 << 2:
427 msg_cdbg("all sectors are protected\n");
428 break;
429 default:
430 msg_cdbg("reserved for future use\n");
431 break;
432 }
433}
434
435int spi_prettyprint_status_register_at25df(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_atmel_at25_swp(status);
444 spi_prettyprint_status_register_welwip(status);
445 return 0;
446}
447
448int spi_prettyprint_status_register_at25df_sec(struct flashctx *flash)
449{
450 /* FIXME: We should check the security lockdown. */
451 msg_cdbg("Ignoring security lockdown (if present)\n");
452 msg_cdbg("Ignoring status register byte 2\n");
453 return spi_prettyprint_status_register_at25df(flash);
454}
455
Stefan Tauner57794ac2012-12-29 15:04:20 +0000456/* used for AT25F512, AT25F1024(A), AT25F2048 */
457int spi_prettyprint_status_register_at25f(struct flashctx *flash)
458{
459 uint8_t status;
460
461 status = spi_read_status_register(flash);
462 spi_prettyprint_status_register_hex(status);
463
464 spi_prettyprint_status_register_atmel_at25_wpen(status);
465 spi_prettyprint_status_register_bit(status, 6);
466 spi_prettyprint_status_register_bit(status, 5);
467 spi_prettyprint_status_register_bit(status, 4);
468 spi_prettyprint_status_register_bp(status, 1);
469 spi_prettyprint_status_register_welwip(status);
470 return 0;
471}
472
473int spi_prettyprint_status_register_at25f512a(struct flashctx *flash)
474{
475 uint8_t status;
476
477 status = spi_read_status_register(flash);
478 spi_prettyprint_status_register_hex(status);
479
480 spi_prettyprint_status_register_atmel_at25_wpen(status);
481 spi_prettyprint_status_register_bit(status, 6);
482 spi_prettyprint_status_register_bit(status, 5);
483 spi_prettyprint_status_register_bit(status, 4);
484 spi_prettyprint_status_register_bit(status, 3);
485 spi_prettyprint_status_register_bp(status, 0);
486 spi_prettyprint_status_register_welwip(status);
487 return 0;
488}
489
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000490int spi_prettyprint_status_register_at25f512b(struct flashctx *flash)
491{
492 uint8_t status = spi_read_status_register(flash);
493 spi_prettyprint_status_register_hex(status);
494
495 spi_prettyprint_status_register_atmel_at25_srpl(status);
496 spi_prettyprint_status_register_bit(status, 6);
497 spi_prettyprint_status_register_atmel_at25_epewpp(status);
498 spi_prettyprint_status_register_bit(status, 3);
499 spi_prettyprint_status_register_bp(status, 0);
500 spi_prettyprint_status_register_welwip(status);
501 return 0;
502}
503
Stefan Tauner57794ac2012-12-29 15:04:20 +0000504int spi_prettyprint_status_register_at25f4096(struct flashctx *flash)
505{
506 uint8_t status;
507
508 status = spi_read_status_register(flash);
509 spi_prettyprint_status_register_hex(status);
510
511 spi_prettyprint_status_register_atmel_at25_wpen(status);
512 spi_prettyprint_status_register_bit(status, 6);
513 spi_prettyprint_status_register_bit(status, 5);
514 spi_prettyprint_status_register_bp(status, 2);
515 spi_prettyprint_status_register_welwip(status);
516 return 0;
517}
518
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000519int spi_prettyprint_status_register_at25fs010(struct flashctx *flash)
520{
521 uint8_t status = spi_read_status_register(flash);
522 spi_prettyprint_status_register_hex(status);
523
524 spi_prettyprint_status_register_atmel_at25_wpen(status);
525 msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is "
526 "%sset\n", (status & (1 << 6)) ? "" : "not ");
527 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
528 "%sset\n", (status & (1 << 5)) ? "" : "not ");
529 spi_prettyprint_status_register_bit(status, 4);
530 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
531 "%sset\n", (status & (1 << 3)) ? "" : "not ");
532 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
533 "%sset\n", (status & (1 << 2)) ? "" : "not ");
534 /* FIXME: Pretty-print detailed sector protection status. */
535 spi_prettyprint_status_register_welwip(status);
536 return 0;
537}
538
539int spi_prettyprint_status_register_at25fs040(struct flashctx *flash)
540{
541 uint8_t status = spi_read_status_register(flash);
542 spi_prettyprint_status_register_hex(status);
543
544 spi_prettyprint_status_register_atmel_at25_wpen(status);
545 spi_prettyprint_status_register_bp(status, 4);
546 /* FIXME: Pretty-print detailed sector protection status. */
547 spi_prettyprint_status_register_welwip(status);
548 return 0;
549}
550
551int spi_prettyprint_status_register_at26df081a(struct flashctx *flash)
552{
553 uint8_t status = spi_read_status_register(flash);
554 spi_prettyprint_status_register_hex(status);
555
556 spi_prettyprint_status_register_atmel_at25_srpl(status);
557 msg_cdbg("Chip status register: Sequential Program Mode Status (SPM) is %sset\n",
558 (status & (1 << 6)) ? "" : "not ");
559 spi_prettyprint_status_register_atmel_at25_epewpp(status);
560 spi_prettyprint_status_register_atmel_at25_swp(status);
561 spi_prettyprint_status_register_welwip(status);
562 return 0;
563}
564
Stefan Taunercecb2c52013-06-20 22:55:41 +0000565/* Some Atmel DataFlash chips support per sector protection bits and the write protection bits in the status
566 * register do indicate if none, some or all sectors are protected. It is possible to globally (un)lock all
567 * sectors at once by writing 0 not only the protection bits (2 and 3) but also completely unrelated bits (4 and
568 * 5) which normally are not touched.
569 * Affected are all known Atmel chips matched by AT2[56]D[FLQ]..1A? but the AT26DF041. */
570int spi_disable_blockprotect_at2x_global_unprotect(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000571{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000572 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 1 << 4, 0x00);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000573}
574
Stefan Taunercecb2c52013-06-20 22:55:41 +0000575int spi_disable_blockprotect_at2x_global_unprotect_sec(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000576{
577 /* FIXME: We should check the security lockdown. */
578 msg_cinfo("Ignoring security lockdown (if present)\n");
Stefan Taunercecb2c52013-06-20 22:55:41 +0000579 return spi_disable_blockprotect_at2x_global_unprotect(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000580}
581
Stefan Tauner57794ac2012-12-29 15:04:20 +0000582int spi_disable_blockprotect_at25f(struct flashctx *flash)
583{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000584 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000585}
586
587int spi_disable_blockprotect_at25f512a(struct flashctx *flash)
588{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000589 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000590}
591
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000592int spi_disable_blockprotect_at25f512b(struct flashctx *flash)
593{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000594 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 1 << 4, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000595}
596
597int spi_disable_blockprotect_at25fs010(struct flashctx *flash)
598{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000599 return spi_disable_blockprotect_generic(flash, 0x6C, 1 << 7, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000600 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000601
602int spi_disable_blockprotect_at25fs040(struct flashctx *flash)
603{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000604 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000605}
606
Nikolay Nikolaevd0e3ea12013-06-28 21:29:08 +0000607/* === Eon === */
608
609int spi_prettyprint_status_register_en25s_wp(struct flashctx *flash)
610{
611 uint8_t status = spi_read_status_register(flash);
612 spi_prettyprint_status_register_hex(status);
613
614 spi_prettyprint_status_register_srwd(status);
615 msg_cdbg("Chip status register: WP# disable (WPDIS) is %sabled\n", (status & (1 << 6)) ? "en " : "dis");
616 spi_prettyprint_status_register_bp(status, 3);
617 spi_prettyprint_status_register_welwip(status);
618 return 0;
619}
620
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000621/* === Intel/Numonyx/Micron - Spansion === */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000622
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000623int spi_disable_blockprotect_n25q(struct flashctx *flash)
624{
625 return spi_disable_blockprotect_generic(flash, 0x5C, 1 << 7, 0, 0xFF);
626}
627
628int spi_prettyprint_status_register_n25q(struct flashctx *flash)
629{
630 uint8_t status = spi_read_status_register(flash);
631 spi_prettyprint_status_register_hex(status);
632
633 spi_prettyprint_status_register_srwd(status);
634 if (flash->chip->total_size <= 32 / 8 * 1024) /* N25Q16 and N25Q32: reserved */
635 spi_prettyprint_status_register_bit(status, 6);
636 else
637 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
638 (status & (1 << 6)) ? "" : "not ");
639 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
640 spi_prettyprint_status_register_bp(status, 2);
641 spi_prettyprint_status_register_welwip(status);
642 return 0;
643}
644
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000645/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000646/* TODO: Clear P_FAIL and E_FAIL with Clear SR Fail Flags Command (30h) here? */
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000647int spi_disable_blockprotect_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000648{
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000649 return spi_disable_blockprotect_bp2_srwd(flash);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000650}
651
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000652/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
653int spi_prettyprint_status_register_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000654{
655 uint8_t status = spi_read_status_register(flash);
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000656 spi_prettyprint_status_register_hex(status);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000657
658 spi_prettyprint_status_register_srwd(status);
659 msg_cdbg("Chip status register: Program Fail Flag (P_FAIL) is %sset\n",
660 (status & (1 << 6)) ? "" : "not ");
661 msg_cdbg("Chip status register: Erase Fail Flag (E_FAIL) is %sset\n",
662 (status & (1 << 5)) ? "" : "not ");
663 spi_prettyprint_status_register_bp(status, 2);
664 spi_prettyprint_status_register_welwip(status);
665 return 0;
666}
667
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000668/* === SST === */
669
670static void spi_prettyprint_status_register_sst25_common(uint8_t status)
671{
672 spi_prettyprint_status_register_hex(status);
673
674 spi_prettyprint_status_register_bpl(status);
675 msg_cdbg("Chip status register: Auto Address Increment Programming (AAI) is %sset\n",
676 (status & (1 << 6)) ? "" : "not ");
677 spi_prettyprint_status_register_bp(status, 3);
678 spi_prettyprint_status_register_welwip(status);
679}
680
681int spi_prettyprint_status_register_sst25(struct flashctx *flash)
682{
683 uint8_t status = spi_read_status_register(flash);
684 spi_prettyprint_status_register_sst25_common(status);
685 return 0;
686}
687
688int spi_prettyprint_status_register_sst25vf016(struct flashctx *flash)
689{
690 static const char *const bpt[] = {
691 "none",
692 "1F0000H-1FFFFFH",
693 "1E0000H-1FFFFFH",
694 "1C0000H-1FFFFFH",
695 "180000H-1FFFFFH",
696 "100000H-1FFFFFH",
697 "all", "all"
698 };
699 uint8_t status = spi_read_status_register(flash);
700 spi_prettyprint_status_register_sst25_common(status);
701 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
702 return 0;
703}
704
705int spi_prettyprint_status_register_sst25vf040b(struct flashctx *flash)
706{
707 static const char *const bpt[] = {
708 "none",
709 "0x70000-0x7ffff",
710 "0x60000-0x7ffff",
711 "0x40000-0x7ffff",
712 "all blocks", "all blocks", "all blocks", "all blocks"
713 };
714 uint8_t status = spi_read_status_register(flash);
715 spi_prettyprint_status_register_sst25_common(status);
716 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
717 return 0;
718}