blob: beb377b487c27d9566e4a8aded8eaaff3b2bbd6c [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
116static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
117{
118 unsigned char readarr[4];
119 uint32_t id1;
120 uint32_t id2;
121
stefanct9e6b98a2011-05-28 02:37:14 +0000122 if (spi_rdid(readarr, bytes)) {
123 msg_cdbg("\n");
snelson8913d082010-02-26 05:48:29 +0000124 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000125 }
snelson8913d082010-02-26 05:48:29 +0000126
127 if (!oddparity(readarr[0]))
snelsonfc007bb2010-03-24 23:14:32 +0000128 msg_cdbg("RDID byte 0 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000129
hailfingercb0564e2010-06-20 10:39:33 +0000130 /* Check if this is a continuation vendor ID.
131 * FIXME: Handle continuation device IDs.
132 */
snelson8913d082010-02-26 05:48:29 +0000133 if (readarr[0] == 0x7f) {
134 if (!oddparity(readarr[1]))
snelsonfc007bb2010-03-24 23:14:32 +0000135 msg_cdbg("RDID byte 1 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000136 id1 = (readarr[0] << 8) | readarr[1];
137 id2 = readarr[2];
138 if (bytes > 3) {
139 id2 <<= 8;
140 id2 |= readarr[3];
141 }
142 } else {
143 id1 = readarr[0];
144 id2 = (readarr[1] << 8) | readarr[2];
145 }
146
snelsonfc007bb2010-03-24 23:14:32 +0000147 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000148
149 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
150 /* Print the status register to tell the
151 * user about possible write protection.
152 */
153 spi_prettyprint_status_register(flash);
154
155 return 1;
156 }
157
158 /* Test if this is a pure vendor match. */
159 if (id1 == flash->manufacture_id &&
160 GENERIC_DEVICE_ID == flash->model_id)
161 return 1;
162
163 /* Test if there is any vendor ID. */
164 if (GENERIC_MANUF_ID == flash->manufacture_id &&
165 id1 != 0xff)
166 return 1;
167
168 return 0;
169}
170
171int probe_spi_rdid(struct flashchip *flash)
172{
173 return probe_spi_rdid_generic(flash, 3);
174}
175
snelson8913d082010-02-26 05:48:29 +0000176int probe_spi_rdid4(struct flashchip *flash)
177{
hailfingercb0564e2010-06-20 10:39:33 +0000178 /* Some SPI controllers do not support commands with writecnt=1 and
179 * readcnt=4.
180 */
mkarcherd264e9e2011-05-11 17:07:07 +0000181 switch (spi_programmer->type) {
hailfinger90c7d542010-05-31 15:27:27 +0000182#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000183#if defined(__i386__) || defined(__x86_64__)
hailfingercb0564e2010-06-20 10:39:33 +0000184 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +0000185 case SPI_CONTROLLER_WBSIO:
hailfingercb0564e2010-06-20 10:39:33 +0000186 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
187 return 0;
188 break;
snelson8913d082010-02-26 05:48:29 +0000189#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000190#endif
snelson8913d082010-02-26 05:48:29 +0000191 default:
hailfingercb0564e2010-06-20 10:39:33 +0000192 return probe_spi_rdid_generic(flash, 4);
snelson8913d082010-02-26 05:48:29 +0000193 }
194
195 return 0;
196}
197
198int probe_spi_rems(struct flashchip *flash)
199{
200 unsigned char readarr[JEDEC_REMS_INSIZE];
201 uint32_t id1, id2;
202
stefanct9e6b98a2011-05-28 02:37:14 +0000203 if (spi_rems(readarr)) {
204 msg_cdbg("\n");
snelson8913d082010-02-26 05:48:29 +0000205 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000206 }
snelson8913d082010-02-26 05:48:29 +0000207
208 id1 = readarr[0];
209 id2 = readarr[1];
210
snelsonfc007bb2010-03-24 23:14:32 +0000211 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000212
213 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
214 /* Print the status register to tell the
215 * user about possible write protection.
216 */
217 spi_prettyprint_status_register(flash);
218
219 return 1;
220 }
221
222 /* Test if this is a pure vendor match. */
223 if (id1 == flash->manufacture_id &&
224 GENERIC_DEVICE_ID == flash->model_id)
225 return 1;
226
227 /* Test if there is any vendor ID. */
228 if (GENERIC_MANUF_ID == flash->manufacture_id &&
229 id1 != 0xff)
230 return 1;
231
232 return 0;
233}
234
hailfinger59a83572010-05-28 17:07:57 +0000235int probe_spi_res1(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +0000236{
krause2eb76212011-01-17 07:50:42 +0000237 static const unsigned char allff[] = {0xff, 0xff, 0xff};
238 static const unsigned char all00[] = {0x00, 0x00, 0x00};
snelson8913d082010-02-26 05:48:29 +0000239 unsigned char readarr[3];
240 uint32_t id2;
snelson8913d082010-02-26 05:48:29 +0000241
hailfinger59a83572010-05-28 17:07:57 +0000242 /* We only want one-byte RES if RDID and REMS are unusable. */
243
snelson8913d082010-02-26 05:48:29 +0000244 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
245 * 0x00 0x00 0x00. In that case, RES is pointless.
246 */
247 if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) &&
248 memcmp(readarr, all00, 3)) {
249 msg_cdbg("Ignoring RES in favour of RDID.\n");
250 return 0;
251 }
252 /* Check if REMS is usable and does not return 0xff 0xff or
253 * 0x00 0x00. In that case, RES is pointless.
254 */
255 if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
256 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
257 msg_cdbg("Ignoring RES in favour of REMS.\n");
258 return 0;
259 }
260
stefanct9e6b98a2011-05-28 02:37:14 +0000261 if (spi_res(readarr, 1)) {
262 msg_cdbg("\n");
snelson8913d082010-02-26 05:48:29 +0000263 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000264 }
snelson8913d082010-02-26 05:48:29 +0000265
snelson8913d082010-02-26 05:48:29 +0000266 id2 = readarr[0];
hailfinger59a83572010-05-28 17:07:57 +0000267
snelsonfc007bb2010-03-24 23:14:32 +0000268 msg_cdbg("%s: id 0x%x\n", __func__, id2);
hailfinger59a83572010-05-28 17:07:57 +0000269
stefanct20f99532011-05-28 22:59:05 +0000270 if (id2 != flash->model_id)
snelson8913d082010-02-26 05:48:29 +0000271 return 0;
272
273 /* Print the status register to tell the
274 * user about possible write protection.
275 */
276 spi_prettyprint_status_register(flash);
277 return 1;
278}
279
hailfinger59a83572010-05-28 17:07:57 +0000280int probe_spi_res2(struct flashchip *flash)
281{
282 unsigned char readarr[2];
283 uint32_t id1, id2;
284
stefanct9e6b98a2011-05-28 02:37:14 +0000285 if (spi_res(readarr, 2)) {
286 msg_cdbg("\n");
hailfinger59a83572010-05-28 17:07:57 +0000287 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000288 }
hailfinger59a83572010-05-28 17:07:57 +0000289
290 id1 = readarr[0];
291 id2 = readarr[1];
292
293 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
294
295 if (id1 != flash->manufacture_id || id2 != flash->model_id)
296 return 0;
297
298 /* Print the status register to tell the
299 * user about possible write protection.
300 */
301 spi_prettyprint_status_register(flash);
302 return 1;
303}
304
snelson8913d082010-02-26 05:48:29 +0000305uint8_t spi_read_status_register(void)
306{
krause2eb76212011-01-17 07:50:42 +0000307 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
snelson8913d082010-02-26 05:48:29 +0000308 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
309 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
310 int ret;
311
312 /* Read Status Register */
313 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
314 if (ret)
snelsonfc007bb2010-03-24 23:14:32 +0000315 msg_cerr("RDSR failed!\n");
snelson8913d082010-02-26 05:48:29 +0000316
317 return readarr[0];
318}
319
320/* Prettyprint the status register. Common definitions. */
hailfinger7533bc82011-05-19 00:06:06 +0000321void spi_prettyprint_status_register_welwip(uint8_t status)
hailfingerc33d4732010-07-29 13:09:18 +0000322{
323 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
324 "%sset\n", (status & (1 << 1)) ? "" : "not ");
325 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
326 "%sset\n", (status & (1 << 0)) ? "" : "not ");
327}
328
329/* Prettyprint the status register. Common definitions. */
hailfinger7533bc82011-05-19 00:06:06 +0000330void spi_prettyprint_status_register_bp3210(uint8_t status, int bp)
331{
332 switch (bp) {
333 /* Fall through. */
334 case 3:
335 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) "
336 "is %sset\n", (status & (1 << 5)) ? "" : "not ");
337 case 2:
338 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) "
339 "is %sset\n", (status & (1 << 4)) ? "" : "not ");
340 case 1:
341 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) "
342 "is %sset\n", (status & (1 << 3)) ? "" : "not ");
343 case 0:
344 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) "
345 "is %sset\n", (status & (1 << 2)) ? "" : "not ");
346 }
347}
348
349/* Prettyprint the status register. Unnamed bits. */
350void spi_prettyprint_status_register_bit(uint8_t status, int bit)
351{
352 msg_cdbg("Chip status register: Bit %i "
353 "is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
354}
355
hailfingerc33d4732010-07-29 13:09:18 +0000356static void spi_prettyprint_status_register_common(uint8_t status)
snelson8913d082010-02-26 05:48:29 +0000357{
hailfinger7533bc82011-05-19 00:06:06 +0000358 spi_prettyprint_status_register_bp3210(status, 3);
hailfingerc33d4732010-07-29 13:09:18 +0000359 spi_prettyprint_status_register_welwip(status);
snelson8913d082010-02-26 05:48:29 +0000360}
361
362/* Prettyprint the status register. Works for
363 * ST M25P series
364 * MX MX25L series
365 */
366void spi_prettyprint_status_register_st_m25p(uint8_t status)
367{
snelsonfc007bb2010-03-24 23:14:32 +0000368 msg_cdbg("Chip status register: Status Register Write Disable "
snelson8913d082010-02-26 05:48:29 +0000369 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000370 msg_cdbg("Chip status register: Bit 6 is "
snelson8913d082010-02-26 05:48:29 +0000371 "%sset\n", (status & (1 << 6)) ? "" : "not ");
372 spi_prettyprint_status_register_common(status);
373}
374
375void spi_prettyprint_status_register_sst25(uint8_t status)
376{
snelsonfc007bb2010-03-24 23:14:32 +0000377 msg_cdbg("Chip status register: Block Protect Write Disable "
snelson8913d082010-02-26 05:48:29 +0000378 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000379 msg_cdbg("Chip status register: Auto Address Increment Programming "
snelson8913d082010-02-26 05:48:29 +0000380 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
381 spi_prettyprint_status_register_common(status);
382}
383
384/* Prettyprint the status register. Works for
385 * SST 25VF016
386 */
387void spi_prettyprint_status_register_sst25vf016(uint8_t status)
388{
krause2eb76212011-01-17 07:50:42 +0000389 static const char *const bpt[] = {
snelson8913d082010-02-26 05:48:29 +0000390 "none",
391 "1F0000H-1FFFFFH",
392 "1E0000H-1FFFFFH",
393 "1C0000H-1FFFFFH",
394 "180000H-1FFFFFH",
395 "100000H-1FFFFFH",
396 "all", "all"
397 };
398 spi_prettyprint_status_register_sst25(status);
snelsonfc007bb2010-03-24 23:14:32 +0000399 msg_cdbg("Resulting block protection : %s\n",
snelson8913d082010-02-26 05:48:29 +0000400 bpt[(status & 0x1c) >> 2]);
401}
402
403void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
404{
krause2eb76212011-01-17 07:50:42 +0000405 static const char *const bpt[] = {
snelson8913d082010-02-26 05:48:29 +0000406 "none",
407 "0x70000-0x7ffff",
408 "0x60000-0x7ffff",
409 "0x40000-0x7ffff",
410 "all blocks", "all blocks", "all blocks", "all blocks"
411 };
412 spi_prettyprint_status_register_sst25(status);
snelsonfc007bb2010-03-24 23:14:32 +0000413 msg_cdbg("Resulting block protection : %s\n",
snelson8913d082010-02-26 05:48:29 +0000414 bpt[(status & 0x1c) >> 2]);
415}
416
hailfinger7533bc82011-05-19 00:06:06 +0000417int spi_prettyprint_status_register(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +0000418{
419 uint8_t status;
420
421 status = spi_read_status_register();
snelsonfc007bb2010-03-24 23:14:32 +0000422 msg_cdbg("Chip status register is %02x\n", status);
snelson8913d082010-02-26 05:48:29 +0000423 switch (flash->manufacture_id) {
424 case ST_ID:
425 if (((flash->model_id & 0xff00) == 0x2000) ||
426 ((flash->model_id & 0xff00) == 0x2500))
427 spi_prettyprint_status_register_st_m25p(status);
428 break;
mhmd3c80cd2010-09-15 23:31:03 +0000429 case MACRONIX_ID:
snelson8913d082010-02-26 05:48:29 +0000430 if ((flash->model_id & 0xff00) == 0x2000)
431 spi_prettyprint_status_register_st_m25p(status);
432 break;
433 case SST_ID:
434 switch (flash->model_id) {
435 case 0x2541:
436 spi_prettyprint_status_register_sst25vf016(status);
437 break;
438 case 0x8d:
439 case 0x258d:
440 spi_prettyprint_status_register_sst25vf040b(status);
441 break;
442 default:
443 spi_prettyprint_status_register_sst25(status);
444 break;
445 }
446 break;
447 }
hailfinger7533bc82011-05-19 00:06:06 +0000448 return 0;
snelson8913d082010-02-26 05:48:29 +0000449}
450
451int spi_chip_erase_60(struct flashchip *flash)
452{
453 int result;
454 struct spi_command cmds[] = {
455 {
456 .writecnt = JEDEC_WREN_OUTSIZE,
457 .writearr = (const unsigned char[]){ JEDEC_WREN },
458 .readcnt = 0,
459 .readarr = NULL,
460 }, {
461 .writecnt = JEDEC_CE_60_OUTSIZE,
462 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
463 .readcnt = 0,
464 .readarr = NULL,
465 }, {
466 .writecnt = 0,
467 .writearr = NULL,
468 .readcnt = 0,
469 .readarr = NULL,
470 }};
471
snelson8913d082010-02-26 05:48:29 +0000472 result = spi_send_multicommand(cmds);
473 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000474 msg_cerr("%s failed during command execution\n",
snelson8913d082010-02-26 05:48:29 +0000475 __func__);
476 return result;
477 }
478 /* Wait until the Write-In-Progress bit is cleared.
479 * This usually takes 1-85 s, so wait in 1 s steps.
480 */
481 /* FIXME: We assume spi_read_status_register will never fail. */
482 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
483 programmer_delay(1000 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000484 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000485 return 0;
486}
487
488int spi_chip_erase_c7(struct flashchip *flash)
489{
490 int result;
491 struct spi_command cmds[] = {
492 {
493 .writecnt = JEDEC_WREN_OUTSIZE,
494 .writearr = (const unsigned char[]){ JEDEC_WREN },
495 .readcnt = 0,
496 .readarr = NULL,
497 }, {
498 .writecnt = JEDEC_CE_C7_OUTSIZE,
499 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
500 .readcnt = 0,
501 .readarr = NULL,
502 }, {
503 .writecnt = 0,
504 .writearr = NULL,
505 .readcnt = 0,
506 .readarr = NULL,
507 }};
508
snelson8913d082010-02-26 05:48:29 +0000509 result = spi_send_multicommand(cmds);
510 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000511 msg_cerr("%s failed during command execution\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000512 return result;
513 }
514 /* Wait until the Write-In-Progress bit is cleared.
515 * This usually takes 1-85 s, so wait in 1 s steps.
516 */
517 /* FIXME: We assume spi_read_status_register will never fail. */
518 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
519 programmer_delay(1000 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000520 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000521 return 0;
522}
523
snelson8913d082010-02-26 05:48:29 +0000524int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
525{
526 int result;
527 struct spi_command cmds[] = {
528 {
529 .writecnt = JEDEC_WREN_OUTSIZE,
530 .writearr = (const unsigned char[]){ JEDEC_WREN },
531 .readcnt = 0,
532 .readarr = NULL,
533 }, {
534 .writecnt = JEDEC_BE_52_OUTSIZE,
535 .writearr = (const unsigned char[]){
536 JEDEC_BE_52,
537 (addr >> 16) & 0xff,
538 (addr >> 8) & 0xff,
539 (addr & 0xff)
540 },
541 .readcnt = 0,
542 .readarr = NULL,
543 }, {
544 .writecnt = 0,
545 .writearr = NULL,
546 .readcnt = 0,
547 .readarr = NULL,
548 }};
549
550 result = spi_send_multicommand(cmds);
551 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000552 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000553 __func__, addr);
554 return result;
555 }
556 /* Wait until the Write-In-Progress bit is cleared.
557 * This usually takes 100-4000 ms, so wait in 100 ms steps.
558 */
559 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
560 programmer_delay(100 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000561 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000562 return 0;
563}
564
565/* Block size is usually
566 * 64k for Macronix
567 * 32k for SST
568 * 4-32k non-uniform for EON
569 */
570int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
571{
572 int result;
573 struct spi_command cmds[] = {
574 {
575 .writecnt = JEDEC_WREN_OUTSIZE,
576 .writearr = (const unsigned char[]){ JEDEC_WREN },
577 .readcnt = 0,
578 .readarr = NULL,
579 }, {
580 .writecnt = JEDEC_BE_D8_OUTSIZE,
581 .writearr = (const unsigned char[]){
582 JEDEC_BE_D8,
583 (addr >> 16) & 0xff,
584 (addr >> 8) & 0xff,
585 (addr & 0xff)
586 },
587 .readcnt = 0,
588 .readarr = NULL,
589 }, {
590 .writecnt = 0,
591 .writearr = NULL,
592 .readcnt = 0,
593 .readarr = NULL,
594 }};
595
596 result = spi_send_multicommand(cmds);
597 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000598 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000599 __func__, addr);
600 return result;
601 }
602 /* Wait until the Write-In-Progress bit is cleared.
603 * This usually takes 100-4000 ms, so wait in 100 ms steps.
604 */
605 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
606 programmer_delay(100 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000607 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000608 return 0;
609}
610
611/* Block size is usually
612 * 4k for PMC
613 */
614int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
615{
616 int result;
617 struct spi_command cmds[] = {
618 {
619 .writecnt = JEDEC_WREN_OUTSIZE,
620 .writearr = (const unsigned char[]){ JEDEC_WREN },
621 .readcnt = 0,
622 .readarr = NULL,
623 }, {
624 .writecnt = JEDEC_BE_D7_OUTSIZE,
625 .writearr = (const unsigned char[]){
626 JEDEC_BE_D7,
627 (addr >> 16) & 0xff,
628 (addr >> 8) & 0xff,
629 (addr & 0xff)
630 },
631 .readcnt = 0,
632 .readarr = NULL,
633 }, {
634 .writecnt = 0,
635 .writearr = NULL,
636 .readcnt = 0,
637 .readarr = NULL,
638 }};
639
640 result = spi_send_multicommand(cmds);
641 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000642 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000643 __func__, addr);
644 return result;
645 }
646 /* Wait until the Write-In-Progress bit is cleared.
647 * This usually takes 100-4000 ms, so wait in 100 ms steps.
648 */
649 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
650 programmer_delay(100 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000651 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000652 return 0;
653}
654
snelson8913d082010-02-26 05:48:29 +0000655/* Sector size is usually 4k, though Macronix eliteflash has 64k */
656int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
657{
658 int result;
659 struct spi_command cmds[] = {
660 {
661 .writecnt = JEDEC_WREN_OUTSIZE,
662 .writearr = (const unsigned char[]){ JEDEC_WREN },
663 .readcnt = 0,
664 .readarr = NULL,
665 }, {
666 .writecnt = JEDEC_SE_OUTSIZE,
667 .writearr = (const unsigned char[]){
668 JEDEC_SE,
669 (addr >> 16) & 0xff,
670 (addr >> 8) & 0xff,
671 (addr & 0xff)
672 },
673 .readcnt = 0,
674 .readarr = NULL,
675 }, {
676 .writecnt = 0,
677 .writearr = NULL,
678 .readcnt = 0,
679 .readarr = NULL,
680 }};
681
682 result = spi_send_multicommand(cmds);
Stefan Reinauercce56d52010-11-22 18:22:21 -0800683
snelson8913d082010-02-26 05:48:29 +0000684 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000685 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000686 __func__, addr);
687 return result;
688 }
689 /* Wait until the Write-In-Progress bit is cleared.
690 * This usually takes 15-800 ms, so wait in 10 ms steps.
691 */
692 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
693 programmer_delay(10 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000694 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000695 return 0;
696}
697
698int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
699{
700 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000701 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000702 __func__);
703 return -1;
704 }
705 return spi_chip_erase_60(flash);
706}
707
708int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
709{
710 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000711 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000712 __func__);
713 return -1;
714 }
715 return spi_chip_erase_c7(flash);
716}
717
718int spi_write_status_enable(void)
719{
krause2eb76212011-01-17 07:50:42 +0000720 static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
snelson8913d082010-02-26 05:48:29 +0000721 int result;
722
723 /* Send EWSR (Enable Write Status Register). */
724 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
725
726 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000727 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000728
729 return result;
730}
731
732/*
733 * This is according the SST25VF016 datasheet, who knows it is more
734 * generic that this...
735 */
hailfingerc33d4732010-07-29 13:09:18 +0000736static int spi_write_status_register_ewsr(struct flashchip *flash, int status)
snelson8913d082010-02-26 05:48:29 +0000737{
738 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000739 int i = 0;
snelson8913d082010-02-26 05:48:29 +0000740 struct spi_command cmds[] = {
741 {
hailfingerc33d4732010-07-29 13:09:18 +0000742 /* WRSR requires either EWSR or WREN depending on chip type. */
snelson8913d082010-02-26 05:48:29 +0000743 .writecnt = JEDEC_EWSR_OUTSIZE,
744 .writearr = (const unsigned char[]){ JEDEC_EWSR },
745 .readcnt = 0,
746 .readarr = NULL,
747 }, {
748 .writecnt = JEDEC_WRSR_OUTSIZE,
749 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
750 .readcnt = 0,
751 .readarr = NULL,
752 }, {
753 .writecnt = 0,
754 .writearr = NULL,
755 .readcnt = 0,
756 .readarr = NULL,
757 }};
758
759 result = spi_send_multicommand(cmds);
760 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000761 msg_cerr("%s failed during command execution\n",
snelson8913d082010-02-26 05:48:29 +0000762 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000763 /* No point in waiting for the command to complete if execution
764 * failed.
765 */
766 return result;
snelson8913d082010-02-26 05:48:29 +0000767 }
hailfingeree9ee132010-10-08 00:37:55 +0000768 /* WRSR performs a self-timed erase before the changes take effect.
769 * This may take 50-85 ms in most cases, and some chips apparently
770 * allow running RDSR only once. Therefore pick an initial delay of
771 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
772 */
hailfingerc33d4732010-07-29 13:09:18 +0000773 programmer_delay(100 * 1000);
hailfingeree9ee132010-10-08 00:37:55 +0000774 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
775 if (++i > 490) {
776 msg_cerr("Error: WIP bit after WRSR never cleared\n");
777 return TIMEOUT_ERROR;
778 }
779 programmer_delay(10 * 1000);
780 }
781 return 0;
snelson8913d082010-02-26 05:48:29 +0000782}
783
hailfingerc33d4732010-07-29 13:09:18 +0000784static int spi_write_status_register_wren(struct flashchip *flash, int status)
785{
786 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000787 int i = 0;
hailfingerc33d4732010-07-29 13:09:18 +0000788 struct spi_command cmds[] = {
789 {
790 /* WRSR requires either EWSR or WREN depending on chip type. */
791 .writecnt = JEDEC_WREN_OUTSIZE,
792 .writearr = (const unsigned char[]){ JEDEC_WREN },
793 .readcnt = 0,
794 .readarr = NULL,
795 }, {
796 .writecnt = JEDEC_WRSR_OUTSIZE,
797 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
798 .readcnt = 0,
799 .readarr = NULL,
800 }, {
801 .writecnt = 0,
802 .writearr = NULL,
803 .readcnt = 0,
804 .readarr = NULL,
805 }};
806
807 result = spi_send_multicommand(cmds);
808 if (result) {
809 msg_cerr("%s failed during command execution\n",
810 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000811 /* No point in waiting for the command to complete if execution
812 * failed.
813 */
814 return result;
hailfingerc33d4732010-07-29 13:09:18 +0000815 }
hailfingeree9ee132010-10-08 00:37:55 +0000816 /* WRSR performs a self-timed erase before the changes take effect.
817 * This may take 50-85 ms in most cases, and some chips apparently
818 * allow running RDSR only once. Therefore pick an initial delay of
819 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
820 */
hailfingerc33d4732010-07-29 13:09:18 +0000821 programmer_delay(100 * 1000);
hailfingeree9ee132010-10-08 00:37:55 +0000822 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
823 if (++i > 490) {
824 msg_cerr("Error: WIP bit after WRSR never cleared\n");
825 return TIMEOUT_ERROR;
826 }
827 programmer_delay(10 * 1000);
828 }
829 return 0;
hailfingerc33d4732010-07-29 13:09:18 +0000830}
831
hailfinger7533bc82011-05-19 00:06:06 +0000832int spi_write_status_register(struct flashchip *flash, int status)
hailfingerc33d4732010-07-29 13:09:18 +0000833{
834 int ret = 1;
835
836 if (!(flash->feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
837 msg_cdbg("Missing status register write definition, assuming "
838 "EWSR is needed\n");
839 flash->feature_bits |= FEATURE_WRSR_EWSR;
840 }
841 if (flash->feature_bits & FEATURE_WRSR_WREN)
842 ret = spi_write_status_register_wren(flash, status);
843 if (ret && (flash->feature_bits & FEATURE_WRSR_EWSR))
844 ret = spi_write_status_register_ewsr(flash, status);
845 return ret;
846}
847
stefancta3cbe392011-09-18 22:42:18 +0000848int spi_byte_program(int addr, uint8_t databyte)
snelson8913d082010-02-26 05:48:29 +0000849{
850 int result;
851 struct spi_command cmds[] = {
852 {
853 .writecnt = JEDEC_WREN_OUTSIZE,
854 .writearr = (const unsigned char[]){ JEDEC_WREN },
855 .readcnt = 0,
856 .readarr = NULL,
857 }, {
858 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
859 .writearr = (const unsigned char[]){
860 JEDEC_BYTE_PROGRAM,
861 (addr >> 16) & 0xff,
862 (addr >> 8) & 0xff,
863 (addr & 0xff),
864 databyte
865 },
866 .readcnt = 0,
867 .readarr = NULL,
868 }, {
869 .writecnt = 0,
870 .writearr = NULL,
871 .readcnt = 0,
872 .readarr = NULL,
873 }};
874
875 result = spi_send_multicommand(cmds);
876 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000877 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000878 __func__, addr);
879 }
880 return result;
881}
882
stefancta3cbe392011-09-18 22:42:18 +0000883int spi_nbyte_program(int addr, uint8_t *bytes, int len)
snelson8913d082010-02-26 05:48:29 +0000884{
885 int result;
886 /* FIXME: Switch to malloc based on len unless that kills speed. */
887 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
888 JEDEC_BYTE_PROGRAM,
889 (addr >> 16) & 0xff,
890 (addr >> 8) & 0xff,
891 (addr >> 0) & 0xff,
892 };
893 struct spi_command cmds[] = {
894 {
895 .writecnt = JEDEC_WREN_OUTSIZE,
896 .writearr = (const unsigned char[]){ JEDEC_WREN },
897 .readcnt = 0,
898 .readarr = NULL,
899 }, {
900 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
901 .writearr = cmd,
902 .readcnt = 0,
903 .readarr = NULL,
904 }, {
905 .writecnt = 0,
906 .writearr = NULL,
907 .readcnt = 0,
908 .readarr = NULL,
909 }};
910
911 if (!len) {
snelsonfc007bb2010-03-24 23:14:32 +0000912 msg_cerr("%s called for zero-length write\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000913 return 1;
914 }
915 if (len > 256) {
snelsonfc007bb2010-03-24 23:14:32 +0000916 msg_cerr("%s called for too long a write\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000917 return 1;
918 }
919
920 memcpy(&cmd[4], bytes, len);
921
922 result = spi_send_multicommand(cmds);
923 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000924 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000925 __func__, addr);
926 }
927 return result;
928}
929
David Hendricksbf36f092010-11-02 23:39:29 -0700930int spi_restore_status(struct flashchip *flash, uint8_t status)
931{
932 msg_cdbg("restoring chip status (0x%02x)\n", status);
933 return spi_write_status_register(flash, status);
934}
935
hailfingerc33d4732010-07-29 13:09:18 +0000936/* A generic brute-force block protection disable works like this:
937 * Write 0x00 to the status register. Check if any locks are still set (that
938 * part is chip specific). Repeat once.
939 */
hailfingerb9560ee2010-07-14 20:21:22 +0000940int spi_disable_blockprotect(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +0000941{
942 uint8_t status;
943 int result;
944
945 status = spi_read_status_register();
hailfingerc33d4732010-07-29 13:09:18 +0000946 /* If block protection is disabled, stop here. */
947 if ((status & 0x3c) == 0)
948 return 0;
949
David Hendricksbf36f092010-11-02 23:39:29 -0700950 /* restore status register content upon exit */
951 register_chip_restore(spi_restore_status, flash, status);
952
hailfingerc33d4732010-07-29 13:09:18 +0000953 msg_cdbg("Some block protection in effect, disabling\n");
954 result = spi_write_status_register(flash, status & ~0x3c);
955 if (result) {
956 msg_cerr("spi_write_status_register failed\n");
957 return result;
958 }
959 status = spi_read_status_register();
snelson8913d082010-02-26 05:48:29 +0000960 if ((status & 0x3c) != 0) {
hailfingerc33d4732010-07-29 13:09:18 +0000961 msg_cerr("Block protection could not be disabled!\n");
962 return 1;
963 }
964 return 0;
965}
966
stefancta3cbe392011-09-18 22:42:18 +0000967int spi_nbyte_read(int address, uint8_t *bytes, int len)
snelson8913d082010-02-26 05:48:29 +0000968{
969 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
970 JEDEC_READ,
971 (address >> 16) & 0xff,
972 (address >> 8) & 0xff,
973 (address >> 0) & 0xff,
974 };
975
976 /* Send Read */
977 return spi_send_command(sizeof(cmd), len, cmd, bytes);
978}
979
980/*
hailfinger39d159a2010-05-21 23:09:42 +0000981 * Read a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000982 * FIXME: Use the chunk code from Michael Karcher instead.
snelson8913d082010-02-26 05:48:29 +0000983 * Each page is read separately in chunks with a maximum size of chunksize.
984 */
stefancta3cbe392011-09-18 22:42:18 +0000985int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
snelson8913d082010-02-26 05:48:29 +0000986{
987 int rc = 0;
988 int i, j, starthere, lenhere;
989 int page_size = flash->page_size;
990 int toread;
991
992 /* Warning: This loop has a very unusual condition and body.
993 * The loop needs to go through each page with at least one affected
994 * byte. The lowest page number is (start / page_size) since that
995 * division rounds down. The highest page number we want is the page
996 * where the last byte of the range lives. That last byte has the
997 * address (start + len - 1), thus the highest page number is
998 * (start + len - 1) / page_size. Since we want to include that last
999 * page as well, the loop condition uses <=.
1000 */
1001 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1002 /* Byte position of the first byte in the range in this page. */
1003 /* starthere is an offset to the base address of the chip. */
1004 starthere = max(start, i * page_size);
1005 /* Length of bytes in the range in this page. */
1006 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1007 for (j = 0; j < lenhere; j += chunksize) {
1008 toread = min(chunksize, lenhere - j);
1009 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
1010 if (rc)
1011 break;
1012 }
1013 if (rc)
1014 break;
1015 }
1016
1017 return rc;
1018}
1019
1020/*
hailfinger39d159a2010-05-21 23:09:42 +00001021 * Write a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +00001022 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +00001023 * Each page is written separately in chunks with a maximum size of chunksize.
1024 */
stefancta3cbe392011-09-18 22:42:18 +00001025int spi_write_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
hailfinger39d159a2010-05-21 23:09:42 +00001026{
1027 int rc = 0;
1028 int i, j, starthere, lenhere;
1029 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
1030 * in struct flashchip to do this properly. All chips using
1031 * spi_chip_write_256 have page_size set to max_writechunk_size, so
1032 * we're OK for now.
1033 */
1034 int page_size = flash->page_size;
1035 int towrite;
1036
1037 /* Warning: This loop has a very unusual condition and body.
1038 * The loop needs to go through each page with at least one affected
1039 * byte. The lowest page number is (start / page_size) since that
1040 * division rounds down. The highest page number we want is the page
1041 * where the last byte of the range lives. That last byte has the
1042 * address (start + len - 1), thus the highest page number is
1043 * (start + len - 1) / page_size. Since we want to include that last
1044 * page as well, the loop condition uses <=.
1045 */
1046 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1047 /* Byte position of the first byte in the range in this page. */
1048 /* starthere is an offset to the base address of the chip. */
1049 starthere = max(start, i * page_size);
1050 /* Length of bytes in the range in this page. */
1051 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1052 for (j = 0; j < lenhere; j += chunksize) {
1053 towrite = min(chunksize, lenhere - j);
1054 rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite);
1055 if (rc)
1056 break;
1057 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1058 programmer_delay(10);
1059 }
1060 if (rc)
1061 break;
1062 }
1063
1064 return rc;
1065}
1066
1067/*
snelson8913d082010-02-26 05:48:29 +00001068 * Program chip using byte programming. (SLOW!)
1069 * This is for chips which can only handle one byte writes
1070 * and for chips where memory mapped programming is impossible
1071 * (e.g. due to size constraints in IT87* for over 512 kB)
1072 */
hailfingerc7d06c62010-07-14 16:19:05 +00001073/* real chunksize is 1, logical chunksize is 1 */
stefancta3cbe392011-09-18 22:42:18 +00001074int spi_chip_write_1(struct flashchip *flash, uint8_t *buf, int start, int len)
snelson8913d082010-02-26 05:48:29 +00001075{
snelson8913d082010-02-26 05:48:29 +00001076 int i, result = 0;
1077
hailfingerc7d06c62010-07-14 16:19:05 +00001078 for (i = start; i < start + len; i++) {
hailfingerdef852d2010-10-27 22:07:11 +00001079 result = spi_byte_program(i, buf[i - start]);
snelson8913d082010-02-26 05:48:29 +00001080 if (result)
1081 return 1;
1082 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1083 programmer_delay(10);
1084 }
1085
1086 return 0;
1087}
1088
stefancta3cbe392011-09-18 22:42:18 +00001089int spi_aai_write(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfingerc7d06c62010-07-14 16:19:05 +00001090{
1091 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +00001092 int result;
hailfinger19db0922010-06-20 10:41:35 +00001093 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1094 JEDEC_AAI_WORD_PROGRAM,
1095 };
1096 struct spi_command cmds[] = {
1097 {
1098 .writecnt = JEDEC_WREN_OUTSIZE,
1099 .writearr = (const unsigned char[]){ JEDEC_WREN },
1100 .readcnt = 0,
1101 .readarr = NULL,
1102 }, {
1103 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1104 .writearr = (const unsigned char[]){
1105 JEDEC_AAI_WORD_PROGRAM,
hailfingerc7d06c62010-07-14 16:19:05 +00001106 (start >> 16) & 0xff,
1107 (start >> 8) & 0xff,
1108 (start & 0xff),
hailfinger19db0922010-06-20 10:41:35 +00001109 buf[0],
1110 buf[1]
1111 },
1112 .readcnt = 0,
1113 .readarr = NULL,
1114 }, {
1115 .writecnt = 0,
1116 .writearr = NULL,
1117 .readcnt = 0,
1118 .readarr = NULL,
1119 }};
snelson8913d082010-02-26 05:48:29 +00001120
mkarcherd264e9e2011-05-11 17:07:07 +00001121 switch (spi_programmer->type) {
hailfinger90c7d542010-05-31 15:27:27 +00001122#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +00001123#if defined(__i386__) || defined(__x86_64__)
hailfinger19db0922010-06-20 10:41:35 +00001124 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +00001125 case SPI_CONTROLLER_WBSIO:
hailfingerc7d06c62010-07-14 16:19:05 +00001126 msg_perr("%s: impossible with this SPI controller,"
snelson8913d082010-02-26 05:48:29 +00001127 " degrading to byte program\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +00001128 return spi_chip_write_1(flash, buf, start, len);
snelson8913d082010-02-26 05:48:29 +00001129#endif
hailfinger324a9cc2010-05-26 01:45:41 +00001130#endif
snelson8913d082010-02-26 05:48:29 +00001131 default:
1132 break;
1133 }
hailfinger19db0922010-06-20 10:41:35 +00001134
hailfingerc7d06c62010-07-14 16:19:05 +00001135 /* The even start address and even length requirements can be either
1136 * honored outside this function, or we can call spi_byte_program
1137 * for the first and/or last byte and use AAI for the rest.
hailfinger71e1bd42010-10-13 22:26:56 +00001138 * FIXME: Move this to generic code.
hailfingerc7d06c62010-07-14 16:19:05 +00001139 */
hailfinger19db0922010-06-20 10:41:35 +00001140 /* The data sheet requires a start address with the low bit cleared. */
hailfingerc7d06c62010-07-14 16:19:05 +00001141 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +00001142 msg_cerr("%s: start address not even! Please report a bug at "
1143 "flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +00001144 if (spi_chip_write_1(flash, buf, start, start % 2))
1145 return SPI_GENERIC_ERROR;
1146 pos += start % 2;
hailfingerdef852d2010-10-27 22:07:11 +00001147 cmds[1].writearr = (const unsigned char[]){
1148 JEDEC_AAI_WORD_PROGRAM,
1149 (pos >> 16) & 0xff,
1150 (pos >> 8) & 0xff,
1151 (pos & 0xff),
1152 buf[pos - start],
1153 buf[pos - start + 1]
1154 };
hailfinger71e1bd42010-10-13 22:26:56 +00001155 /* Do not return an error for now. */
1156 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +00001157 }
1158 /* The data sheet requires total AAI write length to be even. */
1159 if (len % 2) {
1160 msg_cerr("%s: total write length not even! Please report a "
1161 "bug at flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +00001162 /* Do not return an error for now. */
1163 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +00001164 }
1165
hailfinger19db0922010-06-20 10:41:35 +00001166
1167 result = spi_send_multicommand(cmds);
1168 if (result) {
1169 msg_cerr("%s failed during start command execution\n",
1170 __func__);
hailfingerc7d06c62010-07-14 16:19:05 +00001171 /* FIXME: Should we send WRDI here as well to make sure the chip
1172 * is not in AAI mode?
1173 */
snelson8913d082010-02-26 05:48:29 +00001174 return result;
snelson8913d082010-02-26 05:48:29 +00001175 }
hailfinger19db0922010-06-20 10:41:35 +00001176 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1177 programmer_delay(10);
1178
1179 /* We already wrote 2 bytes in the multicommand step. */
1180 pos += 2;
1181
hailfinger71e1bd42010-10-13 22:26:56 +00001182 /* Are there at least two more bytes to write? */
1183 while (pos < start + len - 1) {
hailfingerdef852d2010-10-27 22:07:11 +00001184 cmd[1] = buf[pos++ - start];
1185 cmd[2] = buf[pos++ - start];
hailfinger19db0922010-06-20 10:41:35 +00001186 spi_send_command(JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
1187 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1188 programmer_delay(10);
1189 }
1190
hailfinger71e1bd42010-10-13 22:26:56 +00001191 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
1192 * other non-AAI command.
1193 */
snelson8913d082010-02-26 05:48:29 +00001194 spi_write_disable();
hailfinger71e1bd42010-10-13 22:26:56 +00001195
1196 /* Write remaining byte (if any). */
1197 if (pos < start + len) {
hailfingerdef852d2010-10-27 22:07:11 +00001198 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
hailfinger71e1bd42010-10-13 22:26:56 +00001199 return SPI_GENERIC_ERROR;
1200 pos += pos % 2;
1201 }
1202
snelson8913d082010-02-26 05:48:29 +00001203 return 0;
1204}