blob: 914b82160176414ed2bb2a7c8c9f1b911124aa8d [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{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000120 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000121 unsigned char readarr[4];
122 uint32_t id1;
123 uint32_t id2;
124
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000125 if (spi_rdid(flash, readarr, bytes)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000126 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000127 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000128
129 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000130 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000131
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000132 /* Check if this is a continuation vendor ID.
133 * FIXME: Handle continuation device IDs.
134 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000135 if (readarr[0] == 0x7f) {
136 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000137 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000138 id1 = (readarr[0] << 8) | readarr[1];
139 id2 = readarr[2];
140 if (bytes > 3) {
141 id2 <<= 8;
142 id2 |= readarr[3];
143 }
144 } else {
145 id1 = readarr[0];
146 id2 = (readarr[1] << 8) | readarr[2];
147 }
148
Sean Nelsoned479d22010-03-24 23:14:32 +0000149 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000150
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000151 if (id1 == chip->manufacture_id && id2 == chip->model_id) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000152 /* Print the status register to tell the
153 * user about possible write protection.
154 */
155 spi_prettyprint_status_register(flash);
156
157 return 1;
158 }
159
160 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000161 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000162 return 1;
163
164 /* Test if there is any vendor ID. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000165 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff)
Sean Nelson14ba6682010-02-26 05:48:29 +0000166 return 1;
167
168 return 0;
169}
170
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000171int probe_spi_rdid(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000172{
173 return probe_spi_rdid_generic(flash, 3);
174}
175
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000176int probe_spi_rdid4(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000177{
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000178 /* Some SPI controllers do not support commands with writecnt=1 and
179 * readcnt=4.
180 */
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000181 switch (flash->pgm->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000182#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000183#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000184 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000185 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000186 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
187 return 0;
188 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000189#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000190#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000191 default:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000192 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000193 }
194
195 return 0;
196}
197
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000198int probe_spi_rems(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000199{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000200 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000201 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
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000213 if (id1 == chip->manufacture_id && id2 == chip->model_id) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000214 /* 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. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000223 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000224 return 1;
225
226 /* Test if there is any vendor ID. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000227 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff)
Sean Nelson14ba6682010-02-26 05:48:29 +0000228 return 1;
229
230 return 0;
231}
232
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000233int probe_spi_res1(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000234{
Mathias Krausea60faab2011-01-17 07:50:42 +0000235 static const unsigned char allff[] = {0xff, 0xff, 0xff};
236 static const unsigned char all00[] = {0x00, 0x00, 0x00};
Sean Nelson14ba6682010-02-26 05:48:29 +0000237 unsigned char readarr[3];
238 uint32_t id2;
Sean Nelson14ba6682010-02-26 05:48:29 +0000239
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000240 /* We only want one-byte RES if RDID and REMS are unusable. */
241
Sean Nelson14ba6682010-02-26 05:48:29 +0000242 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
243 * 0x00 0x00 0x00. In that case, RES is pointless.
244 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000245 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000246 memcmp(readarr, all00, 3)) {
247 msg_cdbg("Ignoring RES in favour of RDID.\n");
248 return 0;
249 }
250 /* Check if REMS is usable and does not return 0xff 0xff or
251 * 0x00 0x00. In that case, RES is pointless.
252 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000253 if (!spi_rems(flash, readarr) &&
254 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000255 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
256 msg_cdbg("Ignoring RES in favour of REMS.\n");
257 return 0;
258 }
259
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000260 if (spi_res(flash, readarr, 1)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000261 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000262 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000263
Sean Nelson14ba6682010-02-26 05:48:29 +0000264 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000265
Sean Nelsoned479d22010-03-24 23:14:32 +0000266 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000267
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000268 if (id2 != flash->chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000269 return 0;
270
271 /* Print the status register to tell the
272 * user about possible write protection.
273 */
274 spi_prettyprint_status_register(flash);
275 return 1;
276}
277
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000278int probe_spi_res2(struct flashctx *flash)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000279{
280 unsigned char readarr[2];
281 uint32_t id1, id2;
282
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000283 if (spi_res(flash, readarr, 2)) {
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000284 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000285 }
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000286
287 id1 = readarr[0];
288 id2 = readarr[1];
289
290 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
291
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000292 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000293 return 0;
294
295 /* Print the status register to tell the
296 * user about possible write protection.
297 */
298 spi_prettyprint_status_register(flash);
299 return 1;
300}
301
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000302uint8_t spi_read_status_register(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000303{
Mathias Krausea60faab2011-01-17 07:50:42 +0000304 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Sean Nelson14ba6682010-02-26 05:48:29 +0000305 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
306 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
307 int ret;
308
309 /* Read Status Register */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000310 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd,
311 readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +0000312 if (ret)
Sean Nelsoned479d22010-03-24 23:14:32 +0000313 msg_cerr("RDSR failed!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000314
315 return readarr[0];
316}
317
318/* Prettyprint the status register. Common definitions. */
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000319void spi_prettyprint_status_register_welwip(uint8_t status)
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000320{
321 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
322 "%sset\n", (status & (1 << 1)) ? "" : "not ");
323 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
324 "%sset\n", (status & (1 << 0)) ? "" : "not ");
325}
326
327/* Prettyprint the status register. Common definitions. */
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000328void spi_prettyprint_status_register_bp(uint8_t status, int bp)
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000329{
330 switch (bp) {
331 /* Fall through. */
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000332 case 4:
333 msg_cdbg("Chip status register: Block Protect 4 (BP4) "
334 "is %sset\n", (status & (1 << 5)) ? "" : "not ");
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000335 case 3:
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000336 msg_cdbg("Chip status register: Block Protect 3 (BP3) "
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000337 "is %sset\n", (status & (1 << 5)) ? "" : "not ");
338 case 2:
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000339 msg_cdbg("Chip status register: Block Protect 2 (BP2) "
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000340 "is %sset\n", (status & (1 << 4)) ? "" : "not ");
341 case 1:
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000342 msg_cdbg("Chip status register: Block Protect 1 (BP1) "
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000343 "is %sset\n", (status & (1 << 3)) ? "" : "not ");
344 case 0:
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000345 msg_cdbg("Chip status register: Block Protect 0 (BP0) "
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000346 "is %sset\n", (status & (1 << 2)) ? "" : "not ");
347 }
348}
349
350/* Prettyprint the status register. Unnamed bits. */
351void spi_prettyprint_status_register_bit(uint8_t status, int bit)
352{
353 msg_cdbg("Chip status register: Bit %i "
354 "is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
355}
356
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000357static void spi_prettyprint_status_register_common(uint8_t status)
Sean Nelson14ba6682010-02-26 05:48:29 +0000358{
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000359 spi_prettyprint_status_register_bp(status, 3);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000360 spi_prettyprint_status_register_welwip(status);
Sean Nelson14ba6682010-02-26 05:48:29 +0000361}
362
363/* Prettyprint the status register. Works for
364 * ST M25P series
365 * MX MX25L series
366 */
367void spi_prettyprint_status_register_st_m25p(uint8_t status)
368{
Sean Nelsoned479d22010-03-24 23:14:32 +0000369 msg_cdbg("Chip status register: Status Register Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000370 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000371 msg_cdbg("Chip status register: Bit 6 is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000372 "%sset\n", (status & (1 << 6)) ? "" : "not ");
373 spi_prettyprint_status_register_common(status);
374}
375
376void spi_prettyprint_status_register_sst25(uint8_t status)
377{
Sean Nelsoned479d22010-03-24 23:14:32 +0000378 msg_cdbg("Chip status register: Block Protect Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000379 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000380 msg_cdbg("Chip status register: Auto Address Increment Programming "
Sean Nelson14ba6682010-02-26 05:48:29 +0000381 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
382 spi_prettyprint_status_register_common(status);
383}
384
385/* Prettyprint the status register. Works for
386 * SST 25VF016
387 */
388void spi_prettyprint_status_register_sst25vf016(uint8_t status)
389{
Mathias Krausea60faab2011-01-17 07:50:42 +0000390 static const char *const bpt[] = {
Sean Nelson14ba6682010-02-26 05:48:29 +0000391 "none",
392 "1F0000H-1FFFFFH",
393 "1E0000H-1FFFFFH",
394 "1C0000H-1FFFFFH",
395 "180000H-1FFFFFH",
396 "100000H-1FFFFFH",
397 "all", "all"
398 };
399 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000400 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000401 bpt[(status & 0x1c) >> 2]);
402}
403
404void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
405{
Mathias Krausea60faab2011-01-17 07:50:42 +0000406 static const char *const bpt[] = {
Sean Nelson14ba6682010-02-26 05:48:29 +0000407 "none",
408 "0x70000-0x7ffff",
409 "0x60000-0x7ffff",
410 "0x40000-0x7ffff",
411 "all blocks", "all blocks", "all blocks", "all blocks"
412 };
413 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000414 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000415 bpt[(status & 0x1c) >> 2]);
416}
417
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000418int spi_prettyprint_status_register(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000419{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000420 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000421 uint8_t status;
422
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000423 status = spi_read_status_register(flash);
Sean Nelsoned479d22010-03-24 23:14:32 +0000424 msg_cdbg("Chip status register is %02x\n", status);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000425 switch (chip->manufacture_id) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000426 case ST_ID:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000427 if (((chip->model_id & 0xff00) == 0x2000) ||
428 ((chip->model_id & 0xff00) == 0x2500))
Sean Nelson14ba6682010-02-26 05:48:29 +0000429 spi_prettyprint_status_register_st_m25p(status);
430 break;
Mattias Mattsson6eabe282010-09-15 23:31:03 +0000431 case MACRONIX_ID:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000432 if ((chip->model_id & 0xff00) == 0x2000)
Sean Nelson14ba6682010-02-26 05:48:29 +0000433 spi_prettyprint_status_register_st_m25p(status);
434 break;
435 case SST_ID:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000436 switch (chip->model_id) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000437 case 0x2541:
438 spi_prettyprint_status_register_sst25vf016(status);
439 break;
440 case 0x8d:
441 case 0x258d:
442 spi_prettyprint_status_register_sst25vf040b(status);
443 break;
444 default:
445 spi_prettyprint_status_register_sst25(status);
446 break;
447 }
448 break;
449 }
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000450 return 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000451}
452
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000453int spi_chip_erase_60(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000454{
455 int result;
456 struct spi_command cmds[] = {
457 {
458 .writecnt = JEDEC_WREN_OUTSIZE,
459 .writearr = (const unsigned char[]){ JEDEC_WREN },
460 .readcnt = 0,
461 .readarr = NULL,
462 }, {
463 .writecnt = JEDEC_CE_60_OUTSIZE,
464 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
465 .readcnt = 0,
466 .readarr = NULL,
467 }, {
468 .writecnt = 0,
469 .writearr = NULL,
470 .readcnt = 0,
471 .readarr = NULL,
472 }};
473
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000474 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000475 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000476 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000477 __func__);
478 return result;
479 }
480 /* Wait until the Write-In-Progress bit is cleared.
481 * This usually takes 1-85 s, so wait in 1 s steps.
482 */
483 /* FIXME: We assume spi_read_status_register will never fail. */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000484 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000485 programmer_delay(1000 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000486 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000487 return 0;
488}
489
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000490int spi_chip_erase_c7(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000491{
492 int result;
493 struct spi_command cmds[] = {
494 {
495 .writecnt = JEDEC_WREN_OUTSIZE,
496 .writearr = (const unsigned char[]){ JEDEC_WREN },
497 .readcnt = 0,
498 .readarr = NULL,
499 }, {
500 .writecnt = JEDEC_CE_C7_OUTSIZE,
501 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
502 .readcnt = 0,
503 .readarr = NULL,
504 }, {
505 .writecnt = 0,
506 .writearr = NULL,
507 .readcnt = 0,
508 .readarr = NULL,
509 }};
510
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000511 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000512 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000513 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000514 return result;
515 }
516 /* Wait until the Write-In-Progress bit is cleared.
517 * This usually takes 1-85 s, so wait in 1 s steps.
518 */
519 /* FIXME: We assume spi_read_status_register will never fail. */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000520 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000521 programmer_delay(1000 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000522 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000523 return 0;
524}
525
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000526int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
527 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000528{
529 int result;
530 struct spi_command cmds[] = {
531 {
532 .writecnt = JEDEC_WREN_OUTSIZE,
533 .writearr = (const unsigned char[]){ JEDEC_WREN },
534 .readcnt = 0,
535 .readarr = NULL,
536 }, {
537 .writecnt = JEDEC_BE_52_OUTSIZE,
538 .writearr = (const unsigned char[]){
539 JEDEC_BE_52,
540 (addr >> 16) & 0xff,
541 (addr >> 8) & 0xff,
542 (addr & 0xff)
543 },
544 .readcnt = 0,
545 .readarr = NULL,
546 }, {
547 .writecnt = 0,
548 .writearr = NULL,
549 .readcnt = 0,
550 .readarr = NULL,
551 }};
552
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000553 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000554 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000555 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000556 __func__, addr);
557 return result;
558 }
559 /* Wait until the Write-In-Progress bit is cleared.
560 * This usually takes 100-4000 ms, so wait in 100 ms steps.
561 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000562 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000563 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000564 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000565 return 0;
566}
567
568/* Block size is usually
569 * 64k for Macronix
570 * 32k for SST
571 * 4-32k non-uniform for EON
572 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000573int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
574 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000575{
576 int result;
577 struct spi_command cmds[] = {
578 {
579 .writecnt = JEDEC_WREN_OUTSIZE,
580 .writearr = (const unsigned char[]){ JEDEC_WREN },
581 .readcnt = 0,
582 .readarr = NULL,
583 }, {
584 .writecnt = JEDEC_BE_D8_OUTSIZE,
585 .writearr = (const unsigned char[]){
586 JEDEC_BE_D8,
587 (addr >> 16) & 0xff,
588 (addr >> 8) & 0xff,
589 (addr & 0xff)
590 },
591 .readcnt = 0,
592 .readarr = NULL,
593 }, {
594 .writecnt = 0,
595 .writearr = NULL,
596 .readcnt = 0,
597 .readarr = NULL,
598 }};
599
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000600 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000601 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000602 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000603 __func__, addr);
604 return result;
605 }
606 /* Wait until the Write-In-Progress bit is cleared.
607 * This usually takes 100-4000 ms, so wait in 100 ms steps.
608 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000609 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000610 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000611 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000612 return 0;
613}
614
615/* Block size is usually
616 * 4k for PMC
617 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000618int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
619 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000620{
621 int result;
622 struct spi_command cmds[] = {
623 {
624 .writecnt = JEDEC_WREN_OUTSIZE,
625 .writearr = (const unsigned char[]){ JEDEC_WREN },
626 .readcnt = 0,
627 .readarr = NULL,
628 }, {
629 .writecnt = JEDEC_BE_D7_OUTSIZE,
630 .writearr = (const unsigned char[]){
631 JEDEC_BE_D7,
632 (addr >> 16) & 0xff,
633 (addr >> 8) & 0xff,
634 (addr & 0xff)
635 },
636 .readcnt = 0,
637 .readarr = NULL,
638 }, {
639 .writecnt = 0,
640 .writearr = NULL,
641 .readcnt = 0,
642 .readarr = NULL,
643 }};
644
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000645 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000646 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000647 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000648 __func__, addr);
649 return result;
650 }
651 /* Wait until the Write-In-Progress bit is cleared.
652 * This usually takes 100-4000 ms, so wait in 100 ms steps.
653 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000654 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000655 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000656 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000657 return 0;
658}
659
Sean Nelson14ba6682010-02-26 05:48:29 +0000660/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000661int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
662 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000663{
664 int result;
665 struct spi_command cmds[] = {
666 {
667 .writecnt = JEDEC_WREN_OUTSIZE,
668 .writearr = (const unsigned char[]){ JEDEC_WREN },
669 .readcnt = 0,
670 .readarr = NULL,
671 }, {
672 .writecnt = JEDEC_SE_OUTSIZE,
673 .writearr = (const unsigned char[]){
674 JEDEC_SE,
675 (addr >> 16) & 0xff,
676 (addr >> 8) & 0xff,
677 (addr & 0xff)
678 },
679 .readcnt = 0,
680 .readarr = NULL,
681 }, {
682 .writecnt = 0,
683 .writearr = NULL,
684 .readcnt = 0,
685 .readarr = NULL,
686 }};
687
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000688 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000689 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000690 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000691 __func__, addr);
692 return result;
693 }
694 /* Wait until the Write-In-Progress bit is cleared.
695 * This usually takes 15-800 ms, so wait in 10 ms steps.
696 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000697 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000698 programmer_delay(10 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000699 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000700 return 0;
701}
702
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000703int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
704 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000705{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000706 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000707 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000708 __func__);
709 return -1;
710 }
711 return spi_chip_erase_60(flash);
712}
713
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000714int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
715 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000716{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000717 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000718 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000719 __func__);
720 return -1;
721 }
722 return spi_chip_erase_c7(flash);
723}
724
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000725erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode)
726{
727 switch(opcode){
728 case 0xff:
729 case 0x00:
730 /* Not specified, assuming "not supported". */
731 return NULL;
732 case 0x20:
733 return &spi_block_erase_20;
734 case 0x52:
735 return &spi_block_erase_52;
736 case 0x60:
737 return &spi_block_erase_60;
738 case 0xc7:
739 return &spi_block_erase_c7;
740 case 0xd7:
741 return &spi_block_erase_d7;
742 case 0xd8:
743 return &spi_block_erase_d8;
744 default:
745 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
746 "this at flashrom@flashrom.org\n", __func__, opcode);
747 return NULL;
748 }
749}
750
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000751int spi_write_status_enable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000752{
Mathias Krausea60faab2011-01-17 07:50:42 +0000753 static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Sean Nelson14ba6682010-02-26 05:48:29 +0000754 int result;
755
756 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000757 result = spi_send_command(flash, sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000758
759 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000760 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000761
762 return result;
763}
764
765/*
766 * This is according the SST25VF016 datasheet, who knows it is more
767 * generic that this...
768 */
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000769static int spi_write_status_register_flag(struct flashctx *flash, int status, const unsigned char enable_opcode)
Sean Nelson14ba6682010-02-26 05:48:29 +0000770{
771 int result;
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000772 int i = 0;
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000773 /*
774 * WRSR requires either EWSR or WREN depending on chip type.
775 * The code below relies on the fact hat EWSR and WREN have the same
776 * INSIZE and OUTSIZE.
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000777 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000778 struct spi_command cmds[] = {
779 {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000780 .writecnt = JEDEC_WREN_OUTSIZE,
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000781 .writearr = (const unsigned char[]){ enable_opcode },
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000782 .readcnt = 0,
783 .readarr = NULL,
784 }, {
785 .writecnt = JEDEC_WRSR_OUTSIZE,
786 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
787 .readcnt = 0,
788 .readarr = NULL,
789 }, {
790 .writecnt = 0,
791 .writearr = NULL,
792 .readcnt = 0,
793 .readarr = NULL,
794 }};
795
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000796 result = spi_send_multicommand(flash, cmds);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000797 if (result) {
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000798 msg_cerr("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000799 /* No point in waiting for the command to complete if execution
800 * failed.
801 */
802 return result;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000803 }
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000804 /* WRSR performs a self-timed erase before the changes take effect.
805 * This may take 50-85 ms in most cases, and some chips apparently
806 * allow running RDSR only once. Therefore pick an initial delay of
807 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
808 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000809 programmer_delay(100 * 1000);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000810 while (spi_read_status_register(flash) & SPI_SR_WIP) {
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000811 if (++i > 490) {
812 msg_cerr("Error: WIP bit after WRSR never cleared\n");
813 return TIMEOUT_ERROR;
814 }
815 programmer_delay(10 * 1000);
816 }
817 return 0;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000818}
819
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000820int spi_write_status_register(struct flashctx *flash, int status)
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000821{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000822 int feature_bits = flash->chip->feature_bits;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000823 int ret = 1;
824
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000825 if (!(feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000826 msg_cdbg("Missing status register write definition, assuming "
827 "EWSR is needed\n");
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000828 feature_bits |= FEATURE_WRSR_EWSR;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000829 }
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000830 if (feature_bits & FEATURE_WRSR_WREN)
831 ret = spi_write_status_register_flag(flash, status, JEDEC_WREN);
832 if (ret && (feature_bits & FEATURE_WRSR_EWSR))
833 ret = spi_write_status_register_flag(flash, status, JEDEC_EWSR);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000834 return ret;
835}
836
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000837int spi_byte_program(struct flashctx *flash, unsigned int addr,
838 uint8_t databyte)
Sean Nelson14ba6682010-02-26 05:48:29 +0000839{
840 int result;
841 struct spi_command cmds[] = {
842 {
843 .writecnt = JEDEC_WREN_OUTSIZE,
844 .writearr = (const unsigned char[]){ JEDEC_WREN },
845 .readcnt = 0,
846 .readarr = NULL,
847 }, {
848 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
849 .writearr = (const unsigned char[]){
850 JEDEC_BYTE_PROGRAM,
851 (addr >> 16) & 0xff,
852 (addr >> 8) & 0xff,
853 (addr & 0xff),
854 databyte
855 },
856 .readcnt = 0,
857 .readarr = NULL,
858 }, {
859 .writecnt = 0,
860 .writearr = NULL,
861 .readcnt = 0,
862 .readarr = NULL,
863 }};
864
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000865 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000866 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000867 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000868 __func__, addr);
869 }
870 return result;
871}
872
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000873int spi_nbyte_program(struct flashctx *flash, unsigned int addr, uint8_t *bytes,
874 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000875{
876 int result;
877 /* FIXME: Switch to malloc based on len unless that kills speed. */
878 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
879 JEDEC_BYTE_PROGRAM,
880 (addr >> 16) & 0xff,
881 (addr >> 8) & 0xff,
882 (addr >> 0) & 0xff,
883 };
884 struct spi_command cmds[] = {
885 {
886 .writecnt = JEDEC_WREN_OUTSIZE,
887 .writearr = (const unsigned char[]){ JEDEC_WREN },
888 .readcnt = 0,
889 .readarr = NULL,
890 }, {
891 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
892 .writearr = cmd,
893 .readcnt = 0,
894 .readarr = NULL,
895 }, {
896 .writecnt = 0,
897 .writearr = NULL,
898 .readcnt = 0,
899 .readarr = NULL,
900 }};
901
902 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000903 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000904 return 1;
905 }
906 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000907 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000908 return 1;
909 }
910
911 memcpy(&cmd[4], bytes, len);
912
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000913 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000914 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000915 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000916 __func__, addr);
917 }
918 return result;
919}
920
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000921/* A generic brute-force block protection disable works like this:
922 * Write 0x00 to the status register. Check if any locks are still set (that
923 * part is chip specific). Repeat once.
924 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000925int spi_disable_blockprotect(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000926{
927 uint8_t status;
928 int result;
929
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000930 status = spi_read_status_register(flash);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000931 /* If block protection is disabled, stop here. */
932 if ((status & 0x3c) == 0)
933 return 0;
934
Stefan Tauner87fbb772012-08-02 23:56:49 +0000935 msg_cdbg("Some block protection in effect, disabling... ");
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000936 result = spi_write_status_register(flash, status & ~0x3c);
937 if (result) {
Stefan Tauner87fbb772012-08-02 23:56:49 +0000938 msg_cerr("spi_write_status_register failed.\n");
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000939 return result;
940 }
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000941 status = spi_read_status_register(flash);
Sean Nelson14ba6682010-02-26 05:48:29 +0000942 if ((status & 0x3c) != 0) {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000943 msg_cerr("Block protection could not be disabled!\n");
944 return 1;
945 }
Stefan Tauner87fbb772012-08-02 23:56:49 +0000946 msg_cdbg("done.\n");
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000947 return 0;
948}
949
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000950int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
951 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000952{
953 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
954 JEDEC_READ,
955 (address >> 16) & 0xff,
956 (address >> 8) & 0xff,
957 (address >> 0) & 0xff,
958 };
959
960 /* Send Read */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000961 return spi_send_command(flash, sizeof(cmd), len, cmd, bytes);
Sean Nelson14ba6682010-02-26 05:48:29 +0000962}
963
964/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000965 * Read a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000966 * FIXME: Use the chunk code from Michael Karcher instead.
Sean Nelson14ba6682010-02-26 05:48:29 +0000967 * Each page is read separately in chunks with a maximum size of chunksize.
968 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000969int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
970 unsigned int len, unsigned int chunksize)
Sean Nelson14ba6682010-02-26 05:48:29 +0000971{
972 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000973 unsigned int i, j, starthere, lenhere, toread;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000974 unsigned int page_size = flash->chip->page_size;
Sean Nelson14ba6682010-02-26 05:48:29 +0000975
976 /* Warning: This loop has a very unusual condition and body.
977 * The loop needs to go through each page with at least one affected
978 * byte. The lowest page number is (start / page_size) since that
979 * division rounds down. The highest page number we want is the page
980 * where the last byte of the range lives. That last byte has the
981 * address (start + len - 1), thus the highest page number is
982 * (start + len - 1) / page_size. Since we want to include that last
983 * page as well, the loop condition uses <=.
984 */
985 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
986 /* Byte position of the first byte in the range in this page. */
987 /* starthere is an offset to the base address of the chip. */
988 starthere = max(start, i * page_size);
989 /* Length of bytes in the range in this page. */
990 lenhere = min(start + len, (i + 1) * page_size) - starthere;
991 for (j = 0; j < lenhere; j += chunksize) {
992 toread = min(chunksize, lenhere - j);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000993 rc = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread);
Sean Nelson14ba6682010-02-26 05:48:29 +0000994 if (rc)
995 break;
996 }
997 if (rc)
998 break;
999 }
1000
1001 return rc;
1002}
1003
1004/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001005 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001006 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001007 * Each page is written separately in chunks with a maximum size of chunksize.
1008 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001009int spi_write_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
1010 unsigned int len, unsigned int chunksize)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001011{
1012 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001013 unsigned int i, j, starthere, lenhere, towrite;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001014 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001015 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001016 * spi_chip_write_256 have page_size set to max_writechunk_size, so
1017 * we're OK for now.
1018 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001019 unsigned int page_size = flash->chip->page_size;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001020
1021 /* Warning: This loop has a very unusual condition and body.
1022 * The loop needs to go through each page with at least one affected
1023 * byte. The lowest page number is (start / page_size) since that
1024 * division rounds down. The highest page number we want is the page
1025 * where the last byte of the range lives. That last byte has the
1026 * address (start + len - 1), thus the highest page number is
1027 * (start + len - 1) / page_size. Since we want to include that last
1028 * page as well, the loop condition uses <=.
1029 */
1030 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1031 /* Byte position of the first byte in the range in this page. */
1032 /* starthere is an offset to the base address of the chip. */
1033 starthere = max(start, i * page_size);
1034 /* Length of bytes in the range in this page. */
1035 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1036 for (j = 0; j < lenhere; j += chunksize) {
1037 towrite = min(chunksize, lenhere - j);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001038 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001039 if (rc)
1040 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001041 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001042 programmer_delay(10);
1043 }
1044 if (rc)
1045 break;
1046 }
1047
1048 return rc;
1049}
1050
1051/*
Sean Nelson14ba6682010-02-26 05:48:29 +00001052 * Program chip using byte programming. (SLOW!)
1053 * This is for chips which can only handle one byte writes
1054 * and for chips where memory mapped programming is impossible
1055 * (e.g. due to size constraints in IT87* for over 512 kB)
1056 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001057/* real chunksize is 1, logical chunksize is 1 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001058int spi_chip_write_1(struct flashctx *flash, uint8_t *buf, unsigned int start,
1059 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +00001060{
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001061 unsigned int i;
1062 int result = 0;
Sean Nelson14ba6682010-02-26 05:48:29 +00001063
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001064 for (i = start; i < start + len; i++) {
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001065 result = spi_byte_program(flash, i, buf[i - start]);
Sean Nelson14ba6682010-02-26 05:48:29 +00001066 if (result)
1067 return 1;
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001068 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +00001069 programmer_delay(10);
1070 }
1071
1072 return 0;
1073}
1074
Nico Huber7bca1262012-06-15 22:28:12 +00001075int default_spi_write_aai(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001076{
1077 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +00001078 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001079 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1080 JEDEC_AAI_WORD_PROGRAM,
1081 };
1082 struct spi_command cmds[] = {
1083 {
1084 .writecnt = JEDEC_WREN_OUTSIZE,
1085 .writearr = (const unsigned char[]){ JEDEC_WREN },
1086 .readcnt = 0,
1087 .readarr = NULL,
1088 }, {
1089 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1090 .writearr = (const unsigned char[]){
1091 JEDEC_AAI_WORD_PROGRAM,
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001092 (start >> 16) & 0xff,
1093 (start >> 8) & 0xff,
1094 (start & 0xff),
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001095 buf[0],
1096 buf[1]
1097 },
1098 .readcnt = 0,
1099 .readarr = NULL,
1100 }, {
1101 .writecnt = 0,
1102 .writearr = NULL,
1103 .readcnt = 0,
1104 .readarr = NULL,
1105 }};
Sean Nelson14ba6682010-02-26 05:48:29 +00001106
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001107 switch (flash->pgm->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001108#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001109#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001110 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +00001111 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001112 msg_perr("%s: impossible with this SPI controller,"
Sean Nelson14ba6682010-02-26 05:48:29 +00001113 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001114 return spi_chip_write_1(flash, buf, start, len);
Sean Nelson14ba6682010-02-26 05:48:29 +00001115#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001116#endif
Sean Nelson14ba6682010-02-26 05:48:29 +00001117 default:
1118 break;
1119 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001120
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001121 /* The even start address and even length requirements can be either
1122 * honored outside this function, or we can call spi_byte_program
1123 * for the first and/or last byte and use AAI for the rest.
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001124 * FIXME: Move this to generic code.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001125 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001126 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001127 if (start % 2) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001128 msg_cerr("%s: start address not even! Please report a bug at "
1129 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001130 if (spi_chip_write_1(flash, buf, start, start % 2))
1131 return SPI_GENERIC_ERROR;
1132 pos += start % 2;
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001133 cmds[1].writearr = (const unsigned char[]){
1134 JEDEC_AAI_WORD_PROGRAM,
1135 (pos >> 16) & 0xff,
1136 (pos >> 8) & 0xff,
1137 (pos & 0xff),
1138 buf[pos - start],
1139 buf[pos - start + 1]
1140 };
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001141 /* Do not return an error for now. */
1142 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001143 }
1144 /* The data sheet requires total AAI write length to be even. */
1145 if (len % 2) {
1146 msg_cerr("%s: total write length not even! Please report a "
1147 "bug at flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001148 /* Do not return an error for now. */
1149 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001150 }
1151
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001152
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001153 result = spi_send_multicommand(flash, cmds);
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001154 if (result) {
1155 msg_cerr("%s failed during start command execution\n",
1156 __func__);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001157 /* FIXME: Should we send WRDI here as well to make sure the chip
1158 * is not in AAI mode?
1159 */
Sean Nelson14ba6682010-02-26 05:48:29 +00001160 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +00001161 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001162 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001163 programmer_delay(10);
1164
1165 /* We already wrote 2 bytes in the multicommand step. */
1166 pos += 2;
1167
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001168 /* Are there at least two more bytes to write? */
1169 while (pos < start + len - 1) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001170 cmd[1] = buf[pos++ - start];
1171 cmd[2] = buf[pos++ - start];
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001172 spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0,
1173 cmd, NULL);
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001174 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001175 programmer_delay(10);
1176 }
1177
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001178 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
1179 * other non-AAI command.
1180 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001181 spi_write_disable(flash);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001182
1183 /* Write remaining byte (if any). */
1184 if (pos < start + len) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001185 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001186 return SPI_GENERIC_ERROR;
1187 pos += pos % 2;
1188 }
1189
Sean Nelson14ba6682010-02-26 05:48:29 +00001190 return 0;
1191}