blob: 0ba19948066aa1e4a95313ff62d05b23cae3b371 [file] [log] [blame]
Sean Nelson14ba6682010-02-26 05:48:29 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00004 * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger
Sean Nelson14ba6682010-02-26 05:48:29 +00005 * Copyright (C) 2008 coresystems GmbH
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * Contains the common SPI chip driver functions
23 */
24
25#include <string.h>
26#include "flash.h"
27#include "flashchips.h"
28#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000029#include "programmer.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000030#include "spi.h"
31
Sean Nelson14ba6682010-02-26 05:48:29 +000032static int spi_rdid(unsigned char *readarr, int bytes)
33{
Mathias Krausea60faab2011-01-17 07:50:42 +000034 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Sean Nelson14ba6682010-02-26 05:48:29 +000035 int ret;
36 int i;
37
38 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
39 if (ret)
40 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000041 msg_cspew("RDID returned");
Sean Nelson14ba6682010-02-26 05:48:29 +000042 for (i = 0; i < bytes; i++)
Sean Nelsoned479d22010-03-24 23:14:32 +000043 msg_cspew(" 0x%02x", readarr[i]);
44 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000045 return 0;
46}
47
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;
Sean Nelsoned479d22010-03-24 23:14:32 +000065 msg_cspew("REMS returned %02x %02x. ", readarr[0], readarr[1]);
Sean Nelson14ba6682010-02-26 05:48:29 +000066 return 0;
67}
68
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000069static int spi_res(unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-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;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000074 int i;
Sean Nelson14ba6682010-02-26 05:48:29 +000075
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000076 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-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,
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000083 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000084 }
85 if (ret)
86 return ret;
Carl-Daniel Hailfinger8ae500e2010-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(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000091 return 0;
92}
93
94int spi_write_enable(void)
95{
Mathias Krausea60faab2011-01-17 07:50:42 +000096 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Sean Nelson14ba6682010-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)
Sean Nelsoned479d22010-03-24 23:14:32 +0000103 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000104
105 return result;
106}
107
108int spi_write_disable(void)
109{
Mathias Krausea60faab2011-01-17 07:50:42 +0000110 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Sean Nelson14ba6682010-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
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000122 if (spi_rdid(readarr, bytes)) {
123 msg_cdbg("\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000124 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000125 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000126
127 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000128 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000129
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000130 /* Check if this is a continuation vendor ID.
131 * FIXME: Handle continuation device IDs.
132 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000133 if (readarr[0] == 0x7f) {
134 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000135 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-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
Sean Nelsoned479d22010-03-24 23:14:32 +0000147 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-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
Sean Nelson14ba6682010-02-26 05:48:29 +0000176int probe_spi_rdid4(struct flashchip *flash)
177{
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000178 /* Some SPI controllers do not support commands with writecnt=1 and
179 * readcnt=4.
180 */
Michael Karcherb9dbe482011-05-11 17:07:07 +0000181 switch (spi_programmer->type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000182#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000183#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000184 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000185 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000186 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
187 return 0;
188 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000189#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000190#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000191 default:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000192 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000193 }
194
195 return 0;
196}
197
198int probe_spi_rems(struct flashchip *flash)
199{
200 unsigned char readarr[JEDEC_REMS_INSIZE];
201 uint32_t id1, id2;
202
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000203 if (spi_rems(readarr)) {
204 msg_cdbg("\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000205 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000206 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000207
208 id1 = readarr[0];
209 id2 = readarr[1];
210
Sean Nelsoned479d22010-03-24 23:14:32 +0000211 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000212
213 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
214 /* Print the status register to tell the
215 * user about possible write protection.
216 */
217 spi_prettyprint_status_register(flash);
218
219 return 1;
220 }
221
222 /* Test if this is a pure vendor match. */
223 if (id1 == flash->manufacture_id &&
224 GENERIC_DEVICE_ID == flash->model_id)
225 return 1;
226
227 /* Test if there is any vendor ID. */
228 if (GENERIC_MANUF_ID == flash->manufacture_id &&
229 id1 != 0xff)
230 return 1;
231
232 return 0;
233}
234
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000235int probe_spi_res1(struct flashchip *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000236{
Mathias Krausea60faab2011-01-17 07:50:42 +0000237 static const unsigned char allff[] = {0xff, 0xff, 0xff};
238 static const unsigned char all00[] = {0x00, 0x00, 0x00};
Sean Nelson14ba6682010-02-26 05:48:29 +0000239 unsigned char readarr[3];
240 uint32_t id2;
Sean Nelson14ba6682010-02-26 05:48:29 +0000241
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000242 /* We only want one-byte RES if RDID and REMS are unusable. */
243
Sean Nelson14ba6682010-02-26 05:48:29 +0000244 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
245 * 0x00 0x00 0x00. In that case, RES is pointless.
246 */
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
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000261 if (spi_res(readarr, 1)) {
262 msg_cdbg("\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000263 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000264 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000265
Sean Nelson14ba6682010-02-26 05:48:29 +0000266 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000267
Sean Nelsoned479d22010-03-24 23:14:32 +0000268 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000269
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000270 if (id2 != flash->model_id) {
271 msg_cdbg("\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000272 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000273 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000274
275 /* Print the status register to tell the
276 * user about possible write protection.
277 */
278 spi_prettyprint_status_register(flash);
279 return 1;
280}
281
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000282int probe_spi_res2(struct flashchip *flash)
283{
284 unsigned char readarr[2];
285 uint32_t id1, id2;
286
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000287 if (spi_res(readarr, 2)) {
288 msg_cdbg("\n");
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000289 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000290 }
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000291
292 id1 = readarr[0];
293 id2 = readarr[1];
294
295 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
296
297 if (id1 != flash->manufacture_id || id2 != flash->model_id)
298 return 0;
299
300 /* Print the status register to tell the
301 * user about possible write protection.
302 */
303 spi_prettyprint_status_register(flash);
304 return 1;
305}
306
Sean Nelson14ba6682010-02-26 05:48:29 +0000307uint8_t spi_read_status_register(void)
308{
Mathias Krausea60faab2011-01-17 07:50:42 +0000309 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Sean Nelson14ba6682010-02-26 05:48:29 +0000310 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
311 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
312 int ret;
313
314 /* Read Status Register */
315 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
316 if (ret)
Sean Nelsoned479d22010-03-24 23:14:32 +0000317 msg_cerr("RDSR failed!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000318
319 return readarr[0];
320}
321
322/* Prettyprint the status register. Common definitions. */
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000323void spi_prettyprint_status_register_welwip(uint8_t status)
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000324{
325 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
326 "%sset\n", (status & (1 << 1)) ? "" : "not ");
327 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
328 "%sset\n", (status & (1 << 0)) ? "" : "not ");
329}
330
331/* Prettyprint the status register. Common definitions. */
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000332void spi_prettyprint_status_register_bp3210(uint8_t status, int bp)
333{
334 switch (bp) {
335 /* Fall through. */
336 case 3:
337 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) "
338 "is %sset\n", (status & (1 << 5)) ? "" : "not ");
339 case 2:
340 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) "
341 "is %sset\n", (status & (1 << 4)) ? "" : "not ");
342 case 1:
343 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) "
344 "is %sset\n", (status & (1 << 3)) ? "" : "not ");
345 case 0:
346 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) "
347 "is %sset\n", (status & (1 << 2)) ? "" : "not ");
348 }
349}
350
351/* Prettyprint the status register. Unnamed bits. */
352void spi_prettyprint_status_register_bit(uint8_t status, int bit)
353{
354 msg_cdbg("Chip status register: Bit %i "
355 "is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
356}
357
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000358static void spi_prettyprint_status_register_common(uint8_t status)
Sean Nelson14ba6682010-02-26 05:48:29 +0000359{
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000360 spi_prettyprint_status_register_bp3210(status, 3);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000361 spi_prettyprint_status_register_welwip(status);
Sean Nelson14ba6682010-02-26 05:48:29 +0000362}
363
364/* Prettyprint the status register. Works for
365 * ST M25P series
366 * MX MX25L series
367 */
368void spi_prettyprint_status_register_st_m25p(uint8_t status)
369{
Sean Nelsoned479d22010-03-24 23:14:32 +0000370 msg_cdbg("Chip status register: Status Register Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000371 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000372 msg_cdbg("Chip status register: Bit 6 is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000373 "%sset\n", (status & (1 << 6)) ? "" : "not ");
374 spi_prettyprint_status_register_common(status);
375}
376
377void spi_prettyprint_status_register_sst25(uint8_t status)
378{
Sean Nelsoned479d22010-03-24 23:14:32 +0000379 msg_cdbg("Chip status register: Block Protect Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000380 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000381 msg_cdbg("Chip status register: Auto Address Increment Programming "
Sean Nelson14ba6682010-02-26 05:48:29 +0000382 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
383 spi_prettyprint_status_register_common(status);
384}
385
386/* Prettyprint the status register. Works for
387 * SST 25VF016
388 */
389void spi_prettyprint_status_register_sst25vf016(uint8_t status)
390{
Mathias Krausea60faab2011-01-17 07:50:42 +0000391 static const char *const bpt[] = {
Sean Nelson14ba6682010-02-26 05:48:29 +0000392 "none",
393 "1F0000H-1FFFFFH",
394 "1E0000H-1FFFFFH",
395 "1C0000H-1FFFFFH",
396 "180000H-1FFFFFH",
397 "100000H-1FFFFFH",
398 "all", "all"
399 };
400 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000401 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000402 bpt[(status & 0x1c) >> 2]);
403}
404
405void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
406{
Mathias Krausea60faab2011-01-17 07:50:42 +0000407 static const char *const bpt[] = {
Sean Nelson14ba6682010-02-26 05:48:29 +0000408 "none",
409 "0x70000-0x7ffff",
410 "0x60000-0x7ffff",
411 "0x40000-0x7ffff",
412 "all blocks", "all blocks", "all blocks", "all blocks"
413 };
414 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000415 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000416 bpt[(status & 0x1c) >> 2]);
417}
418
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000419int spi_prettyprint_status_register(struct flashchip *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000420{
421 uint8_t status;
422
423 status = spi_read_status_register();
Sean Nelsoned479d22010-03-24 23:14:32 +0000424 msg_cdbg("Chip status register is %02x\n", status);
Sean Nelson14ba6682010-02-26 05:48:29 +0000425 switch (flash->manufacture_id) {
426 case ST_ID:
427 if (((flash->model_id & 0xff00) == 0x2000) ||
428 ((flash->model_id & 0xff00) == 0x2500))
429 spi_prettyprint_status_register_st_m25p(status);
430 break;
Mattias Mattsson6eabe282010-09-15 23:31:03 +0000431 case MACRONIX_ID:
Sean Nelson14ba6682010-02-26 05:48:29 +0000432 if ((flash->model_id & 0xff00) == 0x2000)
433 spi_prettyprint_status_register_st_m25p(status);
434 break;
435 case SST_ID:
436 switch (flash->model_id) {
437 case 0x2541:
438 spi_prettyprint_status_register_sst25vf016(status);
439 break;
440 case 0x8d:
441 case 0x258d:
442 spi_prettyprint_status_register_sst25vf040b(status);
443 break;
444 default:
445 spi_prettyprint_status_register_sst25(status);
446 break;
447 }
448 break;
449 }
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000450 return 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000451}
452
453int spi_chip_erase_60(struct flashchip *flash)
454{
455 int result;
456 struct spi_command cmds[] = {
457 {
458 .writecnt = JEDEC_WREN_OUTSIZE,
459 .writearr = (const unsigned char[]){ JEDEC_WREN },
460 .readcnt = 0,
461 .readarr = NULL,
462 }, {
463 .writecnt = JEDEC_CE_60_OUTSIZE,
464 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
465 .readcnt = 0,
466 .readarr = NULL,
467 }, {
468 .writecnt = 0,
469 .writearr = NULL,
470 .readcnt = 0,
471 .readarr = NULL,
472 }};
473
Sean Nelson14ba6682010-02-26 05:48:29 +0000474 result = spi_send_multicommand(cmds);
475 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000476 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000477 __func__);
478 return result;
479 }
480 /* Wait until the Write-In-Progress bit is cleared.
481 * This usually takes 1-85 s, so wait in 1 s steps.
482 */
483 /* FIXME: We assume spi_read_status_register will never fail. */
484 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
485 programmer_delay(1000 * 1000);
486 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000487 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000488 return -1;
489 }
490 return 0;
491}
492
493int spi_chip_erase_c7(struct flashchip *flash)
494{
495 int result;
496 struct spi_command cmds[] = {
497 {
498 .writecnt = JEDEC_WREN_OUTSIZE,
499 .writearr = (const unsigned char[]){ JEDEC_WREN },
500 .readcnt = 0,
501 .readarr = NULL,
502 }, {
503 .writecnt = JEDEC_CE_C7_OUTSIZE,
504 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
505 .readcnt = 0,
506 .readarr = NULL,
507 }, {
508 .writecnt = 0,
509 .writearr = NULL,
510 .readcnt = 0,
511 .readarr = NULL,
512 }};
513
Sean Nelson14ba6682010-02-26 05:48:29 +0000514 result = spi_send_multicommand(cmds);
515 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000516 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000517 return result;
518 }
519 /* Wait until the Write-In-Progress bit is cleared.
520 * This usually takes 1-85 s, so wait in 1 s steps.
521 */
522 /* FIXME: We assume spi_read_status_register will never fail. */
523 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
524 programmer_delay(1000 * 1000);
525 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000526 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000527 return -1;
528 }
529 return 0;
530}
531
Sean Nelson14ba6682010-02-26 05:48:29 +0000532int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
533{
534 int result;
535 struct spi_command cmds[] = {
536 {
537 .writecnt = JEDEC_WREN_OUTSIZE,
538 .writearr = (const unsigned char[]){ JEDEC_WREN },
539 .readcnt = 0,
540 .readarr = NULL,
541 }, {
542 .writecnt = JEDEC_BE_52_OUTSIZE,
543 .writearr = (const unsigned char[]){
544 JEDEC_BE_52,
545 (addr >> 16) & 0xff,
546 (addr >> 8) & 0xff,
547 (addr & 0xff)
548 },
549 .readcnt = 0,
550 .readarr = NULL,
551 }, {
552 .writecnt = 0,
553 .writearr = NULL,
554 .readcnt = 0,
555 .readarr = NULL,
556 }};
557
558 result = spi_send_multicommand(cmds);
559 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000560 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000561 __func__, addr);
562 return result;
563 }
564 /* Wait until the Write-In-Progress bit is cleared.
565 * This usually takes 100-4000 ms, so wait in 100 ms steps.
566 */
567 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
568 programmer_delay(100 * 1000);
569 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000570 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000571 return -1;
572 }
573 return 0;
574}
575
576/* Block size is usually
577 * 64k for Macronix
578 * 32k for SST
579 * 4-32k non-uniform for EON
580 */
581int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
582{
583 int result;
584 struct spi_command cmds[] = {
585 {
586 .writecnt = JEDEC_WREN_OUTSIZE,
587 .writearr = (const unsigned char[]){ JEDEC_WREN },
588 .readcnt = 0,
589 .readarr = NULL,
590 }, {
591 .writecnt = JEDEC_BE_D8_OUTSIZE,
592 .writearr = (const unsigned char[]){
593 JEDEC_BE_D8,
594 (addr >> 16) & 0xff,
595 (addr >> 8) & 0xff,
596 (addr & 0xff)
597 },
598 .readcnt = 0,
599 .readarr = NULL,
600 }, {
601 .writecnt = 0,
602 .writearr = NULL,
603 .readcnt = 0,
604 .readarr = NULL,
605 }};
606
607 result = spi_send_multicommand(cmds);
608 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000609 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000610 __func__, addr);
611 return result;
612 }
613 /* Wait until the Write-In-Progress bit is cleared.
614 * This usually takes 100-4000 ms, so wait in 100 ms steps.
615 */
616 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
617 programmer_delay(100 * 1000);
618 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000619 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000620 return -1;
621 }
622 return 0;
623}
624
625/* Block size is usually
626 * 4k for PMC
627 */
628int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
629{
630 int result;
631 struct spi_command cmds[] = {
632 {
633 .writecnt = JEDEC_WREN_OUTSIZE,
634 .writearr = (const unsigned char[]){ JEDEC_WREN },
635 .readcnt = 0,
636 .readarr = NULL,
637 }, {
638 .writecnt = JEDEC_BE_D7_OUTSIZE,
639 .writearr = (const unsigned char[]){
640 JEDEC_BE_D7,
641 (addr >> 16) & 0xff,
642 (addr >> 8) & 0xff,
643 (addr & 0xff)
644 },
645 .readcnt = 0,
646 .readarr = NULL,
647 }, {
648 .writecnt = 0,
649 .writearr = NULL,
650 .readcnt = 0,
651 .readarr = NULL,
652 }};
653
654 result = spi_send_multicommand(cmds);
655 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000656 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000657 __func__, addr);
658 return result;
659 }
660 /* Wait until the Write-In-Progress bit is cleared.
661 * This usually takes 100-4000 ms, so wait in 100 ms steps.
662 */
663 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
664 programmer_delay(100 * 1000);
665 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000666 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000667 return -1;
668 }
669 return 0;
670}
671
Sean Nelson14ba6682010-02-26 05:48:29 +0000672/* Sector size is usually 4k, though Macronix eliteflash has 64k */
673int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
674{
675 int result;
676 struct spi_command cmds[] = {
677 {
678 .writecnt = JEDEC_WREN_OUTSIZE,
679 .writearr = (const unsigned char[]){ JEDEC_WREN },
680 .readcnt = 0,
681 .readarr = NULL,
682 }, {
683 .writecnt = JEDEC_SE_OUTSIZE,
684 .writearr = (const unsigned char[]){
685 JEDEC_SE,
686 (addr >> 16) & 0xff,
687 (addr >> 8) & 0xff,
688 (addr & 0xff)
689 },
690 .readcnt = 0,
691 .readarr = NULL,
692 }, {
693 .writecnt = 0,
694 .writearr = NULL,
695 .readcnt = 0,
696 .readarr = NULL,
697 }};
698
699 result = spi_send_multicommand(cmds);
700 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000701 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000702 __func__, addr);
703 return result;
704 }
705 /* Wait until the Write-In-Progress bit is cleared.
706 * This usually takes 15-800 ms, so wait in 10 ms steps.
707 */
708 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
709 programmer_delay(10 * 1000);
710 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000711 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000712 return -1;
713 }
714 return 0;
715}
716
717int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
718{
719 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000720 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000721 __func__);
722 return -1;
723 }
724 return spi_chip_erase_60(flash);
725}
726
727int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
728{
729 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000730 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000731 __func__);
732 return -1;
733 }
734 return spi_chip_erase_c7(flash);
735}
736
737int spi_write_status_enable(void)
738{
Mathias Krausea60faab2011-01-17 07:50:42 +0000739 static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Sean Nelson14ba6682010-02-26 05:48:29 +0000740 int result;
741
742 /* Send EWSR (Enable Write Status Register). */
743 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
744
745 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000746 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000747
748 return result;
749}
750
751/*
752 * This is according the SST25VF016 datasheet, who knows it is more
753 * generic that this...
754 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000755static int spi_write_status_register_ewsr(struct flashchip *flash, int status)
Sean Nelson14ba6682010-02-26 05:48:29 +0000756{
757 int result;
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000758 int i = 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000759 struct spi_command cmds[] = {
760 {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000761 /* WRSR requires either EWSR or WREN depending on chip type. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000762 .writecnt = JEDEC_EWSR_OUTSIZE,
763 .writearr = (const unsigned char[]){ JEDEC_EWSR },
764 .readcnt = 0,
765 .readarr = NULL,
766 }, {
767 .writecnt = JEDEC_WRSR_OUTSIZE,
768 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
769 .readcnt = 0,
770 .readarr = NULL,
771 }, {
772 .writecnt = 0,
773 .writearr = NULL,
774 .readcnt = 0,
775 .readarr = NULL,
776 }};
777
778 result = spi_send_multicommand(cmds);
779 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000780 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000781 __func__);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000782 /* No point in waiting for the command to complete if execution
783 * failed.
784 */
785 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +0000786 }
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000787 /* WRSR performs a self-timed erase before the changes take effect.
788 * This may take 50-85 ms in most cases, and some chips apparently
789 * allow running RDSR only once. Therefore pick an initial delay of
790 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
791 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000792 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000793 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
794 if (++i > 490) {
795 msg_cerr("Error: WIP bit after WRSR never cleared\n");
796 return TIMEOUT_ERROR;
797 }
798 programmer_delay(10 * 1000);
799 }
800 return 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000801}
802
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000803static int spi_write_status_register_wren(struct flashchip *flash, int status)
804{
805 int result;
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000806 int i = 0;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000807 struct spi_command cmds[] = {
808 {
809 /* WRSR requires either EWSR or WREN depending on chip type. */
810 .writecnt = JEDEC_WREN_OUTSIZE,
811 .writearr = (const unsigned char[]){ JEDEC_WREN },
812 .readcnt = 0,
813 .readarr = NULL,
814 }, {
815 .writecnt = JEDEC_WRSR_OUTSIZE,
816 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
817 .readcnt = 0,
818 .readarr = NULL,
819 }, {
820 .writecnt = 0,
821 .writearr = NULL,
822 .readcnt = 0,
823 .readarr = NULL,
824 }};
825
826 result = spi_send_multicommand(cmds);
827 if (result) {
828 msg_cerr("%s failed during command execution\n",
829 __func__);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000830 /* No point in waiting for the command to complete if execution
831 * failed.
832 */
833 return result;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000834 }
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000835 /* WRSR performs a self-timed erase before the changes take effect.
836 * This may take 50-85 ms in most cases, and some chips apparently
837 * allow running RDSR only once. Therefore pick an initial delay of
838 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
839 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000840 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000841 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
842 if (++i > 490) {
843 msg_cerr("Error: WIP bit after WRSR never cleared\n");
844 return TIMEOUT_ERROR;
845 }
846 programmer_delay(10 * 1000);
847 }
848 return 0;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000849}
850
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000851int spi_write_status_register(struct flashchip *flash, int status)
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000852{
853 int ret = 1;
854
855 if (!(flash->feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
856 msg_cdbg("Missing status register write definition, assuming "
857 "EWSR is needed\n");
858 flash->feature_bits |= FEATURE_WRSR_EWSR;
859 }
860 if (flash->feature_bits & FEATURE_WRSR_WREN)
861 ret = spi_write_status_register_wren(flash, status);
862 if (ret && (flash->feature_bits & FEATURE_WRSR_EWSR))
863 ret = spi_write_status_register_ewsr(flash, status);
864 return ret;
865}
866
Sean Nelson14ba6682010-02-26 05:48:29 +0000867int spi_byte_program(int addr, uint8_t databyte)
868{
869 int result;
870 struct spi_command cmds[] = {
871 {
872 .writecnt = JEDEC_WREN_OUTSIZE,
873 .writearr = (const unsigned char[]){ JEDEC_WREN },
874 .readcnt = 0,
875 .readarr = NULL,
876 }, {
877 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
878 .writearr = (const unsigned char[]){
879 JEDEC_BYTE_PROGRAM,
880 (addr >> 16) & 0xff,
881 (addr >> 8) & 0xff,
882 (addr & 0xff),
883 databyte
884 },
885 .readcnt = 0,
886 .readarr = NULL,
887 }, {
888 .writecnt = 0,
889 .writearr = NULL,
890 .readcnt = 0,
891 .readarr = NULL,
892 }};
893
894 result = spi_send_multicommand(cmds);
895 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000896 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000897 __func__, addr);
898 }
899 return result;
900}
901
902int spi_nbyte_program(int addr, uint8_t *bytes, int len)
903{
904 int result;
905 /* FIXME: Switch to malloc based on len unless that kills speed. */
906 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
907 JEDEC_BYTE_PROGRAM,
908 (addr >> 16) & 0xff,
909 (addr >> 8) & 0xff,
910 (addr >> 0) & 0xff,
911 };
912 struct spi_command cmds[] = {
913 {
914 .writecnt = JEDEC_WREN_OUTSIZE,
915 .writearr = (const unsigned char[]){ JEDEC_WREN },
916 .readcnt = 0,
917 .readarr = NULL,
918 }, {
919 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
920 .writearr = cmd,
921 .readcnt = 0,
922 .readarr = NULL,
923 }, {
924 .writecnt = 0,
925 .writearr = NULL,
926 .readcnt = 0,
927 .readarr = NULL,
928 }};
929
930 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000931 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000932 return 1;
933 }
934 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000935 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000936 return 1;
937 }
938
939 memcpy(&cmd[4], bytes, len);
940
941 result = spi_send_multicommand(cmds);
942 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000943 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000944 __func__, addr);
945 }
946 return result;
947}
948
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000949/* A generic brute-force block protection disable works like this:
950 * Write 0x00 to the status register. Check if any locks are still set (that
951 * part is chip specific). Repeat once.
952 */
Carl-Daniel Hailfinger29a1c662010-07-14 20:21:22 +0000953int spi_disable_blockprotect(struct flashchip *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000954{
955 uint8_t status;
956 int result;
957
958 status = spi_read_status_register();
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000959 /* If block protection is disabled, stop here. */
960 if ((status & 0x3c) == 0)
961 return 0;
962
963 msg_cdbg("Some block protection in effect, disabling\n");
964 result = spi_write_status_register(flash, status & ~0x3c);
965 if (result) {
966 msg_cerr("spi_write_status_register failed\n");
967 return result;
968 }
969 status = spi_read_status_register();
Sean Nelson14ba6682010-02-26 05:48:29 +0000970 if ((status & 0x3c) != 0) {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000971 msg_cerr("Block protection could not be disabled!\n");
972 return 1;
973 }
974 return 0;
975}
976
Sean Nelson14ba6682010-02-26 05:48:29 +0000977int spi_nbyte_read(int address, uint8_t *bytes, int len)
978{
979 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
980 JEDEC_READ,
981 (address >> 16) & 0xff,
982 (address >> 8) & 0xff,
983 (address >> 0) & 0xff,
984 };
985
986 /* Send Read */
987 return spi_send_command(sizeof(cmd), len, cmd, bytes);
988}
989
990/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000991 * Read a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000992 * FIXME: Use the chunk code from Michael Karcher instead.
Sean Nelson14ba6682010-02-26 05:48:29 +0000993 * Each page is read separately in chunks with a maximum size of chunksize.
994 */
995int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
996{
997 int rc = 0;
998 int i, j, starthere, lenhere;
999 int page_size = flash->page_size;
1000 int toread;
1001
1002 /* Warning: This loop has a very unusual condition and body.
1003 * The loop needs to go through each page with at least one affected
1004 * byte. The lowest page number is (start / page_size) since that
1005 * division rounds down. The highest page number we want is the page
1006 * where the last byte of the range lives. That last byte has the
1007 * address (start + len - 1), thus the highest page number is
1008 * (start + len - 1) / page_size. Since we want to include that last
1009 * page as well, the loop condition uses <=.
1010 */
1011 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1012 /* Byte position of the first byte in the range in this page. */
1013 /* starthere is an offset to the base address of the chip. */
1014 starthere = max(start, i * page_size);
1015 /* Length of bytes in the range in this page. */
1016 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1017 for (j = 0; j < lenhere; j += chunksize) {
1018 toread = min(chunksize, lenhere - j);
1019 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
1020 if (rc)
1021 break;
1022 }
1023 if (rc)
1024 break;
1025 }
1026
1027 return rc;
1028}
1029
1030/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001031 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001032 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001033 * Each page is written separately in chunks with a maximum size of chunksize.
1034 */
1035int spi_write_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
1036{
1037 int rc = 0;
1038 int i, j, starthere, lenhere;
1039 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
1040 * in struct flashchip to do this properly. All chips using
1041 * spi_chip_write_256 have page_size set to max_writechunk_size, so
1042 * we're OK for now.
1043 */
1044 int page_size = flash->page_size;
1045 int towrite;
1046
1047 /* Warning: This loop has a very unusual condition and body.
1048 * The loop needs to go through each page with at least one affected
1049 * byte. The lowest page number is (start / page_size) since that
1050 * division rounds down. The highest page number we want is the page
1051 * where the last byte of the range lives. That last byte has the
1052 * address (start + len - 1), thus the highest page number is
1053 * (start + len - 1) / page_size. Since we want to include that last
1054 * page as well, the loop condition uses <=.
1055 */
1056 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1057 /* Byte position of the first byte in the range in this page. */
1058 /* starthere is an offset to the base address of the chip. */
1059 starthere = max(start, i * page_size);
1060 /* Length of bytes in the range in this page. */
1061 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1062 for (j = 0; j < lenhere; j += chunksize) {
1063 towrite = min(chunksize, lenhere - j);
1064 rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite);
1065 if (rc)
1066 break;
1067 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1068 programmer_delay(10);
1069 }
1070 if (rc)
1071 break;
1072 }
1073
1074 return rc;
1075}
1076
1077/*
Sean Nelson14ba6682010-02-26 05:48:29 +00001078 * Program chip using byte programming. (SLOW!)
1079 * This is for chips which can only handle one byte writes
1080 * and for chips where memory mapped programming is impossible
1081 * (e.g. due to size constraints in IT87* for over 512 kB)
1082 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001083/* real chunksize is 1, logical chunksize is 1 */
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001084int spi_chip_write_1(struct flashchip *flash, uint8_t *buf, int start, int len)
Sean Nelson14ba6682010-02-26 05:48:29 +00001085{
Sean Nelson14ba6682010-02-26 05:48:29 +00001086 int i, result = 0;
1087
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001088 for (i = start; i < start + len; i++) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001089 result = spi_byte_program(i, buf[i - start]);
Sean Nelson14ba6682010-02-26 05:48:29 +00001090 if (result)
1091 return 1;
1092 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1093 programmer_delay(10);
1094 }
1095
1096 return 0;
1097}
1098
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001099int spi_aai_write(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001100{
1101 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +00001102 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001103 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1104 JEDEC_AAI_WORD_PROGRAM,
1105 };
1106 struct spi_command cmds[] = {
1107 {
1108 .writecnt = JEDEC_WREN_OUTSIZE,
1109 .writearr = (const unsigned char[]){ JEDEC_WREN },
1110 .readcnt = 0,
1111 .readarr = NULL,
1112 }, {
1113 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1114 .writearr = (const unsigned char[]){
1115 JEDEC_AAI_WORD_PROGRAM,
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001116 (start >> 16) & 0xff,
1117 (start >> 8) & 0xff,
1118 (start & 0xff),
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001119 buf[0],
1120 buf[1]
1121 },
1122 .readcnt = 0,
1123 .readarr = NULL,
1124 }, {
1125 .writecnt = 0,
1126 .writearr = NULL,
1127 .readcnt = 0,
1128 .readarr = NULL,
1129 }};
Sean Nelson14ba6682010-02-26 05:48:29 +00001130
Michael Karcherb9dbe482011-05-11 17:07:07 +00001131 switch (spi_programmer->type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001132#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001133#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001134 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +00001135 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001136 msg_perr("%s: impossible with this SPI controller,"
Sean Nelson14ba6682010-02-26 05:48:29 +00001137 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001138 return spi_chip_write_1(flash, buf, start, len);
Sean Nelson14ba6682010-02-26 05:48:29 +00001139#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001140#endif
Sean Nelson14ba6682010-02-26 05:48:29 +00001141 default:
1142 break;
1143 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001144
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001145 /* The even start address and even length requirements can be either
1146 * honored outside this function, or we can call spi_byte_program
1147 * for the first and/or last byte and use AAI for the rest.
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001148 * FIXME: Move this to generic code.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001149 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001150 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001151 if (start % 2) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001152 msg_cerr("%s: start address not even! Please report a bug at "
1153 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001154 if (spi_chip_write_1(flash, buf, start, start % 2))
1155 return SPI_GENERIC_ERROR;
1156 pos += start % 2;
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001157 cmds[1].writearr = (const unsigned char[]){
1158 JEDEC_AAI_WORD_PROGRAM,
1159 (pos >> 16) & 0xff,
1160 (pos >> 8) & 0xff,
1161 (pos & 0xff),
1162 buf[pos - start],
1163 buf[pos - start + 1]
1164 };
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001165 /* Do not return an error for now. */
1166 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001167 }
1168 /* The data sheet requires total AAI write length to be even. */
1169 if (len % 2) {
1170 msg_cerr("%s: total write length not even! Please report a "
1171 "bug at flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001172 /* Do not return an error for now. */
1173 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001174 }
1175
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001176
1177 result = spi_send_multicommand(cmds);
1178 if (result) {
1179 msg_cerr("%s failed during start command execution\n",
1180 __func__);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001181 /* FIXME: Should we send WRDI here as well to make sure the chip
1182 * is not in AAI mode?
1183 */
Sean Nelson14ba6682010-02-26 05:48:29 +00001184 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +00001185 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001186 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1187 programmer_delay(10);
1188
1189 /* We already wrote 2 bytes in the multicommand step. */
1190 pos += 2;
1191
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001192 /* Are there at least two more bytes to write? */
1193 while (pos < start + len - 1) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001194 cmd[1] = buf[pos++ - start];
1195 cmd[2] = buf[pos++ - start];
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001196 spi_send_command(JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
1197 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1198 programmer_delay(10);
1199 }
1200
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001201 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
1202 * other non-AAI command.
1203 */
Sean Nelson14ba6682010-02-26 05:48:29 +00001204 spi_write_disable();
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001205
1206 /* Write remaining byte (if any). */
1207 if (pos < start + len) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001208 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001209 return SPI_GENERIC_ERROR;
1210 pos += pos % 2;
1211 }
1212
Sean Nelson14ba6682010-02-26 05:48:29 +00001213 return 0;
1214}