blob: 31d6c76986ae239dfd2b02ed0f654a5505a0d34e [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 +110025static int spi_write_register_flag(const struct flashctx *flash, uint8_t enable_opcode, uint8_t *write_cmd, size_t write_cmd_len, enum flash_reg reg)
Stefan Tauner6ee37e22012-12-29 15:03:51 +000026{
Stefan Tauner6ee37e22012-12-29 15:03:51 +000027 /*
Nikolai Artemiev01675222021-10-20 22:30:41 +110028 * Enabling register writes requires either EWSR or WREN depending on
29 * chip type. The code below relies on the fact hat EWSR and WREN have
30 * the same INSIZE and OUTSIZE.
Stefan Tauner6ee37e22012-12-29 15:03:51 +000031 */
Nikolai Artemiev01675222021-10-20 22:30:41 +110032
Stefan Tauner6ee37e22012-12-29 15:03:51 +000033 struct spi_command cmds[] = {
34 {
35 .writecnt = JEDEC_WREN_OUTSIZE,
Nikolai Artemiev01675222021-10-20 22:30:41 +110036 .writearr = &enable_opcode,
Stefan Tauner6ee37e22012-12-29 15:03:51 +000037 .readcnt = 0,
38 .readarr = NULL,
39 }, {
Nikolai Artemiev01675222021-10-20 22:30:41 +110040 .writecnt = write_cmd_len,
41 .writearr = write_cmd,
Stefan Tauner6ee37e22012-12-29 15:03:51 +000042 .readcnt = 0,
43 .readarr = NULL,
44 }, {
45 .writecnt = 0,
46 .writearr = NULL,
47 .readcnt = 0,
48 .readarr = NULL,
49 }};
50
Nikolai Artemiev01675222021-10-20 22:30:41 +110051 int result = spi_send_multicommand(flash, cmds);
Stefan Tauner6ee37e22012-12-29 15:03:51 +000052 if (result) {
53 msg_cerr("%s failed during command execution\n", __func__);
Nikolai Artemiev01675222021-10-20 22:30:41 +110054 /*
55 * No point in waiting for the command to complete if execution
Stefan Tauner6ee37e22012-12-29 15:03:51 +000056 * failed.
57 */
58 return result;
59 }
Nikolai Artemiev01675222021-10-20 22:30:41 +110060
61 /*
62 * WRSR performs a self-timed erase before the changes take effect.
Stefan Tauner6ee37e22012-12-29 15:03:51 +000063 * This may take 50-85 ms in most cases, and some chips apparently
64 * allow running RDSR only once. Therefore pick an initial delay of
65 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
Nikolai Artemiev01675222021-10-20 22:30:41 +110066 *
67 * Newer chips with multiple status registers (SR2 etc.) are unlikely
68 * to have problems with multiple RDSR commands, so only wait for the
69 * initial 100 ms if the register we wrote to was SR1.
Stefan Tauner6ee37e22012-12-29 15:03:51 +000070 */
Nikolai Artemiev01675222021-10-20 22:30:41 +110071 int delay_ms = 5000;
72 if (reg == STATUS1) {
73 programmer_delay(100 * 1000);
74 delay_ms -= 100;
75 }
76
77 for (; delay_ms > 0; delay_ms -= 10) {
78 if ((spi_read_status_register(flash) & SPI_SR_WIP) == 0)
79 return 0;
Stefan Tauner6ee37e22012-12-29 15:03:51 +000080 programmer_delay(10 * 1000);
81 }
Nikolai Artemiev01675222021-10-20 22:30:41 +110082
83 msg_cerr("Error: WIP bit after WRSR never cleared\n");
84 return TIMEOUT_ERROR;
Stefan Tauner6ee37e22012-12-29 15:03:51 +000085}
86
Nikolai Artemiev01675222021-10-20 22:30:41 +110087int spi_write_register(const struct flashctx *flash, enum flash_reg reg, uint8_t value)
Stefan Tauner6ee37e22012-12-29 15:03:51 +000088{
89 int feature_bits = flash->chip->feature_bits;
Nikolai Artemiev01675222021-10-20 22:30:41 +110090
91 uint8_t write_cmd[3];
92 size_t write_cmd_len = 0;
93
94 /*
95 * Create SPI write command sequence based on the destination register
96 * and the chip's supported command set.
97 */
98 switch (reg) {
99 case STATUS1:
100 write_cmd[0] = JEDEC_WRSR;
101 write_cmd[1] = value;
102 write_cmd_len = JEDEC_WRSR_OUTSIZE;
103 break;
Nikolai Artemiev9de3f872021-10-20 22:32:25 +1100104 case STATUS2:
105 if (feature_bits & FEATURE_WRSR2) {
106 write_cmd[0] = JEDEC_WRSR2;
107 write_cmd[1] = value;
108 write_cmd_len = JEDEC_WRSR2_OUTSIZE;
109 break;
110 }
111 if (feature_bits & FEATURE_WRSR_EXT) {
112 /*
113 * Writing SR2 with an extended WRSR command requires
114 * writing SR1 along with SR2, so just read SR1 and
115 * write it back
116 */
117 uint8_t sr1;
118
119 if (spi_read_register(flash, STATUS1, &sr1)) {
120 msg_cerr("Writing SR2 failed: failed to read SR1 for writeback.\n");
121 return 1;
122 }
123 write_cmd[0] = JEDEC_WRSR;
124 write_cmd[1] = sr1;
125 write_cmd[2] = value;
126 write_cmd_len = JEDEC_WRSR_EXT_OUTSIZE;
127 break;
128 }
129 msg_cerr("Cannot write SR2: unsupported by chip\n");
130 return 1;
Nikolai Artemiev01675222021-10-20 22:30:41 +1100131 default:
132 msg_cerr("Cannot write register: unknown register\n");
133 return 1;
134 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000135
136 if (!(feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
137 msg_cdbg("Missing status register write definition, assuming "
138 "EWSR is needed\n");
139 feature_bits |= FEATURE_WRSR_EWSR;
140 }
Nikolai Artemiev01675222021-10-20 22:30:41 +1100141
142 int ret = 1;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000143 if (feature_bits & FEATURE_WRSR_WREN)
Nikolai Artemiev01675222021-10-20 22:30:41 +1100144 ret = spi_write_register_flag(flash, JEDEC_WREN, write_cmd, write_cmd_len, reg);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000145 if (ret && (feature_bits & FEATURE_WRSR_EWSR))
Nikolai Artemiev01675222021-10-20 22:30:41 +1100146 ret = spi_write_register_flag(flash, JEDEC_EWSR, write_cmd, write_cmd_len, reg);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000147 return ret;
148}
149
Nikolai Artemiev01675222021-10-20 22:30:41 +1100150int spi_read_register(const struct flashctx *flash, enum flash_reg reg, uint8_t *value)
151{
Nikolai Artemiev9de3f872021-10-20 22:32:25 +1100152 int feature_bits = flash->chip->feature_bits;
Nikolai Artemiev01675222021-10-20 22:30:41 +1100153 uint8_t read_cmd;
154
155 switch (reg) {
156 case STATUS1:
157 read_cmd = JEDEC_RDSR;
158 break;
Nikolai Artemiev9de3f872021-10-20 22:32:25 +1100159 case STATUS2:
160 if (feature_bits & (FEATURE_WRSR_EXT | FEATURE_WRSR2)) {
161 read_cmd = JEDEC_RDSR2;
162 break;
163 }
164 msg_cerr("Cannot read SR2: unsupported by chip\n");
165 return 1;
Nikolai Artemiev01675222021-10-20 22:30:41 +1100166 default:
167 msg_cerr("Cannot read register: unknown register\n");
168 return 1;
169 }
170
171 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
172 /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
173 uint8_t readarr[2];
174
175 int ret = spi_send_command(flash, sizeof(read_cmd), sizeof(readarr), &read_cmd, readarr);
176 if (ret) {
177 msg_cerr("Register read failed!\n");
178 return ret;
179 }
180
181 *value = readarr[0];
182 return 0;
183}
184
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000185uint8_t spi_read_status_register(const struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000186{
Nikolai Artemiev01675222021-10-20 22:30:41 +1100187 uint8_t status = 0;
188 /* FIXME: We should propagate the error. */
189 spi_read_register(flash, STATUS1, &status);
190 return status;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000191}
192
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100193static int spi_restore_status(struct flashctx *flash, uint8_t status)
194{
195 msg_cdbg("restoring chip status (0x%02x)\n", status);
Nikolai Artemiev01675222021-10-20 22:30:41 +1100196 return spi_write_register(flash, STATUS1, status);
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100197}
198
Stefan Tauner9530a022012-12-29 15:04:05 +0000199/* A generic block protection disable.
200 * Tests if a protection is enabled with the block protection mask (bp_mask) and returns success otherwise.
201 * Tests if the register bits are locked with the lock_mask (lock_mask).
Stefan Taunercecb2c52013-06-20 22:55:41 +0000202 * Tests if a hardware protection is active (i.e. low pin/high bit value) with the write protection mask
203 * (wp_mask) and bails out in that case.
204 * If there are register lock bits set we try to disable them by unsetting those bits of the previous register
205 * contents that are set in the lock_mask. We then check if removing the lock bits has worked and continue as if
206 * they never had been engaged:
207 * If the lock bits are out of the way try to disable engaged protections.
208 * To support uncommon global unprotects (e.g. on most AT2[56]xx1(A)) unprotect_mask can be used to force
209 * bits to 0 additionally to those set in bp_mask and lock_mask. Only bits set in unprotect_mask are potentially
210 * preserved when doing the final unprotect.
211 *
212 * To sum up:
213 * bp_mask: set those bits that correspond to the bits in the status register that indicate an active protection
214 * (which should be unset after this function returns).
215 * lock_mask: set the bits that correspond to the bits that lock changing the bits above.
216 * wp_mask: set the bits that correspond to bits indicating non-software revocable protections.
217 * unprotect_mask: set the bits that should be preserved if possible when unprotecting.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000218 */
Stefan Taunercecb2c52013-06-20 22:55:41 +0000219static 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 +0000220{
221 uint8_t status;
222 int result;
223
224 status = spi_read_status_register(flash);
Stefan Tauner9530a022012-12-29 15:04:05 +0000225 if ((status & bp_mask) == 0) {
226 msg_cdbg2("Block protection is disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000227 return 0;
Stefan Tauner9530a022012-12-29 15:04:05 +0000228 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000229
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100230 /* Restore status register content upon exit in finalize_flash_access(). */
231 register_chip_restore(spi_restore_status, flash, status);
232
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000233 msg_cdbg("Some block protection in effect, disabling... ");
Stefan Tauner9530a022012-12-29 15:04:05 +0000234 if ((status & lock_mask) != 0) {
235 msg_cdbg("\n\tNeed to disable the register lock first... ");
236 if (wp_mask != 0 && (status & wp_mask) == 0) {
237 msg_cerr("Hardware protection is active, disabling write protection is impossible.\n");
238 return 1;
239 }
240 /* All bits except the register lock bit (often called SPRL, SRWD, WPEN) are readonly. */
Nikolai Artemiev01675222021-10-20 22:30:41 +1100241 result = spi_write_register(flash, STATUS1, status & ~lock_mask);
Stefan Tauner9530a022012-12-29 15:04:05 +0000242 if (result) {
Nikolai Artemiev01675222021-10-20 22:30:41 +1100243 msg_cerr("Could not write status register 1.\n");
Stefan Tauner9530a022012-12-29 15:04:05 +0000244 return result;
245 }
Stefan Taunercecb2c52013-06-20 22:55:41 +0000246 status = spi_read_status_register(flash);
247 if ((status & lock_mask) != 0) {
248 msg_cerr("Unsetting lock bit(s) failed.\n");
249 return 1;
250 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000251 msg_cdbg("done.\n");
252 }
253 /* Global unprotect. Make sure to mask the register lock bit as well. */
Nikolai Artemiev01675222021-10-20 22:30:41 +1100254 result = spi_write_register(flash, STATUS1, status & ~(bp_mask | lock_mask) & unprotect_mask);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000255 if (result) {
Nikolai Artemiev01675222021-10-20 22:30:41 +1100256 msg_cerr("Could not write status register 1.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000257 return result;
258 }
259 status = spi_read_status_register(flash);
Stefan Tauner9530a022012-12-29 15:04:05 +0000260 if ((status & bp_mask) != 0) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000261 msg_cerr("Block protection could not be disabled!\n");
Yuji Sasaki4af36092019-03-22 10:59:50 -0700262 if (flash->chip->printlock)
263 flash->chip->printlock(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000264 return 1;
265 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000266 msg_cdbg("disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000267 return 0;
268}
269
Stefan Tauner9530a022012-12-29 15:04:05 +0000270/* A common block protection disable that tries to unset the status register bits masked by 0x3C. */
271int spi_disable_blockprotect(struct flashctx *flash)
272{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000273 return spi_disable_blockprotect_generic(flash, 0x3C, 0, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000274}
275
Wei Hu25584de2018-04-30 14:02:08 -0700276int spi_disable_blockprotect_sst26_global_unprotect(struct flashctx *flash)
277{
278 int result = spi_write_enable(flash);
279 if (result)
280 return result;
281
282 static const unsigned char cmd[] = { 0x98 }; /* ULBPR */
283 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
284 if (result)
285 msg_cerr("ULBPR failed\n");
286 return result;
287}
288
Stefan Taunera60d4082014-06-04 16:17:03 +0000289/* A common block protection disable that tries to unset the status register bits masked by 0x0C (BP0-1) and
290 * protected/locked by bit #7. Useful when bits 4-5 may be non-0). */
291int spi_disable_blockprotect_bp1_srwd(struct flashctx *flash)
292{
293 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
294}
295
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000296/* A common block protection disable that tries to unset the status register bits masked by 0x1C (BP0-2) and
297 * protected/locked by bit #7. Useful when bit #5 is neither a protection bit nor reserved (and hence possibly
298 * non-0). */
299int spi_disable_blockprotect_bp2_srwd(struct flashctx *flash)
300{
301 return spi_disable_blockprotect_generic(flash, 0x1C, 1 << 7, 0, 0xFF);
302}
303
304/* A common block protection disable that tries to unset the status register bits masked by 0x3C (BP0-3) and
305 * protected/locked by bit #7. */
306int spi_disable_blockprotect_bp3_srwd(struct flashctx *flash)
307{
308 return spi_disable_blockprotect_generic(flash, 0x3C, 1 << 7, 0, 0xFF);
309}
310
311/* A common block protection disable that tries to unset the status register bits masked by 0x7C (BP0-4) and
312 * protected/locked by bit #7. */
313int spi_disable_blockprotect_bp4_srwd(struct flashctx *flash)
314{
315 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
316}
Stefan Tauner9530a022012-12-29 15:04:05 +0000317
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000318static void spi_prettyprint_status_register_hex(uint8_t status)
319{
320 msg_cdbg("Chip status register is 0x%02x.\n", status);
321}
322
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000323/* Common highest bit: Status Register Write Disable (SRWD) or Status Register Protect (SRP). */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000324static void spi_prettyprint_status_register_srwd(uint8_t status)
325{
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000326 msg_cdbg("Chip status register: Status Register Write Disable (SRWD, SRP, ...) is %sset\n",
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000327 (status & (1 << 7)) ? "" : "not ");
328}
329
330/* Common highest bit: Block Protect Write Disable (BPL). */
331static void spi_prettyprint_status_register_bpl(uint8_t status)
332{
333 msg_cdbg("Chip status register: Block Protect Write Disable (BPL) is %sset\n",
334 (status & (1 << 7)) ? "" : "not ");
335}
336
337/* Common lowest 2 bits: WEL and WIP. */
338static void spi_prettyprint_status_register_welwip(uint8_t status)
339{
340 msg_cdbg("Chip status register: Write Enable Latch (WEL) is %sset\n",
341 (status & (1 << 1)) ? "" : "not ");
342 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is %sset\n",
343 (status & (1 << 0)) ? "" : "not ");
344}
345
346/* Common block protection (BP) bits. */
347static void spi_prettyprint_status_register_bp(uint8_t status, int bp)
348{
349 switch (bp) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000350 case 4:
351 msg_cdbg("Chip status register: Block Protect 4 (BP4) is %sset\n",
Stefan Tauner5c316f92015-02-08 21:57:52 +0000352 (status & (1 << 6)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000353 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000354 case 3:
355 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
356 (status & (1 << 5)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000357 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000358 case 2:
359 msg_cdbg("Chip status register: Block Protect 2 (BP2) is %sset\n",
360 (status & (1 << 4)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000361 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000362 case 1:
363 msg_cdbg("Chip status register: Block Protect 1 (BP1) is %sset\n",
364 (status & (1 << 3)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000365 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000366 case 0:
367 msg_cdbg("Chip status register: Block Protect 0 (BP0) is %sset\n",
368 (status & (1 << 2)) ? "" : "not ");
369 }
370}
371
372/* Unnamed bits. */
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000373void spi_prettyprint_status_register_bit(uint8_t status, int bit)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000374{
375 msg_cdbg("Chip status register: Bit %i is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
376}
377
378int spi_prettyprint_status_register_plain(struct flashctx *flash)
379{
380 uint8_t status = spi_read_status_register(flash);
381 spi_prettyprint_status_register_hex(status);
382 return 0;
383}
384
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000385/* Print the plain hex value and the welwip bits only. */
386int spi_prettyprint_status_register_default_welwip(struct flashctx *flash)
387{
388 uint8_t status = spi_read_status_register(flash);
389 spi_prettyprint_status_register_hex(status);
390
391 spi_prettyprint_status_register_welwip(status);
392 return 0;
393}
394
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000395/* Works for many chips of the
396 * AMIC A25L series
397 * and MX MX25L512
398 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000399int spi_prettyprint_status_register_bp1_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000400{
401 uint8_t status = spi_read_status_register(flash);
402 spi_prettyprint_status_register_hex(status);
403
404 spi_prettyprint_status_register_srwd(status);
405 spi_prettyprint_status_register_bit(status, 6);
406 spi_prettyprint_status_register_bit(status, 5);
407 spi_prettyprint_status_register_bit(status, 4);
408 spi_prettyprint_status_register_bp(status, 1);
409 spi_prettyprint_status_register_welwip(status);
410 return 0;
411}
412
413/* Works for many chips of the
414 * AMIC A25L series
Stefan Taunerf4451612013-04-19 01:59:15 +0000415 * PMC Pm25LD series
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000416 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000417int spi_prettyprint_status_register_bp2_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000418{
419 uint8_t status = spi_read_status_register(flash);
420 spi_prettyprint_status_register_hex(status);
421
422 spi_prettyprint_status_register_srwd(status);
423 spi_prettyprint_status_register_bit(status, 6);
424 spi_prettyprint_status_register_bit(status, 5);
425 spi_prettyprint_status_register_bp(status, 2);
426 spi_prettyprint_status_register_welwip(status);
427 return 0;
428}
429
430/* Works for many chips of the
431 * ST M25P series
432 * MX MX25L series
433 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000434int spi_prettyprint_status_register_bp3_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000435{
436 uint8_t status = spi_read_status_register(flash);
437 spi_prettyprint_status_register_hex(status);
438
439 spi_prettyprint_status_register_srwd(status);
440 spi_prettyprint_status_register_bit(status, 6);
441 spi_prettyprint_status_register_bp(status, 3);
442 spi_prettyprint_status_register_welwip(status);
443 return 0;
444}
445
Stefan Tauner12f3d512014-05-27 21:27:27 +0000446int spi_prettyprint_status_register_bp4_srwd(struct flashctx *flash)
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000447{
448 uint8_t status = spi_read_status_register(flash);
449 spi_prettyprint_status_register_hex(status);
450
451 spi_prettyprint_status_register_srwd(status);
452 spi_prettyprint_status_register_bp(status, 4);
453 spi_prettyprint_status_register_welwip(status);
454 return 0;
455}
456
Stefan Tauner85f09f72014-05-27 21:27:14 +0000457int spi_prettyprint_status_register_bp2_bpl(struct flashctx *flash)
458{
459 uint8_t status = spi_read_status_register(flash);
460 spi_prettyprint_status_register_hex(status);
461
462 spi_prettyprint_status_register_bpl(status);
463 spi_prettyprint_status_register_bit(status, 6);
464 spi_prettyprint_status_register_bit(status, 5);
465 spi_prettyprint_status_register_bp(status, 2);
466 spi_prettyprint_status_register_welwip(status);
467 return 0;
468}
469
Ben Gardnerbcf61092015-11-22 02:23:31 +0000470int spi_prettyprint_status_register_bp2_tb_bpl(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_bpl(status);
476 spi_prettyprint_status_register_bit(status, 6);
477 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
478 spi_prettyprint_status_register_bp(status, 2);
479 spi_prettyprint_status_register_welwip(status);
480 return 0;
481}
482
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000483/* === Amic ===
484 * FIXME: spi_disable_blockprotect is incorrect but works fine for chips using
Stefan Tauner12f3d512014-05-27 21:27:27 +0000485 * spi_prettyprint_status_register_bp1_srwd or
486 * spi_prettyprint_status_register_bp2_srwd.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000487 * FIXME: spi_disable_blockprotect is incorrect and will fail for chips using
488 * spi_prettyprint_status_register_amic_a25l032 if those have locks controlled
489 * by the second status register.
490 */
491
492int spi_prettyprint_status_register_amic_a25l032(struct flashctx *flash)
493{
494 uint8_t status = spi_read_status_register(flash);
495 spi_prettyprint_status_register_hex(status);
496
497 spi_prettyprint_status_register_srwd(status);
498 msg_cdbg("Chip status register: Sector Protect Size (SEC) is %i KB\n", (status & (1 << 6)) ? 4 : 64);
499 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
500 spi_prettyprint_status_register_bp(status, 2);
501 spi_prettyprint_status_register_welwip(status);
502 msg_cdbg("Chip status register 2 is NOT decoded!\n");
503 return 0;
504}
505
506/* === Atmel === */
507
508static void spi_prettyprint_status_register_atmel_at25_wpen(uint8_t status)
509{
510 msg_cdbg("Chip status register: Write Protect Enable (WPEN) is %sset\n",
511 (status & (1 << 7)) ? "" : "not ");
512}
513
514static void spi_prettyprint_status_register_atmel_at25_srpl(uint8_t status)
515{
516 msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) is %sset\n",
517 (status & (1 << 7)) ? "" : "not ");
518}
519
520static void spi_prettyprint_status_register_atmel_at25_epewpp(uint8_t status)
521{
522 msg_cdbg("Chip status register: Erase/Program Error (EPE) is %sset\n",
523 (status & (1 << 5)) ? "" : "not ");
524 msg_cdbg("Chip status register: WP# pin (WPP) is %sasserted\n",
525 (status & (1 << 4)) ? "not " : "");
526}
527
528static void spi_prettyprint_status_register_atmel_at25_swp(uint8_t status)
529{
530 msg_cdbg("Chip status register: Software Protection Status (SWP): ");
531 switch (status & (3 << 2)) {
532 case 0x0 << 2:
533 msg_cdbg("no sectors are protected\n");
534 break;
535 case 0x1 << 2:
536 msg_cdbg("some sectors are protected\n");
537 /* FIXME: Read individual Sector Protection Registers. */
538 break;
539 case 0x3 << 2:
540 msg_cdbg("all sectors are protected\n");
541 break;
542 default:
543 msg_cdbg("reserved for future use\n");
544 break;
545 }
546}
547
548int spi_prettyprint_status_register_at25df(struct flashctx *flash)
549{
550 uint8_t status = spi_read_status_register(flash);
551 spi_prettyprint_status_register_hex(status);
552
553 spi_prettyprint_status_register_atmel_at25_srpl(status);
554 spi_prettyprint_status_register_bit(status, 6);
555 spi_prettyprint_status_register_atmel_at25_epewpp(status);
556 spi_prettyprint_status_register_atmel_at25_swp(status);
557 spi_prettyprint_status_register_welwip(status);
558 return 0;
559}
560
561int spi_prettyprint_status_register_at25df_sec(struct flashctx *flash)
562{
563 /* FIXME: We should check the security lockdown. */
564 msg_cdbg("Ignoring security lockdown (if present)\n");
565 msg_cdbg("Ignoring status register byte 2\n");
566 return spi_prettyprint_status_register_at25df(flash);
567}
568
Stefan Tauner57794ac2012-12-29 15:04:20 +0000569/* used for AT25F512, AT25F1024(A), AT25F2048 */
570int spi_prettyprint_status_register_at25f(struct flashctx *flash)
571{
572 uint8_t status;
573
574 status = spi_read_status_register(flash);
575 spi_prettyprint_status_register_hex(status);
576
577 spi_prettyprint_status_register_atmel_at25_wpen(status);
578 spi_prettyprint_status_register_bit(status, 6);
579 spi_prettyprint_status_register_bit(status, 5);
580 spi_prettyprint_status_register_bit(status, 4);
581 spi_prettyprint_status_register_bp(status, 1);
582 spi_prettyprint_status_register_welwip(status);
583 return 0;
584}
585
586int spi_prettyprint_status_register_at25f512a(struct flashctx *flash)
587{
588 uint8_t status;
589
590 status = spi_read_status_register(flash);
591 spi_prettyprint_status_register_hex(status);
592
593 spi_prettyprint_status_register_atmel_at25_wpen(status);
594 spi_prettyprint_status_register_bit(status, 6);
595 spi_prettyprint_status_register_bit(status, 5);
596 spi_prettyprint_status_register_bit(status, 4);
597 spi_prettyprint_status_register_bit(status, 3);
598 spi_prettyprint_status_register_bp(status, 0);
599 spi_prettyprint_status_register_welwip(status);
600 return 0;
601}
602
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000603int spi_prettyprint_status_register_at25f512b(struct flashctx *flash)
604{
605 uint8_t status = spi_read_status_register(flash);
606 spi_prettyprint_status_register_hex(status);
607
608 spi_prettyprint_status_register_atmel_at25_srpl(status);
609 spi_prettyprint_status_register_bit(status, 6);
610 spi_prettyprint_status_register_atmel_at25_epewpp(status);
611 spi_prettyprint_status_register_bit(status, 3);
612 spi_prettyprint_status_register_bp(status, 0);
613 spi_prettyprint_status_register_welwip(status);
614 return 0;
615}
616
Stefan Tauner57794ac2012-12-29 15:04:20 +0000617int spi_prettyprint_status_register_at25f4096(struct flashctx *flash)
618{
619 uint8_t status;
620
621 status = spi_read_status_register(flash);
622 spi_prettyprint_status_register_hex(status);
623
624 spi_prettyprint_status_register_atmel_at25_wpen(status);
625 spi_prettyprint_status_register_bit(status, 6);
626 spi_prettyprint_status_register_bit(status, 5);
627 spi_prettyprint_status_register_bp(status, 2);
628 spi_prettyprint_status_register_welwip(status);
629 return 0;
630}
631
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000632int spi_prettyprint_status_register_at25fs010(struct flashctx *flash)
633{
634 uint8_t status = spi_read_status_register(flash);
635 spi_prettyprint_status_register_hex(status);
636
637 spi_prettyprint_status_register_atmel_at25_wpen(status);
638 msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is "
639 "%sset\n", (status & (1 << 6)) ? "" : "not ");
640 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
641 "%sset\n", (status & (1 << 5)) ? "" : "not ");
642 spi_prettyprint_status_register_bit(status, 4);
643 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
644 "%sset\n", (status & (1 << 3)) ? "" : "not ");
645 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
646 "%sset\n", (status & (1 << 2)) ? "" : "not ");
647 /* FIXME: Pretty-print detailed sector protection status. */
648 spi_prettyprint_status_register_welwip(status);
649 return 0;
650}
651
652int spi_prettyprint_status_register_at25fs040(struct flashctx *flash)
653{
654 uint8_t status = spi_read_status_register(flash);
655 spi_prettyprint_status_register_hex(status);
656
657 spi_prettyprint_status_register_atmel_at25_wpen(status);
658 spi_prettyprint_status_register_bp(status, 4);
659 /* FIXME: Pretty-print detailed sector protection status. */
660 spi_prettyprint_status_register_welwip(status);
661 return 0;
662}
663
664int spi_prettyprint_status_register_at26df081a(struct flashctx *flash)
665{
666 uint8_t status = spi_read_status_register(flash);
667 spi_prettyprint_status_register_hex(status);
668
669 spi_prettyprint_status_register_atmel_at25_srpl(status);
670 msg_cdbg("Chip status register: Sequential Program Mode Status (SPM) is %sset\n",
671 (status & (1 << 6)) ? "" : "not ");
672 spi_prettyprint_status_register_atmel_at25_epewpp(status);
673 spi_prettyprint_status_register_atmel_at25_swp(status);
674 spi_prettyprint_status_register_welwip(status);
675 return 0;
676}
677
Stefan Taunercecb2c52013-06-20 22:55:41 +0000678/* Some Atmel DataFlash chips support per sector protection bits and the write protection bits in the status
679 * register do indicate if none, some or all sectors are protected. It is possible to globally (un)lock all
680 * sectors at once by writing 0 not only the protection bits (2 and 3) but also completely unrelated bits (4 and
681 * 5) which normally are not touched.
682 * Affected are all known Atmel chips matched by AT2[56]D[FLQ]..1A? but the AT26DF041. */
683int spi_disable_blockprotect_at2x_global_unprotect(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000684{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000685 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 1 << 4, 0x00);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000686}
687
Stefan Taunercecb2c52013-06-20 22:55:41 +0000688int spi_disable_blockprotect_at2x_global_unprotect_sec(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000689{
690 /* FIXME: We should check the security lockdown. */
691 msg_cinfo("Ignoring security lockdown (if present)\n");
Stefan Taunercecb2c52013-06-20 22:55:41 +0000692 return spi_disable_blockprotect_at2x_global_unprotect(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000693}
694
Stefan Tauner57794ac2012-12-29 15:04:20 +0000695int spi_disable_blockprotect_at25f(struct flashctx *flash)
696{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000697 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000698}
699
700int spi_disable_blockprotect_at25f512a(struct flashctx *flash)
701{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000702 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000703}
704
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000705int spi_disable_blockprotect_at25f512b(struct flashctx *flash)
706{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000707 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 1 << 4, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000708}
709
710int spi_disable_blockprotect_at25fs010(struct flashctx *flash)
711{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000712 return spi_disable_blockprotect_generic(flash, 0x6C, 1 << 7, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000713 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000714
715int spi_disable_blockprotect_at25fs040(struct flashctx *flash)
716{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000717 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000718}
719
Nikolay Nikolaevd0e3ea12013-06-28 21:29:08 +0000720/* === Eon === */
721
722int spi_prettyprint_status_register_en25s_wp(struct flashctx *flash)
723{
724 uint8_t status = spi_read_status_register(flash);
725 spi_prettyprint_status_register_hex(status);
726
727 spi_prettyprint_status_register_srwd(status);
728 msg_cdbg("Chip status register: WP# disable (WPDIS) is %sabled\n", (status & (1 << 6)) ? "en " : "dis");
729 spi_prettyprint_status_register_bp(status, 3);
730 spi_prettyprint_status_register_welwip(status);
731 return 0;
732}
733
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000734/* === Intel/Numonyx/Micron - Spansion === */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000735
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000736int spi_disable_blockprotect_n25q(struct flashctx *flash)
737{
738 return spi_disable_blockprotect_generic(flash, 0x5C, 1 << 7, 0, 0xFF);
739}
740
741int spi_prettyprint_status_register_n25q(struct flashctx *flash)
742{
743 uint8_t status = spi_read_status_register(flash);
744 spi_prettyprint_status_register_hex(status);
745
746 spi_prettyprint_status_register_srwd(status);
747 if (flash->chip->total_size <= 32 / 8 * 1024) /* N25Q16 and N25Q32: reserved */
748 spi_prettyprint_status_register_bit(status, 6);
749 else
750 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
751 (status & (1 << 6)) ? "" : "not ");
752 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
753 spi_prettyprint_status_register_bp(status, 2);
754 spi_prettyprint_status_register_welwip(status);
755 return 0;
756}
757
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000758/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000759/* TODO: Clear P_FAIL and E_FAIL with Clear SR Fail Flags Command (30h) here? */
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000760int spi_disable_blockprotect_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000761{
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000762 return spi_disable_blockprotect_bp2_srwd(flash);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000763}
764
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000765/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
766int spi_prettyprint_status_register_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000767{
768 uint8_t status = spi_read_status_register(flash);
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000769 spi_prettyprint_status_register_hex(status);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000770
771 spi_prettyprint_status_register_srwd(status);
772 msg_cdbg("Chip status register: Program Fail Flag (P_FAIL) is %sset\n",
773 (status & (1 << 6)) ? "" : "not ");
774 msg_cdbg("Chip status register: Erase Fail Flag (E_FAIL) is %sset\n",
775 (status & (1 << 5)) ? "" : "not ");
776 spi_prettyprint_status_register_bp(status, 2);
777 spi_prettyprint_status_register_welwip(status);
778 return 0;
779}
780
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000781/* === SST === */
782
783static void spi_prettyprint_status_register_sst25_common(uint8_t status)
784{
785 spi_prettyprint_status_register_hex(status);
786
787 spi_prettyprint_status_register_bpl(status);
788 msg_cdbg("Chip status register: Auto Address Increment Programming (AAI) is %sset\n",
789 (status & (1 << 6)) ? "" : "not ");
790 spi_prettyprint_status_register_bp(status, 3);
791 spi_prettyprint_status_register_welwip(status);
792}
793
794int spi_prettyprint_status_register_sst25(struct flashctx *flash)
795{
796 uint8_t status = spi_read_status_register(flash);
797 spi_prettyprint_status_register_sst25_common(status);
798 return 0;
799}
800
801int spi_prettyprint_status_register_sst25vf016(struct flashctx *flash)
802{
803 static const char *const bpt[] = {
804 "none",
805 "1F0000H-1FFFFFH",
806 "1E0000H-1FFFFFH",
807 "1C0000H-1FFFFFH",
808 "180000H-1FFFFFH",
809 "100000H-1FFFFFH",
810 "all", "all"
811 };
812 uint8_t status = spi_read_status_register(flash);
813 spi_prettyprint_status_register_sst25_common(status);
814 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
815 return 0;
816}
817
818int spi_prettyprint_status_register_sst25vf040b(struct flashctx *flash)
819{
820 static const char *const bpt[] = {
821 "none",
822 "0x70000-0x7ffff",
823 "0x60000-0x7ffff",
824 "0x40000-0x7ffff",
825 "all blocks", "all blocks", "all blocks", "all blocks"
826 };
827 uint8_t status = spi_read_status_register(flash);
828 spi_prettyprint_status_register_sst25_common(status);
829 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
830 return 0;
831}