blob: 8d82c4e7bc8f6799b1102b4fb4f17a80184575de [file] [log] [blame]
stepand4b13752007-10-15 21:45:29 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfingera1289042009-06-24 08:28:39 +00004 * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger
stepandbd3af12008-06-27 16:28:34 +00005 * Copyright (C) 2008 coresystems GmbH
stepand4b13752007-10-15 21:45:29 +00006 *
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 generic SPI framework
23 */
24
stepand4b13752007-10-15 21:45:29 +000025#include <string.h>
26#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000027#include "flashchips.h"
hailfinger78031562008-05-13 14:58:23 +000028#include "spi.h"
stepand4b13752007-10-15 21:45:29 +000029
hailfinger40167462009-05-31 17:57:34 +000030enum spi_controller spi_controller = SPI_CONTROLLER_NONE;
31void *spibar = NULL;
32
hailfingerb8f7e882008-01-19 00:04:46 +000033void spi_prettyprint_status_register(struct flashchip *flash);
stepand4b13752007-10-15 21:45:29 +000034
hailfinger948b81f2009-07-22 15:36:50 +000035const struct spi_programmer spi_programmer[] = {
36 { /* SPI_CONTROLLER_NONE */
37 .command = NULL,
38 .multicommand = NULL,
39 .read = NULL,
40 .write_256 = NULL,
41 },
42
43 { /* SPI_CONTROLLER_ICH7 */
44 .command = ich_spi_send_command,
45 .multicommand = ich_spi_send_multicommand,
46 .read = ich_spi_read,
47 .write_256 = ich_spi_write_256,
48 },
49
50 { /* SPI_CONTROLLER_ICH9 */
51 .command = ich_spi_send_command,
52 .multicommand = ich_spi_send_multicommand,
53 .read = ich_spi_read,
54 .write_256 = ich_spi_write_256,
55 },
56
57 { /* SPI_CONTROLLER_IT87XX */
58 .command = it8716f_spi_send_command,
59 .multicommand = default_spi_send_multicommand,
60 .read = it8716f_spi_chip_read,
61 .write_256 = it8716f_spi_chip_write_256,
62 },
63
64 { /* SPI_CONTROLLER_SB600 */
65 .command = sb600_spi_send_command,
66 .multicommand = default_spi_send_multicommand,
67 .read = sb600_spi_read,
68 .write_256 = sb600_spi_write_1,
69 },
70
71 { /* SPI_CONTROLLER_VIA */
72 .command = ich_spi_send_command,
73 .multicommand = ich_spi_send_multicommand,
74 .read = ich_spi_read,
75 .write_256 = ich_spi_write_256,
76 },
77
78 { /* SPI_CONTROLLER_WBSIO */
79 .command = wbsio_spi_send_command,
80 .multicommand = default_spi_send_multicommand,
81 .read = wbsio_spi_read,
82 .write_256 = wbsio_spi_write_1,
83 },
84
85 { /* SPI_CONTROLLER_FT2232 */
86 .command = ft2232_spi_send_command,
87 .multicommand = default_spi_send_multicommand,
88 .read = ft2232_spi_read,
89 .write_256 = ft2232_spi_write_256,
90 },
91
92 { /* SPI_CONTROLLER_DUMMY */
93 .command = dummy_spi_send_command,
94 .multicommand = default_spi_send_multicommand,
95 .read = NULL,
96 .write_256 = NULL,
97 },
98};
99
100
hailfinger68002c22009-07-10 21:08:55 +0000101int spi_send_command(unsigned int writecnt, unsigned int readcnt,
uwefa98ca12008-10-18 21:14:13 +0000102 const unsigned char *writearr, unsigned char *readarr)
hailfinger35cc8162007-10-16 21:09:06 +0000103{
hailfinger948b81f2009-07-22 15:36:50 +0000104 if (!spi_programmer[spi_controller].command) {
105 fprintf(stderr, "%s called, but SPI is unsupported on this "
106 "hardware. Please report a bug.\n", __func__);
107 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000108 }
hailfinger948b81f2009-07-22 15:36:50 +0000109
110 return spi_programmer[spi_controller].command(writecnt, readcnt,
111 writearr, readarr);
hailfinger35cc8162007-10-16 21:09:06 +0000112}
113
hailfinger68002c22009-07-10 21:08:55 +0000114int spi_send_multicommand(struct spi_command *spicommands)
115{
hailfinger948b81f2009-07-22 15:36:50 +0000116 if (!spi_programmer[spi_controller].multicommand) {
117 fprintf(stderr, "%s called, but SPI is unsupported on this "
118 "hardware. Please report a bug.\n", __func__);
119 return 1;
hailfinger68002c22009-07-10 21:08:55 +0000120 }
hailfinger948b81f2009-07-22 15:36:50 +0000121
122 return spi_programmer[spi_controller].multicommand(spicommands);
123}
124
125int default_spi_send_command(unsigned int writecnt, unsigned int readcnt,
126 const unsigned char *writearr, unsigned char *readarr)
127{
128 struct spi_command cmd[] = {
129 {
130 .writecnt = writecnt,
131 .readcnt = readcnt,
132 .writearr = writearr,
133 .readarr = readarr,
134 }, {
135 .writecnt = 0,
136 .writearr = NULL,
137 .readcnt = 0,
138 .readarr = NULL,
139 }};
140
141 return spi_send_multicommand(cmd);
142}
143
144int default_spi_send_multicommand(struct spi_command *spicommands)
145{
146 int result = 0;
147 while ((spicommands->writecnt || spicommands->readcnt) && !result) {
148 result = spi_send_command(spicommands->writecnt, spicommands->readcnt,
149 spicommands->writearr, spicommands->readarr);
150 }
151 return result;
hailfinger68002c22009-07-10 21:08:55 +0000152}
153
ruikdbe18ee2008-06-30 21:45:17 +0000154static int spi_rdid(unsigned char *readarr, int bytes)
stepand4b13752007-10-15 21:45:29 +0000155{
uwefa98ca12008-10-18 21:14:13 +0000156 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
hailfinger54c14662009-05-13 11:40:08 +0000157 int ret;
hailfingerf91e3b52009-05-14 12:59:36 +0000158 int i;
stepand4b13752007-10-15 21:45:29 +0000159
hailfinger68002c22009-07-10 21:08:55 +0000160 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
hailfinger54c14662009-05-13 11:40:08 +0000161 if (ret)
162 return ret;
hailfingerf91e3b52009-05-14 12:59:36 +0000163 printf_debug("RDID returned");
164 for (i = 0; i < bytes; i++)
165 printf_debug(" 0x%02x", readarr[i]);
hailfinger0cb68252009-07-23 01:33:43 +0000166 printf_debug(". ");
stepand4b13752007-10-15 21:45:29 +0000167 return 0;
168}
169
hailfinger3dd0c3e2008-11-28 01:25:00 +0000170static int spi_rems(unsigned char *readarr)
171{
hailfinger54c14662009-05-13 11:40:08 +0000172 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
173 uint32_t readaddr;
174 int ret;
hailfinger3dd0c3e2008-11-28 01:25:00 +0000175
hailfinger68002c22009-07-10 21:08:55 +0000176 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
hailfinger54c14662009-05-13 11:40:08 +0000177 if (ret == SPI_INVALID_ADDRESS) {
178 /* Find the lowest even address allowed for reads. */
179 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
180 cmd[1] = (readaddr >> 16) & 0xff,
181 cmd[2] = (readaddr >> 8) & 0xff,
182 cmd[3] = (readaddr >> 0) & 0xff,
hailfinger68002c22009-07-10 21:08:55 +0000183 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
hailfinger54c14662009-05-13 11:40:08 +0000184 }
185 if (ret)
186 return ret;
hailfinger0cb68252009-07-23 01:33:43 +0000187 printf_debug("REMS returned %02x %02x. ", readarr[0], readarr[1]);
hailfinger3dd0c3e2008-11-28 01:25:00 +0000188 return 0;
189}
190
hailfinger82893122008-05-15 03:19:49 +0000191static int spi_res(unsigned char *readarr)
192{
hailfinger54c14662009-05-13 11:40:08 +0000193 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
194 uint32_t readaddr;
195 int ret;
hailfinger82893122008-05-15 03:19:49 +0000196
hailfinger68002c22009-07-10 21:08:55 +0000197 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
hailfinger54c14662009-05-13 11:40:08 +0000198 if (ret == SPI_INVALID_ADDRESS) {
199 /* Find the lowest even address allowed for reads. */
200 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
201 cmd[1] = (readaddr >> 16) & 0xff,
202 cmd[2] = (readaddr >> 8) & 0xff,
203 cmd[3] = (readaddr >> 0) & 0xff,
hailfinger68002c22009-07-10 21:08:55 +0000204 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
hailfinger54c14662009-05-13 11:40:08 +0000205 }
206 if (ret)
207 return ret;
hailfinger0cb68252009-07-23 01:33:43 +0000208 printf_debug("RES returned %02x. ", readarr[0]);
hailfinger82893122008-05-15 03:19:49 +0000209 return 0;
210}
211
uwe5e931bc2009-04-15 10:52:49 +0000212int spi_write_enable(void)
hailfingerf71c0ac2007-10-18 00:24:07 +0000213{
uwefa98ca12008-10-18 21:14:13 +0000214 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
hailfinger61949942009-05-09 02:09:45 +0000215 int result;
hailfingerf71c0ac2007-10-18 00:24:07 +0000216
217 /* Send WREN (Write Enable) */
hailfinger68002c22009-07-10 21:08:55 +0000218 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
hailfinger128df152009-05-15 00:56:22 +0000219
220 if (result)
hailfinger0cb68252009-07-23 01:33:43 +0000221 fprintf(stderr, "%s failed\n", __func__);
hailfinger128df152009-05-15 00:56:22 +0000222
hailfinger61949942009-05-09 02:09:45 +0000223 return result;
hailfingerf71c0ac2007-10-18 00:24:07 +0000224}
225
uwe5e931bc2009-04-15 10:52:49 +0000226int spi_write_disable(void)
hailfingerf71c0ac2007-10-18 00:24:07 +0000227{
uwefa98ca12008-10-18 21:14:13 +0000228 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
hailfingerf71c0ac2007-10-18 00:24:07 +0000229
230 /* Send WRDI (Write Disable) */
hailfinger68002c22009-07-10 21:08:55 +0000231 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
hailfingerf71c0ac2007-10-18 00:24:07 +0000232}
233
ruikdbe18ee2008-06-30 21:45:17 +0000234static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
stepand4b13752007-10-15 21:45:29 +0000235{
ruikdbe18ee2008-06-30 21:45:17 +0000236 unsigned char readarr[4];
hailfingerd7991892009-05-27 11:40:08 +0000237 uint32_t id1;
238 uint32_t id2;
hailfingerf1961cb2007-12-29 10:15:58 +0000239
ruikdbe18ee2008-06-30 21:45:17 +0000240 if (spi_rdid(readarr, bytes))
stuge7be66832008-06-24 01:22:03 +0000241 return 0;
242
243 if (!oddparity(readarr[0]))
hailfinger0cb68252009-07-23 01:33:43 +0000244 printf_debug("RDID byte 0 parity violation. ");
stuge7be66832008-06-24 01:22:03 +0000245
246 /* Check if this is a continuation vendor ID */
247 if (readarr[0] == 0x7f) {
248 if (!oddparity(readarr[1]))
hailfinger0cb68252009-07-23 01:33:43 +0000249 printf_debug("RDID byte 1 parity violation. ");
hailfingerd7991892009-05-27 11:40:08 +0000250 id1 = (readarr[0] << 8) | readarr[1];
251 id2 = readarr[2];
ruikdbe18ee2008-06-30 21:45:17 +0000252 if (bytes > 3) {
hailfingerd7991892009-05-27 11:40:08 +0000253 id2 <<= 8;
254 id2 |= readarr[3];
ruikdbe18ee2008-06-30 21:45:17 +0000255 }
stuge7be66832008-06-24 01:22:03 +0000256 } else {
hailfingerd7991892009-05-27 11:40:08 +0000257 id1 = readarr[0];
258 id2 = (readarr[1] << 8) | readarr[2];
stepand4b13752007-10-15 21:45:29 +0000259 }
260
hailfingerd7991892009-05-27 11:40:08 +0000261 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, id1, id2);
stuge7be66832008-06-24 01:22:03 +0000262
hailfingerd7991892009-05-27 11:40:08 +0000263 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
stuge7be66832008-06-24 01:22:03 +0000264 /* Print the status register to tell the
265 * user about possible write protection.
266 */
267 spi_prettyprint_status_register(flash);
268
269 return 1;
270 }
271
272 /* Test if this is a pure vendor match. */
hailfingerd7991892009-05-27 11:40:08 +0000273 if (id1 == flash->manufacture_id &&
stuge7be66832008-06-24 01:22:03 +0000274 GENERIC_DEVICE_ID == flash->model_id)
275 return 1;
276
stepand4b13752007-10-15 21:45:29 +0000277 return 0;
278}
279
uwefa98ca12008-10-18 21:14:13 +0000280int probe_spi_rdid(struct flashchip *flash)
281{
ruikdbe18ee2008-06-30 21:45:17 +0000282 return probe_spi_rdid_generic(flash, 3);
283}
284
285/* support 4 bytes flash ID */
uwefa98ca12008-10-18 21:14:13 +0000286int probe_spi_rdid4(struct flashchip *flash)
287{
ruikdbe18ee2008-06-30 21:45:17 +0000288 /* only some SPI chipsets support 4 bytes commands */
hailfinger40167462009-05-31 17:57:34 +0000289 switch (spi_controller) {
290 case SPI_CONTROLLER_ICH7:
291 case SPI_CONTROLLER_ICH9:
292 case SPI_CONTROLLER_VIA:
293 case SPI_CONTROLLER_SB600:
294 case SPI_CONTROLLER_WBSIO:
hailfingerf31da3d2009-06-16 21:08:06 +0000295 case SPI_CONTROLLER_FT2232:
hailfinger40167462009-05-31 17:57:34 +0000296 case SPI_CONTROLLER_DUMMY:
stepan3bdf6182008-06-30 23:45:22 +0000297 return probe_spi_rdid_generic(flash, 4);
298 default:
299 printf_debug("4b ID not supported on this SPI controller\n");
300 }
301
302 return 0;
ruikdbe18ee2008-06-30 21:45:17 +0000303}
304
hailfinger3dd0c3e2008-11-28 01:25:00 +0000305int probe_spi_rems(struct flashchip *flash)
306{
307 unsigned char readarr[JEDEC_REMS_INSIZE];
hailfingerd7991892009-05-27 11:40:08 +0000308 uint32_t id1, id2;
hailfinger3dd0c3e2008-11-28 01:25:00 +0000309
310 if (spi_rems(readarr))
311 return 0;
312
hailfingerd7991892009-05-27 11:40:08 +0000313 id1 = readarr[0];
314 id2 = readarr[1];
hailfinger3dd0c3e2008-11-28 01:25:00 +0000315
hailfingerd7991892009-05-27 11:40:08 +0000316 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
hailfinger3dd0c3e2008-11-28 01:25:00 +0000317
hailfingerd7991892009-05-27 11:40:08 +0000318 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
hailfinger3dd0c3e2008-11-28 01:25:00 +0000319 /* Print the status register to tell the
320 * user about possible write protection.
321 */
322 spi_prettyprint_status_register(flash);
323
324 return 1;
325 }
326
327 /* Test if this is a pure vendor match. */
hailfingerd7991892009-05-27 11:40:08 +0000328 if (id1 == flash->manufacture_id &&
hailfinger3dd0c3e2008-11-28 01:25:00 +0000329 GENERIC_DEVICE_ID == flash->model_id)
330 return 1;
331
332 return 0;
333}
334
hailfinger82893122008-05-15 03:19:49 +0000335int probe_spi_res(struct flashchip *flash)
336{
337 unsigned char readarr[3];
hailfingerd7991892009-05-27 11:40:08 +0000338 uint32_t id2;
stuge7be66832008-06-24 01:22:03 +0000339
hailfinger915cc852008-11-27 22:48:48 +0000340 /* Check if RDID was successful and did not return 0xff 0xff 0xff.
341 * In that case, RES is pointless.
342 */
343 if (!spi_rdid(readarr, 3) && ((readarr[0] != 0xff) ||
344 (readarr[1] != 0xff) || (readarr[2] != 0xff)))
stuge7be66832008-06-24 01:22:03 +0000345 return 0;
hailfinger82893122008-05-15 03:19:49 +0000346
stuge7be66832008-06-24 01:22:03 +0000347 if (spi_res(readarr))
348 return 0;
349
hailfingerd7991892009-05-27 11:40:08 +0000350 id2 = readarr[0];
351 printf_debug("%s: id 0x%x\n", __FUNCTION__, id2);
352 if (id2 != flash->model_id)
stuge7be66832008-06-24 01:22:03 +0000353 return 0;
354
355 /* Print the status register to tell the
356 * user about possible write protection.
357 */
358 spi_prettyprint_status_register(flash);
359 return 1;
hailfinger82893122008-05-15 03:19:49 +0000360}
361
uwe5e931bc2009-04-15 10:52:49 +0000362uint8_t spi_read_status_register(void)
hailfingerf71c0ac2007-10-18 00:24:07 +0000363{
uwefa98ca12008-10-18 21:14:13 +0000364 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
hailfinger948b81f2009-07-22 15:36:50 +0000365 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
stugea564bcf2009-01-26 03:08:45 +0000366 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
hailfinger54c14662009-05-13 11:40:08 +0000367 int ret;
hailfingerf71c0ac2007-10-18 00:24:07 +0000368
369 /* Read Status Register */
hailfinger948b81f2009-07-22 15:36:50 +0000370 if (spi_controller == SPI_CONTROLLER_SB600) { /* FIXME */
371 /* Workaround for SB600 hardware bug. Can be killed later. */
uwe17efbed2008-11-28 21:36:51 +0000372 return sb600_read_status_register();
uwe17efbed2008-11-28 21:36:51 +0000373 }
hailfinger948b81f2009-07-22 15:36:50 +0000374 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
375 if (ret)
hailfinger0cb68252009-07-23 01:33:43 +0000376 fprintf(stderr, "RDSR failed!\n");
uwe17efbed2008-11-28 21:36:51 +0000377
hailfingerf71c0ac2007-10-18 00:24:07 +0000378 return readarr[0];
379}
380
uwe5e931bc2009-04-15 10:52:49 +0000381/* Prettyprint the status register. Common definitions. */
hailfingerb8f7e882008-01-19 00:04:46 +0000382void spi_prettyprint_status_register_common(uint8_t status)
383{
384 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
uwefa98ca12008-10-18 21:14:13 +0000385 "%sset\n", (status & (1 << 5)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000386 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
uwefa98ca12008-10-18 21:14:13 +0000387 "%sset\n", (status & (1 << 4)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000388 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
uwefa98ca12008-10-18 21:14:13 +0000389 "%sset\n", (status & (1 << 3)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000390 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
uwefa98ca12008-10-18 21:14:13 +0000391 "%sset\n", (status & (1 << 2)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000392 printf_debug("Chip status register: Write Enable Latch (WEL) is "
uwefa98ca12008-10-18 21:14:13 +0000393 "%sset\n", (status & (1 << 1)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000394 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
uwefa98ca12008-10-18 21:14:13 +0000395 "%sset\n", (status & (1 << 0)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000396}
397
hailfingerf1961cb2007-12-29 10:15:58 +0000398/* Prettyprint the status register. Works for
399 * ST M25P series
400 * MX MX25L series
401 */
hailfingerb8f7e882008-01-19 00:04:46 +0000402void spi_prettyprint_status_register_st_m25p(uint8_t status)
hailfingerf1961cb2007-12-29 10:15:58 +0000403{
404 printf_debug("Chip status register: Status Register Write Disable "
uwefa98ca12008-10-18 21:14:13 +0000405 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
hailfingerf1961cb2007-12-29 10:15:58 +0000406 printf_debug("Chip status register: Bit 6 is "
uwefa98ca12008-10-18 21:14:13 +0000407 "%sset\n", (status & (1 << 6)) ? "" : "not ");
hailfingerb8f7e882008-01-19 00:04:46 +0000408 spi_prettyprint_status_register_common(status);
hailfingerf1961cb2007-12-29 10:15:58 +0000409}
410
hailfinger29c5caa2009-05-06 13:59:44 +0000411void spi_prettyprint_status_register_sst25(uint8_t status)
412{
413 printf_debug("Chip status register: Block Protect Write Disable "
414 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
415 printf_debug("Chip status register: Auto Address Increment Programming "
416 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
417 spi_prettyprint_status_register_common(status);
418}
419
hailfingerb8f7e882008-01-19 00:04:46 +0000420/* Prettyprint the status register. Works for
421 * SST 25VF016
422 */
423void spi_prettyprint_status_register_sst25vf016(uint8_t status)
424{
hailfinger9cd4cf12008-01-22 14:37:31 +0000425 const char *bpt[] = {
hailfingerb8f7e882008-01-19 00:04:46 +0000426 "none",
427 "1F0000H-1FFFFFH",
428 "1E0000H-1FFFFFH",
429 "1C0000H-1FFFFFH",
430 "180000H-1FFFFFH",
431 "100000H-1FFFFFH",
hailfinger9cd4cf12008-01-22 14:37:31 +0000432 "all", "all"
hailfingerb8f7e882008-01-19 00:04:46 +0000433 };
hailfinger29c5caa2009-05-06 13:59:44 +0000434 spi_prettyprint_status_register_sst25(status);
hailfingerb8f7e882008-01-19 00:04:46 +0000435 printf_debug("Resulting block protection : %s\n",
uwefa98ca12008-10-18 21:14:13 +0000436 bpt[(status & 0x1c) >> 2]);
hailfingerb8f7e882008-01-19 00:04:46 +0000437}
438
stuge36539392009-01-26 03:23:50 +0000439void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
440{
441 const char *bpt[] = {
442 "none",
443 "0x70000-0x7ffff",
444 "0x60000-0x7ffff",
445 "0x40000-0x7ffff",
446 "all blocks", "all blocks", "all blocks", "all blocks"
447 };
hailfinger29c5caa2009-05-06 13:59:44 +0000448 spi_prettyprint_status_register_sst25(status);
stuge36539392009-01-26 03:23:50 +0000449 printf_debug("Resulting block protection : %s\n",
hailfinger29c5caa2009-05-06 13:59:44 +0000450 bpt[(status & 0x1c) >> 2]);
stuge36539392009-01-26 03:23:50 +0000451}
452
hailfingerb8f7e882008-01-19 00:04:46 +0000453void spi_prettyprint_status_register(struct flashchip *flash)
hailfingerf1961cb2007-12-29 10:15:58 +0000454{
455 uint8_t status;
456
stuge2bb6ab32008-05-10 23:07:52 +0000457 status = spi_read_status_register();
hailfingerf1961cb2007-12-29 10:15:58 +0000458 printf_debug("Chip status register is %02x\n", status);
459 switch (flash->manufacture_id) {
460 case ST_ID:
hailfinger8b869132008-05-15 22:32:08 +0000461 if (((flash->model_id & 0xff00) == 0x2000) ||
462 ((flash->model_id & 0xff00) == 0x2500))
463 spi_prettyprint_status_register_st_m25p(status);
464 break;
hailfingerf1961cb2007-12-29 10:15:58 +0000465 case MX_ID:
466 if ((flash->model_id & 0xff00) == 0x2000)
hailfingerb8f7e882008-01-19 00:04:46 +0000467 spi_prettyprint_status_register_st_m25p(status);
468 break;
469 case SST_ID:
stuge36539392009-01-26 03:23:50 +0000470 switch (flash->model_id) {
471 case 0x2541:
hailfingerb8f7e882008-01-19 00:04:46 +0000472 spi_prettyprint_status_register_sst25vf016(status);
stuge36539392009-01-26 03:23:50 +0000473 break;
474 case 0x8d:
475 case 0x258d:
476 spi_prettyprint_status_register_sst25vf040b(status);
477 break;
hailfinger56e86ad2009-05-13 22:51:27 +0000478 default:
hailfinger29c5caa2009-05-06 13:59:44 +0000479 spi_prettyprint_status_register_sst25(status);
480 break;
stuge36539392009-01-26 03:23:50 +0000481 }
hailfingerf1961cb2007-12-29 10:15:58 +0000482 break;
483 }
484}
uwefa98ca12008-10-18 21:14:13 +0000485
hailfingerffcf81a2008-11-03 00:02:11 +0000486int spi_chip_erase_60(struct flashchip *flash)
487{
hailfingerc1b2e912008-11-18 00:41:02 +0000488 int result;
hailfingerb7c30022009-07-11 19:28:36 +0000489 struct spi_command spicommands[] = {
490 {
491 .writecnt = JEDEC_WREN_OUTSIZE,
492 .writearr = (const unsigned char[]){ JEDEC_WREN },
493 .readcnt = 0,
494 .readarr = NULL,
495 }, {
496 .writecnt = JEDEC_CE_60_OUTSIZE,
497 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
498 .readcnt = 0,
499 .readarr = NULL,
500 }, {
501 .writecnt = 0,
502 .writearr = NULL,
503 .readcnt = 0,
504 .readarr = NULL,
505 }};
hailfingerffcf81a2008-11-03 00:02:11 +0000506
hailfingerc1b2e912008-11-18 00:41:02 +0000507 result = spi_disable_blockprotect();
508 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000509 fprintf(stderr, "spi_disable_blockprotect failed\n");
hailfingerc1b2e912008-11-18 00:41:02 +0000510 return result;
511 }
hailfingerb7c30022009-07-11 19:28:36 +0000512
513 result = spi_send_multicommand(spicommands);
hailfingerc1b2e912008-11-18 00:41:02 +0000514 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000515 fprintf(stderr, "%s failed during command execution\n",
516 __func__);
hailfingerc1b2e912008-11-18 00:41:02 +0000517 return result;
518 }
hailfingerffcf81a2008-11-03 00:02:11 +0000519 /* Wait until the Write-In-Progress bit is cleared.
520 * This usually takes 1-85 s, so wait in 1 s steps.
521 */
hailfingerc1b2e912008-11-18 00:41:02 +0000522 /* FIXME: We assume spi_read_status_register will never fail. */
hailfingerffcf81a2008-11-03 00:02:11 +0000523 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingere5829f62009-06-05 17:48:08 +0000524 programmer_delay(1000 * 1000);
hailfingera1289042009-06-24 08:28:39 +0000525 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
526 fprintf(stderr, "ERASE FAILED!\n");
527 return -1;
528 }
hailfingerffcf81a2008-11-03 00:02:11 +0000529 return 0;
530}
531
stuge2bb6ab32008-05-10 23:07:52 +0000532int spi_chip_erase_c7(struct flashchip *flash)
hailfingerf71c0ac2007-10-18 00:24:07 +0000533{
hailfingerc1b2e912008-11-18 00:41:02 +0000534 int result;
hailfingerb7c30022009-07-11 19:28:36 +0000535 struct spi_command spicommands[] = {
536 {
537 .writecnt = JEDEC_WREN_OUTSIZE,
538 .writearr = (const unsigned char[]){ JEDEC_WREN },
539 .readcnt = 0,
540 .readarr = NULL,
541 }, {
542 .writecnt = JEDEC_CE_C7_OUTSIZE,
543 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
544 .readcnt = 0,
545 .readarr = NULL,
546 }, {
547 .writecnt = 0,
548 .writearr = NULL,
549 .readcnt = 0,
550 .readarr = NULL,
551 }};
uwefa98ca12008-10-18 21:14:13 +0000552
hailfingerc1b2e912008-11-18 00:41:02 +0000553 result = spi_disable_blockprotect();
554 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000555 fprintf(stderr, "spi_disable_blockprotect failed\n");
hailfingerc1b2e912008-11-18 00:41:02 +0000556 return result;
557 }
hailfingerb7c30022009-07-11 19:28:36 +0000558
559 result = spi_send_multicommand(spicommands);
hailfingerc1b2e912008-11-18 00:41:02 +0000560 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000561 fprintf(stderr, "%s failed during command execution\n", __func__);
hailfingerc1b2e912008-11-18 00:41:02 +0000562 return result;
563 }
hailfinger1b24dbb2007-10-22 16:15:28 +0000564 /* Wait until the Write-In-Progress bit is cleared.
565 * This usually takes 1-85 s, so wait in 1 s steps.
566 */
hailfingerc1b2e912008-11-18 00:41:02 +0000567 /* FIXME: We assume spi_read_status_register will never fail. */
stuge2bb6ab32008-05-10 23:07:52 +0000568 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingere5829f62009-06-05 17:48:08 +0000569 programmer_delay(1000 * 1000);
hailfingera1289042009-06-24 08:28:39 +0000570 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
571 fprintf(stderr, "ERASE FAILED!\n");
572 return -1;
573 }
hailfingerf71c0ac2007-10-18 00:24:07 +0000574 return 0;
575}
576
hailfingerc1b2e912008-11-18 00:41:02 +0000577int spi_chip_erase_60_c7(struct flashchip *flash)
578{
579 int result;
580 result = spi_chip_erase_60(flash);
581 if (result) {
582 printf_debug("spi_chip_erase_60 failed, trying c7\n");
583 result = spi_chip_erase_c7(flash);
584 }
585 return result;
586}
587
hailfingera1289042009-06-24 08:28:39 +0000588int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
hailfingerffcf81a2008-11-03 00:02:11 +0000589{
hailfinger61949942009-05-09 02:09:45 +0000590 int result;
hailfinger8246cc32009-07-11 22:26:52 +0000591 struct spi_command spicommands[] = {
592 {
593 .writecnt = JEDEC_WREN_OUTSIZE,
594 .writearr = (const unsigned char[]){ JEDEC_WREN },
595 .readcnt = 0,
596 .readarr = NULL,
597 }, {
598 .writecnt = JEDEC_BE_52_OUTSIZE,
599 .writearr = (const unsigned char[]){ JEDEC_BE_52, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
600 .readcnt = 0,
601 .readarr = NULL,
602 }, {
603 .writecnt = 0,
604 .writearr = NULL,
605 .readcnt = 0,
606 .readarr = NULL,
607 }};
hailfingerffcf81a2008-11-03 00:02:11 +0000608
hailfinger8246cc32009-07-11 22:26:52 +0000609 result = spi_send_multicommand(spicommands);
610 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000611 fprintf(stderr, "%s failed during command execution\n",
612 __func__);
hailfinger61949942009-05-09 02:09:45 +0000613 return result;
hailfinger8246cc32009-07-11 22:26:52 +0000614 }
hailfingerffcf81a2008-11-03 00:02:11 +0000615 /* Wait until the Write-In-Progress bit is cleared.
616 * This usually takes 100-4000 ms, so wait in 100 ms steps.
617 */
618 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingere5829f62009-06-05 17:48:08 +0000619 programmer_delay(100 * 1000);
hailfingera1289042009-06-24 08:28:39 +0000620 if (check_erased_range(flash, addr, blocklen)) {
621 fprintf(stderr, "ERASE FAILED!\n");
622 return -1;
623 }
hailfingerffcf81a2008-11-03 00:02:11 +0000624 return 0;
625}
626
hailfinger1b24dbb2007-10-22 16:15:28 +0000627/* Block size is usually
628 * 64k for Macronix
629 * 32k for SST
630 * 4-32k non-uniform for EON
631 */
hailfingera1289042009-06-24 08:28:39 +0000632int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
hailfinger1b24dbb2007-10-22 16:15:28 +0000633{
hailfinger61949942009-05-09 02:09:45 +0000634 int result;
hailfinger8246cc32009-07-11 22:26:52 +0000635 struct spi_command spicommands[] = {
636 {
637 .writecnt = JEDEC_WREN_OUTSIZE,
638 .writearr = (const unsigned char[]){ JEDEC_WREN },
639 .readcnt = 0,
640 .readarr = NULL,
641 }, {
642 .writecnt = JEDEC_BE_D8_OUTSIZE,
643 .writearr = (const unsigned char[]){ JEDEC_BE_D8, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
644 .readcnt = 0,
645 .readarr = NULL,
646 }, {
647 .writecnt = 0,
648 .writearr = NULL,
649 .readcnt = 0,
650 .readarr = NULL,
651 }};
hailfinger1b24dbb2007-10-22 16:15:28 +0000652
hailfinger8246cc32009-07-11 22:26:52 +0000653 result = spi_send_multicommand(spicommands);
654 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000655 fprintf(stderr, "%s failed during command execution\n", __func__);
hailfinger61949942009-05-09 02:09:45 +0000656 return result;
hailfinger8246cc32009-07-11 22:26:52 +0000657 }
hailfinger1b24dbb2007-10-22 16:15:28 +0000658 /* Wait until the Write-In-Progress bit is cleared.
659 * This usually takes 100-4000 ms, so wait in 100 ms steps.
660 */
stuge2bb6ab32008-05-10 23:07:52 +0000661 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingere5829f62009-06-05 17:48:08 +0000662 programmer_delay(100 * 1000);
hailfingera1289042009-06-24 08:28:39 +0000663 if (check_erased_range(flash, addr, blocklen)) {
664 fprintf(stderr, "ERASE FAILED!\n");
665 return -1;
666 }
hailfinger1b24dbb2007-10-22 16:15:28 +0000667 return 0;
668}
669
stepan0f7bff02008-10-29 22:13:20 +0000670int spi_chip_erase_d8(struct flashchip *flash)
671{
672 int i, rc = 0;
673 int total_size = flash->total_size * 1024;
674 int erase_size = 64 * 1024;
675
676 spi_disable_blockprotect();
677
678 printf("Erasing chip: \n");
679
680 for (i = 0; i < total_size / erase_size; i++) {
hailfingera1289042009-06-24 08:28:39 +0000681 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
stepan0f7bff02008-10-29 22:13:20 +0000682 if (rc) {
hailfinger0cb68252009-07-23 01:33:43 +0000683 fprintf(stderr, "Error erasing block at 0x%x\n", i);
stepan0f7bff02008-10-29 22:13:20 +0000684 break;
685 }
686 }
687
688 printf("\n");
689
690 return rc;
691}
692
hailfinger1b24dbb2007-10-22 16:15:28 +0000693/* Sector size is usually 4k, though Macronix eliteflash has 64k */
hailfingera1289042009-06-24 08:28:39 +0000694int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
hailfinger1b24dbb2007-10-22 16:15:28 +0000695{
hailfinger61949942009-05-09 02:09:45 +0000696 int result;
hailfinger8246cc32009-07-11 22:26:52 +0000697 struct spi_command spicommands[] = {
698 {
699 .writecnt = JEDEC_WREN_OUTSIZE,
700 .writearr = (const unsigned char[]){ JEDEC_WREN },
701 .readcnt = 0,
702 .readarr = NULL,
703 }, {
704 .writecnt = JEDEC_SE_OUTSIZE,
705 .writearr = (const unsigned char[]){ JEDEC_SE, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff) },
706 .readcnt = 0,
707 .readarr = NULL,
708 }, {
709 .writecnt = 0,
710 .writearr = NULL,
711 .readcnt = 0,
712 .readarr = NULL,
713 }};
hailfinger1b24dbb2007-10-22 16:15:28 +0000714
hailfinger8246cc32009-07-11 22:26:52 +0000715 result = spi_send_multicommand(spicommands);
716 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000717 fprintf(stderr, "%s failed during command execution\n",
718 __func__);
hailfinger61949942009-05-09 02:09:45 +0000719 return result;
hailfinger8246cc32009-07-11 22:26:52 +0000720 }
hailfinger1b24dbb2007-10-22 16:15:28 +0000721 /* Wait until the Write-In-Progress bit is cleared.
722 * This usually takes 15-800 ms, so wait in 10 ms steps.
723 */
stuge2bb6ab32008-05-10 23:07:52 +0000724 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingere5829f62009-06-05 17:48:08 +0000725 programmer_delay(10 * 1000);
hailfingera1289042009-06-24 08:28:39 +0000726 if (check_erased_range(flash, addr, blocklen)) {
727 fprintf(stderr, "ERASE FAILED!\n");
728 return -1;
729 }
hailfinger1b24dbb2007-10-22 16:15:28 +0000730 return 0;
731}
732
hailfingera1289042009-06-24 08:28:39 +0000733int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
734{
735 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
hailfinger0cb68252009-07-23 01:33:43 +0000736 fprintf(stderr, "%s called with incorrect arguments\n",
737 __func__);
hailfingera1289042009-06-24 08:28:39 +0000738 return -1;
739 }
740 return spi_chip_erase_60(flash);
741}
742
743int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
744{
745 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
hailfinger0cb68252009-07-23 01:33:43 +0000746 fprintf(stderr, "%s called with incorrect arguments\n",
747 __func__);
hailfingera1289042009-06-24 08:28:39 +0000748 return -1;
749 }
750 return spi_chip_erase_c7(flash);
751}
752
uwe5e931bc2009-04-15 10:52:49 +0000753int spi_write_status_enable(void)
uwe17efbed2008-11-28 21:36:51 +0000754{
755 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
hailfinger128df152009-05-15 00:56:22 +0000756 int result;
uwe17efbed2008-11-28 21:36:51 +0000757
758 /* Send EWSR (Enable Write Status Register). */
hailfinger68002c22009-07-10 21:08:55 +0000759 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
hailfinger128df152009-05-15 00:56:22 +0000760
761 if (result)
hailfinger0cb68252009-07-23 01:33:43 +0000762 fprintf(stderr, "%s failed\n", __func__);
hailfinger128df152009-05-15 00:56:22 +0000763
764 return result;
uwe17efbed2008-11-28 21:36:51 +0000765}
766
hailfingerb8f7e882008-01-19 00:04:46 +0000767/*
768 * This is according the SST25VF016 datasheet, who knows it is more
769 * generic that this...
770 */
hailfingerc1b2e912008-11-18 00:41:02 +0000771int spi_write_status_register(int status)
hailfingerb8f7e882008-01-19 00:04:46 +0000772{
hailfinger0d3e9eb2009-07-22 20:09:28 +0000773 int result;
774 struct spi_command spicommands[] = {
775 {
776 .writecnt = JEDEC_EWSR_OUTSIZE,
777 .writearr = (const unsigned char[]){ JEDEC_EWSR },
778 .readcnt = 0,
779 .readarr = NULL,
780 }, {
781 .writecnt = JEDEC_WRSR_OUTSIZE,
782 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
783 .readcnt = 0,
784 .readarr = NULL,
785 }, {
786 .writecnt = 0,
787 .writearr = NULL,
788 .readcnt = 0,
789 .readarr = NULL,
790 }};
hailfingerb8f7e882008-01-19 00:04:46 +0000791
hailfinger0d3e9eb2009-07-22 20:09:28 +0000792 result = spi_send_multicommand(spicommands);
793 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000794 fprintf(stderr, "%s failed during command execution\n",
795 __func__);
hailfinger0d3e9eb2009-07-22 20:09:28 +0000796 }
797 return result;
hailfingerb8f7e882008-01-19 00:04:46 +0000798}
799
hailfingerec9334b2009-07-12 12:06:18 +0000800int spi_byte_program(int addr, uint8_t byte)
hailfingerb8f7e882008-01-19 00:04:46 +0000801{
hailfingerec9334b2009-07-12 12:06:18 +0000802 int result;
803 struct spi_command spicommands[] = {
804 {
805 .writecnt = JEDEC_WREN_OUTSIZE,
806 .writearr = (const unsigned char[]){ JEDEC_WREN },
807 .readcnt = 0,
808 .readarr = NULL,
809 }, {
810 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
811 .writearr = (const unsigned char[]){ JEDEC_BYTE_PROGRAM, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr & 0xff), byte },
812 .readcnt = 0,
813 .readarr = NULL,
814 }, {
815 .writecnt = 0,
816 .writearr = NULL,
817 .readcnt = 0,
818 .readarr = NULL,
819 }};
hailfingerb8f7e882008-01-19 00:04:46 +0000820
hailfingerec9334b2009-07-12 12:06:18 +0000821 result = spi_send_multicommand(spicommands);
822 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000823 fprintf(stderr, "%s failed during command execution\n",
824 __func__);
hailfingerec9334b2009-07-12 12:06:18 +0000825 }
826 return result;
hailfingerb8f7e882008-01-19 00:04:46 +0000827}
828
hailfinger07a88442009-06-12 08:10:33 +0000829int spi_nbyte_program(int address, uint8_t *bytes, int len)
830{
hailfingerec9334b2009-07-12 12:06:18 +0000831 int result;
832 /* FIXME: Switch to malloc based on len unless that kills speed. */
hailfinger07a88442009-06-12 08:10:33 +0000833 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
834 JEDEC_BYTE_PROGRAM,
835 (address >> 16) & 0xff,
836 (address >> 8) & 0xff,
837 (address >> 0) & 0xff,
838 };
hailfingerec9334b2009-07-12 12:06:18 +0000839 struct spi_command spicommands[] = {
840 {
841 .writecnt = JEDEC_WREN_OUTSIZE,
842 .writearr = (const unsigned char[]){ JEDEC_WREN },
843 .readcnt = 0,
844 .readarr = NULL,
845 }, {
846 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
847 .writearr = cmd,
848 .readcnt = 0,
849 .readarr = NULL,
850 }, {
851 .writecnt = 0,
852 .writearr = NULL,
853 .readcnt = 0,
854 .readarr = NULL,
855 }};
hailfinger07a88442009-06-12 08:10:33 +0000856
hailfingerec9334b2009-07-12 12:06:18 +0000857 if (!len) {
hailfinger0cb68252009-07-23 01:33:43 +0000858 fprintf(stderr, "%s called for zero-length write\n", __func__);
hailfingerec9334b2009-07-12 12:06:18 +0000859 return 1;
860 }
hailfinger07a88442009-06-12 08:10:33 +0000861 if (len > 256) {
hailfinger0cb68252009-07-23 01:33:43 +0000862 fprintf(stderr, "%s called for too long a write\n", __func__);
hailfinger07a88442009-06-12 08:10:33 +0000863 return 1;
864 }
865
866 memcpy(&cmd[4], bytes, len);
867
hailfingerec9334b2009-07-12 12:06:18 +0000868 result = spi_send_multicommand(spicommands);
869 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000870 fprintf(stderr, "%s failed during command execution\n",
871 __func__);
hailfingerec9334b2009-07-12 12:06:18 +0000872 }
873 return result;
hailfinger07a88442009-06-12 08:10:33 +0000874}
875
hailfingerc1b2e912008-11-18 00:41:02 +0000876int spi_disable_blockprotect(void)
hailfingerb8f7e882008-01-19 00:04:46 +0000877{
878 uint8_t status;
hailfingerc1b2e912008-11-18 00:41:02 +0000879 int result;
hailfingerb8f7e882008-01-19 00:04:46 +0000880
stuge2bb6ab32008-05-10 23:07:52 +0000881 status = spi_read_status_register();
hailfingerb8f7e882008-01-19 00:04:46 +0000882 /* If there is block protection in effect, unprotect it first. */
883 if ((status & 0x3c) != 0) {
884 printf_debug("Some block protection in effect, disabling\n");
hailfingerc1b2e912008-11-18 00:41:02 +0000885 result = spi_write_status_register(status & ~0x3c);
886 if (result) {
hailfinger0cb68252009-07-23 01:33:43 +0000887 fprintf(stderr, "spi_write_status_register failed\n");
hailfingerc1b2e912008-11-18 00:41:02 +0000888 return result;
889 }
hailfingerb8f7e882008-01-19 00:04:46 +0000890 }
hailfingerc1b2e912008-11-18 00:41:02 +0000891 return 0;
hailfingerb8f7e882008-01-19 00:04:46 +0000892}
893
hailfingerc1b2e912008-11-18 00:41:02 +0000894int spi_nbyte_read(int address, uint8_t *bytes, int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000895{
uwefa98ca12008-10-18 21:14:13 +0000896 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
897 JEDEC_READ,
hailfinger9cd4cf12008-01-22 14:37:31 +0000898 (address >> 16) & 0xff,
899 (address >> 8) & 0xff,
900 (address >> 0) & 0xff,
hailfingerb8f7e882008-01-19 00:04:46 +0000901 };
902
903 /* Send Read */
hailfinger68002c22009-07-10 21:08:55 +0000904 return spi_send_command(sizeof(cmd), len, cmd, bytes);
hailfingerb8f7e882008-01-19 00:04:46 +0000905}
906
hailfinger7b414742009-06-13 12:04:03 +0000907/*
908 * Read a complete flash chip.
909 * Each page is read separately in chunks with a maximum size of chunksize.
910 */
hailfinger0f08b7a2009-06-16 08:55:44 +0000911int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
hailfinger7b414742009-06-13 12:04:03 +0000912{
913 int rc = 0;
hailfinger0f08b7a2009-06-16 08:55:44 +0000914 int i, j, starthere, lenhere;
hailfinger7b414742009-06-13 12:04:03 +0000915 int page_size = flash->page_size;
916 int toread;
917
hailfinger0f08b7a2009-06-16 08:55:44 +0000918 /* Warning: This loop has a very unusual condition and body.
919 * The loop needs to go through each page with at least one affected
920 * byte. The lowest page number is (start / page_size) since that
921 * division rounds down. The highest page number we want is the page
922 * where the last byte of the range lives. That last byte has the
923 * address (start + len - 1), thus the highest page number is
924 * (start + len - 1) / page_size. Since we want to include that last
925 * page as well, the loop condition uses <=.
926 */
927 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
928 /* Byte position of the first byte in the range in this page. */
929 /* starthere is an offset to the base address of the chip. */
930 starthere = max(start, i * page_size);
931 /* Length of bytes in the range in this page. */
932 lenhere = min(start + len, (i + 1) * page_size) - starthere;
933 for (j = 0; j < lenhere; j += chunksize) {
934 toread = min(chunksize, lenhere - j);
935 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
hailfinger7b414742009-06-13 12:04:03 +0000936 if (rc)
937 break;
938 }
939 if (rc)
940 break;
941 }
942
943 return rc;
944}
945
hailfinger0f08b7a2009-06-16 08:55:44 +0000946int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000947{
hailfinger948b81f2009-07-22 15:36:50 +0000948 if (!spi_programmer[spi_controller].read) {
949 fprintf(stderr, "%s called, but SPI read is unsupported on this"
950 " hardware. Please report a bug.\n", __func__);
951 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000952 }
953
hailfinger948b81f2009-07-22 15:36:50 +0000954 return spi_programmer[spi_controller].read(flash, buf, start, len);
hailfingerb8f7e882008-01-19 00:04:46 +0000955}
956
hailfingered063f52009-05-09 02:30:21 +0000957/*
958 * Program chip using byte programming. (SLOW!)
959 * This is for chips which can only handle one byte writes
960 * and for chips where memory mapped programming is impossible
961 * (e.g. due to size constraints in IT87* for over 512 kB)
962 */
963int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
964{
965 int total_size = 1024 * flash->total_size;
966 int i;
967
968 spi_disable_blockprotect();
969 for (i = 0; i < total_size; i++) {
hailfingered063f52009-05-09 02:30:21 +0000970 spi_byte_program(i, buf[i]);
971 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingere5829f62009-06-05 17:48:08 +0000972 programmer_delay(10);
hailfingered063f52009-05-09 02:30:21 +0000973 }
974
975 return 0;
976}
977
978/*
979 * Program chip using page (256 bytes) programming.
980 * Some SPI masters can't do this, they use single byte programming instead.
981 */
hailfinger87c05482009-05-09 02:34:18 +0000982int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
hailfinger2c361e42008-05-13 23:03:12 +0000983{
hailfinger948b81f2009-07-22 15:36:50 +0000984 if (!spi_programmer[spi_controller].write_256) {
985 fprintf(stderr, "%s called, but SPI page write is unsupported "
986 " on this hardware. Please report a bug.\n", __func__);
987 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000988 }
989
hailfinger948b81f2009-07-22 15:36:50 +0000990 return spi_programmer[spi_controller].write_256(flash, buf);
hailfingerf71c0ac2007-10-18 00:24:07 +0000991}
stuge712ce862009-01-26 03:37:40 +0000992
hailfinger54c14662009-05-13 11:40:08 +0000993uint32_t spi_get_valid_read_addr(void)
994{
995 /* Need to return BBAR for ICH chipsets. */
996 return 0;
997}
998
uwe5e931bc2009-04-15 10:52:49 +0000999int spi_aai_write(struct flashchip *flash, uint8_t *buf)
1000{
stuge712ce862009-01-26 03:37:40 +00001001 uint32_t pos = 2, size = flash->total_size * 1024;
1002 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
hailfinger61949942009-05-09 02:09:45 +00001003 int result;
1004
hailfinger40167462009-05-31 17:57:34 +00001005 switch (spi_controller) {
1006 case SPI_CONTROLLER_WBSIO:
uwe5e931bc2009-04-15 10:52:49 +00001007 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
1008 " degrading to byte program\n", __func__);
hailfingered063f52009-05-09 02:30:21 +00001009 return spi_chip_write_1(flash, buf);
uwe5e931bc2009-04-15 10:52:49 +00001010 default:
1011 break;
stuge712ce862009-01-26 03:37:40 +00001012 }
hailfingera1289042009-06-24 08:28:39 +00001013 if (flash->erase(flash)) {
1014 fprintf(stderr, "ERASE FAILED!\n");
1015 return -1;
1016 }
hailfinger61949942009-05-09 02:09:45 +00001017 result = spi_write_enable();
1018 if (result)
1019 return result;
hailfinger68002c22009-07-10 21:08:55 +00001020 spi_send_command(6, 0, w, NULL);
stuge712ce862009-01-26 03:37:40 +00001021 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingere5829f62009-06-05 17:48:08 +00001022 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
stuge712ce862009-01-26 03:37:40 +00001023 while (pos < size) {
1024 w[1] = buf[pos++];
1025 w[2] = buf[pos++];
hailfinger68002c22009-07-10 21:08:55 +00001026 spi_send_command(3, 0, w, NULL);
stuge712ce862009-01-26 03:37:40 +00001027 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
hailfingere5829f62009-06-05 17:48:08 +00001028 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
stuge712ce862009-01-26 03:37:40 +00001029 }
1030 spi_write_disable();
1031 return 0;
1032}