blob: 67dd6614603b937274359e439d5bf8267b82612e [file] [log] [blame]
Patrick Georgiac959032020-05-05 22:49:26 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Ravi Sarawadiefa606b2017-08-04 16:26:09 -07002
3#define __SIMPLE_DEVICE__
4
Subrata Banik1366e442020-09-29 13:55:50 +05305#include <arch/ioapic.h>
Ravi Sarawadiefa606b2017-08-04 16:26:09 -07006#include <assert.h>
7#include <console/console.h>
8#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Subrata Banik78463a72020-09-29 14:28:09 +053010#include <intelblocks/itss.h>
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070011#include <intelblocks/lpc_lib.h>
12#include <lib.h>
13#include "lpc_def.h"
Subrata Banik78463a72020-09-29 14:28:09 +053014#include <soc/irq.h>
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070015#include <soc/pci_devs.h>
16
Subrata Banikd83face2018-03-08 14:04:52 +053017uint16_t lpc_enable_fixed_io_ranges(uint16_t io_enables)
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070018{
19 uint16_t reg_io_enables;
20
21 reg_io_enables = pci_read_config16(PCH_DEV_LPC, LPC_IO_ENABLES);
22 io_enables |= reg_io_enables;
23 pci_write_config16(PCH_DEV_LPC, LPC_IO_ENABLES, io_enables);
Subrata Banikd83face2018-03-08 14:04:52 +053024
25 return io_enables;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070026}
27
Wim Vervoorne6db9102020-02-03 14:57:40 +010028uint16_t lpc_get_fixed_io_decode(void)
29{
30 return pci_read_config16(PCH_DEV_LPC, LPC_IO_DECODE);
31}
32
Wim Vervoorn5f2adfe2020-02-03 15:32:54 +010033uint16_t lpc_set_fixed_io_ranges(uint16_t io_ranges, uint16_t mask)
34{
35 uint16_t reg_io_ranges;
36
37 reg_io_ranges = lpc_get_fixed_io_decode() & ~mask;
38 io_ranges |= reg_io_ranges & mask;
39 pci_write_config16(PCH_DEV_LPC, LPC_IO_DECODE, io_ranges);
40
41 return io_ranges;
42}
43
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070044/*
45 * Find the first unused IO window.
46 * Returns -1 if not found, 0 for reg 0x84, 1 for reg 0x88 ...
47 */
48static int find_unused_pmio_window(void)
49{
50 int i;
51 uint32_t lgir;
52
53 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) {
54 lgir = pci_read_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i));
55
56 if (!(lgir & LPC_LGIR_EN))
57 return i;
58 }
59
60 return -1;
61}
62
63void lpc_close_pmio_windows(void)
64{
65 size_t i;
66
67 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++)
68 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i), 0);
69}
70
71void lpc_open_pmio_window(uint16_t base, uint16_t size)
72{
Lijian Zhaoe6db1892018-04-13 16:27:38 -070073 int i, lgir_reg_num;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070074 uint32_t lgir_reg_offset, lgir, window_size, alignment;
75 resource_t bridged_size, bridge_base;
76
77 printk(BIOS_SPEW, "LPC: Trying to open IO window from %x size %x\n",
78 base, size);
79
80 bridged_size = 0;
81 bridge_base = base;
82
83 while (bridged_size < size) {
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070084 /* Each IO range register can only open a 256-byte window. */
85 window_size = MIN(size, LPC_LGIR_MAX_WINDOW_SIZE);
86
John Zhao1ceac4e2019-07-09 14:27:28 -070087 if (window_size <= 0)
John Zhao2bb432e2019-05-21 19:32:51 -070088 return;
89
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070090 /* Window size must be a power of two for the AMASK to work. */
Paul Menzelfa7d2a02017-10-27 15:54:26 +020091 alignment = 1UL << (log2_ceil(window_size));
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070092 window_size = ALIGN_UP(window_size, alignment);
93
94 /* Address[15:2] in LGIR[15:12] and Mask[7:2] in LGIR[23:18]. */
95 lgir = (bridge_base & LPC_LGIR_ADDR_MASK) | LPC_LGIR_EN;
96 lgir |= ((window_size - 1) << 16) & LPC_LGIR_AMASK_MASK;
97
Lijian Zhaoe6db1892018-04-13 16:27:38 -070098 /* Skip programming if same range already programmed. */
99 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) {
100 if (lgir == pci_read_config32(PCH_DEV_LPC,
101 LPC_GENERIC_IO_RANGE(i)))
102 return;
103 }
104
105 lgir_reg_num = find_unused_pmio_window();
106 if (lgir_reg_num < 0) {
107 printk(BIOS_ERR,
108 "LPC: Cannot open IO window: %llx size %llx\n",
109 bridge_base, size - bridged_size);
110 printk(BIOS_ERR, "No more IO windows\n");
111 return;
112 }
113 lgir_reg_offset = LPC_GENERIC_IO_RANGE(lgir_reg_num);
114
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700115 pci_write_config32(PCH_DEV_LPC, lgir_reg_offset, lgir);
116
117 printk(BIOS_DEBUG,
118 "LPC: Opened IO window LGIR%d: base %llx size %x\n",
119 lgir_reg_num, bridge_base, window_size);
120
121 bridged_size += window_size;
122 bridge_base += window_size;
123 }
124}
125
126void lpc_open_mmio_window(uintptr_t base, size_t size)
127{
128 uint32_t lgmr;
129
130 lgmr = pci_read_config32(PCH_DEV_LPC, LPC_GENERIC_MEM_RANGE);
131
132 if (lgmr & LPC_LGMR_EN) {
133 printk(BIOS_ERR,
134 "LPC: Cannot open window to resource %lx size %zx\n",
135 base, size);
136 printk(BIOS_ERR, "LPC: MMIO window already in use\n");
137 return;
138 }
139
140 if (size > LPC_LGMR_WINDOW_SIZE) {
141 printk(BIOS_WARNING,
142 "LPC: Resource %lx size %zx larger than window(%x)\n",
143 base, size, LPC_LGMR_WINDOW_SIZE);
144 }
145
146 lgmr = (base & LPC_LGMR_ADDR_MASK) | LPC_LGMR_EN;
147
148 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_MEM_RANGE, lgmr);
149}
150
151bool lpc_fits_fixed_mmio_window(uintptr_t base, size_t size)
152{
153 resource_t res_end, range_end;
154 const struct lpc_mmio_range *range;
155 const struct lpc_mmio_range *lpc_fixed_mmio_ranges =
156 soc_get_fixed_mmio_ranges();
157
158 for (range = lpc_fixed_mmio_ranges; range->size; range++) {
159 range_end = range->base + range->size;
160 res_end = base + size;
161
162 if ((base >= range->base) && (res_end <= range_end)) {
163 printk(BIOS_DEBUG,
164 "Resource %lx size %zx fits in fixed window"
165 " %lx size %zx\n",
166 base, size, range->base, range->size);
167 return true;
168 }
169 }
170 return false;
171}
172
173/*
174 * Set FAST_SPIBAR BIOS Control register based on input bit field.
175 */
176static void lpc_set_bios_control_reg(uint8_t bios_cntl_bit)
177{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200178 pci_devfn_t dev = PCH_DEV_LPC;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700179 uint8_t bc_cntl;
180
Jonathan Neuschäfer3a182f72017-09-23 17:09:36 +0200181 assert(IS_POWER_OF_2(bios_cntl_bit));
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700182 bc_cntl = pci_read_config8(dev, LPC_BIOS_CNTL);
183 bc_cntl |= bios_cntl_bit;
184 pci_write_config8(dev, LPC_BIOS_CNTL, bc_cntl);
185
186 /*
187 * Ensure an additional read back after performing lock down
188 */
189 pci_read_config8(PCH_DEV_LPC, LPC_BIOS_CNTL);
190}
191
192/*
193* Set LPC BIOS Control BILD bit.
194*/
195void lpc_set_bios_interface_lock_down(void)
196{
197 lpc_set_bios_control_reg(LPC_BC_BILD);
198}
199
200/*
201* Set LPC BIOS Control LE bit.
202*/
203void lpc_set_lock_enable(void)
204{
205 lpc_set_bios_control_reg(LPC_BC_LE);
206}
207
208/*
209* Set LPC BIOS Control EISS bit.
210*/
211void lpc_set_eiss(void)
212{
213 lpc_set_bios_control_reg(LPC_BC_EISS);
214}
215
216/*
217* Set LPC Serial IRQ mode.
218*/
219void lpc_set_serirq_mode(enum serirq_mode mode)
220{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200221 pci_devfn_t dev = PCH_DEV_LPC;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700222 uint8_t scnt;
223
224 scnt = pci_read_config8(dev, LPC_SERIRQ_CTL);
225 scnt &= ~(LPC_SCNT_EN | LPC_SCNT_MODE);
226
227 switch (mode) {
228 case SERIRQ_QUIET:
229 scnt |= LPC_SCNT_EN;
230 break;
231 case SERIRQ_CONTINUOUS:
232 scnt |= LPC_SCNT_EN | LPC_SCNT_MODE;
233 break;
234 case SERIRQ_OFF:
235 default:
236 break;
237 }
238
239 pci_write_config8(dev, LPC_SERIRQ_CTL, scnt);
240}
241
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700242void lpc_io_setup_comm_a_b(void)
243{
Subrata Banikd83face2018-03-08 14:04:52 +0530244 /* ComA Range 3F8h-3FFh [2:0] */
245 uint16_t com_ranges = LPC_IOD_COMA_RANGE;
246 uint16_t com_enable = LPC_IOE_COMA_EN;
247
248 /* ComB Range 2F8h-2FFh [6:4] */
Julius Wernercd49cce2019-03-05 16:53:33 -0800249 if (CONFIG(SOC_INTEL_COMMON_BLOCK_LPC_COMB_ENABLE)) {
Subrata Banikd83face2018-03-08 14:04:52 +0530250 com_ranges |= LPC_IOD_COMB_RANGE;
251 com_enable |= LPC_IOE_COMB_EN;
252 }
253
254 /* Setup I/O Decode Range Register for LPC */
255 pci_write_config16(PCH_DEV_LPC, LPC_IO_DECODE, com_ranges);
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700256 /* Enable ComA and ComB Port */
Subrata Banikd83face2018-03-08 14:04:52 +0530257 lpc_enable_fixed_io_ranges(com_enable);
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700258}
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700259
260static void lpc_set_gen_decode_range(
261 uint32_t gen_io_dec[LPC_NUM_GENERIC_IO_RANGES])
262{
263 size_t i;
264
265 /* Set in PCI generic decode range registers */
266 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++)
267 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i),
268 gen_io_dec[i]);
269}
270
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700271void pch_enable_lpc(void)
272{
273 /* Lookup device tree in romstage */
274 const struct device *dev;
275 uint32_t gen_io_dec[LPC_NUM_GENERIC_IO_RANGES];
276
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300277 dev = pcidev_on_root(PCH_DEV_SLOT_LPC, 0);
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300278 if (!dev)
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700279 return;
280
281 soc_get_gen_io_dec_range(dev, gen_io_dec);
282 lpc_set_gen_decode_range(gen_io_dec);
283 soc_setup_dmi_pcr_io_dec(gen_io_dec);
Subrata Banik42c44c22019-05-15 20:27:04 +0530284 if (ENV_PAYLOAD_LOADER)
Subrata Banik78463a72020-09-29 14:28:09 +0530285 pch_pirq_init();
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700286}
287
288void lpc_enable_pci_clk_cntl(void)
289{
290 pci_write_config8(PCH_DEV_LPC, LPC_PCCTL, LPC_PCCTL_CLKRUN_EN);
291}
Nico Huberdbcf2932018-11-28 15:29:00 +0100292
293void lpc_disable_clkrun(void)
294{
295 const uint8_t pcctl = pci_read_config8(PCH_DEV_LPC, LPC_PCCTL);
296 pci_write_config8(PCH_DEV_LPC, LPC_PCCTL, pcctl & ~LPC_PCCTL_CLKRUN_EN);
297}
Subrata Banik1366e442020-09-29 13:55:50 +0530298
299/* Enable PCH IOAPIC */
300void pch_enable_ioapic(void)
301{
302 uint32_t reg32;
303 /* PCH-LP has 120 redirection entries */
304 const int redir_entries = 120;
305
306 set_ioapic_id((void *)IO_APIC_ADDR, 0x02);
307
308 /* affirm full set of redirection table entries ("write once") */
309 reg32 = io_apic_read((void *)IO_APIC_ADDR, 0x01);
310
311 reg32 &= ~0x00ff0000;
312 reg32 |= (redir_entries - 1) << 16;
313
314 io_apic_write((void *)IO_APIC_ADDR, 0x01, reg32);
315
316 /*
317 * Select Boot Configuration register (0x03) and
318 * use Processor System Bus (0x01) to deliver interrupts.
319 */
320 io_apic_write((void *)IO_APIC_ADDR, 0x03, 0x01);
321}
Subrata Banik78463a72020-09-29 14:28:09 +0530322
323/*
324 * PIRQ[n]_ROUT[3:0] - PIRQ Routing Control
325 * 0x00 - 0000 = Reserved
326 * 0x01 - 0001 = Reserved
327 * 0x02 - 0010 = Reserved
328 * 0x03 - 0011 = IRQ3
329 * 0x04 - 0100 = IRQ4
330 * 0x05 - 0101 = IRQ5
331 * 0x06 - 0110 = IRQ6
332 * 0x07 - 0111 = IRQ7
333 * 0x08 - 1000 = Reserved
334 * 0x09 - 1001 = IRQ9
335 * 0x0A - 1010 = IRQ10
336 * 0x0B - 1011 = IRQ11
337 * 0x0C - 1100 = IRQ12
338 * 0x0D - 1101 = Reserved
339 * 0x0E - 1110 = IRQ14
340 * 0x0F - 1111 = IRQ15
341 * PIRQ[n]_ROUT[7] - PIRQ Routing Control
342 * 0x80 - The PIRQ is not routed.
343 */
344void pch_pirq_init(void)
345{
346 const struct device *irq_dev;
347 uint8_t pch_interrupt_routing[MAX_PXRC_CONFIG];
348
349 pch_interrupt_routing[0] = PCH_IRQ11;
350 pch_interrupt_routing[1] = PCH_IRQ10;
351 pch_interrupt_routing[2] = PCH_IRQ11;
352 pch_interrupt_routing[3] = PCH_IRQ11;
353 pch_interrupt_routing[4] = PCH_IRQ11;
354 pch_interrupt_routing[5] = PCH_IRQ11;
355 pch_interrupt_routing[6] = PCH_IRQ11;
356 pch_interrupt_routing[7] = PCH_IRQ11;
357
358 itss_irq_init(pch_interrupt_routing);
359
360 for (irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
361 uint8_t int_pin = 0, int_line = 0;
362
363 if (!irq_dev->enabled || irq_dev->path.type != DEVICE_PATH_PCI)
364 continue;
365
366 int_pin = pci_read_config8(PCI_BDF(irq_dev), PCI_INTERRUPT_PIN);
367
368 switch (int_pin) {
369 case 1: /* INTA# */
370 int_line = PCH_IRQ11;
371 break;
372 case 2: /* INTB# */
373 int_line = PCH_IRQ10;
374 break;
375 case 3: /* INTC# */
376 int_line = PCH_IRQ11;
377 break;
378 case 4: /* INTD# */
379 int_line = PCH_IRQ11;
380 break;
381 }
382
383 if (!int_line)
384 continue;
385
386 pci_write_config8(PCI_BDF(irq_dev), PCI_INTERRUPT_LINE, int_line);
387 }
388}
Subrata Banik8971ccd2020-09-29 14:36:40 +0530389
390#define PPI_PORT_B 0x61
391#define SERR_DIS (1 << 2)
392#define CMOS_NMI 0x70
393#define NMI_DIS (1 << 7)
394
395/* LPC MISC programming */
396void pch_misc_init(void)
397{
398 uint8_t reg8;
399
400 /* Setup NMI on errors, disable SERR */
401 reg8 = (inb(PPI_PORT_B)) & 0xf0;
402 outb((reg8 | SERR_DIS), PPI_PORT_B);
403
404 /* Disable NMI sources */
405 outb(NMI_DIS, CMOS_NMI);
406}