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