blob: 822bc736589962239ce138a724cebb1337d7d4df [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
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000078static const struct par_programmer par_programmer_dummy = {
79 .chip_readb = dummy_chip_readb,
80 .chip_readw = dummy_chip_readw,
81 .chip_readl = dummy_chip_readl,
82 .chip_readn = dummy_chip_readn,
83 .chip_writeb = dummy_chip_writeb,
84 .chip_writew = dummy_chip_writew,
85 .chip_writel = dummy_chip_writel,
86 .chip_writen = dummy_chip_writen,
87};
88
89enum chipbustype dummy_buses_supported = BUS_NONE;
90
David Hendricks8bb20212011-06-14 01:35:36 +000091static int dummy_shutdown(void *data)
92{
93 msg_pspew("%s\n", __func__);
94#if EMULATE_CHIP
95 if (emu_chip != EMULATE_NONE) {
96 if (emu_persistent_image) {
97 msg_pdbg("Writing %s\n", emu_persistent_image);
98 write_buf_to_file(flashchip_contents, emu_chip_size,
99 emu_persistent_image);
100 }
101 free(flashchip_contents);
102 }
103#endif
104 return 0;
105}
106
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000107int dummy_init(void)
108{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000109 char *bustext = NULL;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000110 char *tmp = NULL;
111#if EMULATE_CHIP
112 struct stat image_stat;
113#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000114
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000115 msg_pspew("%s\n", __func__);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000116
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000117 bustext = extract_programmer_param("bus");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000118 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
119 if (!bustext)
120 bustext = strdup("parallel+lpc+fwh+spi");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000121 /* Convert the parameters to lowercase. */
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000122 tolower_string(bustext);
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000123
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000124 dummy_buses_supported = BUS_NONE;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000125 if (strstr(bustext, "parallel")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000126 dummy_buses_supported |= BUS_PARALLEL;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000127 msg_pdbg("Enabling support for %s flash.\n", "parallel");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000128 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000129 if (strstr(bustext, "lpc")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000130 dummy_buses_supported |= BUS_LPC;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000131 msg_pdbg("Enabling support for %s flash.\n", "LPC");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000132 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000133 if (strstr(bustext, "fwh")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000134 dummy_buses_supported |= BUS_FWH;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000135 msg_pdbg("Enabling support for %s flash.\n", "FWH");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000136 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000137 if (strstr(bustext, "spi")) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000138 dummy_buses_supported |= BUS_SPI;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000139 msg_pdbg("Enabling support for %s flash.\n", "SPI");
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000140 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000141 if (dummy_buses_supported == BUS_NONE)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000142 msg_pdbg("Support for all flash bus types disabled.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000143 free(bustext);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000144
145 tmp = extract_programmer_param("spi_write_256_chunksize");
146 if (tmp) {
147 spi_write_256_chunksize = atoi(tmp);
148 free(tmp);
149 if (spi_write_256_chunksize < 1) {
150 msg_perr("invalid spi_write_256_chunksize\n");
151 return 1;
152 }
153 }
154
155#if EMULATE_CHIP
156 tmp = extract_programmer_param("emulate");
157 if (!tmp) {
158 msg_pdbg("Not emulating any flash chip.\n");
159 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000160 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000161 }
162#if EMULATE_SPI_CHIP
163 if (!strcmp(tmp, "M25P10.RES")) {
164 emu_chip = EMULATE_ST_M25P10_RES;
165 emu_chip_size = 128 * 1024;
166 emu_max_byteprogram_size = 128;
167 emu_max_aai_size = 0;
168 emu_jedec_se_size = 0;
169 emu_jedec_be_52_size = 0;
170 emu_jedec_be_d8_size = 32 * 1024;
171 emu_jedec_ce_60_size = 0;
172 emu_jedec_ce_c7_size = emu_chip_size;
173 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
174 "write)\n");
175 }
176 if (!strcmp(tmp, "SST25VF040.REMS")) {
177 emu_chip = EMULATE_SST_SST25VF040_REMS;
178 emu_chip_size = 512 * 1024;
179 emu_max_byteprogram_size = 1;
180 emu_max_aai_size = 0;
181 emu_jedec_se_size = 4 * 1024;
182 emu_jedec_be_52_size = 32 * 1024;
183 emu_jedec_be_d8_size = 0;
184 emu_jedec_ce_60_size = emu_chip_size;
185 emu_jedec_ce_c7_size = 0;
186 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
187 "byte write)\n");
188 }
189 if (!strcmp(tmp, "SST25VF032B")) {
190 emu_chip = EMULATE_SST_SST25VF032B;
191 emu_chip_size = 4 * 1024 * 1024;
192 emu_max_byteprogram_size = 1;
193 emu_max_aai_size = 2;
194 emu_jedec_se_size = 4 * 1024;
195 emu_jedec_be_52_size = 32 * 1024;
196 emu_jedec_be_d8_size = 64 * 1024;
197 emu_jedec_ce_60_size = emu_chip_size;
198 emu_jedec_ce_c7_size = emu_chip_size;
199 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
200 "write)\n");
201 }
202#endif
203 if (emu_chip == EMULATE_NONE) {
204 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
205 free(tmp);
206 return 1;
207 }
208 free(tmp);
209 flashchip_contents = malloc(emu_chip_size);
210 if (!flashchip_contents) {
211 msg_perr("Out of memory!\n");
212 return 1;
213 }
David Hendricks8bb20212011-06-14 01:35:36 +0000214
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000215 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
216 memset(flashchip_contents, 0xff, emu_chip_size);
217
218 emu_persistent_image = extract_programmer_param("image");
219 if (!emu_persistent_image) {
220 /* Nothing else to do. */
David Hendricks8bb20212011-06-14 01:35:36 +0000221 goto dummy_init_out;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000222 }
223 if (!stat(emu_persistent_image, &image_stat)) {
224 msg_pdbg("Found persistent image %s, size %li ",
225 emu_persistent_image, (long)image_stat.st_size);
226 if (image_stat.st_size == emu_chip_size) {
227 msg_pdbg("matches.\n");
228 msg_pdbg("Reading %s\n", emu_persistent_image);
229 read_buf_from_file(flashchip_contents, emu_chip_size,
230 emu_persistent_image);
231 } else {
232 msg_pdbg("doesn't match.\n");
233 }
234 }
235#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000236
David Hendricks8bb20212011-06-14 01:35:36 +0000237dummy_init_out:
238 if (register_shutdown(dummy_shutdown, NULL)) {
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000239 free(flashchip_contents);
David Hendricks8bb20212011-06-14 01:35:36 +0000240 return 1;
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000241 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000242 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
243 register_par_programmer(&par_programmer_dummy,
244 dummy_buses_supported &
245 (BUS_PARALLEL | BUS_LPC |
246 BUS_FWH));
247 if (dummy_buses_supported & BUS_SPI)
248 register_spi_programmer(&spi_programmer_dummyflasher);
249
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000250 return 0;
251}
252
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000253void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
254{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000255 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
256 __func__, descr, (unsigned long)len, phys_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000257 return (void *)phys_addr;
258}
259
260void dummy_unmap(void *virt_addr, size_t len)
261{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000262 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
263 __func__, (unsigned long)len, virt_addr);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000264}
265
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000266void dummy_chip_writeb(uint8_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000267{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000268 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000269}
270
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000271void dummy_chip_writew(uint16_t val, 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, val=0x%04x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000274}
275
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000276void dummy_chip_writel(uint32_t val, chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000277{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000278 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000279}
280
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000281void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
282{
283 size_t i;
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000284 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
285 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000286 for (i = 0; i < len; i++) {
287 if ((i % 16) == 0)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000288 msg_pspew("\n");
289 msg_pspew("%02x ", buf[i]);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000290 }
291}
292
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000293uint8_t dummy_chip_readb(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000294{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000295 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000296 return 0xff;
297}
298
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000299uint16_t dummy_chip_readw(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000300{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000301 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000302 return 0xffff;
303}
304
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000305uint32_t dummy_chip_readl(const chipaddr addr)
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000306{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000307 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000308 return 0xffffffff;
309}
310
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000311void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
312{
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000313 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
314 __func__, addr, (unsigned long)len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000315 memset(buf, 0xff, len);
316 return;
317}
318
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000319#if EMULATE_SPI_CHIP
320static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
321 const unsigned char *writearr, unsigned char *readarr)
322{
323 int offs;
324 static int aai_offs;
325 static int aai_active = 0;
326
327 if (writecnt == 0) {
328 msg_perr("No command sent to the chip!\n");
329 return 1;
330 }
331 /* TODO: Implement command blacklists here. */
332 switch (writearr[0]) {
333 case JEDEC_RES:
334 if (emu_chip != EMULATE_ST_M25P10_RES)
335 break;
336 /* Respond with ST_M25P10_RES. */
337 if (readcnt > 0)
338 readarr[0] = 0x10;
339 break;
340 case JEDEC_REMS:
341 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
342 break;
343 /* Respond with SST_SST25VF040_REMS. */
344 if (readcnt > 0)
345 readarr[0] = 0xbf;
346 if (readcnt > 1)
347 readarr[1] = 0x44;
348 break;
349 case JEDEC_RDID:
350 if (emu_chip != EMULATE_SST_SST25VF032B)
351 break;
352 /* Respond with SST_SST25VF032B. */
353 if (readcnt > 0)
354 readarr[0] = 0xbf;
355 if (readcnt > 1)
356 readarr[1] = 0x25;
357 if (readcnt > 2)
358 readarr[2] = 0x4a;
359 break;
360 case JEDEC_RDSR:
361 memset(readarr, 0, readcnt);
362 if (aai_active)
363 memset(readarr, 1 << 6, readcnt);
364 break;
365 case JEDEC_READ:
366 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
367 /* Truncate to emu_chip_size. */
368 offs %= emu_chip_size;
369 if (readcnt > 0)
370 memcpy(readarr, flashchip_contents + offs, readcnt);
371 break;
372 case JEDEC_BYTE_PROGRAM:
373 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
374 /* Truncate to emu_chip_size. */
375 offs %= emu_chip_size;
376 if (writecnt < 5) {
377 msg_perr("BYTE PROGRAM size too short!\n");
378 return 1;
379 }
380 if (writecnt - 4 > emu_max_byteprogram_size) {
381 msg_perr("Max BYTE PROGRAM size exceeded!\n");
382 return 1;
383 }
384 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
385 break;
386 case JEDEC_AAI_WORD_PROGRAM:
387 if (!emu_max_aai_size)
388 break;
389 if (!aai_active) {
390 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
391 msg_perr("Initial AAI WORD PROGRAM size too "
392 "short!\n");
393 return 1;
394 }
395 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
396 msg_perr("Initial AAI WORD PROGRAM size too "
397 "long!\n");
398 return 1;
399 }
400 aai_active = 1;
401 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
402 writearr[3];
403 /* Truncate to emu_chip_size. */
404 aai_offs %= emu_chip_size;
405 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
406 aai_offs += 2;
407 } else {
408 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
409 msg_perr("Continuation AAI WORD PROGRAM size "
410 "too short!\n");
411 return 1;
412 }
413 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
414 msg_perr("Continuation AAI WORD PROGRAM size "
415 "too long!\n");
416 return 1;
417 }
418 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
419 aai_offs += 2;
420 }
421 break;
422 case JEDEC_WRDI:
423 if (!emu_max_aai_size)
424 break;
425 aai_active = 0;
426 break;
427 case JEDEC_SE:
428 if (!emu_jedec_se_size)
429 break;
430 if (writecnt != JEDEC_SE_OUTSIZE) {
431 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
432 return 1;
433 }
434 if (readcnt != JEDEC_SE_INSIZE) {
435 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
436 return 1;
437 }
438 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
439 if (offs & (emu_jedec_se_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000440 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000441 offs &= ~(emu_jedec_se_size - 1);
442 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
443 break;
444 case JEDEC_BE_52:
445 if (!emu_jedec_be_52_size)
446 break;
447 if (writecnt != JEDEC_BE_52_OUTSIZE) {
448 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
449 return 1;
450 }
451 if (readcnt != JEDEC_BE_52_INSIZE) {
452 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
453 return 1;
454 }
455 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
456 if (offs & (emu_jedec_be_52_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000457 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000458 offs &= ~(emu_jedec_be_52_size - 1);
459 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
460 break;
461 case JEDEC_BE_D8:
462 if (!emu_jedec_be_d8_size)
463 break;
464 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
465 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
466 return 1;
467 }
468 if (readcnt != JEDEC_BE_D8_INSIZE) {
469 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
470 return 1;
471 }
472 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
473 if (offs & (emu_jedec_be_d8_size - 1))
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000474 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000475 offs &= ~(emu_jedec_be_d8_size - 1);
476 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
477 break;
478 case JEDEC_CE_60:
479 if (!emu_jedec_ce_60_size)
480 break;
481 if (writecnt != JEDEC_CE_60_OUTSIZE) {
482 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
483 return 1;
484 }
485 if (readcnt != JEDEC_CE_60_INSIZE) {
486 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
487 return 1;
488 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000489 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000490 /* emu_jedec_ce_60_size is emu_chip_size. */
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000491 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000492 break;
493 case JEDEC_CE_C7:
494 if (!emu_jedec_ce_c7_size)
495 break;
496 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
497 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
498 return 1;
499 }
500 if (readcnt != JEDEC_CE_C7_INSIZE) {
501 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
502 return 1;
503 }
Carl-Daniel Hailfinger146b77d2011-02-04 22:52:04 +0000504 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000505 /* emu_jedec_ce_c7_size is emu_chip_size. */
506 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
507 break;
508 default:
509 /* No special response. */
510 break;
511 }
512 return 0;
513}
514#endif
515
Michael Karcherb9dbe482011-05-11 17:07:07 +0000516static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000517 const unsigned char *writearr, unsigned char *readarr)
518{
519 int i;
520
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000521 msg_pspew("%s:", __func__);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000522
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000523 msg_pspew(" writing %u bytes:", writecnt);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000524 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000525 msg_pspew(" 0x%02x", writearr[i]);
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000526
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000527 /* Response for unknown commands and missing chip is 0xff. */
528 memset(readarr, 0xff, readcnt);
529#if EMULATE_SPI_CHIP
530 switch (emu_chip) {
531 case EMULATE_ST_M25P10_RES:
532 case EMULATE_SST_SST25VF040_REMS:
533 case EMULATE_SST_SST25VF032B:
534 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
535 readarr)) {
536 msg_perr("Invalid command sent to flash chip!\n");
537 return 1;
538 }
539 break;
540 default:
541 break;
542 }
543#endif
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000544 msg_pspew(" reading %u bytes:", readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000545 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000546 msg_pspew(" 0x%02x", readarr[i]);
Carl-Daniel Hailfinger3ac101c2010-01-09 04:32:23 +0000547 msg_pspew("\n");
Carl-Daniel Hailfingerbfe2e0c2009-05-14 12:59:36 +0000548 return 0;
549}
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000550
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000551static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
Stefan Tauner8c357452011-09-18 22:42:18 +0000552 int start, int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000553{
Carl-Daniel Hailfingerf68aa8a2010-11-01 22:07:04 +0000554 return spi_write_chunked(flash, buf, start, len,
555 spi_write_256_chunksize);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000556}