2 * This file is part of the coreboot project.
4 * Copyright (C) 2007, 2008 Rudolf Marek <r.marek@assembler.cz>
5 * Copyright (C) 2009 Jon Harrison <bothlyn@blueyonder.co.uk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
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.
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
21 /* Inspiration from other VIA SB code. */
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>
32 #include <pc80/keyboard.h>
33 #include <pc80/i8259.h>
38 static void southbridge_init_common(struct device *dev);
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};
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'};
53 static unsigned char *pin_to_irq(const unsigned char *pin)
55 static unsigned char Irqs[4];
57 for (i = 0 ; i < 4 ; i++)
58 Irqs[i] = pciIrqs[ pin[i] - 'A' ];
64 /** Set up PCI IRQ routing, route everything through APIC. */
65 static void pci_routing_fixup(struct device *dev)
67 #if CONFIG_EPIA_VT8237R_INIT
71 /* PCI PNP Interrupt Routing INTE/F - disable */
72 pci_write_config8(dev, 0x44, 0x00);
74 /* PCI PNP Interrupt Routing INTG/H - disable */
75 pci_write_config8(dev, 0x45, 0x00);
77 /* Gate Interrupts until RAM Writes are flushed */
78 pci_write_config8(dev, 0x49, 0x20);
80 #if CONFIG_EPIA_VT8237R_INIT
82 /* Share INTE-INTH with INTA-INTD as per stock BIOS. */
83 pci_write_config8(dev, 0x46, 0x00);
85 /* setup PCI IRQ routing (For PCI Slot)*/
86 pci_write_config8(dev, 0x55, pciIrqs[0] << 4);
87 pci_write_config8(dev, 0x56, pciIrqs[1] | (pciIrqs[2] << 4) );
88 pci_write_config8(dev, 0x57, pciIrqs[3] << 4);
90 /* PCI Routing Fixup */
93 pci_assign_irqs(0, 0x14, pin_to_irq(slotPins));
95 // Via 2 slot riser card 2nd slot
96 pci_assign_irqs(0, 0x13, pin_to_irq(riserPins));
99 pci_assign_irqs(0, 0x10, pin_to_irq(usbPins));
101 //Setup VT8237R Sound
102 pci_assign_irqs(0, 0x11, pin_to_irq(vt8237Pins));
105 pci_assign_irqs(0, 0x12, pin_to_irq(enetPins));
108 pci_assign_irqs(1, 0x00, pin_to_irq(vgaPins));
110 /* APIC Routing Fixup */
113 pdev = dev_find_device(PCI_VENDOR_ID_VIA,
114 PCI_DEVICE_ID_VIA_VT6420_SATA, 0);
115 pci_write_config8(pdev, PCI_INTERRUPT_PIN, 0x02);
116 pci_assign_irqs(0, 0x0f, pin_to_irq(sataPins));
119 // Setup PATA Override
120 pdev = dev_find_device(PCI_VENDOR_ID_VIA,
121 PCI_DEVICE_ID_VIA_82C586_1, 0);
122 pci_write_config8(pdev, PCI_INTERRUPT_PIN, 0x01);
123 pci_write_config8(pdev, PCI_INTERRUPT_LINE, 0xFF);
126 /* Route INTE-INTH through registers above, no map to INTA-INTD. */
127 pci_write_config8(dev, 0x46, 0x10);
129 /* PCI Interrupt Polarity */
130 pci_write_config8(dev, 0x54, 0x00);
132 /* PCI INTA# Routing */
133 pci_write_config8(dev, 0x55, 0x00);
135 /* PCI INTB#/C# Routing */
136 pci_write_config8(dev, 0x56, 0x00);
138 /* PCI INTD# Routing */
139 pci_write_config8(dev, 0x57, 0x00);
146 * Set up the power management capabilities directly into ACPI mode.
147 * This avoids having to handle any System Management Interrupts (SMIs).
150 extern u8 acpi_slp_type;
153 static void setup_pm(device_t dev)
156 /* Debounce LID and PWRBTN# Inputs for 16ms. */
157 pci_write_config8(dev, 0x80, 0x20);
159 /* Set ACPI base address to I/O VT8237R_ACPI_IO_BASE. */
160 pci_write_config16(dev, 0x88, VT8237R_ACPI_IO_BASE | 0x1);
162 /* Set ACPI to 9, must set IRQ 9 override to level! Set PSON gating. */
163 pci_write_config8(dev, 0x82, 0x40 | VT8237R_ACPI_IRQ);
165 #if CONFIG_EPIA_VT8237R_INIT
166 /* Primary interupt channel, define wake events 0=IRQ0 15=IRQ15 1=en. */
167 pci_write_config16(dev, 0x84, 0x3052);
169 /* Primary interupt channel, define wake events 0=IRQ0 15=IRQ15 1=en. */
170 pci_write_config16(dev, 0x84, 0x30b2);
173 /* SMI output level to low, 7.5us throttle clock */
174 pci_write_config8(dev, 0x8d, 0x18);
176 /* GP Timer Control 1s */
177 pci_write_config8(dev, 0x93, 0x88);
180 * 7 = SMBus clock from RTC 32.768KHz
181 * 5 = Internal PLL reset from susp disabled
184 pci_write_config8(dev, 0x94, 0xa0);
187 * 7 = stp to sust delay 1msec
188 * 6 = SUSST# Deasserted Before PWRGD for STD
189 * 5 = Keyboard/Mouse Swap
190 * 4 = PWRGOOD reset on VT8237A/S
191 * 3 = GPO26/GPO27 is GPO
192 * 2 = Disable Alert on Lan
197 #if CONFIG_EPIA_VT8237R_INIT
198 pci_write_config8(dev, 0x95, 0xc2);
200 pci_write_config8(dev, 0x95, 0xcc);
203 /* Disable GP3 timer. */
204 pci_write_config8(dev, 0x98, 0);
206 /* Enable ACPI accessm RTC signal gated with PSON. */
207 pci_write_config8(dev, 0x81, 0x84);
209 /* Clear status events. */
210 outw(0xffff, VT8237R_ACPI_IO_BASE + 0x00);
211 outw(0xffff, VT8237R_ACPI_IO_BASE + 0x20);
212 outw(0xffff, VT8237R_ACPI_IO_BASE + 0x28);
213 outl(0xffffffff, VT8237R_ACPI_IO_BASE + 0x30);
215 /* Disable SCI on GPIO. */
216 outw(0x0, VT8237R_ACPI_IO_BASE + 0x22);
218 /* Disable SMI on GPIO. */
219 outw(0x0, VT8237R_ACPI_IO_BASE + 0x24);
221 /* Disable all global enable SMIs, except SW SMI */
222 outw(0x40, VT8237R_ACPI_IO_BASE + 0x2a);
224 /* Primary activity SMI disable. */
225 outl(0x0, VT8237R_ACPI_IO_BASE + 0x34);
227 /* GP timer reload on none. */
228 outl(0x0, VT8237R_ACPI_IO_BASE + 0x38);
230 /* Disable extended IO traps. */
231 outb(0x0, VT8237R_ACPI_IO_BASE + 0x42);
233 /* SCI is generated for RTC/pwrBtn/slpBtn. */
234 tmp = inw(VT8237R_ACPI_IO_BASE + 0x04);
235 #if CONFIG_HAVE_ACPI_RESUME == 1
236 acpi_slp_type = ((tmp & (7 << 10)) >> 10) == 1 ? 3 : 0 ;
237 printk(BIOS_DEBUG, "SLP_TYP type was %x %x\n", tmp, acpi_slp_type);
240 /* All SMI on, both IDE buses ON, PSON rising edge. */
241 outw(0x1, VT8237R_ACPI_IO_BASE + 0x2c);
246 outw(tmp, VT8237R_ACPI_IO_BASE + 0x04);
249 static void vt8237r_init(struct device *dev)
253 #if CONFIG_EPIA_VT8237R_INIT
254 printk(BIOS_SPEW, "Entering vt8237r_init, for EPIA.\n");
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
262 enables = pci_read_config8(dev, 0xe5);
264 pci_write_config8(dev, 0xe5, enables);
267 * Enable Flash Write Access.
268 * Note EPIA-N Does not use REQ5 or PCISTP#(Hang)
270 enables = pci_read_config8(dev, 0xe4);
272 pci_write_config8(dev, 0xe4, enables);
274 /* Enables Extra RTC Ports */
275 enables = pci_read_config8(dev, 0x4E);
277 pci_write_config8(dev, 0x4E, enables);
280 printk(BIOS_SPEW, "Entering vt8237r_init.\n");
282 * Enable SATA LED, disable special CPU Frequency Change -
283 * GPIO28 GPIO22 GPIO29 GPIO23 are GPIOs.
285 pci_write_config8(dev, 0xe5, 0x09);
287 /* REQ5 as PCI request input - should be together with INTE-INTH. */
288 pci_write_config8(dev, 0xe4, 0x4);
291 /* Set bit 3 of 0x4f (use INIT# as CPU reset). */
292 enables = pci_read_config8(dev, 0x4f);
294 pci_write_config8(dev, 0x4f, enables);
296 #if CONFIG_EPIA_VT8237R_INIT
298 * Set Read Pass Write Control Enable
300 pci_write_config8(dev, 0x48, 0x0c);
303 * Set Read Pass Write Control Enable
304 * (force A2 from APIC FSB to low).
306 pci_write_config8(dev, 0x48, 0x8c);
309 southbridge_init_common(dev);
311 #if !CONFIG_EPIA_VT8237R_INIT
312 /* FIXME: Intel needs more bit set for C2/C3. */
315 * Allow SLP# signal to assert LDTSTOP_L.
316 * Will work for C3 and for FID/VID change.
318 outb(0x1, VT8237R_ACPI_IO_BASE + 0x11);
321 printk(BIOS_SPEW, "Leaving %s.\n", __func__);
324 static void vt8237a_init(struct device *dev)
327 * FIXME: This is based on vt8237s_init() and the values the AMI
328 * BIOS on my M2V wrote to these registers (by loking
329 * at lspci -nxxx output).
334 /* Set bit 3 of 0x4f (use INIT# as CPU reset). */
335 tmp = pci_read_config8(dev, 0x4f);
337 pci_write_config8(dev, 0x4f, tmp);
340 * bit2: REQ5 as PCI request input - should be together with INTE-INTH.
341 * bit5: usb power control lines as gpio
343 pci_write_config8(dev, 0xe4, 0x24);
345 * Enable APIC wakeup from INTH
346 * Enable SATA LED, disable special CPU Frequency Change -
347 * GPIO28 GPIO22 GPIO29 GPIO23 are GPIOs.
349 pci_write_config8(dev, 0xe5, 0x69);
351 /* Reduce further the STPCLK/LDTSTP signal to 5us. */
352 pci_write_config8(dev, 0xec, 0x4);
354 /* Host Bus Power Management Control, maybe not needed */
355 pci_write_config8(dev, 0x8c, 0x5);
357 /* Enable HPET at VT8237R_HPET_ADDR. */
358 pci_write_config32(dev, 0x68, (VT8237R_HPET_ADDR | 0x80));
360 southbridge_init_common(dev);
362 /* Share INTE-INTH with INTA-INTD for simplicity */
363 pci_write_config8(dev, 0x46, 0x00);
365 /* FIXME: Intel needs more bit set for C2/C3. */
368 * Allow SLP# signal to assert LDTSTOP_L.
369 * Will work for C3 and for FID/VID change.
371 outb(0x1, VT8237R_ACPI_IO_BASE + 0x11);
376 static void vt8237s_init(struct device *dev)
380 /* Put SPI base VT8237S_SPI_MEM_BASE. */
381 tmp = pci_read_config32(dev, 0xbc);
382 pci_write_config32(dev, 0xbc,
383 (VT8237S_SPI_MEM_BASE >> 8) | (tmp & 0xFF000000));
386 * REQ5 as PCI request input - should be together with INTE-INTH.
388 pci_write_config8(dev, 0xe4, 0x04);
390 /* Reduce further the STPCLK/LDTSTP signal to 5us. */
391 pci_write_config8(dev, 0xec, 0x4);
393 /* Host Bus Power Management Control, maybe not needed */
394 pci_write_config8(dev, 0x8c, 0x5);
396 /* Enable HPET at VT8237R_HPET_ADDR., does not work correctly on R. */
397 pci_write_config32(dev, 0x68, (VT8237R_HPET_ADDR | 0x80));
399 southbridge_init_common(dev);
401 /* FIXME: Intel needs more bit set for C2/C3. */
404 * Allow SLP# signal to assert LDTSTOP_L.
405 * Will work for C3 and for FID/VID change. FIXME FIXME, pre rev A2.
407 outb(0xff, VT8237R_ACPI_IO_BASE + 0x50);
412 static void vt8237_common_init(struct device *dev)
416 /* Enable addr/data stepping. */
417 byte = pci_read_config8(dev, PCI_COMMAND);
418 byte |= PCI_COMMAND_WAIT;
419 pci_write_config8(dev, PCI_COMMAND, byte);
421 /* EPIA-N(L) Uses CN400 for BIOS Access */
422 #if !CONFIG_EPIA_VT8237R_INIT
423 /* Enable the internal I/O decode. */
424 enables = pci_read_config8(dev, 0x6C);
426 pci_write_config8(dev, 0x6C, enables);
431 * 7 000E0000h-000EFFFFh
432 * 6 FFF00000h-FFF7FFFFh
433 * 5 FFE80000h-FFEFFFFFh
434 * 4 FFE00000h-FFE7FFFFh
435 * 3 FFD80000h-FFDFFFFFh
436 * 2 FFD00000h-FFD7FFFFh
437 * 1 FFC80000h-FFCFFFFFh
438 * 0 FFC00000h-FFC7FFFFh
439 * So 0x7f here sets ROM decode to FFC00000-FFFFFFFF or 4Mbyte.
441 pci_write_config8(dev, 0x41, 0x7f);
445 * Set bit 6 of 0x40 (I/O recovery time).
446 * IMPORTANT FIX - EISA = ECLR reg at 0x4d0! Decoding must be on so
447 * that PCI interrupts can be properly marked as level triggered.
449 enables = pci_read_config8(dev, 0x40);
451 pci_write_config8(dev, 0x40, enables);
453 /* Line buffer control */
454 enables = pci_read_config8(dev, 0x42);
456 pci_write_config8(dev, 0x42, enables);
458 /* Delay transaction control */
459 pci_write_config8(dev, 0x43, 0xb);
461 #if CONFIG_EPIA_VT8237R_INIT
462 /* I/O recovery time, default IDE routing */
463 pci_write_config8(dev, 0x4c, 0x04);
465 /* ROM memory cycles go to LPC. */
466 pci_write_config8(dev, 0x59, 0x80);
471 * 3 | Bypass APIC De-Assert Message (1=Enable)
472 * 1 | possibly "INTE#, INTF#, INTG#, INTH# as PCI"
473 * | bit 1=1 works for Aaron at VIA, bit 1=0 works for jakllsch
474 * 0 | Dynamic Clock Gating Main Switch (1=Enable)
476 pci_write_config8(dev, 0x5b, 0x9);
478 /* Set 0x58 to 0x42 APIC On and RTC Write Protect.*/
479 pci_write_config8(dev, 0x58, 0x42);
481 /* Enable serial IRQ, 6PCI clocks. */
482 pci_write_config8(dev, 0x52, 0x9);
484 /* I/O recovery time, default IDE routing */
485 pci_write_config8(dev, 0x4c, 0x44);
487 /* ROM memory cycles go to LPC. */
488 pci_write_config8(dev, 0x59, 0x80);
493 * 3 | Bypass APIC De-Assert Message (1=Enable)
494 * 1 | possibly "INTE#, INTF#, INTG#, INTH# as PCI"
495 * | bit 1=1 works for Aaron at VIA, bit 1=0 works for jakllsch
496 * 0 | Dynamic Clock Gating Main Switch (1=Enable)
498 pci_write_config8(dev, 0x5b, 0xb);
500 /* Set 0x58 to 0x43 APIC and RTC. */
501 pci_write_config8(dev, 0x58, 0x43);
503 /* Enable serial IRQ, 6PCI clocks. */
504 pci_write_config8(dev, 0x52, 0x9);
506 #if CONFIG_HAVE_SMI_HANDLER
510 /* Power management setup */
517 static void vt8237r_read_resources(device_t dev)
519 struct resource *res;
521 pci_dev_read_resources(dev);
523 /* Fixed ACPI Base IO Base*/
524 res = new_resource(dev, 0x88);
525 res->base = VT8237R_ACPI_IO_BASE;
527 res->limit = 0xffffUL;
528 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_RESERVE |
529 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
531 /* Fixed EISA ECLR I/O Regs */
532 res = new_resource(dev, 3);
535 res->limit = 0xffffUL;
536 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_RESERVE |
537 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
539 /* Fixed System Management Bus I/O Resource */
540 res = new_resource(dev, 0xD0);
541 res->base = VT8237R_SMBUS_IO_BASE;
543 res->limit = 0xffffUL;
544 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_RESERVE |
545 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
547 /* Fixed APIC resource */
548 res = new_resource(dev, 0x44);
549 res->base = IO_APIC_ADDR;
551 res->limit = 0xffffffffUL;
554 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_RESERVE |
555 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
557 /* Fixed flashrom resource */
558 res = new_resource(dev, 4);
559 res->base = 0xff000000UL;
560 res->size = 0x01000000UL; /* 16MB */
561 res->limit = 0xffffffffUL;
562 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_RESERVE |
563 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
565 res = new_resource(dev, 1);
567 res->size = 0x1000UL;
568 res->limit = 0xffffUL;
569 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
572 static void init_keyboard(struct device *dev)
574 u8 regval = pci_read_config8(dev, 0x51);
579 static void southbridge_init_common(struct device *dev)
581 vt8237_common_init(dev);
582 pci_routing_fixup(dev);
583 setup_ioapic(IO_APIC_ADDR, VT8237R_APIC_ID);
588 static const struct device_operations vt8237r_lpc_ops_s = {
589 .read_resources = vt8237r_read_resources,
590 .set_resources = pci_dev_set_resources,
591 .enable_resources = pci_dev_enable_resources,
592 .init = vt8237s_init,
593 .scan_bus = scan_static_bus,
596 static const struct device_operations vt8237r_lpc_ops_r = {
597 .read_resources = vt8237r_read_resources,
598 .set_resources = pci_dev_set_resources,
599 .enable_resources = pci_dev_enable_resources,
600 .init = vt8237r_init,
601 .scan_bus = scan_static_bus,
604 static const struct device_operations vt8237r_lpc_ops_a = {
605 .read_resources = vt8237r_read_resources,
606 .set_resources = pci_dev_set_resources,
607 .enable_resources = pci_dev_enable_resources,
608 .init = vt8237a_init,
609 .scan_bus = scan_static_bus,
612 static const struct pci_driver lpc_driver_r __pci_driver = {
613 .ops = &vt8237r_lpc_ops_r,
614 .vendor = PCI_VENDOR_ID_VIA,
615 .device = PCI_DEVICE_ID_VIA_VT8237R_LPC,
618 static const struct pci_driver lpc_driver_a __pci_driver = {
619 .ops = &vt8237r_lpc_ops_a,
620 .vendor = PCI_VENDOR_ID_VIA,
621 .device = PCI_DEVICE_ID_VIA_VT8237A_LPC,
624 static const struct pci_driver lpc_driver_s __pci_driver = {
625 .ops = &vt8237r_lpc_ops_s,
626 .vendor = PCI_VENDOR_ID_VIA,
627 .device = PCI_DEVICE_ID_VIA_VT8237S_LPC,