Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2007-2009 coresystems GmbH |
| 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 as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <arch/io.h> |
| 21 | #include <console/console.h> |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 22 | #include <device/device.h> |
| 23 | #include <device/pci.h> |
| 24 | #include <device/pci_ops.h> |
| 25 | #include <device/pci_ids.h> |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 26 | #include <pc80/mc146818rtc.h> |
| 27 | #include <pc80/i8259.h> |
| 28 | #include <pc80/keyboard.h> |
| 29 | #include <pc80/isa-dma.h> |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 30 | #include <cpu/x86/lapic.h> |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 31 | #include <arch/ioapic.h> |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | |
| 34 | #define ACPI_IO_BASE 0x400 |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 35 | |
| 36 | static const unsigned char pci_irqs[4] = { 11, 11, 10, 10 }; |
| 37 | |
| 38 | static const unsigned char usb_pins[4] = { 'A', 'B', 'C', 'D' }; |
| 39 | static const unsigned char vga_pins[4] = { 'A', 'B', 'C', 'D' }; |
| 40 | static const unsigned char slot_pins[4] = { 'B', 'C', 'D', 'A' }; |
| 41 | static const unsigned char ac97_pins[4] = { 'B', 'C', 'D', 'A' }; |
| 42 | |
| 43 | static unsigned char *pin_to_irq(const unsigned char *pin) |
| 44 | { |
| 45 | static unsigned char irqs[4]; |
| 46 | int i; |
| 47 | for (i = 0; i < 4; i++) |
| 48 | irqs[i] = pci_irqs[pin[i] - 'A']; |
| 49 | |
| 50 | return irqs; |
| 51 | } |
| 52 | |
| 53 | static void pci_routing_fixup(struct device *dev) |
| 54 | { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 55 | printk(BIOS_DEBUG, "%s: device is %p\n", __FUNCTION__, dev); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 56 | |
| 57 | /* set up PCI IRQ routing */ |
| 58 | pci_write_config8(dev, 0x55, pci_irqs[0] << 4); |
| 59 | pci_write_config8(dev, 0x56, pci_irqs[1] | (pci_irqs[2] << 4)); |
| 60 | pci_write_config8(dev, 0x57, pci_irqs[3] << 4); |
| 61 | |
| 62 | /* Assigning IRQs */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 63 | printk(BIOS_DEBUG, "Setting up USB interrupts.\n"); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 64 | pci_assign_irqs(0, 0x10, pin_to_irq(usb_pins)); |
| 65 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 66 | printk(BIOS_DEBUG, "Setting up VGA interrupts.\n"); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 67 | pci_assign_irqs(1, 0x00, pin_to_irq(vga_pins)); |
| 68 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 69 | printk(BIOS_DEBUG, "Setting up PCI slot interrupts.\n"); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 70 | pci_assign_irqs(2, 0x04, pin_to_irq(slot_pins)); |
| 71 | // more? |
| 72 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 73 | printk(BIOS_DEBUG, "Setting up AC97 interrupts.\n"); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 74 | pci_assign_irqs(0x80, 0x1, pin_to_irq(ac97_pins)); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Set up the power management capabilities directly into ACPI mode. This |
| 79 | * avoids having to handle any System Management Interrupts (SMI's) which I |
| 80 | * can't figure out how to do !!!! |
| 81 | */ |
| 82 | |
Stefan Reinauer | c65666f | 2010-04-03 12:41:41 +0000 | [diff] [blame] | 83 | static void setup_pm(device_t dev) |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 84 | { |
| 85 | /* Debounce LID and PWRBTN# Inputs for 16ms. */ |
| 86 | pci_write_config8(dev, 0x80, 0x20); |
| 87 | |
| 88 | /* Set ACPI base address to IO ACPI_IO_BASE */ |
| 89 | pci_write_config16(dev, 0x88, ACPI_IO_BASE | 1); |
| 90 | |
| 91 | /* set ACPI irq to 9 */ |
| 92 | pci_write_config8(dev, 0x82, 0x49); |
| 93 | |
| 94 | /* Primary interupt channel, define wake events 0=IRQ0 15=IRQ15 1=en. */ |
| 95 | pci_write_config16(dev, 0x84, 0x609a); |
| 96 | |
| 97 | /* SMI output level to low, 7.5us throttle clock */ |
| 98 | pci_write_config8(dev, 0x8d, 0x18); |
| 99 | |
| 100 | /* GP Timer Control 1s */ |
| 101 | pci_write_config8(dev, 0x93, 0x88); |
| 102 | |
| 103 | /* Power Well */ |
| 104 | pci_write_config8(dev, 0x94, 0x20); // 0x20?? |
| 105 | |
| 106 | /* 7 = stp to sust delay 1msec |
| 107 | * 6 = SUSST# Deasserted Before PWRGD for STD |
| 108 | */ |
| 109 | pci_write_config8(dev, 0x95, 0xc0); // 0xc1?? |
| 110 | |
| 111 | /* Disable GP2 & GP3 Timer */ |
| 112 | pci_write_config8(dev, 0x98, 0); |
| 113 | |
| 114 | /* GP2 Timer Counter */ |
| 115 | pci_write_config8(dev, 0x99, 0xfb); |
| 116 | /* GP3 Timer Counter */ |
| 117 | //pci_write_config8(dev, 0x9a, 0x20); |
| 118 | |
| 119 | /* Multi Function Select 1 */ |
| 120 | pci_write_config8(dev, 0xe4, 0x00); |
| 121 | |
| 122 | /* Multi Function Select 2 */ |
| 123 | pci_write_config8(dev, 0xe5, 0x41); //?? |
| 124 | |
| 125 | /* Enable ACPI access (and setup like award) */ |
| 126 | pci_write_config8(dev, 0x81, 0x84); |
| 127 | |
| 128 | /* Clear status events. */ |
| 129 | outw(0xffff, ACPI_IO_BASE + 0x00); |
| 130 | outw(0xffff, ACPI_IO_BASE + 0x20); |
| 131 | outw(0xffff, ACPI_IO_BASE + 0x28); |
| 132 | outl(0xffffffff, ACPI_IO_BASE + 0x30); |
| 133 | |
| 134 | /* Disable SCI on GPIO. */ |
| 135 | outw(0x0, ACPI_IO_BASE + 0x22); |
| 136 | |
| 137 | /* Disable SMI on GPIO. */ |
| 138 | outw(0x0, ACPI_IO_BASE + 0x24); |
| 139 | |
| 140 | /* Disable all global enable SMIs. */ |
| 141 | outw(0x0, ACPI_IO_BASE + 0x2a); |
| 142 | |
| 143 | /* All SMI off, both IDE buses ON, PSON rising edge. */ |
| 144 | outw(0x0, ACPI_IO_BASE + 0x2c); |
| 145 | |
| 146 | /* Primary activity SMI disable. */ |
| 147 | outl(0x0, ACPI_IO_BASE + 0x34); |
| 148 | |
| 149 | /* GP timer reload on none. */ |
| 150 | outl(0x0, ACPI_IO_BASE + 0x38); |
| 151 | |
| 152 | /* Disable extended IO traps. */ |
| 153 | outb(0x0, ACPI_IO_BASE + 0x42); |
| 154 | |
| 155 | /* SCI is generated for RTC/pwrBtn/slpBtn. */ |
| 156 | outw(0x0001, ACPI_IO_BASE + 0x04); |
| 157 | |
| 158 | /* Allow SLP# signal to assert LDTSTOP_L. |
| 159 | * Will work for C3 and for FID/VID change. |
| 160 | */ |
| 161 | outb(0x1, ACPI_IO_BASE + 0x11); |
| 162 | } |
| 163 | |
| 164 | static void cx700_set_lpc_registers(struct device *dev) |
| 165 | { |
| 166 | unsigned char enables; |
| 167 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 168 | printk(BIOS_DEBUG, "VIA CX700 LPC bridge init\n"); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 169 | |
| 170 | // enable the internal I/O decode |
| 171 | enables = pci_read_config8(dev, 0x6C); |
| 172 | enables |= 0x80; |
| 173 | pci_write_config8(dev, 0x6C, enables); |
| 174 | |
| 175 | // Map 4MB of FLASH into the address space |
| 176 | // pci_write_config8(dev, 0x41, 0x7f); |
| 177 | |
| 178 | // Set bit 6 of 0x40, because Award does it (IO recovery time) |
| 179 | // IMPORTANT FIX - EISA 0x4d0 decoding must be on so that PCI |
| 180 | // interrupts can be properly marked as level triggered. |
| 181 | enables = pci_read_config8(dev, 0x40); |
| 182 | enables |= 0x44; |
| 183 | pci_write_config8(dev, 0x40, enables); |
| 184 | |
| 185 | /* DMA Line buffer control */ |
| 186 | enables = pci_read_config8(dev, 0x42); |
| 187 | enables |= 0xf0; |
| 188 | pci_write_config8(dev, 0x42, enables); |
| 189 | |
| 190 | /* I/O recovery time */ |
| 191 | pci_write_config8(dev, 0x4c, 0x44); |
| 192 | |
| 193 | /* ROM memory cycles go to LPC. */ |
| 194 | pci_write_config8(dev, 0x59, 0x80); |
| 195 | |
| 196 | /* Enable SM dynamic clock gating */ |
| 197 | pci_write_config8(dev, 0x5b, 0x01); |
| 198 | |
| 199 | /* Set Read Pass Write Control Enable */ |
| 200 | pci_write_config8(dev, 0x48, 0x0c); |
| 201 | |
| 202 | /* Set SM Misc Control: Enable Internal APIC . */ |
| 203 | enables = pci_read_config8(dev, 0x58); |
| 204 | enables |= 1 << 6; |
| 205 | pci_write_config8(dev, 0x58, enables); |
| 206 | enables = pci_read_config8(dev, 0x4d); |
| 207 | enables |= 1 << 3; |
| 208 | pci_write_config8(dev, 0x4d, enables); |
| 209 | |
| 210 | /* Set bit 3 of 0x4f to match award (use INIT# as cpu reset) */ |
| 211 | enables = pci_read_config8(dev, 0x4f); |
| 212 | enables |= 0x08; |
| 213 | pci_write_config8(dev, 0x4f, enables); |
| 214 | |
| 215 | /* enable KBC configuration */ |
| 216 | pci_write_config8(dev, 0x51, 0x1f); |
| 217 | |
| 218 | /* enable serial irq */ |
| 219 | pci_write_config8(dev, 0x52, 0x9); |
| 220 | |
| 221 | /* dma */ |
| 222 | pci_write_config8(dev, 0x53, 0x00); |
| 223 | |
| 224 | // Power management setup |
| 225 | setup_pm(dev); |
| 226 | |
| 227 | /* set up isa bus -- i/o recovery time, rom write enable, extend-ale */ |
| 228 | pci_write_config8(dev, 0x40, 0x54); |
| 229 | |
| 230 | /* Enable HPET timer */ |
Patrick Georgi | 9aeb694 | 2012-10-05 21:54:38 +0200 | [diff] [blame] | 231 | pci_write_config32(dev, 0x68, (1 << 31) | (CONFIG_HPET_ADDRESS >> 8)); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 232 | |
| 233 | } |
| 234 | |
Stefan Reinauer | c65666f | 2010-04-03 12:41:41 +0000 | [diff] [blame] | 235 | static void cx700_read_resources(device_t dev) |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 236 | { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 237 | struct resource *res; |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 238 | |
| 239 | /* Make sure we call our childrens set/enable functions - these |
| 240 | * are not called unless this device has a resource to set. |
| 241 | */ |
| 242 | |
| 243 | pci_dev_read_resources(dev); |
| 244 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 245 | res = new_resource(dev, 1); |
| 246 | res->base = 0x0UL; |
| 247 | res->size = 0x400UL; |
| 248 | res->limit = 0xffffUL; |
| 249 | res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 250 | |
| 251 | res = new_resource(dev, 3); /* IOAPIC */ |
Uwe Hermann | 74d1a6e | 2010-10-12 17:34:08 +0000 | [diff] [blame] | 252 | res->base = IO_APIC_ADDR; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 253 | res->size = 0x00001000; |
| 254 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Stefan Reinauer | c65666f | 2010-04-03 12:41:41 +0000 | [diff] [blame] | 257 | static void cx700_set_resources(device_t dev) |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 258 | { |
| 259 | struct resource *resource; |
| 260 | resource = find_resource(dev, 1); |
| 261 | resource->flags |= IORESOURCE_STORED; |
| 262 | pci_dev_set_resources(dev); |
| 263 | } |
| 264 | |
Stefan Reinauer | c65666f | 2010-04-03 12:41:41 +0000 | [diff] [blame] | 265 | static void cx700_enable_resources(device_t dev) |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 266 | { |
| 267 | /* Enable SuperIO decoding */ |
| 268 | pci_dev_enable_resources(dev); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static void cx700_lpc_init(struct device *dev) |
| 272 | { |
| 273 | cx700_set_lpc_registers(dev); |
| 274 | |
Myles Watson | ec0ee64 | 2009-10-19 16:21:30 +0000 | [diff] [blame] | 275 | #if CONFIG_IOAPIC |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 276 | #define IO_APIC_ID 2 |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 277 | setup_ioapic(VIO_APIC_VADDR, IO_APIC_ID); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 278 | #endif |
| 279 | |
| 280 | /* Initialize interrupts */ |
| 281 | pci_routing_fixup(dev); |
| 282 | /* make sure interupt controller is configured before keyboard init */ |
| 283 | setup_i8259(); |
| 284 | |
| 285 | /* Start the Real Time Clock */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 286 | cmos_init(0); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 287 | |
| 288 | /* Initialize isa dma */ |
| 289 | isa_dma_init(); |
| 290 | |
| 291 | /* Initialize keyboard controller */ |
Edward O'Callaghan | def00be | 2014-04-30 05:01:52 +1000 | [diff] [blame] | 292 | pc_keyboard_init(); |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static struct device_operations cx700_lpc_ops = { |
| 296 | .read_resources = cx700_read_resources, |
| 297 | .set_resources = cx700_set_resources, |
| 298 | .enable_resources = cx700_enable_resources, |
Edward O'Callaghan | e408dce | 2014-10-31 08:54:41 +1100 | [diff] [blame] | 299 | .init = cx700_lpc_init, |
Kyösti Mälkki | d0e212c | 2015-02-26 20:47:47 +0200 | [diff] [blame] | 300 | .scan_bus = scan_lpc_bus, |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | static const struct pci_driver lpc_driver __pci_driver = { |
Edward O'Callaghan | e408dce | 2014-10-31 08:54:41 +1100 | [diff] [blame] | 304 | .ops = &cx700_lpc_ops, |
Stefan Reinauer | aeba92a | 2009-04-17 08:37:18 +0000 | [diff] [blame] | 305 | .vendor = PCI_VENDOR_ID_VIA, |
| 306 | .device = 0x8324, |
| 307 | }; |