blob: 16dfe365b89d97423b8198b601c877af141eebf1 [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{
64 int res = 0;
65 while ((spicommands->writecnt || spicommands->readcnt) && !res) {
66 res = spi_send_command(spicommands->writecnt, spicommands->readcnt,
67 spicommands->writearr, spicommands->readarr);
68 }
69 return res;
70}
71
Rudolf Marek48a85e42008-06-30 21:45:17 +000072static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000073{
Uwe Hermann394131e2008-10-18 21:14:13 +000074 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000075 int ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000076 int i;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000077
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000078 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000079 if (ret)
80 return ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000081 printf_debug("RDID returned");
82 for (i = 0; i < bytes; i++)
83 printf_debug(" 0x%02x", readarr[i]);
84 printf_debug("\n");
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000085 return 0;
86}
87
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000088static int spi_rems(unsigned char *readarr)
89{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000090 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
91 uint32_t readaddr;
92 int ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000093
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000094 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000095 if (ret == SPI_INVALID_ADDRESS) {
96 /* Find the lowest even address allowed for reads. */
97 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
98 cmd[1] = (readaddr >> 16) & 0xff,
99 cmd[2] = (readaddr >> 8) & 0xff,
100 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000101 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000102 }
103 if (ret)
104 return ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000105 printf_debug("REMS returned %02x %02x.\n", readarr[0], readarr[1]);
106 return 0;
107}
108
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000109static int spi_res(unsigned char *readarr)
110{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000111 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
112 uint32_t readaddr;
113 int ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000114
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000115 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000116 if (ret == SPI_INVALID_ADDRESS) {
117 /* Find the lowest even address allowed for reads. */
118 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
119 cmd[1] = (readaddr >> 16) & 0xff,
120 cmd[2] = (readaddr >> 8) & 0xff,
121 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000122 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000123 }
124 if (ret)
125 return ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000126 printf_debug("RES returned %02x.\n", readarr[0]);
127 return 0;
128}
129
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000130int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000131{
Uwe Hermann394131e2008-10-18 21:14:13 +0000132 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000133 int result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000134
135 /* Send WREN (Write Enable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000136 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000137
138 if (result)
139 printf_debug("%s failed", __func__);
140 if (result == SPI_INVALID_OPCODE) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000141 switch (spi_controller) {
142 case SPI_CONTROLLER_ICH7:
143 case SPI_CONTROLLER_ICH9:
144 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000145 printf_debug(" due to SPI master limitation, ignoring"
146 " and hoping it will be run as PREOP\n");
147 return 0;
148 default:
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000149 break;
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000150 }
151 }
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000152 if (result)
153 printf_debug("\n");
154
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000155 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000156}
157
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000158int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000159{
Uwe Hermann394131e2008-10-18 21:14:13 +0000160 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000161
162 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000163 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000164}
165
Rudolf Marek48a85e42008-06-30 21:45:17 +0000166static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000167{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000168 unsigned char readarr[4];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000169 uint32_t id1;
170 uint32_t id2;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000171
Rudolf Marek48a85e42008-06-30 21:45:17 +0000172 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000173 return 0;
174
175 if (!oddparity(readarr[0]))
176 printf_debug("RDID byte 0 parity violation.\n");
177
178 /* Check if this is a continuation vendor ID */
179 if (readarr[0] == 0x7f) {
180 if (!oddparity(readarr[1]))
181 printf_debug("RDID byte 1 parity violation.\n");
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000182 id1 = (readarr[0] << 8) | readarr[1];
183 id2 = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000184 if (bytes > 3) {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000185 id2 <<= 8;
186 id2 |= readarr[3];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000187 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000188 } else {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000189 id1 = readarr[0];
190 id2 = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000191 }
192
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000193 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000194
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000195 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000196 /* Print the status register to tell the
197 * user about possible write protection.
198 */
199 spi_prettyprint_status_register(flash);
200
201 return 1;
202 }
203
204 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000205 if (id1 == flash->manufacture_id &&
Peter Stugeda4e5f32008-06-24 01:22:03 +0000206 GENERIC_DEVICE_ID == flash->model_id)
207 return 1;
208
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000209 return 0;
210}
211
Uwe Hermann394131e2008-10-18 21:14:13 +0000212int probe_spi_rdid(struct flashchip *flash)
213{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000214 return probe_spi_rdid_generic(flash, 3);
215}
216
217/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000218int probe_spi_rdid4(struct flashchip *flash)
219{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000220 /* only some SPI chipsets support 4 bytes commands */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000221 switch (spi_controller) {
222 case SPI_CONTROLLER_ICH7:
223 case SPI_CONTROLLER_ICH9:
224 case SPI_CONTROLLER_VIA:
225 case SPI_CONTROLLER_SB600:
226 case SPI_CONTROLLER_WBSIO:
Paul Fox05dfbe62009-06-16 21:08:06 +0000227 case SPI_CONTROLLER_FT2232:
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000228 case SPI_CONTROLLER_DUMMY:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000229 return probe_spi_rdid_generic(flash, 4);
230 default:
231 printf_debug("4b ID not supported on this SPI controller\n");
232 }
233
234 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000235}
236
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000237int probe_spi_rems(struct flashchip *flash)
238{
239 unsigned char readarr[JEDEC_REMS_INSIZE];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000240 uint32_t id1, id2;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000241
242 if (spi_rems(readarr))
243 return 0;
244
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000245 id1 = readarr[0];
246 id2 = readarr[1];
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000247
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000248 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000249
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000250 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000251 /* Print the status register to tell the
252 * user about possible write protection.
253 */
254 spi_prettyprint_status_register(flash);
255
256 return 1;
257 }
258
259 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000260 if (id1 == flash->manufacture_id &&
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000261 GENERIC_DEVICE_ID == flash->model_id)
262 return 1;
263
264 return 0;
265}
266
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000267int probe_spi_res(struct flashchip *flash)
268{
269 unsigned char readarr[3];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000270 uint32_t id2;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000271
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000272 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
273 * In that case, RES is pointless.
274 */
275 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
276 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000277 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000278
Peter Stugeda4e5f32008-06-24 01:22:03 +0000279 if (spi_res(readarr))
280 return 0;
281
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000282 id2 = readarr[0];
283 printf_debug("%s: id 0x%x\n", __FUNCTION__, id2);
284 if (id2 != flash->model_id)
Peter Stugeda4e5f32008-06-24 01:22:03 +0000285 return 0;
286
287 /* Print the status register to tell the
288 * user about possible write protection.
289 */
290 spi_prettyprint_status_register(flash);
291 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000292}
293
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000294uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000295{
Uwe Hermann394131e2008-10-18 21:14:13 +0000296 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Peter Stugebf196e92009-01-26 03:08:45 +0000297 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000298 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000299
300 /* Read Status Register */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000301 if (spi_controller == SPI_CONTROLLER_SB600) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000302 /* SB600 uses a different way to read status register. */
303 return sb600_read_status_register();
304 } else {
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000305 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000306 if (ret)
307 printf_debug("RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000308 }
309
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000310 return readarr[0];
311}
312
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000313/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000314void spi_prettyprint_status_register_common(uint8_t status)
315{
316 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000317 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000318 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000319 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000320 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000321 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000322 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000323 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000324 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000325 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000326 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000327 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000328}
329
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000330/* Prettyprint the status register. Works for
331 * ST M25P series
332 * MX MX25L series
333 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000334void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000335{
336 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000337 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000338 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000339 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000340 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000341}
342
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000343void spi_prettyprint_status_register_sst25(uint8_t status)
344{
345 printf_debug("Chip status register: Block Protect Write Disable "
346 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
347 printf_debug("Chip status register: Auto Address Increment Programming "
348 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
349 spi_prettyprint_status_register_common(status);
350}
351
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000352/* Prettyprint the status register. Works for
353 * SST 25VF016
354 */
355void spi_prettyprint_status_register_sst25vf016(uint8_t status)
356{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000357 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000358 "none",
359 "1F0000H-1FFFFFH",
360 "1E0000H-1FFFFFH",
361 "1C0000H-1FFFFFH",
362 "180000H-1FFFFFH",
363 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000364 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000365 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000366 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000367 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000368 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000369}
370
Peter Stuge5fecee42009-01-26 03:23:50 +0000371void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
372{
373 const char *bpt[] = {
374 "none",
375 "0x70000-0x7ffff",
376 "0x60000-0x7ffff",
377 "0x40000-0x7ffff",
378 "all blocks", "all blocks", "all blocks", "all blocks"
379 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000380 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000381 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000382 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000383}
384
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000385void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000386{
387 uint8_t status;
388
Peter Stugefa8c5502008-05-10 23:07:52 +0000389 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000390 printf_debug("Chip status register is %02x\n", status);
391 switch (flash->manufacture_id) {
392 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000393 if (((flash->model_id & 0xff00) == 0x2000) ||
394 ((flash->model_id & 0xff00) == 0x2500))
395 spi_prettyprint_status_register_st_m25p(status);
396 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000397 case MX_ID:
398 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000399 spi_prettyprint_status_register_st_m25p(status);
400 break;
401 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000402 switch (flash->model_id) {
403 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000404 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000405 break;
406 case 0x8d:
407 case 0x258d:
408 spi_prettyprint_status_register_sst25vf040b(status);
409 break;
Carl-Daniel Hailfinger5100a8a2009-05-13 22:51:27 +0000410 default:
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000411 spi_prettyprint_status_register_sst25(status);
412 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000413 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000414 break;
415 }
416}
Uwe Hermann394131e2008-10-18 21:14:13 +0000417
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000418int spi_chip_erase_60(struct flashchip *flash)
419{
420 const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60};
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000421 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000422
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000423 result = spi_disable_blockprotect();
424 if (result) {
425 printf_debug("spi_disable_blockprotect failed\n");
426 return result;
427 }
428 result = spi_write_enable();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000429 if (result)
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000430 return result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000431 /* Send CE (Chip Erase) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000432 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000433 if (result) {
434 printf_debug("spi_chip_erase_60 failed sending erase\n");
435 return result;
436 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000437 /* Wait until the Write-In-Progress bit is cleared.
438 * This usually takes 1-85 s, so wait in 1 s steps.
439 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000440 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000441 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000442 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000443 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
444 fprintf(stderr, "ERASE FAILED!\n");
445 return -1;
446 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000447 return 0;
448}
449
Peter Stugefa8c5502008-05-10 23:07:52 +0000450int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000451{
Uwe Hermann394131e2008-10-18 21:14:13 +0000452 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 };
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000453 int result;
Uwe Hermann394131e2008-10-18 21:14:13 +0000454
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000455 result = spi_disable_blockprotect();
456 if (result) {
457 printf_debug("spi_disable_blockprotect failed\n");
458 return result;
459 }
460 result = spi_write_enable();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000461 if (result)
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000462 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000463 /* Send CE (Chip Erase) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000464 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000465 if (result) {
466 printf_debug("spi_chip_erase_60 failed sending erase\n");
467 return result;
468 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000469 /* Wait until the Write-In-Progress bit is cleared.
470 * This usually takes 1-85 s, so wait in 1 s steps.
471 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000472 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000473 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000474 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000475 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
476 fprintf(stderr, "ERASE FAILED!\n");
477 return -1;
478 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000479 return 0;
480}
481
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000482int spi_chip_erase_60_c7(struct flashchip *flash)
483{
484 int result;
485 result = spi_chip_erase_60(flash);
486 if (result) {
487 printf_debug("spi_chip_erase_60 failed, trying c7\n");
488 result = spi_chip_erase_c7(flash);
489 }
490 return result;
491}
492
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000493int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000494{
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000495 unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52, };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000496 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000497
498 cmd[1] = (addr & 0x00ff0000) >> 16;
499 cmd[2] = (addr & 0x0000ff00) >> 8;
500 cmd[3] = (addr & 0x000000ff);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000501 result = spi_write_enable();
502 if (result)
503 return result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000504 /* Send BE (Block Erase) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000505 spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000506 /* Wait until the Write-In-Progress bit is cleared.
507 * This usually takes 100-4000 ms, so wait in 100 ms steps.
508 */
509 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000510 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000511 if (check_erased_range(flash, addr, blocklen)) {
512 fprintf(stderr, "ERASE FAILED!\n");
513 return -1;
514 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000515 return 0;
516}
517
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000518/* Block size is usually
519 * 64k for Macronix
520 * 32k for SST
521 * 4-32k non-uniform for EON
522 */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000523int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000524{
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000525 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8, };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000526 int result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000527
528 cmd[1] = (addr & 0x00ff0000) >> 16;
529 cmd[2] = (addr & 0x0000ff00) >> 8;
530 cmd[3] = (addr & 0x000000ff);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000531 result = spi_write_enable();
532 if (result)
533 return result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000534 /* Send BE (Block Erase) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000535 spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000536 /* Wait until the Write-In-Progress bit is cleared.
537 * This usually takes 100-4000 ms, so wait in 100 ms steps.
538 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000539 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000540 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000541 if (check_erased_range(flash, addr, blocklen)) {
542 fprintf(stderr, "ERASE FAILED!\n");
543 return -1;
544 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000545 return 0;
546}
547
Stefan Reinauer424ed222008-10-29 22:13:20 +0000548int spi_chip_erase_d8(struct flashchip *flash)
549{
550 int i, rc = 0;
551 int total_size = flash->total_size * 1024;
552 int erase_size = 64 * 1024;
553
554 spi_disable_blockprotect();
555
556 printf("Erasing chip: \n");
557
558 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000559 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
Stefan Reinauer424ed222008-10-29 22:13:20 +0000560 if (rc) {
561 printf("Error erasing block at 0x%x\n", i);
562 break;
563 }
564 }
565
566 printf("\n");
567
568 return rc;
569}
570
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000571/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000572int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000573{
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000574 unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE, };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000575 int result;
576
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000577 cmd[1] = (addr & 0x00ff0000) >> 16;
578 cmd[2] = (addr & 0x0000ff00) >> 8;
579 cmd[3] = (addr & 0x000000ff);
580
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000581 result = spi_write_enable();
582 if (result)
583 return result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000584 /* Send SE (Sector Erase) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000585 spi_send_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000586 /* Wait until the Write-In-Progress bit is cleared.
587 * This usually takes 15-800 ms, so wait in 10 ms steps.
588 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000589 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000590 programmer_delay(10 * 1000);
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000591 if (check_erased_range(flash, addr, blocklen)) {
592 fprintf(stderr, "ERASE FAILED!\n");
593 return -1;
594 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000595 return 0;
596}
597
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000598int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
599{
600 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
601 fprintf(stderr, "%s called with incorrect arguments\n", __func__);
602 return -1;
603 }
604 return spi_chip_erase_60(flash);
605}
606
607int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
608{
609 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
610 fprintf(stderr, "%s called with incorrect arguments\n", __func__);
611 return -1;
612 }
613 return spi_chip_erase_c7(flash);
614}
615
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000616int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000617{
618 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000619 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000620
621 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000622 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000623
624 if (result)
625 printf_debug("%s failed", __func__);
626 if (result == SPI_INVALID_OPCODE) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000627 switch (spi_controller) {
628 case SPI_CONTROLLER_ICH7:
629 case SPI_CONTROLLER_ICH9:
630 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000631 printf_debug(" due to SPI master limitation, ignoring"
632 " and hoping it will be run as PREOP\n");
633 return 0;
634 default:
635 break;
636 }
637 }
638 if (result)
639 printf_debug("\n");
640
641 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000642}
643
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000644/*
645 * This is according the SST25VF016 datasheet, who knows it is more
646 * generic that this...
647 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000648int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000649{
Uwe Hermann394131e2008-10-18 21:14:13 +0000650 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
651 { JEDEC_WRSR, (unsigned char)status };
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000652
653 /* Send WRSR (Write Status Register) */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000654 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000655}
656
657void spi_byte_program(int address, uint8_t byte)
658{
Uwe Hermann394131e2008-10-18 21:14:13 +0000659 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
660 JEDEC_BYTE_PROGRAM,
661 (address >> 16) & 0xff,
662 (address >> 8) & 0xff,
663 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000664 byte
665 };
666
667 /* Send Byte-Program */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000668 spi_send_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000669}
670
Paul Foxeb3acef2009-06-12 08:10:33 +0000671int spi_nbyte_program(int address, uint8_t *bytes, int len)
672{
673 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
674 JEDEC_BYTE_PROGRAM,
675 (address >> 16) & 0xff,
676 (address >> 8) & 0xff,
677 (address >> 0) & 0xff,
678 };
679
680 if (len > 256) {
681 printf_debug ("%s called for too long a write\n",
682 __FUNCTION__);
683 return 1;
684 }
685
686 memcpy(&cmd[4], bytes, len);
687
688 /* Send Byte-Program */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000689 return spi_send_command(4 + len, 0, cmd, NULL);
Paul Foxeb3acef2009-06-12 08:10:33 +0000690}
691
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000692int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000693{
694 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000695 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000696
Peter Stugefa8c5502008-05-10 23:07:52 +0000697 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000698 /* If there is block protection in effect, unprotect it first. */
699 if ((status & 0x3c) != 0) {
700 printf_debug("Some block protection in effect, disabling\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000701 result = spi_write_status_enable();
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000702 if (result) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000703 printf_debug("spi_write_status_enable failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000704 return result;
705 }
706 result = spi_write_status_register(status & ~0x3c);
707 if (result) {
708 printf_debug("spi_write_status_register failed\n");
709 return result;
710 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000711 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000712 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000713}
714
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000715int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000716{
Uwe Hermann394131e2008-10-18 21:14:13 +0000717 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
718 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000719 (address >> 16) & 0xff,
720 (address >> 8) & 0xff,
721 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000722 };
723
724 /* Send Read */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000725 return spi_send_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000726}
727
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000728/*
729 * Read a complete flash chip.
730 * Each page is read separately in chunks with a maximum size of chunksize.
731 */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000732int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000733{
734 int rc = 0;
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000735 int i, j, starthere, lenhere;
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000736 int page_size = flash->page_size;
737 int toread;
738
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000739 /* Warning: This loop has a very unusual condition and body.
740 * The loop needs to go through each page with at least one affected
741 * byte. The lowest page number is (start / page_size) since that
742 * division rounds down. The highest page number we want is the page
743 * where the last byte of the range lives. That last byte has the
744 * address (start + len - 1), thus the highest page number is
745 * (start + len - 1) / page_size. Since we want to include that last
746 * page as well, the loop condition uses <=.
747 */
748 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
749 /* Byte position of the first byte in the range in this page. */
750 /* starthere is an offset to the base address of the chip. */
751 starthere = max(start, i * page_size);
752 /* Length of bytes in the range in this page. */
753 lenhere = min(start + len, (i + 1) * page_size) - starthere;
754 for (j = 0; j < lenhere; j += chunksize) {
755 toread = min(chunksize, lenhere - j);
756 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000757 if (rc)
758 break;
759 }
760 if (rc)
761 break;
762 }
763
764 return rc;
765}
766
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000767int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000768{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000769 switch (spi_controller) {
770 case SPI_CONTROLLER_IT87XX:
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000771 return it8716f_spi_chip_read(flash, buf, start, len);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000772 case SPI_CONTROLLER_SB600:
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000773 return sb600_spi_read(flash, buf, start, len);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000774 case SPI_CONTROLLER_ICH7:
775 case SPI_CONTROLLER_ICH9:
776 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000777 return ich_spi_read(flash, buf, start, len);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000778 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000779 return wbsio_spi_read(flash, buf, start, len);
Paul Fox05dfbe62009-06-16 21:08:06 +0000780 case SPI_CONTROLLER_FT2232:
781 return ft2232_spi_read(flash, buf, start, len);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000782 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000783 printf_debug
784 ("%s called, but no SPI chipset/strapping detected\n",
785 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000786 }
787
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000788 return 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000789}
790
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000791/*
792 * Program chip using byte programming. (SLOW!)
793 * This is for chips which can only handle one byte writes
794 * and for chips where memory mapped programming is impossible
795 * (e.g. due to size constraints in IT87* for over 512 kB)
796 */
797int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
798{
799 int total_size = 1024 * flash->total_size;
800 int i;
801
802 spi_disable_blockprotect();
803 for (i = 0; i < total_size; i++) {
804 spi_write_enable();
805 spi_byte_program(i, buf[i]);
806 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000807 programmer_delay(10);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000808 }
809
810 return 0;
811}
812
813/*
814 * Program chip using page (256 bytes) programming.
815 * Some SPI masters can't do this, they use single byte programming instead.
816 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +0000817int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000818{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000819 switch (spi_controller) {
820 case SPI_CONTROLLER_IT87XX:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000821 return it8716f_spi_chip_write_256(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000822 case SPI_CONTROLLER_SB600:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000823 return sb600_spi_write_1(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000824 case SPI_CONTROLLER_ICH7:
825 case SPI_CONTROLLER_ICH9:
826 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000827 return ich_spi_write_256(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000828 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000829 return wbsio_spi_write_1(flash, buf);
Paul Fox05dfbe62009-06-16 21:08:06 +0000830 case SPI_CONTROLLER_FT2232:
831 return ft2232_spi_write_256(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000832 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000833 printf_debug
834 ("%s called, but no SPI chipset/strapping detected\n",
835 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000836 }
837
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000838 return 1;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000839}
Peter Stugefd9217d2009-01-26 03:37:40 +0000840
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000841uint32_t spi_get_valid_read_addr(void)
842{
843 /* Need to return BBAR for ICH chipsets. */
844 return 0;
845}
846
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000847int spi_aai_write(struct flashchip *flash, uint8_t *buf)
848{
Peter Stugefd9217d2009-01-26 03:37:40 +0000849 uint32_t pos = 2, size = flash->total_size * 1024;
850 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000851 int result;
852
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000853 switch (spi_controller) {
854 case SPI_CONTROLLER_WBSIO:
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000855 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
856 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000857 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000858 default:
859 break;
Peter Stugefd9217d2009-01-26 03:37:40 +0000860 }
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +0000861 if (flash->erase(flash)) {
862 fprintf(stderr, "ERASE FAILED!\n");
863 return -1;
864 }
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000865 result = spi_write_enable();
866 if (result)
867 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000868 spi_send_command(6, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +0000869 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000870 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +0000871 while (pos < size) {
872 w[1] = buf[pos++];
873 w[2] = buf[pos++];
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000874 spi_send_command(3, 0, w, NULL);
Peter Stugefd9217d2009-01-26 03:37:40 +0000875 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000876 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +0000877 }
878 spi_write_disable();
879 return 0;
880}