Initial untested board code for the VIA EPIA-M700 Mini-ITX board.
[coreboot.git] / src / mainboard / via / epia-m700 / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * LinuxBIOS ACPI Table support
5  * written by Stefan Reinauer <stepan@openbios.org>
6  * ACPI FADT, FACS, and DSDT table support added by 
7  * Nick Barker <nick.barker9@btinternet.com>, and those portions
8  * (C) Copyright 2004 Nick Barker
9  * (C) Copyright 2005 Stefan Reinauer
10  * (C) Copyright 2009 One Laptop per Child, Association, Inc.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; version 2 of the License.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
24  */
25
26 /*
27  * most parts of this file copied from src\mainboard\asus\a8v-e_se\acpi_tables.c,
28  * acpi_is_wakeup() is from Rudolf's S3 patch and SSDT was added
29  */
30
31 #include <console/console.h>
32 #include <string.h>
33 #include <arch/acpi.h>
34 #include <device/device.h>
35 #include <device/pci_ids.h>
36 #include <../../../northbridge/via/vx800/vx800.h>
37
38 extern unsigned char AmlCode_dsdt[];
39 extern unsigned char AmlCode_ssdt[];
40
41 /*
42 These four macro copied from  #include <arch/smp/mpspec.h>, I have to do this since    "default HAVE_MP_TABLE = 0" in option.lb,
43 and also since mainboard/via/*.* have no Mptable.c(so that I can not set HAVE_MP_TABLE = 1) as many other mainboard.
44 So I have to copy these four to here. acpi_fill_madt() need this.
45 */
46 #define MP_IRQ_POLARITY_HIGH    0x1
47 #define MP_IRQ_POLARITY_LOW     0x3
48 #define MP_IRQ_TRIGGER_EDGE     0x4
49 #define MP_IRQ_TRIGGER_LEVEL    0xc
50
51
52 unsigned long acpi_fill_mcfg(unsigned long current)
53 {
54        /*       NO MCFG in VX855, no pci-e*/
55         return current;
56 }
57
58 unsigned long acpi_create_madt_lapics(unsigned long current)
59 {
60         device_t cpu;
61         int cpu_index = 0;
62
63         for(cpu = all_devices; cpu; cpu = cpu->next) {
64                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
65                    (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
66                         continue;
67                 }
68                 if (!cpu->enabled) {
69                         continue;
70                 }
71                 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current, cpu_index, cpu->path.apic.apic_id);
72                 cpu_index++;
73         }
74         return current;
75 }
76
77 unsigned long acpi_create_madt_lapic_nmis(unsigned long current, u16 flags, u8 lint)
78 {
79         device_t cpu;
80         int cpu_index = 0;
81
82         for(cpu = all_devices; cpu; cpu = cpu->next) {
83                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
84                    (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
85                         continue;
86                 }
87                 if (!cpu->enabled) {
88                         continue;
89                 }
90                 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current, cpu_index, flags, lint);
91                 cpu_index++;
92         }
93         return current;
94 }
95
96 unsigned long acpi_fill_madt(unsigned long current)
97 {
98         unsigned int gsi_base = 0x18;
99
100         /* Create all subtables for processors. */
101         current = acpi_create_madt_lapics(current);
102
103         /* Write SB IOAPIC. */
104         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
105                                 VX800SB_APIC_ID, VX800SB_APIC_BASE, 0);
106
107         /* IRQ0 -> APIC IRQ2. */
108         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
109                                                 current, 0, 0, 2, 0x0); 
110
111         /* IRQ9 ACPI active low. */
112         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
113                 current, 0, 9, 9, MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
114
115         /* Create all subtables for processors. */
116         current = acpi_create_madt_lapic_nmis(current,
117                         MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH, 1);
118
119         return current;
120 }
121
122 unsigned long acpi_fill_slit(unsigned long current) 
123
124         // Not implemented 
125         return current; 
126 }
127  
128 unsigned long acpi_fill_srat(unsigned long current)
129 {
130         /* No NUMA, no SRAT */
131 }
132
133 #define ALIGN(x,a)              __ALIGN_MASK(x,(typeof(x))(a)-1)
134 #define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))
135
136 unsigned long write_acpi_tables(unsigned long start)
137 {
138         unsigned long current;
139         acpi_rsdp_t *rsdp;
140         acpi_srat_t *srat;
141         acpi_rsdt_t *rsdt;
142         acpi_mcfg_t *mcfg;
143         acpi_hpet_t *hpet;
144         acpi_madt_t *madt;
145         acpi_fadt_t *fadt;
146         acpi_facs_t *facs;
147         acpi_header_t *dsdt;
148         acpi_header_t *ssdt;
149         
150         /* Align ACPI tables to 16byte */
151         start   = ( start + 0x0f ) & -0x10;
152         current = start;
153         
154         printk_info("ACPI: Writing ACPI tables at %lx...\n", start);
155
156         /* We need at least an RSDP and an RSDT Table */
157         rsdp = (acpi_rsdp_t *) current;
158         current += sizeof(acpi_rsdp_t);
159         rsdt = (acpi_rsdt_t *) current;
160         current += sizeof(acpi_rsdt_t);
161
162         /* clear all table memory */
163         memset((void *)start, 0, current - start);
164         
165         acpi_write_rsdp(rsdp, rsdt);
166         acpi_write_rsdt(rsdt);
167         
168         /*
169          * We explicitly add these tables later on:
170          */
171         printk_debug("ACPI:     * FACS\n");
172         current = ALIGN(current, 64);
173         facs = (acpi_facs_t *) current;
174         current += sizeof(acpi_facs_t);
175         acpi_create_facs(facs);
176
177         printk_debug("ACPI:     * DSDT\n");
178         dsdt = (acpi_header_t *) current;
179         current += ((acpi_header_t *)AmlCode_dsdt)->length;
180         memcpy((void *)dsdt,(void *)AmlCode_dsdt, ((acpi_header_t *)AmlCode_dsdt)->length);
181         dsdt->checksum = 0; /* don't trust intel iasl compiler to get this right. */
182         dsdt->checksum = acpi_checksum(dsdt, dsdt->length);
183         printk_debug("ACPI:     * DSDT @ %08x Length %x\n", dsdt, dsdt->length);
184         
185         printk_debug("ACPI:     * FADT\n");
186         fadt = (acpi_fadt_t *) current;
187         current += sizeof(acpi_fadt_t);
188
189         acpi_create_fadt(fadt,facs,dsdt);
190         acpi_add_table(rsdt,fadt);
191
192         /* If we want to use HPET timers Linux wants it in MADT. */
193         printk_debug("ACPI:    * MADT\n");
194         madt = (acpi_madt_t *) current;
195         acpi_create_madt(madt);
196         current += madt->header.length;
197         acpi_add_table(rsdt, madt);
198         
199        /*       NO MCFG in VX855, no pci-e*/
200
201         printk_debug("ACPI:    * HPET\n");
202         hpet = (acpi_mcfg_t *) current;
203         acpi_create_hpet(hpet);
204         current += hpet->header.length;
205         acpi_add_table(rsdt, hpet);
206 /*
207         printk_debug("ACPI:     * SSDT\n");
208         ssdt = (acpi_header_t *) current;
209         current += ((acpi_header_t *)AmlCode_ssdt)->length;
210         memcpy((void *)ssdt,(void *)AmlCode_ssdt, ((acpi_header_t *)AmlCode_ssdt)->length);
211         ssdt->checksum = 0; // don't trust intel iasl compiler to get this right
212         ssdt->checksum = acpi_checksum(ssdt, ssdt->length);
213         acpi_add_table(rsdt, ssdt);
214         printk_debug("ACPI:     * SSDT @ %08x Length %x\n", ssdt, ssdt->length);
215 */
216
217         printk_info("ACPI: done.\n");
218         return current;
219 }
220
221 extern u32 wake_vec;
222 extern u8 acpi_sleep_type;
223
224 int acpi_is_wakeup(void) {
225         return (acpi_sleep_type == 3);
226 }
227