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