2 * This file is part of the coreboot project.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <device/device.h>
17 #include <device/pci_ops.h>
18 #include <device/pci_def.h>
19 #include <amdblocks/lpc.h>
20 #include <soc/iomap.h>
21 #include <soc/southbridge.h>
23 /* The LPC-ISA bridge is always at D14F3 */
24 #if !defined(__SIMPLE_DEVICE__)
25 #include <device/device.h>
26 #define _LPCB_DEV pcidev_on_root(0x14, 0x3)
28 #define _LPCB_DEV PCI_DEV(0, 0x14, 0x3)
32 * Structure to simplify code obtaining the total of used wide IO
33 * registers and the size assigned to each.
35 static const struct wide_io_ioport_and_bits
{
41 .enable
= LPC_WIDEIO0_ENABLE
,
42 .port
= LPC_WIDEIO_GENERIC_PORT
,
43 .alt
= LPC_ALT_WIDEIO0_ENABLE
46 .enable
= LPC_WIDEIO1_ENABLE
,
47 .port
= LPC_WIDEIO1_GENERIC_PORT
,
48 .alt
= LPC_ALT_WIDEIO1_ENABLE
51 .enable
= LPC_WIDEIO2_ENABLE
,
52 .port
= LPC_WIDEIO2_GENERIC_PORT
,
53 .alt
= LPC_ALT_WIDEIO2_ENABLE
58 * @brief Find the size of a particular wide IO
60 * @param index = index of desired wide IO
62 * @return size of desired wide IO
64 uint16_t lpc_wideio_size(int index
)
66 uint32_t enable_register
;
68 uint8_t alternate_register
;
70 if (index
>= ARRAY_SIZE(wio_io_en
))
72 enable_register
= pci_read_config32(_LPCB_DEV
,
73 LPC_IO_OR_MEM_DECODE_ENABLE
);
74 alternate_register
= pci_read_config8(_LPCB_DEV
,
75 LPC_ALT_WIDEIO_RANGE_ENABLE
);
76 if (enable_register
& wio_io_en
[index
].enable
)
77 size
= (alternate_register
& wio_io_en
[index
].alt
) ?
83 * @brief Identify if any LPC wide IO is covering the IO range
85 * @param start = start of IO range
86 * @param size = size of IO range
88 * @return Index of wide IO covering the range or error
90 int lpc_find_wideio_range(uint16_t start
, uint16_t size
)
92 int i
, index
= WIDEIO_RANGE_ERROR
;
93 uint16_t end
, current_size
, start_wideio
, end_wideio
;
96 for (i
= 0; i
< ARRAY_SIZE(wio_io_en
); i
++) {
97 current_size
= lpc_wideio_size(i
);
98 if (current_size
== 0)
100 start_wideio
= pci_read_config16(_LPCB_DEV
,
102 end_wideio
= start_wideio
+ current_size
;
103 if ((start
>= start_wideio
) && (end
<= end_wideio
)) {
112 * @brief Program a LPC wide IO to support an IO range
114 * @param start = start of range to be routed through wide IO
115 * @param size = size of range to be routed through wide IO
117 * @return Index of wide IO register used or error
119 int lpc_set_wideio_range(uint16_t start
, uint16_t size
)
121 int i
, index
= WIDEIO_RANGE_ERROR
;
122 uint32_t enable_register
;
123 uint8_t alternate_register
;
125 enable_register
= pci_read_config32(_LPCB_DEV
,
126 LPC_IO_OR_MEM_DECODE_ENABLE
);
127 alternate_register
= pci_read_config8(_LPCB_DEV
,
128 LPC_ALT_WIDEIO_RANGE_ENABLE
);
129 for (i
= 0; i
< ARRAY_SIZE(wio_io_en
); i
++) {
130 if (enable_register
& wio_io_en
[i
].enable
)
133 pci_write_config16(_LPCB_DEV
, wio_io_en
[i
].port
, start
);
134 enable_register
|= wio_io_en
[i
].enable
;
135 pci_write_config32(_LPCB_DEV
, LPC_IO_OR_MEM_DECODE_ENABLE
,
138 alternate_register
|= wio_io_en
[i
].alt
;
140 alternate_register
&= ~wio_io_en
[i
].alt
;
141 pci_write_config8(_LPCB_DEV
,
142 LPC_ALT_WIDEIO_RANGE_ENABLE
,
149 void lpc_enable_port80(void)
153 byte
= pci_read_config8(_LPCB_DEV
, LPC_IO_OR_MEM_DEC_EN_HIGH
);
154 byte
|= DECODE_IO_PORT_ENABLE4_H
;
155 pci_write_config8(_LPCB_DEV
, LPC_IO_OR_MEM_DEC_EN_HIGH
, byte
);
158 void lpc_enable_pci_port80(void)
162 byte
= pci_read_config8(_LPCB_DEV
, LPC_IO_OR_MEM_DEC_EN_HIGH
);
163 byte
&= ~DECODE_IO_PORT_ENABLE4_H
; /* disable lpc port 80 */
164 pci_write_config8(_LPCB_DEV
, LPC_IO_OR_MEM_DEC_EN_HIGH
, byte
);
167 void lpc_enable_sio_decode(const bool addr
)
172 decodes
= pci_read_config32(_LPCB_DEV
, LPC_IO_OR_MEM_DECODE_ENABLE
);
173 enable
= addr
== LPC_SELECT_SIO_2E2F
?
174 DECODE_SIO_ENABLE
: DECODE_ALTERNATE_SIO_ENABLE
;
176 pci_write_config32(_LPCB_DEV
, LPC_IO_OR_MEM_DECODE_ENABLE
, decodes
);
179 void lpc_enable_decode(uint32_t decodes
)
181 pci_write_config32(_LPCB_DEV
, LPC_IO_PORT_DECODE_ENABLE
, decodes
);
185 * Clear all decoding to the LPC bus and erase any range registers associated
186 * with the enable bits.
188 void lpc_disable_decodes(void)
192 lpc_enable_decode(0);
193 reg
= pci_read_config32(_LPCB_DEV
, LPC_IO_OR_MEM_DECODE_ENABLE
);
194 reg
&= LPC_SYNC_TIMEOUT_COUNT_MASK
| LPC_SYNC_TIMEOUT_COUNT_ENABLE
;
195 pci_write_config32(_LPCB_DEV
, LPC_IO_OR_MEM_DECODE_ENABLE
, reg
);
197 /* D14F3x48 enables ranges configured in additional registers */
198 pci_write_config32(_LPCB_DEV
, LPC_MEM_PORT1
, 0);
199 pci_write_config32(_LPCB_DEV
, LPC_MEM_PORT0
, 0);
200 pci_write_config32(_LPCB_DEV
, LPC_WIDEIO2_GENERIC_PORT
, 0);
203 uintptr_t lpc_spibase(void)
207 /* Make sure the base address is predictable */
208 base
= pci_read_config32(_LPCB_DEV
, SPIROM_BASE_ADDRESS_REGISTER
);
209 enables
= base
& SPI_PRESERVE_BITS
;
210 base
&= ~(SPI_PRESERVE_BITS
| SPI_BASE_RESERVED
);
213 base
= SPI_BASE_ADDRESS
;
214 pci_write_config32(_LPCB_DEV
, SPIROM_BASE_ADDRESS_REGISTER
,
215 base
| enables
| SPI_ROM_ENABLE
);
216 /* PCI_COMMAND_MEMORY is read-only and enabled. */
222 * Enable FCH to decode TPM associated Memory and IO regions
224 * Enable decoding of TPM cycles defined in TPM 1.2 spec
225 * Enable decoding of legacy TPM addresses: IO addresses 0x7f-
226 * 0x7e and 0xef-0xee.
227 * This function should be called if TPM is connected in any way to the FCH and
228 * conforms to the regions decoded.
229 * Absent any other routing configuration the TPM cycles will be claimed by the
232 void lpc_tpm_decode(void)
236 value
= pci_read_config32(_LPCB_DEV
, LPC_TRUSTED_PLATFORM_MODULE
);
237 value
|= TPM_12_EN
| TPM_LEGACY_EN
;
238 pci_write_config32(_LPCB_DEV
, LPC_TRUSTED_PLATFORM_MODULE
, value
);
242 * Enable FCH to decode TPM associated Memory and IO regions to SPI
244 * This should be used if TPM is connected to SPI bus.
245 * Assumes SPI address space is already configured via a call to lpc_spibase().
247 void lpc_tpm_decode_spi(void)
249 /* Enable TPM decoding to FCH */
252 /* Route TPM accesses to SPI */
253 u32 spibase
= pci_read_config32(_LPCB_DEV
,
254 SPIROM_BASE_ADDRESS_REGISTER
);
255 pci_write_config32(_LPCB_DEV
, SPIROM_BASE_ADDRESS_REGISTER
, spibase
260 * Enable 4MB (LPC) ROM access at 0xFFC00000 - 0xFFFFFFFF.
262 * Hardware should enable LPC ROM by pin straps. This function does not
263 * handle the theoretically possible PCI ROM, FWH, or SPI ROM configurations.
265 * The southbridge power-on default is to map 512K ROM space.
268 void lpc_enable_rom(void)
273 * Decode variable LPC ROM address ranges 1 and 2.
274 * Bits 3-4 are not defined in any publicly available datasheet
276 reg8
= pci_read_config8(_LPCB_DEV
, LPC_IO_OR_MEM_DECODE_ENABLE
);
277 reg8
|= (1 << 3) | (1 << 4);
278 pci_write_config8(_LPCB_DEV
, LPC_IO_OR_MEM_DECODE_ENABLE
, reg8
);
281 * LPC ROM address range 1:
282 * Enable LPC ROM range mirroring start at 0x000e(0000).
284 pci_write_config16(_LPCB_DEV
, ROM_ADDRESS_RANGE1_START
, 0x000e);
286 /* Enable LPC ROM range mirroring end at 0x000f(ffff). */
287 pci_write_config16(_LPCB_DEV
, ROM_ADDRESS_RANGE1_END
, 0x000f);
290 * LPC ROM address range 2:
292 * Enable LPC ROM range start at:
293 * 0xfff8(0000): 512KB
298 pci_write_config16(_LPCB_DEV
, ROM_ADDRESS_RANGE2_START
, 0x10000
299 - (CONFIG_COREBOOT_ROMSIZE_KB
>> 6));
301 /* Enable LPC ROM range end at 0xffff(ffff). */
302 pci_write_config16(_LPCB_DEV
, ROM_ADDRESS_RANGE2_END
, 0xffff);
305 void lpc_enable_spi_prefetch(void)
309 dword
= pci_read_config32(_LPCB_DEV
, LPC_ROM_DMA_EC_HOST_CONTROL
);
310 dword
|= SPI_FROM_HOST_PREFETCH_EN
| SPI_FROM_USB_PREFETCH_EN
;
311 pci_write_config32(_LPCB_DEV
, LPC_ROM_DMA_EC_HOST_CONTROL
, dword
);
314 uintptr_t lpc_get_spibase(void)
318 base
= pci_read_config32(_LPCB_DEV
, SPIROM_BASE_ADDRESS_REGISTER
);
319 base
= ALIGN_DOWN(base
, SPI_BASE_ALIGNMENT
);
320 return (uintptr_t)base
;
323 void lpc_set_spibase(u32 base
, u32 enable
)
327 /* only two types of CS# enables are allowed */
328 enable
&= SPI_ROM_ENABLE
| SPI_ROM_ALT_ENABLE
;
330 reg32
= pci_read_config32(_LPCB_DEV
, SPIROM_BASE_ADDRESS_REGISTER
);
332 reg32
&= SPI_BASE_ALIGNMENT
- 1; /* preserve only reserved, enables */
333 reg32
&= ~(SPI_ROM_ENABLE
| SPI_ROM_ALT_ENABLE
);
335 reg32
|= ALIGN_DOWN(base
, SPI_BASE_ALIGNMENT
);
337 pci_write_config32(_LPCB_DEV
, SPIROM_BASE_ADDRESS_REGISTER
, reg32
);