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