* fix a minor power state issue in the ich7 smm handler
[coreboot.git] / src / southbridge / intel / i82801gx / i82801gx_smi.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
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <console/console.h>
26 #include <arch/io.h>
27 #include <cpu/x86/cache.h>
28 #include <cpu/x86/smm.h>
29 #include <string.h>
30 #include "chip.h"
31
32 // Future TODO: Move to i82801gx directory
33 #include "../../../northbridge/intel/i945/ich7.h"
34
35 extern unsigned char smm[];
36 extern unsigned int smm_len;
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_SMRAME      (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 /* While we read PMBASE dynamically in case it changed, let's
82  * initialize it with a sane value
83  */
84 static u16 pmbase = DEFAULT_PMBASE;
85
86 /**
87  * @brief read and clear PM1_STS 
88  * @return PM1_STS register
89  */
90 static u16 reset_pm1_status(void)
91 {
92         u16 reg16;
93         
94         reg16 = inw(pmbase + PM1_STS);
95         /* set status bits are cleared by writing 1 to them */
96         outw(reg16, pmbase + PM1_STS);
97         
98         return reg16;
99 }
100
101 static void dump_pm1_status(u16 pm1_sts)
102 {
103         printk_debug("PM1_STS: ");
104         if (pm1_sts & (1 << 15)) printk_debug("WAK ");
105         if (pm1_sts & (1 << 14)) printk_debug("PCIEXPWAK ");
106         if (pm1_sts & (1 << 11)) printk_debug("PRBTNOR ");
107         if (pm1_sts & (1 << 10)) printk_debug("RTC ");
108         if (pm1_sts & (1 <<  8)) printk_debug("PWRBTN ");
109         if (pm1_sts & (1 <<  5)) printk_debug("GBL ");
110         if (pm1_sts & (1 <<  4)) printk_debug("BM ");
111         if (pm1_sts & (1 <<  0)) printk_debug("TMROF ");
112         printk_debug("\n");
113 }
114
115 /**
116  * @brief read and clear SMI_STS 
117  * @return SMI_STS register
118  */
119 static u32 reset_smi_status(void)
120 {
121         u32 reg32;
122         
123         reg32 = inl(pmbase + SMI_STS);
124         /* set status bits are cleared by writing 1 to them */
125         outl(reg32, pmbase + SMI_STS);
126         
127         return reg32;
128 }
129
130 static void dump_smi_status(u32 smi_sts)
131 {
132         printk_debug("SMI_STS: ");
133         if (smi_sts & (1 << 26)) printk_debug("SPI ");
134         if (smi_sts & (1 << 25)) printk_debug("EL_SMI ");
135         if (smi_sts & (1 << 21)) printk_debug("MONITOR ");
136         if (smi_sts & (1 << 20)) printk_debug("PCI_EXP_SMI ");
137         if (smi_sts & (1 << 18)) printk_debug("INTEL_USB2 ");
138         if (smi_sts & (1 << 17)) printk_debug("LEGACY_USB2 ");
139         if (smi_sts & (1 << 16)) printk_debug("SMBUS_SMI ");
140         if (smi_sts & (1 << 15)) printk_debug("SERIRQ_SMI ");
141         if (smi_sts & (1 << 14)) printk_debug("PERIODIC ");
142         if (smi_sts & (1 << 13)) printk_debug("TCO ");
143         if (smi_sts & (1 << 12)) printk_debug("DEVMON ");
144         if (smi_sts & (1 << 11)) printk_debug("MCSMI ");
145         if (smi_sts & (1 << 10)) printk_debug("GPI ");
146         if (smi_sts & (1 <<  9)) printk_debug("GPE0 ");
147         if (smi_sts & (1 <<  8)) printk_debug("PM1 ");
148         if (smi_sts & (1 <<  6)) printk_debug("SWSMI_TMR ");
149         if (smi_sts & (1 <<  5)) printk_debug("APM ");
150         if (smi_sts & (1 <<  4)) printk_debug("SLP_SMI ");
151         if (smi_sts & (1 <<  3)) printk_debug("LEGACY_USB ");
152         if (smi_sts & (1 <<  2)) printk_debug("BIOS ");
153         printk_debug("\n");
154 }
155
156
157 /**
158  * @brief read and clear GPE0_STS
159  * @return GPE0_STS register
160  */
161 static u32 reset_gpe0_status(void)
162 {
163         u32 reg32;
164         
165         reg32 = inl(pmbase + GPE0_STS);
166         /* set status bits are cleared by writing 1 to them */
167         outl(reg32, pmbase + GPE0_STS);
168         
169         return reg32;
170 }
171
172 static void dump_gpe0_status(u32 gpe0_sts)
173 {
174         int i;
175         printk_debug("GPE0_STS: ");
176         for (i=31; i<= 16; i--) {
177                 if (gpe0_sts & (1 << i)) printk_debug("GPIO%d ", (i-16));
178         }
179         if (gpe0_sts & (1 << 14)) printk_debug("USB4 ");
180         if (gpe0_sts & (1 << 13)) printk_debug("PME_B0 ");
181         if (gpe0_sts & (1 << 12)) printk_debug("USB3 ");
182         if (gpe0_sts & (1 << 11)) printk_debug("PME ");
183         if (gpe0_sts & (1 << 10)) printk_debug("EL_SCI/BATLOW ");
184         if (gpe0_sts & (1 <<  9)) printk_debug("PCI_EXP ");
185         if (gpe0_sts & (1 <<  8)) printk_debug("RI ");
186         if (gpe0_sts & (1 <<  7)) printk_debug("SMB_WAK ");
187         if (gpe0_sts & (1 <<  6)) printk_debug("TCO_SCI ");
188         if (gpe0_sts & (1 <<  5)) printk_debug("AC97 ");
189         if (gpe0_sts & (1 <<  4)) printk_debug("USB2 ");
190         if (gpe0_sts & (1 <<  3)) printk_debug("USB1 ");
191         if (gpe0_sts & (1 <<  2)) printk_debug("HOT_PLUG ");
192         if (gpe0_sts & (1 <<  0)) printk_debug("THRM ");
193         printk_debug("\n");
194 }
195
196 /**
197  * @brief read and clear TCOx_STS 
198  * @return TCOx_STS registers
199  */
200 static u32 reset_tco_status(void)
201 {
202         u32 tcobase = pmbase + 0x60;
203         u32 reg32;
204         
205         reg32 = inl(tcobase + 0x04);
206         /* set status bits are cleared by writing 1 to them */
207         outl(reg32 & ~(1<<18), tcobase + 0x04); //  Don't clear BOOT_STS before SECOND_TO_STS
208         if (reg32 & (1 << 18))
209                 outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS
210         
211         return reg32;
212 }
213
214
215 static void dump_tco_status(u32 tco_sts)
216 {
217         printk_debug("TCO_STS: ");
218         if (tco_sts & (1 << 20)) printk_debug("SMLINK_SLV ");
219         if (tco_sts & (1 << 18)) printk_debug("BOOT ");
220         if (tco_sts & (1 << 17)) printk_debug("SECOND_TO ");
221         if (tco_sts & (1 << 16)) printk_debug("INTRD_DET ");
222         if (tco_sts & (1 << 12)) printk_debug("DMISERR ");
223         if (tco_sts & (1 << 10)) printk_debug("DMISMI ");
224         if (tco_sts & (1 <<  9)) printk_debug("DMISCI ");
225         if (tco_sts & (1 <<  8)) printk_debug("BIOSWR ");
226         if (tco_sts & (1 <<  7)) printk_debug("NEWCENTURY ");
227         if (tco_sts & (1 <<  3)) printk_debug("TIMEOUT ");
228         if (tco_sts & (1 <<  2)) printk_debug("TCO_INT ");
229         if (tco_sts & (1 <<  1)) printk_debug("SW_TCO ");
230         if (tco_sts & (1 <<  0)) printk_debug("NMI2SMI ");
231         printk_debug("\n");
232 }
233
234
235
236 /**
237  * @brief Set the EOS bit
238  */
239 static void smi_set_eos(void)
240 {
241         u8 reg8;
242         
243         reg8 = inb(pmbase + SMI_EN);
244         reg8 |= EOS;
245         outb(reg8, pmbase + SMI_EN);
246 }
247
248 extern uint8_t smm_relocation_start, smm_relocation_end;
249
250 void smm_relocate(void)
251 {
252         u32 smi_en;
253
254         printk_debug("Initializing SMM handler...");
255
256         pmbase = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0x1f, 0)), 0x40) & 0xfffc;
257         printk_spew(" ... pmbase = 0x%04x\n", pmbase);
258
259         smi_en = inl(pmbase + SMI_EN);
260         if (smi_en & APMC_EN) {
261                 printk_info("SMI# handler already enabled?\n");
262                 return;
263         }
264
265         /* copy the SMM relocation code */
266         memcpy((void *)0x38000, &smm_relocation_start,
267                         &smm_relocation_end - &smm_relocation_start);
268
269         printk_debug("\n");
270         dump_smi_status(reset_smi_status());
271         dump_pm1_status(reset_pm1_status());
272         dump_gpe0_status(reset_gpe0_status());
273         dump_tco_status(reset_tco_status());
274
275         /* Enable SMI generation:
276          *  - on TCO events
277          *  - on APMC writes (io 0xb2)
278          *  - on writes to SLP_EN (sleep states)
279          *  - on writes to GBL_RLS (bios commands)
280          * No SMIs:
281          *  - on microcontroller writes (io 0x62/0x66)
282          */
283         outl(smi_en | (TCO_EN | APMC_EN | SLP_SMI_EN | BIOS_EN |
284                                 EOS | GBL_SMI_EN), pmbase + SMI_EN);
285
286         /**
287          * There are several methods of raising a controlled SMI# via
288          * software, among them:
289          *  - Writes to io 0xb2 (APMC)
290          *  - Writes to the Local Apic ICR with Delivery mode SMI.
291          *
292          * Using the local apic is a bit more tricky. According to 
293          * AMD Family 11 Processor BKDG no destination shorthand must be 
294          * used.
295          * The whole SMM initialization is quite a bit hardware specific, so
296          * I'm not too worried about the better of the methods at the moment
297          */
298
299         /* raise an SMI interrupt */
300         printk_spew("  ... raise SMI#\n");
301         outb(0x00, 0xb2);
302 }
303
304 void smm_install(void)
305 {
306         /* enable the SMM memory window */
307         pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM,
308                                 D_OPEN | G_SMRAME | C_BASE_SEG);
309
310         /* copy the real SMM handler */
311         memcpy((void *)0xa0000, smm, smm_len);
312         wbinvd();
313
314         /* close the SMM memory window and enable normal SMM */
315         pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM,
316                         G_SMRAME | C_BASE_SEG);
317 }
318
319 void smm_init(void)
320 {
321         smm_relocate();
322         smm_install();
323 }
324
325 void smm_lock(void)
326 {
327         /* LOCK the SMM memory window and enable normal SMM.
328          * After running this function, only a full reset can
329          * make the SMM registers writable again.
330          */
331         printk_debug("Locking SMM.\n");
332         pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM,
333                         D_LCK | G_SMRAME | C_BASE_SEG);
334 }
335