blob: 249ab9a3e9df39a894cc0a93a6c054461d67293c [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) {
126 if ((spi_read_status_register(flash) & SPI_SR_WIP) == 0)
127 return 0;
128 programmer_delay(10 * 1000);
129 }
130
131
132 msg_cerr("Error: WIP bit after WRSR never cleared\n");
133 return TIMEOUT_ERROR;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000134}
135
Nikolai Artemiev01675222021-10-20 22:30:41 +1100136int spi_read_register(const struct flashctx *flash, enum flash_reg reg, uint8_t *value)
137{
Nikolai Artemiev9de3f872021-10-20 22:32:25 +1100138 int feature_bits = flash->chip->feature_bits;
Nikolai Artemiev01675222021-10-20 22:30:41 +1100139 uint8_t read_cmd;
140
141 switch (reg) {
142 case STATUS1:
143 read_cmd = JEDEC_RDSR;
144 break;
Nikolai Artemiev9de3f872021-10-20 22:32:25 +1100145 case STATUS2:
146 if (feature_bits & (FEATURE_WRSR_EXT | FEATURE_WRSR2)) {
147 read_cmd = JEDEC_RDSR2;
148 break;
149 }
150 msg_cerr("Cannot read SR2: unsupported by chip\n");
151 return 1;
Nikolai Artemiev01675222021-10-20 22:30:41 +1100152 default:
153 msg_cerr("Cannot read register: unknown register\n");
154 return 1;
155 }
156
157 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
158 /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
159 uint8_t readarr[2];
160
161 int ret = spi_send_command(flash, sizeof(read_cmd), sizeof(readarr), &read_cmd, readarr);
162 if (ret) {
163 msg_cerr("Register read failed!\n");
164 return ret;
165 }
166
167 *value = readarr[0];
168 return 0;
169}
170
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000171uint8_t spi_read_status_register(const struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000172{
Nikolai Artemiev01675222021-10-20 22:30:41 +1100173 uint8_t status = 0;
174 /* FIXME: We should propagate the error. */
175 spi_read_register(flash, STATUS1, &status);
176 return status;
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000177}
178
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100179static int spi_restore_status(struct flashctx *flash, uint8_t status)
180{
181 msg_cdbg("restoring chip status (0x%02x)\n", status);
Nikolai Artemiev01675222021-10-20 22:30:41 +1100182 return spi_write_register(flash, STATUS1, status);
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100183}
184
Stefan Tauner9530a022012-12-29 15:04:05 +0000185/* A generic block protection disable.
186 * Tests if a protection is enabled with the block protection mask (bp_mask) and returns success otherwise.
187 * Tests if the register bits are locked with the lock_mask (lock_mask).
Stefan Taunercecb2c52013-06-20 22:55:41 +0000188 * Tests if a hardware protection is active (i.e. low pin/high bit value) with the write protection mask
189 * (wp_mask) and bails out in that case.
190 * If there are register lock bits set we try to disable them by unsetting those bits of the previous register
191 * contents that are set in the lock_mask. We then check if removing the lock bits has worked and continue as if
192 * they never had been engaged:
193 * If the lock bits are out of the way try to disable engaged protections.
194 * To support uncommon global unprotects (e.g. on most AT2[56]xx1(A)) unprotect_mask can be used to force
195 * bits to 0 additionally to those set in bp_mask and lock_mask. Only bits set in unprotect_mask are potentially
196 * preserved when doing the final unprotect.
197 *
198 * To sum up:
199 * bp_mask: set those bits that correspond to the bits in the status register that indicate an active protection
200 * (which should be unset after this function returns).
201 * lock_mask: set the bits that correspond to the bits that lock changing the bits above.
202 * wp_mask: set the bits that correspond to bits indicating non-software revocable protections.
203 * unprotect_mask: set the bits that should be preserved if possible when unprotecting.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000204 */
Stefan Taunercecb2c52013-06-20 22:55:41 +0000205static 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 +0000206{
207 uint8_t status;
208 int result;
209
210 status = spi_read_status_register(flash);
Stefan Tauner9530a022012-12-29 15:04:05 +0000211 if ((status & bp_mask) == 0) {
212 msg_cdbg2("Block protection is disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000213 return 0;
Stefan Tauner9530a022012-12-29 15:04:05 +0000214 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000215
Nikolai Artemiev721a4f32020-12-14 07:39:02 +1100216 /* Restore status register content upon exit in finalize_flash_access(). */
217 register_chip_restore(spi_restore_status, flash, status);
218
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000219 msg_cdbg("Some block protection in effect, disabling... ");
Stefan Tauner9530a022012-12-29 15:04:05 +0000220 if ((status & lock_mask) != 0) {
221 msg_cdbg("\n\tNeed to disable the register lock first... ");
222 if (wp_mask != 0 && (status & wp_mask) == 0) {
223 msg_cerr("Hardware protection is active, disabling write protection is impossible.\n");
224 return 1;
225 }
226 /* All bits except the register lock bit (often called SPRL, SRWD, WPEN) are readonly. */
Nikolai Artemiev01675222021-10-20 22:30:41 +1100227 result = spi_write_register(flash, STATUS1, status & ~lock_mask);
Stefan Tauner9530a022012-12-29 15:04:05 +0000228 if (result) {
Nikolai Artemiev01675222021-10-20 22:30:41 +1100229 msg_cerr("Could not write status register 1.\n");
Stefan Tauner9530a022012-12-29 15:04:05 +0000230 return result;
231 }
Stefan Taunercecb2c52013-06-20 22:55:41 +0000232 status = spi_read_status_register(flash);
233 if ((status & lock_mask) != 0) {
234 msg_cerr("Unsetting lock bit(s) failed.\n");
235 return 1;
236 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000237 msg_cdbg("done.\n");
238 }
239 /* Global unprotect. Make sure to mask the register lock bit as well. */
Nikolai Artemiev01675222021-10-20 22:30:41 +1100240 result = spi_write_register(flash, STATUS1, status & ~(bp_mask | lock_mask) & unprotect_mask);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000241 if (result) {
Nikolai Artemiev01675222021-10-20 22:30:41 +1100242 msg_cerr("Could not write status register 1.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000243 return result;
244 }
245 status = spi_read_status_register(flash);
Stefan Tauner9530a022012-12-29 15:04:05 +0000246 if ((status & bp_mask) != 0) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000247 msg_cerr("Block protection could not be disabled!\n");
Yuji Sasaki4af36092019-03-22 10:59:50 -0700248 if (flash->chip->printlock)
249 flash->chip->printlock(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000250 return 1;
251 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000252 msg_cdbg("disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000253 return 0;
254}
255
Stefan Tauner9530a022012-12-29 15:04:05 +0000256/* A common block protection disable that tries to unset the status register bits masked by 0x3C. */
257int spi_disable_blockprotect(struct flashctx *flash)
258{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000259 return spi_disable_blockprotect_generic(flash, 0x3C, 0, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000260}
261
Wei Hu25584de2018-04-30 14:02:08 -0700262int spi_disable_blockprotect_sst26_global_unprotect(struct flashctx *flash)
263{
264 int result = spi_write_enable(flash);
265 if (result)
266 return result;
267
268 static const unsigned char cmd[] = { 0x98 }; /* ULBPR */
269 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
270 if (result)
271 msg_cerr("ULBPR failed\n");
272 return result;
273}
274
Stefan Taunera60d4082014-06-04 16:17:03 +0000275/* A common block protection disable that tries to unset the status register bits masked by 0x0C (BP0-1) and
276 * protected/locked by bit #7. Useful when bits 4-5 may be non-0). */
277int spi_disable_blockprotect_bp1_srwd(struct flashctx *flash)
278{
279 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
280}
281
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000282/* A common block protection disable that tries to unset the status register bits masked by 0x1C (BP0-2) and
283 * protected/locked by bit #7. Useful when bit #5 is neither a protection bit nor reserved (and hence possibly
284 * non-0). */
285int spi_disable_blockprotect_bp2_srwd(struct flashctx *flash)
286{
287 return spi_disable_blockprotect_generic(flash, 0x1C, 1 << 7, 0, 0xFF);
288}
289
290/* A common block protection disable that tries to unset the status register bits masked by 0x3C (BP0-3) and
291 * protected/locked by bit #7. */
292int spi_disable_blockprotect_bp3_srwd(struct flashctx *flash)
293{
294 return spi_disable_blockprotect_generic(flash, 0x3C, 1 << 7, 0, 0xFF);
295}
296
297/* A common block protection disable that tries to unset the status register bits masked by 0x7C (BP0-4) and
298 * protected/locked by bit #7. */
299int spi_disable_blockprotect_bp4_srwd(struct flashctx *flash)
300{
301 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
302}
Stefan Tauner9530a022012-12-29 15:04:05 +0000303
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000304static void spi_prettyprint_status_register_hex(uint8_t status)
305{
306 msg_cdbg("Chip status register is 0x%02x.\n", status);
307}
308
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000309/* Common highest bit: Status Register Write Disable (SRWD) or Status Register Protect (SRP). */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000310static void spi_prettyprint_status_register_srwd(uint8_t status)
311{
Stefan Taunerb6b00e92013-06-28 21:28:43 +0000312 msg_cdbg("Chip status register: Status Register Write Disable (SRWD, SRP, ...) is %sset\n",
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000313 (status & (1 << 7)) ? "" : "not ");
314}
315
316/* Common highest bit: Block Protect Write Disable (BPL). */
317static void spi_prettyprint_status_register_bpl(uint8_t status)
318{
319 msg_cdbg("Chip status register: Block Protect Write Disable (BPL) is %sset\n",
320 (status & (1 << 7)) ? "" : "not ");
321}
322
323/* Common lowest 2 bits: WEL and WIP. */
324static void spi_prettyprint_status_register_welwip(uint8_t status)
325{
326 msg_cdbg("Chip status register: Write Enable Latch (WEL) is %sset\n",
327 (status & (1 << 1)) ? "" : "not ");
328 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is %sset\n",
329 (status & (1 << 0)) ? "" : "not ");
330}
331
332/* Common block protection (BP) bits. */
333static void spi_prettyprint_status_register_bp(uint8_t status, int bp)
334{
335 switch (bp) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000336 case 4:
337 msg_cdbg("Chip status register: Block Protect 4 (BP4) is %sset\n",
Stefan Tauner5c316f92015-02-08 21:57:52 +0000338 (status & (1 << 6)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000339 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000340 case 3:
341 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
342 (status & (1 << 5)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000343 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000344 case 2:
345 msg_cdbg("Chip status register: Block Protect 2 (BP2) is %sset\n",
346 (status & (1 << 4)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000347 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000348 case 1:
349 msg_cdbg("Chip status register: Block Protect 1 (BP1) is %sset\n",
350 (status & (1 << 3)) ? "" : "not ");
Richard Hughesdb7482b2018-12-19 12:04:30 +0000351 /* Fall through. */
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000352 case 0:
353 msg_cdbg("Chip status register: Block Protect 0 (BP0) is %sset\n",
354 (status & (1 << 2)) ? "" : "not ");
355 }
356}
357
358/* Unnamed bits. */
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000359void spi_prettyprint_status_register_bit(uint8_t status, int bit)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000360{
361 msg_cdbg("Chip status register: Bit %i is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
362}
363
364int spi_prettyprint_status_register_plain(struct flashctx *flash)
365{
366 uint8_t status = spi_read_status_register(flash);
367 spi_prettyprint_status_register_hex(status);
368 return 0;
369}
370
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000371/* Print the plain hex value and the welwip bits only. */
372int spi_prettyprint_status_register_default_welwip(struct flashctx *flash)
373{
374 uint8_t status = spi_read_status_register(flash);
375 spi_prettyprint_status_register_hex(status);
376
377 spi_prettyprint_status_register_welwip(status);
378 return 0;
379}
380
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000381/* Works for many chips of the
382 * AMIC A25L series
383 * and MX MX25L512
384 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000385int spi_prettyprint_status_register_bp1_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000386{
387 uint8_t status = spi_read_status_register(flash);
388 spi_prettyprint_status_register_hex(status);
389
390 spi_prettyprint_status_register_srwd(status);
391 spi_prettyprint_status_register_bit(status, 6);
392 spi_prettyprint_status_register_bit(status, 5);
393 spi_prettyprint_status_register_bit(status, 4);
394 spi_prettyprint_status_register_bp(status, 1);
395 spi_prettyprint_status_register_welwip(status);
396 return 0;
397}
398
399/* Works for many chips of the
400 * AMIC A25L series
Stefan Taunerf4451612013-04-19 01:59:15 +0000401 * PMC Pm25LD series
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000402 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000403int spi_prettyprint_status_register_bp2_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000404{
405 uint8_t status = spi_read_status_register(flash);
406 spi_prettyprint_status_register_hex(status);
407
408 spi_prettyprint_status_register_srwd(status);
409 spi_prettyprint_status_register_bit(status, 6);
410 spi_prettyprint_status_register_bit(status, 5);
411 spi_prettyprint_status_register_bp(status, 2);
412 spi_prettyprint_status_register_welwip(status);
413 return 0;
414}
415
416/* Works for many chips of the
417 * ST M25P series
418 * MX MX25L series
419 */
Stefan Tauner12f3d512014-05-27 21:27:27 +0000420int spi_prettyprint_status_register_bp3_srwd(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000421{
422 uint8_t status = spi_read_status_register(flash);
423 spi_prettyprint_status_register_hex(status);
424
425 spi_prettyprint_status_register_srwd(status);
426 spi_prettyprint_status_register_bit(status, 6);
427 spi_prettyprint_status_register_bp(status, 3);
428 spi_prettyprint_status_register_welwip(status);
429 return 0;
430}
431
Stefan Tauner12f3d512014-05-27 21:27:27 +0000432int spi_prettyprint_status_register_bp4_srwd(struct flashctx *flash)
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000433{
434 uint8_t status = spi_read_status_register(flash);
435 spi_prettyprint_status_register_hex(status);
436
437 spi_prettyprint_status_register_srwd(status);
438 spi_prettyprint_status_register_bp(status, 4);
439 spi_prettyprint_status_register_welwip(status);
440 return 0;
441}
442
Stefan Tauner85f09f72014-05-27 21:27:14 +0000443int spi_prettyprint_status_register_bp2_bpl(struct flashctx *flash)
444{
445 uint8_t status = spi_read_status_register(flash);
446 spi_prettyprint_status_register_hex(status);
447
448 spi_prettyprint_status_register_bpl(status);
449 spi_prettyprint_status_register_bit(status, 6);
450 spi_prettyprint_status_register_bit(status, 5);
451 spi_prettyprint_status_register_bp(status, 2);
452 spi_prettyprint_status_register_welwip(status);
453 return 0;
454}
455
Ben Gardnerbcf61092015-11-22 02:23:31 +0000456int spi_prettyprint_status_register_bp2_tb_bpl(struct flashctx *flash)
457{
458 uint8_t status = spi_read_status_register(flash);
459 spi_prettyprint_status_register_hex(status);
460
461 spi_prettyprint_status_register_bpl(status);
462 spi_prettyprint_status_register_bit(status, 6);
463 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
464 spi_prettyprint_status_register_bp(status, 2);
465 spi_prettyprint_status_register_welwip(status);
466 return 0;
467}
468
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000469/* === Amic ===
470 * FIXME: spi_disable_blockprotect is incorrect but works fine for chips using
Stefan Tauner12f3d512014-05-27 21:27:27 +0000471 * spi_prettyprint_status_register_bp1_srwd or
472 * spi_prettyprint_status_register_bp2_srwd.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000473 * FIXME: spi_disable_blockprotect is incorrect and will fail for chips using
474 * spi_prettyprint_status_register_amic_a25l032 if those have locks controlled
475 * by the second status register.
476 */
477
478int spi_prettyprint_status_register_amic_a25l032(struct flashctx *flash)
479{
480 uint8_t status = spi_read_status_register(flash);
481 spi_prettyprint_status_register_hex(status);
482
483 spi_prettyprint_status_register_srwd(status);
484 msg_cdbg("Chip status register: Sector Protect Size (SEC) is %i KB\n", (status & (1 << 6)) ? 4 : 64);
485 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
486 spi_prettyprint_status_register_bp(status, 2);
487 spi_prettyprint_status_register_welwip(status);
488 msg_cdbg("Chip status register 2 is NOT decoded!\n");
489 return 0;
490}
491
492/* === Atmel === */
493
494static void spi_prettyprint_status_register_atmel_at25_wpen(uint8_t status)
495{
496 msg_cdbg("Chip status register: Write Protect Enable (WPEN) is %sset\n",
497 (status & (1 << 7)) ? "" : "not ");
498}
499
500static void spi_prettyprint_status_register_atmel_at25_srpl(uint8_t status)
501{
502 msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) is %sset\n",
503 (status & (1 << 7)) ? "" : "not ");
504}
505
506static void spi_prettyprint_status_register_atmel_at25_epewpp(uint8_t status)
507{
508 msg_cdbg("Chip status register: Erase/Program Error (EPE) is %sset\n",
509 (status & (1 << 5)) ? "" : "not ");
510 msg_cdbg("Chip status register: WP# pin (WPP) is %sasserted\n",
511 (status & (1 << 4)) ? "not " : "");
512}
513
514static void spi_prettyprint_status_register_atmel_at25_swp(uint8_t status)
515{
516 msg_cdbg("Chip status register: Software Protection Status (SWP): ");
517 switch (status & (3 << 2)) {
518 case 0x0 << 2:
519 msg_cdbg("no sectors are protected\n");
520 break;
521 case 0x1 << 2:
522 msg_cdbg("some sectors are protected\n");
523 /* FIXME: Read individual Sector Protection Registers. */
524 break;
525 case 0x3 << 2:
526 msg_cdbg("all sectors are protected\n");
527 break;
528 default:
529 msg_cdbg("reserved for future use\n");
530 break;
531 }
532}
533
534int spi_prettyprint_status_register_at25df(struct flashctx *flash)
535{
536 uint8_t status = spi_read_status_register(flash);
537 spi_prettyprint_status_register_hex(status);
538
539 spi_prettyprint_status_register_atmel_at25_srpl(status);
540 spi_prettyprint_status_register_bit(status, 6);
541 spi_prettyprint_status_register_atmel_at25_epewpp(status);
542 spi_prettyprint_status_register_atmel_at25_swp(status);
543 spi_prettyprint_status_register_welwip(status);
544 return 0;
545}
546
547int spi_prettyprint_status_register_at25df_sec(struct flashctx *flash)
548{
549 /* FIXME: We should check the security lockdown. */
550 msg_cdbg("Ignoring security lockdown (if present)\n");
551 msg_cdbg("Ignoring status register byte 2\n");
552 return spi_prettyprint_status_register_at25df(flash);
553}
554
Stefan Tauner57794ac2012-12-29 15:04:20 +0000555/* used for AT25F512, AT25F1024(A), AT25F2048 */
556int spi_prettyprint_status_register_at25f(struct flashctx *flash)
557{
558 uint8_t status;
559
560 status = spi_read_status_register(flash);
561 spi_prettyprint_status_register_hex(status);
562
563 spi_prettyprint_status_register_atmel_at25_wpen(status);
564 spi_prettyprint_status_register_bit(status, 6);
565 spi_prettyprint_status_register_bit(status, 5);
566 spi_prettyprint_status_register_bit(status, 4);
567 spi_prettyprint_status_register_bp(status, 1);
568 spi_prettyprint_status_register_welwip(status);
569 return 0;
570}
571
572int spi_prettyprint_status_register_at25f512a(struct flashctx *flash)
573{
574 uint8_t status;
575
576 status = spi_read_status_register(flash);
577 spi_prettyprint_status_register_hex(status);
578
579 spi_prettyprint_status_register_atmel_at25_wpen(status);
580 spi_prettyprint_status_register_bit(status, 6);
581 spi_prettyprint_status_register_bit(status, 5);
582 spi_prettyprint_status_register_bit(status, 4);
583 spi_prettyprint_status_register_bit(status, 3);
584 spi_prettyprint_status_register_bp(status, 0);
585 spi_prettyprint_status_register_welwip(status);
586 return 0;
587}
588
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000589int spi_prettyprint_status_register_at25f512b(struct flashctx *flash)
590{
591 uint8_t status = spi_read_status_register(flash);
592 spi_prettyprint_status_register_hex(status);
593
594 spi_prettyprint_status_register_atmel_at25_srpl(status);
595 spi_prettyprint_status_register_bit(status, 6);
596 spi_prettyprint_status_register_atmel_at25_epewpp(status);
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 Tauner57794ac2012-12-29 15:04:20 +0000603int spi_prettyprint_status_register_at25f4096(struct flashctx *flash)
604{
605 uint8_t status;
606
607 status = spi_read_status_register(flash);
608 spi_prettyprint_status_register_hex(status);
609
610 spi_prettyprint_status_register_atmel_at25_wpen(status);
611 spi_prettyprint_status_register_bit(status, 6);
612 spi_prettyprint_status_register_bit(status, 5);
613 spi_prettyprint_status_register_bp(status, 2);
614 spi_prettyprint_status_register_welwip(status);
615 return 0;
616}
617
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000618int spi_prettyprint_status_register_at25fs010(struct flashctx *flash)
619{
620 uint8_t status = spi_read_status_register(flash);
621 spi_prettyprint_status_register_hex(status);
622
623 spi_prettyprint_status_register_atmel_at25_wpen(status);
624 msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is "
625 "%sset\n", (status & (1 << 6)) ? "" : "not ");
626 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
627 "%sset\n", (status & (1 << 5)) ? "" : "not ");
628 spi_prettyprint_status_register_bit(status, 4);
629 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
630 "%sset\n", (status & (1 << 3)) ? "" : "not ");
631 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
632 "%sset\n", (status & (1 << 2)) ? "" : "not ");
633 /* FIXME: Pretty-print detailed sector protection status. */
634 spi_prettyprint_status_register_welwip(status);
635 return 0;
636}
637
638int spi_prettyprint_status_register_at25fs040(struct flashctx *flash)
639{
640 uint8_t status = spi_read_status_register(flash);
641 spi_prettyprint_status_register_hex(status);
642
643 spi_prettyprint_status_register_atmel_at25_wpen(status);
644 spi_prettyprint_status_register_bp(status, 4);
645 /* FIXME: Pretty-print detailed sector protection status. */
646 spi_prettyprint_status_register_welwip(status);
647 return 0;
648}
649
650int spi_prettyprint_status_register_at26df081a(struct flashctx *flash)
651{
652 uint8_t status = spi_read_status_register(flash);
653 spi_prettyprint_status_register_hex(status);
654
655 spi_prettyprint_status_register_atmel_at25_srpl(status);
656 msg_cdbg("Chip status register: Sequential Program Mode Status (SPM) is %sset\n",
657 (status & (1 << 6)) ? "" : "not ");
658 spi_prettyprint_status_register_atmel_at25_epewpp(status);
659 spi_prettyprint_status_register_atmel_at25_swp(status);
660 spi_prettyprint_status_register_welwip(status);
661 return 0;
662}
663
Stefan Taunercecb2c52013-06-20 22:55:41 +0000664/* Some Atmel DataFlash chips support per sector protection bits and the write protection bits in the status
665 * register do indicate if none, some or all sectors are protected. It is possible to globally (un)lock all
666 * sectors at once by writing 0 not only the protection bits (2 and 3) but also completely unrelated bits (4 and
667 * 5) which normally are not touched.
668 * Affected are all known Atmel chips matched by AT2[56]D[FLQ]..1A? but the AT26DF041. */
669int spi_disable_blockprotect_at2x_global_unprotect(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000670{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000671 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 1 << 4, 0x00);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000672}
673
Stefan Taunercecb2c52013-06-20 22:55:41 +0000674int spi_disable_blockprotect_at2x_global_unprotect_sec(struct flashctx *flash)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000675{
676 /* FIXME: We should check the security lockdown. */
677 msg_cinfo("Ignoring security lockdown (if present)\n");
Stefan Taunercecb2c52013-06-20 22:55:41 +0000678 return spi_disable_blockprotect_at2x_global_unprotect(flash);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000679}
680
Stefan Tauner57794ac2012-12-29 15:04:20 +0000681int spi_disable_blockprotect_at25f(struct flashctx *flash)
682{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000683 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000684}
685
686int spi_disable_blockprotect_at25f512a(struct flashctx *flash)
687{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000688 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 0, 0xFF);
Stefan Tauner57794ac2012-12-29 15:04:20 +0000689}
690
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000691int spi_disable_blockprotect_at25f512b(struct flashctx *flash)
692{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000693 return spi_disable_blockprotect_generic(flash, 0x04, 1 << 7, 1 << 4, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000694}
695
696int spi_disable_blockprotect_at25fs010(struct flashctx *flash)
697{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000698 return spi_disable_blockprotect_generic(flash, 0x6C, 1 << 7, 0, 0xFF);
Stefan Tauner9530a022012-12-29 15:04:05 +0000699 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000700
701int spi_disable_blockprotect_at25fs040(struct flashctx *flash)
702{
Stefan Taunercecb2c52013-06-20 22:55:41 +0000703 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0, 0xFF);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000704}
705
Nikolay Nikolaevd0e3ea12013-06-28 21:29:08 +0000706/* === Eon === */
707
708int spi_prettyprint_status_register_en25s_wp(struct flashctx *flash)
709{
710 uint8_t status = spi_read_status_register(flash);
711 spi_prettyprint_status_register_hex(status);
712
713 spi_prettyprint_status_register_srwd(status);
714 msg_cdbg("Chip status register: WP# disable (WPDIS) is %sabled\n", (status & (1 << 6)) ? "en " : "dis");
715 spi_prettyprint_status_register_bp(status, 3);
716 spi_prettyprint_status_register_welwip(status);
717 return 0;
718}
719
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000720/* === Intel/Numonyx/Micron - Spansion === */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000721
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000722int spi_disable_blockprotect_n25q(struct flashctx *flash)
723{
724 return spi_disable_blockprotect_generic(flash, 0x5C, 1 << 7, 0, 0xFF);
725}
726
727int spi_prettyprint_status_register_n25q(struct flashctx *flash)
728{
729 uint8_t status = spi_read_status_register(flash);
730 spi_prettyprint_status_register_hex(status);
731
732 spi_prettyprint_status_register_srwd(status);
733 if (flash->chip->total_size <= 32 / 8 * 1024) /* N25Q16 and N25Q32: reserved */
734 spi_prettyprint_status_register_bit(status, 6);
735 else
736 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
737 (status & (1 << 6)) ? "" : "not ");
738 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
739 spi_prettyprint_status_register_bp(status, 2);
740 spi_prettyprint_status_register_welwip(status);
741 return 0;
742}
743
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000744/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000745/* TODO: Clear P_FAIL and E_FAIL with Clear SR Fail Flags Command (30h) here? */
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000746int spi_disable_blockprotect_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000747{
Stefan Tauner278ba6e2013-06-28 21:28:27 +0000748 return spi_disable_blockprotect_bp2_srwd(flash);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000749}
750
Nikolay Nikolaevc80c4a32013-06-28 21:29:44 +0000751/* Used by Intel/Numonyx S33 and Spansion S25FL-S chips */
752int spi_prettyprint_status_register_bp2_ep_srwd(struct flashctx *flash)
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000753{
754 uint8_t status = spi_read_status_register(flash);
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000755 spi_prettyprint_status_register_hex(status);
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000756
757 spi_prettyprint_status_register_srwd(status);
758 msg_cdbg("Chip status register: Program Fail Flag (P_FAIL) is %sset\n",
759 (status & (1 << 6)) ? "" : "not ");
760 msg_cdbg("Chip status register: Erase Fail Flag (E_FAIL) is %sset\n",
761 (status & (1 << 5)) ? "" : "not ");
762 spi_prettyprint_status_register_bp(status, 2);
763 spi_prettyprint_status_register_welwip(status);
764 return 0;
765}
766
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000767/* === SST === */
768
769static void spi_prettyprint_status_register_sst25_common(uint8_t status)
770{
771 spi_prettyprint_status_register_hex(status);
772
773 spi_prettyprint_status_register_bpl(status);
774 msg_cdbg("Chip status register: Auto Address Increment Programming (AAI) is %sset\n",
775 (status & (1 << 6)) ? "" : "not ");
776 spi_prettyprint_status_register_bp(status, 3);
777 spi_prettyprint_status_register_welwip(status);
778}
779
780int spi_prettyprint_status_register_sst25(struct flashctx *flash)
781{
782 uint8_t status = spi_read_status_register(flash);
783 spi_prettyprint_status_register_sst25_common(status);
784 return 0;
785}
786
787int spi_prettyprint_status_register_sst25vf016(struct flashctx *flash)
788{
789 static const char *const bpt[] = {
790 "none",
791 "1F0000H-1FFFFFH",
792 "1E0000H-1FFFFFH",
793 "1C0000H-1FFFFFH",
794 "180000H-1FFFFFH",
795 "100000H-1FFFFFH",
796 "all", "all"
797 };
798 uint8_t status = spi_read_status_register(flash);
799 spi_prettyprint_status_register_sst25_common(status);
800 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
801 return 0;
802}
803
804int spi_prettyprint_status_register_sst25vf040b(struct flashctx *flash)
805{
806 static const char *const bpt[] = {
807 "none",
808 "0x70000-0x7ffff",
809 "0x60000-0x7ffff",
810 "0x40000-0x7ffff",
811 "all blocks", "all blocks", "all blocks", "all blocks"
812 };
813 uint8_t status = spi_read_status_register(flash);
814 spi_prettyprint_status_register_sst25_common(status);
815 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
816 return 0;
817}