Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2017 Intel Corporation. |
| 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 |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 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 | |
| 16 | #include <arch/io.h> |
| 17 | #include <assert.h> |
| 18 | #include <device/pci_def.h> |
| 19 | #include <commonlib/helpers.h> |
| 20 | #include <console/console.h> |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 21 | #include <cpu/x86/mtrr.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 22 | #include <fast_spi_def.h> |
| 23 | #include <intelblocks/fast_spi.h> |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 24 | #include <lib.h> |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 25 | #include <soc/intel/common/spi_flash.h> |
| 26 | #include <soc/pci_devs.h> |
| 27 | #include <spi_flash.h> |
| 28 | #include <spi-generic.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | /* |
| 33 | * Get the FAST_SPIBAR. |
| 34 | */ |
| 35 | void *fast_spi_get_bar(void) |
| 36 | { |
| 37 | device_t dev = PCH_DEV_SPI; |
| 38 | uintptr_t bar; |
| 39 | |
| 40 | bar = pci_read_config32(dev, PCI_BASE_ADDRESS_0); |
| 41 | assert(bar != 0); |
| 42 | /* |
| 43 | * Bits 31-12 are the base address as per EDS for SPI, |
| 44 | * Don't care about 0-11 bit |
| 45 | */ |
| 46 | return (void *)(bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Disable the BIOS write protect and Enable Prefetching and Caching. |
| 51 | */ |
| 52 | void fast_spi_init(void) |
| 53 | { |
| 54 | device_t dev = PCH_DEV_SPI; |
| 55 | uint8_t bios_cntl; |
| 56 | |
| 57 | bios_cntl = pci_read_config8(dev, SPIBAR_BIOS_CONTROL); |
| 58 | |
| 59 | /* Disable the BIOS write protect so write commands are allowed. */ |
| 60 | bios_cntl &= ~SPIBAR_BIOS_CONTROL_EISS; |
| 61 | bios_cntl |= SPIBAR_BIOS_CONTROL_WPD; |
| 62 | /* Enable Prefetching and caching. */ |
| 63 | bios_cntl |= SPIBAR_BIOS_CONTROL_PREFETCH_ENABLE; |
| 64 | bios_cntl &= ~SPIBAR_BIOS_CONTROL_CACHE_DISABLE; |
| 65 | |
| 66 | pci_write_config8(dev, SPIBAR_BIOS_CONTROL, bios_cntl); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Set FAST_SPIBAR BIOS Control BILD bit. |
| 71 | */ |
| 72 | static void fast_spi_set_bios_control_reg(uint8_t bios_cntl_bit) |
| 73 | { |
| 74 | device_t dev = PCH_DEV_SPI; |
| 75 | uint8_t bc_cntl; |
| 76 | |
| 77 | assert((bios_cntl_bit & (bios_cntl_bit - 1)) == 0); |
| 78 | bc_cntl = pci_read_config8(dev, SPIBAR_BIOS_CONTROL); |
| 79 | bc_cntl |= bios_cntl_bit; |
| 80 | pci_write_config8(dev, SPIBAR_BIOS_CONTROL, bc_cntl); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Set FAST_SPIBAR BIOS Control BILD bit. |
| 85 | */ |
| 86 | void fast_spi_set_bios_interface_lock_down(void) |
| 87 | { |
| 88 | fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_BILD); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Set FAST_SPIBAR BIOS Control LE bit. |
| 93 | */ |
| 94 | void fast_spi_set_lock_enable(void) |
| 95 | { |
| 96 | fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_LOCK_ENABLE); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Set FAST_SPIBAR BIOS Control EISS bit. |
| 101 | */ |
| 102 | void fast_spi_set_eiss(void) |
| 103 | { |
| 104 | fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_EISS); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Set FAST_SPI opcode menu. |
| 109 | */ |
| 110 | void fast_spi_set_opcode_menu(void) |
| 111 | { |
| 112 | void *spibar = fast_spi_get_bar(); |
| 113 | |
| 114 | write16(spibar + SPIBAR_PREOP, SPI_OPPREFIX); |
| 115 | write16(spibar + SPIBAR_OPTYPE, SPI_OPTYPE); |
| 116 | write32(spibar + SPIBAR_OPMENU_LOWER, SPI_OPMENU_LOWER); |
| 117 | write32(spibar + SPIBAR_OPMENU_UPPER, SPI_OPMENU_UPPER); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Lock FAST_SPIBAR. |
| 122 | */ |
| 123 | void fast_spi_lock_bar(void) |
| 124 | { |
| 125 | void *spibar = fast_spi_get_bar(); |
| 126 | uint32_t hsfs; |
| 127 | |
| 128 | hsfs = read32(spibar + SPIBAR_HSFSTS_CTL); |
| 129 | hsfs |= SPIBAR_HSFSTS_FLOCKDN; |
| 130 | write32(spibar + SPIBAR_HSFSTS_CTL, hsfs); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Set FAST_SPIBAR Soft Reset Data Register value. |
| 135 | */ |
| 136 | void fast_spi_set_strap_msg_data(uint32_t soft_reset_data) |
| 137 | { |
| 138 | void *spibar = fast_spi_get_bar(); |
| 139 | uint32_t ssl, ssms; |
| 140 | |
| 141 | /* Set Strap Lock Disable */ |
| 142 | ssl = read32(spibar + SPIBAR_RESET_LOCK); |
| 143 | ssl &= ~SPIBAR_RESET_LOCK_ENABLE; |
| 144 | write32(spibar + SPIBAR_RESET_LOCK, ssl); |
| 145 | |
| 146 | /* Write Soft Reset Data register at SPIBAR0 offset 0xF8[0:15] */ |
| 147 | write32(spibar + SPIBAR_RESET_DATA, soft_reset_data); |
| 148 | |
| 149 | /* Set Strap Mux Select set to '1' */ |
| 150 | ssms = read32(spibar + SPIBAR_RESET_CTRL); |
| 151 | ssms |= SPIBAR_RESET_CTRL_SSMC; |
| 152 | write32(spibar + SPIBAR_RESET_CTRL, ssms); |
| 153 | |
| 154 | /* Set Strap Lock Enable */ |
| 155 | ssl = read32(spibar + SPIBAR_RESET_LOCK); |
| 156 | ssl |= SPIBAR_RESET_LOCK_ENABLE; |
| 157 | write32(spibar + SPIBAR_RESET_LOCK, ssl); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Returns bios_start and fills in size of the BIOS region. |
| 162 | */ |
| 163 | size_t fast_spi_get_bios_region(size_t *bios_size) |
| 164 | { |
| 165 | size_t bios_start, bios_end; |
| 166 | /* |
| 167 | * BIOS_BFPREG provides info about BIOS Flash Primary Region |
| 168 | * Base and Limit. |
| 169 | * Base and Limit fields are in units of 4KiB. |
| 170 | */ |
| 171 | uint32_t val = read32(fast_spi_get_bar() + SPIBAR_BFPREG); |
| 172 | |
| 173 | bios_start = (val & SPIBAR_BFPREG_PRB_MASK) * 4 * KiB; |
| 174 | bios_end = (((val & SPIBAR_BFPREG_PRL_MASK) >> |
| 175 | SPIBAR_BFPREG_PRL_SHIFT) + 1) * 4 * KiB; |
| 176 | *bios_size = bios_end - bios_start; |
| 177 | return bios_start; |
| 178 | } |
| 179 | |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 180 | void fast_spi_cache_bios_region(void) |
| 181 | { |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 182 | size_t bios_size; |
| 183 | uint32_t alignment; |
Aaron Durbin | 0b34fc6 | 2017-06-08 10:52:58 -0500 | [diff] [blame] | 184 | const int type = MTRR_TYPE_WRPROT; |
| 185 | uintptr_t base; |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 186 | |
| 187 | /* Only the IFD BIOS region is memory mapped (at top of 4G) */ |
| 188 | fast_spi_get_bios_region(&bios_size); |
| 189 | |
| 190 | if (!bios_size) |
| 191 | return; |
| 192 | |
| 193 | /* Round to power of two */ |
| 194 | alignment = 1 << (log2_ceil(bios_size)); |
| 195 | bios_size = ALIGN_UP(bios_size, alignment); |
Aaron Durbin | 0b34fc6 | 2017-06-08 10:52:58 -0500 | [diff] [blame] | 196 | base = 4ULL*GiB - bios_size; |
| 197 | |
| 198 | if (ENV_RAMSTAGE) { |
| 199 | mtrr_use_temp_range(base, bios_size, type); |
| 200 | } else { |
| 201 | int mtrr = get_free_var_mtrr(); |
| 202 | |
| 203 | if (mtrr == -1) |
| 204 | return; |
| 205 | |
| 206 | set_var_mtrr(mtrr, base, bios_size, type); |
| 207 | } |
Aaron Durbin | 5391e55 | 2017-06-02 12:16:04 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Barnali Sarkar | 89331cd | 2017-02-16 17:22:37 +0530 | [diff] [blame] | 210 | /* |
| 211 | * Program temporary BAR for SPI in case any of the stages before ramstage need |
| 212 | * to access FAST_SPI MMIO regs. Ramstage will assign a new BAR during PCI |
| 213 | * enumeration. |
| 214 | */ |
| 215 | void fast_spi_early_init(uintptr_t spi_base_address) |
| 216 | { |
| 217 | device_t dev = PCH_DEV_SPI; |
| 218 | uint8_t pcireg; |
| 219 | |
| 220 | /* Assign Resources to SPI Controller */ |
| 221 | /* Clear BIT 1-2 SPI Command Register */ |
| 222 | pcireg = pci_read_config8(dev, PCI_COMMAND); |
| 223 | pcireg &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY); |
| 224 | pci_write_config8(dev, PCI_COMMAND, pcireg); |
| 225 | |
| 226 | /* Program Temporary BAR for SPI */ |
| 227 | pci_write_config32(dev, PCI_BASE_ADDRESS_0, |
| 228 | spi_base_address | PCI_BASE_ADDRESS_SPACE_MEMORY); |
| 229 | |
| 230 | /* Enable Bus Master and MMIO Space */ |
| 231 | pcireg = pci_read_config8(dev, PCI_COMMAND); |
| 232 | pcireg |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
| 233 | pci_write_config8(dev, PCI_COMMAND, pcireg); |
| 234 | |
| 235 | /* Initialize SPI to allow BIOS to write/erase on flash. */ |
| 236 | fast_spi_init(); |
| 237 | } |