blob: c755469ddf84f82b4b877d3f55cd0f5074a95853 [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
David Hendricks84377002014-09-09 16:09:31 -070020#include <errno.h>
hailfingera9df33c2009-05-09 00:54:55 +000021#include <string.h>
22#include <stdlib.h>
hailfingera9df33c2009-05-09 00:54:55 +000023#include "flash.h"
hailfingera8727712010-06-20 10:58:32 +000024#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000025#include "programmer.h"
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080026#include "flashchips.h"
hailfingera9df33c2009-05-09 00:54:55 +000027
hailfinger6ead7222010-11-01 22:07:04 +000028/* Remove the #define below if you don't want SPI flash chip emulation. */
29#define EMULATE_SPI_CHIP 1
30
31#if EMULATE_SPI_CHIP
32#define EMULATE_CHIP 1
33#include "spi.h"
34#endif
35
36#if EMULATE_CHIP
37#include <sys/types.h>
38#include <sys/stat.h>
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080039
40#if EMULATE_SPI_CHIP
41/* The name of variable-size virtual chip. A 4MB flash example:
42 * flashrom -p dummy:emulate=VARIABLE_SIZE,size=4194304
43 */
44#define VARIABLE_SIZE_CHIP_NAME "VARIABLE_SIZE"
45#endif
hailfinger6ead7222010-11-01 22:07:04 +000046#endif
47
48#if EMULATE_CHIP
49static uint8_t *flashchip_contents = NULL;
50enum emu_chip {
51 EMULATE_NONE,
52 EMULATE_ST_M25P10_RES,
53 EMULATE_SST_SST25VF040_REMS,
54 EMULATE_SST_SST25VF032B,
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +080055 EMULATE_VARIABLE_SIZE,
hailfinger6ead7222010-11-01 22:07:04 +000056};
57static enum emu_chip emu_chip = EMULATE_NONE;
58static char *emu_persistent_image = NULL;
stefanctc5eb8a92011-11-23 09:13:48 +000059static unsigned int emu_chip_size = 0;
Simon Glasscf253a72013-07-10 21:10:11 -070060static int emu_modified; /* is the image modified since reading it? */
David Hendricks0eda2a82014-09-12 16:32:05 -070061static int erase_to_zero;
hailfinger6ead7222010-11-01 22:07:04 +000062#if EMULATE_SPI_CHIP
stefanctc5eb8a92011-11-23 09:13:48 +000063static unsigned int emu_max_byteprogram_size = 0;
64static unsigned int emu_max_aai_size = 0;
65static unsigned int emu_jedec_se_size = 0;
66static unsigned int emu_jedec_be_52_size = 0;
67static unsigned int emu_jedec_be_d8_size = 0;
68static unsigned int emu_jedec_ce_60_size = 0;
69static unsigned int emu_jedec_ce_c7_size = 0;
hailfinger6ead7222010-11-01 22:07:04 +000070#endif
71#endif
72
stefanctc5eb8a92011-11-23 09:13:48 +000073static unsigned int spi_write_256_chunksize = 256;
hailfinger6ead7222010-11-01 22:07:04 +000074
David Hendricks84377002014-09-09 16:09:31 -070075/* If "freq" parameter is passed in from command line, commands will delay
76 * for this period before returning. */
77static unsigned long int delay_us = 0;
78
mkarcherd264e9e2011-05-11 17:07:07 +000079static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
80 const unsigned char *writearr, unsigned char *readarr);
uwe8d342eb2011-07-28 08:13:25 +000081static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000082 unsigned int start, unsigned int len);
mkarcherd264e9e2011-05-11 17:07:07 +000083
84static const struct spi_programmer spi_programmer_dummyflasher = {
uwe8d342eb2011-07-28 08:13:25 +000085 .type = SPI_CONTROLLER_DUMMY,
86 .max_data_read = MAX_DATA_READ_UNLIMITED,
87 .max_data_write = MAX_DATA_UNSPECIFIED,
88 .command = dummy_spi_send_command,
89 .multicommand = default_spi_send_multicommand,
90 .read = default_spi_read,
91 .write_256 = dummy_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +000092};
dhendrix0ffc2eb2011-06-14 01:35:36 +000093
hailfinger76bb7e92011-11-09 23:40:00 +000094static const struct par_programmer par_programmer_dummy = {
95 .chip_readb = dummy_chip_readb,
96 .chip_readw = dummy_chip_readw,
97 .chip_readl = dummy_chip_readl,
98 .chip_readn = dummy_chip_readn,
99 .chip_writeb = dummy_chip_writeb,
100 .chip_writew = dummy_chip_writew,
101 .chip_writel = dummy_chip_writel,
102 .chip_writen = dummy_chip_writen,
103};
104
105enum chipbustype dummy_buses_supported = BUS_NONE;
106
dhendrix0ffc2eb2011-06-14 01:35:36 +0000107static int dummy_shutdown(void *data)
108{
109 msg_pspew("%s\n", __func__);
110#if EMULATE_CHIP
111 if (emu_chip != EMULATE_NONE) {
Simon Glasscf253a72013-07-10 21:10:11 -0700112 if (emu_persistent_image && emu_modified) {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000113 msg_pdbg("Writing %s\n", emu_persistent_image);
114 write_buf_to_file(flashchip_contents, emu_chip_size,
115 emu_persistent_image);
116 }
117 free(flashchip_contents);
118 }
119#endif
120 return 0;
121}
122
Simon Glassd2c64a22013-07-03 22:05:21 +0900123/* Values for the 'size' parameter */
124enum {
125 SIZE_UNKNOWN = -1,
126 SIZE_AUTO = -2,
127};
128
hailfingera9df33c2009-05-09 00:54:55 +0000129int dummy_init(void)
130{
hailfinger1ef766d2010-07-06 09:55:48 +0000131 char *bustext = NULL;
hailfinger6ead7222010-11-01 22:07:04 +0000132 char *tmp = NULL;
133#if EMULATE_CHIP
134 struct stat image_stat;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800135#if EMULATE_SPI_CHIP
Simon Glassd2c64a22013-07-03 22:05:21 +0900136 int size = SIZE_UNKNOWN; /* size for generic chip */
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800137#endif
hailfinger6ead7222010-11-01 22:07:04 +0000138#endif
Simon Glassd2c64a22013-07-03 22:05:21 +0900139 int image_size = SIZE_UNKNOWN;
hailfinger1ef766d2010-07-06 09:55:48 +0000140
hailfinger50c335f2010-01-09 04:32:23 +0000141 msg_pspew("%s\n", __func__);
hailfinger668f3502009-06-01 00:02:11 +0000142
hailfingerddeb4ac2010-07-08 10:13:37 +0000143 bustext = extract_programmer_param("bus");
hailfinger1ef766d2010-07-06 09:55:48 +0000144 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
145 if (!bustext)
146 bustext = strdup("parallel+lpc+fwh+spi");
hailfinger668f3502009-06-01 00:02:11 +0000147 /* Convert the parameters to lowercase. */
hailfinger1ef766d2010-07-06 09:55:48 +0000148 tolower_string(bustext);
hailfinger668f3502009-06-01 00:02:11 +0000149
hailfinger76bb7e92011-11-09 23:40:00 +0000150 dummy_buses_supported = BUS_NONE;
hailfinger1ef766d2010-07-06 09:55:48 +0000151 if (strstr(bustext, "parallel")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000152 dummy_buses_supported |= BUS_PARALLEL;
hailfinger50c335f2010-01-09 04:32:23 +0000153 msg_pdbg("Enabling support for %s flash.\n", "parallel");
hailfinger668f3502009-06-01 00:02:11 +0000154 }
hailfinger1ef766d2010-07-06 09:55:48 +0000155 if (strstr(bustext, "lpc")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000156 dummy_buses_supported |= BUS_LPC;
hailfinger50c335f2010-01-09 04:32:23 +0000157 msg_pdbg("Enabling support for %s flash.\n", "LPC");
hailfinger668f3502009-06-01 00:02:11 +0000158 }
hailfinger1ef766d2010-07-06 09:55:48 +0000159 if (strstr(bustext, "fwh")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000160 dummy_buses_supported |= BUS_FWH;
hailfinger50c335f2010-01-09 04:32:23 +0000161 msg_pdbg("Enabling support for %s flash.\n", "FWH");
hailfinger668f3502009-06-01 00:02:11 +0000162 }
hailfinger1ef766d2010-07-06 09:55:48 +0000163 if (strstr(bustext, "spi")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000164 dummy_buses_supported |= BUS_SPI;
hailfinger50c335f2010-01-09 04:32:23 +0000165 msg_pdbg("Enabling support for %s flash.\n", "SPI");
hailfinger668f3502009-06-01 00:02:11 +0000166 }
hailfinger76bb7e92011-11-09 23:40:00 +0000167 if (dummy_buses_supported == BUS_NONE)
hailfinger50c335f2010-01-09 04:32:23 +0000168 msg_pdbg("Support for all flash bus types disabled.\n");
hailfinger1ef766d2010-07-06 09:55:48 +0000169 free(bustext);
hailfinger6ead7222010-11-01 22:07:04 +0000170
171 tmp = extract_programmer_param("spi_write_256_chunksize");
172 if (tmp) {
173 spi_write_256_chunksize = atoi(tmp);
174 free(tmp);
175 if (spi_write_256_chunksize < 1) {
176 msg_perr("invalid spi_write_256_chunksize\n");
177 return 1;
178 }
179 }
180
David Hendricks84377002014-09-09 16:09:31 -0700181 /* frequency to emulate in Hz (default), KHz, or MHz */
182 tmp = extract_programmer_param("freq");
183 if (tmp) {
184 unsigned long int freq;
185 char *units = tmp;
186 char *end = tmp + strlen(tmp);
187
188 errno = 0;
189 freq = strtoul(tmp, &units, 0);
190 if (errno) {
191 msg_perr("Invalid frequency \"%s\", %s\n",
192 tmp, strerror(errno));
193 goto dummy_init_out;
194 }
195
196 if ((units > tmp) && (units < end)) {
197 int units_valid = 0;
198
199 if (units < end - 3) {
200 ;
201 } else if (units == end - 2) {
202 if (!strcasecmp(units, "hz"))
203 units_valid = 1;
204 } else if (units == end - 3) {
205 if (!strcasecmp(units, "khz")) {
206 freq *= 1000;
207 units_valid = 1;
208 } else if (!strcasecmp(units, "mhz")) {
209 freq *= 1000000;
210 units_valid = 1;
211 }
212 }
213
214 if (!units_valid) {
215 msg_perr("Invalid units: %s\n", units);
216 return 1;
217 }
218 }
219
220 /* Assume we only work with bytes and transfer at 1 bit/Hz */
221 delay_us = (1000000 * 8) / freq;
222 }
223
hailfinger6ead7222010-11-01 22:07:04 +0000224#if EMULATE_CHIP
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800225#if EMULATE_SPI_CHIP
226 tmp = extract_programmer_param("size");
227 if (tmp) {
228 int multiplier = 1;
Simon Glassd2c64a22013-07-03 22:05:21 +0900229 if (!strcmp(tmp, "auto"))
230 size = SIZE_AUTO;
231 else if (strlen(tmp)) {
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800232 int remove_last_char = 1;
233 switch (tmp[strlen(tmp) - 1]) {
234 case 'k': case 'K':
235 multiplier = 1024;
236 break;
237 case 'm': case 'M':
238 multiplier = 1024 * 1024;
239 break;
240 default:
241 remove_last_char = 0;
242 break;
243 }
244 if (remove_last_char) tmp[strlen(tmp) - 1] = '\0';
Simon Glassd2c64a22013-07-03 22:05:21 +0900245 size = atoi(tmp) * multiplier;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800246 }
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800247 }
248#endif
249
hailfinger6ead7222010-11-01 22:07:04 +0000250 tmp = extract_programmer_param("emulate");
251 if (!tmp) {
252 msg_pdbg("Not emulating any flash chip.\n");
253 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000254 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000255 }
David Hendricks84377002014-09-09 16:09:31 -0700256
hailfinger6ead7222010-11-01 22:07:04 +0000257#if EMULATE_SPI_CHIP
258 if (!strcmp(tmp, "M25P10.RES")) {
259 emu_chip = EMULATE_ST_M25P10_RES;
260 emu_chip_size = 128 * 1024;
261 emu_max_byteprogram_size = 128;
262 emu_max_aai_size = 0;
263 emu_jedec_se_size = 0;
264 emu_jedec_be_52_size = 0;
265 emu_jedec_be_d8_size = 32 * 1024;
266 emu_jedec_ce_60_size = 0;
267 emu_jedec_ce_c7_size = emu_chip_size;
268 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
269 "write)\n");
270 }
271 if (!strcmp(tmp, "SST25VF040.REMS")) {
272 emu_chip = EMULATE_SST_SST25VF040_REMS;
273 emu_chip_size = 512 * 1024;
274 emu_max_byteprogram_size = 1;
275 emu_max_aai_size = 0;
276 emu_jedec_se_size = 4 * 1024;
277 emu_jedec_be_52_size = 32 * 1024;
278 emu_jedec_be_d8_size = 0;
279 emu_jedec_ce_60_size = emu_chip_size;
280 emu_jedec_ce_c7_size = 0;
281 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
282 "byte write)\n");
283 }
284 if (!strcmp(tmp, "SST25VF032B")) {
285 emu_chip = EMULATE_SST_SST25VF032B;
286 emu_chip_size = 4 * 1024 * 1024;
287 emu_max_byteprogram_size = 1;
288 emu_max_aai_size = 2;
289 emu_jedec_se_size = 4 * 1024;
290 emu_jedec_be_52_size = 32 * 1024;
291 emu_jedec_be_d8_size = 64 * 1024;
292 emu_jedec_ce_60_size = emu_chip_size;
293 emu_jedec_ce_c7_size = emu_chip_size;
294 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
295 "write)\n");
296 }
Simon Glassd2c64a22013-07-03 22:05:21 +0900297 emu_persistent_image = extract_programmer_param("image");
298 if (!stat(emu_persistent_image, &image_stat))
299 image_size = image_stat.st_size;
300
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800301 if (!strncmp(tmp, VARIABLE_SIZE_CHIP_NAME,
302 strlen(VARIABLE_SIZE_CHIP_NAME))) {
Simon Glassd2c64a22013-07-03 22:05:21 +0900303 if (size == SIZE_UNKNOWN) {
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800304 msg_perr("%s: the size parameter is not given.\n",
305 __func__);
306 free(tmp);
307 return 1;
Simon Glassd2c64a22013-07-03 22:05:21 +0900308 } else if (size == SIZE_AUTO) {
309 if (image_size == SIZE_UNKNOWN) {
310 msg_perr("%s: no image so cannot use automatic size.\n",
311 __func__);
312 free(tmp);
313 return 1;
314 }
315 size = image_size;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800316 }
317 emu_chip = EMULATE_VARIABLE_SIZE;
318 emu_chip_size = size;
319 emu_max_byteprogram_size = 256;
320 emu_max_aai_size = 0;
321 emu_jedec_se_size = 4 * 1024;
322 emu_jedec_be_52_size = 32 * 1024;
323 emu_jedec_be_d8_size = 64 * 1024;
324 emu_jedec_ce_60_size = emu_chip_size;
325 emu_jedec_ce_c7_size = emu_chip_size;
326 msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n",
327 emu_chip_size);
328 }
hailfinger6ead7222010-11-01 22:07:04 +0000329#endif
330 if (emu_chip == EMULATE_NONE) {
331 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
332 free(tmp);
333 return 1;
334 }
David Hendricks0eda2a82014-09-12 16:32:05 -0700335
336 /* Should emulated flash erase to zero (yes/no)? */
337 tmp = extract_programmer_param("erase_to_zero");
338 if (tmp) {
339 if (!strcmp(tmp, "yes")) {
340 msg_pdbg("Emulated chip will erase to 0x00\n");
341 erase_to_zero = 1;
342 } else if (!strcmp(tmp, "no")) {
343 msg_pdbg("Emulated chip will erase to 0xff\n");
344 } else {
345 msg_perr("erase_to_zero can be \"yes\" or \"no\"\n");
346 return 1;
347 }
348 }
349
hailfinger6ead7222010-11-01 22:07:04 +0000350 free(tmp);
351 flashchip_contents = malloc(emu_chip_size);
352 if (!flashchip_contents) {
353 msg_perr("Out of memory!\n");
354 return 1;
355 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000356
David Hendricks0eda2a82014-09-12 16:32:05 -0700357 msg_pdbg("Filling fake flash chip with 0x%02x, size %i\n",
358 erase_to_zero ? 0x00 : 0xff, emu_chip_size);
359 memset(flashchip_contents, erase_to_zero ? 0x00 : 0xff, emu_chip_size);
hailfinger6ead7222010-11-01 22:07:04 +0000360
hailfinger6ead7222010-11-01 22:07:04 +0000361 if (!emu_persistent_image) {
362 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000363 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000364 }
365 if (!stat(emu_persistent_image, &image_stat)) {
366 msg_pdbg("Found persistent image %s, size %li ",
367 emu_persistent_image, (long)image_stat.st_size);
368 if (image_stat.st_size == emu_chip_size) {
369 msg_pdbg("matches.\n");
370 msg_pdbg("Reading %s\n", emu_persistent_image);
371 read_buf_from_file(flashchip_contents, emu_chip_size,
372 emu_persistent_image);
373 } else {
374 msg_pdbg("doesn't match.\n");
375 }
376 }
377#endif
hailfingera9df33c2009-05-09 00:54:55 +0000378
dhendrix0ffc2eb2011-06-14 01:35:36 +0000379dummy_init_out:
380 if (register_shutdown(dummy_shutdown, NULL)) {
hailfinger6ead7222010-11-01 22:07:04 +0000381 free(flashchip_contents);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000382 return 1;
hailfinger6ead7222010-11-01 22:07:04 +0000383 }
hailfinger76bb7e92011-11-09 23:40:00 +0000384 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
385 register_par_programmer(&par_programmer_dummy,
386 dummy_buses_supported &
387 (BUS_PARALLEL | BUS_LPC |
388 BUS_FWH));
389 if (dummy_buses_supported & BUS_SPI)
390 register_spi_programmer(&spi_programmer_dummyflasher);
391
hailfingera9df33c2009-05-09 00:54:55 +0000392 return 0;
393}
394
hailfinger11ae3c42009-05-11 14:13:25 +0000395void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
396{
hailfinger50c335f2010-01-09 04:32:23 +0000397 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
398 __func__, descr, (unsigned long)len, phys_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000399 return (void *)phys_addr;
400}
401
402void dummy_unmap(void *virt_addr, size_t len)
403{
hailfinger50c335f2010-01-09 04:32:23 +0000404 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
405 __func__, (unsigned long)len, virt_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000406}
407
hailfinger82719632009-05-16 21:22:56 +0000408void dummy_chip_writeb(uint8_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000409{
hailfinger50c335f2010-01-09 04:32:23 +0000410 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000411}
412
hailfinger82719632009-05-16 21:22:56 +0000413void dummy_chip_writew(uint16_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000414{
hailfinger50c335f2010-01-09 04:32:23 +0000415 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000416}
417
hailfinger82719632009-05-16 21:22:56 +0000418void dummy_chip_writel(uint32_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000419{
hailfinger50c335f2010-01-09 04:32:23 +0000420 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000421}
422
hailfinger9d987ef2009-06-05 18:32:07 +0000423void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
424{
425 size_t i;
hailfinger50c335f2010-01-09 04:32:23 +0000426 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
427 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000428 for (i = 0; i < len; i++) {
429 if ((i % 16) == 0)
hailfinger50c335f2010-01-09 04:32:23 +0000430 msg_pspew("\n");
431 msg_pspew("%02x ", buf[i]);
hailfinger9d987ef2009-06-05 18:32:07 +0000432 }
433}
434
hailfinger82719632009-05-16 21:22:56 +0000435uint8_t dummy_chip_readb(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000436{
hailfinger50c335f2010-01-09 04:32:23 +0000437 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000438 return 0xff;
439}
440
hailfinger82719632009-05-16 21:22:56 +0000441uint16_t dummy_chip_readw(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000442{
hailfinger50c335f2010-01-09 04:32:23 +0000443 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000444 return 0xffff;
445}
446
hailfinger82719632009-05-16 21:22:56 +0000447uint32_t dummy_chip_readl(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000448{
hailfinger50c335f2010-01-09 04:32:23 +0000449 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000450 return 0xffffffff;
451}
452
hailfinger9d987ef2009-06-05 18:32:07 +0000453void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
454{
hailfinger50c335f2010-01-09 04:32:23 +0000455 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
456 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000457 memset(buf, 0xff, len);
458 return;
459}
460
hailfinger6ead7222010-11-01 22:07:04 +0000461#if EMULATE_SPI_CHIP
462static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
463 const unsigned char *writearr, unsigned char *readarr)
464{
stefanctc5eb8a92011-11-23 09:13:48 +0000465 unsigned int offs;
466 static int unsigned aai_offs;
hailfinger6ead7222010-11-01 22:07:04 +0000467 static int aai_active = 0;
468
469 if (writecnt == 0) {
470 msg_perr("No command sent to the chip!\n");
471 return 1;
472 }
473 /* TODO: Implement command blacklists here. */
474 switch (writearr[0]) {
475 case JEDEC_RES:
476 if (emu_chip != EMULATE_ST_M25P10_RES)
477 break;
478 /* Respond with ST_M25P10_RES. */
479 if (readcnt > 0)
480 readarr[0] = 0x10;
481 break;
482 case JEDEC_REMS:
483 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
484 break;
485 /* Respond with SST_SST25VF040_REMS. */
486 if (readcnt > 0)
487 readarr[0] = 0xbf;
488 if (readcnt > 1)
489 readarr[1] = 0x44;
490 break;
491 case JEDEC_RDID:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800492 if (emu_chip == EMULATE_SST_SST25VF032B) {
493 /* Respond with SST_SST25VF032B. */
494 if (readcnt > 0)
495 readarr[0] = 0xbf;
496 if (readcnt > 1)
497 readarr[1] = 0x25;
498 if (readcnt > 2)
499 readarr[2] = 0x4a;
500 } else if (emu_chip == EMULATE_VARIABLE_SIZE) {
501 const uint16_t man_id = VARIABLE_SIZE_MANUF_ID;
502 const uint16_t dev_id = VARIABLE_SIZE_DEVICE_ID;
503 if (readcnt > 0) readarr[0] = man_id >> 8;
504 if (readcnt > 1) readarr[1] = man_id & 0xff;
505 if (readcnt > 2) readarr[2] = dev_id >> 8;
506 if (readcnt > 3) readarr[3] = dev_id & 0xff;
507 }
hailfinger6ead7222010-11-01 22:07:04 +0000508 break;
509 case JEDEC_RDSR:
510 memset(readarr, 0, readcnt);
511 if (aai_active)
512 memset(readarr, 1 << 6, readcnt);
513 break;
514 case JEDEC_READ:
515 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
516 /* Truncate to emu_chip_size. */
517 offs %= emu_chip_size;
518 if (readcnt > 0)
519 memcpy(readarr, flashchip_contents + offs, readcnt);
520 break;
521 case JEDEC_BYTE_PROGRAM:
522 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
523 /* Truncate to emu_chip_size. */
524 offs %= emu_chip_size;
525 if (writecnt < 5) {
526 msg_perr("BYTE PROGRAM size too short!\n");
527 return 1;
528 }
529 if (writecnt - 4 > emu_max_byteprogram_size) {
530 msg_perr("Max BYTE PROGRAM size exceeded!\n");
531 return 1;
532 }
533 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
Simon Glasscf253a72013-07-10 21:10:11 -0700534 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000535 break;
536 case JEDEC_AAI_WORD_PROGRAM:
537 if (!emu_max_aai_size)
538 break;
539 if (!aai_active) {
540 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
541 msg_perr("Initial AAI WORD PROGRAM size too "
542 "short!\n");
543 return 1;
544 }
545 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
546 msg_perr("Initial AAI WORD PROGRAM size too "
547 "long!\n");
548 return 1;
549 }
550 aai_active = 1;
551 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
552 writearr[3];
553 /* Truncate to emu_chip_size. */
554 aai_offs %= emu_chip_size;
555 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
556 aai_offs += 2;
557 } else {
558 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
559 msg_perr("Continuation AAI WORD PROGRAM size "
560 "too short!\n");
561 return 1;
562 }
563 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
564 msg_perr("Continuation AAI WORD PROGRAM size "
565 "too long!\n");
566 return 1;
567 }
568 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
569 aai_offs += 2;
570 }
Simon Glasscf253a72013-07-10 21:10:11 -0700571 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000572 break;
573 case JEDEC_WRDI:
574 if (!emu_max_aai_size)
575 break;
576 aai_active = 0;
577 break;
578 case JEDEC_SE:
579 if (!emu_jedec_se_size)
580 break;
581 if (writecnt != JEDEC_SE_OUTSIZE) {
582 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
583 return 1;
584 }
585 if (readcnt != JEDEC_SE_INSIZE) {
586 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
587 return 1;
588 }
589 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
590 if (offs & (emu_jedec_se_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000591 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000592 offs &= ~(emu_jedec_se_size - 1);
593 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700594 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000595 break;
596 case JEDEC_BE_52:
597 if (!emu_jedec_be_52_size)
598 break;
599 if (writecnt != JEDEC_BE_52_OUTSIZE) {
600 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
601 return 1;
602 }
603 if (readcnt != JEDEC_BE_52_INSIZE) {
604 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
605 return 1;
606 }
607 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
608 if (offs & (emu_jedec_be_52_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000609 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000610 offs &= ~(emu_jedec_be_52_size - 1);
611 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700612 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000613 break;
614 case JEDEC_BE_D8:
615 if (!emu_jedec_be_d8_size)
616 break;
617 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
618 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
619 return 1;
620 }
621 if (readcnt != JEDEC_BE_D8_INSIZE) {
622 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
623 return 1;
624 }
625 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
626 if (offs & (emu_jedec_be_d8_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000627 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000628 offs &= ~(emu_jedec_be_d8_size - 1);
629 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
630 break;
631 case JEDEC_CE_60:
632 if (!emu_jedec_ce_60_size)
633 break;
634 if (writecnt != JEDEC_CE_60_OUTSIZE) {
635 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
636 return 1;
637 }
638 if (readcnt != JEDEC_CE_60_INSIZE) {
639 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
640 return 1;
641 }
hailfingere53f5e42011-02-04 22:52:04 +0000642 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000643 /* emu_jedec_ce_60_size is emu_chip_size. */
hailfingere53f5e42011-02-04 22:52:04 +0000644 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700645 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000646 break;
647 case JEDEC_CE_C7:
648 if (!emu_jedec_ce_c7_size)
649 break;
650 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
651 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
652 return 1;
653 }
654 if (readcnt != JEDEC_CE_C7_INSIZE) {
655 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
656 return 1;
657 }
hailfingere53f5e42011-02-04 22:52:04 +0000658 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000659 /* emu_jedec_ce_c7_size is emu_chip_size. */
660 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700661 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000662 break;
663 default:
664 /* No special response. */
665 break;
666 }
667 return 0;
668}
669#endif
670
mkarcherd264e9e2011-05-11 17:07:07 +0000671static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
hailfingerf91e3b52009-05-14 12:59:36 +0000672 const unsigned char *writearr, unsigned char *readarr)
673{
674 int i;
675
hailfinger50c335f2010-01-09 04:32:23 +0000676 msg_pspew("%s:", __func__);
hailfingerf91e3b52009-05-14 12:59:36 +0000677
hailfinger50c335f2010-01-09 04:32:23 +0000678 msg_pspew(" writing %u bytes:", writecnt);
hailfingerf91e3b52009-05-14 12:59:36 +0000679 for (i = 0; i < writecnt; i++)
hailfinger50c335f2010-01-09 04:32:23 +0000680 msg_pspew(" 0x%02x", writearr[i]);
hailfingerf91e3b52009-05-14 12:59:36 +0000681
hailfinger6ead7222010-11-01 22:07:04 +0000682 /* Response for unknown commands and missing chip is 0xff. */
683 memset(readarr, 0xff, readcnt);
684#if EMULATE_SPI_CHIP
685 switch (emu_chip) {
686 case EMULATE_ST_M25P10_RES:
687 case EMULATE_SST_SST25VF040_REMS:
688 case EMULATE_SST_SST25VF032B:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800689 case EMULATE_VARIABLE_SIZE:
hailfinger6ead7222010-11-01 22:07:04 +0000690 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
691 readarr)) {
692 msg_perr("Invalid command sent to flash chip!\n");
693 return 1;
694 }
695 break;
696 default:
697 break;
698 }
699#endif
hailfinger50c335f2010-01-09 04:32:23 +0000700 msg_pspew(" reading %u bytes:", readcnt);
uwe8d342eb2011-07-28 08:13:25 +0000701 for (i = 0; i < readcnt; i++)
hailfinger6ead7222010-11-01 22:07:04 +0000702 msg_pspew(" 0x%02x", readarr[i]);
hailfinger50c335f2010-01-09 04:32:23 +0000703 msg_pspew("\n");
David Hendricks84377002014-09-09 16:09:31 -0700704
705 programmer_delay((writecnt + readcnt) * delay_us);
hailfingerf91e3b52009-05-14 12:59:36 +0000706 return 0;
707}
hailfingera8727712010-06-20 10:58:32 +0000708
uwe8d342eb2011-07-28 08:13:25 +0000709static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000710 unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000711{
hailfinger6ead7222010-11-01 22:07:04 +0000712 return spi_write_chunked(flash, buf, start, len,
713 spi_write_256_chunksize);
hailfingerc7d06c62010-07-14 16:19:05 +0000714}
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800715
716#if EMULATE_CHIP && EMULATE_SPI_CHIP
717int probe_variable_size(struct flashchip *flash)
718{
719 int i;
720
721 /* Skip the probing if we don't emulate this chip. */
722 if (emu_chip != EMULATE_VARIABLE_SIZE)
723 return 0;
724
725 /*
726 * This will break if one day flashchip becomes read-only.
727 * Once that happens, we need to have special hacks in functions:
728 *
729 * erase_and_write_flash() in flashrom.c
730 * read_flash_to_file()
731 * handle_romentries()
732 * ...
733 *
734 * Search "total_size * 1024" in code.
735 */
736 if (emu_chip_size % 1024)
737 msg_perr("%s: emu_chip_size is not multipler of 1024.\n",
738 __func__);
739 flash->total_size = emu_chip_size / 1024;
740 msg_cdbg("%s: set flash->total_size to %dK bytes.\n", __func__,
741 flash->total_size);
742
David Hendricks0eda2a82014-09-12 16:32:05 -0700743 if (erase_to_zero)
744 flash->feature_bits |= FEATURE_ERASE_TO_ZERO;
745
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800746 /* Update eraser count */
747 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
748 struct block_eraser *eraser = &flash->block_erasers[i];
749 if (eraser->block_erase == NULL)
750 break;
751
752 eraser->eraseblocks[0].count = emu_chip_size /
753 eraser->eraseblocks[0].size;
754 msg_cdbg("%s: eraser.size=%d, .count=%d\n",
755 __func__, eraser->eraseblocks[0].size,
756 eraser->eraseblocks[0].count);
757 }
758
759 return 1;
760}
761#endif