blob: 9dcf4edc5b83f4a33624f503fc10883fc1a8d7e8 [file] [log] [blame]
snelson8913d082010-02-26 05:48:29 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger39d159a2010-05-21 23:09:42 +00004 * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger
snelson8913d082010-02-26 05:48:29 +00005 * Copyright (C) 2008 coresystems GmbH
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * Contains the common SPI chip driver functions
23 */
24
25#include <string.h>
26#include "flash.h"
27#include "flashchips.h"
28#include "chipdrivers.h"
David Hendricks82fd8ae2010-08-04 14:34:54 -070029#include "programmer.h"
snelson8913d082010-02-26 05:48:29 +000030#include "spi.h"
31
32void spi_prettyprint_status_register(struct flashchip *flash);
33
34static int spi_rdid(unsigned char *readarr, int bytes)
35{
David Hendricks668f29d2011-01-27 18:51:45 -080036 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
snelson8913d082010-02-26 05:48:29 +000037 int ret;
38 int i;
39
40 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
41 if (ret)
42 return ret;
snelsonfc007bb2010-03-24 23:14:32 +000043 msg_cspew("RDID returned");
snelson8913d082010-02-26 05:48:29 +000044 for (i = 0; i < bytes; i++)
snelsonfc007bb2010-03-24 23:14:32 +000045 msg_cspew(" 0x%02x", readarr[i]);
46 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000047 return 0;
48}
49
50static int spi_rems(unsigned char *readarr)
51{
52 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
53 uint32_t readaddr;
54 int ret;
55
56 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
57 if (ret == SPI_INVALID_ADDRESS) {
58 /* Find the lowest even address allowed for reads. */
59 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
60 cmd[1] = (readaddr >> 16) & 0xff,
61 cmd[2] = (readaddr >> 8) & 0xff,
62 cmd[3] = (readaddr >> 0) & 0xff,
63 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
64 }
65 if (ret)
66 return ret;
snelsonfc007bb2010-03-24 23:14:32 +000067 msg_cspew("REMS returned %02x %02x. ", readarr[0], readarr[1]);
snelson8913d082010-02-26 05:48:29 +000068 return 0;
69}
70
hailfinger59a83572010-05-28 17:07:57 +000071static int spi_res(unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000072{
73 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
74 uint32_t readaddr;
75 int ret;
hailfingercb0564e2010-06-20 10:39:33 +000076 int i;
snelson8913d082010-02-26 05:48:29 +000077
hailfinger59a83572010-05-28 17:07:57 +000078 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000079 if (ret == SPI_INVALID_ADDRESS) {
80 /* Find the lowest even address allowed for reads. */
81 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
82 cmd[1] = (readaddr >> 16) & 0xff,
83 cmd[2] = (readaddr >> 8) & 0xff,
84 cmd[3] = (readaddr >> 0) & 0xff,
hailfinger59a83572010-05-28 17:07:57 +000085 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000086 }
87 if (ret)
88 return ret;
hailfingercb0564e2010-06-20 10:39:33 +000089 msg_cspew("RES returned");
90 for (i = 0; i < bytes; i++)
91 msg_cspew(" 0x%02x", readarr[i]);
92 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000093 return 0;
94}
95
96int spi_write_enable(void)
97{
David Hendricks668f29d2011-01-27 18:51:45 -080098 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
snelson8913d082010-02-26 05:48:29 +000099 int result;
100
101 /* Send WREN (Write Enable) */
102 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
103
104 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000105 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000106
107 return result;
108}
109
110int spi_write_disable(void)
111{
David Hendricks668f29d2011-01-27 18:51:45 -0800112 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
snelson8913d082010-02-26 05:48:29 +0000113
114 /* Send WRDI (Write Disable) */
115 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
116}
117
118static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
119{
120 unsigned char readarr[4];
121 uint32_t id1;
122 uint32_t id2;
123
124 if (spi_rdid(readarr, bytes))
125 return 0;
126
127 if (!oddparity(readarr[0]))
snelsonfc007bb2010-03-24 23:14:32 +0000128 msg_cdbg("RDID byte 0 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000129
hailfingercb0564e2010-06-20 10:39:33 +0000130 /* Check if this is a continuation vendor ID.
131 * FIXME: Handle continuation device IDs.
132 */
snelson8913d082010-02-26 05:48:29 +0000133 if (readarr[0] == 0x7f) {
134 if (!oddparity(readarr[1]))
snelsonfc007bb2010-03-24 23:14:32 +0000135 msg_cdbg("RDID byte 1 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000136 id1 = (readarr[0] << 8) | readarr[1];
137 id2 = readarr[2];
138 if (bytes > 3) {
139 id2 <<= 8;
140 id2 |= readarr[3];
141 }
142 } else {
143 id1 = readarr[0];
144 id2 = (readarr[1] << 8) | readarr[2];
145 }
146
snelsonfc007bb2010-03-24 23:14:32 +0000147 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000148
149 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
150 /* Print the status register to tell the
151 * user about possible write protection.
152 */
153 spi_prettyprint_status_register(flash);
154
155 return 1;
156 }
157
158 /* Test if this is a pure vendor match. */
159 if (id1 == flash->manufacture_id &&
160 GENERIC_DEVICE_ID == flash->model_id)
161 return 1;
162
163 /* Test if there is any vendor ID. */
164 if (GENERIC_MANUF_ID == flash->manufacture_id &&
165 id1 != 0xff)
166 return 1;
167
168 return 0;
169}
170
171int probe_spi_rdid(struct flashchip *flash)
172{
173 return probe_spi_rdid_generic(flash, 3);
174}
175
snelson8913d082010-02-26 05:48:29 +0000176int probe_spi_rdid4(struct flashchip *flash)
177{
hailfingercb0564e2010-06-20 10:39:33 +0000178 /* Some SPI controllers do not support commands with writecnt=1 and
179 * readcnt=4.
180 */
snelson8913d082010-02-26 05:48:29 +0000181 switch (spi_controller) {
hailfinger90c7d542010-05-31 15:27:27 +0000182#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000183#if defined(__i386__) || defined(__x86_64__)
hailfingercb0564e2010-06-20 10:39:33 +0000184 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +0000185 case SPI_CONTROLLER_WBSIO:
hailfingercb0564e2010-06-20 10:39:33 +0000186 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
187 return 0;
188 break;
snelson8913d082010-02-26 05:48:29 +0000189#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000190#endif
snelson8913d082010-02-26 05:48:29 +0000191 default:
hailfingercb0564e2010-06-20 10:39:33 +0000192 return probe_spi_rdid_generic(flash, 4);
snelson8913d082010-02-26 05:48:29 +0000193 }
194
195 return 0;
196}
197
198int probe_spi_rems(struct flashchip *flash)
199{
200 unsigned char readarr[JEDEC_REMS_INSIZE];
201 uint32_t id1, id2;
202
203 if (spi_rems(readarr))
204 return 0;
205
206 id1 = readarr[0];
207 id2 = readarr[1];
208
snelsonfc007bb2010-03-24 23:14:32 +0000209 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000210
211 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
212 /* Print the status register to tell the
213 * user about possible write protection.
214 */
215 spi_prettyprint_status_register(flash);
216
217 return 1;
218 }
219
220 /* Test if this is a pure vendor match. */
221 if (id1 == flash->manufacture_id &&
222 GENERIC_DEVICE_ID == flash->model_id)
223 return 1;
224
225 /* Test if there is any vendor ID. */
226 if (GENERIC_MANUF_ID == flash->manufacture_id &&
227 id1 != 0xff)
228 return 1;
229
230 return 0;
231}
232
hailfinger59a83572010-05-28 17:07:57 +0000233int probe_spi_res1(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +0000234{
David Hendricks668f29d2011-01-27 18:51:45 -0800235 static const unsigned char allff[] = {0xff, 0xff, 0xff};
236 static const unsigned char all00[] = {0x00, 0x00, 0x00};
snelson8913d082010-02-26 05:48:29 +0000237 unsigned char readarr[3];
238 uint32_t id2;
snelson8913d082010-02-26 05:48:29 +0000239
hailfinger59a83572010-05-28 17:07:57 +0000240 /* We only want one-byte RES if RDID and REMS are unusable. */
241
snelson8913d082010-02-26 05:48:29 +0000242 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
243 * 0x00 0x00 0x00. In that case, RES is pointless.
244 */
245 if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) &&
246 memcmp(readarr, all00, 3)) {
247 msg_cdbg("Ignoring RES in favour of RDID.\n");
248 return 0;
249 }
250 /* Check if REMS is usable and does not return 0xff 0xff or
251 * 0x00 0x00. In that case, RES is pointless.
252 */
253 if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
254 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
255 msg_cdbg("Ignoring RES in favour of REMS.\n");
256 return 0;
257 }
258
hailfinger59a83572010-05-28 17:07:57 +0000259 if (spi_res(readarr, 1))
snelson8913d082010-02-26 05:48:29 +0000260 return 0;
261
snelson8913d082010-02-26 05:48:29 +0000262 id2 = readarr[0];
hailfinger59a83572010-05-28 17:07:57 +0000263
snelsonfc007bb2010-03-24 23:14:32 +0000264 msg_cdbg("%s: id 0x%x\n", __func__, id2);
hailfinger59a83572010-05-28 17:07:57 +0000265
snelson8913d082010-02-26 05:48:29 +0000266 if (id2 != flash->model_id)
267 return 0;
268
269 /* Print the status register to tell the
270 * user about possible write protection.
271 */
272 spi_prettyprint_status_register(flash);
273 return 1;
274}
275
hailfinger59a83572010-05-28 17:07:57 +0000276int probe_spi_res2(struct flashchip *flash)
277{
278 unsigned char readarr[2];
279 uint32_t id1, id2;
280
281 if (spi_res(readarr, 2))
282 return 0;
283
284 id1 = readarr[0];
285 id2 = readarr[1];
286
287 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
288
289 if (id1 != flash->manufacture_id || id2 != flash->model_id)
290 return 0;
291
292 /* Print the status register to tell the
293 * user about possible write protection.
294 */
295 spi_prettyprint_status_register(flash);
296 return 1;
297}
298
snelson8913d082010-02-26 05:48:29 +0000299uint8_t spi_read_status_register(void)
300{
David Hendricks668f29d2011-01-27 18:51:45 -0800301 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
snelson8913d082010-02-26 05:48:29 +0000302 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
303 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
304 int ret;
305
306 /* Read Status Register */
307 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
308 if (ret)
snelsonfc007bb2010-03-24 23:14:32 +0000309 msg_cerr("RDSR failed!\n");
snelson8913d082010-02-26 05:48:29 +0000310
311 return readarr[0];
312}
313
314/* Prettyprint the status register. Common definitions. */
David Hendricks82fd8ae2010-08-04 14:34:54 -0700315static void spi_prettyprint_status_register_welwip(uint8_t status)
316{
317 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
318 "%sset\n", (status & (1 << 1)) ? "" : "not ");
319 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
320 "%sset\n", (status & (1 << 0)) ? "" : "not ");
321}
322
323/* Prettyprint the status register. Common definitions. */
324static void spi_prettyprint_status_register_common(uint8_t status)
snelson8913d082010-02-26 05:48:29 +0000325{
snelsonfc007bb2010-03-24 23:14:32 +0000326 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
snelson8913d082010-02-26 05:48:29 +0000327 "%sset\n", (status & (1 << 5)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000328 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
snelson8913d082010-02-26 05:48:29 +0000329 "%sset\n", (status & (1 << 4)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000330 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
snelson8913d082010-02-26 05:48:29 +0000331 "%sset\n", (status & (1 << 3)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000332 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
snelson8913d082010-02-26 05:48:29 +0000333 "%sset\n", (status & (1 << 2)) ? "" : "not ");
David Hendricks82fd8ae2010-08-04 14:34:54 -0700334 spi_prettyprint_status_register_welwip(status);
335}
336
337/* Prettyprint the status register. Works for
338 * AMIC A25L series
339 */
340void spi_prettyprint_status_register_amic_a25l(uint8_t status)
341{
342 msg_cdbg("Chip status register: Status Register Write Disable "
343 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
344 spi_prettyprint_status_register_common(status);
345}
346
347/* Prettyprint the status register. Common definitions. */
348static void spi_prettyprint_status_register_at25_srplepewpp(uint8_t status)
349{
350 msg_cdbg("Chip status register: Sector Protection Register Lock (SRPL) "
351 "is %sset\n", (status & (1 << 7)) ? "" : "not ");
352 msg_cdbg("Chip status register: Bit 6 "
353 "is %sset\n", (status & (1 << 6)) ? "" : "not ");
354 msg_cdbg("Chip status register: Erase/Program Error (EPE) "
355 "is %sset\n", (status & (1 << 5)) ? "" : "not ");
356 msg_cdbg("Chip status register: WP# pin (WPP) "
357 "is %sactive\n", (status & (1 << 4)) ? "not " : "");
358}
359
360int spi_prettyprint_status_register_at25df(struct flashchip *flash)
361{
362 uint8_t status;
363
364 status = spi_read_status_register();
365 msg_cdbg("Chip status register is %02x\n", status);
366
367 spi_prettyprint_status_register_at25_srplepewpp(status);
368 msg_cdbg("Chip status register: Software Protection Status (SWP): ");
369 switch (status & (3 << 2)) {
370 case 0x0 << 2:
371 msg_cdbg("no sectors are protected\n");
372 break;
373 case 0x1 << 2:
374 msg_cdbg("some sectors are protected\n");
375 /* FIXME: Read individual Sector Protection Registers. */
376 break;
377 case 0x3 << 2:
378 msg_cdbg("all sectors are protected\n");
379 break;
380 default:
381 msg_cdbg("reserved for future use\n");
382 break;
383 }
384 spi_prettyprint_status_register_welwip(status);
385 return 0;
386}
387
388int spi_prettyprint_status_register_at25df_sec(struct flashchip *flash)
389{
390 /* FIXME: We should check the security lockdown. */
391 msg_cdbg("Ignoring security lockdown (if present)\n");
392 msg_cdbg("Ignoring status register byte 2\n");
393 return spi_prettyprint_status_register_at25df(flash);
394}
395
396int spi_prettyprint_status_register_at25f(struct flashchip *flash)
397{
398 uint8_t status;
399
400 status = spi_read_status_register();
401 msg_cdbg("Chip status register is %02x\n", status);
402
403 spi_prettyprint_status_register_at25_srplepewpp(status);
404 msg_cdbg("Chip status register: Bit 3 "
405 "is %sset\n", (status & (1 << 3)) ? "" : "not ");
406 msg_cdbg("Chip status register: Block Protect 0 (BP0) is "
407 "%sset, %s sectors are protected\n",
408 (status & (1 << 2)) ? "" : "not ",
409 (status & (1 << 2)) ? "all" : "no");
410 spi_prettyprint_status_register_welwip(status);
411 return 0;
412}
413
414int spi_prettyprint_status_register_at25fs010(struct flashchip *flash)
415{
416 uint8_t status;
417
418 status = spi_read_status_register();
419 msg_cdbg("Chip status register is %02x\n", status);
420
421 msg_cdbg("Chip status register: Status Register Write Protect (WPEN) "
422 "is %sset\n", (status & (1 << 7)) ? "" : "not ");
423 msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is "
424 "%sset\n", (status & (1 << 6)) ? "" : "not ");
425 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
426 "%sset\n", (status & (1 << 5)) ? "" : "not ");
427 msg_cdbg("Chip status register: Bit 4 is "
428 "%sset\n", (status & (1 << 4)) ? "" : "not ");
429 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
430 "%sset\n", (status & (1 << 3)) ? "" : "not ");
431 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
432 "%sset\n", (status & (1 << 2)) ? "" : "not ");
433 /* FIXME: Pretty-print detailed sector protection status. */
434 spi_prettyprint_status_register_welwip(status);
435 return 0;
436}
437
438int spi_prettyprint_status_register_at25fs040(struct flashchip *flash)
439{
440 uint8_t status;
441
442 status = spi_read_status_register();
443 msg_cdbg("Chip status register is %02x\n", status);
444
445 msg_cdbg("Chip status register: Status Register Write Protect (WPEN) "
446 "is %sset\n", (status & (1 << 7)) ? "" : "not ");
447 msg_cdbg("Chip status register: Bit 6 / Block Protect 4 (BP4) is "
448 "%sset\n", (status & (1 << 6)) ? "" : "not ");
449 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
450 "%sset\n", (status & (1 << 5)) ? "" : "not ");
451 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
452 "%sset\n", (status & (1 << 4)) ? "" : "not ");
453 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
454 "%sset\n", (status & (1 << 3)) ? "" : "not ");
455 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
456 "%sset\n", (status & (1 << 2)) ? "" : "not ");
457 /* FIXME: Pretty-print detailed sector protection status. */
458 spi_prettyprint_status_register_welwip(status);
459 return 0;
snelson8913d082010-02-26 05:48:29 +0000460}
461
462/* Prettyprint the status register. Works for
463 * ST M25P series
464 * MX MX25L series
465 */
466void spi_prettyprint_status_register_st_m25p(uint8_t status)
467{
snelsonfc007bb2010-03-24 23:14:32 +0000468 msg_cdbg("Chip status register: Status Register Write Disable "
snelson8913d082010-02-26 05:48:29 +0000469 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000470 msg_cdbg("Chip status register: Bit 6 is "
snelson8913d082010-02-26 05:48:29 +0000471 "%sset\n", (status & (1 << 6)) ? "" : "not ");
472 spi_prettyprint_status_register_common(status);
473}
474
475void spi_prettyprint_status_register_sst25(uint8_t status)
476{
snelsonfc007bb2010-03-24 23:14:32 +0000477 msg_cdbg("Chip status register: Block Protect Write Disable "
snelson8913d082010-02-26 05:48:29 +0000478 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000479 msg_cdbg("Chip status register: Auto Address Increment Programming "
snelson8913d082010-02-26 05:48:29 +0000480 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
481 spi_prettyprint_status_register_common(status);
482}
483
484/* Prettyprint the status register. Works for
485 * SST 25VF016
486 */
487void spi_prettyprint_status_register_sst25vf016(uint8_t status)
488{
David Hendricks668f29d2011-01-27 18:51:45 -0800489 static const char *const bpt[] = {
snelson8913d082010-02-26 05:48:29 +0000490 "none",
491 "1F0000H-1FFFFFH",
492 "1E0000H-1FFFFFH",
493 "1C0000H-1FFFFFH",
494 "180000H-1FFFFFH",
495 "100000H-1FFFFFH",
496 "all", "all"
497 };
498 spi_prettyprint_status_register_sst25(status);
snelsonfc007bb2010-03-24 23:14:32 +0000499 msg_cdbg("Resulting block protection : %s\n",
snelson8913d082010-02-26 05:48:29 +0000500 bpt[(status & 0x1c) >> 2]);
501}
502
503void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
504{
David Hendricks668f29d2011-01-27 18:51:45 -0800505 static const char *const bpt[] = {
snelson8913d082010-02-26 05:48:29 +0000506 "none",
507 "0x70000-0x7ffff",
508 "0x60000-0x7ffff",
509 "0x40000-0x7ffff",
510 "all blocks", "all blocks", "all blocks", "all blocks"
511 };
512 spi_prettyprint_status_register_sst25(status);
snelsonfc007bb2010-03-24 23:14:32 +0000513 msg_cdbg("Resulting block protection : %s\n",
snelson8913d082010-02-26 05:48:29 +0000514 bpt[(status & 0x1c) >> 2]);
515}
516
517void spi_prettyprint_status_register(struct flashchip *flash)
518{
519 uint8_t status;
520
521 status = spi_read_status_register();
snelsonfc007bb2010-03-24 23:14:32 +0000522 msg_cdbg("Chip status register is %02x\n", status);
snelson8913d082010-02-26 05:48:29 +0000523 switch (flash->manufacture_id) {
David Hendricks82fd8ae2010-08-04 14:34:54 -0700524 case AMIC_ID:
525 if ((flash->model_id & 0xff00) == 0x2000)
526 spi_prettyprint_status_register_amic_a25l(status);
527 break;
snelson8913d082010-02-26 05:48:29 +0000528 case ST_ID:
529 if (((flash->model_id & 0xff00) == 0x2000) ||
530 ((flash->model_id & 0xff00) == 0x2500))
531 spi_prettyprint_status_register_st_m25p(status);
532 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800533 case MACRONIX_ID:
snelson8913d082010-02-26 05:48:29 +0000534 if ((flash->model_id & 0xff00) == 0x2000)
535 spi_prettyprint_status_register_st_m25p(status);
536 break;
537 case SST_ID:
538 switch (flash->model_id) {
539 case 0x2541:
540 spi_prettyprint_status_register_sst25vf016(status);
541 break;
542 case 0x8d:
543 case 0x258d:
544 spi_prettyprint_status_register_sst25vf040b(status);
545 break;
546 default:
547 spi_prettyprint_status_register_sst25(status);
548 break;
549 }
550 break;
551 }
552}
553
554int spi_chip_erase_60(struct flashchip *flash)
555{
556 int result;
557 struct spi_command cmds[] = {
558 {
559 .writecnt = JEDEC_WREN_OUTSIZE,
560 .writearr = (const unsigned char[]){ JEDEC_WREN },
561 .readcnt = 0,
562 .readarr = NULL,
563 }, {
564 .writecnt = JEDEC_CE_60_OUTSIZE,
565 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
566 .readcnt = 0,
567 .readarr = NULL,
568 }, {
569 .writecnt = 0,
570 .writearr = NULL,
571 .readcnt = 0,
572 .readarr = NULL,
573 }};
574
snelson8913d082010-02-26 05:48:29 +0000575 result = spi_send_multicommand(cmds);
576 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000577 msg_cerr("%s failed during command execution\n",
snelson8913d082010-02-26 05:48:29 +0000578 __func__);
579 return result;
580 }
581 /* Wait until the Write-In-Progress bit is cleared.
582 * This usually takes 1-85 s, so wait in 1 s steps.
583 */
584 /* FIXME: We assume spi_read_status_register will never fail. */
585 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
586 programmer_delay(1000 * 1000);
587 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000588 msg_cerr("ERASE FAILED!\n");
snelson8913d082010-02-26 05:48:29 +0000589 return -1;
590 }
591 return 0;
592}
593
594int spi_chip_erase_c7(struct flashchip *flash)
595{
596 int result;
597 struct spi_command cmds[] = {
598 {
599 .writecnt = JEDEC_WREN_OUTSIZE,
600 .writearr = (const unsigned char[]){ JEDEC_WREN },
601 .readcnt = 0,
602 .readarr = NULL,
603 }, {
604 .writecnt = JEDEC_CE_C7_OUTSIZE,
605 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
606 .readcnt = 0,
607 .readarr = NULL,
608 }, {
609 .writecnt = 0,
610 .writearr = NULL,
611 .readcnt = 0,
612 .readarr = NULL,
613 }};
614
snelson8913d082010-02-26 05:48:29 +0000615 result = spi_send_multicommand(cmds);
616 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000617 msg_cerr("%s failed during command execution\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000618 return result;
619 }
620 /* Wait until the Write-In-Progress bit is cleared.
621 * This usually takes 1-85 s, so wait in 1 s steps.
622 */
623 /* FIXME: We assume spi_read_status_register will never fail. */
624 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
625 programmer_delay(1000 * 1000);
626 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000627 msg_cerr("ERASE FAILED!\n");
snelson8913d082010-02-26 05:48:29 +0000628 return -1;
629 }
630 return 0;
631}
632
snelson8913d082010-02-26 05:48:29 +0000633int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
634{
635 int result;
636 struct spi_command cmds[] = {
637 {
638 .writecnt = JEDEC_WREN_OUTSIZE,
639 .writearr = (const unsigned char[]){ JEDEC_WREN },
640 .readcnt = 0,
641 .readarr = NULL,
642 }, {
643 .writecnt = JEDEC_BE_52_OUTSIZE,
644 .writearr = (const unsigned char[]){
645 JEDEC_BE_52,
646 (addr >> 16) & 0xff,
647 (addr >> 8) & 0xff,
648 (addr & 0xff)
649 },
650 .readcnt = 0,
651 .readarr = NULL,
652 }, {
653 .writecnt = 0,
654 .writearr = NULL,
655 .readcnt = 0,
656 .readarr = NULL,
657 }};
658
659 result = spi_send_multicommand(cmds);
660 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000661 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000662 __func__, addr);
663 return result;
664 }
665 /* Wait until the Write-In-Progress bit is cleared.
666 * This usually takes 100-4000 ms, so wait in 100 ms steps.
667 */
668 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
669 programmer_delay(100 * 1000);
670 if (check_erased_range(flash, addr, blocklen)) {
snelsonfc007bb2010-03-24 23:14:32 +0000671 msg_cerr("ERASE FAILED!\n");
snelson8913d082010-02-26 05:48:29 +0000672 return -1;
673 }
674 return 0;
675}
676
677/* Block size is usually
678 * 64k for Macronix
679 * 32k for SST
680 * 4-32k non-uniform for EON
681 */
682int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
683{
684 int result;
685 struct spi_command cmds[] = {
686 {
687 .writecnt = JEDEC_WREN_OUTSIZE,
688 .writearr = (const unsigned char[]){ JEDEC_WREN },
689 .readcnt = 0,
690 .readarr = NULL,
691 }, {
692 .writecnt = JEDEC_BE_D8_OUTSIZE,
693 .writearr = (const unsigned char[]){
694 JEDEC_BE_D8,
695 (addr >> 16) & 0xff,
696 (addr >> 8) & 0xff,
697 (addr & 0xff)
698 },
699 .readcnt = 0,
700 .readarr = NULL,
701 }, {
702 .writecnt = 0,
703 .writearr = NULL,
704 .readcnt = 0,
705 .readarr = NULL,
706 }};
707
708 result = spi_send_multicommand(cmds);
709 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000710 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000711 __func__, addr);
712 return result;
713 }
714 /* Wait until the Write-In-Progress bit is cleared.
715 * This usually takes 100-4000 ms, so wait in 100 ms steps.
716 */
717 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
718 programmer_delay(100 * 1000);
719 if (check_erased_range(flash, addr, blocklen)) {
snelsonfc007bb2010-03-24 23:14:32 +0000720 msg_cerr("ERASE FAILED!\n");
snelson8913d082010-02-26 05:48:29 +0000721 return -1;
722 }
723 return 0;
724}
725
726/* Block size is usually
727 * 4k for PMC
728 */
729int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
730{
731 int result;
732 struct spi_command cmds[] = {
733 {
734 .writecnt = JEDEC_WREN_OUTSIZE,
735 .writearr = (const unsigned char[]){ JEDEC_WREN },
736 .readcnt = 0,
737 .readarr = NULL,
738 }, {
739 .writecnt = JEDEC_BE_D7_OUTSIZE,
740 .writearr = (const unsigned char[]){
741 JEDEC_BE_D7,
742 (addr >> 16) & 0xff,
743 (addr >> 8) & 0xff,
744 (addr & 0xff)
745 },
746 .readcnt = 0,
747 .readarr = NULL,
748 }, {
749 .writecnt = 0,
750 .writearr = NULL,
751 .readcnt = 0,
752 .readarr = NULL,
753 }};
754
755 result = spi_send_multicommand(cmds);
756 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000757 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000758 __func__, addr);
759 return result;
760 }
761 /* Wait until the Write-In-Progress bit is cleared.
762 * This usually takes 100-4000 ms, so wait in 100 ms steps.
763 */
764 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
765 programmer_delay(100 * 1000);
766 if (check_erased_range(flash, addr, blocklen)) {
snelsonfc007bb2010-03-24 23:14:32 +0000767 msg_cerr("ERASE FAILED!\n");
snelson8913d082010-02-26 05:48:29 +0000768 return -1;
769 }
770 return 0;
771}
772
snelson8913d082010-02-26 05:48:29 +0000773/* Sector size is usually 4k, though Macronix eliteflash has 64k */
774int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
775{
Stefan Reinauerd814f6f2011-03-25 15:07:25 -0700776 int result;
snelson8913d082010-02-26 05:48:29 +0000777 struct spi_command cmds[] = {
778 {
779 .writecnt = JEDEC_WREN_OUTSIZE,
780 .writearr = (const unsigned char[]){ JEDEC_WREN },
781 .readcnt = 0,
782 .readarr = NULL,
783 }, {
784 .writecnt = JEDEC_SE_OUTSIZE,
785 .writearr = (const unsigned char[]){
786 JEDEC_SE,
787 (addr >> 16) & 0xff,
788 (addr >> 8) & 0xff,
789 (addr & 0xff)
790 },
791 .readcnt = 0,
792 .readarr = NULL,
793 }, {
794 .writecnt = 0,
795 .writearr = NULL,
796 .readcnt = 0,
797 .readarr = NULL,
798 }};
799
Stefan Reinauerd814f6f2011-03-25 15:07:25 -0700800 result = spi_send_multicommand(cmds);
Stefan Reinauercce56d52010-11-22 18:22:21 -0800801
snelson8913d082010-02-26 05:48:29 +0000802 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000803 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000804 __func__, addr);
805 return result;
806 }
807 /* Wait until the Write-In-Progress bit is cleared.
808 * This usually takes 15-800 ms, so wait in 10 ms steps.
809 */
810 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
811 programmer_delay(10 * 1000);
812 if (check_erased_range(flash, addr, blocklen)) {
snelsonfc007bb2010-03-24 23:14:32 +0000813 msg_cerr("ERASE FAILED!\n");
snelson8913d082010-02-26 05:48:29 +0000814 return -1;
815 }
816 return 0;
817}
818
819int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
820{
821 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000822 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000823 __func__);
824 return -1;
825 }
826 return spi_chip_erase_60(flash);
827}
828
829int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
830{
831 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000832 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000833 __func__);
834 return -1;
835 }
836 return spi_chip_erase_c7(flash);
837}
838
839int spi_write_status_enable(void)
840{
David Hendricks668f29d2011-01-27 18:51:45 -0800841 static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
snelson8913d082010-02-26 05:48:29 +0000842 int result;
843
844 /* Send EWSR (Enable Write Status Register). */
845 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
846
847 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000848 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000849
850 return result;
851}
852
853/*
854 * This is according the SST25VF016 datasheet, who knows it is more
855 * generic that this...
856 */
David Hendricks82fd8ae2010-08-04 14:34:54 -0700857static int spi_write_status_register_ewsr(struct flashchip *flash, int status)
snelson8913d082010-02-26 05:48:29 +0000858{
859 int result;
David Hendricksc801adb2010-12-09 16:58:56 -0800860 int i = 0;
snelson8913d082010-02-26 05:48:29 +0000861 struct spi_command cmds[] = {
862 {
David Hendricks82fd8ae2010-08-04 14:34:54 -0700863 /* WRSR requires either EWSR or WREN depending on chip type. */
snelson8913d082010-02-26 05:48:29 +0000864 .writecnt = JEDEC_EWSR_OUTSIZE,
865 .writearr = (const unsigned char[]){ JEDEC_EWSR },
866 .readcnt = 0,
867 .readarr = NULL,
868 }, {
869 .writecnt = JEDEC_WRSR_OUTSIZE,
870 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
871 .readcnt = 0,
872 .readarr = NULL,
873 }, {
874 .writecnt = 0,
875 .writearr = NULL,
876 .readcnt = 0,
877 .readarr = NULL,
878 }};
879
880 result = spi_send_multicommand(cmds);
881 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000882 msg_cerr("%s failed during command execution\n",
snelson8913d082010-02-26 05:48:29 +0000883 __func__);
David Hendricksc801adb2010-12-09 16:58:56 -0800884 /* No point in waiting for the command to complete if execution
885 * failed.
886 */
887 return result;
snelson8913d082010-02-26 05:48:29 +0000888 }
David Hendricksc801adb2010-12-09 16:58:56 -0800889 /* WRSR performs a self-timed erase before the changes take effect.
890 * This may take 50-85 ms in most cases, and some chips apparently
891 * allow running RDSR only once. Therefore pick an initial delay of
892 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
893 */
David Hendricks82fd8ae2010-08-04 14:34:54 -0700894 programmer_delay(100 * 1000);
David Hendricksc801adb2010-12-09 16:58:56 -0800895 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
896 if (++i > 490) {
897 msg_cerr("Error: WIP bit after WRSR never cleared\n");
898 return TIMEOUT_ERROR;
899 }
900 programmer_delay(10 * 1000);
901 }
902 return 0;
snelson8913d082010-02-26 05:48:29 +0000903}
904
David Hendricks82fd8ae2010-08-04 14:34:54 -0700905static int spi_write_status_register_wren(struct flashchip *flash, int status)
906{
907 int result;
David Hendricksc801adb2010-12-09 16:58:56 -0800908 int i = 0;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700909 struct spi_command cmds[] = {
910 {
911 /* WRSR requires either EWSR or WREN depending on chip type. */
912 .writecnt = JEDEC_WREN_OUTSIZE,
913 .writearr = (const unsigned char[]){ JEDEC_WREN },
914 .readcnt = 0,
915 .readarr = NULL,
916 }, {
917 .writecnt = JEDEC_WRSR_OUTSIZE,
918 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
919 .readcnt = 0,
920 .readarr = NULL,
921 }, {
922 .writecnt = 0,
923 .writearr = NULL,
924 .readcnt = 0,
925 .readarr = NULL,
926 }};
927
928 result = spi_send_multicommand(cmds);
929 if (result) {
930 msg_cerr("%s failed during command execution\n",
931 __func__);
David Hendricksc801adb2010-12-09 16:58:56 -0800932 /* No point in waiting for the command to complete if execution
933 * failed.
934 */
935 return result;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700936 }
David Hendricksc801adb2010-12-09 16:58:56 -0800937 /* WRSR performs a self-timed erase before the changes take effect.
938 * This may take 50-85 ms in most cases, and some chips apparently
939 * allow running RDSR only once. Therefore pick an initial delay of
940 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
941 */
David Hendricks82fd8ae2010-08-04 14:34:54 -0700942 programmer_delay(100 * 1000);
David Hendricksc801adb2010-12-09 16:58:56 -0800943 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
944 if (++i > 490) {
945 msg_cerr("Error: WIP bit after WRSR never cleared\n");
946 return TIMEOUT_ERROR;
947 }
948 programmer_delay(10 * 1000);
949 }
950 return 0;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700951}
952
953int spi_write_status_register(struct flashchip *flash, int status)
954{
955 int ret = 1;
956
957 if (!(flash->feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
958 msg_cdbg("Missing status register write definition, assuming "
959 "EWSR is needed\n");
960 flash->feature_bits |= FEATURE_WRSR_EWSR;
961 }
962 if (flash->feature_bits & FEATURE_WRSR_WREN)
963 ret = spi_write_status_register_wren(flash, status);
964 if (ret && (flash->feature_bits & FEATURE_WRSR_EWSR))
965 ret = spi_write_status_register_ewsr(flash, status);
966 return ret;
967}
968
snelson8913d082010-02-26 05:48:29 +0000969int spi_byte_program(int addr, uint8_t databyte)
970{
971 int result;
972 struct spi_command cmds[] = {
973 {
974 .writecnt = JEDEC_WREN_OUTSIZE,
975 .writearr = (const unsigned char[]){ JEDEC_WREN },
976 .readcnt = 0,
977 .readarr = NULL,
978 }, {
979 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
980 .writearr = (const unsigned char[]){
981 JEDEC_BYTE_PROGRAM,
982 (addr >> 16) & 0xff,
983 (addr >> 8) & 0xff,
984 (addr & 0xff),
985 databyte
986 },
987 .readcnt = 0,
988 .readarr = NULL,
989 }, {
990 .writecnt = 0,
991 .writearr = NULL,
992 .readcnt = 0,
993 .readarr = NULL,
994 }};
995
996 result = spi_send_multicommand(cmds);
997 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000998 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000999 __func__, addr);
1000 }
1001 return result;
1002}
1003
1004int spi_nbyte_program(int addr, uint8_t *bytes, int len)
1005{
1006 int result;
1007 /* FIXME: Switch to malloc based on len unless that kills speed. */
1008 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
1009 JEDEC_BYTE_PROGRAM,
1010 (addr >> 16) & 0xff,
1011 (addr >> 8) & 0xff,
1012 (addr >> 0) & 0xff,
1013 };
1014 struct spi_command cmds[] = {
1015 {
1016 .writecnt = JEDEC_WREN_OUTSIZE,
1017 .writearr = (const unsigned char[]){ JEDEC_WREN },
1018 .readcnt = 0,
1019 .readarr = NULL,
1020 }, {
1021 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
1022 .writearr = cmd,
1023 .readcnt = 0,
1024 .readarr = NULL,
1025 }, {
1026 .writecnt = 0,
1027 .writearr = NULL,
1028 .readcnt = 0,
1029 .readarr = NULL,
1030 }};
1031
1032 if (!len) {
snelsonfc007bb2010-03-24 23:14:32 +00001033 msg_cerr("%s called for zero-length write\n", __func__);
snelson8913d082010-02-26 05:48:29 +00001034 return 1;
1035 }
1036 if (len > 256) {
snelsonfc007bb2010-03-24 23:14:32 +00001037 msg_cerr("%s called for too long a write\n", __func__);
snelson8913d082010-02-26 05:48:29 +00001038 return 1;
1039 }
1040
1041 memcpy(&cmd[4], bytes, len);
1042
1043 result = spi_send_multicommand(cmds);
1044 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +00001045 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +00001046 __func__, addr);
1047 }
1048 return result;
1049}
1050
David Hendricksbf36f092010-11-02 23:39:29 -07001051int spi_restore_status(struct flashchip *flash, uint8_t status)
1052{
1053 msg_cdbg("restoring chip status (0x%02x)\n", status);
1054 return spi_write_status_register(flash, status);
1055}
1056
David Hendricks82fd8ae2010-08-04 14:34:54 -07001057/* A generic brute-force block protection disable works like this:
1058 * Write 0x00 to the status register. Check if any locks are still set (that
1059 * part is chip specific). Repeat once.
1060 */
1061int spi_disable_blockprotect(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +00001062{
1063 uint8_t status;
1064 int result;
1065
1066 status = spi_read_status_register();
David Hendricks82fd8ae2010-08-04 14:34:54 -07001067 /* If block protection is disabled, stop here. */
1068 if ((status & 0x3c) == 0)
1069 return 0;
1070
David Hendricksbf36f092010-11-02 23:39:29 -07001071 /* restore status register content upon exit */
1072 register_chip_restore(spi_restore_status, flash, status);
1073
David Hendricks82fd8ae2010-08-04 14:34:54 -07001074 msg_cdbg("Some block protection in effect, disabling\n");
1075 result = spi_write_status_register(flash, status & ~0x3c);
1076 if (result) {
1077 msg_cerr("spi_write_status_register failed\n");
1078 return result;
1079 }
1080 status = spi_read_status_register();
snelson8913d082010-02-26 05:48:29 +00001081 if ((status & 0x3c) != 0) {
David Hendricks82fd8ae2010-08-04 14:34:54 -07001082 msg_cerr("Block protection could not be disabled!\n");
1083 return 1;
1084 }
1085 return 0;
1086}
1087
1088int spi_disable_blockprotect_at25df(struct flashchip *flash)
1089{
1090 uint8_t status;
1091 int result;
1092
1093 status = spi_read_status_register();
1094 /* If block protection is disabled, stop here. */
1095 if ((status & (3 << 2)) == 0)
1096 return 0;
1097
1098 msg_cdbg("Some block protection in effect, disabling\n");
1099 if (status & (1 << 7)) {
1100 msg_cdbg("Need to disable Sector Protection Register Lock\n");
1101 if ((status & (1 << 4)) == 0) {
1102 msg_cerr("WP# pin is active, disabling "
1103 "write protection is impossible.\n");
1104 return 1;
1105 }
1106 /* All bits except bit 7 (SPRL) are readonly. */
1107 result = spi_write_status_register(flash, status & ~(1 << 7));
snelson8913d082010-02-26 05:48:29 +00001108 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +00001109 msg_cerr("spi_write_status_register failed\n");
snelson8913d082010-02-26 05:48:29 +00001110 return result;
1111 }
David Hendricks82fd8ae2010-08-04 14:34:54 -07001112
1113 }
1114 /* Global unprotect. Make sure to mask SPRL as well. */
1115 result = spi_write_status_register(flash, status & ~0xbc);
1116 if (result) {
1117 msg_cerr("spi_write_status_register failed\n");
1118 return result;
1119 }
1120 status = spi_read_status_register();
1121 if ((status & (3 << 2)) != 0) {
1122 msg_cerr("Block protection could not be disabled!\n");
1123 return 1;
1124 }
1125 return 0;
1126}
1127
1128int spi_disable_blockprotect_at25df_sec(struct flashchip *flash)
1129{
1130 /* FIXME: We should check the security lockdown. */
1131 msg_cinfo("Ignoring security lockdown (if present)\n");
1132 return spi_disable_blockprotect_at25df(flash);
1133}
1134
1135int spi_disable_blockprotect_at25f(struct flashchip *flash)
1136{
1137 /* spi_disable_blockprotect_at25df is not really the right way to do
1138 * this, but the side effects of said function work here as well.
1139 */
1140 return spi_disable_blockprotect_at25df(flash);
1141}
1142
1143int spi_disable_blockprotect_at25fs010(struct flashchip *flash)
1144{
1145 uint8_t status;
1146 int result;
1147
1148 status = spi_read_status_register();
1149 /* If block protection is disabled, stop here. */
1150 if ((status & 0x6c) == 0)
1151 return 0;
1152
1153 msg_cdbg("Some block protection in effect, disabling\n");
1154 if (status & (1 << 7)) {
1155 msg_cdbg("Need to disable Status Register Write Protect\n");
1156 /* Clear bit 7 (WPEN). */
1157 result = spi_write_status_register(flash, status & ~(1 << 7));
1158 if (result) {
1159 msg_cerr("spi_write_status_register failed\n");
1160 return result;
1161 }
1162 }
1163 /* Global unprotect. Make sure to mask WPEN as well. */
1164 result = spi_write_status_register(flash, status & ~0xec);
1165 if (result) {
1166 msg_cerr("spi_write_status_register failed\n");
1167 return result;
1168 }
1169 status = spi_read_status_register();
1170 if ((status & 0x6c) != 0) {
1171 msg_cerr("Block protection could not be disabled!\n");
1172 return 1;
1173 }
1174 return 0;
1175}
1176int spi_disable_blockprotect_at25fs040(struct flashchip *flash)
1177{
1178 uint8_t status;
1179 int result;
1180
1181 status = spi_read_status_register();
1182 /* If block protection is disabled, stop here. */
1183 if ((status & 0x7c) == 0)
1184 return 0;
1185
1186 msg_cdbg("Some block protection in effect, disabling\n");
1187 if (status & (1 << 7)) {
1188 msg_cdbg("Need to disable Status Register Write Protect\n");
1189 /* Clear bit 7 (WPEN). */
1190 result = spi_write_status_register(flash, status & ~(1 << 7));
1191 if (result) {
1192 msg_cerr("spi_write_status_register failed\n");
1193 return result;
1194 }
1195 }
1196 /* Global unprotect. Make sure to mask WPEN as well. */
1197 result = spi_write_status_register(flash, status & ~0xfc);
1198 if (result) {
1199 msg_cerr("spi_write_status_register failed\n");
1200 return result;
1201 }
1202 status = spi_read_status_register();
1203 if ((status & 0x7c) != 0) {
1204 msg_cerr("Block protection could not be disabled!\n");
1205 return 1;
snelson8913d082010-02-26 05:48:29 +00001206 }
1207 return 0;
1208}
1209
1210int spi_nbyte_read(int address, uint8_t *bytes, int len)
1211{
1212 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
1213 JEDEC_READ,
1214 (address >> 16) & 0xff,
1215 (address >> 8) & 0xff,
1216 (address >> 0) & 0xff,
1217 };
1218
1219 /* Send Read */
1220 return spi_send_command(sizeof(cmd), len, cmd, bytes);
1221}
1222
1223/*
hailfinger39d159a2010-05-21 23:09:42 +00001224 * Read a part of the flash chip.
David Hendricks82fd8ae2010-08-04 14:34:54 -07001225 * FIXME: Use the chunk code from Michael Karcher instead.
snelson8913d082010-02-26 05:48:29 +00001226 * Each page is read separately in chunks with a maximum size of chunksize.
1227 */
1228int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
1229{
1230 int rc = 0;
1231 int i, j, starthere, lenhere;
1232 int page_size = flash->page_size;
1233 int toread;
1234
1235 /* Warning: This loop has a very unusual condition and body.
1236 * The loop needs to go through each page with at least one affected
1237 * byte. The lowest page number is (start / page_size) since that
1238 * division rounds down. The highest page number we want is the page
1239 * where the last byte of the range lives. That last byte has the
1240 * address (start + len - 1), thus the highest page number is
1241 * (start + len - 1) / page_size. Since we want to include that last
1242 * page as well, the loop condition uses <=.
1243 */
1244 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1245 /* Byte position of the first byte in the range in this page. */
1246 /* starthere is an offset to the base address of the chip. */
1247 starthere = max(start, i * page_size);
1248 /* Length of bytes in the range in this page. */
1249 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1250 for (j = 0; j < lenhere; j += chunksize) {
1251 toread = min(chunksize, lenhere - j);
1252 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
1253 if (rc)
1254 break;
1255 }
1256 if (rc)
1257 break;
1258 }
1259
1260 return rc;
1261}
1262
1263/*
hailfinger39d159a2010-05-21 23:09:42 +00001264 * Write a part of the flash chip.
David Hendricks82fd8ae2010-08-04 14:34:54 -07001265 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +00001266 * Each page is written separately in chunks with a maximum size of chunksize.
1267 */
1268int spi_write_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
1269{
1270 int rc = 0;
1271 int i, j, starthere, lenhere;
1272 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
1273 * in struct flashchip to do this properly. All chips using
1274 * spi_chip_write_256 have page_size set to max_writechunk_size, so
1275 * we're OK for now.
1276 */
1277 int page_size = flash->page_size;
1278 int towrite;
1279
1280 /* Warning: This loop has a very unusual condition and body.
1281 * The loop needs to go through each page with at least one affected
1282 * byte. The lowest page number is (start / page_size) since that
1283 * division rounds down. The highest page number we want is the page
1284 * where the last byte of the range lives. That last byte has the
1285 * address (start + len - 1), thus the highest page number is
1286 * (start + len - 1) / page_size. Since we want to include that last
1287 * page as well, the loop condition uses <=.
1288 */
1289 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1290 /* Byte position of the first byte in the range in this page. */
1291 /* starthere is an offset to the base address of the chip. */
1292 starthere = max(start, i * page_size);
1293 /* Length of bytes in the range in this page. */
1294 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1295 for (j = 0; j < lenhere; j += chunksize) {
1296 towrite = min(chunksize, lenhere - j);
1297 rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite);
1298 if (rc)
1299 break;
1300 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1301 programmer_delay(10);
1302 }
1303 if (rc)
1304 break;
1305 }
1306
1307 return rc;
1308}
1309
1310/*
snelson8913d082010-02-26 05:48:29 +00001311 * Program chip using byte programming. (SLOW!)
1312 * This is for chips which can only handle one byte writes
1313 * and for chips where memory mapped programming is impossible
1314 * (e.g. due to size constraints in IT87* for over 512 kB)
1315 */
David Hendricks82fd8ae2010-08-04 14:34:54 -07001316/* real chunksize is 1, logical chunksize is 1 */
David Hendricksc801adb2010-12-09 16:58:56 -08001317int spi_chip_write_1(struct flashchip *flash, uint8_t *buf, int start, int len)
snelson8913d082010-02-26 05:48:29 +00001318{
snelson8913d082010-02-26 05:48:29 +00001319 int i, result = 0;
1320
David Hendricks82fd8ae2010-08-04 14:34:54 -07001321 for (i = start; i < start + len; i++) {
David Hendricksc801adb2010-12-09 16:58:56 -08001322 result = spi_byte_program(i, buf[i - start]);
snelson8913d082010-02-26 05:48:29 +00001323 if (result)
1324 return 1;
1325 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1326 programmer_delay(10);
1327 }
1328
1329 return 0;
1330}
1331
David Hendricks82fd8ae2010-08-04 14:34:54 -07001332int spi_aai_write(struct flashchip *flash, uint8_t *buf, int start, int len)
1333{
1334 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +00001335 int result;
hailfinger19db0922010-06-20 10:41:35 +00001336 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1337 JEDEC_AAI_WORD_PROGRAM,
1338 };
1339 struct spi_command cmds[] = {
1340 {
1341 .writecnt = JEDEC_WREN_OUTSIZE,
1342 .writearr = (const unsigned char[]){ JEDEC_WREN },
1343 .readcnt = 0,
1344 .readarr = NULL,
1345 }, {
1346 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1347 .writearr = (const unsigned char[]){
1348 JEDEC_AAI_WORD_PROGRAM,
David Hendricks82fd8ae2010-08-04 14:34:54 -07001349 (start >> 16) & 0xff,
1350 (start >> 8) & 0xff,
1351 (start & 0xff),
hailfinger19db0922010-06-20 10:41:35 +00001352 buf[0],
1353 buf[1]
1354 },
1355 .readcnt = 0,
1356 .readarr = NULL,
1357 }, {
1358 .writecnt = 0,
1359 .writearr = NULL,
1360 .readcnt = 0,
1361 .readarr = NULL,
1362 }};
snelson8913d082010-02-26 05:48:29 +00001363
1364 switch (spi_controller) {
hailfinger90c7d542010-05-31 15:27:27 +00001365#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +00001366#if defined(__i386__) || defined(__x86_64__)
hailfinger19db0922010-06-20 10:41:35 +00001367 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +00001368 case SPI_CONTROLLER_WBSIO:
David Hendricks82fd8ae2010-08-04 14:34:54 -07001369 msg_perr("%s: impossible with this SPI controller,"
snelson8913d082010-02-26 05:48:29 +00001370 " degrading to byte program\n", __func__);
David Hendricksc801adb2010-12-09 16:58:56 -08001371 return spi_chip_write_1(flash, buf, start, len);
snelson8913d082010-02-26 05:48:29 +00001372#endif
hailfinger324a9cc2010-05-26 01:45:41 +00001373#endif
snelson8913d082010-02-26 05:48:29 +00001374 default:
1375 break;
1376 }
hailfinger19db0922010-06-20 10:41:35 +00001377
David Hendricks82fd8ae2010-08-04 14:34:54 -07001378 /* The even start address and even length requirements can be either
1379 * honored outside this function, or we can call spi_byte_program
1380 * for the first and/or last byte and use AAI for the rest.
David Hendricksc801adb2010-12-09 16:58:56 -08001381 * FIXME: Move this to generic code.
David Hendricks82fd8ae2010-08-04 14:34:54 -07001382 */
hailfinger19db0922010-06-20 10:41:35 +00001383 /* The data sheet requires a start address with the low bit cleared. */
David Hendricks82fd8ae2010-08-04 14:34:54 -07001384 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +00001385 msg_cerr("%s: start address not even! Please report a bug at "
1386 "flashrom@flashrom.org\n", __func__);
David Hendricksc801adb2010-12-09 16:58:56 -08001387 if (spi_chip_write_1(flash, buf, start, start % 2))
1388 return SPI_GENERIC_ERROR;
1389 pos += start % 2;
1390 cmds[1].writearr = (const unsigned char[]){
1391 JEDEC_AAI_WORD_PROGRAM,
1392 (pos >> 16) & 0xff,
1393 (pos >> 8) & 0xff,
1394 (pos & 0xff),
1395 buf[pos - start],
1396 buf[pos - start + 1]
1397 };
1398 /* Do not return an error for now. */
1399 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +00001400 }
1401 /* The data sheet requires total AAI write length to be even. */
1402 if (len % 2) {
1403 msg_cerr("%s: total write length not even! Please report a "
1404 "bug at flashrom@flashrom.org\n", __func__);
David Hendricksc801adb2010-12-09 16:58:56 -08001405 /* Do not return an error for now. */
1406 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +00001407 }
1408
hailfinger19db0922010-06-20 10:41:35 +00001409
1410 result = spi_send_multicommand(cmds);
1411 if (result) {
1412 msg_cerr("%s failed during start command execution\n",
1413 __func__);
David Hendricks82fd8ae2010-08-04 14:34:54 -07001414 /* FIXME: Should we send WRDI here as well to make sure the chip
1415 * is not in AAI mode?
1416 */
snelson8913d082010-02-26 05:48:29 +00001417 return result;
snelson8913d082010-02-26 05:48:29 +00001418 }
hailfinger19db0922010-06-20 10:41:35 +00001419 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1420 programmer_delay(10);
1421
1422 /* We already wrote 2 bytes in the multicommand step. */
1423 pos += 2;
1424
David Hendricksc801adb2010-12-09 16:58:56 -08001425 /* Are there at least two more bytes to write? */
1426 while (pos < start + len - 1) {
1427 cmd[1] = buf[pos++ - start];
1428 cmd[2] = buf[pos++ - start];
hailfinger19db0922010-06-20 10:41:35 +00001429 spi_send_command(JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
1430 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1431 programmer_delay(10);
1432 }
1433
David Hendricksc801adb2010-12-09 16:58:56 -08001434 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
1435 * other non-AAI command.
1436 */
snelson8913d082010-02-26 05:48:29 +00001437 spi_write_disable();
David Hendricksc801adb2010-12-09 16:58:56 -08001438
1439 /* Write remaining byte (if any). */
1440 if (pos < start + len) {
1441 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
1442 return SPI_GENERIC_ERROR;
1443 pos += pos % 2;
1444 }
1445
snelson8913d082010-02-26 05:48:29 +00001446 return 0;
1447}