blob: 73709f2fab4416df0a81454dab2c405d6bd14a03 [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
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +000085#if FT2232_SPI_SUPPORT == 1
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000086 { /* SPI_CONTROLLER_FT2232 */
87 .command = ft2232_spi_send_command,
88 .multicommand = default_spi_send_multicommand,
89 .read = ft2232_spi_read,
90 .write_256 = ft2232_spi_write_256,
91 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +000092#endif
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000093
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000094#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000095 { /* SPI_CONTROLLER_DUMMY */
96 .command = dummy_spi_send_command,
97 .multicommand = default_spi_send_multicommand,
98 .read = NULL,
99 .write_256 = NULL,
100 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000101#endif
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000102
103 {}, /* This entry corresponds to SPI_CONTROLLER_INVALID. */
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000104};
105
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000106const int spi_programmer_count = ARRAY_SIZE(spi_programmer);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000107
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000108int spi_send_command(unsigned int writecnt, unsigned int readcnt,
Uwe Hermann394131e2008-10-18 21:14:13 +0000109 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000110{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000111 if (!spi_programmer[spi_controller].command) {
112 fprintf(stderr, "%s called, but SPI is unsupported on this "
113 "hardware. Please report a bug.\n", __func__);
114 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000115 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000116
117 return spi_programmer[spi_controller].command(writecnt, readcnt,
118 writearr, readarr);
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000119}
120
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000121int spi_send_multicommand(struct spi_command *spicommands)
122{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000123 if (!spi_programmer[spi_controller].multicommand) {
124 fprintf(stderr, "%s called, but SPI is unsupported on this "
125 "hardware. Please report a bug.\n", __func__);
126 return 1;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000127 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000128
129 return spi_programmer[spi_controller].multicommand(spicommands);
130}
131
132int default_spi_send_command(unsigned int writecnt, unsigned int readcnt,
133 const unsigned char *writearr, unsigned char *readarr)
134{
135 struct spi_command cmd[] = {
136 {
137 .writecnt = writecnt,
138 .readcnt = readcnt,
139 .writearr = writearr,
140 .readarr = readarr,
141 }, {
142 .writecnt = 0,
143 .writearr = NULL,
144 .readcnt = 0,
145 .readarr = NULL,
146 }};
147
148 return spi_send_multicommand(cmd);
149}
150
151int default_spi_send_multicommand(struct spi_command *spicommands)
152{
153 int result = 0;
154 while ((spicommands->writecnt || spicommands->readcnt) && !result) {
155 result = spi_send_command(spicommands->writecnt, spicommands->readcnt,
156 spicommands->writearr, spicommands->readarr);
Carl-Daniel Hailfinger5b2f52f2009-08-03 09:35:20 +0000157 spicommands++;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000158 }
159 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000160}
161
Rudolf Marek48a85e42008-06-30 21:45:17 +0000162static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000163{
Uwe Hermann394131e2008-10-18 21:14:13 +0000164 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000165 int ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000166 int i;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000167
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000168 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000169 if (ret)
170 return ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000171 printf_debug("RDID returned");
172 for (i = 0; i < bytes; i++)
173 printf_debug(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000174 printf_debug(". ");
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000175 return 0;
176}
177
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000178static int spi_rems(unsigned char *readarr)
179{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000180 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
181 uint32_t readaddr;
182 int ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000183
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000184 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000185 if (ret == SPI_INVALID_ADDRESS) {
186 /* Find the lowest even address allowed for reads. */
187 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
188 cmd[1] = (readaddr >> 16) & 0xff,
189 cmd[2] = (readaddr >> 8) & 0xff,
190 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000191 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000192 }
193 if (ret)
194 return ret;
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000195 printf_debug("REMS returned %02x %02x. ", readarr[0], readarr[1]);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000196 return 0;
197}
198
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000199static int spi_res(unsigned char *readarr)
200{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000201 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
202 uint32_t readaddr;
203 int ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000204
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000205 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000206 if (ret == SPI_INVALID_ADDRESS) {
207 /* Find the lowest even address allowed for reads. */
208 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
209 cmd[1] = (readaddr >> 16) & 0xff,
210 cmd[2] = (readaddr >> 8) & 0xff,
211 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000212 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000213 }
214 if (ret)
215 return ret;
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000216 printf_debug("RES returned %02x. ", readarr[0]);
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000217 return 0;
218}
219
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000220int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000221{
Uwe Hermann394131e2008-10-18 21:14:13 +0000222 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000223 int result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000224
225 /* Send WREN (Write Enable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000226 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000227
228 if (result)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000229 fprintf(stderr, "%s failed\n", __func__);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000230
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000231 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000232}
233
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000234int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000235{
Uwe Hermann394131e2008-10-18 21:14:13 +0000236 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000237
238 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000239 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000240}
241
Rudolf Marek48a85e42008-06-30 21:45:17 +0000242static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000243{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000244 unsigned char readarr[4];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000245 uint32_t id1;
246 uint32_t id2;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000247
Rudolf Marek48a85e42008-06-30 21:45:17 +0000248 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000249 return 0;
250
251 if (!oddparity(readarr[0]))
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000252 printf_debug("RDID byte 0 parity violation. ");
Peter Stugeda4e5f32008-06-24 01:22:03 +0000253
254 /* Check if this is a continuation vendor ID */
255 if (readarr[0] == 0x7f) {
256 if (!oddparity(readarr[1]))
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000257 printf_debug("RDID byte 1 parity violation. ");
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000258 id1 = (readarr[0] << 8) | readarr[1];
259 id2 = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000260 if (bytes > 3) {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000261 id2 <<= 8;
262 id2 |= readarr[3];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000263 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000264 } else {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000265 id1 = readarr[0];
266 id2 = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000267 }
268
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000269 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000270
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000271 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000272 /* Print the status register to tell the
273 * user about possible write protection.
274 */
275 spi_prettyprint_status_register(flash);
276
277 return 1;
278 }
279
280 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000281 if (id1 == flash->manufacture_id &&
Peter Stugeda4e5f32008-06-24 01:22:03 +0000282 GENERIC_DEVICE_ID == flash->model_id)
283 return 1;
284
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000285 return 0;
286}
287
Uwe Hermann394131e2008-10-18 21:14:13 +0000288int probe_spi_rdid(struct flashchip *flash)
289{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000290 return probe_spi_rdid_generic(flash, 3);
291}
292
293/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000294int probe_spi_rdid4(struct flashchip *flash)
295{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000296 /* only some SPI chipsets support 4 bytes commands */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000297 switch (spi_controller) {
298 case SPI_CONTROLLER_ICH7:
299 case SPI_CONTROLLER_ICH9:
300 case SPI_CONTROLLER_VIA:
301 case SPI_CONTROLLER_SB600:
302 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000303#if FT2232_SPI_SUPPORT == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000304 case SPI_CONTROLLER_FT2232:
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000305#endif
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000306#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000307 case SPI_CONTROLLER_DUMMY:
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000308#endif
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000309 return probe_spi_rdid_generic(flash, 4);
310 default:
311 printf_debug("4b ID not supported on this SPI controller\n");
312 }
313
314 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000315}
316
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000317int probe_spi_rems(struct flashchip *flash)
318{
319 unsigned char readarr[JEDEC_REMS_INSIZE];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000320 uint32_t id1, id2;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000321
322 if (spi_rems(readarr))
323 return 0;
324
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000325 id1 = readarr[0];
326 id2 = readarr[1];
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000327
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000328 printf_debug("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000329
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000330 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000331 /* Print the status register to tell the
332 * user about possible write protection.
333 */
334 spi_prettyprint_status_register(flash);
335
336 return 1;
337 }
338
339 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000340 if (id1 == flash->manufacture_id &&
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000341 GENERIC_DEVICE_ID == flash->model_id)
342 return 1;
343
344 return 0;
345}
346
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000347int probe_spi_res(struct flashchip *flash)
348{
349 unsigned char readarr[3];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000350 uint32_t id2;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000351
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000352 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
353 * In that case, RES is pointless.
354 */
355 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
356 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000357 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000358
Peter Stugeda4e5f32008-06-24 01:22:03 +0000359 if (spi_res(readarr))
360 return 0;
361
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000362 id2 = readarr[0];
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000363 printf_debug("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000364 if (id2 != flash->model_id)
Peter Stugeda4e5f32008-06-24 01:22:03 +0000365 return 0;
366
367 /* Print the status register to tell the
368 * user about possible write protection.
369 */
370 spi_prettyprint_status_register(flash);
371 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000372}
373
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000374uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000375{
Uwe Hermann394131e2008-10-18 21:14:13 +0000376 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000377 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
Peter Stugebf196e92009-01-26 03:08:45 +0000378 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000379 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000380
381 /* Read Status Register */
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000382 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
383 if (ret)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000384 fprintf(stderr, "RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000385
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000386 return readarr[0];
387}
388
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000389/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000390void spi_prettyprint_status_register_common(uint8_t status)
391{
392 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000393 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000394 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000395 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000396 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000397 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000398 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000399 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000400 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000401 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000402 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000403 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000404}
405
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000406/* Prettyprint the status register. Works for
407 * ST M25P series
408 * MX MX25L series
409 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000410void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000411{
412 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000413 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000414 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000415 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000416 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000417}
418
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000419void spi_prettyprint_status_register_sst25(uint8_t status)
420{
421 printf_debug("Chip status register: Block Protect Write Disable "
422 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
423 printf_debug("Chip status register: Auto Address Increment Programming "
424 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
425 spi_prettyprint_status_register_common(status);
426}
427
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000428/* Prettyprint the status register. Works for
429 * SST 25VF016
430 */
431void spi_prettyprint_status_register_sst25vf016(uint8_t status)
432{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000433 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000434 "none",
435 "1F0000H-1FFFFFH",
436 "1E0000H-1FFFFFH",
437 "1C0000H-1FFFFFH",
438 "180000H-1FFFFFH",
439 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000440 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000441 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000442 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000443 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000444 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000445}
446
Peter Stuge5fecee42009-01-26 03:23:50 +0000447void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
448{
449 const char *bpt[] = {
450 "none",
451 "0x70000-0x7ffff",
452 "0x60000-0x7ffff",
453 "0x40000-0x7ffff",
454 "all blocks", "all blocks", "all blocks", "all blocks"
455 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000456 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000457 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000458 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000459}
460
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000461void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000462{
463 uint8_t status;
464
Peter Stugefa8c5502008-05-10 23:07:52 +0000465 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000466 printf_debug("Chip status register is %02x\n", status);
467 switch (flash->manufacture_id) {
468 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000469 if (((flash->model_id & 0xff00) == 0x2000) ||
470 ((flash->model_id & 0xff00) == 0x2500))
471 spi_prettyprint_status_register_st_m25p(status);
472 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000473 case MX_ID:
474 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000475 spi_prettyprint_status_register_st_m25p(status);
476 break;
477 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000478 switch (flash->model_id) {
479 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000480 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000481 break;
482 case 0x8d:
483 case 0x258d:
484 spi_prettyprint_status_register_sst25vf040b(status);
485 break;
Carl-Daniel Hailfinger5100a8a2009-05-13 22:51:27 +0000486 default:
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000487 spi_prettyprint_status_register_sst25(status);
488 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000489 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000490 break;
491 }
492}
Uwe Hermann394131e2008-10-18 21:14:13 +0000493
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000494int spi_chip_erase_60(struct flashchip *flash)
495{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000496 int result;
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000497 struct spi_command spicommands[] = {
498 {
499 .writecnt = JEDEC_WREN_OUTSIZE,
500 .writearr = (const unsigned char[]){ JEDEC_WREN },
501 .readcnt = 0,
502 .readarr = NULL,
503 }, {
504 .writecnt = JEDEC_CE_60_OUTSIZE,
505 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
506 .readcnt = 0,
507 .readarr = NULL,
508 }, {
509 .writecnt = 0,
510 .writearr = NULL,
511 .readcnt = 0,
512 .readarr = NULL,
513 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000514
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000515 result = spi_disable_blockprotect();
516 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000517 fprintf(stderr, "spi_disable_blockprotect failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000518 return result;
519 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000520
521 result = spi_send_multicommand(spicommands);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000522 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000523 fprintf(stderr, "%s failed during command execution\n",
524 __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000525 return result;
526 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000527 /* Wait until the Write-In-Progress bit is cleared.
528 * This usually takes 1-85 s, so wait in 1 s steps.
529 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000530 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000531 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000532 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000533 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
534 fprintf(stderr, "ERASE FAILED!\n");
535 return -1;
536 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000537 return 0;
538}
539
Peter Stugefa8c5502008-05-10 23:07:52 +0000540int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000541{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000542 int result;
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000543 struct spi_command spicommands[] = {
544 {
545 .writecnt = JEDEC_WREN_OUTSIZE,
546 .writearr = (const unsigned char[]){ JEDEC_WREN },
547 .readcnt = 0,
548 .readarr = NULL,
549 }, {
550 .writecnt = JEDEC_CE_C7_OUTSIZE,
551 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
552 .readcnt = 0,
553 .readarr = NULL,
554 }, {
555 .writecnt = 0,
556 .writearr = NULL,
557 .readcnt = 0,
558 .readarr = NULL,
559 }};
Uwe Hermann394131e2008-10-18 21:14:13 +0000560
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000561 result = spi_disable_blockprotect();
562 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000563 fprintf(stderr, "spi_disable_blockprotect failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000564 return result;
565 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000566
567 result = spi_send_multicommand(spicommands);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000568 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000569 fprintf(stderr, "%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000570 return result;
571 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000572 /* Wait until the Write-In-Progress bit is cleared.
573 * This usually takes 1-85 s, so wait in 1 s steps.
574 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000575 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000576 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000577 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000578 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
579 fprintf(stderr, "ERASE FAILED!\n");
580 return -1;
581 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000582 return 0;
583}
584
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000585int spi_chip_erase_60_c7(struct flashchip *flash)
586{
587 int result;
588 result = spi_chip_erase_60(flash);
589 if (result) {
590 printf_debug("spi_chip_erase_60 failed, trying c7\n");
591 result = spi_chip_erase_c7(flash);
592 }
593 return result;
594}
595
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000596int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000597{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000598 int result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000599 struct spi_command spicommands[] = {
600 {
601 .writecnt = JEDEC_WREN_OUTSIZE,
602 .writearr = (const unsigned char[]){ JEDEC_WREN },
603 .readcnt = 0,
604 .readarr = NULL,
605 }, {
606 .writecnt = JEDEC_BE_52_OUTSIZE,
607 .writearr = (const unsigned char[]){ JEDEC_BE_52, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
608 .readcnt = 0,
609 .readarr = NULL,
610 }, {
611 .writecnt = 0,
612 .writearr = NULL,
613 .readcnt = 0,
614 .readarr = NULL,
615 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000616
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000617 result = spi_send_multicommand(spicommands);
618 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000619 fprintf(stderr, "%s failed during command execution\n",
620 __func__);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000621 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000622 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000623 /* Wait until the Write-In-Progress bit is cleared.
624 * This usually takes 100-4000 ms, so wait in 100 ms steps.
625 */
626 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000627 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000628 if (check_erased_range(flash, addr, blocklen)) {
629 fprintf(stderr, "ERASE FAILED!\n");
630 return -1;
631 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000632 return 0;
633}
634
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000635/* Block size is usually
636 * 64k for Macronix
637 * 32k for SST
638 * 4-32k non-uniform for EON
639 */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000640int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000641{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000642 int result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000643 struct spi_command spicommands[] = {
644 {
645 .writecnt = JEDEC_WREN_OUTSIZE,
646 .writearr = (const unsigned char[]){ JEDEC_WREN },
647 .readcnt = 0,
648 .readarr = NULL,
649 }, {
650 .writecnt = JEDEC_BE_D8_OUTSIZE,
651 .writearr = (const unsigned char[]){ JEDEC_BE_D8, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
652 .readcnt = 0,
653 .readarr = NULL,
654 }, {
655 .writecnt = 0,
656 .writearr = NULL,
657 .readcnt = 0,
658 .readarr = NULL,
659 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000660
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000661 result = spi_send_multicommand(spicommands);
662 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000663 fprintf(stderr, "%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000664 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000665 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000666 /* Wait until the Write-In-Progress bit is cleared.
667 * This usually takes 100-4000 ms, so wait in 100 ms steps.
668 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000669 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000670 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000671 if (check_erased_range(flash, addr, blocklen)) {
672 fprintf(stderr, "ERASE FAILED!\n");
673 return -1;
674 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000675 return 0;
676}
677
Stefan Reinauer424ed222008-10-29 22:13:20 +0000678int spi_chip_erase_d8(struct flashchip *flash)
679{
680 int i, rc = 0;
681 int total_size = flash->total_size * 1024;
682 int erase_size = 64 * 1024;
683
684 spi_disable_blockprotect();
685
686 printf("Erasing chip: \n");
687
688 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000689 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000690 if (rc) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000691 fprintf(stderr, "Error erasing block at 0x%x\n", i);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000692 break;
693 }
694 }
695
696 printf("\n");
697
698 return rc;
699}
700
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000701/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000702int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000703{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000704 int result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000705 struct spi_command spicommands[] = {
706 {
707 .writecnt = JEDEC_WREN_OUTSIZE,
708 .writearr = (const unsigned char[]){ JEDEC_WREN },
709 .readcnt = 0,
710 .readarr = NULL,
711 }, {
712 .writecnt = JEDEC_SE_OUTSIZE,
713 .writearr = (const unsigned char[]){ JEDEC_SE, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
714 .readcnt = 0,
715 .readarr = NULL,
716 }, {
717 .writecnt = 0,
718 .writearr = NULL,
719 .readcnt = 0,
720 .readarr = NULL,
721 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000722
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000723 result = spi_send_multicommand(spicommands);
724 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000725 fprintf(stderr, "%s failed during command execution\n",
726 __func__);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000727 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000728 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000729 /* Wait until the Write-In-Progress bit is cleared.
730 * This usually takes 15-800 ms, so wait in 10 ms steps.
731 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000732 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000733 programmer_delay(10 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000734 if (check_erased_range(flash, addr, blocklen)) {
735 fprintf(stderr, "ERASE FAILED!\n");
736 return -1;
737 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000738 return 0;
739}
740
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000741int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
742{
743 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000744 fprintf(stderr, "%s called with incorrect arguments\n",
745 __func__);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000746 return -1;
747 }
748 return spi_chip_erase_60(flash);
749}
750
751int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
752{
753 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000754 fprintf(stderr, "%s called with incorrect arguments\n",
755 __func__);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000756 return -1;
757 }
758 return spi_chip_erase_c7(flash);
759}
760
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000761int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000762{
763 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000764 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000765
766 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000767 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000768
769 if (result)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000770 fprintf(stderr, "%s failed\n", __func__);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000771
772 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000773}
774
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000775/*
776 * This is according the SST25VF016 datasheet, who knows it is more
777 * generic that this...
778 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000779int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000780{
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000781 int result;
782 struct spi_command spicommands[] = {
783 {
784 .writecnt = JEDEC_EWSR_OUTSIZE,
785 .writearr = (const unsigned char[]){ JEDEC_EWSR },
786 .readcnt = 0,
787 .readarr = NULL,
788 }, {
789 .writecnt = JEDEC_WRSR_OUTSIZE,
790 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
791 .readcnt = 0,
792 .readarr = NULL,
793 }, {
794 .writecnt = 0,
795 .writearr = NULL,
796 .readcnt = 0,
797 .readarr = NULL,
798 }};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000799
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000800 result = spi_send_multicommand(spicommands);
801 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000802 fprintf(stderr, "%s failed during command execution\n",
803 __func__);
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000804 }
805 return result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000806}
807
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000808int spi_byte_program(int addr, uint8_t byte)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000809{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000810 int result;
811 struct spi_command spicommands[] = {
812 {
813 .writecnt = JEDEC_WREN_OUTSIZE,
814 .writearr = (const unsigned char[]){ JEDEC_WREN },
815 .readcnt = 0,
816 .readarr = NULL,
817 }, {
818 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
819 .writearr = (const unsigned char[]){ JEDEC_BYTE_PROGRAM, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff), byte },
820 .readcnt = 0,
821 .readarr = NULL,
822 }, {
823 .writecnt = 0,
824 .writearr = NULL,
825 .readcnt = 0,
826 .readarr = NULL,
827 }};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000828
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000829 result = spi_send_multicommand(spicommands);
830 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000831 fprintf(stderr, "%s failed during command execution\n",
832 __func__);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000833 }
834 return result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000835}
836
Paul Foxeb3acef2009-06-12 08:10:33 +0000837int spi_nbyte_program(int address, uint8_t *bytes, int len)
838{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000839 int result;
840 /* FIXME: Switch to malloc based on len unless that kills speed. */
Paul Foxeb3acef2009-06-12 08:10:33 +0000841 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
842 JEDEC_BYTE_PROGRAM,
843 (address >> 16) & 0xff,
844 (address >> 8) & 0xff,
845 (address >> 0) & 0xff,
846 };
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000847 struct spi_command spicommands[] = {
848 {
849 .writecnt = JEDEC_WREN_OUTSIZE,
850 .writearr = (const unsigned char[]){ JEDEC_WREN },
851 .readcnt = 0,
852 .readarr = NULL,
853 }, {
854 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
855 .writearr = cmd,
856 .readcnt = 0,
857 .readarr = NULL,
858 }, {
859 .writecnt = 0,
860 .writearr = NULL,
861 .readcnt = 0,
862 .readarr = NULL,
863 }};
Paul Foxeb3acef2009-06-12 08:10:33 +0000864
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000865 if (!len) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000866 fprintf(stderr, "%s called for zero-length write\n", __func__);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000867 return 1;
868 }
Paul Foxeb3acef2009-06-12 08:10:33 +0000869 if (len > 256) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000870 fprintf(stderr, "%s called for too long a write\n", __func__);
Paul Foxeb3acef2009-06-12 08:10:33 +0000871 return 1;
872 }
873
874 memcpy(&cmd[4], bytes, len);
875
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000876 result = spi_send_multicommand(spicommands);
877 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000878 fprintf(stderr, "%s failed during command execution\n",
879 __func__);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000880 }
881 return result;
Paul Foxeb3acef2009-06-12 08:10:33 +0000882}
883
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000884int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000885{
886 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000887 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000888
Peter Stugefa8c5502008-05-10 23:07:52 +0000889 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000890 /* If there is block protection in effect, unprotect it first. */
891 if ((status & 0x3c) != 0) {
892 printf_debug("Some block protection in effect, disabling\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000893 result = spi_write_status_register(status & ~0x3c);
894 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000895 fprintf(stderr, "spi_write_status_register failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000896 return result;
897 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000898 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000899 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000900}
901
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000902int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000903{
Uwe Hermann394131e2008-10-18 21:14:13 +0000904 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
905 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000906 (address >> 16) & 0xff,
907 (address >> 8) & 0xff,
908 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000909 };
910
911 /* Send Read */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000912 return spi_send_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000913}
914
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000915/*
916 * Read a complete flash chip.
917 * Each page is read separately in chunks with a maximum size of chunksize.
918 */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000919int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000920{
921 int rc = 0;
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000922 int i, j, starthere, lenhere;
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000923 int page_size = flash->page_size;
924 int toread;
925
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000926 /* Warning: This loop has a very unusual condition and body.
927 * The loop needs to go through each page with at least one affected
928 * byte. The lowest page number is (start / page_size) since that
929 * division rounds down. The highest page number we want is the page
930 * where the last byte of the range lives. That last byte has the
931 * address (start + len - 1), thus the highest page number is
932 * (start + len - 1) / page_size. Since we want to include that last
933 * page as well, the loop condition uses <=.
934 */
935 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
936 /* Byte position of the first byte in the range in this page. */
937 /* starthere is an offset to the base address of the chip. */
938 starthere = max(start, i * page_size);
939 /* Length of bytes in the range in this page. */
940 lenhere = min(start + len, (i + 1) * page_size) - starthere;
941 for (j = 0; j < lenhere; j += chunksize) {
942 toread = min(chunksize, lenhere - j);
943 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000944 if (rc)
945 break;
946 }
947 if (rc)
948 break;
949 }
950
951 return rc;
952}
953
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000954int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000955{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000956 if (!spi_programmer[spi_controller].read) {
957 fprintf(stderr, "%s called, but SPI read is unsupported on this"
958 " hardware. Please report a bug.\n", __func__);
959 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000960 }
961
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000962 return spi_programmer[spi_controller].read(flash, buf, start, len);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000963}
964
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000965/*
966 * Program chip using byte programming. (SLOW!)
967 * This is for chips which can only handle one byte writes
968 * and for chips where memory mapped programming is impossible
969 * (e.g. due to size constraints in IT87* for over 512 kB)
970 */
971int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
972{
973 int total_size = 1024 * flash->total_size;
974 int i;
975
976 spi_disable_blockprotect();
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000977 /* Erase first */
978 printf("Erasing flash before programming... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000979 if (erase_flash(flash)) {
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000980 fprintf(stderr, "ERASE FAILED!\n");
981 return -1;
982 }
983 printf("done.\n");
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000984 for (i = 0; i < total_size; i++) {
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000985 spi_byte_program(i, buf[i]);
986 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000987 programmer_delay(10);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000988 }
989
990 return 0;
991}
992
993/*
994 * Program chip using page (256 bytes) programming.
995 * Some SPI masters can't do this, they use single byte programming instead.
996 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +0000997int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000998{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000999 if (!spi_programmer[spi_controller].write_256) {
1000 fprintf(stderr, "%s called, but SPI page write is unsupported "
1001 " on this hardware. Please report a bug.\n", __func__);
1002 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +00001003 }
1004
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001005 return spi_programmer[spi_controller].write_256(flash, buf);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +00001006}
Peter Stugefd9217d2009-01-26 03:37:40 +00001007
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +00001008uint32_t spi_get_valid_read_addr(void)
1009{
1010 /* Need to return BBAR for ICH chipsets. */
1011 return 0;
1012}
1013
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001014int spi_aai_write(struct flashchip *flash, uint8_t *buf)
1015{
Peter Stugefd9217d2009-01-26 03:37:40 +00001016 uint32_t pos = 2, size = flash->total_size * 1024;
1017 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +00001018 int result;
1019
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +00001020 switch (spi_controller) {
1021 case SPI_CONTROLLER_WBSIO:
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001022 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
1023 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001024 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001025 default:
1026 break;
Peter Stugefd9217d2009-01-26 03:37:40 +00001027 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001028 if (erase_flash(flash)) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +00001029 fprintf(stderr, "ERASE FAILED!\n");
1030 return -1;
1031 }
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +00001032 result = spi_write_enable();
1033 if (result)
1034 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001035 spi_send_command(6, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +00001036 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001037 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +00001038 while (pos < size) {
1039 w[1] = buf[pos++];
1040 w[2] = buf[pos++];
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001041 spi_send_command(3, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +00001042 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001043 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +00001044 }
1045 spi_write_disable();
1046 return 0;
1047}