blob: fdb4f2a6dba5fe9e5a2c25dcb9e40acb707b9460 [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 Hailfingerc3129202009-05-09 00:54:55 +000022#include "flash.h"
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +000023#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000024#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000025
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000026/* Remove the #define below if you don't want SPI flash chip emulation. */
27#define EMULATE_SPI_CHIP 1
28
29#if EMULATE_SPI_CHIP
30#define EMULATE_CHIP 1
31#include "spi.h"
32#endif
33
34#if EMULATE_CHIP
35#include <sys/types.h>
36#include <sys/stat.h>
37#endif
38
39#if EMULATE_CHIP
40static uint8_t *flashchip_contents = NULL;
41enum emu_chip {
42 EMULATE_NONE,
43 EMULATE_ST_M25P10_RES,
44 EMULATE_SST_SST25VF040_REMS,
45 EMULATE_SST_SST25VF032B,
46};
47static enum emu_chip emu_chip = EMULATE_NONE;
48static char *emu_persistent_image = NULL;
49static int emu_chip_size = 0;
50#if EMULATE_SPI_CHIP
51static int emu_max_byteprogram_size = 0;
52static int emu_max_aai_size = 0;
53static int emu_jedec_se_size = 0;
54static int emu_jedec_be_52_size = 0;
55static int emu_jedec_be_d8_size = 0;
56static int emu_jedec_ce_60_size = 0;
57static int emu_jedec_ce_c7_size = 0;
58#endif
59#endif
60
61static int spi_write_256_chunksize = 256;
62
Michael Karcherb9dbe482011-05-11 17:07:07 +000063static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
64 const unsigned char *writearr, unsigned char *readarr);
65static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len);
66
67static const struct spi_programmer spi_programmer_dummyflasher = {
68 .type = SPI_CONTROLLER_DUMMY,
69 .max_data_read = MAX_DATA_READ_UNLIMITED,
70 .max_data_write = MAX_DATA_UNSPECIFIED,
71 .command = dummy_spi_send_command,
72 .multicommand = default_spi_send_multicommand,
73 .read = default_spi_read,
74 .write_256 = dummy_spi_write_256,
75};
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000076int dummy_init(void)
77{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000078 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000079 char *tmp = NULL;
80#if EMULATE_CHIP
81 struct stat image_stat;
82#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000083
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000084 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000085
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +000086 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000087 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
88 if (!bustext)
89 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000090 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000091 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000092
93 buses_supported = CHIP_BUSTYPE_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000094 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000095 buses_supported |= CHIP_BUSTYPE_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000096 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000097 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000098 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000099 buses_supported |= CHIP_BUSTYPE_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000100 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000101 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000102 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000103 buses_supported |= CHIP_BUSTYPE_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000104 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000105 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000106 if (strstr(bustext, "spi")) {
Michael Karcherb9dbe482011-05-11 17:07:07 +0000107 register_spi_programmer(&spi_programmer_dummyflasher);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000108 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000109 }
110 if (buses_supported == CHIP_BUSTYPE_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000111 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000112 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000113
114 tmp = extract_programmer_param("spi_write_256_chunksize");
115 if (tmp) {
116 spi_write_256_chunksize = atoi(tmp);
117 free(tmp);
118 if (spi_write_256_chunksize < 1) {
119 msg_perr("invalid spi_write_256_chunksize\n");
120 return 1;
121 }
122 }
123
124#if EMULATE_CHIP
125 tmp = extract_programmer_param("emulate");
126 if (!tmp) {
127 msg_pdbg("Not emulating any flash chip.\n");
128 /* Nothing else to do. */
129 return 0;
130 }
131#if EMULATE_SPI_CHIP
132 if (!strcmp(tmp, "M25P10.RES")) {
133 emu_chip = EMULATE_ST_M25P10_RES;
134 emu_chip_size = 128 * 1024;
135 emu_max_byteprogram_size = 128;
136 emu_max_aai_size = 0;
137 emu_jedec_se_size = 0;
138 emu_jedec_be_52_size = 0;
139 emu_jedec_be_d8_size = 32 * 1024;
140 emu_jedec_ce_60_size = 0;
141 emu_jedec_ce_c7_size = emu_chip_size;
142 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
143 "write)\n");
144 }
145 if (!strcmp(tmp, "SST25VF040.REMS")) {
146 emu_chip = EMULATE_SST_SST25VF040_REMS;
147 emu_chip_size = 512 * 1024;
148 emu_max_byteprogram_size = 1;
149 emu_max_aai_size = 0;
150 emu_jedec_se_size = 4 * 1024;
151 emu_jedec_be_52_size = 32 * 1024;
152 emu_jedec_be_d8_size = 0;
153 emu_jedec_ce_60_size = emu_chip_size;
154 emu_jedec_ce_c7_size = 0;
155 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
156 "byte write)\n");
157 }
158 if (!strcmp(tmp, "SST25VF032B")) {
159 emu_chip = EMULATE_SST_SST25VF032B;
160 emu_chip_size = 4 * 1024 * 1024;
161 emu_max_byteprogram_size = 1;
162 emu_max_aai_size = 2;
163 emu_jedec_se_size = 4 * 1024;
164 emu_jedec_be_52_size = 32 * 1024;
165 emu_jedec_be_d8_size = 64 * 1024;
166 emu_jedec_ce_60_size = emu_chip_size;
167 emu_jedec_ce_c7_size = emu_chip_size;
168 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
169 "write)\n");
170 }
171#endif
172 if (emu_chip == EMULATE_NONE) {
173 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
174 free(tmp);
175 return 1;
176 }
177 free(tmp);
178 flashchip_contents = malloc(emu_chip_size);
179 if (!flashchip_contents) {
180 msg_perr("Out of memory!\n");
181 return 1;
182 }
183 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
184 memset(flashchip_contents, 0xff, emu_chip_size);
185
186 emu_persistent_image = extract_programmer_param("image");
187 if (!emu_persistent_image) {
188 /* Nothing else to do. */
189 return 0;
190 }
191 if (!stat(emu_persistent_image, &image_stat)) {
192 msg_pdbg("Found persistent image %s, size %li ",
193 emu_persistent_image, (long)image_stat.st_size);
194 if (image_stat.st_size == emu_chip_size) {
195 msg_pdbg("matches.\n");
196 msg_pdbg("Reading %s\n", emu_persistent_image);
197 read_buf_from_file(flashchip_contents, emu_chip_size,
198 emu_persistent_image);
199 } else {
200 msg_pdbg("doesn't match.\n");
201 }
202 }
203#endif
Uwe Hermanne9d04d42009-06-02 19:54:22 +0000204 return 0;
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000205}
206
207int dummy_shutdown(void)
208{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000209 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000210#if EMULATE_CHIP
211 if (emu_chip != EMULATE_NONE) {
212 if (emu_persistent_image) {
213 msg_pdbg("Writing %s\n", emu_persistent_image);
214 write_buf_to_file(flashchip_contents, emu_chip_size,
215 emu_persistent_image);
216 }
217 free(flashchip_contents);
218 }
219#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000220 return 0;
221}
222
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000223void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
224{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000225 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
226 __func__, descr, (unsigned long)len, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000227 return (void *)phys_addr;
228}
229
230void dummy_unmap(void *virt_addr, size_t len)
231{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000232 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
233 __func__, (unsigned long)len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000234}
235
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000236void dummy_chip_writeb(uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000237{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000238 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000239}
240
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000241void dummy_chip_writew(uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000242{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000243 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000244}
245
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000246void dummy_chip_writel(uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000247{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000248 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000249}
250
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000251void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
252{
253 size_t i;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000254 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
255 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000256 for (i = 0; i < len; i++) {
257 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000258 msg_pspew("\n");
259 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000260 }
261}
262
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000263uint8_t dummy_chip_readb(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000264{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000265 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000266 return 0xff;
267}
268
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000269uint16_t dummy_chip_readw(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000270{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000271 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000272 return 0xffff;
273}
274
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000275uint32_t dummy_chip_readl(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000276{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000277 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000278 return 0xffffffff;
279}
280
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000281void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
282{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000283 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
284 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000285 memset(buf, 0xff, len);
286 return;
287}
288
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000289#if EMULATE_SPI_CHIP
290static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
291 const unsigned char *writearr, unsigned char *readarr)
292{
293 int offs;
294 static int aai_offs;
295 static int aai_active = 0;
296
297 if (writecnt == 0) {
298 msg_perr("No command sent to the chip!\n");
299 return 1;
300 }
301 /* TODO: Implement command blacklists here. */
302 switch (writearr[0]) {
303 case JEDEC_RES:
304 if (emu_chip != EMULATE_ST_M25P10_RES)
305 break;
306 /* Respond with ST_M25P10_RES. */
307 if (readcnt > 0)
308 readarr[0] = 0x10;
309 break;
310 case JEDEC_REMS:
311 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
312 break;
313 /* Respond with SST_SST25VF040_REMS. */
314 if (readcnt > 0)
315 readarr[0] = 0xbf;
316 if (readcnt > 1)
317 readarr[1] = 0x44;
318 break;
319 case JEDEC_RDID:
320 if (emu_chip != EMULATE_SST_SST25VF032B)
321 break;
322 /* Respond with SST_SST25VF032B. */
323 if (readcnt > 0)
324 readarr[0] = 0xbf;
325 if (readcnt > 1)
326 readarr[1] = 0x25;
327 if (readcnt > 2)
328 readarr[2] = 0x4a;
329 break;
330 case JEDEC_RDSR:
331 memset(readarr, 0, readcnt);
332 if (aai_active)
333 memset(readarr, 1 << 6, readcnt);
334 break;
335 case JEDEC_READ:
336 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
337 /* Truncate to emu_chip_size. */
338 offs %= emu_chip_size;
339 if (readcnt > 0)
340 memcpy(readarr, flashchip_contents + offs, readcnt);
341 break;
342 case JEDEC_BYTE_PROGRAM:
343 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
344 /* Truncate to emu_chip_size. */
345 offs %= emu_chip_size;
346 if (writecnt < 5) {
347 msg_perr("BYTE PROGRAM size too short!\n");
348 return 1;
349 }
350 if (writecnt - 4 > emu_max_byteprogram_size) {
351 msg_perr("Max BYTE PROGRAM size exceeded!\n");
352 return 1;
353 }
354 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
355 break;
356 case JEDEC_AAI_WORD_PROGRAM:
357 if (!emu_max_aai_size)
358 break;
359 if (!aai_active) {
360 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
361 msg_perr("Initial AAI WORD PROGRAM size too "
362 "short!\n");
363 return 1;
364 }
365 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
366 msg_perr("Initial AAI WORD PROGRAM size too "
367 "long!\n");
368 return 1;
369 }
370 aai_active = 1;
371 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
372 writearr[3];
373 /* Truncate to emu_chip_size. */
374 aai_offs %= emu_chip_size;
375 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
376 aai_offs += 2;
377 } else {
378 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
379 msg_perr("Continuation AAI WORD PROGRAM size "
380 "too short!\n");
381 return 1;
382 }
383 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
384 msg_perr("Continuation AAI WORD PROGRAM size "
385 "too long!\n");
386 return 1;
387 }
388 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
389 aai_offs += 2;
390 }
391 break;
392 case JEDEC_WRDI:
393 if (!emu_max_aai_size)
394 break;
395 aai_active = 0;
396 break;
397 case JEDEC_SE:
398 if (!emu_jedec_se_size)
399 break;
400 if (writecnt != JEDEC_SE_OUTSIZE) {
401 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
402 return 1;
403 }
404 if (readcnt != JEDEC_SE_INSIZE) {
405 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
406 return 1;
407 }
408 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
409 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000410 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000411 offs &= ~(emu_jedec_se_size - 1);
412 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
413 break;
414 case JEDEC_BE_52:
415 if (!emu_jedec_be_52_size)
416 break;
417 if (writecnt != JEDEC_BE_52_OUTSIZE) {
418 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
419 return 1;
420 }
421 if (readcnt != JEDEC_BE_52_INSIZE) {
422 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
423 return 1;
424 }
425 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
426 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000427 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000428 offs &= ~(emu_jedec_be_52_size - 1);
429 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
430 break;
431 case JEDEC_BE_D8:
432 if (!emu_jedec_be_d8_size)
433 break;
434 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
435 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
436 return 1;
437 }
438 if (readcnt != JEDEC_BE_D8_INSIZE) {
439 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
440 return 1;
441 }
442 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
443 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000444 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000445 offs &= ~(emu_jedec_be_d8_size - 1);
446 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
447 break;
448 case JEDEC_CE_60:
449 if (!emu_jedec_ce_60_size)
450 break;
451 if (writecnt != JEDEC_CE_60_OUTSIZE) {
452 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
453 return 1;
454 }
455 if (readcnt != JEDEC_CE_60_INSIZE) {
456 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
457 return 1;
458 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000459 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000460 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000461 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000462 break;
463 case JEDEC_CE_C7:
464 if (!emu_jedec_ce_c7_size)
465 break;
466 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
467 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
468 return 1;
469 }
470 if (readcnt != JEDEC_CE_C7_INSIZE) {
471 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
472 return 1;
473 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000474 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000475 /* emu_jedec_ce_c7_size is emu_chip_size. */
476 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
477 break;
478 default:
479 /* No special response. */
480 break;
481 }
482 return 0;
483}
484#endif
485
Michael Karcherb9dbe482011-05-11 17:07:07 +0000486static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000487 const unsigned char *writearr, unsigned char *readarr)
488{
489 int i;
490
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000491 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000492
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000493 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000494 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000495 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000496
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000497 /* Response for unknown commands and missing chip is 0xff. */
498 memset(readarr, 0xff, readcnt);
499#if EMULATE_SPI_CHIP
500 switch (emu_chip) {
501 case EMULATE_ST_M25P10_RES:
502 case EMULATE_SST_SST25VF040_REMS:
503 case EMULATE_SST_SST25VF032B:
504 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
505 readarr)) {
506 msg_perr("Invalid command sent to flash chip!\n");
507 return 1;
508 }
509 break;
510 default:
511 break;
512 }
513#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000514 msg_pspew(" reading %u bytes:", readcnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000515 for (i = 0; i < readcnt; i++) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000516 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000517 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000518 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000519 return 0;
520}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000521
Michael Karcherb9dbe482011-05-11 17:07:07 +0000522static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000523{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000524 return spi_write_chunked(flash, buf, start, len,
525 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000526}