blob: b42030885edd516152e3bde081dd1d4089f2de02 [file] [log] [blame]
Angel Pons0612b272020-04-05 15:46:56 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Barnali Sarkar89331cd2017-02-16 17:22:37 +05303
Kyösti Mälkki13f66502019-03-03 08:01:05 +02004#include <device/mmio.h>
Barnali Sarkar89331cd2017-02-16 17:22:37 +05305#include <assert.h>
6#include <device/pci_def.h>
Patrick Rudolphe56189c2018-04-18 10:11:59 +02007#include <device/pci_ops.h>
Barnali Sarkar89331cd2017-02-16 17:22:37 +05308#include <commonlib/helpers.h>
Aaron Durbin5391e552017-06-02 12:16:04 -05009#include <cpu/x86/mtrr.h>
Barnali Sarkar89331cd2017-02-16 17:22:37 +053010#include <fast_spi_def.h>
11#include <intelblocks/fast_spi.h>
Aaron Durbin5391e552017-06-02 12:16:04 -050012#include <lib.h>
Barnali Sarkar89331cd2017-02-16 17:22:37 +053013#include <soc/pci_devs.h>
14#include <spi_flash.h>
15#include <spi-generic.h>
Barnali Sarkar89331cd2017-02-16 17:22:37 +053016
17/*
18 * Get the FAST_SPIBAR.
19 */
20void *fast_spi_get_bar(void)
21{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +020022#if defined(__SIMPLE_DEVICE__)
23 pci_devfn_t dev = PCH_DEV_SPI;
24#else
25 struct device *dev = PCH_DEV_SPI;
26#endif
Barnali Sarkar89331cd2017-02-16 17:22:37 +053027 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 */
41void fast_spi_init(void)
42{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +020043#if defined(__SIMPLE_DEVICE__)
44 pci_devfn_t dev = PCH_DEV_SPI;
45#else
46 struct device *dev = PCH_DEV_SPI;
47#endif
Barnali Sarkar89331cd2017-02-16 17:22:37 +053048 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 Banik8e390092017-07-21 10:06:17 +053063 * Set FAST_SPIBAR BIOS Control register based on input bit field.
Barnali Sarkar89331cd2017-02-16 17:22:37 +053064 */
65static void fast_spi_set_bios_control_reg(uint8_t bios_cntl_bit)
66{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +020067#if defined(__SIMPLE_DEVICE__)
68 pci_devfn_t dev = PCH_DEV_SPI;
69#else
70 struct device *dev = PCH_DEV_SPI;
71#endif
Barnali Sarkar89331cd2017-02-16 17:22:37 +053072 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 Banik8e390092017-07-21 10:06:17 +053081 * Ensure an additional read back after performing lock down
82 */
83static void fast_spi_read_post_write(uint8_t reg)
84{
85 pci_read_config8(PCH_DEV_SPI, reg);
86}
87
88/*
Barnali Sarkar89331cd2017-02-16 17:22:37 +053089 * Set FAST_SPIBAR BIOS Control BILD bit.
90 */
91void fast_spi_set_bios_interface_lock_down(void)
92{
93 fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_BILD);
Subrata Banik8e390092017-07-21 10:06:17 +053094
95 fast_spi_read_post_write(SPIBAR_BIOS_CONTROL);
Barnali Sarkar89331cd2017-02-16 17:22:37 +053096}
97
98/*
99 * Set FAST_SPIBAR BIOS Control LE bit.
100 */
101void fast_spi_set_lock_enable(void)
102{
103 fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_LOCK_ENABLE);
Subrata Banik8e390092017-07-21 10:06:17 +0530104
105
106 fast_spi_read_post_write(SPIBAR_BIOS_CONTROL);
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530107}
108
109/*
110 * Set FAST_SPIBAR BIOS Control EISS bit.
111 */
112void fast_spi_set_eiss(void)
113{
114 fast_spi_set_bios_control_reg(SPIBAR_BIOS_CONTROL_EISS);
Subrata Banik8e390092017-07-21 10:06:17 +0530115
116 fast_spi_read_post_write(SPIBAR_BIOS_CONTROL);
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530117}
118
119/*
120 * Set FAST_SPI opcode menu.
121 */
122void 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 Sarkar8e513192017-07-19 16:09:56 +0530134 * 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 Sarkar89331cd2017-02-16 17:22:37 +0530140 */
141void fast_spi_lock_bar(void)
142{
143 void *spibar = fast_spi_get_bar();
Duncan Lauriedc1e6bc2017-08-15 13:32:26 -0700144 uint16_t hsfs = SPIBAR_HSFSTS_FLOCKDN;
145
Julius Wernercd49cce2019-03-05 16:53:33 -0800146 if (CONFIG(FAST_SPI_DISABLE_WRITE_STATUS))
Duncan Lauriedc1e6bc2017-08-15 13:32:26 -0700147 hsfs |= SPIBAR_HSFSTS_WRSDIS;
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530148
Barnali Sarkar8e513192017-07-19 16:09:56 +0530149 write16(spibar + SPIBAR_HSFSTS_CTL, hsfs);
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530150}
151
152/*
Barnali Sarkar4f6e3412017-08-17 11:49:27 +0530153 * Set FAST_SPIBAR + DLOCK (0x0C) register bits to discrete lock the
154 * FAST_SPI Protected Range (PR) registers.
155 */
156void 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 Sarkar89331cd2017-02-16 17:22:37 +0530170 * Set FAST_SPIBAR Soft Reset Data Register value.
171 */
172void 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 */
199size_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 Durbin5391e552017-06-02 12:16:04 -0500216void fast_spi_cache_bios_region(void)
217{
Aaron Durbin5391e552017-06-02 12:16:04 -0500218 size_t bios_size;
219 uint32_t alignment;
Aaron Durbin0b34fc62017-06-08 10:52:58 -0500220 const int type = MTRR_TYPE_WRPROT;
221 uintptr_t base;
Aaron Durbin5391e552017-06-02 12:16:04 -0500222
223 /* Only the IFD BIOS region is memory mapped (at top of 4G) */
224 fast_spi_get_bios_region(&bios_size);
225
Lijian Zhaoad1e49a2018-11-29 16:24:24 -0800226 /* LOCAL APIC default address is 0xFEE0000, bios_size over 16MB will
227 * cause memory type conflict when setting memory type to write
Elyes HAOUAS6dc9d032020-02-16 16:22:52 +0100228 * protection, so limit the cached BIOS region to be no more than 16MB.
Lijian Zhaoad1e49a2018-11-29 16:24:24 -0800229 * */
230 bios_size = MIN(bios_size, 16 * MiB);
John Zhao1ceac4e2019-07-09 14:27:28 -0700231 if (bios_size <= 0)
John Zhao2bb432e2019-05-21 19:32:51 -0700232 return;
Lijian Zhaoad1e49a2018-11-29 16:24:24 -0800233
Aaron Durbin5391e552017-06-02 12:16:04 -0500234 /* Round to power of two */
Paul Menzel64e83402017-10-27 11:05:14 +0200235 alignment = 1UL << (log2_ceil(bios_size));
Aaron Durbin5391e552017-06-02 12:16:04 -0500236 bios_size = ALIGN_UP(bios_size, alignment);
Aaron Durbin0b34fc62017-06-08 10:52:58 -0500237 base = 4ULL*GiB - bios_size;
238
Subrata Banik42c44c22019-05-15 20:27:04 +0530239 if (ENV_PAYLOAD_LOADER) {
Aaron Durbin0b34fc62017-06-08 10:52:58 -0500240 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 Durbin5391e552017-06-02 12:16:04 -0500249}
250
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530251/*
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 */
256void fast_spi_early_init(uintptr_t spi_base_address)
257{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200258#if defined(__SIMPLE_DEVICE__)
259 pci_devfn_t dev = PCH_DEV_SPI;
260#else
261 struct device *dev = PCH_DEV_SPI;
262#endif
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530263 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 Sarawadib051a9f52017-09-07 12:15:45 -0700283
284/* Read SPI Write Protect disable status. */
285bool 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. */
292void fast_spi_enable_wp(void)
293{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200294#if defined(__SIMPLE_DEVICE__)
295 pci_devfn_t dev = PCH_DEV_SPI;
296#else
297 struct device *dev = PCH_DEV_SPI;
298#endif
Ravi Sarawadib051a9f52017-09-07 12:15:45 -0700299 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}