blob: a342923883e71fedc83e206e70a8ecc3183d11d4 [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
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000025#include <string.h>
26#include "flash.h"
Carl-Daniel Hailfingerd6cbf762008-05-13 14:58:23 +000027#include "spi.h"
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000028
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000029enum spi_controller spi_controller = SPI_CONTROLLER_NONE;
30void *spibar = NULL;
31
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{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000037 switch (spi_controller) {
38 case SPI_CONTROLLER_IT87XX:
Uwe Hermann394131e2008-10-18 21:14:13 +000039 return it8716f_spi_command(writecnt, readcnt, writearr,
40 readarr);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000041 case SPI_CONTROLLER_ICH7:
42 case SPI_CONTROLLER_ICH9:
43 case SPI_CONTROLLER_VIA:
Uwe Hermann394131e2008-10-18 21:14:13 +000044 return ich_spi_command(writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000045 case SPI_CONTROLLER_SB600:
Jason Wanga3f04be2008-11-28 21:36:51 +000046 return sb600_spi_command(writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000047 case SPI_CONTROLLER_WBSIO:
Peter Stugebf196e92009-01-26 03:08:45 +000048 return wbsio_spi_command(writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000049 case SPI_CONTROLLER_DUMMY:
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000050 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 Hailfinger1dfe0ff2009-05-31 17:57:34 +0000128 switch (spi_controller) {
129 case SPI_CONTROLLER_ICH7:
130 case SPI_CONTROLLER_ICH9:
131 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000132 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 Hailfinger2ad267d2009-05-27 11:40:08 +0000156 uint32_t id1;
157 uint32_t id2;
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");
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000169 id1 = (readarr[0] << 8) | readarr[1];
170 id2 = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000171 if (bytes > 3) {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000172 id2 <<= 8;
173 id2 |= readarr[3];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000174 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000175 } else {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000176 id1 = readarr[0];
177 id2 = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000178 }
179
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000180 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000181
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000182 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000183 /* Print the status register to tell the
184 * user about possible write protection.
185 */
186 spi_prettyprint_status_register(flash);
187
188 return 1;
189 }
190
191 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000192 if (id1 == flash->manufacture_id &&
Peter Stugeda4e5f32008-06-24 01:22:03 +0000193 GENERIC_DEVICE_ID == flash->model_id)
194 return 1;
195
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000196 return 0;
197}
198
Uwe Hermann394131e2008-10-18 21:14:13 +0000199int probe_spi_rdid(struct flashchip *flash)
200{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000201 return probe_spi_rdid_generic(flash, 3);
202}
203
204/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000205int probe_spi_rdid4(struct flashchip *flash)
206{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000207 /* only some SPI chipsets support 4 bytes commands */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000208 switch (spi_controller) {
209 case SPI_CONTROLLER_ICH7:
210 case SPI_CONTROLLER_ICH9:
211 case SPI_CONTROLLER_VIA:
212 case SPI_CONTROLLER_SB600:
213 case SPI_CONTROLLER_WBSIO:
214 case SPI_CONTROLLER_DUMMY:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000215 return probe_spi_rdid_generic(flash, 4);
216 default:
217 printf_debug("4b ID not supported on this SPI controller\n");
218 }
219
220 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000221}
222
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000223int probe_spi_rems(struct flashchip *flash)
224{
225 unsigned char readarr[JEDEC_REMS_INSIZE];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000226 uint32_t id1, id2;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000227
228 if (spi_rems(readarr))
229 return 0;
230
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000231 id1 = readarr[0];
232 id2 = readarr[1];
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000233
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000234 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000235
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000236 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000237 /* Print the status register to tell the
238 * user about possible write protection.
239 */
240 spi_prettyprint_status_register(flash);
241
242 return 1;
243 }
244
245 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000246 if (id1 == flash->manufacture_id &&
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000247 GENERIC_DEVICE_ID == flash->model_id)
248 return 1;
249
250 return 0;
251}
252
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000253int probe_spi_res(struct flashchip *flash)
254{
255 unsigned char readarr[3];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000256 uint32_t id2;
Peter Stugeda4e5f32008-06-24 01:22:03 +0000257
Carl-Daniel Hailfinger92a54ca2008-11-27 22:48:48 +0000258 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
259 * In that case, RES is pointless.
260 */
261 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
262 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000263 return 0;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000264
Peter Stugeda4e5f32008-06-24 01:22:03 +0000265 if (spi_res(readarr))
266 return 0;
267
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000268 id2 = readarr[0];
269 printf_debug("%s: id 0x%x\n", __FUNCTION__, id2);
270 if (id2 != flash->model_id)
Peter Stugeda4e5f32008-06-24 01:22:03 +0000271 return 0;
272
273 /* Print the status register to tell the
274 * user about possible write protection.
275 */
276 spi_prettyprint_status_register(flash);
277 return 1;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000278}
279
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000280uint8_t spi_read_status_register(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000281{
Uwe Hermann394131e2008-10-18 21:14:13 +0000282 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Peter Stugebf196e92009-01-26 03:08:45 +0000283 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000284 int ret;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000285
286 /* Read Status Register */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000287 if (spi_controller == SPI_CONTROLLER_SB600) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000288 /* SB600 uses a different way to read status register. */
289 return sb600_read_status_register();
290 } else {
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000291 ret = spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
292 if (ret)
293 printf_debug("RDSR failed!\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000294 }
295
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000296 return readarr[0];
297}
298
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000299/* Prettyprint the status register. Common definitions. */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000300void spi_prettyprint_status_register_common(uint8_t status)
301{
302 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000303 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000304 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000305 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000306 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000307 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000308 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000309 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000310 printf_debug("Chip status register: Write Enable Latch (WEL) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000311 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000312 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000313 "%sset\n", (status & (1 << 0)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000314}
315
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000316/* Prettyprint the status register. Works for
317 * ST M25P series
318 * MX MX25L series
319 */
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000320void spi_prettyprint_status_register_st_m25p(uint8_t status)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000321{
322 printf_debug("Chip status register: Status Register Write Disable "
Uwe Hermann394131e2008-10-18 21:14:13 +0000323 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000324 printf_debug("Chip status register: Bit 6 is "
Uwe Hermann394131e2008-10-18 21:14:13 +0000325 "%sset\n", (status & (1 << 6)) ? "" : "not ");
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000326 spi_prettyprint_status_register_common(status);
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000327}
328
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000329void spi_prettyprint_status_register_sst25(uint8_t status)
330{
331 printf_debug("Chip status register: Block Protect Write Disable "
332 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
333 printf_debug("Chip status register: Auto Address Increment Programming "
334 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
335 spi_prettyprint_status_register_common(status);
336}
337
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000338/* Prettyprint the status register. Works for
339 * SST 25VF016
340 */
341void spi_prettyprint_status_register_sst25vf016(uint8_t status)
342{
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000343 const char *bpt[] = {
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000344 "none",
345 "1F0000H-1FFFFFH",
346 "1E0000H-1FFFFFH",
347 "1C0000H-1FFFFFH",
348 "180000H-1FFFFFH",
349 "100000H-1FFFFFH",
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000350 "all", "all"
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000351 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000352 spi_prettyprint_status_register_sst25(status);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000353 printf_debug("Resulting block protection : %s\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000354 bpt[(status & 0x1c) >> 2]);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000355}
356
Peter Stuge5fecee42009-01-26 03:23:50 +0000357void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
358{
359 const char *bpt[] = {
360 "none",
361 "0x70000-0x7ffff",
362 "0x60000-0x7ffff",
363 "0x40000-0x7ffff",
364 "all blocks", "all blocks", "all blocks", "all blocks"
365 };
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000366 spi_prettyprint_status_register_sst25(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000367 printf_debug("Resulting block protection : %s\n",
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000368 bpt[(status & 0x1c) >> 2]);
Peter Stuge5fecee42009-01-26 03:23:50 +0000369}
370
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000371void spi_prettyprint_status_register(struct flashchip *flash)
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000372{
373 uint8_t status;
374
Peter Stugefa8c5502008-05-10 23:07:52 +0000375 status = spi_read_status_register();
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000376 printf_debug("Chip status register is %02x\n", status);
377 switch (flash->manufacture_id) {
378 case ST_ID:
Carl-Daniel Hailfingerf43e6422008-05-15 22:32:08 +0000379 if (((flash->model_id & 0xff00) == 0x2000) ||
380 ((flash->model_id & 0xff00) == 0x2500))
381 spi_prettyprint_status_register_st_m25p(status);
382 break;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000383 case MX_ID:
384 if ((flash->model_id & 0xff00) == 0x2000)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000385 spi_prettyprint_status_register_st_m25p(status);
386 break;
387 case SST_ID:
Peter Stuge5fecee42009-01-26 03:23:50 +0000388 switch (flash->model_id) {
389 case 0x2541:
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000390 spi_prettyprint_status_register_sst25vf016(status);
Peter Stuge5fecee42009-01-26 03:23:50 +0000391 break;
392 case 0x8d:
393 case 0x258d:
394 spi_prettyprint_status_register_sst25vf040b(status);
395 break;
Carl-Daniel Hailfinger5100a8a2009-05-13 22:51:27 +0000396 default:
Carl-Daniel Hailfinger1bfd6c92009-05-06 13:59:44 +0000397 spi_prettyprint_status_register_sst25(status);
398 break;
Peter Stuge5fecee42009-01-26 03:23:50 +0000399 }
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000400 break;
401 }
402}
Uwe Hermann394131e2008-10-18 21:14:13 +0000403
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000404int spi_chip_erase_60(struct flashchip *flash)
405{
406 const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60};
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000407 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000408
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000409 result = spi_disable_blockprotect();
410 if (result) {
411 printf_debug("spi_disable_blockprotect failed\n");
412 return result;
413 }
414 result = spi_write_enable();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000415 if (result)
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000416 return result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000417 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000418 result = spi_command(sizeof(cmd), 0, cmd, NULL);
419 if (result) {
420 printf_debug("spi_chip_erase_60 failed sending erase\n");
421 return result;
422 }
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000423 /* Wait until the Write-In-Progress bit is cleared.
424 * This usually takes 1-85 s, so wait in 1 s steps.
425 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000426 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000427 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000428 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000429 return 0;
430}
431
Peter Stugefa8c5502008-05-10 23:07:52 +0000432int spi_chip_erase_c7(struct flashchip *flash)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000433{
Uwe Hermann394131e2008-10-18 21:14:13 +0000434 const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 };
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000435 int result;
Uwe Hermann394131e2008-10-18 21:14:13 +0000436
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000437 result = spi_disable_blockprotect();
438 if (result) {
439 printf_debug("spi_disable_blockprotect failed\n");
440 return result;
441 }
442 result = spi_write_enable();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000443 if (result)
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000444 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000445 /* Send CE (Chip Erase) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000446 result = spi_command(sizeof(cmd), 0, cmd, NULL);
447 if (result) {
448 printf_debug("spi_chip_erase_60 failed sending erase\n");
449 return result;
450 }
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000451 /* Wait until the Write-In-Progress bit is cleared.
452 * This usually takes 1-85 s, so wait in 1 s steps.
453 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000454 /* FIXME: We assume spi_read_status_register will never fail. */
Peter Stugefa8c5502008-05-10 23:07:52 +0000455 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000456 programmer_delay(1000 * 1000);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000457 return 0;
458}
459
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000460int spi_chip_erase_60_c7(struct flashchip *flash)
461{
462 int result;
463 result = spi_chip_erase_60(flash);
464 if (result) {
465 printf_debug("spi_chip_erase_60 failed, trying c7\n");
466 result = spi_chip_erase_c7(flash);
467 }
468 return result;
469}
470
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000471int spi_block_erase_52(const struct flashchip *flash, unsigned long addr)
472{
473 unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000474 int result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000475
476 cmd[1] = (addr & 0x00ff0000) >> 16;
477 cmd[2] = (addr & 0x0000ff00) >> 8;
478 cmd[3] = (addr & 0x000000ff);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000479 result = spi_write_enable();
480 if (result)
481 return result;
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000482 /* Send BE (Block Erase) */
483 spi_command(sizeof(cmd), 0, cmd, NULL);
484 /* Wait until the Write-In-Progress bit is cleared.
485 * This usually takes 100-4000 ms, so wait in 100 ms steps.
486 */
487 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000488 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000489 return 0;
490}
491
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000492/* Block size is usually
493 * 64k for Macronix
494 * 32k for SST
495 * 4-32k non-uniform for EON
496 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000497int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000498{
Uwe Hermann394131e2008-10-18 21:14:13 +0000499 unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8 };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000500 int result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000501
502 cmd[1] = (addr & 0x00ff0000) >> 16;
503 cmd[2] = (addr & 0x0000ff00) >> 8;
504 cmd[3] = (addr & 0x000000ff);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000505 result = spi_write_enable();
506 if (result)
507 return result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000508 /* Send BE (Block Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000509 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000510 /* Wait until the Write-In-Progress bit is cleared.
511 * This usually takes 100-4000 ms, so wait in 100 ms steps.
512 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000513 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000514 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000515 return 0;
516}
517
Stefan Reinauer424ed222008-10-29 22:13:20 +0000518int spi_chip_erase_d8(struct flashchip *flash)
519{
520 int i, rc = 0;
521 int total_size = flash->total_size * 1024;
522 int erase_size = 64 * 1024;
523
524 spi_disable_blockprotect();
525
526 printf("Erasing chip: \n");
527
528 for (i = 0; i < total_size / erase_size; i++) {
529 rc = spi_block_erase_d8(flash, i * erase_size);
530 if (rc) {
531 printf("Error erasing block at 0x%x\n", i);
532 break;
533 }
534 }
535
536 printf("\n");
537
538 return rc;
539}
540
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000541/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Peter Stugefa8c5502008-05-10 23:07:52 +0000542int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000543{
Uwe Hermann394131e2008-10-18 21:14:13 +0000544 unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000545 int result;
546
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000547 cmd[1] = (addr & 0x00ff0000) >> 16;
548 cmd[2] = (addr & 0x0000ff00) >> 8;
549 cmd[3] = (addr & 0x000000ff);
550
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000551 result = spi_write_enable();
552 if (result)
553 return result;
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000554 /* Send SE (Sector Erase) */
Peter Stugef83221b2008-07-07 06:38:51 +0000555 spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000556 /* Wait until the Write-In-Progress bit is cleared.
557 * This usually takes 15-800 ms, so wait in 10 ms steps.
558 */
Peter Stugefa8c5502008-05-10 23:07:52 +0000559 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000560 programmer_delay(10 * 1000);
Carl-Daniel Hailfinger5b1c6ed2007-10-22 16:15:28 +0000561 return 0;
562}
563
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000564int spi_write_status_enable(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000565{
566 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000567 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000568
569 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000570 result = spi_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
571
572 if (result)
573 printf_debug("%s failed", __func__);
574 if (result == SPI_INVALID_OPCODE) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000575 switch (spi_controller) {
576 case SPI_CONTROLLER_ICH7:
577 case SPI_CONTROLLER_ICH9:
578 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000579 printf_debug(" due to SPI master limitation, ignoring"
580 " and hoping it will be run as PREOP\n");
581 return 0;
582 default:
583 break;
584 }
585 }
586 if (result)
587 printf_debug("\n");
588
589 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000590}
591
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000592/*
593 * This is according the SST25VF016 datasheet, who knows it is more
594 * generic that this...
595 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000596int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000597{
Uwe Hermann394131e2008-10-18 21:14:13 +0000598 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
599 { JEDEC_WRSR, (unsigned char)status };
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000600
601 /* Send WRSR (Write Status Register) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000602 return spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000603}
604
605void spi_byte_program(int address, uint8_t byte)
606{
Uwe Hermann394131e2008-10-18 21:14:13 +0000607 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
608 JEDEC_BYTE_PROGRAM,
609 (address >> 16) & 0xff,
610 (address >> 8) & 0xff,
611 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000612 byte
613 };
614
615 /* Send Byte-Program */
Peter Stugef83221b2008-07-07 06:38:51 +0000616 spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000617}
618
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000619int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000620{
621 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000622 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000623
Peter Stugefa8c5502008-05-10 23:07:52 +0000624 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000625 /* If there is block protection in effect, unprotect it first. */
626 if ((status & 0x3c) != 0) {
627 printf_debug("Some block protection in effect, disabling\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000628 result = spi_write_status_enable();
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000629 if (result) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000630 printf_debug("spi_write_status_enable failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000631 return result;
632 }
633 result = spi_write_status_register(status & ~0x3c);
634 if (result) {
635 printf_debug("spi_write_status_register failed\n");
636 return result;
637 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000638 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000639 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000640}
641
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000642int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000643{
Uwe Hermann394131e2008-10-18 21:14:13 +0000644 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
645 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000646 (address >> 16) & 0xff,
647 (address >> 8) & 0xff,
648 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000649 };
650
651 /* Send Read */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000652 return spi_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000653}
654
Peter Stugefa8c5502008-05-10 23:07:52 +0000655int spi_chip_read(struct flashchip *flash, uint8_t *buf)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000656{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000657 switch (spi_controller) {
658 case SPI_CONTROLLER_IT87XX:
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000659 return it8716f_spi_chip_read(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000660 case SPI_CONTROLLER_SB600:
Jason Wanga3f04be2008-11-28 21:36:51 +0000661 return sb600_spi_read(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000662 case SPI_CONTROLLER_ICH7:
663 case SPI_CONTROLLER_ICH9:
664 case SPI_CONTROLLER_VIA:
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000665 return ich_spi_read(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000666 case SPI_CONTROLLER_WBSIO:
Peter Stugebf196e92009-01-26 03:08:45 +0000667 return wbsio_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000668 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000669 printf_debug
670 ("%s called, but no SPI chipset/strapping detected\n",
671 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000672 }
673
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000674 return 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000675}
676
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000677/*
678 * Program chip using byte programming. (SLOW!)
679 * This is for chips which can only handle one byte writes
680 * and for chips where memory mapped programming is impossible
681 * (e.g. due to size constraints in IT87* for over 512 kB)
682 */
683int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
684{
685 int total_size = 1024 * flash->total_size;
686 int i;
687
688 spi_disable_blockprotect();
689 for (i = 0; i < total_size; i++) {
690 spi_write_enable();
691 spi_byte_program(i, buf[i]);
692 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000693 programmer_delay(10);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000694 }
695
696 return 0;
697}
698
699/*
700 * Program chip using page (256 bytes) programming.
701 * Some SPI masters can't do this, they use single byte programming instead.
702 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +0000703int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000704{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000705 switch (spi_controller) {
706 case SPI_CONTROLLER_IT87XX:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000707 return it8716f_spi_chip_write_256(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000708 case SPI_CONTROLLER_SB600:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000709 return sb600_spi_write_1(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000710 case SPI_CONTROLLER_ICH7:
711 case SPI_CONTROLLER_ICH9:
712 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000713 return ich_spi_write_256(flash, buf);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000714 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000715 return wbsio_spi_write_1(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000716 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000717 printf_debug
718 ("%s called, but no SPI chipset/strapping detected\n",
719 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000720 }
721
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000722 return 1;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000723}
Peter Stugefd9217d2009-01-26 03:37:40 +0000724
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000725uint32_t spi_get_valid_read_addr(void)
726{
727 /* Need to return BBAR for ICH chipsets. */
728 return 0;
729}
730
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000731int spi_aai_write(struct flashchip *flash, uint8_t *buf)
732{
Peter Stugefd9217d2009-01-26 03:37:40 +0000733 uint32_t pos = 2, size = flash->total_size * 1024;
734 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000735 int result;
736
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000737 switch (spi_controller) {
738 case SPI_CONTROLLER_WBSIO:
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000739 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
740 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000741 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000742 default:
743 break;
Peter Stugefd9217d2009-01-26 03:37:40 +0000744 }
745 flash->erase(flash);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000746 result = spi_write_enable();
747 if (result)
748 return result;
Peter Stugefd9217d2009-01-26 03:37:40 +0000749 spi_command(6, 0, w, NULL);
750 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000751 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +0000752 while (pos < size) {
753 w[1] = buf[pos++];
754 w[2] = buf[pos++];
755 spi_command(3, 0, w, NULL);
756 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000757 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
Peter Stugefd9217d2009-01-26 03:37:40 +0000758 }
759 spi_write_disable();
760 return 0;
761}