blob: c9a4664851d11de6578aaa29a5369669aef37845 [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
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000032static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000033{
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
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000038 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000039 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
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000048static int spi_rems(struct flashctx *flash, unsigned char *readarr)
Sean Nelson14ba6682010-02-26 05:48:29 +000049{
50 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
51 uint32_t readaddr;
52 int ret;
53
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000054 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd,
55 readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000056 if (ret == SPI_INVALID_ADDRESS) {
57 /* Find the lowest even address allowed for reads. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000058 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
Sean Nelson14ba6682010-02-26 05:48:29 +000059 cmd[1] = (readaddr >> 16) & 0xff,
60 cmd[2] = (readaddr >> 8) & 0xff,
61 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000062 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE,
63 cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000064 }
65 if (ret)
66 return ret;
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +000067 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
Sean Nelson14ba6682010-02-26 05:48:29 +000068 return 0;
69}
70
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000071static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000072{
73 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
74 uint32_t readaddr;
75 int ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000076 int i;
Sean Nelson14ba6682010-02-26 05:48:29 +000077
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000078 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000079 if (ret == SPI_INVALID_ADDRESS) {
80 /* Find the lowest even address allowed for reads. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000081 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
Sean Nelson14ba6682010-02-26 05:48:29 +000082 cmd[1] = (readaddr >> 16) & 0xff,
83 cmd[2] = (readaddr >> 8) & 0xff,
84 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000085 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000086 }
87 if (ret)
88 return ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000089 msg_cspew("RES returned");
90 for (i = 0; i < bytes; i++)
91 msg_cspew(" 0x%02x", readarr[i]);
92 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000093 return 0;
94}
95
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000096int spi_write_enable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +000097{
Mathias Krausea60faab2011-01-17 07:50:42 +000098 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Sean Nelson14ba6682010-02-26 05:48:29 +000099 int result;
100
101 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000102 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000103
104 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000105 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000106
107 return result;
108}
109
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000110int spi_write_disable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000111{
Mathias Krausea60faab2011-01-17 07:50:42 +0000112 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Sean Nelson14ba6682010-02-26 05:48:29 +0000113
114 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000115 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000116}
117
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000118static int probe_spi_rdid_generic(struct flashctx *flash, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +0000119{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000120 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000121 unsigned char readarr[4];
122 uint32_t id1;
123 uint32_t id2;
124
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000125 if (spi_rdid(flash, readarr, bytes)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000126 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000127 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000128
129 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000130 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000131
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000132 /* Check if this is a continuation vendor ID.
133 * FIXME: Handle continuation device IDs.
134 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000135 if (readarr[0] == 0x7f) {
136 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000137 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000138 id1 = (readarr[0] << 8) | readarr[1];
139 id2 = readarr[2];
140 if (bytes > 3) {
141 id2 <<= 8;
142 id2 |= readarr[3];
143 }
144 } else {
145 id1 = readarr[0];
146 id2 = (readarr[1] << 8) | readarr[2];
147 }
148
Sean Nelsoned479d22010-03-24 23:14:32 +0000149 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000150
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000151 if (id1 == chip->manufacture_id && id2 == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000152 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000153
154 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000155 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000156 return 1;
157
158 /* Test if there is any vendor ID. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000159 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff)
Sean Nelson14ba6682010-02-26 05:48:29 +0000160 return 1;
161
162 return 0;
163}
164
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000165int probe_spi_rdid(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000166{
167 return probe_spi_rdid_generic(flash, 3);
168}
169
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000170int probe_spi_rdid4(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000171{
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000172 /* Some SPI controllers do not support commands with writecnt=1 and
173 * readcnt=4.
174 */
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000175 switch (flash->pgm->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000176#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000177#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000178 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000179 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000180 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
181 return 0;
182 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000183#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000184#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000185 default:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000186 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000187 }
188
189 return 0;
190}
191
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000192int probe_spi_rems(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000193{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000194 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000195 unsigned char readarr[JEDEC_REMS_INSIZE];
196 uint32_t id1, id2;
197
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000198 if (spi_rems(flash, readarr)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000199 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000200 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000201
202 id1 = readarr[0];
203 id2 = readarr[1];
204
Sean Nelsoned479d22010-03-24 23:14:32 +0000205 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000206
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000207 if (id1 == chip->manufacture_id && id2 == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000208 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000209
210 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000211 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000212 return 1;
213
214 /* Test if there is any vendor ID. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000215 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff)
Sean Nelson14ba6682010-02-26 05:48:29 +0000216 return 1;
217
218 return 0;
219}
220
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000221int probe_spi_res1(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000222{
Mathias Krausea60faab2011-01-17 07:50:42 +0000223 static const unsigned char allff[] = {0xff, 0xff, 0xff};
224 static const unsigned char all00[] = {0x00, 0x00, 0x00};
Sean Nelson14ba6682010-02-26 05:48:29 +0000225 unsigned char readarr[3];
226 uint32_t id2;
Sean Nelson14ba6682010-02-26 05:48:29 +0000227
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000228 /* We only want one-byte RES if RDID and REMS are unusable. */
229
Sean Nelson14ba6682010-02-26 05:48:29 +0000230 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
231 * 0x00 0x00 0x00. In that case, RES is pointless.
232 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000233 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000234 memcmp(readarr, all00, 3)) {
235 msg_cdbg("Ignoring RES in favour of RDID.\n");
236 return 0;
237 }
238 /* Check if REMS is usable and does not return 0xff 0xff or
239 * 0x00 0x00. In that case, RES is pointless.
240 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000241 if (!spi_rems(flash, readarr) &&
242 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000243 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
244 msg_cdbg("Ignoring RES in favour of REMS.\n");
245 return 0;
246 }
247
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000248 if (spi_res(flash, readarr, 1)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000249 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000250 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000251
Sean Nelson14ba6682010-02-26 05:48:29 +0000252 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000253
Sean Nelsoned479d22010-03-24 23:14:32 +0000254 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000255
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000256 if (id2 != flash->chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000257 return 0;
258
Sean Nelson14ba6682010-02-26 05:48:29 +0000259 return 1;
260}
261
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000262int probe_spi_res2(struct flashctx *flash)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000263{
264 unsigned char readarr[2];
265 uint32_t id1, id2;
266
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000267 if (spi_res(flash, readarr, 2)) {
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000268 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000269 }
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000270
271 id1 = readarr[0];
272 id2 = readarr[1];
273
274 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
275
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000276 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000277 return 0;
278
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000279 return 1;
280}
281
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000282int spi_chip_erase_60(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000283{
284 int result;
285 struct spi_command cmds[] = {
286 {
287 .writecnt = JEDEC_WREN_OUTSIZE,
288 .writearr = (const unsigned char[]){ JEDEC_WREN },
289 .readcnt = 0,
290 .readarr = NULL,
291 }, {
292 .writecnt = JEDEC_CE_60_OUTSIZE,
293 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
294 .readcnt = 0,
295 .readarr = NULL,
296 }, {
297 .writecnt = 0,
298 .writearr = NULL,
299 .readcnt = 0,
300 .readarr = NULL,
301 }};
302
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000303 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000304 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000305 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000306 __func__);
307 return result;
308 }
309 /* Wait until the Write-In-Progress bit is cleared.
310 * This usually takes 1-85 s, so wait in 1 s steps.
311 */
312 /* FIXME: We assume spi_read_status_register will never fail. */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000313 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000314 programmer_delay(1000 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000315 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000316 return 0;
317}
318
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000319int spi_chip_erase_62(struct flashctx *flash)
320{
321 int result;
322 struct spi_command cmds[] = {
323 {
324 .writecnt = JEDEC_WREN_OUTSIZE,
325 .writearr = (const unsigned char[]){ JEDEC_WREN },
326 .readcnt = 0,
327 .readarr = NULL,
328 }, {
329 .writecnt = JEDEC_CE_62_OUTSIZE,
330 .writearr = (const unsigned char[]){ JEDEC_CE_62 },
331 .readcnt = 0,
332 .readarr = NULL,
333 }, {
334 .writecnt = 0,
335 .writearr = NULL,
336 .readcnt = 0,
337 .readarr = NULL,
338 }};
339
340 result = spi_send_multicommand(flash, cmds);
341 if (result) {
342 msg_cerr("%s failed during command execution\n",
343 __func__);
344 return result;
345 }
346 /* Wait until the Write-In-Progress bit is cleared.
347 * This usually takes 2-5 s, so wait in 100 ms steps.
348 */
349 /* FIXME: We assume spi_read_status_register will never fail. */
350 while (spi_read_status_register(flash) & SPI_SR_WIP)
351 programmer_delay(100 * 1000);
352 /* FIXME: Check the status register for errors. */
353 return 0;
354}
355
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000356int spi_chip_erase_c7(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000357{
358 int result;
359 struct spi_command cmds[] = {
360 {
361 .writecnt = JEDEC_WREN_OUTSIZE,
362 .writearr = (const unsigned char[]){ JEDEC_WREN },
363 .readcnt = 0,
364 .readarr = NULL,
365 }, {
366 .writecnt = JEDEC_CE_C7_OUTSIZE,
367 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
368 .readcnt = 0,
369 .readarr = NULL,
370 }, {
371 .writecnt = 0,
372 .writearr = NULL,
373 .readcnt = 0,
374 .readarr = NULL,
375 }};
376
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000377 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000378 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000379 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000380 return result;
381 }
382 /* Wait until the Write-In-Progress bit is cleared.
383 * This usually takes 1-85 s, so wait in 1 s steps.
384 */
385 /* FIXME: We assume spi_read_status_register will never fail. */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000386 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000387 programmer_delay(1000 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000388 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000389 return 0;
390}
391
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000392int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
393 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000394{
395 int result;
396 struct spi_command cmds[] = {
397 {
398 .writecnt = JEDEC_WREN_OUTSIZE,
399 .writearr = (const unsigned char[]){ JEDEC_WREN },
400 .readcnt = 0,
401 .readarr = NULL,
402 }, {
403 .writecnt = JEDEC_BE_52_OUTSIZE,
404 .writearr = (const unsigned char[]){
405 JEDEC_BE_52,
406 (addr >> 16) & 0xff,
407 (addr >> 8) & 0xff,
408 (addr & 0xff)
409 },
410 .readcnt = 0,
411 .readarr = NULL,
412 }, {
413 .writecnt = 0,
414 .writearr = NULL,
415 .readcnt = 0,
416 .readarr = NULL,
417 }};
418
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000419 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000420 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000421 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000422 __func__, addr);
423 return result;
424 }
425 /* Wait until the Write-In-Progress bit is cleared.
426 * This usually takes 100-4000 ms, so wait in 100 ms steps.
427 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000428 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000429 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000430 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000431 return 0;
432}
433
434/* Block size is usually
435 * 64k for Macronix
436 * 32k for SST
437 * 4-32k non-uniform for EON
438 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000439int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
440 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000441{
442 int result;
443 struct spi_command cmds[] = {
444 {
445 .writecnt = JEDEC_WREN_OUTSIZE,
446 .writearr = (const unsigned char[]){ JEDEC_WREN },
447 .readcnt = 0,
448 .readarr = NULL,
449 }, {
450 .writecnt = JEDEC_BE_D8_OUTSIZE,
451 .writearr = (const unsigned char[]){
452 JEDEC_BE_D8,
453 (addr >> 16) & 0xff,
454 (addr >> 8) & 0xff,
455 (addr & 0xff)
456 },
457 .readcnt = 0,
458 .readarr = NULL,
459 }, {
460 .writecnt = 0,
461 .writearr = NULL,
462 .readcnt = 0,
463 .readarr = NULL,
464 }};
465
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000466 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000467 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000468 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000469 __func__, addr);
470 return result;
471 }
472 /* Wait until the Write-In-Progress bit is cleared.
473 * This usually takes 100-4000 ms, so wait in 100 ms steps.
474 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000475 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000476 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000477 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000478 return 0;
479}
480
481/* Block size is usually
482 * 4k for PMC
483 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000484int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
485 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000486{
487 int result;
488 struct spi_command cmds[] = {
489 {
490 .writecnt = JEDEC_WREN_OUTSIZE,
491 .writearr = (const unsigned char[]){ JEDEC_WREN },
492 .readcnt = 0,
493 .readarr = NULL,
494 }, {
495 .writecnt = JEDEC_BE_D7_OUTSIZE,
496 .writearr = (const unsigned char[]){
497 JEDEC_BE_D7,
498 (addr >> 16) & 0xff,
499 (addr >> 8) & 0xff,
500 (addr & 0xff)
501 },
502 .readcnt = 0,
503 .readarr = NULL,
504 }, {
505 .writecnt = 0,
506 .writearr = NULL,
507 .readcnt = 0,
508 .readarr = NULL,
509 }};
510
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000511 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000512 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000513 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000514 __func__, addr);
515 return result;
516 }
517 /* Wait until the Write-In-Progress bit is cleared.
518 * This usually takes 100-4000 ms, so wait in 100 ms steps.
519 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000520 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000521 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000522 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000523 return 0;
524}
525
Sean Nelson14ba6682010-02-26 05:48:29 +0000526/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000527int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
528 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000529{
530 int result;
531 struct spi_command cmds[] = {
532 {
533 .writecnt = JEDEC_WREN_OUTSIZE,
534 .writearr = (const unsigned char[]){ JEDEC_WREN },
535 .readcnt = 0,
536 .readarr = NULL,
537 }, {
538 .writecnt = JEDEC_SE_OUTSIZE,
539 .writearr = (const unsigned char[]){
540 JEDEC_SE,
541 (addr >> 16) & 0xff,
542 (addr >> 8) & 0xff,
543 (addr & 0xff)
544 },
545 .readcnt = 0,
546 .readarr = NULL,
547 }, {
548 .writecnt = 0,
549 .writearr = NULL,
550 .readcnt = 0,
551 .readarr = NULL,
552 }};
553
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000554 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000555 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000556 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000557 __func__, addr);
558 return result;
559 }
560 /* Wait until the Write-In-Progress bit is cleared.
561 * This usually takes 15-800 ms, so wait in 10 ms steps.
562 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000563 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000564 programmer_delay(10 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000565 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000566 return 0;
567}
568
Stefan Tauner94b39b42012-10-27 00:06:02 +0000569int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
570{
571 int result;
572 struct spi_command cmds[] = {
573 {
574/* .writecnt = JEDEC_WREN_OUTSIZE,
575 .writearr = (const unsigned char[]){ JEDEC_WREN },
576 .readcnt = 0,
577 .readarr = NULL,
578 }, { */
579 .writecnt = JEDEC_BE_50_OUTSIZE,
580 .writearr = (const unsigned char[]){
581 JEDEC_BE_50,
582 (addr >> 16) & 0xff,
583 (addr >> 8) & 0xff,
584 (addr & 0xff)
585 },
586 .readcnt = 0,
587 .readarr = NULL,
588 }, {
589 .writecnt = 0,
590 .writearr = NULL,
591 .readcnt = 0,
592 .readarr = NULL,
593 }};
594
595 result = spi_send_multicommand(flash, cmds);
596 if (result) {
597 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
598 return result;
599 }
600 /* Wait until the Write-In-Progress bit is cleared.
601 * This usually takes 10 ms, so wait in 1 ms steps.
602 */
603 while (spi_read_status_register(flash) & SPI_SR_WIP)
604 programmer_delay(1 * 1000);
605 /* FIXME: Check the status register for errors. */
606 return 0;
607}
608
609int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
610{
611 int result;
612 struct spi_command cmds[] = {
613 {
614/* .writecnt = JEDEC_WREN_OUTSIZE,
615 .writearr = (const unsigned char[]){ JEDEC_WREN },
616 .readcnt = 0,
617 .readarr = NULL,
618 }, { */
619 .writecnt = JEDEC_BE_81_OUTSIZE,
620 .writearr = (const unsigned char[]){
621 JEDEC_BE_81,
622 (addr >> 16) & 0xff,
623 (addr >> 8) & 0xff,
624 (addr & 0xff)
625 },
626 .readcnt = 0,
627 .readarr = NULL,
628 }, {
629 .writecnt = 0,
630 .writearr = NULL,
631 .readcnt = 0,
632 .readarr = NULL,
633 }};
634
635 result = spi_send_multicommand(flash, cmds);
636 if (result) {
637 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
638 return result;
639 }
640 /* Wait until the Write-In-Progress bit is cleared.
641 * This usually takes 8 ms, so wait in 1 ms steps.
642 */
643 while (spi_read_status_register(flash) & SPI_SR_WIP)
644 programmer_delay(1 * 1000);
645 /* FIXME: Check the status register for errors. */
646 return 0;
647}
648
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000649int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
650 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000651{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000652 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000653 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000654 __func__);
655 return -1;
656 }
657 return spi_chip_erase_60(flash);
658}
659
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000660int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
661{
662 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
663 msg_cerr("%s called with incorrect arguments\n",
664 __func__);
665 return -1;
666 }
667 return spi_chip_erase_62(flash);
668}
669
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000670int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
671 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000672{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000673 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000674 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000675 __func__);
676 return -1;
677 }
678 return spi_chip_erase_c7(flash);
679}
680
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000681erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode)
682{
683 switch(opcode){
684 case 0xff:
685 case 0x00:
686 /* Not specified, assuming "not supported". */
687 return NULL;
688 case 0x20:
689 return &spi_block_erase_20;
690 case 0x52:
691 return &spi_block_erase_52;
692 case 0x60:
693 return &spi_block_erase_60;
694 case 0xc7:
695 return &spi_block_erase_c7;
696 case 0xd7:
697 return &spi_block_erase_d7;
698 case 0xd8:
699 return &spi_block_erase_d8;
700 default:
701 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
702 "this at flashrom@flashrom.org\n", __func__, opcode);
703 return NULL;
704 }
705}
706
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000707int spi_byte_program(struct flashctx *flash, unsigned int addr,
708 uint8_t databyte)
Sean Nelson14ba6682010-02-26 05:48:29 +0000709{
710 int result;
711 struct spi_command cmds[] = {
712 {
713 .writecnt = JEDEC_WREN_OUTSIZE,
714 .writearr = (const unsigned char[]){ JEDEC_WREN },
715 .readcnt = 0,
716 .readarr = NULL,
717 }, {
718 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
719 .writearr = (const unsigned char[]){
720 JEDEC_BYTE_PROGRAM,
721 (addr >> 16) & 0xff,
722 (addr >> 8) & 0xff,
723 (addr & 0xff),
724 databyte
725 },
726 .readcnt = 0,
727 .readarr = NULL,
728 }, {
729 .writecnt = 0,
730 .writearr = NULL,
731 .readcnt = 0,
732 .readarr = NULL,
733 }};
734
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000735 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000736 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000737 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000738 __func__, addr);
739 }
740 return result;
741}
742
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000743int spi_nbyte_program(struct flashctx *flash, unsigned int addr, uint8_t *bytes,
744 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000745{
746 int result;
747 /* FIXME: Switch to malloc based on len unless that kills speed. */
748 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
749 JEDEC_BYTE_PROGRAM,
750 (addr >> 16) & 0xff,
751 (addr >> 8) & 0xff,
752 (addr >> 0) & 0xff,
753 };
754 struct spi_command cmds[] = {
755 {
756 .writecnt = JEDEC_WREN_OUTSIZE,
757 .writearr = (const unsigned char[]){ JEDEC_WREN },
758 .readcnt = 0,
759 .readarr = NULL,
760 }, {
761 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
762 .writearr = cmd,
763 .readcnt = 0,
764 .readarr = NULL,
765 }, {
766 .writecnt = 0,
767 .writearr = NULL,
768 .readcnt = 0,
769 .readarr = NULL,
770 }};
771
772 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000773 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000774 return 1;
775 }
776 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000777 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000778 return 1;
779 }
780
781 memcpy(&cmd[4], bytes, len);
782
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000783 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000784 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000785 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000786 __func__, addr);
787 }
788 return result;
789}
790
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000791int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
792 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000793{
794 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
795 JEDEC_READ,
796 (address >> 16) & 0xff,
797 (address >> 8) & 0xff,
798 (address >> 0) & 0xff,
799 };
800
801 /* Send Read */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000802 return spi_send_command(flash, sizeof(cmd), len, cmd, bytes);
Sean Nelson14ba6682010-02-26 05:48:29 +0000803}
804
805/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000806 * Read a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000807 * FIXME: Use the chunk code from Michael Karcher instead.
Sean Nelson14ba6682010-02-26 05:48:29 +0000808 * Each page is read separately in chunks with a maximum size of chunksize.
809 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000810int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
811 unsigned int len, unsigned int chunksize)
Sean Nelson14ba6682010-02-26 05:48:29 +0000812{
813 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000814 unsigned int i, j, starthere, lenhere, toread;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000815 unsigned int page_size = flash->chip->page_size;
Sean Nelson14ba6682010-02-26 05:48:29 +0000816
817 /* Warning: This loop has a very unusual condition and body.
818 * The loop needs to go through each page with at least one affected
819 * byte. The lowest page number is (start / page_size) since that
820 * division rounds down. The highest page number we want is the page
821 * where the last byte of the range lives. That last byte has the
822 * address (start + len - 1), thus the highest page number is
823 * (start + len - 1) / page_size. Since we want to include that last
824 * page as well, the loop condition uses <=.
825 */
826 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
827 /* Byte position of the first byte in the range in this page. */
828 /* starthere is an offset to the base address of the chip. */
829 starthere = max(start, i * page_size);
830 /* Length of bytes in the range in this page. */
831 lenhere = min(start + len, (i + 1) * page_size) - starthere;
832 for (j = 0; j < lenhere; j += chunksize) {
833 toread = min(chunksize, lenhere - j);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000834 rc = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread);
Sean Nelson14ba6682010-02-26 05:48:29 +0000835 if (rc)
836 break;
837 }
838 if (rc)
839 break;
840 }
841
842 return rc;
843}
844
845/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000846 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000847 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000848 * Each page is written separately in chunks with a maximum size of chunksize.
849 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000850int spi_write_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
851 unsigned int len, unsigned int chunksize)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000852{
853 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000854 unsigned int i, j, starthere, lenhere, towrite;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000855 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000856 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000857 * spi_chip_write_256 have page_size set to max_writechunk_size, so
858 * we're OK for now.
859 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000860 unsigned int page_size = flash->chip->page_size;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000861
862 /* Warning: This loop has a very unusual condition and body.
863 * The loop needs to go through each page with at least one affected
864 * byte. The lowest page number is (start / page_size) since that
865 * division rounds down. The highest page number we want is the page
866 * where the last byte of the range lives. That last byte has the
867 * address (start + len - 1), thus the highest page number is
868 * (start + len - 1) / page_size. Since we want to include that last
869 * page as well, the loop condition uses <=.
870 */
871 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
872 /* Byte position of the first byte in the range in this page. */
873 /* starthere is an offset to the base address of the chip. */
874 starthere = max(start, i * page_size);
875 /* Length of bytes in the range in this page. */
876 lenhere = min(start + len, (i + 1) * page_size) - starthere;
877 for (j = 0; j < lenhere; j += chunksize) {
878 towrite = min(chunksize, lenhere - j);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000879 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000880 if (rc)
881 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000882 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000883 programmer_delay(10);
884 }
885 if (rc)
886 break;
887 }
888
889 return rc;
890}
891
892/*
Sean Nelson14ba6682010-02-26 05:48:29 +0000893 * Program chip using byte programming. (SLOW!)
894 * This is for chips which can only handle one byte writes
895 * and for chips where memory mapped programming is impossible
896 * (e.g. due to size constraints in IT87* for over 512 kB)
897 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000898/* real chunksize is 1, logical chunksize is 1 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000899int spi_chip_write_1(struct flashctx *flash, uint8_t *buf, unsigned int start,
900 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000901{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000902 unsigned int i;
903 int result = 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000904
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000905 for (i = start; i < start + len; i++) {
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000906 result = spi_byte_program(flash, i, buf[i - start]);
Sean Nelson14ba6682010-02-26 05:48:29 +0000907 if (result)
908 return 1;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000909 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000910 programmer_delay(10);
911 }
912
913 return 0;
914}
915
Nico Huber7bca1262012-06-15 22:28:12 +0000916int default_spi_write_aai(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000917{
918 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +0000919 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000920 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
921 JEDEC_AAI_WORD_PROGRAM,
922 };
923 struct spi_command cmds[] = {
924 {
925 .writecnt = JEDEC_WREN_OUTSIZE,
926 .writearr = (const unsigned char[]){ JEDEC_WREN },
927 .readcnt = 0,
928 .readarr = NULL,
929 }, {
930 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
931 .writearr = (const unsigned char[]){
932 JEDEC_AAI_WORD_PROGRAM,
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000933 (start >> 16) & 0xff,
934 (start >> 8) & 0xff,
935 (start & 0xff),
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000936 buf[0],
937 buf[1]
938 },
939 .readcnt = 0,
940 .readarr = NULL,
941 }, {
942 .writecnt = 0,
943 .writearr = NULL,
944 .readcnt = 0,
945 .readarr = NULL,
946 }};
Sean Nelson14ba6682010-02-26 05:48:29 +0000947
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000948 switch (flash->pgm->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000949#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000950#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000951 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000952 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000953 msg_perr("%s: impossible with this SPI controller,"
Sean Nelson14ba6682010-02-26 05:48:29 +0000954 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000955 return spi_chip_write_1(flash, buf, start, len);
Sean Nelson14ba6682010-02-26 05:48:29 +0000956#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000957#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000958 default:
959 break;
960 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000961
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000962 /* The even start address and even length requirements can be either
963 * honored outside this function, or we can call spi_byte_program
964 * for the first and/or last byte and use AAI for the rest.
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000965 * FIXME: Move this to generic code.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000966 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000967 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000968 if (start % 2) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000969 msg_cerr("%s: start address not even! Please report a bug at "
970 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000971 if (spi_chip_write_1(flash, buf, start, start % 2))
972 return SPI_GENERIC_ERROR;
973 pos += start % 2;
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +0000974 cmds[1].writearr = (const unsigned char[]){
975 JEDEC_AAI_WORD_PROGRAM,
976 (pos >> 16) & 0xff,
977 (pos >> 8) & 0xff,
978 (pos & 0xff),
979 buf[pos - start],
980 buf[pos - start + 1]
981 };
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000982 /* Do not return an error for now. */
983 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000984 }
985 /* The data sheet requires total AAI write length to be even. */
986 if (len % 2) {
987 msg_cerr("%s: total write length not even! Please report a "
988 "bug at flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000989 /* Do not return an error for now. */
990 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000991 }
992
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000993
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000994 result = spi_send_multicommand(flash, cmds);
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000995 if (result) {
996 msg_cerr("%s failed during start command execution\n",
997 __func__);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000998 /* FIXME: Should we send WRDI here as well to make sure the chip
999 * is not in AAI mode?
1000 */
Sean Nelson14ba6682010-02-26 05:48:29 +00001001 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +00001002 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001003 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001004 programmer_delay(10);
1005
1006 /* We already wrote 2 bytes in the multicommand step. */
1007 pos += 2;
1008
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001009 /* Are there at least two more bytes to write? */
1010 while (pos < start + len - 1) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001011 cmd[1] = buf[pos++ - start];
1012 cmd[2] = buf[pos++ - start];
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001013 spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0,
1014 cmd, NULL);
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001015 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001016 programmer_delay(10);
1017 }
1018
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001019 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
1020 * other non-AAI command.
1021 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001022 spi_write_disable(flash);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001023
1024 /* Write remaining byte (if any). */
1025 if (pos < start + len) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001026 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001027 return SPI_GENERIC_ERROR;
1028 pos += pos % 2;
1029 }
1030
Sean Nelson14ba6682010-02-26 05:48:29 +00001031 return 0;
1032}