blob: 71cf00477b7316a7dead5f6355a6d6d4e3700602 [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);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000124
125 if (result)
126 printf_debug("%s failed", __func__);
127 if (result == SPI_INVALID_OPCODE) {
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000128 switch (flashbus) {
129 case BUS_TYPE_ICH7_SPI:
130 case BUS_TYPE_ICH9_SPI:
131 case BUS_TYPE_VIA_SPI:
132 printf_debug(" due to SPI master limitation, ignoring"
133 " and hoping it will be run as PREOP\n");
134 return 0;
135 default:
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000136 break;
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000137 }
138 }
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000139 if (result)
140 printf_debug("\n");
141
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000142 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000143}
144
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000145int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000146{
Uwe Hermann394131e2008-10-18 21:14:13 +0000147 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000148
149 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000150 return spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000151}
152
Rudolf Marek48a85e42008-06-30 21:45:17 +0000153static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000154{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000155 unsigned char readarr[4];
Carl-Daniel Hailfinger1263d2a2008-02-06 22:07:58 +0000156 uint32_t manuf_id;
157 uint32_t model_id;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000158
Rudolf Marek48a85e42008-06-30 21:45:17 +0000159 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000160 return 0;
161
162 if (!oddparity(readarr[0]))
163 printf_debug("RDID byte 0 parity violation.\n");
164
165 /* Check if this is a continuation vendor ID */
166 if (readarr[0] == 0x7f) {
167 if (!oddparity(readarr[1]))
168 printf_debug("RDID byte 1 parity violation.\n");
169 manuf_id = (readarr[0] << 8) | readarr[1];
170 model_id = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000171 if (bytes > 3) {
172 model_id <<= 8;
173 model_id |= readarr[3];
174 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000175 } else {
176 manuf_id = readarr[0];
177 model_id = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000178 }
179
Peter Stuge5cafc332009-01-25 23:52:45 +0000180 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, manuf_id,
Uwe Hermann394131e2008-10-18 21:14:13 +0000181 model_id);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000182
Uwe Hermann394131e2008-10-18 21:14:13 +0000183 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000184 /* Print the status register to tell the
185 * user about possible write protection.
186 */
187 spi_prettyprint_status_register(flash);
188
189 return 1;
190 }
191
192 /* Test if this is a pure vendor match. */
193 if (manuf_id == flash->manufacture_id &&
194 GENERIC_DEVICE_ID == flash->model_id)
195 return 1;
196
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000197 return 0;
198}
199
Uwe Hermann394131e2008-10-18 21:14:13 +0000200int probe_spi_rdid(struct flashchip *flash)
201{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000202 return probe_spi_rdid_generic(flash, 3);
203}
204
205/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000206int probe_spi_rdid4(struct flashchip *flash)
207{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000208 /* only some SPI chipsets support 4 bytes commands */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000209 switch (flashbus) {
210 case BUS_TYPE_ICH7_SPI:
211 case BUS_TYPE_ICH9_SPI:
212 case BUS_TYPE_VIA_SPI:
Jason Wanga3f04be2008-11-28 21:36:51 +0000213 case BUS_TYPE_SB600_SPI:
Peter Stugebf196e92009-01-26 03:08:45 +0000214 case BUS_TYPE_WBSIO_SPI:
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000215 case BUS_TYPE_DUMMY_SPI:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000216 return probe_spi_rdid_generic(flash, 4);
217 default:
218 printf_debug("4b ID not supported on this SPI controller\n");
219 }
220
221 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000222}
223
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000224int probe_spi_rems(struct flashchip *flash)
225{
226 unsigned char readarr[JEDEC_REMS_INSIZE];
227 uint32_t manuf_id, model_id;
228
229 if (spi_rems(readarr))
230 return 0;
231
232 manuf_id = readarr[0];
233 model_id = readarr[1];
234
235 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id,
236 model_id);
237
238 if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
239 /* Print the status register to tell the
240 * user about possible write protection.
241 */
242 spi_prettyprint_status_register(flash);
243
244 return 1;
245 }
246
247 /* Test if this is a pure vendor match. */
248 if (manuf_id == flash->manufacture_id &&
249 GENERIC_DEVICE_ID == flash->model_id)
250 return 1;
251
252 return 0;
253}
254
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000255int probe_spi_res(struct flashchip *flash)
256{
257 unsigned char readarr[3];
258 uint32_t model_id;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000259
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000260 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
261 * In that case, RES is pointless.
262 */
263 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
264 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000265 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000266
Peter Stugeda4e5f32008-06-24 01:22:03 +0000267 if (spi_res(readarr))
268 return 0;
269
270 model_id = readarr[0];
271 printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id);
272 if (model_id != flash->model_id)
273 return 0;
274
275 /* Print the status register to tell the
276 * user about possible write protection.
277 */
278 spi_prettyprint_status_register(flash);
279 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000280}
281
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000282uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000283{
Uwe Hermann394131e2008-10-18 21:14:13 +0000284 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Peter Stugebf196e92009-01-26 03:08:45 +0000285 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000286 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000287
288 /* Read Status Register */
Jason Wanga3f04be2008-11-28 21:36:51 +0000289 if (flashbus == BUS_TYPE_SB600_SPI) {
290 /* SB600 uses a different way to read status register. */
291 return sb600_read_status_register();
292 } else {
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000293 ret = spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
294 if (ret)
295 printf_debug("RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000296 }
297
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000298 return readarr[0];
299}
300
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000301/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000302void spi_prettyprint_status_register_common(uint8_t status)
303{
304 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000305 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000306 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000307 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000308 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000309 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000310 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000311 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000312 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000313 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000314 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000315 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000316}
317
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000318/* Prettyprint the status register. Works for
319 * ST M25P series
320 * MX MX25L series
321 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000322void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000323{
324 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000325 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000326 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000327 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000328 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000329}
330
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000331void spi_prettyprint_status_register_sst25(uint8_t status)
332{
333 printf_debug("Chip status register: Block Protect Write Disable "
334 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
335 printf_debug("Chip status register: Auto Address Increment Programming "
336 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
337 spi_prettyprint_status_register_common(status);
338}
339
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000340/* Prettyprint the status register. Works for
341 * SST 25VF016
342 */
343void spi_prettyprint_status_register_sst25vf016(uint8_t status)
344{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000345 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000346 "none",
347 "1F0000H-1FFFFFH",
348 "1E0000H-1FFFFFH",
349 "1C0000H-1FFFFFH",
350 "180000H-1FFFFFH",
351 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000352 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000353 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000354 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000355 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000356 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000357}
358
Peter Stuge5fecee42009-01-26 03:23:50 +0000359void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
360{
361 const char *bpt[] = {
362 "none",
363 "0x70000-0x7ffff",
364 "0x60000-0x7ffff",
365 "0x40000-0x7ffff",
366 "all blocks", "all blocks", "all blocks", "all blocks"
367 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000368 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000369 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000370 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000371}
372
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000373void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000374{
375 uint8_t status;
376
Peter Stugefa8c5502008-05-10 23:07:52 +0000377 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000378 printf_debug("Chip status register is %02x\n", status);
379 switch (flash->manufacture_id) {
380 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000381 if (((flash->model_id & 0xff00) == 0x2000) ||
382 ((flash->model_id & 0xff00) == 0x2500))
383 spi_prettyprint_status_register_st_m25p(status);
384 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000385 case MX_ID:
386 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000387 spi_prettyprint_status_register_st_m25p(status);
388 break;
389 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000390 switch (flash->model_id) {
391 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000392 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000393 break;
394 case 0x8d:
395 case 0x258d:
396 spi_prettyprint_status_register_sst25vf040b(status);
397 break;
Carl-Daniel Hailfinger5100a8a2009-05-13 22:51:27 +0000398 default:
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000399 spi_prettyprint_status_register_sst25(status);
400 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000401 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000402 break;
403 }
404}
Uwe Hermann394131e2008-10-18 21:14:13 +0000405
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000406int spi_chip_erase_60(struct flashchip *flash)
407{
408 const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60};
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000409 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000410
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000411 result = spi_disable_blockprotect();
412 if (result) {
413 printf_debug("spi_disable_blockprotect failed\n");
414 return result;
415 }
416 result = spi_write_enable();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000417 if (result)
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000418 return result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000419 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000420 result = spi_command(sizeof(cmd), 0, cmd, NULL);
421 if (result) {
422 printf_debug("spi_chip_erase_60 failed sending erase\n");
423 return result;
424 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000425 /* Wait until the Write-In-Progress bit is cleared.
426 * This usually takes 1-85 s, so wait in 1 s steps.
427 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000428 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000429 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
430 sleep(1);
431 return 0;
432}
433
Peter Stugefa8c5502008-05-10 23:07:52 +0000434int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000435{
Uwe Hermann394131e2008-10-18 21:14:13 +0000436 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 };
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000437 int result;
Uwe Hermann394131e2008-10-18 21:14:13 +0000438
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000439 result = spi_disable_blockprotect();
440 if (result) {
441 printf_debug("spi_disable_blockprotect failed\n");
442 return result;
443 }
444 result = spi_write_enable();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000445 if (result)
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000446 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000447 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000448 result = spi_command(sizeof(cmd), 0, cmd, NULL);
449 if (result) {
450 printf_debug("spi_chip_erase_60 failed sending erase\n");
451 return result;
452 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000453 /* Wait until the Write-In-Progress bit is cleared.
454 * This usually takes 1-85 s, so wait in 1 s steps.
455 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000456 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000457 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000458 sleep(1);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000459 return 0;
460}
461
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000462int spi_chip_erase_60_c7(struct flashchip *flash)
463{
464 int result;
465 result = spi_chip_erase_60(flash);
466 if (result) {
467 printf_debug("spi_chip_erase_60 failed, trying c7\n");
468 result = spi_chip_erase_c7(flash);
469 }
470 return result;
471}
472
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000473int spi_block_erase_52(const struct flashchip *flash, unsigned long addr)
474{
475 unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000476 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000477
478 cmd[1] = (addr & 0x00ff0000) >> 16;
479 cmd[2] = (addr & 0x0000ff00) >> 8;
480 cmd[3] = (addr & 0x000000ff);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000481 result = spi_write_enable();
482 if (result)
483 return result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000484 /* Send BE (Block Erase) */
485 spi_command(sizeof(cmd), 0, cmd, NULL);
486 /* Wait until the Write-In-Progress bit is cleared.
487 * This usually takes 100-4000 ms, so wait in 100 ms steps.
488 */
489 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
490 usleep(100 * 1000);
491 return 0;
492}
493
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000494/* Block size is usually
495 * 64k for Macronix
496 * 32k for SST
497 * 4-32k non-uniform for EON
498 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000499int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000500{
Uwe Hermann394131e2008-10-18 21:14:13 +0000501 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8 };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000502 int result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000503
504 cmd[1] = (addr & 0x00ff0000) >> 16;
505 cmd[2] = (addr & 0x0000ff00) >> 8;
506 cmd[3] = (addr & 0x000000ff);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000507 result = spi_write_enable();
508 if (result)
509 return result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000510 /* Send BE (Block Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000511 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000512 /* Wait until the Write-In-Progress bit is cleared.
513 * This usually takes 100-4000 ms, so wait in 100 ms steps.
514 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000515 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000516 usleep(100 * 1000);
517 return 0;
518}
519
Stefan Reinauer424ed222008-10-29 22:13:20 +0000520int spi_chip_erase_d8(struct flashchip *flash)
521{
522 int i, rc = 0;
523 int total_size = flash->total_size * 1024;
524 int erase_size = 64 * 1024;
525
526 spi_disable_blockprotect();
527
528 printf("Erasing chip: \n");
529
530 for (i = 0; i < total_size / erase_size; i++) {
531 rc = spi_block_erase_d8(flash, i * erase_size);
532 if (rc) {
533 printf("Error erasing block at 0x%x\n", i);
534 break;
535 }
536 }
537
538 printf("\n");
539
540 return rc;
541}
542
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000543/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Peter Stugefa8c5502008-05-10 23:07:52 +0000544int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000545{
Uwe Hermann394131e2008-10-18 21:14:13 +0000546 unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000547 int result;
548
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000549 cmd[1] = (addr & 0x00ff0000) >> 16;
550 cmd[2] = (addr & 0x0000ff00) >> 8;
551 cmd[3] = (addr & 0x000000ff);
552
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000553 result = spi_write_enable();
554 if (result)
555 return result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000556 /* Send SE (Sector Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000557 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000558 /* Wait until the Write-In-Progress bit is cleared.
559 * This usually takes 15-800 ms, so wait in 10 ms steps.
560 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000561 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000562 usleep(10 * 1000);
563 return 0;
564}
565
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000566int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000567{
568 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000569 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000570
571 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000572 result = spi_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
573
574 if (result)
575 printf_debug("%s failed", __func__);
576 if (result == SPI_INVALID_OPCODE) {
577 switch (flashbus) {
578 case BUS_TYPE_ICH7_SPI:
579 case BUS_TYPE_ICH9_SPI:
580 case BUS_TYPE_VIA_SPI:
581 printf_debug(" due to SPI master limitation, ignoring"
582 " and hoping it will be run as PREOP\n");
583 return 0;
584 default:
585 break;
586 }
587 }
588 if (result)
589 printf_debug("\n");
590
591 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000592}
593
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000594/*
595 * This is according the SST25VF016 datasheet, who knows it is more
596 * generic that this...
597 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000598int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000599{
Uwe Hermann394131e2008-10-18 21:14:13 +0000600 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
601 { JEDEC_WRSR, (unsigned char)status };
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000602
603 /* Send WRSR (Write Status Register) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000604 return spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000605}
606
607void spi_byte_program(int address, uint8_t byte)
608{
Uwe Hermann394131e2008-10-18 21:14:13 +0000609 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
610 JEDEC_BYTE_PROGRAM,
611 (address >> 16) & 0xff,
612 (address >> 8) & 0xff,
613 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000614 byte
615 };
616
617 /* Send Byte-Program */
Peter Stugef83221b2008-07-07 06:38:51 +0000618 spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000619}
620
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000621int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000622{
623 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000624 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000625
Peter Stugefa8c5502008-05-10 23:07:52 +0000626 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000627 /* If there is block protection in effect, unprotect it first. */
628 if ((status & 0x3c) != 0) {
629 printf_debug("Some block protection in effect, disabling\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000630 result = spi_write_status_enable();
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000631 if (result) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000632 printf_debug("spi_write_status_enable failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000633 return result;
634 }
635 result = spi_write_status_register(status & ~0x3c);
636 if (result) {
637 printf_debug("spi_write_status_register failed\n");
638 return result;
639 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000640 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000641 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000642}
643
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000644int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000645{
Uwe Hermann394131e2008-10-18 21:14:13 +0000646 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
647 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000648 (address >> 16) & 0xff,
649 (address >> 8) & 0xff,
650 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000651 };
652
653 /* Send Read */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000654 return spi_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000655}
656
Peter Stugefa8c5502008-05-10 23:07:52 +0000657int spi_chip_read(struct flashchip *flash, uint8_t *buf)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000658{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000659 switch (flashbus) {
660 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000661 return it8716f_spi_chip_read(flash, buf);
Jason Wanga3f04be2008-11-28 21:36:51 +0000662 case BUS_TYPE_SB600_SPI:
663 return sb600_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000664 case BUS_TYPE_ICH7_SPI:
665 case BUS_TYPE_ICH9_SPI:
666 case BUS_TYPE_VIA_SPI:
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000667 return ich_spi_read(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000668 case BUS_TYPE_WBSIO_SPI:
669 return wbsio_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000670 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000671 printf_debug
672 ("%s called, but no SPI chipset/strapping detected\n",
673 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000674 }
675
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000676 return 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000677}
678
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000679/*
680 * Program chip using byte programming. (SLOW!)
681 * This is for chips which can only handle one byte writes
682 * and for chips where memory mapped programming is impossible
683 * (e.g. due to size constraints in IT87* for over 512 kB)
684 */
685int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
686{
687 int total_size = 1024 * flash->total_size;
688 int i;
689
690 spi_disable_blockprotect();
691 for (i = 0; i < total_size; i++) {
692 spi_write_enable();
693 spi_byte_program(i, buf[i]);
694 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
695 myusec_delay(10);
696 }
697
698 return 0;
699}
700
701/*
702 * Program chip using page (256 bytes) programming.
703 * Some SPI masters can't do this, they use single byte programming instead.
704 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +0000705int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000706{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000707 switch (flashbus) {
708 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000709 return it8716f_spi_chip_write_256(flash, buf);
Jason Wanga3f04be2008-11-28 21:36:51 +0000710 case BUS_TYPE_SB600_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000711 return sb600_spi_write_1(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000712 case BUS_TYPE_ICH7_SPI:
713 case BUS_TYPE_ICH9_SPI:
714 case BUS_TYPE_VIA_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000715 return ich_spi_write_256(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000716 case BUS_TYPE_WBSIO_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000717 return wbsio_spi_write_1(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000718 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000719 printf_debug
720 ("%s called, but no SPI chipset/strapping detected\n",
721 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000722 }
723
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000724 return 1;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000725}
Peter Stugefd9217d2009-01-26 03:37:40 +0000726
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000727uint32_t spi_get_valid_read_addr(void)
728{
729 /* Need to return BBAR for ICH chipsets. */
730 return 0;
731}
732
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000733int spi_aai_write(struct flashchip *flash, uint8_t *buf)
734{
Peter Stugefd9217d2009-01-26 03:37:40 +0000735 uint32_t pos = 2, size = flash->total_size * 1024;
736 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000737 int result;
738
Peter Stugefd9217d2009-01-26 03:37:40 +0000739 switch (flashbus) {
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000740 case BUS_TYPE_WBSIO_SPI:
741 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
742 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000743 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000744 default:
745 break;
Peter Stugefd9217d2009-01-26 03:37:40 +0000746 }
747 flash->erase(flash);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000748 result = spi_write_enable();
749 if (result)
750 return result;
Peter Stugefd9217d2009-01-26 03:37:40 +0000751 spi_command(6, 0, w, NULL);
752 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
753 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
754 while (pos < size) {
755 w[1] = buf[pos++];
756 w[2] = buf[pos++];
757 spi_command(3, 0, w, NULL);
758 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
759 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
760 }
761 spi_write_disable();
762 return 0;
763}