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