blob: d091e54c854e2a75ff0f89f19d87ed821923ee38 [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? */
hailfinger6ead7222010-11-01 22:07:04 +000061#if EMULATE_SPI_CHIP
stefanctc5eb8a92011-11-23 09:13:48 +000062static unsigned int emu_max_byteprogram_size = 0;
63static unsigned int emu_max_aai_size = 0;
64static unsigned int emu_jedec_se_size = 0;
65static unsigned int emu_jedec_be_52_size = 0;
66static unsigned int emu_jedec_be_d8_size = 0;
67static unsigned int emu_jedec_ce_60_size = 0;
68static unsigned int emu_jedec_ce_c7_size = 0;
hailfinger6ead7222010-11-01 22:07:04 +000069#endif
70#endif
71
stefanctc5eb8a92011-11-23 09:13:48 +000072static unsigned int spi_write_256_chunksize = 256;
hailfinger6ead7222010-11-01 22:07:04 +000073
David Hendricks84377002014-09-09 16:09:31 -070074/* If "freq" parameter is passed in from command line, commands will delay
75 * for this period before returning. */
76static unsigned long int delay_us = 0;
77
mkarcherd264e9e2011-05-11 17:07:07 +000078static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
79 const unsigned char *writearr, unsigned char *readarr);
uwe8d342eb2011-07-28 08:13:25 +000080static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000081 unsigned int start, unsigned int len);
mkarcherd264e9e2011-05-11 17:07:07 +000082
83static const struct spi_programmer spi_programmer_dummyflasher = {
uwe8d342eb2011-07-28 08:13:25 +000084 .type = SPI_CONTROLLER_DUMMY,
85 .max_data_read = MAX_DATA_READ_UNLIMITED,
86 .max_data_write = MAX_DATA_UNSPECIFIED,
87 .command = dummy_spi_send_command,
88 .multicommand = default_spi_send_multicommand,
89 .read = default_spi_read,
90 .write_256 = dummy_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +000091};
dhendrix0ffc2eb2011-06-14 01:35:36 +000092
hailfinger76bb7e92011-11-09 23:40:00 +000093static const struct par_programmer par_programmer_dummy = {
94 .chip_readb = dummy_chip_readb,
95 .chip_readw = dummy_chip_readw,
96 .chip_readl = dummy_chip_readl,
97 .chip_readn = dummy_chip_readn,
98 .chip_writeb = dummy_chip_writeb,
99 .chip_writew = dummy_chip_writew,
100 .chip_writel = dummy_chip_writel,
101 .chip_writen = dummy_chip_writen,
102};
103
104enum chipbustype dummy_buses_supported = BUS_NONE;
105
dhendrix0ffc2eb2011-06-14 01:35:36 +0000106static int dummy_shutdown(void *data)
107{
108 msg_pspew("%s\n", __func__);
109#if EMULATE_CHIP
110 if (emu_chip != EMULATE_NONE) {
Simon Glasscf253a72013-07-10 21:10:11 -0700111 if (emu_persistent_image && emu_modified) {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000112 msg_pdbg("Writing %s\n", emu_persistent_image);
113 write_buf_to_file(flashchip_contents, emu_chip_size,
114 emu_persistent_image);
115 }
116 free(flashchip_contents);
117 }
118#endif
119 return 0;
120}
121
Simon Glassd2c64a22013-07-03 22:05:21 +0900122/* Values for the 'size' parameter */
123enum {
124 SIZE_UNKNOWN = -1,
125 SIZE_AUTO = -2,
126};
127
hailfingera9df33c2009-05-09 00:54:55 +0000128int dummy_init(void)
129{
hailfinger1ef766d2010-07-06 09:55:48 +0000130 char *bustext = NULL;
hailfinger6ead7222010-11-01 22:07:04 +0000131 char *tmp = NULL;
132#if EMULATE_CHIP
133 struct stat image_stat;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800134#if EMULATE_SPI_CHIP
Simon Glassd2c64a22013-07-03 22:05:21 +0900135 int size = SIZE_UNKNOWN; /* size for generic chip */
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800136#endif
hailfinger6ead7222010-11-01 22:07:04 +0000137#endif
Simon Glassd2c64a22013-07-03 22:05:21 +0900138 int image_size = SIZE_UNKNOWN;
hailfinger1ef766d2010-07-06 09:55:48 +0000139
hailfinger50c335f2010-01-09 04:32:23 +0000140 msg_pspew("%s\n", __func__);
hailfinger668f3502009-06-01 00:02:11 +0000141
hailfingerddeb4ac2010-07-08 10:13:37 +0000142 bustext = extract_programmer_param("bus");
hailfinger1ef766d2010-07-06 09:55:48 +0000143 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
144 if (!bustext)
145 bustext = strdup("parallel+lpc+fwh+spi");
hailfinger668f3502009-06-01 00:02:11 +0000146 /* Convert the parameters to lowercase. */
hailfinger1ef766d2010-07-06 09:55:48 +0000147 tolower_string(bustext);
hailfinger668f3502009-06-01 00:02:11 +0000148
hailfinger76bb7e92011-11-09 23:40:00 +0000149 dummy_buses_supported = BUS_NONE;
hailfinger1ef766d2010-07-06 09:55:48 +0000150 if (strstr(bustext, "parallel")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000151 dummy_buses_supported |= BUS_PARALLEL;
hailfinger50c335f2010-01-09 04:32:23 +0000152 msg_pdbg("Enabling support for %s flash.\n", "parallel");
hailfinger668f3502009-06-01 00:02:11 +0000153 }
hailfinger1ef766d2010-07-06 09:55:48 +0000154 if (strstr(bustext, "lpc")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000155 dummy_buses_supported |= BUS_LPC;
hailfinger50c335f2010-01-09 04:32:23 +0000156 msg_pdbg("Enabling support for %s flash.\n", "LPC");
hailfinger668f3502009-06-01 00:02:11 +0000157 }
hailfinger1ef766d2010-07-06 09:55:48 +0000158 if (strstr(bustext, "fwh")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000159 dummy_buses_supported |= BUS_FWH;
hailfinger50c335f2010-01-09 04:32:23 +0000160 msg_pdbg("Enabling support for %s flash.\n", "FWH");
hailfinger668f3502009-06-01 00:02:11 +0000161 }
hailfinger1ef766d2010-07-06 09:55:48 +0000162 if (strstr(bustext, "spi")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000163 dummy_buses_supported |= BUS_SPI;
hailfinger50c335f2010-01-09 04:32:23 +0000164 msg_pdbg("Enabling support for %s flash.\n", "SPI");
hailfinger668f3502009-06-01 00:02:11 +0000165 }
hailfinger76bb7e92011-11-09 23:40:00 +0000166 if (dummy_buses_supported == BUS_NONE)
hailfinger50c335f2010-01-09 04:32:23 +0000167 msg_pdbg("Support for all flash bus types disabled.\n");
hailfinger1ef766d2010-07-06 09:55:48 +0000168 free(bustext);
hailfinger6ead7222010-11-01 22:07:04 +0000169
170 tmp = extract_programmer_param("spi_write_256_chunksize");
171 if (tmp) {
172 spi_write_256_chunksize = atoi(tmp);
173 free(tmp);
174 if (spi_write_256_chunksize < 1) {
175 msg_perr("invalid spi_write_256_chunksize\n");
176 return 1;
177 }
178 }
179
David Hendricks84377002014-09-09 16:09:31 -0700180 /* frequency to emulate in Hz (default), KHz, or MHz */
181 tmp = extract_programmer_param("freq");
182 if (tmp) {
183 unsigned long int freq;
184 char *units = tmp;
185 char *end = tmp + strlen(tmp);
186
187 errno = 0;
188 freq = strtoul(tmp, &units, 0);
189 if (errno) {
190 msg_perr("Invalid frequency \"%s\", %s\n",
191 tmp, strerror(errno));
192 goto dummy_init_out;
193 }
194
195 if ((units > tmp) && (units < end)) {
196 int units_valid = 0;
197
198 if (units < end - 3) {
199 ;
200 } else if (units == end - 2) {
201 if (!strcasecmp(units, "hz"))
202 units_valid = 1;
203 } else if (units == end - 3) {
204 if (!strcasecmp(units, "khz")) {
205 freq *= 1000;
206 units_valid = 1;
207 } else if (!strcasecmp(units, "mhz")) {
208 freq *= 1000000;
209 units_valid = 1;
210 }
211 }
212
213 if (!units_valid) {
214 msg_perr("Invalid units: %s\n", units);
215 return 1;
216 }
217 }
218
219 /* Assume we only work with bytes and transfer at 1 bit/Hz */
220 delay_us = (1000000 * 8) / freq;
221 }
222
hailfinger6ead7222010-11-01 22:07:04 +0000223#if EMULATE_CHIP
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800224#if EMULATE_SPI_CHIP
225 tmp = extract_programmer_param("size");
226 if (tmp) {
227 int multiplier = 1;
Simon Glassd2c64a22013-07-03 22:05:21 +0900228 if (!strcmp(tmp, "auto"))
229 size = SIZE_AUTO;
230 else if (strlen(tmp)) {
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800231 int remove_last_char = 1;
232 switch (tmp[strlen(tmp) - 1]) {
233 case 'k': case 'K':
234 multiplier = 1024;
235 break;
236 case 'm': case 'M':
237 multiplier = 1024 * 1024;
238 break;
239 default:
240 remove_last_char = 0;
241 break;
242 }
243 if (remove_last_char) tmp[strlen(tmp) - 1] = '\0';
Simon Glassd2c64a22013-07-03 22:05:21 +0900244 size = atoi(tmp) * multiplier;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800245 }
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800246 }
247#endif
248
hailfinger6ead7222010-11-01 22:07:04 +0000249 tmp = extract_programmer_param("emulate");
250 if (!tmp) {
251 msg_pdbg("Not emulating any flash chip.\n");
252 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000253 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000254 }
David Hendricks84377002014-09-09 16:09:31 -0700255
hailfinger6ead7222010-11-01 22:07:04 +0000256#if EMULATE_SPI_CHIP
257 if (!strcmp(tmp, "M25P10.RES")) {
258 emu_chip = EMULATE_ST_M25P10_RES;
259 emu_chip_size = 128 * 1024;
260 emu_max_byteprogram_size = 128;
261 emu_max_aai_size = 0;
262 emu_jedec_se_size = 0;
263 emu_jedec_be_52_size = 0;
264 emu_jedec_be_d8_size = 32 * 1024;
265 emu_jedec_ce_60_size = 0;
266 emu_jedec_ce_c7_size = emu_chip_size;
267 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
268 "write)\n");
269 }
270 if (!strcmp(tmp, "SST25VF040.REMS")) {
271 emu_chip = EMULATE_SST_SST25VF040_REMS;
272 emu_chip_size = 512 * 1024;
273 emu_max_byteprogram_size = 1;
274 emu_max_aai_size = 0;
275 emu_jedec_se_size = 4 * 1024;
276 emu_jedec_be_52_size = 32 * 1024;
277 emu_jedec_be_d8_size = 0;
278 emu_jedec_ce_60_size = emu_chip_size;
279 emu_jedec_ce_c7_size = 0;
280 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
281 "byte write)\n");
282 }
283 if (!strcmp(tmp, "SST25VF032B")) {
284 emu_chip = EMULATE_SST_SST25VF032B;
285 emu_chip_size = 4 * 1024 * 1024;
286 emu_max_byteprogram_size = 1;
287 emu_max_aai_size = 2;
288 emu_jedec_se_size = 4 * 1024;
289 emu_jedec_be_52_size = 32 * 1024;
290 emu_jedec_be_d8_size = 64 * 1024;
291 emu_jedec_ce_60_size = emu_chip_size;
292 emu_jedec_ce_c7_size = emu_chip_size;
293 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
294 "write)\n");
295 }
Simon Glassd2c64a22013-07-03 22:05:21 +0900296 emu_persistent_image = extract_programmer_param("image");
297 if (!stat(emu_persistent_image, &image_stat))
298 image_size = image_stat.st_size;
299
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800300 if (!strncmp(tmp, VARIABLE_SIZE_CHIP_NAME,
301 strlen(VARIABLE_SIZE_CHIP_NAME))) {
Simon Glassd2c64a22013-07-03 22:05:21 +0900302 if (size == SIZE_UNKNOWN) {
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800303 msg_perr("%s: the size parameter is not given.\n",
304 __func__);
305 free(tmp);
306 return 1;
Simon Glassd2c64a22013-07-03 22:05:21 +0900307 } else if (size == SIZE_AUTO) {
308 if (image_size == SIZE_UNKNOWN) {
309 msg_perr("%s: no image so cannot use automatic size.\n",
310 __func__);
311 free(tmp);
312 return 1;
313 }
314 size = image_size;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800315 }
316 emu_chip = EMULATE_VARIABLE_SIZE;
317 emu_chip_size = size;
318 emu_max_byteprogram_size = 256;
319 emu_max_aai_size = 0;
320 emu_jedec_se_size = 4 * 1024;
321 emu_jedec_be_52_size = 32 * 1024;
322 emu_jedec_be_d8_size = 64 * 1024;
323 emu_jedec_ce_60_size = emu_chip_size;
324 emu_jedec_ce_c7_size = emu_chip_size;
325 msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n",
326 emu_chip_size);
327 }
hailfinger6ead7222010-11-01 22:07:04 +0000328#endif
329 if (emu_chip == EMULATE_NONE) {
330 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
331 free(tmp);
332 return 1;
333 }
334 free(tmp);
335 flashchip_contents = malloc(emu_chip_size);
336 if (!flashchip_contents) {
337 msg_perr("Out of memory!\n");
338 return 1;
339 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000340
hailfinger6ead7222010-11-01 22:07:04 +0000341 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
342 memset(flashchip_contents, 0xff, emu_chip_size);
343
hailfinger6ead7222010-11-01 22:07:04 +0000344 if (!emu_persistent_image) {
345 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000346 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000347 }
348 if (!stat(emu_persistent_image, &image_stat)) {
349 msg_pdbg("Found persistent image %s, size %li ",
350 emu_persistent_image, (long)image_stat.st_size);
351 if (image_stat.st_size == emu_chip_size) {
352 msg_pdbg("matches.\n");
353 msg_pdbg("Reading %s\n", emu_persistent_image);
354 read_buf_from_file(flashchip_contents, emu_chip_size,
355 emu_persistent_image);
356 } else {
357 msg_pdbg("doesn't match.\n");
358 }
359 }
360#endif
hailfingera9df33c2009-05-09 00:54:55 +0000361
dhendrix0ffc2eb2011-06-14 01:35:36 +0000362dummy_init_out:
363 if (register_shutdown(dummy_shutdown, NULL)) {
hailfinger6ead7222010-11-01 22:07:04 +0000364 free(flashchip_contents);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000365 return 1;
hailfinger6ead7222010-11-01 22:07:04 +0000366 }
hailfinger76bb7e92011-11-09 23:40:00 +0000367 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
368 register_par_programmer(&par_programmer_dummy,
369 dummy_buses_supported &
370 (BUS_PARALLEL | BUS_LPC |
371 BUS_FWH));
372 if (dummy_buses_supported & BUS_SPI)
373 register_spi_programmer(&spi_programmer_dummyflasher);
374
hailfingera9df33c2009-05-09 00:54:55 +0000375 return 0;
376}
377
hailfinger11ae3c42009-05-11 14:13:25 +0000378void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
379{
hailfinger50c335f2010-01-09 04:32:23 +0000380 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
381 __func__, descr, (unsigned long)len, phys_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000382 return (void *)phys_addr;
383}
384
385void dummy_unmap(void *virt_addr, size_t len)
386{
hailfinger50c335f2010-01-09 04:32:23 +0000387 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
388 __func__, (unsigned long)len, virt_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000389}
390
hailfinger82719632009-05-16 21:22:56 +0000391void dummy_chip_writeb(uint8_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000392{
hailfinger50c335f2010-01-09 04:32:23 +0000393 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000394}
395
hailfinger82719632009-05-16 21:22:56 +0000396void dummy_chip_writew(uint16_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000397{
hailfinger50c335f2010-01-09 04:32:23 +0000398 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000399}
400
hailfinger82719632009-05-16 21:22:56 +0000401void dummy_chip_writel(uint32_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000402{
hailfinger50c335f2010-01-09 04:32:23 +0000403 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000404}
405
hailfinger9d987ef2009-06-05 18:32:07 +0000406void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
407{
408 size_t i;
hailfinger50c335f2010-01-09 04:32:23 +0000409 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
410 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000411 for (i = 0; i < len; i++) {
412 if ((i % 16) == 0)
hailfinger50c335f2010-01-09 04:32:23 +0000413 msg_pspew("\n");
414 msg_pspew("%02x ", buf[i]);
hailfinger9d987ef2009-06-05 18:32:07 +0000415 }
416}
417
hailfinger82719632009-05-16 21:22:56 +0000418uint8_t dummy_chip_readb(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000419{
hailfinger50c335f2010-01-09 04:32:23 +0000420 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000421 return 0xff;
422}
423
hailfinger82719632009-05-16 21:22:56 +0000424uint16_t dummy_chip_readw(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000425{
hailfinger50c335f2010-01-09 04:32:23 +0000426 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000427 return 0xffff;
428}
429
hailfinger82719632009-05-16 21:22:56 +0000430uint32_t dummy_chip_readl(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000431{
hailfinger50c335f2010-01-09 04:32:23 +0000432 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000433 return 0xffffffff;
434}
435
hailfinger9d987ef2009-06-05 18:32:07 +0000436void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
437{
hailfinger50c335f2010-01-09 04:32:23 +0000438 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
439 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000440 memset(buf, 0xff, len);
441 return;
442}
443
hailfinger6ead7222010-11-01 22:07:04 +0000444#if EMULATE_SPI_CHIP
445static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
446 const unsigned char *writearr, unsigned char *readarr)
447{
stefanctc5eb8a92011-11-23 09:13:48 +0000448 unsigned int offs;
449 static int unsigned aai_offs;
hailfinger6ead7222010-11-01 22:07:04 +0000450 static int aai_active = 0;
451
452 if (writecnt == 0) {
453 msg_perr("No command sent to the chip!\n");
454 return 1;
455 }
456 /* TODO: Implement command blacklists here. */
457 switch (writearr[0]) {
458 case JEDEC_RES:
459 if (emu_chip != EMULATE_ST_M25P10_RES)
460 break;
461 /* Respond with ST_M25P10_RES. */
462 if (readcnt > 0)
463 readarr[0] = 0x10;
464 break;
465 case JEDEC_REMS:
466 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
467 break;
468 /* Respond with SST_SST25VF040_REMS. */
469 if (readcnt > 0)
470 readarr[0] = 0xbf;
471 if (readcnt > 1)
472 readarr[1] = 0x44;
473 break;
474 case JEDEC_RDID:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800475 if (emu_chip == EMULATE_SST_SST25VF032B) {
476 /* Respond with SST_SST25VF032B. */
477 if (readcnt > 0)
478 readarr[0] = 0xbf;
479 if (readcnt > 1)
480 readarr[1] = 0x25;
481 if (readcnt > 2)
482 readarr[2] = 0x4a;
483 } else if (emu_chip == EMULATE_VARIABLE_SIZE) {
484 const uint16_t man_id = VARIABLE_SIZE_MANUF_ID;
485 const uint16_t dev_id = VARIABLE_SIZE_DEVICE_ID;
486 if (readcnt > 0) readarr[0] = man_id >> 8;
487 if (readcnt > 1) readarr[1] = man_id & 0xff;
488 if (readcnt > 2) readarr[2] = dev_id >> 8;
489 if (readcnt > 3) readarr[3] = dev_id & 0xff;
490 }
hailfinger6ead7222010-11-01 22:07:04 +0000491 break;
492 case JEDEC_RDSR:
493 memset(readarr, 0, readcnt);
494 if (aai_active)
495 memset(readarr, 1 << 6, readcnt);
496 break;
497 case JEDEC_READ:
498 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
499 /* Truncate to emu_chip_size. */
500 offs %= emu_chip_size;
501 if (readcnt > 0)
502 memcpy(readarr, flashchip_contents + offs, readcnt);
503 break;
504 case JEDEC_BYTE_PROGRAM:
505 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
506 /* Truncate to emu_chip_size. */
507 offs %= emu_chip_size;
508 if (writecnt < 5) {
509 msg_perr("BYTE PROGRAM size too short!\n");
510 return 1;
511 }
512 if (writecnt - 4 > emu_max_byteprogram_size) {
513 msg_perr("Max BYTE PROGRAM size exceeded!\n");
514 return 1;
515 }
516 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
Simon Glasscf253a72013-07-10 21:10:11 -0700517 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000518 break;
519 case JEDEC_AAI_WORD_PROGRAM:
520 if (!emu_max_aai_size)
521 break;
522 if (!aai_active) {
523 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
524 msg_perr("Initial AAI WORD PROGRAM size too "
525 "short!\n");
526 return 1;
527 }
528 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
529 msg_perr("Initial AAI WORD PROGRAM size too "
530 "long!\n");
531 return 1;
532 }
533 aai_active = 1;
534 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
535 writearr[3];
536 /* Truncate to emu_chip_size. */
537 aai_offs %= emu_chip_size;
538 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
539 aai_offs += 2;
540 } else {
541 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
542 msg_perr("Continuation AAI WORD PROGRAM size "
543 "too short!\n");
544 return 1;
545 }
546 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
547 msg_perr("Continuation AAI WORD PROGRAM size "
548 "too long!\n");
549 return 1;
550 }
551 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
552 aai_offs += 2;
553 }
Simon Glasscf253a72013-07-10 21:10:11 -0700554 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000555 break;
556 case JEDEC_WRDI:
557 if (!emu_max_aai_size)
558 break;
559 aai_active = 0;
560 break;
561 case JEDEC_SE:
562 if (!emu_jedec_se_size)
563 break;
564 if (writecnt != JEDEC_SE_OUTSIZE) {
565 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
566 return 1;
567 }
568 if (readcnt != JEDEC_SE_INSIZE) {
569 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
570 return 1;
571 }
572 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
573 if (offs & (emu_jedec_se_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000574 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000575 offs &= ~(emu_jedec_se_size - 1);
576 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700577 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000578 break;
579 case JEDEC_BE_52:
580 if (!emu_jedec_be_52_size)
581 break;
582 if (writecnt != JEDEC_BE_52_OUTSIZE) {
583 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
584 return 1;
585 }
586 if (readcnt != JEDEC_BE_52_INSIZE) {
587 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
588 return 1;
589 }
590 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
591 if (offs & (emu_jedec_be_52_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000592 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000593 offs &= ~(emu_jedec_be_52_size - 1);
594 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700595 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000596 break;
597 case JEDEC_BE_D8:
598 if (!emu_jedec_be_d8_size)
599 break;
600 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
601 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
602 return 1;
603 }
604 if (readcnt != JEDEC_BE_D8_INSIZE) {
605 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
606 return 1;
607 }
608 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
609 if (offs & (emu_jedec_be_d8_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000610 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000611 offs &= ~(emu_jedec_be_d8_size - 1);
612 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
613 break;
614 case JEDEC_CE_60:
615 if (!emu_jedec_ce_60_size)
616 break;
617 if (writecnt != JEDEC_CE_60_OUTSIZE) {
618 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
619 return 1;
620 }
621 if (readcnt != JEDEC_CE_60_INSIZE) {
622 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
623 return 1;
624 }
hailfingere53f5e42011-02-04 22:52:04 +0000625 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000626 /* emu_jedec_ce_60_size is emu_chip_size. */
hailfingere53f5e42011-02-04 22:52:04 +0000627 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700628 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000629 break;
630 case JEDEC_CE_C7:
631 if (!emu_jedec_ce_c7_size)
632 break;
633 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
634 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
635 return 1;
636 }
637 if (readcnt != JEDEC_CE_C7_INSIZE) {
638 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
639 return 1;
640 }
hailfingere53f5e42011-02-04 22:52:04 +0000641 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000642 /* emu_jedec_ce_c7_size is emu_chip_size. */
643 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
Simon Glasscf253a72013-07-10 21:10:11 -0700644 emu_modified = 1;
hailfinger6ead7222010-11-01 22:07:04 +0000645 break;
646 default:
647 /* No special response. */
648 break;
649 }
650 return 0;
651}
652#endif
653
mkarcherd264e9e2011-05-11 17:07:07 +0000654static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
hailfingerf91e3b52009-05-14 12:59:36 +0000655 const unsigned char *writearr, unsigned char *readarr)
656{
657 int i;
658
hailfinger50c335f2010-01-09 04:32:23 +0000659 msg_pspew("%s:", __func__);
hailfingerf91e3b52009-05-14 12:59:36 +0000660
hailfinger50c335f2010-01-09 04:32:23 +0000661 msg_pspew(" writing %u bytes:", writecnt);
hailfingerf91e3b52009-05-14 12:59:36 +0000662 for (i = 0; i < writecnt; i++)
hailfinger50c335f2010-01-09 04:32:23 +0000663 msg_pspew(" 0x%02x", writearr[i]);
hailfingerf91e3b52009-05-14 12:59:36 +0000664
hailfinger6ead7222010-11-01 22:07:04 +0000665 /* Response for unknown commands and missing chip is 0xff. */
666 memset(readarr, 0xff, readcnt);
667#if EMULATE_SPI_CHIP
668 switch (emu_chip) {
669 case EMULATE_ST_M25P10_RES:
670 case EMULATE_SST_SST25VF040_REMS:
671 case EMULATE_SST_SST25VF032B:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800672 case EMULATE_VARIABLE_SIZE:
hailfinger6ead7222010-11-01 22:07:04 +0000673 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
674 readarr)) {
675 msg_perr("Invalid command sent to flash chip!\n");
676 return 1;
677 }
678 break;
679 default:
680 break;
681 }
682#endif
hailfinger50c335f2010-01-09 04:32:23 +0000683 msg_pspew(" reading %u bytes:", readcnt);
uwe8d342eb2011-07-28 08:13:25 +0000684 for (i = 0; i < readcnt; i++)
hailfinger6ead7222010-11-01 22:07:04 +0000685 msg_pspew(" 0x%02x", readarr[i]);
hailfinger50c335f2010-01-09 04:32:23 +0000686 msg_pspew("\n");
David Hendricks84377002014-09-09 16:09:31 -0700687
688 programmer_delay((writecnt + readcnt) * delay_us);
hailfingerf91e3b52009-05-14 12:59:36 +0000689 return 0;
690}
hailfingera8727712010-06-20 10:58:32 +0000691
uwe8d342eb2011-07-28 08:13:25 +0000692static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000693 unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000694{
hailfinger6ead7222010-11-01 22:07:04 +0000695 return spi_write_chunked(flash, buf, start, len,
696 spi_write_256_chunksize);
hailfingerc7d06c62010-07-14 16:19:05 +0000697}
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800698
699#if EMULATE_CHIP && EMULATE_SPI_CHIP
700int probe_variable_size(struct flashchip *flash)
701{
702 int i;
703
704 /* Skip the probing if we don't emulate this chip. */
705 if (emu_chip != EMULATE_VARIABLE_SIZE)
706 return 0;
707
708 /*
709 * This will break if one day flashchip becomes read-only.
710 * Once that happens, we need to have special hacks in functions:
711 *
712 * erase_and_write_flash() in flashrom.c
713 * read_flash_to_file()
714 * handle_romentries()
715 * ...
716 *
717 * Search "total_size * 1024" in code.
718 */
719 if (emu_chip_size % 1024)
720 msg_perr("%s: emu_chip_size is not multipler of 1024.\n",
721 __func__);
722 flash->total_size = emu_chip_size / 1024;
723 msg_cdbg("%s: set flash->total_size to %dK bytes.\n", __func__,
724 flash->total_size);
725
726 /* Update eraser count */
727 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
728 struct block_eraser *eraser = &flash->block_erasers[i];
729 if (eraser->block_erase == NULL)
730 break;
731
732 eraser->eraseblocks[0].count = emu_chip_size /
733 eraser->eraseblocks[0].size;
734 msg_cdbg("%s: eraser.size=%d, .count=%d\n",
735 __func__, eraser->eraseblocks[0].size,
736 eraser->eraseblocks[0].count);
737 }
738
739 return 1;
740}
741#endif