blob: 0f430565dfb37203572d4cfc5a2c13fa9181a6dc [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 === */
Nikolai Artemiev01675222021-10-20 22:30:41 +110025int spi_write_register(const struct flashctx *flash, enum flash_reg reg, uint8_t value)
Stefan Tauner6ee37e22012-12-29 15:03:51 +000026{
27 int feature_bits = flash->chip->feature_bits;
Nikolai Artemiev01675222021-10-20 22:30:41 +110028
29 uint8_t write_cmd[3];
30 size_t write_cmd_len = 0;
31
32 /*
33 * Create SPI write command sequence based on the destination register
34 * and the chip's supported command set.
35 */
36 switch (reg) {
37 case STATUS1:
38 write_cmd[0] = JEDEC_WRSR;
39 write_cmd[1] = value;
40 write_cmd_len = JEDEC_WRSR_OUTSIZE;
41 break;
Nikolai Artemiev9de3f872021-10-20 22:32:25 +110042 case STATUS2:
43 if (feature_bits & FEATURE_WRSR2) {
44 write_cmd[0] = JEDEC_WRSR2;
45 write_cmd[1] = value;
46 write_cmd_len = JEDEC_WRSR2_OUTSIZE;
47 break;
48 }
49 if (feature_bits & FEATURE_WRSR_EXT) {
50 /*
51 * Writing SR2 with an extended WRSR command requires
52 * writing SR1 along with SR2, so just read SR1 and
53 * write it back
54 */
55 uint8_t sr1;
56
57 if (spi_read_register(flash, STATUS1, &sr1)) {
58 msg_cerr("Writing SR2 failed: failed to read SR1 for writeback.\n");
59 return 1;
60 }
61 write_cmd[0] = JEDEC_WRSR;
62 write_cmd[1] = sr1;
63 write_cmd[2] = value;
64 write_cmd_len = JEDEC_WRSR_EXT_OUTSIZE;
65 break;
66 }
67 msg_cerr("Cannot write SR2: unsupported by chip\n");
68 return 1;
Sergii Dmytruk0b2e7dd2021-12-19 18:37:51 +020069 case STATUS3:
70 write_cmd[0] = JEDEC_WRSR3;
71 write_cmd[1] = value;
72 write_cmd_len = JEDEC_WRSR3_OUTSIZE;
73 break;
Nikolai Artemiev01675222021-10-20 22:30:41 +110074 default:
75 msg_cerr("Cannot write register: unknown register\n");
76 return 1;
77 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +000078
Nikolai Artemieva1d68652021-11-22 13:18:49 +110079 uint8_t enable_cmd;
80 if (feature_bits & FEATURE_WRSR_WREN) {
81 enable_cmd = JEDEC_WREN;
82 } else if (feature_bits & FEATURE_WRSR_EWSR) {
83 enable_cmd = JEDEC_EWSR;
84 } else {
Stefan Tauner6ee37e22012-12-29 15:03:51 +000085 msg_cdbg("Missing status register write definition, assuming "
86 "EWSR is needed\n");
Nikolai Artemieva1d68652021-11-22 13:18:49 +110087 enable_cmd = JEDEC_EWSR;
Stefan Tauner6ee37e22012-12-29 15:03:51 +000088 }
Nikolai Artemiev01675222021-10-20 22:30:41 +110089
Nikolai Artemieva1d68652021-11-22 13:18:49 +110090 struct spi_command cmds[] = {
91 {
92 .writecnt = JEDEC_WREN_OUTSIZE,
93 .writearr = &enable_cmd,
94 .readcnt = 0,
95 .readarr = NULL,
96 }, {
97 .writecnt = write_cmd_len,
98 .writearr = write_cmd,
99 .readcnt = 0,
100 .readarr = NULL,
101 }, {
102 .writecnt = 0,
103 .writearr = NULL,
104 .readcnt = 0,
105 .readarr = NULL,
106 }};
107
108 int result = spi_send_multicommand(flash, cmds);
109 if (result) {
110 msg_cerr("%s failed during command execution\n", __func__);
111 return result;
112 }
113
114 /*
115 * WRSR performs a self-timed erase before the changes take effect.
116 * This may take 50-85 ms in most cases, and some chips apparently
117 * allow running RDSR only once. Therefore pick an initial delay of
118 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
119 *
120 * Newer chips with multiple status registers (SR2 etc.) are unlikely
121 * to have problems with multiple RDSR commands, so only wait for the
122 * initial 100 ms if the register we wrote to was SR1.
123 */
124 int delay_ms = 5000;
125 if (reg == STATUS1) {
126 programmer_delay(100 * 1000);
127 delay_ms -= 100;
128 }
129
130 for (; delay_ms > 0; delay_ms -= 10) {
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100131 uint8_t status;
132 result = spi_read_register(flash, STATUS1, &status);
133 if (result)
134 return result;
135 if ((status & SPI_SR_WIP) == 0)
Nikolai Artemieva1d68652021-11-22 13:18:49 +1100136 return 0;
137 programmer_delay(10 * 1000);
138 }
139
140
141 msg_cerr("Error: WIP bit after WRSR never cleared\n");
142 return TIMEOUT_ERROR;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000143}
144
Nikolai Artemiev01675222021-10-20 22:30:41 +1100145int spi_read_register(const struct flashctx *flash, enum flash_reg reg, uint8_t *value)
146{
Nikolai Artemiev9de3f872021-10-20 22:32:25 +1100147 int feature_bits = flash->chip->feature_bits;
Nikolai Artemiev01675222021-10-20 22:30:41 +1100148 uint8_t read_cmd;
149
150 switch (reg) {
151 case STATUS1:
152 read_cmd = JEDEC_RDSR;
153 break;
Nikolai Artemiev9de3f872021-10-20 22:32:25 +1100154 case STATUS2:
155 if (feature_bits & (FEATURE_WRSR_EXT | FEATURE_WRSR2)) {
156 read_cmd = JEDEC_RDSR2;
157 break;
158 }
159 msg_cerr("Cannot read SR2: unsupported by chip\n");
160 return 1;
Sergii Dmytruk0b2e7dd2021-12-19 18:37:51 +0200161 case STATUS3:
162 read_cmd = JEDEC_RDSR3;
163 break;
Nikolai Artemiev01675222021-10-20 22:30:41 +1100164 default:
165 msg_cerr("Cannot read register: unknown register\n");
166 return 1;
167 }
168
169 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
170 /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
171 uint8_t readarr[2];
172
173 int ret = spi_send_command(flash, sizeof(read_cmd), sizeof(readarr), &read_cmd, readarr);
174 if (ret) {
175 msg_cerr("Register read failed!\n");
176 return ret;
177 }
178
179 *value = readarr[0];
180 return 0;
181}
182
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100183static int spi_restore_status(struct flashctx *flash, uint8_t status)
184{
185 msg_cdbg("restoring chip status (0x%02x)\n", status);
Nikolai Artemiev01675222021-10-20 22:30:41 +1100186 return spi_write_register(flash, STATUS1, status);
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100187}
188
Stefan Tauner9530a022012-12-29 15:04:05 +0000189/* A generic block protection disable.
190 * Tests if a protection is enabled with the block protection mask (bp_mask) and returns success otherwise.
191 * Tests if the register bits are locked with the lock_mask (lock_mask).
Stefan Taunercecb2c52013-06-20 22:55:41 +0000192 * Tests if a hardware protection is active (i.e. low pin/high bit value) with the write protection mask
193 * (wp_mask) and bails out in that case.
194 * If there are register lock bits set we try to disable them by unsetting those bits of the previous register
195 * contents that are set in the lock_mask. We then check if removing the lock bits has worked and continue as if
196 * they never had been engaged:
197 * If the lock bits are out of the way try to disable engaged protections.
198 * To support uncommon global unprotects (e.g. on most AT2[56]xx1(A)) unprotect_mask can be used to force
199 * bits to 0 additionally to those set in bp_mask and lock_mask. Only bits set in unprotect_mask are potentially
200 * preserved when doing the final unprotect.
201 *
202 * To sum up:
203 * bp_mask: set those bits that correspond to the bits in the status register that indicate an active protection
204 * (which should be unset after this function returns).
205 * lock_mask: set the bits that correspond to the bits that lock changing the bits above.
206 * wp_mask: set the bits that correspond to bits indicating non-software revocable protections.
207 * unprotect_mask: set the bits that should be preserved if possible when unprotecting.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000208 */
Stefan Taunercecb2c52013-06-20 22:55:41 +0000209static 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 +0000210{
211 uint8_t status;
212 int result;
213
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100214 int ret = spi_read_register(flash, STATUS1, &status);
215 if (ret)
216 return ret;
217
Stefan Tauner9530a022012-12-29 15:04:05 +0000218 if ((status & bp_mask) == 0) {
219 msg_cdbg2("Block protection is disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000220 return 0;
Stefan Tauner9530a022012-12-29 15:04:05 +0000221 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000222
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100223 /* Restore status register content upon exit in finalize_flash_access(). */
224 register_chip_restore(spi_restore_status, flash, status);
225
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000226 msg_cdbg("Some block protection in effect, disabling... ");
Stefan Tauner9530a022012-12-29 15:04:05 +0000227 if ((status & lock_mask) != 0) {
228 msg_cdbg("\n\tNeed to disable the register lock first... ");
229 if (wp_mask != 0 && (status & wp_mask) == 0) {
230 msg_cerr("Hardware protection is active, disabling write protection is impossible.\n");
231 return 1;
232 }
233 /* All bits except the register lock bit (often called SPRL, SRWD, WPEN) are readonly. */
Nikolai Artemiev01675222021-10-20 22:30:41 +1100234 result = spi_write_register(flash, STATUS1, status & ~lock_mask);
Stefan Tauner9530a022012-12-29 15:04:05 +0000235 if (result) {
Nikolai Artemiev01675222021-10-20 22:30:41 +1100236 msg_cerr("Could not write status register 1.\n");
Stefan Tauner9530a022012-12-29 15:04:05 +0000237 return result;
238 }
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100239
240 ret = spi_read_register(flash, STATUS1, &status);
241 if (ret)
242 return ret;
243
Stefan Taunercecb2c52013-06-20 22:55:41 +0000244 if ((status & lock_mask) != 0) {
245 msg_cerr("Unsetting lock bit(s) failed.\n");
246 return 1;
247 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000248 msg_cdbg("done.\n");
249 }
250 /* Global unprotect. Make sure to mask the register lock bit as well. */
Nikolai Artemiev01675222021-10-20 22:30:41 +1100251 result = spi_write_register(flash, STATUS1, status & ~(bp_mask | lock_mask) & unprotect_mask);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000252 if (result) {
Nikolai Artemiev01675222021-10-20 22:30:41 +1100253 msg_cerr("Could not write status register 1.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000254 return result;
255 }
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100256
257 ret = spi_read_register(flash, STATUS1, &status);
258 if (ret)
259 return ret;
260
Stefan Tauner9530a022012-12-29 15:04:05 +0000261 if ((status & bp_mask) != 0) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000262 msg_cerr("Block protection could not be disabled!\n");
Yuji Sasaki4af36092019-03-22 10:59:50 -0700263 if (flash->chip->printlock)
264 flash->chip->printlock(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000265 return 1;
266 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000267 msg_cdbg("disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000268 return 0;
269}
270
Stefan Tauner9530a022012-12-29 15:04:05 +0000271/* A common block protection disable that tries to unset the status register bits masked by 0x3C. */
272int spi_disable_blockprotect(struct flashctx *flash)
273{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000274 return spi_disable_blockprotect_generic(flash, 0x3C, 0, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000275}
276
Wei Hu25584de2018-04-30 14:02:08 -0700277int spi_disable_blockprotect_sst26_global_unprotect(struct flashctx *flash)
278{
279 int result = spi_write_enable(flash);
280 if (result)
281 return result;
282
283 static const unsigned char cmd[] = { 0x98 }; /* ULBPR */
284 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
285 if (result)
286 msg_cerr("ULBPR failed\n");
287 return result;
288}
289
Stefan Taunera60d4082014-06-04 16:17:03 +0000290/* A common block protection disable that tries to unset the status register bits masked by 0x0C (BP0-1) and
291 * protected/locked by bit #7. Useful when bits 4-5 may be non-0). */
292int spi_disable_blockprotect_bp1_srwd(struct flashctx *flash)
293{
294 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
295}
296
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000297/* A common block protection disable that tries to unset the status register bits masked by 0x1C (BP0-2) and
298 * protected/locked by bit #7. Useful when bit #5 is neither a protection bit nor reserved (and hence possibly
299 * non-0). */
300int spi_disable_blockprotect_bp2_srwd(struct flashctx *flash)
301{
302 return spi_disable_blockprotect_generic(flash, 0x1C, 1 << 7, 0, 0xFF);
303}
304
305/* A common block protection disable that tries to unset the status register bits masked by 0x3C (BP0-3) and
306 * protected/locked by bit #7. */
307int spi_disable_blockprotect_bp3_srwd(struct flashctx *flash)
308{
309 return spi_disable_blockprotect_generic(flash, 0x3C, 1 << 7, 0, 0xFF);
310}
311
312/* A common block protection disable that tries to unset the status register bits masked by 0x7C (BP0-4) and
313 * protected/locked by bit #7. */
314int spi_disable_blockprotect_bp4_srwd(struct flashctx *flash)
315{
316 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
317}
Stefan Tauner9530a022012-12-29 15:04:05 +0000318
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000319static void spi_prettyprint_status_register_hex(uint8_t status)
320{
321 msg_cdbg("Chip status register is 0x%02x.\n", status);
322}
323
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000324/* Common highest bit: Status Register Write Disable (SRWD) or Status Register Protect (SRP). */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000325static void spi_prettyprint_status_register_srwd(uint8_t status)
326{
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000327 msg_cdbg("Chip status register: Status Register Write Disable (SRWD, SRP, ...) is %sset\n",
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000328 (status & (1 << 7)) ? "" : "not ");
329}
330
331/* Common highest bit: Block Protect Write Disable (BPL). */
332static void spi_prettyprint_status_register_bpl(uint8_t status)
333{
334 msg_cdbg("Chip status register: Block Protect Write Disable (BPL) is %sset\n",
335 (status & (1 << 7)) ? "" : "not ");
336}
337
338/* Common lowest 2 bits: WEL and WIP. */
339static void spi_prettyprint_status_register_welwip(uint8_t status)
340{
341 msg_cdbg("Chip status register: Write Enable Latch (WEL) is %sset\n",
342 (status & (1 << 1)) ? "" : "not ");
343 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is %sset\n",
344 (status & (1 << 0)) ? "" : "not ");
345}
346
347/* Common block protection (BP) bits. */
348static void spi_prettyprint_status_register_bp(uint8_t status, int bp)
349{
350 switch (bp) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000351 case 4:
352 msg_cdbg("Chip status register: Block Protect 4 (BP4) is %sset\n",
Stefan Tauner5c316f92015-02-08 21:57:52 +0000353 (status & (1 << 6)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000354 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000355 case 3:
356 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
357 (status & (1 << 5)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000358 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000359 case 2:
360 msg_cdbg("Chip status register: Block Protect 2 (BP2) is %sset\n",
361 (status & (1 << 4)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000362 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000363 case 1:
364 msg_cdbg("Chip status register: Block Protect 1 (BP1) is %sset\n",
365 (status & (1 << 3)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000366 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000367 case 0:
368 msg_cdbg("Chip status register: Block Protect 0 (BP0) is %sset\n",
369 (status & (1 << 2)) ? "" : "not ");
370 }
371}
372
373/* Unnamed bits. */
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000374void spi_prettyprint_status_register_bit(uint8_t status, int bit)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000375{
376 msg_cdbg("Chip status register: Bit %i is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
377}
378
379int spi_prettyprint_status_register_plain(struct flashctx *flash)
380{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100381 uint8_t status;
382 int ret = spi_read_register(flash, STATUS1, &status);
383 if (ret)
384 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000385 spi_prettyprint_status_register_hex(status);
386 return 0;
387}
388
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000389/* Print the plain hex value and the welwip bits only. */
390int spi_prettyprint_status_register_default_welwip(struct flashctx *flash)
391{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100392 uint8_t status;
393 int ret = spi_read_register(flash, STATUS1, &status);
394 if (ret)
395 return ret;
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000396 spi_prettyprint_status_register_hex(status);
397
398 spi_prettyprint_status_register_welwip(status);
399 return 0;
400}
401
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000402/* Works for many chips of the
403 * AMIC A25L series
404 * and MX MX25L512
405 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000406int spi_prettyprint_status_register_bp1_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000407{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100408 uint8_t status;
409 int ret = spi_read_register(flash, STATUS1, &status);
410 if (ret)
411 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000412 spi_prettyprint_status_register_hex(status);
413
414 spi_prettyprint_status_register_srwd(status);
415 spi_prettyprint_status_register_bit(status, 6);
416 spi_prettyprint_status_register_bit(status, 5);
417 spi_prettyprint_status_register_bit(status, 4);
418 spi_prettyprint_status_register_bp(status, 1);
419 spi_prettyprint_status_register_welwip(status);
420 return 0;
421}
422
423/* Works for many chips of the
424 * AMIC A25L series
Stefan Taunerf4451612013-04-19 01:59:15 +0000425 * PMC Pm25LD series
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000426 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000427int spi_prettyprint_status_register_bp2_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000428{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100429 uint8_t status;
430 int ret = spi_read_register(flash, STATUS1, &status);
431 if (ret)
432 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000433 spi_prettyprint_status_register_hex(status);
434
435 spi_prettyprint_status_register_srwd(status);
436 spi_prettyprint_status_register_bit(status, 6);
437 spi_prettyprint_status_register_bit(status, 5);
438 spi_prettyprint_status_register_bp(status, 2);
439 spi_prettyprint_status_register_welwip(status);
440 return 0;
441}
442
443/* Works for many chips of the
444 * ST M25P series
445 * MX MX25L series
446 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000447int spi_prettyprint_status_register_bp3_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000448{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100449 uint8_t status;
450 int ret = spi_read_register(flash, STATUS1, &status);
451 if (ret)
452 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000453 spi_prettyprint_status_register_hex(status);
454
455 spi_prettyprint_status_register_srwd(status);
456 spi_prettyprint_status_register_bit(status, 6);
457 spi_prettyprint_status_register_bp(status, 3);
458 spi_prettyprint_status_register_welwip(status);
459 return 0;
460}
461
Stefan Tauner12f3d512014-05-27 21:27:27 +0000462int spi_prettyprint_status_register_bp4_srwd(struct flashctx *flash)
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000463{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100464 uint8_t status;
465 int ret = spi_read_register(flash, STATUS1, &status);
466 if (ret)
467 return ret;
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000468 spi_prettyprint_status_register_hex(status);
469
470 spi_prettyprint_status_register_srwd(status);
471 spi_prettyprint_status_register_bp(status, 4);
472 spi_prettyprint_status_register_welwip(status);
473 return 0;
474}
475
Stefan Tauner85f09f72014-05-27 21:27:14 +0000476int spi_prettyprint_status_register_bp2_bpl(struct flashctx *flash)
477{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100478 uint8_t status;
479 int ret = spi_read_register(flash, STATUS1, &status);
480 if (ret)
481 return ret;
Stefan Tauner85f09f72014-05-27 21:27:14 +0000482 spi_prettyprint_status_register_hex(status);
483
484 spi_prettyprint_status_register_bpl(status);
485 spi_prettyprint_status_register_bit(status, 6);
486 spi_prettyprint_status_register_bit(status, 5);
487 spi_prettyprint_status_register_bp(status, 2);
488 spi_prettyprint_status_register_welwip(status);
489 return 0;
490}
491
Ben Gardnerbcf61092015-11-22 02:23:31 +0000492int spi_prettyprint_status_register_bp2_tb_bpl(struct flashctx *flash)
493{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100494 uint8_t status;
495 int ret = spi_read_register(flash, STATUS1, &status);
496 if (ret)
497 return ret;
Ben Gardnerbcf61092015-11-22 02:23:31 +0000498 spi_prettyprint_status_register_hex(status);
499
500 spi_prettyprint_status_register_bpl(status);
501 spi_prettyprint_status_register_bit(status, 6);
502 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
503 spi_prettyprint_status_register_bp(status, 2);
504 spi_prettyprint_status_register_welwip(status);
505 return 0;
506}
507
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000508/* === Amic ===
509 * FIXME: spi_disable_blockprotect is incorrect but works fine for chips using
Stefan Tauner12f3d512014-05-27 21:27:27 +0000510 * spi_prettyprint_status_register_bp1_srwd or
511 * spi_prettyprint_status_register_bp2_srwd.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000512 * FIXME: spi_disable_blockprotect is incorrect and will fail for chips using
513 * spi_prettyprint_status_register_amic_a25l032 if those have locks controlled
514 * by the second status register.
515 */
516
517int spi_prettyprint_status_register_amic_a25l032(struct flashctx *flash)
518{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100519 uint8_t status;
520 int ret = spi_read_register(flash, STATUS1, &status);
521 if (ret)
522 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000523 spi_prettyprint_status_register_hex(status);
524
525 spi_prettyprint_status_register_srwd(status);
526 msg_cdbg("Chip status register: Sector Protect Size (SEC) is %i KB\n", (status & (1 << 6)) ? 4 : 64);
527 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
528 spi_prettyprint_status_register_bp(status, 2);
529 spi_prettyprint_status_register_welwip(status);
530 msg_cdbg("Chip status register 2 is NOT decoded!\n");
531 return 0;
532}
533
534/* === Atmel === */
535
536static void spi_prettyprint_status_register_atmel_at25_wpen(uint8_t status)
537{
538 msg_cdbg("Chip status register: Write Protect Enable (WPEN) is %sset\n",
539 (status & (1 << 7)) ? "" : "not ");
540}
541
542static void spi_prettyprint_status_register_atmel_at25_srpl(uint8_t status)
543{
544 msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) is %sset\n",
545 (status & (1 << 7)) ? "" : "not ");
546}
547
548static void spi_prettyprint_status_register_atmel_at25_epewpp(uint8_t status)
549{
550 msg_cdbg("Chip status register: Erase/Program Error (EPE) is %sset\n",
551 (status & (1 << 5)) ? "" : "not ");
552 msg_cdbg("Chip status register: WP# pin (WPP) is %sasserted\n",
553 (status & (1 << 4)) ? "not " : "");
554}
555
556static void spi_prettyprint_status_register_atmel_at25_swp(uint8_t status)
557{
558 msg_cdbg("Chip status register: Software Protection Status (SWP): ");
559 switch (status & (3 << 2)) {
560 case 0x0 << 2:
561 msg_cdbg("no sectors are protected\n");
562 break;
563 case 0x1 << 2:
564 msg_cdbg("some sectors are protected\n");
565 /* FIXME: Read individual Sector Protection Registers. */
566 break;
567 case 0x3 << 2:
568 msg_cdbg("all sectors are protected\n");
569 break;
570 default:
571 msg_cdbg("reserved for future use\n");
572 break;
573 }
574}
575
576int spi_prettyprint_status_register_at25df(struct flashctx *flash)
577{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100578 uint8_t status;
579 int ret = spi_read_register(flash, STATUS1, &status);
580 if (ret)
581 return ret;
582
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000583 spi_prettyprint_status_register_hex(status);
584
585 spi_prettyprint_status_register_atmel_at25_srpl(status);
586 spi_prettyprint_status_register_bit(status, 6);
587 spi_prettyprint_status_register_atmel_at25_epewpp(status);
588 spi_prettyprint_status_register_atmel_at25_swp(status);
589 spi_prettyprint_status_register_welwip(status);
590 return 0;
591}
592
593int spi_prettyprint_status_register_at25df_sec(struct flashctx *flash)
594{
595 /* FIXME: We should check the security lockdown. */
596 msg_cdbg("Ignoring security lockdown (if present)\n");
597 msg_cdbg("Ignoring status register byte 2\n");
598 return spi_prettyprint_status_register_at25df(flash);
599}
600
Stefan Tauner57794ac2012-12-29 15:04:20 +0000601/* used for AT25F512, AT25F1024(A), AT25F2048 */
602int spi_prettyprint_status_register_at25f(struct flashctx *flash)
603{
604 uint8_t status;
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100605 int ret = spi_read_register(flash, STATUS1, &status);
606 if (ret)
607 return ret;
Stefan Tauner57794ac2012-12-29 15:04:20 +0000608
Stefan Tauner57794ac2012-12-29 15:04:20 +0000609 spi_prettyprint_status_register_hex(status);
610
611 spi_prettyprint_status_register_atmel_at25_wpen(status);
612 spi_prettyprint_status_register_bit(status, 6);
613 spi_prettyprint_status_register_bit(status, 5);
614 spi_prettyprint_status_register_bit(status, 4);
615 spi_prettyprint_status_register_bp(status, 1);
616 spi_prettyprint_status_register_welwip(status);
617 return 0;
618}
619
620int spi_prettyprint_status_register_at25f512a(struct flashctx *flash)
621{
622 uint8_t status;
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100623 int ret = spi_read_register(flash, STATUS1, &status);
624 if (ret)
625 return ret;
Stefan Tauner57794ac2012-12-29 15:04:20 +0000626
Stefan Tauner57794ac2012-12-29 15:04:20 +0000627 spi_prettyprint_status_register_hex(status);
628
629 spi_prettyprint_status_register_atmel_at25_wpen(status);
630 spi_prettyprint_status_register_bit(status, 6);
631 spi_prettyprint_status_register_bit(status, 5);
632 spi_prettyprint_status_register_bit(status, 4);
633 spi_prettyprint_status_register_bit(status, 3);
634 spi_prettyprint_status_register_bp(status, 0);
635 spi_prettyprint_status_register_welwip(status);
636 return 0;
637}
638
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000639int spi_prettyprint_status_register_at25f512b(struct flashctx *flash)
640{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100641 uint8_t status;
642 int ret = spi_read_register(flash, STATUS1, &status);
643 if (ret)
644 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000645 spi_prettyprint_status_register_hex(status);
646
647 spi_prettyprint_status_register_atmel_at25_srpl(status);
648 spi_prettyprint_status_register_bit(status, 6);
649 spi_prettyprint_status_register_atmel_at25_epewpp(status);
650 spi_prettyprint_status_register_bit(status, 3);
651 spi_prettyprint_status_register_bp(status, 0);
652 spi_prettyprint_status_register_welwip(status);
653 return 0;
654}
655
Stefan Tauner57794ac2012-12-29 15:04:20 +0000656int spi_prettyprint_status_register_at25f4096(struct flashctx *flash)
657{
658 uint8_t status;
659
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100660 int ret = spi_read_register(flash, STATUS1, &status);
661 if (ret)
662 return ret;
663
Stefan Tauner57794ac2012-12-29 15:04:20 +0000664 spi_prettyprint_status_register_hex(status);
665
666 spi_prettyprint_status_register_atmel_at25_wpen(status);
667 spi_prettyprint_status_register_bit(status, 6);
668 spi_prettyprint_status_register_bit(status, 5);
669 spi_prettyprint_status_register_bp(status, 2);
670 spi_prettyprint_status_register_welwip(status);
671 return 0;
672}
673
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000674int spi_prettyprint_status_register_at25fs010(struct flashctx *flash)
675{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100676 uint8_t status;
677 int ret = spi_read_register(flash, STATUS1, &status);
678 if (ret)
679 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000680 spi_prettyprint_status_register_hex(status);
681
682 spi_prettyprint_status_register_atmel_at25_wpen(status);
683 msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is "
684 "%sset\n", (status & (1 << 6)) ? "" : "not ");
685 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
686 "%sset\n", (status & (1 << 5)) ? "" : "not ");
687 spi_prettyprint_status_register_bit(status, 4);
688 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
689 "%sset\n", (status & (1 << 3)) ? "" : "not ");
690 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
691 "%sset\n", (status & (1 << 2)) ? "" : "not ");
692 /* FIXME: Pretty-print detailed sector protection status. */
693 spi_prettyprint_status_register_welwip(status);
694 return 0;
695}
696
697int spi_prettyprint_status_register_at25fs040(struct flashctx *flash)
698{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100699 uint8_t status;
700 int ret = spi_read_register(flash, STATUS1, &status);
701 if (ret)
702 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000703 spi_prettyprint_status_register_hex(status);
704
705 spi_prettyprint_status_register_atmel_at25_wpen(status);
706 spi_prettyprint_status_register_bp(status, 4);
707 /* FIXME: Pretty-print detailed sector protection status. */
708 spi_prettyprint_status_register_welwip(status);
709 return 0;
710}
711
712int spi_prettyprint_status_register_at26df081a(struct flashctx *flash)
713{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100714 uint8_t status;
715 int ret = spi_read_register(flash, STATUS1, &status);
716 if (ret)
717 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000718 spi_prettyprint_status_register_hex(status);
719
720 spi_prettyprint_status_register_atmel_at25_srpl(status);
721 msg_cdbg("Chip status register: Sequential Program Mode Status (SPM) is %sset\n",
722 (status & (1 << 6)) ? "" : "not ");
723 spi_prettyprint_status_register_atmel_at25_epewpp(status);
724 spi_prettyprint_status_register_atmel_at25_swp(status);
725 spi_prettyprint_status_register_welwip(status);
726 return 0;
727}
728
Stefan Taunercecb2c52013-06-20 22:55:41 +0000729/* Some Atmel DataFlash chips support per sector protection bits and the write protection bits in the status
730 * register do indicate if none, some or all sectors are protected. It is possible to globally (un)lock all
731 * sectors at once by writing 0 not only the protection bits (2 and 3) but also completely unrelated bits (4 and
732 * 5) which normally are not touched.
733 * Affected are all known Atmel chips matched by AT2[56]D[FLQ]..1A? but the AT26DF041. */
734int spi_disable_blockprotect_at2x_global_unprotect(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000735{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000736 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 1 << 4, 0x00);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000737}
738
Stefan Taunercecb2c52013-06-20 22:55:41 +0000739int spi_disable_blockprotect_at2x_global_unprotect_sec(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000740{
741 /* FIXME: We should check the security lockdown. */
742 msg_cinfo("Ignoring security lockdown (if present)\n");
Stefan Taunercecb2c52013-06-20 22:55:41 +0000743 return spi_disable_blockprotect_at2x_global_unprotect(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000744}
745
Stefan Tauner57794ac2012-12-29 15:04:20 +0000746int spi_disable_blockprotect_at25f(struct flashctx *flash)
747{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000748 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000749}
750
751int spi_disable_blockprotect_at25f512a(struct flashctx *flash)
752{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000753 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000754}
755
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000756int spi_disable_blockprotect_at25f512b(struct flashctx *flash)
757{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000758 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 1 << 4, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000759}
760
761int spi_disable_blockprotect_at25fs010(struct flashctx *flash)
762{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000763 return spi_disable_blockprotect_generic(flash, 0x6C, 1 << 7, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000764 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000765
766int spi_disable_blockprotect_at25fs040(struct flashctx *flash)
767{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000768 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000769}
770
Nikolay Nikolaevd0e3ea12013-06-28 21:29:08 +0000771/* === Eon === */
772
773int spi_prettyprint_status_register_en25s_wp(struct flashctx *flash)
774{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100775 uint8_t status;
776 int ret = spi_read_register(flash, STATUS1, &status);
777 if (ret)
778 return ret;
Nikolay Nikolaevd0e3ea12013-06-28 21:29:08 +0000779 spi_prettyprint_status_register_hex(status);
780
781 spi_prettyprint_status_register_srwd(status);
782 msg_cdbg("Chip status register: WP# disable (WPDIS) is %sabled\n", (status & (1 << 6)) ? "en " : "dis");
783 spi_prettyprint_status_register_bp(status, 3);
784 spi_prettyprint_status_register_welwip(status);
785 return 0;
786}
787
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000788/* === Intel/Numonyx/Micron - Spansion === */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000789
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000790int spi_disable_blockprotect_n25q(struct flashctx *flash)
791{
792 return spi_disable_blockprotect_generic(flash, 0x5C, 1 << 7, 0, 0xFF);
793}
794
795int spi_prettyprint_status_register_n25q(struct flashctx *flash)
796{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100797 uint8_t status;
798 int ret = spi_read_register(flash, STATUS1, &status);
799 if (ret)
800 return ret;
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000801 spi_prettyprint_status_register_hex(status);
802
803 spi_prettyprint_status_register_srwd(status);
804 if (flash->chip->total_size <= 32 / 8 * 1024) /* N25Q16 and N25Q32: reserved */
805 spi_prettyprint_status_register_bit(status, 6);
806 else
807 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
808 (status & (1 << 6)) ? "" : "not ");
809 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
810 spi_prettyprint_status_register_bp(status, 2);
811 spi_prettyprint_status_register_welwip(status);
812 return 0;
813}
814
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000815/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000816/* TODO: Clear P_FAIL and E_FAIL with Clear SR Fail Flags Command (30h) here? */
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000817int spi_disable_blockprotect_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000818{
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000819 return spi_disable_blockprotect_bp2_srwd(flash);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000820}
821
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000822/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
823int spi_prettyprint_status_register_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000824{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100825 uint8_t status;
826 int ret = spi_read_register(flash, STATUS1, &status);
827 if (ret)
828 return ret;
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000829 spi_prettyprint_status_register_hex(status);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000830
831 spi_prettyprint_status_register_srwd(status);
832 msg_cdbg("Chip status register: Program Fail Flag (P_FAIL) is %sset\n",
833 (status & (1 << 6)) ? "" : "not ");
834 msg_cdbg("Chip status register: Erase Fail Flag (E_FAIL) is %sset\n",
835 (status & (1 << 5)) ? "" : "not ");
836 spi_prettyprint_status_register_bp(status, 2);
837 spi_prettyprint_status_register_welwip(status);
838 return 0;
839}
840
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000841/* === SST === */
842
843static void spi_prettyprint_status_register_sst25_common(uint8_t status)
844{
845 spi_prettyprint_status_register_hex(status);
846
847 spi_prettyprint_status_register_bpl(status);
848 msg_cdbg("Chip status register: Auto Address Increment Programming (AAI) is %sset\n",
849 (status & (1 << 6)) ? "" : "not ");
850 spi_prettyprint_status_register_bp(status, 3);
851 spi_prettyprint_status_register_welwip(status);
852}
853
854int spi_prettyprint_status_register_sst25(struct flashctx *flash)
855{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100856 uint8_t status;
857 int ret = spi_read_register(flash, STATUS1, &status);
858 if (ret)
859 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000860 spi_prettyprint_status_register_sst25_common(status);
861 return 0;
862}
863
864int spi_prettyprint_status_register_sst25vf016(struct flashctx *flash)
865{
866 static const char *const bpt[] = {
867 "none",
868 "1F0000H-1FFFFFH",
869 "1E0000H-1FFFFFH",
870 "1C0000H-1FFFFFH",
871 "180000H-1FFFFFH",
872 "100000H-1FFFFFH",
873 "all", "all"
874 };
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100875 uint8_t status;
876 int ret = spi_read_register(flash, STATUS1, &status);
877 if (ret)
878 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000879 spi_prettyprint_status_register_sst25_common(status);
880 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
881 return 0;
882}
883
884int spi_prettyprint_status_register_sst25vf040b(struct flashctx *flash)
885{
886 static const char *const bpt[] = {
887 "none",
888 "0x70000-0x7ffff",
889 "0x60000-0x7ffff",
890 "0x40000-0x7ffff",
891 "all blocks", "all blocks", "all blocks", "all blocks"
892 };
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100893 uint8_t status;
894 int ret = spi_read_register(flash, STATUS1, &status);
895 if (ret)
896 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000897 spi_prettyprint_status_register_sst25_common(status);
898 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
899 return 0;
900}