blob: 14beed7fc83fe867551cec5afec94ce739821d86 [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
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000103#if BUSPIRATE_SPI_SUPPORT == 1
104 { /* SPI_CONTROLLER_BUSPIRATE */
105 .command = buspirate_spi_send_command,
106 .multicommand = default_spi_send_multicommand,
107 .read = buspirate_spi_read,
108 .write_256 = spi_chip_write_1,
109 },
110#endif
111
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000112 {}, /* This entry corresponds to SPI_CONTROLLER_INVALID. */
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000113};
114
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000115const int spi_programmer_count = ARRAY_SIZE(spi_programmer);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000116
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000117int spi_send_command(unsigned int writecnt, unsigned int readcnt,
Uwe Hermann394131e2008-10-18 21:14:13 +0000118 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000119{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000120 if (!spi_programmer[spi_controller].command) {
121 fprintf(stderr, "%s called, but SPI is unsupported on this "
122 "hardware. Please report a bug.\n", __func__);
123 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000124 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000125
126 return spi_programmer[spi_controller].command(writecnt, readcnt,
127 writearr, readarr);
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000128}
129
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000130int spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000131{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000132 if (!spi_programmer[spi_controller].multicommand) {
133 fprintf(stderr, "%s called, but SPI is unsupported on this "
134 "hardware. Please report a bug.\n", __func__);
135 return 1;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000136 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000137
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000138 return spi_programmer[spi_controller].multicommand(cmds);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000139}
140
141int default_spi_send_command(unsigned int writecnt, unsigned int readcnt,
142 const unsigned char *writearr, unsigned char *readarr)
143{
144 struct spi_command cmd[] = {
145 {
146 .writecnt = writecnt,
147 .readcnt = readcnt,
148 .writearr = writearr,
149 .readarr = readarr,
150 }, {
151 .writecnt = 0,
152 .writearr = NULL,
153 .readcnt = 0,
154 .readarr = NULL,
155 }};
156
157 return spi_send_multicommand(cmd);
158}
159
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000160int default_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000161{
162 int result = 0;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000163 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
164 result = spi_send_command(cmds->writecnt, cmds->readcnt,
165 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000166 }
167 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000168}
169
Rudolf Marek48a85e42008-06-30 21:45:17 +0000170static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000171{
Uwe Hermann394131e2008-10-18 21:14:13 +0000172 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000173 int ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000174 int i;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000175
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000176 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000177 if (ret)
178 return ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000179 printf_debug("RDID returned");
180 for (i = 0; i < bytes; i++)
181 printf_debug(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000182 printf_debug(". ");
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000183 return 0;
184}
185
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000186static int spi_rems(unsigned char *readarr)
187{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000188 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
189 uint32_t readaddr;
190 int ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000191
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000192 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000193 if (ret == SPI_INVALID_ADDRESS) {
194 /* Find the lowest even address allowed for reads. */
195 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
196 cmd[1] = (readaddr >> 16) & 0xff,
197 cmd[2] = (readaddr >> 8) & 0xff,
198 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000199 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000200 }
201 if (ret)
202 return ret;
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000203 printf_debug("REMS returned %02x %02x. ", readarr[0], readarr[1]);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000204 return 0;
205}
206
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000207static int spi_res(unsigned char *readarr)
208{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000209 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
210 uint32_t readaddr;
211 int ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000212
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000213 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000214 if (ret == SPI_INVALID_ADDRESS) {
215 /* Find the lowest even address allowed for reads. */
216 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
217 cmd[1] = (readaddr >> 16) & 0xff,
218 cmd[2] = (readaddr >> 8) & 0xff,
219 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000220 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000221 }
222 if (ret)
223 return ret;
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000224 printf_debug("RES returned %02x. ", readarr[0]);
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000225 return 0;
226}
227
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000228int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000229{
Uwe Hermann394131e2008-10-18 21:14:13 +0000230 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000231 int result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000232
233 /* Send WREN (Write Enable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000234 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000235
236 if (result)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000237 fprintf(stderr, "%s failed\n", __func__);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000238
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000239 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000240}
241
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000242int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000243{
Uwe Hermann394131e2008-10-18 21:14:13 +0000244 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000245
246 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000247 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000248}
249
Rudolf Marek48a85e42008-06-30 21:45:17 +0000250static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000251{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000252 unsigned char readarr[4];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000253 uint32_t id1;
254 uint32_t id2;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000255
Rudolf Marek48a85e42008-06-30 21:45:17 +0000256 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000257 return 0;
258
259 if (!oddparity(readarr[0]))
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000260 printf_debug("RDID byte 0 parity violation. ");
Peter Stugeda4e5f32008-06-24 01:22:03 +0000261
262 /* Check if this is a continuation vendor ID */
263 if (readarr[0] == 0x7f) {
264 if (!oddparity(readarr[1]))
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000265 printf_debug("RDID byte 1 parity violation. ");
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000266 id1 = (readarr[0] << 8) | readarr[1];
267 id2 = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000268 if (bytes > 3) {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000269 id2 <<= 8;
270 id2 |= readarr[3];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000271 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000272 } else {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000273 id1 = readarr[0];
274 id2 = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000275 }
276
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000277 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000278
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000279 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000280 /* Print the status register to tell the
281 * user about possible write protection.
282 */
283 spi_prettyprint_status_register(flash);
284
285 return 1;
286 }
287
288 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000289 if (id1 == flash->manufacture_id &&
Peter Stugeda4e5f32008-06-24 01:22:03 +0000290 GENERIC_DEVICE_ID == flash->model_id)
291 return 1;
292
Carl-Daniel Hailfinger01d49ed2009-11-20 01:12:45 +0000293 /* Test if there is any vendor ID. */
294 if (GENERIC_MANUF_ID == flash->manufacture_id &&
295 id1 != 0xff)
296 return 1;
297
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000298 return 0;
299}
300
Uwe Hermann394131e2008-10-18 21:14:13 +0000301int probe_spi_rdid(struct flashchip *flash)
302{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000303 return probe_spi_rdid_generic(flash, 3);
304}
305
306/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000307int probe_spi_rdid4(struct flashchip *flash)
308{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000309 /* only some SPI chipsets support 4 bytes commands */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000310 switch (spi_controller) {
311 case SPI_CONTROLLER_ICH7:
312 case SPI_CONTROLLER_ICH9:
313 case SPI_CONTROLLER_VIA:
314 case SPI_CONTROLLER_SB600:
315 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000316#if FT2232_SPI_SUPPORT == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000317 case SPI_CONTROLLER_FT2232:
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000318#endif
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000319#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000320 case SPI_CONTROLLER_DUMMY:
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000321#endif
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000322#if BUSPIRATE_SPI_SUPPORT == 1
323 case SPI_CONTROLLER_BUSPIRATE:
324#endif
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000325 return probe_spi_rdid_generic(flash, 4);
326 default:
327 printf_debug("4b ID not supported on this SPI controller\n");
328 }
329
330 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000331}
332
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000333int probe_spi_rems(struct flashchip *flash)
334{
335 unsigned char readarr[JEDEC_REMS_INSIZE];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000336 uint32_t id1, id2;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000337
338 if (spi_rems(readarr))
339 return 0;
340
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000341 id1 = readarr[0];
342 id2 = readarr[1];
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000343
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000344 printf_debug("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000345
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000346 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000347 /* Print the status register to tell the
348 * user about possible write protection.
349 */
350 spi_prettyprint_status_register(flash);
351
352 return 1;
353 }
354
355 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000356 if (id1 == flash->manufacture_id &&
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000357 GENERIC_DEVICE_ID == flash->model_id)
358 return 1;
359
Carl-Daniel Hailfinger01d49ed2009-11-20 01:12:45 +0000360 /* Test if there is any vendor ID. */
361 if (GENERIC_MANUF_ID == flash->manufacture_id &&
362 id1 != 0xff)
363 return 1;
364
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000365 return 0;
366}
367
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000368int probe_spi_res(struct flashchip *flash)
369{
370 unsigned char readarr[3];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000371 uint32_t id2;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000372
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000373 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
374 * In that case, RES is pointless.
375 */
376 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
377 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000378 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000379
Peter Stugeda4e5f32008-06-24 01:22:03 +0000380 if (spi_res(readarr))
381 return 0;
382
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000383 id2 = readarr[0];
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000384 printf_debug("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000385 if (id2 != flash->model_id)
Peter Stugeda4e5f32008-06-24 01:22:03 +0000386 return 0;
387
388 /* Print the status register to tell the
389 * user about possible write protection.
390 */
391 spi_prettyprint_status_register(flash);
392 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000393}
394
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000395uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000396{
Uwe Hermann394131e2008-10-18 21:14:13 +0000397 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000398 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
Peter Stugebf196e92009-01-26 03:08:45 +0000399 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000400 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000401
402 /* Read Status Register */
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000403 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
404 if (ret)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000405 fprintf(stderr, "RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000406
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000407 return readarr[0];
408}
409
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000410/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000411void spi_prettyprint_status_register_common(uint8_t status)
412{
413 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000414 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000415 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000416 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000417 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000418 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000419 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000420 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000421 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000422 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000423 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000424 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000425}
426
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000427/* Prettyprint the status register. Works for
428 * ST M25P series
429 * MX MX25L series
430 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000431void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000432{
433 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000434 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000435 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000436 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000437 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000438}
439
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000440void spi_prettyprint_status_register_sst25(uint8_t status)
441{
442 printf_debug("Chip status register: Block Protect Write Disable "
443 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
444 printf_debug("Chip status register: Auto Address Increment Programming "
445 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
446 spi_prettyprint_status_register_common(status);
447}
448
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000449/* Prettyprint the status register. Works for
450 * SST 25VF016
451 */
452void spi_prettyprint_status_register_sst25vf016(uint8_t status)
453{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000454 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000455 "none",
456 "1F0000H-1FFFFFH",
457 "1E0000H-1FFFFFH",
458 "1C0000H-1FFFFFH",
459 "180000H-1FFFFFH",
460 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000461 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000462 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000463 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000464 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000465 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000466}
467
Peter Stuge5fecee42009-01-26 03:23:50 +0000468void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
469{
470 const char *bpt[] = {
471 "none",
472 "0x70000-0x7ffff",
473 "0x60000-0x7ffff",
474 "0x40000-0x7ffff",
475 "all blocks", "all blocks", "all blocks", "all blocks"
476 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000477 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000478 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000479 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000480}
481
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000482void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000483{
484 uint8_t status;
485
Peter Stugefa8c5502008-05-10 23:07:52 +0000486 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000487 printf_debug("Chip status register is %02x\n", status);
488 switch (flash->manufacture_id) {
489 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000490 if (((flash->model_id & 0xff00) == 0x2000) ||
491 ((flash->model_id & 0xff00) == 0x2500))
492 spi_prettyprint_status_register_st_m25p(status);
493 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000494 case MX_ID:
495 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000496 spi_prettyprint_status_register_st_m25p(status);
497 break;
498 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000499 switch (flash->model_id) {
500 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000501 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000502 break;
503 case 0x8d:
504 case 0x258d:
505 spi_prettyprint_status_register_sst25vf040b(status);
506 break;
Carl-Daniel Hailfinger5100a8a2009-05-13 22:51:27 +0000507 default:
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000508 spi_prettyprint_status_register_sst25(status);
509 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000510 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000511 break;
512 }
513}
Uwe Hermann394131e2008-10-18 21:14:13 +0000514
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000515int spi_chip_erase_60(struct flashchip *flash)
516{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000517 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000518 struct spi_command cmds[] = {
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000519 {
520 .writecnt = JEDEC_WREN_OUTSIZE,
521 .writearr = (const unsigned char[]){ JEDEC_WREN },
522 .readcnt = 0,
523 .readarr = NULL,
524 }, {
525 .writecnt = JEDEC_CE_60_OUTSIZE,
526 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
527 .readcnt = 0,
528 .readarr = NULL,
529 }, {
530 .writecnt = 0,
531 .writearr = NULL,
532 .readcnt = 0,
533 .readarr = NULL,
534 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000535
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000536 result = spi_disable_blockprotect();
537 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000538 fprintf(stderr, "spi_disable_blockprotect failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000539 return result;
540 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000541
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000542 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000543 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000544 fprintf(stderr, "%s failed during command execution\n",
545 __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000546 return result;
547 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000548 /* Wait until the Write-In-Progress bit is cleared.
549 * This usually takes 1-85 s, so wait in 1 s steps.
550 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000551 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000552 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000553 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000554 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
555 fprintf(stderr, "ERASE FAILED!\n");
556 return -1;
557 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000558 return 0;
559}
560
Peter Stugefa8c5502008-05-10 23:07:52 +0000561int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000562{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000563 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000564 struct spi_command cmds[] = {
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000565 {
566 .writecnt = JEDEC_WREN_OUTSIZE,
567 .writearr = (const unsigned char[]){ JEDEC_WREN },
568 .readcnt = 0,
569 .readarr = NULL,
570 }, {
571 .writecnt = JEDEC_CE_C7_OUTSIZE,
572 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
573 .readcnt = 0,
574 .readarr = NULL,
575 }, {
576 .writecnt = 0,
577 .writearr = NULL,
578 .readcnt = 0,
579 .readarr = NULL,
580 }};
Uwe Hermann394131e2008-10-18 21:14:13 +0000581
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000582 result = spi_disable_blockprotect();
583 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000584 fprintf(stderr, "spi_disable_blockprotect failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000585 return result;
586 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000587
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000588 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000589 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000590 fprintf(stderr, "%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000591 return result;
592 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000593 /* Wait until the Write-In-Progress bit is cleared.
594 * This usually takes 1-85 s, so wait in 1 s steps.
595 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000596 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000597 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000598 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000599 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
600 fprintf(stderr, "ERASE FAILED!\n");
601 return -1;
602 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000603 return 0;
604}
605
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000606int spi_chip_erase_60_c7(struct flashchip *flash)
607{
608 int result;
609 result = spi_chip_erase_60(flash);
610 if (result) {
611 printf_debug("spi_chip_erase_60 failed, trying c7\n");
612 result = spi_chip_erase_c7(flash);
613 }
614 return result;
615}
616
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000617int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000618{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000619 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000620 struct spi_command cmds[] = {
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000621 {
622 .writecnt = JEDEC_WREN_OUTSIZE,
623 .writearr = (const unsigned char[]){ JEDEC_WREN },
624 .readcnt = 0,
625 .readarr = NULL,
626 }, {
627 .writecnt = JEDEC_BE_52_OUTSIZE,
628 .writearr = (const unsigned char[]){ JEDEC_BE_52, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
629 .readcnt = 0,
630 .readarr = NULL,
631 }, {
632 .writecnt = 0,
633 .writearr = NULL,
634 .readcnt = 0,
635 .readarr = NULL,
636 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000637
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000638 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000639 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000640 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
641 __func__, addr);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000642 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000643 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000644 /* Wait until the Write-In-Progress bit is cleared.
645 * This usually takes 100-4000 ms, so wait in 100 ms steps.
646 */
647 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000648 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000649 if (check_erased_range(flash, addr, blocklen)) {
650 fprintf(stderr, "ERASE FAILED!\n");
651 return -1;
652 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000653 return 0;
654}
655
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000656/* Block size is usually
657 * 64k for Macronix
658 * 32k for SST
659 * 4-32k non-uniform for EON
660 */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000661int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000662{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000663 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000664 struct spi_command cmds[] = {
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000665 {
666 .writecnt = JEDEC_WREN_OUTSIZE,
667 .writearr = (const unsigned char[]){ JEDEC_WREN },
668 .readcnt = 0,
669 .readarr = NULL,
670 }, {
671 .writecnt = JEDEC_BE_D8_OUTSIZE,
672 .writearr = (const unsigned char[]){ JEDEC_BE_D8, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
673 .readcnt = 0,
674 .readarr = NULL,
675 }, {
676 .writecnt = 0,
677 .writearr = NULL,
678 .readcnt = 0,
679 .readarr = NULL,
680 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000681
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000682 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000683 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000684 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
685 __func__, addr);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000686 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000687 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000688 /* Wait until the Write-In-Progress bit is cleared.
689 * This usually takes 100-4000 ms, so wait in 100 ms steps.
690 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000691 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000692 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000693 if (check_erased_range(flash, addr, blocklen)) {
694 fprintf(stderr, "ERASE FAILED!\n");
695 return -1;
696 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000697 return 0;
698}
699
Stefan Reinauer424ed222008-10-29 22:13:20 +0000700int spi_chip_erase_d8(struct flashchip *flash)
701{
702 int i, rc = 0;
703 int total_size = flash->total_size * 1024;
704 int erase_size = 64 * 1024;
705
706 spi_disable_blockprotect();
707
708 printf("Erasing chip: \n");
709
710 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000711 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000712 if (rc) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000713 fprintf(stderr, "Error erasing block at 0x%x\n", i);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000714 break;
715 }
716 }
717
718 printf("\n");
719
720 return rc;
721}
722
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000723/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000724int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000725{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000726 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000727 struct spi_command cmds[] = {
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000728 {
729 .writecnt = JEDEC_WREN_OUTSIZE,
730 .writearr = (const unsigned char[]){ JEDEC_WREN },
731 .readcnt = 0,
732 .readarr = NULL,
733 }, {
734 .writecnt = JEDEC_SE_OUTSIZE,
735 .writearr = (const unsigned char[]){ JEDEC_SE, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
736 .readcnt = 0,
737 .readarr = NULL,
738 }, {
739 .writecnt = 0,
740 .writearr = NULL,
741 .readcnt = 0,
742 .readarr = NULL,
743 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000744
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000745 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000746 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000747 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
748 __func__, addr);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000749 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000750 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000751 /* Wait until the Write-In-Progress bit is cleared.
752 * This usually takes 15-800 ms, so wait in 10 ms steps.
753 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000754 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000755 programmer_delay(10 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000756 if (check_erased_range(flash, addr, blocklen)) {
757 fprintf(stderr, "ERASE FAILED!\n");
758 return -1;
759 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000760 return 0;
761}
762
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000763int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
764{
765 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000766 fprintf(stderr, "%s called with incorrect arguments\n",
767 __func__);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000768 return -1;
769 }
770 return spi_chip_erase_60(flash);
771}
772
773int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
774{
775 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000776 fprintf(stderr, "%s called with incorrect arguments\n",
777 __func__);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000778 return -1;
779 }
780 return spi_chip_erase_c7(flash);
781}
782
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000783int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000784{
785 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000786 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000787
788 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000789 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000790
791 if (result)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000792 fprintf(stderr, "%s failed\n", __func__);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000793
794 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000795}
796
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000797/*
798 * This is according the SST25VF016 datasheet, who knows it is more
799 * generic that this...
800 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000801int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000802{
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000803 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000804 struct spi_command cmds[] = {
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000805 {
806 .writecnt = JEDEC_EWSR_OUTSIZE,
807 .writearr = (const unsigned char[]){ JEDEC_EWSR },
808 .readcnt = 0,
809 .readarr = NULL,
810 }, {
811 .writecnt = JEDEC_WRSR_OUTSIZE,
812 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
813 .readcnt = 0,
814 .readarr = NULL,
815 }, {
816 .writecnt = 0,
817 .writearr = NULL,
818 .readcnt = 0,
819 .readarr = NULL,
820 }};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000821
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000822 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000823 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000824 fprintf(stderr, "%s failed during command execution\n",
825 __func__);
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000826 }
827 return result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000828}
829
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000830int spi_byte_program(int addr, uint8_t byte)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000831{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000832 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000833 struct spi_command cmds[] = {
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000834 {
835 .writecnt = JEDEC_WREN_OUTSIZE,
836 .writearr = (const unsigned char[]){ JEDEC_WREN },
837 .readcnt = 0,
838 .readarr = NULL,
839 }, {
840 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
841 .writearr = (const unsigned char[]){ JEDEC_BYTE_PROGRAM, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff), byte },
842 .readcnt = 0,
843 .readarr = NULL,
844 }, {
845 .writecnt = 0,
846 .writearr = NULL,
847 .readcnt = 0,
848 .readarr = NULL,
849 }};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000850
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000851 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000852 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000853 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
854 __func__, addr);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000855 }
856 return result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000857}
858
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000859int spi_nbyte_program(int addr, uint8_t *bytes, int len)
Paul Foxeb3acef2009-06-12 08:10:33 +0000860{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000861 int result;
862 /* FIXME: Switch to malloc based on len unless that kills speed. */
Paul Foxeb3acef2009-06-12 08:10:33 +0000863 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
864 JEDEC_BYTE_PROGRAM,
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000865 (addr >> 16) & 0xff,
866 (addr >> 8) & 0xff,
867 (addr >> 0) & 0xff,
Paul Foxeb3acef2009-06-12 08:10:33 +0000868 };
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000869 struct spi_command cmds[] = {
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000870 {
871 .writecnt = JEDEC_WREN_OUTSIZE,
872 .writearr = (const unsigned char[]){ JEDEC_WREN },
873 .readcnt = 0,
874 .readarr = NULL,
875 }, {
876 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
877 .writearr = cmd,
878 .readcnt = 0,
879 .readarr = NULL,
880 }, {
881 .writecnt = 0,
882 .writearr = NULL,
883 .readcnt = 0,
884 .readarr = NULL,
885 }};
Paul Foxeb3acef2009-06-12 08:10:33 +0000886
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000887 if (!len) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000888 fprintf(stderr, "%s called for zero-length write\n", __func__);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000889 return 1;
890 }
Paul Foxeb3acef2009-06-12 08:10:33 +0000891 if (len > 256) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000892 fprintf(stderr, "%s called for too long a write\n", __func__);
Paul Foxeb3acef2009-06-12 08:10:33 +0000893 return 1;
894 }
895
896 memcpy(&cmd[4], bytes, len);
897
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000898 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000899 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000900 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
901 __func__, addr);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000902 }
903 return result;
Paul Foxeb3acef2009-06-12 08:10:33 +0000904}
905
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000906int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000907{
908 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000909 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000910
Peter Stugefa8c5502008-05-10 23:07:52 +0000911 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000912 /* If there is block protection in effect, unprotect it first. */
913 if ((status & 0x3c) != 0) {
914 printf_debug("Some block protection in effect, disabling\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000915 result = spi_write_status_register(status & ~0x3c);
916 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000917 fprintf(stderr, "spi_write_status_register failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000918 return result;
919 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000920 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000921 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000922}
923
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000924int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000925{
Uwe Hermann394131e2008-10-18 21:14:13 +0000926 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
927 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000928 (address >> 16) & 0xff,
929 (address >> 8) & 0xff,
930 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000931 };
932
933 /* Send Read */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000934 return spi_send_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000935}
936
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000937/*
938 * Read a complete flash chip.
939 * Each page is read separately in chunks with a maximum size of chunksize.
940 */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000941int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000942{
943 int rc = 0;
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000944 int i, j, starthere, lenhere;
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000945 int page_size = flash->page_size;
946 int toread;
947
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000948 /* Warning: This loop has a very unusual condition and body.
949 * The loop needs to go through each page with at least one affected
950 * byte. The lowest page number is (start / page_size) since that
951 * division rounds down. The highest page number we want is the page
952 * where the last byte of the range lives. That last byte has the
953 * address (start + len - 1), thus the highest page number is
954 * (start + len - 1) / page_size. Since we want to include that last
955 * page as well, the loop condition uses <=.
956 */
957 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
958 /* Byte position of the first byte in the range in this page. */
959 /* starthere is an offset to the base address of the chip. */
960 starthere = max(start, i * page_size);
961 /* Length of bytes in the range in this page. */
962 lenhere = min(start + len, (i + 1) * page_size) - starthere;
963 for (j = 0; j < lenhere; j += chunksize) {
964 toread = min(chunksize, lenhere - j);
965 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000966 if (rc)
967 break;
968 }
969 if (rc)
970 break;
971 }
972
973 return rc;
974}
975
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000976int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000977{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000978 if (!spi_programmer[spi_controller].read) {
979 fprintf(stderr, "%s called, but SPI read is unsupported on this"
980 " 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].read(flash, buf, start, len);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000985}
986
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000987/*
988 * Program chip using byte programming. (SLOW!)
989 * This is for chips which can only handle one byte writes
990 * and for chips where memory mapped programming is impossible
991 * (e.g. due to size constraints in IT87* for over 512 kB)
992 */
993int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
994{
995 int total_size = 1024 * flash->total_size;
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +0000996 int i, result = 0;
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000997
998 spi_disable_blockprotect();
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000999 /* Erase first */
1000 printf("Erasing flash before programming... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001001 if (erase_flash(flash)) {
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +00001002 fprintf(stderr, "ERASE FAILED!\n");
1003 return -1;
1004 }
1005 printf("done.\n");
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001006 for (i = 0; i < total_size; i++) {
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +00001007 result = spi_byte_program(i, buf[i]);
1008 if (result)
1009 return 1;
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001010 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001011 programmer_delay(10);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001012 }
1013
1014 return 0;
1015}
1016
1017/*
1018 * Program chip using page (256 bytes) programming.
1019 * Some SPI masters can't do this, they use single byte programming instead.
1020 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +00001021int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +00001022{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001023 if (!spi_programmer[spi_controller].write_256) {
1024 fprintf(stderr, "%s called, but SPI page write is unsupported "
1025 " on this hardware. Please report a bug.\n", __func__);
1026 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +00001027 }
1028
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001029 return spi_programmer[spi_controller].write_256(flash, buf);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +00001030}
Peter Stugefd9217d2009-01-26 03:37:40 +00001031
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +00001032uint32_t spi_get_valid_read_addr(void)
1033{
1034 /* Need to return BBAR for ICH chipsets. */
1035 return 0;
1036}
1037
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001038int spi_aai_write(struct flashchip *flash, uint8_t *buf)
1039{
Peter Stugefd9217d2009-01-26 03:37:40 +00001040 uint32_t pos = 2, size = flash->total_size * 1024;
1041 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +00001042 int result;
1043
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +00001044 switch (spi_controller) {
1045 case SPI_CONTROLLER_WBSIO:
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001046 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
1047 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001048 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001049 default:
1050 break;
Peter Stugefd9217d2009-01-26 03:37:40 +00001051 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001052 if (erase_flash(flash)) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +00001053 fprintf(stderr, "ERASE FAILED!\n");
1054 return -1;
1055 }
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +00001056 result = spi_write_enable();
1057 if (result)
1058 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001059 spi_send_command(6, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +00001060 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001061 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +00001062 while (pos < size) {
1063 w[1] = buf[pos++];
1064 w[2] = buf[pos++];
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001065 spi_send_command(3, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +00001066 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001067 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +00001068 }
1069 spi_write_disable();
1070 return 0;
1071}