blob: 3685a413f0002a607125e7888562f54968b20182 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin76c37002012-10-30 09:03:43 -05002
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
6#include <device/pci_ids.h>
Patrick Rudolphe56189c2018-04-18 10:11:59 +02007#include <device/pci_ops.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +02008#include <option.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05009#include <pc80/isa-dma.h>
10#include <pc80/i8259.h>
11#include <arch/io.h>
12#include <arch/ioapic.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070013#include <acpi/acpi.h>
Aaron Durbin29ffa542012-12-21 21:21:48 -060014#include <cpu/x86/smm.h>
Duncan Laurie9c07c8f2013-03-22 11:08:39 -070015#include <string.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030016#include "chip.h"
Angel Pons2178b722020-05-31 00:55:35 +020017#include "iobp.h"
Aaron Durbin76c37002012-10-30 09:03:43 -050018#include "pch.h"
Furquan Shaikh76cedd22020-05-02 10:24:23 -070019#include <acpi/acpigen.h>
Tristan Corrickf3127d42018-10-31 02:25:54 +130020#include <southbridge/intel/common/acpi_pirq_gen.h>
Patrick Rudolph6b931122018-11-01 17:48:37 +010021#include <southbridge/intel/common/rtc.h>
Arthur Heymansa3121b02019-05-28 13:46:49 +020022#include <southbridge/intel/common/spi.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050023
24#define NMI_OFF 0
25
Aaron Durbin76c37002012-10-30 09:03:43 -050026typedef struct southbridge_intel_lynxpoint_config config_t;
27
Paul Menzel373a20c2013-05-03 12:17:02 +020028/**
29 * Set miscellanous static southbridge features.
30 *
31 * @param dev PCI device with I/O APIC control registers
32 */
33static void pch_enable_ioapic(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -050034{
Aaron Durbin76c37002012-10-30 09:03:43 -050035 u32 reg32;
Aaron Durbin76c37002012-10-30 09:03:43 -050036
Matt DeVilliera51e3792018-03-04 01:44:15 -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);
Aaron Durbin76c37002012-10-30 09:03:43 -050042
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 Lauriec5939992013-05-24 11:06:49 -070045 if (pch_is_lp()) {
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);
Aaron Durbin76c37002012-10-30 09:03:43 -050051
Paul Menzel373a20c2013-05-03 12:17:02 +020052 /*
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);
Aaron Durbin76c37002012-10-30 09:03:43 -050057}
58
59static void pch_enable_serial_irqs(struct device *dev)
60{
61 /* Set packet length and toggle silent mode bit for one frame. */
62 pci_write_config8(dev, SERIRQ_CNTL,
63 (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
Julius Wernercd49cce2019-03-05 16:53:33 -080064#if !CONFIG(SERIRQ_CONTINUOUS_MODE)
Aaron Durbin76c37002012-10-30 09:03:43 -050065 pci_write_config8(dev, SERIRQ_CNTL,
66 (1 << 7) | (0 << 6) | ((21 - 17) << 2) | (0 << 0));
67#endif
68}
69
Angel Pons1464a052020-10-30 20:21:37 +010070static void enable_hpet(struct device *const dev)
71{
72 u32 reg32;
73 size_t i;
74
75 /* Assign unique bus/dev/fn for each HPET */
76 for (i = 0; i < 8; ++i)
77 pci_write_config16(dev, LPC_HnBDF(i),
78 PCH_HPET_PCI_BUS << 8 | PCH_HPET_PCI_SLOT << 3 | i);
79
80 /* Move HPET to default address 0xfed00000 and enable it */
81 reg32 = RCBA32(HPTC);
82 reg32 |= (1 << 7); // HPET Address Enable
83 reg32 &= ~(3 << 0);
84 RCBA32(HPTC) = reg32;
85 /* Read it back to stick. It's affected by posted write syndrome. */
86 RCBA32(HPTC);
87}
88
Aaron Durbin76c37002012-10-30 09:03:43 -050089/* PIRQ[n]_ROUT[3:0] - PIRQ Routing Control
90 * 0x00 - 0000 = Reserved
91 * 0x01 - 0001 = Reserved
92 * 0x02 - 0010 = Reserved
93 * 0x03 - 0011 = IRQ3
94 * 0x04 - 0100 = IRQ4
95 * 0x05 - 0101 = IRQ5
96 * 0x06 - 0110 = IRQ6
97 * 0x07 - 0111 = IRQ7
98 * 0x08 - 1000 = Reserved
99 * 0x09 - 1001 = IRQ9
100 * 0x0A - 1010 = IRQ10
101 * 0x0B - 1011 = IRQ11
102 * 0x0C - 1100 = IRQ12
103 * 0x0D - 1101 = Reserved
104 * 0x0E - 1110 = IRQ14
105 * 0x0F - 1111 = IRQ15
106 * PIRQ[n]_ROUT[7] - PIRQ Routing Control
107 * 0x80 - The PIRQ is not routed.
108 */
109
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200110static void pch_pirq_init(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500111{
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200112 struct device *irq_dev;
Aaron Durbin76c37002012-10-30 09:03:43 -0500113
Angel Pons9f781272020-07-25 14:03:40 +0200114 const uint8_t pirq = 0x80;
Aaron Durbin76c37002012-10-30 09:03:43 -0500115
Angel Pons9f781272020-07-25 14:03:40 +0200116 pci_write_config8(dev, PIRQA_ROUT, pirq);
117 pci_write_config8(dev, PIRQB_ROUT, pirq);
118 pci_write_config8(dev, PIRQC_ROUT, pirq);
119 pci_write_config8(dev, PIRQD_ROUT, pirq);
120
121 pci_write_config8(dev, PIRQE_ROUT, pirq);
122 pci_write_config8(dev, PIRQF_ROUT, pirq);
123 pci_write_config8(dev, PIRQG_ROUT, pirq);
124 pci_write_config8(dev, PIRQH_ROUT, pirq);
Aaron Durbin76c37002012-10-30 09:03:43 -0500125
126 /* Eric Biederman once said we should let the OS do this.
127 * I am not so sure anymore he was right.
128 */
129
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200130 for (irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
Angel Pons2aaf7c02020-09-24 18:03:18 +0200131 u8 int_pin = 0, int_line = 0;
Aaron Durbin76c37002012-10-30 09:03:43 -0500132
133 if (!irq_dev->enabled || irq_dev->path.type != DEVICE_PATH_PCI)
134 continue;
135
136 int_pin = pci_read_config8(irq_dev, PCI_INTERRUPT_PIN);
137
138 switch (int_pin) {
Angel Pons9f781272020-07-25 14:03:40 +0200139 case 1: /* INTA# */
140 case 2: /* INTB# */
141 case 3: /* INTC# */
142 case 4: /* INTD# */
143 int_line = pirq;
144 break;
Aaron Durbin76c37002012-10-30 09:03:43 -0500145 }
146
147 if (!int_line)
148 continue;
149
150 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, int_line);
151 }
152}
153
Angel Ponscbcbb672020-10-23 00:11:26 +0200154static void pch_gpi_routing(struct device *dev, config_t *config)
Aaron Durbin76c37002012-10-30 09:03:43 -0500155{
Aaron Durbin76c37002012-10-30 09:03:43 -0500156 u32 reg32 = 0;
157
158 /* An array would be much nicer here, or some
159 * other method of doing this.
160 */
161 reg32 |= (config->gpi0_routing & 0x03) << 0;
162 reg32 |= (config->gpi1_routing & 0x03) << 2;
163 reg32 |= (config->gpi2_routing & 0x03) << 4;
164 reg32 |= (config->gpi3_routing & 0x03) << 6;
165 reg32 |= (config->gpi4_routing & 0x03) << 8;
166 reg32 |= (config->gpi5_routing & 0x03) << 10;
167 reg32 |= (config->gpi6_routing & 0x03) << 12;
168 reg32 |= (config->gpi7_routing & 0x03) << 14;
169 reg32 |= (config->gpi8_routing & 0x03) << 16;
170 reg32 |= (config->gpi9_routing & 0x03) << 18;
171 reg32 |= (config->gpi10_routing & 0x03) << 20;
172 reg32 |= (config->gpi11_routing & 0x03) << 22;
173 reg32 |= (config->gpi12_routing & 0x03) << 24;
174 reg32 |= (config->gpi13_routing & 0x03) << 26;
175 reg32 |= (config->gpi14_routing & 0x03) << 28;
176 reg32 |= (config->gpi15_routing & 0x03) << 30;
177
Kyösti Mälkkib85a87b2014-12-29 11:32:27 +0200178 pci_write_config32(dev, GPIO_ROUT, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500179}
180
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200181static void pch_power_options(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500182{
183 u8 reg8;
Duncan Laurie467f31d2013-03-08 17:00:37 -0800184 u16 reg16;
Aaron Durbin76c37002012-10-30 09:03:43 -0500185 u32 reg32;
186 const char *state;
Duncan Laurie467f31d2013-03-08 17:00:37 -0800187 u16 pmbase = get_pmbase();
Nico Huber9faae2b2018-11-14 00:00:35 +0100188 int pwr_on = CONFIG_MAINBOARD_POWER_FAILURE_STATE;
Aaron Durbin76c37002012-10-30 09:03:43 -0500189 int nmi_option;
190
191 /* Which state do we want to goto after g3 (power restored)?
192 * 0 == S0 Full On
193 * 1 == S5 Soft Off
194 *
195 * If the option is not existent (Laptops), use Kconfig setting.
196 */
197 get_option(&pwr_on, "power_on_after_fail");
198
199 reg16 = pci_read_config16(dev, GEN_PMCON_3);
200 reg16 &= 0xfffe;
201 switch (pwr_on) {
202 case MAINBOARD_POWER_OFF:
203 reg16 |= 1;
204 state = "off";
205 break;
206 case MAINBOARD_POWER_ON:
207 reg16 &= ~1;
208 state = "on";
209 break;
210 case MAINBOARD_POWER_KEEP:
211 reg16 &= ~1;
212 state = "state keep";
213 break;
214 default:
215 state = "undefined";
216 }
217
218 reg16 &= ~(3 << 4); /* SLP_S4# Assertion Stretch 4s */
219 reg16 |= (1 << 3); /* SLP_S4# Assertion Stretch Enable */
220
221 reg16 &= ~(1 << 10);
222 reg16 |= (1 << 11); /* SLP_S3# Min Assertion Width 50ms */
223
224 reg16 |= (1 << 12); /* Disable SLP stretch after SUS well */
225
226 pci_write_config16(dev, GEN_PMCON_3, reg16);
227 printk(BIOS_INFO, "Set power %s after power failure.\n", state);
228
229 /* Set up NMI on errors. */
230 reg8 = inb(0x61);
231 reg8 &= 0x0f; /* Higher Nibble must be 0 */
232 reg8 &= ~(1 << 3); /* IOCHK# NMI Enable */
233 // reg8 &= ~(1 << 2); /* PCI SERR# Enable */
234 reg8 |= (1 << 2); /* PCI SERR# Disable for now */
235 outb(reg8, 0x61);
236
237 reg8 = inb(0x70);
238 nmi_option = NMI_OFF;
239 get_option(&nmi_option, "nmi");
240 if (nmi_option) {
241 printk(BIOS_INFO, "NMI sources enabled.\n");
242 reg8 &= ~(1 << 7); /* Set NMI. */
243 } else {
244 printk(BIOS_INFO, "NMI sources disabled.\n");
Elyes HAOUAS9c5d4632018-04-26 22:21:21 +0200245 reg8 |= (1 << 7); /* Can't mask NMI from PCI-E and NMI_NOW */
Aaron Durbin76c37002012-10-30 09:03:43 -0500246 }
247 outb(reg8, 0x70);
248
249 /* Enable CPU_SLP# and Intel Speedstep, set SMI# rate down */
250 reg16 = pci_read_config16(dev, GEN_PMCON_1);
251 reg16 &= ~(3 << 0); // SMI# rate 1 minute
252 reg16 &= ~(1 << 10); // Disable BIOS_PCI_EXP_EN for native PME
Aaron Durbin76c37002012-10-30 09:03:43 -0500253 pci_write_config16(dev, GEN_PMCON_1, reg16);
254
Angel Ponscbcbb672020-10-23 00:11:26 +0200255 if (dev->chip_info) {
256 config_t *config = dev->chip_info;
Aaron Durbin76c37002012-10-30 09:03:43 -0500257
Angel Ponscbcbb672020-10-23 00:11:26 +0200258 /*
259 * Set the board's GPI routing on LynxPoint-H.
260 * This is done as part of GPIO configuration on LynxPoint-LP.
261 */
Angel Ponsa7174b72020-10-30 20:23:41 +0100262 if (!pch_is_lp())
Angel Ponscbcbb672020-10-23 00:11:26 +0200263 pch_gpi_routing(dev, config);
Aaron Durbin76c37002012-10-30 09:03:43 -0500264
Angel Ponscbcbb672020-10-23 00:11:26 +0200265 /* GPE setup based on device tree configuration */
266 enable_all_gpe(config->gpe0_en_1, config->gpe0_en_2,
267 config->gpe0_en_3, config->gpe0_en_4);
268
269 /* SMI setup based on device tree configuration */
270 enable_alt_smi(config->alt_gp_smi_en);
271 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500272
273 /* Set up power management block and determine sleep mode */
274 reg32 = inl(pmbase + 0x04); // PM1_CNT
275 reg32 &= ~(7 << 10); // SLP_TYP
276 reg32 |= (1 << 0); // SCI_EN
277 outl(reg32, pmbase + 0x04);
278
279 /* Clear magic status bits to prevent unexpected wake */
280 reg32 = RCBA32(0x3310);
Angel Pons84fa2242020-10-24 11:53:47 +0200281 reg32 |= (1 << 4) | (1 << 5) | (1 << 0);
Aaron Durbin76c37002012-10-30 09:03:43 -0500282 RCBA32(0x3310) = reg32;
283
Ryan Salsamendi889ce9c2017-06-30 17:45:14 -0700284 reg16 = RCBA16(0x3f02);
285 reg16 &= ~0xf;
286 RCBA16(0x3f02) = reg16;
Aaron Durbin76c37002012-10-30 09:03:43 -0500287}
288
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800289/* LynxPoint PCH Power Management init */
290static void lpt_pm_init(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500291{
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800292 printk(BIOS_DEBUG, "LynxPoint PM init\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500293}
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800294
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800295/* LynxPoint LP PCH Power Management init */
296static void lpt_lp_pm_init(struct device *dev)
297{
298 struct southbridge_intel_lynxpoint_config *config = dev->chip_info;
299 u32 data;
300
301 printk(BIOS_DEBUG, "LynxPoint LP PM init\n");
302
303 pci_write_config8(dev, 0xa9, 0x46);
304
Angel Pons90cdf702020-10-24 23:00:34 +0200305 RCBA32_AND_OR(0x232c, ~1, 0);
306
Angel Pons725657a2020-07-03 13:15:00 +0200307 RCBA32_AND_OR(0x1100, ~0xc000, 0xc000);
Angel Pons4fe46612020-10-24 22:22:04 +0200308 RCBA32_OR(0x1100, 0x00000100);
309 RCBA32_OR(0x1100, 0x0000003f);
Angel Pons90cdf702020-10-24 23:00:34 +0200310
Angel Pons725657a2020-07-03 13:15:00 +0200311 RCBA32_AND_OR(0x2320, ~0x60, 0x10);
Angel Pons90cdf702020-10-24 23:00:34 +0200312
Angel Pons4fe46612020-10-24 22:22:04 +0200313 RCBA32(0x3314) = 0x00012fff;
314 RCBA32(0x3318) = 0x0dcf0400;
315 RCBA32(0x3324) = 0x04000000;
316 RCBA32(0x3368) = 0x00041400;
317 RCBA32(0x3388) = 0x3f8ddbff;
318 RCBA32(0x33ac) = 0x00007001;
319 RCBA32(0x33b0) = 0x00181900;
320 RCBA32(0x33c0) = 0x00060A00;
321 RCBA32(0x33d0) = 0x06200840;
322 RCBA32(0x3a28) = 0x01010101;
323 RCBA32(0x3a2c) = 0x04040404;
324 RCBA32(0x2b1c) = 0x03808033;
325 RCBA32(0x2b34) = 0x80000009;
326 RCBA32(0x3348) = 0x022ddfff;
327 RCBA32(0x334c) = 0x00000001;
328 RCBA32(0x3358) = 0x0001c000;
329 RCBA32(0x3380) = 0x3f8ddbff;
330 RCBA32(0x3384) = 0x0001c7e1;
331 RCBA32(0x338c) = 0x0001c7e1;
332 RCBA32(0x3398) = 0x0001c000;
333 RCBA32(0x33a8) = 0x00181900;
334 RCBA32(0x33dc) = 0x00080000;
335 RCBA32(0x33e0) = 0x00000001;
336 RCBA32(0x3a20) = 0x00000404;
337 RCBA32(0x3a24) = 0x01010101;
338 RCBA32(0x3a30) = 0x01010101;
Angel Pons90cdf702020-10-24 23:00:34 +0200339
Angel Pons4fe46612020-10-24 22:22:04 +0200340 RCBA32_OR(0x0410, 0x00000003);
341 RCBA32_OR(0x2618, 0x08000000);
342 RCBA32_OR(0x2300, 0x00000002);
343 RCBA32_OR(0x2600, 0x00000008);
Angel Pons90cdf702020-10-24 23:00:34 +0200344
Angel Pons4fe46612020-10-24 22:22:04 +0200345 RCBA32(0x33b4) = 0x00007001;
346 RCBA32(0x3350) = 0x022ddfff;
347 RCBA32(0x3354) = 0x00000001;
Angel Pons90cdf702020-10-24 23:00:34 +0200348
349 /* Power Optimizer */
350 RCBA32_OR(0x33d4, 0x08000000);
351 RCBA32_OR(0x33c8, 0x00000080);
352
353 RCBA32(0x2b10) = 0x0000883c;
354 RCBA32(0x2b14) = 0x1e0a4616;
355 RCBA32(0x2b24) = 0x40000005;
356 RCBA32(0x2b20) = 0x0005db01;
Angel Pons4fe46612020-10-24 22:22:04 +0200357 RCBA32(0x3a80) = 0x05145005;
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800358
Angel Ponsbf9bc502020-06-08 00:12:43 +0200359 pci_or_config32(dev, 0xac, 1 << 21);
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800360
Elyes HAOUASa0aea562017-07-03 21:38:53 +0200361 pch_iobp_update(0xED00015C, ~(1 << 11), 0x00003700);
Angel Pons8963f7d2020-10-24 12:20:28 +0200362 pch_iobp_update(0xED000118, ~0, 0x00c00000);
363 pch_iobp_update(0xED000120, ~0, 0x00240000);
364 pch_iobp_update(0xCA000000, ~0, 0x00000009);
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800365
366 /* Set RCBA CIR28 0x3A84 based on SATA port enables */
367 data = 0x00001005;
368 /* Port 3 and 2 disabled */
Angel Ponscbcbb672020-10-23 00:11:26 +0200369 if (config && (config->sata_port_map & ((1 << 3) | (1 << 2))) == 0)
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800370 data |= (1 << 24) | (1 << 26);
371 /* Port 1 and 0 disabled */
Angel Ponscbcbb672020-10-23 00:11:26 +0200372 if (config && (config->sata_port_map & ((1 << 1) | (1 << 0))) == 0)
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800373 data |= (1 << 20) | (1 << 18);
374 RCBA32(0x3a84) = data;
375
Duncan Laurie5cf34ce2013-04-26 10:41:33 -0700376 /* Set RCBA 0x2b1c[29]=1 if DSP disabled */
377 if (RCBA32(FD) & PCH_DISABLE_ADSPD)
378 RCBA32_OR(0x2b1c, (1 << 29));
379
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800380 /* Lock */
381 RCBA32_OR(0x3a6c, 0x00000001);
382
383 /* Set RCBA 0x33D4 after other setup */
384 RCBA32_OR(0x33d4, 0x2fff2fb1);
385
386 /* Set RCBA 0x33C8[15]=1 as last step */
387 RCBA32_OR(0x33c8, (1 << 15));
388}
Aaron Durbin76c37002012-10-30 09:03:43 -0500389
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200390static void enable_clock_gating(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500391{
Duncan Laurie74c0d052012-12-17 11:31:40 -0800392 /* LynxPoint Mobile */
393 u32 reg32;
394 u16 reg16;
395
396 /* DMI */
Angel Pons8963f7d2020-10-24 12:20:28 +0200397 RCBA32_AND_OR(0x2234, ~0, 0xf);
Duncan Laurie74c0d052012-12-17 11:31:40 -0800398 reg16 = pci_read_config16(dev, GEN_PMCON_1);
399 reg16 |= (1 << 11) | (1 << 12) | (1 << 14);
400 reg16 |= (1 << 2); // PCI CLKRUN# Enable
401 pci_write_config16(dev, GEN_PMCON_1, reg16);
402 RCBA32_OR(0x900, (1 << 14));
403
404 reg32 = RCBA32(CG);
405 reg32 |= (1 << 22); // HDA Dynamic
Angel Pons8963f7d2020-10-24 12:20:28 +0200406 reg32 |= (1 << 31); // LPC Dynamic
Duncan Laurie74c0d052012-12-17 11:31:40 -0800407 reg32 |= (1 << 16); // PCIe Dynamic
408 reg32 |= (1 << 27); // HPET Dynamic
409 reg32 |= (1 << 28); // GPIO Dynamic
410 RCBA32(CG) = reg32;
411
412 RCBA32_OR(0x38c0, 0x7); // SPI Dynamic
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800413}
414
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200415static void enable_lp_clock_gating(struct device *dev)
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800416{
417 /* LynxPoint LP */
418 u32 reg32;
419 u16 reg16;
420
421 /* DMI */
Angel Pons8963f7d2020-10-24 12:20:28 +0200422 RCBA32_AND_OR(0x2234, ~0, 0xf);
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800423 reg16 = pci_read_config16(dev, GEN_PMCON_1);
424 reg16 &= ~((1 << 11) | (1 << 14));
425 reg16 |= (1 << 5) | (1 << 6) | (1 << 7) | (1 << 12) | (1 << 13);
426 reg16 |= (1 << 2); // PCI CLKRUN# Enable
427 pci_write_config16(dev, GEN_PMCON_1, reg16);
428
Angel Ponsbf9bc502020-06-08 00:12:43 +0200429 pci_or_config32(dev, 0x64, 1 << 6);
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800430
Duncan Laurie4bc107b2013-06-24 13:14:44 -0700431 /*
432 * RCBA + 0x2614[27:25,14:13,10,8] = 101,11,1,1
433 * RCBA + 0x2614[23:16] = 0x20
434 * RCBA + 0x2614[30:28] = 0x0
Duncan Lauried8c7d732013-07-16 09:01:43 -0700435 * RCBA + 0x2614[26] = 1 (IF 0:2.0@0x08 >= 0x0b)
Duncan Laurie4bc107b2013-06-24 13:14:44 -0700436 */
Angel Pons90cdf702020-10-24 23:00:34 +0200437 RCBA32_AND_OR(0x2614, ~0x74000000, 0x0a206500);
Duncan Laurie4bc107b2013-06-24 13:14:44 -0700438
439 /* Check for LPT-LP B2 stepping and 0:31.0@0xFA > 4 */
Nico Huber744d6bd2019-01-12 14:58:20 +0100440 struct device *const gma = pcidev_on_root(2, 0);
441 if (gma && pci_read_config8(gma, 0x8) >= 0x0b)
Elyes HAOUASa0aea562017-07-03 21:38:53 +0200442 RCBA32_OR(0x2614, (1 << 26));
Duncan Laurie4bc107b2013-06-24 13:14:44 -0700443
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800444 RCBA32_OR(0x900, 0x0000031f);
445
446 reg32 = RCBA32(CG);
Duncan Lauriea2d6a402013-03-22 11:24:45 -0700447 if (RCBA32(0x3454) & (1 << 4))
448 reg32 &= ~(1 << 29); // LPC Dynamic
449 else
450 reg32 |= (1 << 29); // LPC Dynamic
Angel Pons2aaf7c02020-09-24 18:03:18 +0200451 reg32 |= (1 << 31); // LP LPC
Duncan Laurie5cf34ce2013-04-26 10:41:33 -0700452 reg32 |= (1 << 30); // LP BLA
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800453 reg32 |= (1 << 28); // GPIO Dynamic
454 reg32 |= (1 << 27); // HPET Dynamic
Duncan Laurie5cf34ce2013-04-26 10:41:33 -0700455 reg32 |= (1 << 26); // Generic Platform Event Clock
456 if (RCBA32(BUC) & PCH_DISABLE_GBE)
457 reg32 |= (1 << 23); // GbE Static
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800458 reg32 |= (1 << 22); // HDA Dynamic
Duncan Laurie5cf34ce2013-04-26 10:41:33 -0700459 reg32 |= (1 << 16); // PCI Dynamic
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800460 RCBA32(CG) = reg32;
461
462 RCBA32_OR(0x3434, 0x7); // LP LPC
463
464 RCBA32_AND_OR(0x333c, 0xffcfffff, 0x00c00000); // SATA
465
466 RCBA32_OR(0x38c0, 0x3c07); // SPI Dynamic
467
Angel Pons8963f7d2020-10-24 12:20:28 +0200468 pch_iobp_update(0xCF000000, ~0, 0x00007001);
469 pch_iobp_update(0xCE00C000, ~1, 0x00000000); // bit0=0 in BWG 1.4.0
Aaron Durbin76c37002012-10-30 09:03:43 -0500470}
471
Aaron Durbin29ffa542012-12-21 21:21:48 -0600472static void pch_set_acpi_mode(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500473{
Kyösti Mälkkiad882c32020-06-02 05:05:30 +0300474 if (!acpi_is_wakeup_s3())
Kyösti Mälkkib6585482020-06-01 15:11:14 +0300475 apm_control(APM_CNT_ACPI_DISABLE);
Aaron Durbin76c37002012-10-30 09:03:43 -0500476}
Aaron Durbin76c37002012-10-30 09:03:43 -0500477
478static void pch_disable_smm_only_flashing(struct device *dev)
479{
Aaron Durbin76c37002012-10-30 09:03:43 -0500480 printk(BIOS_SPEW, "Enabling BIOS updates outside of SMM... ");
Angel Ponsbf9bc502020-06-08 00:12:43 +0200481
482 pci_and_config8(dev, BIOS_CNTL, ~(1 << 5));
Aaron Durbin76c37002012-10-30 09:03:43 -0500483}
484
485static void pch_fixups(struct device *dev)
486{
Aaron Durbin76c37002012-10-30 09:03:43 -0500487 /* Indicate DRAM init done for MRC S3 to know it can resume */
Angel Ponsbf9bc502020-06-08 00:12:43 +0200488 pci_or_config8(dev, GEN_PMCON_2, 1 << 7);
Aaron Durbin76c37002012-10-30 09:03:43 -0500489
490 /*
491 * Enable DMI ASPM in the PCH
492 */
493 RCBA32_AND_OR(0x2304, ~(1 << 10), 0);
Angel Pons84fa2242020-10-24 11:53:47 +0200494 RCBA32_OR(0x21a4, (1 << 11) | (1 << 10));
Aaron Durbin76c37002012-10-30 09:03:43 -0500495 RCBA32_OR(0x21a8, 0x3);
496}
497
Aaron Durbin76c37002012-10-30 09:03:43 -0500498static void lpc_init(struct device *dev)
499{
Elyes HAOUASbfc255a2020-03-07 13:05:14 +0100500 printk(BIOS_DEBUG, "pch: %s\n", __func__);
Aaron Durbin76c37002012-10-30 09:03:43 -0500501
Aaron Durbin76c37002012-10-30 09:03:43 -0500502 /* IO APIC initialization. */
Paul Menzel373a20c2013-05-03 12:17:02 +0200503 pch_enable_ioapic(dev);
Aaron Durbin76c37002012-10-30 09:03:43 -0500504
505 pch_enable_serial_irqs(dev);
506
507 /* Setup the PIRQ. */
508 pch_pirq_init(dev);
509
510 /* Setup power options. */
511 pch_power_options(dev);
512
513 /* Initialize power management */
Duncan Lauriedeb90f42013-03-08 17:22:37 -0800514 if (pch_is_lp()) {
515 lpt_lp_pm_init(dev);
516 enable_lp_clock_gating(dev);
517 } else {
518 lpt_pm_init(dev);
519 enable_clock_gating(dev);
Aaron Durbin76c37002012-10-30 09:03:43 -0500520 }
521
Aaron Durbin76c37002012-10-30 09:03:43 -0500522 /* Initialize the real time clock. */
Patrick Rudolph6b931122018-11-01 17:48:37 +0100523 sb_rtc_init();
Aaron Durbin76c37002012-10-30 09:03:43 -0500524
525 /* Initialize ISA DMA. */
526 isa_dma_init();
527
528 /* Initialize the High Precision Event Timers, if present. */
Matt DeVilliera51e3792018-03-04 01:44:15 -0600529 enable_hpet(dev);
Aaron Durbin76c37002012-10-30 09:03:43 -0500530
Aaron Durbin76c37002012-10-30 09:03:43 -0500531 setup_i8259();
532
Aaron Durbin76c37002012-10-30 09:03:43 -0500533 /* Interrupt 9 should be level triggered (SCI) */
534 i8259_configure_irq_trigger(9, 1);
535
536 pch_disable_smm_only_flashing(dev);
537
Aaron Durbin29ffa542012-12-21 21:21:48 -0600538 pch_set_acpi_mode();
Aaron Durbin76c37002012-10-30 09:03:43 -0500539
540 pch_fixups(dev);
541}
542
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200543static void pch_lpc_add_mmio_resources(struct device *dev)
Aaron Durbin6f561af2012-12-19 14:38:01 -0600544{
545 u32 reg;
546 struct resource *res;
547 const u32 default_decode_base = IO_APIC_ADDR;
548
549 /*
550 * Just report all resources from IO-APIC base to 4GiB. Don't mark
551 * them reserved as that may upset the OS if this range is marked
552 * as reserved in the e820.
553 */
554 res = new_resource(dev, OIC);
555 res->base = default_decode_base;
556 res->size = 0 - default_decode_base;
557 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
558
559 /* RCBA */
Angel Pons6e732d32021-01-28 13:56:18 +0100560 if (CONFIG_FIXED_RCBA_MMIO_BASE < default_decode_base) {
Aaron Durbin6f561af2012-12-19 14:38:01 -0600561 res = new_resource(dev, RCBA);
Angel Pons6e732d32021-01-28 13:56:18 +0100562 res->base = (resource_t)CONFIG_FIXED_RCBA_MMIO_BASE;
Angel Ponsb70ff522021-01-28 14:27:46 +0100563 res->size = CONFIG_RCBA_LENGTH;
Aaron Durbin6f561af2012-12-19 14:38:01 -0600564 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED |
Angel Pons2aaf7c02020-09-24 18:03:18 +0200565 IORESOURCE_FIXED | IORESOURCE_RESERVE;
Aaron Durbin6f561af2012-12-19 14:38:01 -0600566 }
567
568 /* Check LPC Memory Decode register. */
569 reg = pci_read_config32(dev, LGMR);
570 if (reg & 1) {
571 reg &= ~0xffff;
572 if (reg < default_decode_base) {
573 res = new_resource(dev, LGMR);
574 res->base = reg;
575 res->size = 16 * 1024;
576 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED |
Angel Pons2aaf7c02020-09-24 18:03:18 +0200577 IORESOURCE_FIXED | IORESOURCE_RESERVE;
Aaron Durbin6f561af2012-12-19 14:38:01 -0600578 }
579 }
580}
581
582/* Default IO range claimed by the LPC device. The upper bound is exclusive. */
583#define LPC_DEFAULT_IO_RANGE_LOWER 0
584#define LPC_DEFAULT_IO_RANGE_UPPER 0x1000
585
Julius Werner7c712bb2019-05-01 16:51:20 -0700586static inline int pch_io_range_in_default(int base, int size)
Aaron Durbin6f561af2012-12-19 14:38:01 -0600587{
588 /* Does it start above the range? */
589 if (base >= LPC_DEFAULT_IO_RANGE_UPPER)
590 return 0;
591
592 /* Is it entirely contained? */
593 if (base >= LPC_DEFAULT_IO_RANGE_LOWER &&
594 (base + size) < LPC_DEFAULT_IO_RANGE_UPPER)
595 return 1;
596
597 /* This will return not in range for partial overlaps. */
598 return 0;
599}
600
601/*
602 * Note: this function assumes there is no overlap with the default LPC device's
603 * claimed range: LPC_DEFAULT_IO_RANGE_LOWER -> LPC_DEFAULT_IO_RANGE_UPPER.
604 */
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200605static void pch_lpc_add_io_resource(struct device *dev, u16 base, u16 size,
606 int index)
Aaron Durbin6f561af2012-12-19 14:38:01 -0600607{
608 struct resource *res;
609
610 if (pch_io_range_in_default(base, size))
611 return;
612
613 res = new_resource(dev, index);
614 res->base = base;
615 res->size = size;
616 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
617}
618
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200619static void pch_lpc_add_gen_io_resources(struct device *dev, int reg_value,
620 int index)
Aaron Durbin6f561af2012-12-19 14:38:01 -0600621{
622 /*
623 * Check if the register is enabled. If so and the base exceeds the
Kyösti Mälkkib544c002019-01-06 10:41:41 +0200624 * device's default, claim range and add the resource.
Aaron Durbin6f561af2012-12-19 14:38:01 -0600625 */
626 if (reg_value & 1) {
627 u16 base = reg_value & 0xfffc;
628 u16 size = (0x3 | ((reg_value >> 16) & 0xfc)) + 1;
629 pch_lpc_add_io_resource(dev, base, size, index);
630 }
631}
632
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200633static void pch_lpc_add_io_resources(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500634{
635 struct resource *res;
Aaron Durbin76c37002012-10-30 09:03:43 -0500636
Aaron Durbin6f561af2012-12-19 14:38:01 -0600637 /* Add the default claimed IO range for the LPC device. */
638 res = new_resource(dev, 0);
639 res->base = LPC_DEFAULT_IO_RANGE_LOWER;
640 res->size = LPC_DEFAULT_IO_RANGE_UPPER - LPC_DEFAULT_IO_RANGE_LOWER;
641 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
642
643 /* GPIOBASE */
Angel Pons2aaf7c02020-09-24 18:03:18 +0200644 pch_lpc_add_io_resource(dev, get_gpiobase(), DEFAULT_GPIOSIZE, GPIO_BASE);
Aaron Durbin6f561af2012-12-19 14:38:01 -0600645
646 /* PMBASE */
Duncan Laurie7922b462013-03-08 16:34:33 -0800647 pch_lpc_add_io_resource(dev, get_pmbase(), 256, PMBASE);
Aaron Durbin6f561af2012-12-19 14:38:01 -0600648
649 /* LPC Generic IO Decode range. */
Angel Ponscbcbb672020-10-23 00:11:26 +0200650 if (dev->chip_info) {
651 config_t *config = dev->chip_info;
652 pch_lpc_add_gen_io_resources(dev, config->gen1_dec, LPC_GEN1_DEC);
653 pch_lpc_add_gen_io_resources(dev, config->gen2_dec, LPC_GEN2_DEC);
654 pch_lpc_add_gen_io_resources(dev, config->gen3_dec, LPC_GEN3_DEC);
655 pch_lpc_add_gen_io_resources(dev, config->gen4_dec, LPC_GEN4_DEC);
656 }
Aaron Durbin6f561af2012-12-19 14:38:01 -0600657}
658
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200659static void pch_lpc_read_resources(struct device *dev)
Aaron Durbin6f561af2012-12-19 14:38:01 -0600660{
Aaron Durbin76c37002012-10-30 09:03:43 -0500661 /* Get the normal PCI resources of this device. */
662 pci_dev_read_resources(dev);
663
Aaron Durbin6f561af2012-12-19 14:38:01 -0600664 /* Add non-standard MMIO resources. */
665 pch_lpc_add_mmio_resources(dev);
Aaron Durbin76c37002012-10-30 09:03:43 -0500666
Aaron Durbin6f561af2012-12-19 14:38:01 -0600667 /* Add IO resources. */
668 pch_lpc_add_io_resources(dev);
Aaron Durbin76c37002012-10-30 09:03:43 -0500669}
670
Elyes HAOUAS7a5f7712018-06-08 17:20:38 +0200671static void pch_lpc_enable(struct device *dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500672{
673 /* Enable PCH Display Port */
674 RCBA16(DISPBDF) = 0x0010;
675 RCBA32_OR(FD2, PCH_ENABLE_DBDF);
676
677 pch_enable(dev);
678}
679
Tristan Corrickf3127d42018-10-31 02:25:54 +1300680static const char *lpc_acpi_name(const struct device *dev)
681{
682 return "LPCB";
683}
684
Furquan Shaikh7536a392020-04-24 21:59:21 -0700685static void southbridge_fill_ssdt(const struct device *dev)
Tristan Corrickf3127d42018-10-31 02:25:54 +1300686{
687 intel_acpi_gen_def_acpi_pirq(dev);
688}
689
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700690static unsigned long southbridge_write_acpi_tables(const struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200691 unsigned long start,
692 struct acpi_rsdp *rsdp)
Vladimir Serbinenkoc6e566a2014-08-31 17:43:51 +0200693{
694 unsigned long current;
Vladimir Serbinenkoc6e566a2014-08-31 17:43:51 +0200695
696 current = start;
697
698 /* Align ACPI tables to 16byte */
Aaron Durbin07a1b282015-12-10 17:07:38 -0600699 current = acpi_align_current(current);
Vladimir Serbinenkoc6e566a2014-08-31 17:43:51 +0200700
701 /*
702 * We explicitly add these tables later on:
703 */
Angel Pons2d35cf82020-10-29 19:28:44 +0100704 current = acpi_write_hpet(device, current, rsdp);
Vladimir Serbinenkoc6e566a2014-08-31 17:43:51 +0200705
Aaron Durbin07a1b282015-12-10 17:07:38 -0600706 current = acpi_align_current(current);
Vladimir Serbinenkoc6e566a2014-08-31 17:43:51 +0200707
Angel Pons04f1de32021-02-10 13:57:01 +0100708 if (pch_is_lp()) {
709 printk(BIOS_DEBUG, "ACPI: * SSDT2\n");
710 acpi_header_t *ssdt = (acpi_header_t *)current;
711 acpi_create_serialio_ssdt(ssdt);
712 current += ssdt->length;
713 acpi_add_table(rsdp, ssdt);
714 current = acpi_align_current(current);
715 }
Vladimir Serbinenkoc6e566a2014-08-31 17:43:51 +0200716
717 printk(BIOS_DEBUG, "current = %lx\n", current);
718 return current;
719}
720
Tristan Corrick32ceed82018-11-30 22:53:27 +1300721static void lpc_final(struct device *dev)
722{
Arthur Heymansa3121b02019-05-28 13:46:49 +0200723 spi_finalize_ops();
Tristan Corrick63626b12018-11-30 22:53:50 +1300724
Julius Wernercd49cce2019-03-05 16:53:33 -0800725 if (acpi_is_wakeup_s3() || CONFIG(INTEL_CHIPSET_LOCKDOWN))
Kyösti Mälkkib6585482020-06-01 15:11:14 +0300726 apm_control(APM_CNT_FINALIZE);
Tristan Corrick32ceed82018-11-30 22:53:27 +1300727}
Vladimir Serbinenkoc6e566a2014-08-31 17:43:51 +0200728
Aaron Durbin76c37002012-10-30 09:03:43 -0500729static struct device_operations device_ops = {
730 .read_resources = pch_lpc_read_resources,
731 .set_resources = pci_dev_set_resources,
Duncan Laurie8d783b82013-05-14 11:16:34 -0700732 .enable_resources = pci_dev_enable_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200733 .acpi_fill_ssdt = southbridge_fill_ssdt,
Tristan Corrickf3127d42018-10-31 02:25:54 +1300734 .acpi_name = lpc_acpi_name,
Vladimir Serbinenkoc6e566a2014-08-31 17:43:51 +0200735 .write_acpi_tables = southbridge_write_acpi_tables,
Aaron Durbin76c37002012-10-30 09:03:43 -0500736 .init = lpc_init,
Tristan Corrick32ceed82018-11-30 22:53:27 +1300737 .final = lpc_final,
Aaron Durbin76c37002012-10-30 09:03:43 -0500738 .enable = pch_lpc_enable,
Nico Huber51b75ae2019-03-14 16:02:05 +0100739 .scan_bus = scan_static_bus,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200740 .ops_pci = &pci_dev_ops_pci,
Aaron Durbin76c37002012-10-30 09:03:43 -0500741};
742
Aaron Durbinc1989c42012-12-11 17:13:17 -0600743/* IDs for LPC device of Intel 8 Series Chipset (Lynx Point) */
744static const unsigned short pci_device_ids[] = {
Felix Singer4ea08f92020-11-20 12:56:44 +0000745 PCI_DEVICE_ID_INTEL_LPT_MOBILE_SAMPLE,
746 PCI_DEVICE_ID_INTEL_LPT_DESKTOP_SAMPLE,
747 PCI_DEVICE_ID_INTEL_LPT_Z87,
748 PCI_DEVICE_ID_INTEL_LPT_Z85,
749 PCI_DEVICE_ID_INTEL_LPT_HM86,
750 PCI_DEVICE_ID_INTEL_LPT_H87,
751 PCI_DEVICE_ID_INTEL_LPT_HM87,
752 PCI_DEVICE_ID_INTEL_LPT_Q85,
753 PCI_DEVICE_ID_INTEL_LPT_Q87,
754 PCI_DEVICE_ID_INTEL_LPT_QM87,
755 PCI_DEVICE_ID_INTEL_LPT_B85,
756 PCI_DEVICE_ID_INTEL_LPT_C222,
757 PCI_DEVICE_ID_INTEL_LPT_C224,
758 PCI_DEVICE_ID_INTEL_LPT_C226,
759 PCI_DEVICE_ID_INTEL_LPT_H81,
760 PCI_DEVICE_ID_INTEL_LPT_LP_SAMPLE,
761 PCI_DEVICE_ID_INTEL_LPT_LP_PREMIUM,
762 PCI_DEVICE_ID_INTEL_LPT_LP_MAINSTREAM,
763 PCI_DEVICE_ID_INTEL_LPT_LP_VALUE,
764 0
765};
Aaron Durbin76c37002012-10-30 09:03:43 -0500766
767static const struct pci_driver pch_lpc __pci_driver = {
768 .ops = &device_ops,
769 .vendor = PCI_VENDOR_ID_INTEL,
770 .devices = pci_device_ids,
771};