Angel Pons | 0612b27 | 2020-04-05 15:46:56 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 2 | |
Angel Pons | 5207243 | 2021-02-15 13:09:36 +0100 | [diff] [blame^] | 3 | #define __SIMPLE_DEVICE__ |
| 4 | |
Srinidhi N Kaushik | 4eb489f | 2020-11-25 02:21:57 -0800 | [diff] [blame] | 5 | #include <arch/romstage.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 6 | #include <device/mmio.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | #include <device/pci_def.h> |
Patrick Rudolph | e56189c | 2018-04-18 10:11:59 +0200 | [diff] [blame] | 9 | #include <device/pci_ops.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 10 | #include <commonlib/helpers.h> |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 11 | #include <cpu/x86/mtrr.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 12 | #include <fast_spi_def.h> |
Srinidhi N Kaushik | 6094908 | 2020-11-25 01:58:34 -0800 | [diff] [blame] | 13 | #include <intelblocks/dmi.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 14 | #include <intelblocks/fast_spi.h> |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 15 | #include <lib.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 16 | #include <soc/pci_devs.h> |
| 17 | #include <spi_flash.h> |
| 18 | #include <spi-generic.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Get the FAST_SPIBAR. |
| 22 | */ |
| 23 | void *fast_spi_get_bar(void) |
| 24 | { |
Angel Pons | 5207243 | 2021-02-15 13:09:36 +0100 | [diff] [blame^] | 25 | const pci_devfn_t dev = PCH_DEV_SPI; |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 26 | uintptr_t bar; |
| 27 | |
| 28 | bar = pci_read_config32(dev, PCI_BASE_ADDRESS_0); |
| 29 | assert(bar != 0); |
| 30 | /* |
| 31 | * Bits 31-12 are the base address as per EDS for SPI, |
| 32 | * Don't care about 0-11 bit |
| 33 | */ |
| 34 | return (void *)(bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK); |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Disable the BIOS write protect and Enable Prefetching and Caching. |
| 39 | */ |
| 40 | void fast_spi_init(void) |
| 41 | { |
Angel Pons | 5207243 | 2021-02-15 13:09:36 +0100 | [diff] [blame^] | 42 | const pci_devfn_t dev = PCH_DEV_SPI; |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 43 | uint8_t bios_cntl; |
| 44 | |
| 45 | bios_cntl = pci_read_config8(dev, SPIBAR_BIOS_CONTROL); |
| 46 | |
| 47 | /* Disable the BIOS write protect so write commands are allowed. */ |
| 48 | bios_cntl &= ~SPIBAR_BIOS_CONTROL_EISS; |
| 49 | bios_cntl |= SPIBAR_BIOS_CONTROL_WPD; |
| 50 | /* Enable Prefetching and caching. */ |
| 51 | bios_cntl |= SPIBAR_BIOS_CONTROL_PREFETCH_ENABLE; |
| 52 | bios_cntl &= ~SPIBAR_BIOS_CONTROL_CACHE_DISABLE; |
| 53 | |
| 54 | pci_write_config8(dev, SPIBAR_BIOS_CONTROL, bios_cntl); |
| 55 | } |
| 56 | |
| 57 | /* |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 58 | * Set FAST_SPIBAR BIOS Control register based on input bit field. |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 59 | */ |
Srinidhi N Kaushik | 28e1d0e | 2020-11-25 21:57:37 -0800 | [diff] [blame] | 60 | static void fast_spi_set_bios_control_reg(uint32_t bios_cntl_bit) |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 61 | { |
Angel Pons | 5207243 | 2021-02-15 13:09:36 +0100 | [diff] [blame^] | 62 | const pci_devfn_t dev = PCH_DEV_SPI; |
Srinidhi N Kaushik | 28e1d0e | 2020-11-25 21:57:37 -0800 | [diff] [blame] | 63 | uint32_t bc_cntl; |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 64 | |
| 65 | assert((bios_cntl_bit & (bios_cntl_bit - 1)) == 0); |
Srinidhi N Kaushik | 28e1d0e | 2020-11-25 21:57:37 -0800 | [diff] [blame] | 66 | bc_cntl = pci_read_config32(dev, SPIBAR_BIOS_CONTROL); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 67 | bc_cntl |= bios_cntl_bit; |
Srinidhi N Kaushik | 28e1d0e | 2020-11-25 21:57:37 -0800 | [diff] [blame] | 68 | pci_write_config32(dev, SPIBAR_BIOS_CONTROL, bc_cntl); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 72 | * Ensure an additional read back after performing lock down |
| 73 | */ |
| 74 | static void fast_spi_read_post_write(uint8_t reg) |
| 75 | { |
| 76 | pci_read_config8(PCH_DEV_SPI, reg); |
| 77 | } |
| 78 | |
| 79 | /* |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 80 | * Set FAST_SPIBAR BIOS Control BILD bit. |
| 81 | */ |
| 82 | void fast_spi_set_bios_interface_lock_down(void) |
| 83 | { |
| 84 | fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_BILD); |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 85 | |
| 86 | fast_spi_read_post_write(SPIBAR_BIOS_CONTROL); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Set FAST_SPIBAR BIOS Control LE bit. |
| 91 | */ |
| 92 | void fast_spi_set_lock_enable(void) |
| 93 | { |
| 94 | fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_LOCK_ENABLE); |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 95 | |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 96 | fast_spi_read_post_write(SPIBAR_BIOS_CONTROL); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
Srinidhi N Kaushik | 28e1d0e | 2020-11-25 21:57:37 -0800 | [diff] [blame] | 100 | * Set FAST_SPIBAR BIOS Control EXT BIOS LE bit. |
| 101 | */ |
| 102 | void fast_spi_set_ext_bios_lock_enable(void) |
| 103 | { |
| 104 | if (!CONFIG(FAST_SPI_SUPPORTS_EXT_BIOS_WINDOW)) |
| 105 | return; |
| 106 | |
| 107 | fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_EXT_BIOS_LOCK_ENABLE); |
| 108 | |
| 109 | fast_spi_read_post_write(SPIBAR_BIOS_CONTROL); |
| 110 | } |
| 111 | |
| 112 | /* |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 113 | * Set FAST_SPIBAR BIOS Control EISS bit. |
| 114 | */ |
| 115 | void fast_spi_set_eiss(void) |
| 116 | { |
| 117 | fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_EISS); |
Subrata Banik | 8e39009 | 2017-07-21 10:06:17 +0530 | [diff] [blame] | 118 | |
| 119 | fast_spi_read_post_write(SPIBAR_BIOS_CONTROL); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Set FAST_SPI opcode menu. |
| 124 | */ |
| 125 | void fast_spi_set_opcode_menu(void) |
| 126 | { |
| 127 | void *spibar = fast_spi_get_bar(); |
| 128 | |
| 129 | write16(spibar + SPIBAR_PREOP, SPI_OPPREFIX); |
| 130 | write16(spibar + SPIBAR_OPTYPE, SPI_OPTYPE); |
| 131 | write32(spibar + SPIBAR_OPMENU_LOWER, SPI_OPMENU_LOWER); |
| 132 | write32(spibar + SPIBAR_OPMENU_UPPER, SPI_OPMENU_UPPER); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Lock FAST_SPIBAR. |
Barnali Sarkar | 8e51319 | 2017-07-19 16:09:56 +0530 | [diff] [blame] | 137 | * Use 16bit write to avoid touching two upper bytes what may cause the write |
| 138 | * cycle to fail in case a prior transaction has not completed. |
| 139 | * While WRSDIS is lockable with FLOCKDN, writing both in the same |
| 140 | * cycle is guaranteed to work by design. |
| 141 | * |
| 142 | * Avoid read->modify->write not to clear RW1C bits unintentionally. |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 143 | */ |
| 144 | void fast_spi_lock_bar(void) |
| 145 | { |
| 146 | void *spibar = fast_spi_get_bar(); |
Duncan Laurie | dc1e6bc | 2017-08-15 13:32:26 -0700 | [diff] [blame] | 147 | uint16_t hsfs = SPIBAR_HSFSTS_FLOCKDN; |
| 148 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 149 | if (CONFIG(FAST_SPI_DISABLE_WRITE_STATUS)) |
Duncan Laurie | dc1e6bc | 2017-08-15 13:32:26 -0700 | [diff] [blame] | 150 | hsfs |= SPIBAR_HSFSTS_WRSDIS; |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 151 | |
Barnali Sarkar | 8e51319 | 2017-07-19 16:09:56 +0530 | [diff] [blame] | 152 | write16(spibar + SPIBAR_HSFSTS_CTL, hsfs); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
Barnali Sarkar | 4f6e341 | 2017-08-17 11:49:27 +0530 | [diff] [blame] | 156 | * Set FAST_SPIBAR + DLOCK (0x0C) register bits to discrete lock the |
| 157 | * FAST_SPI Protected Range (PR) registers. |
| 158 | */ |
| 159 | void fast_spi_pr_dlock(void) |
| 160 | { |
| 161 | void *spibar = fast_spi_get_bar(); |
| 162 | uint32_t dlock; |
| 163 | |
| 164 | dlock = read32(spibar + SPIBAR_DLOCK); |
| 165 | dlock |= (SPIBAR_DLOCK_PR0LOCKDN | SPIBAR_DLOCK_PR1LOCKDN |
| 166 | | SPIBAR_DLOCK_PR2LOCKDN | SPIBAR_DLOCK_PR3LOCKDN |
| 167 | | SPIBAR_DLOCK_PR4LOCKDN); |
| 168 | |
| 169 | write32(spibar + SPIBAR_DLOCK, dlock); |
| 170 | } |
| 171 | |
| 172 | /* |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 173 | * Set FAST_SPIBAR Soft Reset Data Register value. |
| 174 | */ |
| 175 | void fast_spi_set_strap_msg_data(uint32_t soft_reset_data) |
| 176 | { |
| 177 | void *spibar = fast_spi_get_bar(); |
| 178 | uint32_t ssl, ssms; |
| 179 | |
| 180 | /* Set Strap Lock Disable */ |
| 181 | ssl = read32(spibar + SPIBAR_RESET_LOCK); |
| 182 | ssl &= ~SPIBAR_RESET_LOCK_ENABLE; |
| 183 | write32(spibar + SPIBAR_RESET_LOCK, ssl); |
| 184 | |
| 185 | /* Write Soft Reset Data register at SPIBAR0 offset 0xF8[0:15] */ |
| 186 | write32(spibar + SPIBAR_RESET_DATA, soft_reset_data); |
| 187 | |
| 188 | /* Set Strap Mux Select set to '1' */ |
| 189 | ssms = read32(spibar + SPIBAR_RESET_CTRL); |
| 190 | ssms |= SPIBAR_RESET_CTRL_SSMC; |
| 191 | write32(spibar + SPIBAR_RESET_CTRL, ssms); |
| 192 | |
| 193 | /* Set Strap Lock Enable */ |
| 194 | ssl = read32(spibar + SPIBAR_RESET_LOCK); |
| 195 | ssl |= SPIBAR_RESET_LOCK_ENABLE; |
| 196 | write32(spibar + SPIBAR_RESET_LOCK, ssl); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Returns bios_start and fills in size of the BIOS region. |
| 201 | */ |
| 202 | size_t fast_spi_get_bios_region(size_t *bios_size) |
| 203 | { |
| 204 | size_t bios_start, bios_end; |
| 205 | /* |
| 206 | * BIOS_BFPREG provides info about BIOS Flash Primary Region |
| 207 | * Base and Limit. |
| 208 | * Base and Limit fields are in units of 4KiB. |
| 209 | */ |
| 210 | uint32_t val = read32(fast_spi_get_bar() + SPIBAR_BFPREG); |
| 211 | |
| 212 | bios_start = (val & SPIBAR_BFPREG_PRB_MASK) * 4 * KiB; |
| 213 | bios_end = (((val & SPIBAR_BFPREG_PRL_MASK) >> |
| 214 | SPIBAR_BFPREG_PRL_SHIFT) + 1) * 4 * KiB; |
| 215 | *bios_size = bios_end - bios_start; |
| 216 | return bios_start; |
| 217 | } |
| 218 | |
Srinidhi N Kaushik | 4eb489f | 2020-11-25 02:21:57 -0800 | [diff] [blame] | 219 | static bool fast_spi_ext_bios_cache_range(uintptr_t *base, size_t *size) |
| 220 | { |
| 221 | uint32_t alignment; |
| 222 | if (!CONFIG(FAST_SPI_SUPPORTS_EXT_BIOS_WINDOW)) |
| 223 | return false; |
| 224 | |
| 225 | fast_spi_get_ext_bios_window(base, size); |
| 226 | |
| 227 | /* Enable extended bios only if Size of Bios region is greater than 16MiB */ |
| 228 | if (*size == 0 || *base == 0) |
| 229 | return false; |
| 230 | |
| 231 | /* Round to power of two */ |
| 232 | alignment = 1UL << (log2_ceil(*size)); |
| 233 | *size = ALIGN_UP(*size, alignment); |
| 234 | *base = ALIGN_DOWN(*base, *size); |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | static void fast_spi_cache_ext_bios_window(void) |
| 240 | { |
| 241 | size_t ext_bios_size; |
| 242 | uintptr_t ext_bios_base; |
| 243 | const int type = MTRR_TYPE_WRPROT; |
| 244 | |
| 245 | if (!fast_spi_ext_bios_cache_range(&ext_bios_base, &ext_bios_size)) |
| 246 | return; |
| 247 | |
| 248 | int mtrr = get_free_var_mtrr(); |
| 249 | if (mtrr == -1) |
| 250 | return; |
| 251 | set_var_mtrr(mtrr, ext_bios_base, ext_bios_size, type); |
| 252 | } |
| 253 | |
| 254 | void fast_spi_cache_ext_bios_postcar(struct postcar_frame *pcf) |
| 255 | { |
| 256 | size_t ext_bios_size; |
| 257 | uintptr_t ext_bios_base; |
| 258 | const int type = MTRR_TYPE_WRPROT; |
| 259 | |
| 260 | if (!fast_spi_ext_bios_cache_range(&ext_bios_base, &ext_bios_size)) |
| 261 | return; |
| 262 | |
| 263 | postcar_frame_add_mtrr(pcf, ext_bios_base, ext_bios_size, type); |
| 264 | } |
| 265 | |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 266 | void fast_spi_cache_bios_region(void) |
| 267 | { |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 268 | size_t bios_size; |
| 269 | uint32_t alignment; |
Aaron Durbin | 0b34fc6 | 2017-06-08 10:52:58 -0500 | [diff] [blame] | 270 | const int type = MTRR_TYPE_WRPROT; |
| 271 | uintptr_t base; |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 272 | |
| 273 | /* Only the IFD BIOS region is memory mapped (at top of 4G) */ |
| 274 | fast_spi_get_bios_region(&bios_size); |
| 275 | |
Lijian Zhao | ad1e49a | 2018-11-29 16:24:24 -0800 | [diff] [blame] | 276 | /* LOCAL APIC default address is 0xFEE0000, bios_size over 16MB will |
| 277 | * cause memory type conflict when setting memory type to write |
Elyes HAOUAS | 6dc9d03 | 2020-02-16 16:22:52 +0100 | [diff] [blame] | 278 | * 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] | 279 | * */ |
| 280 | bios_size = MIN(bios_size, 16 * MiB); |
John Zhao | 1ceac4e | 2019-07-09 14:27:28 -0700 | [diff] [blame] | 281 | if (bios_size <= 0) |
John Zhao | 2bb432e | 2019-05-21 19:32:51 -0700 | [diff] [blame] | 282 | return; |
Lijian Zhao | ad1e49a | 2018-11-29 16:24:24 -0800 | [diff] [blame] | 283 | |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 284 | /* Round to power of two */ |
Paul Menzel | 64e8340 | 2017-10-27 11:05:14 +0200 | [diff] [blame] | 285 | alignment = 1UL << (log2_ceil(bios_size)); |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 286 | bios_size = ALIGN_UP(bios_size, alignment); |
Aaron Durbin | 0b34fc6 | 2017-06-08 10:52:58 -0500 | [diff] [blame] | 287 | base = 4ULL*GiB - bios_size; |
| 288 | |
Subrata Banik | 42c44c2 | 2019-05-15 20:27:04 +0530 | [diff] [blame] | 289 | if (ENV_PAYLOAD_LOADER) { |
Aaron Durbin | 0b34fc6 | 2017-06-08 10:52:58 -0500 | [diff] [blame] | 290 | mtrr_use_temp_range(base, bios_size, type); |
| 291 | } else { |
| 292 | int mtrr = get_free_var_mtrr(); |
| 293 | |
| 294 | if (mtrr == -1) |
| 295 | return; |
| 296 | |
| 297 | set_var_mtrr(mtrr, base, bios_size, type); |
| 298 | } |
Srinidhi N Kaushik | 4eb489f | 2020-11-25 02:21:57 -0800 | [diff] [blame] | 299 | |
| 300 | /* Check if caching is needed for extended bios region if supported */ |
| 301 | fast_spi_cache_ext_bios_window(); |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 302 | } |
| 303 | |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 304 | /* |
Srinidhi N Kaushik | 6094908 | 2020-11-25 01:58:34 -0800 | [diff] [blame] | 305 | * Enable extended BIOS support |
| 306 | * Checks BIOS region in the flashmap, if its more than 16Mib, enables extended BIOS |
| 307 | * region support. |
| 308 | */ |
| 309 | static void fast_spi_enable_ext_bios(void) |
| 310 | { |
Angel Pons | 5207243 | 2021-02-15 13:09:36 +0100 | [diff] [blame^] | 311 | const pci_devfn_t dev = PCH_DEV_SPI; |
Srinidhi N Kaushik | 6094908 | 2020-11-25 01:58:34 -0800 | [diff] [blame] | 312 | if (!CONFIG(FAST_SPI_SUPPORTS_EXT_BIOS_WINDOW)) |
| 313 | return; |
| 314 | |
| 315 | #if CONFIG(FAST_SPI_SUPPORTS_EXT_BIOS_WINDOW) |
| 316 | /* |
| 317 | * Ensure that the base for the extended window in host space is a multiple of 32 MiB |
| 318 | * and size is fixed at 32 MiB. Controller assumes that the extended window has a fixed |
| 319 | * size of 32 MiB even if the actual BIOS region is smaller. The mapping of the BIOS |
| 320 | * region happens at the top of the extended window in this case. |
| 321 | */ |
| 322 | _Static_assert(ALIGN_UP(CONFIG_EXT_BIOS_WIN_BASE, 32 * MiB) == CONFIG_EXT_BIOS_WIN_BASE, |
| 323 | "Extended BIOS window base must be a multiple of 32 * MiB!"); |
| 324 | _Static_assert(CONFIG_EXT_BIOS_WIN_SIZE == (32 * MiB), |
| 325 | "Only 32MiB windows are supported for extended BIOS!"); |
| 326 | #endif |
| 327 | |
| 328 | /* Confgiure DMI Source decode for Extended BIOS Region */ |
| 329 | if (dmi_enable_gpmr(CONFIG_EXT_BIOS_WIN_BASE, CONFIG_EXT_BIOS_WIN_SIZE, |
| 330 | soc_get_spi_dmi_destination_id()) == CB_ERR) |
| 331 | return; |
| 332 | |
| 333 | /* Program EXT_BIOS_BAR1 with obtained ext_bios_base */ |
| 334 | pci_write_config32(dev, SPI_CFG_BAR1, |
| 335 | CONFIG_EXT_BIOS_WIN_BASE | PCI_BASE_ADDRESS_SPACE_MEMORY); |
| 336 | |
| 337 | /* |
| 338 | * Since the top 16MiB of the BIOS region is always decoded by the standard window |
| 339 | * below the 4G boundary, we need to map the rest of the BIOS region that lies |
| 340 | * below the top 16MiB in the extended window. Thus, EXT_BIOS_LIMIT will be set to |
| 341 | * 16MiB. This determines the maximum address in the SPI flash space that is mapped |
| 342 | * to the top of the extended window in the host address space. EXT_BIOS_LIMIT is |
| 343 | * basically the offset from the end of the BIOS region that will be mapped to the top |
| 344 | * of the extended window. |
| 345 | * This enables the decoding as follows: |
| 346 | -Standard decode window: (bios_region_top - 16MiB) to bios_region_top |
| 347 | -Extended decode window: |
| 348 | (bios_region_top - 16MiB - MIN(extended_window_size, bios_size - 16MiB)) |
| 349 | to (bios_region_top - 16MiB). |
| 350 | */ |
| 351 | pci_or_config32(dev, SPIBAR_BIOS_CONTROL, SPIBAR_BIOS_CONTROL_EXT_BIOS_LIMIT(16 * MiB)); |
| 352 | |
| 353 | /* Program EXT_BIOS EN */ |
| 354 | pci_or_config32(dev, SPIBAR_BIOS_CONTROL, SPIBAR_BIOS_CONTROL_EXT_BIOS_ENABLE); |
| 355 | } |
| 356 | |
| 357 | /* |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 358 | * Program temporary BAR for SPI in case any of the stages before ramstage need |
| 359 | * to access FAST_SPI MMIO regs. Ramstage will assign a new BAR during PCI |
| 360 | * enumeration. |
| 361 | */ |
| 362 | void fast_spi_early_init(uintptr_t spi_base_address) |
| 363 | { |
Angel Pons | 5207243 | 2021-02-15 13:09:36 +0100 | [diff] [blame^] | 364 | const pci_devfn_t dev = PCH_DEV_SPI; |
Elyes HAOUAS | 2ec1c13 | 2020-04-29 09:57:05 +0200 | [diff] [blame] | 365 | uint16_t pcireg; |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 366 | |
| 367 | /* Assign Resources to SPI Controller */ |
| 368 | /* Clear BIT 1-2 SPI Command Register */ |
Elyes HAOUAS | 2ec1c13 | 2020-04-29 09:57:05 +0200 | [diff] [blame] | 369 | pcireg = pci_read_config16(dev, PCI_COMMAND); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 370 | pcireg &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY); |
Elyes HAOUAS | 2ec1c13 | 2020-04-29 09:57:05 +0200 | [diff] [blame] | 371 | pci_write_config16(dev, PCI_COMMAND, pcireg); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 372 | |
| 373 | /* Program Temporary BAR for SPI */ |
| 374 | pci_write_config32(dev, PCI_BASE_ADDRESS_0, |
| 375 | spi_base_address | PCI_BASE_ADDRESS_SPACE_MEMORY); |
| 376 | |
Srinidhi N Kaushik | 6094908 | 2020-11-25 01:58:34 -0800 | [diff] [blame] | 377 | /* |
| 378 | * Enable extended bios support. Since it configures memory BAR, this is done before |
| 379 | * enabling MMIO space. |
| 380 | */ |
| 381 | fast_spi_enable_ext_bios(); |
| 382 | |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 383 | /* Enable Bus Master and MMIO Space */ |
Elyes HAOUAS | 2ec1c13 | 2020-04-29 09:57:05 +0200 | [diff] [blame] | 384 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY); |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 385 | |
| 386 | /* Initialize SPI to allow BIOS to write/erase on flash. */ |
| 387 | fast_spi_init(); |
| 388 | } |
Ravi Sarawadi | b051a9f5 | 2017-09-07 12:15:45 -0700 | [diff] [blame] | 389 | |
| 390 | /* Read SPI Write Protect disable status. */ |
| 391 | bool fast_spi_wpd_status(void) |
| 392 | { |
| 393 | return pci_read_config16(PCH_DEV_SPI, SPIBAR_BIOS_CONTROL) & |
| 394 | SPIBAR_BIOS_CONTROL_WPD; |
| 395 | } |
| 396 | |
| 397 | /* Enable SPI Write Protect. */ |
| 398 | void fast_spi_enable_wp(void) |
| 399 | { |
Angel Pons | 5207243 | 2021-02-15 13:09:36 +0100 | [diff] [blame^] | 400 | const pci_devfn_t dev = PCH_DEV_SPI; |
Ravi Sarawadi | b051a9f5 | 2017-09-07 12:15:45 -0700 | [diff] [blame] | 401 | uint8_t bios_cntl; |
| 402 | |
| 403 | bios_cntl = pci_read_config8(dev, SPIBAR_BIOS_CONTROL); |
| 404 | bios_cntl &= ~SPIBAR_BIOS_CONTROL_WPD; |
| 405 | pci_write_config8(dev, SPIBAR_BIOS_CONTROL, bios_cntl); |
| 406 | } |