blob: 72940c139326729735b1db58cb98841dd0940072 [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;
75
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000076 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000077 if (ret == SPI_INVALID_ADDRESS) {
78 /* Find the lowest even address allowed for reads. */
79 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
80 cmd[1] = (readaddr >> 16) & 0xff,
81 cmd[2] = (readaddr >> 8) & 0xff,
82 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000083 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000084 }
85 if (ret)
86 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000087 msg_cspew("RES returned %02x. ", readarr[0]);
Sean Nelson14ba6682010-02-26 05:48:29 +000088 return 0;
89}
90
91int spi_write_enable(void)
92{
93 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
94 int result;
95
96 /* Send WREN (Write Enable) */
97 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
98
99 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000100 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000101
102 return result;
103}
104
105int spi_write_disable(void)
106{
107 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
108
109 /* Send WRDI (Write Disable) */
110 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
111}
112
113static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
114{
115 unsigned char readarr[4];
116 uint32_t id1;
117 uint32_t id2;
118
119 if (spi_rdid(readarr, bytes))
120 return 0;
121
122 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000123 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000124
125 /* Check if this is a continuation vendor ID */
126 if (readarr[0] == 0x7f) {
127 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000128 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000129 id1 = (readarr[0] << 8) | readarr[1];
130 id2 = readarr[2];
131 if (bytes > 3) {
132 id2 <<= 8;
133 id2 |= readarr[3];
134 }
135 } else {
136 id1 = readarr[0];
137 id2 = (readarr[1] << 8) | readarr[2];
138 }
139
Sean Nelsoned479d22010-03-24 23:14:32 +0000140 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000141
142 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
143 /* Print the status register to tell the
144 * user about possible write protection.
145 */
146 spi_prettyprint_status_register(flash);
147
148 return 1;
149 }
150
151 /* Test if this is a pure vendor match. */
152 if (id1 == flash->manufacture_id &&
153 GENERIC_DEVICE_ID == flash->model_id)
154 return 1;
155
156 /* Test if there is any vendor ID. */
157 if (GENERIC_MANUF_ID == flash->manufacture_id &&
158 id1 != 0xff)
159 return 1;
160
161 return 0;
162}
163
164int probe_spi_rdid(struct flashchip *flash)
165{
166 return probe_spi_rdid_generic(flash, 3);
167}
168
169/* support 4 bytes flash ID */
170int probe_spi_rdid4(struct flashchip *flash)
171{
172 /* only some SPI chipsets support 4 bytes commands */
173 switch (spi_controller) {
174#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000175#if defined(__i386__) || defined(__x86_64__)
Sean Nelson14ba6682010-02-26 05:48:29 +0000176 case SPI_CONTROLLER_ICH7:
177 case SPI_CONTROLLER_ICH9:
178 case SPI_CONTROLLER_VIA:
179 case SPI_CONTROLLER_SB600:
180 case SPI_CONTROLLER_WBSIO:
181#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000182#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000183#if FT2232_SPI_SUPPORT == 1
184 case SPI_CONTROLLER_FT2232:
185#endif
186#if DUMMY_SUPPORT == 1
187 case SPI_CONTROLLER_DUMMY:
188#endif
189#if BUSPIRATE_SPI_SUPPORT == 1
190 case SPI_CONTROLLER_BUSPIRATE:
191#endif
192#if DEDIPROG_SUPPORT == 1
193 case SPI_CONTROLLER_DEDIPROG:
194#endif
195 return probe_spi_rdid_generic(flash, 4);
196 default:
Sean Nelsoned479d22010-03-24 23:14:32 +0000197 msg_cinfo("4b ID not supported on this SPI controller\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000198 }
199
200 return 0;
201}
202
203int probe_spi_rems(struct flashchip *flash)
204{
205 unsigned char readarr[JEDEC_REMS_INSIZE];
206 uint32_t id1, id2;
207
208 if (spi_rems(readarr))
209 return 0;
210
211 id1 = readarr[0];
212 id2 = readarr[1];
213
Sean Nelsoned479d22010-03-24 23:14:32 +0000214 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000215
216 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
217 /* Print the status register to tell the
218 * user about possible write protection.
219 */
220 spi_prettyprint_status_register(flash);
221
222 return 1;
223 }
224
225 /* Test if this is a pure vendor match. */
226 if (id1 == flash->manufacture_id &&
227 GENERIC_DEVICE_ID == flash->model_id)
228 return 1;
229
230 /* Test if there is any vendor ID. */
231 if (GENERIC_MANUF_ID == flash->manufacture_id &&
232 id1 != 0xff)
233 return 1;
234
235 return 0;
236}
237
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000238int probe_spi_res1(struct flashchip *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000239{
240 unsigned char readarr[3];
241 uint32_t id2;
242 const unsigned char allff[] = {0xff, 0xff, 0xff};
243 const unsigned char all00[] = {0x00, 0x00, 0x00};
244
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000245 /* We only want one-byte RES if RDID and REMS are unusable. */
246
Sean Nelson14ba6682010-02-26 05:48:29 +0000247 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
248 * 0x00 0x00 0x00. In that case, RES is pointless.
249 */
250 if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) &&
251 memcmp(readarr, all00, 3)) {
252 msg_cdbg("Ignoring RES in favour of RDID.\n");
253 return 0;
254 }
255 /* Check if REMS is usable and does not return 0xff 0xff or
256 * 0x00 0x00. In that case, RES is pointless.
257 */
258 if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
259 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
260 msg_cdbg("Ignoring RES in favour of REMS.\n");
261 return 0;
262 }
263
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000264 if (spi_res(readarr, 1))
Sean Nelson14ba6682010-02-26 05:48:29 +0000265 return 0;
266
Sean Nelson14ba6682010-02-26 05:48:29 +0000267 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000268
Sean Nelsoned479d22010-03-24 23:14:32 +0000269 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000270
Sean Nelson14ba6682010-02-26 05:48:29 +0000271 if (id2 != flash->model_id)
272 return 0;
273
274 /* Print the status register to tell the
275 * user about possible write protection.
276 */
277 spi_prettyprint_status_register(flash);
278 return 1;
279}
280
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000281int probe_spi_res2(struct flashchip *flash)
282{
283 unsigned char readarr[2];
284 uint32_t id1, id2;
285
286 if (spi_res(readarr, 2))
287 return 0;
288
289 id1 = readarr[0];
290 id2 = readarr[1];
291
292 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
293
294 if (id1 != flash->manufacture_id || id2 != flash->model_id)
295 return 0;
296
297 /* Print the status register to tell the
298 * user about possible write protection.
299 */
300 spi_prettyprint_status_register(flash);
301 return 1;
302}
303
Sean Nelson14ba6682010-02-26 05:48:29 +0000304uint8_t spi_read_status_register(void)
305{
306 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
307 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
308 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
309 int ret;
310
311 /* Read Status Register */
312 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
313 if (ret)
Sean Nelsoned479d22010-03-24 23:14:32 +0000314 msg_cerr("RDSR failed!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000315
316 return readarr[0];
317}
318
319/* Prettyprint the status register. Common definitions. */
320void spi_prettyprint_status_register_common(uint8_t status)
321{
Sean Nelsoned479d22010-03-24 23:14:32 +0000322 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000323 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000324 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000325 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000326 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000327 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000328 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000329 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000330 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000331 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000332 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000333 "%sset\n", (status & (1 << 0)) ? "" : "not ");
334}
335
336/* Prettyprint the status register. Works for
337 * ST M25P series
338 * MX MX25L series
339 */
340void spi_prettyprint_status_register_st_m25p(uint8_t status)
341{
Sean Nelsoned479d22010-03-24 23:14:32 +0000342 msg_cdbg("Chip status register: Status Register Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000343 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000344 msg_cdbg("Chip status register: Bit 6 is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000345 "%sset\n", (status & (1 << 6)) ? "" : "not ");
346 spi_prettyprint_status_register_common(status);
347}
348
349void spi_prettyprint_status_register_sst25(uint8_t status)
350{
Sean Nelsoned479d22010-03-24 23:14:32 +0000351 msg_cdbg("Chip status register: Block Protect Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000352 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000353 msg_cdbg("Chip status register: Auto Address Increment Programming "
Sean Nelson14ba6682010-02-26 05:48:29 +0000354 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
355 spi_prettyprint_status_register_common(status);
356}
357
358/* Prettyprint the status register. Works for
359 * SST 25VF016
360 */
361void spi_prettyprint_status_register_sst25vf016(uint8_t status)
362{
363 const char *bpt[] = {
364 "none",
365 "1F0000H-1FFFFFH",
366 "1E0000H-1FFFFFH",
367 "1C0000H-1FFFFFH",
368 "180000H-1FFFFFH",
369 "100000H-1FFFFFH",
370 "all", "all"
371 };
372 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000373 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000374 bpt[(status & 0x1c) >> 2]);
375}
376
377void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
378{
379 const char *bpt[] = {
380 "none",
381 "0x70000-0x7ffff",
382 "0x60000-0x7ffff",
383 "0x40000-0x7ffff",
384 "all blocks", "all blocks", "all blocks", "all blocks"
385 };
386 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000387 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000388 bpt[(status & 0x1c) >> 2]);
389}
390
391void spi_prettyprint_status_register(struct flashchip *flash)
392{
393 uint8_t status;
394
395 status = spi_read_status_register();
Sean Nelsoned479d22010-03-24 23:14:32 +0000396 msg_cdbg("Chip status register is %02x\n", status);
Sean Nelson14ba6682010-02-26 05:48:29 +0000397 switch (flash->manufacture_id) {
398 case ST_ID:
399 if (((flash->model_id & 0xff00) == 0x2000) ||
400 ((flash->model_id & 0xff00) == 0x2500))
401 spi_prettyprint_status_register_st_m25p(status);
402 break;
403 case MX_ID:
404 if ((flash->model_id & 0xff00) == 0x2000)
405 spi_prettyprint_status_register_st_m25p(status);
406 break;
407 case SST_ID:
408 switch (flash->model_id) {
409 case 0x2541:
410 spi_prettyprint_status_register_sst25vf016(status);
411 break;
412 case 0x8d:
413 case 0x258d:
414 spi_prettyprint_status_register_sst25vf040b(status);
415 break;
416 default:
417 spi_prettyprint_status_register_sst25(status);
418 break;
419 }
420 break;
421 }
422}
423
424int spi_chip_erase_60(struct flashchip *flash)
425{
426 int result;
427 struct spi_command cmds[] = {
428 {
429 .writecnt = JEDEC_WREN_OUTSIZE,
430 .writearr = (const unsigned char[]){ JEDEC_WREN },
431 .readcnt = 0,
432 .readarr = NULL,
433 }, {
434 .writecnt = JEDEC_CE_60_OUTSIZE,
435 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
436 .readcnt = 0,
437 .readarr = NULL,
438 }, {
439 .writecnt = 0,
440 .writearr = NULL,
441 .readcnt = 0,
442 .readarr = NULL,
443 }};
444
445 result = spi_disable_blockprotect();
446 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000447 msg_cerr("spi_disable_blockprotect failed\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000448 return result;
449 }
450
451 result = spi_send_multicommand(cmds);
452 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000453 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000454 __func__);
455 return result;
456 }
457 /* Wait until the Write-In-Progress bit is cleared.
458 * This usually takes 1-85 s, so wait in 1 s steps.
459 */
460 /* FIXME: We assume spi_read_status_register will never fail. */
461 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
462 programmer_delay(1000 * 1000);
463 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000464 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000465 return -1;
466 }
467 return 0;
468}
469
470int spi_chip_erase_c7(struct flashchip *flash)
471{
472 int result;
473 struct spi_command cmds[] = {
474 {
475 .writecnt = JEDEC_WREN_OUTSIZE,
476 .writearr = (const unsigned char[]){ JEDEC_WREN },
477 .readcnt = 0,
478 .readarr = NULL,
479 }, {
480 .writecnt = JEDEC_CE_C7_OUTSIZE,
481 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
482 .readcnt = 0,
483 .readarr = NULL,
484 }, {
485 .writecnt = 0,
486 .writearr = NULL,
487 .readcnt = 0,
488 .readarr = NULL,
489 }};
490
491 result = spi_disable_blockprotect();
492 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000493 msg_cerr("spi_disable_blockprotect failed\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000494 return result;
495 }
496
497 result = spi_send_multicommand(cmds);
498 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000499 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000500 return result;
501 }
502 /* Wait until the Write-In-Progress bit is cleared.
503 * This usually takes 1-85 s, so wait in 1 s steps.
504 */
505 /* FIXME: We assume spi_read_status_register will never fail. */
506 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
507 programmer_delay(1000 * 1000);
508 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000509 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000510 return -1;
511 }
512 return 0;
513}
514
Sean Nelson14ba6682010-02-26 05:48:29 +0000515int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
516{
517 int result;
518 struct spi_command cmds[] = {
519 {
520 .writecnt = JEDEC_WREN_OUTSIZE,
521 .writearr = (const unsigned char[]){ JEDEC_WREN },
522 .readcnt = 0,
523 .readarr = NULL,
524 }, {
525 .writecnt = JEDEC_BE_52_OUTSIZE,
526 .writearr = (const unsigned char[]){
527 JEDEC_BE_52,
528 (addr >> 16) & 0xff,
529 (addr >> 8) & 0xff,
530 (addr & 0xff)
531 },
532 .readcnt = 0,
533 .readarr = NULL,
534 }, {
535 .writecnt = 0,
536 .writearr = NULL,
537 .readcnt = 0,
538 .readarr = NULL,
539 }};
540
541 result = spi_send_multicommand(cmds);
542 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000543 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000544 __func__, addr);
545 return result;
546 }
547 /* Wait until the Write-In-Progress bit is cleared.
548 * This usually takes 100-4000 ms, so wait in 100 ms steps.
549 */
550 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
551 programmer_delay(100 * 1000);
552 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000553 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000554 return -1;
555 }
556 return 0;
557}
558
559/* Block size is usually
560 * 64k for Macronix
561 * 32k for SST
562 * 4-32k non-uniform for EON
563 */
564int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
565{
566 int result;
567 struct spi_command cmds[] = {
568 {
569 .writecnt = JEDEC_WREN_OUTSIZE,
570 .writearr = (const unsigned char[]){ JEDEC_WREN },
571 .readcnt = 0,
572 .readarr = NULL,
573 }, {
574 .writecnt = JEDEC_BE_D8_OUTSIZE,
575 .writearr = (const unsigned char[]){
576 JEDEC_BE_D8,
577 (addr >> 16) & 0xff,
578 (addr >> 8) & 0xff,
579 (addr & 0xff)
580 },
581 .readcnt = 0,
582 .readarr = NULL,
583 }, {
584 .writecnt = 0,
585 .writearr = NULL,
586 .readcnt = 0,
587 .readarr = NULL,
588 }};
589
590 result = spi_send_multicommand(cmds);
591 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000592 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000593 __func__, addr);
594 return result;
595 }
596 /* Wait until the Write-In-Progress bit is cleared.
597 * This usually takes 100-4000 ms, so wait in 100 ms steps.
598 */
599 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
600 programmer_delay(100 * 1000);
601 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000602 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000603 return -1;
604 }
605 return 0;
606}
607
608/* Block size is usually
609 * 4k for PMC
610 */
611int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
612{
613 int result;
614 struct spi_command cmds[] = {
615 {
616 .writecnt = JEDEC_WREN_OUTSIZE,
617 .writearr = (const unsigned char[]){ JEDEC_WREN },
618 .readcnt = 0,
619 .readarr = NULL,
620 }, {
621 .writecnt = JEDEC_BE_D7_OUTSIZE,
622 .writearr = (const unsigned char[]){
623 JEDEC_BE_D7,
624 (addr >> 16) & 0xff,
625 (addr >> 8) & 0xff,
626 (addr & 0xff)
627 },
628 .readcnt = 0,
629 .readarr = NULL,
630 }, {
631 .writecnt = 0,
632 .writearr = NULL,
633 .readcnt = 0,
634 .readarr = NULL,
635 }};
636
637 result = spi_send_multicommand(cmds);
638 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000639 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000640 __func__, addr);
641 return result;
642 }
643 /* Wait until the Write-In-Progress bit is cleared.
644 * This usually takes 100-4000 ms, so wait in 100 ms steps.
645 */
646 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
647 programmer_delay(100 * 1000);
648 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000649 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000650 return -1;
651 }
652 return 0;
653}
654
655int spi_chip_erase_d8(struct flashchip *flash)
656{
657 int i, rc = 0;
658 int total_size = flash->total_size * 1024;
659 int erase_size = 64 * 1024;
660
661 spi_disable_blockprotect();
662
Sean Nelsoned479d22010-03-24 23:14:32 +0000663 msg_cinfo("Erasing chip: \n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000664
665 for (i = 0; i < total_size / erase_size; i++) {
666 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
667 if (rc) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000668 msg_cerr("Error erasing block at 0x%x\n", i);
Sean Nelson14ba6682010-02-26 05:48:29 +0000669 break;
670 }
671 }
672
Sean Nelsoned479d22010-03-24 23:14:32 +0000673 msg_cinfo("\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000674
675 return rc;
676}
677
678/* Sector size is usually 4k, though Macronix eliteflash has 64k */
679int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
680{
681 int result;
682 struct spi_command cmds[] = {
683 {
684 .writecnt = JEDEC_WREN_OUTSIZE,
685 .writearr = (const unsigned char[]){ JEDEC_WREN },
686 .readcnt = 0,
687 .readarr = NULL,
688 }, {
689 .writecnt = JEDEC_SE_OUTSIZE,
690 .writearr = (const unsigned char[]){
691 JEDEC_SE,
692 (addr >> 16) & 0xff,
693 (addr >> 8) & 0xff,
694 (addr & 0xff)
695 },
696 .readcnt = 0,
697 .readarr = NULL,
698 }, {
699 .writecnt = 0,
700 .writearr = NULL,
701 .readcnt = 0,
702 .readarr = NULL,
703 }};
704
705 result = spi_send_multicommand(cmds);
706 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000707 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000708 __func__, addr);
709 return result;
710 }
711 /* Wait until the Write-In-Progress bit is cleared.
712 * This usually takes 15-800 ms, so wait in 10 ms steps.
713 */
714 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
715 programmer_delay(10 * 1000);
716 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000717 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000718 return -1;
719 }
720 return 0;
721}
722
723int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
724{
725 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000726 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000727 __func__);
728 return -1;
729 }
730 return spi_chip_erase_60(flash);
731}
732
733int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
734{
735 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000736 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000737 __func__);
738 return -1;
739 }
740 return spi_chip_erase_c7(flash);
741}
742
743int spi_write_status_enable(void)
744{
745 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
746 int result;
747
748 /* Send EWSR (Enable Write Status Register). */
749 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
750
751 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000752 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000753
754 return result;
755}
756
757/*
758 * This is according the SST25VF016 datasheet, who knows it is more
759 * generic that this...
760 */
761int spi_write_status_register(int status)
762{
763 int result;
764 struct spi_command cmds[] = {
765 {
766 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
767 .writecnt = JEDEC_EWSR_OUTSIZE,
768 .writearr = (const unsigned char[]){ JEDEC_EWSR },
769 .readcnt = 0,
770 .readarr = NULL,
771 }, {
772 .writecnt = JEDEC_WRSR_OUTSIZE,
773 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
774 .readcnt = 0,
775 .readarr = NULL,
776 }, {
777 .writecnt = 0,
778 .writearr = NULL,
779 .readcnt = 0,
780 .readarr = NULL,
781 }};
782
783 result = spi_send_multicommand(cmds);
784 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000785 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000786 __func__);
787 }
788 return result;
789}
790
791int spi_byte_program(int addr, uint8_t databyte)
792{
793 int result;
794 struct spi_command cmds[] = {
795 {
796 .writecnt = JEDEC_WREN_OUTSIZE,
797 .writearr = (const unsigned char[]){ JEDEC_WREN },
798 .readcnt = 0,
799 .readarr = NULL,
800 }, {
801 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
802 .writearr = (const unsigned char[]){
803 JEDEC_BYTE_PROGRAM,
804 (addr >> 16) & 0xff,
805 (addr >> 8) & 0xff,
806 (addr & 0xff),
807 databyte
808 },
809 .readcnt = 0,
810 .readarr = NULL,
811 }, {
812 .writecnt = 0,
813 .writearr = NULL,
814 .readcnt = 0,
815 .readarr = NULL,
816 }};
817
818 result = spi_send_multicommand(cmds);
819 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000820 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000821 __func__, addr);
822 }
823 return result;
824}
825
826int spi_nbyte_program(int addr, uint8_t *bytes, int len)
827{
828 int result;
829 /* FIXME: Switch to malloc based on len unless that kills speed. */
830 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
831 JEDEC_BYTE_PROGRAM,
832 (addr >> 16) & 0xff,
833 (addr >> 8) & 0xff,
834 (addr >> 0) & 0xff,
835 };
836 struct spi_command cmds[] = {
837 {
838 .writecnt = JEDEC_WREN_OUTSIZE,
839 .writearr = (const unsigned char[]){ JEDEC_WREN },
840 .readcnt = 0,
841 .readarr = NULL,
842 }, {
843 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
844 .writearr = cmd,
845 .readcnt = 0,
846 .readarr = NULL,
847 }, {
848 .writecnt = 0,
849 .writearr = NULL,
850 .readcnt = 0,
851 .readarr = NULL,
852 }};
853
854 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000855 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000856 return 1;
857 }
858 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000859 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000860 return 1;
861 }
862
863 memcpy(&cmd[4], bytes, len);
864
865 result = spi_send_multicommand(cmds);
866 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000867 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000868 __func__, addr);
869 }
870 return result;
871}
872
873int spi_disable_blockprotect(void)
874{
875 uint8_t status;
876 int result;
877
878 status = spi_read_status_register();
879 /* If there is block protection in effect, unprotect it first. */
880 if ((status & 0x3c) != 0) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000881 msg_cdbg("Some block protection in effect, disabling\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000882 result = spi_write_status_register(status & ~0x3c);
883 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000884 msg_cerr("spi_write_status_register failed\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000885 return result;
886 }
887 }
888 return 0;
889}
890
891int spi_nbyte_read(int address, uint8_t *bytes, int len)
892{
893 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
894 JEDEC_READ,
895 (address >> 16) & 0xff,
896 (address >> 8) & 0xff,
897 (address >> 0) & 0xff,
898 };
899
900 /* Send Read */
901 return spi_send_command(sizeof(cmd), len, cmd, bytes);
902}
903
904/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000905 * Read a part of the flash chip.
Sean Nelson14ba6682010-02-26 05:48:29 +0000906 * Each page is read separately in chunks with a maximum size of chunksize.
907 */
908int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
909{
910 int rc = 0;
911 int i, j, starthere, lenhere;
912 int page_size = flash->page_size;
913 int toread;
914
915 /* Warning: This loop has a very unusual condition and body.
916 * The loop needs to go through each page with at least one affected
917 * byte. The lowest page number is (start / page_size) since that
918 * division rounds down. The highest page number we want is the page
919 * where the last byte of the range lives. That last byte has the
920 * address (start + len - 1), thus the highest page number is
921 * (start + len - 1) / page_size. Since we want to include that last
922 * page as well, the loop condition uses <=.
923 */
924 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
925 /* Byte position of the first byte in the range in this page. */
926 /* starthere is an offset to the base address of the chip. */
927 starthere = max(start, i * page_size);
928 /* Length of bytes in the range in this page. */
929 lenhere = min(start + len, (i + 1) * page_size) - starthere;
930 for (j = 0; j < lenhere; j += chunksize) {
931 toread = min(chunksize, lenhere - j);
932 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
933 if (rc)
934 break;
935 }
936 if (rc)
937 break;
938 }
939
940 return rc;
941}
942
943/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000944 * Write a part of the flash chip.
945 * Each page is written separately in chunks with a maximum size of chunksize.
946 */
947int spi_write_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
948{
949 int rc = 0;
950 int i, j, starthere, lenhere;
951 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
952 * in struct flashchip to do this properly. All chips using
953 * spi_chip_write_256 have page_size set to max_writechunk_size, so
954 * we're OK for now.
955 */
956 int page_size = flash->page_size;
957 int towrite;
958
959 /* Warning: This loop has a very unusual condition and body.
960 * The loop needs to go through each page with at least one affected
961 * byte. The lowest page number is (start / page_size) since that
962 * division rounds down. The highest page number we want is the page
963 * where the last byte of the range lives. That last byte has the
964 * address (start + len - 1), thus the highest page number is
965 * (start + len - 1) / page_size. Since we want to include that last
966 * page as well, the loop condition uses <=.
967 */
968 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
969 /* Byte position of the first byte in the range in this page. */
970 /* starthere is an offset to the base address of the chip. */
971 starthere = max(start, i * page_size);
972 /* Length of bytes in the range in this page. */
973 lenhere = min(start + len, (i + 1) * page_size) - starthere;
974 for (j = 0; j < lenhere; j += chunksize) {
975 towrite = min(chunksize, lenhere - j);
976 rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite);
977 if (rc)
978 break;
979 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
980 programmer_delay(10);
981 }
982 if (rc)
983 break;
984 }
985
986 return rc;
987}
988
989/*
Sean Nelson14ba6682010-02-26 05:48:29 +0000990 * Program chip using byte programming. (SLOW!)
991 * This is for chips which can only handle one byte writes
992 * and for chips where memory mapped programming is impossible
993 * (e.g. due to size constraints in IT87* for over 512 kB)
994 */
995int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
996{
997 int total_size = 1024 * flash->total_size;
998 int i, result = 0;
999
1000 spi_disable_blockprotect();
1001 /* Erase first */
Sean Nelsoned479d22010-03-24 23:14:32 +00001002 msg_cinfo("Erasing flash before programming... ");
Sean Nelson14ba6682010-02-26 05:48:29 +00001003 if (erase_flash(flash)) {
Sean Nelsoned479d22010-03-24 23:14:32 +00001004 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +00001005 return -1;
1006 }
Sean Nelsoned479d22010-03-24 23:14:32 +00001007 msg_cinfo("done.\n");
Sean Nelson14ba6682010-02-26 05:48:29 +00001008 for (i = 0; i < total_size; i++) {
1009 result = spi_byte_program(i, buf[i]);
1010 if (result)
1011 return 1;
1012 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1013 programmer_delay(10);
1014 }
1015
1016 return 0;
1017}
1018
1019int spi_aai_write(struct flashchip *flash, uint8_t *buf)
1020{
1021 uint32_t pos = 2, size = flash->total_size * 1024;
1022 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
1023 int result;
1024
1025 switch (spi_controller) {
1026#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001027#if defined(__i386__) || defined(__x86_64__)
Sean Nelson14ba6682010-02-26 05:48:29 +00001028 case SPI_CONTROLLER_WBSIO:
Sean Nelsoned479d22010-03-24 23:14:32 +00001029 msg_cerr("%s: impossible with Winbond SPI masters,"
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 }
1037 if (erase_flash(flash)) {
Sean Nelsoned479d22010-03-24 23:14:32 +00001038 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +00001039 return -1;
1040 }
1041 /* FIXME: This will fail on ICH/VIA SPI. */
1042 result = spi_write_enable();
1043 if (result)
1044 return result;
1045 spi_send_command(6, 0, w, NULL);
1046 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1047 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
1048 while (pos < size) {
1049 w[1] = buf[pos++];
1050 w[2] = buf[pos++];
1051 spi_send_command(3, 0, w, NULL);
1052 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1053 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
1054 }
1055 spi_write_disable();
1056 return 0;
1057}