blob: 8cd5a286cf7b9d045f4c7414e7f6e6d991711825 [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 === */
Stefan Tauner6ee37e22012-12-29 15:03:51 +000025static int spi_write_status_register_flag(struct flashctx *flash, int status, const unsigned char enable_opcode)
26{
27 int result;
28 int i = 0;
29 /*
30 * WRSR requires either EWSR or WREN depending on chip type.
31 * The code below relies on the fact hat EWSR and WREN have the same
32 * INSIZE and OUTSIZE.
33 */
34 struct spi_command cmds[] = {
35 {
36 .writecnt = JEDEC_WREN_OUTSIZE,
37 .writearr = (const unsigned char[]){ enable_opcode },
38 .readcnt = 0,
39 .readarr = NULL,
40 }, {
41 .writecnt = JEDEC_WRSR_OUTSIZE,
42 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
43 .readcnt = 0,
44 .readarr = NULL,
45 }, {
46 .writecnt = 0,
47 .writearr = NULL,
48 .readcnt = 0,
49 .readarr = NULL,
50 }};
51
52 result = spi_send_multicommand(flash, cmds);
53 if (result) {
54 msg_cerr("%s failed during command execution\n", __func__);
55 /* No point in waiting for the command to complete if execution
56 * failed.
57 */
58 return result;
59 }
60 /* WRSR performs a self-timed erase before the changes take effect.
61 * This may take 50-85 ms in most cases, and some chips apparently
62 * allow running RDSR only once. Therefore pick an initial delay of
63 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
64 */
65 programmer_delay(100 * 1000);
66 while (spi_read_status_register(flash) & SPI_SR_WIP) {
67 if (++i > 490) {
68 msg_cerr("Error: WIP bit after WRSR never cleared\n");
69 return TIMEOUT_ERROR;
70 }
71 programmer_delay(10 * 1000);
72 }
73 return 0;
74}
75
76int spi_write_status_register(struct flashctx *flash, int status)
77{
78 int feature_bits = flash->chip->feature_bits;
79 int ret = 1;
80
81 if (!(feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
82 msg_cdbg("Missing status register write definition, assuming "
83 "EWSR is needed\n");
84 feature_bits |= FEATURE_WRSR_EWSR;
85 }
86 if (feature_bits & FEATURE_WRSR_WREN)
87 ret = spi_write_status_register_flag(flash, status, JEDEC_WREN);
88 if (ret && (feature_bits & FEATURE_WRSR_EWSR))
89 ret = spi_write_status_register_flag(flash, status, JEDEC_EWSR);
90 return ret;
91}
92
93uint8_t spi_read_status_register(struct flashctx *flash)
94{
95 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
96 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
97 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
98 int ret;
99
100 /* Read Status Register */
101 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
Nico Huber1f081532017-10-14 15:01:13 +0200102 if (ret) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000103 msg_cerr("RDSR failed!\n");
Nico Huber1f081532017-10-14 15:01:13 +0200104 /* FIXME: We should propagate the error. */
105 return 0;
106 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000107
108 return readarr[0];
109}
110
Stefan Tauner9530a022012-12-29 15:04:05 +0000111/* A generic block protection disable.
112 * Tests if a protection is enabled with the block protection mask (bp_mask) and returns success otherwise.
113 * Tests if the register bits are locked with the lock_mask (lock_mask).
Stefan Taunercecb2c52013-06-20 22:55:41 +0000114 * Tests if a hardware protection is active (i.e. low pin/high bit value) with the write protection mask
115 * (wp_mask) and bails out in that case.
116 * If there are register lock bits set we try to disable them by unsetting those bits of the previous register
117 * contents that are set in the lock_mask. We then check if removing the lock bits has worked and continue as if
118 * they never had been engaged:
119 * If the lock bits are out of the way try to disable engaged protections.
120 * To support uncommon global unprotects (e.g. on most AT2[56]xx1(A)) unprotect_mask can be used to force
121 * bits to 0 additionally to those set in bp_mask and lock_mask. Only bits set in unprotect_mask are potentially
122 * preserved when doing the final unprotect.
123 *
124 * To sum up:
125 * bp_mask: set those bits that correspond to the bits in the status register that indicate an active protection
126 * (which should be unset after this function returns).
127 * lock_mask: set the bits that correspond to the bits that lock changing the bits above.
128 * wp_mask: set the bits that correspond to bits indicating non-software revocable protections.
129 * unprotect_mask: set the bits that should be preserved if possible when unprotecting.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000130 */
Stefan Taunercecb2c52013-06-20 22:55:41 +0000131static 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 +0000132{
133 uint8_t status;
134 int result;
135
136 status = spi_read_status_register(flash);
Stefan Tauner9530a022012-12-29 15:04:05 +0000137 if ((status & bp_mask) == 0) {
138 msg_cdbg2("Block protection is disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000139 return 0;
Stefan Tauner9530a022012-12-29 15:04:05 +0000140 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000141
142 msg_cdbg("Some block protection in effect, disabling... ");
Stefan Tauner9530a022012-12-29 15:04:05 +0000143 if ((status & lock_mask) != 0) {
144 msg_cdbg("\n\tNeed to disable the register lock first... ");
145 if (wp_mask != 0 && (status & wp_mask) == 0) {
146 msg_cerr("Hardware protection is active, disabling write protection is impossible.\n");
147 return 1;
148 }
149 /* All bits except the register lock bit (often called SPRL, SRWD, WPEN) are readonly. */
150 result = spi_write_status_register(flash, status & ~lock_mask);
151 if (result) {
152 msg_cerr("spi_write_status_register failed.\n");
153 return result;
154 }
Stefan Taunercecb2c52013-06-20 22:55:41 +0000155 status = spi_read_status_register(flash);
156 if ((status & lock_mask) != 0) {
157 msg_cerr("Unsetting lock bit(s) failed.\n");
158 return 1;
159 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000160 msg_cdbg("done.\n");
161 }
162 /* Global unprotect. Make sure to mask the register lock bit as well. */
Stefan Taunercecb2c52013-06-20 22:55:41 +0000163 result = spi_write_status_register(flash, status & ~(bp_mask | lock_mask) & unprotect_mask);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000164 if (result) {
165 msg_cerr("spi_write_status_register failed.\n");
166 return result;
167 }
168 status = spi_read_status_register(flash);
Stefan Tauner9530a022012-12-29 15:04:05 +0000169 if ((status & bp_mask) != 0) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000170 msg_cerr("Block protection could not be disabled!\n");
Stefan Taunercecb2c52013-06-20 22:55:41 +0000171 flash->chip->printlock(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000172 return 1;
173 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000174 msg_cdbg("disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000175 return 0;
176}
177
Stefan Tauner9530a022012-12-29 15:04:05 +0000178/* A common block protection disable that tries to unset the status register bits masked by 0x3C. */
179int spi_disable_blockprotect(struct flashctx *flash)
180{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000181 return spi_disable_blockprotect_generic(flash, 0x3C, 0, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000182}
183
Wei Hu25584de2018-04-30 14:02:08 -0700184int spi_disable_blockprotect_sst26_global_unprotect(struct flashctx *flash)
185{
186 int result = spi_write_enable(flash);
187 if (result)
188 return result;
189
190 static const unsigned char cmd[] = { 0x98 }; /* ULBPR */
191 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
192 if (result)
193 msg_cerr("ULBPR failed\n");
194 return result;
195}
196
Stefan Taunera60d4082014-06-04 16:17:03 +0000197/* A common block protection disable that tries to unset the status register bits masked by 0x0C (BP0-1) and
198 * protected/locked by bit #7. Useful when bits 4-5 may be non-0). */
199int spi_disable_blockprotect_bp1_srwd(struct flashctx *flash)
200{
201 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
202}
203
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000204/* A common block protection disable that tries to unset the status register bits masked by 0x1C (BP0-2) and
205 * protected/locked by bit #7. Useful when bit #5 is neither a protection bit nor reserved (and hence possibly
206 * non-0). */
207int spi_disable_blockprotect_bp2_srwd(struct flashctx *flash)
208{
209 return spi_disable_blockprotect_generic(flash, 0x1C, 1 << 7, 0, 0xFF);
210}
211
212/* A common block protection disable that tries to unset the status register bits masked by 0x3C (BP0-3) and
213 * protected/locked by bit #7. */
214int spi_disable_blockprotect_bp3_srwd(struct flashctx *flash)
215{
216 return spi_disable_blockprotect_generic(flash, 0x3C, 1 << 7, 0, 0xFF);
217}
218
219/* A common block protection disable that tries to unset the status register bits masked by 0x7C (BP0-4) and
220 * protected/locked by bit #7. */
221int spi_disable_blockprotect_bp4_srwd(struct flashctx *flash)
222{
223 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
224}
Stefan Tauner9530a022012-12-29 15:04:05 +0000225
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000226static void spi_prettyprint_status_register_hex(uint8_t status)
227{
228 msg_cdbg("Chip status register is 0x%02x.\n", status);
229}
230
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000231/* Common highest bit: Status Register Write Disable (SRWD) or Status Register Protect (SRP). */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000232static void spi_prettyprint_status_register_srwd(uint8_t status)
233{
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000234 msg_cdbg("Chip status register: Status Register Write Disable (SRWD, SRP, ...) is %sset\n",
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000235 (status & (1 << 7)) ? "" : "not ");
236}
237
238/* Common highest bit: Block Protect Write Disable (BPL). */
239static void spi_prettyprint_status_register_bpl(uint8_t status)
240{
241 msg_cdbg("Chip status register: Block Protect Write Disable (BPL) is %sset\n",
242 (status & (1 << 7)) ? "" : "not ");
243}
244
245/* Common lowest 2 bits: WEL and WIP. */
246static void spi_prettyprint_status_register_welwip(uint8_t status)
247{
248 msg_cdbg("Chip status register: Write Enable Latch (WEL) is %sset\n",
249 (status & (1 << 1)) ? "" : "not ");
250 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is %sset\n",
251 (status & (1 << 0)) ? "" : "not ");
252}
253
254/* Common block protection (BP) bits. */
255static void spi_prettyprint_status_register_bp(uint8_t status, int bp)
256{
257 switch (bp) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000258 case 4:
259 msg_cdbg("Chip status register: Block Protect 4 (BP4) is %sset\n",
Stefan Tauner5c316f92015-02-08 21:57:52 +0000260 (status & (1 << 6)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000261 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000262 case 3:
263 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
264 (status & (1 << 5)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000265 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000266 case 2:
267 msg_cdbg("Chip status register: Block Protect 2 (BP2) is %sset\n",
268 (status & (1 << 4)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000269 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000270 case 1:
271 msg_cdbg("Chip status register: Block Protect 1 (BP1) is %sset\n",
272 (status & (1 << 3)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000273 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000274 case 0:
275 msg_cdbg("Chip status register: Block Protect 0 (BP0) is %sset\n",
276 (status & (1 << 2)) ? "" : "not ");
277 }
278}
279
280/* Unnamed bits. */
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000281void spi_prettyprint_status_register_bit(uint8_t status, int bit)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000282{
283 msg_cdbg("Chip status register: Bit %i is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
284}
285
286int spi_prettyprint_status_register_plain(struct flashctx *flash)
287{
288 uint8_t status = spi_read_status_register(flash);
289 spi_prettyprint_status_register_hex(status);
290 return 0;
291}
292
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000293/* Print the plain hex value and the welwip bits only. */
294int spi_prettyprint_status_register_default_welwip(struct flashctx *flash)
295{
296 uint8_t status = spi_read_status_register(flash);
297 spi_prettyprint_status_register_hex(status);
298
299 spi_prettyprint_status_register_welwip(status);
300 return 0;
301}
302
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000303/* Works for many chips of the
304 * AMIC A25L series
305 * and MX MX25L512
306 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000307int spi_prettyprint_status_register_bp1_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000308{
309 uint8_t status = spi_read_status_register(flash);
310 spi_prettyprint_status_register_hex(status);
311
312 spi_prettyprint_status_register_srwd(status);
313 spi_prettyprint_status_register_bit(status, 6);
314 spi_prettyprint_status_register_bit(status, 5);
315 spi_prettyprint_status_register_bit(status, 4);
316 spi_prettyprint_status_register_bp(status, 1);
317 spi_prettyprint_status_register_welwip(status);
318 return 0;
319}
320
321/* Works for many chips of the
322 * AMIC A25L series
Stefan Taunerf4451612013-04-19 01:59:15 +0000323 * PMC Pm25LD series
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000324 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000325int spi_prettyprint_status_register_bp2_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000326{
327 uint8_t status = spi_read_status_register(flash);
328 spi_prettyprint_status_register_hex(status);
329
330 spi_prettyprint_status_register_srwd(status);
331 spi_prettyprint_status_register_bit(status, 6);
332 spi_prettyprint_status_register_bit(status, 5);
333 spi_prettyprint_status_register_bp(status, 2);
334 spi_prettyprint_status_register_welwip(status);
335 return 0;
336}
337
338/* Works for many chips of the
339 * ST M25P series
340 * MX MX25L series
341 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000342int spi_prettyprint_status_register_bp3_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000343{
344 uint8_t status = spi_read_status_register(flash);
345 spi_prettyprint_status_register_hex(status);
346
347 spi_prettyprint_status_register_srwd(status);
348 spi_prettyprint_status_register_bit(status, 6);
349 spi_prettyprint_status_register_bp(status, 3);
350 spi_prettyprint_status_register_welwip(status);
351 return 0;
352}
353
Stefan Tauner12f3d512014-05-27 21:27:27 +0000354int spi_prettyprint_status_register_bp4_srwd(struct flashctx *flash)
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000355{
356 uint8_t status = spi_read_status_register(flash);
357 spi_prettyprint_status_register_hex(status);
358
359 spi_prettyprint_status_register_srwd(status);
360 spi_prettyprint_status_register_bp(status, 4);
361 spi_prettyprint_status_register_welwip(status);
362 return 0;
363}
364
Stefan Tauner85f09f72014-05-27 21:27:14 +0000365int spi_prettyprint_status_register_bp2_bpl(struct flashctx *flash)
366{
367 uint8_t status = spi_read_status_register(flash);
368 spi_prettyprint_status_register_hex(status);
369
370 spi_prettyprint_status_register_bpl(status);
371 spi_prettyprint_status_register_bit(status, 6);
372 spi_prettyprint_status_register_bit(status, 5);
373 spi_prettyprint_status_register_bp(status, 2);
374 spi_prettyprint_status_register_welwip(status);
375 return 0;
376}
377
Ben Gardnerbcf61092015-11-22 02:23:31 +0000378int spi_prettyprint_status_register_bp2_tb_bpl(struct flashctx *flash)
379{
380 uint8_t status = spi_read_status_register(flash);
381 spi_prettyprint_status_register_hex(status);
382
383 spi_prettyprint_status_register_bpl(status);
384 spi_prettyprint_status_register_bit(status, 6);
385 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
386 spi_prettyprint_status_register_bp(status, 2);
387 spi_prettyprint_status_register_welwip(status);
388 return 0;
389}
390
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000391/* === Amic ===
392 * FIXME: spi_disable_blockprotect is incorrect but works fine for chips using
Stefan Tauner12f3d512014-05-27 21:27:27 +0000393 * spi_prettyprint_status_register_bp1_srwd or
394 * spi_prettyprint_status_register_bp2_srwd.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000395 * FIXME: spi_disable_blockprotect is incorrect and will fail for chips using
396 * spi_prettyprint_status_register_amic_a25l032 if those have locks controlled
397 * by the second status register.
398 */
399
400int spi_prettyprint_status_register_amic_a25l032(struct flashctx *flash)
401{
402 uint8_t status = spi_read_status_register(flash);
403 spi_prettyprint_status_register_hex(status);
404
405 spi_prettyprint_status_register_srwd(status);
406 msg_cdbg("Chip status register: Sector Protect Size (SEC) is %i KB\n", (status & (1 << 6)) ? 4 : 64);
407 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
408 spi_prettyprint_status_register_bp(status, 2);
409 spi_prettyprint_status_register_welwip(status);
410 msg_cdbg("Chip status register 2 is NOT decoded!\n");
411 return 0;
412}
413
414/* === Atmel === */
415
416static void spi_prettyprint_status_register_atmel_at25_wpen(uint8_t status)
417{
418 msg_cdbg("Chip status register: Write Protect Enable (WPEN) is %sset\n",
419 (status & (1 << 7)) ? "" : "not ");
420}
421
422static void spi_prettyprint_status_register_atmel_at25_srpl(uint8_t status)
423{
424 msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) is %sset\n",
425 (status & (1 << 7)) ? "" : "not ");
426}
427
428static void spi_prettyprint_status_register_atmel_at25_epewpp(uint8_t status)
429{
430 msg_cdbg("Chip status register: Erase/Program Error (EPE) is %sset\n",
431 (status & (1 << 5)) ? "" : "not ");
432 msg_cdbg("Chip status register: WP# pin (WPP) is %sasserted\n",
433 (status & (1 << 4)) ? "not " : "");
434}
435
436static void spi_prettyprint_status_register_atmel_at25_swp(uint8_t status)
437{
438 msg_cdbg("Chip status register: Software Protection Status (SWP): ");
439 switch (status & (3 << 2)) {
440 case 0x0 << 2:
441 msg_cdbg("no sectors are protected\n");
442 break;
443 case 0x1 << 2:
444 msg_cdbg("some sectors are protected\n");
445 /* FIXME: Read individual Sector Protection Registers. */
446 break;
447 case 0x3 << 2:
448 msg_cdbg("all sectors are protected\n");
449 break;
450 default:
451 msg_cdbg("reserved for future use\n");
452 break;
453 }
454}
455
456int spi_prettyprint_status_register_at25df(struct flashctx *flash)
457{
458 uint8_t status = spi_read_status_register(flash);
459 spi_prettyprint_status_register_hex(status);
460
461 spi_prettyprint_status_register_atmel_at25_srpl(status);
462 spi_prettyprint_status_register_bit(status, 6);
463 spi_prettyprint_status_register_atmel_at25_epewpp(status);
464 spi_prettyprint_status_register_atmel_at25_swp(status);
465 spi_prettyprint_status_register_welwip(status);
466 return 0;
467}
468
469int spi_prettyprint_status_register_at25df_sec(struct flashctx *flash)
470{
471 /* FIXME: We should check the security lockdown. */
472 msg_cdbg("Ignoring security lockdown (if present)\n");
473 msg_cdbg("Ignoring status register byte 2\n");
474 return spi_prettyprint_status_register_at25df(flash);
475}
476
Stefan Tauner57794ac2012-12-29 15:04:20 +0000477/* used for AT25F512, AT25F1024(A), AT25F2048 */
478int spi_prettyprint_status_register_at25f(struct flashctx *flash)
479{
480 uint8_t status;
481
482 status = spi_read_status_register(flash);
483 spi_prettyprint_status_register_hex(status);
484
485 spi_prettyprint_status_register_atmel_at25_wpen(status);
486 spi_prettyprint_status_register_bit(status, 6);
487 spi_prettyprint_status_register_bit(status, 5);
488 spi_prettyprint_status_register_bit(status, 4);
489 spi_prettyprint_status_register_bp(status, 1);
490 spi_prettyprint_status_register_welwip(status);
491 return 0;
492}
493
494int spi_prettyprint_status_register_at25f512a(struct flashctx *flash)
495{
496 uint8_t status;
497
498 status = spi_read_status_register(flash);
499 spi_prettyprint_status_register_hex(status);
500
501 spi_prettyprint_status_register_atmel_at25_wpen(status);
502 spi_prettyprint_status_register_bit(status, 6);
503 spi_prettyprint_status_register_bit(status, 5);
504 spi_prettyprint_status_register_bit(status, 4);
505 spi_prettyprint_status_register_bit(status, 3);
506 spi_prettyprint_status_register_bp(status, 0);
507 spi_prettyprint_status_register_welwip(status);
508 return 0;
509}
510
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000511int spi_prettyprint_status_register_at25f512b(struct flashctx *flash)
512{
513 uint8_t status = spi_read_status_register(flash);
514 spi_prettyprint_status_register_hex(status);
515
516 spi_prettyprint_status_register_atmel_at25_srpl(status);
517 spi_prettyprint_status_register_bit(status, 6);
518 spi_prettyprint_status_register_atmel_at25_epewpp(status);
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 Tauner57794ac2012-12-29 15:04:20 +0000525int spi_prettyprint_status_register_at25f4096(struct flashctx *flash)
526{
527 uint8_t status;
528
529 status = spi_read_status_register(flash);
530 spi_prettyprint_status_register_hex(status);
531
532 spi_prettyprint_status_register_atmel_at25_wpen(status);
533 spi_prettyprint_status_register_bit(status, 6);
534 spi_prettyprint_status_register_bit(status, 5);
535 spi_prettyprint_status_register_bp(status, 2);
536 spi_prettyprint_status_register_welwip(status);
537 return 0;
538}
539
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000540int spi_prettyprint_status_register_at25fs010(struct flashctx *flash)
541{
542 uint8_t status = spi_read_status_register(flash);
543 spi_prettyprint_status_register_hex(status);
544
545 spi_prettyprint_status_register_atmel_at25_wpen(status);
546 msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is "
547 "%sset\n", (status & (1 << 6)) ? "" : "not ");
548 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
549 "%sset\n", (status & (1 << 5)) ? "" : "not ");
550 spi_prettyprint_status_register_bit(status, 4);
551 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
552 "%sset\n", (status & (1 << 3)) ? "" : "not ");
553 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
554 "%sset\n", (status & (1 << 2)) ? "" : "not ");
555 /* FIXME: Pretty-print detailed sector protection status. */
556 spi_prettyprint_status_register_welwip(status);
557 return 0;
558}
559
560int spi_prettyprint_status_register_at25fs040(struct flashctx *flash)
561{
562 uint8_t status = spi_read_status_register(flash);
563 spi_prettyprint_status_register_hex(status);
564
565 spi_prettyprint_status_register_atmel_at25_wpen(status);
566 spi_prettyprint_status_register_bp(status, 4);
567 /* FIXME: Pretty-print detailed sector protection status. */
568 spi_prettyprint_status_register_welwip(status);
569 return 0;
570}
571
572int spi_prettyprint_status_register_at26df081a(struct flashctx *flash)
573{
574 uint8_t status = spi_read_status_register(flash);
575 spi_prettyprint_status_register_hex(status);
576
577 spi_prettyprint_status_register_atmel_at25_srpl(status);
578 msg_cdbg("Chip status register: Sequential Program Mode Status (SPM) is %sset\n",
579 (status & (1 << 6)) ? "" : "not ");
580 spi_prettyprint_status_register_atmel_at25_epewpp(status);
581 spi_prettyprint_status_register_atmel_at25_swp(status);
582 spi_prettyprint_status_register_welwip(status);
583 return 0;
584}
585
Stefan Taunercecb2c52013-06-20 22:55:41 +0000586/* Some Atmel DataFlash chips support per sector protection bits and the write protection bits in the status
587 * register do indicate if none, some or all sectors are protected. It is possible to globally (un)lock all
588 * sectors at once by writing 0 not only the protection bits (2 and 3) but also completely unrelated bits (4 and
589 * 5) which normally are not touched.
590 * Affected are all known Atmel chips matched by AT2[56]D[FLQ]..1A? but the AT26DF041. */
591int spi_disable_blockprotect_at2x_global_unprotect(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000592{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000593 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 1 << 4, 0x00);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000594}
595
Stefan Taunercecb2c52013-06-20 22:55:41 +0000596int spi_disable_blockprotect_at2x_global_unprotect_sec(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000597{
598 /* FIXME: We should check the security lockdown. */
599 msg_cinfo("Ignoring security lockdown (if present)\n");
Stefan Taunercecb2c52013-06-20 22:55:41 +0000600 return spi_disable_blockprotect_at2x_global_unprotect(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000601}
602
Stefan Tauner57794ac2012-12-29 15:04:20 +0000603int spi_disable_blockprotect_at25f(struct flashctx *flash)
604{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000605 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000606}
607
608int spi_disable_blockprotect_at25f512a(struct flashctx *flash)
609{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000610 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000611}
612
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000613int spi_disable_blockprotect_at25f512b(struct flashctx *flash)
614{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000615 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 1 << 4, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000616}
617
618int spi_disable_blockprotect_at25fs010(struct flashctx *flash)
619{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000620 return spi_disable_blockprotect_generic(flash, 0x6C, 1 << 7, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000621 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000622
623int spi_disable_blockprotect_at25fs040(struct flashctx *flash)
624{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000625 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000626}
627
Nikolay Nikolaevd0e3ea12013-06-28 21:29:08 +0000628/* === Eon === */
629
630int spi_prettyprint_status_register_en25s_wp(struct flashctx *flash)
631{
632 uint8_t status = spi_read_status_register(flash);
633 spi_prettyprint_status_register_hex(status);
634
635 spi_prettyprint_status_register_srwd(status);
636 msg_cdbg("Chip status register: WP# disable (WPDIS) is %sabled\n", (status & (1 << 6)) ? "en " : "dis");
637 spi_prettyprint_status_register_bp(status, 3);
638 spi_prettyprint_status_register_welwip(status);
639 return 0;
640}
641
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000642/* === Intel/Numonyx/Micron - Spansion === */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000643
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000644int spi_disable_blockprotect_n25q(struct flashctx *flash)
645{
646 return spi_disable_blockprotect_generic(flash, 0x5C, 1 << 7, 0, 0xFF);
647}
648
649int spi_prettyprint_status_register_n25q(struct flashctx *flash)
650{
651 uint8_t status = spi_read_status_register(flash);
652 spi_prettyprint_status_register_hex(status);
653
654 spi_prettyprint_status_register_srwd(status);
655 if (flash->chip->total_size <= 32 / 8 * 1024) /* N25Q16 and N25Q32: reserved */
656 spi_prettyprint_status_register_bit(status, 6);
657 else
658 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
659 (status & (1 << 6)) ? "" : "not ");
660 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
661 spi_prettyprint_status_register_bp(status, 2);
662 spi_prettyprint_status_register_welwip(status);
663 return 0;
664}
665
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000666/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000667/* TODO: Clear P_FAIL and E_FAIL with Clear SR Fail Flags Command (30h) here? */
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000668int spi_disable_blockprotect_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000669{
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000670 return spi_disable_blockprotect_bp2_srwd(flash);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000671}
672
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000673/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
674int spi_prettyprint_status_register_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000675{
676 uint8_t status = spi_read_status_register(flash);
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000677 spi_prettyprint_status_register_hex(status);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000678
679 spi_prettyprint_status_register_srwd(status);
680 msg_cdbg("Chip status register: Program Fail Flag (P_FAIL) is %sset\n",
681 (status & (1 << 6)) ? "" : "not ");
682 msg_cdbg("Chip status register: Erase Fail Flag (E_FAIL) is %sset\n",
683 (status & (1 << 5)) ? "" : "not ");
684 spi_prettyprint_status_register_bp(status, 2);
685 spi_prettyprint_status_register_welwip(status);
686 return 0;
687}
688
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000689/* === SST === */
690
691static void spi_prettyprint_status_register_sst25_common(uint8_t status)
692{
693 spi_prettyprint_status_register_hex(status);
694
695 spi_prettyprint_status_register_bpl(status);
696 msg_cdbg("Chip status register: Auto Address Increment Programming (AAI) is %sset\n",
697 (status & (1 << 6)) ? "" : "not ");
698 spi_prettyprint_status_register_bp(status, 3);
699 spi_prettyprint_status_register_welwip(status);
700}
701
702int spi_prettyprint_status_register_sst25(struct flashctx *flash)
703{
704 uint8_t status = spi_read_status_register(flash);
705 spi_prettyprint_status_register_sst25_common(status);
706 return 0;
707}
708
709int spi_prettyprint_status_register_sst25vf016(struct flashctx *flash)
710{
711 static const char *const bpt[] = {
712 "none",
713 "1F0000H-1FFFFFH",
714 "1E0000H-1FFFFFH",
715 "1C0000H-1FFFFFH",
716 "180000H-1FFFFFH",
717 "100000H-1FFFFFH",
718 "all", "all"
719 };
720 uint8_t status = spi_read_status_register(flash);
721 spi_prettyprint_status_register_sst25_common(status);
722 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
723 return 0;
724}
725
726int spi_prettyprint_status_register_sst25vf040b(struct flashctx *flash)
727{
728 static const char *const bpt[] = {
729 "none",
730 "0x70000-0x7ffff",
731 "0x60000-0x7ffff",
732 "0x40000-0x7ffff",
733 "all blocks", "all blocks", "all blocks", "all blocks"
734 };
735 uint8_t status = spi_read_status_register(flash);
736 spi_prettyprint_status_register_sst25_common(status);
737 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
738 return 0;
739}