I missed three files.
[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 = (u32 *)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
153 extern u8 acpi_slp_type;
154
155
156 static void setup_pm(device_t dev)
157 {
158         u16 tmp;
159         /* Debounce LID and PWRBTN# Inputs for 16ms. */
160         pci_write_config8(dev, 0x80, 0x20);
161
162         /* Set ACPI base address to I/O VT8237R_ACPI_IO_BASE. */
163         pci_write_config16(dev, 0x88, VT8237R_ACPI_IO_BASE | 0x1);
164
165         /* Set ACPI to 9, must set IRQ 9 override to level! Set PSON gating. */
166         pci_write_config8(dev, 0x82, 0x40 | VT8237R_ACPI_IRQ);
167
168         /* Primary interupt channel, define wake events 0=IRQ0 15=IRQ15 1=en. */
169         pci_write_config16(dev, 0x84, 0x30b2);
170
171         /* SMI output level to low, 7.5us throttle clock */
172         pci_write_config8(dev, 0x8d, 0x18);
173
174         /* GP Timer Control 1s */
175         pci_write_config8(dev, 0x93, 0x88);
176
177         /*
178          * 7 = SMBus clock from RTC 32.768KHz
179          * 5 = Internal PLL reset from susp disabled
180          * 2 = GPO2 is SUSA#
181          */
182         pci_write_config8(dev, 0x94, 0xa0);
183
184         /*
185          * 7 = stp to sust delay 1msec
186          * 6 = SUSST# Deasserted Before PWRGD for STD
187          * 4 = PWRGOOD reset on VT8237A/S
188          * 3 = GPO26/GPO27 is GPO 
189          * 2 = Disable Alert on Lan
190          */
191         pci_write_config8(dev, 0x95, 0xcc);
192
193         /* Disable GP3 timer. */
194         pci_write_config8(dev, 0x98, 0);
195
196         /* Enable ACPI accessm RTC signal gated with PSON. */
197         pci_write_config8(dev, 0x81, 0x84);
198
199         /* Clear status events. */
200         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x00);
201         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x20);
202         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x28);
203         outl(0xffffffff, VT8237R_ACPI_IO_BASE + 0x30);
204
205         /* Disable SCI on GPIO. */
206         outw(0x0, VT8237R_ACPI_IO_BASE + 0x22);
207
208         /* Disable SMI on GPIO. */
209         outw(0x0, VT8237R_ACPI_IO_BASE + 0x24);
210
211         /* Disable all global enable SMIs. */
212         outw(0x0, VT8237R_ACPI_IO_BASE + 0x2a);
213
214         /* All SMI off, both IDE buses ON, PSON rising edge. */
215         outw(0x0, VT8237R_ACPI_IO_BASE + 0x2c);
216
217         /* Primary activity SMI disable. */
218         outl(0x0, VT8237R_ACPI_IO_BASE + 0x34);
219
220         /* GP timer reload on none. */
221         outl(0x0, VT8237R_ACPI_IO_BASE + 0x38);
222
223         /* Disable extended IO traps. */
224         outb(0x0, VT8237R_ACPI_IO_BASE + 0x42);
225
226         /* SCI is generated for RTC/pwrBtn/slpBtn. */
227         tmp = inw(VT8237R_ACPI_IO_BASE + 0x04);
228 #if CONFIG_HAVE_ACPI_RESUME == 1
229         acpi_slp_type = ((tmp & (7 << 10)) >> 10) == 1 ? 3 : 0 ;
230         printk_debug("SLP_TYP type was %x %x\n", tmp, acpi_slp_type);
231 #endif
232         /* clear sleep */
233         tmp &= ~(7 << 10);
234         tmp |= 1;
235         outw(tmp, VT8237R_ACPI_IO_BASE + 0x04);
236
237
238
239
240 }
241
242 static void vt8237r_init(struct device *dev)
243 {
244         u8 enables;
245
246         printk_spew("Entering vt8237r_init.\n");
247         
248 #ifdef CONFIG_EPIA_VT8237R_INIT
249         printk_spew("vt8237r_init SATA LED.\n");
250         /*
251          * TODO: Looks like stock BIOS can do this but causes a hang
252          * Enable SATA LED, disable special CPU Frequency Change -
253          * GPIO28 GPIO22 GPIO29 GPIO23 are GPIOs.
254          * Setup to match EPIA default
255          * PCS0# on Pin U1
256          */
257         enables = pci_read_config8(dev, 0xe5);
258         enables |= 0x02;
259         pci_write_config8(dev, 0xe5, enables);
260         
261         printk_spew("vt8237r_init PCI Req.\n");
262         /* 
263          * Enable Flash Write Access. 
264          * Note EPIA-N Does not use REQ5 or PCISTP#(Hang)
265          */
266         enables = pci_read_config8(dev, 0xe4);
267         enables |= 0x2B;
268         pci_write_config8(dev, 0xe4, enables);
269
270 #else 
271         /*
272          * Enable SATA LED, disable special CPU Frequency Change -
273          * GPIO28 GPIO22 GPIO29 GPIO23 are GPIOs.
274          */
275         pci_write_config8(dev, 0xe5, 0x09);
276         
277         /* REQ5 as PCI request input - should be together with INTE-INTH. */
278         pci_write_config8(dev, 0xe4, 0x4);
279 #endif
280
281
282         printk_spew("vt8237r_init CPU Rst.\n");
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         printk_spew("vt8237r_init Read Pass Write Ctrl.\n");
289         /*
290          * Set Read Pass Write Control Enable
291          * (force A2 from APIC FSB to low).
292          */
293         pci_write_config8(dev, 0x48, 0x8c);
294
295         printk_spew("vt8237r_init calling southbridge_init_common.\n");
296         southbridge_init_common(dev);
297
298         /* FIXME: Intel needs more bit set for C2/C3. */
299
300         /*
301          * Allow SLP# signal to assert LDTSTOP_L.
302          * Will work for C3 and for FID/VID change.
303          */
304         outb(0x1, VT8237R_ACPI_IO_BASE + 0x11);
305         
306         printk_spew("Leaving vt8237r_init.\n");
307 }
308
309 static void vt8237s_init(struct device *dev)
310 {
311         u32 tmp;
312
313         /* Put SPI base VT8237S_SPI_MEM_BASE. */
314         tmp = pci_read_config32(dev, 0xbc);
315         pci_write_config32(dev, 0xbc,
316                            (VT8237S_SPI_MEM_BASE >> 8) | (tmp & 0xFF000000));
317
318         /*
319          * REQ5 as PCI request input - should be together with INTE-INTH. 
320          */
321         pci_write_config8(dev, 0xe4, 0x04);
322
323         /* Reduce further the STPCLK/LDTSTP signal to 5us. */
324         pci_write_config8(dev, 0xec, 0x4);
325
326         /* Host Bus Power Management Control, maybe not needed */
327         pci_write_config8(dev, 0x8c, 0x5);
328
329         /* Enable HPET at VT8237R_HPET_ADDR., does not work correctly on R. */
330         pci_write_config32(dev, 0x68, (VT8237R_HPET_ADDR | 0x80));
331
332         southbridge_init_common(dev);
333
334         /* FIXME: Intel needs more bit set for C2/C3. */
335
336         /*
337          * Allow SLP# signal to assert LDTSTOP_L.
338          * Will work for C3 and for FID/VID change. FIXME FIXME, pre rev A2.
339          */
340         outb(0xff, VT8237R_ACPI_IO_BASE + 0x50);
341
342         dump_south(dev);
343 }
344
345 static void vt8237_common_init(struct device *dev)
346 {
347         u8 enables, byte;
348
349         /* Enable addr/data stepping. */
350         byte = pci_read_config8(dev, PCI_COMMAND);
351         byte |= PCI_COMMAND_WAIT;
352         pci_write_config8(dev, PCI_COMMAND, byte);
353
354         /* Enable the internal I/O decode. */
355         enables = pci_read_config8(dev, 0x6C);
356         enables |= 0x80;
357         pci_write_config8(dev, 0x6C, enables);
358
359         /*
360          * ROM decode
361          * bit range
362          *   7 000E0000h-000EFFFFh
363          *   6 FFF00000h-FFF7FFFFh
364          *   5 FFE80000h-FFEFFFFFh
365          *   4 FFE00000h-FFE7FFFFh
366          *   3 FFD80000h-FFDFFFFFh
367          *   2 FFD00000h-FFD7FFFFh
368          *   1 FFC80000h-FFCFFFFFh
369          *   0 FFC00000h-FFC7FFFFh
370          * So 0x7f here sets ROM decode to FFC00000-FFFFFFFF or 4Mbyte.
371          */
372         pci_write_config8(dev, 0x41, 0x7f);
373
374         /*
375          * Set bit 6 of 0x40 (I/O recovery time).
376          * IMPORTANT FIX - EISA = ECLR reg at 0x4d0! Decoding must be on so
377          * that PCI interrupts can be properly marked as level triggered.
378          */
379         enables = pci_read_config8(dev, 0x40);
380         enables |= 0x44;
381         pci_write_config8(dev, 0x40, enables);
382
383         /* Line buffer control */
384         enables = pci_read_config8(dev, 0x42);
385         enables |= 0xf8;
386         pci_write_config8(dev, 0x42, enables);
387
388         /* Delay transaction control */
389         pci_write_config8(dev, 0x43, 0xb);
390
391         /* I/O recovery time, default IDE routing */
392         pci_write_config8(dev, 0x4c, 0x44);
393
394         /* ROM memory cycles go to LPC. */
395         pci_write_config8(dev, 0x59, 0x80);
396
397         /*
398          * Bit | Meaning
399          * -------------
400          *   3 | Bypass APIC De-Assert Message (1=Enable)
401          *   1 | possibly "INTE#, INTF#, INTG#, INTH# as PCI"
402          *     | bit 1=1 works for Aaron at VIA, bit 1=0 works for jakllsch
403          *   0 | Dynamic Clock Gating Main Switch (1=Enable)
404          */
405         pci_write_config8(dev, 0x5b, 0xb);
406
407         /* Set 0x58 to 0x43 APIC and RTC. */
408         pci_write_config8(dev, 0x58, 0x43);
409
410         /* Enable serial IRQ, 6PCI clocks. */
411         pci_write_config8(dev, 0x52, 0x9);
412
413         /* Power management setup */
414         setup_pm(dev);
415
416         /* Start the RTC. */
417         rtc_init(0);
418 }
419
420 static void vt8237r_read_resources(device_t dev)
421 {
422         struct resource *res;
423
424         pci_dev_read_resources(dev);
425         /* Fixed APIC resource */
426         res = new_resource(dev, 0x44);
427         res->base = VT8237R_APIC_BASE;
428         res->size = 256;
429         res->limit = res->base + res->size - 1;
430         res->align = 8;
431         res->gran = 8;
432         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
433                      IORESOURCE_STORED | IORESOURCE_ASSIGNED;
434 }
435
436 /**
437  * The VT8237R is not a PCI bridge and has no resources of its own (other
438  * than standard PC I/O addresses), however it does control the ISA bus
439  * and so we need to manually call enable childrens resources on that bus.
440  */
441 static void vt8237r_enable_resources(device_t dev)
442 {
443         pci_dev_enable_resources(dev);
444         enable_childrens_resources(dev);
445 }
446
447 static void init_keyboard(struct device *dev)
448 {
449         u8 regval = pci_read_config8(dev, 0x51);
450         if (regval & 0x1)
451                 init_pc_keyboard(0x60, 0x64, 0);
452 }
453
454 static void southbridge_init_common(struct device *dev)
455 {
456         vt8237_common_init(dev);
457         pci_routing_fixup(dev);
458         setup_ioapic(VT8237R_APIC_BASE);
459         setup_i8259();
460         init_keyboard(dev);
461 }
462
463 static const struct device_operations vt8237r_lpc_ops_s = {
464         .read_resources         = vt8237r_read_resources,
465         .set_resources          = pci_dev_set_resources,
466         .enable_resources       = vt8237r_enable_resources,
467         .init                   = &vt8237s_init,
468         .scan_bus               = scan_static_bus,
469 };
470
471 static const struct device_operations vt8237r_lpc_ops_r = {
472         .read_resources         = vt8237r_read_resources,
473         .set_resources          = pci_dev_set_resources,
474         .enable_resources       = vt8237r_enable_resources,
475         .init                   = &vt8237r_init,
476         .scan_bus               = scan_static_bus,
477 };
478
479 static const struct pci_driver lpc_driver_r __pci_driver = {
480         .ops    = &vt8237r_lpc_ops_r,
481         .vendor = PCI_VENDOR_ID_VIA,
482         .device = PCI_DEVICE_ID_VIA_VT8237R_LPC,
483 };
484
485 static const struct pci_driver lpc_driver_s __pci_driver = {
486         .ops    = &vt8237r_lpc_ops_s,
487         .vendor = PCI_VENDOR_ID_VIA,
488         .device = PCI_DEVICE_ID_VIA_VT8237S_LPC,
489 };