blob: 51be3971ee0548a3d62fdada108d86454ee13129 [file] [log] [blame]
Sean Nelson14ba6682010-02-26 05:48:29 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00004 * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger
Sean Nelson14ba6682010-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"
29#include "spi.h"
30
31void spi_prettyprint_status_register(struct flashchip *flash);
32
33static int spi_rdid(unsigned char *readarr, int bytes)
34{
35 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
36 int ret;
37 int i;
38
39 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
40 if (ret)
41 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000042 msg_cspew("RDID returned");
Sean Nelson14ba6682010-02-26 05:48:29 +000043 for (i = 0; i < bytes; i++)
Sean Nelsoned479d22010-03-24 23:14:32 +000044 msg_cspew(" 0x%02x", readarr[i]);
45 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000046 return 0;
47}
48
49static int spi_rems(unsigned char *readarr)
50{
51 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
52 uint32_t readaddr;
53 int ret;
54
55 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
56 if (ret == SPI_INVALID_ADDRESS) {
57 /* Find the lowest even address allowed for reads. */
58 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
59 cmd[1] = (readaddr >> 16) & 0xff,
60 cmd[2] = (readaddr >> 8) & 0xff,
61 cmd[3] = (readaddr >> 0) & 0xff,
62 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
63 }
64 if (ret)
65 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000066 msg_cspew("REMS returned %02x %02x. ", readarr[0], readarr[1]);
Sean Nelson14ba6682010-02-26 05:48:29 +000067 return 0;
68}
69
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000070static int spi_res(unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000071{
72 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
73 uint32_t readaddr;
74 int ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000075 int i;
Sean Nelson14ba6682010-02-26 05:48:29 +000076
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000077 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000078 if (ret == SPI_INVALID_ADDRESS) {
79 /* Find the lowest even address allowed for reads. */
80 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
81 cmd[1] = (readaddr >> 16) & 0xff,
82 cmd[2] = (readaddr >> 8) & 0xff,
83 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000084 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000085 }
86 if (ret)
87 return ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000088 msg_cspew("RES returned");
89 for (i = 0; i < bytes; i++)
90 msg_cspew(" 0x%02x", readarr[i]);
91 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000092 return 0;
93}
94
95int spi_write_enable(void)
96{
97 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
98 int result;
99
100 /* Send WREN (Write Enable) */
101 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
102
103 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000104 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000105
106 return result;
107}
108
109int spi_write_disable(void)
110{
111 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
112
113 /* Send WRDI (Write Disable) */
114 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
115}
116
117static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
118{
119 unsigned char readarr[4];
120 uint32_t id1;
121 uint32_t id2;
122
123 if (spi_rdid(readarr, bytes))
124 return 0;
125
126 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000127 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000128
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000129 /* Check if this is a continuation vendor ID.
130 * FIXME: Handle continuation device IDs.
131 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000132 if (readarr[0] == 0x7f) {
133 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000134 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000135 id1 = (readarr[0] << 8) | readarr[1];
136 id2 = readarr[2];
137 if (bytes > 3) {
138 id2 <<= 8;
139 id2 |= readarr[3];
140 }
141 } else {
142 id1 = readarr[0];
143 id2 = (readarr[1] << 8) | readarr[2];
144 }
145
Sean Nelsoned479d22010-03-24 23:14:32 +0000146 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000147
148 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
149 /* Print the status register to tell the
150 * user about possible write protection.
151 */
152 spi_prettyprint_status_register(flash);
153
154 return 1;
155 }
156
157 /* Test if this is a pure vendor match. */
158 if (id1 == flash->manufacture_id &&
159 GENERIC_DEVICE_ID == flash->model_id)
160 return 1;
161
162 /* Test if there is any vendor ID. */
163 if (GENERIC_MANUF_ID == flash->manufacture_id &&
164 id1 != 0xff)
165 return 1;
166
167 return 0;
168}
169
170int probe_spi_rdid(struct flashchip *flash)
171{
172 return probe_spi_rdid_generic(flash, 3);
173}
174
Sean Nelson14ba6682010-02-26 05:48:29 +0000175int probe_spi_rdid4(struct flashchip *flash)
176{
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000177 /* Some SPI controllers do not support commands with writecnt=1 and
178 * readcnt=4.
179 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000180 switch (spi_controller) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000181#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000182#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000183 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000184 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000185 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
186 return 0;
187 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000188#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000189#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000190 default:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000191 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000192 }
193
194 return 0;
195}
196
197int probe_spi_rems(struct flashchip *flash)
198{
199 unsigned char readarr[JEDEC_REMS_INSIZE];
200 uint32_t id1, id2;
201
202 if (spi_rems(readarr))
203 return 0;
204
205 id1 = readarr[0];
206 id2 = readarr[1];
207
Sean Nelsoned479d22010-03-24 23:14:32 +0000208 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000209
210 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
211 /* Print the status register to tell the
212 * user about possible write protection.
213 */
214 spi_prettyprint_status_register(flash);
215
216 return 1;
217 }
218
219 /* Test if this is a pure vendor match. */
220 if (id1 == flash->manufacture_id &&
221 GENERIC_DEVICE_ID == flash->model_id)
222 return 1;
223
224 /* Test if there is any vendor ID. */
225 if (GENERIC_MANUF_ID == flash->manufacture_id &&
226 id1 != 0xff)
227 return 1;
228
229 return 0;
230}
231
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000232int probe_spi_res1(struct flashchip *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000233{
234 unsigned char readarr[3];
235 uint32_t id2;
236 const unsigned char allff[] = {0xff, 0xff, 0xff};
237 const unsigned char all00[] = {0x00, 0x00, 0x00};
238
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000239 /* We only want one-byte RES if RDID and REMS are unusable. */
240
Sean Nelson14ba6682010-02-26 05:48:29 +0000241 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
242 * 0x00 0x00 0x00. In that case, RES is pointless.
243 */
244 if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) &&
245 memcmp(readarr, all00, 3)) {
246 msg_cdbg("Ignoring RES in favour of RDID.\n");
247 return 0;
248 }
249 /* Check if REMS is usable and does not return 0xff 0xff or
250 * 0x00 0x00. In that case, RES is pointless.
251 */
252 if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
253 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
254 msg_cdbg("Ignoring RES in favour of REMS.\n");
255 return 0;
256 }
257
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000258 if (spi_res(readarr, 1))
Sean Nelson14ba6682010-02-26 05:48:29 +0000259 return 0;
260
Sean Nelson14ba6682010-02-26 05:48:29 +0000261 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000262
Sean Nelsoned479d22010-03-24 23:14:32 +0000263 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000264
Sean Nelson14ba6682010-02-26 05:48:29 +0000265 if (id2 != flash->model_id)
266 return 0;
267
268 /* Print the status register to tell the
269 * user about possible write protection.
270 */
271 spi_prettyprint_status_register(flash);
272 return 1;
273}
274
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000275int probe_spi_res2(struct flashchip *flash)
276{
277 unsigned char readarr[2];
278 uint32_t id1, id2;
279
280 if (spi_res(readarr, 2))
281 return 0;
282
283 id1 = readarr[0];
284 id2 = readarr[1];
285
286 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
287
288 if (id1 != flash->manufacture_id || id2 != flash->model_id)
289 return 0;
290
291 /* Print the status register to tell the
292 * user about possible write protection.
293 */
294 spi_prettyprint_status_register(flash);
295 return 1;
296}
297
Sean Nelson14ba6682010-02-26 05:48:29 +0000298uint8_t spi_read_status_register(void)
299{
300 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
301 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
302 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
303 int ret;
304
305 /* Read Status Register */
306 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
307 if (ret)
Sean Nelsoned479d22010-03-24 23:14:32 +0000308 msg_cerr("RDSR failed!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000309
310 return readarr[0];
311}
312
313/* Prettyprint the status register. Common definitions. */
314void spi_prettyprint_status_register_common(uint8_t status)
315{
Sean Nelsoned479d22010-03-24 23:14:32 +0000316 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000317 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000318 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000319 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000320 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000321 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000322 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000323 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000324 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000325 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000326 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000327 "%sset\n", (status & (1 << 0)) ? "" : "not ");
328}
329
330/* Prettyprint the status register. Works for
331 * ST M25P series
332 * MX MX25L series
333 */
334void spi_prettyprint_status_register_st_m25p(uint8_t status)
335{
Sean Nelsoned479d22010-03-24 23:14:32 +0000336 msg_cdbg("Chip status register: Status Register Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000337 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000338 msg_cdbg("Chip status register: Bit 6 is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000339 "%sset\n", (status & (1 << 6)) ? "" : "not ");
340 spi_prettyprint_status_register_common(status);
341}
342
343void spi_prettyprint_status_register_sst25(uint8_t status)
344{
Sean Nelsoned479d22010-03-24 23:14:32 +0000345 msg_cdbg("Chip status register: Block Protect Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000346 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000347 msg_cdbg("Chip status register: Auto Address Increment Programming "
Sean Nelson14ba6682010-02-26 05:48:29 +0000348 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
349 spi_prettyprint_status_register_common(status);
350}
351
352/* Prettyprint the status register. Works for
353 * SST 25VF016
354 */
355void spi_prettyprint_status_register_sst25vf016(uint8_t status)
356{
357 const char *bpt[] = {
358 "none",
359 "1F0000H-1FFFFFH",
360 "1E0000H-1FFFFFH",
361 "1C0000H-1FFFFFH",
362 "180000H-1FFFFFH",
363 "100000H-1FFFFFH",
364 "all", "all"
365 };
366 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000367 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000368 bpt[(status & 0x1c) >> 2]);
369}
370
371void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
372{
373 const char *bpt[] = {
374 "none",
375 "0x70000-0x7ffff",
376 "0x60000-0x7ffff",
377 "0x40000-0x7ffff",
378 "all blocks", "all blocks", "all blocks", "all blocks"
379 };
380 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000381 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000382 bpt[(status & 0x1c) >> 2]);
383}
384
385void spi_prettyprint_status_register(struct flashchip *flash)
386{
387 uint8_t status;
388
389 status = spi_read_status_register();
Sean Nelsoned479d22010-03-24 23:14:32 +0000390 msg_cdbg("Chip status register is %02x\n", status);
Sean Nelson14ba6682010-02-26 05:48:29 +0000391 switch (flash->manufacture_id) {
392 case ST_ID:
393 if (((flash->model_id & 0xff00) == 0x2000) ||
394 ((flash->model_id & 0xff00) == 0x2500))
395 spi_prettyprint_status_register_st_m25p(status);
396 break;
397 case MX_ID:
398 if ((flash->model_id & 0xff00) == 0x2000)
399 spi_prettyprint_status_register_st_m25p(status);
400 break;
401 case SST_ID:
402 switch (flash->model_id) {
403 case 0x2541:
404 spi_prettyprint_status_register_sst25vf016(status);
405 break;
406 case 0x8d:
407 case 0x258d:
408 spi_prettyprint_status_register_sst25vf040b(status);
409 break;
410 default:
411 spi_prettyprint_status_register_sst25(status);
412 break;
413 }
414 break;
415 }
416}
417
418int spi_chip_erase_60(struct flashchip *flash)
419{
420 int result;
421 struct spi_command cmds[] = {
422 {
423 .writecnt = JEDEC_WREN_OUTSIZE,
424 .writearr = (const unsigned char[]){ JEDEC_WREN },
425 .readcnt = 0,
426 .readarr = NULL,
427 }, {
428 .writecnt = JEDEC_CE_60_OUTSIZE,
429 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
430 .readcnt = 0,
431 .readarr = NULL,
432 }, {
433 .writecnt = 0,
434 .writearr = NULL,
435 .readcnt = 0,
436 .readarr = NULL,
437 }};
438
439 result = spi_disable_blockprotect();
440 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000441 msg_cerr("spi_disable_blockprotect failed\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000442 return result;
443 }
444
445 result = spi_send_multicommand(cmds);
446 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000447 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000448 __func__);
449 return result;
450 }
451 /* Wait until the Write-In-Progress bit is cleared.
452 * This usually takes 1-85 s, so wait in 1 s steps.
453 */
454 /* FIXME: We assume spi_read_status_register will never fail. */
455 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
456 programmer_delay(1000 * 1000);
457 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000458 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000459 return -1;
460 }
461 return 0;
462}
463
464int spi_chip_erase_c7(struct flashchip *flash)
465{
466 int result;
467 struct spi_command cmds[] = {
468 {
469 .writecnt = JEDEC_WREN_OUTSIZE,
470 .writearr = (const unsigned char[]){ JEDEC_WREN },
471 .readcnt = 0,
472 .readarr = NULL,
473 }, {
474 .writecnt = JEDEC_CE_C7_OUTSIZE,
475 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
476 .readcnt = 0,
477 .readarr = NULL,
478 }, {
479 .writecnt = 0,
480 .writearr = NULL,
481 .readcnt = 0,
482 .readarr = NULL,
483 }};
484
485 result = spi_disable_blockprotect();
486 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000487 msg_cerr("spi_disable_blockprotect failed\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000488 return result;
489 }
490
491 result = spi_send_multicommand(cmds);
492 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000493 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000494 return result;
495 }
496 /* Wait until the Write-In-Progress bit is cleared.
497 * This usually takes 1-85 s, so wait in 1 s steps.
498 */
499 /* FIXME: We assume spi_read_status_register will never fail. */
500 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
501 programmer_delay(1000 * 1000);
502 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000503 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000504 return -1;
505 }
506 return 0;
507}
508
Sean Nelson14ba6682010-02-26 05:48:29 +0000509int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
510{
511 int result;
512 struct spi_command cmds[] = {
513 {
514 .writecnt = JEDEC_WREN_OUTSIZE,
515 .writearr = (const unsigned char[]){ JEDEC_WREN },
516 .readcnt = 0,
517 .readarr = NULL,
518 }, {
519 .writecnt = JEDEC_BE_52_OUTSIZE,
520 .writearr = (const unsigned char[]){
521 JEDEC_BE_52,
522 (addr >> 16) & 0xff,
523 (addr >> 8) & 0xff,
524 (addr & 0xff)
525 },
526 .readcnt = 0,
527 .readarr = NULL,
528 }, {
529 .writecnt = 0,
530 .writearr = NULL,
531 .readcnt = 0,
532 .readarr = NULL,
533 }};
534
535 result = spi_send_multicommand(cmds);
536 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000537 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000538 __func__, addr);
539 return result;
540 }
541 /* Wait until the Write-In-Progress bit is cleared.
542 * This usually takes 100-4000 ms, so wait in 100 ms steps.
543 */
544 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
545 programmer_delay(100 * 1000);
546 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000547 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000548 return -1;
549 }
550 return 0;
551}
552
553/* Block size is usually
554 * 64k for Macronix
555 * 32k for SST
556 * 4-32k non-uniform for EON
557 */
558int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
559{
560 int result;
561 struct spi_command cmds[] = {
562 {
563 .writecnt = JEDEC_WREN_OUTSIZE,
564 .writearr = (const unsigned char[]){ JEDEC_WREN },
565 .readcnt = 0,
566 .readarr = NULL,
567 }, {
568 .writecnt = JEDEC_BE_D8_OUTSIZE,
569 .writearr = (const unsigned char[]){
570 JEDEC_BE_D8,
571 (addr >> 16) & 0xff,
572 (addr >> 8) & 0xff,
573 (addr & 0xff)
574 },
575 .readcnt = 0,
576 .readarr = NULL,
577 }, {
578 .writecnt = 0,
579 .writearr = NULL,
580 .readcnt = 0,
581 .readarr = NULL,
582 }};
583
584 result = spi_send_multicommand(cmds);
585 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000586 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000587 __func__, addr);
588 return result;
589 }
590 /* Wait until the Write-In-Progress bit is cleared.
591 * This usually takes 100-4000 ms, so wait in 100 ms steps.
592 */
593 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
594 programmer_delay(100 * 1000);
595 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000596 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000597 return -1;
598 }
599 return 0;
600}
601
602/* Block size is usually
603 * 4k for PMC
604 */
605int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
606{
607 int result;
608 struct spi_command cmds[] = {
609 {
610 .writecnt = JEDEC_WREN_OUTSIZE,
611 .writearr = (const unsigned char[]){ JEDEC_WREN },
612 .readcnt = 0,
613 .readarr = NULL,
614 }, {
615 .writecnt = JEDEC_BE_D7_OUTSIZE,
616 .writearr = (const unsigned char[]){
617 JEDEC_BE_D7,
618 (addr >> 16) & 0xff,
619 (addr >> 8) & 0xff,
620 (addr & 0xff)
621 },
622 .readcnt = 0,
623 .readarr = NULL,
624 }, {
625 .writecnt = 0,
626 .writearr = NULL,
627 .readcnt = 0,
628 .readarr = NULL,
629 }};
630
631 result = spi_send_multicommand(cmds);
632 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000633 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000634 __func__, addr);
635 return result;
636 }
637 /* Wait until the Write-In-Progress bit is cleared.
638 * This usually takes 100-4000 ms, so wait in 100 ms steps.
639 */
640 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
641 programmer_delay(100 * 1000);
642 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000643 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000644 return -1;
645 }
646 return 0;
647}
648
Sean Nelson14ba6682010-02-26 05:48:29 +0000649/* Sector size is usually 4k, though Macronix eliteflash has 64k */
650int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
651{
652 int result;
653 struct spi_command cmds[] = {
654 {
655 .writecnt = JEDEC_WREN_OUTSIZE,
656 .writearr = (const unsigned char[]){ JEDEC_WREN },
657 .readcnt = 0,
658 .readarr = NULL,
659 }, {
660 .writecnt = JEDEC_SE_OUTSIZE,
661 .writearr = (const unsigned char[]){
662 JEDEC_SE,
663 (addr >> 16) & 0xff,
664 (addr >> 8) & 0xff,
665 (addr & 0xff)
666 },
667 .readcnt = 0,
668 .readarr = NULL,
669 }, {
670 .writecnt = 0,
671 .writearr = NULL,
672 .readcnt = 0,
673 .readarr = NULL,
674 }};
675
676 result = spi_send_multicommand(cmds);
677 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000678 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000679 __func__, addr);
680 return result;
681 }
682 /* Wait until the Write-In-Progress bit is cleared.
683 * This usually takes 15-800 ms, so wait in 10 ms steps.
684 */
685 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
686 programmer_delay(10 * 1000);
687 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000688 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000689 return -1;
690 }
691 return 0;
692}
693
694int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
695{
696 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000697 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000698 __func__);
699 return -1;
700 }
701 return spi_chip_erase_60(flash);
702}
703
704int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
705{
706 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000707 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000708 __func__);
709 return -1;
710 }
711 return spi_chip_erase_c7(flash);
712}
713
714int spi_write_status_enable(void)
715{
716 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
717 int result;
718
719 /* Send EWSR (Enable Write Status Register). */
720 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
721
722 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000723 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000724
725 return result;
726}
727
728/*
729 * This is according the SST25VF016 datasheet, who knows it is more
730 * generic that this...
731 */
732int spi_write_status_register(int status)
733{
734 int result;
735 struct spi_command cmds[] = {
736 {
737 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
738 .writecnt = JEDEC_EWSR_OUTSIZE,
739 .writearr = (const unsigned char[]){ JEDEC_EWSR },
740 .readcnt = 0,
741 .readarr = NULL,
742 }, {
743 .writecnt = JEDEC_WRSR_OUTSIZE,
744 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
745 .readcnt = 0,
746 .readarr = NULL,
747 }, {
748 .writecnt = 0,
749 .writearr = NULL,
750 .readcnt = 0,
751 .readarr = NULL,
752 }};
753
754 result = spi_send_multicommand(cmds);
755 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000756 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000757 __func__);
758 }
759 return result;
760}
761
762int spi_byte_program(int addr, uint8_t databyte)
763{
764 int result;
765 struct spi_command cmds[] = {
766 {
767 .writecnt = JEDEC_WREN_OUTSIZE,
768 .writearr = (const unsigned char[]){ JEDEC_WREN },
769 .readcnt = 0,
770 .readarr = NULL,
771 }, {
772 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
773 .writearr = (const unsigned char[]){
774 JEDEC_BYTE_PROGRAM,
775 (addr >> 16) & 0xff,
776 (addr >> 8) & 0xff,
777 (addr & 0xff),
778 databyte
779 },
780 .readcnt = 0,
781 .readarr = NULL,
782 }, {
783 .writecnt = 0,
784 .writearr = NULL,
785 .readcnt = 0,
786 .readarr = NULL,
787 }};
788
789 result = spi_send_multicommand(cmds);
790 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000791 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000792 __func__, addr);
793 }
794 return result;
795}
796
797int spi_nbyte_program(int addr, uint8_t *bytes, int len)
798{
799 int result;
800 /* FIXME: Switch to malloc based on len unless that kills speed. */
801 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
802 JEDEC_BYTE_PROGRAM,
803 (addr >> 16) & 0xff,
804 (addr >> 8) & 0xff,
805 (addr >> 0) & 0xff,
806 };
807 struct spi_command cmds[] = {
808 {
809 .writecnt = JEDEC_WREN_OUTSIZE,
810 .writearr = (const unsigned char[]){ JEDEC_WREN },
811 .readcnt = 0,
812 .readarr = NULL,
813 }, {
814 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
815 .writearr = cmd,
816 .readcnt = 0,
817 .readarr = NULL,
818 }, {
819 .writecnt = 0,
820 .writearr = NULL,
821 .readcnt = 0,
822 .readarr = NULL,
823 }};
824
825 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000826 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000827 return 1;
828 }
829 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000830 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000831 return 1;
832 }
833
834 memcpy(&cmd[4], bytes, len);
835
836 result = spi_send_multicommand(cmds);
837 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000838 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000839 __func__, addr);
840 }
841 return result;
842}
843
844int spi_disable_blockprotect(void)
845{
846 uint8_t status;
847 int result;
848
849 status = spi_read_status_register();
850 /* If there is block protection in effect, unprotect it first. */
851 if ((status & 0x3c) != 0) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000852 msg_cdbg("Some block protection in effect, disabling\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000853 result = spi_write_status_register(status & ~0x3c);
854 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000855 msg_cerr("spi_write_status_register failed\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000856 return result;
857 }
858 }
859 return 0;
860}
861
862int spi_nbyte_read(int address, uint8_t *bytes, int len)
863{
864 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
865 JEDEC_READ,
866 (address >> 16) & 0xff,
867 (address >> 8) & 0xff,
868 (address >> 0) & 0xff,
869 };
870
871 /* Send Read */
872 return spi_send_command(sizeof(cmd), len, cmd, bytes);
873}
874
875/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000876 * Read a part of the flash chip.
Sean Nelson14ba6682010-02-26 05:48:29 +0000877 * Each page is read separately in chunks with a maximum size of chunksize.
878 */
879int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
880{
881 int rc = 0;
882 int i, j, starthere, lenhere;
883 int page_size = flash->page_size;
884 int toread;
885
886 /* Warning: This loop has a very unusual condition and body.
887 * The loop needs to go through each page with at least one affected
888 * byte. The lowest page number is (start / page_size) since that
889 * division rounds down. The highest page number we want is the page
890 * where the last byte of the range lives. That last byte has the
891 * address (start + len - 1), thus the highest page number is
892 * (start + len - 1) / page_size. Since we want to include that last
893 * page as well, the loop condition uses <=.
894 */
895 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
896 /* Byte position of the first byte in the range in this page. */
897 /* starthere is an offset to the base address of the chip. */
898 starthere = max(start, i * page_size);
899 /* Length of bytes in the range in this page. */
900 lenhere = min(start + len, (i + 1) * page_size) - starthere;
901 for (j = 0; j < lenhere; j += chunksize) {
902 toread = min(chunksize, lenhere - j);
903 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
904 if (rc)
905 break;
906 }
907 if (rc)
908 break;
909 }
910
911 return rc;
912}
913
914/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000915 * Write a part of the flash chip.
916 * Each page is written separately in chunks with a maximum size of chunksize.
917 */
918int spi_write_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
919{
920 int rc = 0;
921 int i, j, starthere, lenhere;
922 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
923 * in struct flashchip to do this properly. All chips using
924 * spi_chip_write_256 have page_size set to max_writechunk_size, so
925 * we're OK for now.
926 */
927 int page_size = flash->page_size;
928 int towrite;
929
930 /* Warning: This loop has a very unusual condition and body.
931 * The loop needs to go through each page with at least one affected
932 * byte. The lowest page number is (start / page_size) since that
933 * division rounds down. The highest page number we want is the page
934 * where the last byte of the range lives. That last byte has the
935 * address (start + len - 1), thus the highest page number is
936 * (start + len - 1) / page_size. Since we want to include that last
937 * page as well, the loop condition uses <=.
938 */
939 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
940 /* Byte position of the first byte in the range in this page. */
941 /* starthere is an offset to the base address of the chip. */
942 starthere = max(start, i * page_size);
943 /* Length of bytes in the range in this page. */
944 lenhere = min(start + len, (i + 1) * page_size) - starthere;
945 for (j = 0; j < lenhere; j += chunksize) {
946 towrite = min(chunksize, lenhere - j);
947 rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite);
948 if (rc)
949 break;
950 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
951 programmer_delay(10);
952 }
953 if (rc)
954 break;
955 }
956
957 return rc;
958}
959
960/*
Sean Nelson14ba6682010-02-26 05:48:29 +0000961 * Program chip using byte programming. (SLOW!)
962 * This is for chips which can only handle one byte writes
963 * and for chips where memory mapped programming is impossible
964 * (e.g. due to size constraints in IT87* for over 512 kB)
965 */
966int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
967{
968 int total_size = 1024 * flash->total_size;
969 int i, result = 0;
970
971 spi_disable_blockprotect();
972 /* Erase first */
Sean Nelsoned479d22010-03-24 23:14:32 +0000973 msg_cinfo("Erasing flash before programming... ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000974 if (erase_flash(flash)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000975 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000976 return -1;
977 }
Sean Nelsoned479d22010-03-24 23:14:32 +0000978 msg_cinfo("done.\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000979 for (i = 0; i < total_size; i++) {
980 result = spi_byte_program(i, buf[i]);
981 if (result)
982 return 1;
983 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
984 programmer_delay(10);
985 }
986
987 return 0;
988}
989
990int spi_aai_write(struct flashchip *flash, uint8_t *buf)
991{
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000992 uint32_t addr = 0;
993 uint32_t len = flash->total_size * 1024;
994 uint32_t pos = addr;
Sean Nelson14ba6682010-02-26 05:48:29 +0000995 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000996 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
997 JEDEC_AAI_WORD_PROGRAM,
998 };
999 struct spi_command cmds[] = {
1000 {
1001 .writecnt = JEDEC_WREN_OUTSIZE,
1002 .writearr = (const unsigned char[]){ JEDEC_WREN },
1003 .readcnt = 0,
1004 .readarr = NULL,
1005 }, {
1006 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1007 .writearr = (const unsigned char[]){
1008 JEDEC_AAI_WORD_PROGRAM,
1009 (pos >> 16) & 0xff,
1010 (pos >> 8) & 0xff,
1011 (pos & 0xff),
1012 buf[0],
1013 buf[1]
1014 },
1015 .readcnt = 0,
1016 .readarr = NULL,
1017 }, {
1018 .writecnt = 0,
1019 .writearr = NULL,
1020 .readcnt = 0,
1021 .readarr = NULL,
1022 }};
Sean Nelson14ba6682010-02-26 05:48:29 +00001023
1024 switch (spi_controller) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001025#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001026#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001027 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +00001028 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001029 msg_cerr("%s: impossible with this SPI controller,"
Sean Nelson14ba6682010-02-26 05:48:29 +00001030 " degrading to byte program\n", __func__);
1031 return spi_chip_write_1(flash, buf);
1032#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001033#endif
Sean Nelson14ba6682010-02-26 05:48:29 +00001034 default:
1035 break;
1036 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001037
1038 /* The data sheet requires a start address with the low bit cleared. */
1039 if (addr % 2) {
1040 msg_cerr("%s: start address not even! Please report a bug at "
1041 "flashrom@flashrom.org\n", __func__);
1042 return SPI_GENERIC_ERROR;
1043 }
1044 /* The data sheet requires total AAI write length to be even. */
1045 if (len % 2) {
1046 msg_cerr("%s: total write length not even! Please report a "
1047 "bug at flashrom@flashrom.org\n", __func__);
1048 return SPI_GENERIC_ERROR;
1049 }
1050
Sean Nelson14ba6682010-02-26 05:48:29 +00001051 if (erase_flash(flash)) {
Sean Nelsoned479d22010-03-24 23:14:32 +00001052 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +00001053 return -1;
1054 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001055
1056 result = spi_send_multicommand(cmds);
1057 if (result) {
1058 msg_cerr("%s failed during start command execution\n",
1059 __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +00001060 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +00001061 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001062 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1063 programmer_delay(10);
1064
1065 /* We already wrote 2 bytes in the multicommand step. */
1066 pos += 2;
1067
1068 while (pos < addr + len) {
1069 cmd[1] = buf[pos++];
1070 cmd[2] = buf[pos++];
1071 spi_send_command(JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
1072 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1073 programmer_delay(10);
1074 }
1075
1076 /* Use WRDI to exit AAI mode. */
Sean Nelson14ba6682010-02-26 05:48:29 +00001077 spi_write_disable();
1078 return 0;
1079}