i82801gx: enable ACPI during S3 resume
[coreboot.git] / src / southbridge / intel / i82801gx / lpc.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008-2009 coresystems GmbH
5  *
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
9  * the License.
10  *
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.
15  *
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
19  */
20
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>
28 #include <arch/io.h>
29 #include <arch/ioapic.h>
30 #include <cpu/cpu.h>
31 #include "i82801gx.h"
32 #include <cpu/x86/smm.h>
33
34 #define NMI_OFF 0
35
36 #define ENABLE_ACPI_MODE_IN_COREBOOT    0
37 #define TEST_SMM_FLASH_LOCKDOWN         0
38
39 typedef struct southbridge_intel_i82801gx_config config_t;
40
41 static void i82801gx_enable_apic(struct device *dev)
42 {
43         int i;
44         u32 reg32;
45         volatile u32 *ioapic_index = (volatile u32 *)(IO_APIC_ADDR);
46         volatile u32 *ioapic_data = (volatile u32 *)(IO_APIC_ADDR + 0x10);
47
48         /* Enable ACPI I/O and power management.
49          * Set SCI IRQ to IRQ9
50          */
51         pci_write_config8(dev, ACPI_CNTL, 0x80);
52
53         *ioapic_index = 0;
54         *ioapic_data = (1 << 25);
55
56         *ioapic_index = 0;
57         reg32 = *ioapic_data;
58         printk(BIOS_DEBUG, "Southbridge APIC ID = %x\n", (reg32 >> 24) & 0x0f);
59         if (reg32 != (1 << 25))
60                 die("APIC Error\n");
61
62         printk(BIOS_SPEW, "Dumping IOAPIC registers\n");
63         for (i=0; i<3; i++) {
64                 *ioapic_index = i;
65                 printk(BIOS_SPEW, "  reg 0x%04x:", i);
66                 reg32 = *ioapic_data;
67                 printk(BIOS_SPEW, " 0x%08x\n", reg32);
68         }
69
70         *ioapic_index = 3; /* Select Boot Configuration register. */
71         *ioapic_data = 1; /* Use Processor System Bus to deliver interrupts. */
72 }
73
74 static void i82801gx_enable_serial_irqs(struct device *dev)
75 {
76         /* Set packet length and toggle silent mode bit for one frame. */
77         pci_write_config8(dev, SERIRQ_CNTL,
78                           (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
79 }
80
81 /* PIRQ[n]_ROUT[3:0] - PIRQ Routing Control
82  * 0x00 - 0000 = Reserved
83  * 0x01 - 0001 = Reserved
84  * 0x02 - 0010 = Reserved
85  * 0x03 - 0011 = IRQ3
86  * 0x04 - 0100 = IRQ4
87  * 0x05 - 0101 = IRQ5
88  * 0x06 - 0110 = IRQ6
89  * 0x07 - 0111 = IRQ7
90  * 0x08 - 1000 = Reserved
91  * 0x09 - 1001 = IRQ9
92  * 0x0A - 1010 = IRQ10
93  * 0x0B - 1011 = IRQ11
94  * 0x0C - 1100 = IRQ12
95  * 0x0D - 1101 = Reserved
96  * 0x0E - 1110 = IRQ14
97  * 0x0F - 1111 = IRQ15
98  * PIRQ[n]_ROUT[7] - PIRQ Routing Control
99  * 0x80 - The PIRQ is not routed.
100  */
101
102 static void i82801gx_pirq_init(device_t dev)
103 {
104         device_t irq_dev;
105         /* Get the chip configuration */
106         config_t *config = dev->chip_info;
107
108         pci_write_config8(dev, PIRQA_ROUT, config->pirqa_routing);
109         pci_write_config8(dev, PIRQB_ROUT, config->pirqb_routing);
110         pci_write_config8(dev, PIRQC_ROUT, config->pirqc_routing);
111         pci_write_config8(dev, PIRQD_ROUT, config->pirqd_routing);
112
113         pci_write_config8(dev, PIRQE_ROUT, config->pirqe_routing);
114         pci_write_config8(dev, PIRQF_ROUT, config->pirqf_routing);
115         pci_write_config8(dev, PIRQG_ROUT, config->pirqg_routing);
116         pci_write_config8(dev, PIRQH_ROUT, config->pirqh_routing);
117
118         /* Eric Biederman once said we should let the OS do this.
119          * I am not so sure anymore he was right.
120          */
121
122         for(irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
123                 u8 int_pin=0, int_line=0;
124
125                 if (!irq_dev->enabled || irq_dev->path.type != DEVICE_PATH_PCI)
126                         continue;
127
128                 int_pin = pci_read_config8(irq_dev, PCI_INTERRUPT_PIN);
129
130                 switch (int_pin) {
131                 case 1: /* INTA# */ int_line = config->pirqa_routing; break;
132                 case 2: /* INTB# */ int_line = config->pirqb_routing; break;
133                 case 3: /* INTC# */ int_line = config->pirqc_routing; break;
134                 case 4: /* INTD# */ int_line = config->pirqd_routing; break;
135                 }
136
137                 if (!int_line)
138                         continue;
139
140                 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, int_line);
141         }
142 }
143
144 static void i82801gx_gpi_routing(device_t dev)
145 {
146         /* Get the chip configuration */
147         config_t *config = dev->chip_info;
148         u32 reg32 = 0;
149
150         /* An array would be much nicer here, or some
151          * other method of doing this.
152          */
153         reg32 |= (config->gpi0_routing & 0x03) << 0;
154         reg32 |= (config->gpi1_routing & 0x03) << 2;
155         reg32 |= (config->gpi2_routing & 0x03) << 4;
156         reg32 |= (config->gpi3_routing & 0x03) << 6;
157         reg32 |= (config->gpi4_routing & 0x03) << 8;
158         reg32 |= (config->gpi5_routing & 0x03) << 10;
159         reg32 |= (config->gpi6_routing & 0x03) << 12;
160         reg32 |= (config->gpi7_routing & 0x03) << 14;
161         reg32 |= (config->gpi8_routing & 0x03) << 16;
162         reg32 |= (config->gpi9_routing & 0x03) << 18;
163         reg32 |= (config->gpi10_routing & 0x03) << 20;
164         reg32 |= (config->gpi11_routing & 0x03) << 22;
165         reg32 |= (config->gpi12_routing & 0x03) << 24;
166         reg32 |= (config->gpi13_routing & 0x03) << 26;
167         reg32 |= (config->gpi14_routing & 0x03) << 28;
168         reg32 |= (config->gpi15_routing & 0x03) << 30;
169
170         pci_write_config32(dev, 0xb8, reg32);
171 }
172
173 extern u8 acpi_slp_type;
174
175 static void i82801gx_power_options(device_t dev)
176 {
177         u8 reg8;
178         u16 reg16, pmbase;
179         u32 reg32;
180         const char *state;
181         /* Get the chip configuration */
182         config_t *config = dev->chip_info;
183
184         int pwr_on=CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
185         int nmi_option;
186
187         /* Which state do we want to goto after g3 (power restored)?
188          * 0 == S0 Full On
189          * 1 == S5 Soft Off
190          *
191          * If the option is not existent (Laptops), use MAINBOARD_POWER_ON.
192          */
193         if (get_option(&pwr_on, "power_on_after_fail") < 0)
194                 pwr_on = MAINBOARD_POWER_ON;
195
196         reg8 = pci_read_config8(dev, GEN_PMCON_3);
197         reg8 &= 0xfe;
198         switch (pwr_on) {
199         case MAINBOARD_POWER_OFF:
200                 reg8 |= 1;
201                 state = "off";
202                 break;
203         case MAINBOARD_POWER_ON:
204                 reg8 &= ~1;
205                 state = "on";
206                 break;
207         case MAINBOARD_POWER_KEEP:
208                 reg8 &= ~1;
209                 state = "state keep";
210                 break;
211         default:
212                 state = "undefined";
213         }
214
215         reg8 |= (3 << 4);       /* avoid #S4 assertions */
216         reg8 &= ~(1 << 3);      /* minimum asssertion is 1 to 2 RTCCLK */
217
218         pci_write_config8(dev, GEN_PMCON_3, reg8);
219         printk(BIOS_INFO, "Set power %s after power failure.\n", state);
220
221         /* Set up NMI on errors. */
222         reg8 = inb(0x61);
223         reg8 &= 0x0f;           /* Higher Nibble must be 0 */
224         reg8 &= ~(1 << 3);      /* IOCHK# NMI Enable */
225         // reg8 &= ~(1 << 2);   /* PCI SERR# Enable */
226         reg8 |= (1 << 2); /* PCI SERR# Disable for now */
227         outb(reg8, 0x61);
228
229         reg8 = inb(0x70);
230         nmi_option = NMI_OFF;
231         get_option(&nmi_option, "nmi");
232         if (nmi_option) {
233                 printk(BIOS_INFO, "NMI sources enabled.\n");
234                 reg8 &= ~(1 << 7);      /* Set NMI. */
235         } else {
236                 printk(BIOS_INFO, "NMI sources disabled.\n");
237                 reg8 |= ( 1 << 7);      /* Can't mask NMI from PCI-E and NMI_NOW */
238         }
239         outb(reg8, 0x70);
240
241         /* Enable CPU_SLP# and Intel Speedstep, set SMI# rate down */
242         reg16 = pci_read_config16(dev, GEN_PMCON_1);
243         reg16 &= ~(3 << 0);     // SMI# rate 1 minute
244         reg16 |= (1 << 2);      // CLKRUN_EN - Mobile/Ultra only
245         reg16 |= (1 << 3);      // Speedstep Enable - Mobile/Ultra only
246         reg16 |= (1 << 5);      // CPUSLP_EN Desktop only
247         // another laptop wants this?
248         // reg16 &= ~(1 << 10); // BIOS_PCI_EXP_EN - Desktop/Mobile only
249         reg16 |= (1 << 10);     // BIOS_PCI_EXP_EN - Desktop/Mobile only
250 #if DEBUG_PERIODIC_SMIS
251         /* Set DEBUG_PERIODIC_SMIS in i82801gx.h to debug using
252          * periodic SMIs.
253          */
254         reg16 |= (3 << 0); // Periodic SMI every 8s
255 #endif
256         pci_write_config16(dev, GEN_PMCON_1, reg16);
257
258         // Set the board's GPI routing.
259         i82801gx_gpi_routing(dev);
260
261         pmbase = pci_read_config16(dev, 0x40) & 0xfffe;
262
263         outl(config->gpe0_en, pmbase + GPE0_EN);
264         outw(config->alt_gp_smi_en, pmbase + ALT_GP_SMI_EN);
265
266         /* Set up power management block and determine sleep mode */
267         reg32 = inl(pmbase + 0x04); // PM1_CNT
268
269         reg32 &= ~(7 << 10);    // SLP_TYP
270         reg32 |= (1 << 1);      // enable C3->C0 transition on bus master
271         reg32 |= (1 << 0);      // SCI_EN
272         outl(reg32, pmbase + 0x04);
273 }
274
275 static void i82801gx_configure_cstates(device_t dev)
276 {
277         u8 reg8;
278
279         reg8 = pci_read_config8(dev, 0xa9); // Cx state configuration
280         reg8 |= (1 << 4) | (1 << 3) | (1 << 2); // Enable Popup & Popdown
281         pci_write_config8(dev, 0xa9, reg8);
282
283         // Set Deeper Sleep configuration to recommended values
284         reg8 = pci_read_config8(dev, 0xaa);
285         reg8 &= 0xf0;
286         reg8 |= (2 << 2);       // Deeper Sleep to Stop CPU: 34-40us
287         reg8 |= (2 << 0);       // Deeper Sleep to Sleep: 15us
288         pci_write_config8(dev, 0xaa, reg8);
289 }
290
291 static void i82801gx_rtc_init(struct device *dev)
292 {
293         u8 reg8;
294         int rtc_failed;
295
296         reg8 = pci_read_config8(dev, GEN_PMCON_3);
297         rtc_failed = reg8 & RTC_BATTERY_DEAD;
298         if (rtc_failed) {
299                 reg8 &= ~RTC_BATTERY_DEAD;
300                 pci_write_config8(dev, GEN_PMCON_3, reg8);
301         }
302         printk(BIOS_DEBUG, "rtc_failed = 0x%x\n", rtc_failed);
303
304         rtc_init(rtc_failed);
305 }
306
307 static void enable_hpet(void)
308 {
309         u32 reg32;
310
311         /* Move HPET to default address 0xfed00000 and enable it */
312         reg32 = RCBA32(HPTC);
313         reg32 |= (1 << 7); // HPET Address Enable
314         reg32 &= ~(3 << 0);
315         RCBA32(HPTC) = reg32;
316 }
317
318 static void enable_clock_gating(void)
319 {
320         u32 reg32;
321
322         /* Enable Clock Gating for most devices */
323         reg32 = RCBA32(CG);
324         reg32 |= (1 << 31);     // LPC clock gating
325         reg32 |= (1 << 30);     // PATA clock gating
326         // SATA clock gating
327         reg32 |= (1 << 27) | (1 << 26) | (1 << 25) | (1 << 24);
328         reg32 |= (1 << 23);     // AC97 clock gating
329         reg32 |= (1 << 19);     // USB EHCI clock gating
330         reg32 |= (1 << 3) | (1 << 1);   // DMI clock gating
331         reg32 |= (1 << 2);      // PCIe clock gating;
332         reg32 &= ~(1 << 20); // No static clock gating for USB
333         reg32 &= ~( (1 << 29) | (1 << 28) ); // Disable UHCI clock gating
334         RCBA32(CG) = reg32;
335 }
336
337 #if CONFIG_HAVE_SMI_HANDLER
338 static void i82801gx_lock_smm(struct device *dev)
339 {
340 #if TEST_SMM_FLASH_LOCKDOWN
341         u8 reg8;
342 #endif
343
344         if (acpi_slp_type != 3) {
345 #if ENABLE_ACPI_MODE_IN_COREBOOT
346                 printk(BIOS_DEBUG, "Enabling ACPI via APMC:\n");
347                 outb(APM_CNT_ACPI_ENABLE, APM_CNT); // Enable ACPI mode
348                 printk(BIOS_DEBUG, "done.\n");
349 #else
350                 printk(BIOS_DEBUG, "Disabling ACPI via APMC:\n");
351                 outb(APM_CNT_ACPI_DISABLE, APM_CNT); // Disable ACPI mode
352                 printk(BIOS_DEBUG, "done.\n");
353 #endif
354         } else {
355                 printk(BIOS_DEBUG, "S3 wakeup, enabling ACPI via APMC\n");
356                 outb(APM_CNT_ACPI_ENABLE, APM_CNT);
357         }
358         /* Don't allow evil boot loaders, kernels, or
359          * userspace applications to deceive us:
360          */
361         smm_lock();
362
363 #if TEST_SMM_FLASH_LOCKDOWN
364         /* Now try this: */
365         printk(BIOS_DEBUG, "Locking BIOS to RO... ");
366         reg8 = pci_read_config8(dev, 0xdc);     /* BIOS_CNTL */
367         printk(BIOS_DEBUG, " BLE: %s; BWE: %s\n", (reg8&2)?"on":"off",
368                         (reg8&1)?"rw":"ro");
369         reg8 &= ~(1 << 0);                      /* clear BIOSWE */
370         pci_write_config8(dev, 0xdc, reg8);
371         reg8 |= (1 << 1);                       /* set BLE */
372         pci_write_config8(dev, 0xdc, reg8);
373         printk(BIOS_DEBUG, "ok.\n");
374         reg8 = pci_read_config8(dev, 0xdc);     /* BIOS_CNTL */
375         printk(BIOS_DEBUG, " BLE: %s; BWE: %s\n", (reg8&2)?"on":"off",
376                         (reg8&1)?"rw":"ro");
377
378         printk(BIOS_DEBUG, "Writing:\n");
379         *(volatile u8 *)0xfff00000 = 0x00;
380         printk(BIOS_DEBUG, "Testing:\n");
381         reg8 |= (1 << 0);                       /* set BIOSWE */
382         pci_write_config8(dev, 0xdc, reg8);
383
384         reg8 = pci_read_config8(dev, 0xdc);     /* BIOS_CNTL */
385         printk(BIOS_DEBUG, " BLE: %s; BWE: %s\n", (reg8&2)?"on":"off",
386                         (reg8&1)?"rw":"ro");
387         printk(BIOS_DEBUG, "Done.\n");
388 #endif
389 }
390 #endif
391
392 #define SPIBASE 0x3020
393 static void i82801gx_spi_init(void)
394 {
395         u16 spicontrol;
396
397         spicontrol = RCBA16(SPIBASE + 2);
398         spicontrol &= ~(1 << 0); // SPI Access Request
399         RCBA16(SPIBASE + 2) = spicontrol;
400 }
401
402 static void i82801gx_fixups(struct device *dev)
403 {
404         /* This needs to happen after PCI enumeration */
405         RCBA32(0x1d40) |= 1;
406
407         /* USB Transient Disconnect Detect:
408          * Prevent a SE0 condition on the USB ports from being
409          * interpreted by the UHCI controller as a disconnect
410          */
411         pci_write_config8(dev, 0xad, 0x3);
412 }
413
414 static void lpc_init(struct device *dev)
415 {
416         printk(BIOS_DEBUG, "i82801gx: lpc_init\n");
417
418         /* Set the value for PCI command register. */
419         pci_write_config16(dev, PCI_COMMAND, 0x000f);
420
421         /* IO APIC initialization. */
422         i82801gx_enable_apic(dev);
423
424         i82801gx_enable_serial_irqs(dev);
425
426         /* Setup the PIRQ. */
427         i82801gx_pirq_init(dev);
428
429         /* Setup power options. */
430         i82801gx_power_options(dev);
431
432         /* Configure Cx state registers */
433         i82801gx_configure_cstates(dev);
434
435         /* Set the state of the GPIO lines. */
436         //gpio_init(dev);
437
438         /* Initialize the real time clock. */
439         i82801gx_rtc_init(dev);
440
441         /* Initialize ISA DMA. */
442         isa_dma_init();
443
444         /* Initialize the High Precision Event Timers, if present. */
445         enable_hpet();
446
447         /* Initialize Clock Gating */
448         enable_clock_gating();
449
450         setup_i8259();
451
452         /* The OS should do this? */
453         /* Interrupt 9 should be level triggered (SCI) */
454         i8259_configure_irq_trigger(9, 1);
455
456 #if CONFIG_HAVE_SMI_HANDLER
457         i82801gx_lock_smm(dev);
458 #endif
459
460         i82801gx_spi_init();
461
462         i82801gx_fixups(dev);
463 }
464
465 static void i82801gx_lpc_read_resources(device_t dev)
466 {
467         struct resource *res;
468
469         /* Get the normal PCI resources of this device. */
470         pci_dev_read_resources(dev);
471
472         /* Add an extra subtractive resource for both memory and I/O. */
473         res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
474         res->base = 0;
475         res->size = 0x1000;
476         res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
477                      IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
478
479         res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
480         res->base = 0xff800000;
481         res->size = 0x00800000; /* 8 MB for flash */
482         res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
483                      IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
484
485         res = new_resource(dev, 3); /* IOAPIC */
486         res->base = IO_APIC_ADDR;
487         res->size = 0x00001000;
488         res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
489 }
490
491 static void set_subsystem(device_t dev, unsigned vendor, unsigned device)
492 {
493         if (!vendor || !device) {
494                 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
495                                 pci_read_config32(dev, PCI_VENDOR_ID));
496         } else {
497                 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
498                                 ((device & 0xffff) << 16) | (vendor & 0xffff));
499         }
500 }
501
502 static struct pci_operations pci_ops = {
503         .set_subsystem = set_subsystem,
504 };
505
506 static struct device_operations device_ops = {
507         .read_resources         = i82801gx_lpc_read_resources,
508         .set_resources          = pci_dev_set_resources,
509         .enable_resources       = pci_dev_enable_resources,
510         .init                   = lpc_init,
511         .scan_bus               = scan_static_bus,
512         .enable                 = i82801gx_enable,
513         .ops_pci                = &pci_ops,
514 };
515
516 /* 82801GH (ICH7 DH) */
517 static const struct pci_driver ich7_dh_lpc __pci_driver = {
518         .ops    = &device_ops,
519         .vendor = PCI_VENDOR_ID_INTEL,
520         .device = 0x27b0,
521 };
522
523 /* 82801GB/GR (ICH7/ICH7R) */
524 static const struct pci_driver ich7_ich7r_lpc __pci_driver = {
525         .ops    = &device_ops,
526         .vendor = PCI_VENDOR_ID_INTEL,
527         .device = 0x27b8,
528 };
529
530 /* 82801GBM/GU (ICH7-M/ICH7-U) */
531 static const struct pci_driver ich7m_ich7u_lpc __pci_driver = {
532         .ops    = &device_ops,
533         .vendor = PCI_VENDOR_ID_INTEL,
534         .device = 0x27b9,
535 };
536
537 /* 82801GHM (ICH7-M DH) */
538 static const struct pci_driver ich7m_dh_lpc __pci_driver = {
539         .ops    = &device_ops,
540         .vendor = PCI_VENDOR_ID_INTEL,
541         .device = 0x27bd,
542 };