2 * This file is part of the coreboot project.
4 * Copyright (C) 2008-2009 coresystems GmbH
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <console/console.h>
22 #include <device/device.h>
23 #include <device/pci.h>
24 #include <device/pci_ids.h>
25 #include <pc80/mc146818rtc.h>
26 #include <pc80/isa-dma.h>
27 #include <pc80/i8259.h>
33 #define ENABLE_ACPI_MODE_IN_COREBOOT 0
34 #define TEST_SMM_FLASH_LOCKDOWN 0
36 typedef struct southbridge_intel_i82801gx_config config_t;
38 static void i82801gx_enable_apic(struct device *dev)
42 volatile u32 *ioapic_index = (volatile u32 *)(IO_APIC_ADDR);
43 volatile u32 *ioapic_data = (volatile u32 *)(IO_APIC_ADDR + 0x10);
45 /* Enable ACPI I/O and power management.
48 pci_write_config8(dev, ACPI_CNTL, 0x80);
51 *ioapic_data = (1 << 25);
55 printk(BIOS_DEBUG, "Southbridge APIC ID = %x\n", (reg32 >> 24) & 0x0f);
56 if (reg32 != (1 << 25))
59 printk(BIOS_SPEW, "Dumping IOAPIC registers\n");
62 printk(BIOS_SPEW, " reg 0x%04x:", i);
64 printk(BIOS_SPEW, " 0x%08x\n", reg32);
67 *ioapic_index = 3; /* Select Boot Configuration register. */
68 *ioapic_data = 1; /* Use Processor System Bus to deliver interrupts. */
71 static void i82801gx_enable_serial_irqs(struct device *dev)
73 /* Set packet length and toggle silent mode bit for one frame. */
74 pci_write_config8(dev, SERIRQ_CNTL,
75 (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
78 /* PIRQ[n]_ROUT[3:0] - PIRQ Routing Control
79 * 0x00 - 0000 = Reserved
80 * 0x01 - 0001 = Reserved
81 * 0x02 - 0010 = Reserved
87 * 0x08 - 1000 = Reserved
92 * 0x0D - 1101 = Reserved
95 * PIRQ[n]_ROUT[7] - PIRQ Routing Control
96 * 0x80 - The PIRQ is not routed.
99 static void i82801gx_pirq_init(device_t dev)
102 /* Get the chip configuration */
103 config_t *config = dev->chip_info;
105 pci_write_config8(dev, PIRQA_ROUT, config->pirqa_routing);
106 pci_write_config8(dev, PIRQB_ROUT, config->pirqb_routing);
107 pci_write_config8(dev, PIRQC_ROUT, config->pirqc_routing);
108 pci_write_config8(dev, PIRQD_ROUT, config->pirqd_routing);
110 pci_write_config8(dev, PIRQE_ROUT, config->pirqe_routing);
111 pci_write_config8(dev, PIRQF_ROUT, config->pirqf_routing);
112 pci_write_config8(dev, PIRQG_ROUT, config->pirqg_routing);
113 pci_write_config8(dev, PIRQH_ROUT, config->pirqh_routing);
115 /* Eric Biederman once said we should let the OS do this.
116 * I am not so sure anymore he was right.
119 for(irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
120 u8 int_pin=0, int_line=0;
122 if (!irq_dev->enabled || irq_dev->path.type != DEVICE_PATH_PCI)
125 int_pin = pci_read_config8(irq_dev, PCI_INTERRUPT_PIN);
128 case 1: /* INTA# */ int_line = config->pirqa_routing; break;
129 case 2: /* INTB# */ int_line = config->pirqb_routing; break;
130 case 3: /* INTC# */ int_line = config->pirqc_routing; break;
131 case 4: /* INTD# */ int_line = config->pirqd_routing; break;
137 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, int_line);
141 static void i82801gx_gpi_routing(device_t dev)
143 /* Get the chip configuration */
144 config_t *config = dev->chip_info;
147 /* An array would be much nicer here, or some
148 * other method of doing this.
150 reg32 |= (config->gpi0_routing & 0x03) << 0;
151 reg32 |= (config->gpi1_routing & 0x03) << 2;
152 reg32 |= (config->gpi2_routing & 0x03) << 4;
153 reg32 |= (config->gpi3_routing & 0x03) << 6;
154 reg32 |= (config->gpi4_routing & 0x03) << 8;
155 reg32 |= (config->gpi5_routing & 0x03) << 10;
156 reg32 |= (config->gpi6_routing & 0x03) << 12;
157 reg32 |= (config->gpi7_routing & 0x03) << 14;
158 reg32 |= (config->gpi8_routing & 0x03) << 16;
159 reg32 |= (config->gpi9_routing & 0x03) << 18;
160 reg32 |= (config->gpi10_routing & 0x03) << 20;
161 reg32 |= (config->gpi11_routing & 0x03) << 22;
162 reg32 |= (config->gpi12_routing & 0x03) << 24;
163 reg32 |= (config->gpi13_routing & 0x03) << 26;
164 reg32 |= (config->gpi14_routing & 0x03) << 28;
165 reg32 |= (config->gpi15_routing & 0x03) << 30;
167 pci_write_config32(dev, 0xb8, reg32);
170 extern u8 acpi_slp_type;
172 static void i82801gx_power_options(device_t dev)
178 /* Get the chip configuration */
179 config_t *config = dev->chip_info;
181 int pwr_on=CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
184 /* Which state do we want to goto after g3 (power restored)?
188 * If the option is not existent (Laptops), use MAINBOARD_POWER_ON.
190 if (get_option(&pwr_on, "power_on_after_fail") < 0)
191 pwr_on = MAINBOARD_POWER_ON;
193 reg8 = pci_read_config8(dev, GEN_PMCON_3);
196 case MAINBOARD_POWER_OFF:
200 case MAINBOARD_POWER_ON:
204 case MAINBOARD_POWER_KEEP:
206 state = "state keep";
212 reg8 |= (3 << 4); /* avoid #S4 assertions */
213 reg8 &= ~(1 << 3); /* minimum asssertion is 1 to 2 RTCCLK */
215 pci_write_config8(dev, GEN_PMCON_3, reg8);
216 printk(BIOS_INFO, "Set power %s after power failure.\n", state);
218 /* Set up NMI on errors. */
220 reg8 &= 0x0f; /* Higher Nibble must be 0 */
221 reg8 &= ~(1 << 3); /* IOCHK# NMI Enable */
222 // reg8 &= ~(1 << 2); /* PCI SERR# Enable */
223 reg8 |= (1 << 2); /* PCI SERR# Disable for now */
227 nmi_option = NMI_OFF;
228 get_option(&nmi_option, "nmi");
230 printk(BIOS_INFO, "NMI sources enabled.\n");
231 reg8 &= ~(1 << 7); /* Set NMI. */
233 printk(BIOS_INFO, "NMI sources disabled.\n");
234 reg8 |= ( 1 << 7); /* Can't mask NMI from PCI-E and NMI_NOW */
238 /* Enable CPU_SLP# and Intel Speedstep, set SMI# rate down */
239 reg16 = pci_read_config16(dev, GEN_PMCON_1);
240 reg16 &= ~(3 << 0); // SMI# rate 1 minute
241 reg16 |= (1 << 2); // CLKRUN_EN - Mobile/Ultra only
242 reg16 |= (1 << 3); // Speedstep Enable - Mobile/Ultra only
243 reg16 |= (1 << 5); // CPUSLP_EN Desktop only
244 // another laptop wants this?
245 // reg16 &= ~(1 << 10); // BIOS_PCI_EXP_EN - Desktop/Mobile only
246 reg16 |= (1 << 10); // BIOS_PCI_EXP_EN - Desktop/Mobile only
247 #if DEBUG_PERIODIC_SMIS
248 /* Set DEBUG_PERIODIC_SMIS in i82801gx.h to debug using
251 reg16 |= (3 << 0); // Periodic SMI every 8s
253 pci_write_config16(dev, GEN_PMCON_1, reg16);
255 // Set the board's GPI routing.
256 i82801gx_gpi_routing(dev);
258 pmbase = pci_read_config16(dev, 0x40) & 0xfffe;
260 outl(config->gpe0_en, pmbase + GPE0_EN);
261 outw(config->alt_gp_smi_en, pmbase + ALT_GP_SMI_EN);
263 /* Set up power management block and determine sleep mode */
264 reg32 = inl(pmbase + 0x04); // PM1_CNT
266 reg32 &= ~(7 << 10); // SLP_TYP
267 reg32 |= (1 << 1); // enable C3->C0 transition on bus master
268 reg32 |= (1 << 0); // SCI_EN
269 outl(reg32, pmbase + 0x04);
272 static void i82801gx_configure_cstates(device_t dev)
276 reg8 = pci_read_config8(dev, 0xa9); // Cx state configuration
277 reg8 |= (1 << 4) | (1 << 3) | (1 << 2); // Enable Popup & Popdown
278 pci_write_config8(dev, 0xa9, reg8);
280 // Set Deeper Sleep configuration to recommended values
281 reg8 = pci_read_config8(dev, 0xaa);
283 reg8 |= (2 << 2); // Deeper Sleep to Stop CPU: 34-40us
284 reg8 |= (2 << 0); // Deeper Sleep to Sleep: 15us
285 pci_write_config8(dev, 0xaa, reg8);
288 static void i82801gx_rtc_init(struct device *dev)
293 reg8 = pci_read_config8(dev, GEN_PMCON_3);
294 rtc_failed = reg8 & RTC_BATTERY_DEAD;
296 reg8 &= ~RTC_BATTERY_DEAD;
297 pci_write_config8(dev, GEN_PMCON_3, reg8);
299 printk(BIOS_DEBUG, "rtc_failed = 0x%x\n", rtc_failed);
301 rtc_init(rtc_failed);
304 static void enable_hpet(void)
308 /* Move HPET to default address 0xfed00000 and enable it */
309 reg32 = RCBA32(HPTC);
310 reg32 |= (1 << 7); // HPET Address Enable
312 RCBA32(HPTC) = reg32;
315 static void enable_clock_gating(void)
319 /* Enable Clock Gating for most devices */
321 reg32 |= (1 << 31); // LPC clock gating
322 reg32 |= (1 << 30); // PATA clock gating
324 reg32 |= (1 << 27) | (1 << 26) | (1 << 25) | (1 << 24);
325 reg32 |= (1 << 23); // AC97 clock gating
326 reg32 |= (1 << 19); // USB EHCI clock gating
327 reg32 |= (1 << 3) | (1 << 1); // DMI clock gating
328 reg32 |= (1 << 2); // PCIe clock gating;
329 reg32 &= ~(1 << 20); // No static clock gating for USB
330 reg32 &= ~( (1 << 29) | (1 << 28) ); // Disable UHCI clock gating
334 #if CONFIG_HAVE_SMI_HANDLER
335 static void i82801gx_lock_smm(struct device *dev)
338 #if TEST_SMM_FLASH_LOCKDOWN
342 #if ENABLE_ACPI_MODE_IN_COREBOOT
343 printk(BIOS_DEBUG, "Enabling ACPI via APMC:\n");
344 outb(0xe1, 0xb2); // Enable ACPI mode
345 printk(BIOS_DEBUG, "done.\n");
347 printk(BIOS_DEBUG, "Disabling ACPI via APMC:\n");
348 outb(0x1e, 0xb2); // Disable ACPI mode
349 printk(BIOS_DEBUG, "done.\n");
351 /* Don't allow evil boot loaders, kernels, or
352 * userspace applications to deceive us:
356 #if TEST_SMM_FLASH_LOCKDOWN
358 printk(BIOS_DEBUG, "Locking BIOS to RO... ");
359 reg8 = pci_read_config8(dev, 0xdc); /* BIOS_CNTL */
360 printk(BIOS_DEBUG, " BLE: %s; BWE: %s\n", (reg8&2)?"on":"off",
362 reg8 &= ~(1 << 0); /* clear BIOSWE */
363 pci_write_config8(dev, 0xdc, reg8);
364 reg8 |= (1 << 1); /* set BLE */
365 pci_write_config8(dev, 0xdc, reg8);
366 printk(BIOS_DEBUG, "ok.\n");
367 reg8 = pci_read_config8(dev, 0xdc); /* BIOS_CNTL */
368 printk(BIOS_DEBUG, " BLE: %s; BWE: %s\n", (reg8&2)?"on":"off",
371 printk(BIOS_DEBUG, "Writing:\n");
372 *(volatile u8 *)0xfff00000 = 0x00;
373 printk(BIOS_DEBUG, "Testing:\n");
374 reg8 |= (1 << 0); /* set BIOSWE */
375 pci_write_config8(dev, 0xdc, reg8);
377 reg8 = pci_read_config8(dev, 0xdc); /* BIOS_CNTL */
378 printk(BIOS_DEBUG, " BLE: %s; BWE: %s\n", (reg8&2)?"on":"off",
380 printk(BIOS_DEBUG, "Done.\n");
385 #define SPIBASE 0x3020
386 static void i82801gx_spi_init(void)
390 spicontrol = RCBA16(SPIBASE + 2);
391 spicontrol &= ~(1 << 0); // SPI Access Request
392 RCBA16(SPIBASE + 2) = spicontrol;
395 static void i82801gx_fixups(struct device *dev)
397 /* This needs to happen after PCI enumeration */
400 /* USB Transient Disconnect Detect:
401 * Prevent a SE0 condition on the USB ports from being
402 * interpreted by the UHCI controller as a disconnect
404 pci_write_config8(dev, 0xad, 0x3);
407 static void lpc_init(struct device *dev)
409 printk(BIOS_DEBUG, "i82801gx: lpc_init\n");
411 /* Set the value for PCI command register. */
412 pci_write_config16(dev, PCI_COMMAND, 0x000f);
414 /* IO APIC initialization. */
415 i82801gx_enable_apic(dev);
417 i82801gx_enable_serial_irqs(dev);
419 /* Setup the PIRQ. */
420 i82801gx_pirq_init(dev);
422 /* Setup power options. */
423 i82801gx_power_options(dev);
425 /* Configure Cx state registers */
426 i82801gx_configure_cstates(dev);
428 /* Set the state of the GPIO lines. */
431 /* Initialize the real time clock. */
432 i82801gx_rtc_init(dev);
434 /* Initialize ISA DMA. */
437 /* Initialize the High Precision Event Timers, if present. */
440 /* Initialize Clock Gating */
441 enable_clock_gating();
445 /* The OS should do this? */
446 /* Interrupt 9 should be level triggered (SCI) */
447 i8259_configure_irq_trigger(9, 1);
449 #if CONFIG_HAVE_SMI_HANDLER
450 i82801gx_lock_smm(dev);
455 i82801gx_fixups(dev);
458 static void i82801gx_lpc_read_resources(device_t dev)
460 struct resource *res;
462 /* Get the normal PCI resources of this device. */
463 pci_dev_read_resources(dev);
465 /* Add an extra subtractive resource for both memory and I/O. */
466 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
469 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
470 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
472 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
473 res->base = 0xff800000;
474 res->size = 0x00800000; /* 8 MB for flash */
475 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
476 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
478 res = new_resource(dev, 3); /* IOAPIC */
479 res->base = 0xfec00000;
480 res->size = 0x00001000;
481 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
484 static void set_subsystem(device_t dev, unsigned vendor, unsigned device)
486 if (!vendor || !device) {
487 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
488 pci_read_config32(dev, PCI_VENDOR_ID));
490 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
491 ((device & 0xffff) << 16) | (vendor & 0xffff));
495 static struct pci_operations pci_ops = {
496 .set_subsystem = set_subsystem,
499 static struct device_operations device_ops = {
500 .read_resources = i82801gx_lpc_read_resources,
501 .set_resources = pci_dev_set_resources,
502 .enable_resources = pci_dev_enable_resources,
504 .scan_bus = scan_static_bus,
505 .enable = i82801gx_enable,
509 /* 82801GH (ICH7 DH) */
510 static const struct pci_driver ich7_dh_lpc __pci_driver = {
512 .vendor = PCI_VENDOR_ID_INTEL,
516 /* 82801GB/GR (ICH7/ICH7R) */
517 static const struct pci_driver ich7_ich7r_lpc __pci_driver = {
519 .vendor = PCI_VENDOR_ID_INTEL,
523 /* 82801GBM/GU (ICH7-M/ICH7-U) */
524 static const struct pci_driver ich7m_ich7u_lpc __pci_driver = {
526 .vendor = PCI_VENDOR_ID_INTEL,
530 /* 82801GHM (ICH7-M DH) */
531 static const struct pci_driver ich7m_dh_lpc __pci_driver = {
533 .vendor = PCI_VENDOR_ID_INTEL,