blob: 4cf7023235d6e0ddd79176370a958c1fbdd7bcc2 [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) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000272 case 4:
273 msg_cdbg("Chip status register: Block Protect 4 (BP4) is %sset\n",
Stefan Tauner5c316f92015-02-08 21:57:52 +0000274 (status & (1 << 6)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000275 /* Fall through. */
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 ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000279 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000280 case 2:
281 msg_cdbg("Chip status register: Block Protect 2 (BP2) is %sset\n",
282 (status & (1 << 4)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000283 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000284 case 1:
285 msg_cdbg("Chip status register: Block Protect 1 (BP1) is %sset\n",
286 (status & (1 << 3)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000287 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000288 case 0:
289 msg_cdbg("Chip status register: Block Protect 0 (BP0) is %sset\n",
290 (status & (1 << 2)) ? "" : "not ");
291 }
292}
293
294/* Unnamed bits. */
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000295void spi_prettyprint_status_register_bit(uint8_t status, int bit)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000296{
297 msg_cdbg("Chip status register: Bit %i is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
298}
299
300int spi_prettyprint_status_register_plain(struct flashctx *flash)
301{
302 uint8_t status = spi_read_status_register(flash);
303 spi_prettyprint_status_register_hex(status);
304 return 0;
305}
306
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000307/* Print the plain hex value and the welwip bits only. */
308int spi_prettyprint_status_register_default_welwip(struct flashctx *flash)
309{
310 uint8_t status = spi_read_status_register(flash);
311 spi_prettyprint_status_register_hex(status);
312
313 spi_prettyprint_status_register_welwip(status);
314 return 0;
315}
316
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000317/* Works for many chips of the
318 * AMIC A25L series
319 * and MX MX25L512
320 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000321int spi_prettyprint_status_register_bp1_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000322{
323 uint8_t status = spi_read_status_register(flash);
324 spi_prettyprint_status_register_hex(status);
325
326 spi_prettyprint_status_register_srwd(status);
327 spi_prettyprint_status_register_bit(status, 6);
328 spi_prettyprint_status_register_bit(status, 5);
329 spi_prettyprint_status_register_bit(status, 4);
330 spi_prettyprint_status_register_bp(status, 1);
331 spi_prettyprint_status_register_welwip(status);
332 return 0;
333}
334
335/* Works for many chips of the
336 * AMIC A25L series
Stefan Taunerf4451612013-04-19 01:59:15 +0000337 * PMC Pm25LD series
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000338 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000339int spi_prettyprint_status_register_bp2_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000340{
341 uint8_t status = spi_read_status_register(flash);
342 spi_prettyprint_status_register_hex(status);
343
344 spi_prettyprint_status_register_srwd(status);
345 spi_prettyprint_status_register_bit(status, 6);
346 spi_prettyprint_status_register_bit(status, 5);
347 spi_prettyprint_status_register_bp(status, 2);
348 spi_prettyprint_status_register_welwip(status);
349 return 0;
350}
351
352/* Works for many chips of the
353 * ST M25P series
354 * MX MX25L series
355 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000356int spi_prettyprint_status_register_bp3_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000357{
358 uint8_t status = spi_read_status_register(flash);
359 spi_prettyprint_status_register_hex(status);
360
361 spi_prettyprint_status_register_srwd(status);
362 spi_prettyprint_status_register_bit(status, 6);
363 spi_prettyprint_status_register_bp(status, 3);
364 spi_prettyprint_status_register_welwip(status);
365 return 0;
366}
367
Stefan Tauner12f3d512014-05-27 21:27:27 +0000368int spi_prettyprint_status_register_bp4_srwd(struct flashctx *flash)
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000369{
370 uint8_t status = spi_read_status_register(flash);
371 spi_prettyprint_status_register_hex(status);
372
373 spi_prettyprint_status_register_srwd(status);
374 spi_prettyprint_status_register_bp(status, 4);
375 spi_prettyprint_status_register_welwip(status);
376 return 0;
377}
378
Stefan Tauner85f09f72014-05-27 21:27:14 +0000379int spi_prettyprint_status_register_bp2_bpl(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_bpl(status);
385 spi_prettyprint_status_register_bit(status, 6);
386 spi_prettyprint_status_register_bit(status, 5);
387 spi_prettyprint_status_register_bp(status, 2);
388 spi_prettyprint_status_register_welwip(status);
389 return 0;
390}
391
Ben Gardnerbcf61092015-11-22 02:23:31 +0000392int spi_prettyprint_status_register_bp2_tb_bpl(struct flashctx *flash)
393{
394 uint8_t status = spi_read_status_register(flash);
395 spi_prettyprint_status_register_hex(status);
396
397 spi_prettyprint_status_register_bpl(status);
398 spi_prettyprint_status_register_bit(status, 6);
399 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
400 spi_prettyprint_status_register_bp(status, 2);
401 spi_prettyprint_status_register_welwip(status);
402 return 0;
403}
404
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000405/* === Amic ===
406 * FIXME: spi_disable_blockprotect is incorrect but works fine for chips using
Stefan Tauner12f3d512014-05-27 21:27:27 +0000407 * spi_prettyprint_status_register_bp1_srwd or
408 * spi_prettyprint_status_register_bp2_srwd.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000409 * FIXME: spi_disable_blockprotect is incorrect and will fail for chips using
410 * spi_prettyprint_status_register_amic_a25l032 if those have locks controlled
411 * by the second status register.
412 */
413
414int spi_prettyprint_status_register_amic_a25l032(struct flashctx *flash)
415{
416 uint8_t status = spi_read_status_register(flash);
417 spi_prettyprint_status_register_hex(status);
418
419 spi_prettyprint_status_register_srwd(status);
420 msg_cdbg("Chip status register: Sector Protect Size (SEC) is %i KB\n", (status & (1 << 6)) ? 4 : 64);
421 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
422 spi_prettyprint_status_register_bp(status, 2);
423 spi_prettyprint_status_register_welwip(status);
424 msg_cdbg("Chip status register 2 is NOT decoded!\n");
425 return 0;
426}
427
428/* === Atmel === */
429
430static void spi_prettyprint_status_register_atmel_at25_wpen(uint8_t status)
431{
432 msg_cdbg("Chip status register: Write Protect Enable (WPEN) is %sset\n",
433 (status & (1 << 7)) ? "" : "not ");
434}
435
436static void spi_prettyprint_status_register_atmel_at25_srpl(uint8_t status)
437{
438 msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) is %sset\n",
439 (status & (1 << 7)) ? "" : "not ");
440}
441
442static void spi_prettyprint_status_register_atmel_at25_epewpp(uint8_t status)
443{
444 msg_cdbg("Chip status register: Erase/Program Error (EPE) is %sset\n",
445 (status & (1 << 5)) ? "" : "not ");
446 msg_cdbg("Chip status register: WP# pin (WPP) is %sasserted\n",
447 (status & (1 << 4)) ? "not " : "");
448}
449
450static void spi_prettyprint_status_register_atmel_at25_swp(uint8_t status)
451{
452 msg_cdbg("Chip status register: Software Protection Status (SWP): ");
453 switch (status & (3 << 2)) {
454 case 0x0 << 2:
455 msg_cdbg("no sectors are protected\n");
456 break;
457 case 0x1 << 2:
458 msg_cdbg("some sectors are protected\n");
459 /* FIXME: Read individual Sector Protection Registers. */
460 break;
461 case 0x3 << 2:
462 msg_cdbg("all sectors are protected\n");
463 break;
464 default:
465 msg_cdbg("reserved for future use\n");
466 break;
467 }
468}
469
470int spi_prettyprint_status_register_at25df(struct flashctx *flash)
471{
472 uint8_t status = spi_read_status_register(flash);
473 spi_prettyprint_status_register_hex(status);
474
475 spi_prettyprint_status_register_atmel_at25_srpl(status);
476 spi_prettyprint_status_register_bit(status, 6);
477 spi_prettyprint_status_register_atmel_at25_epewpp(status);
478 spi_prettyprint_status_register_atmel_at25_swp(status);
479 spi_prettyprint_status_register_welwip(status);
480 return 0;
481}
482
483int spi_prettyprint_status_register_at25df_sec(struct flashctx *flash)
484{
485 /* FIXME: We should check the security lockdown. */
486 msg_cdbg("Ignoring security lockdown (if present)\n");
487 msg_cdbg("Ignoring status register byte 2\n");
488 return spi_prettyprint_status_register_at25df(flash);
489}
490
Stefan Tauner57794ac2012-12-29 15:04:20 +0000491/* used for AT25F512, AT25F1024(A), AT25F2048 */
492int spi_prettyprint_status_register_at25f(struct flashctx *flash)
493{
494 uint8_t status;
495
496 status = spi_read_status_register(flash);
497 spi_prettyprint_status_register_hex(status);
498
499 spi_prettyprint_status_register_atmel_at25_wpen(status);
500 spi_prettyprint_status_register_bit(status, 6);
501 spi_prettyprint_status_register_bit(status, 5);
502 spi_prettyprint_status_register_bit(status, 4);
503 spi_prettyprint_status_register_bp(status, 1);
504 spi_prettyprint_status_register_welwip(status);
505 return 0;
506}
507
508int spi_prettyprint_status_register_at25f512a(struct flashctx *flash)
509{
510 uint8_t status;
511
512 status = spi_read_status_register(flash);
513 spi_prettyprint_status_register_hex(status);
514
515 spi_prettyprint_status_register_atmel_at25_wpen(status);
516 spi_prettyprint_status_register_bit(status, 6);
517 spi_prettyprint_status_register_bit(status, 5);
518 spi_prettyprint_status_register_bit(status, 4);
519 spi_prettyprint_status_register_bit(status, 3);
520 spi_prettyprint_status_register_bp(status, 0);
521 spi_prettyprint_status_register_welwip(status);
522 return 0;
523}
524
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000525int spi_prettyprint_status_register_at25f512b(struct flashctx *flash)
526{
527 uint8_t status = spi_read_status_register(flash);
528 spi_prettyprint_status_register_hex(status);
529
530 spi_prettyprint_status_register_atmel_at25_srpl(status);
531 spi_prettyprint_status_register_bit(status, 6);
532 spi_prettyprint_status_register_atmel_at25_epewpp(status);
533 spi_prettyprint_status_register_bit(status, 3);
534 spi_prettyprint_status_register_bp(status, 0);
535 spi_prettyprint_status_register_welwip(status);
536 return 0;
537}
538
Stefan Tauner57794ac2012-12-29 15:04:20 +0000539int spi_prettyprint_status_register_at25f4096(struct flashctx *flash)
540{
541 uint8_t status;
542
543 status = spi_read_status_register(flash);
544 spi_prettyprint_status_register_hex(status);
545
546 spi_prettyprint_status_register_atmel_at25_wpen(status);
547 spi_prettyprint_status_register_bit(status, 6);
548 spi_prettyprint_status_register_bit(status, 5);
549 spi_prettyprint_status_register_bp(status, 2);
550 spi_prettyprint_status_register_welwip(status);
551 return 0;
552}
553
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000554int spi_prettyprint_status_register_at25fs010(struct flashctx *flash)
555{
556 uint8_t status = spi_read_status_register(flash);
557 spi_prettyprint_status_register_hex(status);
558
559 spi_prettyprint_status_register_atmel_at25_wpen(status);
560 msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is "
561 "%sset\n", (status & (1 << 6)) ? "" : "not ");
562 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
563 "%sset\n", (status & (1 << 5)) ? "" : "not ");
564 spi_prettyprint_status_register_bit(status, 4);
565 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
566 "%sset\n", (status & (1 << 3)) ? "" : "not ");
567 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
568 "%sset\n", (status & (1 << 2)) ? "" : "not ");
569 /* FIXME: Pretty-print detailed sector protection status. */
570 spi_prettyprint_status_register_welwip(status);
571 return 0;
572}
573
574int spi_prettyprint_status_register_at25fs040(struct flashctx *flash)
575{
576 uint8_t status = spi_read_status_register(flash);
577 spi_prettyprint_status_register_hex(status);
578
579 spi_prettyprint_status_register_atmel_at25_wpen(status);
580 spi_prettyprint_status_register_bp(status, 4);
581 /* FIXME: Pretty-print detailed sector protection status. */
582 spi_prettyprint_status_register_welwip(status);
583 return 0;
584}
585
586int spi_prettyprint_status_register_at26df081a(struct flashctx *flash)
587{
588 uint8_t status = spi_read_status_register(flash);
589 spi_prettyprint_status_register_hex(status);
590
591 spi_prettyprint_status_register_atmel_at25_srpl(status);
592 msg_cdbg("Chip status register: Sequential Program Mode Status (SPM) is %sset\n",
593 (status & (1 << 6)) ? "" : "not ");
594 spi_prettyprint_status_register_atmel_at25_epewpp(status);
595 spi_prettyprint_status_register_atmel_at25_swp(status);
596 spi_prettyprint_status_register_welwip(status);
597 return 0;
598}
599
Stefan Taunercecb2c52013-06-20 22:55:41 +0000600/* Some Atmel DataFlash chips support per sector protection bits and the write protection bits in the status
601 * register do indicate if none, some or all sectors are protected. It is possible to globally (un)lock all
602 * sectors at once by writing 0 not only the protection bits (2 and 3) but also completely unrelated bits (4 and
603 * 5) which normally are not touched.
604 * Affected are all known Atmel chips matched by AT2[56]D[FLQ]..1A? but the AT26DF041. */
605int spi_disable_blockprotect_at2x_global_unprotect(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000606{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000607 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 1 << 4, 0x00);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000608}
609
Stefan Taunercecb2c52013-06-20 22:55:41 +0000610int spi_disable_blockprotect_at2x_global_unprotect_sec(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000611{
612 /* FIXME: We should check the security lockdown. */
613 msg_cinfo("Ignoring security lockdown (if present)\n");
Stefan Taunercecb2c52013-06-20 22:55:41 +0000614 return spi_disable_blockprotect_at2x_global_unprotect(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000615}
616
Stefan Tauner57794ac2012-12-29 15:04:20 +0000617int spi_disable_blockprotect_at25f(struct flashctx *flash)
618{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000619 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000620}
621
622int spi_disable_blockprotect_at25f512a(struct flashctx *flash)
623{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000624 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000625}
626
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000627int spi_disable_blockprotect_at25f512b(struct flashctx *flash)
628{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000629 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 1 << 4, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000630}
631
632int spi_disable_blockprotect_at25fs010(struct flashctx *flash)
633{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000634 return spi_disable_blockprotect_generic(flash, 0x6C, 1 << 7, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000635 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000636
637int spi_disable_blockprotect_at25fs040(struct flashctx *flash)
638{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000639 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000640}
641
Nikolay Nikolaevd0e3ea12013-06-28 21:29:08 +0000642/* === Eon === */
643
644int spi_prettyprint_status_register_en25s_wp(struct flashctx *flash)
645{
646 uint8_t status = spi_read_status_register(flash);
647 spi_prettyprint_status_register_hex(status);
648
649 spi_prettyprint_status_register_srwd(status);
650 msg_cdbg("Chip status register: WP# disable (WPDIS) is %sabled\n", (status & (1 << 6)) ? "en " : "dis");
651 spi_prettyprint_status_register_bp(status, 3);
652 spi_prettyprint_status_register_welwip(status);
653 return 0;
654}
655
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000656/* === Intel/Numonyx/Micron - Spansion === */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000657
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000658int spi_disable_blockprotect_n25q(struct flashctx *flash)
659{
660 return spi_disable_blockprotect_generic(flash, 0x5C, 1 << 7, 0, 0xFF);
661}
662
663int spi_prettyprint_status_register_n25q(struct flashctx *flash)
664{
665 uint8_t status = spi_read_status_register(flash);
666 spi_prettyprint_status_register_hex(status);
667
668 spi_prettyprint_status_register_srwd(status);
669 if (flash->chip->total_size <= 32 / 8 * 1024) /* N25Q16 and N25Q32: reserved */
670 spi_prettyprint_status_register_bit(status, 6);
671 else
672 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
673 (status & (1 << 6)) ? "" : "not ");
674 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
675 spi_prettyprint_status_register_bp(status, 2);
676 spi_prettyprint_status_register_welwip(status);
677 return 0;
678}
679
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000680/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000681/* TODO: Clear P_FAIL and E_FAIL with Clear SR Fail Flags Command (30h) here? */
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000682int spi_disable_blockprotect_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000683{
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000684 return spi_disable_blockprotect_bp2_srwd(flash);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000685}
686
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000687/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
688int spi_prettyprint_status_register_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000689{
690 uint8_t status = spi_read_status_register(flash);
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000691 spi_prettyprint_status_register_hex(status);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000692
693 spi_prettyprint_status_register_srwd(status);
694 msg_cdbg("Chip status register: Program Fail Flag (P_FAIL) is %sset\n",
695 (status & (1 << 6)) ? "" : "not ");
696 msg_cdbg("Chip status register: Erase Fail Flag (E_FAIL) is %sset\n",
697 (status & (1 << 5)) ? "" : "not ");
698 spi_prettyprint_status_register_bp(status, 2);
699 spi_prettyprint_status_register_welwip(status);
700 return 0;
701}
702
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000703/* === SST === */
704
705static void spi_prettyprint_status_register_sst25_common(uint8_t status)
706{
707 spi_prettyprint_status_register_hex(status);
708
709 spi_prettyprint_status_register_bpl(status);
710 msg_cdbg("Chip status register: Auto Address Increment Programming (AAI) is %sset\n",
711 (status & (1 << 6)) ? "" : "not ");
712 spi_prettyprint_status_register_bp(status, 3);
713 spi_prettyprint_status_register_welwip(status);
714}
715
716int spi_prettyprint_status_register_sst25(struct flashctx *flash)
717{
718 uint8_t status = spi_read_status_register(flash);
719 spi_prettyprint_status_register_sst25_common(status);
720 return 0;
721}
722
723int spi_prettyprint_status_register_sst25vf016(struct flashctx *flash)
724{
725 static const char *const bpt[] = {
726 "none",
727 "1F0000H-1FFFFFH",
728 "1E0000H-1FFFFFH",
729 "1C0000H-1FFFFFH",
730 "180000H-1FFFFFH",
731 "100000H-1FFFFFH",
732 "all", "all"
733 };
734 uint8_t status = spi_read_status_register(flash);
735 spi_prettyprint_status_register_sst25_common(status);
736 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
737 return 0;
738}
739
740int spi_prettyprint_status_register_sst25vf040b(struct flashctx *flash)
741{
742 static const char *const bpt[] = {
743 "none",
744 "0x70000-0x7ffff",
745 "0x60000-0x7ffff",
746 "0x40000-0x7ffff",
747 "all blocks", "all blocks", "all blocks", "all blocks"
748 };
749 uint8_t status = spi_read_status_register(flash);
750 spi_prettyprint_status_register_sst25_common(status);
751 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
752 return 0;
753}