blob: d818a84ced4eb9a6219cc7def8c8857503fe83fc [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
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000063int dummy_init(void)
64{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000065 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000066 char *tmp = NULL;
67#if EMULATE_CHIP
68 struct stat image_stat;
69#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000070
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000071 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000072
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +000073 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000074 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
75 if (!bustext)
76 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000077 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000078 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000079
80 buses_supported = CHIP_BUSTYPE_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000081 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000082 buses_supported |= CHIP_BUSTYPE_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000083 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000084 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000085 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000086 buses_supported |= CHIP_BUSTYPE_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000087 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000088 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000089 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000090 buses_supported |= CHIP_BUSTYPE_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000091 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000092 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000093 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000094 buses_supported |= CHIP_BUSTYPE_SPI;
95 spi_controller = SPI_CONTROLLER_DUMMY;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000096 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000097 }
98 if (buses_supported == CHIP_BUSTYPE_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000099 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000100 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000101
102 tmp = extract_programmer_param("spi_write_256_chunksize");
103 if (tmp) {
104 spi_write_256_chunksize = atoi(tmp);
105 free(tmp);
106 if (spi_write_256_chunksize < 1) {
107 msg_perr("invalid spi_write_256_chunksize\n");
108 return 1;
109 }
110 }
111
112#if EMULATE_CHIP
113 tmp = extract_programmer_param("emulate");
114 if (!tmp) {
115 msg_pdbg("Not emulating any flash chip.\n");
116 /* Nothing else to do. */
117 return 0;
118 }
119#if EMULATE_SPI_CHIP
120 if (!strcmp(tmp, "M25P10.RES")) {
121 emu_chip = EMULATE_ST_M25P10_RES;
122 emu_chip_size = 128 * 1024;
123 emu_max_byteprogram_size = 128;
124 emu_max_aai_size = 0;
125 emu_jedec_se_size = 0;
126 emu_jedec_be_52_size = 0;
127 emu_jedec_be_d8_size = 32 * 1024;
128 emu_jedec_ce_60_size = 0;
129 emu_jedec_ce_c7_size = emu_chip_size;
130 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
131 "write)\n");
132 }
133 if (!strcmp(tmp, "SST25VF040.REMS")) {
134 emu_chip = EMULATE_SST_SST25VF040_REMS;
135 emu_chip_size = 512 * 1024;
136 emu_max_byteprogram_size = 1;
137 emu_max_aai_size = 0;
138 emu_jedec_se_size = 4 * 1024;
139 emu_jedec_be_52_size = 32 * 1024;
140 emu_jedec_be_d8_size = 0;
141 emu_jedec_ce_60_size = emu_chip_size;
142 emu_jedec_ce_c7_size = 0;
143 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
144 "byte write)\n");
145 }
146 if (!strcmp(tmp, "SST25VF032B")) {
147 emu_chip = EMULATE_SST_SST25VF032B;
148 emu_chip_size = 4 * 1024 * 1024;
149 emu_max_byteprogram_size = 1;
150 emu_max_aai_size = 2;
151 emu_jedec_se_size = 4 * 1024;
152 emu_jedec_be_52_size = 32 * 1024;
153 emu_jedec_be_d8_size = 64 * 1024;
154 emu_jedec_ce_60_size = emu_chip_size;
155 emu_jedec_ce_c7_size = emu_chip_size;
156 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
157 "write)\n");
158 }
159#endif
160 if (emu_chip == EMULATE_NONE) {
161 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
162 free(tmp);
163 return 1;
164 }
165 free(tmp);
166 flashchip_contents = malloc(emu_chip_size);
167 if (!flashchip_contents) {
168 msg_perr("Out of memory!\n");
169 return 1;
170 }
171 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
172 memset(flashchip_contents, 0xff, emu_chip_size);
173
174 emu_persistent_image = extract_programmer_param("image");
175 if (!emu_persistent_image) {
176 /* Nothing else to do. */
177 return 0;
178 }
179 if (!stat(emu_persistent_image, &image_stat)) {
180 msg_pdbg("Found persistent image %s, size %li ",
181 emu_persistent_image, (long)image_stat.st_size);
182 if (image_stat.st_size == emu_chip_size) {
183 msg_pdbg("matches.\n");
184 msg_pdbg("Reading %s\n", emu_persistent_image);
185 read_buf_from_file(flashchip_contents, emu_chip_size,
186 emu_persistent_image);
187 } else {
188 msg_pdbg("doesn't match.\n");
189 }
190 }
191#endif
Uwe Hermanne9d04d42009-06-02 19:54:22 +0000192 return 0;
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000193}
194
195int dummy_shutdown(void)
196{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000197 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000198#if EMULATE_CHIP
199 if (emu_chip != EMULATE_NONE) {
200 if (emu_persistent_image) {
201 msg_pdbg("Writing %s\n", emu_persistent_image);
202 write_buf_to_file(flashchip_contents, emu_chip_size,
203 emu_persistent_image);
204 }
205 free(flashchip_contents);
206 }
207#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000208 return 0;
209}
210
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000211void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
212{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000213 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
214 __func__, descr, (unsigned long)len, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000215 return (void *)phys_addr;
216}
217
218void dummy_unmap(void *virt_addr, size_t len)
219{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000220 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
221 __func__, (unsigned long)len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000222}
223
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000224void dummy_chip_writeb(uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000225{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000226 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000227}
228
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000229void dummy_chip_writew(uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000230{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000231 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000232}
233
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000234void dummy_chip_writel(uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000235{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000236 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000237}
238
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000239void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
240{
241 size_t i;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000242 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
243 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000244 for (i = 0; i < len; i++) {
245 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000246 msg_pspew("\n");
247 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000248 }
249}
250
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000251uint8_t dummy_chip_readb(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000252{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000253 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000254 return 0xff;
255}
256
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000257uint16_t dummy_chip_readw(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000258{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000259 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000260 return 0xffff;
261}
262
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000263uint32_t dummy_chip_readl(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 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000266 return 0xffffffff;
267}
268
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000269void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
270{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000271 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
272 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000273 memset(buf, 0xff, len);
274 return;
275}
276
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000277#if EMULATE_SPI_CHIP
278static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
279 const unsigned char *writearr, unsigned char *readarr)
280{
281 int offs;
282 static int aai_offs;
283 static int aai_active = 0;
284
285 if (writecnt == 0) {
286 msg_perr("No command sent to the chip!\n");
287 return 1;
288 }
289 /* TODO: Implement command blacklists here. */
290 switch (writearr[0]) {
291 case JEDEC_RES:
292 if (emu_chip != EMULATE_ST_M25P10_RES)
293 break;
294 /* Respond with ST_M25P10_RES. */
295 if (readcnt > 0)
296 readarr[0] = 0x10;
297 break;
298 case JEDEC_REMS:
299 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
300 break;
301 /* Respond with SST_SST25VF040_REMS. */
302 if (readcnt > 0)
303 readarr[0] = 0xbf;
304 if (readcnt > 1)
305 readarr[1] = 0x44;
306 break;
307 case JEDEC_RDID:
308 if (emu_chip != EMULATE_SST_SST25VF032B)
309 break;
310 /* Respond with SST_SST25VF032B. */
311 if (readcnt > 0)
312 readarr[0] = 0xbf;
313 if (readcnt > 1)
314 readarr[1] = 0x25;
315 if (readcnt > 2)
316 readarr[2] = 0x4a;
317 break;
318 case JEDEC_RDSR:
319 memset(readarr, 0, readcnt);
320 if (aai_active)
321 memset(readarr, 1 << 6, readcnt);
322 break;
323 case JEDEC_READ:
324 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
325 /* Truncate to emu_chip_size. */
326 offs %= emu_chip_size;
327 if (readcnt > 0)
328 memcpy(readarr, flashchip_contents + offs, readcnt);
329 break;
330 case JEDEC_BYTE_PROGRAM:
331 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
332 /* Truncate to emu_chip_size. */
333 offs %= emu_chip_size;
334 if (writecnt < 5) {
335 msg_perr("BYTE PROGRAM size too short!\n");
336 return 1;
337 }
338 if (writecnt - 4 > emu_max_byteprogram_size) {
339 msg_perr("Max BYTE PROGRAM size exceeded!\n");
340 return 1;
341 }
342 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
343 break;
344 case JEDEC_AAI_WORD_PROGRAM:
345 if (!emu_max_aai_size)
346 break;
347 if (!aai_active) {
348 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
349 msg_perr("Initial AAI WORD PROGRAM size too "
350 "short!\n");
351 return 1;
352 }
353 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
354 msg_perr("Initial AAI WORD PROGRAM size too "
355 "long!\n");
356 return 1;
357 }
358 aai_active = 1;
359 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
360 writearr[3];
361 /* Truncate to emu_chip_size. */
362 aai_offs %= emu_chip_size;
363 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
364 aai_offs += 2;
365 } else {
366 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
367 msg_perr("Continuation AAI WORD PROGRAM size "
368 "too short!\n");
369 return 1;
370 }
371 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
372 msg_perr("Continuation AAI WORD PROGRAM size "
373 "too long!\n");
374 return 1;
375 }
376 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
377 aai_offs += 2;
378 }
379 break;
380 case JEDEC_WRDI:
381 if (!emu_max_aai_size)
382 break;
383 aai_active = 0;
384 break;
385 case JEDEC_SE:
386 if (!emu_jedec_se_size)
387 break;
388 if (writecnt != JEDEC_SE_OUTSIZE) {
389 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
390 return 1;
391 }
392 if (readcnt != JEDEC_SE_INSIZE) {
393 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
394 return 1;
395 }
396 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
397 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000398 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000399 offs &= ~(emu_jedec_se_size - 1);
400 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
401 break;
402 case JEDEC_BE_52:
403 if (!emu_jedec_be_52_size)
404 break;
405 if (writecnt != JEDEC_BE_52_OUTSIZE) {
406 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
407 return 1;
408 }
409 if (readcnt != JEDEC_BE_52_INSIZE) {
410 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
411 return 1;
412 }
413 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
414 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000415 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000416 offs &= ~(emu_jedec_be_52_size - 1);
417 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
418 break;
419 case JEDEC_BE_D8:
420 if (!emu_jedec_be_d8_size)
421 break;
422 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
423 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
424 return 1;
425 }
426 if (readcnt != JEDEC_BE_D8_INSIZE) {
427 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
428 return 1;
429 }
430 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
431 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000432 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000433 offs &= ~(emu_jedec_be_d8_size - 1);
434 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
435 break;
436 case JEDEC_CE_60:
437 if (!emu_jedec_ce_60_size)
438 break;
439 if (writecnt != JEDEC_CE_60_OUTSIZE) {
440 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
441 return 1;
442 }
443 if (readcnt != JEDEC_CE_60_INSIZE) {
444 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
445 return 1;
446 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000447 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000448 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000449 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000450 break;
451 case JEDEC_CE_C7:
452 if (!emu_jedec_ce_c7_size)
453 break;
454 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
455 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
456 return 1;
457 }
458 if (readcnt != JEDEC_CE_C7_INSIZE) {
459 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
460 return 1;
461 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000462 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000463 /* emu_jedec_ce_c7_size is emu_chip_size. */
464 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
465 break;
466 default:
467 /* No special response. */
468 break;
469 }
470 return 0;
471}
472#endif
473
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000474int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000475 const unsigned char *writearr, unsigned char *readarr)
476{
477 int i;
478
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000479 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000480
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000481 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000482 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000483 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000484
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000485 /* Response for unknown commands and missing chip is 0xff. */
486 memset(readarr, 0xff, readcnt);
487#if EMULATE_SPI_CHIP
488 switch (emu_chip) {
489 case EMULATE_ST_M25P10_RES:
490 case EMULATE_SST_SST25VF040_REMS:
491 case EMULATE_SST_SST25VF032B:
492 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
493 readarr)) {
494 msg_perr("Invalid command sent to flash chip!\n");
495 return 1;
496 }
497 break;
498 default:
499 break;
500 }
501#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000502 msg_pspew(" reading %u bytes:", readcnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000503 for (i = 0; i < readcnt; i++) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000504 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000505 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000506 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000507 return 0;
508}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000509
510int dummy_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
511{
512 /* Maximum read length is unlimited, use 64kB. */
513 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
514}
515
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000516int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
517{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000518 return spi_write_chunked(flash, buf, start, len,
519 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000520}