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