blob: 2b9f3f98835921db748cf5c14bb75a3f9d7dc821 [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;
stefanctc5eb8a92011-11-23 09:13:48 +000058static unsigned int emu_chip_size = 0;
hailfinger6ead7222010-11-01 22:07:04 +000059#if EMULATE_SPI_CHIP
stefanctc5eb8a92011-11-23 09:13:48 +000060static unsigned int emu_max_byteprogram_size = 0;
61static unsigned int emu_max_aai_size = 0;
62static unsigned int emu_jedec_se_size = 0;
63static unsigned int emu_jedec_be_52_size = 0;
64static unsigned int emu_jedec_be_d8_size = 0;
65static unsigned int emu_jedec_ce_60_size = 0;
66static unsigned int emu_jedec_ce_c7_size = 0;
hailfinger6ead7222010-11-01 22:07:04 +000067#endif
68#endif
69
stefanctc5eb8a92011-11-23 09:13:48 +000070static unsigned int spi_write_256_chunksize = 256;
hailfinger6ead7222010-11-01 22:07:04 +000071
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);
uwe8d342eb2011-07-28 08:13:25 +000074static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000075 unsigned int start, unsigned int len);
mkarcherd264e9e2011-05-11 17:07:07 +000076
77static const struct spi_programmer spi_programmer_dummyflasher = {
uwe8d342eb2011-07-28 08:13:25 +000078 .type = SPI_CONTROLLER_DUMMY,
79 .max_data_read = MAX_DATA_READ_UNLIMITED,
80 .max_data_write = MAX_DATA_UNSPECIFIED,
81 .command = dummy_spi_send_command,
82 .multicommand = default_spi_send_multicommand,
83 .read = default_spi_read,
84 .write_256 = dummy_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +000085};
dhendrix0ffc2eb2011-06-14 01:35:36 +000086
hailfinger76bb7e92011-11-09 23:40:00 +000087static const struct par_programmer par_programmer_dummy = {
88 .chip_readb = dummy_chip_readb,
89 .chip_readw = dummy_chip_readw,
90 .chip_readl = dummy_chip_readl,
91 .chip_readn = dummy_chip_readn,
92 .chip_writeb = dummy_chip_writeb,
93 .chip_writew = dummy_chip_writew,
94 .chip_writel = dummy_chip_writel,
95 .chip_writen = dummy_chip_writen,
96};
97
98enum chipbustype dummy_buses_supported = BUS_NONE;
99
dhendrix0ffc2eb2011-06-14 01:35:36 +0000100static int dummy_shutdown(void *data)
101{
102 msg_pspew("%s\n", __func__);
103#if EMULATE_CHIP
104 if (emu_chip != EMULATE_NONE) {
105 if (emu_persistent_image) {
106 msg_pdbg("Writing %s\n", emu_persistent_image);
107 write_buf_to_file(flashchip_contents, emu_chip_size,
108 emu_persistent_image);
109 }
110 free(flashchip_contents);
111 }
112#endif
113 return 0;
114}
115
Simon Glassd2c64a22013-07-03 22:05:21 +0900116/* Values for the 'size' parameter */
117enum {
118 SIZE_UNKNOWN = -1,
119 SIZE_AUTO = -2,
120};
121
hailfingera9df33c2009-05-09 00:54:55 +0000122int dummy_init(void)
123{
hailfinger1ef766d2010-07-06 09:55:48 +0000124 char *bustext = NULL;
hailfinger6ead7222010-11-01 22:07:04 +0000125 char *tmp = NULL;
126#if EMULATE_CHIP
127 struct stat image_stat;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800128#if EMULATE_SPI_CHIP
Simon Glassd2c64a22013-07-03 22:05:21 +0900129 int size = SIZE_UNKNOWN; /* size for generic chip */
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800130#endif
hailfinger6ead7222010-11-01 22:07:04 +0000131#endif
Simon Glassd2c64a22013-07-03 22:05:21 +0900132 int image_size = SIZE_UNKNOWN;
hailfinger1ef766d2010-07-06 09:55:48 +0000133
hailfinger50c335f2010-01-09 04:32:23 +0000134 msg_pspew("%s\n", __func__);
hailfinger668f3502009-06-01 00:02:11 +0000135
hailfingerddeb4ac2010-07-08 10:13:37 +0000136 bustext = extract_programmer_param("bus");
hailfinger1ef766d2010-07-06 09:55:48 +0000137 msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
138 if (!bustext)
139 bustext = strdup("parallel+lpc+fwh+spi");
hailfinger668f3502009-06-01 00:02:11 +0000140 /* Convert the parameters to lowercase. */
hailfinger1ef766d2010-07-06 09:55:48 +0000141 tolower_string(bustext);
hailfinger668f3502009-06-01 00:02:11 +0000142
hailfinger76bb7e92011-11-09 23:40:00 +0000143 dummy_buses_supported = BUS_NONE;
hailfinger1ef766d2010-07-06 09:55:48 +0000144 if (strstr(bustext, "parallel")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000145 dummy_buses_supported |= BUS_PARALLEL;
hailfinger50c335f2010-01-09 04:32:23 +0000146 msg_pdbg("Enabling support for %s flash.\n", "parallel");
hailfinger668f3502009-06-01 00:02:11 +0000147 }
hailfinger1ef766d2010-07-06 09:55:48 +0000148 if (strstr(bustext, "lpc")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000149 dummy_buses_supported |= BUS_LPC;
hailfinger50c335f2010-01-09 04:32:23 +0000150 msg_pdbg("Enabling support for %s flash.\n", "LPC");
hailfinger668f3502009-06-01 00:02:11 +0000151 }
hailfinger1ef766d2010-07-06 09:55:48 +0000152 if (strstr(bustext, "fwh")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000153 dummy_buses_supported |= BUS_FWH;
hailfinger50c335f2010-01-09 04:32:23 +0000154 msg_pdbg("Enabling support for %s flash.\n", "FWH");
hailfinger668f3502009-06-01 00:02:11 +0000155 }
hailfinger1ef766d2010-07-06 09:55:48 +0000156 if (strstr(bustext, "spi")) {
hailfinger76bb7e92011-11-09 23:40:00 +0000157 dummy_buses_supported |= BUS_SPI;
hailfinger50c335f2010-01-09 04:32:23 +0000158 msg_pdbg("Enabling support for %s flash.\n", "SPI");
hailfinger668f3502009-06-01 00:02:11 +0000159 }
hailfinger76bb7e92011-11-09 23:40:00 +0000160 if (dummy_buses_supported == BUS_NONE)
hailfinger50c335f2010-01-09 04:32:23 +0000161 msg_pdbg("Support for all flash bus types disabled.\n");
hailfinger1ef766d2010-07-06 09:55:48 +0000162 free(bustext);
hailfinger6ead7222010-11-01 22:07:04 +0000163
164 tmp = extract_programmer_param("spi_write_256_chunksize");
165 if (tmp) {
166 spi_write_256_chunksize = atoi(tmp);
167 free(tmp);
168 if (spi_write_256_chunksize < 1) {
169 msg_perr("invalid spi_write_256_chunksize\n");
170 return 1;
171 }
172 }
173
174#if EMULATE_CHIP
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800175#if EMULATE_SPI_CHIP
176 tmp = extract_programmer_param("size");
177 if (tmp) {
178 int multiplier = 1;
Simon Glassd2c64a22013-07-03 22:05:21 +0900179 if (!strcmp(tmp, "auto"))
180 size = SIZE_AUTO;
181 else if (strlen(tmp)) {
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800182 int remove_last_char = 1;
183 switch (tmp[strlen(tmp) - 1]) {
184 case 'k': case 'K':
185 multiplier = 1024;
186 break;
187 case 'm': case 'M':
188 multiplier = 1024 * 1024;
189 break;
190 default:
191 remove_last_char = 0;
192 break;
193 }
194 if (remove_last_char) tmp[strlen(tmp) - 1] = '\0';
Simon Glassd2c64a22013-07-03 22:05:21 +0900195 size = atoi(tmp) * multiplier;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800196 }
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800197 }
198#endif
199
hailfinger6ead7222010-11-01 22:07:04 +0000200 tmp = extract_programmer_param("emulate");
201 if (!tmp) {
202 msg_pdbg("Not emulating any flash chip.\n");
203 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000204 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000205 }
206#if EMULATE_SPI_CHIP
207 if (!strcmp(tmp, "M25P10.RES")) {
208 emu_chip = EMULATE_ST_M25P10_RES;
209 emu_chip_size = 128 * 1024;
210 emu_max_byteprogram_size = 128;
211 emu_max_aai_size = 0;
212 emu_jedec_se_size = 0;
213 emu_jedec_be_52_size = 0;
214 emu_jedec_be_d8_size = 32 * 1024;
215 emu_jedec_ce_60_size = 0;
216 emu_jedec_ce_c7_size = emu_chip_size;
217 msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
218 "write)\n");
219 }
220 if (!strcmp(tmp, "SST25VF040.REMS")) {
221 emu_chip = EMULATE_SST_SST25VF040_REMS;
222 emu_chip_size = 512 * 1024;
223 emu_max_byteprogram_size = 1;
224 emu_max_aai_size = 0;
225 emu_jedec_se_size = 4 * 1024;
226 emu_jedec_be_52_size = 32 * 1024;
227 emu_jedec_be_d8_size = 0;
228 emu_jedec_ce_60_size = emu_chip_size;
229 emu_jedec_ce_c7_size = 0;
230 msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
231 "byte write)\n");
232 }
233 if (!strcmp(tmp, "SST25VF032B")) {
234 emu_chip = EMULATE_SST_SST25VF032B;
235 emu_chip_size = 4 * 1024 * 1024;
236 emu_max_byteprogram_size = 1;
237 emu_max_aai_size = 2;
238 emu_jedec_se_size = 4 * 1024;
239 emu_jedec_be_52_size = 32 * 1024;
240 emu_jedec_be_d8_size = 64 * 1024;
241 emu_jedec_ce_60_size = emu_chip_size;
242 emu_jedec_ce_c7_size = emu_chip_size;
243 msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
244 "write)\n");
245 }
Simon Glassd2c64a22013-07-03 22:05:21 +0900246 emu_persistent_image = extract_programmer_param("image");
247 if (!stat(emu_persistent_image, &image_stat))
248 image_size = image_stat.st_size;
249
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800250 if (!strncmp(tmp, VARIABLE_SIZE_CHIP_NAME,
251 strlen(VARIABLE_SIZE_CHIP_NAME))) {
Simon Glassd2c64a22013-07-03 22:05:21 +0900252 if (size == SIZE_UNKNOWN) {
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800253 msg_perr("%s: the size parameter is not given.\n",
254 __func__);
255 free(tmp);
256 return 1;
Simon Glassd2c64a22013-07-03 22:05:21 +0900257 } else if (size == SIZE_AUTO) {
258 if (image_size == SIZE_UNKNOWN) {
259 msg_perr("%s: no image so cannot use automatic size.\n",
260 __func__);
261 free(tmp);
262 return 1;
263 }
264 size = image_size;
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800265 }
266 emu_chip = EMULATE_VARIABLE_SIZE;
267 emu_chip_size = size;
268 emu_max_byteprogram_size = 256;
269 emu_max_aai_size = 0;
270 emu_jedec_se_size = 4 * 1024;
271 emu_jedec_be_52_size = 32 * 1024;
272 emu_jedec_be_d8_size = 64 * 1024;
273 emu_jedec_ce_60_size = emu_chip_size;
274 emu_jedec_ce_c7_size = emu_chip_size;
275 msg_pdbg("Emulating generic SPI flash chip (size=%d bytes)\n",
276 emu_chip_size);
277 }
hailfinger6ead7222010-11-01 22:07:04 +0000278#endif
279 if (emu_chip == EMULATE_NONE) {
280 msg_perr("Invalid chip specified for emulation: %s\n", tmp);
281 free(tmp);
282 return 1;
283 }
284 free(tmp);
285 flashchip_contents = malloc(emu_chip_size);
286 if (!flashchip_contents) {
287 msg_perr("Out of memory!\n");
288 return 1;
289 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000290
hailfinger6ead7222010-11-01 22:07:04 +0000291 msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
292 memset(flashchip_contents, 0xff, emu_chip_size);
293
hailfinger6ead7222010-11-01 22:07:04 +0000294 if (!emu_persistent_image) {
295 /* Nothing else to do. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000296 goto dummy_init_out;
hailfinger6ead7222010-11-01 22:07:04 +0000297 }
298 if (!stat(emu_persistent_image, &image_stat)) {
299 msg_pdbg("Found persistent image %s, size %li ",
300 emu_persistent_image, (long)image_stat.st_size);
301 if (image_stat.st_size == emu_chip_size) {
302 msg_pdbg("matches.\n");
303 msg_pdbg("Reading %s\n", emu_persistent_image);
304 read_buf_from_file(flashchip_contents, emu_chip_size,
305 emu_persistent_image);
306 } else {
307 msg_pdbg("doesn't match.\n");
308 }
309 }
310#endif
hailfingera9df33c2009-05-09 00:54:55 +0000311
dhendrix0ffc2eb2011-06-14 01:35:36 +0000312dummy_init_out:
313 if (register_shutdown(dummy_shutdown, NULL)) {
hailfinger6ead7222010-11-01 22:07:04 +0000314 free(flashchip_contents);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000315 return 1;
hailfinger6ead7222010-11-01 22:07:04 +0000316 }
hailfinger76bb7e92011-11-09 23:40:00 +0000317 if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH))
318 register_par_programmer(&par_programmer_dummy,
319 dummy_buses_supported &
320 (BUS_PARALLEL | BUS_LPC |
321 BUS_FWH));
322 if (dummy_buses_supported & BUS_SPI)
323 register_spi_programmer(&spi_programmer_dummyflasher);
324
hailfingera9df33c2009-05-09 00:54:55 +0000325 return 0;
326}
327
hailfinger11ae3c42009-05-11 14:13:25 +0000328void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
329{
hailfinger50c335f2010-01-09 04:32:23 +0000330 msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
331 __func__, descr, (unsigned long)len, phys_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000332 return (void *)phys_addr;
333}
334
335void dummy_unmap(void *virt_addr, size_t len)
336{
hailfinger50c335f2010-01-09 04:32:23 +0000337 msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
338 __func__, (unsigned long)len, virt_addr);
hailfinger11ae3c42009-05-11 14:13:25 +0000339}
340
hailfinger82719632009-05-16 21:22:56 +0000341void dummy_chip_writeb(uint8_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000342{
hailfinger50c335f2010-01-09 04:32:23 +0000343 msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000344}
345
hailfinger82719632009-05-16 21:22:56 +0000346void dummy_chip_writew(uint16_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000347{
hailfinger50c335f2010-01-09 04:32:23 +0000348 msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000349}
350
hailfinger82719632009-05-16 21:22:56 +0000351void dummy_chip_writel(uint32_t val, chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000352{
hailfinger50c335f2010-01-09 04:32:23 +0000353 msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
hailfingera9df33c2009-05-09 00:54:55 +0000354}
355
hailfinger9d987ef2009-06-05 18:32:07 +0000356void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
357{
358 size_t i;
hailfinger50c335f2010-01-09 04:32:23 +0000359 msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
360 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000361 for (i = 0; i < len; i++) {
362 if ((i % 16) == 0)
hailfinger50c335f2010-01-09 04:32:23 +0000363 msg_pspew("\n");
364 msg_pspew("%02x ", buf[i]);
hailfinger9d987ef2009-06-05 18:32:07 +0000365 }
366}
367
hailfinger82719632009-05-16 21:22:56 +0000368uint8_t dummy_chip_readb(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000369{
hailfinger50c335f2010-01-09 04:32:23 +0000370 msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000371 return 0xff;
372}
373
hailfinger82719632009-05-16 21:22:56 +0000374uint16_t dummy_chip_readw(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000375{
hailfinger50c335f2010-01-09 04:32:23 +0000376 msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000377 return 0xffff;
378}
379
hailfinger82719632009-05-16 21:22:56 +0000380uint32_t dummy_chip_readl(const chipaddr addr)
hailfingera9df33c2009-05-09 00:54:55 +0000381{
hailfinger50c335f2010-01-09 04:32:23 +0000382 msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr);
hailfingera9df33c2009-05-09 00:54:55 +0000383 return 0xffffffff;
384}
385
hailfinger9d987ef2009-06-05 18:32:07 +0000386void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
387{
hailfinger50c335f2010-01-09 04:32:23 +0000388 msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n",
389 __func__, addr, (unsigned long)len);
hailfinger9d987ef2009-06-05 18:32:07 +0000390 memset(buf, 0xff, len);
391 return;
392}
393
hailfinger6ead7222010-11-01 22:07:04 +0000394#if EMULATE_SPI_CHIP
395static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
396 const unsigned char *writearr, unsigned char *readarr)
397{
stefanctc5eb8a92011-11-23 09:13:48 +0000398 unsigned int offs;
399 static int unsigned aai_offs;
hailfinger6ead7222010-11-01 22:07:04 +0000400 static int aai_active = 0;
401
402 if (writecnt == 0) {
403 msg_perr("No command sent to the chip!\n");
404 return 1;
405 }
406 /* TODO: Implement command blacklists here. */
407 switch (writearr[0]) {
408 case JEDEC_RES:
409 if (emu_chip != EMULATE_ST_M25P10_RES)
410 break;
411 /* Respond with ST_M25P10_RES. */
412 if (readcnt > 0)
413 readarr[0] = 0x10;
414 break;
415 case JEDEC_REMS:
416 if (emu_chip != EMULATE_SST_SST25VF040_REMS)
417 break;
418 /* Respond with SST_SST25VF040_REMS. */
419 if (readcnt > 0)
420 readarr[0] = 0xbf;
421 if (readcnt > 1)
422 readarr[1] = 0x44;
423 break;
424 case JEDEC_RDID:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800425 if (emu_chip == EMULATE_SST_SST25VF032B) {
426 /* Respond with SST_SST25VF032B. */
427 if (readcnt > 0)
428 readarr[0] = 0xbf;
429 if (readcnt > 1)
430 readarr[1] = 0x25;
431 if (readcnt > 2)
432 readarr[2] = 0x4a;
433 } else if (emu_chip == EMULATE_VARIABLE_SIZE) {
434 const uint16_t man_id = VARIABLE_SIZE_MANUF_ID;
435 const uint16_t dev_id = VARIABLE_SIZE_DEVICE_ID;
436 if (readcnt > 0) readarr[0] = man_id >> 8;
437 if (readcnt > 1) readarr[1] = man_id & 0xff;
438 if (readcnt > 2) readarr[2] = dev_id >> 8;
439 if (readcnt > 3) readarr[3] = dev_id & 0xff;
440 }
hailfinger6ead7222010-11-01 22:07:04 +0000441 break;
442 case JEDEC_RDSR:
443 memset(readarr, 0, readcnt);
444 if (aai_active)
445 memset(readarr, 1 << 6, readcnt);
446 break;
447 case JEDEC_READ:
448 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
449 /* Truncate to emu_chip_size. */
450 offs %= emu_chip_size;
451 if (readcnt > 0)
452 memcpy(readarr, flashchip_contents + offs, readcnt);
453 break;
454 case JEDEC_BYTE_PROGRAM:
455 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
456 /* Truncate to emu_chip_size. */
457 offs %= emu_chip_size;
458 if (writecnt < 5) {
459 msg_perr("BYTE PROGRAM size too short!\n");
460 return 1;
461 }
462 if (writecnt - 4 > emu_max_byteprogram_size) {
463 msg_perr("Max BYTE PROGRAM size exceeded!\n");
464 return 1;
465 }
466 memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
467 break;
468 case JEDEC_AAI_WORD_PROGRAM:
469 if (!emu_max_aai_size)
470 break;
471 if (!aai_active) {
472 if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
473 msg_perr("Initial AAI WORD PROGRAM size too "
474 "short!\n");
475 return 1;
476 }
477 if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
478 msg_perr("Initial AAI WORD PROGRAM size too "
479 "long!\n");
480 return 1;
481 }
482 aai_active = 1;
483 aai_offs = writearr[1] << 16 | writearr[2] << 8 |
484 writearr[3];
485 /* Truncate to emu_chip_size. */
486 aai_offs %= emu_chip_size;
487 memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
488 aai_offs += 2;
489 } else {
490 if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
491 msg_perr("Continuation AAI WORD PROGRAM size "
492 "too short!\n");
493 return 1;
494 }
495 if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
496 msg_perr("Continuation AAI WORD PROGRAM size "
497 "too long!\n");
498 return 1;
499 }
500 memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
501 aai_offs += 2;
502 }
503 break;
504 case JEDEC_WRDI:
505 if (!emu_max_aai_size)
506 break;
507 aai_active = 0;
508 break;
509 case JEDEC_SE:
510 if (!emu_jedec_se_size)
511 break;
512 if (writecnt != JEDEC_SE_OUTSIZE) {
513 msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
514 return 1;
515 }
516 if (readcnt != JEDEC_SE_INSIZE) {
517 msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
518 return 1;
519 }
520 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
521 if (offs & (emu_jedec_se_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000522 msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000523 offs &= ~(emu_jedec_se_size - 1);
524 memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
525 break;
526 case JEDEC_BE_52:
527 if (!emu_jedec_be_52_size)
528 break;
529 if (writecnt != JEDEC_BE_52_OUTSIZE) {
530 msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
531 return 1;
532 }
533 if (readcnt != JEDEC_BE_52_INSIZE) {
534 msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
535 return 1;
536 }
537 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
538 if (offs & (emu_jedec_be_52_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000539 msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000540 offs &= ~(emu_jedec_be_52_size - 1);
541 memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
542 break;
543 case JEDEC_BE_D8:
544 if (!emu_jedec_be_d8_size)
545 break;
546 if (writecnt != JEDEC_BE_D8_OUTSIZE) {
547 msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
548 return 1;
549 }
550 if (readcnt != JEDEC_BE_D8_INSIZE) {
551 msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
552 return 1;
553 }
554 offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
555 if (offs & (emu_jedec_be_d8_size - 1))
hailfingere53f5e42011-02-04 22:52:04 +0000556 msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
hailfinger6ead7222010-11-01 22:07:04 +0000557 offs &= ~(emu_jedec_be_d8_size - 1);
558 memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
559 break;
560 case JEDEC_CE_60:
561 if (!emu_jedec_ce_60_size)
562 break;
563 if (writecnt != JEDEC_CE_60_OUTSIZE) {
564 msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
565 return 1;
566 }
567 if (readcnt != JEDEC_CE_60_INSIZE) {
568 msg_perr("CHIP ERASE 0x60 insize invalid!\n");
569 return 1;
570 }
hailfingere53f5e42011-02-04 22:52:04 +0000571 /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000572 /* emu_jedec_ce_60_size is emu_chip_size. */
hailfingere53f5e42011-02-04 22:52:04 +0000573 memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
hailfinger6ead7222010-11-01 22:07:04 +0000574 break;
575 case JEDEC_CE_C7:
576 if (!emu_jedec_ce_c7_size)
577 break;
578 if (writecnt != JEDEC_CE_C7_OUTSIZE) {
579 msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
580 return 1;
581 }
582 if (readcnt != JEDEC_CE_C7_INSIZE) {
583 msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
584 return 1;
585 }
hailfingere53f5e42011-02-04 22:52:04 +0000586 /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
hailfinger6ead7222010-11-01 22:07:04 +0000587 /* emu_jedec_ce_c7_size is emu_chip_size. */
588 memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
589 break;
590 default:
591 /* No special response. */
592 break;
593 }
594 return 0;
595}
596#endif
597
mkarcherd264e9e2011-05-11 17:07:07 +0000598static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
hailfingerf91e3b52009-05-14 12:59:36 +0000599 const unsigned char *writearr, unsigned char *readarr)
600{
601 int i;
602
hailfinger50c335f2010-01-09 04:32:23 +0000603 msg_pspew("%s:", __func__);
hailfingerf91e3b52009-05-14 12:59:36 +0000604
hailfinger50c335f2010-01-09 04:32:23 +0000605 msg_pspew(" writing %u bytes:", writecnt);
hailfingerf91e3b52009-05-14 12:59:36 +0000606 for (i = 0; i < writecnt; i++)
hailfinger50c335f2010-01-09 04:32:23 +0000607 msg_pspew(" 0x%02x", writearr[i]);
hailfingerf91e3b52009-05-14 12:59:36 +0000608
hailfinger6ead7222010-11-01 22:07:04 +0000609 /* Response for unknown commands and missing chip is 0xff. */
610 memset(readarr, 0xff, readcnt);
611#if EMULATE_SPI_CHIP
612 switch (emu_chip) {
613 case EMULATE_ST_M25P10_RES:
614 case EMULATE_SST_SST25VF040_REMS:
615 case EMULATE_SST_SST25VF032B:
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800616 case EMULATE_VARIABLE_SIZE:
hailfinger6ead7222010-11-01 22:07:04 +0000617 if (emulate_spi_chip_response(writecnt, readcnt, writearr,
618 readarr)) {
619 msg_perr("Invalid command sent to flash chip!\n");
620 return 1;
621 }
622 break;
623 default:
624 break;
625 }
626#endif
hailfinger50c335f2010-01-09 04:32:23 +0000627 msg_pspew(" reading %u bytes:", readcnt);
uwe8d342eb2011-07-28 08:13:25 +0000628 for (i = 0; i < readcnt; i++)
hailfinger6ead7222010-11-01 22:07:04 +0000629 msg_pspew(" 0x%02x", readarr[i]);
hailfinger50c335f2010-01-09 04:32:23 +0000630 msg_pspew("\n");
hailfingerf91e3b52009-05-14 12:59:36 +0000631 return 0;
632}
hailfingera8727712010-06-20 10:58:32 +0000633
uwe8d342eb2011-07-28 08:13:25 +0000634static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000635 unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000636{
hailfinger6ead7222010-11-01 22:07:04 +0000637 return spi_write_chunked(flash, buf, start, len,
638 spi_write_256_chunksize);
hailfingerc7d06c62010-07-14 16:19:05 +0000639}
Louis Yung-Chieh Loe53fa0f2011-04-11 17:18:41 +0800640
641#if EMULATE_CHIP && EMULATE_SPI_CHIP
642int probe_variable_size(struct flashchip *flash)
643{
644 int i;
645
646 /* Skip the probing if we don't emulate this chip. */
647 if (emu_chip != EMULATE_VARIABLE_SIZE)
648 return 0;
649
650 /*
651 * This will break if one day flashchip becomes read-only.
652 * Once that happens, we need to have special hacks in functions:
653 *
654 * erase_and_write_flash() in flashrom.c
655 * read_flash_to_file()
656 * handle_romentries()
657 * ...
658 *
659 * Search "total_size * 1024" in code.
660 */
661 if (emu_chip_size % 1024)
662 msg_perr("%s: emu_chip_size is not multipler of 1024.\n",
663 __func__);
664 flash->total_size = emu_chip_size / 1024;
665 msg_cdbg("%s: set flash->total_size to %dK bytes.\n", __func__,
666 flash->total_size);
667
668 /* Update eraser count */
669 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
670 struct block_eraser *eraser = &flash->block_erasers[i];
671 if (eraser->block_erase == NULL)
672 break;
673
674 eraser->eraseblocks[0].count = emu_chip_size /
675 eraser->eraseblocks[0].size;
676 msg_cdbg("%s: eraser.size=%d, .count=%d\n",
677 __func__, eraser->eraseblocks[0].size,
678 eraser->eraseblocks[0].count);
679 }
680
681 return 1;
682}
683#endif