4fbc29575afd44c2aa8216f7643316a61ba04101
[coreboot.git] / src / southbridge / via / vt8237r / vt8237r_lpc.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007, 2008 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 <pc80/keyboard.h>
30 #include <pc80/i8259.h>
31 #include <stdlib.h>
32 #include "vt8237r.h"
33 #include "chip.h"
34
35 #define ALL             (0xff << 24)
36 #define NONE            (0)
37 #define DISABLED        (1 << 16)
38 #define ENABLED         (0 << 16)
39 #define TRIGGER_EDGE    (0 << 15)
40 #define TRIGGER_LEVEL   (1 << 15)
41 #define POLARITY_HIGH   (0 << 13)
42 #define POLARITY_LOW    (1 << 13)
43 #define PHYSICAL_DEST   (0 << 11)
44 #define LOGICAL_DEST    (1 << 11)
45 #define ExtINT          (7 << 8)
46 #define NMI             (4 << 8)
47 #define SMI             (2 << 8)
48 #define INT             (1 << 8)
49
50 extern void dump_south(device_t dev);
51
52 static struct ioapicreg {
53         u32 reg;
54         u32 value_low;
55         u32 value_high;
56 } ioapic_table[] = {
57         /* IO-APIC virtual wire mode configuration. */
58         /* mask, trigger, polarity, destination, delivery, vector */
59         {0, ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST |
60                     ExtINT, NONE},
61         {1,  DISABLED, NONE},
62         {2,  DISABLED, NONE},
63         {3,  DISABLED, NONE},
64         {4,  DISABLED, NONE},
65         {5,  DISABLED, NONE},
66         {6,  DISABLED, NONE},
67         {7,  DISABLED, NONE},
68         {8,  DISABLED, NONE},
69         {9,  DISABLED, NONE},
70         {10, DISABLED, NONE},
71         {11, DISABLED, NONE},
72         {12, DISABLED, NONE},
73         {13, DISABLED, NONE},
74         {14, DISABLED, NONE},
75         {15, DISABLED, NONE},
76         {16, DISABLED, NONE},
77         {17, DISABLED, NONE},
78         {18, DISABLED, NONE},
79         {19, DISABLED, NONE},
80         {20, DISABLED, NONE},
81         {21, DISABLED, NONE},
82         {22, DISABLED, NONE},
83         {23, DISABLED, NONE},
84 };
85
86 static void setup_ioapic(u32 ioapic_base)
87 {
88         u32 value_low, value_high, val;
89         volatile u32 *l;
90         int i;
91
92         /* All delivered to CPU0. */
93         ioapic_table[0].value_high = (lapicid()) << (56 - 32);
94         l = (unsigned long *)ioapic_base;
95
96         /* Set APIC to FSB message bus. */
97         l[0] = 0x3;
98         val = l[4];
99         l[4] = (val & 0xFFFFFE) | 1;
100
101         /* Set APIC ADDR - this will be VT8237R_APIC_ID. */
102         l[0] = 0;
103         val = l[4];
104         l[4] = (val & 0xF0FFFF) | (VT8237R_APIC_ID << 24);
105
106         for (i = 0; i < ARRAY_SIZE(ioapic_table); i++) {
107                 l[0] = (ioapic_table[i].reg * 2) + 0x10;
108                 l[4] = ioapic_table[i].value_low;
109                 value_low = l[4];
110                 l[0] = (ioapic_table[i].reg * 2) + 0x11;
111                 l[4] = ioapic_table[i].value_high;
112                 value_high = l[4];
113
114                 if ((i == 0) && (value_low == 0xffffffff)) {
115                         printk_warning("IO APIC not responding.\n");
116                         return;
117                 }
118         }
119 }
120
121 static void southbridge_init_common(struct device *dev);
122
123 /** Set up PCI IRQ routing, route everything through APIC. */
124 static void pci_routing_fixup(struct device *dev)
125 {
126         /* PCI PNP Interrupt Routing INTE/F - disable */
127         pci_write_config8(dev, 0x44, 0x00);
128
129         /* PCI PNP Interrupt Routing INTG/H - disable */
130         pci_write_config8(dev, 0x45, 0x00);
131
132         /* Route INTE-INTH through registers above, no map to INTA-INTD. */
133         pci_write_config8(dev, 0x46, 0x10);
134
135         /* PCI Interrupt Polarity */
136         pci_write_config8(dev, 0x54, 0x00);
137
138         /* PCI INTA# Routing */
139         pci_write_config8(dev, 0x55, 0x00);
140
141         /* PCI INTB#/C# Routing */
142         pci_write_config8(dev, 0x56, 0x00);
143
144         /* PCI INTD# Routing */
145         pci_write_config8(dev, 0x57, 0x00);
146 }
147
148 /**
149  * Set up the power management capabilities directly into ACPI mode.
150  * This avoids having to handle any System Management Interrupts (SMIs).
151  */
152 static void setup_pm(device_t dev)
153 {
154         /* Debounce LID and PWRBTN# Inputs for 16ms. */
155         pci_write_config8(dev, 0x80, 0x20);
156
157         /* Set ACPI base address to I/O VT8237R_ACPI_IO_BASE. */
158         pci_write_config16(dev, 0x88, VT8237R_ACPI_IO_BASE | 0x1);
159
160         /* Set ACPI to 9, must set IRQ 9 override to level! Set PSON gating. */
161         pci_write_config8(dev, 0x82, 0x40 | VT8237R_ACPI_IRQ);
162
163         /* Primary interupt channel, define wake events 0=IRQ0 15=IRQ15 1=en. */
164         pci_write_config16(dev, 0x84, 0x30b2);
165
166         /* SMI output level to low, 7.5us throttle clock */
167         pci_write_config8(dev, 0x8d, 0x18);
168
169         /* GP Timer Control 1s */
170         pci_write_config8(dev, 0x93, 0x88);
171
172         /*
173          * 7 = SMBus clock from RTC 32.768KHz
174          * 5 = Internal PLL reset from susp
175          * 2 = GPO2 is GPIO
176          */
177         pci_write_config8(dev, 0x94, 0xa4);
178
179         /*
180          * 7 = stp to sust delay 1msec
181          * 6 = SUSST# Deasserted Before PWRGD for STD
182          * 4 = PWRGOOD reset on VT8237A/S
183          * 3 = GPO26/GPO27 is GPO 
184          * 2 = Disable Alert on Lan
185          */
186         pci_write_config8(dev, 0x95, 0xcc);
187
188         /* Disable GP3 timer. */
189         pci_write_config8(dev, 0x98, 0);
190
191         /* Enable ACPI accessm RTC signal gated with PSON. */
192         pci_write_config8(dev, 0x81, 0x84);
193
194         /* Clear status events. */
195         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x00);
196         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x20);
197         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x28);
198         outl(0xffffffff, VT8237R_ACPI_IO_BASE + 0x30);
199
200         /* Disable SCI on GPIO. */
201         outw(0x0, VT8237R_ACPI_IO_BASE + 0x22);
202
203         /* Disable SMI on GPIO. */
204         outw(0x0, VT8237R_ACPI_IO_BASE + 0x24);
205
206         /* Disable all global enable SMIs. */
207         outw(0x0, VT8237R_ACPI_IO_BASE + 0x2a);
208
209         /* All SMI off, both IDE buses ON, PSON rising edge. */
210         outw(0x0, VT8237R_ACPI_IO_BASE + 0x2c);
211
212         /* Primary activity SMI disable. */
213         outl(0x0, VT8237R_ACPI_IO_BASE + 0x34);
214
215         /* GP timer reload on none. */
216         outl(0x0, VT8237R_ACPI_IO_BASE + 0x38);
217
218         /* Disable extended IO traps. */
219         outb(0x0, VT8237R_ACPI_IO_BASE + 0x42);
220
221         /* SCI is generated for RTC/pwrBtn/slpBtn. */
222         outw(0x001, VT8237R_ACPI_IO_BASE + 0x04);
223 }
224
225 static void vt8237r_init(struct device *dev)
226 {
227         u8 enables;
228
229         /*
230          * Enable SATA LED, disable special CPU Frequency Change -
231          * GPIO28 GPIO22 GPIO29 GPIO23 are GPIOs.
232          */
233         pci_write_config8(dev, 0xe5, 0x9);
234
235         /* REQ5 as PCI request input - should be together with INTE-INTH. */
236         pci_write_config8(dev, 0xe4, 0x4);
237
238         /* Set bit 3 of 0x4f (use INIT# as CPU reset). */
239         enables = pci_read_config8(dev, 0x4f);
240         enables |= 0x08;
241         pci_write_config8(dev, 0x4f, enables);
242
243         /*
244          * Set Read Pass Write Control Enable
245          * (force A2 from APIC FSB to low).
246          */
247         pci_write_config8(dev, 0x48, 0x8c);
248
249         southbridge_init_common(dev);
250
251         /* FIXME: Intel needs more bit set for C2/C3. */
252
253         /*
254          * Allow SLP# signal to assert LDTSTOP_L.
255          * Will work for C3 and for FID/VID change.
256          */
257         outb(0x1, VT8237R_ACPI_IO_BASE + 0x11);
258 }
259
260 static void vt8237s_init(struct device *dev)
261 {
262         u32 tmp;
263
264         /* Put SPI base VT8237S_SPI_MEM_BASE. */
265         tmp = pci_read_config32(dev, 0xbc);
266         pci_write_config32(dev, 0xbc,
267                            (VT8237S_SPI_MEM_BASE >> 8) | (tmp & 0xFF000000));
268
269         /*
270          * REQ5 as PCI request input - should be together with INTE-INTH. 
271          */
272         pci_write_config8(dev, 0xe4, 0x04);
273
274         /* Reduce further the STPCLK/LDTSTP signal to 5us. */
275         pci_write_config8(dev, 0xec, 0x4);
276
277         /* Host Bus Power Management Control, maybe not needed */
278         pci_write_config8(dev, 0x8c, 0x5);
279
280         /* Enable HPET at VT8237R_HPET_ADDR., does not work correctly on R. */
281         pci_write_config32(dev, 0x68, (VT8237R_HPET_ADDR | 0x80));
282
283         southbridge_init_common(dev);
284
285         /* FIXME: Intel needs more bit set for C2/C3. */
286
287         /*
288          * Allow SLP# signal to assert LDTSTOP_L.
289          * Will work for C3 and for FID/VID change. FIXME FIXME, pre rev A2.
290          */
291         outb(0xff, VT8237R_ACPI_IO_BASE + 0x50);
292
293         dump_south(dev);
294 }
295
296 static void vt8237_common_init(struct device *dev)
297 {
298         u8 enables, byte;
299
300         /* Enable addr/data stepping. */
301         byte = pci_read_config8(dev, PCI_COMMAND);
302         byte |= PCI_COMMAND_WAIT;
303         pci_write_config8(dev, PCI_COMMAND, byte);
304
305         /* Enable the internal I/O decode. */
306         enables = pci_read_config8(dev, 0x6C);
307         enables |= 0x80;
308         pci_write_config8(dev, 0x6C, enables);
309
310         /*
311          * ROM decode
312          * bit range
313          *   7 000E0000h-000EFFFFh
314          *   6 FFF00000h-FFF7FFFFh
315          *   5 FFE80000h-FFEFFFFFh
316          *   4 FFE00000h-FFE7FFFFh
317          *   3 FFD80000h-FFDFFFFFh
318          *   2 FFD00000h-FFD7FFFFh
319          *   1 FFC80000h-FFCFFFFFh
320          *   0 FFC00000h-FFC7FFFFh
321          * So 0x7f here sets ROM decode to FFC00000-FFFFFFFF or 4Mbyte.
322          */
323         pci_write_config8(dev, 0x41, 0x7f);
324
325         /*
326          * Set bit 6 of 0x40 (I/O recovery time).
327          * IMPORTANT FIX - EISA = ECLR reg at 0x4d0! Decoding must be on so
328          * that PCI interrupts can be properly marked as level triggered.
329          */
330         enables = pci_read_config8(dev, 0x40);
331         enables |= 0x44;
332         pci_write_config8(dev, 0x40, enables);
333
334         /* Line buffer control */
335         enables = pci_read_config8(dev, 0x42);
336         enables |= 0xf8;
337         pci_write_config8(dev, 0x42, enables);
338
339         /* Delay transaction control */
340         pci_write_config8(dev, 0x43, 0xb);
341
342         /* I/O recovery time, default IDE routing */
343         pci_write_config8(dev, 0x4c, 0x44);
344
345         /* ROM memory cycles go to LPC. */
346         pci_write_config8(dev, 0x59, 0x80);
347
348         /*
349          * Bit | Meaning
350          * -------------
351          *   3 | Bypass APIC De-Assert Message (1=Enable)
352          *   1 | possibly "INTE#, INTF#, INTG#, INTH# as PCI"
353          *     | bit 1=1 works for Aaron at VIA, bit 1=0 works for jakllsch
354          *   0 | Dynamic Clock Gating Main Switch (1=Enable)
355          */
356         pci_write_config8(dev, 0x5b, 0xb);
357
358         /* Set 0x58 to 0x43 APIC and RTC. */
359         pci_write_config8(dev, 0x58, 0x43);
360
361         /* Enable serial IRQ, 6PCI clocks. */
362         pci_write_config8(dev, 0x52, 0x9);
363
364         /* Power management setup */
365         setup_pm(dev);
366
367         /* Start the RTC. */
368         rtc_init(0);
369 }
370
371 static void vt8237r_read_resources(device_t dev)
372 {
373         struct resource *res;
374
375         pci_dev_read_resources(dev);
376         /* Fixed APIC resource */
377         res = new_resource(dev, 0x44);
378         res->base = VT8237R_APIC_BASE;
379         res->size = 256;
380         res->limit = res->base + res->size - 1;
381         res->align = 8;
382         res->gran = 8;
383         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
384                      IORESOURCE_STORED | IORESOURCE_ASSIGNED;
385 }
386
387 /**
388  * The VT8237R is not a PCI bridge and has no resources of its own (other
389  * than standard PC I/O addresses), however it does control the ISA bus
390  * and so we need to manually call enable childrens resources on that bus.
391  */
392 static void vt8237r_enable_resources(device_t dev)
393 {
394         pci_dev_enable_resources(dev);
395         enable_childrens_resources(dev);
396 }
397
398 static void init_keyboard(struct device *dev)
399 {
400         u8 regval = pci_read_config8(dev, 0x51);
401         if (regval & 0x1)
402                 init_pc_keyboard(0x60, 0x64, 0);
403 }
404
405 static void southbridge_init_common(struct device *dev)
406 {
407         vt8237_common_init(dev);
408         pci_routing_fixup(dev);
409         setup_ioapic(VT8237R_APIC_BASE);
410         setup_i8259();
411         init_keyboard(dev);
412 }
413
414 static const struct device_operations vt8237r_lpc_ops_s = {
415         .read_resources         = vt8237r_read_resources,
416         .set_resources          = pci_dev_set_resources,
417         .enable_resources       = vt8237r_enable_resources,
418         .init                   = &vt8237s_init,
419         .scan_bus               = scan_static_bus,
420 };
421
422 static const struct device_operations vt8237r_lpc_ops_r = {
423         .read_resources         = vt8237r_read_resources,
424         .set_resources          = pci_dev_set_resources,
425         .enable_resources       = vt8237r_enable_resources,
426         .init                   = &vt8237r_init,
427         .scan_bus               = scan_static_bus,
428 };
429
430 static const struct pci_driver lpc_driver_r __pci_driver = {
431         .ops    = &vt8237r_lpc_ops_r,
432         .vendor = PCI_VENDOR_ID_VIA,
433         .device = PCI_DEVICE_ID_VIA_VT8237R_LPC,
434 };
435
436 static const struct pci_driver lpc_driver_s __pci_driver = {
437         .ops    = &vt8237r_lpc_ops_s,
438         .vendor = PCI_VENDOR_ID_VIA,
439         .device = PCI_DEVICE_ID_VIA_VT8237S_LPC,
440 };