blob: f1711282d911cd8cf34d9eb757f853a9c5e53427 [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,
114 .max_data_read = MAX_DATA_READ_UNLIMITED,
115 .max_data_write = MAX_DATA_UNSPECIFIED,
116 .command = dummy_spi_send_command,
117 .multicommand = default_spi_send_multicommand,
118 .read = default_spi_read,
119 .write_256 = dummy_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000120 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000121};
David Hendricks8bb20212011-06-14 01:35:36 +0000122
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000123static const struct par_master par_master_dummy = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000124 .chip_readb = dummy_chip_readb,
125 .chip_readw = dummy_chip_readw,
126 .chip_readl = dummy_chip_readl,
127 .chip_readn = dummy_chip_readn,
128 .chip_writeb = dummy_chip_writeb,
129 .chip_writew = dummy_chip_writew,
130 .chip_writel = dummy_chip_writel,
131 .chip_writen = dummy_chip_writen,
132};
133
134enum chipbustype dummy_buses_supported = BUS_NONE;
135
David Hendricks8bb20212011-06-14 01:35:36 +0000136static int dummy_shutdown(void *data)
137{
138 msg_pspew("%s\n", __func__);
139#if EMULATE_CHIP
140 if (emu_chip != EMULATE_NONE) {
141 if (emu_persistent_image) {
142 msg_pdbg("Writing %s\n", emu_persistent_image);
Stefan Taunere0ff1652012-09-22 22:56:09 +0000143 write_buf_to_file(flashchip_contents, emu_chip_size, emu_persistent_image);
144 free(emu_persistent_image);
145 emu_persistent_image = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000146 }
147 free(flashchip_contents);
148 }
149#endif
150 return 0;
151}
152
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000153int dummy_init(void)
154{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000155 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000156 char *tmp = NULL;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000157 int i;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000158#if EMULATE_SPI_CHIP
159 char *status = NULL;
160#endif
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000161#if EMULATE_CHIP
162 struct stat image_stat;
163#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000164
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000165 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000166
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000167 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000168 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
169 if (!bustext)
170 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000171 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000172 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000173
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000174 dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000175 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000176 dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000177 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000178 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000179 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000180 dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000181 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000182 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000183 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000184 dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000185 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000186 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000187 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000188 dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000189 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000190 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000191 if (dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000192 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000193 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000194
195 tmp = extract_programmer_param("spi_write_256_chunksize");
196 if (tmp) {
197 spi_write_256_chunksize = atoi(tmp);
198 free(tmp);
199 if (spi_write_256_chunksize < 1) {
200 msg_perr("invalid spi_write_256_chunksize\n");
201 return 1;
202 }
203 }
204
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000205 tmp = extract_programmer_param("spi_blacklist");
206 if (tmp) {
207 i = strlen(tmp);
208 if (!strncmp(tmp, "0x", 2)) {
209 i -= 2;
210 memmove(tmp, tmp + 2, i + 1);
211 }
212 if ((i > 512) || (i % 2)) {
213 msg_perr("Invalid SPI command blacklist length\n");
214 free(tmp);
215 return 1;
216 }
217 spi_blacklist_size = i / 2;
218 for (i = 0; i < spi_blacklist_size * 2; i++) {
219 if (!isxdigit((unsigned char)tmp[i])) {
220 msg_perr("Invalid char \"%c\" in SPI command "
221 "blacklist\n", tmp[i]);
222 free(tmp);
223 return 1;
224 }
225 }
226 for (i = 0; i < spi_blacklist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000227 unsigned int tmp2;
228 /* SCNx8 is apparently not supported by MSVC (and thus
229 * MinGW), so work around it with an extra variable
230 */
231 sscanf(tmp + i * 2, "%2x", &tmp2);
232 spi_blacklist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000233 }
234 msg_pdbg("SPI blacklist is ");
235 for (i = 0; i < spi_blacklist_size; i++)
236 msg_pdbg("%02x ", spi_blacklist[i]);
237 msg_pdbg(", size %i\n", spi_blacklist_size);
238 }
239 free(tmp);
240
241 tmp = extract_programmer_param("spi_ignorelist");
242 if (tmp) {
243 i = strlen(tmp);
244 if (!strncmp(tmp, "0x", 2)) {
245 i -= 2;
246 memmove(tmp, tmp + 2, i + 1);
247 }
248 if ((i > 512) || (i % 2)) {
249 msg_perr("Invalid SPI command ignorelist length\n");
250 free(tmp);
251 return 1;
252 }
253 spi_ignorelist_size = i / 2;
254 for (i = 0; i < spi_ignorelist_size * 2; i++) {
255 if (!isxdigit((unsigned char)tmp[i])) {
256 msg_perr("Invalid char \"%c\" in SPI command "
257 "ignorelist\n", tmp[i]);
258 free(tmp);
259 return 1;
260 }
261 }
262 for (i = 0; i < spi_ignorelist_size; i++) {
Carl-Daniel Hailfinger5b554712012-02-16 01:43:06 +0000263 unsigned int tmp2;
264 /* SCNx8 is apparently not supported by MSVC (and thus
265 * MinGW), so work around it with an extra variable
266 */
267 sscanf(tmp + i * 2, "%2x", &tmp2);
268 spi_ignorelist[i] = (uint8_t)tmp2;
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000269 }
270 msg_pdbg("SPI ignorelist is ");
271 for (i = 0; i < spi_ignorelist_size; i++)
272 msg_pdbg("%02x ", spi_ignorelist[i]);
273 msg_pdbg(", size %i\n", spi_ignorelist_size);
274 }
275 free(tmp);
276
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000277#if EMULATE_CHIP
278 tmp = extract_programmer_param("emulate");
279 if (!tmp) {
280 msg_pdbg("Not emulating any flash chip.\n");
281 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000282 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000283 }
284#if EMULATE_SPI_CHIP
285 if (!strcmp(tmp, "M25P10.RES")) {
286 emu_chip = EMULATE_ST_M25P10_RES;
287 emu_chip_size = 128 * 1024;
288 emu_max_byteprogram_size = 128;
289 emu_max_aai_size = 0;
290 emu_jedec_se_size = 0;
291 emu_jedec_be_52_size = 0;
292 emu_jedec_be_d8_size = 32 * 1024;
293 emu_jedec_ce_60_size = 0;
294 emu_jedec_ce_c7_size = emu_chip_size;
295 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
296 "write)\n");
297 }
298 if (!strcmp(tmp, "SST25VF040.REMS")) {
299 emu_chip = EMULATE_SST_SST25VF040_REMS;
300 emu_chip_size = 512 * 1024;
301 emu_max_byteprogram_size = 1;
302 emu_max_aai_size = 0;
303 emu_jedec_se_size = 4 * 1024;
304 emu_jedec_be_52_size = 32 * 1024;
305 emu_jedec_be_d8_size = 0;
306 emu_jedec_ce_60_size = emu_chip_size;
307 emu_jedec_ce_c7_size = 0;
308 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
309 "byte write)\n");
310 }
311 if (!strcmp(tmp, "SST25VF032B")) {
312 emu_chip = EMULATE_SST_SST25VF032B;
313 emu_chip_size = 4 * 1024 * 1024;
314 emu_max_byteprogram_size = 1;
315 emu_max_aai_size = 2;
316 emu_jedec_se_size = 4 * 1024;
317 emu_jedec_be_52_size = 32 * 1024;
318 emu_jedec_be_d8_size = 64 * 1024;
319 emu_jedec_ce_60_size = emu_chip_size;
320 emu_jedec_ce_c7_size = emu_chip_size;
321 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
322 "write)\n");
323 }
Stefan Tauner0b9df972012-05-07 22:12:16 +0000324 if (!strcmp(tmp, "MX25L6436")) {
325 emu_chip = EMULATE_MACRONIX_MX25L6436;
326 emu_chip_size = 8 * 1024 * 1024;
327 emu_max_byteprogram_size = 256;
328 emu_max_aai_size = 0;
329 emu_jedec_se_size = 4 * 1024;
330 emu_jedec_be_52_size = 32 * 1024;
331 emu_jedec_be_d8_size = 64 * 1024;
332 emu_jedec_ce_60_size = emu_chip_size;
333 emu_jedec_ce_c7_size = emu_chip_size;
334 msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, "
335 "SFDP)\n");
336 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000337#endif
338 if (emu_chip == EMULATE_NONE) {
339 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
340 free(tmp);
341 return 1;
342 }
343 free(tmp);
344 flashchip_contents = malloc(emu_chip_size);
345 if (!flashchip_contents) {
346 msg_perr("Out of memory!\n");
347 return 1;
348 }
David Hendricks8bb20212011-06-14 01:35:36 +0000349
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000350#ifdef EMULATE_SPI_CHIP
351 status = extract_programmer_param("spi_status");
352 if (status) {
353 char *endptr;
354 errno = 0;
355 emu_status = strtoul(status, &endptr, 0);
356 free(status);
357 if (errno != 0 || status == endptr) {
358 msg_perr("Error: initial status register specified, "
359 "but the value could not be converted.\n");
360 return 1;
361 }
362 msg_pdbg("Initial status register is set to 0x%02x.\n",
363 emu_status);
364 }
365#endif
366
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000367 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
368 memset(flashchip_contents, 0xff, emu_chip_size);
369
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000370 /* Will be freed by shutdown function if necessary. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000371 emu_persistent_image = extract_programmer_param("image");
372 if (!emu_persistent_image) {
373 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000374 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000375 }
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000376 /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does
377 * not match the emulated chip. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000378 if (!stat(emu_persistent_image, &image_stat)) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000379 msg_pdbg("Found persistent image %s, %jd B ",
380 emu_persistent_image, (intmax_t)image_stat.st_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000381 if (image_stat.st_size == emu_chip_size) {
382 msg_pdbg("matches.\n");
383 msg_pdbg("Reading %s\n", emu_persistent_image);
384 read_buf_from_file(flashchip_contents, emu_chip_size,
385 emu_persistent_image);
386 } else {
387 msg_pdbg("doesn't match.\n");
388 }
389 }
390#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000391
David Hendricks8bb20212011-06-14 01:35:36 +0000392dummy_init_out:
393 if (register_shutdown(dummy_shutdown, NULL)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000394 free(flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000395 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000396 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000397 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000398 register_par_master(&par_master_dummy,
399 dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH));
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000400 if (dummy_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000401 register_spi_master(&spi_master_dummyflasher);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000402
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000403 return 0;
404}
405
Stefan Tauner305e0b92013-07-17 23:46:44 +0000406void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000407{
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000408 msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
Stefan Tauner0554ca52013-07-25 22:54:25 +0000409 __func__, descr, len, PRIxPTR_WIDTH, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000410 return (void *)phys_addr;
411}
412
413void dummy_unmap(void *virt_addr, size_t len)
414{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000415 msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000416}
417
Mark Marshallf20b7be2014-05-09 21:16:21 +0000418static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000419{
Stefan Taunerc2333752013-07-13 23:31:37 +0000420 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000421}
422
Mark Marshallf20b7be2014-05-09 21:16:21 +0000423static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000424{
Stefan Taunerc2333752013-07-13 23:31:37 +0000425 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000426}
427
Mark Marshallf20b7be2014-05-09 21:16:21 +0000428static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000429{
Stefan Taunerc2333752013-07-13 23:31:37 +0000430 msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000431}
432
Mark Marshallf20b7be2014-05-09 21:16:21 +0000433static 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 +0000434{
435 size_t i;
Stefan Tauner0554ca52013-07-25 22:54:25 +0000436 msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000437 for (i = 0; i < len; i++) {
438 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000439 msg_pspew("\n");
440 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000441 }
442}
443
Mark Marshallf20b7be2014-05-09 21:16:21 +0000444static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000445{
Stefan Taunerc2333752013-07-13 23:31:37 +0000446 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000447 return 0xff;
448}
449
Mark Marshallf20b7be2014-05-09 21:16:21 +0000450static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000451{
Stefan Taunerc2333752013-07-13 23:31:37 +0000452 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000453 return 0xffff;
454}
455
Mark Marshallf20b7be2014-05-09 21:16:21 +0000456static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000457{
Stefan Taunerc2333752013-07-13 23:31:37 +0000458 msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000459 return 0xffffffff;
460}
461
Mark Marshallf20b7be2014-05-09 21:16:21 +0000462static 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 +0000463{
Stefan Tauner0554ca52013-07-25 22:54:25 +0000464 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 +0000465 memset(buf, 0xff, len);
466 return;
467}
468
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000469#if EMULATE_SPI_CHIP
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000470static int emulate_spi_chip_response(unsigned int writecnt,
471 unsigned int readcnt,
472 const unsigned char *writearr,
473 unsigned char *readarr)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000474{
Stefan Tauner0b9df972012-05-07 22:12:16 +0000475 unsigned int offs, i, toread;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000476 static int unsigned aai_offs;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000477 const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44};
478 const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a};
479 const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16};
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000480
481 if (writecnt == 0) {
482 msg_perr("No command sent to the chip!\n");
483 return 1;
484 }
Paul Menzelac427b22012-02-16 21:07:07 +0000485 /* spi_blacklist has precedence over spi_ignorelist. */
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000486 for (i = 0; i < spi_blacklist_size; i++) {
487 if (writearr[0] == spi_blacklist[i]) {
488 msg_pdbg("Refusing blacklisted SPI command 0x%02x\n",
489 spi_blacklist[i]);
490 return SPI_INVALID_OPCODE;
491 }
492 }
493 for (i = 0; i < spi_ignorelist_size; i++) {
494 if (writearr[0] == spi_ignorelist[i]) {
495 msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n",
496 spi_ignorelist[i]);
497 /* Return success because the command does not fail,
498 * it is simply ignored.
499 */
500 return 0;
501 }
502 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000503
504 if (emu_max_aai_size && (emu_status & SPI_SR_AAI)) {
505 if (writearr[0] != JEDEC_AAI_WORD_PROGRAM &&
506 writearr[0] != JEDEC_WRDI &&
507 writearr[0] != JEDEC_RDSR) {
508 msg_perr("Forbidden opcode (0x%02x) attempted during "
509 "AAI sequence!\n", writearr[0]);
510 return 0;
511 }
512 }
513
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000514 switch (writearr[0]) {
515 case JEDEC_RES:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000516 if (writecnt < JEDEC_RES_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000517 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000518 /* offs calculation is only needed for SST chips which treat RES like REMS. */
519 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
520 offs += writecnt - JEDEC_REMS_OUTSIZE;
521 switch (emu_chip) {
522 case EMULATE_ST_M25P10_RES:
523 if (readcnt > 0)
524 memset(readarr, 0x10, readcnt);
525 break;
526 case EMULATE_SST_SST25VF040_REMS:
527 for (i = 0; i < readcnt; i++)
528 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
529 break;
530 case EMULATE_SST_SST25VF032B:
531 for (i = 0; i < readcnt; i++)
532 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
533 break;
534 case EMULATE_MACRONIX_MX25L6436:
535 if (readcnt > 0)
536 memset(readarr, 0x16, readcnt);
537 break;
538 default: /* ignore */
539 break;
540 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000541 break;
542 case JEDEC_REMS:
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000543 /* REMS response has wraparound and uses an address parameter. */
544 if (writecnt < JEDEC_REMS_OUTSIZE)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000545 break;
Carl-Daniel Hailfingeraf2cac02012-08-30 21:41:00 +0000546 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
547 offs += writecnt - JEDEC_REMS_OUTSIZE;
548 switch (emu_chip) {
549 case EMULATE_SST_SST25VF040_REMS:
550 for (i = 0; i < readcnt; i++)
551 readarr[i] = sst25vf040_rems_response[(offs + i) % 2];
552 break;
553 case EMULATE_SST_SST25VF032B:
554 for (i = 0; i < readcnt; i++)
555 readarr[i] = sst25vf032b_rems_response[(offs + i) % 2];
556 break;
557 case EMULATE_MACRONIX_MX25L6436:
558 for (i = 0; i < readcnt; i++)
559 readarr[i] = mx25l6436_rems_response[(offs + i) % 2];
560 break;
561 default: /* ignore */
562 break;
563 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000564 break;
565 case JEDEC_RDID:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000566 switch (emu_chip) {
567 case EMULATE_SST_SST25VF032B:
568 if (readcnt > 0)
569 readarr[0] = 0xbf;
570 if (readcnt > 1)
571 readarr[1] = 0x25;
572 if (readcnt > 2)
573 readarr[2] = 0x4a;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000574 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000575 case EMULATE_MACRONIX_MX25L6436:
576 if (readcnt > 0)
577 readarr[0] = 0xc2;
578 if (readcnt > 1)
579 readarr[1] = 0x20;
580 if (readcnt > 2)
581 readarr[2] = 0x17;
582 break;
583 default: /* ignore */
584 break;
585 }
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000586 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000587 case JEDEC_RDSR:
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000588 memset(readarr, emu_status, readcnt);
589 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000590 /* FIXME: this should be chip-specific. */
591 case JEDEC_EWSR:
592 case JEDEC_WREN:
593 emu_status |= SPI_SR_WEL;
594 break;
595 case JEDEC_WRSR:
596 if (!(emu_status & SPI_SR_WEL)) {
597 msg_perr("WRSR attempted, but WEL is 0!\n");
598 break;
599 }
600 /* FIXME: add some reasonable simulation of the busy flag */
601 emu_status = writearr[1] & ~SPI_SR_WIP;
602 msg_pdbg2("WRSR wrote 0x%02x.\n", emu_status);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000603 break;
604 case JEDEC_READ:
605 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
606 /* Truncate to emu_chip_size. */
607 offs %= emu_chip_size;
608 if (readcnt > 0)
609 memcpy(readarr, flashchip_contents + offs, readcnt);
610 break;
611 case JEDEC_BYTE_PROGRAM:
612 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
613 /* Truncate to emu_chip_size. */
614 offs %= emu_chip_size;
615 if (writecnt < 5) {
616 msg_perr("BYTE PROGRAM size too short!\n");
617 return 1;
618 }
619 if (writecnt - 4 > emu_max_byteprogram_size) {
620 msg_perr("Max BYTE PROGRAM size exceeded!\n");
621 return 1;
622 }
623 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
624 break;
625 case JEDEC_AAI_WORD_PROGRAM:
626 if (!emu_max_aai_size)
627 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000628 if (!(emu_status & SPI_SR_AAI)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000629 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
630 msg_perr("Initial AAI WORD PROGRAM size too "
631 "short!\n");
632 return 1;
633 }
634 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
635 msg_perr("Initial AAI WORD PROGRAM size too "
636 "long!\n");
637 return 1;
638 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000639 emu_status |= SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000640 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
641 writearr[3];
642 /* Truncate to emu_chip_size. */
643 aai_offs %= emu_chip_size;
644 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
645 aai_offs += 2;
646 } else {
647 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
648 msg_perr("Continuation AAI WORD PROGRAM size "
649 "too short!\n");
650 return 1;
651 }
652 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
653 msg_perr("Continuation AAI WORD PROGRAM size "
654 "too long!\n");
655 return 1;
656 }
657 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
658 aai_offs += 2;
659 }
660 break;
661 case JEDEC_WRDI:
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000662 if (emu_max_aai_size)
663 emu_status &= ~SPI_SR_AAI;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000664 break;
665 case JEDEC_SE:
666 if (!emu_jedec_se_size)
667 break;
668 if (writecnt != JEDEC_SE_OUTSIZE) {
669 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
670 return 1;
671 }
672 if (readcnt != JEDEC_SE_INSIZE) {
673 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
674 return 1;
675 }
676 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
677 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000678 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000679 offs &= ~(emu_jedec_se_size - 1);
680 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
681 break;
682 case JEDEC_BE_52:
683 if (!emu_jedec_be_52_size)
684 break;
685 if (writecnt != JEDEC_BE_52_OUTSIZE) {
686 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
687 return 1;
688 }
689 if (readcnt != JEDEC_BE_52_INSIZE) {
690 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
691 return 1;
692 }
693 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
694 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000695 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000696 offs &= ~(emu_jedec_be_52_size - 1);
697 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
698 break;
699 case JEDEC_BE_D8:
700 if (!emu_jedec_be_d8_size)
701 break;
702 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
703 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
704 return 1;
705 }
706 if (readcnt != JEDEC_BE_D8_INSIZE) {
707 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
708 return 1;
709 }
710 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
711 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000712 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000713 offs &= ~(emu_jedec_be_d8_size - 1);
714 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
715 break;
716 case JEDEC_CE_60:
717 if (!emu_jedec_ce_60_size)
718 break;
719 if (writecnt != JEDEC_CE_60_OUTSIZE) {
720 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
721 return 1;
722 }
723 if (readcnt != JEDEC_CE_60_INSIZE) {
724 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
725 return 1;
726 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000727 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000728 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000729 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000730 break;
731 case JEDEC_CE_C7:
732 if (!emu_jedec_ce_c7_size)
733 break;
734 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
735 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
736 return 1;
737 }
738 if (readcnt != JEDEC_CE_C7_INSIZE) {
739 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
740 return 1;
741 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000742 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000743 /* emu_jedec_ce_c7_size is emu_chip_size. */
744 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
745 break;
Stefan Tauner0b9df972012-05-07 22:12:16 +0000746 case JEDEC_SFDP:
747 if (emu_chip != EMULATE_MACRONIX_MX25L6436)
748 break;
749 if (writecnt < 4)
750 break;
751 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
752
753 /* SFDP expects one dummy byte after the address. */
754 if (writecnt == 4) {
755 /* The dummy byte was not written, make sure it is read instead.
756 * Shifting and shortening the read array does achieve this goal.
757 */
758 readarr++;
759 readcnt--;
760 } else {
761 /* The response is shifted if more than 5 bytes are written, because SFDP data is
762 * already shifted out by the chip while those superfluous bytes are written. */
763 offs += writecnt - 5;
764 }
765
766 /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the
767 * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size.
768 * This is a reasonable implementation choice in hardware because it saves a few gates. */
769 if (offs >= sizeof(sfdp_table)) {
770 msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x "
771 "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs);
772 offs %= sizeof(sfdp_table);
773 }
774 toread = min(sizeof(sfdp_table) - offs, readcnt);
775 memcpy(readarr, sfdp_table + offs, toread);
776 if (toread < readcnt)
777 msg_pdbg("Crossing the SFDP table boundary in a single "
778 "continuous chunk produces undefined results "
779 "after that point.\n");
780 break;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000781 default:
782 /* No special response. */
783 break;
784 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000785 if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR)
786 emu_status &= ~SPI_SR_WEL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000787 return 0;
788}
789#endif
790
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000791static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt,
792 unsigned int readcnt,
793 const unsigned char *writearr,
794 unsigned char *readarr)
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000795{
796 int i;
797
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000798 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000799
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000800 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000801 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000802 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000803
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000804 /* Response for unknown commands and missing chip is 0xff. */
805 memset(readarr, 0xff, readcnt);
806#if EMULATE_SPI_CHIP
807 switch (emu_chip) {
808 case EMULATE_ST_M25P10_RES:
809 case EMULATE_SST_SST25VF040_REMS:
810 case EMULATE_SST_SST25VF032B:
Stefan Tauner0b9df972012-05-07 22:12:16 +0000811 case EMULATE_MACRONIX_MX25L6436:
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000812 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
813 readarr)) {
Carl-Daniel Hailfinger1b83be52012-02-08 23:28:54 +0000814 msg_pdbg("Invalid command sent to flash chip!\n");
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000815 return 1;
816 }
817 break;
818 default:
819 break;
820 }
821#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000822 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000823 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000824 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000825 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000826 return 0;
827}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000828
Mark Marshallf20b7be2014-05-09 21:16:21 +0000829static 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 +0000830{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000831 return spi_write_chunked(flash, buf, start, len,
832 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000833}