blob: d40ae9412b690fb2aa1afa50e6e221b1831e9741 [file] [log] [blame]
hailfingera9df33c2009-05-09 00:54:55 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger6ead7222010-11-01 22:07:04 +00004 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
hailfingera9df33c2009-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
hailfinger6ead7222010-11-01 22:07:04 +00008 * the Free Software Foundation; version 2 of the License.
hailfingera9df33c2009-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
hailfingera9df33c2009-05-09 00:54:55 +000020#include <string.h>
21#include <stdlib.h>
hailfingera9df33c2009-05-09 00:54:55 +000022#include "flash.h"
hailfingera8727712010-06-20 10:58:32 +000023#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000024#include "programmer.h"
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080025#include "flashchips.h"
hailfingera9df33c2009-05-09 00:54:55 +000026
hailfinger6ead7222010-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>
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080038
39#if EMULATE_SPI_CHIP
40/* The name of variable-size virtual chip. A 4MB flash example:
41 * flashrom -p dummy:emulate=VARIABLE_SIZE,size=4194304
42 */
43#define VARIABLE_SIZE_CHIP_NAME "VARIABLE_SIZE"
44#endif
hailfinger6ead7222010-11-01 22:07:04 +000045#endif
46
47#if EMULATE_CHIP
48static uint8_t *flashchip_contents = NULL;
49enum emu_chip {
50 EMULATE_NONE,
51 EMULATE_ST_M25P10_RES,
52 EMULATE_SST_SST25VF040_REMS,
53 EMULATE_SST_SST25VF032B,
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080054 EMULATE_VARIABLE_SIZE,
hailfinger6ead7222010-11-01 22:07:04 +000055};
56static enum emu_chip emu_chip = EMULATE_NONE;
57static char *emu_persistent_image = NULL;
58static int emu_chip_size = 0;
59#if EMULATE_SPI_CHIP
60static int emu_max_byteprogram_size = 0;
61static int emu_max_aai_size = 0;
62static int emu_jedec_se_size = 0;
63static int emu_jedec_be_52_size = 0;
64static int emu_jedec_be_d8_size = 0;
65static int emu_jedec_ce_60_size = 0;
66static int emu_jedec_ce_c7_size = 0;
67#endif
68#endif
69
70static int spi_write_256_chunksize = 256;
71
mkarcherd264e9e2011-05-11 17:07:07 +000072static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
73 const unsigned char *writearr, unsigned char *readarr);
74static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len);
75
76static const struct spi_programmer spi_programmer_dummyflasher = {
77 .type = SPI_CONTROLLER_DUMMY,
78 .max_data_read = MAX_DATA_READ_UNLIMITED,
79 .max_data_write = MAX_DATA_UNSPECIFIED,
80 .command = dummy_spi_send_command,
81 .multicommand = default_spi_send_multicommand,
82 .read = default_spi_read,
83 .write_256 = dummy_spi_write_256,
84};
dhendrix0ffc2eb2011-06-14 01:35:36 +000085
86static int dummy_shutdown(void *data)
87{
88 msg_pspew("%s\n", __func__);
89#if EMULATE_CHIP
90 if (emu_chip != EMULATE_NONE) {
91 if (emu_persistent_image) {
92 msg_pdbg("Writing %s\n", emu_persistent_image);
93 write_buf_to_file(flashchip_contents, emu_chip_size,
94 emu_persistent_image);
95 }
96 free(flashchip_contents);
97 }
98#endif
99 return 0;
100}
101
hailfingera9df33c2009-05-09 00:54:55 +0000102int dummy_init(void)
103{
hailfinger1ef766d2010-07-06 09:55:48 +0000104 char *bustext = NULL;
hailfinger6ead7222010-11-01 22:07:04 +0000105 char *tmp = NULL;
106#if EMULATE_CHIP
107 struct stat image_stat;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800108#if EMULATE_SPI_CHIP
109 int size = -1; /* size for generic chip */
110#endif
hailfinger6ead7222010-11-01 22:07:04 +0000111#endif
hailfinger1ef766d2010-07-06 09:55:48 +0000112
hailfinger50c335f2010-01-09 04:32:23 +0000113 msg_pspew("%s\n", __func__);
hailfinger668f3502009-06-01 00:02:11 +0000114
hailfingerddeb4ac2010-07-08 10:13:37 +0000115 bustext = extract_programmer_param("bus");
hailfinger1ef766d2010-07-06 09:55:48 +0000116 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
117 if (!bustext)
118 bustext = strdup("parallel+lpc+fwh+spi");
hailfinger668f3502009-06-01 00:02:11 +0000119 /* Convert the parameters to lowercase. */
hailfinger1ef766d2010-07-06 09:55:48 +0000120 tolower_string(bustext);
hailfinger668f3502009-06-01 00:02:11 +0000121
hailfingere1e41ea2011-07-27 07:13:06 +0000122 buses_supported = BUS_NONE;
hailfinger1ef766d2010-07-06 09:55:48 +0000123 if (strstr(bustext, "parallel")) {
hailfingere1e41ea2011-07-27 07:13:06 +0000124 buses_supported |= BUS_PARALLEL;
hailfinger50c335f2010-01-09 04:32:23 +0000125 msg_pdbg("Enabling support for %s flash.\n", "parallel");
hailfinger668f3502009-06-01 00:02:11 +0000126 }
hailfinger1ef766d2010-07-06 09:55:48 +0000127 if (strstr(bustext, "lpc")) {
hailfingere1e41ea2011-07-27 07:13:06 +0000128 buses_supported |= BUS_LPC;
hailfinger50c335f2010-01-09 04:32:23 +0000129 msg_pdbg("Enabling support for %s flash.\n", "LPC");
hailfinger668f3502009-06-01 00:02:11 +0000130 }
hailfinger1ef766d2010-07-06 09:55:48 +0000131 if (strstr(bustext, "fwh")) {
hailfingere1e41ea2011-07-27 07:13:06 +0000132 buses_supported |= BUS_FWH;
hailfinger50c335f2010-01-09 04:32:23 +0000133 msg_pdbg("Enabling support for %s flash.\n", "FWH");
hailfinger668f3502009-06-01 00:02:11 +0000134 }
hailfinger1ef766d2010-07-06 09:55:48 +0000135 if (strstr(bustext, "spi")) {
mkarcherd264e9e2011-05-11 17:07:07 +0000136 register_spi_programmer(&spi_programmer_dummyflasher);
hailfinger50c335f2010-01-09 04:32:23 +0000137 msg_pdbg("Enabling support for %s flash.\n", "SPI");
hailfinger668f3502009-06-01 00:02:11 +0000138 }
hailfingere1e41ea2011-07-27 07:13:06 +0000139 if (buses_supported == BUS_NONE)
hailfinger50c335f2010-01-09 04:32:23 +0000140 msg_pdbg("Support for all flash bus types disabled.\n");
hailfinger1ef766d2010-07-06 09:55:48 +0000141 free(bustext);
hailfinger6ead7222010-11-01 22:07:04 +0000142
143 tmp = extract_programmer_param("spi_write_256_chunksize");
144 if (tmp) {
145 spi_write_256_chunksize = atoi(tmp);
146 free(tmp);
147 if (spi_write_256_chunksize < 1) {
148 msg_perr("invalid spi_write_256_chunksize\n");
149 return 1;
150 }
151 }
152
153#if EMULATE_CHIP
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800154#if EMULATE_SPI_CHIP
155 tmp = extract_programmer_param("size");
156 if (tmp) {
157 int multiplier = 1;
158 if (strlen(tmp)) {
159 int remove_last_char = 1;
160 switch (tmp[strlen(tmp) - 1]) {
161 case 'k': case 'K':
162 multiplier = 1024;
163 break;
164 case 'm': case 'M':
165 multiplier = 1024 * 1024;
166 break;
167 default:
168 remove_last_char = 0;
169 break;
170 }
171 if (remove_last_char) tmp[strlen(tmp) - 1] = '\0';
172 }
173 size = atoi(tmp) * multiplier;
174 }
175#endif
176
hailfinger6ead7222010-11-01 22:07:04 +0000177 tmp = extract_programmer_param("emulate");
178 if (!tmp) {
179 msg_pdbg("Not emulating any flash chip.\n");
180 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000181 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000182 }
183#if EMULATE_SPI_CHIP
184 if (!strcmp(tmp, "M25P10.RES")) {
185 emu_chip = EMULATE_ST_M25P10_RES;
186 emu_chip_size = 128 * 1024;
187 emu_max_byteprogram_size = 128;
188 emu_max_aai_size = 0;
189 emu_jedec_se_size = 0;
190 emu_jedec_be_52_size = 0;
191 emu_jedec_be_d8_size = 32 * 1024;
192 emu_jedec_ce_60_size = 0;
193 emu_jedec_ce_c7_size = emu_chip_size;
194 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
195 "write)\n");
196 }
197 if (!strcmp(tmp, "SST25VF040.REMS")) {
198 emu_chip = EMULATE_SST_SST25VF040_REMS;
199 emu_chip_size = 512 * 1024;
200 emu_max_byteprogram_size = 1;
201 emu_max_aai_size = 0;
202 emu_jedec_se_size = 4 * 1024;
203 emu_jedec_be_52_size = 32 * 1024;
204 emu_jedec_be_d8_size = 0;
205 emu_jedec_ce_60_size = emu_chip_size;
206 emu_jedec_ce_c7_size = 0;
207 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
208 "byte write)\n");
209 }
210 if (!strcmp(tmp, "SST25VF032B")) {
211 emu_chip = EMULATE_SST_SST25VF032B;
212 emu_chip_size = 4 * 1024 * 1024;
213 emu_max_byteprogram_size = 1;
214 emu_max_aai_size = 2;
215 emu_jedec_se_size = 4 * 1024;
216 emu_jedec_be_52_size = 32 * 1024;
217 emu_jedec_be_d8_size = 64 * 1024;
218 emu_jedec_ce_60_size = emu_chip_size;
219 emu_jedec_ce_c7_size = emu_chip_size;
220 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
221 "write)\n");
222 }
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800223 if (!strncmp(tmp, VARIABLE_SIZE_CHIP_NAME,
224 strlen(VARIABLE_SIZE_CHIP_NAME))) {
225 if (size == -1) {
226 msg_perr("%s: the size parameter is not given.\n",
227 __func__);
228 free(tmp);
229 return 1;
230 }
231 emu_chip = EMULATE_VARIABLE_SIZE;
232 emu_chip_size = size;
233 emu_max_byteprogram_size = 256;
234 emu_max_aai_size = 0;
235 emu_jedec_se_size = 4 * 1024;
236 emu_jedec_be_52_size = 32 * 1024;
237 emu_jedec_be_d8_size = 64 * 1024;
238 emu_jedec_ce_60_size = emu_chip_size;
239 emu_jedec_ce_c7_size = emu_chip_size;
240 msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n",
241 emu_chip_size);
242 }
hailfinger6ead7222010-11-01 22:07:04 +0000243#endif
244 if (emu_chip == EMULATE_NONE) {
245 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
246 free(tmp);
247 return 1;
248 }
249 free(tmp);
250 flashchip_contents = malloc(emu_chip_size);
251 if (!flashchip_contents) {
252 msg_perr("Out of memory!\n");
253 return 1;
254 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000255
hailfinger6ead7222010-11-01 22:07:04 +0000256 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
257 memset(flashchip_contents, 0xff, emu_chip_size);
258
259 emu_persistent_image = extract_programmer_param("image");
260 if (!emu_persistent_image) {
261 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000262 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000263 }
264 if (!stat(emu_persistent_image, &image_stat)) {
265 msg_pdbg("Found persistent image %s, size %li ",
266 emu_persistent_image, (long)image_stat.st_size);
267 if (image_stat.st_size == emu_chip_size) {
268 msg_pdbg("matches.\n");
269 msg_pdbg("Reading %s\n", emu_persistent_image);
270 read_buf_from_file(flashchip_contents, emu_chip_size,
271 emu_persistent_image);
272 } else {
273 msg_pdbg("doesn't match.\n");
274 }
275 }
276#endif
hailfingera9df33c2009-05-09 00:54:55 +0000277
dhendrix0ffc2eb2011-06-14 01:35:36 +0000278dummy_init_out:
279 if (register_shutdown(dummy_shutdown, NULL)) {
hailfinger6ead7222010-11-01 22:07:04 +0000280 free(flashchip_contents);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000281 return 1;
hailfinger6ead7222010-11-01 22:07:04 +0000282 }
hailfingera9df33c2009-05-09 00:54:55 +0000283 return 0;
284}
285
hailfinger11ae3c42009-05-11 14:13:25 +0000286void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
287{
hailfinger50c335f2010-01-09 04:32:23 +0000288 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
289 __func__, descr, (unsigned long)len, phys_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000290 return (void *)phys_addr;
291}
292
293void dummy_unmap(void *virt_addr, size_t len)
294{
hailfinger50c335f2010-01-09 04:32:23 +0000295 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
296 __func__, (unsigned long)len, virt_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000297}
298
hailfinger82719632009-05-16 21:22:56 +0000299void dummy_chip_writeb(uint8_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000300{
hailfinger50c335f2010-01-09 04:32:23 +0000301 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000302}
303
hailfinger82719632009-05-16 21:22:56 +0000304void dummy_chip_writew(uint16_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000305{
hailfinger50c335f2010-01-09 04:32:23 +0000306 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000307}
308
hailfinger82719632009-05-16 21:22:56 +0000309void dummy_chip_writel(uint32_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000310{
hailfinger50c335f2010-01-09 04:32:23 +0000311 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000312}
313
hailfinger9d987ef2009-06-05 18:32:07 +0000314void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
315{
316 size_t i;
hailfinger50c335f2010-01-09 04:32:23 +0000317 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
318 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000319 for (i = 0; i < len; i++) {
320 if ((i % 16) == 0)
hailfinger50c335f2010-01-09 04:32:23 +0000321 msg_pspew("\n");
322 msg_pspew("%02x ", buf[i]);
hailfinger9d987ef2009-06-05 18:32:07 +0000323 }
324}
325
hailfinger82719632009-05-16 21:22:56 +0000326uint8_t dummy_chip_readb(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000327{
hailfinger50c335f2010-01-09 04:32:23 +0000328 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000329 return 0xff;
330}
331
hailfinger82719632009-05-16 21:22:56 +0000332uint16_t dummy_chip_readw(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000333{
hailfinger50c335f2010-01-09 04:32:23 +0000334 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000335 return 0xffff;
336}
337
hailfinger82719632009-05-16 21:22:56 +0000338uint32_t dummy_chip_readl(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000339{
hailfinger50c335f2010-01-09 04:32:23 +0000340 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000341 return 0xffffffff;
342}
343
hailfinger9d987ef2009-06-05 18:32:07 +0000344void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
345{
hailfinger50c335f2010-01-09 04:32:23 +0000346 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
347 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000348 memset(buf, 0xff, len);
349 return;
350}
351
hailfinger6ead7222010-11-01 22:07:04 +0000352#if EMULATE_SPI_CHIP
353static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
354 const unsigned char *writearr, unsigned char *readarr)
355{
356 int offs;
357 static int aai_offs;
358 static int aai_active = 0;
359
360 if (writecnt == 0) {
361 msg_perr("No command sent to the chip!\n");
362 return 1;
363 }
364 /* TODO: Implement command blacklists here. */
365 switch (writearr[0]) {
366 case JEDEC_RES:
367 if (emu_chip != EMULATE_ST_M25P10_RES)
368 break;
369 /* Respond with ST_M25P10_RES. */
370 if (readcnt > 0)
371 readarr[0] = 0x10;
372 break;
373 case JEDEC_REMS:
374 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
375 break;
376 /* Respond with SST_SST25VF040_REMS. */
377 if (readcnt > 0)
378 readarr[0] = 0xbf;
379 if (readcnt > 1)
380 readarr[1] = 0x44;
381 break;
382 case JEDEC_RDID:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800383 if (emu_chip == EMULATE_SST_SST25VF032B) {
384 /* Respond with SST_SST25VF032B. */
385 if (readcnt > 0)
386 readarr[0] = 0xbf;
387 if (readcnt > 1)
388 readarr[1] = 0x25;
389 if (readcnt > 2)
390 readarr[2] = 0x4a;
391 } else if (emu_chip == EMULATE_VARIABLE_SIZE) {
392 const uint16_t man_id = VARIABLE_SIZE_MANUF_ID;
393 const uint16_t dev_id = VARIABLE_SIZE_DEVICE_ID;
394 if (readcnt > 0) readarr[0] = man_id >> 8;
395 if (readcnt > 1) readarr[1] = man_id & 0xff;
396 if (readcnt > 2) readarr[2] = dev_id >> 8;
397 if (readcnt > 3) readarr[3] = dev_id & 0xff;
398 }
hailfinger6ead7222010-11-01 22:07:04 +0000399 break;
400 case JEDEC_RDSR:
401 memset(readarr, 0, readcnt);
402 if (aai_active)
403 memset(readarr, 1 << 6, readcnt);
404 break;
405 case JEDEC_READ:
406 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
407 /* Truncate to emu_chip_size. */
408 offs %= emu_chip_size;
409 if (readcnt > 0)
410 memcpy(readarr, flashchip_contents + offs, readcnt);
411 break;
412 case JEDEC_BYTE_PROGRAM:
413 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
414 /* Truncate to emu_chip_size. */
415 offs %= emu_chip_size;
416 if (writecnt < 5) {
417 msg_perr("BYTE PROGRAM size too short!\n");
418 return 1;
419 }
420 if (writecnt - 4 > emu_max_byteprogram_size) {
421 msg_perr("Max BYTE PROGRAM size exceeded!\n");
422 return 1;
423 }
424 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
425 break;
426 case JEDEC_AAI_WORD_PROGRAM:
427 if (!emu_max_aai_size)
428 break;
429 if (!aai_active) {
430 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
431 msg_perr("Initial AAI WORD PROGRAM size too "
432 "short!\n");
433 return 1;
434 }
435 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
436 msg_perr("Initial AAI WORD PROGRAM size too "
437 "long!\n");
438 return 1;
439 }
440 aai_active = 1;
441 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
442 writearr[3];
443 /* Truncate to emu_chip_size. */
444 aai_offs %= emu_chip_size;
445 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
446 aai_offs += 2;
447 } else {
448 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
449 msg_perr("Continuation AAI WORD PROGRAM size "
450 "too short!\n");
451 return 1;
452 }
453 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
454 msg_perr("Continuation AAI WORD PROGRAM size "
455 "too long!\n");
456 return 1;
457 }
458 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
459 aai_offs += 2;
460 }
461 break;
462 case JEDEC_WRDI:
463 if (!emu_max_aai_size)
464 break;
465 aai_active = 0;
466 break;
467 case JEDEC_SE:
468 if (!emu_jedec_se_size)
469 break;
470 if (writecnt != JEDEC_SE_OUTSIZE) {
471 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
472 return 1;
473 }
474 if (readcnt != JEDEC_SE_INSIZE) {
475 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
476 return 1;
477 }
478 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
479 if (offs & (emu_jedec_se_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000480 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000481 offs &= ~(emu_jedec_se_size - 1);
482 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
483 break;
484 case JEDEC_BE_52:
485 if (!emu_jedec_be_52_size)
486 break;
487 if (writecnt != JEDEC_BE_52_OUTSIZE) {
488 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
489 return 1;
490 }
491 if (readcnt != JEDEC_BE_52_INSIZE) {
492 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
493 return 1;
494 }
495 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
496 if (offs & (emu_jedec_be_52_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000497 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000498 offs &= ~(emu_jedec_be_52_size - 1);
499 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
500 break;
501 case JEDEC_BE_D8:
502 if (!emu_jedec_be_d8_size)
503 break;
504 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
505 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
506 return 1;
507 }
508 if (readcnt != JEDEC_BE_D8_INSIZE) {
509 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
510 return 1;
511 }
512 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
513 if (offs & (emu_jedec_be_d8_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000514 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000515 offs &= ~(emu_jedec_be_d8_size - 1);
516 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
517 break;
518 case JEDEC_CE_60:
519 if (!emu_jedec_ce_60_size)
520 break;
521 if (writecnt != JEDEC_CE_60_OUTSIZE) {
522 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
523 return 1;
524 }
525 if (readcnt != JEDEC_CE_60_INSIZE) {
526 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
527 return 1;
528 }
hailfingere53f5e42011-02-04 22:52:04 +0000529 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000530 /* emu_jedec_ce_60_size is emu_chip_size. */
hailfingere53f5e42011-02-04 22:52:04 +0000531 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
hailfinger6ead7222010-11-01 22:07:04 +0000532 break;
533 case JEDEC_CE_C7:
534 if (!emu_jedec_ce_c7_size)
535 break;
536 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
537 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
538 return 1;
539 }
540 if (readcnt != JEDEC_CE_C7_INSIZE) {
541 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
542 return 1;
543 }
hailfingere53f5e42011-02-04 22:52:04 +0000544 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000545 /* emu_jedec_ce_c7_size is emu_chip_size. */
546 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
547 break;
548 default:
549 /* No special response. */
550 break;
551 }
552 return 0;
553}
554#endif
555
mkarcherd264e9e2011-05-11 17:07:07 +0000556static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
hailfingerf91e3b52009-05-14 12:59:36 +0000557 const unsigned char *writearr, unsigned char *readarr)
558{
559 int i;
560
hailfinger50c335f2010-01-09 04:32:23 +0000561 msg_pspew("%s:", __func__);
hailfingerf91e3b52009-05-14 12:59:36 +0000562
hailfinger50c335f2010-01-09 04:32:23 +0000563 msg_pspew(" writing %u bytes:", writecnt);
hailfingerf91e3b52009-05-14 12:59:36 +0000564 for (i = 0; i < writecnt; i++)
hailfinger50c335f2010-01-09 04:32:23 +0000565 msg_pspew(" 0x%02x", writearr[i]);
hailfingerf91e3b52009-05-14 12:59:36 +0000566
hailfinger6ead7222010-11-01 22:07:04 +0000567 /* Response for unknown commands and missing chip is 0xff. */
568 memset(readarr, 0xff, readcnt);
569#if EMULATE_SPI_CHIP
570 switch (emu_chip) {
571 case EMULATE_ST_M25P10_RES:
572 case EMULATE_SST_SST25VF040_REMS:
573 case EMULATE_SST_SST25VF032B:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800574 case EMULATE_VARIABLE_SIZE:
hailfinger6ead7222010-11-01 22:07:04 +0000575 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
576 readarr)) {
577 msg_perr("Invalid command sent to flash chip!\n");
578 return 1;
579 }
580 break;
581 default:
582 break;
583 }
584#endif
hailfinger50c335f2010-01-09 04:32:23 +0000585 msg_pspew(" reading %u bytes:", readcnt);
hailfingerf91e3b52009-05-14 12:59:36 +0000586 for (i = 0; i < readcnt; i++) {
hailfinger6ead7222010-11-01 22:07:04 +0000587 msg_pspew(" 0x%02x", readarr[i]);
hailfingerf91e3b52009-05-14 12:59:36 +0000588 }
hailfinger50c335f2010-01-09 04:32:23 +0000589 msg_pspew("\n");
hailfingerf91e3b52009-05-14 12:59:36 +0000590 return 0;
591}
hailfingera8727712010-06-20 10:58:32 +0000592
mkarcherd264e9e2011-05-11 17:07:07 +0000593static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000594{
hailfinger6ead7222010-11-01 22:07:04 +0000595 return spi_write_chunked(flash, buf, start, len,
596 spi_write_256_chunksize);
hailfingerc7d06c62010-07-14 16:19:05 +0000597}
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800598
599#if EMULATE_CHIP && EMULATE_SPI_CHIP
600int probe_variable_size(struct flashchip *flash)
601{
602 int i;
603
604 /* Skip the probing if we don't emulate this chip. */
605 if (emu_chip != EMULATE_VARIABLE_SIZE)
606 return 0;
607
608 /*
609 * This will break if one day flashchip becomes read-only.
610 * Once that happens, we need to have special hacks in functions:
611 *
612 * erase_and_write_flash() in flashrom.c
613 * read_flash_to_file()
614 * handle_romentries()
615 * ...
616 *
617 * Search "total_size * 1024" in code.
618 */
619 if (emu_chip_size % 1024)
620 msg_perr("%s: emu_chip_size is not multipler of 1024.\n",
621 __func__);
622 flash->total_size = emu_chip_size / 1024;
623 msg_cdbg("%s: set flash->total_size to %dK bytes.\n", __func__,
624 flash->total_size);
625
626 /* Update eraser count */
627 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
628 struct block_eraser *eraser = &flash->block_erasers[i];
629 if (eraser->block_erase == NULL)
630 break;
631
632 eraser->eraseblocks[0].count = emu_chip_size /
633 eraser->eraseblocks[0].size;
634 msg_cdbg("%s: eraser.size=%d, .count=%d\n",
635 __func__, eraser->eraseblocks[0].size,
636 eraser->eraseblocks[0].count);
637 }
638
639 return 1;
640}
641#endif