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