blob: 66f1db87564d1fe19bfcca1cab043a224a11dcab [file] [log] [blame]
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +00004 * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger
Stefan Reinauera9424d52008-06-27 16:28:34 +00005 * Copyright (C) 2008 coresystems GmbH
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +00006 *
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 generic SPI framework
23 */
24
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000025#include <string.h>
26#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000027#include "flashchips.h"
Carl-Daniel Hailfingerd6cbf762008-05-13 14:58:23 +000028#include "spi.h"
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000029
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000030enum spi_controller spi_controller = SPI_CONTROLLER_NONE;
31void *spibar = NULL;
32
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +000033void spi_prettyprint_status_register(struct flashchip *flash);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000034
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000035const struct spi_programmer spi_programmer[] = {
36 { /* SPI_CONTROLLER_NONE */
37 .command = NULL,
38 .multicommand = NULL,
39 .read = NULL,
40 .write_256 = NULL,
41 },
42
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000043#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000044 { /* SPI_CONTROLLER_ICH7 */
45 .command = ich_spi_send_command,
46 .multicommand = ich_spi_send_multicommand,
47 .read = ich_spi_read,
48 .write_256 = ich_spi_write_256,
49 },
50
51 { /* SPI_CONTROLLER_ICH9 */
52 .command = ich_spi_send_command,
53 .multicommand = ich_spi_send_multicommand,
54 .read = ich_spi_read,
55 .write_256 = ich_spi_write_256,
56 },
57
58 { /* SPI_CONTROLLER_IT87XX */
59 .command = it8716f_spi_send_command,
60 .multicommand = default_spi_send_multicommand,
61 .read = it8716f_spi_chip_read,
62 .write_256 = it8716f_spi_chip_write_256,
63 },
64
65 { /* SPI_CONTROLLER_SB600 */
66 .command = sb600_spi_send_command,
67 .multicommand = default_spi_send_multicommand,
68 .read = sb600_spi_read,
69 .write_256 = sb600_spi_write_1,
70 },
71
72 { /* SPI_CONTROLLER_VIA */
73 .command = ich_spi_send_command,
74 .multicommand = ich_spi_send_multicommand,
75 .read = ich_spi_read,
76 .write_256 = ich_spi_write_256,
77 },
78
79 { /* SPI_CONTROLLER_WBSIO */
80 .command = wbsio_spi_send_command,
81 .multicommand = default_spi_send_multicommand,
82 .read = wbsio_spi_read,
83 .write_256 = wbsio_spi_write_1,
84 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000085#endif
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000086
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +000087#if FT2232_SPI_SUPPORT == 1
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000088 { /* SPI_CONTROLLER_FT2232 */
89 .command = ft2232_spi_send_command,
90 .multicommand = default_spi_send_multicommand,
91 .read = ft2232_spi_read,
92 .write_256 = ft2232_spi_write_256,
93 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +000094#endif
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000095
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000096#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000097 { /* SPI_CONTROLLER_DUMMY */
98 .command = dummy_spi_send_command,
99 .multicommand = default_spi_send_multicommand,
100 .read = NULL,
101 .write_256 = NULL,
102 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000103#endif
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000104
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000105#if BUSPIRATE_SPI_SUPPORT == 1
106 { /* SPI_CONTROLLER_BUSPIRATE */
107 .command = buspirate_spi_send_command,
108 .multicommand = default_spi_send_multicommand,
109 .read = buspirate_spi_read,
110 .write_256 = spi_chip_write_1,
111 },
112#endif
113
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000114#if DEDIPROG_SUPPORT == 1
115 { /* SPI_CONTROLLER_DEDIPROG */
116 .command = dediprog_spi_send_command,
117 .multicommand = default_spi_send_multicommand,
118 .read = dediprog_spi_read,
119 .write_256 = spi_chip_write_1,
120 },
121#endif
122
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000123 {}, /* This entry corresponds to SPI_CONTROLLER_INVALID. */
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000124};
125
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000126const int spi_programmer_count = ARRAY_SIZE(spi_programmer);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000127
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000128int spi_send_command(unsigned int writecnt, unsigned int readcnt,
Uwe Hermann394131e2008-10-18 21:14:13 +0000129 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000130{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000131 if (!spi_programmer[spi_controller].command) {
132 fprintf(stderr, "%s called, but SPI is unsupported on this "
133 "hardware. Please report a bug.\n", __func__);
134 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000135 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000136
137 return spi_programmer[spi_controller].command(writecnt, readcnt,
138 writearr, readarr);
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000139}
140
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000141int spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000142{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000143 if (!spi_programmer[spi_controller].multicommand) {
144 fprintf(stderr, "%s called, but SPI is unsupported on this "
145 "hardware. Please report a bug.\n", __func__);
146 return 1;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000147 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000148
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000149 return spi_programmer[spi_controller].multicommand(cmds);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000150}
151
152int default_spi_send_command(unsigned int writecnt, unsigned int readcnt,
153 const unsigned char *writearr, unsigned char *readarr)
154{
155 struct spi_command cmd[] = {
156 {
157 .writecnt = writecnt,
158 .readcnt = readcnt,
159 .writearr = writearr,
160 .readarr = readarr,
161 }, {
162 .writecnt = 0,
163 .writearr = NULL,
164 .readcnt = 0,
165 .readarr = NULL,
166 }};
167
168 return spi_send_multicommand(cmd);
169}
170
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000171int default_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000172{
173 int result = 0;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000174 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
175 result = spi_send_command(cmds->writecnt, cmds->readcnt,
176 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000177 }
178 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000179}
180
Rudolf Marek48a85e42008-06-30 21:45:17 +0000181static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000182{
Uwe Hermann394131e2008-10-18 21:14:13 +0000183 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000184 int ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000185 int i;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000186
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000187 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000188 if (ret)
189 return ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000190 printf_debug("RDID returned");
191 for (i = 0; i < bytes; i++)
192 printf_debug(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000193 printf_debug(". ");
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000194 return 0;
195}
196
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000197static int spi_rems(unsigned char *readarr)
198{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000199 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
200 uint32_t readaddr;
201 int ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000202
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000203 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000204 if (ret == SPI_INVALID_ADDRESS) {
205 /* Find the lowest even address allowed for reads. */
206 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
207 cmd[1] = (readaddr >> 16) & 0xff,
208 cmd[2] = (readaddr >> 8) & 0xff,
209 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000210 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000211 }
212 if (ret)
213 return ret;
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000214 printf_debug("REMS returned %02x %02x. ", readarr[0], readarr[1]);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000215 return 0;
216}
217
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000218static int spi_res(unsigned char *readarr)
219{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000220 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
221 uint32_t readaddr;
222 int ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000223
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000224 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000225 if (ret == SPI_INVALID_ADDRESS) {
226 /* Find the lowest even address allowed for reads. */
227 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
228 cmd[1] = (readaddr >> 16) & 0xff,
229 cmd[2] = (readaddr >> 8) & 0xff,
230 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000231 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000232 }
233 if (ret)
234 return ret;
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000235 printf_debug("RES returned %02x. ", readarr[0]);
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000236 return 0;
237}
238
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000239int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000240{
Uwe Hermann394131e2008-10-18 21:14:13 +0000241 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000242 int result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000243
244 /* Send WREN (Write Enable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000245 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000246
247 if (result)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000248 fprintf(stderr, "%s failed\n", __func__);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000249
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000250 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000251}
252
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000253int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000254{
Uwe Hermann394131e2008-10-18 21:14:13 +0000255 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000256
257 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000258 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000259}
260
Rudolf Marek48a85e42008-06-30 21:45:17 +0000261static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000262{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000263 unsigned char readarr[4];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000264 uint32_t id1;
265 uint32_t id2;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000266
Rudolf Marek48a85e42008-06-30 21:45:17 +0000267 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000268 return 0;
269
270 if (!oddparity(readarr[0]))
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000271 printf_debug("RDID byte 0 parity violation. ");
Peter Stugeda4e5f32008-06-24 01:22:03 +0000272
273 /* Check if this is a continuation vendor ID */
274 if (readarr[0] == 0x7f) {
275 if (!oddparity(readarr[1]))
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000276 printf_debug("RDID byte 1 parity violation. ");
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000277 id1 = (readarr[0] << 8) | readarr[1];
278 id2 = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000279 if (bytes > 3) {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000280 id2 <<= 8;
281 id2 |= readarr[3];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000282 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000283 } else {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000284 id1 = readarr[0];
285 id2 = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000286 }
287
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000288 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000289
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000290 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000291 /* Print the status register to tell the
292 * user about possible write protection.
293 */
294 spi_prettyprint_status_register(flash);
295
296 return 1;
297 }
298
299 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000300 if (id1 == flash->manufacture_id &&
Peter Stugeda4e5f32008-06-24 01:22:03 +0000301 GENERIC_DEVICE_ID == flash->model_id)
302 return 1;
303
Carl-Daniel Hailfinger01d49ed2009-11-20 01:12:45 +0000304 /* Test if there is any vendor ID. */
305 if (GENERIC_MANUF_ID == flash->manufacture_id &&
306 id1 != 0xff)
307 return 1;
308
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000309 return 0;
310}
311
Uwe Hermann394131e2008-10-18 21:14:13 +0000312int probe_spi_rdid(struct flashchip *flash)
313{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000314 return probe_spi_rdid_generic(flash, 3);
315}
316
317/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000318int probe_spi_rdid4(struct flashchip *flash)
319{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000320 /* only some SPI chipsets support 4 bytes commands */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000321 switch (spi_controller) {
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000322#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000323 case SPI_CONTROLLER_ICH7:
324 case SPI_CONTROLLER_ICH9:
325 case SPI_CONTROLLER_VIA:
326 case SPI_CONTROLLER_SB600:
327 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000328#endif
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000329#if FT2232_SPI_SUPPORT == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000330 case SPI_CONTROLLER_FT2232:
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000331#endif
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000332#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000333 case SPI_CONTROLLER_DUMMY:
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000334#endif
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000335#if BUSPIRATE_SPI_SUPPORT == 1
336 case SPI_CONTROLLER_BUSPIRATE:
337#endif
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000338#if DEDIPROG_SUPPORT == 1
339 case SPI_CONTROLLER_DEDIPROG:
340#endif
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000341 return probe_spi_rdid_generic(flash, 4);
342 default:
343 printf_debug("4b ID not supported on this SPI controller\n");
344 }
345
346 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000347}
348
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000349int probe_spi_rems(struct flashchip *flash)
350{
351 unsigned char readarr[JEDEC_REMS_INSIZE];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000352 uint32_t id1, id2;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000353
354 if (spi_rems(readarr))
355 return 0;
356
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000357 id1 = readarr[0];
358 id2 = readarr[1];
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000359
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000360 printf_debug("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000361
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000362 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000363 /* Print the status register to tell the
364 * user about possible write protection.
365 */
366 spi_prettyprint_status_register(flash);
367
368 return 1;
369 }
370
371 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000372 if (id1 == flash->manufacture_id &&
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000373 GENERIC_DEVICE_ID == flash->model_id)
374 return 1;
375
Carl-Daniel Hailfinger01d49ed2009-11-20 01:12:45 +0000376 /* Test if there is any vendor ID. */
377 if (GENERIC_MANUF_ID == flash->manufacture_id &&
378 id1 != 0xff)
379 return 1;
380
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000381 return 0;
382}
383
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000384int probe_spi_res(struct flashchip *flash)
385{
386 unsigned char readarr[3];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000387 uint32_t id2;
Carl-Daniel Hailfingere4edb062010-02-12 19:37:25 +0000388 const unsigned char allff[] = {0xff, 0xff, 0xff};
389 const unsigned char all00[] = {0x00, 0x00, 0x00};
Peter Stugeda4e5f32008-06-24 01:22:03 +0000390
Carl-Daniel Hailfingere4edb062010-02-12 19:37:25 +0000391 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
392 * 0x00 0x00 0x00. In that case, RES is pointless.
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000393 */
Carl-Daniel Hailfingere4edb062010-02-12 19:37:25 +0000394 if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) &&
395 memcmp(readarr, all00, 3)) {
396 msg_cdbg("Ignoring RES in favour of RDID.\n");
Peter Stugeda4e5f32008-06-24 01:22:03 +0000397 return 0;
Carl-Daniel Hailfingere4edb062010-02-12 19:37:25 +0000398 }
399 /* Check if REMS is usable and does not return 0xff 0xff or
400 * 0x00 0x00. In that case, RES is pointless.
401 */
402 if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
403 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
404 msg_cdbg("Ignoring RES in favour of REMS.\n");
405 return 0;
406 }
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000407
Peter Stugeda4e5f32008-06-24 01:22:03 +0000408 if (spi_res(readarr))
409 return 0;
410
Carl-Daniel Hailfingere4edb062010-02-12 19:37:25 +0000411 /* FIXME: Handle the case where RES gives a 2-byte response. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000412 id2 = readarr[0];
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000413 printf_debug("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000414 if (id2 != flash->model_id)
Peter Stugeda4e5f32008-06-24 01:22:03 +0000415 return 0;
416
417 /* Print the status register to tell the
418 * user about possible write protection.
419 */
420 spi_prettyprint_status_register(flash);
421 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000422}
423
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000424uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000425{
Uwe Hermann394131e2008-10-18 21:14:13 +0000426 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000427 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
Peter Stugebf196e92009-01-26 03:08:45 +0000428 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000429 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000430
431 /* Read Status Register */
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000432 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
433 if (ret)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000434 fprintf(stderr, "RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000435
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000436 return readarr[0];
437}
438
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000439/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000440void spi_prettyprint_status_register_common(uint8_t status)
441{
442 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000443 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000444 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000445 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000446 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000447 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000448 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000449 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000450 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000451 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000452 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000453 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000454}
455
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000456/* Prettyprint the status register. Works for
457 * ST M25P series
458 * MX MX25L series
459 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000460void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000461{
462 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000463 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000464 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000465 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000466 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000467}
468
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000469void spi_prettyprint_status_register_sst25(uint8_t status)
470{
471 printf_debug("Chip status register: Block Protect Write Disable "
472 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
473 printf_debug("Chip status register: Auto Address Increment Programming "
474 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
475 spi_prettyprint_status_register_common(status);
476}
477
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000478/* Prettyprint the status register. Works for
479 * SST 25VF016
480 */
481void spi_prettyprint_status_register_sst25vf016(uint8_t status)
482{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000483 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000484 "none",
485 "1F0000H-1FFFFFH",
486 "1E0000H-1FFFFFH",
487 "1C0000H-1FFFFFH",
488 "180000H-1FFFFFH",
489 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000490 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000491 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000492 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000493 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000494 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000495}
496
Peter Stuge5fecee42009-01-26 03:23:50 +0000497void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
498{
499 const char *bpt[] = {
500 "none",
501 "0x70000-0x7ffff",
502 "0x60000-0x7ffff",
503 "0x40000-0x7ffff",
504 "all blocks", "all blocks", "all blocks", "all blocks"
505 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000506 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000507 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000508 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000509}
510
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000511void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000512{
513 uint8_t status;
514
Peter Stugefa8c5502008-05-10 23:07:52 +0000515 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000516 printf_debug("Chip status register is %02x\n", status);
517 switch (flash->manufacture_id) {
518 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000519 if (((flash->model_id & 0xff00) == 0x2000) ||
520 ((flash->model_id & 0xff00) == 0x2500))
521 spi_prettyprint_status_register_st_m25p(status);
522 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000523 case MX_ID:
524 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000525 spi_prettyprint_status_register_st_m25p(status);
526 break;
527 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000528 switch (flash->model_id) {
529 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000530 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000531 break;
532 case 0x8d:
533 case 0x258d:
534 spi_prettyprint_status_register_sst25vf040b(status);
535 break;
Carl-Daniel Hailfinger5100a8a2009-05-13 22:51:27 +0000536 default:
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000537 spi_prettyprint_status_register_sst25(status);
538 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000539 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000540 break;
541 }
542}
Uwe Hermann394131e2008-10-18 21:14:13 +0000543
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000544int spi_chip_erase_60(struct flashchip *flash)
545{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000546 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000547 struct spi_command cmds[] = {
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000548 {
549 .writecnt = JEDEC_WREN_OUTSIZE,
550 .writearr = (const unsigned char[]){ JEDEC_WREN },
551 .readcnt = 0,
552 .readarr = NULL,
553 }, {
554 .writecnt = JEDEC_CE_60_OUTSIZE,
555 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
556 .readcnt = 0,
557 .readarr = NULL,
558 }, {
559 .writecnt = 0,
560 .writearr = NULL,
561 .readcnt = 0,
562 .readarr = NULL,
563 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000564
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000565 result = spi_disable_blockprotect();
566 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000567 fprintf(stderr, "spi_disable_blockprotect failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000568 return result;
569 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000570
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000571 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000572 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000573 fprintf(stderr, "%s failed during command execution\n",
574 __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000575 return result;
576 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000577 /* Wait until the Write-In-Progress bit is cleared.
578 * This usually takes 1-85 s, so wait in 1 s steps.
579 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000580 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000581 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000582 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000583 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
584 fprintf(stderr, "ERASE FAILED!\n");
585 return -1;
586 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000587 return 0;
588}
589
Peter Stugefa8c5502008-05-10 23:07:52 +0000590int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000591{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000592 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000593 struct spi_command cmds[] = {
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000594 {
595 .writecnt = JEDEC_WREN_OUTSIZE,
596 .writearr = (const unsigned char[]){ JEDEC_WREN },
597 .readcnt = 0,
598 .readarr = NULL,
599 }, {
600 .writecnt = JEDEC_CE_C7_OUTSIZE,
601 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
602 .readcnt = 0,
603 .readarr = NULL,
604 }, {
605 .writecnt = 0,
606 .writearr = NULL,
607 .readcnt = 0,
608 .readarr = NULL,
609 }};
Uwe Hermann394131e2008-10-18 21:14:13 +0000610
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000611 result = spi_disable_blockprotect();
612 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000613 fprintf(stderr, "spi_disable_blockprotect failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000614 return result;
615 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000616
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000617 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000618 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000619 fprintf(stderr, "%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000620 return result;
621 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000622 /* Wait until the Write-In-Progress bit is cleared.
623 * This usually takes 1-85 s, so wait in 1 s steps.
624 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000625 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000626 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000627 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000628 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
629 fprintf(stderr, "ERASE FAILED!\n");
630 return -1;
631 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000632 return 0;
633}
634
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000635int spi_chip_erase_60_c7(struct flashchip *flash)
636{
637 int result;
638 result = spi_chip_erase_60(flash);
639 if (result) {
640 printf_debug("spi_chip_erase_60 failed, trying c7\n");
641 result = spi_chip_erase_c7(flash);
642 }
643 return result;
644}
645
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000646int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000647{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000648 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000649 struct spi_command cmds[] = {
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000650 {
651 .writecnt = JEDEC_WREN_OUTSIZE,
652 .writearr = (const unsigned char[]){ JEDEC_WREN },
653 .readcnt = 0,
654 .readarr = NULL,
655 }, {
656 .writecnt = JEDEC_BE_52_OUTSIZE,
657 .writearr = (const unsigned char[]){ JEDEC_BE_52, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
658 .readcnt = 0,
659 .readarr = NULL,
660 }, {
661 .writecnt = 0,
662 .writearr = NULL,
663 .readcnt = 0,
664 .readarr = NULL,
665 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000666
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000667 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000668 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000669 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
670 __func__, addr);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000671 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000672 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000673 /* Wait until the Write-In-Progress bit is cleared.
674 * This usually takes 100-4000 ms, so wait in 100 ms steps.
675 */
676 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000677 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000678 if (check_erased_range(flash, addr, blocklen)) {
679 fprintf(stderr, "ERASE FAILED!\n");
680 return -1;
681 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000682 return 0;
683}
684
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000685/* Block size is usually
686 * 64k for Macronix
687 * 32k for SST
688 * 4-32k non-uniform for EON
689 */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000690int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000691{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000692 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000693 struct spi_command cmds[] = {
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000694 {
695 .writecnt = JEDEC_WREN_OUTSIZE,
696 .writearr = (const unsigned char[]){ JEDEC_WREN },
697 .readcnt = 0,
698 .readarr = NULL,
699 }, {
700 .writecnt = JEDEC_BE_D8_OUTSIZE,
701 .writearr = (const unsigned char[]){ JEDEC_BE_D8, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
702 .readcnt = 0,
703 .readarr = NULL,
704 }, {
705 .writecnt = 0,
706 .writearr = NULL,
707 .readcnt = 0,
708 .readarr = NULL,
709 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000710
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000711 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000712 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000713 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
714 __func__, addr);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000715 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000716 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000717 /* Wait until the Write-In-Progress bit is cleared.
718 * This usually takes 100-4000 ms, so wait in 100 ms steps.
719 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000720 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000721 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000722 if (check_erased_range(flash, addr, blocklen)) {
723 fprintf(stderr, "ERASE FAILED!\n");
724 return -1;
725 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000726 return 0;
727}
728
Sean Nelson5643c072010-01-19 03:23:07 +0000729/* Block size is usually
730 * 4k for PMC
731 */
732int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
733{
734 int result;
735 struct spi_command cmds[] = {
736 {
737 .writecnt = JEDEC_WREN_OUTSIZE,
738 .writearr = (const unsigned char[]){ JEDEC_WREN },
739 .readcnt = 0,
740 .readarr = NULL,
741 }, {
742 .writecnt = JEDEC_BE_D7_OUTSIZE,
743 .writearr = (const unsigned char[]){ JEDEC_BE_D7, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
744 .readcnt = 0,
745 .readarr = NULL,
746 }, {
747 .writecnt = 0,
748 .writearr = NULL,
749 .readcnt = 0,
750 .readarr = NULL,
751 }};
752
753 result = spi_send_multicommand(cmds);
754 if (result) {
755 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
756 __func__, addr);
757 return result;
758 }
759 /* Wait until the Write-In-Progress bit is cleared.
760 * This usually takes 100-4000 ms, so wait in 100 ms steps.
761 */
762 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
763 programmer_delay(100 * 1000);
764 if (check_erased_range(flash, addr, blocklen)) {
765 fprintf(stderr, "ERASE FAILED!\n");
766 return -1;
767 }
768 return 0;
769}
770
Stefan Reinauer424ed222008-10-29 22:13:20 +0000771int spi_chip_erase_d8(struct flashchip *flash)
772{
773 int i, rc = 0;
774 int total_size = flash->total_size * 1024;
775 int erase_size = 64 * 1024;
776
777 spi_disable_blockprotect();
778
779 printf("Erasing chip: \n");
780
781 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000782 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000783 if (rc) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000784 fprintf(stderr, "Error erasing block at 0x%x\n", i);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000785 break;
786 }
787 }
788
789 printf("\n");
790
791 return rc;
792}
793
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000794/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000795int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000796{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000797 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000798 struct spi_command cmds[] = {
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000799 {
800 .writecnt = JEDEC_WREN_OUTSIZE,
801 .writearr = (const unsigned char[]){ JEDEC_WREN },
802 .readcnt = 0,
803 .readarr = NULL,
804 }, {
805 .writecnt = JEDEC_SE_OUTSIZE,
806 .writearr = (const unsigned char[]){ JEDEC_SE, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
807 .readcnt = 0,
808 .readarr = NULL,
809 }, {
810 .writecnt = 0,
811 .writearr = NULL,
812 .readcnt = 0,
813 .readarr = NULL,
814 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000815
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000816 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000817 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000818 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
819 __func__, addr);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000820 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000821 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000822 /* Wait until the Write-In-Progress bit is cleared.
823 * This usually takes 15-800 ms, so wait in 10 ms steps.
824 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000825 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000826 programmer_delay(10 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000827 if (check_erased_range(flash, addr, blocklen)) {
828 fprintf(stderr, "ERASE FAILED!\n");
829 return -1;
830 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000831 return 0;
832}
833
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000834int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
835{
836 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000837 fprintf(stderr, "%s called with incorrect arguments\n",
838 __func__);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000839 return -1;
840 }
841 return spi_chip_erase_60(flash);
842}
843
844int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
845{
846 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000847 fprintf(stderr, "%s called with incorrect arguments\n",
848 __func__);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000849 return -1;
850 }
851 return spi_chip_erase_c7(flash);
852}
853
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000854int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000855{
856 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000857 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000858
859 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000860 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000861
862 if (result)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000863 fprintf(stderr, "%s failed\n", __func__);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000864
865 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000866}
867
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000868/*
869 * This is according the SST25VF016 datasheet, who knows it is more
870 * generic that this...
871 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000872int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000873{
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000874 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000875 struct spi_command cmds[] = {
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000876 {
Carl-Daniel Hailfingerdb53ec52009-12-22 23:54:10 +0000877 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000878 .writecnt = JEDEC_EWSR_OUTSIZE,
879 .writearr = (const unsigned char[]){ JEDEC_EWSR },
880 .readcnt = 0,
881 .readarr = NULL,
882 }, {
883 .writecnt = JEDEC_WRSR_OUTSIZE,
884 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
885 .readcnt = 0,
886 .readarr = NULL,
887 }, {
888 .writecnt = 0,
889 .writearr = NULL,
890 .readcnt = 0,
891 .readarr = NULL,
892 }};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000893
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000894 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000895 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000896 fprintf(stderr, "%s failed during command execution\n",
897 __func__);
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000898 }
899 return result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000900}
901
Michael Karcher4e2fb0e2010-01-12 23:29:26 +0000902int spi_byte_program(int addr, uint8_t databyte)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000903{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000904 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000905 struct spi_command cmds[] = {
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000906 {
907 .writecnt = JEDEC_WREN_OUTSIZE,
908 .writearr = (const unsigned char[]){ JEDEC_WREN },
909 .readcnt = 0,
910 .readarr = NULL,
911 }, {
912 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
Michael Karcher4e2fb0e2010-01-12 23:29:26 +0000913 .writearr = (const unsigned char[]){ JEDEC_BYTE_PROGRAM, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff), databyte },
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000914 .readcnt = 0,
915 .readarr = NULL,
916 }, {
917 .writecnt = 0,
918 .writearr = NULL,
919 .readcnt = 0,
920 .readarr = NULL,
921 }};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000922
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000923 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000924 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000925 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
926 __func__, addr);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000927 }
928 return result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000929}
930
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000931int spi_nbyte_program(int addr, uint8_t *bytes, int len)
Paul Foxeb3acef2009-06-12 08:10:33 +0000932{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000933 int result;
934 /* FIXME: Switch to malloc based on len unless that kills speed. */
Paul Foxeb3acef2009-06-12 08:10:33 +0000935 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
936 JEDEC_BYTE_PROGRAM,
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000937 (addr >> 16) & 0xff,
938 (addr >> 8) & 0xff,
939 (addr >> 0) & 0xff,
Paul Foxeb3acef2009-06-12 08:10:33 +0000940 };
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000941 struct spi_command cmds[] = {
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000942 {
943 .writecnt = JEDEC_WREN_OUTSIZE,
944 .writearr = (const unsigned char[]){ JEDEC_WREN },
945 .readcnt = 0,
946 .readarr = NULL,
947 }, {
948 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
949 .writearr = cmd,
950 .readcnt = 0,
951 .readarr = NULL,
952 }, {
953 .writecnt = 0,
954 .writearr = NULL,
955 .readcnt = 0,
956 .readarr = NULL,
957 }};
Paul Foxeb3acef2009-06-12 08:10:33 +0000958
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000959 if (!len) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000960 fprintf(stderr, "%s called for zero-length write\n", __func__);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000961 return 1;
962 }
Paul Foxeb3acef2009-06-12 08:10:33 +0000963 if (len > 256) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000964 fprintf(stderr, "%s called for too long a write\n", __func__);
Paul Foxeb3acef2009-06-12 08:10:33 +0000965 return 1;
966 }
967
968 memcpy(&cmd[4], bytes, len);
969
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000970 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000971 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000972 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
973 __func__, addr);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000974 }
975 return result;
Paul Foxeb3acef2009-06-12 08:10:33 +0000976}
977
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000978int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000979{
980 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000981 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000982
Peter Stugefa8c5502008-05-10 23:07:52 +0000983 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000984 /* If there is block protection in effect, unprotect it first. */
985 if ((status & 0x3c) != 0) {
986 printf_debug("Some block protection in effect, disabling\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000987 result = spi_write_status_register(status & ~0x3c);
988 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000989 fprintf(stderr, "spi_write_status_register failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000990 return result;
991 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000992 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000993 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000994}
995
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000996int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000997{
Uwe Hermann394131e2008-10-18 21:14:13 +0000998 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
999 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +00001000 (address >> 16) & 0xff,
1001 (address >> 8) & 0xff,
1002 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +00001003 };
1004
1005 /* Send Read */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001006 return spi_send_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +00001007}
1008
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +00001009/*
1010 * Read a complete flash chip.
1011 * Each page is read separately in chunks with a maximum size of chunksize.
1012 */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +00001013int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +00001014{
1015 int rc = 0;
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +00001016 int i, j, starthere, lenhere;
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +00001017 int page_size = flash->page_size;
1018 int toread;
1019
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +00001020 /* Warning: This loop has a very unusual condition and body.
1021 * The loop needs to go through each page with at least one affected
1022 * byte. The lowest page number is (start / page_size) since that
1023 * division rounds down. The highest page number we want is the page
1024 * where the last byte of the range lives. That last byte has the
1025 * address (start + len - 1), thus the highest page number is
1026 * (start + len - 1) / page_size. Since we want to include that last
1027 * page as well, the loop condition uses <=.
1028 */
1029 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1030 /* Byte position of the first byte in the range in this page. */
1031 /* starthere is an offset to the base address of the chip. */
1032 starthere = max(start, i * page_size);
1033 /* Length of bytes in the range in this page. */
1034 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1035 for (j = 0; j < lenhere; j += chunksize) {
1036 toread = min(chunksize, lenhere - j);
1037 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +00001038 if (rc)
1039 break;
1040 }
1041 if (rc)
1042 break;
1043 }
1044
1045 return rc;
1046}
1047
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +00001048int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +00001049{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001050 if (!spi_programmer[spi_controller].read) {
1051 fprintf(stderr, "%s called, but SPI read is unsupported on this"
1052 " hardware. Please report a bug.\n", __func__);
1053 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +00001054 }
1055
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001056 return spi_programmer[spi_controller].read(flash, buf, start, len);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +00001057}
1058
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001059/*
1060 * Program chip using byte programming. (SLOW!)
1061 * This is for chips which can only handle one byte writes
1062 * and for chips where memory mapped programming is impossible
1063 * (e.g. due to size constraints in IT87* for over 512 kB)
1064 */
1065int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
1066{
1067 int total_size = 1024 * flash->total_size;
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +00001068 int i, result = 0;
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001069
1070 spi_disable_blockprotect();
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +00001071 /* Erase first */
1072 printf("Erasing flash before programming... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001073 if (erase_flash(flash)) {
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +00001074 fprintf(stderr, "ERASE FAILED!\n");
1075 return -1;
1076 }
1077 printf("done.\n");
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001078 for (i = 0; i < total_size; i++) {
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +00001079 result = spi_byte_program(i, buf[i]);
1080 if (result)
1081 return 1;
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001082 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001083 programmer_delay(10);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001084 }
1085
1086 return 0;
1087}
1088
1089/*
1090 * Program chip using page (256 bytes) programming.
1091 * Some SPI masters can't do this, they use single byte programming instead.
1092 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +00001093int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +00001094{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001095 if (!spi_programmer[spi_controller].write_256) {
1096 fprintf(stderr, "%s called, but SPI page write is unsupported "
1097 " on this hardware. Please report a bug.\n", __func__);
1098 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +00001099 }
1100
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001101 return spi_programmer[spi_controller].write_256(flash, buf);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +00001102}
Peter Stugefd9217d2009-01-26 03:37:40 +00001103
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +00001104uint32_t spi_get_valid_read_addr(void)
1105{
1106 /* Need to return BBAR for ICH chipsets. */
1107 return 0;
1108}
1109
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001110int spi_aai_write(struct flashchip *flash, uint8_t *buf)
1111{
Peter Stugefd9217d2009-01-26 03:37:40 +00001112 uint32_t pos = 2, size = flash->total_size * 1024;
1113 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +00001114 int result;
1115
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +00001116 switch (spi_controller) {
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001117#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +00001118 case SPI_CONTROLLER_WBSIO:
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001119 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
1120 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001121 return spi_chip_write_1(flash, buf);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001122#endif
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001123 default:
1124 break;
Peter Stugefd9217d2009-01-26 03:37:40 +00001125 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001126 if (erase_flash(flash)) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +00001127 fprintf(stderr, "ERASE FAILED!\n");
1128 return -1;
1129 }
Carl-Daniel Hailfingerdb53ec52009-12-22 23:54:10 +00001130 /* FIXME: This will fail on ICH/VIA SPI. */
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +00001131 result = spi_write_enable();
1132 if (result)
1133 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001134 spi_send_command(6, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +00001135 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001136 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +00001137 while (pos < size) {
1138 w[1] = buf[pos++];
1139 w[2] = buf[pos++];
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001140 spi_send_command(3, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +00001141 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001142 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +00001143 }
1144 spi_write_disable();
1145 return 0;
1146}