blob: 21ee9b4def0ffc2cd17b08e2759f24ab387582d7 [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 Hailfinger3504b532009-06-01 00:02:11 +000022#include <ctype.h>
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000023#include "flash.h"
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +000024#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000025#include "programmer.h"
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000026
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000027/* Remove the #define below if you don't want SPI flash chip emulation. */
28#define EMULATE_SPI_CHIP 1
29
30#if EMULATE_SPI_CHIP
31#define EMULATE_CHIP 1
32#include "spi.h"
33#endif
34
35#if EMULATE_CHIP
36#include <sys/types.h>
37#include <sys/stat.h>
38#endif
39
40#if EMULATE_CHIP
41static uint8_t *flashchip_contents = NULL;
42enum emu_chip {
43 EMULATE_NONE,
44 EMULATE_ST_M25P10_RES,
45 EMULATE_SST_SST25VF040_REMS,
46 EMULATE_SST_SST25VF032B,
47};
48static enum emu_chip emu_chip = EMULATE_NONE;
49static char *emu_persistent_image = NULL;
50static int emu_chip_size = 0;
51#if EMULATE_SPI_CHIP
52static int emu_max_byteprogram_size = 0;
53static int emu_max_aai_size = 0;
54static int emu_jedec_se_size = 0;
55static int emu_jedec_be_52_size = 0;
56static int emu_jedec_be_d8_size = 0;
57static int emu_jedec_ce_60_size = 0;
58static int emu_jedec_ce_c7_size = 0;
59#endif
60#endif
61
62static int spi_write_256_chunksize = 256;
63
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000064static void tolower_string(char *str)
65{
66 for (; *str != '\0'; str++)
67 *str = (char)tolower((unsigned char)*str);
68}
69
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000070int dummy_init(void)
71{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000072 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000073 char *tmp = NULL;
74#if EMULATE_CHIP
75 struct stat image_stat;
76#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000077
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000078 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000079
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +000080 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000081 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
82 if (!bustext)
83 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000084 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000085 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000086
87 buses_supported = CHIP_BUSTYPE_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000088 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000089 buses_supported |= CHIP_BUSTYPE_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000090 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000091 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000092 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000093 buses_supported |= CHIP_BUSTYPE_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000094 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000095 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000096 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000097 buses_supported |= CHIP_BUSTYPE_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +000098 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +000099 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000100 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000101 buses_supported |= CHIP_BUSTYPE_SPI;
102 spi_controller = SPI_CONTROLLER_DUMMY;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000103 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000104 }
105 if (buses_supported == CHIP_BUSTYPE_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000106 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000107 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000108
109 tmp = extract_programmer_param("spi_write_256_chunksize");
110 if (tmp) {
111 spi_write_256_chunksize = atoi(tmp);
112 free(tmp);
113 if (spi_write_256_chunksize < 1) {
114 msg_perr("invalid spi_write_256_chunksize\n");
115 return 1;
116 }
117 }
118
119#if EMULATE_CHIP
120 tmp = extract_programmer_param("emulate");
121 if (!tmp) {
122 msg_pdbg("Not emulating any flash chip.\n");
123 /* Nothing else to do. */
124 return 0;
125 }
126#if EMULATE_SPI_CHIP
127 if (!strcmp(tmp, "M25P10.RES")) {
128 emu_chip = EMULATE_ST_M25P10_RES;
129 emu_chip_size = 128 * 1024;
130 emu_max_byteprogram_size = 128;
131 emu_max_aai_size = 0;
132 emu_jedec_se_size = 0;
133 emu_jedec_be_52_size = 0;
134 emu_jedec_be_d8_size = 32 * 1024;
135 emu_jedec_ce_60_size = 0;
136 emu_jedec_ce_c7_size = emu_chip_size;
137 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
138 "write)\n");
139 }
140 if (!strcmp(tmp, "SST25VF040.REMS")) {
141 emu_chip = EMULATE_SST_SST25VF040_REMS;
142 emu_chip_size = 512 * 1024;
143 emu_max_byteprogram_size = 1;
144 emu_max_aai_size = 0;
145 emu_jedec_se_size = 4 * 1024;
146 emu_jedec_be_52_size = 32 * 1024;
147 emu_jedec_be_d8_size = 0;
148 emu_jedec_ce_60_size = emu_chip_size;
149 emu_jedec_ce_c7_size = 0;
150 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
151 "byte write)\n");
152 }
153 if (!strcmp(tmp, "SST25VF032B")) {
154 emu_chip = EMULATE_SST_SST25VF032B;
155 emu_chip_size = 4 * 1024 * 1024;
156 emu_max_byteprogram_size = 1;
157 emu_max_aai_size = 2;
158 emu_jedec_se_size = 4 * 1024;
159 emu_jedec_be_52_size = 32 * 1024;
160 emu_jedec_be_d8_size = 64 * 1024;
161 emu_jedec_ce_60_size = emu_chip_size;
162 emu_jedec_ce_c7_size = emu_chip_size;
163 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
164 "write)\n");
165 }
166#endif
167 if (emu_chip == EMULATE_NONE) {
168 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
169 free(tmp);
170 return 1;
171 }
172 free(tmp);
173 flashchip_contents = malloc(emu_chip_size);
174 if (!flashchip_contents) {
175 msg_perr("Out of memory!\n");
176 return 1;
177 }
178 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
179 memset(flashchip_contents, 0xff, emu_chip_size);
180
181 emu_persistent_image = extract_programmer_param("image");
182 if (!emu_persistent_image) {
183 /* Nothing else to do. */
184 return 0;
185 }
186 if (!stat(emu_persistent_image, &image_stat)) {
187 msg_pdbg("Found persistent image %s, size %li ",
188 emu_persistent_image, (long)image_stat.st_size);
189 if (image_stat.st_size == emu_chip_size) {
190 msg_pdbg("matches.\n");
191 msg_pdbg("Reading %s\n", emu_persistent_image);
192 read_buf_from_file(flashchip_contents, emu_chip_size,
193 emu_persistent_image);
194 } else {
195 msg_pdbg("doesn't match.\n");
196 }
197 }
198#endif
Uwe Hermanne9d04d42009-06-02 19:54:22 +0000199 return 0;
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000200}
201
202int dummy_shutdown(void)
203{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000204 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000205#if EMULATE_CHIP
206 if (emu_chip != EMULATE_NONE) {
207 if (emu_persistent_image) {
208 msg_pdbg("Writing %s\n", emu_persistent_image);
209 write_buf_to_file(flashchip_contents, emu_chip_size,
210 emu_persistent_image);
211 }
212 free(flashchip_contents);
213 }
214#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000215 return 0;
216}
217
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000218void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
219{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000220 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
221 __func__, descr, (unsigned long)len, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000222 return (void *)phys_addr;
223}
224
225void dummy_unmap(void *virt_addr, size_t len)
226{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000227 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
228 __func__, (unsigned long)len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000229}
230
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000231void dummy_chip_writeb(uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000232{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000233 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000234}
235
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000236void dummy_chip_writew(uint16_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%04x\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_writel(uint32_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%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000244}
245
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000246void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
247{
248 size_t i;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000249 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
250 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000251 for (i = 0; i < len; i++) {
252 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000253 msg_pspew("\n");
254 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000255 }
256}
257
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000258uint8_t dummy_chip_readb(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000259{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000260 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000261 return 0xff;
262}
263
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000264uint16_t dummy_chip_readw(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000265{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000266 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000267 return 0xffff;
268}
269
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000270uint32_t dummy_chip_readl(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000271{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000272 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000273 return 0xffffffff;
274}
275
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000276void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
277{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000278 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
279 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000280 memset(buf, 0xff, len);
281 return;
282}
283
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000284#if EMULATE_SPI_CHIP
285static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
286 const unsigned char *writearr, unsigned char *readarr)
287{
288 int offs;
289 static int aai_offs;
290 static int aai_active = 0;
291
292 if (writecnt == 0) {
293 msg_perr("No command sent to the chip!\n");
294 return 1;
295 }
296 /* TODO: Implement command blacklists here. */
297 switch (writearr[0]) {
298 case JEDEC_RES:
299 if (emu_chip != EMULATE_ST_M25P10_RES)
300 break;
301 /* Respond with ST_M25P10_RES. */
302 if (readcnt > 0)
303 readarr[0] = 0x10;
304 break;
305 case JEDEC_REMS:
306 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
307 break;
308 /* Respond with SST_SST25VF040_REMS. */
309 if (readcnt > 0)
310 readarr[0] = 0xbf;
311 if (readcnt > 1)
312 readarr[1] = 0x44;
313 break;
314 case JEDEC_RDID:
315 if (emu_chip != EMULATE_SST_SST25VF032B)
316 break;
317 /* Respond with SST_SST25VF032B. */
318 if (readcnt > 0)
319 readarr[0] = 0xbf;
320 if (readcnt > 1)
321 readarr[1] = 0x25;
322 if (readcnt > 2)
323 readarr[2] = 0x4a;
324 break;
325 case JEDEC_RDSR:
326 memset(readarr, 0, readcnt);
327 if (aai_active)
328 memset(readarr, 1 << 6, readcnt);
329 break;
330 case JEDEC_READ:
331 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
332 /* Truncate to emu_chip_size. */
333 offs %= emu_chip_size;
334 if (readcnt > 0)
335 memcpy(readarr, flashchip_contents + offs, readcnt);
336 break;
337 case JEDEC_BYTE_PROGRAM:
338 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
339 /* Truncate to emu_chip_size. */
340 offs %= emu_chip_size;
341 if (writecnt < 5) {
342 msg_perr("BYTE PROGRAM size too short!\n");
343 return 1;
344 }
345 if (writecnt - 4 > emu_max_byteprogram_size) {
346 msg_perr("Max BYTE PROGRAM size exceeded!\n");
347 return 1;
348 }
349 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
350 break;
351 case JEDEC_AAI_WORD_PROGRAM:
352 if (!emu_max_aai_size)
353 break;
354 if (!aai_active) {
355 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
356 msg_perr("Initial AAI WORD PROGRAM size too "
357 "short!\n");
358 return 1;
359 }
360 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
361 msg_perr("Initial AAI WORD PROGRAM size too "
362 "long!\n");
363 return 1;
364 }
365 aai_active = 1;
366 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
367 writearr[3];
368 /* Truncate to emu_chip_size. */
369 aai_offs %= emu_chip_size;
370 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
371 aai_offs += 2;
372 } else {
373 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
374 msg_perr("Continuation AAI WORD PROGRAM size "
375 "too short!\n");
376 return 1;
377 }
378 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
379 msg_perr("Continuation AAI WORD PROGRAM size "
380 "too long!\n");
381 return 1;
382 }
383 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
384 aai_offs += 2;
385 }
386 break;
387 case JEDEC_WRDI:
388 if (!emu_max_aai_size)
389 break;
390 aai_active = 0;
391 break;
392 case JEDEC_SE:
393 if (!emu_jedec_se_size)
394 break;
395 if (writecnt != JEDEC_SE_OUTSIZE) {
396 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
397 return 1;
398 }
399 if (readcnt != JEDEC_SE_INSIZE) {
400 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
401 return 1;
402 }
403 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
404 if (offs & (emu_jedec_se_size - 1))
405 msg_pdbg("Unaligned SECTOR ERASE 0x20\n");
406 offs &= ~(emu_jedec_se_size - 1);
407 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
408 break;
409 case JEDEC_BE_52:
410 if (!emu_jedec_be_52_size)
411 break;
412 if (writecnt != JEDEC_BE_52_OUTSIZE) {
413 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
414 return 1;
415 }
416 if (readcnt != JEDEC_BE_52_INSIZE) {
417 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
418 return 1;
419 }
420 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
421 if (offs & (emu_jedec_be_52_size - 1))
422 msg_pdbg("Unaligned BLOCK ERASE 0x52\n");
423 offs &= ~(emu_jedec_be_52_size - 1);
424 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
425 break;
426 case JEDEC_BE_D8:
427 if (!emu_jedec_be_d8_size)
428 break;
429 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
430 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
431 return 1;
432 }
433 if (readcnt != JEDEC_BE_D8_INSIZE) {
434 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
435 return 1;
436 }
437 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
438 if (offs & (emu_jedec_be_d8_size - 1))
439 msg_pdbg("Unaligned BLOCK ERASE 0xd8\n");
440 offs &= ~(emu_jedec_be_d8_size - 1);
441 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
442 break;
443 case JEDEC_CE_60:
444 if (!emu_jedec_ce_60_size)
445 break;
446 if (writecnt != JEDEC_CE_60_OUTSIZE) {
447 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
448 return 1;
449 }
450 if (readcnt != JEDEC_CE_60_INSIZE) {
451 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
452 return 1;
453 }
454 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
455 if (offs & (emu_jedec_ce_60_size - 1))
456 msg_pdbg("Unaligned CHIP ERASE 0x60\n");
457 offs &= ~(emu_jedec_ce_60_size - 1);
458 /* emu_jedec_ce_60_size is emu_chip_size. */
459 memset(flashchip_contents + offs, 0xff, emu_jedec_ce_60_size);
460 break;
461 case JEDEC_CE_C7:
462 if (!emu_jedec_ce_c7_size)
463 break;
464 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
465 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
466 return 1;
467 }
468 if (readcnt != JEDEC_CE_C7_INSIZE) {
469 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
470 return 1;
471 }
472 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
473 if (offs & (emu_jedec_ce_c7_size - 1))
474 msg_pdbg("Unaligned CHIP ERASE 0xc7\n");
475 offs &= ~(emu_jedec_ce_c7_size - 1);
476 /* emu_jedec_ce_c7_size is emu_chip_size. */
477 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
478 break;
479 default:
480 /* No special response. */
481 break;
482 }
483 return 0;
484}
485#endif
486
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000487int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000488 const unsigned char *writearr, unsigned char *readarr)
489{
490 int i;
491
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000492 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000493
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000494 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000495 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000496 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000497
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000498 /* Response for unknown commands and missing chip is 0xff. */
499 memset(readarr, 0xff, readcnt);
500#if EMULATE_SPI_CHIP
501 switch (emu_chip) {
502 case EMULATE_ST_M25P10_RES:
503 case EMULATE_SST_SST25VF040_REMS:
504 case EMULATE_SST_SST25VF032B:
505 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
506 readarr)) {
507 msg_perr("Invalid command sent to flash chip!\n");
508 return 1;
509 }
510 break;
511 default:
512 break;
513 }
514#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000515 msg_pspew(" reading %u bytes:", readcnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000516 for (i = 0; i < readcnt; i++) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000517 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000518 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000519 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000520 return 0;
521}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000522
523int dummy_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
524{
525 /* Maximum read length is unlimited, use 64kB. */
526 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
527}
528
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000529int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
530{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000531 return spi_write_chunked(flash, buf, start, len,
532 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000533}