blob: bc6bc6b2003087e1547c92f453d0eeea85ef2753 [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
Duncan Lauriec88c54c2014-04-30 16:36:13 -07004 *
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.
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.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070013 */
14
15#include <console/console.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070016#include <device/device.h>
17#include <device/pci.h>
18#include <device/pci_ids.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020019#include <option.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070020#include <pc80/isa-dma.h>
21#include <pc80/i8259.h>
22#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020023#include <device/pci_ops.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070024#include <arch/ioapic.h>
25#include <arch/acpi.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070026#include <cpu/x86/smm.h>
27#include <cbmem.h>
28#include <reg_script.h>
29#include <string.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070030#include <soc/gpio.h>
31#include <soc/iobp.h>
32#include <soc/iomap.h>
33#include <soc/lpc.h>
34#include <soc/nvs.h>
35#include <soc/pch.h>
36#include <soc/pci_devs.h>
37#include <soc/pm.h>
38#include <soc/ramstage.h>
39#include <soc/rcba.h>
40#include <soc/intel/broadwell/chip.h>
Vladimir Serbinenkob219da82014-11-09 03:29:30 +010041#include <arch/acpigen.h>
Arthur Heymans2abbe462019-06-04 14:12:01 +020042#include <southbridge/intel/common/rtc.h>
Duncan Laurie35dc00f2015-01-18 14:06:42 -080043
Duncan Lauriec88c54c2014-04-30 16:36:13 -070044static void pch_enable_ioapic(struct device *dev)
45{
46 u32 reg32;
47
Matt DeVillier81a6f102018-02-19 17:33:48 -060048 /* Assign unique bus/dev/fn for I/O APIC */
49 pci_write_config16(dev, LPC_IBDF,
50 PCH_IOAPIC_PCI_BUS << 8 | PCH_IOAPIC_PCI_SLOT << 3);
51
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080052 set_ioapic_id(VIO_APIC_VADDR, 0x02);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070053
54 /* affirm full set of redirection table entries ("write once") */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080055 reg32 = io_apic_read(VIO_APIC_VADDR, 0x01);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070056
57 /* PCH-LP has 39 redirection entries */
58 reg32 &= ~0x00ff0000;
59 reg32 |= 0x00270000;
60
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080061 io_apic_write(VIO_APIC_VADDR, 0x01, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070062
63 /*
64 * Select Boot Configuration register (0x03) and
65 * use Processor System Bus (0x01) to deliver interrupts.
66 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080067 io_apic_write(VIO_APIC_VADDR, 0x03, 0x01);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070068}
69
Matt DeVillier81a6f102018-02-19 17:33:48 -060070static void enable_hpet(struct device *dev)
71{
72 size_t i;
73
74 /* Assign unique bus/dev/fn for each HPET */
75 for (i = 0; i < 8; ++i)
76 pci_write_config16(dev, LPC_HnBDF(i),
77 PCH_HPET_PCI_BUS << 8 | PCH_HPET_PCI_SLOT << 3 | i);
78}
79
Duncan Lauriec88c54c2014-04-30 16:36:13 -070080/* PIRQ[n]_ROUT[3:0] - PIRQ Routing Control
81 * 0x00 - 0000 = Reserved
82 * 0x01 - 0001 = Reserved
83 * 0x02 - 0010 = Reserved
84 * 0x03 - 0011 = IRQ3
85 * 0x04 - 0100 = IRQ4
86 * 0x05 - 0101 = IRQ5
87 * 0x06 - 0110 = IRQ6
88 * 0x07 - 0111 = IRQ7
89 * 0x08 - 1000 = Reserved
90 * 0x09 - 1001 = IRQ9
91 * 0x0A - 1010 = IRQ10
92 * 0x0B - 1011 = IRQ11
93 * 0x0C - 1100 = IRQ12
94 * 0x0D - 1101 = Reserved
95 * 0x0E - 1110 = IRQ14
96 * 0x0F - 1111 = IRQ15
97 * PIRQ[n]_ROUT[7] - PIRQ Routing Control
98 * 0x80 - The PIRQ is not routed.
99 */
100
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200101static void pch_pirq_init(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700102{
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200103 struct device *irq_dev;
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300104 config_t *config = config_of(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700105
106 pci_write_config8(dev, PIRQA_ROUT, config->pirqa_routing);
107 pci_write_config8(dev, PIRQB_ROUT, config->pirqb_routing);
108 pci_write_config8(dev, PIRQC_ROUT, config->pirqc_routing);
109 pci_write_config8(dev, PIRQD_ROUT, config->pirqd_routing);
110
111 pci_write_config8(dev, PIRQE_ROUT, config->pirqe_routing);
112 pci_write_config8(dev, PIRQF_ROUT, config->pirqf_routing);
113 pci_write_config8(dev, PIRQG_ROUT, config->pirqg_routing);
114 pci_write_config8(dev, PIRQH_ROUT, config->pirqh_routing);
115
Elyes HAOUAS4a83f1c2016-08-25 21:07:59 +0200116 for (irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
Lee Leahy26b7cd02017-03-16 18:47:55 -0700117 u8 int_pin = 0, int_line = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700118
119 if (!irq_dev->enabled || irq_dev->path.type != DEVICE_PATH_PCI)
120 continue;
121
122 int_pin = pci_read_config8(irq_dev, PCI_INTERRUPT_PIN);
123
124 switch (int_pin) {
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700125 case 1: /* INTA# */
126 int_line = config->pirqa_routing;
127 break;
128 case 2: /* INTB# */
129 int_line = config->pirqb_routing;
130 break;
131 case 3: /* INTC# */
132 int_line = config->pirqc_routing;
133 break;
134 case 4: /* INTD# */
135 int_line = config->pirqd_routing;
136 break;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700137 }
138
139 if (!int_line)
140 continue;
141
142 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, int_line);
143 }
144}
145
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200146static void pch_power_options(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700147{
148 u16 reg16;
149 const char *state;
150 /* Get the chip configuration */
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300151 config_t *config = config_of(dev);
Nico Huber9faae2b2018-11-14 00:00:35 +0100152 int pwr_on = CONFIG_MAINBOARD_POWER_FAILURE_STATE;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700153
154 /* Which state do we want to goto after g3 (power restored)?
155 * 0 == S0 Full On
156 * 1 == S5 Soft Off
157 *
158 * If the option is not existent (Laptops), use Kconfig setting.
159 */
160 get_option(&pwr_on, "power_on_after_fail");
161
162 reg16 = pci_read_config16(dev, GEN_PMCON_3);
163 reg16 &= 0xfffe;
164 switch (pwr_on) {
165 case MAINBOARD_POWER_OFF:
166 reg16 |= 1;
167 state = "off";
168 break;
169 case MAINBOARD_POWER_ON:
170 reg16 &= ~1;
171 state = "on";
172 break;
173 case MAINBOARD_POWER_KEEP:
174 reg16 &= ~1;
175 state = "state keep";
176 break;
177 default:
178 state = "undefined";
179 }
180 pci_write_config16(dev, GEN_PMCON_3, reg16);
181 printk(BIOS_INFO, "Set power %s after power failure.\n", state);
182
183 /* GPE setup based on device tree configuration */
184 enable_all_gpe(config->gpe0_en_1, config->gpe0_en_2,
185 config->gpe0_en_3, config->gpe0_en_4);
186
187 /* SMI setup based on device tree configuration */
188 enable_alt_smi(config->alt_gp_smi_en);
189}
190
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700191static const struct reg_script pch_misc_init_script[] = {
192 /* Setup SLP signal assertion, SLP_S4=4s, SLP_S3=50ms */
193 REG_PCI_RMW16(GEN_PMCON_3, ~((3 << 4)|(1 << 10)),
194 (1 << 3)|(1 << 11)|(1 << 12)),
195 /* Prepare sleep mode */
196 REG_IO_RMW32(ACPI_BASE_ADDRESS + PM1_CNT, ~SLP_TYP, SCI_EN),
197 /* Setup NMI on errors, disable SERR */
198 REG_IO_RMW8(0x61, ~0xf0, (1 << 2)),
199 /* Disable NMI sources */
200 REG_IO_OR8(0x70, (1 << 7)),
201 /* Indicate DRAM init done for MRC */
202 REG_PCI_OR8(GEN_PMCON_2, (1 << 7)),
203 /* Enable BIOS updates outside of SMM */
204 REG_PCI_RMW8(0xdc, ~(1 << 5), 0),
205 /* Clear status bits to prevent unexpected wake */
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700206 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x3310, 0x0000002f),
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700207 REG_MMIO_RMW32(RCBA_BASE_ADDRESS + 0x3f02, ~0x0000000f, 0),
Kenji Chen074a0282014-09-20 01:39:20 +0800208 /* Enable PCIe Releaxed Order */
209 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x2314, (1 << 31) | (1 << 7)),
210 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x1114, (1 << 15) | (1 << 14)),
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700211 /* Setup SERIRQ, enable continuous mode */
212 REG_PCI_OR8(SERIRQ_CNTL, (1 << 7) | (1 << 6)),
Julius Wernercd49cce2019-03-05 16:53:33 -0800213#if !CONFIG(SERIRQ_CONTINUOUS_MODE)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700214 REG_PCI_RMW8(SERIRQ_CNTL, ~(1 << 6), 0),
215#endif
216 REG_SCRIPT_END
217};
218
219/* Magic register settings for power management */
220static const struct reg_script pch_pm_init_script[] = {
221 REG_PCI_WRITE8(0xa9, 0x46),
222 REG_MMIO_RMW32(RCBA_BASE_ADDRESS + 0x232c, ~1, 0),
223 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x1100, 0x0000c13f),
224 REG_MMIO_RMW32(RCBA_BASE_ADDRESS + 0x2320, ~0x60, 0x10),
225 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3314, 0x00012fff),
226 REG_MMIO_RMW32(RCBA_BASE_ADDRESS + 0x3318, ~0x000f0330, 0x0dcf0400),
227 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3324, 0x04000000),
228 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3368, 0x00041400),
229 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3388, 0x3f8ddbff),
230 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x33ac, 0x00007001),
231 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x33b0, 0x00181900),
232 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x33c0, 0x00060A00),
233 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x33d0, 0x06200840),
234 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3a28, 0x01010101),
235 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3a2c, 0x040c0404),
236 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3a9c, 0x9000000a),
237 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x2b1c, 0x03808033),
238 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x2b34, 0x80000009),
239 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3348, 0x022ddfff),
240 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x334c, 0x00000001),
241 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3358, 0x0001c000),
242 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3380, 0x3f8ddbff),
243 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3384, 0x0001c7e1),
244 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x338c, 0x0001c7e1),
245 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3398, 0x0001c000),
246 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x33a8, 0x00181900),
247 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x33dc, 0x00080000),
248 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x33e0, 0x00000001),
249 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3a20, 0x0000040c),
250 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3a24, 0x01010101),
251 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3a30, 0x01010101),
252 REG_PCI_RMW32(0xac, ~0x00200000, 0),
253 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x0410, 0x00000003),
254 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x2618, 0x08000000),
255 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x2300, 0x00000002),
256 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x2600, 0x00000008),
257 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x33b4, 0x00007001),
258 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3350, 0x022ddfff),
259 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3354, 0x00000001),
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700260 /* Power Optimizer */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700261 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x33d4, 0x08000000),
Matt DeVillierc97e0422017-02-16 11:36:16 -0600262 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x33c8, 0x00000080),
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700263 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x2b10, 0x0000883c),
264 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x2b14, 0x1e0a4616),
265 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x2b24, 0x40000005),
266 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x2b20, 0x0005db01),
267 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3a80, 0x05145005),
268 REG_MMIO_WRITE32(RCBA_BASE_ADDRESS + 0x3a84, 0x00001005),
269 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x33d4, 0x2fff2fb1),
270 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x33c8, 0x00008000),
271 REG_SCRIPT_END
272};
273
274static void pch_enable_mphy(void)
275{
276 u32 gpio71_native = gpio_is_native(71);
277 u32 data_and = 0xffffffff;
278 u32 data_or = (1 << 14) | (1 << 13) | (1 << 12);
279
280 if (gpio71_native) {
281 data_or |= (1 << 0);
282 if (pch_is_wpt()) {
283 data_and &= ~((1 << 7) | (1 << 6) | (1 << 3));
284 data_or |= (1 << 5) | (1 << 4);
285
286 if (pch_is_wpt_ulx()) {
287 /* Check if SATA and USB3 MPHY are enabled */
288 u32 strap19 = pch_read_soft_strap(19);
289 strap19 &= ((1 << 31) | (1 << 30));
290 strap19 >>= 30;
291 if (strap19 == 3) {
292 data_or |= (1 << 3);
293 printk(BIOS_DEBUG, "Enable ULX MPHY PG "
294 "control in single domain\n");
295 } else if (strap19 == 0) {
296 printk(BIOS_DEBUG, "Enable ULX MPHY PG "
297 "control in split domains\n");
298 } else {
299 printk(BIOS_DEBUG, "Invalid PCH Soft "
300 "Strap 19 configuration\n");
301 }
302 } else {
303 data_or |= (1 << 3);
304 }
305 }
306 }
307
308 pch_iobp_update(0xCF000000, data_and, data_or);
309}
310
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700311static void pch_init_deep_sx(struct device *dev)
312{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300313 config_t *config = config_of(dev);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700314
315 if (config->deep_sx_enable_ac) {
316 RCBA32_OR(DEEP_S3_POL, DEEP_S3_EN_AC);
317 RCBA32_OR(DEEP_S5_POL, DEEP_S5_EN_AC);
318 }
319
320 if (config->deep_sx_enable_dc) {
321 RCBA32_OR(DEEP_S3_POL, DEEP_S3_EN_DC);
322 RCBA32_OR(DEEP_S5_POL, DEEP_S5_EN_DC);
323 }
324
325 if (config->deep_sx_enable_ac || config->deep_sx_enable_dc)
326 RCBA32_OR(DEEP_SX_CONFIG,
327 DEEP_SX_WAKE_PIN_EN | DEEP_SX_GP27_PIN_EN);
328}
329
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700330/* Power Management init */
331static void pch_pm_init(struct device *dev)
332{
333 printk(BIOS_DEBUG, "PCH PM init\n");
334
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700335 pch_init_deep_sx(dev);
336
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700337 pch_enable_mphy();
338
339 reg_script_run_on_dev(dev, pch_pm_init_script);
340
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700341 if (pch_is_wpt()) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700342 RCBA32_OR(0x33e0, (1 << 4) | (1 << 1));
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700343 RCBA32_OR(0x2b1c, (1 << 22) | (1 << 14) | (1 << 13));
344 RCBA32(0x33e4) = 0x16bf0002;
345 RCBA32_OR(0x33e4, 0x1);
346 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700347
348 pch_iobp_update(0xCA000000, ~0UL, 0x00000009);
349
350 /* Set RCBA 0x2b1c[29]=1 if DSP disabled */
351 if (RCBA32(FD) & PCH_DISABLE_ADSPD)
352 RCBA32_OR(0x2b1c, (1 << 29));
353
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700354}
355
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200356static void pch_cg_init(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700357{
358 u32 reg32;
359 u16 reg16;
Kyösti Mälkki71756c212019-07-12 13:10:19 +0300360 struct device *igd_dev = pcidev_path_on_root(SA_DEVFN_IGD);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700361
362 /* DMI */
363 RCBA32_OR(0x2234, 0xf);
364
365 reg16 = pci_read_config16(dev, GEN_PMCON_1);
366 reg16 &= ~(1 << 10); /* Disable BIOS_PCI_EXP_EN for native PME */
367 if (pch_is_wpt())
368 reg16 &= ~(1 << 11);
369 else
370 reg16 |= (1 << 11);
371 reg16 |= (1 << 5) | (1 << 6) | (1 << 7) | (1 << 12);
372 reg16 |= (1 << 2); // PCI CLKRUN# Enable
373 pci_write_config16(dev, GEN_PMCON_1, reg16);
374
375 /*
376 * RCBA + 0x2614[27:25,14:13,10,8] = 101,11,1,1
377 * RCBA + 0x2614[23:16] = 0x20
378 * RCBA + 0x2614[30:28] = 0x0
379 * RCBA + 0x2614[26] = 1 (IF 0:2.0@0x08 >= 0x0b)
380 */
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700381 RCBA32_AND_OR(0x2614, ~0x64ff0000, 0x0a206500);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700382
383 /* Check for 0:2.0@0x08 >= 0x0b */
Kyösti Mälkki71756c212019-07-12 13:10:19 +0300384 if (pch_is_wpt() || pci_read_config8(igd_dev, 0x8) >= 0x0b)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700385 RCBA32_OR(0x2614, (1 << 26));
386
387 RCBA32_OR(0x900, 0x0000031f);
388
389 reg32 = RCBA32(CG);
390 if (RCBA32(0x3454) & (1 << 4))
391 reg32 &= ~(1 << 29); // LPC Dynamic
392 else
393 reg32 |= (1 << 29); // LPC Dynamic
394 reg32 |= (1 << 31); // LP LPC
395 reg32 |= (1 << 30); // LP BLA
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700396 if (RCBA32(0x3454) & (1 << 4))
397 reg32 &= ~(1 << 29);
398 else
399 reg32 |= (1 << 29);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700400 reg32 |= (1 << 28); // GPIO Dynamic
401 reg32 |= (1 << 27); // HPET Dynamic
402 reg32 |= (1 << 26); // Generic Platform Event Clock
403 if (RCBA32(BUC) & PCH_DISABLE_GBE)
404 reg32 |= (1 << 23); // GbE Static
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700405 if (RCBA32(FD) & PCH_DISABLE_HD_AUDIO)
406 reg32 |= (1 << 21); // HDA Static
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700407 reg32 |= (1 << 22); // HDA Dynamic
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700408 RCBA32(CG) = reg32;
409
410 /* PCH-LP LPC */
411 if (pch_is_wpt())
412 RCBA32_AND_OR(0x3434, ~0x1f, 0x17);
413 else
414 RCBA32_OR(0x3434, 0x7);
415
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700416 /* SPI */
417 RCBA32_OR(0x38c0, 0x3c07);
418
419 pch_iobp_update(0xCE00C000, ~1UL, 0x00000000);
420}
421
422static void pch_set_acpi_mode(void)
423{
Kyösti Mälkkib4905622019-07-12 08:02:35 +0300424 if (CONFIG(HAVE_SMI_HANDLER) && !acpi_is_wakeup_s3()) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700425 printk(BIOS_DEBUG, "Disabling ACPI via APMC:\n");
426 outb(APM_CNT_ACPI_DISABLE, APM_CNT);
427 printk(BIOS_DEBUG, "done.\n");
428 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700429}
430
431static void lpc_init(struct device *dev)
432{
433 /* Legacy initialization */
434 isa_dma_init();
Arthur Heymans2abbe462019-06-04 14:12:01 +0200435 sb_rtc_init();
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700436 reg_script_run_on_dev(dev, pch_misc_init_script);
437
438 /* Interrupt configuration */
439 pch_enable_ioapic(dev);
440 pch_pirq_init(dev);
441 setup_i8259();
442 i8259_configure_irq_trigger(9, 1);
Matt DeVillier81a6f102018-02-19 17:33:48 -0600443 enable_hpet(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700444
445 /* Initialize power management */
446 pch_power_options(dev);
447 pch_pm_init(dev);
448 pch_cg_init(dev);
449
450 pch_set_acpi_mode();
451}
452
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200453static void pch_lpc_add_mmio_resources(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700454{
455 u32 reg;
456 struct resource *res;
457 const u32 default_decode_base = IO_APIC_ADDR;
458
459 /*
460 * Just report all resources from IO-APIC base to 4GiB. Don't mark
461 * them reserved as that may upset the OS if this range is marked
462 * as reserved in the e820.
463 */
464 res = new_resource(dev, OIC);
465 res->base = default_decode_base;
466 res->size = 0 - default_decode_base;
467 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
468
469 /* RCBA */
Lee Leahy6ef51922017-03-17 10:56:08 -0700470 if (default_decode_base > RCBA_BASE_ADDRESS) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700471 res = new_resource(dev, RCBA);
472 res->base = RCBA_BASE_ADDRESS;
473 res->size = 16 * 1024;
474 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED |
Lee Leahy26b7cd02017-03-16 18:47:55 -0700475 IORESOURCE_FIXED | IORESOURCE_RESERVE;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700476 }
477
478 /* Check LPC Memory Decode register. */
479 reg = pci_read_config32(dev, LGMR);
480 if (reg & 1) {
481 reg &= ~0xffff;
482 if (reg < default_decode_base) {
483 res = new_resource(dev, LGMR);
484 res->base = reg;
485 res->size = 16 * 1024;
486 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED |
Lee Leahy26b7cd02017-03-16 18:47:55 -0700487 IORESOURCE_FIXED | IORESOURCE_RESERVE;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700488 }
489 }
490}
491
492/* Default IO range claimed by the LPC device. The upper bound is exclusive. */
493#define LPC_DEFAULT_IO_RANGE_LOWER 0
494#define LPC_DEFAULT_IO_RANGE_UPPER 0x1000
495
Julius Werner7c712bb2019-05-01 16:51:20 -0700496static inline int pch_io_range_in_default(int base, int size)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700497{
498 /* Does it start above the range? */
499 if (base >= LPC_DEFAULT_IO_RANGE_UPPER)
500 return 0;
501
502 /* Is it entirely contained? */
503 if (base >= LPC_DEFAULT_IO_RANGE_LOWER &&
504 (base + size) < LPC_DEFAULT_IO_RANGE_UPPER)
505 return 1;
506
507 /* This will return not in range for partial overlaps. */
508 return 0;
509}
510
511/*
512 * Note: this function assumes there is no overlap with the default LPC device's
513 * claimed range: LPC_DEFAULT_IO_RANGE_LOWER -> LPC_DEFAULT_IO_RANGE_UPPER.
514 */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200515static void pch_lpc_add_io_resource(struct device *dev, u16 base, u16 size,
516 int index)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700517{
518 struct resource *res;
519
520 if (pch_io_range_in_default(base, size))
521 return;
522
523 res = new_resource(dev, index);
524 res->base = base;
525 res->size = size;
526 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
527}
528
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200529static void pch_lpc_add_gen_io_resources(struct device *dev, int reg_value,
530 int index)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700531{
532 /*
533 * Check if the register is enabled. If so and the base exceeds the
Martin Rothde7ed6f2014-12-07 14:58:18 -0700534 * device's default claim range add the resource.
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700535 */
536 if (reg_value & 1) {
537 u16 base = reg_value & 0xfffc;
538 u16 size = (0x3 | ((reg_value >> 16) & 0xfc)) + 1;
539 pch_lpc_add_io_resource(dev, base, size, index);
540 }
541}
542
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200543static void pch_lpc_add_io_resources(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700544{
545 struct resource *res;
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300546 config_t *config = config_of(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700547
548 /* Add the default claimed IO range for the LPC device. */
549 res = new_resource(dev, 0);
550 res->base = LPC_DEFAULT_IO_RANGE_LOWER;
551 res->size = LPC_DEFAULT_IO_RANGE_UPPER - LPC_DEFAULT_IO_RANGE_LOWER;
552 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
553
554 /* GPIOBASE */
555 pch_lpc_add_io_resource(dev, GPIO_BASE_ADDRESS,
556 GPIO_BASE_SIZE, GPIO_BASE);
557
558 /* PMBASE */
559 pch_lpc_add_io_resource(dev, ACPI_BASE_ADDRESS, ACPI_BASE_SIZE, PMBASE);
560
561 /* LPC Generic IO Decode range. */
562 pch_lpc_add_gen_io_resources(dev, config->gen1_dec, LPC_GEN1_DEC);
563 pch_lpc_add_gen_io_resources(dev, config->gen2_dec, LPC_GEN2_DEC);
564 pch_lpc_add_gen_io_resources(dev, config->gen3_dec, LPC_GEN3_DEC);
565 pch_lpc_add_gen_io_resources(dev, config->gen4_dec, LPC_GEN4_DEC);
566}
567
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200568static void pch_lpc_read_resources(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700569{
570 global_nvs_t *gnvs;
571
572 /* Get the normal PCI resources of this device. */
573 pci_dev_read_resources(dev);
574
575 /* Add non-standard MMIO resources. */
576 pch_lpc_add_mmio_resources(dev);
577
578 /* Add IO resources. */
579 pch_lpc_add_io_resources(dev);
580
581 /* Allocate ACPI NVS in CBMEM */
582 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(global_nvs_t));
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200583 if (!acpi_is_wakeup_s3() && gnvs)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700584 memset(gnvs, 0, sizeof(global_nvs_t));
585}
586
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200587static void southcluster_inject_dsdt(struct device *device)
Vladimir Serbinenkob219da82014-11-09 03:29:30 +0100588{
589 global_nvs_t *gnvs;
590
591 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
592 if (!gnvs) {
Lee Leahy26b7cd02017-03-16 18:47:55 -0700593 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(*gnvs));
Vladimir Serbinenkob219da82014-11-09 03:29:30 +0100594 if (gnvs)
595 memset(gnvs, 0, sizeof(*gnvs));
596 }
597
598 if (gnvs) {
Vladimir Serbinenkob219da82014-11-09 03:29:30 +0100599 acpi_create_gnvs(gnvs);
Vladimir Serbinenkob219da82014-11-09 03:29:30 +0100600 /* And tell SMI about it */
601 smm_setup_structures(gnvs, NULL, NULL);
602
603 /* Add it to DSDT. */
604 acpigen_write_scope("\\");
605 acpigen_write_name_dword("NVSA", (u32) gnvs);
606 acpigen_pop_len();
607 }
608}
609
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200610static unsigned long broadwell_write_acpi_tables(struct device *device,
Duncan Laurie93bbd412017-11-11 20:03:29 -0800611 unsigned long current,
612 struct acpi_rsdp *rsdp)
613{
Julius Wernercd49cce2019-03-05 16:53:33 -0800614 if (CONFIG(INTEL_PCH_UART_CONSOLE))
Duncan Laurie93bbd412017-11-11 20:03:29 -0800615 current = acpi_write_dbg2_pci_uart(rsdp, current,
616 (CONFIG_INTEL_PCH_UART_CONSOLE_NUMBER == 1) ?
617 PCH_DEV_UART1 : PCH_DEV_UART0,
618 ACPI_ACCESS_SIZE_BYTE_ACCESS);
619 return acpi_write_hpet(device, current, rsdp);
620}
621
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700622static struct device_operations device_ops = {
623 .read_resources = &pch_lpc_read_resources,
624 .set_resources = &pci_dev_set_resources,
625 .enable_resources = &pci_dev_enable_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200626 .acpi_inject_dsdt = southcluster_inject_dsdt,
Duncan Laurie93bbd412017-11-11 20:03:29 -0800627 .write_acpi_tables = broadwell_write_acpi_tables,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700628 .init = &lpc_init,
Nico Huber51b75ae2019-03-14 16:02:05 +0100629 .scan_bus = &scan_static_bus,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700630 .ops_pci = &broadwell_pci_ops,
631};
632
633static const unsigned short pci_device_ids[] = {
634 PCH_LPT_LP_SAMPLE,
635 PCH_LPT_LP_PREMIUM,
636 PCH_LPT_LP_MAINSTREAM,
637 PCH_LPT_LP_VALUE,
638 PCH_WPT_HSW_U_SAMPLE,
639 PCH_WPT_BDW_U_SAMPLE,
640 PCH_WPT_BDW_U_PREMIUM,
641 PCH_WPT_BDW_U_BASE,
642 PCH_WPT_BDW_Y_SAMPLE,
643 PCH_WPT_BDW_Y_PREMIUM,
644 PCH_WPT_BDW_Y_BASE,
645 PCH_WPT_BDW_H,
646 0
647};
648
649static const struct pci_driver pch_lpc __pci_driver = {
650 .ops = &device_ops,
651 .vendor = PCI_VENDOR_ID_INTEL,
652 .devices = pci_device_ids,
653};