3bd4868932ebce9189a4f142e9cf1864e1e60837
[coreboot.git] / src / mainboard / roda / rk886ex / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007-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 <string.h>
23 #include <console/console.h>
24 #include <arch/io.h>
25 #include <arch/acpi.h>
26 #include <arch/acpigen.h>
27 #include <arch/smp/mpspec.h>
28 #include <device/device.h>
29 #include <device/pci.h>
30 #include <device/pci_ids.h>
31 #include "dmi.h"
32
33 extern const unsigned char AmlCode[];
34 #if CONFIG_HAVE_ACPI_SLIC
35 unsigned long acpi_create_slic(unsigned long current);
36 #endif
37
38 #define OLD_ACPI 0
39 #if OLD_ACPI
40 static void acpi_create_gnvs(global_nvs_t *gnvs)
41 {
42         memset (gnvs, 0, sizeof(global_nvs_t));
43
44         gnvs->LIDS = 1;
45         gnvs->PWRS = 1;
46
47         gnvs->ACTT = 0x37;
48         gnvs->PSVT = 0x4f;
49
50         gnvs->TC1V = 0x00;
51         gnvs->TC2V = 0x0a;
52         gnvs->TSPV = 0x02;
53
54         gnvs->CRTT = 0x77;
55
56         gnvs->B0SC = 0x54;
57         gnvs->APIC = 0x01;
58         gnvs->MPEN = 0x01;
59
60         gnvs->PPCM = 0x02;
61         gnvs->PCP0 = 0xbf;
62         gnvs->PCP1 = 0xbf;
63
64         gnvs->CMAP = 0x01;
65         gnvs->CMBP = 0x01;
66         gnvs->LT0  = 0x01;
67         gnvs->FDCP = 0x01;
68         gnvs->CMCP = 0x01;
69         gnvs->CMDP = 0x01;
70         gnvs->P2M  = 0x02;
71
72         gnvs->IGDS = 0x01;
73
74         gnvs->CADL = 0x09;
75         gnvs->PADL = 0x09;
76
77         gnvs->NDID = 3;
78         gnvs->DID1 = 0x80000100;
79         gnvs->DID2 = 0x80000240;
80         gnvs->DID3 = 0x80000410;
81         gnvs->DID4 = 0x80000410;
82         gnvs->DID5 = 0x00000005;
83
84         gnvs->ALAF = 0x64;
85         gnvs->LLOW = 0x2c;
86         gnvs->LHIH = 0x01;
87
88         // tolud = pci_read_config32(dev_find_slot(0, PCI_DEVFN(2, 0)), 0x5c);
89         // oemb->topm = tolud;
90 }
91 #endif
92
93 #include "southbridge/intel/i82801gx/i82801gx_nvs.h"
94 static void acpi_create_gnvs(global_nvs_t *gnvs)
95 {
96         memset((void *)gnvs, 0, sizeof(*gnvs));
97         gnvs->apic = 1;
98         gnvs->mpen = 1; /* Enable Multi Processing */
99
100         /* Enable both COM ports */
101         gnvs->cmap = 0x01;
102         gnvs->cmbp = 0x01;
103
104         /* IGD Displays */
105         gnvs->ndid = 3;
106         gnvs->did[0] = 0x80000100;
107         gnvs->did[1] = 0x80000240;
108         gnvs->did[2] = 0x80000410;
109         gnvs->did[3] = 0x80000410;
110         gnvs->did[4] = 0x00000005;
111 }
112
113 static void acpi_create_intel_hpet(acpi_hpet_t * hpet)
114 {
115 #define HPET_ADDR  0xfed00000ULL
116         acpi_header_t *header = &(hpet->header);
117         acpi_addr_t *addr = &(hpet->addr);
118
119         memset((void *) hpet, 0, sizeof(acpi_hpet_t));
120
121         /* fill out header fields */
122         memcpy(header->signature, "HPET", 4);
123         memcpy(header->oem_id, OEM_ID, 6);
124         memcpy(header->oem_table_id, "COREBOOT", 8);
125         memcpy(header->asl_compiler_id, ASLC, 4);
126
127         header->length = sizeof(acpi_hpet_t);
128         header->revision = 1;
129
130         /* fill out HPET address */
131         addr->space_id = 0;     /* Memory */
132         addr->bit_width = 64;
133         addr->bit_offset = 0;
134         addr->addrl = HPET_ADDR & 0xffffffff;
135         addr->addrh = HPET_ADDR >> 32;
136
137         hpet->id = 0x8086a201;  /* Intel */
138         hpet->number = 0x00;
139         hpet->min_tick = 0x0080;
140
141         header->checksum =
142             acpi_checksum((void *) hpet, sizeof(acpi_hpet_t));
143 }
144
145
146
147 #define IO_APIC_ADDR    0xfec00000UL
148
149 unsigned long acpi_fill_madt(unsigned long current)
150 {
151         /* Local APICs */
152         current = acpi_create_madt_lapics(current);
153
154         /* IOAPIC */
155         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
156                                 2, IO_APIC_ADDR, 0);
157
158         /* LAPIC_NMI */
159         current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)
160                                 current, 0,
161                                 MP_IRQ_POLARITY_HIGH |
162                                 MP_IRQ_TRIGGER_EDGE, 0x01);
163         current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)
164                                 current, 1, MP_IRQ_POLARITY_HIGH |
165                                 MP_IRQ_TRIGGER_EDGE, 0x01);
166
167         /* INT_SRC_OVR */
168         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
169                  current, 0, 0, 2, MP_IRQ_POLARITY_HIGH | MP_IRQ_TRIGGER_EDGE);
170         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
171                  current, 0, 9, 9, MP_IRQ_POLARITY_HIGH | MP_IRQ_TRIGGER_LEVEL);
172
173
174         return current;
175 }
176
177 unsigned long acpi_fill_ssdt_generator(unsigned long current, const char *oem_table_id)
178 {
179         generate_cpu_entries();
180         return (unsigned long) (acpigen_get_current());
181 }
182
183 unsigned long acpi_fill_slit(unsigned long current)
184 {
185         // Not implemented
186         return current;
187 }
188
189 unsigned long acpi_fill_srat(unsigned long current)
190 {
191         /* No NUMA, no SRAT */
192         return current;
193 }
194
195 void smm_setup_structures(void *gnvs, void *tcg, void *smi1);
196
197 #define ALIGN_CURRENT current = ((current + 0x0f) & -0x10)
198 unsigned long write_acpi_tables(unsigned long start)
199 {
200         unsigned long current;
201         int i;
202         acpi_rsdp_t *rsdp;
203         acpi_rsdt_t *rsdt;
204         acpi_xsdt_t *xsdt;
205         acpi_hpet_t *hpet;
206         acpi_madt_t *madt;
207         acpi_mcfg_t *mcfg;
208         acpi_fadt_t *fadt;
209         acpi_facs_t *facs;
210 #if CONFIG_HAVE_ACPI_SLIC
211         acpi_header_t *slic;
212 #endif
213         acpi_header_t *ssdt;
214         acpi_header_t *dsdt;
215         void *gnvs;
216
217         current = start;
218
219         /* Align ACPI tables to 16byte */
220         ALIGN_CURRENT;
221
222         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
223
224         /* We need at least an RSDP and an RSDT Table */
225         rsdp = (acpi_rsdp_t *) current;
226         current += sizeof(acpi_rsdp_t);
227         ALIGN_CURRENT;
228         rsdt = (acpi_rsdt_t *) current;
229         current += sizeof(acpi_rsdt_t);
230         ALIGN_CURRENT;
231         xsdt = (acpi_xsdt_t *) current;
232         current += sizeof(acpi_xsdt_t);
233         ALIGN_CURRENT;
234
235         /* clear all table memory */
236         memset((void *) start, 0, current - start);
237
238         acpi_write_rsdp(rsdp, rsdt, xsdt);
239         acpi_write_rsdt(rsdt);
240         acpi_write_xsdt(xsdt);
241
242         /*
243          * We explicitly add these tables later on:
244          */
245         printk(BIOS_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(rsdp, hpet);
252
253         /* If we want to use HPET Timers Linux wants an MADT */
254         printk(BIOS_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(rsdp, madt);
261
262         printk(BIOS_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(rsdp, mcfg);
268
269         printk(BIOS_DEBUG, "ACPI:     * FACS\n");
270         facs = (acpi_facs_t *) current;
271         current += sizeof(acpi_facs_t);
272         ALIGN_CURRENT;
273         acpi_create_facs(facs);
274
275         dsdt = (acpi_header_t *) current;
276         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
277         current += dsdt->length;
278         memcpy(dsdt, &AmlCode, dsdt->length);
279
280         /* Fix up global NVS region for SMI handler. The GNVS region lives
281          * in the (high) table area. The low memory map looks like this:
282          *
283          * 0x00000000 - 0x000003ff      Real Mode IVT
284          * 0x00000020 - 0x0000019c      Low MP Table (XXX conflict?)
285          * 0x00000400 - 0x000004ff      BDA (somewhat unused)
286          * 0x00000500 - 0x0000052f      Moved GDT
287          * 0x00000530 - 0x00000b64      coreboot table
288          * 0x0007c000 - 0x0007dfff      OS boot sector (unused?)
289          * 0x0007e000 - 0x0007ffff      free to use (so no good for acpi+smi)
290          * 0x00080000 - 0x0009fbff      usable ram
291          * 0x0009fc00 - 0x0009ffff      EBDA (unused?)
292          * 0x000a0000 - 0x000bffff      VGA memory
293          * 0x000c0000 - 0x000cffff      VGA option rom
294          * 0x000d0000 - 0x000dffff      free for other option roms?
295          * 0x000e0000 - 0x000fffff      SeaBIOS? (conflict with low tables:)
296          * 0x000f0000 - 0x000f03ff      PIRQ table
297          * 0x000f0400 - 0x000f66??      ACPI tables
298          * 0x000f66?? - 0x000f????      DMI tables
299          */
300
301         ALIGN_CURRENT;
302
303         /* Pack GNVS into the ACPI table area */
304         for (i=0; i < dsdt->length; i++) {
305                 if (*(u32*)(((u32)dsdt) + i) == 0xC0DEBABE) {
306                         printk(BIOS_DEBUG, "ACPI: Patching up global NVS in DSDT at offset 0x%04x -> 0x%08x\n", i, (u32)current);
307                         *(u32*)(((u32)dsdt) + i) = current; // 0x92 bytes
308                         break;
309                 }
310         }
311
312         /* And fill it */
313         acpi_create_gnvs((global_nvs_t *)current);
314
315         /* Keep pointer around */
316         gnvs = (void *)current;
317
318         current += 0x100;
319         ALIGN_CURRENT;
320
321         /* And tell SMI about it */
322         smm_setup_structures(gnvs, NULL, NULL);
323
324         /* We patched up the DSDT, so we need to recalculate the checksum */
325         dsdt->checksum = 0;
326         dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
327
328         printk(BIOS_DEBUG, "ACPI:     * DSDT @ %p Length %x\n", dsdt,
329                      dsdt->length);
330
331 #if CONFIG_HAVE_ACPI_SLIC
332         printk(BIOS_DEBUG, "ACPI:     * SLIC\n");
333         slic = (acpi_header_t *)current;
334         current += acpi_create_slic(current);
335         ALIGN_CURRENT;
336         acpi_add_table(rsdp, slic);
337 #endif
338
339         printk(BIOS_DEBUG, "ACPI:     * FADT\n");
340         fadt = (acpi_fadt_t *) current;
341         current += sizeof(acpi_fadt_t);
342         ALIGN_CURRENT;
343
344         acpi_create_fadt(fadt, facs, dsdt);
345         acpi_add_table(rsdp, fadt);
346
347         printk(BIOS_DEBUG, "ACPI:     * SSDT\n");
348         ssdt = (acpi_header_t *)current;
349         acpi_create_ssdt_generator(ssdt, "COREBOOT");
350         current += ssdt->length;
351         acpi_add_table(rsdp, ssdt);
352         ALIGN_CURRENT;
353
354         printk(BIOS_DEBUG, "current = %lx\n", current);
355
356         printk(BIOS_DEBUG, "ACPI:     * DMI (Linux workaround)\n");
357         memcpy((void *)0xfff80, dmi_table, DMI_TABLE_SIZE);
358 #if CONFIG_WRITE_HIGH_TABLES == 1
359         memcpy((void *)current, dmi_table, DMI_TABLE_SIZE);
360         current += DMI_TABLE_SIZE;
361         ALIGN_CURRENT;
362 #endif
363
364         printk(BIOS_INFO, "ACPI: done.\n");
365
366         /* Enable Dummy DCC ON# for DVI */
367         printk(BIOS_DEBUG, "Laptop handling...\n");
368         outb(inb(0x60f) & ~(1 << 5), 0x60f);
369
370         return current;
371 }