blob: dcd9f19e28e68af911a24202f1df4d6d56ac4433 [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
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +000029void spi_prettyprint_status_register(struct flashchip *flash);
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000030
Uwe Hermann394131e2008-10-18 21:14:13 +000031int spi_command(unsigned int writecnt, unsigned int readcnt,
32 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000033{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000034 switch (flashbus) {
35 case BUS_TYPE_IT87XX_SPI:
Uwe Hermann394131e2008-10-18 21:14:13 +000036 return it8716f_spi_command(writecnt, readcnt, writearr,
37 readarr);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000038 case BUS_TYPE_ICH7_SPI:
39 case BUS_TYPE_ICH9_SPI:
40 case BUS_TYPE_VIA_SPI:
Uwe Hermann394131e2008-10-18 21:14:13 +000041 return ich_spi_command(writecnt, readcnt, writearr, readarr);
Jason Wanga3f04be2008-11-28 21:36:51 +000042 case BUS_TYPE_SB600_SPI:
43 return sb600_spi_command(writecnt, readcnt, writearr, readarr);
Peter Stugebf196e92009-01-26 03:08:45 +000044 case BUS_TYPE_WBSIO_SPI:
45 return wbsio_spi_command(writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000046 case BUS_TYPE_DUMMY_SPI:
47 return dummy_spi_command(writecnt, readcnt, writearr, readarr);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000048 default:
Uwe Hermann394131e2008-10-18 21:14:13 +000049 printf_debug
50 ("%s called, but no SPI chipset/strapping detected\n",
51 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000052 }
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000053 return 1;
54}
55
Rudolf Marek48a85e42008-06-30 21:45:17 +000056static int spi_rdid(unsigned char *readarr, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000057{
Uwe Hermann394131e2008-10-18 21:14:13 +000058 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000059 int ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000060 int i;
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000061
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000062 ret = spi_command(sizeof(cmd), bytes, cmd, readarr);
63 if (ret)
64 return ret;
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +000065 printf_debug("RDID returned");
66 for (i = 0; i < bytes; i++)
67 printf_debug(" 0x%02x", readarr[i]);
68 printf_debug("\n");
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000069 return 0;
70}
71
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000072static int spi_rems(unsigned char *readarr)
73{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000074 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
75 uint32_t readaddr;
76 int ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000077
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000078 ret = spi_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
79 if (ret == SPI_INVALID_ADDRESS) {
80 /* Find the lowest even address allowed for reads. */
81 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
82 cmd[1] = (readaddr >> 16) & 0xff,
83 cmd[2] = (readaddr >> 8) & 0xff,
84 cmd[3] = (readaddr >> 0) & 0xff,
85 ret = spi_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
86 }
87 if (ret)
88 return ret;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +000089 printf_debug("REMS returned %02x %02x.\n", readarr[0], readarr[1]);
90 return 0;
91}
92
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000093static int spi_res(unsigned char *readarr)
94{
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000095 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
96 uint32_t readaddr;
97 int ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +000098
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +000099 ret = spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
100 if (ret == SPI_INVALID_ADDRESS) {
101 /* Find the lowest even address allowed for reads. */
102 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
103 cmd[1] = (readaddr >> 16) & 0xff,
104 cmd[2] = (readaddr >> 8) & 0xff,
105 cmd[3] = (readaddr >> 0) & 0xff,
106 ret = spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
107 }
108 if (ret)
109 return ret;
Carl-Daniel Hailfinger42c54972008-05-15 03:19:49 +0000110 printf_debug("RES returned %02x.\n", readarr[0]);
111 return 0;
112}
113
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000114int spi_write_enable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000115{
Uwe Hermann394131e2008-10-18 21:14:13 +0000116 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000117 int result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000118
119 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000120 result = spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000121
122 if (result)
123 printf_debug("%s failed", __func__);
124 if (result == SPI_INVALID_OPCODE) {
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000125 switch (flashbus) {
126 case BUS_TYPE_ICH7_SPI:
127 case BUS_TYPE_ICH9_SPI:
128 case BUS_TYPE_VIA_SPI:
129 printf_debug(" due to SPI master limitation, ignoring"
130 " and hoping it will be run as PREOP\n");
131 return 0;
132 default:
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000133 break;
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000134 }
135 }
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000136 if (result)
137 printf_debug("\n");
138
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000139 return result;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000140}
141
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000142int spi_write_disable(void)
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000143{
Uwe Hermann394131e2008-10-18 21:14:13 +0000144 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000145
146 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000147 return spi_command(sizeof(cmd), 0, cmd, NULL);
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000148}
149
Rudolf Marek48a85e42008-06-30 21:45:17 +0000150static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000151{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000152 unsigned char readarr[4];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000153 uint32_t id1;
154 uint32_t id2;
Carl-Daniel Hailfinger9a3ec822007-12-29 10:15:58 +0000155
Rudolf Marek48a85e42008-06-30 21:45:17 +0000156 if (spi_rdid(readarr, bytes))
Peter Stugeda4e5f32008-06-24 01:22:03 +0000157 return 0;
158
159 if (!oddparity(readarr[0]))
160 printf_debug("RDID byte 0 parity violation.\n");
161
162 /* Check if this is a continuation vendor ID */
163 if (readarr[0] == 0x7f) {
164 if (!oddparity(readarr[1]))
165 printf_debug("RDID byte 1 parity violation.\n");
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000166 id1 = (readarr[0] << 8) | readarr[1];
167 id2 = readarr[2];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000168 if (bytes > 3) {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000169 id2 <<= 8;
170 id2 |= readarr[3];
Rudolf Marek48a85e42008-06-30 21:45:17 +0000171 }
Peter Stugeda4e5f32008-06-24 01:22:03 +0000172 } else {
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000173 id1 = readarr[0];
174 id2 = (readarr[1] << 8) | readarr[2];
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000175 }
176
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000177 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
Peter Stugeda4e5f32008-06-24 01:22:03 +0000178
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000179 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Peter Stugeda4e5f32008-06-24 01:22:03 +0000180 /* Print the status register to tell the
181 * user about possible write protection.
182 */
183 spi_prettyprint_status_register(flash);
184
185 return 1;
186 }
187
188 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000189 if (id1 == flash->manufacture_id &&
Peter Stugeda4e5f32008-06-24 01:22:03 +0000190 GENERIC_DEVICE_ID == flash->model_id)
191 return 1;
192
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +0000193 return 0;
194}
195
Uwe Hermann394131e2008-10-18 21:14:13 +0000196int probe_spi_rdid(struct flashchip *flash)
197{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000198 return probe_spi_rdid_generic(flash, 3);
199}
200
201/* support 4 bytes flash ID */
Uwe Hermann394131e2008-10-18 21:14:13 +0000202int probe_spi_rdid4(struct flashchip *flash)
203{
Rudolf Marek48a85e42008-06-30 21:45:17 +0000204 /* only some SPI chipsets support 4 bytes commands */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000205 switch (flashbus) {
206 case BUS_TYPE_ICH7_SPI:
207 case BUS_TYPE_ICH9_SPI:
208 case BUS_TYPE_VIA_SPI:
Jason Wanga3f04be2008-11-28 21:36:51 +0000209 case BUS_TYPE_SB600_SPI:
Peter Stugebf196e92009-01-26 03:08:45 +0000210 case BUS_TYPE_WBSIO_SPI:
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000211 case BUS_TYPE_DUMMY_SPI:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000212 return probe_spi_rdid_generic(flash, 4);
213 default:
214 printf_debug("4b ID not supported on this SPI controller\n");
215 }
216
217 return 0;
Rudolf Marek48a85e42008-06-30 21:45:17 +0000218}
219
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000220int probe_spi_rems(struct flashchip *flash)
221{
222 unsigned char readarr[JEDEC_REMS_INSIZE];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000223 uint32_t id1, id2;
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000224
225 if (spi_rems(readarr))
226 return 0;
227
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000228 id1 = readarr[0];
229 id2 = readarr[1];
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000230
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000231 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000232
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000233 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000234 /* 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. */
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000243 if (id1 == flash->manufacture_id &&
Carl-Daniel Hailfinger14e50ac2008-11-28 01:25:00 +0000244 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];
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000253 uint32_t id2;
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
Carl-Daniel Hailfinger2ad267d2009-05-27 11:40:08 +0000265 id2 = readarr[0];
266 printf_debug("%s: id 0x%x\n", __FUNCTION__, id2);
267 if (id2 != flash->model_id)
Peter Stugeda4e5f32008-06-24 01:22:03 +0000268 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 };
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000564 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000565
566 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfinger1e637842009-05-15 00:56:22 +0000567 result = spi_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
568
569 if (result)
570 printf_debug("%s failed", __func__);
571 if (result == SPI_INVALID_OPCODE) {
572 switch (flashbus) {
573 case BUS_TYPE_ICH7_SPI:
574 case BUS_TYPE_ICH9_SPI:
575 case BUS_TYPE_VIA_SPI:
576 printf_debug(" due to SPI master limitation, ignoring"
577 " and hoping it will be run as PREOP\n");
578 return 0;
579 default:
580 break;
581 }
582 }
583 if (result)
584 printf_debug("\n");
585
586 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +0000587}
588
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000589/*
590 * This is according the SST25VF016 datasheet, who knows it is more
591 * generic that this...
592 */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000593int spi_write_status_register(int status)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000594{
Uwe Hermann394131e2008-10-18 21:14:13 +0000595 const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
596 { JEDEC_WRSR, (unsigned char)status };
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000597
598 /* Send WRSR (Write Status Register) */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000599 return spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000600}
601
602void spi_byte_program(int address, uint8_t byte)
603{
Uwe Hermann394131e2008-10-18 21:14:13 +0000604 const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
605 JEDEC_BYTE_PROGRAM,
606 (address >> 16) & 0xff,
607 (address >> 8) & 0xff,
608 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000609 byte
610 };
611
612 /* Send Byte-Program */
Peter Stugef83221b2008-07-07 06:38:51 +0000613 spi_command(sizeof(cmd), 0, cmd, NULL);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000614}
615
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000616int spi_disable_blockprotect(void)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000617{
618 uint8_t status;
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000619 int result;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000620
Peter Stugefa8c5502008-05-10 23:07:52 +0000621 status = spi_read_status_register();
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000622 /* If there is block protection in effect, unprotect it first. */
623 if ((status & 0x3c) != 0) {
624 printf_debug("Some block protection in effect, disabling\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000625 result = spi_write_status_enable();
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000626 if (result) {
Jason Wanga3f04be2008-11-28 21:36:51 +0000627 printf_debug("spi_write_status_enable failed\n");
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000628 return result;
629 }
630 result = spi_write_status_register(status & ~0x3c);
631 if (result) {
632 printf_debug("spi_write_status_register failed\n");
633 return result;
634 }
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000635 }
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000636 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000637}
638
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000639int spi_nbyte_read(int address, uint8_t *bytes, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000640{
Uwe Hermann394131e2008-10-18 21:14:13 +0000641 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
642 JEDEC_READ,
Carl-Daniel Hailfingerd3568ad2008-01-22 14:37:31 +0000643 (address >> 16) & 0xff,
644 (address >> 8) & 0xff,
645 (address >> 0) & 0xff,
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000646 };
647
648 /* Send Read */
Carl-Daniel Hailfinger598ec582008-11-18 00:41:02 +0000649 return spi_command(sizeof(cmd), len, cmd, bytes);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000650}
651
Peter Stugefa8c5502008-05-10 23:07:52 +0000652int spi_chip_read(struct flashchip *flash, uint8_t *buf)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000653{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000654 switch (flashbus) {
655 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000656 return it8716f_spi_chip_read(flash, buf);
Jason Wanga3f04be2008-11-28 21:36:51 +0000657 case BUS_TYPE_SB600_SPI:
658 return sb600_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000659 case BUS_TYPE_ICH7_SPI:
660 case BUS_TYPE_ICH9_SPI:
661 case BUS_TYPE_VIA_SPI:
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000662 return ich_spi_read(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000663 case BUS_TYPE_WBSIO_SPI:
664 return wbsio_spi_read(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000665 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000666 printf_debug
667 ("%s called, but no SPI chipset/strapping detected\n",
668 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000669 }
670
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000671 return 1;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000672}
673
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000674/*
675 * Program chip using byte programming. (SLOW!)
676 * This is for chips which can only handle one byte writes
677 * and for chips where memory mapped programming is impossible
678 * (e.g. due to size constraints in IT87* for over 512 kB)
679 */
680int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
681{
682 int total_size = 1024 * flash->total_size;
683 int i;
684
685 spi_disable_blockprotect();
686 for (i = 0; i < total_size; i++) {
687 spi_write_enable();
688 spi_byte_program(i, buf[i]);
689 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
690 myusec_delay(10);
691 }
692
693 return 0;
694}
695
696/*
697 * Program chip using page (256 bytes) programming.
698 * Some SPI masters can't do this, they use single byte programming instead.
699 */
Carl-Daniel Hailfinger8d497012009-05-09 02:34:18 +0000700int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000701{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000702 switch (flashbus) {
703 case BUS_TYPE_IT87XX_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000704 return it8716f_spi_chip_write_256(flash, buf);
Jason Wanga3f04be2008-11-28 21:36:51 +0000705 case BUS_TYPE_SB600_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000706 return sb600_spi_write_1(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000707 case BUS_TYPE_ICH7_SPI:
708 case BUS_TYPE_ICH9_SPI:
709 case BUS_TYPE_VIA_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000710 return ich_spi_write_256(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000711 case BUS_TYPE_WBSIO_SPI:
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000712 return wbsio_spi_write_1(flash, buf);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000713 default:
Uwe Hermann394131e2008-10-18 21:14:13 +0000714 printf_debug
715 ("%s called, but no SPI chipset/strapping detected\n",
716 __FUNCTION__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000717 }
718
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000719 return 1;
Carl-Daniel Hailfinger6b444962007-10-18 00:24:07 +0000720}
Peter Stugefd9217d2009-01-26 03:37:40 +0000721
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000722uint32_t spi_get_valid_read_addr(void)
723{
724 /* Need to return BBAR for ICH chipsets. */
725 return 0;
726}
727
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000728int spi_aai_write(struct flashchip *flash, uint8_t *buf)
729{
Peter Stugefd9217d2009-01-26 03:37:40 +0000730 uint32_t pos = 2, size = flash->total_size * 1024;
731 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000732 int result;
733
Peter Stugefd9217d2009-01-26 03:37:40 +0000734 switch (flashbus) {
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000735 case BUS_TYPE_WBSIO_SPI:
736 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
737 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000738 return spi_chip_write_1(flash, buf);
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000739 default:
740 break;
Peter Stugefd9217d2009-01-26 03:37:40 +0000741 }
742 flash->erase(flash);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000743 result = spi_write_enable();
744 if (result)
745 return result;
Peter Stugefd9217d2009-01-26 03:37:40 +0000746 spi_command(6, 0, w, NULL);
747 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
748 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
749 while (pos < size) {
750 w[1] = buf[pos++];
751 w[2] = buf[pos++];
752 spi_command(3, 0, w, NULL);
753 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
754 myusec_delay(5); /* SST25VF040B Tbp is max 10us */
755 }
756 spi_write_disable();
757 return 0;
758}