blob: 2f2842c9bd9180e4b8be843b0e090647edbb58bd [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);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000049 case BUS_TYPE_DUMMY_SPI:
50 return dummy_spi_command(writecnt, readcnt, writearr, readarr);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000051 default:
Uwe Hermann394131e2008-10-18 21:14:13 +000052 printf_debug
53 ("%s called, but no SPI chipset/strapping detected\n",
54 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000055 }
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000056 return 1;
57}
58
Rudolf Marek48a85e42008-06-30 21:45:17 +000059static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000060{
Uwe Hermann394131e2008-10-18 21:14:13 +000061 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000062 int ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000063 int i;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000064
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000065 ret = spi_command(sizeof(cmd), bytes, cmd, readarr);
66 if (ret)
67 return ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000068 printf_debug("RDID returned");
69 for (i = 0; i < bytes; i++)
70 printf_debug(" 0x%02x", readarr[i]);
71 printf_debug("\n");
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000072 return 0;
73}
74
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000075static int spi_rems(unsigned char *readarr)
76{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000077 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
78 uint32_t readaddr;
79 int ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000080
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000081 ret = spi_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
82 if (ret == SPI_INVALID_ADDRESS) {
83 /* Find the lowest even address allowed for reads. */
84 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
85 cmd[1] = (readaddr >> 16) & 0xff,
86 cmd[2] = (readaddr >> 8) & 0xff,
87 cmd[3] = (readaddr >> 0) & 0xff,
88 ret = spi_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
89 }
90 if (ret)
91 return ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000092 printf_debug("REMS returned %02x %02x.\n", readarr[0], readarr[1]);
93 return 0;
94}
95
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000096static int spi_res(unsigned char *readarr)
97{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000098 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
99 uint32_t readaddr;
100 int ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000101
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000102 ret = spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
103 if (ret == SPI_INVALID_ADDRESS) {
104 /* Find the lowest even address allowed for reads. */
105 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
106 cmd[1] = (readaddr >> 16) & 0xff,
107 cmd[2] = (readaddr >> 8) & 0xff,
108 cmd[3] = (readaddr >> 0) & 0xff,
109 ret = spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
110 }
111 if (ret)
112 return ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000113 printf_debug("RES returned %02x.\n", readarr[0]);
114 return 0;
115}
116
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000117int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000118{
Uwe Hermann394131e2008-10-18 21:14:13 +0000119 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000120 int result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000121
122 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000123 result = spi_command(sizeof(cmd), 0, cmd, NULL);
124 if (result) {
125 printf_debug("spi_write_enable failed");
126 switch (flashbus) {
127 case BUS_TYPE_ICH7_SPI:
128 case BUS_TYPE_ICH9_SPI:
129 case BUS_TYPE_VIA_SPI:
130 printf_debug(" due to SPI master limitation, ignoring"
131 " and hoping it will be run as PREOP\n");
132 return 0;
133 default:
134 printf_debug("\n");
135 }
136 }
137 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000138}
139
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000140int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000141{
Uwe Hermann394131e2008-10-18 21:14:13 +0000142 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000143
144 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000145 return spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000146}
147
Rudolf Marek48a85e42008-06-30 21:45:17 +0000148static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000149{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000150 unsigned char readarr[4];
Carl-Daniel Hailfinger1263d2a2008-02-06 22:07:58 +0000151 uint32_t manuf_id;
152 uint32_t model_id;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000153
Rudolf Marek48a85e42008-06-30 21:45:17 +0000154 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000155 return 0;
156
157 if (!oddparity(readarr[0]))
158 printf_debug("RDID byte 0 parity violation.\n");
159
160 /* Check if this is a continuation vendor ID */
161 if (readarr[0] == 0x7f) {
162 if (!oddparity(readarr[1]))
163 printf_debug("RDID byte 1 parity violation.\n");
164 manuf_id = (readarr[0] << 8) | readarr[1];
165 model_id = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000166 if (bytes > 3) {
167 model_id <<= 8;
168 model_id |= readarr[3];
169 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000170 } else {
171 manuf_id = readarr[0];
172 model_id = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000173 }
174
Peter Stuge5cafc332009-01-25 23:52:45 +0000175 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, manuf_id,
Uwe Hermann394131e2008-10-18 21:14:13 +0000176 model_id);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000177
Uwe Hermann394131e2008-10-18 21:14:13 +0000178 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000179 /* Print the status register to tell the
180 * user about possible write protection.
181 */
182 spi_prettyprint_status_register(flash);
183
184 return 1;
185 }
186
187 /* Test if this is a pure vendor match. */
188 if (manuf_id == flash->manufacture_id &&
189 GENERIC_DEVICE_ID == flash->model_id)
190 return 1;
191
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000192 return 0;
193}
194
Uwe Hermann394131e2008-10-18 21:14:13 +0000195int probe_spi_rdid(struct flashchip *flash)
196{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000197 return probe_spi_rdid_generic(flash, 3);
198}
199
200/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000201int probe_spi_rdid4(struct flashchip *flash)
202{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000203 /* only some SPI chipsets support 4 bytes commands */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000204 switch (flashbus) {
205 case BUS_TYPE_ICH7_SPI:
206 case BUS_TYPE_ICH9_SPI:
207 case BUS_TYPE_VIA_SPI:
Jason Wanga3f04be2008-11-28 21:36:51 +0000208 case BUS_TYPE_SB600_SPI:
Peter Stugebf196e92009-01-26 03:08:45 +0000209 case BUS_TYPE_WBSIO_SPI:
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000210 case BUS_TYPE_DUMMY_SPI:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000211 return probe_spi_rdid_generic(flash, 4);
212 default:
213 printf_debug("4b ID not supported on this SPI controller\n");
214 }
215
216 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000217}
218
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000219int probe_spi_rems(struct flashchip *flash)
220{
221 unsigned char readarr[JEDEC_REMS_INSIZE];
222 uint32_t manuf_id, model_id;
223
224 if (spi_rems(readarr))
225 return 0;
226
227 manuf_id = readarr[0];
228 model_id = readarr[1];
229
230 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id,
231 model_id);
232
233 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
234 /* Print the status register to tell the
235 * user about possible write protection.
236 */
237 spi_prettyprint_status_register(flash);
238
239 return 1;
240 }
241
242 /* Test if this is a pure vendor match. */
243 if (manuf_id == flash->manufacture_id &&
244 GENERIC_DEVICE_ID == flash->model_id)
245 return 1;
246
247 return 0;
248}
249
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000250int probe_spi_res(struct flashchip *flash)
251{
252 unsigned char readarr[3];
253 uint32_t model_id;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000254
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000255 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
256 * In that case, RES is pointless.
257 */
258 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
259 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000260 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000261
Peter Stugeda4e5f32008-06-24 01:22:03 +0000262 if (spi_res(readarr))
263 return 0;
264
265 model_id = readarr[0];
266 printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id);
267 if (model_id != flash->model_id)
268 return 0;
269
270 /* Print the status register to tell the
271 * user about possible write protection.
272 */
273 spi_prettyprint_status_register(flash);
274 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000275}
276
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000277uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000278{
Uwe Hermann394131e2008-10-18 21:14:13 +0000279 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Peter Stugebf196e92009-01-26 03:08:45 +0000280 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000281 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000282
283 /* Read Status Register */
Jason Wanga3f04be2008-11-28 21:36:51 +0000284 if (flashbus == BUS_TYPE_SB600_SPI) {
285 /* SB600 uses a different way to read status register. */
286 return sb600_read_status_register();
287 } else {
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000288 ret = spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
289 if (ret)
290 printf_debug("RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000291 }
292
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000293 return readarr[0];
294}
295
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000296/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000297void spi_prettyprint_status_register_common(uint8_t status)
298{
299 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000300 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000301 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000302 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000303 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000304 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000305 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000306 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000307 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000308 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000309 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000310 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000311}
312
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000313/* Prettyprint the status register. Works for
314 * ST M25P series
315 * MX MX25L series
316 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000317void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000318{
319 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000320 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000321 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000322 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000323 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000324}
325
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000326void spi_prettyprint_status_register_sst25(uint8_t status)
327{
328 printf_debug("Chip status register: Block Protect Write Disable "
329 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
330 printf_debug("Chip status register: Auto Address Increment Programming "
331 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
332 spi_prettyprint_status_register_common(status);
333}
334
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000335/* Prettyprint the status register. Works for
336 * SST 25VF016
337 */
338void spi_prettyprint_status_register_sst25vf016(uint8_t status)
339{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000340 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000341 "none",
342 "1F0000H-1FFFFFH",
343 "1E0000H-1FFFFFH",
344 "1C0000H-1FFFFFH",
345 "180000H-1FFFFFH",
346 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000347 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000348 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000349 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000350 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000351 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000352}
353
Peter Stuge5fecee42009-01-26 03:23:50 +0000354void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
355{
356 const char *bpt[] = {
357 "none",
358 "0x70000-0x7ffff",
359 "0x60000-0x7ffff",
360 "0x40000-0x7ffff",
361 "all blocks", "all blocks", "all blocks", "all blocks"
362 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000363 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000364 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000365 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000366}
367
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000368void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000369{
370 uint8_t status;
371
Peter Stugefa8c5502008-05-10 23:07:52 +0000372 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000373 printf_debug("Chip status register is %02x\n", status);
374 switch (flash->manufacture_id) {
375 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000376 if (((flash->model_id & 0xff00) == 0x2000) ||
377 ((flash->model_id & 0xff00) == 0x2500))
378 spi_prettyprint_status_register_st_m25p(status);
379 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000380 case MX_ID:
381 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000382 spi_prettyprint_status_register_st_m25p(status);
383 break;
384 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000385 switch (flash->model_id) {
386 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000387 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000388 break;
389 case 0x8d:
390 case 0x258d:
391 spi_prettyprint_status_register_sst25vf040b(status);
392 break;
Carl-Daniel Hailfinger5100a8a2009-05-13 22:51:27 +0000393 default:
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000394 spi_prettyprint_status_register_sst25(status);
395 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000396 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000397 break;
398 }
399}
Uwe Hermann394131e2008-10-18 21:14:13 +0000400
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000401int spi_chip_erase_60(struct flashchip *flash)
402{
403 const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60};
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000404 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000405
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000406 result = spi_disable_blockprotect();
407 if (result) {
408 printf_debug("spi_disable_blockprotect failed\n");
409 return result;
410 }
411 result = spi_write_enable();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000412 if (result)
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000413 return result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000414 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000415 result = spi_command(sizeof(cmd), 0, cmd, NULL);
416 if (result) {
417 printf_debug("spi_chip_erase_60 failed sending erase\n");
418 return result;
419 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000420 /* Wait until the Write-In-Progress bit is cleared.
421 * This usually takes 1-85 s, so wait in 1 s steps.
422 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000423 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000424 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
425 sleep(1);
426 return 0;
427}
428
Peter Stugefa8c5502008-05-10 23:07:52 +0000429int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000430{
Uwe Hermann394131e2008-10-18 21:14:13 +0000431 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 };
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000432 int result;
Uwe Hermann394131e2008-10-18 21:14:13 +0000433
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000434 result = spi_disable_blockprotect();
435 if (result) {
436 printf_debug("spi_disable_blockprotect failed\n");
437 return result;
438 }
439 result = spi_write_enable();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000440 if (result)
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000441 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000442 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000443 result = spi_command(sizeof(cmd), 0, cmd, NULL);
444 if (result) {
445 printf_debug("spi_chip_erase_60 failed sending erase\n");
446 return result;
447 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000448 /* Wait until the Write-In-Progress bit is cleared.
449 * This usually takes 1-85 s, so wait in 1 s steps.
450 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000451 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000452 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000453 sleep(1);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000454 return 0;
455}
456
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000457int spi_chip_erase_60_c7(struct flashchip *flash)
458{
459 int result;
460 result = spi_chip_erase_60(flash);
461 if (result) {
462 printf_debug("spi_chip_erase_60 failed, trying c7\n");
463 result = spi_chip_erase_c7(flash);
464 }
465 return result;
466}
467
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000468int spi_block_erase_52(const struct flashchip *flash, unsigned long addr)
469{
470 unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000471 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000472
473 cmd[1] = (addr & 0x00ff0000) >> 16;
474 cmd[2] = (addr & 0x0000ff00) >> 8;
475 cmd[3] = (addr & 0x000000ff);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000476 result = spi_write_enable();
477 if (result)
478 return result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000479 /* Send BE (Block Erase) */
480 spi_command(sizeof(cmd), 0, cmd, NULL);
481 /* Wait until the Write-In-Progress bit is cleared.
482 * This usually takes 100-4000 ms, so wait in 100 ms steps.
483 */
484 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
485 usleep(100 * 1000);
486 return 0;
487}
488
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000489/* Block size is usually
490 * 64k for Macronix
491 * 32k for SST
492 * 4-32k non-uniform for EON
493 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000494int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000495{
Uwe Hermann394131e2008-10-18 21:14:13 +0000496 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8 };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000497 int result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000498
499 cmd[1] = (addr & 0x00ff0000) >> 16;
500 cmd[2] = (addr & 0x0000ff00) >> 8;
501 cmd[3] = (addr & 0x000000ff);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000502 result = spi_write_enable();
503 if (result)
504 return result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000505 /* Send BE (Block Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000506 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000507 /* Wait until the Write-In-Progress bit is cleared.
508 * This usually takes 100-4000 ms, so wait in 100 ms steps.
509 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000510 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000511 usleep(100 * 1000);
512 return 0;
513}
514
Stefan Reinauer424ed222008-10-29 22:13:20 +0000515int spi_chip_erase_d8(struct flashchip *flash)
516{
517 int i, rc = 0;
518 int total_size = flash->total_size * 1024;
519 int erase_size = 64 * 1024;
520
521 spi_disable_blockprotect();
522
523 printf("Erasing chip: \n");
524
525 for (i = 0; i < total_size / erase_size; i++) {
526 rc = spi_block_erase_d8(flash, i * erase_size);
527 if (rc) {
528 printf("Error erasing block at 0x%x\n", i);
529 break;
530 }
531 }
532
533 printf("\n");
534
535 return rc;
536}
537
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000538/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Peter Stugefa8c5502008-05-10 23:07:52 +0000539int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000540{
Uwe Hermann394131e2008-10-18 21:14:13 +0000541 unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000542 int result;
543
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000544 cmd[1] = (addr & 0x00ff0000) >> 16;
545 cmd[2] = (addr & 0x0000ff00) >> 8;
546 cmd[3] = (addr & 0x000000ff);
547
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000548 result = spi_write_enable();
549 if (result)
550 return result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000551 /* Send SE (Sector Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000552 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000553 /* Wait until the Write-In-Progress bit is cleared.
554 * This usually takes 15-800 ms, so wait in 10 ms steps.
555 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000556 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000557 usleep(10 * 1000);
558 return 0;
559}
560
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000561int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000562{
563 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
564
565 /* Send EWSR (Enable Write Status Register). */
566 return spi_command(JEDEC_EWSR_OUTSIZE, JEDEC_EWSR_INSIZE, cmd, NULL);
567}
568
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000569/*
570 * This is according the SST25VF016 datasheet, who knows it is more
571 * generic that this...
572 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000573int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000574{
Uwe Hermann394131e2008-10-18 21:14:13 +0000575 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
576 { JEDEC_WRSR, (unsigned char)status };
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000577
578 /* Send WRSR (Write Status Register) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000579 return spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000580}
581
582void spi_byte_program(int address, uint8_t byte)
583{
Uwe Hermann394131e2008-10-18 21:14:13 +0000584 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
585 JEDEC_BYTE_PROGRAM,
586 (address >> 16) & 0xff,
587 (address >> 8) & 0xff,
588 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000589 byte
590 };
591
592 /* Send Byte-Program */
Peter Stugef83221b2008-07-07 06:38:51 +0000593 spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000594}
595
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000596int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000597{
598 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000599 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000600
Peter Stugefa8c5502008-05-10 23:07:52 +0000601 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000602 /* If there is block protection in effect, unprotect it first. */
603 if ((status & 0x3c) != 0) {
604 printf_debug("Some block protection in effect, disabling\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000605 result = spi_write_status_enable();
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000606 if (result) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000607 printf_debug("spi_write_status_enable failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000608 return result;
609 }
610 result = spi_write_status_register(status & ~0x3c);
611 if (result) {
612 printf_debug("spi_write_status_register failed\n");
613 return result;
614 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000615 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000616 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000617}
618
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000619int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000620{
Uwe Hermann394131e2008-10-18 21:14:13 +0000621 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
622 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000623 (address >> 16) & 0xff,
624 (address >> 8) & 0xff,
625 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000626 };
627
628 /* Send Read */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000629 return spi_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000630}
631
Peter Stugefa8c5502008-05-10 23:07:52 +0000632int spi_chip_read(struct flashchip *flash, uint8_t *buf)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000633{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000634 switch (flashbus) {
635 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000636 return it8716f_spi_chip_read(flash, buf);
Jason Wanga3f04be2008-11-28 21:36:51 +0000637 case BUS_TYPE_SB600_SPI:
638 return sb600_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000639 case BUS_TYPE_ICH7_SPI:
640 case BUS_TYPE_ICH9_SPI:
641 case BUS_TYPE_VIA_SPI:
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000642 return ich_spi_read(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000643 case BUS_TYPE_WBSIO_SPI:
644 return wbsio_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000645 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000646 printf_debug
647 ("%s called, but no SPI chipset/strapping detected\n",
648 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000649 }
650
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000651 return 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000652}
653
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000654/*
655 * Program chip using byte programming. (SLOW!)
656 * This is for chips which can only handle one byte writes
657 * and for chips where memory mapped programming is impossible
658 * (e.g. due to size constraints in IT87* for over 512 kB)
659 */
660int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
661{
662 int total_size = 1024 * flash->total_size;
663 int i;
664
665 spi_disable_blockprotect();
666 for (i = 0; i < total_size; i++) {
667 spi_write_enable();
668 spi_byte_program(i, buf[i]);
669 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
670 myusec_delay(10);
671 }
672
673 return 0;
674}
675
676/*
677 * Program chip using page (256 bytes) programming.
678 * Some SPI masters can't do this, they use single byte programming instead.
679 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +0000680int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000681{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000682 switch (flashbus) {
683 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000684 return it8716f_spi_chip_write_256(flash, buf);
Jason Wanga3f04be2008-11-28 21:36:51 +0000685 case BUS_TYPE_SB600_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000686 return sb600_spi_write_1(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000687 case BUS_TYPE_ICH7_SPI:
688 case BUS_TYPE_ICH9_SPI:
689 case BUS_TYPE_VIA_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000690 return ich_spi_write_256(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000691 case BUS_TYPE_WBSIO_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000692 return wbsio_spi_write_1(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000693 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000694 printf_debug
695 ("%s called, but no SPI chipset/strapping detected\n",
696 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000697 }
698
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000699 return 1;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000700}
Peter Stugefd9217d2009-01-26 03:37:40 +0000701
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000702uint32_t spi_get_valid_read_addr(void)
703{
704 /* Need to return BBAR for ICH chipsets. */
705 return 0;
706}
707
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000708int spi_aai_write(struct flashchip *flash, uint8_t *buf)
709{
Peter Stugefd9217d2009-01-26 03:37:40 +0000710 uint32_t pos = 2, size = flash->total_size * 1024;
711 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000712 int result;
713
Peter Stugefd9217d2009-01-26 03:37:40 +0000714 switch (flashbus) {
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000715 case BUS_TYPE_WBSIO_SPI:
716 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
717 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000718 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000719 default:
720 break;
Peter Stugefd9217d2009-01-26 03:37:40 +0000721 }
722 flash->erase(flash);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000723 result = spi_write_enable();
724 if (result)
725 return result;
Peter Stugefd9217d2009-01-26 03:37:40 +0000726 spi_command(6, 0, w, NULL);
727 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
728 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
729 while (pos < size) {
730 w[1] = buf[pos++];
731 w[2] = buf[pos++];
732 spi_command(3, 0, w, NULL);
733 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
734 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
735 }
736 spi_write_disable();
737 return 0;
738}