blob: 2e01c06d6504a80c6487c4653229b11050e1db23 [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.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include "flash.h"
25#include "chipdrivers.h"
26#include "spi.h"
27
28/* === Generic functions === */
29int spi_write_status_enable(struct flashctx *flash)
30{
31 static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
32 int result;
33
34 /* Send EWSR (Enable Write Status Register). */
35 result = spi_send_command(flash, sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
36
37 if (result)
38 msg_cerr("%s failed\n", __func__);
39
40 return result;
41}
42
43static int spi_write_status_register_flag(struct flashctx *flash, int status, const unsigned char enable_opcode)
44{
45 int result;
46 int i = 0;
47 /*
48 * WRSR requires either EWSR or WREN depending on chip type.
49 * The code below relies on the fact hat EWSR and WREN have the same
50 * INSIZE and OUTSIZE.
51 */
52 struct spi_command cmds[] = {
53 {
54 .writecnt = JEDEC_WREN_OUTSIZE,
55 .writearr = (const unsigned char[]){ enable_opcode },
56 .readcnt = 0,
57 .readarr = NULL,
58 }, {
59 .writecnt = JEDEC_WRSR_OUTSIZE,
60 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
61 .readcnt = 0,
62 .readarr = NULL,
63 }, {
64 .writecnt = 0,
65 .writearr = NULL,
66 .readcnt = 0,
67 .readarr = NULL,
68 }};
69
70 result = spi_send_multicommand(flash, cmds);
71 if (result) {
72 msg_cerr("%s failed during command execution\n", __func__);
73 /* No point in waiting for the command to complete if execution
74 * failed.
75 */
76 return result;
77 }
78 /* WRSR performs a self-timed erase before the changes take effect.
79 * This may take 50-85 ms in most cases, and some chips apparently
80 * allow running RDSR only once. Therefore pick an initial delay of
81 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
82 */
83 programmer_delay(100 * 1000);
84 while (spi_read_status_register(flash) & SPI_SR_WIP) {
85 if (++i > 490) {
86 msg_cerr("Error: WIP bit after WRSR never cleared\n");
87 return TIMEOUT_ERROR;
88 }
89 programmer_delay(10 * 1000);
90 }
91 return 0;
92}
93
94int spi_write_status_register(struct flashctx *flash, int status)
95{
96 int feature_bits = flash->chip->feature_bits;
97 int ret = 1;
98
99 if (!(feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
100 msg_cdbg("Missing status register write definition, assuming "
101 "EWSR is needed\n");
102 feature_bits |= FEATURE_WRSR_EWSR;
103 }
104 if (feature_bits & FEATURE_WRSR_WREN)
105 ret = spi_write_status_register_flag(flash, status, JEDEC_WREN);
106 if (ret && (feature_bits & FEATURE_WRSR_EWSR))
107 ret = spi_write_status_register_flag(flash, status, JEDEC_EWSR);
108 return ret;
109}
110
111uint8_t spi_read_status_register(struct flashctx *flash)
112{
113 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
114 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
115 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
116 int ret;
117
118 /* Read Status Register */
119 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
120 if (ret)
121 msg_cerr("RDSR failed!\n");
122
123 return readarr[0];
124}
125
Stefan Tauner9530a022012-12-29 15:04:05 +0000126/* A generic block protection disable.
127 * Tests if a protection is enabled with the block protection mask (bp_mask) and returns success otherwise.
128 * Tests if the register bits are locked with the lock_mask (lock_mask).
129 * Tests if a hardware protection is active (i.e. low) with the write protection mask (wp_mask) and bails out
130 * in that case.
131 * Finally tries to disable engaged protections and checks if any locks are still set.
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000132 */
Stefan Tauner9530a022012-12-29 15:04:05 +0000133static int spi_disable_blockprotect_generic(struct flashctx *flash, uint8_t bp_mask, uint8_t lock_mask, uint8_t wp_mask)
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000134{
135 uint8_t status;
136 int result;
137
138 status = spi_read_status_register(flash);
Stefan Tauner9530a022012-12-29 15:04:05 +0000139 if ((status & bp_mask) == 0) {
140 msg_cdbg2("Block protection is disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000141 return 0;
Stefan Tauner9530a022012-12-29 15:04:05 +0000142 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000143
144 msg_cdbg("Some block protection in effect, disabling... ");
Stefan Tauner9530a022012-12-29 15:04:05 +0000145 if ((status & lock_mask) != 0) {
146 msg_cdbg("\n\tNeed to disable the register lock first... ");
147 if (wp_mask != 0 && (status & wp_mask) == 0) {
148 msg_cerr("Hardware protection is active, disabling write protection is impossible.\n");
149 return 1;
150 }
151 /* All bits except the register lock bit (often called SPRL, SRWD, WPEN) are readonly. */
152 result = spi_write_status_register(flash, status & ~lock_mask);
153 if (result) {
154 msg_cerr("spi_write_status_register failed.\n");
155 return result;
156 }
157 msg_cdbg("done.\n");
158 }
159 /* Global unprotect. Make sure to mask the register lock bit as well. */
160 result = spi_write_status_register(flash, status & ~(bp_mask | lock_mask));
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000161 if (result) {
162 msg_cerr("spi_write_status_register failed.\n");
163 return result;
164 }
165 status = spi_read_status_register(flash);
Stefan Tauner9530a022012-12-29 15:04:05 +0000166 if ((status & bp_mask) != 0) {
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000167 msg_cerr("Block protection could not be disabled!\n");
168 return 1;
169 }
Stefan Tauner9530a022012-12-29 15:04:05 +0000170 msg_cdbg("disabled.\n");
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000171 return 0;
172}
173
Stefan Tauner9530a022012-12-29 15:04:05 +0000174/* A common block protection disable that tries to unset the status register bits masked by 0x3C. */
175int spi_disable_blockprotect(struct flashctx *flash)
176{
177 return spi_disable_blockprotect_generic(flash, 0x3C, 0, 0);
178}
179
180
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000181static void spi_prettyprint_status_register_hex(uint8_t status)
182{
183 msg_cdbg("Chip status register is 0x%02x.\n", status);
184}
185
186/* Common highest bit: Status Register Write Disable (SRWD). */
187static void spi_prettyprint_status_register_srwd(uint8_t status)
188{
189 msg_cdbg("Chip status register: Status Register Write Disable (SRWD) is %sset\n",
190 (status & (1 << 7)) ? "" : "not ");
191}
192
193/* Common highest bit: Block Protect Write Disable (BPL). */
194static void spi_prettyprint_status_register_bpl(uint8_t status)
195{
196 msg_cdbg("Chip status register: Block Protect Write Disable (BPL) is %sset\n",
197 (status & (1 << 7)) ? "" : "not ");
198}
199
200/* Common lowest 2 bits: WEL and WIP. */
201static void spi_prettyprint_status_register_welwip(uint8_t status)
202{
203 msg_cdbg("Chip status register: Write Enable Latch (WEL) is %sset\n",
204 (status & (1 << 1)) ? "" : "not ");
205 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is %sset\n",
206 (status & (1 << 0)) ? "" : "not ");
207}
208
209/* Common block protection (BP) bits. */
210static void spi_prettyprint_status_register_bp(uint8_t status, int bp)
211{
212 switch (bp) {
213 /* Fall through. */
214 case 4:
215 msg_cdbg("Chip status register: Block Protect 4 (BP4) is %sset\n",
216 (status & (1 << 5)) ? "" : "not ");
217 case 3:
218 msg_cdbg("Chip status register: Block Protect 3 (BP3) is %sset\n",
219 (status & (1 << 5)) ? "" : "not ");
220 case 2:
221 msg_cdbg("Chip status register: Block Protect 2 (BP2) is %sset\n",
222 (status & (1 << 4)) ? "" : "not ");
223 case 1:
224 msg_cdbg("Chip status register: Block Protect 1 (BP1) is %sset\n",
225 (status & (1 << 3)) ? "" : "not ");
226 case 0:
227 msg_cdbg("Chip status register: Block Protect 0 (BP0) is %sset\n",
228 (status & (1 << 2)) ? "" : "not ");
229 }
230}
231
232/* Unnamed bits. */
233static void spi_prettyprint_status_register_bit(uint8_t status, int bit)
234{
235 msg_cdbg("Chip status register: Bit %i is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
236}
237
238int spi_prettyprint_status_register_plain(struct flashctx *flash)
239{
240 uint8_t status = spi_read_status_register(flash);
241 spi_prettyprint_status_register_hex(status);
242 return 0;
243}
244
245/* Works for many chips of the
246 * AMIC A25L series
247 * and MX MX25L512
248 */
249int spi_prettyprint_status_register_default_bp1(struct flashctx *flash)
250{
251 uint8_t status = spi_read_status_register(flash);
252 spi_prettyprint_status_register_hex(status);
253
254 spi_prettyprint_status_register_srwd(status);
255 spi_prettyprint_status_register_bit(status, 6);
256 spi_prettyprint_status_register_bit(status, 5);
257 spi_prettyprint_status_register_bit(status, 4);
258 spi_prettyprint_status_register_bp(status, 1);
259 spi_prettyprint_status_register_welwip(status);
260 return 0;
261}
262
263/* Works for many chips of the
264 * AMIC A25L series
265 */
266int spi_prettyprint_status_register_default_bp2(struct flashctx *flash)
267{
268 uint8_t status = spi_read_status_register(flash);
269 spi_prettyprint_status_register_hex(status);
270
271 spi_prettyprint_status_register_srwd(status);
272 spi_prettyprint_status_register_bit(status, 6);
273 spi_prettyprint_status_register_bit(status, 5);
274 spi_prettyprint_status_register_bp(status, 2);
275 spi_prettyprint_status_register_welwip(status);
276 return 0;
277}
278
279/* Works for many chips of the
280 * ST M25P series
281 * MX MX25L series
282 */
283int spi_prettyprint_status_register_default_bp3(struct flashctx *flash)
284{
285 uint8_t status = spi_read_status_register(flash);
286 spi_prettyprint_status_register_hex(status);
287
288 spi_prettyprint_status_register_srwd(status);
289 spi_prettyprint_status_register_bit(status, 6);
290 spi_prettyprint_status_register_bp(status, 3);
291 spi_prettyprint_status_register_welwip(status);
292 return 0;
293}
294
295/* === Amic ===
296 * FIXME: spi_disable_blockprotect is incorrect but works fine for chips using
297 * spi_prettyprint_status_register_default_bp1 or
298 * spi_prettyprint_status_register_default_bp2.
299 * FIXME: spi_disable_blockprotect is incorrect and will fail for chips using
300 * spi_prettyprint_status_register_amic_a25l032 if those have locks controlled
301 * by the second status register.
302 */
303
304int spi_prettyprint_status_register_amic_a25l032(struct flashctx *flash)
305{
306 uint8_t status = spi_read_status_register(flash);
307 spi_prettyprint_status_register_hex(status);
308
309 spi_prettyprint_status_register_srwd(status);
310 msg_cdbg("Chip status register: Sector Protect Size (SEC) is %i KB\n", (status & (1 << 6)) ? 4 : 64);
311 msg_cdbg("Chip status register: Top/Bottom (TB) is %s\n", (status & (1 << 5)) ? "bottom" : "top");
312 spi_prettyprint_status_register_bp(status, 2);
313 spi_prettyprint_status_register_welwip(status);
314 msg_cdbg("Chip status register 2 is NOT decoded!\n");
315 return 0;
316}
317
318/* === Atmel === */
319
320static void spi_prettyprint_status_register_atmel_at25_wpen(uint8_t status)
321{
322 msg_cdbg("Chip status register: Write Protect Enable (WPEN) is %sset\n",
323 (status & (1 << 7)) ? "" : "not ");
324}
325
326static void spi_prettyprint_status_register_atmel_at25_srpl(uint8_t status)
327{
328 msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) is %sset\n",
329 (status & (1 << 7)) ? "" : "not ");
330}
331
332static void spi_prettyprint_status_register_atmel_at25_epewpp(uint8_t status)
333{
334 msg_cdbg("Chip status register: Erase/Program Error (EPE) is %sset\n",
335 (status & (1 << 5)) ? "" : "not ");
336 msg_cdbg("Chip status register: WP# pin (WPP) is %sasserted\n",
337 (status & (1 << 4)) ? "not " : "");
338}
339
340static void spi_prettyprint_status_register_atmel_at25_swp(uint8_t status)
341{
342 msg_cdbg("Chip status register: Software Protection Status (SWP): ");
343 switch (status & (3 << 2)) {
344 case 0x0 << 2:
345 msg_cdbg("no sectors are protected\n");
346 break;
347 case 0x1 << 2:
348 msg_cdbg("some sectors are protected\n");
349 /* FIXME: Read individual Sector Protection Registers. */
350 break;
351 case 0x3 << 2:
352 msg_cdbg("all sectors are protected\n");
353 break;
354 default:
355 msg_cdbg("reserved for future use\n");
356 break;
357 }
358}
359
360int spi_prettyprint_status_register_at25df(struct flashctx *flash)
361{
362 uint8_t status = spi_read_status_register(flash);
363 spi_prettyprint_status_register_hex(status);
364
365 spi_prettyprint_status_register_atmel_at25_srpl(status);
366 spi_prettyprint_status_register_bit(status, 6);
367 spi_prettyprint_status_register_atmel_at25_epewpp(status);
368 spi_prettyprint_status_register_atmel_at25_swp(status);
369 spi_prettyprint_status_register_welwip(status);
370 return 0;
371}
372
373int spi_prettyprint_status_register_at25df_sec(struct flashctx *flash)
374{
375 /* FIXME: We should check the security lockdown. */
376 msg_cdbg("Ignoring security lockdown (if present)\n");
377 msg_cdbg("Ignoring status register byte 2\n");
378 return spi_prettyprint_status_register_at25df(flash);
379}
380
381int spi_prettyprint_status_register_at25f512b(struct flashctx *flash)
382{
383 uint8_t status = spi_read_status_register(flash);
384 spi_prettyprint_status_register_hex(status);
385
386 spi_prettyprint_status_register_atmel_at25_srpl(status);
387 spi_prettyprint_status_register_bit(status, 6);
388 spi_prettyprint_status_register_atmel_at25_epewpp(status);
389 spi_prettyprint_status_register_bit(status, 3);
390 spi_prettyprint_status_register_bp(status, 0);
391 spi_prettyprint_status_register_welwip(status);
392 return 0;
393}
394
395int spi_prettyprint_status_register_at25fs010(struct flashctx *flash)
396{
397 uint8_t status = spi_read_status_register(flash);
398 spi_prettyprint_status_register_hex(status);
399
400 spi_prettyprint_status_register_atmel_at25_wpen(status);
401 msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is "
402 "%sset\n", (status & (1 << 6)) ? "" : "not ");
403 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
404 "%sset\n", (status & (1 << 5)) ? "" : "not ");
405 spi_prettyprint_status_register_bit(status, 4);
406 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
407 "%sset\n", (status & (1 << 3)) ? "" : "not ");
408 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
409 "%sset\n", (status & (1 << 2)) ? "" : "not ");
410 /* FIXME: Pretty-print detailed sector protection status. */
411 spi_prettyprint_status_register_welwip(status);
412 return 0;
413}
414
415int spi_prettyprint_status_register_at25fs040(struct flashctx *flash)
416{
417 uint8_t status = spi_read_status_register(flash);
418 spi_prettyprint_status_register_hex(status);
419
420 spi_prettyprint_status_register_atmel_at25_wpen(status);
421 spi_prettyprint_status_register_bp(status, 4);
422 /* FIXME: Pretty-print detailed sector protection status. */
423 spi_prettyprint_status_register_welwip(status);
424 return 0;
425}
426
427int spi_prettyprint_status_register_at26df081a(struct flashctx *flash)
428{
429 uint8_t status = spi_read_status_register(flash);
430 spi_prettyprint_status_register_hex(status);
431
432 spi_prettyprint_status_register_atmel_at25_srpl(status);
433 msg_cdbg("Chip status register: Sequential Program Mode Status (SPM) is %sset\n",
434 (status & (1 << 6)) ? "" : "not ");
435 spi_prettyprint_status_register_atmel_at25_epewpp(status);
436 spi_prettyprint_status_register_atmel_at25_swp(status);
437 spi_prettyprint_status_register_welwip(status);
438 return 0;
439}
440
441int spi_disable_blockprotect_at25df(struct flashctx *flash)
442{
Stefan Tauner9530a022012-12-29 15:04:05 +0000443 return spi_disable_blockprotect_generic(flash, 0x0C, 1 << 7, 1 << 4);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000444}
445
446int spi_disable_blockprotect_at25df_sec(struct flashctx *flash)
447{
448 /* FIXME: We should check the security lockdown. */
449 msg_cinfo("Ignoring security lockdown (if present)\n");
450 return spi_disable_blockprotect_at25df(flash);
451}
452
453int spi_disable_blockprotect_at25f512b(struct flashctx *flash)
454{
455 /* spi_disable_blockprotect_at25df is not really the right way to do
456 * this, but the side effects of said function work here as well.
457 */
458 return spi_disable_blockprotect_at25df(flash);
459}
460
461int spi_disable_blockprotect_at25fs010(struct flashctx *flash)
462{
Stefan Tauner9530a022012-12-29 15:04:05 +0000463 return spi_disable_blockprotect_generic(flash, 0x6C, 1 << 7, 0);
464 }
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000465
466int spi_disable_blockprotect_at25fs040(struct flashctx *flash)
467{
Stefan Tauner9530a022012-12-29 15:04:05 +0000468 return spi_disable_blockprotect_generic(flash, 0x7C, 1 << 7, 0);
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000469}
470
Stefan Tauner54aaa4a2012-12-29 15:04:12 +0000471/* === Intel === */
472
473/* TODO: Clear P_FAIL and E_FAIL with Clear SR Fail Flags Command (30h) here? */
474int spi_disable_blockprotect_s33(struct flashctx *flash)
475{
476 return spi_disable_blockprotect_generic(flash, 0x1C, 1 << 7, 0);
477}
478
479int spi_prettyprint_status_register_s33(struct flashctx *flash)
480{
481 uint8_t status = spi_read_status_register(flash);
482 msg_cdbg("Chip status register is %02x\n", status);
483
484 spi_prettyprint_status_register_srwd(status);
485 msg_cdbg("Chip status register: Program Fail Flag (P_FAIL) is %sset\n",
486 (status & (1 << 6)) ? "" : "not ");
487 msg_cdbg("Chip status register: Erase Fail Flag (E_FAIL) is %sset\n",
488 (status & (1 << 5)) ? "" : "not ");
489 spi_prettyprint_status_register_bp(status, 2);
490 spi_prettyprint_status_register_welwip(status);
491 return 0;
492}
493
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000494/* === SST === */
495
496static void spi_prettyprint_status_register_sst25_common(uint8_t status)
497{
498 spi_prettyprint_status_register_hex(status);
499
500 spi_prettyprint_status_register_bpl(status);
501 msg_cdbg("Chip status register: Auto Address Increment Programming (AAI) is %sset\n",
502 (status & (1 << 6)) ? "" : "not ");
503 spi_prettyprint_status_register_bp(status, 3);
504 spi_prettyprint_status_register_welwip(status);
505}
506
507int spi_prettyprint_status_register_sst25(struct flashctx *flash)
508{
509 uint8_t status = spi_read_status_register(flash);
510 spi_prettyprint_status_register_sst25_common(status);
511 return 0;
512}
513
514int spi_prettyprint_status_register_sst25vf016(struct flashctx *flash)
515{
516 static const char *const bpt[] = {
517 "none",
518 "1F0000H-1FFFFFH",
519 "1E0000H-1FFFFFH",
520 "1C0000H-1FFFFFH",
521 "180000H-1FFFFFH",
522 "100000H-1FFFFFH",
523 "all", "all"
524 };
525 uint8_t status = spi_read_status_register(flash);
526 spi_prettyprint_status_register_sst25_common(status);
527 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
528 return 0;
529}
530
531int spi_prettyprint_status_register_sst25vf040b(struct flashctx *flash)
532{
533 static const char *const bpt[] = {
534 "none",
535 "0x70000-0x7ffff",
536 "0x60000-0x7ffff",
537 "0x40000-0x7ffff",
538 "all blocks", "all blocks", "all blocks", "all blocks"
539 };
540 uint8_t status = spi_read_status_register(flash);
541 spi_prettyprint_status_register_sst25_common(status);
542 msg_cdbg("Resulting block protection : %s\n", bpt[(status & 0x1c) >> 2]);
543 return 0;
544}