blob: be8f55211d4c3ce13f7336848bab54dce2eae87b [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);
Uwe Hermann91f4afa2011-07-28 08:13:25 +000065static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
Stefan Tauner8c357452011-09-18 22:42:18 +000066 int start, int len);
Michael Karcherb9dbe482011-05-11 17:07:07 +000067
68static const struct spi_programmer spi_programmer_dummyflasher = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +000069 .type = SPI_CONTROLLER_DUMMY,
70 .max_data_read = MAX_DATA_READ_UNLIMITED,
71 .max_data_write = MAX_DATA_UNSPECIFIED,
72 .command = dummy_spi_send_command,
73 .multicommand = default_spi_send_multicommand,
74 .read = default_spi_read,
75 .write_256 = dummy_spi_write_256,
Michael Karcherb9dbe482011-05-11 17:07:07 +000076};
David Hendricks8bb20212011-06-14 01:35:36 +000077
78static int dummy_shutdown(void *data)
79{
80 msg_pspew("%s\n", __func__);
81#if EMULATE_CHIP
82 if (emu_chip != EMULATE_NONE) {
83 if (emu_persistent_image) {
84 msg_pdbg("Writing %s\n", emu_persistent_image);
85 write_buf_to_file(flashchip_contents, emu_chip_size,
86 emu_persistent_image);
87 }
88 free(flashchip_contents);
89 }
90#endif
91 return 0;
92}
93
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000094int dummy_init(void)
95{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000096 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +000097 char *tmp = NULL;
98#if EMULATE_CHIP
99 struct stat image_stat;
100#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000101
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000102 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000103
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000104 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000105 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
106 if (!bustext)
107 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000108 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000109 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000110
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000111 buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000112 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000113 buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000114 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000115 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000116 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000117 buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000118 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000119 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000120 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000121 buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000122 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000123 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000124 if (strstr(bustext, "spi")) {
Michael Karcherb9dbe482011-05-11 17:07:07 +0000125 register_spi_programmer(&spi_programmer_dummyflasher);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000126 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000127 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000128 if (buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000129 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000130 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000131
132 tmp = extract_programmer_param("spi_write_256_chunksize");
133 if (tmp) {
134 spi_write_256_chunksize = atoi(tmp);
135 free(tmp);
136 if (spi_write_256_chunksize < 1) {
137 msg_perr("invalid spi_write_256_chunksize\n");
138 return 1;
139 }
140 }
141
142#if EMULATE_CHIP
143 tmp = extract_programmer_param("emulate");
144 if (!tmp) {
145 msg_pdbg("Not emulating any flash chip.\n");
146 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000147 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000148 }
149#if EMULATE_SPI_CHIP
150 if (!strcmp(tmp, "M25P10.RES")) {
151 emu_chip = EMULATE_ST_M25P10_RES;
152 emu_chip_size = 128 * 1024;
153 emu_max_byteprogram_size = 128;
154 emu_max_aai_size = 0;
155 emu_jedec_se_size = 0;
156 emu_jedec_be_52_size = 0;
157 emu_jedec_be_d8_size = 32 * 1024;
158 emu_jedec_ce_60_size = 0;
159 emu_jedec_ce_c7_size = emu_chip_size;
160 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
161 "write)\n");
162 }
163 if (!strcmp(tmp, "SST25VF040.REMS")) {
164 emu_chip = EMULATE_SST_SST25VF040_REMS;
165 emu_chip_size = 512 * 1024;
166 emu_max_byteprogram_size = 1;
167 emu_max_aai_size = 0;
168 emu_jedec_se_size = 4 * 1024;
169 emu_jedec_be_52_size = 32 * 1024;
170 emu_jedec_be_d8_size = 0;
171 emu_jedec_ce_60_size = emu_chip_size;
172 emu_jedec_ce_c7_size = 0;
173 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
174 "byte write)\n");
175 }
176 if (!strcmp(tmp, "SST25VF032B")) {
177 emu_chip = EMULATE_SST_SST25VF032B;
178 emu_chip_size = 4 * 1024 * 1024;
179 emu_max_byteprogram_size = 1;
180 emu_max_aai_size = 2;
181 emu_jedec_se_size = 4 * 1024;
182 emu_jedec_be_52_size = 32 * 1024;
183 emu_jedec_be_d8_size = 64 * 1024;
184 emu_jedec_ce_60_size = emu_chip_size;
185 emu_jedec_ce_c7_size = emu_chip_size;
186 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
187 "write)\n");
188 }
189#endif
190 if (emu_chip == EMULATE_NONE) {
191 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
192 free(tmp);
193 return 1;
194 }
195 free(tmp);
196 flashchip_contents = malloc(emu_chip_size);
197 if (!flashchip_contents) {
198 msg_perr("Out of memory!\n");
199 return 1;
200 }
David Hendricks8bb20212011-06-14 01:35:36 +0000201
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000202 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
203 memset(flashchip_contents, 0xff, emu_chip_size);
204
205 emu_persistent_image = extract_programmer_param("image");
206 if (!emu_persistent_image) {
207 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000208 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000209 }
210 if (!stat(emu_persistent_image, &image_stat)) {
211 msg_pdbg("Found persistent image %s, size %li ",
212 emu_persistent_image, (long)image_stat.st_size);
213 if (image_stat.st_size == emu_chip_size) {
214 msg_pdbg("matches.\n");
215 msg_pdbg("Reading %s\n", emu_persistent_image);
216 read_buf_from_file(flashchip_contents, emu_chip_size,
217 emu_persistent_image);
218 } else {
219 msg_pdbg("doesn't match.\n");
220 }
221 }
222#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000223
David Hendricks8bb20212011-06-14 01:35:36 +0000224dummy_init_out:
225 if (register_shutdown(dummy_shutdown, NULL)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000226 free(flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000227 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000228 }
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000229 return 0;
230}
231
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000232void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
233{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000234 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
235 __func__, descr, (unsigned long)len, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000236 return (void *)phys_addr;
237}
238
239void dummy_unmap(void *virt_addr, size_t len)
240{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000241 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
242 __func__, (unsigned long)len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000243}
244
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000245void dummy_chip_writeb(uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000246{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000247 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000248}
249
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000250void dummy_chip_writew(uint16_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000251{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000252 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000253}
254
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000255void dummy_chip_writel(uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000256{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000257 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000258}
259
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000260void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
261{
262 size_t i;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000263 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
264 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000265 for (i = 0; i < len; i++) {
266 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000267 msg_pspew("\n");
268 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000269 }
270}
271
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000272uint8_t dummy_chip_readb(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000273{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000274 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000275 return 0xff;
276}
277
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000278uint16_t dummy_chip_readw(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000279{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000280 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000281 return 0xffff;
282}
283
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000284uint32_t dummy_chip_readl(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000285{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000286 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000287 return 0xffffffff;
288}
289
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000290void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
291{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000292 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
293 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000294 memset(buf, 0xff, len);
295 return;
296}
297
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000298#if EMULATE_SPI_CHIP
299static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
300 const unsigned char *writearr, unsigned char *readarr)
301{
302 int offs;
303 static int aai_offs;
304 static int aai_active = 0;
305
306 if (writecnt == 0) {
307 msg_perr("No command sent to the chip!\n");
308 return 1;
309 }
310 /* TODO: Implement command blacklists here. */
311 switch (writearr[0]) {
312 case JEDEC_RES:
313 if (emu_chip != EMULATE_ST_M25P10_RES)
314 break;
315 /* Respond with ST_M25P10_RES. */
316 if (readcnt > 0)
317 readarr[0] = 0x10;
318 break;
319 case JEDEC_REMS:
320 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
321 break;
322 /* Respond with SST_SST25VF040_REMS. */
323 if (readcnt > 0)
324 readarr[0] = 0xbf;
325 if (readcnt > 1)
326 readarr[1] = 0x44;
327 break;
328 case JEDEC_RDID:
329 if (emu_chip != EMULATE_SST_SST25VF032B)
330 break;
331 /* Respond with SST_SST25VF032B. */
332 if (readcnt > 0)
333 readarr[0] = 0xbf;
334 if (readcnt > 1)
335 readarr[1] = 0x25;
336 if (readcnt > 2)
337 readarr[2] = 0x4a;
338 break;
339 case JEDEC_RDSR:
340 memset(readarr, 0, readcnt);
341 if (aai_active)
342 memset(readarr, 1 << 6, readcnt);
343 break;
344 case JEDEC_READ:
345 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
346 /* Truncate to emu_chip_size. */
347 offs %= emu_chip_size;
348 if (readcnt > 0)
349 memcpy(readarr, flashchip_contents + offs, readcnt);
350 break;
351 case JEDEC_BYTE_PROGRAM:
352 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
353 /* Truncate to emu_chip_size. */
354 offs %= emu_chip_size;
355 if (writecnt < 5) {
356 msg_perr("BYTE PROGRAM size too short!\n");
357 return 1;
358 }
359 if (writecnt - 4 > emu_max_byteprogram_size) {
360 msg_perr("Max BYTE PROGRAM size exceeded!\n");
361 return 1;
362 }
363 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
364 break;
365 case JEDEC_AAI_WORD_PROGRAM:
366 if (!emu_max_aai_size)
367 break;
368 if (!aai_active) {
369 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
370 msg_perr("Initial AAI WORD PROGRAM size too "
371 "short!\n");
372 return 1;
373 }
374 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
375 msg_perr("Initial AAI WORD PROGRAM size too "
376 "long!\n");
377 return 1;
378 }
379 aai_active = 1;
380 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
381 writearr[3];
382 /* Truncate to emu_chip_size. */
383 aai_offs %= emu_chip_size;
384 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
385 aai_offs += 2;
386 } else {
387 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
388 msg_perr("Continuation AAI WORD PROGRAM size "
389 "too short!\n");
390 return 1;
391 }
392 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
393 msg_perr("Continuation AAI WORD PROGRAM size "
394 "too long!\n");
395 return 1;
396 }
397 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
398 aai_offs += 2;
399 }
400 break;
401 case JEDEC_WRDI:
402 if (!emu_max_aai_size)
403 break;
404 aai_active = 0;
405 break;
406 case JEDEC_SE:
407 if (!emu_jedec_se_size)
408 break;
409 if (writecnt != JEDEC_SE_OUTSIZE) {
410 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
411 return 1;
412 }
413 if (readcnt != JEDEC_SE_INSIZE) {
414 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
415 return 1;
416 }
417 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
418 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000419 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000420 offs &= ~(emu_jedec_se_size - 1);
421 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
422 break;
423 case JEDEC_BE_52:
424 if (!emu_jedec_be_52_size)
425 break;
426 if (writecnt != JEDEC_BE_52_OUTSIZE) {
427 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
428 return 1;
429 }
430 if (readcnt != JEDEC_BE_52_INSIZE) {
431 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
432 return 1;
433 }
434 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
435 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000436 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000437 offs &= ~(emu_jedec_be_52_size - 1);
438 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
439 break;
440 case JEDEC_BE_D8:
441 if (!emu_jedec_be_d8_size)
442 break;
443 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
444 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
445 return 1;
446 }
447 if (readcnt != JEDEC_BE_D8_INSIZE) {
448 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
449 return 1;
450 }
451 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
452 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000453 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000454 offs &= ~(emu_jedec_be_d8_size - 1);
455 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
456 break;
457 case JEDEC_CE_60:
458 if (!emu_jedec_ce_60_size)
459 break;
460 if (writecnt != JEDEC_CE_60_OUTSIZE) {
461 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
462 return 1;
463 }
464 if (readcnt != JEDEC_CE_60_INSIZE) {
465 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
466 return 1;
467 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000468 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000469 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000470 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000471 break;
472 case JEDEC_CE_C7:
473 if (!emu_jedec_ce_c7_size)
474 break;
475 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
476 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
477 return 1;
478 }
479 if (readcnt != JEDEC_CE_C7_INSIZE) {
480 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
481 return 1;
482 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000483 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000484 /* emu_jedec_ce_c7_size is emu_chip_size. */
485 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
486 break;
487 default:
488 /* No special response. */
489 break;
490 }
491 return 0;
492}
493#endif
494
Michael Karcherb9dbe482011-05-11 17:07:07 +0000495static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000496 const unsigned char *writearr, unsigned char *readarr)
497{
498 int i;
499
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000500 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000501
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000502 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000503 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000504 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000505
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000506 /* Response for unknown commands and missing chip is 0xff. */
507 memset(readarr, 0xff, readcnt);
508#if EMULATE_SPI_CHIP
509 switch (emu_chip) {
510 case EMULATE_ST_M25P10_RES:
511 case EMULATE_SST_SST25VF040_REMS:
512 case EMULATE_SST_SST25VF032B:
513 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
514 readarr)) {
515 msg_perr("Invalid command sent to flash chip!\n");
516 return 1;
517 }
518 break;
519 default:
520 break;
521 }
522#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000523 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000524 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000525 msg_pspew(" 0x%02x", readarr[i]);
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
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000530static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
Stefan Tauner8c357452011-09-18 22:42:18 +0000531 int start, int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000532{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000533 return spi_write_chunked(flash, buf, start, len,
534 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000535}