blob: ceda14ef95c2dc5bef6330e99672aa203760a156 [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 Hailfingerd0478292009-07-10 21:08:55 +000035int spi_send_command(unsigned int writecnt, unsigned int readcnt,
Uwe Hermann394131e2008-10-18 21:14:13 +000036 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000037{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000038 switch (spi_controller) {
39 case SPI_CONTROLLER_IT87XX:
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000040 return it8716f_spi_send_command(writecnt, readcnt, writearr,
Uwe Hermann394131e2008-10-18 21:14:13 +000041 readarr);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000042 case SPI_CONTROLLER_ICH7:
43 case SPI_CONTROLLER_ICH9:
44 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000045 return ich_spi_send_command(writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000046 case SPI_CONTROLLER_SB600:
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000047 return sb600_spi_send_command(writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000048 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000049 return wbsio_spi_send_command(writecnt, readcnt, writearr, readarr);
Paul Fox05dfbe62009-06-16 21:08:06 +000050 case SPI_CONTROLLER_FT2232:
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000051 return ft2232_spi_send_command(writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000052 case SPI_CONTROLLER_DUMMY:
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000053 return dummy_spi_send_command(writecnt, readcnt, writearr, readarr);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000054 default:
Uwe Hermann394131e2008-10-18 21:14:13 +000055 printf_debug
56 ("%s called, but no SPI chipset/strapping detected\n",
57 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000058 }
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000059 return 1;
60}
61
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000062int spi_send_multicommand(struct spi_command *spicommands)
63{
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +000064 int ret = 0;
65 while ((spicommands->writecnt || spicommands->readcnt) && !ret) {
66 ret = spi_send_command(spicommands->writecnt, spicommands->readcnt,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000067 spicommands->writearr, spicommands->readarr);
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +000068 /* This awful hack needs to be replaced with a multicommand
69 * capable ICH/VIA SPI driver.
70 */
71 if ((ret == SPI_INVALID_OPCODE) &&
72 ((spicommands->writearr[0] == JEDEC_WREN) ||
73 (spicommands->writearr[0] == JEDEC_EWSR))) {
74 switch (spi_controller) {
75 case SPI_CONTROLLER_ICH7:
76 case SPI_CONTROLLER_ICH9:
77 case SPI_CONTROLLER_VIA:
78 printf_debug(" due to SPI master limitation, ignoring"
79 " and hoping it will be run as PREOP\n");
80 ret = 0;
81 default:
82 break;
83 }
84 }
85 spicommands++;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000086 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +000087 return ret;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000088}
89
Rudolf Marek48a85e42008-06-30 21:45:17 +000090static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000091{
Uwe Hermann394131e2008-10-18 21:14:13 +000092 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000093 int ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000094 int i;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000095
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000096 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000097 if (ret)
98 return ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000099 printf_debug("RDID returned");
100 for (i = 0; i < bytes; i++)
101 printf_debug(" 0x%02x", readarr[i]);
102 printf_debug("\n");
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000103 return 0;
104}
105
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000106static int spi_rems(unsigned char *readarr)
107{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000108 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
109 uint32_t readaddr;
110 int ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000111
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000112 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000113 if (ret == SPI_INVALID_ADDRESS) {
114 /* Find the lowest even address allowed for reads. */
115 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
116 cmd[1] = (readaddr >> 16) & 0xff,
117 cmd[2] = (readaddr >> 8) & 0xff,
118 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000119 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000120 }
121 if (ret)
122 return ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000123 printf_debug("REMS returned %02x %02x.\n", readarr[0], readarr[1]);
124 return 0;
125}
126
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000127static int spi_res(unsigned char *readarr)
128{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000129 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
130 uint32_t readaddr;
131 int ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000132
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000133 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000134 if (ret == SPI_INVALID_ADDRESS) {
135 /* Find the lowest even address allowed for reads. */
136 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
137 cmd[1] = (readaddr >> 16) & 0xff,
138 cmd[2] = (readaddr >> 8) & 0xff,
139 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000140 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000141 }
142 if (ret)
143 return ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000144 printf_debug("RES returned %02x.\n", readarr[0]);
145 return 0;
146}
147
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000148int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000149{
Uwe Hermann394131e2008-10-18 21:14:13 +0000150 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000151 int result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000152
153 /* Send WREN (Write Enable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000154 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000155
156 if (result)
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000157 printf_debug("%s failed\n", __func__);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000158
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000159 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000160}
161
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000162int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000163{
Uwe Hermann394131e2008-10-18 21:14:13 +0000164 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000165
166 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000167 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000168}
169
Rudolf Marek48a85e42008-06-30 21:45:17 +0000170static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000171{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000172 unsigned char readarr[4];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000173 uint32_t id1;
174 uint32_t id2;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000175
Rudolf Marek48a85e42008-06-30 21:45:17 +0000176 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000177 return 0;
178
179 if (!oddparity(readarr[0]))
180 printf_debug("RDID byte 0 parity violation.\n");
181
182 /* Check if this is a continuation vendor ID */
183 if (readarr[0] == 0x7f) {
184 if (!oddparity(readarr[1]))
185 printf_debug("RDID byte 1 parity violation.\n");
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000186 id1 = (readarr[0] << 8) | readarr[1];
187 id2 = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000188 if (bytes > 3) {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000189 id2 <<= 8;
190 id2 |= readarr[3];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000191 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000192 } else {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000193 id1 = readarr[0];
194 id2 = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000195 }
196
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000197 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000198
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000199 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000200 /* Print the status register to tell the
201 * user about possible write protection.
202 */
203 spi_prettyprint_status_register(flash);
204
205 return 1;
206 }
207
208 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000209 if (id1 == flash->manufacture_id &&
Peter Stugeda4e5f32008-06-24 01:22:03 +0000210 GENERIC_DEVICE_ID == flash->model_id)
211 return 1;
212
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000213 return 0;
214}
215
Uwe Hermann394131e2008-10-18 21:14:13 +0000216int probe_spi_rdid(struct flashchip *flash)
217{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000218 return probe_spi_rdid_generic(flash, 3);
219}
220
221/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000222int probe_spi_rdid4(struct flashchip *flash)
223{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000224 /* only some SPI chipsets support 4 bytes commands */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000225 switch (spi_controller) {
226 case SPI_CONTROLLER_ICH7:
227 case SPI_CONTROLLER_ICH9:
228 case SPI_CONTROLLER_VIA:
229 case SPI_CONTROLLER_SB600:
230 case SPI_CONTROLLER_WBSIO:
Paul Fox05dfbe62009-06-16 21:08:06 +0000231 case SPI_CONTROLLER_FT2232:
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000232 case SPI_CONTROLLER_DUMMY:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000233 return probe_spi_rdid_generic(flash, 4);
234 default:
235 printf_debug("4b ID not supported on this SPI controller\n");
236 }
237
238 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000239}
240
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000241int probe_spi_rems(struct flashchip *flash)
242{
243 unsigned char readarr[JEDEC_REMS_INSIZE];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000244 uint32_t id1, id2;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000245
246 if (spi_rems(readarr))
247 return 0;
248
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000249 id1 = readarr[0];
250 id2 = readarr[1];
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000251
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000252 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000253
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000254 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000255 /* Print the status register to tell the
256 * user about possible write protection.
257 */
258 spi_prettyprint_status_register(flash);
259
260 return 1;
261 }
262
263 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000264 if (id1 == flash->manufacture_id &&
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000265 GENERIC_DEVICE_ID == flash->model_id)
266 return 1;
267
268 return 0;
269}
270
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000271int probe_spi_res(struct flashchip *flash)
272{
273 unsigned char readarr[3];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000274 uint32_t id2;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000275
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000276 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
277 * In that case, RES is pointless.
278 */
279 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
280 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000281 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000282
Peter Stugeda4e5f32008-06-24 01:22:03 +0000283 if (spi_res(readarr))
284 return 0;
285
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000286 id2 = readarr[0];
287 printf_debug("%s: id 0x%x\n", __FUNCTION__, id2);
288 if (id2 != flash->model_id)
Peter Stugeda4e5f32008-06-24 01:22:03 +0000289 return 0;
290
291 /* Print the status register to tell the
292 * user about possible write protection.
293 */
294 spi_prettyprint_status_register(flash);
295 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000296}
297
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000298uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000299{
Uwe Hermann394131e2008-10-18 21:14:13 +0000300 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Peter Stugebf196e92009-01-26 03:08:45 +0000301 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000302 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000303
304 /* Read Status Register */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000305 if (spi_controller == SPI_CONTROLLER_SB600) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000306 /* SB600 uses a different way to read status register. */
307 return sb600_read_status_register();
308 } else {
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000309 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000310 if (ret)
311 printf_debug("RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000312 }
313
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000314 return readarr[0];
315}
316
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000317/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000318void spi_prettyprint_status_register_common(uint8_t status)
319{
320 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000321 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000322 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000323 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000324 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000325 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000326 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000327 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000328 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000329 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000330 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000331 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000332}
333
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000334/* Prettyprint the status register. Works for
335 * ST M25P series
336 * MX MX25L series
337 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000338void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000339{
340 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000341 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000342 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000343 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000344 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000345}
346
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000347void spi_prettyprint_status_register_sst25(uint8_t status)
348{
349 printf_debug("Chip status register: Block Protect Write Disable "
350 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
351 printf_debug("Chip status register: Auto Address Increment Programming "
352 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
353 spi_prettyprint_status_register_common(status);
354}
355
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000356/* Prettyprint the status register. Works for
357 * SST 25VF016
358 */
359void spi_prettyprint_status_register_sst25vf016(uint8_t status)
360{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000361 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000362 "none",
363 "1F0000H-1FFFFFH",
364 "1E0000H-1FFFFFH",
365 "1C0000H-1FFFFFH",
366 "180000H-1FFFFFH",
367 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000368 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000369 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000370 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000371 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000372 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000373}
374
Peter Stuge5fecee42009-01-26 03:23:50 +0000375void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
376{
377 const char *bpt[] = {
378 "none",
379 "0x70000-0x7ffff",
380 "0x60000-0x7ffff",
381 "0x40000-0x7ffff",
382 "all blocks", "all blocks", "all blocks", "all blocks"
383 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000384 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000385 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000386 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000387}
388
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000389void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000390{
391 uint8_t status;
392
Peter Stugefa8c5502008-05-10 23:07:52 +0000393 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000394 printf_debug("Chip status register is %02x\n", status);
395 switch (flash->manufacture_id) {
396 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000397 if (((flash->model_id & 0xff00) == 0x2000) ||
398 ((flash->model_id & 0xff00) == 0x2500))
399 spi_prettyprint_status_register_st_m25p(status);
400 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000401 case MX_ID:
402 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000403 spi_prettyprint_status_register_st_m25p(status);
404 break;
405 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000406 switch (flash->model_id) {
407 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000408 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000409 break;
410 case 0x8d:
411 case 0x258d:
412 spi_prettyprint_status_register_sst25vf040b(status);
413 break;
Carl-Daniel Hailfinger5100a8a2009-05-13 22:51:27 +0000414 default:
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000415 spi_prettyprint_status_register_sst25(status);
416 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000417 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000418 break;
419 }
420}
Uwe Hermann394131e2008-10-18 21:14:13 +0000421
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000422int spi_chip_erase_60(struct flashchip *flash)
423{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000424 int result;
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000425 struct spi_command spicommands[] = {
426 {
427 .writecnt = JEDEC_WREN_OUTSIZE,
428 .writearr = (const unsigned char[]){ JEDEC_WREN },
429 .readcnt = 0,
430 .readarr = NULL,
431 }, {
432 .writecnt = JEDEC_CE_60_OUTSIZE,
433 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
434 .readcnt = 0,
435 .readarr = NULL,
436 }, {
437 .writecnt = 0,
438 .writearr = NULL,
439 .readcnt = 0,
440 .readarr = NULL,
441 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000442
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000443 result = spi_disable_blockprotect();
444 if (result) {
445 printf_debug("spi_disable_blockprotect failed\n");
446 return result;
447 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000448
449 result = spi_send_multicommand(spicommands);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000450 if (result) {
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000451 printf_debug("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000452 return result;
453 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000454 /* Wait until the Write-In-Progress bit is cleared.
455 * This usually takes 1-85 s, so wait in 1 s steps.
456 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000457 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000458 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000459 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000460 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
461 fprintf(stderr, "ERASE FAILED!\n");
462 return -1;
463 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000464 return 0;
465}
466
Peter Stugefa8c5502008-05-10 23:07:52 +0000467int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000468{
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000469 int result;
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000470 struct spi_command spicommands[] = {
471 {
472 .writecnt = JEDEC_WREN_OUTSIZE,
473 .writearr = (const unsigned char[]){ JEDEC_WREN },
474 .readcnt = 0,
475 .readarr = NULL,
476 }, {
477 .writecnt = JEDEC_CE_C7_OUTSIZE,
478 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
479 .readcnt = 0,
480 .readarr = NULL,
481 }, {
482 .writecnt = 0,
483 .writearr = NULL,
484 .readcnt = 0,
485 .readarr = NULL,
486 }};
Uwe Hermann394131e2008-10-18 21:14:13 +0000487
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000488 result = spi_disable_blockprotect();
489 if (result) {
490 printf_debug("spi_disable_blockprotect failed\n");
491 return result;
492 }
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000493
494 result = spi_send_multicommand(spicommands);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000495 if (result) {
Carl-Daniel Hailfinger60d71182009-07-11 19:28:36 +0000496 printf_debug("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000497 return result;
498 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000499 /* Wait until the Write-In-Progress bit is cleared.
500 * This usually takes 1-85 s, so wait in 1 s steps.
501 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000502 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000503 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000504 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000505 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
506 fprintf(stderr, "ERASE FAILED!\n");
507 return -1;
508 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000509 return 0;
510}
511
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000512int spi_chip_erase_60_c7(struct flashchip *flash)
513{
514 int result;
515 result = spi_chip_erase_60(flash);
516 if (result) {
517 printf_debug("spi_chip_erase_60 failed, trying c7\n");
518 result = spi_chip_erase_c7(flash);
519 }
520 return result;
521}
522
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000523int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000524{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000525 int result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000526 struct spi_command spicommands[] = {
527 {
528 .writecnt = JEDEC_WREN_OUTSIZE,
529 .writearr = (const unsigned char[]){ JEDEC_WREN },
530 .readcnt = 0,
531 .readarr = NULL,
532 }, {
533 .writecnt = JEDEC_BE_52_OUTSIZE,
534 .writearr = (const unsigned char[]){ JEDEC_BE_52, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
535 .readcnt = 0,
536 .readarr = NULL,
537 }, {
538 .writecnt = 0,
539 .writearr = NULL,
540 .readcnt = 0,
541 .readarr = NULL,
542 }};
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000543
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000544 result = spi_send_multicommand(spicommands);
545 if (result) {
546 printf_debug("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000547 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000548 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000549 /* Wait until the Write-In-Progress bit is cleared.
550 * This usually takes 100-4000 ms, so wait in 100 ms steps.
551 */
552 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000553 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000554 if (check_erased_range(flash, addr, blocklen)) {
555 fprintf(stderr, "ERASE FAILED!\n");
556 return -1;
557 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000558 return 0;
559}
560
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000561/* Block size is usually
562 * 64k for Macronix
563 * 32k for SST
564 * 4-32k non-uniform for EON
565 */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000566int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000567{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000568 int result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000569 struct spi_command spicommands[] = {
570 {
571 .writecnt = JEDEC_WREN_OUTSIZE,
572 .writearr = (const unsigned char[]){ JEDEC_WREN },
573 .readcnt = 0,
574 .readarr = NULL,
575 }, {
576 .writecnt = JEDEC_BE_D8_OUTSIZE,
577 .writearr = (const unsigned char[]){ JEDEC_BE_D8, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
578 .readcnt = 0,
579 .readarr = NULL,
580 }, {
581 .writecnt = 0,
582 .writearr = NULL,
583 .readcnt = 0,
584 .readarr = NULL,
585 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000586
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000587 result = spi_send_multicommand(spicommands);
588 if (result) {
589 printf_debug("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000590 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000591 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000592 /* Wait until the Write-In-Progress bit is cleared.
593 * This usually takes 100-4000 ms, so wait in 100 ms steps.
594 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000595 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000596 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000597 if (check_erased_range(flash, addr, blocklen)) {
598 fprintf(stderr, "ERASE FAILED!\n");
599 return -1;
600 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000601 return 0;
602}
603
Stefan Reinauer424ed222008-10-29 22:13:20 +0000604int spi_chip_erase_d8(struct flashchip *flash)
605{
606 int i, rc = 0;
607 int total_size = flash->total_size * 1024;
608 int erase_size = 64 * 1024;
609
610 spi_disable_blockprotect();
611
612 printf("Erasing chip: \n");
613
614 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000615 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000616 if (rc) {
617 printf("Error erasing block at 0x%x\n", i);
618 break;
619 }
620 }
621
622 printf("\n");
623
624 return rc;
625}
626
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000627/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000628int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000629{
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000630 int result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000631 struct spi_command spicommands[] = {
632 {
633 .writecnt = JEDEC_WREN_OUTSIZE,
634 .writearr = (const unsigned char[]){ JEDEC_WREN },
635 .readcnt = 0,
636 .readarr = NULL,
637 }, {
638 .writecnt = JEDEC_SE_OUTSIZE,
639 .writearr = (const unsigned char[]){ JEDEC_SE, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
640 .readcnt = 0,
641 .readarr = NULL,
642 }, {
643 .writecnt = 0,
644 .writearr = NULL,
645 .readcnt = 0,
646 .readarr = NULL,
647 }};
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000648
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000649 result = spi_send_multicommand(spicommands);
650 if (result) {
651 printf_debug("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000652 return result;
Carl-Daniel Hailfinger39fa9b52009-07-11 22:26:52 +0000653 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000654 /* Wait until the Write-In-Progress bit is cleared.
655 * This usually takes 15-800 ms, so wait in 10 ms steps.
656 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000657 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000658 programmer_delay(10 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000659 if (check_erased_range(flash, addr, blocklen)) {
660 fprintf(stderr, "ERASE FAILED!\n");
661 return -1;
662 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000663 return 0;
664}
665
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000666int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
667{
668 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
669 fprintf(stderr, "%s called with incorrect arguments\n", __func__);
670 return -1;
671 }
672 return spi_chip_erase_60(flash);
673}
674
675int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
676{
677 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
678 fprintf(stderr, "%s called with incorrect arguments\n", __func__);
679 return -1;
680 }
681 return spi_chip_erase_c7(flash);
682}
683
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000684int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000685{
686 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000687 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000688
689 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000690 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000691
692 if (result)
693 printf_debug("%s failed", __func__);
694 if (result == SPI_INVALID_OPCODE) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000695 switch (spi_controller) {
696 case SPI_CONTROLLER_ICH7:
697 case SPI_CONTROLLER_ICH9:
698 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000699 printf_debug(" due to SPI master limitation, ignoring"
700 " and hoping it will be run as PREOP\n");
701 return 0;
702 default:
703 break;
704 }
705 }
706 if (result)
707 printf_debug("\n");
708
709 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000710}
711
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000712/*
713 * This is according the SST25VF016 datasheet, who knows it is more
714 * generic that this...
715 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000716int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000717{
Uwe Hermann394131e2008-10-18 21:14:13 +0000718 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
719 { JEDEC_WRSR, (unsigned char)status };
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000720
721 /* Send WRSR (Write Status Register) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000722 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000723}
724
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000725int spi_byte_program(int addr, uint8_t byte)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000726{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000727 int result;
728 struct spi_command spicommands[] = {
729 {
730 .writecnt = JEDEC_WREN_OUTSIZE,
731 .writearr = (const unsigned char[]){ JEDEC_WREN },
732 .readcnt = 0,
733 .readarr = NULL,
734 }, {
735 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
736 .writearr = (const unsigned char[]){ JEDEC_BYTE_PROGRAM, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff), byte },
737 .readcnt = 0,
738 .readarr = NULL,
739 }, {
740 .writecnt = 0,
741 .writearr = NULL,
742 .readcnt = 0,
743 .readarr = NULL,
744 }};
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000745
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000746 result = spi_send_multicommand(spicommands);
747 if (result) {
748 printf_debug("%s failed during command execution\n", __func__);
749 return result;
750 }
751 return result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000752}
753
Paul Foxeb3acef2009-06-12 08:10:33 +0000754int spi_nbyte_program(int address, uint8_t *bytes, int len)
755{
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000756 int result;
757 /* FIXME: Switch to malloc based on len unless that kills speed. */
Paul Foxeb3acef2009-06-12 08:10:33 +0000758 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
759 JEDEC_BYTE_PROGRAM,
760 (address >> 16) & 0xff,
761 (address >> 8) & 0xff,
762 (address >> 0) & 0xff,
763 };
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000764 struct spi_command spicommands[] = {
765 {
766 .writecnt = JEDEC_WREN_OUTSIZE,
767 .writearr = (const unsigned char[]){ JEDEC_WREN },
768 .readcnt = 0,
769 .readarr = NULL,
770 }, {
771 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
772 .writearr = cmd,
773 .readcnt = 0,
774 .readarr = NULL,
775 }, {
776 .writecnt = 0,
777 .writearr = NULL,
778 .readcnt = 0,
779 .readarr = NULL,
780 }};
Paul Foxeb3acef2009-06-12 08:10:33 +0000781
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000782 if (!len) {
783 printf_debug ("%s called for zero-length write\n", __func__);
784 return 1;
785 }
Paul Foxeb3acef2009-06-12 08:10:33 +0000786 if (len > 256) {
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000787 printf_debug ("%s called for too long a write\n", __func__);
Paul Foxeb3acef2009-06-12 08:10:33 +0000788 return 1;
789 }
790
791 memcpy(&cmd[4], bytes, len);
792
Carl-Daniel Hailfinger2f1b36f2009-07-12 12:06:18 +0000793 result = spi_send_multicommand(spicommands);
794 if (result) {
795 printf_debug("%s failed during command execution\n", __func__);
796 return result;
797 }
798 return result;
Paul Foxeb3acef2009-06-12 08:10:33 +0000799}
800
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000801int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000802{
803 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000804 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000805
Peter Stugefa8c5502008-05-10 23:07:52 +0000806 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000807 /* If there is block protection in effect, unprotect it first. */
808 if ((status & 0x3c) != 0) {
809 printf_debug("Some block protection in effect, disabling\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000810 result = spi_write_status_enable();
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000811 if (result) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000812 printf_debug("spi_write_status_enable failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000813 return result;
814 }
815 result = spi_write_status_register(status & ~0x3c);
816 if (result) {
817 printf_debug("spi_write_status_register failed\n");
818 return result;
819 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000820 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000821 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000822}
823
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000824int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000825{
Uwe Hermann394131e2008-10-18 21:14:13 +0000826 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
827 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000828 (address >> 16) & 0xff,
829 (address >> 8) & 0xff,
830 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000831 };
832
833 /* Send Read */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000834 return spi_send_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000835}
836
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000837/*
838 * Read a complete flash chip.
839 * Each page is read separately in chunks with a maximum size of chunksize.
840 */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000841int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000842{
843 int rc = 0;
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000844 int i, j, starthere, lenhere;
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000845 int page_size = flash->page_size;
846 int toread;
847
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000848 /* Warning: This loop has a very unusual condition and body.
849 * The loop needs to go through each page with at least one affected
850 * byte. The lowest page number is (start / page_size) since that
851 * division rounds down. The highest page number we want is the page
852 * where the last byte of the range lives. That last byte has the
853 * address (start + len - 1), thus the highest page number is
854 * (start + len - 1) / page_size. Since we want to include that last
855 * page as well, the loop condition uses <=.
856 */
857 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
858 /* Byte position of the first byte in the range in this page. */
859 /* starthere is an offset to the base address of the chip. */
860 starthere = max(start, i * page_size);
861 /* Length of bytes in the range in this page. */
862 lenhere = min(start + len, (i + 1) * page_size) - starthere;
863 for (j = 0; j < lenhere; j += chunksize) {
864 toread = min(chunksize, lenhere - j);
865 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000866 if (rc)
867 break;
868 }
869 if (rc)
870 break;
871 }
872
873 return rc;
874}
875
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000876int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000877{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000878 switch (spi_controller) {
879 case SPI_CONTROLLER_IT87XX:
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000880 return it8716f_spi_chip_read(flash, buf, start, len);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000881 case SPI_CONTROLLER_SB600:
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000882 return sb600_spi_read(flash, buf, start, len);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000883 case SPI_CONTROLLER_ICH7:
884 case SPI_CONTROLLER_ICH9:
885 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000886 return ich_spi_read(flash, buf, start, len);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000887 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000888 return wbsio_spi_read(flash, buf, start, len);
Paul Fox05dfbe62009-06-16 21:08:06 +0000889 case SPI_CONTROLLER_FT2232:
890 return ft2232_spi_read(flash, buf, start, len);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000891 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000892 printf_debug
893 ("%s called, but no SPI chipset/strapping detected\n",
894 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000895 }
896
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000897 return 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000898}
899
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000900/*
901 * Program chip using byte programming. (SLOW!)
902 * This is for chips which can only handle one byte writes
903 * and for chips where memory mapped programming is impossible
904 * (e.g. due to size constraints in IT87* for over 512 kB)
905 */
906int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
907{
908 int total_size = 1024 * flash->total_size;
909 int i;
910
911 spi_disable_blockprotect();
912 for (i = 0; i < total_size; i++) {
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000913 spi_byte_program(i, buf[i]);
914 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000915 programmer_delay(10);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000916 }
917
918 return 0;
919}
920
921/*
922 * Program chip using page (256 bytes) programming.
923 * Some SPI masters can't do this, they use single byte programming instead.
924 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +0000925int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000926{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000927 switch (spi_controller) {
928 case SPI_CONTROLLER_IT87XX:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000929 return it8716f_spi_chip_write_256(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000930 case SPI_CONTROLLER_SB600:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000931 return sb600_spi_write_1(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000932 case SPI_CONTROLLER_ICH7:
933 case SPI_CONTROLLER_ICH9:
934 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000935 return ich_spi_write_256(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000936 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000937 return wbsio_spi_write_1(flash, buf);
Paul Fox05dfbe62009-06-16 21:08:06 +0000938 case SPI_CONTROLLER_FT2232:
939 return ft2232_spi_write_256(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000940 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000941 printf_debug
942 ("%s called, but no SPI chipset/strapping detected\n",
943 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000944 }
945
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000946 return 1;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000947}
Peter Stugefd9217d2009-01-26 03:37:40 +0000948
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000949uint32_t spi_get_valid_read_addr(void)
950{
951 /* Need to return BBAR for ICH chipsets. */
952 return 0;
953}
954
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000955int spi_aai_write(struct flashchip *flash, uint8_t *buf)
956{
Peter Stugefd9217d2009-01-26 03:37:40 +0000957 uint32_t pos = 2, size = flash->total_size * 1024;
958 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000959 int result;
960
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000961 switch (spi_controller) {
962 case SPI_CONTROLLER_WBSIO:
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000963 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
964 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000965 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000966 default:
967 break;
Peter Stugefd9217d2009-01-26 03:37:40 +0000968 }
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000969 if (flash->erase(flash)) {
970 fprintf(stderr, "ERASE FAILED!\n");
971 return -1;
972 }
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000973 result = spi_write_enable();
974 if (result)
975 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000976 spi_send_command(6, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +0000977 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000978 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +0000979 while (pos < size) {
980 w[1] = buf[pos++];
981 w[2] = buf[pos++];
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000982 spi_send_command(3, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +0000983 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000984 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +0000985 }
986 spi_write_disable();
987 return 0;
988}