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