Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 8 | * the Free Software Foundation; version 2 of the License. |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 9 | * |
| 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. |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 16 | #include <string.h> |
| 17 | #include <stdlib.h> |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <ctype.h> |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 20 | #include <errno.h> |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 21 | #include "flash.h" |
Carl-Daniel Hailfinger | 1b0ba89 | 2010-06-20 10:58:32 +0000 | [diff] [blame] | 22 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 23 | #include "programmer.h" |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 24 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 25 | /* Remove the #define below if you don't want SPI flash chip emulation. */ |
| 26 | #define EMULATE_SPI_CHIP 1 |
| 27 | |
| 28 | #if EMULATE_SPI_CHIP |
| 29 | #define EMULATE_CHIP 1 |
| 30 | #include "spi.h" |
| 31 | #endif |
| 32 | |
| 33 | #if EMULATE_CHIP |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #endif |
| 37 | |
| 38 | #if EMULATE_CHIP |
| 39 | static uint8_t *flashchip_contents = NULL; |
| 40 | enum emu_chip { |
| 41 | EMULATE_NONE, |
| 42 | EMULATE_ST_M25P10_RES, |
| 43 | EMULATE_SST_SST25VF040_REMS, |
| 44 | EMULATE_SST_SST25VF032B, |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 45 | EMULATE_MACRONIX_MX25L6436, |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 46 | EMULATE_WINBOND_W25Q128FV, |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 47 | }; |
| 48 | static enum emu_chip emu_chip = EMULATE_NONE; |
| 49 | static char *emu_persistent_image = NULL; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 50 | static unsigned int emu_chip_size = 0; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 51 | #if EMULATE_SPI_CHIP |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 52 | static unsigned int emu_max_byteprogram_size = 0; |
| 53 | static unsigned int emu_max_aai_size = 0; |
| 54 | static unsigned int emu_jedec_se_size = 0; |
| 55 | static unsigned int emu_jedec_be_52_size = 0; |
| 56 | static unsigned int emu_jedec_be_d8_size = 0; |
| 57 | static unsigned int emu_jedec_ce_60_size = 0; |
| 58 | static unsigned int emu_jedec_ce_c7_size = 0; |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 59 | unsigned char spi_blacklist[256]; |
| 60 | unsigned char spi_ignorelist[256]; |
| 61 | int spi_blacklist_size = 0; |
| 62 | int spi_ignorelist_size = 0; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 63 | static uint8_t emu_status = 0; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 64 | |
| 65 | /* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */ |
Stefan Tauner | 67d163d | 2013-01-15 17:37:48 +0000 | [diff] [blame] | 66 | static const uint8_t sfdp_table[] = { |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 67 | 0x53, 0x46, 0x44, 0x50, // @0x00: SFDP signature |
| 68 | 0x00, 0x01, 0x01, 0xFF, // @0x04: revision 1.0, 2 headers |
| 69 | 0x00, 0x00, 0x01, 0x09, // @0x08: JEDEC SFDP header rev. 1.0, 9 DW long |
| 70 | 0x1C, 0x00, 0x00, 0xFF, // @0x0C: PTP0 = 0x1C (instead of 0x30) |
| 71 | 0xC2, 0x00, 0x01, 0x04, // @0x10: Macronix header rev. 1.0, 4 DW long |
| 72 | 0x48, 0x00, 0x00, 0xFF, // @0x14: PTP1 = 0x48 (instead of 0x60) |
| 73 | 0xFF, 0xFF, 0xFF, 0xFF, // @0x18: hole. |
| 74 | 0xE5, 0x20, 0xC9, 0xFF, // @0x1C: SFDP parameter table start |
| 75 | 0xFF, 0xFF, 0xFF, 0x03, // @0x20 |
| 76 | 0x00, 0xFF, 0x08, 0x6B, // @0x24 |
| 77 | 0x08, 0x3B, 0x00, 0xFF, // @0x28 |
| 78 | 0xEE, 0xFF, 0xFF, 0xFF, // @0x2C |
| 79 | 0xFF, 0xFF, 0x00, 0x00, // @0x30 |
| 80 | 0xFF, 0xFF, 0x00, 0xFF, // @0x34 |
| 81 | 0x0C, 0x20, 0x0F, 0x52, // @0x38 |
| 82 | 0x10, 0xD8, 0x00, 0xFF, // @0x3C: SFDP parameter table end |
| 83 | 0xFF, 0xFF, 0xFF, 0xFF, // @0x40: hole. |
| 84 | 0xFF, 0xFF, 0xFF, 0xFF, // @0x44: hole. |
| 85 | 0x00, 0x36, 0x00, 0x27, // @0x48: Macronix parameter table start |
| 86 | 0xF4, 0x4F, 0xFF, 0xFF, // @0x4C |
| 87 | 0xD9, 0xC8, 0xFF, 0xFF, // @0x50 |
| 88 | 0xFF, 0xFF, 0xFF, 0xFF, // @0x54: Macronix parameter table end |
| 89 | }; |
| 90 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 91 | #endif |
| 92 | #endif |
| 93 | |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 94 | static unsigned int spi_write_256_chunksize = 256; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 95 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 96 | static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
| 97 | const unsigned char *writearr, unsigned char *readarr); |
| 98 | static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 99 | unsigned int start, unsigned int len); |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 100 | static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr); |
| 101 | static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr); |
| 102 | static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr); |
| 103 | static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len); |
| 104 | static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr); |
| 105 | static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr); |
| 106 | static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr); |
| 107 | static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 108 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 109 | static const struct spi_master spi_master_dummyflasher = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 110 | .type = SPI_CONTROLLER_DUMMY, |
Nico Huber | 1cf407b | 2017-11-10 20:18:23 +0100 | [diff] [blame] | 111 | .features = SPI_MASTER_4BA, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 112 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 113 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 114 | .command = dummy_spi_send_command, |
| 115 | .multicommand = default_spi_send_multicommand, |
| 116 | .read = default_spi_read, |
| 117 | .write_256 = dummy_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 118 | .write_aai = default_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 119 | }; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 120 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 121 | static const struct par_master par_master_dummy = { |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 122 | .chip_readb = dummy_chip_readb, |
| 123 | .chip_readw = dummy_chip_readw, |
| 124 | .chip_readl = dummy_chip_readl, |
| 125 | .chip_readn = dummy_chip_readn, |
| 126 | .chip_writeb = dummy_chip_writeb, |
| 127 | .chip_writew = dummy_chip_writew, |
| 128 | .chip_writel = dummy_chip_writel, |
| 129 | .chip_writen = dummy_chip_writen, |
| 130 | }; |
| 131 | |
| 132 | enum chipbustype dummy_buses_supported = BUS_NONE; |
| 133 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 134 | static int dummy_shutdown(void *data) |
| 135 | { |
| 136 | msg_pspew("%s\n", __func__); |
| 137 | #if EMULATE_CHIP |
| 138 | if (emu_chip != EMULATE_NONE) { |
| 139 | if (emu_persistent_image) { |
| 140 | msg_pdbg("Writing %s\n", emu_persistent_image); |
Stefan Tauner | e0ff165 | 2012-09-22 22:56:09 +0000 | [diff] [blame] | 141 | write_buf_to_file(flashchip_contents, emu_chip_size, emu_persistent_image); |
| 142 | free(emu_persistent_image); |
| 143 | emu_persistent_image = NULL; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 144 | } |
| 145 | free(flashchip_contents); |
| 146 | } |
| 147 | #endif |
| 148 | return 0; |
| 149 | } |
| 150 | |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 151 | int dummy_init(void) |
| 152 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 153 | char *bustext = NULL; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 154 | char *tmp = NULL; |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 155 | int i; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 156 | #if EMULATE_SPI_CHIP |
| 157 | char *status = NULL; |
| 158 | #endif |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 159 | #if EMULATE_CHIP |
| 160 | struct stat image_stat; |
| 161 | #endif |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 162 | |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 163 | msg_pspew("%s\n", __func__); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 164 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 165 | bustext = extract_programmer_param("bus"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 166 | msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default"); |
| 167 | if (!bustext) |
| 168 | bustext = strdup("parallel+lpc+fwh+spi"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 169 | /* Convert the parameters to lowercase. */ |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 170 | tolower_string(bustext); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 171 | |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 172 | dummy_buses_supported = BUS_NONE; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 173 | if (strstr(bustext, "parallel")) { |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 174 | dummy_buses_supported |= BUS_PARALLEL; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 175 | msg_pdbg("Enabling support for %s flash.\n", "parallel"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 176 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 177 | if (strstr(bustext, "lpc")) { |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 178 | dummy_buses_supported |= BUS_LPC; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 179 | msg_pdbg("Enabling support for %s flash.\n", "LPC"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 180 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 181 | if (strstr(bustext, "fwh")) { |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 182 | dummy_buses_supported |= BUS_FWH; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 183 | msg_pdbg("Enabling support for %s flash.\n", "FWH"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 184 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 185 | if (strstr(bustext, "spi")) { |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 186 | dummy_buses_supported |= BUS_SPI; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 187 | msg_pdbg("Enabling support for %s flash.\n", "SPI"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 188 | } |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 189 | if (dummy_buses_supported == BUS_NONE) |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 190 | msg_pdbg("Support for all flash bus types disabled.\n"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 191 | free(bustext); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 192 | |
| 193 | tmp = extract_programmer_param("spi_write_256_chunksize"); |
| 194 | if (tmp) { |
| 195 | spi_write_256_chunksize = atoi(tmp); |
| 196 | free(tmp); |
| 197 | if (spi_write_256_chunksize < 1) { |
| 198 | msg_perr("invalid spi_write_256_chunksize\n"); |
| 199 | return 1; |
| 200 | } |
| 201 | } |
| 202 | |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 203 | tmp = extract_programmer_param("spi_blacklist"); |
| 204 | if (tmp) { |
| 205 | i = strlen(tmp); |
| 206 | if (!strncmp(tmp, "0x", 2)) { |
| 207 | i -= 2; |
| 208 | memmove(tmp, tmp + 2, i + 1); |
| 209 | } |
| 210 | if ((i > 512) || (i % 2)) { |
| 211 | msg_perr("Invalid SPI command blacklist length\n"); |
| 212 | free(tmp); |
| 213 | return 1; |
| 214 | } |
| 215 | spi_blacklist_size = i / 2; |
| 216 | for (i = 0; i < spi_blacklist_size * 2; i++) { |
| 217 | if (!isxdigit((unsigned char)tmp[i])) { |
| 218 | msg_perr("Invalid char \"%c\" in SPI command " |
| 219 | "blacklist\n", tmp[i]); |
| 220 | free(tmp); |
| 221 | return 1; |
| 222 | } |
| 223 | } |
| 224 | for (i = 0; i < spi_blacklist_size; i++) { |
Carl-Daniel Hailfinger | 5b55471 | 2012-02-16 01:43:06 +0000 | [diff] [blame] | 225 | unsigned int tmp2; |
| 226 | /* SCNx8 is apparently not supported by MSVC (and thus |
| 227 | * MinGW), so work around it with an extra variable |
| 228 | */ |
| 229 | sscanf(tmp + i * 2, "%2x", &tmp2); |
| 230 | spi_blacklist[i] = (uint8_t)tmp2; |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 231 | } |
| 232 | msg_pdbg("SPI blacklist is "); |
| 233 | for (i = 0; i < spi_blacklist_size; i++) |
| 234 | msg_pdbg("%02x ", spi_blacklist[i]); |
| 235 | msg_pdbg(", size %i\n", spi_blacklist_size); |
| 236 | } |
| 237 | free(tmp); |
| 238 | |
| 239 | tmp = extract_programmer_param("spi_ignorelist"); |
| 240 | if (tmp) { |
| 241 | i = strlen(tmp); |
| 242 | if (!strncmp(tmp, "0x", 2)) { |
| 243 | i -= 2; |
| 244 | memmove(tmp, tmp + 2, i + 1); |
| 245 | } |
| 246 | if ((i > 512) || (i % 2)) { |
| 247 | msg_perr("Invalid SPI command ignorelist length\n"); |
| 248 | free(tmp); |
| 249 | return 1; |
| 250 | } |
| 251 | spi_ignorelist_size = i / 2; |
| 252 | for (i = 0; i < spi_ignorelist_size * 2; i++) { |
| 253 | if (!isxdigit((unsigned char)tmp[i])) { |
| 254 | msg_perr("Invalid char \"%c\" in SPI command " |
| 255 | "ignorelist\n", tmp[i]); |
| 256 | free(tmp); |
| 257 | return 1; |
| 258 | } |
| 259 | } |
| 260 | for (i = 0; i < spi_ignorelist_size; i++) { |
Carl-Daniel Hailfinger | 5b55471 | 2012-02-16 01:43:06 +0000 | [diff] [blame] | 261 | unsigned int tmp2; |
| 262 | /* SCNx8 is apparently not supported by MSVC (and thus |
| 263 | * MinGW), so work around it with an extra variable |
| 264 | */ |
| 265 | sscanf(tmp + i * 2, "%2x", &tmp2); |
| 266 | spi_ignorelist[i] = (uint8_t)tmp2; |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 267 | } |
| 268 | msg_pdbg("SPI ignorelist is "); |
| 269 | for (i = 0; i < spi_ignorelist_size; i++) |
| 270 | msg_pdbg("%02x ", spi_ignorelist[i]); |
| 271 | msg_pdbg(", size %i\n", spi_ignorelist_size); |
| 272 | } |
| 273 | free(tmp); |
| 274 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 275 | #if EMULATE_CHIP |
| 276 | tmp = extract_programmer_param("emulate"); |
| 277 | if (!tmp) { |
| 278 | msg_pdbg("Not emulating any flash chip.\n"); |
| 279 | /* Nothing else to do. */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 280 | goto dummy_init_out; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 281 | } |
| 282 | #if EMULATE_SPI_CHIP |
| 283 | if (!strcmp(tmp, "M25P10.RES")) { |
| 284 | emu_chip = EMULATE_ST_M25P10_RES; |
| 285 | emu_chip_size = 128 * 1024; |
| 286 | emu_max_byteprogram_size = 128; |
| 287 | emu_max_aai_size = 0; |
| 288 | emu_jedec_se_size = 0; |
| 289 | emu_jedec_be_52_size = 0; |
| 290 | emu_jedec_be_d8_size = 32 * 1024; |
| 291 | emu_jedec_ce_60_size = 0; |
| 292 | emu_jedec_ce_c7_size = emu_chip_size; |
| 293 | msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page " |
| 294 | "write)\n"); |
| 295 | } |
| 296 | if (!strcmp(tmp, "SST25VF040.REMS")) { |
| 297 | emu_chip = EMULATE_SST_SST25VF040_REMS; |
| 298 | emu_chip_size = 512 * 1024; |
| 299 | emu_max_byteprogram_size = 1; |
| 300 | emu_max_aai_size = 0; |
| 301 | emu_jedec_se_size = 4 * 1024; |
| 302 | emu_jedec_be_52_size = 32 * 1024; |
| 303 | emu_jedec_be_d8_size = 0; |
| 304 | emu_jedec_ce_60_size = emu_chip_size; |
| 305 | emu_jedec_ce_c7_size = 0; |
| 306 | msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, " |
| 307 | "byte write)\n"); |
| 308 | } |
| 309 | if (!strcmp(tmp, "SST25VF032B")) { |
| 310 | emu_chip = EMULATE_SST_SST25VF032B; |
| 311 | emu_chip_size = 4 * 1024 * 1024; |
| 312 | emu_max_byteprogram_size = 1; |
| 313 | emu_max_aai_size = 2; |
| 314 | emu_jedec_se_size = 4 * 1024; |
| 315 | emu_jedec_be_52_size = 32 * 1024; |
| 316 | emu_jedec_be_d8_size = 64 * 1024; |
| 317 | emu_jedec_ce_60_size = emu_chip_size; |
| 318 | emu_jedec_ce_c7_size = emu_chip_size; |
| 319 | msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI " |
| 320 | "write)\n"); |
| 321 | } |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 322 | if (!strcmp(tmp, "MX25L6436")) { |
| 323 | emu_chip = EMULATE_MACRONIX_MX25L6436; |
| 324 | emu_chip_size = 8 * 1024 * 1024; |
| 325 | emu_max_byteprogram_size = 256; |
| 326 | emu_max_aai_size = 0; |
| 327 | emu_jedec_se_size = 4 * 1024; |
| 328 | emu_jedec_be_52_size = 32 * 1024; |
| 329 | emu_jedec_be_d8_size = 64 * 1024; |
| 330 | emu_jedec_ce_60_size = emu_chip_size; |
| 331 | emu_jedec_ce_c7_size = emu_chip_size; |
| 332 | msg_pdbg("Emulating Macronix MX25L6436 SPI flash chip (RDID, " |
| 333 | "SFDP)\n"); |
| 334 | } |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 335 | if (!strcmp(tmp, "W25Q128FV")) { |
| 336 | emu_chip = EMULATE_WINBOND_W25Q128FV; |
| 337 | emu_chip_size = 16 * 1024 * 1024; |
| 338 | emu_max_byteprogram_size = 256; |
| 339 | emu_max_aai_size = 0; |
| 340 | emu_jedec_se_size = 4 * 1024; |
| 341 | emu_jedec_be_52_size = 32 * 1024; |
| 342 | emu_jedec_be_d8_size = 64 * 1024; |
| 343 | emu_jedec_ce_60_size = emu_chip_size; |
| 344 | emu_jedec_ce_c7_size = emu_chip_size; |
| 345 | msg_pdbg("Emulating Winbond W25Q128FV SPI flash chip (RDID)\n"); |
| 346 | } |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 347 | #endif |
| 348 | if (emu_chip == EMULATE_NONE) { |
| 349 | msg_perr("Invalid chip specified for emulation: %s\n", tmp); |
| 350 | free(tmp); |
| 351 | return 1; |
| 352 | } |
| 353 | free(tmp); |
| 354 | flashchip_contents = malloc(emu_chip_size); |
| 355 | if (!flashchip_contents) { |
| 356 | msg_perr("Out of memory!\n"); |
| 357 | return 1; |
| 358 | } |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 359 | |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 360 | #ifdef EMULATE_SPI_CHIP |
| 361 | status = extract_programmer_param("spi_status"); |
| 362 | if (status) { |
| 363 | char *endptr; |
| 364 | errno = 0; |
| 365 | emu_status = strtoul(status, &endptr, 0); |
| 366 | free(status); |
| 367 | if (errno != 0 || status == endptr) { |
| 368 | msg_perr("Error: initial status register specified, " |
| 369 | "but the value could not be converted.\n"); |
| 370 | return 1; |
| 371 | } |
| 372 | msg_pdbg("Initial status register is set to 0x%02x.\n", |
| 373 | emu_status); |
| 374 | } |
| 375 | #endif |
| 376 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 377 | msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size); |
| 378 | memset(flashchip_contents, 0xff, emu_chip_size); |
| 379 | |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 380 | /* Will be freed by shutdown function if necessary. */ |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 381 | emu_persistent_image = extract_programmer_param("image"); |
| 382 | if (!emu_persistent_image) { |
| 383 | /* Nothing else to do. */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 384 | goto dummy_init_out; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 385 | } |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 386 | /* We will silently (in default verbosity) ignore the file if it does not exist (yet) or the size does |
| 387 | * not match the emulated chip. */ |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 388 | if (!stat(emu_persistent_image, &image_stat)) { |
Stefan Tauner | 23e10b8 | 2016-01-23 16:16:49 +0000 | [diff] [blame] | 389 | msg_pdbg("Found persistent image %s, %jd B ", |
| 390 | emu_persistent_image, (intmax_t)image_stat.st_size); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 391 | if (image_stat.st_size == emu_chip_size) { |
| 392 | msg_pdbg("matches.\n"); |
| 393 | msg_pdbg("Reading %s\n", emu_persistent_image); |
| 394 | read_buf_from_file(flashchip_contents, emu_chip_size, |
| 395 | emu_persistent_image); |
| 396 | } else { |
| 397 | msg_pdbg("doesn't match.\n"); |
| 398 | } |
| 399 | } |
| 400 | #endif |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 401 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 402 | dummy_init_out: |
| 403 | if (register_shutdown(dummy_shutdown, NULL)) { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 404 | free(flashchip_contents); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 405 | return 1; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 406 | } |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 407 | if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH)) |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 408 | register_par_master(&par_master_dummy, |
| 409 | dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH)); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 410 | if (dummy_buses_supported & BUS_SPI) |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 411 | register_spi_master(&spi_master_dummyflasher); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 412 | |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 416 | void *dummy_map(const char *descr, uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 417 | { |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 418 | msg_pspew("%s: Mapping %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n", |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 419 | __func__, descr, len, PRIxPTR_WIDTH, phys_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 420 | return (void *)phys_addr; |
| 421 | } |
| 422 | |
| 423 | void dummy_unmap(void *virt_addr, size_t len) |
| 424 | { |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 425 | msg_pspew("%s: Unmapping 0x%zx bytes at %p\n", __func__, len, virt_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 428 | static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 429 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 430 | msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%02x\n", __func__, addr, val); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 433 | static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 434 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 435 | msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%04x\n", __func__, addr, val); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 438 | static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 439 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 440 | msg_pspew("%s: addr=0x%" PRIxPTR ", val=0x%08x\n", __func__, addr, val); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 443 | static void dummy_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len) |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 444 | { |
| 445 | size_t i; |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 446 | msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, writing data (hex):", __func__, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 447 | for (i = 0; i < len; i++) { |
| 448 | if ((i % 16) == 0) |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 449 | msg_pspew("\n"); |
| 450 | msg_pspew("%02x ", buf[i]); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 454 | static uint8_t dummy_chip_readb(const struct flashctx *flash, const chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 455 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 456 | msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xff\n", __func__, addr); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 457 | return 0xff; |
| 458 | } |
| 459 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 460 | static uint16_t dummy_chip_readw(const struct flashctx *flash, const chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 461 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 462 | msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffff\n", __func__, addr); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 463 | return 0xffff; |
| 464 | } |
| 465 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 466 | static uint32_t dummy_chip_readl(const struct flashctx *flash, const chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 467 | { |
Stefan Tauner | c233375 | 2013-07-13 23:31:37 +0000 | [diff] [blame] | 468 | msg_pspew("%s: addr=0x%" PRIxPTR ", returning 0xffffffff\n", __func__, addr); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 469 | return 0xffffffff; |
| 470 | } |
| 471 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 472 | static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len) |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 473 | { |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 474 | msg_pspew("%s: addr=0x%" PRIxPTR ", len=0x%zx, returning array of 0xff\n", __func__, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 475 | memset(buf, 0xff, len); |
| 476 | return; |
| 477 | } |
| 478 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 479 | #if EMULATE_SPI_CHIP |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 480 | static int emulate_spi_chip_response(unsigned int writecnt, |
| 481 | unsigned int readcnt, |
| 482 | const unsigned char *writearr, |
| 483 | unsigned char *readarr) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 484 | { |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 485 | unsigned int offs, i, toread; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 486 | static int unsigned aai_offs; |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 487 | const unsigned char sst25vf040_rems_response[2] = {0xbf, 0x44}; |
| 488 | const unsigned char sst25vf032b_rems_response[2] = {0xbf, 0x4a}; |
| 489 | const unsigned char mx25l6436_rems_response[2] = {0xc2, 0x16}; |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 490 | const unsigned char w25q128fv_rems_response[2] = {0xef, 0x17}; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 491 | |
| 492 | if (writecnt == 0) { |
| 493 | msg_perr("No command sent to the chip!\n"); |
| 494 | return 1; |
| 495 | } |
Paul Menzel | ac427b2 | 2012-02-16 21:07:07 +0000 | [diff] [blame] | 496 | /* spi_blacklist has precedence over spi_ignorelist. */ |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 497 | for (i = 0; i < spi_blacklist_size; i++) { |
| 498 | if (writearr[0] == spi_blacklist[i]) { |
| 499 | msg_pdbg("Refusing blacklisted SPI command 0x%02x\n", |
| 500 | spi_blacklist[i]); |
| 501 | return SPI_INVALID_OPCODE; |
| 502 | } |
| 503 | } |
| 504 | for (i = 0; i < spi_ignorelist_size; i++) { |
| 505 | if (writearr[0] == spi_ignorelist[i]) { |
| 506 | msg_cdbg("Ignoring ignorelisted SPI command 0x%02x\n", |
| 507 | spi_ignorelist[i]); |
| 508 | /* Return success because the command does not fail, |
| 509 | * it is simply ignored. |
| 510 | */ |
| 511 | return 0; |
| 512 | } |
| 513 | } |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 514 | |
| 515 | if (emu_max_aai_size && (emu_status & SPI_SR_AAI)) { |
| 516 | if (writearr[0] != JEDEC_AAI_WORD_PROGRAM && |
| 517 | writearr[0] != JEDEC_WRDI && |
| 518 | writearr[0] != JEDEC_RDSR) { |
| 519 | msg_perr("Forbidden opcode (0x%02x) attempted during " |
| 520 | "AAI sequence!\n", writearr[0]); |
| 521 | return 0; |
| 522 | } |
| 523 | } |
| 524 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 525 | switch (writearr[0]) { |
| 526 | case JEDEC_RES: |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 527 | if (writecnt < JEDEC_RES_OUTSIZE) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 528 | break; |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 529 | /* offs calculation is only needed for SST chips which treat RES like REMS. */ |
| 530 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 531 | offs += writecnt - JEDEC_REMS_OUTSIZE; |
| 532 | switch (emu_chip) { |
| 533 | case EMULATE_ST_M25P10_RES: |
| 534 | if (readcnt > 0) |
| 535 | memset(readarr, 0x10, readcnt); |
| 536 | break; |
| 537 | case EMULATE_SST_SST25VF040_REMS: |
| 538 | for (i = 0; i < readcnt; i++) |
| 539 | readarr[i] = sst25vf040_rems_response[(offs + i) % 2]; |
| 540 | break; |
| 541 | case EMULATE_SST_SST25VF032B: |
| 542 | for (i = 0; i < readcnt; i++) |
| 543 | readarr[i] = sst25vf032b_rems_response[(offs + i) % 2]; |
| 544 | break; |
| 545 | case EMULATE_MACRONIX_MX25L6436: |
| 546 | if (readcnt > 0) |
| 547 | memset(readarr, 0x16, readcnt); |
| 548 | break; |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 549 | case EMULATE_WINBOND_W25Q128FV: |
| 550 | if (readcnt > 0) |
| 551 | memset(readarr, 0x17, readcnt); |
| 552 | break; |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 553 | default: /* ignore */ |
| 554 | break; |
| 555 | } |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 556 | break; |
| 557 | case JEDEC_REMS: |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 558 | /* REMS response has wraparound and uses an address parameter. */ |
| 559 | if (writecnt < JEDEC_REMS_OUTSIZE) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 560 | break; |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 561 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 562 | offs += writecnt - JEDEC_REMS_OUTSIZE; |
| 563 | switch (emu_chip) { |
| 564 | case EMULATE_SST_SST25VF040_REMS: |
| 565 | for (i = 0; i < readcnt; i++) |
| 566 | readarr[i] = sst25vf040_rems_response[(offs + i) % 2]; |
| 567 | break; |
| 568 | case EMULATE_SST_SST25VF032B: |
| 569 | for (i = 0; i < readcnt; i++) |
| 570 | readarr[i] = sst25vf032b_rems_response[(offs + i) % 2]; |
| 571 | break; |
| 572 | case EMULATE_MACRONIX_MX25L6436: |
| 573 | for (i = 0; i < readcnt; i++) |
| 574 | readarr[i] = mx25l6436_rems_response[(offs + i) % 2]; |
| 575 | break; |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 576 | case EMULATE_WINBOND_W25Q128FV: |
| 577 | for (i = 0; i < readcnt; i++) |
| 578 | readarr[i] = w25q128fv_rems_response[(offs + i) % 2]; |
| 579 | break; |
Carl-Daniel Hailfinger | af2cac0 | 2012-08-30 21:41:00 +0000 | [diff] [blame] | 580 | default: /* ignore */ |
| 581 | break; |
| 582 | } |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 583 | break; |
| 584 | case JEDEC_RDID: |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 585 | switch (emu_chip) { |
| 586 | case EMULATE_SST_SST25VF032B: |
| 587 | if (readcnt > 0) |
| 588 | readarr[0] = 0xbf; |
| 589 | if (readcnt > 1) |
| 590 | readarr[1] = 0x25; |
| 591 | if (readcnt > 2) |
| 592 | readarr[2] = 0x4a; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 593 | break; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 594 | case EMULATE_MACRONIX_MX25L6436: |
| 595 | if (readcnt > 0) |
| 596 | readarr[0] = 0xc2; |
| 597 | if (readcnt > 1) |
| 598 | readarr[1] = 0x20; |
| 599 | if (readcnt > 2) |
| 600 | readarr[2] = 0x17; |
| 601 | break; |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 602 | case EMULATE_WINBOND_W25Q128FV: |
| 603 | if (readcnt > 0) |
| 604 | readarr[0] = 0xef; |
| 605 | if (readcnt > 1) |
| 606 | readarr[1] = 0x40; |
| 607 | if (readcnt > 2) |
| 608 | readarr[2] = 0x18; |
| 609 | break; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 610 | default: /* ignore */ |
| 611 | break; |
| 612 | } |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 613 | break; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 614 | case JEDEC_RDSR: |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 615 | memset(readarr, emu_status, readcnt); |
| 616 | break; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 617 | /* FIXME: this should be chip-specific. */ |
| 618 | case JEDEC_EWSR: |
| 619 | case JEDEC_WREN: |
| 620 | emu_status |= SPI_SR_WEL; |
| 621 | break; |
| 622 | case JEDEC_WRSR: |
| 623 | if (!(emu_status & SPI_SR_WEL)) { |
| 624 | msg_perr("WRSR attempted, but WEL is 0!\n"); |
| 625 | break; |
| 626 | } |
| 627 | /* FIXME: add some reasonable simulation of the busy flag */ |
| 628 | emu_status = writearr[1] & ~SPI_SR_WIP; |
| 629 | msg_pdbg2("WRSR wrote 0x%02x.\n", emu_status); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 630 | break; |
| 631 | case JEDEC_READ: |
| 632 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 633 | /* Truncate to emu_chip_size. */ |
| 634 | offs %= emu_chip_size; |
| 635 | if (readcnt > 0) |
| 636 | memcpy(readarr, flashchip_contents + offs, readcnt); |
| 637 | break; |
| 638 | case JEDEC_BYTE_PROGRAM: |
| 639 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 640 | /* Truncate to emu_chip_size. */ |
| 641 | offs %= emu_chip_size; |
| 642 | if (writecnt < 5) { |
| 643 | msg_perr("BYTE PROGRAM size too short!\n"); |
| 644 | return 1; |
| 645 | } |
| 646 | if (writecnt - 4 > emu_max_byteprogram_size) { |
| 647 | msg_perr("Max BYTE PROGRAM size exceeded!\n"); |
| 648 | return 1; |
| 649 | } |
| 650 | memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4); |
| 651 | break; |
| 652 | case JEDEC_AAI_WORD_PROGRAM: |
| 653 | if (!emu_max_aai_size) |
| 654 | break; |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 655 | if (!(emu_status & SPI_SR_AAI)) { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 656 | if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) { |
| 657 | msg_perr("Initial AAI WORD PROGRAM size too " |
| 658 | "short!\n"); |
| 659 | return 1; |
| 660 | } |
| 661 | if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) { |
| 662 | msg_perr("Initial AAI WORD PROGRAM size too " |
| 663 | "long!\n"); |
| 664 | return 1; |
| 665 | } |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 666 | emu_status |= SPI_SR_AAI; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 667 | aai_offs = writearr[1] << 16 | writearr[2] << 8 | |
| 668 | writearr[3]; |
| 669 | /* Truncate to emu_chip_size. */ |
| 670 | aai_offs %= emu_chip_size; |
| 671 | memcpy(flashchip_contents + aai_offs, writearr + 4, 2); |
| 672 | aai_offs += 2; |
| 673 | } else { |
| 674 | if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) { |
| 675 | msg_perr("Continuation AAI WORD PROGRAM size " |
| 676 | "too short!\n"); |
| 677 | return 1; |
| 678 | } |
| 679 | if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) { |
| 680 | msg_perr("Continuation AAI WORD PROGRAM size " |
| 681 | "too long!\n"); |
| 682 | return 1; |
| 683 | } |
| 684 | memcpy(flashchip_contents + aai_offs, writearr + 1, 2); |
| 685 | aai_offs += 2; |
| 686 | } |
| 687 | break; |
| 688 | case JEDEC_WRDI: |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 689 | if (emu_max_aai_size) |
| 690 | emu_status &= ~SPI_SR_AAI; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 691 | break; |
| 692 | case JEDEC_SE: |
| 693 | if (!emu_jedec_se_size) |
| 694 | break; |
| 695 | if (writecnt != JEDEC_SE_OUTSIZE) { |
| 696 | msg_perr("SECTOR ERASE 0x20 outsize invalid!\n"); |
| 697 | return 1; |
| 698 | } |
| 699 | if (readcnt != JEDEC_SE_INSIZE) { |
| 700 | msg_perr("SECTOR ERASE 0x20 insize invalid!\n"); |
| 701 | return 1; |
| 702 | } |
| 703 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 704 | if (offs & (emu_jedec_se_size - 1)) |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 705 | msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 706 | offs &= ~(emu_jedec_se_size - 1); |
| 707 | memset(flashchip_contents + offs, 0xff, emu_jedec_se_size); |
| 708 | break; |
| 709 | case JEDEC_BE_52: |
| 710 | if (!emu_jedec_be_52_size) |
| 711 | break; |
| 712 | if (writecnt != JEDEC_BE_52_OUTSIZE) { |
| 713 | msg_perr("BLOCK ERASE 0x52 outsize invalid!\n"); |
| 714 | return 1; |
| 715 | } |
| 716 | if (readcnt != JEDEC_BE_52_INSIZE) { |
| 717 | msg_perr("BLOCK ERASE 0x52 insize invalid!\n"); |
| 718 | return 1; |
| 719 | } |
| 720 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 721 | if (offs & (emu_jedec_be_52_size - 1)) |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 722 | msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 723 | offs &= ~(emu_jedec_be_52_size - 1); |
| 724 | memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size); |
| 725 | break; |
| 726 | case JEDEC_BE_D8: |
| 727 | if (!emu_jedec_be_d8_size) |
| 728 | break; |
| 729 | if (writecnt != JEDEC_BE_D8_OUTSIZE) { |
| 730 | msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n"); |
| 731 | return 1; |
| 732 | } |
| 733 | if (readcnt != JEDEC_BE_D8_INSIZE) { |
| 734 | msg_perr("BLOCK ERASE 0xd8 insize invalid!\n"); |
| 735 | return 1; |
| 736 | } |
| 737 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 738 | if (offs & (emu_jedec_be_d8_size - 1)) |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 739 | msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 740 | offs &= ~(emu_jedec_be_d8_size - 1); |
| 741 | memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size); |
| 742 | break; |
| 743 | case JEDEC_CE_60: |
| 744 | if (!emu_jedec_ce_60_size) |
| 745 | break; |
| 746 | if (writecnt != JEDEC_CE_60_OUTSIZE) { |
| 747 | msg_perr("CHIP ERASE 0x60 outsize invalid!\n"); |
| 748 | return 1; |
| 749 | } |
| 750 | if (readcnt != JEDEC_CE_60_INSIZE) { |
| 751 | msg_perr("CHIP ERASE 0x60 insize invalid!\n"); |
| 752 | return 1; |
| 753 | } |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 754 | /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */ |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 755 | /* emu_jedec_ce_60_size is emu_chip_size. */ |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 756 | memset(flashchip_contents, 0xff, emu_jedec_ce_60_size); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 757 | break; |
| 758 | case JEDEC_CE_C7: |
| 759 | if (!emu_jedec_ce_c7_size) |
| 760 | break; |
| 761 | if (writecnt != JEDEC_CE_C7_OUTSIZE) { |
| 762 | msg_perr("CHIP ERASE 0xc7 outsize invalid!\n"); |
| 763 | return 1; |
| 764 | } |
| 765 | if (readcnt != JEDEC_CE_C7_INSIZE) { |
| 766 | msg_perr("CHIP ERASE 0xc7 insize invalid!\n"); |
| 767 | return 1; |
| 768 | } |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 769 | /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */ |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 770 | /* emu_jedec_ce_c7_size is emu_chip_size. */ |
| 771 | memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size); |
| 772 | break; |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 773 | case JEDEC_SFDP: |
| 774 | if (emu_chip != EMULATE_MACRONIX_MX25L6436) |
| 775 | break; |
| 776 | if (writecnt < 4) |
| 777 | break; |
| 778 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 779 | |
| 780 | /* SFDP expects one dummy byte after the address. */ |
| 781 | if (writecnt == 4) { |
| 782 | /* The dummy byte was not written, make sure it is read instead. |
| 783 | * Shifting and shortening the read array does achieve this goal. |
| 784 | */ |
| 785 | readarr++; |
| 786 | readcnt--; |
| 787 | } else { |
| 788 | /* The response is shifted if more than 5 bytes are written, because SFDP data is |
| 789 | * already shifted out by the chip while those superfluous bytes are written. */ |
| 790 | offs += writecnt - 5; |
| 791 | } |
| 792 | |
| 793 | /* The SFDP spec implies that the start address of an SFDP read may be truncated to fit in the |
| 794 | * SFDP table address space, i.e. the start address may be wrapped around at SFDP table size. |
| 795 | * This is a reasonable implementation choice in hardware because it saves a few gates. */ |
| 796 | if (offs >= sizeof(sfdp_table)) { |
| 797 | msg_pdbg("Wrapping the start address around the SFDP table boundary (using 0x%x " |
| 798 | "instead of 0x%x).\n", (unsigned int)(offs % sizeof(sfdp_table)), offs); |
| 799 | offs %= sizeof(sfdp_table); |
| 800 | } |
| 801 | toread = min(sizeof(sfdp_table) - offs, readcnt); |
| 802 | memcpy(readarr, sfdp_table + offs, toread); |
| 803 | if (toread < readcnt) |
| 804 | msg_pdbg("Crossing the SFDP table boundary in a single " |
| 805 | "continuous chunk produces undefined results " |
| 806 | "after that point.\n"); |
| 807 | break; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 808 | default: |
| 809 | /* No special response. */ |
| 810 | break; |
| 811 | } |
Stefan Tauner | 5e695ab | 2012-05-06 17:03:40 +0000 | [diff] [blame] | 812 | if (writearr[0] != JEDEC_WREN && writearr[0] != JEDEC_EWSR) |
| 813 | emu_status &= ~SPI_SR_WEL; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 814 | return 0; |
| 815 | } |
| 816 | #endif |
| 817 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 818 | static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt, |
| 819 | unsigned int readcnt, |
| 820 | const unsigned char *writearr, |
| 821 | unsigned char *readarr) |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 822 | { |
| 823 | int i; |
| 824 | |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 825 | msg_pspew("%s:", __func__); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 826 | |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 827 | msg_pspew(" writing %u bytes:", writecnt); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 828 | for (i = 0; i < writecnt; i++) |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 829 | msg_pspew(" 0x%02x", writearr[i]); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 830 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 831 | /* Response for unknown commands and missing chip is 0xff. */ |
| 832 | memset(readarr, 0xff, readcnt); |
| 833 | #if EMULATE_SPI_CHIP |
| 834 | switch (emu_chip) { |
| 835 | case EMULATE_ST_M25P10_RES: |
| 836 | case EMULATE_SST_SST25VF040_REMS: |
| 837 | case EMULATE_SST_SST25VF032B: |
Stefan Tauner | 0b9df97 | 2012-05-07 22:12:16 +0000 | [diff] [blame] | 838 | case EMULATE_MACRONIX_MX25L6436: |
Nico Huber | f9632d8 | 2019-01-20 11:23:49 +0100 | [diff] [blame] | 839 | case EMULATE_WINBOND_W25Q128FV: |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 840 | if (emulate_spi_chip_response(writecnt, readcnt, writearr, |
| 841 | readarr)) { |
Carl-Daniel Hailfinger | 1b83be5 | 2012-02-08 23:28:54 +0000 | [diff] [blame] | 842 | msg_pdbg("Invalid command sent to flash chip!\n"); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 843 | return 1; |
| 844 | } |
| 845 | break; |
| 846 | default: |
| 847 | break; |
| 848 | } |
| 849 | #endif |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 850 | msg_pspew(" reading %u bytes:", readcnt); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 851 | for (i = 0; i < readcnt; i++) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 852 | msg_pspew(" 0x%02x", readarr[i]); |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 853 | msg_pspew("\n"); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 854 | return 0; |
| 855 | } |
Carl-Daniel Hailfinger | 1b0ba89 | 2010-06-20 10:58:32 +0000 | [diff] [blame] | 856 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 857 | static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 858 | { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 859 | return spi_write_chunked(flash, buf, start, len, |
| 860 | spi_write_256_chunksize); |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 861 | } |