Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
Nico Huber | f5fcd74 | 2023-03-11 17:11:12 +0000 | [diff] [blame^] | 15 | #include <stdbool.h> |
Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 16 | #include <stdint.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include "flash.h" |
| 21 | #include "hwaccess_physmap.h" |
| 22 | #include "programmer.h" |
| 23 | #include "spi.h" |
| 24 | |
| 25 | #define SPI100_FIFO_SIZE 71 |
| 26 | |
| 27 | struct spi100 { |
| 28 | uint8_t *spibar; |
Nico Huber | e3c305d | 2023-01-29 21:45:56 +0000 | [diff] [blame] | 29 | uint8_t *memory; |
| 30 | size_t mapped_len; |
Nico Huber | f5fcd74 | 2023-03-11 17:11:12 +0000 | [diff] [blame^] | 31 | bool no_4ba_mmap; |
Nico Huber | e3c305d | 2023-01-29 21:45:56 +0000 | [diff] [blame] | 32 | |
Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 33 | unsigned int altspeed; |
| 34 | }; |
| 35 | |
| 36 | static void spi100_write8(const struct spi100 *spi100, unsigned int reg, uint8_t val) |
| 37 | { |
| 38 | mmio_writeb(val, spi100->spibar + reg); |
| 39 | } |
| 40 | |
| 41 | static void spi100_write16(const struct spi100 *spi100, unsigned int reg, uint16_t val) |
| 42 | { |
| 43 | mmio_writew(val, spi100->spibar + reg); |
| 44 | } |
| 45 | |
| 46 | static void spi100_writen(const struct spi100 *spi100, unsigned int reg, const uint8_t *data, size_t len) |
| 47 | { |
| 48 | for (; len > 0; --len, ++reg, ++data) |
| 49 | mmio_writeb(*data, spi100->spibar + reg); |
| 50 | } |
| 51 | |
| 52 | static uint8_t spi100_read8(const struct spi100 *spi100, unsigned int reg) |
| 53 | { |
| 54 | return mmio_readb(spi100->spibar + reg); |
| 55 | } |
| 56 | |
| 57 | static uint16_t spi100_read16(const struct spi100 *spi100, unsigned int reg) |
| 58 | { |
| 59 | return mmio_readw(spi100->spibar + reg); |
| 60 | } |
| 61 | |
| 62 | static uint32_t spi100_read32(const struct spi100 *spi100, unsigned int reg) |
| 63 | { |
| 64 | return mmio_readl(spi100->spibar + reg); |
| 65 | } |
| 66 | |
| 67 | static void spi100_readn(const struct spi100 *spi100, unsigned int reg, uint8_t *data, size_t len) |
| 68 | { |
Nico Huber | 0705878 | 2023-01-29 19:56:39 +0000 | [diff] [blame] | 69 | mmio_readn_aligned(spi100->spibar + reg, data, len, 4); |
Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static int spi100_check_readwritecnt(const unsigned int writecnt, const unsigned int readcnt) |
| 73 | { |
| 74 | if (writecnt < 1) { |
| 75 | msg_perr("ERROR: SPI controller needs to send at least 1 byte.\n"); |
| 76 | return SPI_INVALID_LENGTH; |
| 77 | } |
| 78 | |
| 79 | if (writecnt - 1 > SPI100_FIFO_SIZE) { |
| 80 | msg_perr("ERROR: SPI controller can not send %u bytes, it is limited to %u bytes.\n", |
| 81 | writecnt, SPI100_FIFO_SIZE + 1); |
| 82 | return SPI_INVALID_LENGTH; |
| 83 | } |
| 84 | |
| 85 | const unsigned int maxreadcnt = SPI100_FIFO_SIZE - (writecnt - 1); |
| 86 | if (readcnt > maxreadcnt) { |
| 87 | msg_perr("ERROR: SPI controller can not receive %u bytes for this command,\n" |
| 88 | "it is limited to %u bytes write+read count.\n", |
| 89 | readcnt, SPI100_FIFO_SIZE + 1); |
| 90 | return SPI_INVALID_LENGTH; |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int spi100_send_command(const struct flashctx *const flash, |
| 96 | const unsigned int writecnt, const unsigned int readcnt, |
| 97 | const unsigned char *const writearr, unsigned char *const readarr) |
| 98 | { |
| 99 | const struct spi100 *const spi100 = flash->mst->spi.data; |
| 100 | |
| 101 | int ret = spi100_check_readwritecnt(writecnt, readcnt); |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | |
| 105 | spi100_write8(spi100, 0x45, writearr[0]); /* First "command" byte is sent separately. */ |
| 106 | spi100_write8(spi100, 0x48, writecnt - 1); |
| 107 | spi100_write8(spi100, 0x4b, readcnt); |
| 108 | if (writecnt > 1) |
| 109 | spi100_writen(spi100, 0x80, &writearr[1], writecnt - 1); |
| 110 | |
| 111 | /* Trigger command */ |
| 112 | spi100_write8(spi100, 0x47, BIT(7)); |
| 113 | |
| 114 | /* Wait for completion */ |
| 115 | int timeout_us = 10*1000*1000; |
| 116 | uint32_t spistatus; |
| 117 | while (((spistatus = spi100_read32(spi100, 0x4c)) & BIT(31)) && timeout_us--) |
| 118 | programmer_delay(1); |
| 119 | if (spistatus & BIT(31)) { |
| 120 | msg_perr("ERROR: SPI transfer timed out (0x%08x)!\n", spistatus); |
| 121 | return SPI_PROGRAMMER_ERROR; |
| 122 | } |
| 123 | msg_pspew("%s: spistatus: 0x%08x\n", __func__, spistatus); |
| 124 | |
| 125 | if (readcnt) |
| 126 | spi100_readn(spi100, 0x80 + writecnt - 1, readarr, readcnt); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Nico Huber | e3c305d | 2023-01-29 21:45:56 +0000 | [diff] [blame] | 131 | static int spi100_read(struct flashctx *const flash, uint8_t *buf, unsigned int start, unsigned int len) |
| 132 | { |
| 133 | const struct spi100 *const spi100 = flash->mst->spi.data; |
Nico Huber | f5fcd74 | 2023-03-11 17:11:12 +0000 | [diff] [blame^] | 134 | const chipsize_t chip_size = flashrom_flash_getsize(flash); |
| 135 | |
| 136 | /* Don't consider memory mapping at all |
| 137 | if 4BA chips are not mapped as expected. */ |
| 138 | if (chip_size > 16*MiB && spi100->no_4ba_mmap) |
| 139 | return default_spi_read(flash, buf, start, len); |
Nico Huber | e3c305d | 2023-01-29 21:45:56 +0000 | [diff] [blame] | 140 | |
Nico Huber | 2d614d6 | 2023-03-11 01:06:15 +0100 | [diff] [blame] | 141 | /* Where in the flash does the memory mapped part start? |
| 142 | Can be negative if the mapping is bigger than the chip. */ |
Nico Huber | f5fcd74 | 2023-03-11 17:11:12 +0000 | [diff] [blame^] | 143 | const long long mapped_start = chip_size - spi100->mapped_len; |
Nico Huber | 2d614d6 | 2023-03-11 01:06:15 +0100 | [diff] [blame] | 144 | |
| 145 | /* Use SPI100 engine for data outside the memory-mapped range. */ |
| 146 | if ((long long)start < mapped_start) { |
| 147 | const chipsize_t unmapped_len = MIN(len, mapped_start - start); |
Nico Huber | e3c305d | 2023-01-29 21:45:56 +0000 | [diff] [blame] | 148 | const int ret = default_spi_read(flash, buf, start, unmapped_len); |
| 149 | if (ret) |
| 150 | return ret; |
| 151 | start += unmapped_len; |
| 152 | buf += unmapped_len; |
| 153 | len -= unmapped_len; |
| 154 | } |
| 155 | |
Nico Huber | 2d614d6 | 2023-03-11 01:06:15 +0100 | [diff] [blame] | 156 | /* Translate `start` to memory-mapped offset. */ |
| 157 | start -= mapped_start; |
| 158 | |
Nico Huber | e3c305d | 2023-01-29 21:45:56 +0000 | [diff] [blame] | 159 | mmio_readn_aligned(spi100->memory + start, buf, len, 8); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 164 | static int spi100_shutdown(void *data) |
| 165 | { |
| 166 | struct spi100 *const spi100 = data; |
| 167 | |
| 168 | const uint16_t speed_cfg = spi100_read16(spi100, 0x22); |
| 169 | spi100_write16(spi100, 0x22, (speed_cfg & ~0xf0) | spi100->altspeed << 4); |
| 170 | |
| 171 | free(spi100); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static struct spi_master spi100_master = { |
Nico Huber | f5fcd74 | 2023-03-11 17:11:12 +0000 | [diff] [blame^] | 176 | .features = SPI_MASTER_4BA | SPI_MASTER_NO_4BA_MODES, |
Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 177 | .max_data_read = SPI100_FIFO_SIZE - 4, /* Account for up to 4 address bytes. */ |
| 178 | .max_data_write = SPI100_FIFO_SIZE - 4, |
| 179 | .command = spi100_send_command, |
| 180 | .multicommand = default_spi_send_multicommand, |
Nico Huber | e3c305d | 2023-01-29 21:45:56 +0000 | [diff] [blame] | 181 | .read = spi100_read, |
Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 182 | .write_256 = default_spi_write_256, |
| 183 | .probe_opcode = default_spi_probe_opcode, |
| 184 | .shutdown = spi100_shutdown, |
| 185 | }; |
| 186 | |
| 187 | const char *const spimodes[] = { |
| 188 | "Normal read (up to 33MHz)", |
| 189 | "Reserved", |
| 190 | "Dual IO (1-1-2)", |
| 191 | "Quad IO (1-1-4)", |
| 192 | "Dual IO (1-2-2)", |
| 193 | "Quad IO (1-1-4)", |
| 194 | "Normal read (up to 66MHz)", |
| 195 | "Fast Read", |
| 196 | }; |
| 197 | |
| 198 | const struct { |
| 199 | unsigned int khz; |
| 200 | const char *speed; |
| 201 | } spispeeds[] = { |
| 202 | { 66666, "66.66 MHz" }, |
| 203 | { 33333, "33.33 MHz" }, |
| 204 | { 22222, "22.22 MHz" }, |
| 205 | { 16666, "16.66 MHz" }, |
| 206 | { 100000, "100 MHz" }, |
| 207 | { 800, "800 kHz" }, |
| 208 | { 0, "Reserved" }, |
| 209 | { 0, "Reserved" }, |
| 210 | }; |
| 211 | |
| 212 | static void spi100_print(const struct spi100 *const spi100) |
| 213 | { |
| 214 | const uint32_t spi_cntrl0 = spi100_read32(spi100, 0x00); |
| 215 | msg_pdbg("(0x%08" PRIx32 ") ", spi_cntrl0); |
| 216 | msg_pdbg("SpiArbEnable=%u, ", spi_cntrl0 >> 19 & 1); |
| 217 | msg_pdbg("IllegalAccess=%u, ", spi_cntrl0 >> 21 & 1); |
| 218 | msg_pdbg("SpiAccessMacRomEn=%u, ", spi_cntrl0 >> 22 & 1); |
| 219 | msg_pdbg("SpiHostAccessRomEn=%u,\n", spi_cntrl0 >> 23 & 1); |
| 220 | msg_pdbg(" "); |
| 221 | msg_pdbg("ArbWaitCount=%u, ", spi_cntrl0 >> 24 & 7); |
| 222 | msg_pdbg("SpiBridgeDisable=%u, ", spi_cntrl0 >> 27 & 1); |
| 223 | msg_pdbg("SpiClkGate=%u,\n", spi_cntrl0 >> 28 & 1); |
| 224 | msg_pdbg(" "); |
| 225 | msg_pdbg("SpiReadMode=%s, ", spimodes[(spi_cntrl0 >> 28 & 6) | (spi_cntrl0 >> 18 & 1)]); |
| 226 | msg_pdbg("SpiBusy=%u\n", spi_cntrl0 >> 31 & 1); |
| 227 | |
| 228 | const uint8_t alt_spi_cs = spi100_read8(spi100, 0x1d); |
| 229 | msg_pdbg("Using SPI_CS%u\n", alt_spi_cs & 0x3); |
| 230 | |
| 231 | const uint16_t speed_cfg = spi100_read16(spi100, 0x22); |
| 232 | msg_pdbg("NormSpeed: %s\n", spispeeds[speed_cfg >> 12 & 0xf].speed); |
| 233 | msg_pdbg("FastSpeed: %s\n", spispeeds[speed_cfg >> 8 & 0xf].speed); |
| 234 | msg_pdbg("AltSpeed: %s\n", spispeeds[speed_cfg >> 4 & 0xf].speed); |
| 235 | msg_pdbg("TpmSpeed: %s\n", spispeeds[speed_cfg >> 0 & 0xf].speed); |
| 236 | } |
| 237 | |
Nico Huber | f5fcd74 | 2023-03-11 17:11:12 +0000 | [diff] [blame^] | 238 | static void spi100_check_4ba(struct spi100 *const spi100) |
| 239 | { |
| 240 | const uint16_t rom2_addr_override = spi100_read16(spi100, 0x30); |
| 241 | const uint32_t addr32_ctrl0 = spi100_read32(spi100, 0x50); |
| 242 | const uint32_t addr32_ctrl3 = spi100_read32(spi100, 0x5c); |
| 243 | |
| 244 | spi100->no_4ba_mmap = false; |
| 245 | |
| 246 | /* Most bits are undocumented ("reserved"), so we play safe. */ |
| 247 | if (rom2_addr_override != 0x14c0) { |
| 248 | msg_pdbg("ROM2 address override *not* in default configuration.\n"); |
| 249 | spi100->no_4ba_mmap = true; |
| 250 | } |
| 251 | |
| 252 | /* Check if the controller would use 4-byte addresses by itself. */ |
| 253 | if (addr32_ctrl0 & 1) { |
| 254 | msg_pdbg("Memory-mapped access uses 32-bit addresses.\n"); |
| 255 | } else { |
| 256 | msg_pdbg("Memory-mapped access uses 24-bit addresses.\n"); |
| 257 | spi100->no_4ba_mmap = true; |
| 258 | } |
| 259 | |
| 260 | /* Another override (xor'ed) for the most-significant address bits. */ |
| 261 | if (addr32_ctrl3 & 0xff) { |
| 262 | msg_pdbg("SPI ROM page bits set: 0x%02x\n", addr32_ctrl3 & 0xff); |
| 263 | spi100->no_4ba_mmap = true; |
| 264 | } |
| 265 | } |
| 266 | |
Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 267 | static void spi100_set_altspeed(struct spi100 *const spi100) |
| 268 | { |
| 269 | const uint16_t speed_cfg = spi100_read16(spi100, 0x22); |
| 270 | const unsigned int normspeed = speed_cfg >> 12 & 0xf; |
| 271 | spi100->altspeed = speed_cfg >> 4 & 0xf; |
| 272 | |
| 273 | /* Set SPI speed to 33MHz but not higher than `normal read` speed */ |
| 274 | unsigned int altspeed; |
| 275 | if (spispeeds[normspeed].khz != 0 && spispeeds[normspeed].khz < 33333) |
| 276 | altspeed = normspeed; |
| 277 | else |
| 278 | altspeed = 1; |
| 279 | |
| 280 | if (altspeed != spi100->altspeed) { |
| 281 | msg_pinfo("Setting SPI speed to %s.\n", spispeeds[altspeed].speed); |
| 282 | spi100_write16(spi100, 0x22, (speed_cfg & ~0xf0) | altspeed << 4); |
| 283 | } |
| 284 | } |
| 285 | |
Nico Huber | e3c305d | 2023-01-29 21:45:56 +0000 | [diff] [blame] | 286 | int amd_spi100_probe(void *const spibar, void *const memory_mapping, const size_t mapped_len) |
Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 287 | { |
| 288 | struct spi100 *const spi100 = malloc(sizeof(*spi100)); |
| 289 | if (!spi100) { |
| 290 | msg_perr("Out of memory!\n"); |
| 291 | return ERROR_FATAL; |
| 292 | } |
| 293 | spi100->spibar = spibar; |
Nico Huber | e3c305d | 2023-01-29 21:45:56 +0000 | [diff] [blame] | 294 | spi100->memory = memory_mapping; |
| 295 | spi100->mapped_len = mapped_len; |
Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 296 | |
| 297 | spi100_print(spi100); |
| 298 | |
| 299 | spi100_set_altspeed(spi100); |
| 300 | |
Nico Huber | f5fcd74 | 2023-03-11 17:11:12 +0000 | [diff] [blame^] | 301 | spi100_check_4ba(spi100); |
| 302 | |
Nico Huber | 735b186 | 2023-01-29 18:28:45 +0000 | [diff] [blame] | 303 | return register_spi_master(&spi100_master, spi100); |
| 304 | } |