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 | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 22 | #include "flash.h" |
Carl-Daniel Hailfinger | 1b0ba89 | 2010-06-20 10:58:32 +0000 | [diff] [blame] | 23 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 24 | #include "programmer.h" |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 25 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 26 | /* Remove the #define below if you don't want SPI flash chip emulation. */ |
| 27 | #define EMULATE_SPI_CHIP 1 |
| 28 | |
| 29 | #if EMULATE_SPI_CHIP |
| 30 | #define EMULATE_CHIP 1 |
| 31 | #include "spi.h" |
| 32 | #endif |
| 33 | |
| 34 | #if EMULATE_CHIP |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #endif |
| 38 | |
| 39 | #if EMULATE_CHIP |
| 40 | static uint8_t *flashchip_contents = NULL; |
| 41 | enum emu_chip { |
| 42 | EMULATE_NONE, |
| 43 | EMULATE_ST_M25P10_RES, |
| 44 | EMULATE_SST_SST25VF040_REMS, |
| 45 | EMULATE_SST_SST25VF032B, |
| 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 | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 58 | #endif |
| 59 | #endif |
| 60 | |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 61 | static unsigned int spi_write_256_chunksize = 256; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 62 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 63 | static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt, |
| 64 | unsigned int readcnt, |
| 65 | const unsigned char *writearr, |
| 66 | unsigned char *readarr); |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 67 | static int dummy_spi_write_256(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 68 | unsigned int start, unsigned int len); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 69 | static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, |
| 70 | chipaddr addr); |
| 71 | static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, |
| 72 | chipaddr addr); |
| 73 | static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, |
| 74 | chipaddr addr); |
| 75 | static void dummy_chip_writen(const struct flashctx *flash, uint8_t *buf, |
| 76 | chipaddr addr, size_t len); |
| 77 | static uint8_t dummy_chip_readb(const struct flashctx *flash, |
| 78 | const chipaddr addr); |
| 79 | static uint16_t dummy_chip_readw(const struct flashctx *flash, |
| 80 | const chipaddr addr); |
| 81 | static uint32_t dummy_chip_readl(const struct flashctx *flash, |
| 82 | const chipaddr addr); |
| 83 | static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, |
| 84 | const chipaddr addr, size_t len); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 85 | |
| 86 | static const struct spi_programmer spi_programmer_dummyflasher = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 87 | .type = SPI_CONTROLLER_DUMMY, |
| 88 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 89 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 90 | .command = dummy_spi_send_command, |
| 91 | .multicommand = default_spi_send_multicommand, |
| 92 | .read = default_spi_read, |
| 93 | .write_256 = dummy_spi_write_256, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 94 | }; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 95 | |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 96 | static const struct par_programmer par_programmer_dummy = { |
| 97 | .chip_readb = dummy_chip_readb, |
| 98 | .chip_readw = dummy_chip_readw, |
| 99 | .chip_readl = dummy_chip_readl, |
| 100 | .chip_readn = dummy_chip_readn, |
| 101 | .chip_writeb = dummy_chip_writeb, |
| 102 | .chip_writew = dummy_chip_writew, |
| 103 | .chip_writel = dummy_chip_writel, |
| 104 | .chip_writen = dummy_chip_writen, |
| 105 | }; |
| 106 | |
| 107 | enum chipbustype dummy_buses_supported = BUS_NONE; |
| 108 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 109 | static int dummy_shutdown(void *data) |
| 110 | { |
| 111 | msg_pspew("%s\n", __func__); |
| 112 | #if EMULATE_CHIP |
| 113 | if (emu_chip != EMULATE_NONE) { |
| 114 | if (emu_persistent_image) { |
| 115 | msg_pdbg("Writing %s\n", emu_persistent_image); |
| 116 | write_buf_to_file(flashchip_contents, emu_chip_size, |
| 117 | emu_persistent_image); |
| 118 | } |
| 119 | free(flashchip_contents); |
| 120 | } |
| 121 | #endif |
| 122 | return 0; |
| 123 | } |
| 124 | |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 125 | int dummy_init(void) |
| 126 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 127 | char *bustext = NULL; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 128 | char *tmp = NULL; |
| 129 | #if EMULATE_CHIP |
| 130 | struct stat image_stat; |
| 131 | #endif |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 132 | |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 133 | msg_pspew("%s\n", __func__); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 134 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 135 | bustext = extract_programmer_param("bus"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 136 | msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default"); |
| 137 | if (!bustext) |
| 138 | bustext = strdup("parallel+lpc+fwh+spi"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 139 | /* Convert the parameters to lowercase. */ |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 140 | tolower_string(bustext); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 141 | |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 142 | dummy_buses_supported = BUS_NONE; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 143 | if (strstr(bustext, "parallel")) { |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 144 | dummy_buses_supported |= BUS_PARALLEL; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 145 | msg_pdbg("Enabling support for %s flash.\n", "parallel"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 146 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 147 | if (strstr(bustext, "lpc")) { |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 148 | dummy_buses_supported |= BUS_LPC; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 149 | msg_pdbg("Enabling support for %s flash.\n", "LPC"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 150 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 151 | if (strstr(bustext, "fwh")) { |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 152 | dummy_buses_supported |= BUS_FWH; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 153 | msg_pdbg("Enabling support for %s flash.\n", "FWH"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 154 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 155 | if (strstr(bustext, "spi")) { |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 156 | dummy_buses_supported |= BUS_SPI; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 157 | msg_pdbg("Enabling support for %s flash.\n", "SPI"); |
Carl-Daniel Hailfinger | 3504b53 | 2009-06-01 00:02:11 +0000 | [diff] [blame] | 158 | } |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 159 | if (dummy_buses_supported == BUS_NONE) |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 160 | msg_pdbg("Support for all flash bus types disabled.\n"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 161 | free(bustext); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 162 | |
| 163 | tmp = extract_programmer_param("spi_write_256_chunksize"); |
| 164 | if (tmp) { |
| 165 | spi_write_256_chunksize = atoi(tmp); |
| 166 | free(tmp); |
| 167 | if (spi_write_256_chunksize < 1) { |
| 168 | msg_perr("invalid spi_write_256_chunksize\n"); |
| 169 | return 1; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | #if EMULATE_CHIP |
| 174 | tmp = extract_programmer_param("emulate"); |
| 175 | if (!tmp) { |
| 176 | msg_pdbg("Not emulating any flash chip.\n"); |
| 177 | /* Nothing else to do. */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 178 | goto dummy_init_out; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 179 | } |
| 180 | #if EMULATE_SPI_CHIP |
| 181 | if (!strcmp(tmp, "M25P10.RES")) { |
| 182 | emu_chip = EMULATE_ST_M25P10_RES; |
| 183 | emu_chip_size = 128 * 1024; |
| 184 | emu_max_byteprogram_size = 128; |
| 185 | emu_max_aai_size = 0; |
| 186 | emu_jedec_se_size = 0; |
| 187 | emu_jedec_be_52_size = 0; |
| 188 | emu_jedec_be_d8_size = 32 * 1024; |
| 189 | emu_jedec_ce_60_size = 0; |
| 190 | emu_jedec_ce_c7_size = emu_chip_size; |
| 191 | msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page " |
| 192 | "write)\n"); |
| 193 | } |
| 194 | if (!strcmp(tmp, "SST25VF040.REMS")) { |
| 195 | emu_chip = EMULATE_SST_SST25VF040_REMS; |
| 196 | emu_chip_size = 512 * 1024; |
| 197 | emu_max_byteprogram_size = 1; |
| 198 | emu_max_aai_size = 0; |
| 199 | emu_jedec_se_size = 4 * 1024; |
| 200 | emu_jedec_be_52_size = 32 * 1024; |
| 201 | emu_jedec_be_d8_size = 0; |
| 202 | emu_jedec_ce_60_size = emu_chip_size; |
| 203 | emu_jedec_ce_c7_size = 0; |
| 204 | msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, " |
| 205 | "byte write)\n"); |
| 206 | } |
| 207 | if (!strcmp(tmp, "SST25VF032B")) { |
| 208 | emu_chip = EMULATE_SST_SST25VF032B; |
| 209 | emu_chip_size = 4 * 1024 * 1024; |
| 210 | emu_max_byteprogram_size = 1; |
| 211 | emu_max_aai_size = 2; |
| 212 | emu_jedec_se_size = 4 * 1024; |
| 213 | emu_jedec_be_52_size = 32 * 1024; |
| 214 | emu_jedec_be_d8_size = 64 * 1024; |
| 215 | emu_jedec_ce_60_size = emu_chip_size; |
| 216 | emu_jedec_ce_c7_size = emu_chip_size; |
| 217 | msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI " |
| 218 | "write)\n"); |
| 219 | } |
| 220 | #endif |
| 221 | if (emu_chip == EMULATE_NONE) { |
| 222 | msg_perr("Invalid chip specified for emulation: %s\n", tmp); |
| 223 | free(tmp); |
| 224 | return 1; |
| 225 | } |
| 226 | free(tmp); |
| 227 | flashchip_contents = malloc(emu_chip_size); |
| 228 | if (!flashchip_contents) { |
| 229 | msg_perr("Out of memory!\n"); |
| 230 | return 1; |
| 231 | } |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 232 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 233 | msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size); |
| 234 | memset(flashchip_contents, 0xff, emu_chip_size); |
| 235 | |
| 236 | emu_persistent_image = extract_programmer_param("image"); |
| 237 | if (!emu_persistent_image) { |
| 238 | /* Nothing else to do. */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 239 | goto dummy_init_out; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 240 | } |
| 241 | if (!stat(emu_persistent_image, &image_stat)) { |
| 242 | msg_pdbg("Found persistent image %s, size %li ", |
| 243 | emu_persistent_image, (long)image_stat.st_size); |
| 244 | if (image_stat.st_size == emu_chip_size) { |
| 245 | msg_pdbg("matches.\n"); |
| 246 | msg_pdbg("Reading %s\n", emu_persistent_image); |
| 247 | read_buf_from_file(flashchip_contents, emu_chip_size, |
| 248 | emu_persistent_image); |
| 249 | } else { |
| 250 | msg_pdbg("doesn't match.\n"); |
| 251 | } |
| 252 | } |
| 253 | #endif |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 254 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 255 | dummy_init_out: |
| 256 | if (register_shutdown(dummy_shutdown, NULL)) { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 257 | free(flashchip_contents); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 258 | return 1; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 259 | } |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 260 | if (dummy_buses_supported & (BUS_PARALLEL | BUS_LPC | BUS_FWH)) |
| 261 | register_par_programmer(&par_programmer_dummy, |
| 262 | dummy_buses_supported & |
| 263 | (BUS_PARALLEL | BUS_LPC | |
| 264 | BUS_FWH)); |
| 265 | if (dummy_buses_supported & BUS_SPI) |
| 266 | register_spi_programmer(&spi_programmer_dummyflasher); |
| 267 | |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 271 | void *dummy_map(const char *descr, unsigned long phys_addr, size_t len) |
| 272 | { |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 273 | msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n", |
| 274 | __func__, descr, (unsigned long)len, phys_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 275 | return (void *)phys_addr; |
| 276 | } |
| 277 | |
| 278 | void dummy_unmap(void *virt_addr, size_t len) |
| 279 | { |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 280 | msg_pspew("%s: Unmapping 0x%lx bytes at %p\n", |
| 281 | __func__, (unsigned long)len, virt_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 284 | static void dummy_chip_writeb(const struct flashctx *flash, uint8_t val, |
| 285 | chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 286 | { |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 287 | msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 290 | static void dummy_chip_writew(const struct flashctx *flash, uint16_t val, |
| 291 | chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 292 | { |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 293 | msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 296 | static void dummy_chip_writel(const struct flashctx *flash, uint32_t val, |
| 297 | chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 298 | { |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 299 | msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 302 | static void dummy_chip_writen(const struct flashctx *flash, uint8_t *buf, |
| 303 | chipaddr addr, size_t len) |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 304 | { |
| 305 | size_t i; |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 306 | msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):", |
| 307 | __func__, addr, (unsigned long)len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 308 | for (i = 0; i < len; i++) { |
| 309 | if ((i % 16) == 0) |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 310 | msg_pspew("\n"); |
| 311 | msg_pspew("%02x ", buf[i]); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 315 | static uint8_t dummy_chip_readb(const struct flashctx *flash, |
| 316 | const chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 317 | { |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 318 | msg_pspew("%s: addr=0x%lx, returning 0xff\n", __func__, addr); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 319 | return 0xff; |
| 320 | } |
| 321 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 322 | static uint16_t dummy_chip_readw(const struct flashctx *flash, |
| 323 | const chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 324 | { |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 325 | msg_pspew("%s: addr=0x%lx, returning 0xffff\n", __func__, addr); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 326 | return 0xffff; |
| 327 | } |
| 328 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 329 | static uint32_t dummy_chip_readl(const struct flashctx *flash, |
| 330 | const chipaddr addr) |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 331 | { |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 332 | msg_pspew("%s: addr=0x%lx, returning 0xffffffff\n", __func__, addr); |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 333 | return 0xffffffff; |
| 334 | } |
| 335 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 336 | static void dummy_chip_readn(const struct flashctx *flash, uint8_t *buf, |
| 337 | const chipaddr addr, size_t len) |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 338 | { |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 339 | msg_pspew("%s: addr=0x%lx, len=0x%lx, returning array of 0xff\n", |
| 340 | __func__, addr, (unsigned long)len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 341 | memset(buf, 0xff, len); |
| 342 | return; |
| 343 | } |
| 344 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 345 | #if EMULATE_SPI_CHIP |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 346 | static int emulate_spi_chip_response(unsigned int writecnt, |
| 347 | unsigned int readcnt, |
| 348 | const unsigned char *writearr, |
| 349 | unsigned char *readarr) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 350 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 351 | unsigned int offs; |
| 352 | static int unsigned aai_offs; |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 353 | static int aai_active = 0; |
| 354 | |
| 355 | if (writecnt == 0) { |
| 356 | msg_perr("No command sent to the chip!\n"); |
| 357 | return 1; |
| 358 | } |
| 359 | /* TODO: Implement command blacklists here. */ |
| 360 | switch (writearr[0]) { |
| 361 | case JEDEC_RES: |
| 362 | if (emu_chip != EMULATE_ST_M25P10_RES) |
| 363 | break; |
| 364 | /* Respond with ST_M25P10_RES. */ |
| 365 | if (readcnt > 0) |
| 366 | readarr[0] = 0x10; |
| 367 | break; |
| 368 | case JEDEC_REMS: |
| 369 | if (emu_chip != EMULATE_SST_SST25VF040_REMS) |
| 370 | break; |
| 371 | /* Respond with SST_SST25VF040_REMS. */ |
| 372 | if (readcnt > 0) |
| 373 | readarr[0] = 0xbf; |
| 374 | if (readcnt > 1) |
| 375 | readarr[1] = 0x44; |
| 376 | break; |
| 377 | case JEDEC_RDID: |
| 378 | if (emu_chip != EMULATE_SST_SST25VF032B) |
| 379 | break; |
| 380 | /* Respond with SST_SST25VF032B. */ |
| 381 | if (readcnt > 0) |
| 382 | readarr[0] = 0xbf; |
| 383 | if (readcnt > 1) |
| 384 | readarr[1] = 0x25; |
| 385 | if (readcnt > 2) |
| 386 | readarr[2] = 0x4a; |
| 387 | break; |
| 388 | case JEDEC_RDSR: |
| 389 | memset(readarr, 0, readcnt); |
| 390 | if (aai_active) |
| 391 | memset(readarr, 1 << 6, readcnt); |
| 392 | break; |
| 393 | case JEDEC_READ: |
| 394 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 395 | /* Truncate to emu_chip_size. */ |
| 396 | offs %= emu_chip_size; |
| 397 | if (readcnt > 0) |
| 398 | memcpy(readarr, flashchip_contents + offs, readcnt); |
| 399 | break; |
| 400 | case JEDEC_BYTE_PROGRAM: |
| 401 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 402 | /* Truncate to emu_chip_size. */ |
| 403 | offs %= emu_chip_size; |
| 404 | if (writecnt < 5) { |
| 405 | msg_perr("BYTE PROGRAM size too short!\n"); |
| 406 | return 1; |
| 407 | } |
| 408 | if (writecnt - 4 > emu_max_byteprogram_size) { |
| 409 | msg_perr("Max BYTE PROGRAM size exceeded!\n"); |
| 410 | return 1; |
| 411 | } |
| 412 | memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4); |
| 413 | break; |
| 414 | case JEDEC_AAI_WORD_PROGRAM: |
| 415 | if (!emu_max_aai_size) |
| 416 | break; |
| 417 | if (!aai_active) { |
| 418 | if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) { |
| 419 | msg_perr("Initial AAI WORD PROGRAM size too " |
| 420 | "short!\n"); |
| 421 | return 1; |
| 422 | } |
| 423 | if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) { |
| 424 | msg_perr("Initial AAI WORD PROGRAM size too " |
| 425 | "long!\n"); |
| 426 | return 1; |
| 427 | } |
| 428 | aai_active = 1; |
| 429 | aai_offs = writearr[1] << 16 | writearr[2] << 8 | |
| 430 | writearr[3]; |
| 431 | /* Truncate to emu_chip_size. */ |
| 432 | aai_offs %= emu_chip_size; |
| 433 | memcpy(flashchip_contents + aai_offs, writearr + 4, 2); |
| 434 | aai_offs += 2; |
| 435 | } else { |
| 436 | if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) { |
| 437 | msg_perr("Continuation AAI WORD PROGRAM size " |
| 438 | "too short!\n"); |
| 439 | return 1; |
| 440 | } |
| 441 | if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) { |
| 442 | msg_perr("Continuation AAI WORD PROGRAM size " |
| 443 | "too long!\n"); |
| 444 | return 1; |
| 445 | } |
| 446 | memcpy(flashchip_contents + aai_offs, writearr + 1, 2); |
| 447 | aai_offs += 2; |
| 448 | } |
| 449 | break; |
| 450 | case JEDEC_WRDI: |
| 451 | if (!emu_max_aai_size) |
| 452 | break; |
| 453 | aai_active = 0; |
| 454 | break; |
| 455 | case JEDEC_SE: |
| 456 | if (!emu_jedec_se_size) |
| 457 | break; |
| 458 | if (writecnt != JEDEC_SE_OUTSIZE) { |
| 459 | msg_perr("SECTOR ERASE 0x20 outsize invalid!\n"); |
| 460 | return 1; |
| 461 | } |
| 462 | if (readcnt != JEDEC_SE_INSIZE) { |
| 463 | msg_perr("SECTOR ERASE 0x20 insize invalid!\n"); |
| 464 | return 1; |
| 465 | } |
| 466 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 467 | if (offs & (emu_jedec_se_size - 1)) |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 468 | msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 469 | offs &= ~(emu_jedec_se_size - 1); |
| 470 | memset(flashchip_contents + offs, 0xff, emu_jedec_se_size); |
| 471 | break; |
| 472 | case JEDEC_BE_52: |
| 473 | if (!emu_jedec_be_52_size) |
| 474 | break; |
| 475 | if (writecnt != JEDEC_BE_52_OUTSIZE) { |
| 476 | msg_perr("BLOCK ERASE 0x52 outsize invalid!\n"); |
| 477 | return 1; |
| 478 | } |
| 479 | if (readcnt != JEDEC_BE_52_INSIZE) { |
| 480 | msg_perr("BLOCK ERASE 0x52 insize invalid!\n"); |
| 481 | return 1; |
| 482 | } |
| 483 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 484 | if (offs & (emu_jedec_be_52_size - 1)) |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 485 | msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 486 | offs &= ~(emu_jedec_be_52_size - 1); |
| 487 | memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size); |
| 488 | break; |
| 489 | case JEDEC_BE_D8: |
| 490 | if (!emu_jedec_be_d8_size) |
| 491 | break; |
| 492 | if (writecnt != JEDEC_BE_D8_OUTSIZE) { |
| 493 | msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n"); |
| 494 | return 1; |
| 495 | } |
| 496 | if (readcnt != JEDEC_BE_D8_INSIZE) { |
| 497 | msg_perr("BLOCK ERASE 0xd8 insize invalid!\n"); |
| 498 | return 1; |
| 499 | } |
| 500 | offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3]; |
| 501 | if (offs & (emu_jedec_be_d8_size - 1)) |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 502 | msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 503 | offs &= ~(emu_jedec_be_d8_size - 1); |
| 504 | memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size); |
| 505 | break; |
| 506 | case JEDEC_CE_60: |
| 507 | if (!emu_jedec_ce_60_size) |
| 508 | break; |
| 509 | if (writecnt != JEDEC_CE_60_OUTSIZE) { |
| 510 | msg_perr("CHIP ERASE 0x60 outsize invalid!\n"); |
| 511 | return 1; |
| 512 | } |
| 513 | if (readcnt != JEDEC_CE_60_INSIZE) { |
| 514 | msg_perr("CHIP ERASE 0x60 insize invalid!\n"); |
| 515 | return 1; |
| 516 | } |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 517 | /* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */ |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 518 | /* emu_jedec_ce_60_size is emu_chip_size. */ |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 519 | memset(flashchip_contents, 0xff, emu_jedec_ce_60_size); |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 520 | break; |
| 521 | case JEDEC_CE_C7: |
| 522 | if (!emu_jedec_ce_c7_size) |
| 523 | break; |
| 524 | if (writecnt != JEDEC_CE_C7_OUTSIZE) { |
| 525 | msg_perr("CHIP ERASE 0xc7 outsize invalid!\n"); |
| 526 | return 1; |
| 527 | } |
| 528 | if (readcnt != JEDEC_CE_C7_INSIZE) { |
| 529 | msg_perr("CHIP ERASE 0xc7 insize invalid!\n"); |
| 530 | return 1; |
| 531 | } |
Carl-Daniel Hailfinger | 146b77d | 2011-02-04 22:52:04 +0000 | [diff] [blame] | 532 | /* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */ |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 533 | /* emu_jedec_ce_c7_size is emu_chip_size. */ |
| 534 | memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size); |
| 535 | break; |
| 536 | default: |
| 537 | /* No special response. */ |
| 538 | break; |
| 539 | } |
| 540 | return 0; |
| 541 | } |
| 542 | #endif |
| 543 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 544 | static int dummy_spi_send_command(struct flashctx *flash, unsigned int writecnt, |
| 545 | unsigned int readcnt, |
| 546 | const unsigned char *writearr, |
| 547 | unsigned char *readarr) |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 548 | { |
| 549 | int i; |
| 550 | |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 551 | msg_pspew("%s:", __func__); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 552 | |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 553 | msg_pspew(" writing %u bytes:", writecnt); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 554 | for (i = 0; i < writecnt; i++) |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 555 | msg_pspew(" 0x%02x", writearr[i]); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 556 | |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 557 | /* Response for unknown commands and missing chip is 0xff. */ |
| 558 | memset(readarr, 0xff, readcnt); |
| 559 | #if EMULATE_SPI_CHIP |
| 560 | switch (emu_chip) { |
| 561 | case EMULATE_ST_M25P10_RES: |
| 562 | case EMULATE_SST_SST25VF040_REMS: |
| 563 | case EMULATE_SST_SST25VF032B: |
| 564 | if (emulate_spi_chip_response(writecnt, readcnt, writearr, |
| 565 | readarr)) { |
| 566 | msg_perr("Invalid command sent to flash chip!\n"); |
| 567 | return 1; |
| 568 | } |
| 569 | break; |
| 570 | default: |
| 571 | break; |
| 572 | } |
| 573 | #endif |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 574 | msg_pspew(" reading %u bytes:", readcnt); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 575 | for (i = 0; i < readcnt; i++) |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 576 | msg_pspew(" 0x%02x", readarr[i]); |
Carl-Daniel Hailfinger | 3ac101c | 2010-01-09 04:32:23 +0000 | [diff] [blame] | 577 | msg_pspew("\n"); |
Carl-Daniel Hailfinger | bfe2e0c | 2009-05-14 12:59:36 +0000 | [diff] [blame] | 578 | return 0; |
| 579 | } |
Carl-Daniel Hailfinger | 1b0ba89 | 2010-06-20 10:58:32 +0000 | [diff] [blame] | 580 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 581 | static int dummy_spi_write_256(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 582 | unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 583 | { |
Carl-Daniel Hailfinger | f68aa8a | 2010-11-01 22:07:04 +0000 | [diff] [blame] | 584 | return spi_write_chunked(flash, buf, start, len, |
| 585 | spi_write_256_chunksize); |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 586 | } |