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