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,
24 #include <arch/romcc_io.h>
25 #include <console/console.h>
26 #include <cpu/x86/cache.h>
27 #include <cpu/x86/smm.h>
29 #include "i82801gx_power.h"
34 #define CST_CONTROL 0x85 // 0x85 crashes the box
35 #define PST_CONTROL 0x80 // 0x80 crashes the box
36 #define ACPI_DISABLE 0x1e
37 #define ACPI_ENABLE 0xe1
38 #define GNVS_UPDATE 0xea
43 #define D_OPEN (1 << 6)
44 #define D_CLS (1 << 5)
45 #define D_LCK (1 << 4)
46 #define G_SMRANE (1 << 3)
47 #define C_BASE_SEG ((0 << 2) | (1 << 1) | (0 << 0))
53 #define SLP_EN (1 << 13)
54 #define SLP_TYP (7 << 10)
55 #define GBL_RLS (1 << 2)
56 #define BM_RLD (1 << 1)
57 #define SCI_EN (1 << 0)
63 #define PM2_CNT 0x20 // mobile only
66 #define PME_B0_EN (1 << 13)
68 #define EL_SMI_EN (1 << 25) // Intel Quick Resume Technology
69 #define INTEL_USB2_EN (1 << 18) // Intel-Specific USB2 SMI logic
70 #define LEGACY_USB2_EN (1 << 17) // Legacy USB2 SMI logic
71 #define PERIODIC_EN (1 << 14) // SMI on PERIODIC_STS in SMI_STS
72 #define TCO_EN (1 << 13) // Enable TCO Logic (BIOSWE et al)
73 #define MCSMI_EN (1 << 11) // Trap microcontroller range access
74 #define BIOS_RLS (1 << 7) // asserts SCI on bit set
75 #define SWSMI_TMR_EN (1 << 6) // start software smi timer on bit set
76 #define APMC_EN (1 << 5) // Writes to APM_CNT cause SMI#
77 #define SLP_SMI_EN (1 << 4) // Write to SLP_EN in PM1_CNT asserts SMI#
78 #define LEGACY_USB_EN (1 << 3) // Legacy USB circuit SMI logic
79 #define BIOS_EN (1 << 2) // Assert SMI# on setting GBL_RLS bit
80 #define EOS (1 << 1) // End of SMI (deassert SMI#)
81 #define GBL_SMI_EN (1 << 0) // SMI# generation at all?
83 #define ALT_GP_SMI_EN 0x38
84 #define ALT_GP_SMI_STS 0x3a
86 #define DEVACT_STS 0x44
90 #include "i82801gx_nvs.h"
92 /* While we read PMBASE dynamically in case it changed, let's
93 * initialize it with a sane value
95 u16 pmbase = DEFAULT_PMBASE;
97 /* GNVS needs to be updated by an 0xEA PM Trap (B2) after it has been located
100 global_nvs_t *gnvs = (global_nvs_t *)0x0;
101 void *tcg = (void *)0x0;
102 void *smi1 = (void *)0x0;
105 * @brief read and clear PM1_STS
106 * @return PM1_STS register
108 static u16 reset_pm1_status(void)
112 reg16 = inw(pmbase + PM1_STS);
113 /* set status bits are cleared by writing 1 to them */
114 outw(reg16, pmbase + PM1_STS);
119 static void dump_pm1_status(u16 pm1_sts)
121 printk_debug("PM1_STS: ");
122 if (pm1_sts & (1 << 15)) printk_debug("WAK ");
123 if (pm1_sts & (1 << 14)) printk_debug("PCIEXPWAK ");
124 if (pm1_sts & (1 << 11)) printk_debug("PRBTNOR ");
125 if (pm1_sts & (1 << 10)) printk_debug("RTC ");
126 if (pm1_sts & (1 << 8)) printk_debug("PWRBTN ");
127 if (pm1_sts & (1 << 5)) printk_debug("GBL ");
128 if (pm1_sts & (1 << 4)) printk_debug("BM ");
129 if (pm1_sts & (1 << 0)) printk_debug("TMROF ");
134 * @brief read and clear SMI_STS
135 * @return SMI_STS register
137 static u32 reset_smi_status(void)
141 reg32 = inl(pmbase + SMI_STS);
142 /* set status bits are cleared by writing 1 to them */
143 outl(reg32, pmbase + SMI_STS);
148 static void dump_smi_status(u32 smi_sts)
150 printk_debug("SMI_STS: ");
151 if (smi_sts & (1 << 26)) printk_debug("SPI ");
152 if (smi_sts & (1 << 25)) printk_debug("EL_SMI ");
153 if (smi_sts & (1 << 21)) printk_debug("MONITOR ");
154 if (smi_sts & (1 << 20)) printk_debug("PCI_EXP_SMI ");
155 if (smi_sts & (1 << 18)) printk_debug("INTEL_USB2 ");
156 if (smi_sts & (1 << 17)) printk_debug("LEGACY_USB2 ");
157 if (smi_sts & (1 << 16)) printk_debug("SMBUS_SMI ");
158 if (smi_sts & (1 << 15)) printk_debug("SERIRQ_SMI ");
159 if (smi_sts & (1 << 14)) printk_debug("PERIODIC ");
160 if (smi_sts & (1 << 13)) printk_debug("TCO ");
161 if (smi_sts & (1 << 12)) printk_debug("DEVMON ");
162 if (smi_sts & (1 << 11)) printk_debug("MCSMI ");
163 if (smi_sts & (1 << 10)) printk_debug("GPI ");
164 if (smi_sts & (1 << 9)) printk_debug("GPE0 ");
165 if (smi_sts & (1 << 8)) printk_debug("PM1 ");
166 if (smi_sts & (1 << 6)) printk_debug("SWSMI_TMR ");
167 if (smi_sts & (1 << 5)) printk_debug("APM ");
168 if (smi_sts & (1 << 4)) printk_debug("SLP_SMI ");
169 if (smi_sts & (1 << 3)) printk_debug("LEGACY_USB ");
170 if (smi_sts & (1 << 2)) printk_debug("BIOS ");
176 * @brief read and clear GPE0_STS
177 * @return GPE0_STS register
179 static u32 reset_gpe0_status(void)
183 reg32 = inl(pmbase + GPE0_STS);
184 /* set status bits are cleared by writing 1 to them */
185 outl(reg32, pmbase + GPE0_STS);
190 static void dump_gpe0_status(u32 gpe0_sts)
193 printk_debug("GPE0_STS: ");
194 for (i=31; i<= 16; i--) {
195 if (gpe0_sts & (1 << i)) printk_debug("GPIO%d ", (i-16));
197 if (gpe0_sts & (1 << 14)) printk_debug("USB4 ");
198 if (gpe0_sts & (1 << 13)) printk_debug("PME_B0 ");
199 if (gpe0_sts & (1 << 12)) printk_debug("USB3 ");
200 if (gpe0_sts & (1 << 11)) printk_debug("PME ");
201 if (gpe0_sts & (1 << 10)) printk_debug("EL_SCI/BATLOW ");
202 if (gpe0_sts & (1 << 9)) printk_debug("PCI_EXP ");
203 if (gpe0_sts & (1 << 8)) printk_debug("RI ");
204 if (gpe0_sts & (1 << 7)) printk_debug("SMB_WAK ");
205 if (gpe0_sts & (1 << 6)) printk_debug("TCO_SCI ");
206 if (gpe0_sts & (1 << 5)) printk_debug("AC97 ");
207 if (gpe0_sts & (1 << 4)) printk_debug("USB2 ");
208 if (gpe0_sts & (1 << 3)) printk_debug("USB1 ");
209 if (gpe0_sts & (1 << 2)) printk_debug("HOT_PLUG ");
210 if (gpe0_sts & (1 << 0)) printk_debug("THRM ");
216 * @brief read and clear TCOx_STS
217 * @return TCOx_STS registers
219 static u32 reset_tco_status(void)
221 u32 tcobase = pmbase + 0x60;
224 reg32 = inl(tcobase + 0x04);
225 /* set status bits are cleared by writing 1 to them */
226 outl(reg32 & ~(1<<18), tcobase + 0x04); // Don't clear BOOT_STS before SECOND_TO_STS
227 if (reg32 & (1 << 18))
228 outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS
234 static void dump_tco_status(u32 tco_sts)
236 printk_debug("TCO_STS: ");
237 if (tco_sts & (1 << 20)) printk_debug("SMLINK_SLV ");
238 if (tco_sts & (1 << 18)) printk_debug("BOOT ");
239 if (tco_sts & (1 << 17)) printk_debug("SECOND_TO ");
240 if (tco_sts & (1 << 16)) printk_debug("INTRD_DET ");
241 if (tco_sts & (1 << 12)) printk_debug("DMISERR ");
242 if (tco_sts & (1 << 10)) printk_debug("DMISMI ");
243 if (tco_sts & (1 << 9)) printk_debug("DMISCI ");
244 if (tco_sts & (1 << 8)) printk_debug("BIOSWR ");
245 if (tco_sts & (1 << 7)) printk_debug("NEWCENTURY ");
246 if (tco_sts & (1 << 3)) printk_debug("TIMEOUT ");
247 if (tco_sts & (1 << 2)) printk_debug("TCO_INT ");
248 if (tco_sts & (1 << 1)) printk_debug("SW_TCO ");
249 if (tco_sts & (1 << 0)) printk_debug("NMI2SMI ");
253 /* We are using PCIe accesses for now
254 * 1. the chipset can do it
255 * 2. we don't need to worry about how we leave 0xcf8/0xcfc behind
257 #include "../../../northbridge/intel/i945/pcie_config.c"
259 int southbridge_io_trap_handler(int smif)
263 printk_debug("OS Init\n");
271 /* On success, the IO Trap Handler returns 0
272 * On failure, the IO Trap Handler returns a value != 0
274 * For now, we force the return value to 0 and log all traps to
275 * see what's going on.
278 return 1; /* IO trap handled */
282 * @brief Set the EOS bit
284 void southbridge_smi_set_eos(void)
288 reg8 = inb(pmbase + SMI_EN);
290 outb(reg8, pmbase + SMI_EN);
294 static void southbridge_smi_sleep(unsigned int node, smm_state_save_area_t *state_save)
299 /* FIXME: the power state on boot should be read from
300 * CMOS or even better from GNVS. Right now it's hard
301 * coded at compile time.
303 u8 s5pwr = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
305 /* First, disable further SMIs */
306 reg8 = inb(pmbase + SMI_EN);
308 outb(reg8, pmbase + SMI_EN);
310 /* Figure out SLP_TYP */
311 reg32 = inl(pmbase + PM1_CNT);
312 printk_spew("SMI#: SLP = 0x%08x\n", reg32);
313 slp_typ = (reg32 >> 10) & 7;
315 /* Next, do the deed.
319 case 0: printk_debug("SMI#: Entering S0 (On)\n"); break;
320 case 1: printk_debug("SMI#: Entering S1 (Assert STPCLK#)\n"); break;
322 printk_debug("SMI#: Entering S3 (Suspend-To-RAM)\n");
323 /* Invalidate the cache before going to S3 */
326 case 6: printk_debug("SMI#: Entering S4 (Suspend-To-Disk)\n"); break;
328 printk_debug("SMI#: Entering S5 (Soft Power off)\n");
330 /* Set PME_B0_EN before going to S5 */
331 reg32 = inl(pmbase + GPE0_EN);
333 outl(reg32, pmbase + GPE0_EN);
335 /* Should we keep the power state after a power loss?
336 * In case the setting is "ON" or "OFF" we don't have
337 * to do anything. But if it's "KEEP" we have to switch
338 * to "OFF" before entering S5.
340 if (s5pwr == MAINBOARD_POWER_KEEP) {
341 reg8 = pcie_read_config8(PCI_DEV(0, 0x1f, 0), GEN_PMCON_3);
343 pcie_write_config8(PCI_DEV(0, 0x1f, 0), GEN_PMCON_3, reg8);
346 default: printk_debug("SMI#: ERROR: SLP_TYP reserved\n"); break;
349 /* Write back to the SLP register to cause the originally intended
350 * event again. We need to set BIT13 (SLP_EN) though to make the
353 outl(reg32 | SLP_EN, pmbase + PM1_CNT);
355 /* In most sleep states, the code flow of this function ends at
356 * the line above. However, if we entered sleep state S1 and wake
357 * up again, we will continue to execute code in this function.
359 reg32 = inl(pmbase + PM1_CNT);
360 if (reg32 & SCI_EN) {
361 /* The OS is not an ACPI OS, so we set the state to S0 */
362 reg32 &= ~(SLP_EN | SLP_TYP);
363 outl(reg32, pmbase + PM1_CNT);
367 static void southbridge_smi_apmc(unsigned int node, smm_state_save_area_t *state_save)
372 /* Emulate B2 register as the FADT / Linux expects it */
377 /* Calling this function seems to cause
378 * some kind of race condition in Linux
379 * and causes a kernel oops
381 printk_debug("C-state control\n");
384 /* Calling this function seems to cause
385 * some kind of race condition in Linux
386 * and causes a kernel oops
388 printk_debug("P-state control\n");
391 pmctrl = inl(pmbase + PM1_CNT);
393 outl(pmctrl, pmbase + PM1_CNT);
394 printk_debug("SMI#: ACPI disabled.\n");
397 pmctrl = inl(pmbase + PM1_CNT);
399 outl(pmctrl, pmbase + PM1_CNT);
400 printk_debug("SMI#: ACPI enabled.\n");
403 gnvs = *(global_nvs_t **)0x500;
404 tcg = *(void **)0x504;
405 smi1 = *(void **)0x508;
406 printk_debug("SMI#: Setting up structures to %p, %p, %p\n", gnvs, tcg, smi1);
409 printk_debug("SMI#: Unknown function APM_CNT=%02x\n", reg8);
413 static void southbridge_smi_pm1(unsigned int node, smm_state_save_area_t *state_save)
417 pm1_sts = reset_pm1_status();
418 dump_pm1_status(pm1_sts);
421 static void southbridge_smi_gpe0(unsigned int node, smm_state_save_area_t *state_save)
425 gpe0_sts = reset_gpe0_status();
426 dump_gpe0_status(gpe0_sts);
429 static void southbridge_smi_mc(unsigned int node, smm_state_save_area_t *state_save)
433 reg32 = inl(pmbase + SMI_EN);
435 /* Are periodic SMIs enabled? */
436 if ((reg32 & MCSMI_EN) == 0)
439 printk_debug("Microcontroller SMI.\n");
444 static void southbridge_smi_tco(unsigned int node, smm_state_save_area_t *state_save)
448 tco_sts = reset_tco_status();
454 if (tco_sts & (1 << 8)) { // BIOSWR
457 bios_cntl = pcie_read_config16(PCI_DEV(0, 0x1f, 0), 0xdc);
460 /* BWE is RW, so the SMI was caused by a
461 * write to BWE, not by a write to the BIOS
464 /* This is the place where we notice someone
465 * is trying to tinker with the BIOS. We are
466 * trying to be nice and just ignore it. A more
467 * resolute answer would be to power down the
470 printk_debug("Switching back to RO\n");
471 pcie_write_config32(PCI_DEV(0, 0x1f, 0), 0xdc, (bios_cntl & ~1));
472 } /* No else for now? */
473 } else if (tco_sts & (1 << 3)) { /* TIMEOUT */
474 /* Handle TCO timeout */
475 printk_debug("TCO Timeout.\n");
476 } else if (!tco_sts) {
477 dump_tco_status(tco_sts);
481 static void southbridge_smi_periodic(unsigned int node, smm_state_save_area_t *state_save)
485 reg32 = inl(pmbase + SMI_EN);
487 /* Are periodic SMIs enabled? */
488 if ((reg32 & PERIODIC_EN) == 0)
491 printk_debug("Periodic SMI.\n");
494 static void southbridge_smi_monitor(unsigned int node, smm_state_save_area_t *state_save)
496 #define IOTRAP(x) (trap_sts & (1 << x))
497 u32 trap_sts, trap_cycle;
501 trap_sts = RCBA32(0x1e00); // TRSR - Trap Status Register
502 RCBA32(0x1e00) = trap_sts; // Clear trap(s) in TRSR
504 trap_cycle = RCBA32(0x1e10);
505 for (i=16; i<20; i++) {
506 if (trap_cycle & (1 << i))
507 mask |= (0xff << ((i - 16) << 2));
511 /* IOTRAP(3) SMI function call */
513 if (gnvs && gnvs->smif)
514 io_trap_handler(gnvs->smif); // call function smif
518 /* IOTRAP(2) currently unused
519 * IOTRAP(1) currently unused */
523 if (!(trap_cycle & (1 << 24))) { // It's a write
524 printk_debug("SMI1 command\n");
525 data = RCBA32(0x1e18);
528 // southbridge_smi_command(data);
531 // Fall through to debug
534 printk_debug(" trapped io address = 0x%x\n", trap_cycle & 0xfffc);
535 for (i=0; i < 4; i++) if(IOTRAP(i)) printk_debug(" TRAPĀ = %d\n", i);
536 printk_debug(" AHBE = %x\n", (trap_cycle >> 16) & 0xf);
537 printk_debug(" MASK = 0x%08x\n", mask);
538 printk_debug(" read/write: %s\n", (trap_cycle & (1 << 24)) ? "read" : "write");
540 if (!(trap_cycle & (1 << 24))) {
542 data = RCBA32(0x1e18);
543 printk_debug(" iotrap written data = 0x%08x\n", data);
548 typedef void (*smi_handler)(unsigned int node,
549 smm_state_save_area_t *state_save);
551 smi_handler southbridge_smi[32] = {
552 NULL, // [0] reserved
553 NULL, // [1] reserved
554 NULL, // [2] BIOS_STS
555 NULL, // [3] LEGACY_USB_STS
556 southbridge_smi_sleep, // [4] SLP_SMI_STS
557 southbridge_smi_apmc, // [5] APM_STS
558 NULL, // [6] SWSMI_TMR_STS
559 NULL, // [7] reserved
560 southbridge_smi_pm1, // [8] PM1_STS
561 southbridge_smi_gpe0, // [9] GPE0_STS
562 NULL, // [10] GPI_STS
563 southbridge_smi_mc, // [11] MCSMI_STS
564 NULL, // [12] DEVMON_STS
565 southbridge_smi_tco, // [13] TCO_STS
566 southbridge_smi_periodic, // [14] PERIODIC_STS
567 NULL, // [15] SERIRQ_SMI_STS
568 NULL, // [16] SMBUS_SMI_STS
569 NULL, // [17] LEGACY_USB2_STS
570 NULL, // [18] INTEL_USB2_STS
571 NULL, // [19] reserved
572 NULL, // [20] PCI_EXP_SMI_STS
573 southbridge_smi_monitor, // [21] MONITOR_STS
574 NULL, // [22] reserved
575 NULL, // [23] reserved
576 NULL, // [24] reserved
577 NULL, // [25] EL_SMI_STS
578 NULL, // [26] SPI_STS
579 NULL, // [27] reserved
580 NULL, // [28] reserved
581 NULL, // [29] reserved
582 NULL, // [30] reserved
583 NULL // [31] reserved
587 * @brief Interrupt handler for SMI#
589 * @param smm_revision revision of the smm state save map
592 void southbridge_smi_handler(unsigned int node, smm_state_save_area_t *state_save)
597 /* Update global variable pmbase */
598 pmbase = pcie_read_config16(PCI_DEV(0, 0x1f, 0), 0x40) & 0xfffc;
600 /* We need to clear the SMI status registers, or we won't see what's
601 * happening in the following calls.
603 smi_sts = reset_smi_status();
605 /* Filter all non-enabled SMI events */
606 // FIXME Double check, this clears MONITOR
607 // smi_sts &= inl(pmbase + SMI_EN);
609 /* Call SMI sub handler for each of the status bits */
610 for (i = 0; i < 31; i++) {
611 if (smi_sts & (1 << i)) {
612 if (southbridge_smi[i])
613 southbridge_smi[i](node, state_save);
615 printk_debug("SMI_STS[%d] occured, but no "
616 "handler available.\n", i);
623 dump_smi_status(smi_sts);