blob: 30b862f1774283fa1b116e28cafef7167f8a2ea9 [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"
Boris Baykov99127182016-06-11 18:29:00 +020031#include "spi4ba.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000032
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000033static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000034{
Mathias Krausea60faab2011-01-17 07:50:42 +000035 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Sean Nelson14ba6682010-02-26 05:48:29 +000036 int ret;
37 int i;
38
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000039 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000040 if (ret)
41 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000042 msg_cspew("RDID returned");
Sean Nelson14ba6682010-02-26 05:48:29 +000043 for (i = 0; i < bytes; i++)
Sean Nelsoned479d22010-03-24 23:14:32 +000044 msg_cspew(" 0x%02x", readarr[i]);
45 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000046 return 0;
47}
48
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000049static int spi_rems(struct flashctx *flash, unsigned char *readarr)
Sean Nelson14ba6682010-02-26 05:48:29 +000050{
51 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
52 uint32_t readaddr;
53 int ret;
54
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000055 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd,
56 readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000057 if (ret == SPI_INVALID_ADDRESS) {
58 /* Find the lowest even address allowed for reads. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000059 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
Sean Nelson14ba6682010-02-26 05:48:29 +000060 cmd[1] = (readaddr >> 16) & 0xff,
61 cmd[2] = (readaddr >> 8) & 0xff,
62 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000063 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE,
64 cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000065 }
66 if (ret)
67 return ret;
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +000068 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
Sean Nelson14ba6682010-02-26 05:48:29 +000069 return 0;
70}
71
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000072static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000073{
74 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
75 uint32_t readaddr;
76 int ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000077 int i;
Sean Nelson14ba6682010-02-26 05:48:29 +000078
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000079 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000080 if (ret == SPI_INVALID_ADDRESS) {
81 /* Find the lowest even address allowed for reads. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000082 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
Sean Nelson14ba6682010-02-26 05:48:29 +000083 cmd[1] = (readaddr >> 16) & 0xff,
84 cmd[2] = (readaddr >> 8) & 0xff,
85 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000086 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000087 }
88 if (ret)
89 return ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000090 msg_cspew("RES returned");
91 for (i = 0; i < bytes; i++)
92 msg_cspew(" 0x%02x", readarr[i]);
93 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000094 return 0;
95}
96
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000097int spi_write_enable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +000098{
Mathias Krausea60faab2011-01-17 07:50:42 +000099 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Sean Nelson14ba6682010-02-26 05:48:29 +0000100 int result;
101
102 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000103 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000104
105 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000106 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000107
108 return result;
109}
110
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000111int spi_write_disable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000112{
Mathias Krausea60faab2011-01-17 07:50:42 +0000113 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Sean Nelson14ba6682010-02-26 05:48:29 +0000114
115 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000116 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000117}
118
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000119static int probe_spi_rdid_generic(struct flashctx *flash, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +0000120{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000121 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000122 unsigned char readarr[4];
123 uint32_t id1;
124 uint32_t id2;
125
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000126 if (spi_rdid(flash, readarr, bytes)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000127 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000128 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000129
130 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000131 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000132
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000133 /* Check if this is a continuation vendor ID.
134 * FIXME: Handle continuation device IDs.
135 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000136 if (readarr[0] == 0x7f) {
137 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000138 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000139 id1 = (readarr[0] << 8) | readarr[1];
140 id2 = readarr[2];
141 if (bytes > 3) {
142 id2 <<= 8;
143 id2 |= readarr[3];
144 }
145 } else {
146 id1 = readarr[0];
147 id2 = (readarr[1] << 8) | readarr[2];
148 }
149
Sean Nelsoned479d22010-03-24 23:14:32 +0000150 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000151
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000152 if (id1 == chip->manufacture_id && id2 == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000153 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000154
155 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000156 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000157 return 1;
158
159 /* Test if there is any vendor ID. */
Urja Rannikko0a5f6e42015-06-22 23:59:15 +0000160 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
Sean Nelson14ba6682010-02-26 05:48:29 +0000161 return 1;
162
163 return 0;
164}
165
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000166int probe_spi_rdid(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000167{
168 return probe_spi_rdid_generic(flash, 3);
169}
170
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000171int probe_spi_rdid4(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000172{
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000173 /* Some SPI controllers do not support commands with writecnt=1 and
174 * readcnt=4.
175 */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000176 switch (flash->mst->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000177#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000178#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000179 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000180 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000181 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
182 return 0;
183 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000184#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000185#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000186 default:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000187 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000188 }
189
190 return 0;
191}
192
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000193int probe_spi_rems(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000194{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000195 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000196 unsigned char readarr[JEDEC_REMS_INSIZE];
197 uint32_t id1, id2;
198
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000199 if (spi_rems(flash, readarr)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000200 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000201 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000202
203 id1 = readarr[0];
204 id2 = readarr[1];
205
Sean Nelsoned479d22010-03-24 23:14:32 +0000206 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000207
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000208 if (id1 == chip->manufacture_id && id2 == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000209 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000210
211 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000212 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000213 return 1;
214
215 /* Test if there is any vendor ID. */
Urja Rannikko0a5f6e42015-06-22 23:59:15 +0000216 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
Sean Nelson14ba6682010-02-26 05:48:29 +0000217 return 1;
218
219 return 0;
220}
221
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000222int probe_spi_res1(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000223{
Mathias Krausea60faab2011-01-17 07:50:42 +0000224 static const unsigned char allff[] = {0xff, 0xff, 0xff};
225 static const unsigned char all00[] = {0x00, 0x00, 0x00};
Sean Nelson14ba6682010-02-26 05:48:29 +0000226 unsigned char readarr[3];
227 uint32_t id2;
Sean Nelson14ba6682010-02-26 05:48:29 +0000228
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000229 /* We only want one-byte RES if RDID and REMS are unusable. */
230
Sean Nelson14ba6682010-02-26 05:48:29 +0000231 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
232 * 0x00 0x00 0x00. In that case, RES is pointless.
233 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000234 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000235 memcmp(readarr, all00, 3)) {
236 msg_cdbg("Ignoring RES in favour of RDID.\n");
237 return 0;
238 }
239 /* Check if REMS is usable and does not return 0xff 0xff or
240 * 0x00 0x00. In that case, RES is pointless.
241 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000242 if (!spi_rems(flash, readarr) &&
243 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000244 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
245 msg_cdbg("Ignoring RES in favour of REMS.\n");
246 return 0;
247 }
248
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000249 if (spi_res(flash, readarr, 1)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000250 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000251 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000252
Sean Nelson14ba6682010-02-26 05:48:29 +0000253 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000254
Sean Nelsoned479d22010-03-24 23:14:32 +0000255 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000256
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000257 if (id2 != flash->chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000258 return 0;
259
Sean Nelson14ba6682010-02-26 05:48:29 +0000260 return 1;
261}
262
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000263int probe_spi_res2(struct flashctx *flash)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000264{
265 unsigned char readarr[2];
266 uint32_t id1, id2;
267
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000268 if (spi_res(flash, readarr, 2)) {
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000269 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000270 }
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000271
272 id1 = readarr[0];
273 id2 = readarr[1];
274
275 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
276
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000277 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000278 return 0;
279
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000280 return 1;
281}
282
Stefan Tauner3f5e35d2013-04-19 01:58:33 +0000283int probe_spi_res3(struct flashctx *flash)
284{
285 unsigned char readarr[3];
286 uint32_t id1, id2;
287
288 if (spi_res(flash, readarr, 3)) {
289 return 0;
290 }
291
292 id1 = (readarr[0] << 8) | readarr[1];
293 id2 = readarr[2];
294
295 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
296
297 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
298 return 0;
299
300 return 1;
301}
302
Stefan Tauner57794ac2012-12-29 15:04:20 +0000303/* Only used for some Atmel chips. */
304int probe_spi_at25f(struct flashctx *flash)
305{
306 static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID };
307 unsigned char readarr[AT25F_RDID_INSIZE];
308 uint32_t id1;
309 uint32_t id2;
310
311 if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr))
312 return 0;
313
314 id1 = readarr[0];
315 id2 = readarr[1];
316
317 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
318
319 if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id)
320 return 1;
321
322 return 0;
323}
324
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000325int spi_chip_erase_60(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000326{
327 int result;
328 struct spi_command cmds[] = {
329 {
330 .writecnt = JEDEC_WREN_OUTSIZE,
331 .writearr = (const unsigned char[]){ JEDEC_WREN },
332 .readcnt = 0,
333 .readarr = NULL,
334 }, {
335 .writecnt = JEDEC_CE_60_OUTSIZE,
336 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
337 .readcnt = 0,
338 .readarr = NULL,
339 }, {
340 .writecnt = 0,
341 .writearr = NULL,
342 .readcnt = 0,
343 .readarr = NULL,
344 }};
345
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000346 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000347 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000348 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000349 __func__);
350 return result;
351 }
352 /* Wait until the Write-In-Progress bit is cleared.
353 * This usually takes 1-85 s, so wait in 1 s steps.
354 */
355 /* FIXME: We assume spi_read_status_register will never fail. */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000356 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000357 programmer_delay(1000 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000358 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000359 return 0;
360}
361
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000362int spi_chip_erase_62(struct flashctx *flash)
363{
364 int result;
365 struct spi_command cmds[] = {
366 {
367 .writecnt = JEDEC_WREN_OUTSIZE,
368 .writearr = (const unsigned char[]){ JEDEC_WREN },
369 .readcnt = 0,
370 .readarr = NULL,
371 }, {
372 .writecnt = JEDEC_CE_62_OUTSIZE,
373 .writearr = (const unsigned char[]){ JEDEC_CE_62 },
374 .readcnt = 0,
375 .readarr = NULL,
376 }, {
377 .writecnt = 0,
378 .writearr = NULL,
379 .readcnt = 0,
380 .readarr = NULL,
381 }};
382
383 result = spi_send_multicommand(flash, cmds);
384 if (result) {
385 msg_cerr("%s failed during command execution\n",
386 __func__);
387 return result;
388 }
389 /* Wait until the Write-In-Progress bit is cleared.
390 * This usually takes 2-5 s, so wait in 100 ms steps.
391 */
392 /* FIXME: We assume spi_read_status_register will never fail. */
393 while (spi_read_status_register(flash) & SPI_SR_WIP)
394 programmer_delay(100 * 1000);
395 /* FIXME: Check the status register for errors. */
396 return 0;
397}
398
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000399int spi_chip_erase_c7(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000400{
401 int result;
402 struct spi_command cmds[] = {
403 {
404 .writecnt = JEDEC_WREN_OUTSIZE,
405 .writearr = (const unsigned char[]){ JEDEC_WREN },
406 .readcnt = 0,
407 .readarr = NULL,
408 }, {
409 .writecnt = JEDEC_CE_C7_OUTSIZE,
410 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
411 .readcnt = 0,
412 .readarr = NULL,
413 }, {
414 .writecnt = 0,
415 .writearr = NULL,
416 .readcnt = 0,
417 .readarr = NULL,
418 }};
419
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000420 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000421 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000422 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000423 return result;
424 }
425 /* Wait until the Write-In-Progress bit is cleared.
426 * This usually takes 1-85 s, so wait in 1 s steps.
427 */
428 /* FIXME: We assume spi_read_status_register will never fail. */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000429 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000430 programmer_delay(1000 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000431 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000432 return 0;
433}
434
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000435int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
436 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000437{
438 int result;
439 struct spi_command cmds[] = {
440 {
441 .writecnt = JEDEC_WREN_OUTSIZE,
442 .writearr = (const unsigned char[]){ JEDEC_WREN },
443 .readcnt = 0,
444 .readarr = NULL,
445 }, {
446 .writecnt = JEDEC_BE_52_OUTSIZE,
447 .writearr = (const unsigned char[]){
448 JEDEC_BE_52,
449 (addr >> 16) & 0xff,
450 (addr >> 8) & 0xff,
451 (addr & 0xff)
452 },
453 .readcnt = 0,
454 .readarr = NULL,
455 }, {
456 .writecnt = 0,
457 .writearr = NULL,
458 .readcnt = 0,
459 .readarr = NULL,
460 }};
461
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000462 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000463 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000464 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000465 __func__, addr);
466 return result;
467 }
468 /* Wait until the Write-In-Progress bit is cleared.
469 * This usually takes 100-4000 ms, so wait in 100 ms steps.
470 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000471 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000472 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000473 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000474 return 0;
475}
476
477/* Block size is usually
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000478 * 32M (one die) for Micron
479 */
480int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
481{
482 int result;
483 struct spi_command cmds[] = {
484 {
485 .writecnt = JEDEC_WREN_OUTSIZE,
486 .writearr = (const unsigned char[]){ JEDEC_WREN },
487 .readcnt = 0,
488 .readarr = NULL,
489 }, {
490 .writecnt = JEDEC_BE_C4_OUTSIZE,
491 .writearr = (const unsigned char[]){
492 JEDEC_BE_C4,
493 (addr >> 16) & 0xff,
494 (addr >> 8) & 0xff,
495 (addr & 0xff)
496 },
497 .readcnt = 0,
498 .readarr = NULL,
499 }, {
500 .writecnt = 0,
501 .writearr = NULL,
502 .readcnt = 0,
503 .readarr = NULL,
504 }};
505
506 result = spi_send_multicommand(flash, cmds);
507 if (result) {
508 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
509 return result;
510 }
511 /* Wait until the Write-In-Progress bit is cleared.
512 * This usually takes 240-480 s, so wait in 500 ms steps.
513 */
514 while (spi_read_status_register(flash) & SPI_SR_WIP)
515 programmer_delay(500 * 1000 * 1000);
516 /* FIXME: Check the status register for errors. */
517 return 0;
518}
519
520/* Block size is usually
Sean Nelson14ba6682010-02-26 05:48:29 +0000521 * 64k for Macronix
522 * 32k for SST
523 * 4-32k non-uniform for EON
524 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000525int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
526 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000527{
528 int result;
529 struct spi_command cmds[] = {
530 {
531 .writecnt = JEDEC_WREN_OUTSIZE,
532 .writearr = (const unsigned char[]){ JEDEC_WREN },
533 .readcnt = 0,
534 .readarr = NULL,
535 }, {
536 .writecnt = JEDEC_BE_D8_OUTSIZE,
537 .writearr = (const unsigned char[]){
538 JEDEC_BE_D8,
539 (addr >> 16) & 0xff,
540 (addr >> 8) & 0xff,
541 (addr & 0xff)
542 },
543 .readcnt = 0,
544 .readarr = NULL,
545 }, {
546 .writecnt = 0,
547 .writearr = NULL,
548 .readcnt = 0,
549 .readarr = NULL,
550 }};
551
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000552 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000553 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000554 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000555 __func__, addr);
556 return result;
557 }
558 /* Wait until the Write-In-Progress bit is cleared.
559 * This usually takes 100-4000 ms, so wait in 100 ms steps.
560 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000561 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000562 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000563 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000564 return 0;
565}
566
567/* Block size is usually
568 * 4k for PMC
569 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000570int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
571 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000572{
573 int result;
574 struct spi_command cmds[] = {
575 {
576 .writecnt = JEDEC_WREN_OUTSIZE,
577 .writearr = (const unsigned char[]){ JEDEC_WREN },
578 .readcnt = 0,
579 .readarr = NULL,
580 }, {
581 .writecnt = JEDEC_BE_D7_OUTSIZE,
582 .writearr = (const unsigned char[]){
583 JEDEC_BE_D7,
584 (addr >> 16) & 0xff,
585 (addr >> 8) & 0xff,
586 (addr & 0xff)
587 },
588 .readcnt = 0,
589 .readarr = NULL,
590 }, {
591 .writecnt = 0,
592 .writearr = NULL,
593 .readcnt = 0,
594 .readarr = NULL,
595 }};
596
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000597 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000598 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000599 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000600 __func__, addr);
601 return result;
602 }
603 /* Wait until the Write-In-Progress bit is cleared.
604 * This usually takes 100-4000 ms, so wait in 100 ms steps.
605 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000606 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000607 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000608 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000609 return 0;
610}
611
Nikolay Nikolaev579f1e02013-06-28 21:28:37 +0000612/* Page erase (usually 256B blocks) */
613int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
614{
615 int result;
616 struct spi_command cmds[] = {
617 {
618 .writecnt = JEDEC_WREN_OUTSIZE,
619 .writearr = (const unsigned char[]){ JEDEC_WREN },
620 .readcnt = 0,
621 .readarr = NULL,
622 }, {
623 .writecnt = JEDEC_PE_OUTSIZE,
624 .writearr = (const unsigned char[]){
625 JEDEC_PE,
626 (addr >> 16) & 0xff,
627 (addr >> 8) & 0xff,
628 (addr & 0xff)
629 },
630 .readcnt = 0,
631 .readarr = NULL,
632 }, {
633 .writecnt = 0,
634 .writearr = NULL,
635 .readcnt = 0,
636 .readarr = NULL,
637 } };
638
639 result = spi_send_multicommand(flash, cmds);
640 if (result) {
641 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
642 return result;
643 }
644
645 /* Wait until the Write-In-Progress bit is cleared.
646 * This takes up to 20 ms usually (on worn out devices up to the 0.5s range), so wait in 1 ms steps. */
647 while (spi_read_status_register(flash) & SPI_SR_WIP)
648 programmer_delay(1 * 1000);
649 /* FIXME: Check the status register for errors. */
650 return 0;
651}
652
Sean Nelson14ba6682010-02-26 05:48:29 +0000653/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000654int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
655 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000656{
657 int result;
658 struct spi_command cmds[] = {
659 {
660 .writecnt = JEDEC_WREN_OUTSIZE,
661 .writearr = (const unsigned char[]){ JEDEC_WREN },
662 .readcnt = 0,
663 .readarr = NULL,
664 }, {
665 .writecnt = JEDEC_SE_OUTSIZE,
666 .writearr = (const unsigned char[]){
667 JEDEC_SE,
668 (addr >> 16) & 0xff,
669 (addr >> 8) & 0xff,
670 (addr & 0xff)
671 },
672 .readcnt = 0,
673 .readarr = NULL,
674 }, {
675 .writecnt = 0,
676 .writearr = NULL,
677 .readcnt = 0,
678 .readarr = NULL,
679 }};
680
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000681 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000682 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000683 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000684 __func__, addr);
685 return result;
686 }
687 /* Wait until the Write-In-Progress bit is cleared.
688 * This usually takes 15-800 ms, so wait in 10 ms steps.
689 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000690 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000691 programmer_delay(10 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000692 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000693 return 0;
694}
695
Stefan Tauner94b39b42012-10-27 00:06:02 +0000696int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
697{
698 int result;
699 struct spi_command cmds[] = {
700 {
701/* .writecnt = JEDEC_WREN_OUTSIZE,
702 .writearr = (const unsigned char[]){ JEDEC_WREN },
703 .readcnt = 0,
704 .readarr = NULL,
705 }, { */
706 .writecnt = JEDEC_BE_50_OUTSIZE,
707 .writearr = (const unsigned char[]){
708 JEDEC_BE_50,
709 (addr >> 16) & 0xff,
710 (addr >> 8) & 0xff,
711 (addr & 0xff)
712 },
713 .readcnt = 0,
714 .readarr = NULL,
715 }, {
716 .writecnt = 0,
717 .writearr = NULL,
718 .readcnt = 0,
719 .readarr = NULL,
720 }};
721
722 result = spi_send_multicommand(flash, cmds);
723 if (result) {
724 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
725 return result;
726 }
727 /* Wait until the Write-In-Progress bit is cleared.
728 * This usually takes 10 ms, so wait in 1 ms steps.
729 */
730 while (spi_read_status_register(flash) & SPI_SR_WIP)
731 programmer_delay(1 * 1000);
732 /* FIXME: Check the status register for errors. */
733 return 0;
734}
735
736int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
737{
738 int result;
739 struct spi_command cmds[] = {
740 {
741/* .writecnt = JEDEC_WREN_OUTSIZE,
742 .writearr = (const unsigned char[]){ JEDEC_WREN },
743 .readcnt = 0,
744 .readarr = NULL,
745 }, { */
746 .writecnt = JEDEC_BE_81_OUTSIZE,
747 .writearr = (const unsigned char[]){
748 JEDEC_BE_81,
749 (addr >> 16) & 0xff,
750 (addr >> 8) & 0xff,
751 (addr & 0xff)
752 },
753 .readcnt = 0,
754 .readarr = NULL,
755 }, {
756 .writecnt = 0,
757 .writearr = NULL,
758 .readcnt = 0,
759 .readarr = NULL,
760 }};
761
762 result = spi_send_multicommand(flash, cmds);
763 if (result) {
764 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
765 return result;
766 }
767 /* Wait until the Write-In-Progress bit is cleared.
768 * This usually takes 8 ms, so wait in 1 ms steps.
769 */
770 while (spi_read_status_register(flash) & SPI_SR_WIP)
771 programmer_delay(1 * 1000);
772 /* FIXME: Check the status register for errors. */
773 return 0;
774}
775
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000776int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
777 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000778{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000779 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000780 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000781 __func__);
782 return -1;
783 }
784 return spi_chip_erase_60(flash);
785}
786
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000787int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
788{
789 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
790 msg_cerr("%s called with incorrect arguments\n",
791 __func__);
792 return -1;
793 }
794 return spi_chip_erase_62(flash);
795}
796
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000797int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
798 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000799{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000800 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000801 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000802 __func__);
803 return -1;
804 }
805 return spi_chip_erase_c7(flash);
806}
807
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000808erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode)
809{
810 switch(opcode){
811 case 0xff:
812 case 0x00:
813 /* Not specified, assuming "not supported". */
814 return NULL;
815 case 0x20:
816 return &spi_block_erase_20;
Stefan Tauner730e7e72013-05-01 14:04:19 +0000817 case 0x50:
818 return &spi_block_erase_50;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000819 case 0x52:
820 return &spi_block_erase_52;
821 case 0x60:
822 return &spi_block_erase_60;
Stefan Tauner730e7e72013-05-01 14:04:19 +0000823 case 0x62:
824 return &spi_block_erase_62;
825 case 0x81:
826 return &spi_block_erase_81;
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000827 case 0xc4:
828 return &spi_block_erase_c4;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000829 case 0xc7:
830 return &spi_block_erase_c7;
831 case 0xd7:
832 return &spi_block_erase_d7;
833 case 0xd8:
834 return &spi_block_erase_d8;
Nikolay Nikolaev579f1e02013-06-28 21:28:37 +0000835 case 0xdb:
836 return &spi_block_erase_db;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000837 default:
838 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
839 "this at flashrom@flashrom.org\n", __func__, opcode);
840 return NULL;
841 }
842}
843
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000844int spi_byte_program(struct flashctx *flash, unsigned int addr,
845 uint8_t databyte)
Sean Nelson14ba6682010-02-26 05:48:29 +0000846{
847 int result;
848 struct spi_command cmds[] = {
849 {
850 .writecnt = JEDEC_WREN_OUTSIZE,
851 .writearr = (const unsigned char[]){ JEDEC_WREN },
852 .readcnt = 0,
853 .readarr = NULL,
854 }, {
855 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
856 .writearr = (const unsigned char[]){
857 JEDEC_BYTE_PROGRAM,
858 (addr >> 16) & 0xff,
859 (addr >> 8) & 0xff,
860 (addr & 0xff),
861 databyte
862 },
863 .readcnt = 0,
864 .readarr = NULL,
865 }, {
866 .writecnt = 0,
867 .writearr = NULL,
868 .readcnt = 0,
869 .readarr = NULL,
870 }};
871
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000872 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000873 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000874 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000875 __func__, addr);
876 }
877 return result;
878}
879
Mark Marshallf20b7be2014-05-09 21:16:21 +0000880int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000881{
882 int result;
883 /* FIXME: Switch to malloc based on len unless that kills speed. */
884 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
885 JEDEC_BYTE_PROGRAM,
886 (addr >> 16) & 0xff,
887 (addr >> 8) & 0xff,
888 (addr >> 0) & 0xff,
889 };
890 struct spi_command cmds[] = {
891 {
892 .writecnt = JEDEC_WREN_OUTSIZE,
893 .writearr = (const unsigned char[]){ JEDEC_WREN },
894 .readcnt = 0,
895 .readarr = NULL,
896 }, {
897 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
898 .writearr = cmd,
899 .readcnt = 0,
900 .readarr = NULL,
901 }, {
902 .writecnt = 0,
903 .writearr = NULL,
904 .readcnt = 0,
905 .readarr = NULL,
906 }};
907
908 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000909 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000910 return 1;
911 }
912 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000913 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000914 return 1;
915 }
916
917 memcpy(&cmd[4], bytes, len);
918
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000919 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000920 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000921 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000922 __func__, addr);
923 }
924 return result;
925}
926
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000927int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
928 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000929{
930 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
931 JEDEC_READ,
932 (address >> 16) & 0xff,
933 (address >> 8) & 0xff,
934 (address >> 0) & 0xff,
935 };
936
937 /* Send Read */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000938 return spi_send_command(flash, sizeof(cmd), len, cmd, bytes);
Sean Nelson14ba6682010-02-26 05:48:29 +0000939}
940
941/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000942 * Read a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000943 * FIXME: Use the chunk code from Michael Karcher instead.
Urja Rannikko731316a2017-06-15 13:32:01 +0300944 * Each naturally aligned area is read separately in chunks with a maximum size of chunksize.
Sean Nelson14ba6682010-02-26 05:48:29 +0000945 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000946int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
947 unsigned int len, unsigned int chunksize)
Sean Nelson14ba6682010-02-26 05:48:29 +0000948{
949 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000950 unsigned int i, j, starthere, lenhere, toread;
Urja Rannikko731316a2017-06-15 13:32:01 +0300951 /* Limit for multi-die 4-byte-addressing chips. */
952 unsigned int area_size = min(flash->chip->total_size * 1024, 16 * 1024 * 1024);
Sean Nelson14ba6682010-02-26 05:48:29 +0000953
954 /* Warning: This loop has a very unusual condition and body.
Urja Rannikko731316a2017-06-15 13:32:01 +0300955 * The loop needs to go through each area with at least one affected
956 * byte. The lowest area number is (start / area_size) since that
957 * division rounds down. The highest area number we want is the area
Sean Nelson14ba6682010-02-26 05:48:29 +0000958 * where the last byte of the range lives. That last byte has the
Urja Rannikko731316a2017-06-15 13:32:01 +0300959 * address (start + len - 1), thus the highest area number is
960 * (start + len - 1) / area_size. Since we want to include that last
961 * area as well, the loop condition uses <=.
Sean Nelson14ba6682010-02-26 05:48:29 +0000962 */
Urja Rannikko731316a2017-06-15 13:32:01 +0300963 for (i = start / area_size; i <= (start + len - 1) / area_size; i++) {
964 /* Byte position of the first byte in the range in this area. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000965 /* starthere is an offset to the base address of the chip. */
Urja Rannikko731316a2017-06-15 13:32:01 +0300966 starthere = max(start, i * area_size);
967 /* Length of bytes in the range in this area. */
968 lenhere = min(start + len, (i + 1) * area_size) - starthere;
Sean Nelson14ba6682010-02-26 05:48:29 +0000969 for (j = 0; j < lenhere; j += chunksize) {
970 toread = min(chunksize, lenhere - j);
Boris Baykov99127182016-06-11 18:29:00 +0200971 rc = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
972 ? spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread)
973 : flash->chip->four_bytes_addr_funcs.read_nbyte(flash, starthere + j,
974 buf + starthere - start + j, toread);
Sean Nelson14ba6682010-02-26 05:48:29 +0000975 if (rc)
976 break;
977 }
978 if (rc)
979 break;
980 }
981
982 return rc;
983}
984
985/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000986 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000987 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000988 * Each page is written separately in chunks with a maximum size of chunksize.
989 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000990int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000991 unsigned int len, unsigned int chunksize)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000992{
993 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000994 unsigned int i, j, starthere, lenhere, towrite;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000995 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000996 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000997 * spi_chip_write_256 have page_size set to max_writechunk_size, so
998 * we're OK for now.
999 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001000 unsigned int page_size = flash->chip->page_size;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001001
1002 /* Warning: This loop has a very unusual condition and body.
1003 * The loop needs to go through each page with at least one affected
1004 * byte. The lowest page number is (start / page_size) since that
1005 * division rounds down. The highest page number we want is the page
1006 * where the last byte of the range lives. That last byte has the
1007 * address (start + len - 1), thus the highest page number is
1008 * (start + len - 1) / page_size. Since we want to include that last
1009 * page as well, the loop condition uses <=.
1010 */
1011 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1012 /* Byte position of the first byte in the range in this page. */
1013 /* starthere is an offset to the base address of the chip. */
1014 starthere = max(start, i * page_size);
1015 /* Length of bytes in the range in this page. */
1016 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1017 for (j = 0; j < lenhere; j += chunksize) {
1018 towrite = min(chunksize, lenhere - j);
Boris Baykov99127182016-06-11 18:29:00 +02001019 rc = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
1020 ? spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite)
1021 : flash->chip->four_bytes_addr_funcs.program_nbyte(flash, starthere + j,
1022 buf + starthere - start + j, towrite);
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001023 if (rc)
1024 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001025 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001026 programmer_delay(10);
1027 }
1028 if (rc)
1029 break;
1030 }
1031
1032 return rc;
1033}
1034
1035/*
Sean Nelson14ba6682010-02-26 05:48:29 +00001036 * Program chip using byte programming. (SLOW!)
1037 * This is for chips which can only handle one byte writes
1038 * and for chips where memory mapped programming is impossible
1039 * (e.g. due to size constraints in IT87* for over 512 kB)
1040 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001041/* real chunksize is 1, logical chunksize is 1 */
Mark Marshallf20b7be2014-05-09 21:16:21 +00001042int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +00001043{
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001044 unsigned int i;
1045 int result = 0;
Sean Nelson14ba6682010-02-26 05:48:29 +00001046
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001047 for (i = start; i < start + len; i++) {
Boris Baykov99127182016-06-11 18:29:00 +02001048 result = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
1049 ? spi_byte_program(flash, i, buf[i - start])
1050 : flash->chip->four_bytes_addr_funcs.program_byte(flash, i, buf[i - start]);
Sean Nelson14ba6682010-02-26 05:48:29 +00001051 if (result)
1052 return 1;
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001053 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +00001054 programmer_delay(10);
1055 }
1056
1057 return 0;
1058}
1059
Mark Marshallf20b7be2014-05-09 21:16:21 +00001060int default_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001061{
1062 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +00001063 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001064 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1065 JEDEC_AAI_WORD_PROGRAM,
1066 };
1067 struct spi_command cmds[] = {
1068 {
1069 .writecnt = JEDEC_WREN_OUTSIZE,
1070 .writearr = (const unsigned char[]){ JEDEC_WREN },
1071 .readcnt = 0,
1072 .readarr = NULL,
1073 }, {
1074 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1075 .writearr = (const unsigned char[]){
1076 JEDEC_AAI_WORD_PROGRAM,
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001077 (start >> 16) & 0xff,
1078 (start >> 8) & 0xff,
1079 (start & 0xff),
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001080 buf[0],
1081 buf[1]
1082 },
1083 .readcnt = 0,
1084 .readarr = NULL,
1085 }, {
1086 .writecnt = 0,
1087 .writearr = NULL,
1088 .readcnt = 0,
1089 .readarr = NULL,
1090 }};
Sean Nelson14ba6682010-02-26 05:48:29 +00001091
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001092 switch (flash->mst->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001093#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001094#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001095 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +00001096 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001097 msg_perr("%s: impossible with this SPI controller,"
Sean Nelson14ba6682010-02-26 05:48:29 +00001098 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001099 return spi_chip_write_1(flash, buf, start, len);
Sean Nelson14ba6682010-02-26 05:48:29 +00001100#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001101#endif
Sean Nelson14ba6682010-02-26 05:48:29 +00001102 default:
1103 break;
1104 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001105
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001106 /* The even start address and even length requirements can be either
1107 * honored outside this function, or we can call spi_byte_program
1108 * for the first and/or last byte and use AAI for the rest.
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001109 * FIXME: Move this to generic code.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001110 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001111 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001112 if (start % 2) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001113 msg_cerr("%s: start address not even! Please report a bug at "
1114 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001115 if (spi_chip_write_1(flash, buf, start, start % 2))
1116 return SPI_GENERIC_ERROR;
1117 pos += start % 2;
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001118 cmds[1].writearr = (const unsigned char[]){
1119 JEDEC_AAI_WORD_PROGRAM,
1120 (pos >> 16) & 0xff,
1121 (pos >> 8) & 0xff,
1122 (pos & 0xff),
1123 buf[pos - start],
1124 buf[pos - start + 1]
1125 };
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001126 /* Do not return an error for now. */
1127 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001128 }
1129 /* The data sheet requires total AAI write length to be even. */
1130 if (len % 2) {
1131 msg_cerr("%s: total write length not even! Please report a "
1132 "bug at flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001133 /* Do not return an error for now. */
1134 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001135 }
1136
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001137
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001138 result = spi_send_multicommand(flash, cmds);
Stefan Reinauer87ace662014-04-26 16:12:55 +00001139 if (result != 0) {
1140 msg_cerr("%s failed during start command execution: %d\n", __func__, result);
1141 goto bailout;
Sean Nelson14ba6682010-02-26 05:48:29 +00001142 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001143 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001144 programmer_delay(10);
1145
1146 /* We already wrote 2 bytes in the multicommand step. */
1147 pos += 2;
1148
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001149 /* Are there at least two more bytes to write? */
1150 while (pos < start + len - 1) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001151 cmd[1] = buf[pos++ - start];
1152 cmd[2] = buf[pos++ - start];
Stefan Reinauer87ace662014-04-26 16:12:55 +00001153 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
1154 if (result != 0) {
1155 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
1156 goto bailout;
1157 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001158 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001159 programmer_delay(10);
1160 }
1161
Stefan Tauner59c4d792014-04-26 16:13:09 +00001162 /* Use WRDI to exit AAI mode. This needs to be done before issuing any other non-AAI command. */
1163 result = spi_write_disable(flash);
1164 if (result != 0) {
1165 msg_cerr("%s failed to disable AAI mode.\n", __func__);
1166 return SPI_GENERIC_ERROR;
1167 }
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001168
1169 /* Write remaining byte (if any). */
1170 if (pos < start + len) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001171 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001172 return SPI_GENERIC_ERROR;
1173 pos += pos % 2;
1174 }
1175
Sean Nelson14ba6682010-02-26 05:48:29 +00001176 return 0;
Stefan Reinauer87ace662014-04-26 16:12:55 +00001177
1178bailout:
Stefan Tauner59c4d792014-04-26 16:13:09 +00001179 result = spi_write_disable(flash);
1180 if (result != 0)
1181 msg_cerr("%s failed to disable AAI mode.\n", __func__);
Stefan Reinauer87ace662014-04-26 16:12:55 +00001182 return SPI_GENERIC_ERROR;
Sean Nelson14ba6682010-02-26 05:48:29 +00001183}