blob: b61f4331694749472c37f677a85a74cf1138ff6c [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 Hailfinger3e9dbea2009-05-13 11:40:08 +000060 int ret;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000061
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000062 ret = spi_command(sizeof(cmd), bytes, cmd, readarr);
63 if (ret)
64 return ret;
Uwe Hermann394131e2008-10-18 21:14:13 +000065 printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1],
66 readarr[2]);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000067 return 0;
68}
69
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000070static int spi_rems(unsigned char *readarr)
71{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000072 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
73 uint32_t readaddr;
74 int ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000075
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000076 ret = spi_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
77 if (ret == SPI_INVALID_ADDRESS) {
78 /* Find the lowest even address allowed for reads. */
79 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
80 cmd[1] = (readaddr >> 16) & 0xff,
81 cmd[2] = (readaddr >> 8) & 0xff,
82 cmd[3] = (readaddr >> 0) & 0xff,
83 ret = spi_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
84 }
85 if (ret)
86 return ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000087 printf_debug("REMS returned %02x %02x.\n", readarr[0], readarr[1]);
88 return 0;
89}
90
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000091static int spi_res(unsigned char *readarr)
92{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000093 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
94 uint32_t readaddr;
95 int ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000096
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000097 ret = spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
98 if (ret == SPI_INVALID_ADDRESS) {
99 /* Find the lowest even address allowed for reads. */
100 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
101 cmd[1] = (readaddr >> 16) & 0xff,
102 cmd[2] = (readaddr >> 8) & 0xff,
103 cmd[3] = (readaddr >> 0) & 0xff,
104 ret = spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
105 }
106 if (ret)
107 return ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000108 printf_debug("RES returned %02x.\n", readarr[0]);
109 return 0;
110}
111
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000112int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000113{
Uwe Hermann394131e2008-10-18 21:14:13 +0000114 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000115 int result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000116
117 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000118 result = spi_command(sizeof(cmd), 0, cmd, NULL);
119 if (result) {
120 printf_debug("spi_write_enable failed");
121 switch (flashbus) {
122 case BUS_TYPE_ICH7_SPI:
123 case BUS_TYPE_ICH9_SPI:
124 case BUS_TYPE_VIA_SPI:
125 printf_debug(" due to SPI master limitation, ignoring"
126 " and hoping it will be run as PREOP\n");
127 return 0;
128 default:
129 printf_debug("\n");
130 }
131 }
132 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000133}
134
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000135int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000136{
Uwe Hermann394131e2008-10-18 21:14:13 +0000137 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000138
139 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000140 return spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000141}
142
Rudolf Marek48a85e42008-06-30 21:45:17 +0000143static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000144{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000145 unsigned char readarr[4];
Carl-Daniel Hailfinger1263d2a2008-02-06 22:07:58 +0000146 uint32_t manuf_id;
147 uint32_t model_id;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000148
Rudolf Marek48a85e42008-06-30 21:45:17 +0000149 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000150 return 0;
151
152 if (!oddparity(readarr[0]))
153 printf_debug("RDID byte 0 parity violation.\n");
154
155 /* Check if this is a continuation vendor ID */
156 if (readarr[0] == 0x7f) {
157 if (!oddparity(readarr[1]))
158 printf_debug("RDID byte 1 parity violation.\n");
159 manuf_id = (readarr[0] << 8) | readarr[1];
160 model_id = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000161 if (bytes > 3) {
162 model_id <<= 8;
163 model_id |= readarr[3];
164 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000165 } else {
166 manuf_id = readarr[0];
167 model_id = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000168 }
169
Peter Stuge5cafc332009-01-25 23:52:45 +0000170 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, manuf_id,
Uwe Hermann394131e2008-10-18 21:14:13 +0000171 model_id);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000172
Uwe Hermann394131e2008-10-18 21:14:13 +0000173 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000174 /* Print the status register to tell the
175 * user about possible write protection.
176 */
177 spi_prettyprint_status_register(flash);
178
179 return 1;
180 }
181
182 /* Test if this is a pure vendor match. */
183 if (manuf_id == flash->manufacture_id &&
184 GENERIC_DEVICE_ID == flash->model_id)
185 return 1;
186
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000187 return 0;
188}
189
Uwe Hermann394131e2008-10-18 21:14:13 +0000190int probe_spi_rdid(struct flashchip *flash)
191{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000192 return probe_spi_rdid_generic(flash, 3);
193}
194
195/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000196int probe_spi_rdid4(struct flashchip *flash)
197{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000198 /* only some SPI chipsets support 4 bytes commands */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000199 switch (flashbus) {
200 case BUS_TYPE_ICH7_SPI:
201 case BUS_TYPE_ICH9_SPI:
202 case BUS_TYPE_VIA_SPI:
Jason Wanga3f04be2008-11-28 21:36:51 +0000203 case BUS_TYPE_SB600_SPI:
Peter Stugebf196e92009-01-26 03:08:45 +0000204 case BUS_TYPE_WBSIO_SPI:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000205 return probe_spi_rdid_generic(flash, 4);
206 default:
207 printf_debug("4b ID not supported on this SPI controller\n");
208 }
209
210 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000211}
212
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000213int probe_spi_rems(struct flashchip *flash)
214{
215 unsigned char readarr[JEDEC_REMS_INSIZE];
216 uint32_t manuf_id, model_id;
217
218 if (spi_rems(readarr))
219 return 0;
220
221 manuf_id = readarr[0];
222 model_id = readarr[1];
223
224 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id,
225 model_id);
226
227 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
228 /* Print the status register to tell the
229 * user about possible write protection.
230 */
231 spi_prettyprint_status_register(flash);
232
233 return 1;
234 }
235
236 /* Test if this is a pure vendor match. */
237 if (manuf_id == flash->manufacture_id &&
238 GENERIC_DEVICE_ID == flash->model_id)
239 return 1;
240
241 return 0;
242}
243
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000244int probe_spi_res(struct flashchip *flash)
245{
246 unsigned char readarr[3];
247 uint32_t model_id;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000248
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000249 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
250 * In that case, RES is pointless.
251 */
252 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
253 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000254 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000255
Peter Stugeda4e5f32008-06-24 01:22:03 +0000256 if (spi_res(readarr))
257 return 0;
258
259 model_id = readarr[0];
260 printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id);
261 if (model_id != flash->model_id)
262 return 0;
263
264 /* Print the status register to tell the
265 * user about possible write protection.
266 */
267 spi_prettyprint_status_register(flash);
268 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000269}
270
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000271uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000272{
Uwe Hermann394131e2008-10-18 21:14:13 +0000273 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Peter Stugebf196e92009-01-26 03:08:45 +0000274 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000275 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000276
277 /* Read Status Register */
Jason Wanga3f04be2008-11-28 21:36:51 +0000278 if (flashbus == BUS_TYPE_SB600_SPI) {
279 /* SB600 uses a different way to read status register. */
280 return sb600_read_status_register();
281 } else {
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000282 ret = spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
283 if (ret)
284 printf_debug("RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000285 }
286
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000287 return readarr[0];
288}
289
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000290/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000291void spi_prettyprint_status_register_common(uint8_t status)
292{
293 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000294 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000295 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000296 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000297 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000298 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000299 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000300 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000301 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000302 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000303 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000304 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000305}
306
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000307/* Prettyprint the status register. Works for
308 * ST M25P series
309 * MX MX25L series
310 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000311void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000312{
313 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000314 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000315 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000316 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000317 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000318}
319
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000320void spi_prettyprint_status_register_sst25(uint8_t status)
321{
322 printf_debug("Chip status register: Block Protect Write Disable "
323 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
324 printf_debug("Chip status register: Auto Address Increment Programming "
325 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
326 spi_prettyprint_status_register_common(status);
327}
328
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000329/* Prettyprint the status register. Works for
330 * SST 25VF016
331 */
332void spi_prettyprint_status_register_sst25vf016(uint8_t status)
333{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000334 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000335 "none",
336 "1F0000H-1FFFFFH",
337 "1E0000H-1FFFFFH",
338 "1C0000H-1FFFFFH",
339 "180000H-1FFFFFH",
340 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000341 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000342 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000343 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000344 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000345 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000346}
347
Peter Stuge5fecee42009-01-26 03:23:50 +0000348void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
349{
350 const char *bpt[] = {
351 "none",
352 "0x70000-0x7ffff",
353 "0x60000-0x7ffff",
354 "0x40000-0x7ffff",
355 "all blocks", "all blocks", "all blocks", "all blocks"
356 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000357 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000358 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000359 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000360}
361
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000362void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000363{
364 uint8_t status;
365
Peter Stugefa8c5502008-05-10 23:07:52 +0000366 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000367 printf_debug("Chip status register is %02x\n", status);
368 switch (flash->manufacture_id) {
369 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000370 if (((flash->model_id & 0xff00) == 0x2000) ||
371 ((flash->model_id & 0xff00) == 0x2500))
372 spi_prettyprint_status_register_st_m25p(status);
373 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000374 case MX_ID:
375 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000376 spi_prettyprint_status_register_st_m25p(status);
377 break;
378 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000379 switch (flash->model_id) {
380 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000381 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000382 break;
383 case 0x8d:
384 case 0x258d:
385 spi_prettyprint_status_register_sst25vf040b(status);
386 break;
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000387 case 0x258e:
388 spi_prettyprint_status_register_sst25(status);
389 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000390 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000391 break;
392 }
393}
Uwe Hermann394131e2008-10-18 21:14:13 +0000394
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000395int spi_chip_erase_60(struct flashchip *flash)
396{
397 const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60};
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000398 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000399
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000400 result = spi_disable_blockprotect();
401 if (result) {
402 printf_debug("spi_disable_blockprotect failed\n");
403 return result;
404 }
405 result = spi_write_enable();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000406 if (result)
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000407 return result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000408 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000409 result = spi_command(sizeof(cmd), 0, cmd, NULL);
410 if (result) {
411 printf_debug("spi_chip_erase_60 failed sending erase\n");
412 return result;
413 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000414 /* Wait until the Write-In-Progress bit is cleared.
415 * This usually takes 1-85 s, so wait in 1 s steps.
416 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000417 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000418 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
419 sleep(1);
420 return 0;
421}
422
Peter Stugefa8c5502008-05-10 23:07:52 +0000423int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000424{
Uwe Hermann394131e2008-10-18 21:14:13 +0000425 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 };
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000426 int result;
Uwe Hermann394131e2008-10-18 21:14:13 +0000427
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000428 result = spi_disable_blockprotect();
429 if (result) {
430 printf_debug("spi_disable_blockprotect failed\n");
431 return result;
432 }
433 result = spi_write_enable();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000434 if (result)
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000435 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000436 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000437 result = spi_command(sizeof(cmd), 0, cmd, NULL);
438 if (result) {
439 printf_debug("spi_chip_erase_60 failed sending erase\n");
440 return result;
441 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000442 /* Wait until the Write-In-Progress bit is cleared.
443 * This usually takes 1-85 s, so wait in 1 s steps.
444 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000445 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000446 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000447 sleep(1);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000448 return 0;
449}
450
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000451int spi_chip_erase_60_c7(struct flashchip *flash)
452{
453 int result;
454 result = spi_chip_erase_60(flash);
455 if (result) {
456 printf_debug("spi_chip_erase_60 failed, trying c7\n");
457 result = spi_chip_erase_c7(flash);
458 }
459 return result;
460}
461
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000462int spi_block_erase_52(const struct flashchip *flash, unsigned long addr)
463{
464 unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000465 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000466
467 cmd[1] = (addr & 0x00ff0000) >> 16;
468 cmd[2] = (addr & 0x0000ff00) >> 8;
469 cmd[3] = (addr & 0x000000ff);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000470 result = spi_write_enable();
471 if (result)
472 return result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000473 /* Send BE (Block Erase) */
474 spi_command(sizeof(cmd), 0, cmd, NULL);
475 /* Wait until the Write-In-Progress bit is cleared.
476 * This usually takes 100-4000 ms, so wait in 100 ms steps.
477 */
478 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
479 usleep(100 * 1000);
480 return 0;
481}
482
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000483/* Block size is usually
484 * 64k for Macronix
485 * 32k for SST
486 * 4-32k non-uniform for EON
487 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000488int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000489{
Uwe Hermann394131e2008-10-18 21:14:13 +0000490 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8 };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000491 int result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000492
493 cmd[1] = (addr & 0x00ff0000) >> 16;
494 cmd[2] = (addr & 0x0000ff00) >> 8;
495 cmd[3] = (addr & 0x000000ff);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000496 result = spi_write_enable();
497 if (result)
498 return result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000499 /* Send BE (Block Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000500 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000501 /* Wait until the Write-In-Progress bit is cleared.
502 * This usually takes 100-4000 ms, so wait in 100 ms steps.
503 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000504 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000505 usleep(100 * 1000);
506 return 0;
507}
508
Stefan Reinauer424ed222008-10-29 22:13:20 +0000509int spi_chip_erase_d8(struct flashchip *flash)
510{
511 int i, rc = 0;
512 int total_size = flash->total_size * 1024;
513 int erase_size = 64 * 1024;
514
515 spi_disable_blockprotect();
516
517 printf("Erasing chip: \n");
518
519 for (i = 0; i < total_size / erase_size; i++) {
520 rc = spi_block_erase_d8(flash, i * erase_size);
521 if (rc) {
522 printf("Error erasing block at 0x%x\n", i);
523 break;
524 }
525 }
526
527 printf("\n");
528
529 return rc;
530}
531
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000532/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Peter Stugefa8c5502008-05-10 23:07:52 +0000533int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000534{
Uwe Hermann394131e2008-10-18 21:14:13 +0000535 unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000536 int result;
537
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000538 cmd[1] = (addr & 0x00ff0000) >> 16;
539 cmd[2] = (addr & 0x0000ff00) >> 8;
540 cmd[3] = (addr & 0x000000ff);
541
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000542 result = spi_write_enable();
543 if (result)
544 return result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000545 /* Send SE (Sector Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000546 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000547 /* Wait until the Write-In-Progress bit is cleared.
548 * This usually takes 15-800 ms, so wait in 10 ms steps.
549 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000550 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000551 usleep(10 * 1000);
552 return 0;
553}
554
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000555int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000556{
557 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
558
559 /* Send EWSR (Enable Write Status Register). */
560 return spi_command(JEDEC_EWSR_OUTSIZE, JEDEC_EWSR_INSIZE, cmd, NULL);
561}
562
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000563/*
564 * This is according the SST25VF016 datasheet, who knows it is more
565 * generic that this...
566 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000567int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000568{
Uwe Hermann394131e2008-10-18 21:14:13 +0000569 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
570 { JEDEC_WRSR, (unsigned char)status };
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000571
572 /* Send WRSR (Write Status Register) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000573 return spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000574}
575
576void spi_byte_program(int address, uint8_t byte)
577{
Uwe Hermann394131e2008-10-18 21:14:13 +0000578 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
579 JEDEC_BYTE_PROGRAM,
580 (address >> 16) & 0xff,
581 (address >> 8) & 0xff,
582 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000583 byte
584 };
585
586 /* Send Byte-Program */
Peter Stugef83221b2008-07-07 06:38:51 +0000587 spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000588}
589
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000590int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000591{
592 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000593 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000594
Peter Stugefa8c5502008-05-10 23:07:52 +0000595 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000596 /* If there is block protection in effect, unprotect it first. */
597 if ((status & 0x3c) != 0) {
598 printf_debug("Some block protection in effect, disabling\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000599 result = spi_write_status_enable();
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000600 if (result) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000601 printf_debug("spi_write_status_enable failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000602 return result;
603 }
604 result = spi_write_status_register(status & ~0x3c);
605 if (result) {
606 printf_debug("spi_write_status_register failed\n");
607 return result;
608 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000609 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000610 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000611}
612
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000613int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000614{
Uwe Hermann394131e2008-10-18 21:14:13 +0000615 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
616 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000617 (address >> 16) & 0xff,
618 (address >> 8) & 0xff,
619 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000620 };
621
622 /* Send Read */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000623 return spi_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000624}
625
Peter Stugefa8c5502008-05-10 23:07:52 +0000626int spi_chip_read(struct flashchip *flash, uint8_t *buf)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000627{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000628 switch (flashbus) {
629 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000630 return it8716f_spi_chip_read(flash, buf);
Jason Wanga3f04be2008-11-28 21:36:51 +0000631 case BUS_TYPE_SB600_SPI:
632 return sb600_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000633 case BUS_TYPE_ICH7_SPI:
634 case BUS_TYPE_ICH9_SPI:
635 case BUS_TYPE_VIA_SPI:
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000636 return ich_spi_read(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000637 case BUS_TYPE_WBSIO_SPI:
638 return wbsio_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000639 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000640 printf_debug
641 ("%s called, but no SPI chipset/strapping detected\n",
642 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000643 }
644
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000645 return 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000646}
647
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000648/*
649 * Program chip using byte programming. (SLOW!)
650 * This is for chips which can only handle one byte writes
651 * and for chips where memory mapped programming is impossible
652 * (e.g. due to size constraints in IT87* for over 512 kB)
653 */
654int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
655{
656 int total_size = 1024 * flash->total_size;
657 int i;
658
659 spi_disable_blockprotect();
660 for (i = 0; i < total_size; i++) {
661 spi_write_enable();
662 spi_byte_program(i, buf[i]);
663 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
664 myusec_delay(10);
665 }
666
667 return 0;
668}
669
670/*
671 * Program chip using page (256 bytes) programming.
672 * Some SPI masters can't do this, they use single byte programming instead.
673 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +0000674int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000675{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000676 switch (flashbus) {
677 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000678 return it8716f_spi_chip_write_256(flash, buf);
Jason Wanga3f04be2008-11-28 21:36:51 +0000679 case BUS_TYPE_SB600_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000680 return sb600_spi_write_1(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000681 case BUS_TYPE_ICH7_SPI:
682 case BUS_TYPE_ICH9_SPI:
683 case BUS_TYPE_VIA_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000684 return ich_spi_write_256(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000685 case BUS_TYPE_WBSIO_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000686 return wbsio_spi_write_1(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000687 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000688 printf_debug
689 ("%s called, but no SPI chipset/strapping detected\n",
690 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000691 }
692
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000693 return 1;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000694}
Peter Stugefd9217d2009-01-26 03:37:40 +0000695
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000696uint32_t spi_get_valid_read_addr(void)
697{
698 /* Need to return BBAR for ICH chipsets. */
699 return 0;
700}
701
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000702int spi_aai_write(struct flashchip *flash, uint8_t *buf)
703{
Peter Stugefd9217d2009-01-26 03:37:40 +0000704 uint32_t pos = 2, size = flash->total_size * 1024;
705 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000706 int result;
707
Peter Stugefd9217d2009-01-26 03:37:40 +0000708 switch (flashbus) {
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000709 case BUS_TYPE_WBSIO_SPI:
710 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
711 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000712 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000713 default:
714 break;
Peter Stugefd9217d2009-01-26 03:37:40 +0000715 }
716 flash->erase(flash);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000717 result = spi_write_enable();
718 if (result)
719 return result;
Peter Stugefd9217d2009-01-26 03:37:40 +0000720 spi_command(6, 0, w, NULL);
721 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
722 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
723 while (pos < size) {
724 w[1] = buf[pos++];
725 w[2] = buf[pos++];
726 spi_command(3, 0, w, NULL);
727 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
728 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
729 }
730 spi_write_disable();
731 return 0;
732}