blob: 495f1343b7be50a597b5430900c671c170514b13 [file] [log] [blame]
Angel Ponsf94ac9a2020-04-05 15:46:48 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Lauriec88c54c2014-04-30 16:36:13 -07002
3#include <console/console.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -07004#include <device/device.h>
5#include <device/pci.h>
6#include <device/pci_ids.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +02007#include <option.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -07008#include <pc80/isa-dma.h>
9#include <pc80/i8259.h>
10#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020011#include <device/pci_ops.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070012#include <arch/ioapic.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070013#include <acpi/acpi.h>
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +030014#include <acpi/acpi_gnvs.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070015#include <cpu/x86/smm.h>
16#include <cbmem.h>
17#include <reg_script.h>
18#include <string.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070019#include <soc/gpio.h>
20#include <soc/iobp.h>
21#include <soc/iomap.h>
22#include <soc/lpc.h>
23#include <soc/nvs.h>
24#include <soc/pch.h>
25#include <soc/pci_devs.h>
26#include <soc/pm.h>
27#include <soc/ramstage.h>
28#include <soc/rcba.h>
29#include <soc/intel/broadwell/chip.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070030#include <acpi/acpigen.h>
Arthur Heymans2abbe462019-06-04 14:12:01 +020031#include <southbridge/intel/common/rtc.h>
Duncan Laurie35dc00f2015-01-18 14:06:42 -080032
Duncan Lauriec88c54c2014-04-30 16:36:13 -070033static void pch_enable_ioapic(struct device *dev)
34{
35 u32 reg32;
36
Matt DeVillier81a6f102018-02-19 17:33:48 -060037 /* Assign unique bus/dev/fn for I/O APIC */
38 pci_write_config16(dev, LPC_IBDF,
39 PCH_IOAPIC_PCI_BUS << 8 | PCH_IOAPIC_PCI_SLOT << 3);
40
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080041 set_ioapic_id(VIO_APIC_VADDR, 0x02);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070042
43 /* affirm full set of redirection table entries ("write once") */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080044 reg32 = io_apic_read(VIO_APIC_VADDR, 0x01);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070045
46 /* PCH-LP has 39 redirection entries */
47 reg32 &= ~0x00ff0000;
48 reg32 |= 0x00270000;
49
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080050 io_apic_write(VIO_APIC_VADDR, 0x01, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070051
52 /*
53 * Select Boot Configuration register (0x03) and
54 * use Processor System Bus (0x01) to deliver interrupts.
55 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080056 io_apic_write(VIO_APIC_VADDR, 0x03, 0x01);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070057}
58
Matt DeVillier81a6f102018-02-19 17:33:48 -060059static void enable_hpet(struct device *dev)
60{
61 size_t i;
62
63 /* Assign unique bus/dev/fn for each HPET */
64 for (i = 0; i < 8; ++i)
65 pci_write_config16(dev, LPC_HnBDF(i),
66 PCH_HPET_PCI_BUS << 8 | PCH_HPET_PCI_SLOT << 3 | i);
67}
68
Duncan Lauriec88c54c2014-04-30 16:36:13 -070069/* PIRQ[n]_ROUT[3:0] - PIRQ Routing Control
70 * 0x00 - 0000 = Reserved
71 * 0x01 - 0001 = Reserved
72 * 0x02 - 0010 = Reserved
73 * 0x03 - 0011 = IRQ3
74 * 0x04 - 0100 = IRQ4
75 * 0x05 - 0101 = IRQ5
76 * 0x06 - 0110 = IRQ6
77 * 0x07 - 0111 = IRQ7
78 * 0x08 - 1000 = Reserved
79 * 0x09 - 1001 = IRQ9
80 * 0x0A - 1010 = IRQ10
81 * 0x0B - 1011 = IRQ11
82 * 0x0C - 1100 = IRQ12
83 * 0x0D - 1101 = Reserved
84 * 0x0E - 1110 = IRQ14
85 * 0x0F - 1111 = IRQ15
86 * PIRQ[n]_ROUT[7] - PIRQ Routing Control
87 * 0x80 - The PIRQ is not routed.
88 */
89
Elyes HAOUAS040aff22018-05-27 16:30:36 +020090static void pch_pirq_init(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070091{
Elyes HAOUAS040aff22018-05-27 16:30:36 +020092 struct device *irq_dev;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070093
Angel Pons4a6c0a32020-07-25 15:11:15 +020094 const uint8_t pirq = 0x80;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070095
Angel Pons4a6c0a32020-07-25 15:11:15 +020096 pci_write_config8(dev, PIRQA_ROUT, pirq);
97 pci_write_config8(dev, PIRQB_ROUT, pirq);
98 pci_write_config8(dev, PIRQC_ROUT, pirq);
99 pci_write_config8(dev, PIRQD_ROUT, pirq);
100
101 pci_write_config8(dev, PIRQE_ROUT, pirq);
102 pci_write_config8(dev, PIRQF_ROUT, pirq);
103 pci_write_config8(dev, PIRQG_ROUT, pirq);
104 pci_write_config8(dev, PIRQH_ROUT, pirq);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700105
Elyes HAOUAS4a83f1c2016-08-25 21:07:59 +0200106 for (irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
Lee Leahy26b7cd02017-03-16 18:47:55 -0700107 u8 int_pin = 0, int_line = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700108
109 if (!irq_dev->enabled || irq_dev->path.type != DEVICE_PATH_PCI)
110 continue;
111
112 int_pin = pci_read_config8(irq_dev, PCI_INTERRUPT_PIN);
113
114 switch (int_pin) {
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700115 case 1: /* INTA# */
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700116 case 2: /* INTB# */
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700117 case 3: /* INTC# */
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700118 case 4: /* INTD# */
Angel Pons4a6c0a32020-07-25 15:11:15 +0200119 int_line = pirq;
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700120 break;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700121 }
122
123 if (!int_line)
124 continue;
125
126 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, int_line);
127 }
128}
129
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200130static void pch_power_options(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700131{
132 u16 reg16;
133 const char *state;
134 /* Get the chip configuration */
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300135 config_t *config = config_of(dev);
Nico Huber9faae2b2018-11-14 00:00:35 +0100136 int pwr_on = CONFIG_MAINBOARD_POWER_FAILURE_STATE;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700137
138 /* Which state do we want to goto after g3 (power restored)?
139 * 0 == S0 Full On
140 * 1 == S5 Soft Off
141 *
142 * If the option is not existent (Laptops), use Kconfig setting.
143 */
144 get_option(&pwr_on, "power_on_after_fail");
145
146 reg16 = pci_read_config16(dev, GEN_PMCON_3);
147 reg16 &= 0xfffe;
148 switch (pwr_on) {
149 case MAINBOARD_POWER_OFF:
150 reg16 |= 1;
151 state = "off";
152 break;
153 case MAINBOARD_POWER_ON:
154 reg16 &= ~1;
155 state = "on";
156 break;
157 case MAINBOARD_POWER_KEEP:
158 reg16 &= ~1;
159 state = "state keep";
160 break;
161 default:
162 state = "undefined";
163 }
164 pci_write_config16(dev, GEN_PMCON_3, reg16);
165 printk(BIOS_INFO, "Set power %s after power failure.\n", state);
166
167 /* GPE setup based on device tree configuration */
168 enable_all_gpe(config->gpe0_en_1, config->gpe0_en_2,
169 config->gpe0_en_3, config->gpe0_en_4);
170
171 /* SMI setup based on device tree configuration */
172 enable_alt_smi(config->alt_gp_smi_en);
173}
174
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700175static const struct reg_script pch_misc_init_script[] = {
176 /* Setup SLP signal assertion, SLP_S4=4s, SLP_S3=50ms */
177 REG_PCI_RMW16(GEN_PMCON_3, ~((3 << 4)|(1 << 10)),
178 (1 << 3)|(1 << 11)|(1 << 12)),
179 /* Prepare sleep mode */
180 REG_IO_RMW32(ACPI_BASE_ADDRESS + PM1_CNT, ~SLP_TYP, SCI_EN),
181 /* Setup NMI on errors, disable SERR */
182 REG_IO_RMW8(0x61, ~0xf0, (1 << 2)),
183 /* Disable NMI sources */
184 REG_IO_OR8(0x70, (1 << 7)),
185 /* Indicate DRAM init done for MRC */
186 REG_PCI_OR8(GEN_PMCON_2, (1 << 7)),
187 /* Enable BIOS updates outside of SMM */
188 REG_PCI_RMW8(0xdc, ~(1 << 5), 0),
189 /* Clear status bits to prevent unexpected wake */
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700190 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x3310, 0x0000002f),
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700191 REG_MMIO_RMW32(RCBA_BASE_ADDRESS + 0x3f02, ~0x0000000f, 0),
Kenji Chen074a0282014-09-20 01:39:20 +0800192 /* Enable PCIe Releaxed Order */
193 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x2314, (1 << 31) | (1 << 7)),
194 REG_MMIO_OR32(RCBA_BASE_ADDRESS + 0x1114, (1 << 15) | (1 << 14)),
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700195 /* Setup SERIRQ, enable continuous mode */
196 REG_PCI_OR8(SERIRQ_CNTL, (1 << 7) | (1 << 6)),
Julius Wernercd49cce2019-03-05 16:53:33 -0800197#if !CONFIG(SERIRQ_CONTINUOUS_MODE)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700198 REG_PCI_RMW8(SERIRQ_CNTL, ~(1 << 6), 0),
199#endif
200 REG_SCRIPT_END
201};
202
203/* Magic register settings for power management */
Angel Pons2436ac02020-10-13 20:03:49 +0200204static void pch_pm_init_magic(struct device *dev)
205{
206 pci_write_config8(dev, 0xa9, 0x46);
207
208 RCBA32_AND_OR(0x232c, ~1, 0);
209
210 RCBA32_OR(0x1100, 0x0000c13f);
211
212 RCBA32_AND_OR(0x2320, ~0x60, 0x10);
213
214 RCBA32(0x3314) = 0x00012fff;
215
216 RCBA32_AND_OR(0x3318, ~0x000f0330, 0x0dcf0400);
217
218 RCBA32(0x3324) = 0x04000000;
219 RCBA32(0x3368) = 0x00041400;
220 RCBA32(0x3388) = 0x3f8ddbff;
221 RCBA32(0x33ac) = 0x00007001;
222 RCBA32(0x33b0) = 0x00181900;
223 RCBA32(0x33c0) = 0x00060A00;
224 RCBA32(0x33d0) = 0x06200840;
225 RCBA32(0x3a28) = 0x01010101;
226 RCBA32(0x3a2c) = 0x040c0404;
227 RCBA32(0x3a9c) = 0x9000000a;
228 RCBA32(0x2b1c) = 0x03808033;
229 RCBA32(0x2b34) = 0x80000009;
230 RCBA32(0x3348) = 0x022ddfff;
231 RCBA32(0x334c) = 0x00000001;
232 RCBA32(0x3358) = 0x0001c000;
233 RCBA32(0x3380) = 0x3f8ddbff;
234 RCBA32(0x3384) = 0x0001c7e1;
235 RCBA32(0x338c) = 0x0001c7e1;
236 RCBA32(0x3398) = 0x0001c000;
237 RCBA32(0x33a8) = 0x00181900;
238 RCBA32(0x33dc) = 0x00080000;
239 RCBA32(0x33e0) = 0x00000001;
240 RCBA32(0x3a20) = 0x0000040c;
241 RCBA32(0x3a24) = 0x01010101;
242 RCBA32(0x3a30) = 0x01010101;
243
244 pci_update_config32(dev, 0xac, ~0x00200000, 0);
245
246 RCBA32_OR(0x0410, 0x00000003);
247 RCBA32_OR(0x2618, 0x08000000);
248 RCBA32_OR(0x2300, 0x00000002);
249 RCBA32_OR(0x2600, 0x00000008);
250
251 RCBA32(0x33b4) = 0x00007001;
252 RCBA32(0x3350) = 0x022ddfff;
253 RCBA32(0x3354) = 0x00000001;
254
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700255 /* Power Optimizer */
Angel Pons2436ac02020-10-13 20:03:49 +0200256 RCBA32_OR(0x33d4, 0x08000000);
257 RCBA32_OR(0x33c8, 0x00000080);
258
259 RCBA32(0x2b10) = 0x0000883c;
260 RCBA32(0x2b14) = 0x1e0a4616;
261 RCBA32(0x2b24) = 0x40000005;
262 RCBA32(0x2b20) = 0x0005db01;
263 RCBA32(0x3a80) = 0x05145005;
264 RCBA32(0x3a84) = 0x00001005;
265
266 RCBA32_OR(0x33d4, 0x2fff2fb1);
267 RCBA32_OR(0x33c8, 0x00008000);
268}
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700269
270static void pch_enable_mphy(void)
271{
272 u32 gpio71_native = gpio_is_native(71);
273 u32 data_and = 0xffffffff;
274 u32 data_or = (1 << 14) | (1 << 13) | (1 << 12);
275
276 if (gpio71_native) {
277 data_or |= (1 << 0);
278 if (pch_is_wpt()) {
279 data_and &= ~((1 << 7) | (1 << 6) | (1 << 3));
280 data_or |= (1 << 5) | (1 << 4);
281
282 if (pch_is_wpt_ulx()) {
283 /* Check if SATA and USB3 MPHY are enabled */
284 u32 strap19 = pch_read_soft_strap(19);
285 strap19 &= ((1 << 31) | (1 << 30));
286 strap19 >>= 30;
287 if (strap19 == 3) {
288 data_or |= (1 << 3);
289 printk(BIOS_DEBUG, "Enable ULX MPHY PG "
290 "control in single domain\n");
291 } else if (strap19 == 0) {
292 printk(BIOS_DEBUG, "Enable ULX MPHY PG "
293 "control in split domains\n");
294 } else {
295 printk(BIOS_DEBUG, "Invalid PCH Soft "
296 "Strap 19 configuration\n");
297 }
298 } else {
299 data_or |= (1 << 3);
300 }
301 }
302 }
303
304 pch_iobp_update(0xCF000000, data_and, data_or);
305}
306
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700307static void pch_init_deep_sx(struct device *dev)
308{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300309 config_t *config = config_of(dev);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700310
311 if (config->deep_sx_enable_ac) {
312 RCBA32_OR(DEEP_S3_POL, DEEP_S3_EN_AC);
313 RCBA32_OR(DEEP_S5_POL, DEEP_S5_EN_AC);
314 }
315
316 if (config->deep_sx_enable_dc) {
317 RCBA32_OR(DEEP_S3_POL, DEEP_S3_EN_DC);
318 RCBA32_OR(DEEP_S5_POL, DEEP_S5_EN_DC);
319 }
320
321 if (config->deep_sx_enable_ac || config->deep_sx_enable_dc)
322 RCBA32_OR(DEEP_SX_CONFIG,
323 DEEP_SX_WAKE_PIN_EN | DEEP_SX_GP27_PIN_EN);
324}
325
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700326/* Power Management init */
327static void pch_pm_init(struct device *dev)
328{
329 printk(BIOS_DEBUG, "PCH PM init\n");
330
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700331 pch_init_deep_sx(dev);
332
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700333 pch_enable_mphy();
334
Angel Pons2436ac02020-10-13 20:03:49 +0200335 pch_pm_init_magic(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700336
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700337 if (pch_is_wpt()) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700338 RCBA32_OR(0x33e0, (1 << 4) | (1 << 1));
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700339 RCBA32_OR(0x2b1c, (1 << 22) | (1 << 14) | (1 << 13));
340 RCBA32(0x33e4) = 0x16bf0002;
341 RCBA32_OR(0x33e4, 0x1);
342 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700343
344 pch_iobp_update(0xCA000000, ~0UL, 0x00000009);
345
346 /* Set RCBA 0x2b1c[29]=1 if DSP disabled */
347 if (RCBA32(FD) & PCH_DISABLE_ADSPD)
348 RCBA32_OR(0x2b1c, (1 << 29));
349
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700350}
351
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200352static void pch_cg_init(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700353{
354 u32 reg32;
355 u16 reg16;
Kyösti Mälkki71756c212019-07-12 13:10:19 +0300356 struct device *igd_dev = pcidev_path_on_root(SA_DEVFN_IGD);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700357
358 /* DMI */
359 RCBA32_OR(0x2234, 0xf);
360
361 reg16 = pci_read_config16(dev, GEN_PMCON_1);
362 reg16 &= ~(1 << 10); /* Disable BIOS_PCI_EXP_EN for native PME */
363 if (pch_is_wpt())
364 reg16 &= ~(1 << 11);
365 else
366 reg16 |= (1 << 11);
367 reg16 |= (1 << 5) | (1 << 6) | (1 << 7) | (1 << 12);
368 reg16 |= (1 << 2); // PCI CLKRUN# Enable
369 pci_write_config16(dev, GEN_PMCON_1, reg16);
370
371 /*
372 * RCBA + 0x2614[27:25,14:13,10,8] = 101,11,1,1
373 * RCBA + 0x2614[23:16] = 0x20
374 * RCBA + 0x2614[30:28] = 0x0
375 * RCBA + 0x2614[26] = 1 (IF 0:2.0@0x08 >= 0x0b)
376 */
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700377 RCBA32_AND_OR(0x2614, ~0x64ff0000, 0x0a206500);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700378
379 /* Check for 0:2.0@0x08 >= 0x0b */
Kyösti Mälkki71756c212019-07-12 13:10:19 +0300380 if (pch_is_wpt() || pci_read_config8(igd_dev, 0x8) >= 0x0b)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700381 RCBA32_OR(0x2614, (1 << 26));
382
383 RCBA32_OR(0x900, 0x0000031f);
384
385 reg32 = RCBA32(CG);
386 if (RCBA32(0x3454) & (1 << 4))
387 reg32 &= ~(1 << 29); // LPC Dynamic
388 else
389 reg32 |= (1 << 29); // LPC Dynamic
390 reg32 |= (1 << 31); // LP LPC
391 reg32 |= (1 << 30); // LP BLA
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700392 if (RCBA32(0x3454) & (1 << 4))
393 reg32 &= ~(1 << 29);
394 else
395 reg32 |= (1 << 29);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700396 reg32 |= (1 << 28); // GPIO Dynamic
397 reg32 |= (1 << 27); // HPET Dynamic
398 reg32 |= (1 << 26); // Generic Platform Event Clock
399 if (RCBA32(BUC) & PCH_DISABLE_GBE)
400 reg32 |= (1 << 23); // GbE Static
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700401 if (RCBA32(FD) & PCH_DISABLE_HD_AUDIO)
402 reg32 |= (1 << 21); // HDA Static
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700403 reg32 |= (1 << 22); // HDA Dynamic
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700404 RCBA32(CG) = reg32;
405
406 /* PCH-LP LPC */
407 if (pch_is_wpt())
408 RCBA32_AND_OR(0x3434, ~0x1f, 0x17);
409 else
410 RCBA32_OR(0x3434, 0x7);
411
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700412 /* SPI */
413 RCBA32_OR(0x38c0, 0x3c07);
414
415 pch_iobp_update(0xCE00C000, ~1UL, 0x00000000);
416}
417
418static void pch_set_acpi_mode(void)
419{
Kyösti Mälkkiad882c32020-06-02 05:05:30 +0300420 if (!acpi_is_wakeup_s3()) {
Kyösti Mälkkib6585482020-06-01 15:11:14 +0300421 apm_control(APM_CNT_ACPI_DISABLE);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700422 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700423}
424
425static void lpc_init(struct device *dev)
426{
427 /* Legacy initialization */
428 isa_dma_init();
Arthur Heymans2abbe462019-06-04 14:12:01 +0200429 sb_rtc_init();
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700430 reg_script_run_on_dev(dev, pch_misc_init_script);
431
432 /* Interrupt configuration */
433 pch_enable_ioapic(dev);
434 pch_pirq_init(dev);
435 setup_i8259();
436 i8259_configure_irq_trigger(9, 1);
Matt DeVillier81a6f102018-02-19 17:33:48 -0600437 enable_hpet(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700438
439 /* Initialize power management */
440 pch_power_options(dev);
441 pch_pm_init(dev);
442 pch_cg_init(dev);
443
444 pch_set_acpi_mode();
445}
446
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200447static void pch_lpc_add_mmio_resources(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700448{
449 u32 reg;
450 struct resource *res;
451 const u32 default_decode_base = IO_APIC_ADDR;
452
453 /*
454 * Just report all resources from IO-APIC base to 4GiB. Don't mark
455 * them reserved as that may upset the OS if this range is marked
456 * as reserved in the e820.
457 */
458 res = new_resource(dev, OIC);
459 res->base = default_decode_base;
460 res->size = 0 - default_decode_base;
461 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
462
463 /* RCBA */
Lee Leahy6ef51922017-03-17 10:56:08 -0700464 if (default_decode_base > RCBA_BASE_ADDRESS) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700465 res = new_resource(dev, RCBA);
466 res->base = RCBA_BASE_ADDRESS;
467 res->size = 16 * 1024;
468 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED |
Lee Leahy26b7cd02017-03-16 18:47:55 -0700469 IORESOURCE_FIXED | IORESOURCE_RESERVE;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700470 }
471
472 /* Check LPC Memory Decode register. */
473 reg = pci_read_config32(dev, LGMR);
474 if (reg & 1) {
475 reg &= ~0xffff;
476 if (reg < default_decode_base) {
477 res = new_resource(dev, LGMR);
478 res->base = reg;
479 res->size = 16 * 1024;
480 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED |
Lee Leahy26b7cd02017-03-16 18:47:55 -0700481 IORESOURCE_FIXED | IORESOURCE_RESERVE;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700482 }
483 }
484}
485
486/* Default IO range claimed by the LPC device. The upper bound is exclusive. */
487#define LPC_DEFAULT_IO_RANGE_LOWER 0
488#define LPC_DEFAULT_IO_RANGE_UPPER 0x1000
489
Julius Werner7c712bb2019-05-01 16:51:20 -0700490static inline int pch_io_range_in_default(int base, int size)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700491{
492 /* Does it start above the range? */
493 if (base >= LPC_DEFAULT_IO_RANGE_UPPER)
494 return 0;
495
496 /* Is it entirely contained? */
497 if (base >= LPC_DEFAULT_IO_RANGE_LOWER &&
498 (base + size) < LPC_DEFAULT_IO_RANGE_UPPER)
499 return 1;
500
501 /* This will return not in range for partial overlaps. */
502 return 0;
503}
504
505/*
506 * Note: this function assumes there is no overlap with the default LPC device's
507 * claimed range: LPC_DEFAULT_IO_RANGE_LOWER -> LPC_DEFAULT_IO_RANGE_UPPER.
508 */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200509static void pch_lpc_add_io_resource(struct device *dev, u16 base, u16 size,
510 int index)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700511{
512 struct resource *res;
513
514 if (pch_io_range_in_default(base, size))
515 return;
516
517 res = new_resource(dev, index);
518 res->base = base;
519 res->size = size;
520 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
521}
522
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200523static void pch_lpc_add_gen_io_resources(struct device *dev, int reg_value,
524 int index)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700525{
526 /*
527 * Check if the register is enabled. If so and the base exceeds the
Martin Rothde7ed6f2014-12-07 14:58:18 -0700528 * device's default claim range add the resource.
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700529 */
530 if (reg_value & 1) {
531 u16 base = reg_value & 0xfffc;
532 u16 size = (0x3 | ((reg_value >> 16) & 0xfc)) + 1;
533 pch_lpc_add_io_resource(dev, base, size, index);
534 }
535}
536
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200537static void pch_lpc_add_io_resources(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700538{
539 struct resource *res;
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300540 config_t *config = config_of(dev);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700541
542 /* Add the default claimed IO range for the LPC device. */
543 res = new_resource(dev, 0);
544 res->base = LPC_DEFAULT_IO_RANGE_LOWER;
545 res->size = LPC_DEFAULT_IO_RANGE_UPPER - LPC_DEFAULT_IO_RANGE_LOWER;
546 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
547
548 /* GPIOBASE */
549 pch_lpc_add_io_resource(dev, GPIO_BASE_ADDRESS,
550 GPIO_BASE_SIZE, GPIO_BASE);
551
552 /* PMBASE */
553 pch_lpc_add_io_resource(dev, ACPI_BASE_ADDRESS, ACPI_BASE_SIZE, PMBASE);
554
555 /* LPC Generic IO Decode range. */
556 pch_lpc_add_gen_io_resources(dev, config->gen1_dec, LPC_GEN1_DEC);
557 pch_lpc_add_gen_io_resources(dev, config->gen2_dec, LPC_GEN2_DEC);
558 pch_lpc_add_gen_io_resources(dev, config->gen3_dec, LPC_GEN3_DEC);
559 pch_lpc_add_gen_io_resources(dev, config->gen4_dec, LPC_GEN4_DEC);
560}
561
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200562static void pch_lpc_read_resources(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700563{
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300564 struct global_nvs *gnvs;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700565
566 /* Get the normal PCI resources of this device. */
567 pci_dev_read_resources(dev);
568
569 /* Add non-standard MMIO resources. */
570 pch_lpc_add_mmio_resources(dev);
571
572 /* Add IO resources. */
573 pch_lpc_add_io_resources(dev);
574
575 /* Allocate ACPI NVS in CBMEM */
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300576 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(struct global_nvs));
Kyösti Mälkki9e94dbf2015-01-08 20:03:18 +0200577 if (!acpi_is_wakeup_s3() && gnvs)
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300578 memset(gnvs, 0, sizeof(struct global_nvs));
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700579}
580
Furquan Shaikh338fd9a2020-04-24 22:57:05 -0700581static void southcluster_inject_dsdt(const struct device *device)
Vladimir Serbinenkob219da82014-11-09 03:29:30 +0100582{
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300583 struct global_nvs *gnvs;
Vladimir Serbinenkob219da82014-11-09 03:29:30 +0100584
585 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
586 if (!gnvs) {
Lee Leahy26b7cd02017-03-16 18:47:55 -0700587 gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, sizeof(*gnvs));
Vladimir Serbinenkob219da82014-11-09 03:29:30 +0100588 if (gnvs)
589 memset(gnvs, 0, sizeof(*gnvs));
590 }
591
592 if (gnvs) {
Vladimir Serbinenkob219da82014-11-09 03:29:30 +0100593 acpi_create_gnvs(gnvs);
Vladimir Serbinenkob219da82014-11-09 03:29:30 +0100594 /* And tell SMI about it */
Kyösti Mälkkic3c55212020-06-17 10:34:26 +0300595 apm_control(APM_CNT_GNVS_UPDATE);
Vladimir Serbinenkob219da82014-11-09 03:29:30 +0100596
597 /* Add it to DSDT. */
598 acpigen_write_scope("\\");
599 acpigen_write_name_dword("NVSA", (u32) gnvs);
600 acpigen_pop_len();
601 }
602}
603
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700604static unsigned long broadwell_write_acpi_tables(const struct device *device,
Duncan Laurie93bbd412017-11-11 20:03:29 -0800605 unsigned long current,
606 struct acpi_rsdp *rsdp)
607{
Julius Wernercd49cce2019-03-05 16:53:33 -0800608 if (CONFIG(INTEL_PCH_UART_CONSOLE))
Duncan Laurie93bbd412017-11-11 20:03:29 -0800609 current = acpi_write_dbg2_pci_uart(rsdp, current,
610 (CONFIG_INTEL_PCH_UART_CONSOLE_NUMBER == 1) ?
611 PCH_DEV_UART1 : PCH_DEV_UART0,
612 ACPI_ACCESS_SIZE_BYTE_ACCESS);
613 return acpi_write_hpet(device, current, rsdp);
614}
615
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700616static struct device_operations device_ops = {
617 .read_resources = &pch_lpc_read_resources,
618 .set_resources = &pci_dev_set_resources,
619 .enable_resources = &pci_dev_enable_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200620 .acpi_inject_dsdt = southcluster_inject_dsdt,
Duncan Laurie93bbd412017-11-11 20:03:29 -0800621 .write_acpi_tables = broadwell_write_acpi_tables,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700622 .init = &lpc_init,
Nico Huber51b75ae2019-03-14 16:02:05 +0100623 .scan_bus = &scan_static_bus,
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700624 .ops_pci = &broadwell_pci_ops,
625};
626
627static const unsigned short pci_device_ids[] = {
628 PCH_LPT_LP_SAMPLE,
629 PCH_LPT_LP_PREMIUM,
630 PCH_LPT_LP_MAINSTREAM,
631 PCH_LPT_LP_VALUE,
632 PCH_WPT_HSW_U_SAMPLE,
633 PCH_WPT_BDW_U_SAMPLE,
634 PCH_WPT_BDW_U_PREMIUM,
635 PCH_WPT_BDW_U_BASE,
636 PCH_WPT_BDW_Y_SAMPLE,
637 PCH_WPT_BDW_Y_PREMIUM,
638 PCH_WPT_BDW_Y_BASE,
639 PCH_WPT_BDW_H,
640 0
641};
642
643static const struct pci_driver pch_lpc __pci_driver = {
644 .ops = &device_ops,
645 .vendor = PCI_VENDOR_ID_INTEL,
646 .devices = pci_device_ids,
647};