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