blob: b7e8189e15fa661f2fe7b9fb4705b8bd59ab3a73 [file] [log] [blame]
Sean Nelson14ba6682010-02-26 05:48:29 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00004 * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger
Sean Nelson14ba6682010-02-26 05:48:29 +00005 * Copyright (C) 2008 coresystems GmbH
6 *
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 common SPI chip driver functions
23 */
24
25#include <string.h>
26#include "flash.h"
27#include "flashchips.h"
28#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000029#include "programmer.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000030#include "spi.h"
31
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000032static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000033{
Mathias Krausea60faab2011-01-17 07:50:42 +000034 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Sean Nelson14ba6682010-02-26 05:48:29 +000035 int ret;
36 int i;
37
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000038 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000039 if (ret)
40 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000041 msg_cspew("RDID returned");
Sean Nelson14ba6682010-02-26 05:48:29 +000042 for (i = 0; i < bytes; i++)
Sean Nelsoned479d22010-03-24 23:14:32 +000043 msg_cspew(" 0x%02x", readarr[i]);
44 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000045 return 0;
46}
47
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000048static int spi_rems(struct flashctx *flash, unsigned char *readarr)
Sean Nelson14ba6682010-02-26 05:48:29 +000049{
50 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
51 uint32_t readaddr;
52 int ret;
53
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000054 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd,
55 readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000056 if (ret == SPI_INVALID_ADDRESS) {
57 /* Find the lowest even address allowed for reads. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000058 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
Sean Nelson14ba6682010-02-26 05:48:29 +000059 cmd[1] = (readaddr >> 16) & 0xff,
60 cmd[2] = (readaddr >> 8) & 0xff,
61 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000062 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE,
63 cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000064 }
65 if (ret)
66 return ret;
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +000067 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
Sean Nelson14ba6682010-02-26 05:48:29 +000068 return 0;
69}
70
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000071static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000072{
73 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
74 uint32_t readaddr;
75 int ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000076 int i;
Sean Nelson14ba6682010-02-26 05:48:29 +000077
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000078 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000079 if (ret == SPI_INVALID_ADDRESS) {
80 /* Find the lowest even address allowed for reads. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000081 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
Sean Nelson14ba6682010-02-26 05:48:29 +000082 cmd[1] = (readaddr >> 16) & 0xff,
83 cmd[2] = (readaddr >> 8) & 0xff,
84 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000085 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000086 }
87 if (ret)
88 return ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000089 msg_cspew("RES returned");
90 for (i = 0; i < bytes; i++)
91 msg_cspew(" 0x%02x", readarr[i]);
92 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000093 return 0;
94}
95
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000096int spi_write_enable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +000097{
Mathias Krausea60faab2011-01-17 07:50:42 +000098 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Sean Nelson14ba6682010-02-26 05:48:29 +000099 int result;
100
101 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000102 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000103
104 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000105 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000106
107 return result;
108}
109
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000110int spi_write_disable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000111{
Mathias Krausea60faab2011-01-17 07:50:42 +0000112 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Sean Nelson14ba6682010-02-26 05:48:29 +0000113
114 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000115 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000116}
117
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000118static int probe_spi_rdid_generic(struct flashctx *flash, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +0000119{
120 unsigned char readarr[4];
121 uint32_t id1;
122 uint32_t id2;
123
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000124 if (spi_rdid(flash, readarr, bytes)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000125 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000126 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000127
128 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000129 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000130
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000131 /* Check if this is a continuation vendor ID.
132 * FIXME: Handle continuation device IDs.
133 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000134 if (readarr[0] == 0x7f) {
135 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000136 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000137 id1 = (readarr[0] << 8) | readarr[1];
138 id2 = readarr[2];
139 if (bytes > 3) {
140 id2 <<= 8;
141 id2 |= readarr[3];
142 }
143 } else {
144 id1 = readarr[0];
145 id2 = (readarr[1] << 8) | readarr[2];
146 }
147
Sean Nelsoned479d22010-03-24 23:14:32 +0000148 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000149
150 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
151 /* Print the status register to tell the
152 * user about possible write protection.
153 */
154 spi_prettyprint_status_register(flash);
155
156 return 1;
157 }
158
159 /* Test if this is a pure vendor match. */
160 if (id1 == flash->manufacture_id &&
161 GENERIC_DEVICE_ID == flash->model_id)
162 return 1;
163
164 /* Test if there is any vendor ID. */
165 if (GENERIC_MANUF_ID == flash->manufacture_id &&
166 id1 != 0xff)
167 return 1;
168
169 return 0;
170}
171
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000172int probe_spi_rdid(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000173{
174 return probe_spi_rdid_generic(flash, 3);
175}
176
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000177int probe_spi_rdid4(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000178{
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000179 /* Some SPI controllers do not support commands with writecnt=1 and
180 * readcnt=4.
181 */
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000182 switch (flash->pgm->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000183#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000184#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000185 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000186 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000187 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
188 return 0;
189 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000190#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000191#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000192 default:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000193 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000194 }
195
196 return 0;
197}
198
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000199int probe_spi_rems(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000200{
201 unsigned char readarr[JEDEC_REMS_INSIZE];
202 uint32_t id1, id2;
203
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000204 if (spi_rems(flash, readarr)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000205 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000206 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000207
208 id1 = readarr[0];
209 id2 = readarr[1];
210
Sean Nelsoned479d22010-03-24 23:14:32 +0000211 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000212
213 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
214 /* Print the status register to tell the
215 * user about possible write protection.
216 */
217 spi_prettyprint_status_register(flash);
218
219 return 1;
220 }
221
222 /* Test if this is a pure vendor match. */
223 if (id1 == flash->manufacture_id &&
224 GENERIC_DEVICE_ID == flash->model_id)
225 return 1;
226
227 /* Test if there is any vendor ID. */
228 if (GENERIC_MANUF_ID == flash->manufacture_id &&
229 id1 != 0xff)
230 return 1;
231
232 return 0;
233}
234
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000235int probe_spi_res1(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000236{
Mathias Krausea60faab2011-01-17 07:50:42 +0000237 static const unsigned char allff[] = {0xff, 0xff, 0xff};
238 static const unsigned char all00[] = {0x00, 0x00, 0x00};
Sean Nelson14ba6682010-02-26 05:48:29 +0000239 unsigned char readarr[3];
240 uint32_t id2;
Sean Nelson14ba6682010-02-26 05:48:29 +0000241
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000242 /* We only want one-byte RES if RDID and REMS are unusable. */
243
Sean Nelson14ba6682010-02-26 05:48:29 +0000244 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
245 * 0x00 0x00 0x00. In that case, RES is pointless.
246 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000247 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000248 memcmp(readarr, all00, 3)) {
249 msg_cdbg("Ignoring RES in favour of RDID.\n");
250 return 0;
251 }
252 /* Check if REMS is usable and does not return 0xff 0xff or
253 * 0x00 0x00. In that case, RES is pointless.
254 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000255 if (!spi_rems(flash, readarr) &&
256 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000257 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
258 msg_cdbg("Ignoring RES in favour of REMS.\n");
259 return 0;
260 }
261
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000262 if (spi_res(flash, readarr, 1)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000263 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000264 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000265
Sean Nelson14ba6682010-02-26 05:48:29 +0000266 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000267
Sean Nelsoned479d22010-03-24 23:14:32 +0000268 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000269
Stefan Taunerdb45ab52011-05-28 22:59:05 +0000270 if (id2 != flash->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +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;
278}
279
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000280int probe_spi_res2(struct flashctx *flash)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000281{
282 unsigned char readarr[2];
283 uint32_t id1, id2;
284
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000285 if (spi_res(flash, readarr, 2)) {
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000286 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000287 }
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000288
289 id1 = readarr[0];
290 id2 = readarr[1];
291
292 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
293
294 if (id1 != flash->manufacture_id || id2 != flash->model_id)
295 return 0;
296
297 /* Print the status register to tell the
298 * user about possible write protection.
299 */
300 spi_prettyprint_status_register(flash);
301 return 1;
302}
303
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000304uint8_t spi_read_status_register(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000305{
Mathias Krausea60faab2011-01-17 07:50:42 +0000306 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Sean Nelson14ba6682010-02-26 05:48:29 +0000307 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
308 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
309 int ret;
310
311 /* Read Status Register */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000312 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd,
313 readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +0000314 if (ret)
Sean Nelsoned479d22010-03-24 23:14:32 +0000315 msg_cerr("RDSR failed!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000316
317 return readarr[0];
318}
319
320/* Prettyprint the status register. Common definitions. */
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000321void spi_prettyprint_status_register_welwip(uint8_t status)
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000322{
323 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
324 "%sset\n", (status & (1 << 1)) ? "" : "not ");
325 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
326 "%sset\n", (status & (1 << 0)) ? "" : "not ");
327}
328
329/* Prettyprint the status register. Common definitions. */
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000330void spi_prettyprint_status_register_bp3210(uint8_t status, int bp)
331{
332 switch (bp) {
333 /* Fall through. */
334 case 3:
335 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) "
336 "is %sset\n", (status & (1 << 5)) ? "" : "not ");
337 case 2:
338 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) "
339 "is %sset\n", (status & (1 << 4)) ? "" : "not ");
340 case 1:
341 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) "
342 "is %sset\n", (status & (1 << 3)) ? "" : "not ");
343 case 0:
344 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) "
345 "is %sset\n", (status & (1 << 2)) ? "" : "not ");
346 }
347}
348
349/* Prettyprint the status register. Unnamed bits. */
350void spi_prettyprint_status_register_bit(uint8_t status, int bit)
351{
352 msg_cdbg("Chip status register: Bit %i "
353 "is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
354}
355
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000356static void spi_prettyprint_status_register_common(uint8_t status)
Sean Nelson14ba6682010-02-26 05:48:29 +0000357{
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000358 spi_prettyprint_status_register_bp3210(status, 3);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000359 spi_prettyprint_status_register_welwip(status);
Sean Nelson14ba6682010-02-26 05:48:29 +0000360}
361
362/* Prettyprint the status register. Works for
363 * ST M25P series
364 * MX MX25L series
365 */
366void spi_prettyprint_status_register_st_m25p(uint8_t status)
367{
Sean Nelsoned479d22010-03-24 23:14:32 +0000368 msg_cdbg("Chip status register: Status Register Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000369 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000370 msg_cdbg("Chip status register: Bit 6 is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000371 "%sset\n", (status & (1 << 6)) ? "" : "not ");
372 spi_prettyprint_status_register_common(status);
373}
374
375void spi_prettyprint_status_register_sst25(uint8_t status)
376{
Sean Nelsoned479d22010-03-24 23:14:32 +0000377 msg_cdbg("Chip status register: Block Protect Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000378 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000379 msg_cdbg("Chip status register: Auto Address Increment Programming "
Sean Nelson14ba6682010-02-26 05:48:29 +0000380 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
381 spi_prettyprint_status_register_common(status);
382}
383
384/* Prettyprint the status register. Works for
385 * SST 25VF016
386 */
387void spi_prettyprint_status_register_sst25vf016(uint8_t status)
388{
Mathias Krausea60faab2011-01-17 07:50:42 +0000389 static const char *const bpt[] = {
Sean Nelson14ba6682010-02-26 05:48:29 +0000390 "none",
391 "1F0000H-1FFFFFH",
392 "1E0000H-1FFFFFH",
393 "1C0000H-1FFFFFH",
394 "180000H-1FFFFFH",
395 "100000H-1FFFFFH",
396 "all", "all"
397 };
398 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000399 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000400 bpt[(status & 0x1c) >> 2]);
401}
402
403void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
404{
Mathias Krausea60faab2011-01-17 07:50:42 +0000405 static const char *const bpt[] = {
Sean Nelson14ba6682010-02-26 05:48:29 +0000406 "none",
407 "0x70000-0x7ffff",
408 "0x60000-0x7ffff",
409 "0x40000-0x7ffff",
410 "all blocks", "all blocks", "all blocks", "all blocks"
411 };
412 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000413 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000414 bpt[(status & 0x1c) >> 2]);
415}
416
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000417int spi_prettyprint_status_register(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000418{
419 uint8_t status;
420
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000421 status = spi_read_status_register(flash);
Sean Nelsoned479d22010-03-24 23:14:32 +0000422 msg_cdbg("Chip status register is %02x\n", status);
Sean Nelson14ba6682010-02-26 05:48:29 +0000423 switch (flash->manufacture_id) {
424 case ST_ID:
425 if (((flash->model_id & 0xff00) == 0x2000) ||
426 ((flash->model_id & 0xff00) == 0x2500))
427 spi_prettyprint_status_register_st_m25p(status);
428 break;
Mattias Mattsson6eabe282010-09-15 23:31:03 +0000429 case MACRONIX_ID:
Sean Nelson14ba6682010-02-26 05:48:29 +0000430 if ((flash->model_id & 0xff00) == 0x2000)
431 spi_prettyprint_status_register_st_m25p(status);
432 break;
433 case SST_ID:
434 switch (flash->model_id) {
435 case 0x2541:
436 spi_prettyprint_status_register_sst25vf016(status);
437 break;
438 case 0x8d:
439 case 0x258d:
440 spi_prettyprint_status_register_sst25vf040b(status);
441 break;
442 default:
443 spi_prettyprint_status_register_sst25(status);
444 break;
445 }
446 break;
447 }
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000448 return 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000449}
450
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000451int spi_chip_erase_60(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000452{
453 int result;
454 struct spi_command cmds[] = {
455 {
456 .writecnt = JEDEC_WREN_OUTSIZE,
457 .writearr = (const unsigned char[]){ JEDEC_WREN },
458 .readcnt = 0,
459 .readarr = NULL,
460 }, {
461 .writecnt = JEDEC_CE_60_OUTSIZE,
462 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
463 .readcnt = 0,
464 .readarr = NULL,
465 }, {
466 .writecnt = 0,
467 .writearr = NULL,
468 .readcnt = 0,
469 .readarr = NULL,
470 }};
471
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000472 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000473 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000474 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000475 __func__);
476 return result;
477 }
478 /* Wait until the Write-In-Progress bit is cleared.
479 * This usually takes 1-85 s, so wait in 1 s steps.
480 */
481 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000482 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000483 programmer_delay(1000 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000484 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000485 return 0;
486}
487
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000488int spi_chip_erase_c7(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000489{
490 int result;
491 struct spi_command cmds[] = {
492 {
493 .writecnt = JEDEC_WREN_OUTSIZE,
494 .writearr = (const unsigned char[]){ JEDEC_WREN },
495 .readcnt = 0,
496 .readarr = NULL,
497 }, {
498 .writecnt = JEDEC_CE_C7_OUTSIZE,
499 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
500 .readcnt = 0,
501 .readarr = NULL,
502 }, {
503 .writecnt = 0,
504 .writearr = NULL,
505 .readcnt = 0,
506 .readarr = NULL,
507 }};
508
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000509 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000510 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000511 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000512 return result;
513 }
514 /* Wait until the Write-In-Progress bit is cleared.
515 * This usually takes 1-85 s, so wait in 1 s steps.
516 */
517 /* FIXME: We assume spi_read_status_register will never fail. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000518 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000519 programmer_delay(1000 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000520 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000521 return 0;
522}
523
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000524int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
525 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000526{
527 int result;
528 struct spi_command cmds[] = {
529 {
530 .writecnt = JEDEC_WREN_OUTSIZE,
531 .writearr = (const unsigned char[]){ JEDEC_WREN },
532 .readcnt = 0,
533 .readarr = NULL,
534 }, {
535 .writecnt = JEDEC_BE_52_OUTSIZE,
536 .writearr = (const unsigned char[]){
537 JEDEC_BE_52,
538 (addr >> 16) & 0xff,
539 (addr >> 8) & 0xff,
540 (addr & 0xff)
541 },
542 .readcnt = 0,
543 .readarr = NULL,
544 }, {
545 .writecnt = 0,
546 .writearr = NULL,
547 .readcnt = 0,
548 .readarr = NULL,
549 }};
550
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000551 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000552 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000553 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000554 __func__, addr);
555 return result;
556 }
557 /* Wait until the Write-In-Progress bit is cleared.
558 * This usually takes 100-4000 ms, so wait in 100 ms steps.
559 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000560 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000561 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000562 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000563 return 0;
564}
565
566/* Block size is usually
567 * 64k for Macronix
568 * 32k for SST
569 * 4-32k non-uniform for EON
570 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000571int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
572 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000573{
574 int result;
575 struct spi_command cmds[] = {
576 {
577 .writecnt = JEDEC_WREN_OUTSIZE,
578 .writearr = (const unsigned char[]){ JEDEC_WREN },
579 .readcnt = 0,
580 .readarr = NULL,
581 }, {
582 .writecnt = JEDEC_BE_D8_OUTSIZE,
583 .writearr = (const unsigned char[]){
584 JEDEC_BE_D8,
585 (addr >> 16) & 0xff,
586 (addr >> 8) & 0xff,
587 (addr & 0xff)
588 },
589 .readcnt = 0,
590 .readarr = NULL,
591 }, {
592 .writecnt = 0,
593 .writearr = NULL,
594 .readcnt = 0,
595 .readarr = NULL,
596 }};
597
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000598 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000599 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000600 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000601 __func__, addr);
602 return result;
603 }
604 /* Wait until the Write-In-Progress bit is cleared.
605 * This usually takes 100-4000 ms, so wait in 100 ms steps.
606 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000607 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000608 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000609 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000610 return 0;
611}
612
613/* Block size is usually
614 * 4k for PMC
615 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000616int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
617 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000618{
619 int result;
620 struct spi_command cmds[] = {
621 {
622 .writecnt = JEDEC_WREN_OUTSIZE,
623 .writearr = (const unsigned char[]){ JEDEC_WREN },
624 .readcnt = 0,
625 .readarr = NULL,
626 }, {
627 .writecnt = JEDEC_BE_D7_OUTSIZE,
628 .writearr = (const unsigned char[]){
629 JEDEC_BE_D7,
630 (addr >> 16) & 0xff,
631 (addr >> 8) & 0xff,
632 (addr & 0xff)
633 },
634 .readcnt = 0,
635 .readarr = NULL,
636 }, {
637 .writecnt = 0,
638 .writearr = NULL,
639 .readcnt = 0,
640 .readarr = NULL,
641 }};
642
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000643 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000644 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000645 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000646 __func__, addr);
647 return result;
648 }
649 /* Wait until the Write-In-Progress bit is cleared.
650 * This usually takes 100-4000 ms, so wait in 100 ms steps.
651 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000652 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000653 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000654 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000655 return 0;
656}
657
Sean Nelson14ba6682010-02-26 05:48:29 +0000658/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000659int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
660 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000661{
662 int result;
663 struct spi_command cmds[] = {
664 {
665 .writecnt = JEDEC_WREN_OUTSIZE,
666 .writearr = (const unsigned char[]){ JEDEC_WREN },
667 .readcnt = 0,
668 .readarr = NULL,
669 }, {
670 .writecnt = JEDEC_SE_OUTSIZE,
671 .writearr = (const unsigned char[]){
672 JEDEC_SE,
673 (addr >> 16) & 0xff,
674 (addr >> 8) & 0xff,
675 (addr & 0xff)
676 },
677 .readcnt = 0,
678 .readarr = NULL,
679 }, {
680 .writecnt = 0,
681 .writearr = NULL,
682 .readcnt = 0,
683 .readarr = NULL,
684 }};
685
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000686 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000687 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000688 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000689 __func__, addr);
690 return result;
691 }
692 /* Wait until the Write-In-Progress bit is cleared.
693 * This usually takes 15-800 ms, so wait in 10 ms steps.
694 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000695 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000696 programmer_delay(10 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000697 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000698 return 0;
699}
700
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000701int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
702 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000703{
704 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000705 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000706 __func__);
707 return -1;
708 }
709 return spi_chip_erase_60(flash);
710}
711
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000712int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
713 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000714{
715 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000716 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000717 __func__);
718 return -1;
719 }
720 return spi_chip_erase_c7(flash);
721}
722
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000723erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode)
724{
725 switch(opcode){
726 case 0xff:
727 case 0x00:
728 /* Not specified, assuming "not supported". */
729 return NULL;
730 case 0x20:
731 return &spi_block_erase_20;
732 case 0x52:
733 return &spi_block_erase_52;
734 case 0x60:
735 return &spi_block_erase_60;
736 case 0xc7:
737 return &spi_block_erase_c7;
738 case 0xd7:
739 return &spi_block_erase_d7;
740 case 0xd8:
741 return &spi_block_erase_d8;
742 default:
743 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
744 "this at flashrom@flashrom.org\n", __func__, opcode);
745 return NULL;
746 }
747}
748
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000749int spi_write_status_enable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000750{
Mathias Krausea60faab2011-01-17 07:50:42 +0000751 static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Sean Nelson14ba6682010-02-26 05:48:29 +0000752 int result;
753
754 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000755 result = spi_send_command(flash, sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000756
757 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000758 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000759
760 return result;
761}
762
763/*
764 * This is according the SST25VF016 datasheet, who knows it is more
765 * generic that this...
766 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000767static int spi_write_status_register_ewsr(struct flashctx *flash, int status)
Sean Nelson14ba6682010-02-26 05:48:29 +0000768{
769 int result;
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000770 int i = 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000771 struct spi_command cmds[] = {
772 {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000773 /* WRSR requires either EWSR or WREN depending on chip type. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000774 .writecnt = JEDEC_EWSR_OUTSIZE,
775 .writearr = (const unsigned char[]){ JEDEC_EWSR },
776 .readcnt = 0,
777 .readarr = NULL,
778 }, {
779 .writecnt = JEDEC_WRSR_OUTSIZE,
780 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
781 .readcnt = 0,
782 .readarr = NULL,
783 }, {
784 .writecnt = 0,
785 .writearr = NULL,
786 .readcnt = 0,
787 .readarr = NULL,
788 }};
789
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000790 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000791 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000792 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000793 __func__);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000794 /* No point in waiting for the command to complete if execution
795 * failed.
796 */
797 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +0000798 }
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000799 /* WRSR performs a self-timed erase before the changes take effect.
800 * This may take 50-85 ms in most cases, and some chips apparently
801 * allow running RDSR only once. Therefore pick an initial delay of
802 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
803 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000804 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000805 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP) {
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000806 if (++i > 490) {
807 msg_cerr("Error: WIP bit after WRSR never cleared\n");
808 return TIMEOUT_ERROR;
809 }
810 programmer_delay(10 * 1000);
811 }
812 return 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000813}
814
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000815static int spi_write_status_register_wren(struct flashctx *flash, int status)
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000816{
817 int result;
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000818 int i = 0;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000819 struct spi_command cmds[] = {
820 {
821 /* WRSR requires either EWSR or WREN depending on chip type. */
822 .writecnt = JEDEC_WREN_OUTSIZE,
823 .writearr = (const unsigned char[]){ JEDEC_WREN },
824 .readcnt = 0,
825 .readarr = NULL,
826 }, {
827 .writecnt = JEDEC_WRSR_OUTSIZE,
828 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
829 .readcnt = 0,
830 .readarr = NULL,
831 }, {
832 .writecnt = 0,
833 .writearr = NULL,
834 .readcnt = 0,
835 .readarr = NULL,
836 }};
837
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000838 result = spi_send_multicommand(flash, cmds);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000839 if (result) {
840 msg_cerr("%s failed during command execution\n",
841 __func__);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000842 /* No point in waiting for the command to complete if execution
843 * failed.
844 */
845 return result;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000846 }
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000847 /* WRSR performs a self-timed erase before the changes take effect.
848 * This may take 50-85 ms in most cases, and some chips apparently
849 * allow running RDSR only once. Therefore pick an initial delay of
850 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
851 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000852 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000853 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP) {
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000854 if (++i > 490) {
855 msg_cerr("Error: WIP bit after WRSR never cleared\n");
856 return TIMEOUT_ERROR;
857 }
858 programmer_delay(10 * 1000);
859 }
860 return 0;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000861}
862
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000863int spi_write_status_register(struct flashctx *flash, int status)
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000864{
865 int ret = 1;
866
867 if (!(flash->feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
868 msg_cdbg("Missing status register write definition, assuming "
869 "EWSR is needed\n");
870 flash->feature_bits |= FEATURE_WRSR_EWSR;
871 }
872 if (flash->feature_bits & FEATURE_WRSR_WREN)
873 ret = spi_write_status_register_wren(flash, status);
874 if (ret && (flash->feature_bits & FEATURE_WRSR_EWSR))
875 ret = spi_write_status_register_ewsr(flash, status);
876 return ret;
877}
878
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000879int spi_byte_program(struct flashctx *flash, unsigned int addr,
880 uint8_t databyte)
Sean Nelson14ba6682010-02-26 05:48:29 +0000881{
882 int result;
883 struct spi_command cmds[] = {
884 {
885 .writecnt = JEDEC_WREN_OUTSIZE,
886 .writearr = (const unsigned char[]){ JEDEC_WREN },
887 .readcnt = 0,
888 .readarr = NULL,
889 }, {
890 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
891 .writearr = (const unsigned char[]){
892 JEDEC_BYTE_PROGRAM,
893 (addr >> 16) & 0xff,
894 (addr >> 8) & 0xff,
895 (addr & 0xff),
896 databyte
897 },
898 .readcnt = 0,
899 .readarr = NULL,
900 }, {
901 .writecnt = 0,
902 .writearr = NULL,
903 .readcnt = 0,
904 .readarr = NULL,
905 }};
906
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000907 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000908 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000909 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000910 __func__, addr);
911 }
912 return result;
913}
914
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000915int spi_nbyte_program(struct flashctx *flash, unsigned int addr, uint8_t *bytes,
916 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000917{
918 int result;
919 /* FIXME: Switch to malloc based on len unless that kills speed. */
920 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
921 JEDEC_BYTE_PROGRAM,
922 (addr >> 16) & 0xff,
923 (addr >> 8) & 0xff,
924 (addr >> 0) & 0xff,
925 };
926 struct spi_command cmds[] = {
927 {
928 .writecnt = JEDEC_WREN_OUTSIZE,
929 .writearr = (const unsigned char[]){ JEDEC_WREN },
930 .readcnt = 0,
931 .readarr = NULL,
932 }, {
933 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
934 .writearr = cmd,
935 .readcnt = 0,
936 .readarr = NULL,
937 }, {
938 .writecnt = 0,
939 .writearr = NULL,
940 .readcnt = 0,
941 .readarr = NULL,
942 }};
943
944 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000945 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000946 return 1;
947 }
948 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000949 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000950 return 1;
951 }
952
953 memcpy(&cmd[4], bytes, len);
954
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000955 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000956 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000957 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000958 __func__, addr);
959 }
960 return result;
961}
962
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000963/* A generic brute-force block protection disable works like this:
964 * Write 0x00 to the status register. Check if any locks are still set (that
965 * part is chip specific). Repeat once.
966 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000967int spi_disable_blockprotect(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000968{
969 uint8_t status;
970 int result;
971
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000972 status = spi_read_status_register(flash);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000973 /* If block protection is disabled, stop here. */
974 if ((status & 0x3c) == 0)
975 return 0;
976
977 msg_cdbg("Some block protection in effect, disabling\n");
978 result = spi_write_status_register(flash, status & ~0x3c);
979 if (result) {
980 msg_cerr("spi_write_status_register failed\n");
981 return result;
982 }
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000983 status = spi_read_status_register(flash);
Sean Nelson14ba6682010-02-26 05:48:29 +0000984 if ((status & 0x3c) != 0) {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000985 msg_cerr("Block protection could not be disabled!\n");
986 return 1;
987 }
988 return 0;
989}
990
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000991int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
992 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000993{
994 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
995 JEDEC_READ,
996 (address >> 16) & 0xff,
997 (address >> 8) & 0xff,
998 (address >> 0) & 0xff,
999 };
1000
1001 /* Send Read */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001002 return spi_send_command(flash, sizeof(cmd), len, cmd, bytes);
Sean Nelson14ba6682010-02-26 05:48:29 +00001003}
1004
1005/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001006 * Read a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001007 * FIXME: Use the chunk code from Michael Karcher instead.
Sean Nelson14ba6682010-02-26 05:48:29 +00001008 * Each page is read separately in chunks with a maximum size of chunksize.
1009 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001010int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
1011 unsigned int len, unsigned int chunksize)
Sean Nelson14ba6682010-02-26 05:48:29 +00001012{
1013 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001014 unsigned int i, j, starthere, lenhere, toread;
1015 unsigned int page_size = flash->page_size;
Sean Nelson14ba6682010-02-26 05:48:29 +00001016
1017 /* Warning: This loop has a very unusual condition and body.
1018 * The loop needs to go through each page with at least one affected
1019 * byte. The lowest page number is (start / page_size) since that
1020 * division rounds down. The highest page number we want is the page
1021 * where the last byte of the range lives. That last byte has the
1022 * address (start + len - 1), thus the highest page number is
1023 * (start + len - 1) / page_size. Since we want to include that last
1024 * page as well, the loop condition uses <=.
1025 */
1026 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1027 /* Byte position of the first byte in the range in this page. */
1028 /* starthere is an offset to the base address of the chip. */
1029 starthere = max(start, i * page_size);
1030 /* Length of bytes in the range in this page. */
1031 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1032 for (j = 0; j < lenhere; j += chunksize) {
1033 toread = min(chunksize, lenhere - j);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001034 rc = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread);
Sean Nelson14ba6682010-02-26 05:48:29 +00001035 if (rc)
1036 break;
1037 }
1038 if (rc)
1039 break;
1040 }
1041
1042 return rc;
1043}
1044
1045/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001046 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001047 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001048 * Each page is written separately in chunks with a maximum size of chunksize.
1049 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001050int spi_write_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
1051 unsigned int len, unsigned int chunksize)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001052{
1053 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001054 unsigned int i, j, starthere, lenhere, towrite;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001055 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001056 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001057 * spi_chip_write_256 have page_size set to max_writechunk_size, so
1058 * we're OK for now.
1059 */
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001060 unsigned int page_size = flash->page_size;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001061
1062 /* Warning: This loop has a very unusual condition and body.
1063 * The loop needs to go through each page with at least one affected
1064 * byte. The lowest page number is (start / page_size) since that
1065 * division rounds down. The highest page number we want is the page
1066 * where the last byte of the range lives. That last byte has the
1067 * address (start + len - 1), thus the highest page number is
1068 * (start + len - 1) / page_size. Since we want to include that last
1069 * page as well, the loop condition uses <=.
1070 */
1071 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1072 /* Byte position of the first byte in the range in this page. */
1073 /* starthere is an offset to the base address of the chip. */
1074 starthere = max(start, i * page_size);
1075 /* Length of bytes in the range in this page. */
1076 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1077 for (j = 0; j < lenhere; j += chunksize) {
1078 towrite = min(chunksize, lenhere - j);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001079 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001080 if (rc)
1081 break;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001082 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001083 programmer_delay(10);
1084 }
1085 if (rc)
1086 break;
1087 }
1088
1089 return rc;
1090}
1091
1092/*
Sean Nelson14ba6682010-02-26 05:48:29 +00001093 * Program chip using byte programming. (SLOW!)
1094 * This is for chips which can only handle one byte writes
1095 * and for chips where memory mapped programming is impossible
1096 * (e.g. due to size constraints in IT87* for over 512 kB)
1097 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001098/* real chunksize is 1, logical chunksize is 1 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001099int spi_chip_write_1(struct flashctx *flash, uint8_t *buf, unsigned int start,
1100 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +00001101{
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001102 unsigned int i;
1103 int result = 0;
Sean Nelson14ba6682010-02-26 05:48:29 +00001104
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001105 for (i = start; i < start + len; i++) {
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001106 result = spi_byte_program(flash, i, buf[i - start]);
Sean Nelson14ba6682010-02-26 05:48:29 +00001107 if (result)
1108 return 1;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001109 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +00001110 programmer_delay(10);
1111 }
1112
1113 return 0;
1114}
1115
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001116int spi_aai_write(struct flashctx *flash, uint8_t *buf, unsigned int start,
1117 unsigned int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001118{
1119 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +00001120 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001121 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1122 JEDEC_AAI_WORD_PROGRAM,
1123 };
1124 struct spi_command cmds[] = {
1125 {
1126 .writecnt = JEDEC_WREN_OUTSIZE,
1127 .writearr = (const unsigned char[]){ JEDEC_WREN },
1128 .readcnt = 0,
1129 .readarr = NULL,
1130 }, {
1131 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1132 .writearr = (const unsigned char[]){
1133 JEDEC_AAI_WORD_PROGRAM,
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001134 (start >> 16) & 0xff,
1135 (start >> 8) & 0xff,
1136 (start & 0xff),
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001137 buf[0],
1138 buf[1]
1139 },
1140 .readcnt = 0,
1141 .readarr = NULL,
1142 }, {
1143 .writecnt = 0,
1144 .writearr = NULL,
1145 .readcnt = 0,
1146 .readarr = NULL,
1147 }};
Sean Nelson14ba6682010-02-26 05:48:29 +00001148
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001149 switch (flash->pgm->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001150#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001151#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001152 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +00001153 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001154 msg_perr("%s: impossible with this SPI controller,"
Sean Nelson14ba6682010-02-26 05:48:29 +00001155 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001156 return spi_chip_write_1(flash, buf, start, len);
Sean Nelson14ba6682010-02-26 05:48:29 +00001157#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001158#endif
Sean Nelson14ba6682010-02-26 05:48:29 +00001159 default:
1160 break;
1161 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001162
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001163 /* The even start address and even length requirements can be either
1164 * honored outside this function, or we can call spi_byte_program
1165 * for the first and/or last byte and use AAI for the rest.
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001166 * FIXME: Move this to generic code.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001167 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001168 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001169 if (start % 2) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001170 msg_cerr("%s: start address not even! Please report a bug at "
1171 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001172 if (spi_chip_write_1(flash, buf, start, start % 2))
1173 return SPI_GENERIC_ERROR;
1174 pos += start % 2;
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001175 cmds[1].writearr = (const unsigned char[]){
1176 JEDEC_AAI_WORD_PROGRAM,
1177 (pos >> 16) & 0xff,
1178 (pos >> 8) & 0xff,
1179 (pos & 0xff),
1180 buf[pos - start],
1181 buf[pos - start + 1]
1182 };
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001183 /* Do not return an error for now. */
1184 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001185 }
1186 /* The data sheet requires total AAI write length to be even. */
1187 if (len % 2) {
1188 msg_cerr("%s: total write length not even! Please report a "
1189 "bug at flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001190 /* Do not return an error for now. */
1191 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001192 }
1193
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001194
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001195 result = spi_send_multicommand(flash, cmds);
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001196 if (result) {
1197 msg_cerr("%s failed during start command execution\n",
1198 __func__);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001199 /* FIXME: Should we send WRDI here as well to make sure the chip
1200 * is not in AAI mode?
1201 */
Sean Nelson14ba6682010-02-26 05:48:29 +00001202 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +00001203 }
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001204 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001205 programmer_delay(10);
1206
1207 /* We already wrote 2 bytes in the multicommand step. */
1208 pos += 2;
1209
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001210 /* Are there at least two more bytes to write? */
1211 while (pos < start + len - 1) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001212 cmd[1] = buf[pos++ - start];
1213 cmd[2] = buf[pos++ - start];
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001214 spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0,
1215 cmd, NULL);
1216 while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001217 programmer_delay(10);
1218 }
1219
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001220 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
1221 * other non-AAI command.
1222 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001223 spi_write_disable(flash);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001224
1225 /* Write remaining byte (if any). */
1226 if (pos < start + len) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001227 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001228 return SPI_GENERIC_ERROR;
1229 pos += pos % 2;
1230 }
1231
Sean Nelson14ba6682010-02-26 05:48:29 +00001232 return 0;
1233}