Please bear with me - another rename checkin. This qualifies as trivial, no
[coreboot.git] / src / southbridge / via / vt8237r / vt8237r_lpc.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License v2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 /* Inspiration from other VIA SB code. */
21
22 #include <arch/io.h>
23 #include <console/console.h>
24 #include <device/device.h>
25 #include <device/pci.h>
26 #include <device/pci_ids.h>
27 #include <pc80/mc146818rtc.h>
28 #include <cpu/x86/lapic.h>
29 #include <stdlib.h>
30 #include "vt8237r.h"
31 #include "chip.h"
32
33 #define ALL             (0xff << 24)
34 #define NONE            (0)
35 #define DISABLED        (1 << 16)
36 #define ENABLED         (0 << 16)
37 #define TRIGGER_EDGE    (0 << 15)
38 #define TRIGGER_LEVEL   (1 << 15)
39 #define POLARITY_HIGH   (0 << 13)
40 #define POLARITY_LOW    (1 << 13)
41 #define PHYSICAL_DEST   (0 << 11)
42 #define LOGICAL_DEST    (1 << 11)
43 #define ExtINT          (7 << 8)
44 #define NMI             (4 << 8)
45 #define SMI             (2 << 8)
46 #define INT             (1 << 8)
47
48 extern void dump_south(device_t dev);
49
50 static struct ioapicreg {
51         u32 reg;
52         u32 value_low;
53         u32 value_high;
54 } ioapic_table[] = {
55         /* IO-APIC virtual wire mode configuration. */
56         /* mask, trigger, polarity, destination, delivery, vector */
57         {0,  ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST |
58              ExtINT, NONE},
59         {1,  DISABLED, NONE},
60         {2,  DISABLED, NONE},
61         {3,  DISABLED, NONE},
62         {4,  DISABLED, NONE},
63         {5,  DISABLED, NONE},
64         {6,  DISABLED, NONE},
65         {7,  DISABLED, NONE},
66         {8,  DISABLED, NONE},
67         {9,  DISABLED, NONE},
68         {10, DISABLED, NONE},
69         {11, DISABLED, NONE},
70         {12, DISABLED, NONE},
71         {13, DISABLED, NONE},
72         {14, DISABLED, NONE},
73         {15, DISABLED, NONE},
74         {16, DISABLED, NONE},
75         {17, DISABLED, NONE},
76         {18, DISABLED, NONE},
77         {19, DISABLED, NONE},
78         {20, DISABLED, NONE},
79         {21, DISABLED, NONE},
80         {22, DISABLED, NONE},
81         {23, DISABLED, NONE},
82 };
83
84 static void setup_ioapic(u32 ioapic_base)
85 {
86         u32 value_low, value_high, val;
87         volatile u32 *l;
88         int i;
89
90         /* All delivered to CPU0. */
91         ioapic_table[0].value_high = (lapicid()) << (56 - 32);
92         l = (unsigned long *)ioapic_base;
93
94         /* Set APIC to FSB message bus. */
95         l[0] = 0x3;
96         val = l[4];
97         l[4] = (val & 0xFFFFFE) | 1;
98
99         /* Set APIC ADDR - this will be VT8237R_APIC_ID. */
100         l[0] = 0;
101         val = l[4];
102         l[4] = (val & 0xF0FFFF) | (VT8237R_APIC_ID << 24);
103
104         for (i = 0; i < ARRAY_SIZE(ioapic_table); i++) {
105                 l[0] = (ioapic_table[i].reg * 2) + 0x10;
106                 l[4] = ioapic_table[i].value_low;
107                 value_low = l[4];
108                 l[0] = (ioapic_table[i].reg * 2) + 0x11;
109                 l[4] = ioapic_table[i].value_high;
110                 value_high = l[4];
111
112                 if ((i == 0) && (value_low == 0xffffffff)) {
113                         printk_warning("IO APIC not responding.\n");
114                         return;
115                 }
116         }
117 }
118
119 /** Set up PCI IRQ routing, route everything through APIC. */
120 static void pci_routing_fixup(struct device *dev)
121 {
122         /* PCI PNP Interrupt Routing INTE/F - disable */
123         pci_write_config8(dev, 0x44, 0x00);
124
125         /* PCI PNP Interrupt Routing INTG/H - disable */
126         pci_write_config8(dev, 0x45, 0x00);
127
128         /* Route INTE-INTH through registers above, no map to INTA-INTD. */
129         pci_write_config8(dev, 0x46, 0x10);
130
131         /* PCI Interrupt Polarity */
132         pci_write_config8(dev, 0x54, 0x00);
133
134         /* PCI INTA# Routing */
135         pci_write_config8(dev, 0x55, 0x00);
136
137         /* PCI INTB#/C# Routing */
138         pci_write_config8(dev, 0x56, 0x00);
139
140         /* PCI INTD# Routing */
141         pci_write_config8(dev, 0x57, 0x00);
142 }
143
144 /**
145  * Set up the power management capabilities directly into ACPI mode.
146  * This avoids having to handle any System Management Interrupts (SMIs).
147  */
148 static void setup_pm(device_t dev)
149 {
150         /* Debounce LID and PWRBTN# Inputs for 16ms. */
151         pci_write_config8(dev, 0x80, 0x20);
152
153         /* Set ACPI base address to I/O VT8237R_ACPI_IO_BASE. */
154         pci_write_config16(dev, 0x88, VT8237R_ACPI_IO_BASE | 0x1);
155
156         /* Set ACPI to 9, must set IRQ 9 override to level! Set PSON gating. */
157         pci_write_config8(dev, 0x82, 0x40 | VT8237R_ACPI_IRQ);
158
159         /* Primary interupt channel, define wake events 0=IRQ0 15=IRQ15 1=en. */
160         pci_write_config16(dev, 0x84, 0x30b2);
161
162         /* SMI output level to low, 7.5us throttle clock */
163         pci_write_config8(dev, 0x8d, 0x18);
164
165         /* GP Timer Control 1s */
166         pci_write_config8(dev, 0x93, 0x88);
167
168         /* 7 = SMBus clock from RTC 32.768KHz
169          * 5 = Internal PLL reset from susp
170          * 2 = GPO2 is GPIO
171          */
172         pci_write_config8(dev, 0x94, 0xa4);
173
174         /* 7 = stp to sust delay 1msec
175          * 6 = SUSST# Deasserted Before PWRGD for STD
176          * 3 = GPO26/GPO27 is GPO 
177          * 2 = Disable Alert on Lan
178          */
179         pci_write_config8(dev, 0x95, 0xcc);
180
181         /* Disable GP3 timer. */
182         pci_write_config8(dev, 0x98, 0);
183
184         /* Enable SATA LED, disable special CPU Frequency Change -
185          * GPIO28 GPIO22 GPIO29 GPIO23 are GPIOs.
186          */
187         pci_write_config8(dev, 0xe5, 0x9);
188
189         /* REQ5 as PCI request input - should be together with INTE-INTH. */
190         pci_write_config8(dev, 0xe4, 0x4);
191
192         /* Enable ACPI accessm RTC signal gated with PSON. */
193         pci_write_config8(dev, 0x81, 0x84);
194
195         /* Clear status events. */
196         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x00);
197         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x20);
198         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x28);
199         outl(0xffffffff, VT8237R_ACPI_IO_BASE + 0x30);
200
201         /* Disable SCI on GPIO. */
202         outw(0x0, VT8237R_ACPI_IO_BASE + 0x22);
203
204         /* Disable SMI on GPIO. */
205         outw(0x0, VT8237R_ACPI_IO_BASE + 0x24);
206
207         /* Disable all global enable SMIs. */
208         outw(0x0, VT8237R_ACPI_IO_BASE + 0x2a);
209
210         /* All SMI off, both IDE buses ON, PSON rising edge. */
211         outw(0x0, VT8237R_ACPI_IO_BASE + 0x2c);
212
213         /* Primary activity SMI disable. */
214         outl(0x0, VT8237R_ACPI_IO_BASE + 0x34);
215
216         /* GP timer reload on none. */
217         outl(0x0, VT8237R_ACPI_IO_BASE + 0x38);
218
219         /* Disable extended IO traps. */
220         outb(0x0, VT8237R_ACPI_IO_BASE + 0x42);
221
222         /* SCI is generated for RTC/pwrBtn/slpBtn. */
223         outw(0x001, VT8237R_ACPI_IO_BASE + 0x04);
224
225         /* FIXME: Intel needs more bit set for C2/C3. */
226
227         /* Allow SLP# signal to assert LDTSTOP_L.
228          * Will work for C3 and for FID/VID change.
229          */
230         outb(0x1, VT8237R_ACPI_IO_BASE + 0x11);
231 }
232
233 static void vt8237r_init(struct device *dev)
234 {
235         u8 enables, byte;
236
237         /* Enable addr/data stepping. */
238         byte = pci_read_config8(dev, PCI_COMMAND);
239         byte |= PCI_COMMAND_WAIT;
240         pci_write_config8(dev, PCI_COMMAND, byte);
241
242         /* Enable the internal I/O decode. */
243         enables = pci_read_config8(dev, 0x6C);
244         enables |= 0x80;
245         pci_write_config8(dev, 0x6C, enables);
246
247         /* FIXME: Map 4MB of flash into the address space,
248          * this should be in CAR call.
249          */
250         /* pci_write_config8(dev, 0x41, 0x7f); */
251
252         /* Set bit 6 of 0x40 (I/O recovery time).
253          * IMPORTANT FIX - EISA = ECLR reg at 0x4d0! Decoding must be on so
254          * that PCI interrupts can be properly marked as level triggered.
255          */
256         enables = pci_read_config8(dev, 0x40);
257         enables |= 0x44;
258         pci_write_config8(dev, 0x40, enables);
259
260         /* Line buffer control */
261         enables = pci_read_config8(dev, 0x42);
262         enables |= 0xf8;
263         pci_write_config8(dev, 0x42, enables);
264
265         /* Delay transaction control */
266         pci_write_config8(dev, 0x43, 0xb);
267
268         /* I/O recovery time */
269         pci_write_config8(dev, 0x4c, 0x44);
270
271         /* ROM memory cycles go to LPC. */
272         pci_write_config8(dev, 0x59, 0x80);
273
274         /* Bypass APIC De-Assert Message, INTE#, INTF#, INTG#, INTH# as PCI. */
275         pci_write_config8(dev, 0x5B, 0xb);
276
277         /* Set Read Pass Write Control Enable (force A2 from APIC FSB to low). */
278         pci_write_config8(dev, 0x48, 0x8c);
279
280         /* Set 0x58 to 0x43 APIC and RTC. */
281         pci_write_config8(dev, 0x58, 0x43);
282
283         /* Set bit 3 of 0x4f (use INIT# as CPU reset). */
284         enables = pci_read_config8(dev, 0x4f);
285         enables |= 0x08;
286         pci_write_config8(dev, 0x4f, enables);
287
288         /* Enable serial IRQ, 6PCI clocks. */
289         pci_write_config8(dev, 0x52, 0x9);
290
291         /* Enable HPET at VT8237R_HPET_ADDR. */
292         pci_write_config32(dev, 0x68, (VT8237R_HPET_ADDR | 0x80));
293
294         /* Power management setup */
295         setup_pm(dev);
296
297         /* Start the RTC. */
298         rtc_init(0);
299 }
300
301 static void vt8237r_read_resources(device_t dev)
302 {
303         struct resource *res;
304
305         pci_dev_read_resources(dev);
306         /* Fixed APIC resource */
307         res = new_resource(dev, 0x44);
308         res->base = VT8237R_APIC_BASE;
309         res->size = 256;
310         res->limit = res->base + res->size - 1;
311         res->align = 8;
312         res->gran = 8;
313         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
314                      IORESOURCE_STORED | IORESOURCE_ASSIGNED;
315 }
316
317 /**
318  * The VT8237R is not a PCI bridge and has no resources of its own (other
319  * than standard PC I/O addresses), however it does control the ISA bus
320  * and so we need to manually call enable childrens resources on that bus.
321  */
322 static void vt8237r_enable_resources(device_t dev)
323 {
324         pci_dev_enable_resources(dev);
325         enable_childrens_resources(dev);
326 }
327
328 static void init_keyboard(struct device *dev)
329 {
330         u8 regval = pci_read_config8(dev, 0x51);
331         if (regval & 0x1)
332                 init_pc_keyboard(0x60, 0x64, 0);
333 }
334
335 static void southbridge_init(struct device *dev)
336 {
337         vt8237r_init(dev);
338         pci_routing_fixup(dev);
339         setup_ioapic(VT8237R_APIC_BASE);
340         setup_i8259();
341         init_keyboard(dev);
342 }
343
344 static const struct device_operations vt8237r_lpc_ops = {
345         .read_resources         = vt8237r_read_resources,
346         .set_resources          = pci_dev_set_resources,
347         .enable_resources       = vt8237r_enable_resources,
348         .init                   = &southbridge_init,
349         .scan_bus               = scan_static_bus,
350 };
351
352 static const struct pci_driver lpc_driver __pci_driver = {
353         .ops    = &vt8237r_lpc_ops,
354         .vendor = PCI_VENDOR_ID_VIA,
355         .device = PCI_DEVICE_ID_VIA_VT8237R_LPC,
356 };