blob: 0a7fd2ef4ef6fb24429c9d36300a03dbb169c476 [file] [log] [blame]
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +00001/*
2 * This file is part of the flashrom project.
3 *
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +00004 * Copyright (C) 2007, 2008 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
25#include <stdio.h>
26#include <pci/pci.h>
27#include <stdint.h>
28#include <string.h>
29#include "flash.h"
Carl-Daniel Hailfingerd6cbf762008-05-13 14:58:23 +000030#include "spi.h"
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000031
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +000032void spi_prettyprint_status_register(struct flashchip *flash);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000033
Uwe Hermann394131e2008-10-18 21:14:13 +000034int spi_command(unsigned int writecnt, unsigned int readcnt,
35 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000036{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000037 switch (flashbus) {
38 case BUS_TYPE_IT87XX_SPI:
Uwe Hermann394131e2008-10-18 21:14:13 +000039 return it8716f_spi_command(writecnt, readcnt, writearr,
40 readarr);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000041 case BUS_TYPE_ICH7_SPI:
42 case BUS_TYPE_ICH9_SPI:
43 case BUS_TYPE_VIA_SPI:
Uwe Hermann394131e2008-10-18 21:14:13 +000044 return ich_spi_command(writecnt, readcnt, writearr, readarr);
Jason Wanga3f04be2008-11-28 21:36:51 +000045 case BUS_TYPE_SB600_SPI:
46 return sb600_spi_command(writecnt, readcnt, writearr, readarr);
Peter Stugebf196e92009-01-26 03:08:45 +000047 case BUS_TYPE_WBSIO_SPI:
48 return wbsio_spi_command(writecnt, readcnt, writearr, readarr);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000049 default:
Uwe Hermann394131e2008-10-18 21:14:13 +000050 printf_debug
51 ("%s called, but no SPI chipset/strapping detected\n",
52 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000053 }
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000054 return 1;
55}
56
Rudolf Marek48a85e42008-06-30 21:45:17 +000057static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000058{
Uwe Hermann394131e2008-10-18 21:14:13 +000059 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000060
Peter Stugef83221b2008-07-07 06:38:51 +000061 if (spi_command(sizeof(cmd), bytes, cmd, readarr))
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000062 return 1;
Uwe Hermann394131e2008-10-18 21:14:13 +000063 printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1],
64 readarr[2]);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000065 return 0;
66}
67
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000068static int spi_rems(unsigned char *readarr)
69{
70 const unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
71
72 if (spi_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr))
73 return 1;
74 printf_debug("REMS returned %02x %02x.\n", readarr[0], readarr[1]);
75 return 0;
76}
77
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000078static int spi_res(unsigned char *readarr)
79{
Uwe Hermann394131e2008-10-18 21:14:13 +000080 const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000081
Peter Stugef83221b2008-07-07 06:38:51 +000082 if (spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr))
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000083 return 1;
84 printf_debug("RES returned %02x.\n", readarr[0]);
85 return 0;
86}
87
Uwe Hermann7b2969b2009-04-15 10:52:49 +000088int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000089{
Uwe Hermann394131e2008-10-18 21:14:13 +000090 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000091
92 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +000093 return spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000094}
95
Uwe Hermann7b2969b2009-04-15 10:52:49 +000096int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000097{
Uwe Hermann394131e2008-10-18 21:14:13 +000098 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +000099
100 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000101 return spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000102}
103
Rudolf Marek48a85e42008-06-30 21:45:17 +0000104static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000105{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000106 unsigned char readarr[4];
Carl-Daniel Hailfinger1263d2a2008-02-06 22:07:58 +0000107 uint32_t manuf_id;
108 uint32_t model_id;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000109
Rudolf Marek48a85e42008-06-30 21:45:17 +0000110 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000111 return 0;
112
113 if (!oddparity(readarr[0]))
114 printf_debug("RDID byte 0 parity violation.\n");
115
116 /* Check if this is a continuation vendor ID */
117 if (readarr[0] == 0x7f) {
118 if (!oddparity(readarr[1]))
119 printf_debug("RDID byte 1 parity violation.\n");
120 manuf_id = (readarr[0] << 8) | readarr[1];
121 model_id = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000122 if (bytes > 3) {
123 model_id <<= 8;
124 model_id |= readarr[3];
125 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000126 } else {
127 manuf_id = readarr[0];
128 model_id = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000129 }
130
Peter Stuge5cafc332009-01-25 23:52:45 +0000131 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, manuf_id,
Uwe Hermann394131e2008-10-18 21:14:13 +0000132 model_id);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000133
Uwe Hermann394131e2008-10-18 21:14:13 +0000134 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000135 /* Print the status register to tell the
136 * user about possible write protection.
137 */
138 spi_prettyprint_status_register(flash);
139
140 return 1;
141 }
142
143 /* Test if this is a pure vendor match. */
144 if (manuf_id == flash->manufacture_id &&
145 GENERIC_DEVICE_ID == flash->model_id)
146 return 1;
147
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000148 return 0;
149}
150
Uwe Hermann394131e2008-10-18 21:14:13 +0000151int probe_spi_rdid(struct flashchip *flash)
152{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000153 return probe_spi_rdid_generic(flash, 3);
154}
155
156/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000157int probe_spi_rdid4(struct flashchip *flash)
158{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000159 /* only some SPI chipsets support 4 bytes commands */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000160 switch (flashbus) {
161 case BUS_TYPE_ICH7_SPI:
162 case BUS_TYPE_ICH9_SPI:
163 case BUS_TYPE_VIA_SPI:
Jason Wanga3f04be2008-11-28 21:36:51 +0000164 case BUS_TYPE_SB600_SPI:
Peter Stugebf196e92009-01-26 03:08:45 +0000165 case BUS_TYPE_WBSIO_SPI:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000166 return probe_spi_rdid_generic(flash, 4);
167 default:
168 printf_debug("4b ID not supported on this SPI controller\n");
169 }
170
171 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000172}
173
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000174int probe_spi_rems(struct flashchip *flash)
175{
176 unsigned char readarr[JEDEC_REMS_INSIZE];
177 uint32_t manuf_id, model_id;
178
179 if (spi_rems(readarr))
180 return 0;
181
182 manuf_id = readarr[0];
183 model_id = readarr[1];
184
185 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id,
186 model_id);
187
188 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
189 /* Print the status register to tell the
190 * user about possible write protection.
191 */
192 spi_prettyprint_status_register(flash);
193
194 return 1;
195 }
196
197 /* Test if this is a pure vendor match. */
198 if (manuf_id == flash->manufacture_id &&
199 GENERIC_DEVICE_ID == flash->model_id)
200 return 1;
201
202 return 0;
203}
204
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000205int probe_spi_res(struct flashchip *flash)
206{
207 unsigned char readarr[3];
208 uint32_t model_id;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000209
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000210 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
211 * In that case, RES is pointless.
212 */
213 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
214 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000215 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000216
Peter Stugeda4e5f32008-06-24 01:22:03 +0000217 if (spi_res(readarr))
218 return 0;
219
220 model_id = readarr[0];
221 printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id);
222 if (model_id != flash->model_id)
223 return 0;
224
225 /* Print the status register to tell the
226 * user about possible write protection.
227 */
228 spi_prettyprint_status_register(flash);
229 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000230}
231
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000232uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000233{
Uwe Hermann394131e2008-10-18 21:14:13 +0000234 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Peter Stugebf196e92009-01-26 03:08:45 +0000235 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000236
237 /* Read Status Register */
Jason Wanga3f04be2008-11-28 21:36:51 +0000238 if (flashbus == BUS_TYPE_SB600_SPI) {
239 /* SB600 uses a different way to read status register. */
240 return sb600_read_status_register();
241 } else {
242 spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
243 }
244
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000245 return readarr[0];
246}
247
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000248/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000249void spi_prettyprint_status_register_common(uint8_t status)
250{
251 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000252 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000253 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000254 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000255 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000256 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000257 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000258 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000259 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000260 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000261 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000262 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000263}
264
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000265/* Prettyprint the status register. Works for
266 * ST M25P series
267 * MX MX25L series
268 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000269void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000270{
271 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000272 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000273 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000274 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000275 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000276}
277
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000278void spi_prettyprint_status_register_sst25(uint8_t status)
279{
280 printf_debug("Chip status register: Block Protect Write Disable "
281 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
282 printf_debug("Chip status register: Auto Address Increment Programming "
283 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
284 spi_prettyprint_status_register_common(status);
285}
286
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000287/* Prettyprint the status register. Works for
288 * SST 25VF016
289 */
290void spi_prettyprint_status_register_sst25vf016(uint8_t status)
291{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000292 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000293 "none",
294 "1F0000H-1FFFFFH",
295 "1E0000H-1FFFFFH",
296 "1C0000H-1FFFFFH",
297 "180000H-1FFFFFH",
298 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000299 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000300 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000301 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000302 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000303 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000304}
305
Peter Stuge5fecee42009-01-26 03:23:50 +0000306void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
307{
308 const char *bpt[] = {
309 "none",
310 "0x70000-0x7ffff",
311 "0x60000-0x7ffff",
312 "0x40000-0x7ffff",
313 "all blocks", "all blocks", "all blocks", "all blocks"
314 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000315 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000316 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000317 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000318}
319
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000320void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000321{
322 uint8_t status;
323
Peter Stugefa8c5502008-05-10 23:07:52 +0000324 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000325 printf_debug("Chip status register is %02x\n", status);
326 switch (flash->manufacture_id) {
327 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000328 if (((flash->model_id & 0xff00) == 0x2000) ||
329 ((flash->model_id & 0xff00) == 0x2500))
330 spi_prettyprint_status_register_st_m25p(status);
331 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000332 case MX_ID:
333 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000334 spi_prettyprint_status_register_st_m25p(status);
335 break;
336 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000337 switch (flash->model_id) {
338 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000339 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000340 break;
341 case 0x8d:
342 case 0x258d:
343 spi_prettyprint_status_register_sst25vf040b(status);
344 break;
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000345 case 0x258e:
346 spi_prettyprint_status_register_sst25(status);
347 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000348 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000349 break;
350 }
351}
Uwe Hermann394131e2008-10-18 21:14:13 +0000352
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000353int spi_chip_erase_60(struct flashchip *flash)
354{
355 const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60};
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000356 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000357
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000358 result = spi_disable_blockprotect();
359 if (result) {
360 printf_debug("spi_disable_blockprotect failed\n");
361 return result;
362 }
363 result = spi_write_enable();
364 if (result) {
365 printf_debug("spi_write_enable failed\n");
366 return result;
367 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000368 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000369 result = spi_command(sizeof(cmd), 0, cmd, NULL);
370 if (result) {
371 printf_debug("spi_chip_erase_60 failed sending erase\n");
372 return result;
373 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000374 /* Wait until the Write-In-Progress bit is cleared.
375 * This usually takes 1-85 s, so wait in 1 s steps.
376 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000377 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000378 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
379 sleep(1);
380 return 0;
381}
382
Peter Stugefa8c5502008-05-10 23:07:52 +0000383int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000384{
Uwe Hermann394131e2008-10-18 21:14:13 +0000385 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 };
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000386 int result;
Uwe Hermann394131e2008-10-18 21:14:13 +0000387
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000388 result = spi_disable_blockprotect();
389 if (result) {
390 printf_debug("spi_disable_blockprotect failed\n");
391 return result;
392 }
393 result = spi_write_enable();
394 if (result) {
395 printf_debug("spi_write_enable failed\n");
396 return result;
397 }
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000398 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000399 result = spi_command(sizeof(cmd), 0, cmd, NULL);
400 if (result) {
401 printf_debug("spi_chip_erase_60 failed sending erase\n");
402 return result;
403 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000404 /* Wait until the Write-In-Progress bit is cleared.
405 * This usually takes 1-85 s, so wait in 1 s steps.
406 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000407 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000408 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000409 sleep(1);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000410 return 0;
411}
412
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000413int spi_chip_erase_60_c7(struct flashchip *flash)
414{
415 int result;
416 result = spi_chip_erase_60(flash);
417 if (result) {
418 printf_debug("spi_chip_erase_60 failed, trying c7\n");
419 result = spi_chip_erase_c7(flash);
420 }
421 return result;
422}
423
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000424int spi_block_erase_52(const struct flashchip *flash, unsigned long addr)
425{
426 unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52};
427
428 cmd[1] = (addr & 0x00ff0000) >> 16;
429 cmd[2] = (addr & 0x0000ff00) >> 8;
430 cmd[3] = (addr & 0x000000ff);
431 spi_write_enable();
432 /* Send BE (Block Erase) */
433 spi_command(sizeof(cmd), 0, cmd, NULL);
434 /* Wait until the Write-In-Progress bit is cleared.
435 * This usually takes 100-4000 ms, so wait in 100 ms steps.
436 */
437 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
438 usleep(100 * 1000);
439 return 0;
440}
441
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000442/* Block size is usually
443 * 64k for Macronix
444 * 32k for SST
445 * 4-32k non-uniform for EON
446 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000447int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000448{
Uwe Hermann394131e2008-10-18 21:14:13 +0000449 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8 };
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000450
451 cmd[1] = (addr & 0x00ff0000) >> 16;
452 cmd[2] = (addr & 0x0000ff00) >> 8;
453 cmd[3] = (addr & 0x000000ff);
Peter Stugefa8c5502008-05-10 23:07:52 +0000454 spi_write_enable();
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000455 /* Send BE (Block Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000456 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000457 /* Wait until the Write-In-Progress bit is cleared.
458 * This usually takes 100-4000 ms, so wait in 100 ms steps.
459 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000460 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000461 usleep(100 * 1000);
462 return 0;
463}
464
Stefan Reinauer424ed222008-10-29 22:13:20 +0000465int spi_chip_erase_d8(struct flashchip *flash)
466{
467 int i, rc = 0;
468 int total_size = flash->total_size * 1024;
469 int erase_size = 64 * 1024;
470
471 spi_disable_blockprotect();
472
473 printf("Erasing chip: \n");
474
475 for (i = 0; i < total_size / erase_size; i++) {
476 rc = spi_block_erase_d8(flash, i * erase_size);
477 if (rc) {
478 printf("Error erasing block at 0x%x\n", i);
479 break;
480 }
481 }
482
483 printf("\n");
484
485 return rc;
486}
487
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000488/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Peter Stugefa8c5502008-05-10 23:07:52 +0000489int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000490{
Uwe Hermann394131e2008-10-18 21:14:13 +0000491 unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE };
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000492 cmd[1] = (addr & 0x00ff0000) >> 16;
493 cmd[2] = (addr & 0x0000ff00) >> 8;
494 cmd[3] = (addr & 0x000000ff);
495
Peter Stugefa8c5502008-05-10 23:07:52 +0000496 spi_write_enable();
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000497 /* Send SE (Sector Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000498 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000499 /* Wait until the Write-In-Progress bit is cleared.
500 * This usually takes 15-800 ms, so wait in 10 ms steps.
501 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000502 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000503 usleep(10 * 1000);
504 return 0;
505}
506
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000507int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000508{
509 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
510
511 /* Send EWSR (Enable Write Status Register). */
512 return spi_command(JEDEC_EWSR_OUTSIZE, JEDEC_EWSR_INSIZE, cmd, NULL);
513}
514
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000515/*
516 * This is according the SST25VF016 datasheet, who knows it is more
517 * generic that this...
518 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000519int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000520{
Uwe Hermann394131e2008-10-18 21:14:13 +0000521 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
522 { JEDEC_WRSR, (unsigned char)status };
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000523
524 /* Send WRSR (Write Status Register) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000525 return spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000526}
527
528void spi_byte_program(int address, uint8_t byte)
529{
Uwe Hermann394131e2008-10-18 21:14:13 +0000530 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
531 JEDEC_BYTE_PROGRAM,
532 (address >> 16) & 0xff,
533 (address >> 8) & 0xff,
534 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000535 byte
536 };
537
538 /* Send Byte-Program */
Peter Stugef83221b2008-07-07 06:38:51 +0000539 spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000540}
541
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000542int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000543{
544 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000545 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000546
Peter Stugefa8c5502008-05-10 23:07:52 +0000547 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000548 /* If there is block protection in effect, unprotect it first. */
549 if ((status & 0x3c) != 0) {
550 printf_debug("Some block protection in effect, disabling\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000551 result = spi_write_status_enable();
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000552 if (result) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000553 printf_debug("spi_write_status_enable failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000554 return result;
555 }
556 result = spi_write_status_register(status & ~0x3c);
557 if (result) {
558 printf_debug("spi_write_status_register failed\n");
559 return result;
560 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000561 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000562 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000563}
564
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000565int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000566{
Uwe Hermann394131e2008-10-18 21:14:13 +0000567 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
568 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000569 (address >> 16) & 0xff,
570 (address >> 8) & 0xff,
571 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000572 };
573
574 /* Send Read */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000575 return spi_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000576}
577
Peter Stugefa8c5502008-05-10 23:07:52 +0000578int spi_chip_read(struct flashchip *flash, uint8_t *buf)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000579{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000580 switch (flashbus) {
581 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000582 return it8716f_spi_chip_read(flash, buf);
Jason Wanga3f04be2008-11-28 21:36:51 +0000583 case BUS_TYPE_SB600_SPI:
584 return sb600_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000585 case BUS_TYPE_ICH7_SPI:
586 case BUS_TYPE_ICH9_SPI:
587 case BUS_TYPE_VIA_SPI:
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000588 return ich_spi_read(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000589 case BUS_TYPE_WBSIO_SPI:
590 return wbsio_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000591 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000592 printf_debug
593 ("%s called, but no SPI chipset/strapping detected\n",
594 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000595 }
596
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000597 return 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000598}
599
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000600int spi_chip_write(struct flashchip *flash, uint8_t *buf)
601{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000602 switch (flashbus) {
603 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000604 return it8716f_spi_chip_write(flash, buf);
Jason Wanga3f04be2008-11-28 21:36:51 +0000605 case BUS_TYPE_SB600_SPI:
606 return sb600_spi_write(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000607 case BUS_TYPE_ICH7_SPI:
608 case BUS_TYPE_ICH9_SPI:
609 case BUS_TYPE_VIA_SPI:
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000610 return ich_spi_write(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000611 case BUS_TYPE_WBSIO_SPI:
612 return wbsio_spi_write(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000613 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000614 printf_debug
615 ("%s called, but no SPI chipset/strapping detected\n",
616 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000617 }
618
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000619 return 1;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000620}
Peter Stugefd9217d2009-01-26 03:37:40 +0000621
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000622int spi_aai_write(struct flashchip *flash, uint8_t *buf)
623{
Peter Stugefd9217d2009-01-26 03:37:40 +0000624 uint32_t pos = 2, size = flash->total_size * 1024;
625 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
626 switch (flashbus) {
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000627 case BUS_TYPE_WBSIO_SPI:
628 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
629 " degrading to byte program\n", __func__);
630 return spi_chip_write(flash, buf);
631 default:
632 break;
Peter Stugefd9217d2009-01-26 03:37:40 +0000633 }
634 flash->erase(flash);
635 spi_write_enable();
636 spi_command(6, 0, w, NULL);
637 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
638 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
639 while (pos < size) {
640 w[1] = buf[pos++];
641 w[2] = buf[pos++];
642 spi_command(3, 0, w, NULL);
643 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
644 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
645 }
646 spi_write_disable();
647 return 0;
648}