blob: 29c202c1358246d7510c295e0de26668f43e2c30 [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 Hailfinger26f7e642009-09-18 15:50:56 +0000121int spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000122{
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
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000129 return spi_programmer[spi_controller].multicommand(cmds);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000130}
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
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000151int default_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000152{
153 int result = 0;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000154 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
155 result = spi_send_command(cmds->writecnt, cmds->readcnt,
156 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000157 }
158 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000159}
160
Rudolf Marek48a85e42008-06-30 21:45:17 +0000161static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000162{
Uwe Hermann394131e2008-10-18 21:14:13 +0000163 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000164 int ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000165 int i;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000166
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000167 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000168 if (ret)
169 return ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000170 printf_debug("RDID returned");
171 for (i = 0; i < bytes; i++)
172 printf_debug(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000173 printf_debug(". ");
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000174 return 0;
175}
176
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000177static int spi_rems(unsigned char *readarr)
178{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000179 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
180 uint32_t readaddr;
181 int ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000182
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000183 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000184 if (ret == SPI_INVALID_ADDRESS) {
185 /* Find the lowest even address allowed for reads. */
186 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
187 cmd[1] = (readaddr >> 16) & 0xff,
188 cmd[2] = (readaddr >> 8) & 0xff,
189 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000190 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000191 }
192 if (ret)
193 return ret;
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000194 printf_debug("REMS returned %02x %02x. ", readarr[0], readarr[1]);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000195 return 0;
196}
197
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000198static int spi_res(unsigned char *readarr)
199{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000200 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
201 uint32_t readaddr;
202 int ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000203
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000204 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000205 if (ret == SPI_INVALID_ADDRESS) {
206 /* Find the lowest even address allowed for reads. */
207 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
208 cmd[1] = (readaddr >> 16) & 0xff,
209 cmd[2] = (readaddr >> 8) & 0xff,
210 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000211 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000212 }
213 if (ret)
214 return ret;
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000215 printf_debug("RES returned %02x. ", readarr[0]);
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000216 return 0;
217}
218
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000219int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000220{
Uwe Hermann394131e2008-10-18 21:14:13 +0000221 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000222 int result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000223
224 /* Send WREN (Write Enable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000225 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000226
227 if (result)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000228 fprintf(stderr, "%s failed\n", __func__);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000229
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000230 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000231}
232
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000233int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000234{
Uwe Hermann394131e2008-10-18 21:14:13 +0000235 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000236
237 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000238 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000239}
240
Rudolf Marek48a85e42008-06-30 21:45:17 +0000241static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000242{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000243 unsigned char readarr[4];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000244 uint32_t id1;
245 uint32_t id2;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000246
Rudolf Marek48a85e42008-06-30 21:45:17 +0000247 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000248 return 0;
249
250 if (!oddparity(readarr[0]))
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000251 printf_debug("RDID byte 0 parity violation. ");
Peter Stugeda4e5f32008-06-24 01:22:03 +0000252
253 /* Check if this is a continuation vendor ID */
254 if (readarr[0] == 0x7f) {
255 if (!oddparity(readarr[1]))
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000256 printf_debug("RDID byte 1 parity violation. ");
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000257 id1 = (readarr[0] << 8) | readarr[1];
258 id2 = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000259 if (bytes > 3) {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000260 id2 <<= 8;
261 id2 |= readarr[3];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000262 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000263 } else {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000264 id1 = readarr[0];
265 id2 = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000266 }
267
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000268 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000269
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000270 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000271 /* Print the status register to tell the
272 * user about possible write protection.
273 */
274 spi_prettyprint_status_register(flash);
275
276 return 1;
277 }
278
279 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000280 if (id1 == flash->manufacture_id &&
Peter Stugeda4e5f32008-06-24 01:22:03 +0000281 GENERIC_DEVICE_ID == flash->model_id)
282 return 1;
283
Carl-Daniel Hailfinger01d49ed2009-11-20 01:12:45 +0000284 /* Test if there is any vendor ID. */
285 if (GENERIC_MANUF_ID == flash->manufacture_id &&
286 id1 != 0xff)
287 return 1;
288
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000289 return 0;
290}
291
Uwe Hermann394131e2008-10-18 21:14:13 +0000292int probe_spi_rdid(struct flashchip *flash)
293{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000294 return probe_spi_rdid_generic(flash, 3);
295}
296
297/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000298int probe_spi_rdid4(struct flashchip *flash)
299{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000300 /* only some SPI chipsets support 4 bytes commands */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000301 switch (spi_controller) {
302 case SPI_CONTROLLER_ICH7:
303 case SPI_CONTROLLER_ICH9:
304 case SPI_CONTROLLER_VIA:
305 case SPI_CONTROLLER_SB600:
306 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000307#if FT2232_SPI_SUPPORT == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000308 case SPI_CONTROLLER_FT2232:
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000309#endif
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000310#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000311 case SPI_CONTROLLER_DUMMY:
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000312#endif
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000313 return probe_spi_rdid_generic(flash, 4);
314 default:
315 printf_debug("4b ID not supported on this SPI controller\n");
316 }
317
318 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000319}
320
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000321int probe_spi_rems(struct flashchip *flash)
322{
323 unsigned char readarr[JEDEC_REMS_INSIZE];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000324 uint32_t id1, id2;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000325
326 if (spi_rems(readarr))
327 return 0;
328
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000329 id1 = readarr[0];
330 id2 = readarr[1];
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000331
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000332 printf_debug("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000333
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000334 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000335 /* Print the status register to tell the
336 * user about possible write protection.
337 */
338 spi_prettyprint_status_register(flash);
339
340 return 1;
341 }
342
343 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000344 if (id1 == flash->manufacture_id &&
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000345 GENERIC_DEVICE_ID == flash->model_id)
346 return 1;
347
Carl-Daniel Hailfinger01d49ed2009-11-20 01:12:45 +0000348 /* Test if there is any vendor ID. */
349 if (GENERIC_MANUF_ID == flash->manufacture_id &&
350 id1 != 0xff)
351 return 1;
352
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000353 return 0;
354}
355
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000356int probe_spi_res(struct flashchip *flash)
357{
358 unsigned char readarr[3];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000359 uint32_t id2;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000360
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000361 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
362 * In that case, RES is pointless.
363 */
364 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
365 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000366 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000367
Peter Stugeda4e5f32008-06-24 01:22:03 +0000368 if (spi_res(readarr))
369 return 0;
370
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000371 id2 = readarr[0];
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000372 printf_debug("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000373 if (id2 != flash->model_id)
Peter Stugeda4e5f32008-06-24 01:22:03 +0000374 return 0;
375
376 /* Print the status register to tell the
377 * user about possible write protection.
378 */
379 spi_prettyprint_status_register(flash);
380 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000381}
382
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000383uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000384{
Uwe Hermann394131e2008-10-18 21:14:13 +0000385 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000386 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
Peter Stugebf196e92009-01-26 03:08:45 +0000387 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000388 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000389
390 /* Read Status Register */
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000391 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
392 if (ret)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000393 fprintf(stderr, "RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000394
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000395 return readarr[0];
396}
397
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000398/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000399void spi_prettyprint_status_register_common(uint8_t status)
400{
401 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000402 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000403 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000404 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000405 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000406 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000407 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000408 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000409 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000410 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000411 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000412 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000413}
414
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000415/* Prettyprint the status register. Works for
416 * ST M25P series
417 * MX MX25L series
418 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000419void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000420{
421 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000422 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000423 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000424 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000425 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000426}
427
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000428void spi_prettyprint_status_register_sst25(uint8_t status)
429{
430 printf_debug("Chip status register: Block Protect Write Disable "
431 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
432 printf_debug("Chip status register: Auto Address Increment Programming "
433 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
434 spi_prettyprint_status_register_common(status);
435}
436
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000437/* Prettyprint the status register. Works for
438 * SST 25VF016
439 */
440void spi_prettyprint_status_register_sst25vf016(uint8_t status)
441{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000442 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000443 "none",
444 "1F0000H-1FFFFFH",
445 "1E0000H-1FFFFFH",
446 "1C0000H-1FFFFFH",
447 "180000H-1FFFFFH",
448 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000449 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000450 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000451 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000452 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000453 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000454}
455
Peter Stuge5fecee42009-01-26 03:23:50 +0000456void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
457{
458 const char *bpt[] = {
459 "none",
460 "0x70000-0x7ffff",
461 "0x60000-0x7ffff",
462 "0x40000-0x7ffff",
463 "all blocks", "all blocks", "all blocks", "all blocks"
464 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000465 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000466 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000467 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000468}
469
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000470void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000471{
472 uint8_t status;
473
Peter Stugefa8c5502008-05-10 23:07:52 +0000474 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000475 printf_debug("Chip status register is %02x\n", status);
476 switch (flash->manufacture_id) {
477 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000478 if (((flash->model_id & 0xff00) == 0x2000) ||
479 ((flash->model_id & 0xff00) == 0x2500))
480 spi_prettyprint_status_register_st_m25p(status);
481 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000482 case MX_ID:
483 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000484 spi_prettyprint_status_register_st_m25p(status);
485 break;
486 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000487 switch (flash->model_id) {
488 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000489 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000490 break;
491 case 0x8d:
492 case 0x258d:
493 spi_prettyprint_status_register_sst25vf040b(status);
494 break;
Carl-Daniel Hailfinger5100a8a2009-05-13 22:51:27 +0000495 default:
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000496 spi_prettyprint_status_register_sst25(status);
497 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000498 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000499 break;
500 }
501}
Uwe Hermann394131e2008-10-18 21:14:13 +0000502
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000503int spi_chip_erase_60(struct flashchip *flash)
504{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000505 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000506 struct spi_command cmds[] = {
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000507 {
508 .writecnt = JEDEC_WREN_OUTSIZE,
509 .writearr = (const unsigned char[]){ JEDEC_WREN },
510 .readcnt = 0,
511 .readarr = NULL,
512 }, {
513 .writecnt = JEDEC_CE_60_OUTSIZE,
514 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
515 .readcnt = 0,
516 .readarr = NULL,
517 }, {
518 .writecnt = 0,
519 .writearr = NULL,
520 .readcnt = 0,
521 .readarr = NULL,
522 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000523
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000524 result = spi_disable_blockprotect();
525 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000526 fprintf(stderr, "spi_disable_blockprotect failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000527 return result;
528 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000529
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000530 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000531 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000532 fprintf(stderr, "%s failed during command execution\n",
533 __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000534 return result;
535 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000536 /* Wait until the Write-In-Progress bit is cleared.
537 * This usually takes 1-85 s, so wait in 1 s steps.
538 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000539 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000540 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000541 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000542 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
543 fprintf(stderr, "ERASE FAILED!\n");
544 return -1;
545 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000546 return 0;
547}
548
Peter Stugefa8c5502008-05-10 23:07:52 +0000549int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000550{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000551 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000552 struct spi_command cmds[] = {
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000553 {
554 .writecnt = JEDEC_WREN_OUTSIZE,
555 .writearr = (const unsigned char[]){ JEDEC_WREN },
556 .readcnt = 0,
557 .readarr = NULL,
558 }, {
559 .writecnt = JEDEC_CE_C7_OUTSIZE,
560 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
561 .readcnt = 0,
562 .readarr = NULL,
563 }, {
564 .writecnt = 0,
565 .writearr = NULL,
566 .readcnt = 0,
567 .readarr = NULL,
568 }};
Uwe Hermann394131e2008-10-18 21:14:13 +0000569
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000570 result = spi_disable_blockprotect();
571 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000572 fprintf(stderr, "spi_disable_blockprotect failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000573 return result;
574 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000575
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000576 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000577 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000578 fprintf(stderr, "%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000579 return result;
580 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000581 /* Wait until the Write-In-Progress bit is cleared.
582 * This usually takes 1-85 s, so wait in 1 s steps.
583 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000584 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000585 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000586 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000587 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
588 fprintf(stderr, "ERASE FAILED!\n");
589 return -1;
590 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000591 return 0;
592}
593
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000594int spi_chip_erase_60_c7(struct flashchip *flash)
595{
596 int result;
597 result = spi_chip_erase_60(flash);
598 if (result) {
599 printf_debug("spi_chip_erase_60 failed, trying c7\n");
600 result = spi_chip_erase_c7(flash);
601 }
602 return result;
603}
604
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000605int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000606{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000607 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000608 struct spi_command cmds[] = {
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000609 {
610 .writecnt = JEDEC_WREN_OUTSIZE,
611 .writearr = (const unsigned char[]){ JEDEC_WREN },
612 .readcnt = 0,
613 .readarr = NULL,
614 }, {
615 .writecnt = JEDEC_BE_52_OUTSIZE,
616 .writearr = (const unsigned char[]){ JEDEC_BE_52, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
617 .readcnt = 0,
618 .readarr = NULL,
619 }, {
620 .writecnt = 0,
621 .writearr = NULL,
622 .readcnt = 0,
623 .readarr = NULL,
624 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000625
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000626 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000627 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000628 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
629 __func__, addr);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000630 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000631 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000632 /* Wait until the Write-In-Progress bit is cleared.
633 * This usually takes 100-4000 ms, so wait in 100 ms steps.
634 */
635 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000636 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000637 if (check_erased_range(flash, addr, blocklen)) {
638 fprintf(stderr, "ERASE FAILED!\n");
639 return -1;
640 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000641 return 0;
642}
643
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000644/* Block size is usually
645 * 64k for Macronix
646 * 32k for SST
647 * 4-32k non-uniform for EON
648 */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000649int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000650{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000651 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000652 struct spi_command cmds[] = {
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000653 {
654 .writecnt = JEDEC_WREN_OUTSIZE,
655 .writearr = (const unsigned char[]){ JEDEC_WREN },
656 .readcnt = 0,
657 .readarr = NULL,
658 }, {
659 .writecnt = JEDEC_BE_D8_OUTSIZE,
660 .writearr = (const unsigned char[]){ JEDEC_BE_D8, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
661 .readcnt = 0,
662 .readarr = NULL,
663 }, {
664 .writecnt = 0,
665 .writearr = NULL,
666 .readcnt = 0,
667 .readarr = NULL,
668 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000669
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000670 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000671 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000672 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
673 __func__, addr);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000674 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000675 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000676 /* Wait until the Write-In-Progress bit is cleared.
677 * This usually takes 100-4000 ms, so wait in 100 ms steps.
678 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000679 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000680 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000681 if (check_erased_range(flash, addr, blocklen)) {
682 fprintf(stderr, "ERASE FAILED!\n");
683 return -1;
684 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000685 return 0;
686}
687
Stefan Reinauer424ed222008-10-29 22:13:20 +0000688int spi_chip_erase_d8(struct flashchip *flash)
689{
690 int i, rc = 0;
691 int total_size = flash->total_size * 1024;
692 int erase_size = 64 * 1024;
693
694 spi_disable_blockprotect();
695
696 printf("Erasing chip: \n");
697
698 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000699 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000700 if (rc) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000701 fprintf(stderr, "Error erasing block at 0x%x\n", i);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000702 break;
703 }
704 }
705
706 printf("\n");
707
708 return rc;
709}
710
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000711/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000712int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000713{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000714 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000715 struct spi_command cmds[] = {
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000716 {
717 .writecnt = JEDEC_WREN_OUTSIZE,
718 .writearr = (const unsigned char[]){ JEDEC_WREN },
719 .readcnt = 0,
720 .readarr = NULL,
721 }, {
722 .writecnt = JEDEC_SE_OUTSIZE,
723 .writearr = (const unsigned char[]){ JEDEC_SE, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
724 .readcnt = 0,
725 .readarr = NULL,
726 }, {
727 .writecnt = 0,
728 .writearr = NULL,
729 .readcnt = 0,
730 .readarr = NULL,
731 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000732
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000733 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000734 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000735 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
736 __func__, addr);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000737 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000738 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000739 /* Wait until the Write-In-Progress bit is cleared.
740 * This usually takes 15-800 ms, so wait in 10 ms steps.
741 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000742 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000743 programmer_delay(10 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000744 if (check_erased_range(flash, addr, blocklen)) {
745 fprintf(stderr, "ERASE FAILED!\n");
746 return -1;
747 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000748 return 0;
749}
750
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000751int spi_block_erase_60(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_60(flash);
759}
760
761int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
762{
763 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000764 fprintf(stderr, "%s called with incorrect arguments\n",
765 __func__);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000766 return -1;
767 }
768 return spi_chip_erase_c7(flash);
769}
770
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000771int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000772{
773 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000774 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000775
776 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000777 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000778
779 if (result)
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000780 fprintf(stderr, "%s failed\n", __func__);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000781
782 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000783}
784
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000785/*
786 * This is according the SST25VF016 datasheet, who knows it is more
787 * generic that this...
788 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000789int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000790{
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000791 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000792 struct spi_command cmds[] = {
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000793 {
794 .writecnt = JEDEC_EWSR_OUTSIZE,
795 .writearr = (const unsigned char[]){ JEDEC_EWSR },
796 .readcnt = 0,
797 .readarr = NULL,
798 }, {
799 .writecnt = JEDEC_WRSR_OUTSIZE,
800 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
801 .readcnt = 0,
802 .readarr = NULL,
803 }, {
804 .writecnt = 0,
805 .writearr = NULL,
806 .readcnt = 0,
807 .readarr = NULL,
808 }};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000809
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000810 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000811 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000812 fprintf(stderr, "%s failed during command execution\n",
813 __func__);
Carl-Daniel Hailfingerfcbdbbc2009-07-22 20:09:28 +0000814 }
815 return result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000816}
817
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000818int spi_byte_program(int addr, uint8_t byte)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000819{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000820 int result;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000821 struct spi_command cmds[] = {
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000822 {
823 .writecnt = JEDEC_WREN_OUTSIZE,
824 .writearr = (const unsigned char[]){ JEDEC_WREN },
825 .readcnt = 0,
826 .readarr = NULL,
827 }, {
828 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
829 .writearr = (const unsigned char[]){ JEDEC_BYTE_PROGRAM, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff), byte },
830 .readcnt = 0,
831 .readarr = NULL,
832 }, {
833 .writecnt = 0,
834 .writearr = NULL,
835 .readcnt = 0,
836 .readarr = NULL,
837 }};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000838
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000839 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000840 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000841 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
842 __func__, addr);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000843 }
844 return result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000845}
846
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000847int spi_nbyte_program(int addr, uint8_t *bytes, int len)
Paul Foxeb3acef2009-06-12 08:10:33 +0000848{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000849 int result;
850 /* FIXME: Switch to malloc based on len unless that kills speed. */
Paul Foxeb3acef2009-06-12 08:10:33 +0000851 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
852 JEDEC_BYTE_PROGRAM,
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000853 (addr >> 16) & 0xff,
854 (addr >> 8) & 0xff,
855 (addr >> 0) & 0xff,
Paul Foxeb3acef2009-06-12 08:10:33 +0000856 };
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000857 struct spi_command cmds[] = {
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000858 {
859 .writecnt = JEDEC_WREN_OUTSIZE,
860 .writearr = (const unsigned char[]){ JEDEC_WREN },
861 .readcnt = 0,
862 .readarr = NULL,
863 }, {
864 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
865 .writearr = cmd,
866 .readcnt = 0,
867 .readarr = NULL,
868 }, {
869 .writecnt = 0,
870 .writearr = NULL,
871 .readcnt = 0,
872 .readarr = NULL,
873 }};
Paul Foxeb3acef2009-06-12 08:10:33 +0000874
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000875 if (!len) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000876 fprintf(stderr, "%s called for zero-length write\n", __func__);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000877 return 1;
878 }
Paul Foxeb3acef2009-06-12 08:10:33 +0000879 if (len > 256) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000880 fprintf(stderr, "%s called for too long a write\n", __func__);
Paul Foxeb3acef2009-06-12 08:10:33 +0000881 return 1;
882 }
883
884 memcpy(&cmd[4], bytes, len);
885
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000886 result = spi_send_multicommand(cmds);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000887 if (result) {
Carl-Daniel Hailfinger3efc51c2009-11-16 15:03:35 +0000888 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
889 __func__, addr);
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000890 }
891 return result;
Paul Foxeb3acef2009-06-12 08:10:33 +0000892}
893
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000894int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000895{
896 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000897 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000898
Peter Stugefa8c5502008-05-10 23:07:52 +0000899 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000900 /* If there is block protection in effect, unprotect it first. */
901 if ((status & 0x3c) != 0) {
902 printf_debug("Some block protection in effect, disabling\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000903 result = spi_write_status_register(status & ~0x3c);
904 if (result) {
Carl-Daniel Hailfinger414bd322009-07-23 01:33:43 +0000905 fprintf(stderr, "spi_write_status_register failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000906 return result;
907 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000908 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000909 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000910}
911
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000912int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000913{
Uwe Hermann394131e2008-10-18 21:14:13 +0000914 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
915 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000916 (address >> 16) & 0xff,
917 (address >> 8) & 0xff,
918 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000919 };
920
921 /* Send Read */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000922 return spi_send_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000923}
924
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000925/*
926 * Read a complete flash chip.
927 * Each page is read separately in chunks with a maximum size of chunksize.
928 */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000929int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000930{
931 int rc = 0;
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000932 int i, j, starthere, lenhere;
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000933 int page_size = flash->page_size;
934 int toread;
935
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000936 /* Warning: This loop has a very unusual condition and body.
937 * The loop needs to go through each page with at least one affected
938 * byte. The lowest page number is (start / page_size) since that
939 * division rounds down. The highest page number we want is the page
940 * where the last byte of the range lives. That last byte has the
941 * address (start + len - 1), thus the highest page number is
942 * (start + len - 1) / page_size. Since we want to include that last
943 * page as well, the loop condition uses <=.
944 */
945 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
946 /* Byte position of the first byte in the range in this page. */
947 /* starthere is an offset to the base address of the chip. */
948 starthere = max(start, i * page_size);
949 /* Length of bytes in the range in this page. */
950 lenhere = min(start + len, (i + 1) * page_size) - starthere;
951 for (j = 0; j < lenhere; j += chunksize) {
952 toread = min(chunksize, lenhere - j);
953 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000954 if (rc)
955 break;
956 }
957 if (rc)
958 break;
959 }
960
961 return rc;
962}
963
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000964int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000965{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000966 if (!spi_programmer[spi_controller].read) {
967 fprintf(stderr, "%s called, but SPI read is unsupported on this"
968 " hardware. Please report a bug.\n", __func__);
969 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000970 }
971
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000972 return spi_programmer[spi_controller].read(flash, buf, start, len);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000973}
974
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000975/*
976 * Program chip using byte programming. (SLOW!)
977 * This is for chips which can only handle one byte writes
978 * and for chips where memory mapped programming is impossible
979 * (e.g. due to size constraints in IT87* for over 512 kB)
980 */
981int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
982{
983 int total_size = 1024 * flash->total_size;
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +0000984 int i, result = 0;
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000985
986 spi_disable_blockprotect();
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000987 /* Erase first */
988 printf("Erasing flash before programming... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000989 if (erase_flash(flash)) {
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000990 fprintf(stderr, "ERASE FAILED!\n");
991 return -1;
992 }
993 printf("done.\n");
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000994 for (i = 0; i < total_size; i++) {
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +0000995 result = spi_byte_program(i, buf[i]);
996 if (result)
997 return 1;
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000998 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000999 programmer_delay(10);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001000 }
1001
1002 return 0;
1003}
1004
1005/*
1006 * Program chip using page (256 bytes) programming.
1007 * Some SPI masters can't do this, they use single byte programming instead.
1008 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +00001009int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +00001010{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001011 if (!spi_programmer[spi_controller].write_256) {
1012 fprintf(stderr, "%s called, but SPI page write is unsupported "
1013 " on this hardware. Please report a bug.\n", __func__);
1014 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +00001015 }
1016
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001017 return spi_programmer[spi_controller].write_256(flash, buf);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +00001018}
Peter Stugefd9217d2009-01-26 03:37:40 +00001019
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +00001020uint32_t spi_get_valid_read_addr(void)
1021{
1022 /* Need to return BBAR for ICH chipsets. */
1023 return 0;
1024}
1025
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001026int spi_aai_write(struct flashchip *flash, uint8_t *buf)
1027{
Peter Stugefd9217d2009-01-26 03:37:40 +00001028 uint32_t pos = 2, size = flash->total_size * 1024;
1029 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +00001030 int result;
1031
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +00001032 switch (spi_controller) {
1033 case SPI_CONTROLLER_WBSIO:
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001034 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
1035 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +00001036 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +00001037 default:
1038 break;
Peter Stugefd9217d2009-01-26 03:37:40 +00001039 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001040 if (erase_flash(flash)) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +00001041 fprintf(stderr, "ERASE FAILED!\n");
1042 return -1;
1043 }
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +00001044 result = spi_write_enable();
1045 if (result)
1046 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001047 spi_send_command(6, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +00001048 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001049 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +00001050 while (pos < size) {
1051 w[1] = buf[pos++];
1052 w[2] = buf[pos++];
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +00001053 spi_send_command(3, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +00001054 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001055 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +00001056 }
1057 spi_write_disable();
1058 return 0;
1059}