Debian Codegraph @

src/southbridge/intel/i82801ix/lpc.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <cpu/intel/speedstep.h>
4 #include <console/console.h>
5 #include <device/device.h>
6 #include <device/pci.h>
7 #include <device/pci_ids.h>
8 #include <option.h>
9 #include <pc80/mc146818rtc.h>
10 #include <pc80/isa-dma.h>
11 #include <pc80/i8259.h>
12 #include <arch/io.h>
13 #include <device/pci_ops.h>
14 #include <arch/ioapic.h>
15 #include <acpi/acpi.h>
16 #include <cpu/x86/smm.h>
17 #include <acpi/acpigen.h>
18 #include "chip.h"
19 #include "i82801ix.h"
20 #include <southbridge/intel/common/pciehp.h>
21 #include <southbridge/intel/common/pmutil.h>
22 #include <southbridge/intel/common/acpi_pirq_gen.h>
23 #include <southbridge/intel/common/rcba_pirq.h>
24 #include <static.h>
25
26 #define NMI_OFF 0
27
28 typedef struct southbridge_intel_i82801ix_config config_t;
29
30 static void i82801ix_enable_apic(struct device *dev)
31 {
32 /* Enable IOAPIC. Keep APIC Range Select at zero. */
33 RCBA8(0x31ff) = 0x03;
34 /* We have to read 0x31ff back if bit0 changed. */
35 RCBA8(0x31ff);
36
37 /* Lock maximum redirection entries (MRE), R/WO register. */
38 ioapic_lock_max_vectors(IO_APIC_ADDR);
39
40 register_new_ioapic_gsi0(IO_APIC_ADDR);
41 }
42
43 static void i82801ix_enable_serial_irqs(struct device *dev)
44 {
45 /* Set packet length and toggle silent mode bit for one frame. */
46 pci_write_config8(dev, D31F0_SERIRQ_CNTL,
47 (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
48 }
49
50 /* PIRQ[n]_ROUT[3:0] - PIRQ Routing Control
51 * 0x00 - 0000 = Reserved
52 * 0x01 - 0001 = Reserved
53 * 0x02 - 0010 = Reserved
54 * 0x03 - 0011 = IRQ3
55 * 0x04 - 0100 = IRQ4
56 * 0x05 - 0101 = IRQ5
57 * 0x06 - 0110 = IRQ6
58 * 0x07 - 0111 = IRQ7
59 * 0x08 - 1000 = Reserved
60 * 0x09 - 1001 = IRQ9
61 * 0x0A - 1010 = IRQ10
62 * 0x0B - 1011 = IRQ11
63 * 0x0C - 1100 = IRQ12
64 * 0x0D - 1101 = Reserved
65 * 0x0E - 1110 = IRQ14
66 * 0x0F - 1111 = IRQ15
67 * PIRQ[n]_ROUT[7] - PIRQ Routing Control
68 * 0x80 - The PIRQ is not routed.
69 */
70
71 static void i82801ix_pirq_init(struct device *dev)
72 {
73 struct device *irq_dev;
74 /* Get the chip configuration */
75 config_t *config = dev->chip_info;
76
77 pci_write_config8(dev, D31F0_PIRQA_ROUT, config->pirqa_routing);
78 pci_write_config8(dev, D31F0_PIRQB_ROUT, config->pirqb_routing);
79 pci_write_config8(dev, D31F0_PIRQC_ROUT, config->pirqc_routing);
80 pci_write_config8(dev, D31F0_PIRQD_ROUT, config->pirqd_routing);
81
82 pci_write_config8(dev, D31F0_PIRQE_ROUT, config->pirqe_routing);
83 pci_write_config8(dev, D31F0_PIRQF_ROUT, config->pirqf_routing);
84 pci_write_config8(dev, D31F0_PIRQG_ROUT, config->pirqg_routing);
85 pci_write_config8(dev, D31F0_PIRQH_ROUT, config->pirqh_routing);
86
87 /* Eric Biederman once said we should let the OS do this.
88 * I am not so sure anymore he was right.
89 */
90
91 for (irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
92 u8 int_pin = 0, int_line = 0;
93
94 if (!is_enabled_pci(irq_dev))
95 continue;
96
97 int_pin = pci_read_config8(irq_dev, PCI_INTERRUPT_PIN);
98
99 switch (int_pin) {
100 case 1: /* INTA# */ int_line = config->pirqa_routing; break;
101 case 2: /* INTB# */ int_line = config->pirqb_routing; break;
102 case 3: /* INTC# */ int_line = config->pirqc_routing; break;
103 case 4: /* INTD# */ int_line = config->pirqd_routing; break;
104 }
105
106 if (!int_line)
107 continue;
108
109 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, int_line);
110 }
111 }
112
113 static void i82801ix_gpi_routing(struct device *dev)
114 {
115 /* Get the chip configuration */
116 config_t *config = dev->chip_info;
117 u32 reg32 = 0;
118
119 /* An array would be much nicer here, or some
120 * other method of doing this.
121 */
122 reg32 |= (config->gpi0_routing & 0x03) << 0;
123 reg32 |= (config->gpi1_routing & 0x03) << 2;
124 reg32 |= (config->gpi2_routing & 0x03) << 4;
125 reg32 |= (config->gpi3_routing & 0x03) << 6;
126 reg32 |= (config->gpi4_routing & 0x03) << 8;
127 reg32 |= (config->gpi5_routing & 0x03) << 10;
128 reg32 |= (config->gpi6_routing & 0x03) << 12;
129 reg32 |= (config->gpi7_routing & 0x03) << 14;
130 reg32 |= (config->gpi8_routing & 0x03) << 16;
131 reg32 |= (config->gpi9_routing & 0x03) << 18;
132 reg32 |= (config->gpi10_routing & 0x03) << 20;
133 reg32 |= (config->gpi11_routing & 0x03) << 22;
134 reg32 |= (config->gpi12_routing & 0x03) << 24;
135 reg32 |= (config->gpi13_routing & 0x03) << 26;
136 reg32 |= (config->gpi14_routing & 0x03) << 28;
137 reg32 |= (config->gpi15_routing & 0x03) << 30;
138
139 pci_write_config32(dev, D31F0_GPIO_ROUT, reg32);
140 }
141
142 bool southbridge_support_c5(void)
143 {
144 struct device *lpc_dev = __pci_0_1f_0;
145 struct southbridge_intel_i82801ix_config *config = lpc_dev->chip_info;
146 return config->c5_enable;
147 }
148
149 bool southbridge_support_c6(void)
150 {
151 struct device *lpc_dev = __pci_0_1f_0;
152 struct southbridge_intel_i82801ix_config *config = lpc_dev->chip_info;
153 return config->c6_enable;
154 }
155
156 static void i82801ix_power_options(struct device *dev)
157 {
158 u8 reg8;
159 u16 reg16, pmbase;
160 u32 reg32;
161 const char *state;
162 /* Get the chip configuration */
163 config_t *config = dev->chip_info;
164
165 /* BIOS must program... */
166 pci_or_config32(dev, 0xac, (1 << 30) | (3 << 8));
167
168 /* Which state do we want to goto after g3 (power restored)?
169 * 0 == S0 Full On
170 * 1 == S5 Soft Off
171 *
172 * If the option is not existent (Laptops), use MAINBOARD_POWER_ON.
173 */
174 const unsigned int pwr_on = get_uint_option("power_on_after_fail", MAINBOARD_POWER_ON);
175
176 reg8 = pci_read_config8(dev, D31F0_GEN_PMCON_3);
177 reg8 &= 0xfe;
178 switch (pwr_on) {
179 case MAINBOARD_POWER_OFF:
180 reg8 |= 1;
181 state = "off";
182 break;
183 case MAINBOARD_POWER_ON:
184 reg8 &= ~1;
185 state = "on";
186 break;
187 case MAINBOARD_POWER_KEEP:
188 reg8 &= ~1;
189 state = "state keep";
190 break;
191 default:
192 state = "undefined";
193 }
194
195 reg8 |= (3 << 4); /* avoid #S4 assertions */
196 reg8 &= ~(1 << 3); /* minimum assertion is 1 to 2 RTCCLK */
197
198 pci_write_config8(dev, D31F0_GEN_PMCON_3, reg8);
199 printk(BIOS_INFO, "Set power %s after power failure.\n", state);
200
201 /* Set up NMI on errors. */
202 reg8 = inb(0x61);
203 reg8 &= 0x0f; /* Higher Nibble must be 0 */
204 reg8 &= ~(1 << 3); /* IOCHK# NMI Enable */
205 // reg8 &= ~(1 << 2); /* PCI SERR# Enable */
206 reg8 |= (1 << 2); /* PCI SERR# Disable for now */
207 outb(reg8, 0x61);
208
209 reg8 = inb(0x74); /* Read from 0x74 as 0x70 is write only. */
210 const unsigned int nmi_option = get_uint_option("nmi", NMI_OFF);
211 if (nmi_option) {
212 printk(BIOS_INFO, "NMI sources enabled.\n");
213 reg8 &= ~(1 << 7); /* Set NMI. */
214 } else {
215 printk(BIOS_INFO, "NMI sources disabled.\n");
216 reg8 |= (1 << 7); /* Can't mask NMI from PCI-E and NMI_NOW */
217 }
218 outb(reg8, 0x70);
219
220 /* Enable CPU_SLP# and Intel Speedstep, set SMI# rate down */
221 reg16 = pci_read_config16(dev, D31F0_GEN_PMCON_1);
222 reg16 &= ~(3 << 0); // SMI# rate 1 minute
223 reg16 |= (1 << 2); // CLKRUN_EN - Mobile/Ultra only
224 reg16 |= (1 << 3); // Speedstep Enable - Mobile/Ultra only
225 reg16 |= (1 << 5); // CPUSLP_EN Desktop only
226
227 if (config->c4onc3_enable)
228 reg16 |= (1 << 7);
229
230 // another laptop wants this?
231 // reg16 &= ~(1 << 10); // BIOS_PCI_EXP_EN - Desktop/Mobile only
232 reg16 |= (1 << 10); // BIOS_PCI_EXP_EN - Desktop/Mobile only
233 if (CONFIG(DEBUG_PERIODIC_SMI))
234 reg16 |= (3 << 0); // Periodic SMI every 8s
235 if (southbridge_support_c5())
236 reg16 |= (1 << 11); /* Enable C5, C6 and PMSYNC# */
237 pci_write_config16(dev, D31F0_GEN_PMCON_1, reg16);
238
239 /* Set exit timings for C5/C6. */
240 if (southbridge_support_c5()) {
241 reg8 = pci_read_config8(dev, D31F0_C5_EXIT_TIMING);
242 reg8 &= ~((7 << 3) | (7 << 0));
243 if (southbridge_support_c6())
244 reg8 |= (5 << 3) | (3 << 0); /* 38-44us PMSYNC# to STPCLK#,
245 95-102us DPRSTP# to STP_CPU# */
246 else
247 reg8 |= (0 << 3) | (1 << 0); /* 16-17us PMSYNC# to STPCLK#,
248 34-40us DPRSTP# to STP_CPU# */
249 pci_write_config8(dev, D31F0_C5_EXIT_TIMING, reg8);
250 }
251
252 // Set the board's GPI routing.
253 i82801ix_gpi_routing(dev);
254
255 pmbase = pci_read_config16(dev, 0x40) & 0xfffe;
256
257 outl(config->gpe0_en, pmbase + 0x28);
258 outw(config->alt_gp_smi_en, pmbase + 0x38);
259
260 /* Set up power management block and determine sleep mode */
261 reg16 = inw(pmbase + 0x00); /* PM1_STS */
262 outw(reg16, pmbase + 0x00); /* Clear status bits. At least bit11 (power
263 button override) must be cleared or SCI
264 will be constantly fired and OSPM must
265 not know about it (ACPI spec says to
266 ignore the bit). */
267
268 /* Set duty cycle for hardware throttling (defaults to 0x0: 50%). */
269 reg32 = inl(pmbase + 0x10);
270 reg32 &= ~(7 << 5);
271 reg32 |= (config->throttle_duty & 7) << 5;
272 outl(reg32, pmbase + 0x10);
273 }
274
275 static void i82801ix_configure_cstates(struct device *dev)
276 {
277 // Enable Popup & Popdown
278 pci_or_config8(dev, D31F0_CxSTATE_CNF, (1 << 4) | (1 << 3) | (1 << 2));
279
280 // Set Deeper Sleep configuration to recommended values
281 // Deeper Sleep to Stop CPU: 34-40us
282 // Deeper Sleep to Sleep: 15us
283 pci_update_config8(dev, D31F0_C4TIMING_CNT, ~0x0f, (2 << 2) | (2 << 0));
284
285 /* We could enable slow-C4 exit here, if someone needs it? */
286 }
287
288 static void i82801ix_rtc_init(struct device *dev)
289 {
290 u8 reg8;
291 int rtc_failed;
292
293 reg8 = pci_read_config8(dev, D31F0_GEN_PMCON_3);
294 rtc_failed = reg8 & RTC_BATTERY_DEAD;
295 if (rtc_failed) {
296 reg8 &= ~RTC_BATTERY_DEAD;
297 pci_write_config8(dev, D31F0_GEN_PMCON_3, reg8);
298 }
299 printk(BIOS_DEBUG, "rtc_failed = 0x%x\n", rtc_failed);
300
301 cmos_init(rtc_failed);
302 }
303
304 static void enable_hpet(void)
305 {
306 u32 reg32;
307
308 /* Move HPET to default address 0xfed00000 and enable it */
309 reg32 = RCBA32(RCBA_HPTC);
310 reg32 |= (1 << 7); // HPET Address Enable
311 reg32 &= ~(3 << 0);
312 RCBA32(RCBA_HPTC) = reg32;
313 }
314
315 static void enable_clock_gating(void)
316 {
317 u32 reg32;
318
319 /* Enable DMI dynamic clock gating. */
320 RCBA32(RCBA_DMIC) |= 3;
321
322 /* Enable Clock Gating for most devices. */
323 reg32 = RCBA32(RCBA_CG);
324 reg32 |= (1 << 31); /* LPC dynamic clock gating */
325 /* USB UHCI dynamic clock gating: */
326 reg32 |= (1 << 29) | (1 << 28);
327 /* SATA dynamic clock gating [0-3]: */
328 reg32 |= (1 << 27) | (1 << 26) | (1 << 25) | (1 << 24);
329 reg32 |= (1 << 23); /* LAN static clock gating (if LAN disabled) */
330 reg32 |= (1 << 22); /* HD audio dynamic clock gating */
331 reg32 &= ~(1 << 21); /* No HD audio static clock gating */
332 reg32 &= ~(1 << 20); /* No USB EHCI static clock gating */
333 reg32 |= (1 << 19); /* USB EHCI dynamic clock gating */
334 /* More SATA dynamic clock gating [4-5]: */
335 reg32 |= (1 << 18) | (1 << 17);
336 reg32 |= (1 << 16); /* PCI dynamic clock gating */
337 /* PCIe, DMI dynamic clock gating: */
338 reg32 |= (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1);
339 reg32 |= (1 << 0); /* PCIe root port static clock gating */
340 RCBA32(RCBA_CG) = reg32;
341
342 /* Enable SPI dynamic clock gating. */
343 RCBA32(0x38c0) |= 7;
344 }
345
346 static void i82801ix_set_acpi_mode(struct device *dev)
347 {
348 if (!acpi_is_wakeup_s3()) {
349 apm_control(APM_CNT_ACPI_DISABLE);
350 } else {
351 apm_control(APM_CNT_ACPI_ENABLE);
352 }
353 }
354
355 static void lpc_init(struct device *dev)
356 {
357 printk(BIOS_DEBUG, "i82801ix: %s\n", __func__);
358
359 /* IO APIC initialization. */
360 i82801ix_enable_apic(dev);
361
362 i82801ix_enable_serial_irqs(dev);
363
364 /* Setup the PIRQ. */
365 i82801ix_pirq_init(dev);
366
367 /* Setup power options. */
368 i82801ix_power_options(dev);
369
370 /* Configure Cx state registers */
371 if (LPC_IS_MOBILE(dev))
372 i82801ix_configure_cstates(dev);
373
374 /* Initialize the real time clock. */
375 i82801ix_rtc_init(dev);
376
377 /* Initialize ISA DMA. */
378 isa_dma_init();
379
380 /* Initialize the High Precision Event Timers, if present. */
381 enable_hpet();
382
383 /* Initialize Clock Gating */
384 enable_clock_gating();
385
386 setup_i8259();
387
388 /* The OS should do this? */
389 /* Interrupt 9 should be level triggered (SCI) */
390 i8259_configure_irq_trigger(9, 1);
391
392 i82801ix_set_acpi_mode(dev);
393 }
394
395 static void i82801ix_lpc_read_resources(struct device *dev)
396 {
397 /*
398 * I/O Resources
399 *
400 * 0x0000 - 0x000f....ISA DMA
401 * 0x0010 - 0x001f....ISA DMA aliases
402 * 0x0020 ~ 0x003d....PIC
403 * 0x002e - 0x002f....Maybe Super I/O
404 * 0x0040 - 0x0043....Timer
405 * 0x004e - 0x004f....Maybe Super I/O
406 * 0x0050 - 0x0053....Timer aliases
407 * 0x0061.............NMI_SC
408 * 0x0070.............NMI_EN (readable in alternative access mode)
409 * 0x0070 - 0x0077....RTC
410 * 0x0080 - 0x008f....ISA DMA
411 * 0x0090 ~ 0x009f....ISA DMA aliases
412 * 0x0092.............Fast A20 and Init
413 * 0x00a0 ~ 0x00bd....PIC
414 * 0x00b2 - 0x00b3....APM
415 * 0x00c0 ~ 0x00de....ISA DMA
416 * 0x00c1 ~ 0x00df....ISA DMA aliases
417 * 0x00f0.............Coprocessor Error
418 * (0x0400-0x041f)....SMBus (CONFIG_FIXED_SMBUS_IO_BASE, during raminit)
419 * 0x04d0 - 0x04d1....PIC
420 * 0x0500 - 0x057f....PM (DEFAULT_PMBASE)
421 * 0x0580 - 0x05bf....SB GPIO (DEFAULT_GPIOBASE)
422 * 0x05c0 - 0x05ff....SB GPIO cont. (mobile only)
423 * 0x0cf8 - 0x0cff....PCI
424 * 0x0cf9.............Reset Control
425 */
426
427 struct resource *res;
428
429 /* Get the normal PCI resources of this device. */
430 pci_dev_read_resources(dev);
431
432 /* Add an extra subtractive resource for both memory and I/O. */
433 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
434 res->base = 0;
435 res->size = 0x1000;
436 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
437 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
438
439 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
440 res->base = 0xff800000;
441 res->size = 0x00800000; /* 8 MB for flash */
442 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
443 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
444
445 res = new_resource(dev, 3); /* IOAPIC */
446 res->base = IO_APIC_ADDR;
447 res->size = 0x00001000;
448 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
449 }
450
451 static const char *lpc_acpi_name(const struct device *dev)
452 {
453 return "LPCB";
454 }
455
456 static void southbridge_fill_ssdt(const struct device *device)
457 {
458 struct device *dev = pcidev_on_root(0x1f, 0);
459 config_t *chip = dev->chip_info;
460
461 intel_acpi_pcie_hotplug_generator(chip->pcie_hotplug_map, 8);
462 intel_acpi_gen_def_acpi_pirq(device);
463 }
464
465 static struct device_operations device_ops = {
466 .read_resources = i82801ix_lpc_read_resources,
467 .set_resources = pci_dev_set_resources,
468 .enable_resources = pci_dev_enable_resources,
469 .write_acpi_tables = acpi_write_hpet,
470 .acpi_fill_ssdt = southbridge_fill_ssdt,
471 .acpi_name = lpc_acpi_name,
472 .init = lpc_init,
473 .scan_bus = scan_static_bus,
474 .ops_pci = &pci_dev_ops_pci,
475 };
476
477 static const unsigned short pci_device_ids[] = {
478 PCI_DID_INTEL_82801IH_LPC, /* ICH9DH */
479 PCI_DID_INTEL_82801IO_LPC, /* ICH9DO */
480 PCI_DID_INTEL_82801IR_LPC, /* ICH9R */
481 PCI_DID_INTEL_82801IEM_LPC, /* ICH9M-E */
482 PCI_DID_INTEL_82801IB_LPC, /* ICH9 */
483 PCI_DID_INTEL_82801IBM_LPC, /* ICH9M */
484 0
485 };
486
487 static const struct pci_driver ich9_lpc __pci_driver = {
488 .ops = &device_ops,
489 .vendor = PCI_VID_INTEL,
490 .devices = pci_device_ids,
491 };