25a854ce22c0d694099a7b457abf5415eba0b02d
[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  * Copyright (C) 2009       Jon Harrison <bothlyn@blueyonder.co.uk>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License v2 as published by
9  * the Free Software Foundation.
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, MA  02110-1301 USA
19  */
20
21 /* Inspiration from other VIA SB code. */
22
23 #include <arch/io.h>
24 #include <console/console.h>
25 #include <device/device.h>
26 #include <device/pci.h>
27 #include <device/pci_ids.h>
28 #include <pc80/mc146818rtc.h>
29 #include <arch/ioapic.h>
30 #include <cpu/x86/lapic.h>
31 #include <pc80/keyboard.h>
32 #include <pc80/i8259.h>
33 #include <stdlib.h>
34 #include "vt8237r.h"
35 #include "chip.h"
36
37 extern void dump_south(device_t dev);
38 static void southbridge_init_common(struct device *dev);
39
40 #if CONFIG_EPIA_VT8237R_INIT
41                    /* Interrupts for  INT# A   B   C   D */
42 static const unsigned char pciIrqs[4]  = { 10, 11, 12, 0};
43
44             /* Interrupt Assignments for Pins   1   2   3   4  */
45 static const unsigned char sataPins[4] =     { 'A','B','C','D'};
46 static const unsigned char vgaPins[4] =      { 'A','B','C','D'};
47 static const unsigned char usbPins[4] =      { 'A','B','C','D'};
48 static const unsigned char enetPins[4] =     { 'A','B','C','D'};
49 static const unsigned char vt8237Pins[4] =   { 'A','B','C','D'};
50 static const unsigned char slotPins[4] =     { 'C','D','A','B'};
51 static const unsigned char riserPins[4] =    { 'D','C','B','A'};
52
53 static unsigned char *pin_to_irq(const unsigned char *pin)
54 {
55         static unsigned char Irqs[4];
56         int i;
57         for (i = 0 ; i < 4 ; i++)
58                 Irqs[i] = pciIrqs[ pin[i] - 'A' ];
59
60         return Irqs;
61 }
62 #endif
63
64 /** Set up PCI IRQ routing, route everything through APIC. */
65 static void pci_routing_fixup(struct device *dev)
66 {
67 #if CONFIG_EPIA_VT8237R_INIT
68         device_t pdev;
69         u8 reg;
70 #endif
71
72         /* PCI PNP Interrupt Routing INTE/F - disable */
73         pci_write_config8(dev, 0x44, 0x00);
74
75         /* PCI PNP Interrupt Routing INTG/H - disable */
76         pci_write_config8(dev, 0x45, 0x00);
77
78         /* Gate Interrupts until RAM Writes are flushed */
79         pci_write_config8(dev, 0x49, 0x20);
80
81 #if CONFIG_EPIA_VT8237R_INIT
82
83         /* Share INTE-INTH with INTA-INTD as per stock BIOS. */
84         pci_write_config8(dev, 0x46, 0x00);
85
86         /* setup PCI IRQ routing (For PCI Slot)*/
87         pci_write_config8(dev, 0x55, pciIrqs[0] << 4);
88         pci_write_config8(dev, 0x56, pciIrqs[1] | (pciIrqs[2] << 4) );
89         pci_write_config8(dev, 0x57, pciIrqs[3] << 4);
90
91         /* PCI Routing Fixup */
92
93         //Setup MiniPCI Slot
94         pci_assign_irqs(0, 0x14, pin_to_irq(slotPins));
95
96         // Via 2 slot riser card 2nd slot
97         pci_assign_irqs(0, 0x13, pin_to_irq(riserPins));
98
99         //Setup USB
100         pci_assign_irqs(0, 0x10, pin_to_irq(usbPins));
101
102         //Setup VT8237R Sound
103         pci_assign_irqs(0, 0x11, pin_to_irq(vt8237Pins));
104
105         //Setup Ethernet
106         pci_assign_irqs(0, 0x12, pin_to_irq(enetPins));
107
108         //Setup VGA
109         pci_assign_irqs(1, 0x00, pin_to_irq(vgaPins));
110
111         /* APIC Routing Fixup */
112
113         // Setup SATA
114         pdev = dev_find_device(PCI_VENDOR_ID_VIA,
115                                 PCI_DEVICE_ID_VIA_VT6420_SATA, 0);
116         pci_write_config8(pdev, PCI_INTERRUPT_PIN, 0x02);
117         pci_assign_irqs(0, 0x0f, pin_to_irq(sataPins));
118
119
120         // Setup PATA Override
121         pdev = dev_find_device(PCI_VENDOR_ID_VIA,
122                                 PCI_DEVICE_ID_VIA_82C586_1, 0);
123         pci_write_config8(pdev, PCI_INTERRUPT_PIN, 0x01);
124         pci_write_config8(pdev, PCI_INTERRUPT_LINE, 0xFF);
125
126 #else
127         /* Route INTE-INTH through registers above, no map to INTA-INTD. */
128         pci_write_config8(dev, 0x46, 0x10);
129
130         /* PCI Interrupt Polarity */
131         pci_write_config8(dev, 0x54, 0x00);
132
133         /* PCI INTA# Routing */
134         pci_write_config8(dev, 0x55, 0x00);
135
136         /* PCI INTB#/C# Routing */
137         pci_write_config8(dev, 0x56, 0x00);
138
139         /* PCI INTD# Routing */
140         pci_write_config8(dev, 0x57, 0x00);
141 #endif
142 }
143
144
145
146 /**
147  * Set up the power management capabilities directly into ACPI mode.
148  * This avoids having to handle any System Management Interrupts (SMIs).
149  */
150
151 extern u8 acpi_slp_type;
152
153
154 static void setup_pm(device_t dev)
155 {
156         u16 tmp;
157         /* Debounce LID and PWRBTN# Inputs for 16ms. */
158         pci_write_config8(dev, 0x80, 0x20);
159
160         /* Set ACPI base address to I/O VT8237R_ACPI_IO_BASE. */
161         pci_write_config16(dev, 0x88, VT8237R_ACPI_IO_BASE | 0x1);
162
163         /* Set ACPI to 9, must set IRQ 9 override to level! Set PSON gating. */
164         pci_write_config8(dev, 0x82, 0x40 | VT8237R_ACPI_IRQ);
165
166 #if CONFIG_EPIA_VT8237R_INIT
167         /* Primary interupt channel, define wake events 0=IRQ0 15=IRQ15 1=en. */
168         pci_write_config16(dev, 0x84, 0x3052);
169 #else
170         /* Primary interupt channel, define wake events 0=IRQ0 15=IRQ15 1=en. */
171         pci_write_config16(dev, 0x84, 0x30b2);
172
173 #endif
174         /* SMI output level to low, 7.5us throttle clock */
175         pci_write_config8(dev, 0x8d, 0x18);
176
177         /* GP Timer Control 1s */
178         pci_write_config8(dev, 0x93, 0x88);
179
180         /*
181          * 7 = SMBus clock from RTC 32.768KHz
182          * 5 = Internal PLL reset from susp disabled
183          * 2 = GPO2 is SUSA#
184          */
185         pci_write_config8(dev, 0x94, 0xa0);
186
187         /*
188          * 7 = stp to sust delay 1msec
189          * 6 = SUSST# Deasserted Before PWRGD for STD
190          * 5 = Keyboard/Mouse Swap
191          * 4 = PWRGOOD reset on VT8237A/S
192          * 3 = GPO26/GPO27 is GPO 
193          * 2 = Disable Alert on Lan
194          * 1 = SUSCLK/GPO4
195          * 0 = USB Wakeup
196          */
197
198 #if CONFIG_EPIA_VT8237R_INIT
199         pci_write_config8(dev, 0x95, 0xc2);
200 #else
201         pci_write_config8(dev, 0x95, 0xcc);
202 #endif
203
204         /* Disable GP3 timer. */
205         pci_write_config8(dev, 0x98, 0);
206
207         /* Enable ACPI accessm RTC signal gated with PSON. */
208         pci_write_config8(dev, 0x81, 0x84);
209
210         /* Clear status events. */
211         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x00);
212         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x20);
213         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x28);
214         outl(0xffffffff, VT8237R_ACPI_IO_BASE + 0x30);
215
216         /* Disable SCI on GPIO. */
217         outw(0x0, VT8237R_ACPI_IO_BASE + 0x22);
218
219         /* Disable SMI on GPIO. */
220         outw(0x0, VT8237R_ACPI_IO_BASE + 0x24);
221
222         /* Disable all global enable SMIs. */
223         outw(0x0, VT8237R_ACPI_IO_BASE + 0x2a);
224
225         /* All SMI off, both IDE buses ON, PSON rising edge. */
226         outw(0x0, VT8237R_ACPI_IO_BASE + 0x2c);
227
228         /* Primary activity SMI disable. */
229         outl(0x0, VT8237R_ACPI_IO_BASE + 0x34);
230
231         /* GP timer reload on none. */
232         outl(0x0, VT8237R_ACPI_IO_BASE + 0x38);
233
234         /* Disable extended IO traps. */
235         outb(0x0, VT8237R_ACPI_IO_BASE + 0x42);
236
237         /* SCI is generated for RTC/pwrBtn/slpBtn. */
238         tmp = inw(VT8237R_ACPI_IO_BASE + 0x04);
239 #if CONFIG_HAVE_ACPI_RESUME == 1
240         acpi_slp_type = ((tmp & (7 << 10)) >> 10) == 1 ? 3 : 0 ;
241         printk_debug("SLP_TYP type was %x %x\n", tmp, acpi_slp_type);
242 #endif
243         /* clear sleep */
244         tmp &= ~(7 << 10);
245         tmp |= 1;
246         outw(tmp, VT8237R_ACPI_IO_BASE + 0x04);
247 }
248
249 static void vt8237r_init(struct device *dev)
250 {
251         u8 enables, reg8;
252         
253 #if CONFIG_EPIA_VT8237R_INIT
254         printk_spew("Entering vt8237r_init, for EPIA.\n");
255         /*
256          * TODO: Looks like stock BIOS can do this but causes a hang
257          * Enable SATA LED, disable special CPU Frequency Change -
258          * GPIO28 GPIO22 GPIO29 GPIO23 are GPIOs.
259          * Setup to match EPIA default
260          * PCS0# on Pin U1
261          */
262         enables = pci_read_config8(dev, 0xe5);
263         enables |= 0x23;
264         pci_write_config8(dev, 0xe5, enables);
265         
266         /* 
267          * Enable Flash Write Access. 
268          * Note EPIA-N Does not use REQ5 or PCISTP#(Hang)
269          */
270         enables = pci_read_config8(dev, 0xe4);
271         enables |= 0x2B;
272         pci_write_config8(dev, 0xe4, enables);
273
274         /* Enables Extra RTC Ports */
275         enables = pci_read_config8(dev, 0x4E);
276         enables |= 0x80;
277         pci_write_config8(dev, 0x4E, enables);
278
279 #else 
280         printk_spew("Entering vt8237r_init.\n");
281         /*
282          * Enable SATA LED, disable special CPU Frequency Change -
283          * GPIO28 GPIO22 GPIO29 GPIO23 are GPIOs.
284          */
285         pci_write_config8(dev, 0xe5, 0x09);
286         
287         /* REQ5 as PCI request input - should be together with INTE-INTH. */
288         pci_write_config8(dev, 0xe4, 0x4);
289 #endif
290
291         /* Set bit 3 of 0x4f (use INIT# as CPU reset). */
292         enables = pci_read_config8(dev, 0x4f);
293         enables |= 0x08;
294         pci_write_config8(dev, 0x4f, enables);
295
296 #if CONFIG_EPIA_VT8237R_INIT
297         /*
298          * Set Read Pass Write Control Enable
299          */
300         pci_write_config8(dev, 0x48, 0x0c);
301 #else
302         /*
303          * Set Read Pass Write Control Enable
304          * (force A2 from APIC FSB to low).
305          */
306         pci_write_config8(dev, 0x48, 0x8c);
307 #endif
308
309         southbridge_init_common(dev);
310
311 #if !CONFIG_EPIA_VT8237R_INIT
312         /* FIXME: Intel needs more bit set for C2/C3. */
313
314         /*
315          * Allow SLP# signal to assert LDTSTOP_L.
316          * Will work for C3 and for FID/VID change.
317          */
318         outb(0x1, VT8237R_ACPI_IO_BASE + 0x11);
319 #endif
320
321         printk_spew("Leaving %s.\n", __func__);
322 }
323
324 static void vt8237s_init(struct device *dev)
325 {
326         u32 tmp;
327
328         /* Put SPI base VT8237S_SPI_MEM_BASE. */
329         tmp = pci_read_config32(dev, 0xbc);
330         pci_write_config32(dev, 0xbc,
331                            (VT8237S_SPI_MEM_BASE >> 8) | (tmp & 0xFF000000));
332
333         /*
334          * REQ5 as PCI request input - should be together with INTE-INTH. 
335          */
336         pci_write_config8(dev, 0xe4, 0x04);
337
338         /* Reduce further the STPCLK/LDTSTP signal to 5us. */
339         pci_write_config8(dev, 0xec, 0x4);
340
341         /* Host Bus Power Management Control, maybe not needed */
342         pci_write_config8(dev, 0x8c, 0x5);
343
344         /* Enable HPET at VT8237R_HPET_ADDR., does not work correctly on R. */
345         pci_write_config32(dev, 0x68, (VT8237R_HPET_ADDR | 0x80));
346
347         southbridge_init_common(dev);
348
349         /* FIXME: Intel needs more bit set for C2/C3. */
350
351         /*
352          * Allow SLP# signal to assert LDTSTOP_L.
353          * Will work for C3 and for FID/VID change. FIXME FIXME, pre rev A2.
354          */
355         outb(0xff, VT8237R_ACPI_IO_BASE + 0x50);
356
357         dump_south(dev);
358 }
359
360 static void vt8237_common_init(struct device *dev)
361 {
362         u8 enables, byte;
363
364         /* Enable addr/data stepping. */
365         byte = pci_read_config8(dev, PCI_COMMAND);
366         byte |= PCI_COMMAND_WAIT;
367         pci_write_config8(dev, PCI_COMMAND, byte);
368
369 /* EPIA-N(L) Uses CN400 for BIOS Access */
370 #if !CONFIG_EPIA_VT8237R_INIT
371         /* Enable the internal I/O decode. */
372         enables = pci_read_config8(dev, 0x6C);
373         enables |= 0x80;
374         pci_write_config8(dev, 0x6C, enables);
375
376         /*
377          * ROM decode
378          * bit range
379          *   7 000E0000h-000EFFFFh
380          *   6 FFF00000h-FFF7FFFFh
381          *   5 FFE80000h-FFEFFFFFh
382          *   4 FFE00000h-FFE7FFFFh
383          *   3 FFD80000h-FFDFFFFFh
384          *   2 FFD00000h-FFD7FFFFh
385          *   1 FFC80000h-FFCFFFFFh
386          *   0 FFC00000h-FFC7FFFFh
387          * So 0x7f here sets ROM decode to FFC00000-FFFFFFFF or 4Mbyte.
388          */
389         pci_write_config8(dev, 0x41, 0x7f);
390 #endif
391
392         /*
393          * Set bit 6 of 0x40 (I/O recovery time).
394          * IMPORTANT FIX - EISA = ECLR reg at 0x4d0! Decoding must be on so
395          * that PCI interrupts can be properly marked as level triggered.
396          */
397         enables = pci_read_config8(dev, 0x40);
398         enables |= 0x44;
399         pci_write_config8(dev, 0x40, enables);
400
401         /* Line buffer control */
402         enables = pci_read_config8(dev, 0x42);
403         enables |= 0xf8;
404         pci_write_config8(dev, 0x42, enables);
405
406         /* Delay transaction control */
407         pci_write_config8(dev, 0x43, 0xb);
408
409 #if CONFIG_EPIA_VT8237R_INIT
410         /* I/O recovery time, default IDE routing */
411         pci_write_config8(dev, 0x4c, 0x04);
412
413         /* ROM memory cycles go to LPC. */
414         pci_write_config8(dev, 0x59, 0x80);
415
416         /*
417          * Bit | Meaning
418          * -------------
419          *   3 | Bypass APIC De-Assert Message (1=Enable)
420          *   1 | possibly "INTE#, INTF#, INTG#, INTH# as PCI"
421          *     | bit 1=1 works for Aaron at VIA, bit 1=0 works for jakllsch
422          *   0 | Dynamic Clock Gating Main Switch (1=Enable)
423          */
424         pci_write_config8(dev, 0x5b, 0x9);
425
426         /* Set 0x58 to 0x42 APIC On and RTC Write Protect.*/
427         pci_write_config8(dev, 0x58, 0x42);
428
429         /* Enable serial IRQ, 6PCI clocks. */
430         pci_write_config8(dev, 0x52, 0x9);
431 #else
432         /* I/O recovery time, default IDE routing */
433         pci_write_config8(dev, 0x4c, 0x44);
434
435         /* ROM memory cycles go to LPC. */
436         pci_write_config8(dev, 0x59, 0x80);
437
438         /*
439          * Bit | Meaning
440          * -------------
441          *   3 | Bypass APIC De-Assert Message (1=Enable)
442          *   1 | possibly "INTE#, INTF#, INTG#, INTH# as PCI"
443          *     | bit 1=1 works for Aaron at VIA, bit 1=0 works for jakllsch
444          *   0 | Dynamic Clock Gating Main Switch (1=Enable)
445          */
446         pci_write_config8(dev, 0x5b, 0xb);
447
448         /* Set 0x58 to 0x43 APIC and RTC. */
449         pci_write_config8(dev, 0x58, 0x43);
450
451         /* Enable serial IRQ, 6PCI clocks. */
452         pci_write_config8(dev, 0x52, 0x9);
453
454 #endif
455
456         /* Power management setup */
457         setup_pm(dev);
458
459         /* Start the RTC. */
460         rtc_init(0);
461 }
462
463 static void vt8237r_read_resources(device_t dev)
464 {
465         struct resource *res;
466
467         pci_dev_read_resources(dev);
468
469         /* Fixed ACPI Base IO Base*/
470         res = new_resource(dev, 0x88);
471         res->base = VT8237R_ACPI_IO_BASE;
472         res->size = 128;
473         res->limit = 0xffffUL;
474         res->flags = IORESOURCE_IO | IORESOURCE_FIXED |
475                      IORESOURCE_STORED | IORESOURCE_ASSIGNED;
476
477         /* Fixed EISA ECLR I/O Regs     */
478         res = new_resource(dev, 3);
479         res->base = 0x4d0;
480         res->size = 2;
481         res->limit = 0xffffUL;
482         res->flags = IORESOURCE_IO | IORESOURCE_FIXED |
483                      IORESOURCE_STORED | IORESOURCE_ASSIGNED;
484
485         /* Fixed System Management Bus I/O Resource */
486         res = new_resource(dev, 0xD0);
487         res->base = VT8237R_SMBUS_IO_BASE;
488         res->size = 16;
489         res->limit = 0xffffUL;
490         res->flags = IORESOURCE_IO | IORESOURCE_FIXED |
491                      IORESOURCE_STORED | IORESOURCE_ASSIGNED;
492
493         /* Fixed APIC resource */
494         res = new_resource(dev, 0x44);
495         res->base = VT8237R_APIC_BASE;
496         res->size = 256;
497         res->limit = 0xffffffffUL;
498         res->align = 8;
499         res->gran = 8;
500         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
501                      IORESOURCE_STORED | IORESOURCE_ASSIGNED;
502
503         res = new_resource(dev, 1);
504         res->base = 0x0UL;
505         res->size = 0x1000UL;
506         res->limit = 0xffffUL;
507         res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
508 }
509
510 /**
511  * The VT8237R is not a PCI bridge and has no resources of its own (other
512  * than standard PC I/O addresses), however it does control the ISA bus
513  * and so we need to manually call enable childrens resources on that bus.
514  */
515 static void vt8237r_enable_resources(device_t dev)
516 {
517         pci_dev_enable_resources(dev);
518         enable_childrens_resources(dev);
519 }
520
521 static void init_keyboard(struct device *dev)
522 {
523         u8 regval = pci_read_config8(dev, 0x51);
524         if (regval & 0x1)
525                 init_pc_keyboard(0x60, 0x64, 0);
526 }
527
528 static void southbridge_init_common(struct device *dev)
529 {
530         vt8237_common_init(dev);
531         pci_routing_fixup(dev);
532         setup_ioapic(VT8237R_APIC_BASE, VT8237R_APIC_ID);
533         setup_i8259();
534         init_keyboard(dev);
535 }
536
537 static const struct device_operations vt8237r_lpc_ops_s = {
538         .read_resources         = vt8237r_read_resources,
539         .set_resources          = pci_dev_set_resources,
540         .enable_resources       = vt8237r_enable_resources,
541         .init                   = &vt8237s_init,
542         .scan_bus               = scan_static_bus,
543 };
544
545 static const struct device_operations vt8237r_lpc_ops_r = {
546         .read_resources         = vt8237r_read_resources,
547         .set_resources          = pci_dev_set_resources,
548         .enable_resources       = vt8237r_enable_resources,
549         .init                   = &vt8237r_init,
550         .scan_bus               = scan_static_bus,
551 };
552
553 static const struct pci_driver lpc_driver_r __pci_driver = {
554         .ops    = &vt8237r_lpc_ops_r,
555         .vendor = PCI_VENDOR_ID_VIA,
556         .device = PCI_DEVICE_ID_VIA_VT8237R_LPC,
557 };
558
559 static const struct pci_driver lpc_driver_s __pci_driver = {
560         .ops    = &vt8237r_lpc_ops_s,
561         .vendor = PCI_VENDOR_ID_VIA,
562         .device = PCI_DEVICE_ID_VIA_VT8237S_LPC,
563 };