blob: a53499eba630b7d2141cb95629bc95d622ac26dc [file] [log] [blame]
Barnali Sarkar89331cd2017-02-16 17:22:37 +05301/*
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 Durbin5391e552017-06-02 12:16:04 -050021#include <cpu/x86/mtrr.h>
Barnali Sarkar89331cd2017-02-16 17:22:37 +053022#include <fast_spi_def.h>
23#include <intelblocks/fast_spi.h>
Aaron Durbin5391e552017-06-02 12:16:04 -050024#include <lib.h>
Barnali Sarkar89331cd2017-02-16 17:22:37 +053025#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 */
35void *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 */
52void 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 */
72static 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 */
86void 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 */
94void 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 */
102void 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 */
110void 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 */
123void 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 */
136void 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 */
163size_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 Durbin5391e552017-06-02 12:16:04 -0500180void fast_spi_cache_bios_region(void)
181{
182 int mtrr;
183 size_t bios_size;
184 uint32_t alignment;
185
186 mtrr = get_free_var_mtrr();
187
188 if (mtrr == -1)
189 return;
190
191 /* Only the IFD BIOS region is memory mapped (at top of 4G) */
192 fast_spi_get_bios_region(&bios_size);
193
194 if (!bios_size)
195 return;
196
197 /* Round to power of two */
198 alignment = 1 << (log2_ceil(bios_size));
199 bios_size = ALIGN_UP(bios_size, alignment);
200 set_var_mtrr(mtrr, 4ULL*GiB - bios_size, bios_size, MTRR_TYPE_WRPROT);
201}
202
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530203/*
204 * Program temporary BAR for SPI in case any of the stages before ramstage need
205 * to access FAST_SPI MMIO regs. Ramstage will assign a new BAR during PCI
206 * enumeration.
207 */
208void fast_spi_early_init(uintptr_t spi_base_address)
209{
210 device_t dev = PCH_DEV_SPI;
211 uint8_t pcireg;
212
213 /* Assign Resources to SPI Controller */
214 /* Clear BIT 1-2 SPI Command Register */
215 pcireg = pci_read_config8(dev, PCI_COMMAND);
216 pcireg &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
217 pci_write_config8(dev, PCI_COMMAND, pcireg);
218
219 /* Program Temporary BAR for SPI */
220 pci_write_config32(dev, PCI_BASE_ADDRESS_0,
221 spi_base_address | PCI_BASE_ADDRESS_SPACE_MEMORY);
222
223 /* Enable Bus Master and MMIO Space */
224 pcireg = pci_read_config8(dev, PCI_COMMAND);
225 pcireg |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
226 pci_write_config8(dev, PCI_COMMAND, pcireg);
227
228 /* Initialize SPI to allow BIOS to write/erase on flash. */
229 fast_spi_init();
230}