blob: 60eb644a0136d64c0fa706fb8d4c43a16e8fc3ba [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;
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +000065 msg_cspew("REMS returned 0x%02x 0x%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)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000123 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000124 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000125
126 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000127 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000128
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000129 /* Check if this is a continuation vendor ID.
130 * FIXME: Handle continuation device IDs.
131 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000132 if (readarr[0] == 0x7f) {
133 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000134 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-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
Sean Nelsoned479d22010-03-24 23:14:32 +0000146 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-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
Sean Nelson14ba6682010-02-26 05:48:29 +0000175int probe_spi_rdid4(struct flashchip *flash)
176{
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000177 /* Some SPI controllers do not support commands with writecnt=1 and
178 * readcnt=4.
179 */
Michael Karcherb9dbe482011-05-11 17:07:07 +0000180 switch (spi_programmer->type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000181#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000182#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000183 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000184 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000185 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
186 return 0;
187 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000188#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000189#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000190 default:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000191 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-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
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000202 if (spi_rems(readarr)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000203 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000204 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000205
206 id1 = readarr[0];
207 id2 = readarr[1];
208
Sean Nelsoned479d22010-03-24 23:14:32 +0000209 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-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
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000233int probe_spi_res1(struct flashchip *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000234{
Mathias Krausea60faab2011-01-17 07:50:42 +0000235 static const unsigned char allff[] = {0xff, 0xff, 0xff};
236 static const unsigned char all00[] = {0x00, 0x00, 0x00};
Sean Nelson14ba6682010-02-26 05:48:29 +0000237 unsigned char readarr[3];
238 uint32_t id2;
Sean Nelson14ba6682010-02-26 05:48:29 +0000239
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000240 /* We only want one-byte RES if RDID and REMS are unusable. */
241
Sean Nelson14ba6682010-02-26 05:48:29 +0000242 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
243 * 0x00 0x00 0x00. In that case, RES is pointless.
244 */
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
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000259 if (spi_res(readarr, 1)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000260 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000261 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000262
Sean Nelson14ba6682010-02-26 05:48:29 +0000263 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000264
Sean Nelsoned479d22010-03-24 23:14:32 +0000265 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000266
Stefan Taunerdb45ab52011-05-28 22:59:05 +0000267 if (id2 != flash->model_id)
Sean Nelson14ba6682010-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
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000277int probe_spi_res2(struct flashchip *flash)
278{
279 unsigned char readarr[2];
280 uint32_t id1, id2;
281
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000282 if (spi_res(readarr, 2)) {
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000283 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000284 }
Carl-Daniel Hailfingerdc1cda12010-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
Sean Nelson14ba6682010-02-26 05:48:29 +0000301uint8_t spi_read_status_register(void)
302{
Mathias Krausea60faab2011-01-17 07:50:42 +0000303 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Sean Nelson14ba6682010-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)
Sean Nelsoned479d22010-03-24 23:14:32 +0000311 msg_cerr("RDSR failed!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000312
313 return readarr[0];
314}
315
316/* Prettyprint the status register. Common definitions. */
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000317void spi_prettyprint_status_register_welwip(uint8_t status)
Carl-Daniel Hailfingerfd7075a2010-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. */
Carl-Daniel Hailfinger7a3bd8f2011-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
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000352static void spi_prettyprint_status_register_common(uint8_t status)
Sean Nelson14ba6682010-02-26 05:48:29 +0000353{
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000354 spi_prettyprint_status_register_bp3210(status, 3);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000355 spi_prettyprint_status_register_welwip(status);
Sean Nelson14ba6682010-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{
Sean Nelsoned479d22010-03-24 23:14:32 +0000364 msg_cdbg("Chip status register: Status Register Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000365 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000366 msg_cdbg("Chip status register: Bit 6 is "
Sean Nelson14ba6682010-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{
Sean Nelsoned479d22010-03-24 23:14:32 +0000373 msg_cdbg("Chip status register: Block Protect Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000374 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000375 msg_cdbg("Chip status register: Auto Address Increment Programming "
Sean Nelson14ba6682010-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{
Mathias Krausea60faab2011-01-17 07:50:42 +0000385 static const char *const bpt[] = {
Sean Nelson14ba6682010-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);
Sean Nelsoned479d22010-03-24 23:14:32 +0000395 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000396 bpt[(status & 0x1c) >> 2]);
397}
398
399void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
400{
Mathias Krausea60faab2011-01-17 07:50:42 +0000401 static const char *const bpt[] = {
Sean Nelson14ba6682010-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);
Sean Nelsoned479d22010-03-24 23:14:32 +0000409 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000410 bpt[(status & 0x1c) >> 2]);
411}
412
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000413int spi_prettyprint_status_register(struct flashchip *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000414{
415 uint8_t status;
416
417 status = spi_read_status_register();
Sean Nelsoned479d22010-03-24 23:14:32 +0000418 msg_cdbg("Chip status register is %02x\n", status);
Sean Nelson14ba6682010-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;
Mattias Mattsson6eabe282010-09-15 23:31:03 +0000425 case MACRONIX_ID:
Sean Nelson14ba6682010-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 }
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000444 return 0;
Sean Nelson14ba6682010-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
Sean Nelson14ba6682010-02-26 05:48:29 +0000468 result = spi_send_multicommand(cmds);
469 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000470 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-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);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000480 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-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
Sean Nelson14ba6682010-02-26 05:48:29 +0000505 result = spi_send_multicommand(cmds);
506 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000507 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-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);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000516 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000517 return 0;
518}
519
Sean Nelson14ba6682010-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) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000548 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-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);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000557 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-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) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000594 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-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);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000603 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-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) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000638 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-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);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000647 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000648 return 0;
649}
650
Sean Nelson14ba6682010-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);
679 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000680 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000681 __func__, addr);
682 return result;
683 }
684 /* Wait until the Write-In-Progress bit is cleared.
685 * This usually takes 15-800 ms, so wait in 10 ms steps.
686 */
687 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
688 programmer_delay(10 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000689 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000690 return 0;
691}
692
693int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
694{
695 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000696 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000697 __func__);
698 return -1;
699 }
700 return spi_chip_erase_60(flash);
701}
702
703int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
704{
705 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000706 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000707 __func__);
708 return -1;
709 }
710 return spi_chip_erase_c7(flash);
711}
712
713int spi_write_status_enable(void)
714{
Mathias Krausea60faab2011-01-17 07:50:42 +0000715 static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Sean Nelson14ba6682010-02-26 05:48:29 +0000716 int result;
717
718 /* Send EWSR (Enable Write Status Register). */
719 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
720
721 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000722 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000723
724 return result;
725}
726
727/*
728 * This is according the SST25VF016 datasheet, who knows it is more
729 * generic that this...
730 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000731static int spi_write_status_register_ewsr(struct flashchip *flash, int status)
Sean Nelson14ba6682010-02-26 05:48:29 +0000732{
733 int result;
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000734 int i = 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000735 struct spi_command cmds[] = {
736 {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000737 /* WRSR requires either EWSR or WREN depending on chip type. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000738 .writecnt = JEDEC_EWSR_OUTSIZE,
739 .writearr = (const unsigned char[]){ JEDEC_EWSR },
740 .readcnt = 0,
741 .readarr = NULL,
742 }, {
743 .writecnt = JEDEC_WRSR_OUTSIZE,
744 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
745 .readcnt = 0,
746 .readarr = NULL,
747 }, {
748 .writecnt = 0,
749 .writearr = NULL,
750 .readcnt = 0,
751 .readarr = NULL,
752 }};
753
754 result = spi_send_multicommand(cmds);
755 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000756 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000757 __func__);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000758 /* No point in waiting for the command to complete if execution
759 * failed.
760 */
761 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +0000762 }
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000763 /* WRSR performs a self-timed erase before the changes take effect.
764 * This may take 50-85 ms in most cases, and some chips apparently
765 * allow running RDSR only once. Therefore pick an initial delay of
766 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
767 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000768 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000769 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
770 if (++i > 490) {
771 msg_cerr("Error: WIP bit after WRSR never cleared\n");
772 return TIMEOUT_ERROR;
773 }
774 programmer_delay(10 * 1000);
775 }
776 return 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000777}
778
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000779static int spi_write_status_register_wren(struct flashchip *flash, int status)
780{
781 int result;
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000782 int i = 0;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000783 struct spi_command cmds[] = {
784 {
785 /* WRSR requires either EWSR or WREN depending on chip type. */
786 .writecnt = JEDEC_WREN_OUTSIZE,
787 .writearr = (const unsigned char[]){ JEDEC_WREN },
788 .readcnt = 0,
789 .readarr = NULL,
790 }, {
791 .writecnt = JEDEC_WRSR_OUTSIZE,
792 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
793 .readcnt = 0,
794 .readarr = NULL,
795 }, {
796 .writecnt = 0,
797 .writearr = NULL,
798 .readcnt = 0,
799 .readarr = NULL,
800 }};
801
802 result = spi_send_multicommand(cmds);
803 if (result) {
804 msg_cerr("%s failed during command execution\n",
805 __func__);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000806 /* No point in waiting for the command to complete if execution
807 * failed.
808 */
809 return result;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000810 }
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000811 /* WRSR performs a self-timed erase before the changes take effect.
812 * This may take 50-85 ms in most cases, and some chips apparently
813 * allow running RDSR only once. Therefore pick an initial delay of
814 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
815 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000816 programmer_delay(100 * 1000);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000817 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) {
818 if (++i > 490) {
819 msg_cerr("Error: WIP bit after WRSR never cleared\n");
820 return TIMEOUT_ERROR;
821 }
822 programmer_delay(10 * 1000);
823 }
824 return 0;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000825}
826
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000827int spi_write_status_register(struct flashchip *flash, int status)
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000828{
829 int ret = 1;
830
831 if (!(flash->feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
832 msg_cdbg("Missing status register write definition, assuming "
833 "EWSR is needed\n");
834 flash->feature_bits |= FEATURE_WRSR_EWSR;
835 }
836 if (flash->feature_bits & FEATURE_WRSR_WREN)
837 ret = spi_write_status_register_wren(flash, status);
838 if (ret && (flash->feature_bits & FEATURE_WRSR_EWSR))
839 ret = spi_write_status_register_ewsr(flash, status);
840 return ret;
841}
842
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000843int spi_byte_program(unsigned int addr, uint8_t databyte)
Sean Nelson14ba6682010-02-26 05:48:29 +0000844{
845 int result;
846 struct spi_command cmds[] = {
847 {
848 .writecnt = JEDEC_WREN_OUTSIZE,
849 .writearr = (const unsigned char[]){ JEDEC_WREN },
850 .readcnt = 0,
851 .readarr = NULL,
852 }, {
853 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
854 .writearr = (const unsigned char[]){
855 JEDEC_BYTE_PROGRAM,
856 (addr >> 16) & 0xff,
857 (addr >> 8) & 0xff,
858 (addr & 0xff),
859 databyte
860 },
861 .readcnt = 0,
862 .readarr = NULL,
863 }, {
864 .writecnt = 0,
865 .writearr = NULL,
866 .readcnt = 0,
867 .readarr = NULL,
868 }};
869
870 result = spi_send_multicommand(cmds);
871 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000872 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000873 __func__, addr);
874 }
875 return result;
876}
877
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000878int spi_nbyte_program(unsigned int addr, uint8_t *bytes, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000879{
880 int result;
881 /* FIXME: Switch to malloc based on len unless that kills speed. */
882 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
883 JEDEC_BYTE_PROGRAM,
884 (addr >> 16) & 0xff,
885 (addr >> 8) & 0xff,
886 (addr >> 0) & 0xff,
887 };
888 struct spi_command cmds[] = {
889 {
890 .writecnt = JEDEC_WREN_OUTSIZE,
891 .writearr = (const unsigned char[]){ JEDEC_WREN },
892 .readcnt = 0,
893 .readarr = NULL,
894 }, {
895 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
896 .writearr = cmd,
897 .readcnt = 0,
898 .readarr = NULL,
899 }, {
900 .writecnt = 0,
901 .writearr = NULL,
902 .readcnt = 0,
903 .readarr = NULL,
904 }};
905
906 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000907 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000908 return 1;
909 }
910 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000911 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000912 return 1;
913 }
914
915 memcpy(&cmd[4], bytes, len);
916
917 result = spi_send_multicommand(cmds);
918 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000919 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000920 __func__, addr);
921 }
922 return result;
923}
924
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000925/* A generic brute-force block protection disable works like this:
926 * Write 0x00 to the status register. Check if any locks are still set (that
927 * part is chip specific). Repeat once.
928 */
Carl-Daniel Hailfinger29a1c662010-07-14 20:21:22 +0000929int spi_disable_blockprotect(struct flashchip *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000930{
931 uint8_t status;
932 int result;
933
934 status = spi_read_status_register();
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000935 /* If block protection is disabled, stop here. */
936 if ((status & 0x3c) == 0)
937 return 0;
938
939 msg_cdbg("Some block protection in effect, disabling\n");
940 result = spi_write_status_register(flash, status & ~0x3c);
941 if (result) {
942 msg_cerr("spi_write_status_register failed\n");
943 return result;
944 }
945 status = spi_read_status_register();
Sean Nelson14ba6682010-02-26 05:48:29 +0000946 if ((status & 0x3c) != 0) {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000947 msg_cerr("Block protection could not be disabled!\n");
948 return 1;
949 }
950 return 0;
951}
952
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000953int spi_nbyte_read(unsigned int address, uint8_t *bytes, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000954{
955 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
956 JEDEC_READ,
957 (address >> 16) & 0xff,
958 (address >> 8) & 0xff,
959 (address >> 0) & 0xff,
960 };
961
962 /* Send Read */
963 return spi_send_command(sizeof(cmd), len, cmd, bytes);
964}
965
966/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000967 * Read a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000968 * FIXME: Use the chunk code from Michael Karcher instead.
Sean Nelson14ba6682010-02-26 05:48:29 +0000969 * Each page is read separately in chunks with a maximum size of chunksize.
970 */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000971int spi_read_chunked(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
Sean Nelson14ba6682010-02-26 05:48:29 +0000972{
973 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000974 unsigned int i, j, starthere, lenhere, toread;
975 unsigned int page_size = flash->page_size;
Sean Nelson14ba6682010-02-26 05:48:29 +0000976
977 /* Warning: This loop has a very unusual condition and body.
978 * The loop needs to go through each page with at least one affected
979 * byte. The lowest page number is (start / page_size) since that
980 * division rounds down. The highest page number we want is the page
981 * where the last byte of the range lives. That last byte has the
982 * address (start + len - 1), thus the highest page number is
983 * (start + len - 1) / page_size. Since we want to include that last
984 * page as well, the loop condition uses <=.
985 */
986 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
987 /* Byte position of the first byte in the range in this page. */
988 /* starthere is an offset to the base address of the chip. */
989 starthere = max(start, i * page_size);
990 /* Length of bytes in the range in this page. */
991 lenhere = min(start + len, (i + 1) * page_size) - starthere;
992 for (j = 0; j < lenhere; j += chunksize) {
993 toread = min(chunksize, lenhere - j);
994 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
995 if (rc)
996 break;
997 }
998 if (rc)
999 break;
1000 }
1001
1002 return rc;
1003}
1004
1005/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001006 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001007 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001008 * Each page is written separately in chunks with a maximum size of chunksize.
1009 */
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001010int spi_write_chunked(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001011{
1012 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001013 unsigned int i, j, starthere, lenhere, towrite;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001014 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
1015 * in struct flashchip to do this properly. All chips using
1016 * spi_chip_write_256 have page_size set to max_writechunk_size, so
1017 * we're OK for now.
1018 */
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001019 unsigned int page_size = flash->page_size;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001020
1021 /* Warning: This loop has a very unusual condition and body.
1022 * The loop needs to go through each page with at least one affected
1023 * byte. The lowest page number is (start / page_size) since that
1024 * division rounds down. The highest page number we want is the page
1025 * where the last byte of the range lives. That last byte has the
1026 * address (start + len - 1), thus the highest page number is
1027 * (start + len - 1) / page_size. Since we want to include that last
1028 * page as well, the loop condition uses <=.
1029 */
1030 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1031 /* Byte position of the first byte in the range in this page. */
1032 /* starthere is an offset to the base address of the chip. */
1033 starthere = max(start, i * page_size);
1034 /* Length of bytes in the range in this page. */
1035 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1036 for (j = 0; j < lenhere; j += chunksize) {
1037 towrite = min(chunksize, lenhere - j);
1038 rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite);
1039 if (rc)
1040 break;
1041 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1042 programmer_delay(10);
1043 }
1044 if (rc)
1045 break;
1046 }
1047
1048 return rc;
1049}
1050
1051/*
Sean Nelson14ba6682010-02-26 05:48:29 +00001052 * Program chip using byte programming. (SLOW!)
1053 * This is for chips which can only handle one byte writes
1054 * and for chips where memory mapped programming is impossible
1055 * (e.g. due to size constraints in IT87* for over 512 kB)
1056 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001057/* real chunksize is 1, logical chunksize is 1 */
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001058int spi_chip_write_1(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +00001059{
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001060 unsigned int i;
1061 int result = 0;
Sean Nelson14ba6682010-02-26 05:48:29 +00001062
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001063 for (i = start; i < start + len; i++) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001064 result = spi_byte_program(i, buf[i - start]);
Sean Nelson14ba6682010-02-26 05:48:29 +00001065 if (result)
1066 return 1;
1067 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1068 programmer_delay(10);
1069 }
1070
1071 return 0;
1072}
1073
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001074int spi_aai_write(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001075{
1076 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +00001077 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001078 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1079 JEDEC_AAI_WORD_PROGRAM,
1080 };
1081 struct spi_command cmds[] = {
1082 {
1083 .writecnt = JEDEC_WREN_OUTSIZE,
1084 .writearr = (const unsigned char[]){ JEDEC_WREN },
1085 .readcnt = 0,
1086 .readarr = NULL,
1087 }, {
1088 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1089 .writearr = (const unsigned char[]){
1090 JEDEC_AAI_WORD_PROGRAM,
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001091 (start >> 16) & 0xff,
1092 (start >> 8) & 0xff,
1093 (start & 0xff),
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001094 buf[0],
1095 buf[1]
1096 },
1097 .readcnt = 0,
1098 .readarr = NULL,
1099 }, {
1100 .writecnt = 0,
1101 .writearr = NULL,
1102 .readcnt = 0,
1103 .readarr = NULL,
1104 }};
Sean Nelson14ba6682010-02-26 05:48:29 +00001105
Michael Karcherb9dbe482011-05-11 17:07:07 +00001106 switch (spi_programmer->type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001107#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001108#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001109 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +00001110 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001111 msg_perr("%s: impossible with this SPI controller,"
Sean Nelson14ba6682010-02-26 05:48:29 +00001112 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001113 return spi_chip_write_1(flash, buf, start, len);
Sean Nelson14ba6682010-02-26 05:48:29 +00001114#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001115#endif
Sean Nelson14ba6682010-02-26 05:48:29 +00001116 default:
1117 break;
1118 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001119
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001120 /* The even start address and even length requirements can be either
1121 * honored outside this function, or we can call spi_byte_program
1122 * for the first and/or last byte and use AAI for the rest.
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001123 * FIXME: Move this to generic code.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001124 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001125 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001126 if (start % 2) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001127 msg_cerr("%s: start address not even! Please report a bug at "
1128 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001129 if (spi_chip_write_1(flash, buf, start, start % 2))
1130 return SPI_GENERIC_ERROR;
1131 pos += start % 2;
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001132 cmds[1].writearr = (const unsigned char[]){
1133 JEDEC_AAI_WORD_PROGRAM,
1134 (pos >> 16) & 0xff,
1135 (pos >> 8) & 0xff,
1136 (pos & 0xff),
1137 buf[pos - start],
1138 buf[pos - start + 1]
1139 };
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001140 /* Do not return an error for now. */
1141 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001142 }
1143 /* The data sheet requires total AAI write length to be even. */
1144 if (len % 2) {
1145 msg_cerr("%s: total write length not even! Please report a "
1146 "bug at flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001147 /* Do not return an error for now. */
1148 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001149 }
1150
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001151
1152 result = spi_send_multicommand(cmds);
1153 if (result) {
1154 msg_cerr("%s failed during start command execution\n",
1155 __func__);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001156 /* FIXME: Should we send WRDI here as well to make sure the chip
1157 * is not in AAI mode?
1158 */
Sean Nelson14ba6682010-02-26 05:48:29 +00001159 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +00001160 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001161 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1162 programmer_delay(10);
1163
1164 /* We already wrote 2 bytes in the multicommand step. */
1165 pos += 2;
1166
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001167 /* Are there at least two more bytes to write? */
1168 while (pos < start + len - 1) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001169 cmd[1] = buf[pos++ - start];
1170 cmd[2] = buf[pos++ - start];
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001171 spi_send_command(JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
1172 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1173 programmer_delay(10);
1174 }
1175
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001176 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
1177 * other non-AAI command.
1178 */
Sean Nelson14ba6682010-02-26 05:48:29 +00001179 spi_write_disable();
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001180
1181 /* Write remaining byte (if any). */
1182 if (pos < start + len) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001183 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001184 return SPI_GENERIC_ERROR;
1185 pos += pos % 2;
1186 }
1187
Sean Nelson14ba6682010-02-26 05:48:29 +00001188 return 0;
1189}