This patch unifies the use of config options in v2 to all start with CONFIG_
[coreboot.git] / src / mainboard / kontron / 986lcd-m / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  * 
4  * Copyright (C) 2007-2008 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 <string.h>
23 #include <console/console.h>
24 #include <arch/acpi.h>
25 #include <device/device.h>
26 #include <device/pci.h>
27 #include <device/pci_ids.h>
28 #include "dmi.h"
29
30 extern unsigned char AmlCode[];
31
32 typedef struct acpi_oemb {
33         acpi_header_t header;
34         u8  ss;
35         u16 iost;
36         u32 topm;
37         u32 roms;
38         u32 mg1b;
39         u32 mg1l;
40         u32 mg2b;
41         u32 mg2l;
42         u8  rsvd;
43         u8  dmax;
44         u32 hpta;
45         u32 cpb0;
46         u32 cpb1;
47         u32 cpb2;
48         u32 cpb3;
49         u8  assb;
50         u8  aotb;
51         u32 aaxb;
52         u8  smif;
53         u8  dtse;
54         u8  dts1;
55         u8  dts2;
56         u8  mpen;
57 } __attribute__((packed)) acpi_oemb_t;
58
59 void acpi_create_oemb(acpi_oemb_t *oemb)
60 {
61         acpi_header_t *header = &(oemb->header);
62         unsigned long tolud;
63
64         memset (oemb, 0, sizeof(oemb));
65
66         /* fill out header fields */
67         memcpy(header->signature, "OEMB", 4);
68         memcpy(header->oem_id, OEM_ID, 6);
69         memcpy(header->oem_table_id, "COREBOOT", 8);
70         memcpy(header->asl_compiler_id, ASLC, 4);
71
72         header->length = sizeof(acpi_oemb_t);
73         header->revision = 1;
74
75         oemb->ss   =   0x09; // ss1 + ss 4
76         oemb->iost = 0x0403; // ??
77
78         tolud = pci_read_config32(dev_find_slot(0, PCI_DEVFN(2, 0)), 0x5c);
79         oemb->topm = tolud;
80
81         oemb->roms = 0xfff00000; // 1M hardcoded
82
83         oemb->mg1b = 0x000d0000;
84         oemb->mg1l = 0x00010000;
85
86         oemb->mg2b = tolud;
87         oemb->mg2l = 0-tolud;
88
89         oemb->dmax = 0x87;
90         oemb->hpta = 0x000e36c0;
91
92         header->checksum =
93             acpi_checksum((void *) oemb, sizeof(acpi_oemb_t));
94
95 };
96
97 unsigned long acpi_fill_mcfg(unsigned long current)
98 {
99         device_t dev;
100         u32 pciexbar = 0;
101         u32 pciexbar_reg;
102         int max_buses;
103
104         dev = dev_find_device(0x8086, 0x27a0, 0);
105         if (!dev)
106                 return current;
107
108         // MMCFG not supported or not enabled.
109         pciexbar_reg=pci_read_config32(dev, 0x48);
110
111         if (!(pciexbar_reg & (1 << 0)))
112                 return current;
113
114         switch ((pciexbar_reg >> 1) & 3) {
115         case 0: // 256MB
116                 pciexbar = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28));
117                 max_buses = 256;
118                 break;
119         case 1: // 128M
120                 pciexbar = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
121                 max_buses = 128;
122                 break;
123         case 2: // 64M
124                 pciexbar = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28)|(1 << 27)|(1 << 26));
125                 max_buses = 64;
126                 break;
127         default: // RSVD
128                 return current;
129         }
130
131         if (!pciexbar)
132                 return current;
133
134         current += acpi_create_mcfg_mmconfig((acpi_mcfg_mmconfig_t *) current,
135                         pciexbar, 0x0, 0x0, max_buses - 1);
136
137         return current;
138 }
139
140 void acpi_create_intel_hpet(acpi_hpet_t * hpet)
141 {
142 #define HPET_ADDR  0xfed00000ULL
143         acpi_header_t *header = &(hpet->header);
144         acpi_addr_t *addr = &(hpet->addr);
145
146         memset((void *) hpet, 0, sizeof(acpi_hpet_t));
147
148         /* fill out header fields */
149         memcpy(header->signature, HPET_NAME, 4);
150         memcpy(header->oem_id, OEM_ID, 6);
151         memcpy(header->oem_table_id, "COREBOOT", 8);
152         memcpy(header->asl_compiler_id, ASLC, 4);
153
154         header->length = sizeof(acpi_hpet_t);
155         header->revision = 1;
156
157         /* fill out HPET address */
158         addr->space_id = 0;     /* Memory */
159         addr->bit_width = 64;
160         addr->bit_offset = 0;
161         addr->addrl = HPET_ADDR & 0xffffffff;
162         addr->addrh = HPET_ADDR >> 32;
163
164         hpet->id = 0x8086a201;  /* Intel */
165         hpet->number = 0x00;
166         hpet->min_tick = 0x0080;
167
168         header->checksum =
169             acpi_checksum((void *) hpet, sizeof(acpi_hpet_t));
170 }
171
172
173
174 #define IO_APIC_ADDR    0xfec00000UL
175
176 unsigned long acpi_fill_madt(unsigned long current)
177 {
178         /* Local Apic */
179         current += acpi_create_madt_lapic((acpi_madt_lapic_t *) current, 1, 0);
180         // This one is for the second core... Will it hurt?
181         current += acpi_create_madt_lapic((acpi_madt_lapic_t *) current, 2, 1);
182
183         /* IOAPIC */
184         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current, 2, IO_APIC_ADDR, 0);
185
186         /* INT_SRC_OVR */
187         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *) current, 0, 0, 2, 0);
188         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *) current, 0, 9, 9, 0x000d);  // high/level
189
190         return current;
191 }
192
193 unsigned long acpi_fill_slit(unsigned long current)
194 {
195         // Not implemented
196         return current;
197 }
198
199 unsigned long acpi_fill_srat(unsigned long current)
200 {
201         /* No NUMA, no SRAT */
202         return current;
203 }
204
205
206 #define ALIGN_CURRENT current = ((current + 0x0f) & -0x10)
207 unsigned long write_acpi_tables(unsigned long start)
208 {
209         unsigned long current;
210         int i;
211         acpi_rsdp_t *rsdp;
212         acpi_rsdt_t *rsdt;
213         acpi_hpet_t *hpet;
214         acpi_madt_t *madt;
215         acpi_mcfg_t *mcfg;
216         acpi_fadt_t *fadt;
217         acpi_facs_t *facs;
218         acpi_oemb_t *oemb;
219         acpi_header_t *dsdt;
220
221         current = start;
222
223         /* Align ACPI tables to 16byte */
224         ALIGN_CURRENT;
225
226         printk_info("ACPI: Writing ACPI tables at %lx.\n", start);
227
228         /* We need at least an RSDP and an RSDT Table */
229         rsdp = (acpi_rsdp_t *) current;
230         current += sizeof(acpi_rsdp_t);
231         ALIGN_CURRENT;
232         rsdt = (acpi_rsdt_t *) current;
233         current += sizeof(acpi_rsdt_t);
234         ALIGN_CURRENT;
235
236         /* clear all table memory */
237         memset((void *) start, 0, current - start);
238
239         acpi_write_rsdp(rsdp, rsdt);
240         acpi_write_rsdt(rsdt);
241
242         /*
243          * We explicitly add these tables later on:
244          */
245         printk_debug("ACPI:    * HPET\n");
246
247         hpet = (acpi_hpet_t *) current;
248         current += sizeof(acpi_hpet_t);
249         ALIGN_CURRENT;
250         acpi_create_intel_hpet(hpet);
251         acpi_add_table(rsdt, hpet);
252
253         /* If we want to use HPET Timers Linux wants an MADT */
254         printk_debug("ACPI:    * MADT\n");
255
256         madt = (acpi_madt_t *) current;
257         acpi_create_madt(madt);
258         current += madt->header.length;
259         ALIGN_CURRENT;
260         acpi_add_table(rsdt, madt);
261
262         printk_debug("ACPI:    * MCFG\n");
263         mcfg = (acpi_mcfg_t *) current;
264         acpi_create_mcfg(mcfg);
265         current += mcfg->header.length;
266         ALIGN_CURRENT;
267         acpi_add_table(rsdt, mcfg);
268
269         printk_debug("ACPI:    * OEMB\n");
270         oemb=(acpi_oemb_t *)current;
271         current += sizeof(acpi_oemb_t);
272         ALIGN_CURRENT;
273         acpi_create_oemb(oemb);
274         acpi_add_table(rsdt, oemb);
275
276         printk_debug("ACPI:     * FACS\n");
277         facs = (acpi_facs_t *) current;
278         current += sizeof(acpi_facs_t);
279         ALIGN_CURRENT;
280         acpi_create_facs(facs);
281
282         dsdt = (acpi_header_t *) current;
283         current += ((acpi_header_t *) AmlCode)->length;
284         ALIGN_CURRENT;
285         memcpy((void *) dsdt, (void *) AmlCode,
286                ((acpi_header_t *) AmlCode)->length);
287
288 #if 1
289         for (i=0; i < dsdt->length; i++) {
290                 if (*(u32*)(((u32)dsdt) + i) == 0xC0DEBABE) {
291                         printk_debug("ACPI: Patching up DSDT at offset 0x%04x -> 0x%08x\n", i, 0x24 + (u32)oemb);
292                         *(u32*)(((u32)dsdt) + i) = 0x24 + (u32)oemb;
293                         break;
294                 }
295         }
296
297         /* We patched up the DSDT, so we need to recalculate the checksum */
298         dsdt->checksum = 0;
299         dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
300 #endif
301
302         printk_debug("ACPI:     * DSDT @ %p Length %x\n", dsdt,
303                      dsdt->length);
304
305         printk_debug("ACPI:     * FADT\n");
306         fadt = (acpi_fadt_t *) current;
307         current += sizeof(acpi_fadt_t);
308         ALIGN_CURRENT;
309
310         acpi_create_fadt(fadt, facs, dsdt);
311         acpi_add_table(rsdt, fadt);
312         printk_debug("current = %lx\n", current);
313
314         printk_debug("ACPI:     * DMI (Linux workaround)\n");
315         memcpy((void *)0xfff80, dmi_table, DMI_TABLE_SIZE);
316 #if CONFIG_HAVE_HIGH_TABLES == 1
317         memcpy((void *)current, dmi_table, DMI_TABLE_SIZE);
318         current += DMI_TABLE_SIZE;
319         ALIGN_CURRENT;
320 #endif
321
322         printk_info("ACPI: done.\n");
323         return current;
324 }