blob: fda4d426ceb8f66788a063391a66ea9ac298e6a7 [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)) {
snelson8913d082010-02-26 05:48:29 +0000123 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000124 }
snelson8913d082010-02-26 05:48:29 +0000125
126 if (!oddparity(readarr[0]))
snelsonfc007bb2010-03-24 23:14:32 +0000127 msg_cdbg("RDID byte 0 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000128
hailfingercb0564e2010-06-20 10:39:33 +0000129 /* Check if this is a continuation vendor ID.
130 * FIXME: Handle continuation device IDs.
131 */
snelson8913d082010-02-26 05:48:29 +0000132 if (readarr[0] == 0x7f) {
133 if (!oddparity(readarr[1]))
snelsonfc007bb2010-03-24 23:14:32 +0000134 msg_cdbg("RDID byte 1 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000135 id1 = (readarr[0] << 8) | readarr[1];
136 id2 = readarr[2];
137 if (bytes > 3) {
138 id2 <<= 8;
139 id2 |= readarr[3];
140 }
141 } else {
142 id1 = readarr[0];
143 id2 = (readarr[1] << 8) | readarr[2];
144 }
145
snelsonfc007bb2010-03-24 23:14:32 +0000146 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000147
148 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
149 /* Print the status register to tell the
150 * user about possible write protection.
151 */
152 spi_prettyprint_status_register(flash);
153
154 return 1;
155 }
156
157 /* Test if this is a pure vendor match. */
158 if (id1 == flash->manufacture_id &&
159 GENERIC_DEVICE_ID == flash->model_id)
160 return 1;
161
162 /* Test if there is any vendor ID. */
163 if (GENERIC_MANUF_ID == flash->manufacture_id &&
164 id1 != 0xff)
165 return 1;
166
167 return 0;
168}
169
170int probe_spi_rdid(struct flashchip *flash)
171{
172 return probe_spi_rdid_generic(flash, 3);
173}
174
snelson8913d082010-02-26 05:48:29 +0000175int probe_spi_rdid4(struct flashchip *flash)
176{
hailfingercb0564e2010-06-20 10:39:33 +0000177 /* Some SPI controllers do not support commands with writecnt=1 and
178 * readcnt=4.
179 */
mkarcherd264e9e2011-05-11 17:07:07 +0000180 switch (spi_programmer->type) {
hailfinger90c7d542010-05-31 15:27:27 +0000181#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000182#if defined(__i386__) || defined(__x86_64__)
hailfingercb0564e2010-06-20 10:39:33 +0000183 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +0000184 case SPI_CONTROLLER_WBSIO:
hailfingercb0564e2010-06-20 10:39:33 +0000185 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
186 return 0;
187 break;
snelson8913d082010-02-26 05:48:29 +0000188#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000189#endif
snelson8913d082010-02-26 05:48:29 +0000190 default:
hailfingercb0564e2010-06-20 10:39:33 +0000191 return probe_spi_rdid_generic(flash, 4);
snelson8913d082010-02-26 05:48:29 +0000192 }
193
194 return 0;
195}
196
197int probe_spi_rems(struct flashchip *flash)
198{
199 unsigned char readarr[JEDEC_REMS_INSIZE];
200 uint32_t id1, id2;
201
stefanct9e6b98a2011-05-28 02:37:14 +0000202 if (spi_rems(readarr)) {
snelson8913d082010-02-26 05:48:29 +0000203 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000204 }
snelson8913d082010-02-26 05:48:29 +0000205
206 id1 = readarr[0];
207 id2 = readarr[1];
208
snelsonfc007bb2010-03-24 23:14:32 +0000209 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000210
211 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
212 /* Print the status register to tell the
213 * user about possible write protection.
214 */
215 spi_prettyprint_status_register(flash);
216
217 return 1;
218 }
219
220 /* Test if this is a pure vendor match. */
221 if (id1 == flash->manufacture_id &&
222 GENERIC_DEVICE_ID == flash->model_id)
223 return 1;
224
225 /* Test if there is any vendor ID. */
226 if (GENERIC_MANUF_ID == flash->manufacture_id &&
227 id1 != 0xff)
228 return 1;
229
230 return 0;
231}
232
hailfinger59a83572010-05-28 17:07:57 +0000233int probe_spi_res1(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +0000234{
krause2eb76212011-01-17 07:50:42 +0000235 static const unsigned char allff[] = {0xff, 0xff, 0xff};
236 static const unsigned char all00[] = {0x00, 0x00, 0x00};
snelson8913d082010-02-26 05:48:29 +0000237 unsigned char readarr[3];
238 uint32_t id2;
snelson8913d082010-02-26 05:48:29 +0000239
hailfinger59a83572010-05-28 17:07:57 +0000240 /* We only want one-byte RES if RDID and REMS are unusable. */
241
snelson8913d082010-02-26 05:48:29 +0000242 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
243 * 0x00 0x00 0x00. In that case, RES is pointless.
244 */
245 if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) &&
246 memcmp(readarr, all00, 3)) {
247 msg_cdbg("Ignoring RES in favour of RDID.\n");
248 return 0;
249 }
250 /* Check if REMS is usable and does not return 0xff 0xff or
251 * 0x00 0x00. In that case, RES is pointless.
252 */
253 if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
254 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
255 msg_cdbg("Ignoring RES in favour of REMS.\n");
256 return 0;
257 }
258
stefanct9e6b98a2011-05-28 02:37:14 +0000259 if (spi_res(readarr, 1)) {
snelson8913d082010-02-26 05:48:29 +0000260 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000261 }
snelson8913d082010-02-26 05:48:29 +0000262
snelson8913d082010-02-26 05:48:29 +0000263 id2 = readarr[0];
hailfinger59a83572010-05-28 17:07:57 +0000264
snelsonfc007bb2010-03-24 23:14:32 +0000265 msg_cdbg("%s: id 0x%x\n", __func__, id2);
hailfinger59a83572010-05-28 17:07:57 +0000266
stefanct20f99532011-05-28 22:59:05 +0000267 if (id2 != flash->model_id)
snelson8913d082010-02-26 05:48:29 +0000268 return 0;
269
270 /* Print the status register to tell the
271 * user about possible write protection.
272 */
273 spi_prettyprint_status_register(flash);
274 return 1;
275}
276
hailfinger59a83572010-05-28 17:07:57 +0000277int probe_spi_res2(struct flashchip *flash)
278{
279 unsigned char readarr[2];
280 uint32_t id1, id2;
281
stefanct9e6b98a2011-05-28 02:37:14 +0000282 if (spi_res(readarr, 2)) {
hailfinger59a83572010-05-28 17:07:57 +0000283 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000284 }
hailfinger59a83572010-05-28 17:07:57 +0000285
286 id1 = readarr[0];
287 id2 = readarr[1];
288
289 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
290
291 if (id1 != flash->manufacture_id || id2 != flash->model_id)
292 return 0;
293
294 /* Print the status register to tell the
295 * user about possible write protection.
296 */
297 spi_prettyprint_status_register(flash);
298 return 1;
299}
300
snelson8913d082010-02-26 05:48:29 +0000301uint8_t spi_read_status_register(void)
302{
krause2eb76212011-01-17 07:50:42 +0000303 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
snelson8913d082010-02-26 05:48:29 +0000304 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
305 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
306 int ret;
307
308 /* Read Status Register */
309 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
310 if (ret)
snelsonfc007bb2010-03-24 23:14:32 +0000311 msg_cerr("RDSR failed!\n");
snelson8913d082010-02-26 05:48:29 +0000312
313 return readarr[0];
314}
315
316/* Prettyprint the status register. Common definitions. */
hailfinger7533bc82011-05-19 00:06:06 +0000317void spi_prettyprint_status_register_welwip(uint8_t status)
hailfingerc33d4732010-07-29 13:09:18 +0000318{
319 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
320 "%sset\n", (status & (1 << 1)) ? "" : "not ");
321 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
322 "%sset\n", (status & (1 << 0)) ? "" : "not ");
323}
324
325/* Prettyprint the status register. Common definitions. */
hailfinger7533bc82011-05-19 00:06:06 +0000326void spi_prettyprint_status_register_bp3210(uint8_t status, int bp)
327{
328 switch (bp) {
329 /* Fall through. */
330 case 3:
331 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) "
332 "is %sset\n", (status & (1 << 5)) ? "" : "not ");
333 case 2:
334 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) "
335 "is %sset\n", (status & (1 << 4)) ? "" : "not ");
336 case 1:
337 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) "
338 "is %sset\n", (status & (1 << 3)) ? "" : "not ");
339 case 0:
340 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) "
341 "is %sset\n", (status & (1 << 2)) ? "" : "not ");
342 }
343}
344
345/* Prettyprint the status register. Unnamed bits. */
346void spi_prettyprint_status_register_bit(uint8_t status, int bit)
347{
348 msg_cdbg("Chip status register: Bit %i "
349 "is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
350}
351
hailfingerc33d4732010-07-29 13:09:18 +0000352static void spi_prettyprint_status_register_common(uint8_t status)
snelson8913d082010-02-26 05:48:29 +0000353{
hailfinger7533bc82011-05-19 00:06:06 +0000354 spi_prettyprint_status_register_bp3210(status, 3);
hailfingerc33d4732010-07-29 13:09:18 +0000355 spi_prettyprint_status_register_welwip(status);
snelson8913d082010-02-26 05:48:29 +0000356}
357
358/* Prettyprint the status register. Works for
359 * ST M25P series
360 * MX MX25L series
361 */
362void spi_prettyprint_status_register_st_m25p(uint8_t status)
363{
snelsonfc007bb2010-03-24 23:14:32 +0000364 msg_cdbg("Chip status register: Status Register Write Disable "
snelson8913d082010-02-26 05:48:29 +0000365 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000366 msg_cdbg("Chip status register: Bit 6 is "
snelson8913d082010-02-26 05:48:29 +0000367 "%sset\n", (status & (1 << 6)) ? "" : "not ");
368 spi_prettyprint_status_register_common(status);
369}
370
371void spi_prettyprint_status_register_sst25(uint8_t status)
372{
snelsonfc007bb2010-03-24 23:14:32 +0000373 msg_cdbg("Chip status register: Block Protect Write Disable "
snelson8913d082010-02-26 05:48:29 +0000374 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
snelsonfc007bb2010-03-24 23:14:32 +0000375 msg_cdbg("Chip status register: Auto Address Increment Programming "
snelson8913d082010-02-26 05:48:29 +0000376 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
377 spi_prettyprint_status_register_common(status);
378}
379
380/* Prettyprint the status register. Works for
381 * SST 25VF016
382 */
383void spi_prettyprint_status_register_sst25vf016(uint8_t status)
384{
krause2eb76212011-01-17 07:50:42 +0000385 static const char *const bpt[] = {
snelson8913d082010-02-26 05:48:29 +0000386 "none",
387 "1F0000H-1FFFFFH",
388 "1E0000H-1FFFFFH",
389 "1C0000H-1FFFFFH",
390 "180000H-1FFFFFH",
391 "100000H-1FFFFFH",
392 "all", "all"
393 };
394 spi_prettyprint_status_register_sst25(status);
snelsonfc007bb2010-03-24 23:14:32 +0000395 msg_cdbg("Resulting block protection : %s\n",
snelson8913d082010-02-26 05:48:29 +0000396 bpt[(status & 0x1c) >> 2]);
397}
398
399void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
400{
krause2eb76212011-01-17 07:50:42 +0000401 static const char *const bpt[] = {
snelson8913d082010-02-26 05:48:29 +0000402 "none",
403 "0x70000-0x7ffff",
404 "0x60000-0x7ffff",
405 "0x40000-0x7ffff",
406 "all blocks", "all blocks", "all blocks", "all blocks"
407 };
408 spi_prettyprint_status_register_sst25(status);
snelsonfc007bb2010-03-24 23:14:32 +0000409 msg_cdbg("Resulting block protection : %s\n",
snelson8913d082010-02-26 05:48:29 +0000410 bpt[(status & 0x1c) >> 2]);
411}
412
hailfinger7533bc82011-05-19 00:06:06 +0000413int spi_prettyprint_status_register(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +0000414{
415 uint8_t status;
416
417 status = spi_read_status_register();
snelsonfc007bb2010-03-24 23:14:32 +0000418 msg_cdbg("Chip status register is %02x\n", status);
snelson8913d082010-02-26 05:48:29 +0000419 switch (flash->manufacture_id) {
420 case ST_ID:
421 if (((flash->model_id & 0xff00) == 0x2000) ||
422 ((flash->model_id & 0xff00) == 0x2500))
423 spi_prettyprint_status_register_st_m25p(status);
424 break;
mhmd3c80cd2010-09-15 23:31:03 +0000425 case MACRONIX_ID:
snelson8913d082010-02-26 05:48:29 +0000426 if ((flash->model_id & 0xff00) == 0x2000)
427 spi_prettyprint_status_register_st_m25p(status);
428 break;
429 case SST_ID:
430 switch (flash->model_id) {
431 case 0x2541:
432 spi_prettyprint_status_register_sst25vf016(status);
433 break;
434 case 0x8d:
435 case 0x258d:
436 spi_prettyprint_status_register_sst25vf040b(status);
437 break;
438 default:
439 spi_prettyprint_status_register_sst25(status);
440 break;
441 }
442 break;
443 }
hailfinger7533bc82011-05-19 00:06:06 +0000444 return 0;
snelson8913d082010-02-26 05:48:29 +0000445}
446
447int spi_chip_erase_60(struct flashchip *flash)
448{
449 int result;
450 struct spi_command cmds[] = {
451 {
452 .writecnt = JEDEC_WREN_OUTSIZE,
453 .writearr = (const unsigned char[]){ JEDEC_WREN },
454 .readcnt = 0,
455 .readarr = NULL,
456 }, {
457 .writecnt = JEDEC_CE_60_OUTSIZE,
458 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
459 .readcnt = 0,
460 .readarr = NULL,
461 }, {
462 .writecnt = 0,
463 .writearr = NULL,
464 .readcnt = 0,
465 .readarr = NULL,
466 }};
467
snelson8913d082010-02-26 05:48:29 +0000468 result = spi_send_multicommand(cmds);
469 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000470 msg_cerr("%s failed during command execution\n",
snelson8913d082010-02-26 05:48:29 +0000471 __func__);
472 return result;
473 }
474 /* Wait until the Write-In-Progress bit is cleared.
475 * This usually takes 1-85 s, so wait in 1 s steps.
476 */
477 /* FIXME: We assume spi_read_status_register will never fail. */
478 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
479 programmer_delay(1000 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000480 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000481 return 0;
482}
483
484int spi_chip_erase_c7(struct flashchip *flash)
485{
486 int result;
487 struct spi_command cmds[] = {
488 {
489 .writecnt = JEDEC_WREN_OUTSIZE,
490 .writearr = (const unsigned char[]){ JEDEC_WREN },
491 .readcnt = 0,
492 .readarr = NULL,
493 }, {
494 .writecnt = JEDEC_CE_C7_OUTSIZE,
495 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
496 .readcnt = 0,
497 .readarr = NULL,
498 }, {
499 .writecnt = 0,
500 .writearr = NULL,
501 .readcnt = 0,
502 .readarr = NULL,
503 }};
504
snelson8913d082010-02-26 05:48:29 +0000505 result = spi_send_multicommand(cmds);
506 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000507 msg_cerr("%s failed during command execution\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000508 return result;
509 }
510 /* Wait until the Write-In-Progress bit is cleared.
511 * This usually takes 1-85 s, so wait in 1 s steps.
512 */
513 /* FIXME: We assume spi_read_status_register will never fail. */
514 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
515 programmer_delay(1000 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000516 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000517 return 0;
518}
519
snelson8913d082010-02-26 05:48:29 +0000520int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
521{
522 int result;
523 struct spi_command cmds[] = {
524 {
525 .writecnt = JEDEC_WREN_OUTSIZE,
526 .writearr = (const unsigned char[]){ JEDEC_WREN },
527 .readcnt = 0,
528 .readarr = NULL,
529 }, {
530 .writecnt = JEDEC_BE_52_OUTSIZE,
531 .writearr = (const unsigned char[]){
532 JEDEC_BE_52,
533 (addr >> 16) & 0xff,
534 (addr >> 8) & 0xff,
535 (addr & 0xff)
536 },
537 .readcnt = 0,
538 .readarr = NULL,
539 }, {
540 .writecnt = 0,
541 .writearr = NULL,
542 .readcnt = 0,
543 .readarr = NULL,
544 }};
545
546 result = spi_send_multicommand(cmds);
547 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000548 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000549 __func__, addr);
550 return result;
551 }
552 /* Wait until the Write-In-Progress bit is cleared.
553 * This usually takes 100-4000 ms, so wait in 100 ms steps.
554 */
555 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
556 programmer_delay(100 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000557 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000558 return 0;
559}
560
561/* Block size is usually
562 * 64k for Macronix
563 * 32k for SST
564 * 4-32k non-uniform for EON
565 */
566int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
567{
568 int result;
569 struct spi_command cmds[] = {
570 {
571 .writecnt = JEDEC_WREN_OUTSIZE,
572 .writearr = (const unsigned char[]){ JEDEC_WREN },
573 .readcnt = 0,
574 .readarr = NULL,
575 }, {
576 .writecnt = JEDEC_BE_D8_OUTSIZE,
577 .writearr = (const unsigned char[]){
578 JEDEC_BE_D8,
579 (addr >> 16) & 0xff,
580 (addr >> 8) & 0xff,
581 (addr & 0xff)
582 },
583 .readcnt = 0,
584 .readarr = NULL,
585 }, {
586 .writecnt = 0,
587 .writearr = NULL,
588 .readcnt = 0,
589 .readarr = NULL,
590 }};
591
592 result = spi_send_multicommand(cmds);
593 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000594 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000595 __func__, addr);
596 return result;
597 }
598 /* Wait until the Write-In-Progress bit is cleared.
599 * This usually takes 100-4000 ms, so wait in 100 ms steps.
600 */
601 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
602 programmer_delay(100 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000603 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000604 return 0;
605}
606
607/* Block size is usually
608 * 4k for PMC
609 */
610int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
611{
612 int result;
613 struct spi_command cmds[] = {
614 {
615 .writecnt = JEDEC_WREN_OUTSIZE,
616 .writearr = (const unsigned char[]){ JEDEC_WREN },
617 .readcnt = 0,
618 .readarr = NULL,
619 }, {
620 .writecnt = JEDEC_BE_D7_OUTSIZE,
621 .writearr = (const unsigned char[]){
622 JEDEC_BE_D7,
623 (addr >> 16) & 0xff,
624 (addr >> 8) & 0xff,
625 (addr & 0xff)
626 },
627 .readcnt = 0,
628 .readarr = NULL,
629 }, {
630 .writecnt = 0,
631 .writearr = NULL,
632 .readcnt = 0,
633 .readarr = NULL,
634 }};
635
636 result = spi_send_multicommand(cmds);
637 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000638 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000639 __func__, addr);
640 return result;
641 }
642 /* Wait until the Write-In-Progress bit is cleared.
643 * This usually takes 100-4000 ms, so wait in 100 ms steps.
644 */
645 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
646 programmer_delay(100 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000647 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000648 return 0;
649}
650
snelson8913d082010-02-26 05:48:29 +0000651/* Sector size is usually 4k, though Macronix eliteflash has 64k */
652int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
653{
654 int result;
655 struct spi_command cmds[] = {
656 {
657 .writecnt = JEDEC_WREN_OUTSIZE,
658 .writearr = (const unsigned char[]){ JEDEC_WREN },
659 .readcnt = 0,
660 .readarr = NULL,
661 }, {
662 .writecnt = JEDEC_SE_OUTSIZE,
663 .writearr = (const unsigned char[]){
664 JEDEC_SE,
665 (addr >> 16) & 0xff,
666 (addr >> 8) & 0xff,
667 (addr & 0xff)
668 },
669 .readcnt = 0,
670 .readarr = NULL,
671 }, {
672 .writecnt = 0,
673 .writearr = NULL,
674 .readcnt = 0,
675 .readarr = NULL,
676 }};
677
678 result = spi_send_multicommand(cmds);
Stefan Reinauercce56d52010-11-22 18:22:21 -0800679
snelson8913d082010-02-26 05:48:29 +0000680 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000681 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000682 __func__, addr);
683 return result;
684 }
685 /* Wait until the Write-In-Progress bit is cleared.
686 * This usually takes 15-800 ms, so wait in 10 ms steps.
687 */
688 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
689 programmer_delay(10 * 1000);
hailfingerac8e3182011-06-26 17:04:16 +0000690 /* FIXME: Check the status register for errors. */
snelson8913d082010-02-26 05:48:29 +0000691 return 0;
692}
693
694int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
695{
696 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000697 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000698 __func__);
699 return -1;
700 }
701 return spi_chip_erase_60(flash);
702}
703
704int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
705{
706 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000707 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000708 __func__);
709 return -1;
710 }
711 return spi_chip_erase_c7(flash);
712}
713
714int spi_write_status_enable(void)
715{
krause2eb76212011-01-17 07:50:42 +0000716 static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
snelson8913d082010-02-26 05:48:29 +0000717 int result;
718
719 /* Send EWSR (Enable Write Status Register). */
720 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
721
722 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000723 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000724
725 return result;
726}
727
728/*
729 * This is according the SST25VF016 datasheet, who knows it is more
730 * generic that this...
731 */
hailfingerc33d4732010-07-29 13:09:18 +0000732static int spi_write_status_register_ewsr(struct flashchip *flash, int status)
snelson8913d082010-02-26 05:48:29 +0000733{
734 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000735 int i = 0;
snelson8913d082010-02-26 05:48:29 +0000736 struct spi_command cmds[] = {
737 {
hailfingerc33d4732010-07-29 13:09:18 +0000738 /* WRSR requires either EWSR or WREN depending on chip type. */
snelson8913d082010-02-26 05:48:29 +0000739 .writecnt = JEDEC_EWSR_OUTSIZE,
740 .writearr = (const unsigned char[]){ JEDEC_EWSR },
741 .readcnt = 0,
742 .readarr = NULL,
743 }, {
744 .writecnt = JEDEC_WRSR_OUTSIZE,
745 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
746 .readcnt = 0,
747 .readarr = NULL,
748 }, {
749 .writecnt = 0,
750 .writearr = NULL,
751 .readcnt = 0,
752 .readarr = NULL,
753 }};
754
755 result = spi_send_multicommand(cmds);
756 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000757 msg_cerr("%s failed during command execution\n",
snelson8913d082010-02-26 05:48:29 +0000758 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000759 /* No point in waiting for the command to complete if execution
760 * failed.
761 */
762 return result;
snelson8913d082010-02-26 05:48:29 +0000763 }
hailfingeree9ee132010-10-08 00:37:55 +0000764 /* WRSR performs a self-timed erase before the changes take effect.
765 * This may take 50-85 ms in most cases, and some chips apparently
766 * allow running RDSR only once. Therefore pick an initial delay of
767 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
768 */
hailfingerc33d4732010-07-29 13:09:18 +0000769 programmer_delay(100 * 1000);
hailfingeree9ee132010-10-08 00:37:55 +0000770 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
771 if (++i > 490) {
772 msg_cerr("Error: WIP bit after WRSR never cleared\n");
773 return TIMEOUT_ERROR;
774 }
775 programmer_delay(10 * 1000);
776 }
777 return 0;
snelson8913d082010-02-26 05:48:29 +0000778}
779
hailfingerc33d4732010-07-29 13:09:18 +0000780static int spi_write_status_register_wren(struct flashchip *flash, int status)
781{
782 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000783 int i = 0;
hailfingerc33d4732010-07-29 13:09:18 +0000784 struct spi_command cmds[] = {
785 {
786 /* WRSR requires either EWSR or WREN depending on chip type. */
787 .writecnt = JEDEC_WREN_OUTSIZE,
788 .writearr = (const unsigned char[]){ JEDEC_WREN },
789 .readcnt = 0,
790 .readarr = NULL,
791 }, {
792 .writecnt = JEDEC_WRSR_OUTSIZE,
793 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
794 .readcnt = 0,
795 .readarr = NULL,
796 }, {
797 .writecnt = 0,
798 .writearr = NULL,
799 .readcnt = 0,
800 .readarr = NULL,
801 }};
802
803 result = spi_send_multicommand(cmds);
804 if (result) {
805 msg_cerr("%s failed during command execution\n",
806 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000807 /* No point in waiting for the command to complete if execution
808 * failed.
809 */
810 return result;
hailfingerc33d4732010-07-29 13:09:18 +0000811 }
hailfingeree9ee132010-10-08 00:37:55 +0000812 /* WRSR performs a self-timed erase before the changes take effect.
813 * This may take 50-85 ms in most cases, and some chips apparently
814 * allow running RDSR only once. Therefore pick an initial delay of
815 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
816 */
hailfingerc33d4732010-07-29 13:09:18 +0000817 programmer_delay(100 * 1000);
hailfingeree9ee132010-10-08 00:37:55 +0000818 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
819 if (++i > 490) {
820 msg_cerr("Error: WIP bit after WRSR never cleared\n");
821 return TIMEOUT_ERROR;
822 }
823 programmer_delay(10 * 1000);
824 }
825 return 0;
hailfingerc33d4732010-07-29 13:09:18 +0000826}
827
hailfinger7533bc82011-05-19 00:06:06 +0000828int spi_write_status_register(struct flashchip *flash, int status)
hailfingerc33d4732010-07-29 13:09:18 +0000829{
830 int ret = 1;
831
832 if (!(flash->feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
833 msg_cdbg("Missing status register write definition, assuming "
834 "EWSR is needed\n");
835 flash->feature_bits |= FEATURE_WRSR_EWSR;
836 }
837 if (flash->feature_bits & FEATURE_WRSR_WREN)
838 ret = spi_write_status_register_wren(flash, status);
839 if (ret && (flash->feature_bits & FEATURE_WRSR_EWSR))
840 ret = spi_write_status_register_ewsr(flash, status);
841 return ret;
842}
843
stefanctc5eb8a92011-11-23 09:13:48 +0000844int spi_byte_program(unsigned int addr, uint8_t databyte)
snelson8913d082010-02-26 05:48:29 +0000845{
846 int result;
847 struct spi_command cmds[] = {
848 {
849 .writecnt = JEDEC_WREN_OUTSIZE,
850 .writearr = (const unsigned char[]){ JEDEC_WREN },
851 .readcnt = 0,
852 .readarr = NULL,
853 }, {
854 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
855 .writearr = (const unsigned char[]){
856 JEDEC_BYTE_PROGRAM,
857 (addr >> 16) & 0xff,
858 (addr >> 8) & 0xff,
859 (addr & 0xff),
860 databyte
861 },
862 .readcnt = 0,
863 .readarr = NULL,
864 }, {
865 .writecnt = 0,
866 .writearr = NULL,
867 .readcnt = 0,
868 .readarr = NULL,
869 }};
870
871 result = spi_send_multicommand(cmds);
872 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000873 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000874 __func__, addr);
875 }
876 return result;
877}
878
stefanctc5eb8a92011-11-23 09:13:48 +0000879int spi_nbyte_program(unsigned int addr, uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000880{
881 int result;
882 /* FIXME: Switch to malloc based on len unless that kills speed. */
883 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
884 JEDEC_BYTE_PROGRAM,
885 (addr >> 16) & 0xff,
886 (addr >> 8) & 0xff,
887 (addr >> 0) & 0xff,
888 };
889 struct spi_command cmds[] = {
890 {
891 .writecnt = JEDEC_WREN_OUTSIZE,
892 .writearr = (const unsigned char[]){ JEDEC_WREN },
893 .readcnt = 0,
894 .readarr = NULL,
895 }, {
896 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
897 .writearr = cmd,
898 .readcnt = 0,
899 .readarr = NULL,
900 }, {
901 .writecnt = 0,
902 .writearr = NULL,
903 .readcnt = 0,
904 .readarr = NULL,
905 }};
906
907 if (!len) {
snelsonfc007bb2010-03-24 23:14:32 +0000908 msg_cerr("%s called for zero-length write\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000909 return 1;
910 }
911 if (len > 256) {
snelsonfc007bb2010-03-24 23:14:32 +0000912 msg_cerr("%s called for too long a write\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000913 return 1;
914 }
915
916 memcpy(&cmd[4], bytes, len);
917
918 result = spi_send_multicommand(cmds);
919 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000920 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000921 __func__, addr);
922 }
923 return result;
924}
925
David Hendricksbf36f092010-11-02 23:39:29 -0700926int spi_restore_status(struct flashchip *flash, uint8_t status)
927{
928 msg_cdbg("restoring chip status (0x%02x)\n", status);
929 return spi_write_status_register(flash, status);
930}
931
hailfingerc33d4732010-07-29 13:09:18 +0000932/* A generic brute-force block protection disable works like this:
933 * Write 0x00 to the status register. Check if any locks are still set (that
934 * part is chip specific). Repeat once.
935 */
hailfingerb9560ee2010-07-14 20:21:22 +0000936int spi_disable_blockprotect(struct flashchip *flash)
snelson8913d082010-02-26 05:48:29 +0000937{
938 uint8_t status;
939 int result;
940
941 status = spi_read_status_register();
hailfingerc33d4732010-07-29 13:09:18 +0000942 /* If block protection is disabled, stop here. */
943 if ((status & 0x3c) == 0)
944 return 0;
945
David Hendricksbf36f092010-11-02 23:39:29 -0700946 /* restore status register content upon exit */
947 register_chip_restore(spi_restore_status, flash, status);
948
hailfingerc33d4732010-07-29 13:09:18 +0000949 msg_cdbg("Some block protection in effect, disabling\n");
950 result = spi_write_status_register(flash, status & ~0x3c);
951 if (result) {
952 msg_cerr("spi_write_status_register failed\n");
953 return result;
954 }
955 status = spi_read_status_register();
snelson8913d082010-02-26 05:48:29 +0000956 if ((status & 0x3c) != 0) {
hailfingerc33d4732010-07-29 13:09:18 +0000957 msg_cerr("Block protection could not be disabled!\n");
958 return 1;
959 }
960 return 0;
961}
962
stefanctc5eb8a92011-11-23 09:13:48 +0000963int spi_nbyte_read(unsigned int address, uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000964{
965 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
966 JEDEC_READ,
967 (address >> 16) & 0xff,
968 (address >> 8) & 0xff,
969 (address >> 0) & 0xff,
970 };
971
972 /* Send Read */
973 return spi_send_command(sizeof(cmd), len, cmd, bytes);
974}
975
976/*
hailfinger39d159a2010-05-21 23:09:42 +0000977 * Read a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000978 * FIXME: Use the chunk code from Michael Karcher instead.
snelson8913d082010-02-26 05:48:29 +0000979 * Each page is read separately in chunks with a maximum size of chunksize.
980 */
stefanctc5eb8a92011-11-23 09:13:48 +0000981int spi_read_chunked(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
snelson8913d082010-02-26 05:48:29 +0000982{
983 int rc = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000984 unsigned int i, j, starthere, lenhere, toread;
985 unsigned int page_size = flash->page_size;
snelson8913d082010-02-26 05:48:29 +0000986
987 /* Warning: This loop has a very unusual condition and body.
988 * The loop needs to go through each page with at least one affected
989 * byte. The lowest page number is (start / page_size) since that
990 * division rounds down. The highest page number we want is the page
991 * where the last byte of the range lives. That last byte has the
992 * address (start + len - 1), thus the highest page number is
993 * (start + len - 1) / page_size. Since we want to include that last
994 * page as well, the loop condition uses <=.
995 */
996 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
997 /* Byte position of the first byte in the range in this page. */
998 /* starthere is an offset to the base address of the chip. */
999 starthere = max(start, i * page_size);
1000 /* Length of bytes in the range in this page. */
1001 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1002 for (j = 0; j < lenhere; j += chunksize) {
1003 toread = min(chunksize, lenhere - j);
1004 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
1005 if (rc)
1006 break;
1007 }
1008 if (rc)
1009 break;
1010 }
1011
1012 return rc;
1013}
1014
1015/*
hailfinger39d159a2010-05-21 23:09:42 +00001016 * Write a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +00001017 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +00001018 * Each page is written separately in chunks with a maximum size of chunksize.
1019 */
stefanctc5eb8a92011-11-23 09:13:48 +00001020int spi_write_chunked(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
hailfinger39d159a2010-05-21 23:09:42 +00001021{
1022 int rc = 0;
stefanctc5eb8a92011-11-23 09:13:48 +00001023 unsigned int i, j, starthere, lenhere, towrite;
hailfinger39d159a2010-05-21 23:09:42 +00001024 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
1025 * in struct flashchip to do this properly. All chips using
1026 * spi_chip_write_256 have page_size set to max_writechunk_size, so
1027 * we're OK for now.
1028 */
stefanctc5eb8a92011-11-23 09:13:48 +00001029 unsigned int page_size = flash->page_size;
hailfinger39d159a2010-05-21 23:09:42 +00001030
1031 /* Warning: This loop has a very unusual condition and body.
1032 * The loop needs to go through each page with at least one affected
1033 * byte. The lowest page number is (start / page_size) since that
1034 * division rounds down. The highest page number we want is the page
1035 * where the last byte of the range lives. That last byte has the
1036 * address (start + len - 1), thus the highest page number is
1037 * (start + len - 1) / page_size. Since we want to include that last
1038 * page as well, the loop condition uses <=.
1039 */
1040 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1041 /* Byte position of the first byte in the range in this page. */
1042 /* starthere is an offset to the base address of the chip. */
1043 starthere = max(start, i * page_size);
1044 /* Length of bytes in the range in this page. */
1045 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1046 for (j = 0; j < lenhere; j += chunksize) {
1047 towrite = min(chunksize, lenhere - j);
1048 rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite);
1049 if (rc)
1050 break;
1051 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1052 programmer_delay(10);
1053 }
1054 if (rc)
1055 break;
1056 }
1057
1058 return rc;
1059}
1060
1061/*
snelson8913d082010-02-26 05:48:29 +00001062 * Program chip using byte programming. (SLOW!)
1063 * This is for chips which can only handle one byte writes
1064 * and for chips where memory mapped programming is impossible
1065 * (e.g. due to size constraints in IT87* for over 512 kB)
1066 */
hailfingerc7d06c62010-07-14 16:19:05 +00001067/* real chunksize is 1, logical chunksize is 1 */
stefanctc5eb8a92011-11-23 09:13:48 +00001068int spi_chip_write_1(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len)
snelson8913d082010-02-26 05:48:29 +00001069{
stefanctc5eb8a92011-11-23 09:13:48 +00001070 unsigned int i;
1071 int result = 0;
snelson8913d082010-02-26 05:48:29 +00001072
hailfingerc7d06c62010-07-14 16:19:05 +00001073 for (i = start; i < start + len; i++) {
hailfingerdef852d2010-10-27 22:07:11 +00001074 result = spi_byte_program(i, buf[i - start]);
snelson8913d082010-02-26 05:48:29 +00001075 if (result)
1076 return 1;
1077 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1078 programmer_delay(10);
1079 }
1080
1081 return 0;
1082}
1083
stefanctc5eb8a92011-11-23 09:13:48 +00001084int spi_aai_write(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +00001085{
1086 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +00001087 int result;
hailfinger19db0922010-06-20 10:41:35 +00001088 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1089 JEDEC_AAI_WORD_PROGRAM,
1090 };
1091 struct spi_command cmds[] = {
1092 {
1093 .writecnt = JEDEC_WREN_OUTSIZE,
1094 .writearr = (const unsigned char[]){ JEDEC_WREN },
1095 .readcnt = 0,
1096 .readarr = NULL,
1097 }, {
1098 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1099 .writearr = (const unsigned char[]){
1100 JEDEC_AAI_WORD_PROGRAM,
hailfingerc7d06c62010-07-14 16:19:05 +00001101 (start >> 16) & 0xff,
1102 (start >> 8) & 0xff,
1103 (start & 0xff),
hailfinger19db0922010-06-20 10:41:35 +00001104 buf[0],
1105 buf[1]
1106 },
1107 .readcnt = 0,
1108 .readarr = NULL,
1109 }, {
1110 .writecnt = 0,
1111 .writearr = NULL,
1112 .readcnt = 0,
1113 .readarr = NULL,
1114 }};
snelson8913d082010-02-26 05:48:29 +00001115
mkarcherd264e9e2011-05-11 17:07:07 +00001116 switch (spi_programmer->type) {
hailfinger90c7d542010-05-31 15:27:27 +00001117#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +00001118#if defined(__i386__) || defined(__x86_64__)
hailfinger19db0922010-06-20 10:41:35 +00001119 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +00001120 case SPI_CONTROLLER_WBSIO:
hailfingerc7d06c62010-07-14 16:19:05 +00001121 msg_perr("%s: impossible with this SPI controller,"
snelson8913d082010-02-26 05:48:29 +00001122 " degrading to byte program\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +00001123 return spi_chip_write_1(flash, buf, start, len);
snelson8913d082010-02-26 05:48:29 +00001124#endif
hailfinger324a9cc2010-05-26 01:45:41 +00001125#endif
snelson8913d082010-02-26 05:48:29 +00001126 default:
1127 break;
1128 }
hailfinger19db0922010-06-20 10:41:35 +00001129
hailfingerc7d06c62010-07-14 16:19:05 +00001130 /* The even start address and even length requirements can be either
1131 * honored outside this function, or we can call spi_byte_program
1132 * for the first and/or last byte and use AAI for the rest.
hailfinger71e1bd42010-10-13 22:26:56 +00001133 * FIXME: Move this to generic code.
hailfingerc7d06c62010-07-14 16:19:05 +00001134 */
hailfinger19db0922010-06-20 10:41:35 +00001135 /* The data sheet requires a start address with the low bit cleared. */
hailfingerc7d06c62010-07-14 16:19:05 +00001136 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +00001137 msg_cerr("%s: start address not even! Please report a bug at "
1138 "flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +00001139 if (spi_chip_write_1(flash, buf, start, start % 2))
1140 return SPI_GENERIC_ERROR;
1141 pos += start % 2;
hailfingerdef852d2010-10-27 22:07:11 +00001142 cmds[1].writearr = (const unsigned char[]){
1143 JEDEC_AAI_WORD_PROGRAM,
1144 (pos >> 16) & 0xff,
1145 (pos >> 8) & 0xff,
1146 (pos & 0xff),
1147 buf[pos - start],
1148 buf[pos - start + 1]
1149 };
hailfinger71e1bd42010-10-13 22:26:56 +00001150 /* Do not return an error for now. */
1151 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +00001152 }
1153 /* The data sheet requires total AAI write length to be even. */
1154 if (len % 2) {
1155 msg_cerr("%s: total write length not even! Please report a "
1156 "bug at flashrom@flashrom.org\n", __func__);
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
hailfinger19db0922010-06-20 10:41:35 +00001161
1162 result = spi_send_multicommand(cmds);
1163 if (result) {
1164 msg_cerr("%s failed during start command execution\n",
1165 __func__);
hailfingerc7d06c62010-07-14 16:19:05 +00001166 /* FIXME: Should we send WRDI here as well to make sure the chip
1167 * is not in AAI mode?
1168 */
snelson8913d082010-02-26 05:48:29 +00001169 return result;
snelson8913d082010-02-26 05:48:29 +00001170 }
hailfinger19db0922010-06-20 10:41:35 +00001171 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1172 programmer_delay(10);
1173
1174 /* We already wrote 2 bytes in the multicommand step. */
1175 pos += 2;
1176
hailfinger71e1bd42010-10-13 22:26:56 +00001177 /* Are there at least two more bytes to write? */
1178 while (pos < start + len - 1) {
hailfingerdef852d2010-10-27 22:07:11 +00001179 cmd[1] = buf[pos++ - start];
1180 cmd[2] = buf[pos++ - start];
hailfinger19db0922010-06-20 10:41:35 +00001181 spi_send_command(JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
1182 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1183 programmer_delay(10);
1184 }
1185
hailfinger71e1bd42010-10-13 22:26:56 +00001186 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
1187 * other non-AAI command.
1188 */
snelson8913d082010-02-26 05:48:29 +00001189 spi_write_disable();
hailfinger71e1bd42010-10-13 22:26:56 +00001190
1191 /* Write remaining byte (if any). */
1192 if (pos < start + len) {
hailfingerdef852d2010-10-27 22:07:11 +00001193 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
hailfinger71e1bd42010-10-13 22:26:56 +00001194 return SPI_GENERIC_ERROR;
1195 pos += pos % 2;
1196 }
1197
snelson8913d082010-02-26 05:48:29 +00001198 return 0;
1199}