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