blob: c57f2e30e2ca34d73b3c1fab435d44d8d2790919 [file] [log] [blame]
snelson8913d082010-02-26 05:48:29 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger39d159a2010-05-21 23:09:42 +00004 * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger
snelson8913d082010-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"
hailfinger428f6852010-07-27 22:41:39 +000029#include "programmer.h"
snelson8913d082010-02-26 05:48:29 +000030#include "spi.h"
31
snelson8913d082010-02-26 05:48:29 +000032static int spi_rdid(unsigned char *readarr, int bytes)
33{
krause2eb76212011-01-17 07:50:42 +000034 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
snelson8913d082010-02-26 05:48:29 +000035 int ret;
36 int i;
37
38 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
39 if (ret)
40 return ret;
snelsonfc007bb2010-03-24 23:14:32 +000041 msg_cspew("RDID returned");
snelson8913d082010-02-26 05:48:29 +000042 for (i = 0; i < bytes; i++)
snelsonfc007bb2010-03-24 23:14:32 +000043 msg_cspew(" 0x%02x", readarr[i]);
44 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000045 return 0;
46}
47
48static int spi_rems(unsigned char *readarr)
49{
50 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
51 uint32_t readaddr;
52 int ret;
53
54 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
55 if (ret == SPI_INVALID_ADDRESS) {
56 /* Find the lowest even address allowed for reads. */
57 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
58 cmd[1] = (readaddr >> 16) & 0xff,
59 cmd[2] = (readaddr >> 8) & 0xff,
60 cmd[3] = (readaddr >> 0) & 0xff,
61 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
62 }
63 if (ret)
64 return ret;
stefanct371e7e82011-07-07 19:56:58 +000065 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
snelson8913d082010-02-26 05:48:29 +000066 return 0;
67}
68
hailfinger59a83572010-05-28 17:07:57 +000069static int spi_res(unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000070{
71 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
72 uint32_t readaddr;
73 int ret;
hailfingercb0564e2010-06-20 10:39:33 +000074 int i;
snelson8913d082010-02-26 05:48:29 +000075
hailfinger59a83572010-05-28 17:07:57 +000076 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000077 if (ret == SPI_INVALID_ADDRESS) {
78 /* Find the lowest even address allowed for reads. */
79 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
80 cmd[1] = (readaddr >> 16) & 0xff,
81 cmd[2] = (readaddr >> 8) & 0xff,
82 cmd[3] = (readaddr >> 0) & 0xff,
hailfinger59a83572010-05-28 17:07:57 +000083 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000084 }
85 if (ret)
86 return ret;
hailfingercb0564e2010-06-20 10:39:33 +000087 msg_cspew("RES returned");
88 for (i = 0; i < bytes; i++)
89 msg_cspew(" 0x%02x", readarr[i]);
90 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000091 return 0;
92}
93
94int spi_write_enable(void)
95{
krause2eb76212011-01-17 07:50:42 +000096 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
snelson8913d082010-02-26 05:48:29 +000097 int result;
98
99 /* Send WREN (Write Enable) */
100 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
101
102 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000103 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000104
105 return result;
106}
107
108int spi_write_disable(void)
109{
krause2eb76212011-01-17 07:50:42 +0000110 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
snelson8913d082010-02-26 05:48:29 +0000111
112 /* Send WRDI (Write Disable) */
113 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
114}
115
David Hendricks7f7c7112012-10-11 17:15:48 -0700116static void rdid_get_ids(unsigned char *readarr,
117 int bytes, uint32_t *id1, uint32_t *id2)
snelson8913d082010-02-26 05:48:29 +0000118{
snelson8913d082010-02-26 05:48:29 +0000119 if (!oddparity(readarr[0]))
snelsonfc007bb2010-03-24 23:14:32 +0000120 msg_cdbg("RDID byte 0 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000121
hailfingercb0564e2010-06-20 10:39:33 +0000122 /* Check if this is a continuation vendor ID.
123 * FIXME: Handle continuation device IDs.
124 */
snelson8913d082010-02-26 05:48:29 +0000125 if (readarr[0] == 0x7f) {
126 if (!oddparity(readarr[1]))
snelsonfc007bb2010-03-24 23:14:32 +0000127 msg_cdbg("RDID byte 1 parity violation. ");
David Hendricks7f7c7112012-10-11 17:15:48 -0700128 *id1 = (readarr[0] << 8) | readarr[1];
129 *id2 = readarr[2];
snelson8913d082010-02-26 05:48:29 +0000130 if (bytes > 3) {
David Hendricks7f7c7112012-10-11 17:15:48 -0700131 *id2 <<= 8;
132 *id2 |= readarr[3];
snelson8913d082010-02-26 05:48:29 +0000133 }
134 } else {
David Hendricks7f7c7112012-10-11 17:15:48 -0700135 *id1 = readarr[0];
136 *id2 = (readarr[1] << 8) | readarr[2];
snelson8913d082010-02-26 05:48:29 +0000137 }
David Hendricks7f7c7112012-10-11 17:15:48 -0700138}
snelson8913d082010-02-26 05:48:29 +0000139
David Hendricks7f7c7112012-10-11 17:15:48 -0700140static int compare_id(struct flashchip *flash, uint32_t id1, uint32_t id2)
141{
142 msg_cdbg("id1 0x%02x, id2 0x%02x\n", id1, id2);
snelson8913d082010-02-26 05:48:29 +0000143
144 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
145 /* Print the status register to tell the
146 * user about possible write protection.
147 */
148 spi_prettyprint_status_register(flash);
149
150 return 1;
151 }
152
153 /* Test if this is a pure vendor match. */
154 if (id1 == flash->manufacture_id &&
155 GENERIC_DEVICE_ID == flash->model_id)
156 return 1;
157
158 /* Test if there is any vendor ID. */
159 if (GENERIC_MANUF_ID == flash->manufacture_id &&
160 id1 != 0xff)
161 return 1;
162
163 return 0;
164}
165
166int probe_spi_rdid(struct flashchip *flash)
167{
David Hendricks7f7c7112012-10-11 17:15:48 -0700168 unsigned char readarr[3];
169 static int cached = 0;
170 static uint32_t id1, id2;
171
172 if (!cached) {
173 if (spi_rdid(readarr, 3))
174 return 0;
175 rdid_get_ids(readarr, 3, &id1, &id2);
176 cached = 1;
177 }
178
179 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000180}
181
snelson8913d082010-02-26 05:48:29 +0000182int probe_spi_rdid4(struct flashchip *flash)
183{
David Hendricks7f7c7112012-10-11 17:15:48 -0700184 unsigned char readarr[4];
185 static uint32_t id1, id2;
186 static int cached = 0;
187
hailfingercb0564e2010-06-20 10:39:33 +0000188 /* Some SPI controllers do not support commands with writecnt=1 and
189 * readcnt=4.
190 */
mkarcherd264e9e2011-05-11 17:07:07 +0000191 switch (spi_programmer->type) {
hailfinger90c7d542010-05-31 15:27:27 +0000192#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000193#if defined(__i386__) || defined(__x86_64__)
hailfingercb0564e2010-06-20 10:39:33 +0000194 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +0000195 case SPI_CONTROLLER_WBSIO:
hailfingercb0564e2010-06-20 10:39:33 +0000196 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
hailfingercb0564e2010-06-20 10:39:33 +0000197 break;
snelson8913d082010-02-26 05:48:29 +0000198#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000199#endif
snelson8913d082010-02-26 05:48:29 +0000200 default:
David Hendricks7f7c7112012-10-11 17:15:48 -0700201 break;
snelson8913d082010-02-26 05:48:29 +0000202 }
203
David Hendricks7f7c7112012-10-11 17:15:48 -0700204 if (!cached) {
205 if (spi_rdid(readarr, 4))
206 return 0;
207 rdid_get_ids(readarr, 4, &id1, &id2);
208 cached = 1;
209 }
210 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000211}
212
213int probe_spi_rems(struct flashchip *flash)
214{
215 unsigned char readarr[JEDEC_REMS_INSIZE];
David Hendricks7f7c7112012-10-11 17:15:48 -0700216 static uint32_t id1, id2;
217 static int cached = 0;
snelson8913d082010-02-26 05:48:29 +0000218
David Hendricks7f7c7112012-10-11 17:15:48 -0700219 if (!cached) {
220 if (spi_rems(readarr)) {
221 return 0;
222 }
223
224 id1 = readarr[0];
225 id2 = readarr[1];
226 cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000227 }
snelson8913d082010-02-26 05:48:29 +0000228
David Hendricks7f7c7112012-10-11 17:15:48 -0700229 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000230}
231
hailfinger59a83572010-05-28 17:07:57 +0000232int probe_spi_res1(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +0000233{
krause2eb76212011-01-17 07:50:42 +0000234 static const unsigned char allff[] = {0xff, 0xff, 0xff};
235 static const unsigned char all00[] = {0x00, 0x00, 0x00};
snelson8913d082010-02-26 05:48:29 +0000236 unsigned char readarr[3];
237 uint32_t id2;
snelson8913d082010-02-26 05:48:29 +0000238
hailfinger59a83572010-05-28 17:07:57 +0000239 /* We only want one-byte RES if RDID and REMS are unusable. */
240
snelson8913d082010-02-26 05:48:29 +0000241 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
242 * 0x00 0x00 0x00. In that case, RES is pointless.
243 */
244 if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) &&
245 memcmp(readarr, all00, 3)) {
246 msg_cdbg("Ignoring RES in favour of RDID.\n");
247 return 0;
248 }
249 /* Check if REMS is usable and does not return 0xff 0xff or
250 * 0x00 0x00. In that case, RES is pointless.
251 */
252 if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
253 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
254 msg_cdbg("Ignoring RES in favour of REMS.\n");
255 return 0;
256 }
257
stefanct9e6b98a2011-05-28 02:37:14 +0000258 if (spi_res(readarr, 1)) {
snelson8913d082010-02-26 05:48:29 +0000259 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000260 }
snelson8913d082010-02-26 05:48:29 +0000261
snelson8913d082010-02-26 05:48:29 +0000262 id2 = readarr[0];
hailfinger59a83572010-05-28 17:07:57 +0000263
snelsonfc007bb2010-03-24 23:14:32 +0000264 msg_cdbg("%s: id 0x%x\n", __func__, id2);
hailfinger59a83572010-05-28 17:07:57 +0000265
stefanct20f99532011-05-28 22:59:05 +0000266 if (id2 != flash->model_id)
snelson8913d082010-02-26 05:48:29 +0000267 return 0;
268
269 /* Print the status register to tell the
270 * user about possible write protection.
271 */
272 spi_prettyprint_status_register(flash);
273 return 1;
274}
275
hailfinger59a83572010-05-28 17:07:57 +0000276int probe_spi_res2(struct flashchip *flash)
277{
278 unsigned char readarr[2];
279 uint32_t id1, id2;
280
stefanct9e6b98a2011-05-28 02:37:14 +0000281 if (spi_res(readarr, 2)) {
hailfinger59a83572010-05-28 17:07:57 +0000282 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000283 }
hailfinger59a83572010-05-28 17:07:57 +0000284
285 id1 = readarr[0];
286 id2 = readarr[1];
287
288 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
289
290 if (id1 != flash->manufacture_id || id2 != flash->model_id)
291 return 0;
292
293 /* Print the status register to tell the
294 * user about possible write protection.
295 */
296 spi_prettyprint_status_register(flash);
297 return 1;
298}
299
snelson8913d082010-02-26 05:48:29 +0000300uint8_t spi_read_status_register(void)
301{
krause2eb76212011-01-17 07:50:42 +0000302 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
snelson8913d082010-02-26 05:48:29 +0000303 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
304 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
305 int ret;
306
307 /* Read Status Register */
308 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
309 if (ret)
snelsonfc007bb2010-03-24 23:14:32 +0000310 msg_cerr("RDSR failed!\n");
snelson8913d082010-02-26 05:48:29 +0000311
312 return readarr[0];
313}
314
315/* Prettyprint the status register. Common definitions. */
hailfinger7533bc82011-05-19 00:06:06 +0000316void spi_prettyprint_status_register_welwip(uint8_t status)
hailfingerc33d4732010-07-29 13:09:18 +0000317{
318 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
319 "%sset\n", (status & (1 << 1)) ? "" : "not ");
320 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
321 "%sset\n", (status & (1 << 0)) ? "" : "not ");
322}
323
324/* Prettyprint the status register. Common definitions. */
hailfinger7533bc82011-05-19 00:06:06 +0000325void spi_prettyprint_status_register_bp3210(uint8_t status, int bp)
326{
327 switch (bp) {
328 /* Fall through. */
329 case 3:
330 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) "
331 "is %sset\n", (status & (1 << 5)) ? "" : "not ");
332 case 2:
333 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) "
334 "is %sset\n", (status & (1 << 4)) ? "" : "not ");
335 case 1:
336 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) "
337 "is %sset\n", (status & (1 << 3)) ? "" : "not ");
338 case 0:
339 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) "
340 "is %sset\n", (status & (1 << 2)) ? "" : "not ");
341 }
342}
343
344/* Prettyprint the status register. Unnamed bits. */
345void spi_prettyprint_status_register_bit(uint8_t status, int bit)
346{
347 msg_cdbg("Chip status register: Bit %i "
348 "is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
349}
350
hailfingerc33d4732010-07-29 13:09:18 +0000351static void spi_prettyprint_status_register_common(uint8_t status)
snelson8913d082010-02-26 05:48:29 +0000352{
hailfinger7533bc82011-05-19 00:06:06 +0000353 spi_prettyprint_status_register_bp3210(status, 3);
hailfingerc33d4732010-07-29 13:09:18 +0000354 spi_prettyprint_status_register_welwip(status);
snelson8913d082010-02-26 05:48:29 +0000355}
356
357/* Prettyprint the status register. Works for
358 * ST M25P series
359 * MX MX25L series
360 */
361void spi_prettyprint_status_register_st_m25p(uint8_t status)
362{
snelsonfc007bb2010-03-24 23:14:32 +0000363 msg_cdbg("Chip status register: Status Register Write Disable "
snelson8913d082010-02-26 05:48:29 +0000364 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000365 msg_cdbg("Chip status register: Bit 6 is "
snelson8913d082010-02-26 05:48:29 +0000366 "%sset\n", (status & (1 << 6)) ? "" : "not ");
367 spi_prettyprint_status_register_common(status);
368}
369
370void spi_prettyprint_status_register_sst25(uint8_t status)
371{
snelsonfc007bb2010-03-24 23:14:32 +0000372 msg_cdbg("Chip status register: Block Protect Write Disable "
snelson8913d082010-02-26 05:48:29 +0000373 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000374 msg_cdbg("Chip status register: Auto Address Increment Programming "
snelson8913d082010-02-26 05:48:29 +0000375 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
376 spi_prettyprint_status_register_common(status);
377}
378
379/* Prettyprint the status register. Works for
380 * SST 25VF016
381 */
382void spi_prettyprint_status_register_sst25vf016(uint8_t status)
383{
krause2eb76212011-01-17 07:50:42 +0000384 static const char *const bpt[] = {
snelson8913d082010-02-26 05:48:29 +0000385 "none",
386 "1F0000H-1FFFFFH",
387 "1E0000H-1FFFFFH",
388 "1C0000H-1FFFFFH",
389 "180000H-1FFFFFH",
390 "100000H-1FFFFFH",
391 "all", "all"
392 };
393 spi_prettyprint_status_register_sst25(status);
snelsonfc007bb2010-03-24 23:14:32 +0000394 msg_cdbg("Resulting block protection : %s\n",
snelson8913d082010-02-26 05:48:29 +0000395 bpt[(status & 0x1c) >> 2]);
396}
397
398void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
399{
krause2eb76212011-01-17 07:50:42 +0000400 static const char *const bpt[] = {
snelson8913d082010-02-26 05:48:29 +0000401 "none",
402 "0x70000-0x7ffff",
403 "0x60000-0x7ffff",
404 "0x40000-0x7ffff",
405 "all blocks", "all blocks", "all blocks", "all blocks"
406 };
407 spi_prettyprint_status_register_sst25(status);
snelsonfc007bb2010-03-24 23:14:32 +0000408 msg_cdbg("Resulting block protection : %s\n",
snelson8913d082010-02-26 05:48:29 +0000409 bpt[(status & 0x1c) >> 2]);
410}
411
hailfinger7533bc82011-05-19 00:06:06 +0000412int spi_prettyprint_status_register(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +0000413{
414 uint8_t status;
415
416 status = spi_read_status_register();
snelsonfc007bb2010-03-24 23:14:32 +0000417 msg_cdbg("Chip status register is %02x\n", status);
snelson8913d082010-02-26 05:48:29 +0000418 switch (flash->manufacture_id) {
419 case ST_ID:
420 if (((flash->model_id & 0xff00) == 0x2000) ||
421 ((flash->model_id & 0xff00) == 0x2500))
422 spi_prettyprint_status_register_st_m25p(status);
423 break;
mhmd3c80cd2010-09-15 23:31:03 +0000424 case MACRONIX_ID:
snelson8913d082010-02-26 05:48:29 +0000425 if ((flash->model_id & 0xff00) == 0x2000)
426 spi_prettyprint_status_register_st_m25p(status);
427 break;
428 case SST_ID:
429 switch (flash->model_id) {
430 case 0x2541:
431 spi_prettyprint_status_register_sst25vf016(status);
432 break;
433 case 0x8d:
434 case 0x258d:
435 spi_prettyprint_status_register_sst25vf040b(status);
436 break;
437 default:
438 spi_prettyprint_status_register_sst25(status);
439 break;
440 }
441 break;
442 }
hailfinger7533bc82011-05-19 00:06:06 +0000443 return 0;
snelson8913d082010-02-26 05:48:29 +0000444}
445
446int spi_chip_erase_60(struct flashchip *flash)
447{
448 int result;
449 struct spi_command cmds[] = {
450 {
451 .writecnt = JEDEC_WREN_OUTSIZE,
452 .writearr = (const unsigned char[]){ JEDEC_WREN },
453 .readcnt = 0,
454 .readarr = NULL,
455 }, {
456 .writecnt = JEDEC_CE_60_OUTSIZE,
457 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
458 .readcnt = 0,
459 .readarr = NULL,
460 }, {
461 .writecnt = 0,
462 .writearr = NULL,
463 .readcnt = 0,
464 .readarr = NULL,
465 }};
466
snelson8913d082010-02-26 05:48:29 +0000467 result = spi_send_multicommand(cmds);
468 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000469 msg_cerr("%s failed during command execution\n",
snelson8913d082010-02-26 05:48:29 +0000470 __func__);
471 return result;
472 }
473 /* Wait until the Write-In-Progress bit is cleared.
474 * This usually takes 1-85 s, so wait in 1 s steps.
475 */
476 /* FIXME: We assume spi_read_status_register will never fail. */
477 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
478 programmer_delay(1000 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000479 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000480 return 0;
481}
482
483int spi_chip_erase_c7(struct flashchip *flash)
484{
485 int result;
486 struct spi_command cmds[] = {
487 {
488 .writecnt = JEDEC_WREN_OUTSIZE,
489 .writearr = (const unsigned char[]){ JEDEC_WREN },
490 .readcnt = 0,
491 .readarr = NULL,
492 }, {
493 .writecnt = JEDEC_CE_C7_OUTSIZE,
494 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
495 .readcnt = 0,
496 .readarr = NULL,
497 }, {
498 .writecnt = 0,
499 .writearr = NULL,
500 .readcnt = 0,
501 .readarr = NULL,
502 }};
503
snelson8913d082010-02-26 05:48:29 +0000504 result = spi_send_multicommand(cmds);
505 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000506 msg_cerr("%s failed during command execution\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000507 return result;
508 }
509 /* Wait until the Write-In-Progress bit is cleared.
510 * This usually takes 1-85 s, so wait in 1 s steps.
511 */
512 /* FIXME: We assume spi_read_status_register will never fail. */
513 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
514 programmer_delay(1000 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000515 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000516 return 0;
517}
518
snelson8913d082010-02-26 05:48:29 +0000519int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
520{
521 int result;
522 struct spi_command cmds[] = {
523 {
524 .writecnt = JEDEC_WREN_OUTSIZE,
525 .writearr = (const unsigned char[]){ JEDEC_WREN },
526 .readcnt = 0,
527 .readarr = NULL,
528 }, {
529 .writecnt = JEDEC_BE_52_OUTSIZE,
530 .writearr = (const unsigned char[]){
531 JEDEC_BE_52,
532 (addr >> 16) & 0xff,
533 (addr >> 8) & 0xff,
534 (addr & 0xff)
535 },
536 .readcnt = 0,
537 .readarr = NULL,
538 }, {
539 .writecnt = 0,
540 .writearr = NULL,
541 .readcnt = 0,
542 .readarr = NULL,
543 }};
544
545 result = spi_send_multicommand(cmds);
546 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000547 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000548 __func__, addr);
549 return result;
550 }
551 /* Wait until the Write-In-Progress bit is cleared.
552 * This usually takes 100-4000 ms, so wait in 100 ms steps.
553 */
554 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
555 programmer_delay(100 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000556 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000557 return 0;
558}
559
560/* Block size is usually
561 * 64k for Macronix
562 * 32k for SST
563 * 4-32k non-uniform for EON
564 */
565int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
566{
567 int result;
568 struct spi_command cmds[] = {
569 {
570 .writecnt = JEDEC_WREN_OUTSIZE,
571 .writearr = (const unsigned char[]){ JEDEC_WREN },
572 .readcnt = 0,
573 .readarr = NULL,
574 }, {
575 .writecnt = JEDEC_BE_D8_OUTSIZE,
576 .writearr = (const unsigned char[]){
577 JEDEC_BE_D8,
578 (addr >> 16) & 0xff,
579 (addr >> 8) & 0xff,
580 (addr & 0xff)
581 },
582 .readcnt = 0,
583 .readarr = NULL,
584 }, {
585 .writecnt = 0,
586 .writearr = NULL,
587 .readcnt = 0,
588 .readarr = NULL,
589 }};
590
591 result = spi_send_multicommand(cmds);
592 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000593 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000594 __func__, addr);
595 return result;
596 }
597 /* Wait until the Write-In-Progress bit is cleared.
598 * This usually takes 100-4000 ms, so wait in 100 ms steps.
599 */
600 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
601 programmer_delay(100 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000602 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000603 return 0;
604}
605
606/* Block size is usually
607 * 4k for PMC
608 */
609int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
610{
611 int result;
612 struct spi_command cmds[] = {
613 {
614 .writecnt = JEDEC_WREN_OUTSIZE,
615 .writearr = (const unsigned char[]){ JEDEC_WREN },
616 .readcnt = 0,
617 .readarr = NULL,
618 }, {
619 .writecnt = JEDEC_BE_D7_OUTSIZE,
620 .writearr = (const unsigned char[]){
621 JEDEC_BE_D7,
622 (addr >> 16) & 0xff,
623 (addr >> 8) & 0xff,
624 (addr & 0xff)
625 },
626 .readcnt = 0,
627 .readarr = NULL,
628 }, {
629 .writecnt = 0,
630 .writearr = NULL,
631 .readcnt = 0,
632 .readarr = NULL,
633 }};
634
635 result = spi_send_multicommand(cmds);
636 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000637 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000638 __func__, addr);
639 return result;
640 }
641 /* Wait until the Write-In-Progress bit is cleared.
642 * This usually takes 100-4000 ms, so wait in 100 ms steps.
643 */
644 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
645 programmer_delay(100 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000646 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000647 return 0;
648}
649
snelson8913d082010-02-26 05:48:29 +0000650/* Sector size is usually 4k, though Macronix eliteflash has 64k */
651int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
652{
653 int result;
654 struct spi_command cmds[] = {
655 {
656 .writecnt = JEDEC_WREN_OUTSIZE,
657 .writearr = (const unsigned char[]){ JEDEC_WREN },
658 .readcnt = 0,
659 .readarr = NULL,
660 }, {
661 .writecnt = JEDEC_SE_OUTSIZE,
662 .writearr = (const unsigned char[]){
663 JEDEC_SE,
664 (addr >> 16) & 0xff,
665 (addr >> 8) & 0xff,
666 (addr & 0xff)
667 },
668 .readcnt = 0,
669 .readarr = NULL,
670 }, {
671 .writecnt = 0,
672 .writearr = NULL,
673 .readcnt = 0,
674 .readarr = NULL,
675 }};
676
677 result = spi_send_multicommand(cmds);
Stefan Reinauercce56d52010-11-22 18:22:21 -0800678
snelson8913d082010-02-26 05:48:29 +0000679 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000680 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000681 __func__, addr);
682 return result;
683 }
684 /* Wait until the Write-In-Progress bit is cleared.
685 * This usually takes 15-800 ms, so wait in 10 ms steps.
686 */
687 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
688 programmer_delay(10 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000689 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000690 return 0;
691}
692
693int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
694{
695 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000696 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000697 __func__);
698 return -1;
699 }
700 return spi_chip_erase_60(flash);
701}
702
703int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
704{
705 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000706 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000707 __func__);
708 return -1;
709 }
710 return spi_chip_erase_c7(flash);
711}
712
713int spi_write_status_enable(void)
714{
krause2eb76212011-01-17 07:50:42 +0000715 static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
snelson8913d082010-02-26 05:48:29 +0000716 int result;
717
718 /* Send EWSR (Enable Write Status Register). */
719 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
720
721 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000722 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000723
724 return result;
725}
726
727/*
728 * This is according the SST25VF016 datasheet, who knows it is more
729 * generic that this...
730 */
David Hendricks2d6d1d52014-12-11 18:32:20 -0800731static int spi_write_status_register_ewsr(const struct flashchip *flash, int status)
snelson8913d082010-02-26 05:48:29 +0000732{
733 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000734 int i = 0;
snelson8913d082010-02-26 05:48:29 +0000735 struct spi_command cmds[] = {
736 {
hailfingerc33d4732010-07-29 13:09:18 +0000737 /* WRSR requires either EWSR or WREN depending on chip type. */
snelson8913d082010-02-26 05:48:29 +0000738 .writecnt = JEDEC_EWSR_OUTSIZE,
739 .writearr = (const unsigned char[]){ JEDEC_EWSR },
740 .readcnt = 0,
741 .readarr = NULL,
742 }, {
743 .writecnt = JEDEC_WRSR_OUTSIZE,
744 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
745 .readcnt = 0,
746 .readarr = NULL,
747 }, {
748 .writecnt = 0,
749 .writearr = NULL,
750 .readcnt = 0,
751 .readarr = NULL,
752 }};
753
754 result = spi_send_multicommand(cmds);
755 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000756 msg_cerr("%s failed during command execution\n",
snelson8913d082010-02-26 05:48:29 +0000757 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000758 /* No point in waiting for the command to complete if execution
759 * failed.
760 */
761 return result;
snelson8913d082010-02-26 05:48:29 +0000762 }
hailfingeree9ee132010-10-08 00:37:55 +0000763 /* WRSR performs a self-timed erase before the changes take effect.
764 * This may take 50-85 ms in most cases, and some chips apparently
765 * allow running RDSR only once. Therefore pick an initial delay of
766 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
767 */
hailfingerc33d4732010-07-29 13:09:18 +0000768 programmer_delay(100 * 1000);
hailfingeree9ee132010-10-08 00:37:55 +0000769 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
770 if (++i > 490) {
771 msg_cerr("Error: WIP bit after WRSR never cleared\n");
772 return TIMEOUT_ERROR;
773 }
774 programmer_delay(10 * 1000);
775 }
776 return 0;
snelson8913d082010-02-26 05:48:29 +0000777}
778
David Hendricks2d6d1d52014-12-11 18:32:20 -0800779int spi_write_status_register_wren(const struct flashchip *flash, int status)
hailfingerc33d4732010-07-29 13:09:18 +0000780{
781 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000782 int i = 0;
hailfingerc33d4732010-07-29 13:09:18 +0000783 struct spi_command cmds[] = {
784 {
785 /* WRSR requires either EWSR or WREN depending on chip type. */
786 .writecnt = JEDEC_WREN_OUTSIZE,
787 .writearr = (const unsigned char[]){ JEDEC_WREN },
788 .readcnt = 0,
789 .readarr = NULL,
790 }, {
791 .writecnt = JEDEC_WRSR_OUTSIZE,
792 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
793 .readcnt = 0,
794 .readarr = NULL,
795 }, {
796 .writecnt = 0,
797 .writearr = NULL,
798 .readcnt = 0,
799 .readarr = NULL,
800 }};
801
802 result = spi_send_multicommand(cmds);
803 if (result) {
804 msg_cerr("%s failed during command execution\n",
805 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000806 /* No point in waiting for the command to complete if execution
807 * failed.
808 */
809 return result;
hailfingerc33d4732010-07-29 13:09:18 +0000810 }
hailfingeree9ee132010-10-08 00:37:55 +0000811 /* WRSR performs a self-timed erase before the changes take effect.
812 * This may take 50-85 ms in most cases, and some chips apparently
813 * allow running RDSR only once. Therefore pick an initial delay of
814 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
815 */
hailfingerc33d4732010-07-29 13:09:18 +0000816 programmer_delay(100 * 1000);
hailfingeree9ee132010-10-08 00:37:55 +0000817 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
818 if (++i > 490) {
819 msg_cerr("Error: WIP bit after WRSR never cleared\n");
820 return TIMEOUT_ERROR;
821 }
822 programmer_delay(10 * 1000);
823 }
824 return 0;
hailfingerc33d4732010-07-29 13:09:18 +0000825}
826
David Hendricks2d6d1d52014-12-11 18:32:20 -0800827int spi_write_status_register(const struct flashchip *flash, int status)
hailfingerc33d4732010-07-29 13:09:18 +0000828{
829 int ret = 1;
830
hailfingerc33d4732010-07-29 13:09:18 +0000831 if (flash->feature_bits & FEATURE_WRSR_WREN)
832 ret = spi_write_status_register_wren(flash, status);
833 if (ret && (flash->feature_bits & FEATURE_WRSR_EWSR))
834 ret = spi_write_status_register_ewsr(flash, status);
835 return ret;
836}
837
stefanctc5eb8a92011-11-23 09:13:48 +0000838int spi_byte_program(unsigned int addr, uint8_t databyte)
snelson8913d082010-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
865 result = spi_send_multicommand(cmds);
866 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000867 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000868 __func__, addr);
869 }
870 return result;
871}
872
stefanctc5eb8a92011-11-23 09:13:48 +0000873int spi_nbyte_program(unsigned int addr, uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000874{
875 int result;
876 /* FIXME: Switch to malloc based on len unless that kills speed. */
877 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
878 JEDEC_BYTE_PROGRAM,
879 (addr >> 16) & 0xff,
880 (addr >> 8) & 0xff,
881 (addr >> 0) & 0xff,
882 };
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 - 1 + len,
891 .writearr = cmd,
892 .readcnt = 0,
893 .readarr = NULL,
894 }, {
895 .writecnt = 0,
896 .writearr = NULL,
897 .readcnt = 0,
898 .readarr = NULL,
899 }};
900
901 if (!len) {
snelsonfc007bb2010-03-24 23:14:32 +0000902 msg_cerr("%s called for zero-length write\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000903 return 1;
904 }
905 if (len > 256) {
snelsonfc007bb2010-03-24 23:14:32 +0000906 msg_cerr("%s called for too long a write\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000907 return 1;
908 }
909
910 memcpy(&cmd[4], bytes, len);
911
912 result = spi_send_multicommand(cmds);
913 if (result) {
David Hendricks1ed1d352011-11-23 17:54:37 -0800914 if (result != SPI_ACCESS_DENIED) {
915 msg_cerr("%s failed during command execution at address 0x%x\n",
916 __func__, addr);
917 }
snelson8913d082010-02-26 05:48:29 +0000918 }
919 return result;
920}
921
David Hendricksbf36f092010-11-02 23:39:29 -0700922int spi_restore_status(struct flashchip *flash, uint8_t status)
923{
924 msg_cdbg("restoring chip status (0x%02x)\n", status);
925 return spi_write_status_register(flash, status);
926}
927
hailfingerc33d4732010-07-29 13:09:18 +0000928/* A generic brute-force block protection disable works like this:
929 * Write 0x00 to the status register. Check if any locks are still set (that
930 * part is chip specific). Repeat once.
931 */
hailfingerb9560ee2010-07-14 20:21:22 +0000932int spi_disable_blockprotect(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +0000933{
934 uint8_t status;
935 int result;
936
937 status = spi_read_status_register();
hailfingerc33d4732010-07-29 13:09:18 +0000938 /* If block protection is disabled, stop here. */
939 if ((status & 0x3c) == 0)
940 return 0;
941
David Hendricksbf36f092010-11-02 23:39:29 -0700942 /* restore status register content upon exit */
943 register_chip_restore(spi_restore_status, flash, status);
944
hailfingerc33d4732010-07-29 13:09:18 +0000945 msg_cdbg("Some block protection in effect, disabling\n");
946 result = spi_write_status_register(flash, status & ~0x3c);
947 if (result) {
948 msg_cerr("spi_write_status_register failed\n");
949 return result;
950 }
951 status = spi_read_status_register();
snelson8913d082010-02-26 05:48:29 +0000952 if ((status & 0x3c) != 0) {
hailfingerc33d4732010-07-29 13:09:18 +0000953 msg_cerr("Block protection could not be disabled!\n");
954 return 1;
955 }
956 return 0;
957}
958
stefanctc5eb8a92011-11-23 09:13:48 +0000959int spi_nbyte_read(unsigned int address, uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000960{
961 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
962 JEDEC_READ,
963 (address >> 16) & 0xff,
964 (address >> 8) & 0xff,
965 (address >> 0) & 0xff,
966 };
967
968 /* Send Read */
969 return spi_send_command(sizeof(cmd), len, cmd, bytes);
970}
971
972/*
hailfinger39d159a2010-05-21 23:09:42 +0000973 * Read a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000974 * FIXME: Use the chunk code from Michael Karcher instead.
snelson8913d082010-02-26 05:48:29 +0000975 * Each page is read separately in chunks with a maximum size of chunksize.
976 */
stefanctc5eb8a92011-11-23 09:13:48 +0000977int spi_read_chunked(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
snelson8913d082010-02-26 05:48:29 +0000978{
David Hendricks1ed1d352011-11-23 17:54:37 -0800979 int rc = 0, chunk_status = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000980 unsigned int i, j, starthere, lenhere, toread;
981 unsigned int page_size = flash->page_size;
snelson8913d082010-02-26 05:48:29 +0000982
983 /* Warning: This loop has a very unusual condition and body.
984 * The loop needs to go through each page with at least one affected
985 * byte. The lowest page number is (start / page_size) since that
986 * division rounds down. The highest page number we want is the page
987 * where the last byte of the range lives. That last byte has the
988 * address (start + len - 1), thus the highest page number is
989 * (start + len - 1) / page_size. Since we want to include that last
990 * page as well, the loop condition uses <=.
991 */
992 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
993 /* Byte position of the first byte in the range in this page. */
994 /* starthere is an offset to the base address of the chip. */
995 starthere = max(start, i * page_size);
996 /* Length of bytes in the range in this page. */
997 lenhere = min(start + len, (i + 1) * page_size) - starthere;
998 for (j = 0; j < lenhere; j += chunksize) {
999 toread = min(chunksize, lenhere - j);
David Hendricks1ed1d352011-11-23 17:54:37 -08001000 chunk_status = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
1001 if (chunk_status) {
1002 if (ignore_error(chunk_status)) {
1003 /* fill this chunk with 0xff bytes and
1004 let caller know about the error */
1005 memset(buf + starthere - start + j, 0xff, toread);
1006 rc = chunk_status;
1007 chunk_status = 0;
1008 continue;
1009 } else {
1010 rc = chunk_status;
1011 break;
1012 }
1013 }
snelson8913d082010-02-26 05:48:29 +00001014 }
David Hendricks1ed1d352011-11-23 17:54:37 -08001015 if (chunk_status)
snelson8913d082010-02-26 05:48:29 +00001016 break;
1017 }
1018
1019 return rc;
1020}
1021
1022/*
hailfinger39d159a2010-05-21 23:09:42 +00001023 * Write a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +00001024 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +00001025 * Each page is written separately in chunks with a maximum size of chunksize.
1026 */
stefanctc5eb8a92011-11-23 09:13:48 +00001027int spi_write_chunked(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
hailfinger39d159a2010-05-21 23:09:42 +00001028{
1029 int rc = 0;
stefanctc5eb8a92011-11-23 09:13:48 +00001030 unsigned int i, j, starthere, lenhere, towrite;
hailfinger39d159a2010-05-21 23:09:42 +00001031 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
1032 * in struct flashchip to do this properly. All chips using
1033 * spi_chip_write_256 have page_size set to max_writechunk_size, so
1034 * we're OK for now.
1035 */
stefanctc5eb8a92011-11-23 09:13:48 +00001036 unsigned int page_size = flash->page_size;
hailfinger39d159a2010-05-21 23:09:42 +00001037
1038 /* Warning: This loop has a very unusual condition and body.
1039 * The loop needs to go through each page with at least one affected
1040 * byte. The lowest page number is (start / page_size) since that
1041 * division rounds down. The highest page number we want is the page
1042 * where the last byte of the range lives. That last byte has the
1043 * address (start + len - 1), thus the highest page number is
1044 * (start + len - 1) / page_size. Since we want to include that last
1045 * page as well, the loop condition uses <=.
1046 */
1047 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1048 /* Byte position of the first byte in the range in this page. */
1049 /* starthere is an offset to the base address of the chip. */
1050 starthere = max(start, i * page_size);
1051 /* Length of bytes in the range in this page. */
1052 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1053 for (j = 0; j < lenhere; j += chunksize) {
1054 towrite = min(chunksize, lenhere - j);
1055 rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite);
1056 if (rc)
1057 break;
1058 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1059 programmer_delay(10);
1060 }
1061 if (rc)
1062 break;
1063 }
1064
1065 return rc;
1066}
1067
1068/*
snelson8913d082010-02-26 05:48:29 +00001069 * Program chip using byte programming. (SLOW!)
1070 * This is for chips which can only handle one byte writes
1071 * and for chips where memory mapped programming is impossible
1072 * (e.g. due to size constraints in IT87* for over 512 kB)
1073 */
hailfingerc7d06c62010-07-14 16:19:05 +00001074/* real chunksize is 1, logical chunksize is 1 */
stefanctc5eb8a92011-11-23 09:13:48 +00001075int spi_chip_write_1(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len)
snelson8913d082010-02-26 05:48:29 +00001076{
stefanctc5eb8a92011-11-23 09:13:48 +00001077 unsigned int i;
1078 int result = 0;
snelson8913d082010-02-26 05:48:29 +00001079
hailfingerc7d06c62010-07-14 16:19:05 +00001080 for (i = start; i < start + len; i++) {
hailfingerdef852d2010-10-27 22:07:11 +00001081 result = spi_byte_program(i, buf[i - start]);
snelson8913d082010-02-26 05:48:29 +00001082 if (result)
1083 return 1;
1084 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1085 programmer_delay(10);
1086 }
1087
1088 return 0;
1089}
1090
stefanctc5eb8a92011-11-23 09:13:48 +00001091int spi_aai_write(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +00001092{
1093 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +00001094 int result;
hailfinger19db0922010-06-20 10:41:35 +00001095 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1096 JEDEC_AAI_WORD_PROGRAM,
1097 };
1098 struct spi_command cmds[] = {
1099 {
1100 .writecnt = JEDEC_WREN_OUTSIZE,
1101 .writearr = (const unsigned char[]){ JEDEC_WREN },
1102 .readcnt = 0,
1103 .readarr = NULL,
1104 }, {
1105 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1106 .writearr = (const unsigned char[]){
1107 JEDEC_AAI_WORD_PROGRAM,
hailfingerc7d06c62010-07-14 16:19:05 +00001108 (start >> 16) & 0xff,
1109 (start >> 8) & 0xff,
1110 (start & 0xff),
hailfinger19db0922010-06-20 10:41:35 +00001111 buf[0],
1112 buf[1]
1113 },
1114 .readcnt = 0,
1115 .readarr = NULL,
1116 }, {
1117 .writecnt = 0,
1118 .writearr = NULL,
1119 .readcnt = 0,
1120 .readarr = NULL,
1121 }};
snelson8913d082010-02-26 05:48:29 +00001122
mkarcherd264e9e2011-05-11 17:07:07 +00001123 switch (spi_programmer->type) {
hailfinger90c7d542010-05-31 15:27:27 +00001124#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +00001125#if defined(__i386__) || defined(__x86_64__)
hailfinger19db0922010-06-20 10:41:35 +00001126 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +00001127 case SPI_CONTROLLER_WBSIO:
hailfingerc7d06c62010-07-14 16:19:05 +00001128 msg_perr("%s: impossible with this SPI controller,"
snelson8913d082010-02-26 05:48:29 +00001129 " degrading to byte program\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +00001130 return spi_chip_write_1(flash, buf, start, len);
snelson8913d082010-02-26 05:48:29 +00001131#endif
hailfinger324a9cc2010-05-26 01:45:41 +00001132#endif
snelson8913d082010-02-26 05:48:29 +00001133 default:
1134 break;
1135 }
hailfinger19db0922010-06-20 10:41:35 +00001136
hailfingerc7d06c62010-07-14 16:19:05 +00001137 /* The even start address and even length requirements can be either
1138 * honored outside this function, or we can call spi_byte_program
1139 * for the first and/or last byte and use AAI for the rest.
hailfinger71e1bd42010-10-13 22:26:56 +00001140 * FIXME: Move this to generic code.
hailfingerc7d06c62010-07-14 16:19:05 +00001141 */
hailfinger19db0922010-06-20 10:41:35 +00001142 /* The data sheet requires a start address with the low bit cleared. */
hailfingerc7d06c62010-07-14 16:19:05 +00001143 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +00001144 msg_cerr("%s: start address not even! Please report a bug at "
1145 "flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +00001146 if (spi_chip_write_1(flash, buf, start, start % 2))
1147 return SPI_GENERIC_ERROR;
1148 pos += start % 2;
hailfingerdef852d2010-10-27 22:07:11 +00001149 cmds[1].writearr = (const unsigned char[]){
1150 JEDEC_AAI_WORD_PROGRAM,
1151 (pos >> 16) & 0xff,
1152 (pos >> 8) & 0xff,
1153 (pos & 0xff),
1154 buf[pos - start],
1155 buf[pos - start + 1]
1156 };
hailfinger71e1bd42010-10-13 22:26:56 +00001157 /* Do not return an error for now. */
1158 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +00001159 }
1160 /* The data sheet requires total AAI write length to be even. */
1161 if (len % 2) {
1162 msg_cerr("%s: total write length not even! Please report a "
1163 "bug at flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +00001164 /* Do not return an error for now. */
1165 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +00001166 }
1167
hailfinger19db0922010-06-20 10:41:35 +00001168
1169 result = spi_send_multicommand(cmds);
1170 if (result) {
1171 msg_cerr("%s failed during start command execution\n",
1172 __func__);
hailfingerc7d06c62010-07-14 16:19:05 +00001173 /* FIXME: Should we send WRDI here as well to make sure the chip
1174 * is not in AAI mode?
1175 */
snelson8913d082010-02-26 05:48:29 +00001176 return result;
snelson8913d082010-02-26 05:48:29 +00001177 }
hailfinger19db0922010-06-20 10:41:35 +00001178 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1179 programmer_delay(10);
1180
1181 /* We already wrote 2 bytes in the multicommand step. */
1182 pos += 2;
1183
hailfinger71e1bd42010-10-13 22:26:56 +00001184 /* Are there at least two more bytes to write? */
1185 while (pos < start + len - 1) {
hailfingerdef852d2010-10-27 22:07:11 +00001186 cmd[1] = buf[pos++ - start];
1187 cmd[2] = buf[pos++ - start];
hailfinger19db0922010-06-20 10:41:35 +00001188 spi_send_command(JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
1189 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1190 programmer_delay(10);
1191 }
1192
hailfinger71e1bd42010-10-13 22:26:56 +00001193 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
1194 * other non-AAI command.
1195 */
snelson8913d082010-02-26 05:48:29 +00001196 spi_write_disable();
hailfinger71e1bd42010-10-13 22:26:56 +00001197
1198 /* Write remaining byte (if any). */
1199 if (pos < start + len) {
hailfingerdef852d2010-10-27 22:07:11 +00001200 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
hailfinger71e1bd42010-10-13 22:26:56 +00001201 return SPI_GENERIC_ERROR;
1202 pos += pos % 2;
1203 }
1204
snelson8913d082010-02-26 05:48:29 +00001205 return 0;
1206}