Angel Pons | 0612b27 | 2020-04-05 15:46:56 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 3 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 4 | #include <device/mmio.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 5 | #include <assert.h> |
| 6 | #include <device/pci_def.h> |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 7 | #include <device/pci_ops.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 8 | #include <commonlib/helpers.h> |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 9 | #include <cpu/x86/mtrr.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 10 | #include <fast_spi_def.h> |
| 11 | #include <intelblocks/fast_spi.h> |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 12 | #include <lib.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 13 | #include <soc/pci_devs.h> |
| 14 | #include <spi_flash.h> |
| 15 | #include <spi-generic.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * Get the FAST_SPIBAR. |
| 19 | */ |
| 20 | void *fast_spi_get_bar(void) |
| 21 | { |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 22 | #if defined(__SIMPLE_DEVICE__) |
| 23 | pci_devfn_t dev = PCH_DEV_SPI; |
| 24 | #else |
| 25 | struct device *dev = PCH_DEV_SPI; |
| 26 | #endif |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 27 | uintptr_t bar; |
| 28 | |
| 29 | bar = pci_read_config32(dev, PCI_BASE_ADDRESS_0); |
| 30 | assert(bar != 0); |
| 31 | /* |
| 32 | * Bits 31-12 are the base address as per EDS for SPI, |
| 33 | * Don't care about 0-11 bit |
| 34 | */ |
| 35 | return (void *)(bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK); |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Disable the BIOS write protect and Enable Prefetching and Caching. |
| 40 | */ |
| 41 | void fast_spi_init(void) |
| 42 | { |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 43 | #if defined(__SIMPLE_DEVICE__) |
| 44 | pci_devfn_t dev = PCH_DEV_SPI; |
| 45 | #else |
| 46 | struct device *dev = PCH_DEV_SPI; |
| 47 | #endif |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 48 | uint8_t bios_cntl; |
| 49 | |
| 50 | bios_cntl = pci_read_config8(dev, SPIBAR_BIOS_CONTROL); |
| 51 | |
| 52 | /* Disable the BIOS write protect so write commands are allowed. */ |
| 53 | bios_cntl &= ~SPIBAR_BIOS_CONTROL_EISS; |
| 54 | bios_cntl |= SPIBAR_BIOS_CONTROL_WPD; |
| 55 | /* Enable Prefetching and caching. */ |
| 56 | bios_cntl |= SPIBAR_BIOS_CONTROL_PREFETCH_ENABLE; |
| 57 | bios_cntl &= ~SPIBAR_BIOS_CONTROL_CACHE_DISABLE; |
| 58 | |
| 59 | pci_write_config8(dev, SPIBAR_BIOS_CONTROL, bios_cntl); |
| 60 | } |
| 61 | |
| 62 | /* |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 63 | * Set FAST_SPIBAR BIOS Control register based on input bit field. |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 64 | */ |
| 65 | static void fast_spi_set_bios_control_reg(uint8_t bios_cntl_bit) |
| 66 | { |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 67 | #if defined(__SIMPLE_DEVICE__) |
| 68 | pci_devfn_t dev = PCH_DEV_SPI; |
| 69 | #else |
| 70 | struct device *dev = PCH_DEV_SPI; |
| 71 | #endif |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 72 | uint8_t bc_cntl; |
| 73 | |
| 74 | assert((bios_cntl_bit & (bios_cntl_bit - 1)) == 0); |
| 75 | bc_cntl = pci_read_config8(dev, SPIBAR_BIOS_CONTROL); |
| 76 | bc_cntl |= bios_cntl_bit; |
| 77 | pci_write_config8(dev, SPIBAR_BIOS_CONTROL, bc_cntl); |
| 78 | } |
| 79 | |
| 80 | /* |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 81 | * Ensure an additional read back after performing lock down |
| 82 | */ |
| 83 | static void fast_spi_read_post_write(uint8_t reg) |
| 84 | { |
| 85 | pci_read_config8(PCH_DEV_SPI, reg); |
| 86 | } |
| 87 | |
| 88 | /* |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 89 | * Set FAST_SPIBAR BIOS Control BILD bit. |
| 90 | */ |
| 91 | void fast_spi_set_bios_interface_lock_down(void) |
| 92 | { |
| 93 | fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_BILD); |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 94 | |
| 95 | fast_spi_read_post_write(SPIBAR_BIOS_CONTROL); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Set FAST_SPIBAR BIOS Control LE bit. |
| 100 | */ |
| 101 | void fast_spi_set_lock_enable(void) |
| 102 | { |
| 103 | fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_LOCK_ENABLE); |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 104 | |
| 105 | |
| 106 | fast_spi_read_post_write(SPIBAR_BIOS_CONTROL); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Set FAST_SPIBAR BIOS Control EISS bit. |
| 111 | */ |
| 112 | void fast_spi_set_eiss(void) |
| 113 | { |
| 114 | fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_EISS); |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 115 | |
| 116 | fast_spi_read_post_write(SPIBAR_BIOS_CONTROL); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Set FAST_SPI opcode menu. |
| 121 | */ |
| 122 | void fast_spi_set_opcode_menu(void) |
| 123 | { |
| 124 | void *spibar = fast_spi_get_bar(); |
| 125 | |
| 126 | write16(spibar + SPIBAR_PREOP, SPI_OPPREFIX); |
| 127 | write16(spibar + SPIBAR_OPTYPE, SPI_OPTYPE); |
| 128 | write32(spibar + SPIBAR_OPMENU_LOWER, SPI_OPMENU_LOWER); |
| 129 | write32(spibar + SPIBAR_OPMENU_UPPER, SPI_OPMENU_UPPER); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Lock FAST_SPIBAR. |
Barnali Sarkar | 8e51319 | 2017-07-19 16:09:56 +0530 | [diff] [blame] | 134 | * Use 16bit write to avoid touching two upper bytes what may cause the write |
| 135 | * cycle to fail in case a prior transaction has not completed. |
| 136 | * While WRSDIS is lockable with FLOCKDN, writing both in the same |
| 137 | * cycle is guaranteed to work by design. |
| 138 | * |
| 139 | * Avoid read->modify->write not to clear RW1C bits unintentionally. |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 140 | */ |
| 141 | void fast_spi_lock_bar(void) |
| 142 | { |
| 143 | void *spibar = fast_spi_get_bar(); |
Duncan Laurie | dc1e6bc | 2017-08-15 13:32:26 -0700 | [diff] [blame] | 144 | uint16_t hsfs = SPIBAR_HSFSTS_FLOCKDN; |
| 145 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 146 | if (CONFIG(FAST_SPI_DISABLE_WRITE_STATUS)) |
Duncan Laurie | dc1e6bc | 2017-08-15 13:32:26 -0700 | [diff] [blame] | 147 | hsfs |= SPIBAR_HSFSTS_WRSDIS; |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 148 | |
Barnali Sarkar | 8e51319 | 2017-07-19 16:09:56 +0530 | [diff] [blame] | 149 | write16(spibar + SPIBAR_HSFSTS_CTL, hsfs); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
Barnali Sarkar | 4f6e341 | 2017-08-17 11:49:27 +0530 | [diff] [blame] | 153 | * Set FAST_SPIBAR + DLOCK (0x0C) register bits to discrete lock the |
| 154 | * FAST_SPI Protected Range (PR) registers. |
| 155 | */ |
| 156 | void fast_spi_pr_dlock(void) |
| 157 | { |
| 158 | void *spibar = fast_spi_get_bar(); |
| 159 | uint32_t dlock; |
| 160 | |
| 161 | dlock = read32(spibar + SPIBAR_DLOCK); |
| 162 | dlock |= (SPIBAR_DLOCK_PR0LOCKDN | SPIBAR_DLOCK_PR1LOCKDN |
| 163 | | SPIBAR_DLOCK_PR2LOCKDN | SPIBAR_DLOCK_PR3LOCKDN |
| 164 | | SPIBAR_DLOCK_PR4LOCKDN); |
| 165 | |
| 166 | write32(spibar + SPIBAR_DLOCK, dlock); |
| 167 | } |
| 168 | |
| 169 | /* |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 170 | * Set FAST_SPIBAR Soft Reset Data Register value. |
| 171 | */ |
| 172 | void fast_spi_set_strap_msg_data(uint32_t soft_reset_data) |
| 173 | { |
| 174 | void *spibar = fast_spi_get_bar(); |
| 175 | uint32_t ssl, ssms; |
| 176 | |
| 177 | /* Set Strap Lock Disable */ |
| 178 | ssl = read32(spibar + SPIBAR_RESET_LOCK); |
| 179 | ssl &= ~SPIBAR_RESET_LOCK_ENABLE; |
| 180 | write32(spibar + SPIBAR_RESET_LOCK, ssl); |
| 181 | |
| 182 | /* Write Soft Reset Data register at SPIBAR0 offset 0xF8[0:15] */ |
| 183 | write32(spibar + SPIBAR_RESET_DATA, soft_reset_data); |
| 184 | |
| 185 | /* Set Strap Mux Select set to '1' */ |
| 186 | ssms = read32(spibar + SPIBAR_RESET_CTRL); |
| 187 | ssms |= SPIBAR_RESET_CTRL_SSMC; |
| 188 | write32(spibar + SPIBAR_RESET_CTRL, ssms); |
| 189 | |
| 190 | /* Set Strap Lock Enable */ |
| 191 | ssl = read32(spibar + SPIBAR_RESET_LOCK); |
| 192 | ssl |= SPIBAR_RESET_LOCK_ENABLE; |
| 193 | write32(spibar + SPIBAR_RESET_LOCK, ssl); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Returns bios_start and fills in size of the BIOS region. |
| 198 | */ |
| 199 | size_t fast_spi_get_bios_region(size_t *bios_size) |
| 200 | { |
| 201 | size_t bios_start, bios_end; |
| 202 | /* |
| 203 | * BIOS_BFPREG provides info about BIOS Flash Primary Region |
| 204 | * Base and Limit. |
| 205 | * Base and Limit fields are in units of 4KiB. |
| 206 | */ |
| 207 | uint32_t val = read32(fast_spi_get_bar() + SPIBAR_BFPREG); |
| 208 | |
| 209 | bios_start = (val & SPIBAR_BFPREG_PRB_MASK) * 4 * KiB; |
| 210 | bios_end = (((val & SPIBAR_BFPREG_PRL_MASK) >> |
| 211 | SPIBAR_BFPREG_PRL_SHIFT) + 1) * 4 * KiB; |
| 212 | *bios_size = bios_end - bios_start; |
| 213 | return bios_start; |
| 214 | } |
| 215 | |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 216 | void fast_spi_cache_bios_region(void) |
| 217 | { |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 218 | size_t bios_size; |
| 219 | uint32_t alignment; |
Aaron Durbin | 0b34fc6 | 2017-06-08 10:52:58 -0500 | [diff] [blame] | 220 | const int type = MTRR_TYPE_WRPROT; |
| 221 | uintptr_t base; |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 222 | |
| 223 | /* Only the IFD BIOS region is memory mapped (at top of 4G) */ |
| 224 | fast_spi_get_bios_region(&bios_size); |
| 225 | |
Lijian Zhao | ad1e49a | 2018-11-29 16:24:24 -0800 | [diff] [blame] | 226 | /* LOCAL APIC default address is 0xFEE0000, bios_size over 16MB will |
| 227 | * cause memory type conflict when setting memory type to write |
Elyes HAOUAS | 6dc9d03 | 2020-02-16 16:22:52 +0100 | [diff] [blame] | 228 | * protection, so limit the cached BIOS region to be no more than 16MB. |
Lijian Zhao | ad1e49a | 2018-11-29 16:24:24 -0800 | [diff] [blame] | 229 | * */ |
| 230 | bios_size = MIN(bios_size, 16 * MiB); |
John Zhao | 1ceac4e | 2019-07-09 14:27:28 -0700 | [diff] [blame] | 231 | if (bios_size <= 0) |
John Zhao | 2bb432e | 2019-05-21 19:32:51 -0700 | [diff] [blame] | 232 | return; |
Lijian Zhao | ad1e49a | 2018-11-29 16:24:24 -0800 | [diff] [blame] | 233 | |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 234 | /* Round to power of two */ |
Paul Menzel | 64e8340 | 2017-10-27 11:05:14 +0200 | [diff] [blame] | 235 | alignment = 1UL << (log2_ceil(bios_size)); |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 236 | bios_size = ALIGN_UP(bios_size, alignment); |
Aaron Durbin | 0b34fc6 | 2017-06-08 10:52:58 -0500 | [diff] [blame] | 237 | base = 4ULL*GiB - bios_size; |
| 238 | |
Subrata Banik | 42c44c2 | 2019-05-15 20:27:04 +0530 | [diff] [blame] | 239 | if (ENV_PAYLOAD_LOADER) { |
Aaron Durbin | 0b34fc6 | 2017-06-08 10:52:58 -0500 | [diff] [blame] | 240 | mtrr_use_temp_range(base, bios_size, type); |
| 241 | } else { |
| 242 | int mtrr = get_free_var_mtrr(); |
| 243 | |
| 244 | if (mtrr == -1) |
| 245 | return; |
| 246 | |
| 247 | set_var_mtrr(mtrr, base, bios_size, type); |
| 248 | } |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 251 | /* |
| 252 | * Program temporary BAR for SPI in case any of the stages before ramstage need |
| 253 | * to access FAST_SPI MMIO regs. Ramstage will assign a new BAR during PCI |
| 254 | * enumeration. |
| 255 | */ |
| 256 | void fast_spi_early_init(uintptr_t spi_base_address) |
| 257 | { |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 258 | #if defined(__SIMPLE_DEVICE__) |
| 259 | pci_devfn_t dev = PCH_DEV_SPI; |
| 260 | #else |
| 261 | struct device *dev = PCH_DEV_SPI; |
| 262 | #endif |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 263 | uint8_t pcireg; |
| 264 | |
| 265 | /* Assign Resources to SPI Controller */ |
| 266 | /* Clear BIT 1-2 SPI Command Register */ |
| 267 | pcireg = pci_read_config8(dev, PCI_COMMAND); |
| 268 | pcireg &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY); |
| 269 | pci_write_config8(dev, PCI_COMMAND, pcireg); |
| 270 | |
| 271 | /* Program Temporary BAR for SPI */ |
| 272 | pci_write_config32(dev, PCI_BASE_ADDRESS_0, |
| 273 | spi_base_address | PCI_BASE_ADDRESS_SPACE_MEMORY); |
| 274 | |
| 275 | /* Enable Bus Master and MMIO Space */ |
| 276 | pcireg = pci_read_config8(dev, PCI_COMMAND); |
| 277 | pcireg |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
| 278 | pci_write_config8(dev, PCI_COMMAND, pcireg); |
| 279 | |
| 280 | /* Initialize SPI to allow BIOS to write/erase on flash. */ |
| 281 | fast_spi_init(); |
| 282 | } |
Ravi Sarawadi | b051a9f5 | 2017-09-07 12:15:45 -0700 | [diff] [blame] | 283 | |
| 284 | /* Read SPI Write Protect disable status. */ |
| 285 | bool fast_spi_wpd_status(void) |
| 286 | { |
| 287 | return pci_read_config16(PCH_DEV_SPI, SPIBAR_BIOS_CONTROL) & |
| 288 | SPIBAR_BIOS_CONTROL_WPD; |
| 289 | } |
| 290 | |
| 291 | /* Enable SPI Write Protect. */ |
| 292 | void fast_spi_enable_wp(void) |
| 293 | { |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 294 | #if defined(__SIMPLE_DEVICE__) |
| 295 | pci_devfn_t dev = PCH_DEV_SPI; |
| 296 | #else |
| 297 | struct device *dev = PCH_DEV_SPI; |
| 298 | #endif |
Ravi Sarawadi | b051a9f5 | 2017-09-07 12:15:45 -0700 | [diff] [blame] | 299 | uint8_t bios_cntl; |
| 300 | |
| 301 | bios_cntl = pci_read_config8(dev, SPIBAR_BIOS_CONTROL); |
| 302 | bios_cntl &= ~SPIBAR_BIOS_CONTROL_WPD; |
| 303 | pci_write_config8(dev, SPIBAR_BIOS_CONTROL, bios_cntl); |
| 304 | } |