11096fecd15f079023eda1939d87090355e9152f
[coreboot.git] / src / southbridge / intel / i82801gx / i82801gx_smihandler.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,
19  * MA 02110-1301 USA
20  */
21
22 #include <arch/io.h>
23 #include <arch/romcc_io.h>
24 #include <console/console.h>
25 #include <cpu/x86/cache.h>
26 #include <cpu/x86/smm.h>
27 //#include "chip.h"
28
29 // Future TODO: Move to i82801gx directory
30 #include "../../../northbridge/intel/i945/ich7.h"
31
32 #define DEBUG_SMI
33
34 #define ACPI_DISABLE    0x1e
35 #define ACPI_ENABLE     0xe1
36
37
38 /* I945 */
39 #define SMRAM           0x9d
40 #define   D_OPEN        (1 << 6)
41 #define   D_CLS         (1 << 5)
42 #define   D_LCK         (1 << 4)
43 #define   G_SMRANE      (1 << 3)
44 #define   C_BASE_SEG    ((0 << 2) | (1 << 1) | (0 << 0))
45
46 /* ICH7 */
47 #define PM1_STS         0x00
48 #define PM1_EN          0x02
49 #define PM1_CNT         0x04
50 #define PM1_TMR         0x08
51 #define PROC_CNT        0x10
52 #define LV2             0x14
53 #define LV3             0x15
54 #define LV4             0x16
55 #define PM2_CNT         0x20 // mobile only
56 #define GPE0_STS        0x28
57 #define GPE0_EN         0x2c
58 #define SMI_EN          0x30
59 #define   EL_SMI_EN      (1 << 25) // Intel Quick Resume Technology
60 #define   INTEL_USB2_EN  (1 << 18) // Intel-Specific USB2 SMI logic
61 #define   LEGACY_USB2_EN (1 << 17) // Legacy USB2 SMI logic
62 #define   PERIODIC_EN    (1 << 14) // SMI on PERIODIC_STS in SMI_STS
63 #define   TCO_EN         (1 << 13) // Enable TCO Logic (BIOSWE et al)
64 #define   MCSMI_EN       (1 << 11) // Trap microcontroller range access
65 #define   BIOS_RLS       (1 <<  7) // asserts SCI on bit set
66 #define   SWSMI_TMR_EN   (1 <<  6) // start software smi timer on bit set
67 #define   APMC_EN        (1 <<  5) // Writes to APM_CNT cause SMI#
68 #define   SLP_SMI_EN     (1 <<  4) // Write to SLP_EN in PM1_CNT asserts SMI#
69 #define   LEGACY_USB_EN  (1 <<  3) // Legacy USB circuit SMI logic
70 #define   BIOS_EN        (1 <<  2) // Assert SMI# on setting GBL_RLS bit
71 #define   EOS            (1 <<  1) // End of SMI (deassert SMI#)
72 #define   GBL_SMI_EN     (1 <<  0) // SMI# generation at all?
73 #define SMI_STS         0x34
74 #define ALT_GP_SMI_EN   0x38
75 #define ALT_GP_SMI_STS  0x3a
76 #define GPE_CNTL        0x42
77 #define DEVACT_STS      0x44
78 #define SS_CNT          0x50
79 #define C3_RES          0x54
80
81 #include "i82801gx_nvs.h"
82
83 /* While we read PMBASE dynamically in case it changed, let's
84  * initialize it with a sane value
85  */
86 static u16 pmbase = DEFAULT_PMBASE;
87
88 /**
89  * @brief read and clear PM1_STS 
90  * @return PM1_STS register
91  */
92 static u16 reset_pm1_status(void)
93 {
94         u16 reg16;
95         
96         reg16 = inw(pmbase + PM1_STS);
97         /* set status bits are cleared by writing 1 to them */
98         outw(reg16, pmbase + PM1_STS);
99         
100         return reg16;
101 }
102
103 static void dump_pm1_status(u16 pm1_sts)
104 {
105         printk_debug("PM1_STS: ");
106         if (pm1_sts & (1 << 15)) printk_debug("WAK ");
107         if (pm1_sts & (1 << 14)) printk_debug("PCIEXPWAK ");
108         if (pm1_sts & (1 << 11)) printk_debug("PRBTNOR ");
109         if (pm1_sts & (1 << 10)) printk_debug("RTC ");
110         if (pm1_sts & (1 <<  8)) printk_debug("PWRBTN ");
111         if (pm1_sts & (1 <<  5)) printk_debug("GBL ");
112         if (pm1_sts & (1 <<  4)) printk_debug("BM ");
113         if (pm1_sts & (1 <<  0)) printk_debug("TMROF ");
114         printk_debug("\n");
115 }
116
117 /**
118  * @brief read and clear SMI_STS 
119  * @return SMI_STS register
120  */
121 static u32 reset_smi_status(void)
122 {
123         u32 reg32;
124         
125         reg32 = inl(pmbase + SMI_STS);
126         /* set status bits are cleared by writing 1 to them */
127         outl(reg32, pmbase + SMI_STS);
128         
129         return reg32;
130 }
131
132 static void dump_smi_status(u32 smi_sts)
133 {
134         printk_debug("SMI_STS: ");
135         if (smi_sts & (1 << 26)) printk_debug("SPI ");
136         if (smi_sts & (1 << 25)) printk_debug("EL_SMI ");
137         if (smi_sts & (1 << 21)) printk_debug("MONITOR ");
138         if (smi_sts & (1 << 20)) printk_debug("PCI_EXP_SMI ");
139         if (smi_sts & (1 << 18)) printk_debug("INTEL_USB2 ");
140         if (smi_sts & (1 << 17)) printk_debug("LEGACY_USB2 ");
141         if (smi_sts & (1 << 16)) printk_debug("SMBUS_SMI ");
142         if (smi_sts & (1 << 15)) printk_debug("SERIRQ_SMI ");
143         if (smi_sts & (1 << 14)) printk_debug("PERIODIC ");
144         if (smi_sts & (1 << 13)) printk_debug("TCO ");
145         if (smi_sts & (1 << 12)) printk_debug("DEVMON ");
146         if (smi_sts & (1 << 11)) printk_debug("MCSMI ");
147         if (smi_sts & (1 << 10)) printk_debug("GPI ");
148         if (smi_sts & (1 <<  9)) printk_debug("GPE0 ");
149         if (smi_sts & (1 <<  8)) printk_debug("PM1 ");
150         if (smi_sts & (1 <<  6)) printk_debug("SWSMI_TMR ");
151         if (smi_sts & (1 <<  5)) printk_debug("APM ");
152         if (smi_sts & (1 <<  4)) printk_debug("SLP_SMI ");
153         if (smi_sts & (1 <<  3)) printk_debug("LEGACY_USB ");
154         if (smi_sts & (1 <<  2)) printk_debug("BIOS ");
155         printk_debug("\n");
156 }
157
158
159 /**
160  * @brief read and clear GPE0_STS
161  * @return GPE0_STS register
162  */
163 static u32 reset_gpe0_status(void)
164 {
165         u32 reg32;
166         
167         reg32 = inl(pmbase + GPE0_STS);
168         /* set status bits are cleared by writing 1 to them */
169         outl(reg32, pmbase + GPE0_STS);
170         
171         return reg32;
172 }
173
174 static void dump_gpe0_status(u32 gpe0_sts)
175 {
176         int i;
177         printk_debug("GPE0_STS: ");
178         for (i=31; i<= 16; i--) {
179                 if (gpe0_sts & (1 << i)) printk_debug("GPIO%d ", (i-16));
180         }
181         if (gpe0_sts & (1 << 14)) printk_debug("USB4 ");
182         if (gpe0_sts & (1 << 13)) printk_debug("PME_B0 ");
183         if (gpe0_sts & (1 << 12)) printk_debug("USB3 ");
184         if (gpe0_sts & (1 << 11)) printk_debug("PME ");
185         if (gpe0_sts & (1 << 10)) printk_debug("EL_SCI/BATLOW ");
186         if (gpe0_sts & (1 <<  9)) printk_debug("PCI_EXP ");
187         if (gpe0_sts & (1 <<  8)) printk_debug("RI ");
188         if (gpe0_sts & (1 <<  7)) printk_debug("SMB_WAK ");
189         if (gpe0_sts & (1 <<  6)) printk_debug("TCO_SCI ");
190         if (gpe0_sts & (1 <<  5)) printk_debug("AC97 ");
191         if (gpe0_sts & (1 <<  4)) printk_debug("USB2 ");
192         if (gpe0_sts & (1 <<  3)) printk_debug("USB1 ");
193         if (gpe0_sts & (1 <<  2)) printk_debug("HOT_PLUG ");
194         if (gpe0_sts & (1 <<  0)) printk_debug("THRM ");
195         printk_debug("\n");
196 }
197
198
199 /**
200  * @brief read and clear TCOx_STS 
201  * @return TCOx_STS registers
202  */
203 static u32 reset_tco_status(void)
204 {
205         u32 tcobase = pmbase + 0x60;
206         u32 reg32;
207         
208         reg32 = inl(tcobase + 0x04);
209         /* set status bits are cleared by writing 1 to them */
210         outl(reg32 & ~(1<<18), tcobase + 0x04); //  Don't clear BOOT_STS before SECOND_TO_STS
211         if (reg32 & (1 << 18))
212                 outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS
213         
214         return reg32;
215 }
216
217
218 static void dump_tco_status(u32 tco_sts)
219 {
220         printk_debug("TCO_STS: ");
221         if (tco_sts & (1 << 20)) printk_debug("SMLINK_SLV ");
222         if (tco_sts & (1 << 18)) printk_debug("BOOT ");
223         if (tco_sts & (1 << 17)) printk_debug("SECOND_TO ");
224         if (tco_sts & (1 << 16)) printk_debug("INTRD_DET ");
225         if (tco_sts & (1 << 12)) printk_debug("DMISERR ");
226         if (tco_sts & (1 << 10)) printk_debug("DMISMI ");
227         if (tco_sts & (1 <<  9)) printk_debug("DMISCI ");
228         if (tco_sts & (1 <<  8)) printk_debug("BIOSWR ");
229         if (tco_sts & (1 <<  7)) printk_debug("NEWCENTURY ");
230         if (tco_sts & (1 <<  3)) printk_debug("TIMEOUT ");
231         if (tco_sts & (1 <<  2)) printk_debug("TCO_INT ");
232         if (tco_sts & (1 <<  1)) printk_debug("SW_TCO ");
233         if (tco_sts & (1 <<  0)) printk_debug("NMI2SMI ");
234         printk_debug("\n");
235 }
236
237
238 /* We are using PCIe accesses for now
239  *  1. the chipset can do it
240  *  2. we don't need to worry about how we leave 0xcf8/0xcfc behind
241  */
242 #include "../../../northbridge/intel/i945/pcie_config.c"
243
244 void southbridge_io_trap_handler(int smif)
245 {
246         u8 reg8;
247         global_nvs_t *gnvs = (global_nvs_t *)0xc00;
248
249         printk_debug("SMI function trap 0x%x: ", smif);
250
251         switch (smif) {
252         case 0x32:
253                 printk_debug("OS Init\n");
254                 //gnvs->smif = 0;
255                 break;
256         case 0xd5:
257                 printk_debug("Set Brightness\n");
258                 reg8 = gnvs->brtl;
259                 printk_debug("brtl: %x\n", reg8);
260                 outb(0x17, 0x66);
261                 outb(reg8, 0x62);
262                 //gnvs->smif = 0;
263                 break;
264         case 0xd6:
265                 printk_debug("Get Brightness\n");
266                 outb(0x17, 0x66);
267                 reg8 = inb(0x62);
268                 printk_debug("brtl: %x\n", reg8);
269                 gnvs->brtl = reg8;
270                 //gnvs->smif = 0;
271                 break;
272         default:
273                 printk_debug("Unknown function\n");
274                 break;
275         }
276
277         /* On success, the IO Trap Handler returns 0
278          * On failure, the IO Trap Handler returns a value != 0
279          *
280          * For now, we force the return value to 0 and log all traps to
281          * see what's going on.
282          */
283         //gnvs->smif = 0;
284 }
285
286 /**
287  * @brief Set the EOS bit
288  */
289 void southbridge_smi_set_eos(void)
290 {
291         u8 reg8;
292
293         reg8 = inb(pmbase + SMI_EN);
294         reg8 |= EOS;
295         outb(reg8, pmbase + SMI_EN);
296 }
297
298 /**
299  * @brief Interrupt handler for SMI#
300  *
301  * @param smm_revision revision of the smm state save map
302  */
303
304 void southbridge_smi_handler(unsigned int node, smm_state_save_area_t *state_save)
305 {
306         u8 reg8;
307         u16 pmctrl;
308         u16 pm1_sts;
309         u32 smi_sts, gpe0_sts, tco_sts;
310
311         pmbase = pcie_read_config16(PCI_DEV(0, 0x1f, 0), 0x40) & 0xfffc;
312         printk_spew("SMI#: pmbase = 0x%04x\n", pmbase);
313
314         /* We need to clear the SMI status registers, or we won't see what's
315          * happening in the following calls.
316          */
317         smi_sts = reset_smi_status();
318         dump_smi_status(smi_sts);
319
320         if (smi_sts & (1 << 21)) { // MONITOR
321                 global_nvs_t *gnvs = (global_nvs_t *)0xc00;
322                 int i;
323                 u32 reg32;
324
325                 reg32 = RCBA32(0x1e00); // TRSR - Trap Status Register
326 #if 0
327                 /* Comment in for some useful debug */
328                 for (i=0; i<4; i++) {
329                         if (reg32 & (1 << i)) {
330                                 printk_debug("  io trap #%d\n", i);
331                         }
332                 }
333 #endif
334                 RCBA32(0x1e00) = reg32; // TRSR
335
336                 reg32 = RCBA32(0x1e10);
337
338                 if ((reg32 & 0xfffc) != 0x808) {
339                         printk_debug("  trapped io address = 0x%x\n", reg32 & 0xfffc);
340                         printk_debug("  AHBE = %x\n", (reg32 >> 16) & 0xf);
341                         printk_debug("  read/write: %s\n", (reg32 & (1 << 24)) ? "read" :
342                                 "write");
343                 }
344
345                 if (!(reg32 & (1 << 24))) {
346                         /* Write Cycle */
347                         reg32 = RCBA32(0x1e18);
348                         printk_debug("  iotrap written data = 0x%08x\n", reg32);
349
350                 }
351
352                 if (gnvs->smif)
353                         io_trap_handler(gnvs->smif); // call function smif
354         }
355
356         if (smi_sts & (1 << 13)) { // TCO
357                 tco_sts = reset_tco_status();
358                 dump_tco_status(tco_sts);
359
360                 if (tco_sts & (1 << 8)) { // BIOSWR
361                         u8 bios_cntl;
362
363                         bios_cntl = pcie_read_config16(PCI_DEV(0, 0x1f, 0), 0xdc);
364
365                         if (bios_cntl & 1) {
366                                 /* BWE is RW, so the SMI was caused by a
367                                  * write to BWE, not by a write to the BIOS
368                                  */
369
370                                 /* This is the place where we notice someone
371                                  * is trying to tinker with the BIOS. We are
372                                  * trying to be nice and just ignore it. A more
373                                  * resolute answer would be to power down the
374                                  * box.
375                                  */
376                                 printk_debug("Switching back to RO\n");
377                                 pcie_write_config32(PCI_DEV(0, 0x1f, 0), 0xdc, (bios_cntl & ~1));
378                         } /* No else for now? */
379                 }
380         }
381
382         if (smi_sts & (1 << 8)) { // PM1
383                 pm1_sts = reset_pm1_status();
384                 dump_pm1_status(pm1_sts);
385         }
386
387         if (smi_sts & (1 << 9)) { // GPE0
388                 gpe0_sts = reset_gpe0_status();
389                 dump_gpe0_status(gpe0_sts);
390         }
391
392         if (smi_sts & (1 << 5)) { // APM
393                 /* Emulate B2 register as the FADT / Linux expects it */
394
395                 reg8 = inb(0xb2);
396                 switch (reg8) {
397                 case ACPI_DISABLE:
398                         pmctrl = inw(pmbase + 0x04);
399                         pmctrl |= (1 << 0);
400                         outw(pmctrl, pmbase + 0x04);
401                         printk_debug("SMI#: ACPI disabled.\n");
402                         break;
403                 case ACPI_ENABLE:
404                         pmctrl = inw(pmbase + 0x04);
405                         pmctrl &= ~(1 << 0);
406                         outw(pmctrl, pmbase + 0x04);
407                         printk_debug("SMI#: ACPI enabled.\n");
408                         break;
409                 }
410         }
411
412         if (smi_sts & (1 << 4)) { // SLP_SMI
413                 u32 reg32;
414
415                 /* First, disable further SMIs */
416                 reg8 = inb(pmbase + SMI_EN);
417                 reg8 &= ~SLP_SMI_EN;
418                 outb(reg8, pmbase + SMI_EN);
419
420                 /* Next, do the deed, we should change
421                  * power on after power loss bits here
422                  * if we're going to S5
423                  */
424
425                 /* Write back to the SLP register to cause the
426                  * originally intended event again.
427                  */
428                 reg32 = inl(pmbase + 0x04);
429                 printk_debug("SMI#: SLP = 0x%08x\n");
430                 printk_debug("SMI#: Powering off.\n");
431                 outl(reg32, pmbase + 0x04);
432         }
433
434 }