blob: f8b467a849183cae7caac3ed7ef2e6d277ade093 [file] [log] [blame]
Patrick Georgi02363b52020-05-05 20:48:50 +02001/* This file is part of the coreboot project. */
Ravi Sarawadiefa606b2017-08-04 16:26:09 -07002/*
Ravi Sarawadiefa606b2017-08-04 16:26:09 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
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.
13 */
14
15#define __SIMPLE_DEVICE__
16
17#include <assert.h>
18#include <console/console.h>
19#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020020#include <device/pci_ops.h>
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070021#include <intelblocks/lpc_lib.h>
22#include <lib.h>
23#include "lpc_def.h"
24#include <soc/pci_devs.h>
25
Subrata Banikd83face2018-03-08 14:04:52 +053026uint16_t lpc_enable_fixed_io_ranges(uint16_t io_enables)
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070027{
28 uint16_t reg_io_enables;
29
30 reg_io_enables = pci_read_config16(PCH_DEV_LPC, LPC_IO_ENABLES);
31 io_enables |= reg_io_enables;
32 pci_write_config16(PCH_DEV_LPC, LPC_IO_ENABLES, io_enables);
Subrata Banikd83face2018-03-08 14:04:52 +053033
34 return io_enables;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070035}
36
Wim Vervoorne6db9102020-02-03 14:57:40 +010037uint16_t lpc_get_fixed_io_decode(void)
38{
39 return pci_read_config16(PCH_DEV_LPC, LPC_IO_DECODE);
40}
41
Wim Vervoorn5f2adfe2020-02-03 15:32:54 +010042uint16_t lpc_set_fixed_io_ranges(uint16_t io_ranges, uint16_t mask)
43{
44 uint16_t reg_io_ranges;
45
46 reg_io_ranges = lpc_get_fixed_io_decode() & ~mask;
47 io_ranges |= reg_io_ranges & mask;
48 pci_write_config16(PCH_DEV_LPC, LPC_IO_DECODE, io_ranges);
49
50 return io_ranges;
51}
52
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070053/*
54 * Find the first unused IO window.
55 * Returns -1 if not found, 0 for reg 0x84, 1 for reg 0x88 ...
56 */
57static int find_unused_pmio_window(void)
58{
59 int i;
60 uint32_t lgir;
61
62 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) {
63 lgir = pci_read_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i));
64
65 if (!(lgir & LPC_LGIR_EN))
66 return i;
67 }
68
69 return -1;
70}
71
72void lpc_close_pmio_windows(void)
73{
74 size_t i;
75
76 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++)
77 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i), 0);
78}
79
80void lpc_open_pmio_window(uint16_t base, uint16_t size)
81{
Lijian Zhaoe6db1892018-04-13 16:27:38 -070082 int i, lgir_reg_num;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070083 uint32_t lgir_reg_offset, lgir, window_size, alignment;
84 resource_t bridged_size, bridge_base;
85
86 printk(BIOS_SPEW, "LPC: Trying to open IO window from %x size %x\n",
87 base, size);
88
89 bridged_size = 0;
90 bridge_base = base;
91
92 while (bridged_size < size) {
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070093 /* Each IO range register can only open a 256-byte window. */
94 window_size = MIN(size, LPC_LGIR_MAX_WINDOW_SIZE);
95
John Zhao1ceac4e2019-07-09 14:27:28 -070096 if (window_size <= 0)
John Zhao2bb432e2019-05-21 19:32:51 -070097 return;
98
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070099 /* Window size must be a power of two for the AMASK to work. */
Paul Menzelfa7d2a02017-10-27 15:54:26 +0200100 alignment = 1UL << (log2_ceil(window_size));
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700101 window_size = ALIGN_UP(window_size, alignment);
102
103 /* Address[15:2] in LGIR[15:12] and Mask[7:2] in LGIR[23:18]. */
104 lgir = (bridge_base & LPC_LGIR_ADDR_MASK) | LPC_LGIR_EN;
105 lgir |= ((window_size - 1) << 16) & LPC_LGIR_AMASK_MASK;
106
Lijian Zhaoe6db1892018-04-13 16:27:38 -0700107 /* Skip programming if same range already programmed. */
108 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) {
109 if (lgir == pci_read_config32(PCH_DEV_LPC,
110 LPC_GENERIC_IO_RANGE(i)))
111 return;
112 }
113
114 lgir_reg_num = find_unused_pmio_window();
115 if (lgir_reg_num < 0) {
116 printk(BIOS_ERR,
117 "LPC: Cannot open IO window: %llx size %llx\n",
118 bridge_base, size - bridged_size);
119 printk(BIOS_ERR, "No more IO windows\n");
120 return;
121 }
122 lgir_reg_offset = LPC_GENERIC_IO_RANGE(lgir_reg_num);
123
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700124 pci_write_config32(PCH_DEV_LPC, lgir_reg_offset, lgir);
125
126 printk(BIOS_DEBUG,
127 "LPC: Opened IO window LGIR%d: base %llx size %x\n",
128 lgir_reg_num, bridge_base, window_size);
129
130 bridged_size += window_size;
131 bridge_base += window_size;
132 }
133}
134
135void lpc_open_mmio_window(uintptr_t base, size_t size)
136{
137 uint32_t lgmr;
138
139 lgmr = pci_read_config32(PCH_DEV_LPC, LPC_GENERIC_MEM_RANGE);
140
141 if (lgmr & LPC_LGMR_EN) {
142 printk(BIOS_ERR,
143 "LPC: Cannot open window to resource %lx size %zx\n",
144 base, size);
145 printk(BIOS_ERR, "LPC: MMIO window already in use\n");
146 return;
147 }
148
149 if (size > LPC_LGMR_WINDOW_SIZE) {
150 printk(BIOS_WARNING,
151 "LPC: Resource %lx size %zx larger than window(%x)\n",
152 base, size, LPC_LGMR_WINDOW_SIZE);
153 }
154
155 lgmr = (base & LPC_LGMR_ADDR_MASK) | LPC_LGMR_EN;
156
157 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_MEM_RANGE, lgmr);
158}
159
160bool lpc_fits_fixed_mmio_window(uintptr_t base, size_t size)
161{
162 resource_t res_end, range_end;
163 const struct lpc_mmio_range *range;
164 const struct lpc_mmio_range *lpc_fixed_mmio_ranges =
165 soc_get_fixed_mmio_ranges();
166
167 for (range = lpc_fixed_mmio_ranges; range->size; range++) {
168 range_end = range->base + range->size;
169 res_end = base + size;
170
171 if ((base >= range->base) && (res_end <= range_end)) {
172 printk(BIOS_DEBUG,
173 "Resource %lx size %zx fits in fixed window"
174 " %lx size %zx\n",
175 base, size, range->base, range->size);
176 return true;
177 }
178 }
179 return false;
180}
181
182/*
183 * Set FAST_SPIBAR BIOS Control register based on input bit field.
184 */
185static void lpc_set_bios_control_reg(uint8_t bios_cntl_bit)
186{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200187 pci_devfn_t dev = PCH_DEV_LPC;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700188 uint8_t bc_cntl;
189
Jonathan Neuschäfer3a182f72017-09-23 17:09:36 +0200190 assert(IS_POWER_OF_2(bios_cntl_bit));
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700191 bc_cntl = pci_read_config8(dev, LPC_BIOS_CNTL);
192 bc_cntl |= bios_cntl_bit;
193 pci_write_config8(dev, LPC_BIOS_CNTL, bc_cntl);
194
195 /*
196 * Ensure an additional read back after performing lock down
197 */
198 pci_read_config8(PCH_DEV_LPC, LPC_BIOS_CNTL);
199}
200
201/*
202* Set LPC BIOS Control BILD bit.
203*/
204void lpc_set_bios_interface_lock_down(void)
205{
206 lpc_set_bios_control_reg(LPC_BC_BILD);
207}
208
209/*
210* Set LPC BIOS Control LE bit.
211*/
212void lpc_set_lock_enable(void)
213{
214 lpc_set_bios_control_reg(LPC_BC_LE);
215}
216
217/*
218* Set LPC BIOS Control EISS bit.
219*/
220void lpc_set_eiss(void)
221{
222 lpc_set_bios_control_reg(LPC_BC_EISS);
223}
224
225/*
226* Set LPC Serial IRQ mode.
227*/
228void lpc_set_serirq_mode(enum serirq_mode mode)
229{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200230 pci_devfn_t dev = PCH_DEV_LPC;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700231 uint8_t scnt;
232
233 scnt = pci_read_config8(dev, LPC_SERIRQ_CTL);
234 scnt &= ~(LPC_SCNT_EN | LPC_SCNT_MODE);
235
236 switch (mode) {
237 case SERIRQ_QUIET:
238 scnt |= LPC_SCNT_EN;
239 break;
240 case SERIRQ_CONTINUOUS:
241 scnt |= LPC_SCNT_EN | LPC_SCNT_MODE;
242 break;
243 case SERIRQ_OFF:
244 default:
245 break;
246 }
247
248 pci_write_config8(dev, LPC_SERIRQ_CTL, scnt);
249}
250
251
252void lpc_io_setup_comm_a_b(void)
253{
Subrata Banikd83face2018-03-08 14:04:52 +0530254 /* ComA Range 3F8h-3FFh [2:0] */
255 uint16_t com_ranges = LPC_IOD_COMA_RANGE;
256 uint16_t com_enable = LPC_IOE_COMA_EN;
257
258 /* ComB Range 2F8h-2FFh [6:4] */
Julius Wernercd49cce2019-03-05 16:53:33 -0800259 if (CONFIG(SOC_INTEL_COMMON_BLOCK_LPC_COMB_ENABLE)) {
Subrata Banikd83face2018-03-08 14:04:52 +0530260 com_ranges |= LPC_IOD_COMB_RANGE;
261 com_enable |= LPC_IOE_COMB_EN;
262 }
263
264 /* Setup I/O Decode Range Register for LPC */
265 pci_write_config16(PCH_DEV_LPC, LPC_IO_DECODE, com_ranges);
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700266 /* Enable ComA and ComB Port */
Subrata Banikd83face2018-03-08 14:04:52 +0530267 lpc_enable_fixed_io_ranges(com_enable);
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700268}
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700269
270static void lpc_set_gen_decode_range(
271 uint32_t gen_io_dec[LPC_NUM_GENERIC_IO_RANGES])
272{
273 size_t i;
274
275 /* Set in PCI generic decode range registers */
276 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++)
277 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i),
278 gen_io_dec[i]);
279}
280
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700281void pch_enable_lpc(void)
282{
283 /* Lookup device tree in romstage */
284 const struct device *dev;
285 uint32_t gen_io_dec[LPC_NUM_GENERIC_IO_RANGES];
286
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300287 dev = pcidev_on_root(PCH_DEV_SLOT_LPC, 0);
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300288 if (!dev)
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700289 return;
290
291 soc_get_gen_io_dec_range(dev, gen_io_dec);
292 lpc_set_gen_decode_range(gen_io_dec);
293 soc_setup_dmi_pcr_io_dec(gen_io_dec);
Subrata Banik42c44c22019-05-15 20:27:04 +0530294 if (ENV_PAYLOAD_LOADER)
Subrata Banik0d866f82020-02-18 11:20:30 +0530295 soc_pch_pirq_init(dev);
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700296}
297
298void lpc_enable_pci_clk_cntl(void)
299{
300 pci_write_config8(PCH_DEV_LPC, LPC_PCCTL, LPC_PCCTL_CLKRUN_EN);
301}
Nico Huberdbcf2932018-11-28 15:29:00 +0100302
303void lpc_disable_clkrun(void)
304{
305 const uint8_t pcctl = pci_read_config8(PCH_DEV_LPC, LPC_PCCTL);
306 pci_write_config8(PCH_DEV_LPC, LPC_PCCTL, pcctl & ~LPC_PCCTL_CLKRUN_EN);
307}