blob: 6b16839849da6380d1f466df17f4870c3d10ade8 [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;
Nikolai Artemiev01675222021-10-20 22:30:41 +110069 default:
70 msg_cerr("Cannot write register: unknown register\n");
71 return 1;
72 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +000073
Nikolai Artemieva1d68652021-11-22 13:18:49 +110074 uint8_t enable_cmd;
75 if (feature_bits & FEATURE_WRSR_WREN) {
76 enable_cmd = JEDEC_WREN;
77 } else if (feature_bits & FEATURE_WRSR_EWSR) {
78 enable_cmd = JEDEC_EWSR;
79 } else {
Stefan Tauner6ee37e22012-12-29 15:03:51 +000080 msg_cdbg("Missing status register write definition, assuming "
81 "EWSR is needed\n");
Nikolai Artemieva1d68652021-11-22 13:18:49 +110082 enable_cmd = JEDEC_EWSR;
Stefan Tauner6ee37e22012-12-29 15:03:51 +000083 }
Nikolai Artemiev01675222021-10-20 22:30:41 +110084
Nikolai Artemieva1d68652021-11-22 13:18:49 +110085 struct spi_command cmds[] = {
86 {
87 .writecnt = JEDEC_WREN_OUTSIZE,
88 .writearr = &enable_cmd,
89 .readcnt = 0,
90 .readarr = NULL,
91 }, {
92 .writecnt = write_cmd_len,
93 .writearr = write_cmd,
94 .readcnt = 0,
95 .readarr = NULL,
96 }, {
97 .writecnt = 0,
98 .writearr = NULL,
99 .readcnt = 0,
100 .readarr = NULL,
101 }};
102
103 int result = spi_send_multicommand(flash, cmds);
104 if (result) {
105 msg_cerr("%s failed during command execution\n", __func__);
106 return result;
107 }
108
109 /*
110 * WRSR performs a self-timed erase before the changes take effect.
111 * This may take 50-85 ms in most cases, and some chips apparently
112 * allow running RDSR only once. Therefore pick an initial delay of
113 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
114 *
115 * Newer chips with multiple status registers (SR2 etc.) are unlikely
116 * to have problems with multiple RDSR commands, so only wait for the
117 * initial 100 ms if the register we wrote to was SR1.
118 */
119 int delay_ms = 5000;
120 if (reg == STATUS1) {
121 programmer_delay(100 * 1000);
122 delay_ms -= 100;
123 }
124
125 for (; delay_ms > 0; delay_ms -= 10) {
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100126 uint8_t status;
127 result = spi_read_register(flash, STATUS1, &status);
128 if (result)
129 return result;
130 if ((status & SPI_SR_WIP) == 0)
Nikolai Artemieva1d68652021-11-22 13:18:49 +1100131 return 0;
132 programmer_delay(10 * 1000);
133 }
134
135
136 msg_cerr("Error: WIP bit after WRSR never cleared\n");
137 return TIMEOUT_ERROR;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000138}
139
Nikolai Artemiev01675222021-10-20 22:30:41 +1100140int spi_read_register(const struct flashctx *flash, enum flash_reg reg, uint8_t *value)
141{
Nikolai Artemiev9de3f872021-10-20 22:32:25 +1100142 int feature_bits = flash->chip->feature_bits;
Nikolai Artemiev01675222021-10-20 22:30:41 +1100143 uint8_t read_cmd;
144
145 switch (reg) {
146 case STATUS1:
147 read_cmd = JEDEC_RDSR;
148 break;
Nikolai Artemiev9de3f872021-10-20 22:32:25 +1100149 case STATUS2:
150 if (feature_bits & (FEATURE_WRSR_EXT | FEATURE_WRSR2)) {
151 read_cmd = JEDEC_RDSR2;
152 break;
153 }
154 msg_cerr("Cannot read SR2: unsupported by chip\n");
155 return 1;
Nikolai Artemiev01675222021-10-20 22:30:41 +1100156 default:
157 msg_cerr("Cannot read register: unknown register\n");
158 return 1;
159 }
160
161 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
162 /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
163 uint8_t readarr[2];
164
165 int ret = spi_send_command(flash, sizeof(read_cmd), sizeof(readarr), &read_cmd, readarr);
166 if (ret) {
167 msg_cerr("Register read failed!\n");
168 return ret;
169 }
170
171 *value = readarr[0];
172 return 0;
173}
174
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100175static int spi_restore_status(struct flashctx *flash, uint8_t status)
176{
177 msg_cdbg("restoring chip status (0x%02x)\n", status);
Nikolai Artemiev01675222021-10-20 22:30:41 +1100178 return spi_write_register(flash, STATUS1, status);
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100179}
180
Stefan Tauner9530a022012-12-29 15:04:05 +0000181/* A generic block protection disable.
182 * Tests if a protection is enabled with the block protection mask (bp_mask) and returns success otherwise.
183 * Tests if the register bits are locked with the lock_mask (lock_mask).
Stefan Taunercecb2c52013-06-20 22:55:41 +0000184 * Tests if a hardware protection is active (i.e. low pin/high bit value) with the write protection mask
185 * (wp_mask) and bails out in that case.
186 * If there are register lock bits set we try to disable them by unsetting those bits of the previous register
187 * contents that are set in the lock_mask. We then check if removing the lock bits has worked and continue as if
188 * they never had been engaged:
189 * If the lock bits are out of the way try to disable engaged protections.
190 * To support uncommon global unprotects (e.g. on most AT2[56]xx1(A)) unprotect_mask can be used to force
191 * bits to 0 additionally to those set in bp_mask and lock_mask. Only bits set in unprotect_mask are potentially
192 * preserved when doing the final unprotect.
193 *
194 * To sum up:
195 * bp_mask: set those bits that correspond to the bits in the status register that indicate an active protection
196 * (which should be unset after this function returns).
197 * lock_mask: set the bits that correspond to the bits that lock changing the bits above.
198 * wp_mask: set the bits that correspond to bits indicating non-software revocable protections.
199 * unprotect_mask: set the bits that should be preserved if possible when unprotecting.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000200 */
Stefan Taunercecb2c52013-06-20 22:55:41 +0000201static 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 +0000202{
203 uint8_t status;
204 int result;
205
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100206 int ret = spi_read_register(flash, STATUS1, &status);
207 if (ret)
208 return ret;
209
Stefan Tauner9530a022012-12-29 15:04:05 +0000210 if ((status & bp_mask) == 0) {
211 msg_cdbg2("Block protection is disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000212 return 0;
Stefan Tauner9530a022012-12-29 15:04:05 +0000213 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000214
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100215 /* Restore status register content upon exit in finalize_flash_access(). */
216 register_chip_restore(spi_restore_status, flash, status);
217
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000218 msg_cdbg("Some block protection in effect, disabling... ");
Stefan Tauner9530a022012-12-29 15:04:05 +0000219 if ((status & lock_mask) != 0) {
220 msg_cdbg("\n\tNeed to disable the register lock first... ");
221 if (wp_mask != 0 && (status & wp_mask) == 0) {
222 msg_cerr("Hardware protection is active, disabling write protection is impossible.\n");
223 return 1;
224 }
225 /* All bits except the register lock bit (often called SPRL, SRWD, WPEN) are readonly. */
Nikolai Artemiev01675222021-10-20 22:30:41 +1100226 result = spi_write_register(flash, STATUS1, status & ~lock_mask);
Stefan Tauner9530a022012-12-29 15:04:05 +0000227 if (result) {
Nikolai Artemiev01675222021-10-20 22:30:41 +1100228 msg_cerr("Could not write status register 1.\n");
Stefan Tauner9530a022012-12-29 15:04:05 +0000229 return result;
230 }
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100231
232 ret = spi_read_register(flash, STATUS1, &status);
233 if (ret)
234 return ret;
235
Stefan Taunercecb2c52013-06-20 22:55:41 +0000236 if ((status & lock_mask) != 0) {
237 msg_cerr("Unsetting lock bit(s) failed.\n");
238 return 1;
239 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000240 msg_cdbg("done.\n");
241 }
242 /* Global unprotect. Make sure to mask the register lock bit as well. */
Nikolai Artemiev01675222021-10-20 22:30:41 +1100243 result = spi_write_register(flash, STATUS1, status & ~(bp_mask | lock_mask) & unprotect_mask);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000244 if (result) {
Nikolai Artemiev01675222021-10-20 22:30:41 +1100245 msg_cerr("Could not write status register 1.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000246 return result;
247 }
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100248
249 ret = spi_read_register(flash, STATUS1, &status);
250 if (ret)
251 return ret;
252
Stefan Tauner9530a022012-12-29 15:04:05 +0000253 if ((status & bp_mask) != 0) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000254 msg_cerr("Block protection could not be disabled!\n");
Yuji Sasaki4af36092019-03-22 10:59:50 -0700255 if (flash->chip->printlock)
256 flash->chip->printlock(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000257 return 1;
258 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000259 msg_cdbg("disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000260 return 0;
261}
262
Stefan Tauner9530a022012-12-29 15:04:05 +0000263/* A common block protection disable that tries to unset the status register bits masked by 0x3C. */
264int spi_disable_blockprotect(struct flashctx *flash)
265{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000266 return spi_disable_blockprotect_generic(flash, 0x3C, 0, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000267}
268
Wei Hu25584de2018-04-30 14:02:08 -0700269int spi_disable_blockprotect_sst26_global_unprotect(struct flashctx *flash)
270{
271 int result = spi_write_enable(flash);
272 if (result)
273 return result;
274
275 static const unsigned char cmd[] = { 0x98 }; /* ULBPR */
276 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
277 if (result)
278 msg_cerr("ULBPR failed\n");
279 return result;
280}
281
Stefan Taunera60d4082014-06-04 16:17:03 +0000282/* A common block protection disable that tries to unset the status register bits masked by 0x0C (BP0-1) and
283 * protected/locked by bit #7. Useful when bits 4-5 may be non-0). */
284int spi_disable_blockprotect_bp1_srwd(struct flashctx *flash)
285{
286 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
287}
288
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000289/* A common block protection disable that tries to unset the status register bits masked by 0x1C (BP0-2) and
290 * protected/locked by bit #7. Useful when bit #5 is neither a protection bit nor reserved (and hence possibly
291 * non-0). */
292int spi_disable_blockprotect_bp2_srwd(struct flashctx *flash)
293{
294 return spi_disable_blockprotect_generic(flash, 0x1C, 1 << 7, 0, 0xFF);
295}
296
297/* A common block protection disable that tries to unset the status register bits masked by 0x3C (BP0-3) and
298 * protected/locked by bit #7. */
299int spi_disable_blockprotect_bp3_srwd(struct flashctx *flash)
300{
301 return spi_disable_blockprotect_generic(flash, 0x3C, 1 << 7, 0, 0xFF);
302}
303
304/* A common block protection disable that tries to unset the status register bits masked by 0x7C (BP0-4) and
305 * protected/locked by bit #7. */
306int spi_disable_blockprotect_bp4_srwd(struct flashctx *flash)
307{
308 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
309}
Stefan Tauner9530a022012-12-29 15:04:05 +0000310
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000311static void spi_prettyprint_status_register_hex(uint8_t status)
312{
313 msg_cdbg("Chip status register is 0x%02x.\n", status);
314}
315
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000316/* Common highest bit: Status Register Write Disable (SRWD) or Status Register Protect (SRP). */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000317static void spi_prettyprint_status_register_srwd(uint8_t status)
318{
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000319 msg_cdbg("Chip status register: Status Register Write Disable (SRWD, SRP, ...) is %sset\n",
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000320 (status & (1 << 7)) ? "" : "not ");
321}
322
323/* Common highest bit: Block Protect Write Disable (BPL). */
324static void spi_prettyprint_status_register_bpl(uint8_t status)
325{
326 msg_cdbg("Chip status register: Block Protect Write Disable (BPL) is %sset\n",
327 (status & (1 << 7)) ? "" : "not ");
328}
329
330/* Common lowest 2 bits: WEL and WIP. */
331static void spi_prettyprint_status_register_welwip(uint8_t status)
332{
333 msg_cdbg("Chip status register: Write Enable Latch (WEL) is %sset\n",
334 (status & (1 << 1)) ? "" : "not ");
335 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is %sset\n",
336 (status & (1 << 0)) ? "" : "not ");
337}
338
339/* Common block protection (BP) bits. */
340static void spi_prettyprint_status_register_bp(uint8_t status, int bp)
341{
342 switch (bp) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000343 case 4:
344 msg_cdbg("Chip status register: Block Protect 4 (BP4) is %sset\n",
Stefan Tauner5c316f92015-02-08 21:57:52 +0000345 (status & (1 << 6)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000346 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000347 case 3:
348 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
349 (status & (1 << 5)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000350 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000351 case 2:
352 msg_cdbg("Chip status register: Block Protect 2 (BP2) is %sset\n",
353 (status & (1 << 4)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000354 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000355 case 1:
356 msg_cdbg("Chip status register: Block Protect 1 (BP1) is %sset\n",
357 (status & (1 << 3)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000358 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000359 case 0:
360 msg_cdbg("Chip status register: Block Protect 0 (BP0) is %sset\n",
361 (status & (1 << 2)) ? "" : "not ");
362 }
363}
364
365/* Unnamed bits. */
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000366void spi_prettyprint_status_register_bit(uint8_t status, int bit)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000367{
368 msg_cdbg("Chip status register: Bit %i is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
369}
370
371int spi_prettyprint_status_register_plain(struct flashctx *flash)
372{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100373 uint8_t status;
374 int ret = spi_read_register(flash, STATUS1, &status);
375 if (ret)
376 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000377 spi_prettyprint_status_register_hex(status);
378 return 0;
379}
380
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000381/* Print the plain hex value and the welwip bits only. */
382int spi_prettyprint_status_register_default_welwip(struct flashctx *flash)
383{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100384 uint8_t status;
385 int ret = spi_read_register(flash, STATUS1, &status);
386 if (ret)
387 return ret;
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000388 spi_prettyprint_status_register_hex(status);
389
390 spi_prettyprint_status_register_welwip(status);
391 return 0;
392}
393
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000394/* Works for many chips of the
395 * AMIC A25L series
396 * and MX MX25L512
397 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000398int spi_prettyprint_status_register_bp1_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000399{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100400 uint8_t status;
401 int ret = spi_read_register(flash, STATUS1, &status);
402 if (ret)
403 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000404 spi_prettyprint_status_register_hex(status);
405
406 spi_prettyprint_status_register_srwd(status);
407 spi_prettyprint_status_register_bit(status, 6);
408 spi_prettyprint_status_register_bit(status, 5);
409 spi_prettyprint_status_register_bit(status, 4);
410 spi_prettyprint_status_register_bp(status, 1);
411 spi_prettyprint_status_register_welwip(status);
412 return 0;
413}
414
415/* Works for many chips of the
416 * AMIC A25L series
Stefan Taunerf4451612013-04-19 01:59:15 +0000417 * PMC Pm25LD series
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000418 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000419int spi_prettyprint_status_register_bp2_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000420{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100421 uint8_t status;
422 int ret = spi_read_register(flash, STATUS1, &status);
423 if (ret)
424 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000425 spi_prettyprint_status_register_hex(status);
426
427 spi_prettyprint_status_register_srwd(status);
428 spi_prettyprint_status_register_bit(status, 6);
429 spi_prettyprint_status_register_bit(status, 5);
430 spi_prettyprint_status_register_bp(status, 2);
431 spi_prettyprint_status_register_welwip(status);
432 return 0;
433}
434
435/* Works for many chips of the
436 * ST M25P series
437 * MX MX25L series
438 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000439int spi_prettyprint_status_register_bp3_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000440{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100441 uint8_t status;
442 int ret = spi_read_register(flash, STATUS1, &status);
443 if (ret)
444 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000445 spi_prettyprint_status_register_hex(status);
446
447 spi_prettyprint_status_register_srwd(status);
448 spi_prettyprint_status_register_bit(status, 6);
449 spi_prettyprint_status_register_bp(status, 3);
450 spi_prettyprint_status_register_welwip(status);
451 return 0;
452}
453
Stefan Tauner12f3d512014-05-27 21:27:27 +0000454int spi_prettyprint_status_register_bp4_srwd(struct flashctx *flash)
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000455{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100456 uint8_t status;
457 int ret = spi_read_register(flash, STATUS1, &status);
458 if (ret)
459 return ret;
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000460 spi_prettyprint_status_register_hex(status);
461
462 spi_prettyprint_status_register_srwd(status);
463 spi_prettyprint_status_register_bp(status, 4);
464 spi_prettyprint_status_register_welwip(status);
465 return 0;
466}
467
Stefan Tauner85f09f72014-05-27 21:27:14 +0000468int spi_prettyprint_status_register_bp2_bpl(struct flashctx *flash)
469{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100470 uint8_t status;
471 int ret = spi_read_register(flash, STATUS1, &status);
472 if (ret)
473 return ret;
Stefan Tauner85f09f72014-05-27 21:27:14 +0000474 spi_prettyprint_status_register_hex(status);
475
476 spi_prettyprint_status_register_bpl(status);
477 spi_prettyprint_status_register_bit(status, 6);
478 spi_prettyprint_status_register_bit(status, 5);
479 spi_prettyprint_status_register_bp(status, 2);
480 spi_prettyprint_status_register_welwip(status);
481 return 0;
482}
483
Ben Gardnerbcf61092015-11-22 02:23:31 +0000484int spi_prettyprint_status_register_bp2_tb_bpl(struct flashctx *flash)
485{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100486 uint8_t status;
487 int ret = spi_read_register(flash, STATUS1, &status);
488 if (ret)
489 return ret;
Ben Gardnerbcf61092015-11-22 02:23:31 +0000490 spi_prettyprint_status_register_hex(status);
491
492 spi_prettyprint_status_register_bpl(status);
493 spi_prettyprint_status_register_bit(status, 6);
494 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
495 spi_prettyprint_status_register_bp(status, 2);
496 spi_prettyprint_status_register_welwip(status);
497 return 0;
498}
499
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000500/* === Amic ===
501 * FIXME: spi_disable_blockprotect is incorrect but works fine for chips using
Stefan Tauner12f3d512014-05-27 21:27:27 +0000502 * spi_prettyprint_status_register_bp1_srwd or
503 * spi_prettyprint_status_register_bp2_srwd.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000504 * FIXME: spi_disable_blockprotect is incorrect and will fail for chips using
505 * spi_prettyprint_status_register_amic_a25l032 if those have locks controlled
506 * by the second status register.
507 */
508
509int spi_prettyprint_status_register_amic_a25l032(struct flashctx *flash)
510{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100511 uint8_t status;
512 int ret = spi_read_register(flash, STATUS1, &status);
513 if (ret)
514 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000515 spi_prettyprint_status_register_hex(status);
516
517 spi_prettyprint_status_register_srwd(status);
518 msg_cdbg("Chip status register: Sector Protect Size (SEC) is %i KB\n", (status & (1 << 6)) ? 4 : 64);
519 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
520 spi_prettyprint_status_register_bp(status, 2);
521 spi_prettyprint_status_register_welwip(status);
522 msg_cdbg("Chip status register 2 is NOT decoded!\n");
523 return 0;
524}
525
526/* === Atmel === */
527
528static void spi_prettyprint_status_register_atmel_at25_wpen(uint8_t status)
529{
530 msg_cdbg("Chip status register: Write Protect Enable (WPEN) is %sset\n",
531 (status & (1 << 7)) ? "" : "not ");
532}
533
534static void spi_prettyprint_status_register_atmel_at25_srpl(uint8_t status)
535{
536 msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) is %sset\n",
537 (status & (1 << 7)) ? "" : "not ");
538}
539
540static void spi_prettyprint_status_register_atmel_at25_epewpp(uint8_t status)
541{
542 msg_cdbg("Chip status register: Erase/Program Error (EPE) is %sset\n",
543 (status & (1 << 5)) ? "" : "not ");
544 msg_cdbg("Chip status register: WP# pin (WPP) is %sasserted\n",
545 (status & (1 << 4)) ? "not " : "");
546}
547
548static void spi_prettyprint_status_register_atmel_at25_swp(uint8_t status)
549{
550 msg_cdbg("Chip status register: Software Protection Status (SWP): ");
551 switch (status & (3 << 2)) {
552 case 0x0 << 2:
553 msg_cdbg("no sectors are protected\n");
554 break;
555 case 0x1 << 2:
556 msg_cdbg("some sectors are protected\n");
557 /* FIXME: Read individual Sector Protection Registers. */
558 break;
559 case 0x3 << 2:
560 msg_cdbg("all sectors are protected\n");
561 break;
562 default:
563 msg_cdbg("reserved for future use\n");
564 break;
565 }
566}
567
568int spi_prettyprint_status_register_at25df(struct flashctx *flash)
569{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100570 uint8_t status;
571 int ret = spi_read_register(flash, STATUS1, &status);
572 if (ret)
573 return ret;
574
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000575 spi_prettyprint_status_register_hex(status);
576
577 spi_prettyprint_status_register_atmel_at25_srpl(status);
578 spi_prettyprint_status_register_bit(status, 6);
579 spi_prettyprint_status_register_atmel_at25_epewpp(status);
580 spi_prettyprint_status_register_atmel_at25_swp(status);
581 spi_prettyprint_status_register_welwip(status);
582 return 0;
583}
584
585int spi_prettyprint_status_register_at25df_sec(struct flashctx *flash)
586{
587 /* FIXME: We should check the security lockdown. */
588 msg_cdbg("Ignoring security lockdown (if present)\n");
589 msg_cdbg("Ignoring status register byte 2\n");
590 return spi_prettyprint_status_register_at25df(flash);
591}
592
Stefan Tauner57794ac2012-12-29 15:04:20 +0000593/* used for AT25F512, AT25F1024(A), AT25F2048 */
594int spi_prettyprint_status_register_at25f(struct flashctx *flash)
595{
596 uint8_t status;
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100597 int ret = spi_read_register(flash, STATUS1, &status);
598 if (ret)
599 return ret;
Stefan Tauner57794ac2012-12-29 15:04:20 +0000600
Stefan Tauner57794ac2012-12-29 15:04:20 +0000601 spi_prettyprint_status_register_hex(status);
602
603 spi_prettyprint_status_register_atmel_at25_wpen(status);
604 spi_prettyprint_status_register_bit(status, 6);
605 spi_prettyprint_status_register_bit(status, 5);
606 spi_prettyprint_status_register_bit(status, 4);
607 spi_prettyprint_status_register_bp(status, 1);
608 spi_prettyprint_status_register_welwip(status);
609 return 0;
610}
611
612int spi_prettyprint_status_register_at25f512a(struct flashctx *flash)
613{
614 uint8_t status;
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100615 int ret = spi_read_register(flash, STATUS1, &status);
616 if (ret)
617 return ret;
Stefan Tauner57794ac2012-12-29 15:04:20 +0000618
Stefan Tauner57794ac2012-12-29 15:04:20 +0000619 spi_prettyprint_status_register_hex(status);
620
621 spi_prettyprint_status_register_atmel_at25_wpen(status);
622 spi_prettyprint_status_register_bit(status, 6);
623 spi_prettyprint_status_register_bit(status, 5);
624 spi_prettyprint_status_register_bit(status, 4);
625 spi_prettyprint_status_register_bit(status, 3);
626 spi_prettyprint_status_register_bp(status, 0);
627 spi_prettyprint_status_register_welwip(status);
628 return 0;
629}
630
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000631int spi_prettyprint_status_register_at25f512b(struct flashctx *flash)
632{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100633 uint8_t status;
634 int ret = spi_read_register(flash, STATUS1, &status);
635 if (ret)
636 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000637 spi_prettyprint_status_register_hex(status);
638
639 spi_prettyprint_status_register_atmel_at25_srpl(status);
640 spi_prettyprint_status_register_bit(status, 6);
641 spi_prettyprint_status_register_atmel_at25_epewpp(status);
642 spi_prettyprint_status_register_bit(status, 3);
643 spi_prettyprint_status_register_bp(status, 0);
644 spi_prettyprint_status_register_welwip(status);
645 return 0;
646}
647
Stefan Tauner57794ac2012-12-29 15:04:20 +0000648int spi_prettyprint_status_register_at25f4096(struct flashctx *flash)
649{
650 uint8_t status;
651
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100652 int ret = spi_read_register(flash, STATUS1, &status);
653 if (ret)
654 return ret;
655
Stefan Tauner57794ac2012-12-29 15:04:20 +0000656 spi_prettyprint_status_register_hex(status);
657
658 spi_prettyprint_status_register_atmel_at25_wpen(status);
659 spi_prettyprint_status_register_bit(status, 6);
660 spi_prettyprint_status_register_bit(status, 5);
661 spi_prettyprint_status_register_bp(status, 2);
662 spi_prettyprint_status_register_welwip(status);
663 return 0;
664}
665
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000666int spi_prettyprint_status_register_at25fs010(struct flashctx *flash)
667{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100668 uint8_t status;
669 int ret = spi_read_register(flash, STATUS1, &status);
670 if (ret)
671 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000672 spi_prettyprint_status_register_hex(status);
673
674 spi_prettyprint_status_register_atmel_at25_wpen(status);
675 msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is "
676 "%sset\n", (status & (1 << 6)) ? "" : "not ");
677 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
678 "%sset\n", (status & (1 << 5)) ? "" : "not ");
679 spi_prettyprint_status_register_bit(status, 4);
680 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
681 "%sset\n", (status & (1 << 3)) ? "" : "not ");
682 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
683 "%sset\n", (status & (1 << 2)) ? "" : "not ");
684 /* FIXME: Pretty-print detailed sector protection status. */
685 spi_prettyprint_status_register_welwip(status);
686 return 0;
687}
688
689int spi_prettyprint_status_register_at25fs040(struct flashctx *flash)
690{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100691 uint8_t status;
692 int ret = spi_read_register(flash, STATUS1, &status);
693 if (ret)
694 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000695 spi_prettyprint_status_register_hex(status);
696
697 spi_prettyprint_status_register_atmel_at25_wpen(status);
698 spi_prettyprint_status_register_bp(status, 4);
699 /* FIXME: Pretty-print detailed sector protection status. */
700 spi_prettyprint_status_register_welwip(status);
701 return 0;
702}
703
704int spi_prettyprint_status_register_at26df081a(struct flashctx *flash)
705{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100706 uint8_t status;
707 int ret = spi_read_register(flash, STATUS1, &status);
708 if (ret)
709 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000710 spi_prettyprint_status_register_hex(status);
711
712 spi_prettyprint_status_register_atmel_at25_srpl(status);
713 msg_cdbg("Chip status register: Sequential Program Mode Status (SPM) is %sset\n",
714 (status & (1 << 6)) ? "" : "not ");
715 spi_prettyprint_status_register_atmel_at25_epewpp(status);
716 spi_prettyprint_status_register_atmel_at25_swp(status);
717 spi_prettyprint_status_register_welwip(status);
718 return 0;
719}
720
Stefan Taunercecb2c52013-06-20 22:55:41 +0000721/* Some Atmel DataFlash chips support per sector protection bits and the write protection bits in the status
722 * register do indicate if none, some or all sectors are protected. It is possible to globally (un)lock all
723 * sectors at once by writing 0 not only the protection bits (2 and 3) but also completely unrelated bits (4 and
724 * 5) which normally are not touched.
725 * Affected are all known Atmel chips matched by AT2[56]D[FLQ]..1A? but the AT26DF041. */
726int spi_disable_blockprotect_at2x_global_unprotect(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000727{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000728 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 1 << 4, 0x00);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000729}
730
Stefan Taunercecb2c52013-06-20 22:55:41 +0000731int spi_disable_blockprotect_at2x_global_unprotect_sec(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000732{
733 /* FIXME: We should check the security lockdown. */
734 msg_cinfo("Ignoring security lockdown (if present)\n");
Stefan Taunercecb2c52013-06-20 22:55:41 +0000735 return spi_disable_blockprotect_at2x_global_unprotect(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000736}
737
Stefan Tauner57794ac2012-12-29 15:04:20 +0000738int spi_disable_blockprotect_at25f(struct flashctx *flash)
739{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000740 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000741}
742
743int spi_disable_blockprotect_at25f512a(struct flashctx *flash)
744{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000745 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000746}
747
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000748int spi_disable_blockprotect_at25f512b(struct flashctx *flash)
749{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000750 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 1 << 4, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000751}
752
753int spi_disable_blockprotect_at25fs010(struct flashctx *flash)
754{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000755 return spi_disable_blockprotect_generic(flash, 0x6C, 1 << 7, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000756 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000757
758int spi_disable_blockprotect_at25fs040(struct flashctx *flash)
759{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000760 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000761}
762
Nikolay Nikolaevd0e3ea12013-06-28 21:29:08 +0000763/* === Eon === */
764
765int spi_prettyprint_status_register_en25s_wp(struct flashctx *flash)
766{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100767 uint8_t status;
768 int ret = spi_read_register(flash, STATUS1, &status);
769 if (ret)
770 return ret;
Nikolay Nikolaevd0e3ea12013-06-28 21:29:08 +0000771 spi_prettyprint_status_register_hex(status);
772
773 spi_prettyprint_status_register_srwd(status);
774 msg_cdbg("Chip status register: WP# disable (WPDIS) is %sabled\n", (status & (1 << 6)) ? "en " : "dis");
775 spi_prettyprint_status_register_bp(status, 3);
776 spi_prettyprint_status_register_welwip(status);
777 return 0;
778}
779
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000780/* === Intel/Numonyx/Micron - Spansion === */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000781
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000782int spi_disable_blockprotect_n25q(struct flashctx *flash)
783{
784 return spi_disable_blockprotect_generic(flash, 0x5C, 1 << 7, 0, 0xFF);
785}
786
787int spi_prettyprint_status_register_n25q(struct flashctx *flash)
788{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100789 uint8_t status;
790 int ret = spi_read_register(flash, STATUS1, &status);
791 if (ret)
792 return ret;
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000793 spi_prettyprint_status_register_hex(status);
794
795 spi_prettyprint_status_register_srwd(status);
796 if (flash->chip->total_size <= 32 / 8 * 1024) /* N25Q16 and N25Q32: reserved */
797 spi_prettyprint_status_register_bit(status, 6);
798 else
799 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
800 (status & (1 << 6)) ? "" : "not ");
801 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
802 spi_prettyprint_status_register_bp(status, 2);
803 spi_prettyprint_status_register_welwip(status);
804 return 0;
805}
806
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000807/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000808/* TODO: Clear P_FAIL and E_FAIL with Clear SR Fail Flags Command (30h) here? */
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000809int spi_disable_blockprotect_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000810{
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000811 return spi_disable_blockprotect_bp2_srwd(flash);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000812}
813
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000814/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
815int spi_prettyprint_status_register_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000816{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100817 uint8_t status;
818 int ret = spi_read_register(flash, STATUS1, &status);
819 if (ret)
820 return ret;
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000821 spi_prettyprint_status_register_hex(status);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000822
823 spi_prettyprint_status_register_srwd(status);
824 msg_cdbg("Chip status register: Program Fail Flag (P_FAIL) is %sset\n",
825 (status & (1 << 6)) ? "" : "not ");
826 msg_cdbg("Chip status register: Erase Fail Flag (E_FAIL) is %sset\n",
827 (status & (1 << 5)) ? "" : "not ");
828 spi_prettyprint_status_register_bp(status, 2);
829 spi_prettyprint_status_register_welwip(status);
830 return 0;
831}
832
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000833/* === SST === */
834
835static void spi_prettyprint_status_register_sst25_common(uint8_t status)
836{
837 spi_prettyprint_status_register_hex(status);
838
839 spi_prettyprint_status_register_bpl(status);
840 msg_cdbg("Chip status register: Auto Address Increment Programming (AAI) is %sset\n",
841 (status & (1 << 6)) ? "" : "not ");
842 spi_prettyprint_status_register_bp(status, 3);
843 spi_prettyprint_status_register_welwip(status);
844}
845
846int spi_prettyprint_status_register_sst25(struct flashctx *flash)
847{
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100848 uint8_t status;
849 int ret = spi_read_register(flash, STATUS1, &status);
850 if (ret)
851 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000852 spi_prettyprint_status_register_sst25_common(status);
853 return 0;
854}
855
856int spi_prettyprint_status_register_sst25vf016(struct flashctx *flash)
857{
858 static const char *const bpt[] = {
859 "none",
860 "1F0000H-1FFFFFH",
861 "1E0000H-1FFFFFH",
862 "1C0000H-1FFFFFH",
863 "180000H-1FFFFFH",
864 "100000H-1FFFFFH",
865 "all", "all"
866 };
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100867 uint8_t status;
868 int ret = spi_read_register(flash, STATUS1, &status);
869 if (ret)
870 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000871 spi_prettyprint_status_register_sst25_common(status);
872 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
873 return 0;
874}
875
876int spi_prettyprint_status_register_sst25vf040b(struct flashctx *flash)
877{
878 static const char *const bpt[] = {
879 "none",
880 "0x70000-0x7ffff",
881 "0x60000-0x7ffff",
882 "0x40000-0x7ffff",
883 "all blocks", "all blocks", "all blocks", "all blocks"
884 };
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100885 uint8_t status;
886 int ret = spi_read_register(flash, STATUS1, &status);
887 if (ret)
888 return ret;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000889 spi_prettyprint_status_register_sst25_common(status);
890 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
891 return 0;
892}