blob: 3a3c77a00553b0aa0551d95ff88e08e40006f0b9 [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
43 { /* SPI_CONTROLLER_ICH7 */
44 .command = ich_spi_send_command,
45 .multicommand = ich_spi_send_multicommand,
46 .read = ich_spi_read,
47 .write_256 = ich_spi_write_256,
48 },
49
50 { /* SPI_CONTROLLER_ICH9 */
51 .command = ich_spi_send_command,
52 .multicommand = ich_spi_send_multicommand,
53 .read = ich_spi_read,
54 .write_256 = ich_spi_write_256,
55 },
56
57 { /* SPI_CONTROLLER_IT87XX */
58 .command = it8716f_spi_send_command,
59 .multicommand = default_spi_send_multicommand,
60 .read = it8716f_spi_chip_read,
61 .write_256 = it8716f_spi_chip_write_256,
62 },
63
64 { /* SPI_CONTROLLER_SB600 */
65 .command = sb600_spi_send_command,
66 .multicommand = default_spi_send_multicommand,
67 .read = sb600_spi_read,
68 .write_256 = sb600_spi_write_1,
69 },
70
71 { /* SPI_CONTROLLER_VIA */
72 .command = ich_spi_send_command,
73 .multicommand = ich_spi_send_multicommand,
74 .read = ich_spi_read,
75 .write_256 = ich_spi_write_256,
76 },
77
78 { /* SPI_CONTROLLER_WBSIO */
79 .command = wbsio_spi_send_command,
80 .multicommand = default_spi_send_multicommand,
81 .read = wbsio_spi_read,
82 .write_256 = wbsio_spi_write_1,
83 },
84
85 { /* SPI_CONTROLLER_FT2232 */
86 .command = ft2232_spi_send_command,
87 .multicommand = default_spi_send_multicommand,
88 .read = ft2232_spi_read,
89 .write_256 = ft2232_spi_write_256,
90 },
91
92 { /* SPI_CONTROLLER_DUMMY */
93 .command = dummy_spi_send_command,
94 .multicommand = default_spi_send_multicommand,
95 .read = NULL,
96 .write_256 = NULL,
97 },
98};
99
100
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000101int spi_send_command(unsigned int writecnt, unsigned int readcnt,
Uwe Hermann394131e2008-10-18 21:14:13 +0000102 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000103{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000104 if (!spi_programmer[spi_controller].command) {
105 fprintf(stderr, "%s called, but SPI is unsupported on this "
106 "hardware. Please report a bug.\n", __func__);
107 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000108 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000109
110 return spi_programmer[spi_controller].command(writecnt, readcnt,
111 writearr, readarr);
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000112}
113
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000114int spi_send_multicommand(struct spi_command *spicommands)
115{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000116 if (!spi_programmer[spi_controller].multicommand) {
117 fprintf(stderr, "%s called, but SPI is unsupported on this "
118 "hardware. Please report a bug.\n", __func__);
119 return 1;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000120 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000121
122 return spi_programmer[spi_controller].multicommand(spicommands);
123}
124
125int default_spi_send_command(unsigned int writecnt, unsigned int readcnt,
126 const unsigned char *writearr, unsigned char *readarr)
127{
128 struct spi_command cmd[] = {
129 {
130 .writecnt = writecnt,
131 .readcnt = readcnt,
132 .writearr = writearr,
133 .readarr = readarr,
134 }, {
135 .writecnt = 0,
136 .writearr = NULL,
137 .readcnt = 0,
138 .readarr = NULL,
139 }};
140
141 return spi_send_multicommand(cmd);
142}
143
144int default_spi_send_multicommand(struct spi_command *spicommands)
145{
146 int result = 0;
147 while ((spicommands->writecnt || spicommands->readcnt) && !result) {
148 result = spi_send_command(spicommands->writecnt, spicommands->readcnt,
149 spicommands->writearr, spicommands->readarr);
150 }
151 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000152}
153
Rudolf Marek48a85e42008-06-30 21:45:17 +0000154static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000155{
Uwe Hermann394131e2008-10-18 21:14:13 +0000156 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000157 int ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000158 int i;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000159
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000160 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000161 if (ret)
162 return ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000163 printf_debug("RDID returned");
164 for (i = 0; i < bytes; i++)
165 printf_debug(" 0x%02x", readarr[i]);
166 printf_debug("\n");
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000167 return 0;
168}
169
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000170static int spi_rems(unsigned char *readarr)
171{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000172 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
173 uint32_t readaddr;
174 int ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000175
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000176 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000177 if (ret == SPI_INVALID_ADDRESS) {
178 /* Find the lowest even address allowed for reads. */
179 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
180 cmd[1] = (readaddr >> 16) & 0xff,
181 cmd[2] = (readaddr >> 8) & 0xff,
182 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000183 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000184 }
185 if (ret)
186 return ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000187 printf_debug("REMS returned %02x %02x.\n", readarr[0], readarr[1]);
188 return 0;
189}
190
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000191static int spi_res(unsigned char *readarr)
192{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000193 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
194 uint32_t readaddr;
195 int ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000196
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000197 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000198 if (ret == SPI_INVALID_ADDRESS) {
199 /* Find the lowest even address allowed for reads. */
200 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
201 cmd[1] = (readaddr >> 16) & 0xff,
202 cmd[2] = (readaddr >> 8) & 0xff,
203 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000204 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000205 }
206 if (ret)
207 return ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000208 printf_debug("RES returned %02x.\n", readarr[0]);
209 return 0;
210}
211
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000212int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000213{
Uwe Hermann394131e2008-10-18 21:14:13 +0000214 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000215 int result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000216
217 /* Send WREN (Write Enable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000218 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000219
220 if (result)
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000221 printf_debug("%s failed\n", __func__);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000222
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000223 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000224}
225
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000226int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000227{
Uwe Hermann394131e2008-10-18 21:14:13 +0000228 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000229
230 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000231 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000232}
233
Rudolf Marek48a85e42008-06-30 21:45:17 +0000234static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000235{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000236 unsigned char readarr[4];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000237 uint32_t id1;
238 uint32_t id2;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000239
Rudolf Marek48a85e42008-06-30 21:45:17 +0000240 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000241 return 0;
242
243 if (!oddparity(readarr[0]))
244 printf_debug("RDID byte 0 parity violation.\n");
245
246 /* Check if this is a continuation vendor ID */
247 if (readarr[0] == 0x7f) {
248 if (!oddparity(readarr[1]))
249 printf_debug("RDID byte 1 parity violation.\n");
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000250 id1 = (readarr[0] << 8) | readarr[1];
251 id2 = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000252 if (bytes > 3) {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000253 id2 <<= 8;
254 id2 |= readarr[3];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000255 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000256 } else {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000257 id1 = readarr[0];
258 id2 = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000259 }
260
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000261 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000262
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000263 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000264 /* Print the status register to tell the
265 * user about possible write protection.
266 */
267 spi_prettyprint_status_register(flash);
268
269 return 1;
270 }
271
272 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000273 if (id1 == flash->manufacture_id &&
Peter Stugeda4e5f32008-06-24 01:22:03 +0000274 GENERIC_DEVICE_ID == flash->model_id)
275 return 1;
276
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000277 return 0;
278}
279
Uwe Hermann394131e2008-10-18 21:14:13 +0000280int probe_spi_rdid(struct flashchip *flash)
281{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000282 return probe_spi_rdid_generic(flash, 3);
283}
284
285/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000286int probe_spi_rdid4(struct flashchip *flash)
287{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000288 /* only some SPI chipsets support 4 bytes commands */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000289 switch (spi_controller) {
290 case SPI_CONTROLLER_ICH7:
291 case SPI_CONTROLLER_ICH9:
292 case SPI_CONTROLLER_VIA:
293 case SPI_CONTROLLER_SB600:
294 case SPI_CONTROLLER_WBSIO:
Paul Fox05dfbe62009-06-16 21:08:06 +0000295 case SPI_CONTROLLER_FT2232:
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000296 case SPI_CONTROLLER_DUMMY:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000297 return probe_spi_rdid_generic(flash, 4);
298 default:
299 printf_debug("4b ID not supported on this SPI controller\n");
300 }
301
302 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000303}
304
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000305int probe_spi_rems(struct flashchip *flash)
306{
307 unsigned char readarr[JEDEC_REMS_INSIZE];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000308 uint32_t id1, id2;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000309
310 if (spi_rems(readarr))
311 return 0;
312
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000313 id1 = readarr[0];
314 id2 = readarr[1];
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000315
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000316 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000317
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000318 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000319 /* Print the status register to tell the
320 * user about possible write protection.
321 */
322 spi_prettyprint_status_register(flash);
323
324 return 1;
325 }
326
327 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000328 if (id1 == flash->manufacture_id &&
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000329 GENERIC_DEVICE_ID == flash->model_id)
330 return 1;
331
332 return 0;
333}
334
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000335int probe_spi_res(struct flashchip *flash)
336{
337 unsigned char readarr[3];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000338 uint32_t id2;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000339
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000340 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
341 * In that case, RES is pointless.
342 */
343 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
344 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000345 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000346
Peter Stugeda4e5f32008-06-24 01:22:03 +0000347 if (spi_res(readarr))
348 return 0;
349
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000350 id2 = readarr[0];
351 printf_debug("%s: id 0x%x\n", __FUNCTION__, id2);
352 if (id2 != flash->model_id)
Peter Stugeda4e5f32008-06-24 01:22:03 +0000353 return 0;
354
355 /* Print the status register to tell the
356 * user about possible write protection.
357 */
358 spi_prettyprint_status_register(flash);
359 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000360}
361
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000362uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000363{
Uwe Hermann394131e2008-10-18 21:14:13 +0000364 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000365 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
Peter Stugebf196e92009-01-26 03:08:45 +0000366 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000367 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000368
369 /* Read Status Register */
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000370 if (spi_controller == SPI_CONTROLLER_SB600) { /* FIXME */
371 /* Workaround for SB600 hardware bug. Can be killed later. */
Jason Wanga3f04be2008-11-28 21:36:51 +0000372 return sb600_read_status_register();
Jason Wanga3f04be2008-11-28 21:36:51 +0000373 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000374 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
375 if (ret)
376 printf_debug("RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000377
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000378 return readarr[0];
379}
380
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000381/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000382void spi_prettyprint_status_register_common(uint8_t status)
383{
384 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000385 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000386 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000387 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000388 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000389 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000390 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000391 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000392 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000393 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000394 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000395 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000396}
397
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000398/* Prettyprint the status register. Works for
399 * ST M25P series
400 * MX MX25L series
401 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000402void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000403{
404 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000405 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000406 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000407 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000408 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000409}
410
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000411void spi_prettyprint_status_register_sst25(uint8_t status)
412{
413 printf_debug("Chip status register: Block Protect Write Disable "
414 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
415 printf_debug("Chip status register: Auto Address Increment Programming "
416 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
417 spi_prettyprint_status_register_common(status);
418}
419
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000420/* Prettyprint the status register. Works for
421 * SST 25VF016
422 */
423void spi_prettyprint_status_register_sst25vf016(uint8_t status)
424{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000425 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000426 "none",
427 "1F0000H-1FFFFFH",
428 "1E0000H-1FFFFFH",
429 "1C0000H-1FFFFFH",
430 "180000H-1FFFFFH",
431 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000432 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000433 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000434 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000435 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000436 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000437}
438
Peter Stuge5fecee42009-01-26 03:23:50 +0000439void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
440{
441 const char *bpt[] = {
442 "none",
443 "0x70000-0x7ffff",
444 "0x60000-0x7ffff",
445 "0x40000-0x7ffff",
446 "all blocks", "all blocks", "all blocks", "all blocks"
447 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000448 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000449 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000450 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000451}
452
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000453void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000454{
455 uint8_t status;
456
Peter Stugefa8c5502008-05-10 23:07:52 +0000457 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000458 printf_debug("Chip status register is %02x\n", status);
459 switch (flash->manufacture_id) {
460 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000461 if (((flash->model_id & 0xff00) == 0x2000) ||
462 ((flash->model_id & 0xff00) == 0x2500))
463 spi_prettyprint_status_register_st_m25p(status);
464 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000465 case MX_ID:
466 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000467 spi_prettyprint_status_register_st_m25p(status);
468 break;
469 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000470 switch (flash->model_id) {
471 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000472 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000473 break;
474 case 0x8d:
475 case 0x258d:
476 spi_prettyprint_status_register_sst25vf040b(status);
477 break;
Carl-Daniel Hailfinger5100a8a2009-05-13 22:51:27 +0000478 default:
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000479 spi_prettyprint_status_register_sst25(status);
480 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000481 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000482 break;
483 }
484}
Uwe Hermann394131e2008-10-18 21:14:13 +0000485
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000486int spi_chip_erase_60(struct flashchip *flash)
487{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000488 int result;
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000489 struct spi_command spicommands[] = {
490 {
491 .writecnt = JEDEC_WREN_OUTSIZE,
492 .writearr = (const unsigned char[]){ JEDEC_WREN },
493 .readcnt = 0,
494 .readarr = NULL,
495 }, {
496 .writecnt = JEDEC_CE_60_OUTSIZE,
497 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
498 .readcnt = 0,
499 .readarr = NULL,
500 }, {
501 .writecnt = 0,
502 .writearr = NULL,
503 .readcnt = 0,
504 .readarr = NULL,
505 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000506
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000507 result = spi_disable_blockprotect();
508 if (result) {
509 printf_debug("spi_disable_blockprotect failed\n");
510 return result;
511 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000512
513 result = spi_send_multicommand(spicommands);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000514 if (result) {
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000515 printf_debug("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000516 return result;
517 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000518 /* Wait until the Write-In-Progress bit is cleared.
519 * This usually takes 1-85 s, so wait in 1 s steps.
520 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000521 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000522 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000523 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000524 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
525 fprintf(stderr, "ERASE FAILED!\n");
526 return -1;
527 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000528 return 0;
529}
530
Peter Stugefa8c5502008-05-10 23:07:52 +0000531int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000532{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000533 int result;
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000534 struct spi_command spicommands[] = {
535 {
536 .writecnt = JEDEC_WREN_OUTSIZE,
537 .writearr = (const unsigned char[]){ JEDEC_WREN },
538 .readcnt = 0,
539 .readarr = NULL,
540 }, {
541 .writecnt = JEDEC_CE_C7_OUTSIZE,
542 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
543 .readcnt = 0,
544 .readarr = NULL,
545 }, {
546 .writecnt = 0,
547 .writearr = NULL,
548 .readcnt = 0,
549 .readarr = NULL,
550 }};
Uwe Hermann394131e2008-10-18 21:14:13 +0000551
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000552 result = spi_disable_blockprotect();
553 if (result) {
554 printf_debug("spi_disable_blockprotect failed\n");
555 return result;
556 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000557
558 result = spi_send_multicommand(spicommands);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000559 if (result) {
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000560 printf_debug("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000561 return result;
562 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000563 /* Wait until the Write-In-Progress bit is cleared.
564 * This usually takes 1-85 s, so wait in 1 s steps.
565 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000566 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000567 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000568 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000569 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
570 fprintf(stderr, "ERASE FAILED!\n");
571 return -1;
572 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000573 return 0;
574}
575
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000576int spi_chip_erase_60_c7(struct flashchip *flash)
577{
578 int result;
579 result = spi_chip_erase_60(flash);
580 if (result) {
581 printf_debug("spi_chip_erase_60 failed, trying c7\n");
582 result = spi_chip_erase_c7(flash);
583 }
584 return result;
585}
586
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000587int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000588{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000589 int result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000590 struct spi_command spicommands[] = {
591 {
592 .writecnt = JEDEC_WREN_OUTSIZE,
593 .writearr = (const unsigned char[]){ JEDEC_WREN },
594 .readcnt = 0,
595 .readarr = NULL,
596 }, {
597 .writecnt = JEDEC_BE_52_OUTSIZE,
598 .writearr = (const unsigned char[]){ JEDEC_BE_52, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
599 .readcnt = 0,
600 .readarr = NULL,
601 }, {
602 .writecnt = 0,
603 .writearr = NULL,
604 .readcnt = 0,
605 .readarr = NULL,
606 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000607
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000608 result = spi_send_multicommand(spicommands);
609 if (result) {
610 printf_debug("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000611 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000612 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000613 /* Wait until the Write-In-Progress bit is cleared.
614 * This usually takes 100-4000 ms, so wait in 100 ms steps.
615 */
616 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000617 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000618 if (check_erased_range(flash, addr, blocklen)) {
619 fprintf(stderr, "ERASE FAILED!\n");
620 return -1;
621 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000622 return 0;
623}
624
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000625/* Block size is usually
626 * 64k for Macronix
627 * 32k for SST
628 * 4-32k non-uniform for EON
629 */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000630int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000631{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000632 int result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000633 struct spi_command spicommands[] = {
634 {
635 .writecnt = JEDEC_WREN_OUTSIZE,
636 .writearr = (const unsigned char[]){ JEDEC_WREN },
637 .readcnt = 0,
638 .readarr = NULL,
639 }, {
640 .writecnt = JEDEC_BE_D8_OUTSIZE,
641 .writearr = (const unsigned char[]){ JEDEC_BE_D8, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
642 .readcnt = 0,
643 .readarr = NULL,
644 }, {
645 .writecnt = 0,
646 .writearr = NULL,
647 .readcnt = 0,
648 .readarr = NULL,
649 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000650
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000651 result = spi_send_multicommand(spicommands);
652 if (result) {
653 printf_debug("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000654 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000655 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000656 /* Wait until the Write-In-Progress bit is cleared.
657 * This usually takes 100-4000 ms, so wait in 100 ms steps.
658 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000659 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000660 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000661 if (check_erased_range(flash, addr, blocklen)) {
662 fprintf(stderr, "ERASE FAILED!\n");
663 return -1;
664 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000665 return 0;
666}
667
Stefan Reinauer424ed222008-10-29 22:13:20 +0000668int spi_chip_erase_d8(struct flashchip *flash)
669{
670 int i, rc = 0;
671 int total_size = flash->total_size * 1024;
672 int erase_size = 64 * 1024;
673
674 spi_disable_blockprotect();
675
676 printf("Erasing chip: \n");
677
678 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000679 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000680 if (rc) {
681 printf("Error erasing block at 0x%x\n", i);
682 break;
683 }
684 }
685
686 printf("\n");
687
688 return rc;
689}
690
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000691/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000692int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000693{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000694 int result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000695 struct spi_command spicommands[] = {
696 {
697 .writecnt = JEDEC_WREN_OUTSIZE,
698 .writearr = (const unsigned char[]){ JEDEC_WREN },
699 .readcnt = 0,
700 .readarr = NULL,
701 }, {
702 .writecnt = JEDEC_SE_OUTSIZE,
703 .writearr = (const unsigned char[]){ JEDEC_SE, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
704 .readcnt = 0,
705 .readarr = NULL,
706 }, {
707 .writecnt = 0,
708 .writearr = NULL,
709 .readcnt = 0,
710 .readarr = NULL,
711 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000712
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000713 result = spi_send_multicommand(spicommands);
714 if (result) {
715 printf_debug("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000716 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000717 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000718 /* Wait until the Write-In-Progress bit is cleared.
719 * This usually takes 15-800 ms, so wait in 10 ms steps.
720 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000721 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000722 programmer_delay(10 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000723 if (check_erased_range(flash, addr, blocklen)) {
724 fprintf(stderr, "ERASE FAILED!\n");
725 return -1;
726 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000727 return 0;
728}
729
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000730int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
731{
732 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
733 fprintf(stderr, "%s called with incorrect arguments\n", __func__);
734 return -1;
735 }
736 return spi_chip_erase_60(flash);
737}
738
739int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
740{
741 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
742 fprintf(stderr, "%s called with incorrect arguments\n", __func__);
743 return -1;
744 }
745 return spi_chip_erase_c7(flash);
746}
747
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000748int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000749{
750 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000751 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000752
753 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000754 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000755
756 if (result)
757 printf_debug("%s failed", __func__);
758 if (result == SPI_INVALID_OPCODE) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000759 switch (spi_controller) {
760 case SPI_CONTROLLER_ICH7:
761 case SPI_CONTROLLER_ICH9:
762 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000763 printf_debug(" due to SPI master limitation, ignoring"
764 " and hoping it will be run as PREOP\n");
765 return 0;
766 default:
767 break;
768 }
769 }
770 if (result)
771 printf_debug("\n");
772
773 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000774}
775
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000776/*
777 * This is according the SST25VF016 datasheet, who knows it is more
778 * generic that this...
779 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000780int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000781{
Uwe Hermann394131e2008-10-18 21:14:13 +0000782 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
783 { JEDEC_WRSR, (unsigned char)status };
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000784
785 /* Send WRSR (Write Status Register) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000786 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000787}
788
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000789int spi_byte_program(int addr, uint8_t byte)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000790{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000791 int result;
792 struct spi_command spicommands[] = {
793 {
794 .writecnt = JEDEC_WREN_OUTSIZE,
795 .writearr = (const unsigned char[]){ JEDEC_WREN },
796 .readcnt = 0,
797 .readarr = NULL,
798 }, {
799 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
800 .writearr = (const unsigned char[]){ JEDEC_BYTE_PROGRAM, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff), byte },
801 .readcnt = 0,
802 .readarr = NULL,
803 }, {
804 .writecnt = 0,
805 .writearr = NULL,
806 .readcnt = 0,
807 .readarr = NULL,
808 }};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000809
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000810 result = spi_send_multicommand(spicommands);
811 if (result) {
812 printf_debug("%s failed during command execution\n", __func__);
813 return result;
814 }
815 return result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000816}
817
Paul Foxeb3acef2009-06-12 08:10:33 +0000818int spi_nbyte_program(int address, uint8_t *bytes, int len)
819{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000820 int result;
821 /* FIXME: Switch to malloc based on len unless that kills speed. */
Paul Foxeb3acef2009-06-12 08:10:33 +0000822 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
823 JEDEC_BYTE_PROGRAM,
824 (address >> 16) & 0xff,
825 (address >> 8) & 0xff,
826 (address >> 0) & 0xff,
827 };
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000828 struct spi_command spicommands[] = {
829 {
830 .writecnt = JEDEC_WREN_OUTSIZE,
831 .writearr = (const unsigned char[]){ JEDEC_WREN },
832 .readcnt = 0,
833 .readarr = NULL,
834 }, {
835 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
836 .writearr = cmd,
837 .readcnt = 0,
838 .readarr = NULL,
839 }, {
840 .writecnt = 0,
841 .writearr = NULL,
842 .readcnt = 0,
843 .readarr = NULL,
844 }};
Paul Foxeb3acef2009-06-12 08:10:33 +0000845
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000846 if (!len) {
847 printf_debug ("%s called for zero-length write\n", __func__);
848 return 1;
849 }
Paul Foxeb3acef2009-06-12 08:10:33 +0000850 if (len > 256) {
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000851 printf_debug ("%s called for too long a write\n", __func__);
Paul Foxeb3acef2009-06-12 08:10:33 +0000852 return 1;
853 }
854
855 memcpy(&cmd[4], bytes, len);
856
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000857 result = spi_send_multicommand(spicommands);
858 if (result) {
859 printf_debug("%s failed during command execution\n", __func__);
860 return result;
861 }
862 return result;
Paul Foxeb3acef2009-06-12 08:10:33 +0000863}
864
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000865int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000866{
867 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000868 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000869
Peter Stugefa8c5502008-05-10 23:07:52 +0000870 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000871 /* If there is block protection in effect, unprotect it first. */
872 if ((status & 0x3c) != 0) {
873 printf_debug("Some block protection in effect, disabling\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000874 result = spi_write_status_enable();
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000875 if (result) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000876 printf_debug("spi_write_status_enable failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000877 return result;
878 }
879 result = spi_write_status_register(status & ~0x3c);
880 if (result) {
881 printf_debug("spi_write_status_register failed\n");
882 return result;
883 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000884 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000885 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000886}
887
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000888int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000889{
Uwe Hermann394131e2008-10-18 21:14:13 +0000890 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
891 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000892 (address >> 16) & 0xff,
893 (address >> 8) & 0xff,
894 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000895 };
896
897 /* Send Read */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000898 return spi_send_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000899}
900
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000901/*
902 * Read a complete flash chip.
903 * Each page is read separately in chunks with a maximum size of chunksize.
904 */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000905int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000906{
907 int rc = 0;
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000908 int i, j, starthere, lenhere;
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000909 int page_size = flash->page_size;
910 int toread;
911
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000912 /* Warning: This loop has a very unusual condition and body.
913 * The loop needs to go through each page with at least one affected
914 * byte. The lowest page number is (start / page_size) since that
915 * division rounds down. The highest page number we want is the page
916 * where the last byte of the range lives. That last byte has the
917 * address (start + len - 1), thus the highest page number is
918 * (start + len - 1) / page_size. Since we want to include that last
919 * page as well, the loop condition uses <=.
920 */
921 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
922 /* Byte position of the first byte in the range in this page. */
923 /* starthere is an offset to the base address of the chip. */
924 starthere = max(start, i * page_size);
925 /* Length of bytes in the range in this page. */
926 lenhere = min(start + len, (i + 1) * page_size) - starthere;
927 for (j = 0; j < lenhere; j += chunksize) {
928 toread = min(chunksize, lenhere - j);
929 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000930 if (rc)
931 break;
932 }
933 if (rc)
934 break;
935 }
936
937 return rc;
938}
939
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000940int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000941{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000942 if (!spi_programmer[spi_controller].read) {
943 fprintf(stderr, "%s called, but SPI read is unsupported on this"
944 " hardware. Please report a bug.\n", __func__);
945 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000946 }
947
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000948 return spi_programmer[spi_controller].read(flash, buf, start, len);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000949}
950
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000951/*
952 * Program chip using byte programming. (SLOW!)
953 * This is for chips which can only handle one byte writes
954 * and for chips where memory mapped programming is impossible
955 * (e.g. due to size constraints in IT87* for over 512 kB)
956 */
957int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
958{
959 int total_size = 1024 * flash->total_size;
960 int i;
961
962 spi_disable_blockprotect();
963 for (i = 0; i < total_size; i++) {
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000964 spi_byte_program(i, buf[i]);
965 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000966 programmer_delay(10);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000967 }
968
969 return 0;
970}
971
972/*
973 * Program chip using page (256 bytes) programming.
974 * Some SPI masters can't do this, they use single byte programming instead.
975 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +0000976int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000977{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000978 if (!spi_programmer[spi_controller].write_256) {
979 fprintf(stderr, "%s called, but SPI page write is unsupported "
980 " on this hardware. Please report a bug.\n", __func__);
981 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000982 }
983
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000984 return spi_programmer[spi_controller].write_256(flash, buf);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000985}
Peter Stugefd9217d2009-01-26 03:37:40 +0000986
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000987uint32_t spi_get_valid_read_addr(void)
988{
989 /* Need to return BBAR for ICH chipsets. */
990 return 0;
991}
992
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000993int spi_aai_write(struct flashchip *flash, uint8_t *buf)
994{
Peter Stugefd9217d2009-01-26 03:37:40 +0000995 uint32_t pos = 2, size = flash->total_size * 1024;
996 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000997 int result;
998
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000999 switch (spi_controller) {
1000 case SPI_CONTROLLER_WBSIO:
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001001 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
1002 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001003 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001004 default:
1005 break;
Peter Stugefd9217d2009-01-26 03:37:40 +00001006 }
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +00001007 if (flash->erase(flash)) {
1008 fprintf(stderr, "ERASE FAILED!\n");
1009 return -1;
1010 }
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +00001011 result = spi_write_enable();
1012 if (result)
1013 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001014 spi_send_command(6, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +00001015 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001016 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +00001017 while (pos < size) {
1018 w[1] = buf[pos++];
1019 w[2] = buf[pos++];
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001020 spi_send_command(3, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +00001021 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001022 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +00001023 }
1024 spi_write_disable();
1025 return 0;
1026}