blob: 1c0c85b0ac10a7feaf77732bdeb22e0698fc0353 [file] [log] [blame]
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00004 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +00008 * the Free Software Foundation; version 2 of the License.
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +00009 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000020#include <string.h>
21#include <stdlib.h>
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +000022#include <stdio.h>
23#include <ctype.h>
Stefan Tauner5e695ab2012-05-06 17:03:40 +000024#include <errno.h>
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000025#include "flash.h"
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +000026#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000028
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000029/* Remove the #define below if you don't want SPI flash chip emulation. */
30#define EMULATE_SPI_CHIP 1
31
32#if EMULATE_SPI_CHIP
33#define EMULATE_CHIP 1
34#include "spi.h"
35#endif
36
37#if EMULATE_CHIP
38#include <sys/types.h>
39#include <sys/stat.h>
40#endif
41
42#if EMULATE_CHIP
43static uint8_t *flashchip_contents = NULL;
44enum emu_chip {
45 EMULATE_NONE,
46 EMULATE_ST_M25P10_RES,
47 EMULATE_SST_SST25VF040_REMS,
48 EMULATE_SST_SST25VF032B,
Stefan Tauner0b9df972012-05-07 22:12:16 +000049 EMULATE_MACRONIX_MX25L6436,
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000050};
51static enum emu_chip emu_chip = EMULATE_NONE;
52static char *emu_persistent_image = NULL;
Stefan Taunerc69c9c82011-11-23 09:13:48 +000053static unsigned int emu_chip_size = 0;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000054#if EMULATE_SPI_CHIP
Stefan Taunerc69c9c82011-11-23 09:13:48 +000055static unsigned int emu_max_byteprogram_size = 0;
56static unsigned int emu_max_aai_size = 0;
57static unsigned int emu_jedec_se_size = 0;
58static unsigned int emu_jedec_be_52_size = 0;
59static unsigned int emu_jedec_be_d8_size = 0;
60static unsigned int emu_jedec_ce_60_size = 0;
61static unsigned int emu_jedec_ce_c7_size = 0;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +000062unsigned char spi_blacklist[256];
63unsigned char spi_ignorelist[256];
64int spi_blacklist_size = 0;
65int spi_ignorelist_size = 0;
Stefan Tauner5e695ab2012-05-06 17:03:40 +000066static uint8_t emu_status = 0;
Stefan Tauner0b9df972012-05-07 22:12:16 +000067
68/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000069static const uint8_t sfdp_table[] = {
Stefan Tauner0b9df972012-05-07 22:12:16 +000070 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature
71 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers
72 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long
73 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30)
74 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long
75 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60)
76 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole.
77 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start
78 0xFF, 0xFF, 0xFF, 0x03, // @0x20
79 0x00, 0xFF, 0x08, 0x6B, // @0x24
80 0x08, 0x3B, 0x00, 0xFF, // @0x28
81 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C
82 0xFF, 0xFF, 0x00, 0x00, // @0x30
83 0xFF, 0xFF, 0x00, 0xFF, // @0x34
84 0x0C, 0x20, 0x0F, 0x52, // @0x38
85 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end
86 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole.
87 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole.
88 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start
89 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C
90 0xD9, 0xC8, 0xFF, 0xFF, // @0x50
91 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end
92};
93
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000094#endif
95#endif
96
Stefan Taunerc69c9c82011-11-23 09:13:48 +000097static unsigned int spi_write_256_chunksize = 256;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000098
Mark Marshallf20b7be2014-05-09 21:16:21 +000099static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
100 const unsigned char *writearr, unsigned char *readarr);
101static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000102 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +0000103static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
104static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
105static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
106static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
107static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr);
108static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr);
109static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr);
110static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000111
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000112static const struct spi_master spi_master_dummyflasher = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000113 .type = SPI_CONTROLLER_DUMMY,
Nico Huber1cf407b2017-11-10 20:18:23 +0100114 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000115 .max_data_read = MAX_DATA_READ_UNLIMITED,
116 .max_data_write = MAX_DATA_UNSPECIFIED,
117 .command = dummy_spi_send_command,
118 .multicommand = default_spi_send_multicommand,
119 .read = default_spi_read,
120 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000121 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000122};
David Hendricks8bb20212011-06-14 01:35:36 +0000123
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000124static const struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000125 .chip_readb = dummy_chip_readb,
126 .chip_readw = dummy_chip_readw,
127 .chip_readl = dummy_chip_readl,
128 .chip_readn = dummy_chip_readn,
129 .chip_writeb = dummy_chip_writeb,
130 .chip_writew = dummy_chip_writew,
131 .chip_writel = dummy_chip_writel,
132 .chip_writen = dummy_chip_writen,
133};
134
135enum chipbustype dummy_buses_supported = BUS_NONE;
136
David Hendricks8bb20212011-06-14 01:35:36 +0000137static int dummy_shutdown(void *data)
138{
139 msg_pspew("%s\n", __func__);
140#if EMULATE_CHIP
141 if (emu_chip != EMULATE_NONE) {
142 if (emu_persistent_image) {
143 msg_pdbg("Writing %s\n", emu_persistent_image);
Stefan Taunere0ff1652012-09-22 22:56:09 +0000144 write_buf_to_file(flashchip_contents, emu_chip_size, emu_persistent_image);
145 free(emu_persistent_image);
146 emu_persistent_image = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000147 }
148 free(flashchip_contents);
149 }
150#endif
151 return 0;
152}
153
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000154int dummy_init(void)
155{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000156 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000157 char *tmp = NULL;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000158 int i;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000159#if EMULATE_SPI_CHIP
160 char *status = NULL;
161#endif
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000162#if EMULATE_CHIP
163 struct stat image_stat;
164#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000165
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000166 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000167
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000168 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000169 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
170 if (!bustext)
171 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000172 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000173 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000174
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000175 dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000176 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000177 dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000178 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000179 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000180 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000181 dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000182 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000183 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000184 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000185 dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000186 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000187 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000188 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000189 dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000190 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000191 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000192 if (dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000193 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000194 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000195
196 tmp = extract_programmer_param("spi_write_256_chunksize");
197 if (tmp) {
198 spi_write_256_chunksize = atoi(tmp);
199 free(tmp);
200 if (spi_write_256_chunksize < 1) {
201 msg_perr("invalid spi_write_256_chunksize\n");
202 return 1;
203 }
204 }
205
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000206 tmp = extract_programmer_param("spi_blacklist");
207 if (tmp) {
208 i = strlen(tmp);
209 if (!strncmp(tmp, "0x", 2)) {
210 i -= 2;
211 memmove(tmp, tmp + 2, i + 1);
212 }
213 if ((i > 512) || (i % 2)) {
214 msg_perr("Invalid SPI command blacklist length\n");
215 free(tmp);
216 return 1;
217 }
218 spi_blacklist_size = i / 2;
219 for (i = 0; i < spi_blacklist_size * 2; i++) {
220 if (!isxdigit((unsigned char)tmp[i])) {
221 msg_perr("Invalid char \"%c\" in SPI command "
222 "blacklist\n", tmp[i]);
223 free(tmp);
224 return 1;
225 }
226 }
227 for (i = 0; i < spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000228 unsigned int tmp2;
229 /* SCNx8 is apparently not supported by MSVC (and thus
230 * MinGW), so work around it with an extra variable
231 */
232 sscanf(tmp + i * 2, "%2x", &tmp2);
233 spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000234 }
235 msg_pdbg("SPI blacklist is ");
236 for (i = 0; i < spi_blacklist_size; i++)
237 msg_pdbg("%02x ", spi_blacklist[i]);
238 msg_pdbg(", size %i\n", spi_blacklist_size);
239 }
240 free(tmp);
241
242 tmp = extract_programmer_param("spi_ignorelist");
243 if (tmp) {
244 i = strlen(tmp);
245 if (!strncmp(tmp, "0x", 2)) {
246 i -= 2;
247 memmove(tmp, tmp + 2, i + 1);
248 }
249 if ((i > 512) || (i % 2)) {
250 msg_perr("Invalid SPI command ignorelist length\n");
251 free(tmp);
252 return 1;
253 }
254 spi_ignorelist_size = i / 2;
255 for (i = 0; i < spi_ignorelist_size * 2; i++) {
256 if (!isxdigit((unsigned char)tmp[i])) {
257 msg_perr("Invalid char \"%c\" in SPI command "
258 "ignorelist\n", tmp[i]);
259 free(tmp);
260 return 1;
261 }
262 }
263 for (i = 0; i < spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000264 unsigned int tmp2;
265 /* SCNx8 is apparently not supported by MSVC (and thus
266 * MinGW), so work around it with an extra variable
267 */
268 sscanf(tmp + i * 2, "%2x", &tmp2);
269 spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000270 }
271 msg_pdbg("SPI ignorelist is ");
272 for (i = 0; i < spi_ignorelist_size; i++)
273 msg_pdbg("%02x ", spi_ignorelist[i]);
274 msg_pdbg(", size %i\n", spi_ignorelist_size);
275 }
276 free(tmp);
277
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000278#if EMULATE_CHIP
279 tmp = extract_programmer_param("emulate");
280 if (!tmp) {
281 msg_pdbg("Not emulating any flash chip.\n");
282 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000283 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000284 }
285#if EMULATE_SPI_CHIP
286 if (!strcmp(tmp, "M25P10.RES")) {
287 emu_chip = EMULATE_ST_M25P10_RES;
288 emu_chip_size = 128 * 1024;
289 emu_max_byteprogram_size = 128;
290 emu_max_aai_size = 0;
291 emu_jedec_se_size = 0;
292 emu_jedec_be_52_size = 0;
293 emu_jedec_be_d8_size = 32 * 1024;
294 emu_jedec_ce_60_size = 0;
295 emu_jedec_ce_c7_size = emu_chip_size;
296 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
297 "write)\n");
298 }
299 if (!strcmp(tmp, "SST25VF040.REMS")) {
300 emu_chip = EMULATE_SST_SST25VF040_REMS;
301 emu_chip_size = 512 * 1024;
302 emu_max_byteprogram_size = 1;
303 emu_max_aai_size = 0;
304 emu_jedec_se_size = 4 * 1024;
305 emu_jedec_be_52_size = 32 * 1024;
306 emu_jedec_be_d8_size = 0;
307 emu_jedec_ce_60_size = emu_chip_size;
308 emu_jedec_ce_c7_size = 0;
309 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
310 "byte write)\n");
311 }
312 if (!strcmp(tmp, "SST25VF032B")) {
313 emu_chip = EMULATE_SST_SST25VF032B;
314 emu_chip_size = 4 * 1024 * 1024;
315 emu_max_byteprogram_size = 1;
316 emu_max_aai_size = 2;
317 emu_jedec_se_size = 4 * 1024;
318 emu_jedec_be_52_size = 32 * 1024;
319 emu_jedec_be_d8_size = 64 * 1024;
320 emu_jedec_ce_60_size = emu_chip_size;
321 emu_jedec_ce_c7_size = emu_chip_size;
322 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
323 "write)\n");
324 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000325 if (!strcmp(tmp, "MX25L6436")) {
326 emu_chip = EMULATE_MACRONIX_MX25L6436;
327 emu_chip_size = 8 * 1024 * 1024;
328 emu_max_byteprogram_size = 256;
329 emu_max_aai_size = 0;
330 emu_jedec_se_size = 4 * 1024;
331 emu_jedec_be_52_size = 32 * 1024;
332 emu_jedec_be_d8_size = 64 * 1024;
333 emu_jedec_ce_60_size = emu_chip_size;
334 emu_jedec_ce_c7_size = emu_chip_size;
335 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
336 "SFDP)\n");
337 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000338#endif
339 if (emu_chip == EMULATE_NONE) {
340 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
341 free(tmp);
342 return 1;
343 }
344 free(tmp);
345 flashchip_contents = malloc(emu_chip_size);
346 if (!flashchip_contents) {
347 msg_perr("Out of memory!\n");
348 return 1;
349 }
David Hendricks8bb20212011-06-14 01:35:36 +0000350
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000351#ifdef EMULATE_SPI_CHIP
352 status = extract_programmer_param("spi_status");
353 if (status) {
354 char *endptr;
355 errno = 0;
356 emu_status = strtoul(status, &endptr, 0);
357 free(status);
358 if (errno != 0 || status == endptr) {
359 msg_perr("Error: initial status register specified, "
360 "but the value could not be converted.\n");
361 return 1;
362 }
363 msg_pdbg("Initial status register is set to 0x%02x.\n",
364 emu_status);
365 }
366#endif
367
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000368 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
369 memset(flashchip_contents, 0xff, emu_chip_size);
370
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000371 /* Will be freed by shutdown function if necessary. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000372 emu_persistent_image = extract_programmer_param("image");
373 if (!emu_persistent_image) {
374 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000375 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000376 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000377 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
378 * not match the emulated chip. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000379 if (!stat(emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000380 msg_pdbg("Found persistent image %s, %jd B ",
381 emu_persistent_image, (intmax_t)image_stat.st_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000382 if (image_stat.st_size == emu_chip_size) {
383 msg_pdbg("matches.\n");
384 msg_pdbg("Reading %s\n", emu_persistent_image);
385 read_buf_from_file(flashchip_contents, emu_chip_size,
386 emu_persistent_image);
387 } else {
388 msg_pdbg("doesn't match.\n");
389 }
390 }
391#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000392
David Hendricks8bb20212011-06-14 01:35:36 +0000393dummy_init_out:
394 if (register_shutdown(dummy_shutdown, NULL)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000395 free(flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000396 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000397 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000398 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000399 register_par_master(&par_master_dummy,
400 dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH));
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000401 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000402 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000403
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000404 return 0;
405}
406
Stefan Tauner305e0b92013-07-17 23:46:44 +0000407void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000408{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000409 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000410 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000411 return (void *)phys_addr;
412}
413
414void dummy_unmap(void *virt_addr, size_t len)
415{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000416 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000417}
418
Mark Marshallf20b7be2014-05-09 21:16:21 +0000419static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000420{
Stefan Taunerc2333752013-07-13 23:31:37 +0000421 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000422}
423
Mark Marshallf20b7be2014-05-09 21:16:21 +0000424static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000425{
Stefan Taunerc2333752013-07-13 23:31:37 +0000426 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000427}
428
Mark Marshallf20b7be2014-05-09 21:16:21 +0000429static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000430{
Stefan Taunerc2333752013-07-13 23:31:37 +0000431 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000432}
433
Mark Marshallf20b7be2014-05-09 21:16:21 +0000434static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000435{
436 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000437 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000438 for (i = 0; i < len; i++) {
439 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000440 msg_pspew("\n");
441 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000442 }
443}
444
Mark Marshallf20b7be2014-05-09 21:16:21 +0000445static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000446{
Stefan Taunerc2333752013-07-13 23:31:37 +0000447 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000448 return 0xff;
449}
450
Mark Marshallf20b7be2014-05-09 21:16:21 +0000451static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000452{
Stefan Taunerc2333752013-07-13 23:31:37 +0000453 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000454 return 0xffff;
455}
456
Mark Marshallf20b7be2014-05-09 21:16:21 +0000457static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000458{
Stefan Taunerc2333752013-07-13 23:31:37 +0000459 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000460 return 0xffffffff;
461}
462
Mark Marshallf20b7be2014-05-09 21:16:21 +0000463static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000464{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000465 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, returning array of 0xff\n", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000466 memset(buf, 0xff, len);
467 return;
468}
469
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000470#if EMULATE_SPI_CHIP
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000471static int emulate_spi_chip_response(unsigned int writecnt,
472 unsigned int readcnt,
473 const unsigned char *writearr,
474 unsigned char *readarr)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000475{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000476 unsigned int offs, i, toread;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000477 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000478 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
479 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
480 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000481
482 if (writecnt == 0) {
483 msg_perr("No command sent to the chip!\n");
484 return 1;
485 }
Paul Menzelac427b22012-02-16 21:07:07 +0000486 /* spi_blacklist has precedence over spi_ignorelist. */
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000487 for (i = 0; i < spi_blacklist_size; i++) {
488 if (writearr[0] == spi_blacklist[i]) {
489 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
490 spi_blacklist[i]);
491 return SPI_INVALID_OPCODE;
492 }
493 }
494 for (i = 0; i < spi_ignorelist_size; i++) {
495 if (writearr[0] == spi_ignorelist[i]) {
496 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
497 spi_ignorelist[i]);
498 /* Return success because the command does not fail,
499 * it is simply ignored.
500 */
501 return 0;
502 }
503 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000504
505 if (emu_max_aai_size && (emu_status & SPI_SR_AAI)) {
506 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
507 writearr[0] != JEDEC_WRDI &&
508 writearr[0] != JEDEC_RDSR) {
509 msg_perr("Forbidden opcode (0x%02x) attempted during "
510 "AAI sequence!\n", writearr[0]);
511 return 0;
512 }
513 }
514
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000515 switch (writearr[0]) {
516 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000517 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000518 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000519 /* offs calculation is only needed for SST chips which treat RES like REMS. */
520 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
521 offs += writecnt - JEDEC_REMS_OUTSIZE;
522 switch (emu_chip) {
523 case EMULATE_ST_M25P10_RES:
524 if (readcnt > 0)
525 memset(readarr, 0x10, readcnt);
526 break;
527 case EMULATE_SST_SST25VF040_REMS:
528 for (i = 0; i < readcnt; i++)
529 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
530 break;
531 case EMULATE_SST_SST25VF032B:
532 for (i = 0; i < readcnt; i++)
533 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
534 break;
535 case EMULATE_MACRONIX_MX25L6436:
536 if (readcnt > 0)
537 memset(readarr, 0x16, readcnt);
538 break;
539 default: /* ignore */
540 break;
541 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000542 break;
543 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000544 /* REMS response has wraparound and uses an address parameter. */
545 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000546 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000547 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
548 offs += writecnt - JEDEC_REMS_OUTSIZE;
549 switch (emu_chip) {
550 case EMULATE_SST_SST25VF040_REMS:
551 for (i = 0; i < readcnt; i++)
552 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
553 break;
554 case EMULATE_SST_SST25VF032B:
555 for (i = 0; i < readcnt; i++)
556 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
557 break;
558 case EMULATE_MACRONIX_MX25L6436:
559 for (i = 0; i < readcnt; i++)
560 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
561 break;
562 default: /* ignore */
563 break;
564 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000565 break;
566 case JEDEC_RDID:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000567 switch (emu_chip) {
568 case EMULATE_SST_SST25VF032B:
569 if (readcnt > 0)
570 readarr[0] = 0xbf;
571 if (readcnt > 1)
572 readarr[1] = 0x25;
573 if (readcnt > 2)
574 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000575 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000576 case EMULATE_MACRONIX_MX25L6436:
577 if (readcnt > 0)
578 readarr[0] = 0xc2;
579 if (readcnt > 1)
580 readarr[1] = 0x20;
581 if (readcnt > 2)
582 readarr[2] = 0x17;
583 break;
584 default: /* ignore */
585 break;
586 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000587 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000588 case JEDEC_RDSR:
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000589 memset(readarr, emu_status, readcnt);
590 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000591 /* FIXME: this should be chip-specific. */
592 case JEDEC_EWSR:
593 case JEDEC_WREN:
594 emu_status |= SPI_SR_WEL;
595 break;
596 case JEDEC_WRSR:
597 if (!(emu_status & SPI_SR_WEL)) {
598 msg_perr("WRSR attempted, but WEL is 0!\n");
599 break;
600 }
601 /* FIXME: add some reasonable simulation of the busy flag */
602 emu_status = writearr[1] & ~SPI_SR_WIP;
603 msg_pdbg2("WRSR wrote 0x%02x.\n", emu_status);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000604 break;
605 case JEDEC_READ:
606 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
607 /* Truncate to emu_chip_size. */
608 offs %= emu_chip_size;
609 if (readcnt > 0)
610 memcpy(readarr, flashchip_contents + offs, readcnt);
611 break;
612 case JEDEC_BYTE_PROGRAM:
613 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
614 /* Truncate to emu_chip_size. */
615 offs %= emu_chip_size;
616 if (writecnt < 5) {
617 msg_perr("BYTE PROGRAM size too short!\n");
618 return 1;
619 }
620 if (writecnt - 4 > emu_max_byteprogram_size) {
621 msg_perr("Max BYTE PROGRAM size exceeded!\n");
622 return 1;
623 }
624 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
625 break;
626 case JEDEC_AAI_WORD_PROGRAM:
627 if (!emu_max_aai_size)
628 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000629 if (!(emu_status & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000630 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
631 msg_perr("Initial AAI WORD PROGRAM size too "
632 "short!\n");
633 return 1;
634 }
635 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
636 msg_perr("Initial AAI WORD PROGRAM size too "
637 "long!\n");
638 return 1;
639 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000640 emu_status |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000641 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
642 writearr[3];
643 /* Truncate to emu_chip_size. */
644 aai_offs %= emu_chip_size;
645 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
646 aai_offs += 2;
647 } else {
648 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
649 msg_perr("Continuation AAI WORD PROGRAM size "
650 "too short!\n");
651 return 1;
652 }
653 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
654 msg_perr("Continuation AAI WORD PROGRAM size "
655 "too long!\n");
656 return 1;
657 }
658 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
659 aai_offs += 2;
660 }
661 break;
662 case JEDEC_WRDI:
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000663 if (emu_max_aai_size)
664 emu_status &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000665 break;
666 case JEDEC_SE:
667 if (!emu_jedec_se_size)
668 break;
669 if (writecnt != JEDEC_SE_OUTSIZE) {
670 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
671 return 1;
672 }
673 if (readcnt != JEDEC_SE_INSIZE) {
674 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
675 return 1;
676 }
677 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
678 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000679 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000680 offs &= ~(emu_jedec_se_size - 1);
681 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
682 break;
683 case JEDEC_BE_52:
684 if (!emu_jedec_be_52_size)
685 break;
686 if (writecnt != JEDEC_BE_52_OUTSIZE) {
687 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
688 return 1;
689 }
690 if (readcnt != JEDEC_BE_52_INSIZE) {
691 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
692 return 1;
693 }
694 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
695 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000696 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000697 offs &= ~(emu_jedec_be_52_size - 1);
698 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
699 break;
700 case JEDEC_BE_D8:
701 if (!emu_jedec_be_d8_size)
702 break;
703 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
704 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
705 return 1;
706 }
707 if (readcnt != JEDEC_BE_D8_INSIZE) {
708 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
709 return 1;
710 }
711 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
712 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000713 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000714 offs &= ~(emu_jedec_be_d8_size - 1);
715 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
716 break;
717 case JEDEC_CE_60:
718 if (!emu_jedec_ce_60_size)
719 break;
720 if (writecnt != JEDEC_CE_60_OUTSIZE) {
721 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
722 return 1;
723 }
724 if (readcnt != JEDEC_CE_60_INSIZE) {
725 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
726 return 1;
727 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000728 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000729 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000730 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000731 break;
732 case JEDEC_CE_C7:
733 if (!emu_jedec_ce_c7_size)
734 break;
735 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
736 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
737 return 1;
738 }
739 if (readcnt != JEDEC_CE_C7_INSIZE) {
740 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
741 return 1;
742 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000743 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000744 /* emu_jedec_ce_c7_size is emu_chip_size. */
745 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
746 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000747 case JEDEC_SFDP:
748 if (emu_chip != EMULATE_MACRONIX_MX25L6436)
749 break;
750 if (writecnt < 4)
751 break;
752 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
753
754 /* SFDP expects one dummy byte after the address. */
755 if (writecnt == 4) {
756 /* The dummy byte was not written, make sure it is read instead.
757 * Shifting and shortening the read array does achieve this goal.
758 */
759 readarr++;
760 readcnt--;
761 } else {
762 /* The response is shifted if more than 5 bytes are written, because SFDP data is
763 * already shifted out by the chip while those superfluous bytes are written. */
764 offs += writecnt - 5;
765 }
766
767 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
768 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
769 * This is a reasonable implementation choice in hardware because it saves a few gates. */
770 if (offs >= sizeof(sfdp_table)) {
771 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
772 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
773 offs %= sizeof(sfdp_table);
774 }
775 toread = min(sizeof(sfdp_table) - offs, readcnt);
776 memcpy(readarr, sfdp_table + offs, toread);
777 if (toread < readcnt)
778 msg_pdbg("Crossing the SFDP table boundary in a single "
779 "continuous chunk produces undefined results "
780 "after that point.\n");
781 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000782 default:
783 /* No special response. */
784 break;
785 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000786 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
787 emu_status &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000788 return 0;
789}
790#endif
791
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000792static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt,
793 unsigned int readcnt,
794 const unsigned char *writearr,
795 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000796{
797 int i;
798
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000799 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000800
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000801 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000802 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000803 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000804
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000805 /* Response for unknown commands and missing chip is 0xff. */
806 memset(readarr, 0xff, readcnt);
807#if EMULATE_SPI_CHIP
808 switch (emu_chip) {
809 case EMULATE_ST_M25P10_RES:
810 case EMULATE_SST_SST25VF040_REMS:
811 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000812 case EMULATE_MACRONIX_MX25L6436:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000813 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
814 readarr)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000815 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000816 return 1;
817 }
818 break;
819 default:
820 break;
821 }
822#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000823 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000824 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000825 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000826 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000827 return 0;
828}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000829
Mark Marshallf20b7be2014-05-09 21:16:21 +0000830static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000831{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000832 return spi_write_chunked(flash, buf, start, len,
833 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000834}