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