blob: fca228cfb0163d35b5eafbcd1513cf374f282ff2 [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};
David Hendricks8bb20212011-06-14 01:35:36 +000076
77static int dummy_shutdown(void *data)
78{
79 msg_pspew("%s\n", __func__);
80#if EMULATE_CHIP
81 if (emu_chip != EMULATE_NONE) {
82 if (emu_persistent_image) {
83 msg_pdbg("Writing %s\n", emu_persistent_image);
84 write_buf_to_file(flashchip_contents, emu_chip_size,
85 emu_persistent_image);
86 }
87 free(flashchip_contents);
88 }
89#endif
90 return 0;
91}
92
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000093int dummy_init(void)
94{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000095 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000096 char *tmp = NULL;
97#if EMULATE_CHIP
98 struct stat image_stat;
99#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000100
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000101 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000102
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000103 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000104 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
105 if (!bustext)
106 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000107 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000108 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000109
110 buses_supported = CHIP_BUSTYPE_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000111 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000112 buses_supported |= CHIP_BUSTYPE_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000113 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000114 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000115 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000116 buses_supported |= CHIP_BUSTYPE_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000117 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000118 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000119 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000120 buses_supported |= CHIP_BUSTYPE_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000121 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000122 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000123 if (strstr(bustext, "spi")) {
Michael Karcherb9dbe482011-05-11 17:07:07 +0000124 register_spi_programmer(&spi_programmer_dummyflasher);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000125 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000126 }
127 if (buses_supported == CHIP_BUSTYPE_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000128 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000129 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000130
131 tmp = extract_programmer_param("spi_write_256_chunksize");
132 if (tmp) {
133 spi_write_256_chunksize = atoi(tmp);
134 free(tmp);
135 if (spi_write_256_chunksize < 1) {
136 msg_perr("invalid spi_write_256_chunksize\n");
137 return 1;
138 }
139 }
140
141#if EMULATE_CHIP
142 tmp = extract_programmer_param("emulate");
143 if (!tmp) {
144 msg_pdbg("Not emulating any flash chip.\n");
145 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000146 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000147 }
148#if EMULATE_SPI_CHIP
149 if (!strcmp(tmp, "M25P10.RES")) {
150 emu_chip = EMULATE_ST_M25P10_RES;
151 emu_chip_size = 128 * 1024;
152 emu_max_byteprogram_size = 128;
153 emu_max_aai_size = 0;
154 emu_jedec_se_size = 0;
155 emu_jedec_be_52_size = 0;
156 emu_jedec_be_d8_size = 32 * 1024;
157 emu_jedec_ce_60_size = 0;
158 emu_jedec_ce_c7_size = emu_chip_size;
159 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
160 "write)\n");
161 }
162 if (!strcmp(tmp, "SST25VF040.REMS")) {
163 emu_chip = EMULATE_SST_SST25VF040_REMS;
164 emu_chip_size = 512 * 1024;
165 emu_max_byteprogram_size = 1;
166 emu_max_aai_size = 0;
167 emu_jedec_se_size = 4 * 1024;
168 emu_jedec_be_52_size = 32 * 1024;
169 emu_jedec_be_d8_size = 0;
170 emu_jedec_ce_60_size = emu_chip_size;
171 emu_jedec_ce_c7_size = 0;
172 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
173 "byte write)\n");
174 }
175 if (!strcmp(tmp, "SST25VF032B")) {
176 emu_chip = EMULATE_SST_SST25VF032B;
177 emu_chip_size = 4 * 1024 * 1024;
178 emu_max_byteprogram_size = 1;
179 emu_max_aai_size = 2;
180 emu_jedec_se_size = 4 * 1024;
181 emu_jedec_be_52_size = 32 * 1024;
182 emu_jedec_be_d8_size = 64 * 1024;
183 emu_jedec_ce_60_size = emu_chip_size;
184 emu_jedec_ce_c7_size = emu_chip_size;
185 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
186 "write)\n");
187 }
188#endif
189 if (emu_chip == EMULATE_NONE) {
190 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
191 free(tmp);
192 return 1;
193 }
194 free(tmp);
195 flashchip_contents = malloc(emu_chip_size);
196 if (!flashchip_contents) {
197 msg_perr("Out of memory!\n");
198 return 1;
199 }
David Hendricks8bb20212011-06-14 01:35:36 +0000200
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000201 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
202 memset(flashchip_contents, 0xff, emu_chip_size);
203
204 emu_persistent_image = extract_programmer_param("image");
205 if (!emu_persistent_image) {
206 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000207 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000208 }
209 if (!stat(emu_persistent_image, &image_stat)) {
210 msg_pdbg("Found persistent image %s, size %li ",
211 emu_persistent_image, (long)image_stat.st_size);
212 if (image_stat.st_size == emu_chip_size) {
213 msg_pdbg("matches.\n");
214 msg_pdbg("Reading %s\n", emu_persistent_image);
215 read_buf_from_file(flashchip_contents, emu_chip_size,
216 emu_persistent_image);
217 } else {
218 msg_pdbg("doesn't match.\n");
219 }
220 }
221#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000222
David Hendricks8bb20212011-06-14 01:35:36 +0000223dummy_init_out:
224 if (register_shutdown(dummy_shutdown, NULL)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000225 free(flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000226 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000227 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000228 return 0;
229}
230
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000231void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
232{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000233 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
234 __func__, descr, (unsigned long)len, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000235 return (void *)phys_addr;
236}
237
238void dummy_unmap(void *virt_addr, size_t len)
239{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000240 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
241 __func__, (unsigned long)len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000242}
243
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000244void dummy_chip_writeb(uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000245{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000246 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000247}
248
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000249void dummy_chip_writew(uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000250{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000251 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000252}
253
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000254void dummy_chip_writel(uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000255{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000256 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000257}
258
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000259void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
260{
261 size_t i;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000262 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
263 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000264 for (i = 0; i < len; i++) {
265 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000266 msg_pspew("\n");
267 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000268 }
269}
270
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000271uint8_t dummy_chip_readb(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000272{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000273 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000274 return 0xff;
275}
276
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000277uint16_t dummy_chip_readw(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000278{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000279 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000280 return 0xffff;
281}
282
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000283uint32_t dummy_chip_readl(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000284{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000285 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000286 return 0xffffffff;
287}
288
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000289void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
290{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000291 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
292 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000293 memset(buf, 0xff, len);
294 return;
295}
296
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000297#if EMULATE_SPI_CHIP
298static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
299 const unsigned char *writearr, unsigned char *readarr)
300{
301 int offs;
302 static int aai_offs;
303 static int aai_active = 0;
304
305 if (writecnt == 0) {
306 msg_perr("No command sent to the chip!\n");
307 return 1;
308 }
309 /* TODO: Implement command blacklists here. */
310 switch (writearr[0]) {
311 case JEDEC_RES:
312 if (emu_chip != EMULATE_ST_M25P10_RES)
313 break;
314 /* Respond with ST_M25P10_RES. */
315 if (readcnt > 0)
316 readarr[0] = 0x10;
317 break;
318 case JEDEC_REMS:
319 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
320 break;
321 /* Respond with SST_SST25VF040_REMS. */
322 if (readcnt > 0)
323 readarr[0] = 0xbf;
324 if (readcnt > 1)
325 readarr[1] = 0x44;
326 break;
327 case JEDEC_RDID:
328 if (emu_chip != EMULATE_SST_SST25VF032B)
329 break;
330 /* Respond with SST_SST25VF032B. */
331 if (readcnt > 0)
332 readarr[0] = 0xbf;
333 if (readcnt > 1)
334 readarr[1] = 0x25;
335 if (readcnt > 2)
336 readarr[2] = 0x4a;
337 break;
338 case JEDEC_RDSR:
339 memset(readarr, 0, readcnt);
340 if (aai_active)
341 memset(readarr, 1 << 6, readcnt);
342 break;
343 case JEDEC_READ:
344 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
345 /* Truncate to emu_chip_size. */
346 offs %= emu_chip_size;
347 if (readcnt > 0)
348 memcpy(readarr, flashchip_contents + offs, readcnt);
349 break;
350 case JEDEC_BYTE_PROGRAM:
351 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
352 /* Truncate to emu_chip_size. */
353 offs %= emu_chip_size;
354 if (writecnt < 5) {
355 msg_perr("BYTE PROGRAM size too short!\n");
356 return 1;
357 }
358 if (writecnt - 4 > emu_max_byteprogram_size) {
359 msg_perr("Max BYTE PROGRAM size exceeded!\n");
360 return 1;
361 }
362 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
363 break;
364 case JEDEC_AAI_WORD_PROGRAM:
365 if (!emu_max_aai_size)
366 break;
367 if (!aai_active) {
368 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
369 msg_perr("Initial AAI WORD PROGRAM size too "
370 "short!\n");
371 return 1;
372 }
373 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
374 msg_perr("Initial AAI WORD PROGRAM size too "
375 "long!\n");
376 return 1;
377 }
378 aai_active = 1;
379 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
380 writearr[3];
381 /* Truncate to emu_chip_size. */
382 aai_offs %= emu_chip_size;
383 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
384 aai_offs += 2;
385 } else {
386 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
387 msg_perr("Continuation AAI WORD PROGRAM size "
388 "too short!\n");
389 return 1;
390 }
391 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
392 msg_perr("Continuation AAI WORD PROGRAM size "
393 "too long!\n");
394 return 1;
395 }
396 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
397 aai_offs += 2;
398 }
399 break;
400 case JEDEC_WRDI:
401 if (!emu_max_aai_size)
402 break;
403 aai_active = 0;
404 break;
405 case JEDEC_SE:
406 if (!emu_jedec_se_size)
407 break;
408 if (writecnt != JEDEC_SE_OUTSIZE) {
409 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
410 return 1;
411 }
412 if (readcnt != JEDEC_SE_INSIZE) {
413 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
414 return 1;
415 }
416 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
417 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000418 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000419 offs &= ~(emu_jedec_se_size - 1);
420 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
421 break;
422 case JEDEC_BE_52:
423 if (!emu_jedec_be_52_size)
424 break;
425 if (writecnt != JEDEC_BE_52_OUTSIZE) {
426 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
427 return 1;
428 }
429 if (readcnt != JEDEC_BE_52_INSIZE) {
430 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
431 return 1;
432 }
433 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
434 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000435 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000436 offs &= ~(emu_jedec_be_52_size - 1);
437 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
438 break;
439 case JEDEC_BE_D8:
440 if (!emu_jedec_be_d8_size)
441 break;
442 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
443 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
444 return 1;
445 }
446 if (readcnt != JEDEC_BE_D8_INSIZE) {
447 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
448 return 1;
449 }
450 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
451 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000452 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000453 offs &= ~(emu_jedec_be_d8_size - 1);
454 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
455 break;
456 case JEDEC_CE_60:
457 if (!emu_jedec_ce_60_size)
458 break;
459 if (writecnt != JEDEC_CE_60_OUTSIZE) {
460 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
461 return 1;
462 }
463 if (readcnt != JEDEC_CE_60_INSIZE) {
464 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
465 return 1;
466 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000467 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000468 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000469 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000470 break;
471 case JEDEC_CE_C7:
472 if (!emu_jedec_ce_c7_size)
473 break;
474 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
475 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
476 return 1;
477 }
478 if (readcnt != JEDEC_CE_C7_INSIZE) {
479 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
480 return 1;
481 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000482 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000483 /* emu_jedec_ce_c7_size is emu_chip_size. */
484 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
485 break;
486 default:
487 /* No special response. */
488 break;
489 }
490 return 0;
491}
492#endif
493
Michael Karcherb9dbe482011-05-11 17:07:07 +0000494static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000495 const unsigned char *writearr, unsigned char *readarr)
496{
497 int i;
498
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000499 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000500
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000501 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000502 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000503 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000504
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000505 /* Response for unknown commands and missing chip is 0xff. */
506 memset(readarr, 0xff, readcnt);
507#if EMULATE_SPI_CHIP
508 switch (emu_chip) {
509 case EMULATE_ST_M25P10_RES:
510 case EMULATE_SST_SST25VF040_REMS:
511 case EMULATE_SST_SST25VF032B:
512 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
513 readarr)) {
514 msg_perr("Invalid command sent to flash chip!\n");
515 return 1;
516 }
517 break;
518 default:
519 break;
520 }
521#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000522 msg_pspew(" reading %u bytes:", readcnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000523 for (i = 0; i < readcnt; i++) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000524 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000525 }
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000526 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000527 return 0;
528}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000529
Michael Karcherb9dbe482011-05-11 17:07:07 +0000530static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000531{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000532 return spi_write_chunked(flash, buf, start, len,
533 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000534}