2 * This file is part of the coreboot project.
4 * Copyright (C) 2007-2009 coresystems GmbH
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.
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.
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
21 #include <console/console.h>
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <device/pci_ops.h>
26 #include <device/pci_ids.h>
28 #include <pc80/mc146818rtc.h>
29 #include <pc80/i8259.h>
30 #include <pc80/keyboard.h>
31 #include <pc80/isa-dma.h>
33 #include <cpu/x86/lapic.h>
36 #define ACPI_IO_BASE 0x400
37 #define HPET_ADDR 0xfe800000UL
38 #define IOAPIC_ADDR 0xfec00000ULL
43 unsigned int value_low, value_high;
46 static struct ioapicreg ioapicregvalues[] = {
47 #define ALL (0xff << 24)
49 #define DISABLED (1 << 16)
50 #define ENABLED (0 << 16)
51 #define TRIGGER_EDGE (0 << 15)
52 #define TRIGGER_LEVEL (1 << 15)
53 #define POLARITY_HIGH (0 << 13)
54 #define POLARITY_LOW (1 << 13)
55 #define PHYSICAL_DEST (0 << 11)
56 #define LOGICAL_DEST (1 << 11)
57 #define ExtINT (7 << 8)
61 /* IO-APIC virtual wire mode configuration */
62 /* mask, trigger, polarity, destination, delivery, vector */
63 { 0, ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT, NONE},
89 static void setup_ioapic(void)
92 unsigned long value_low, value_high, val;
93 unsigned long ioapic_base = IOAPIC_ADDR;
94 volatile unsigned long *l;
95 struct ioapicreg *a = ioapicregvalues;
96 unsigned long bsp_lapicid = lapicid();
98 l = (unsigned long *)ioapic_base;
103 l[4] = (val & 0xF0FFFF) | (2 << 24); // 2 == ID as programmed elsewhere. should be a define? XXX
105 /* Set APIC to FSB message bus. */
108 l[4] = (val & 0xFFFFFE) | 1;
110 ioapicregvalues[0].value_high = bsp_lapicid << (56 - 32);
112 printk_debug("IOAPIC: Bootstrap Processor Local APIC ID = %02x\n", bsp_lapicid);
114 for (i = 0; i < ARRAY_SIZE(ioapicregvalues); i++, a++) {
115 l[0] = (a->reg * 2) + 0x10;
118 l[0] = (a->reg * 2) + 0x11;
119 l[4] = a->value_high;
121 if ((i == 0) && (value_low == 0xffffffff)) {
122 printk_warning("IOAPIC is not responding.\n");
125 printk_debug("IOAPIC: IRQ reg 0x%08x value 0x%08x 0x%08x\n",
126 a->reg, a->value_low, a->value_high);
131 static const unsigned char pci_irqs[4] = { 11, 11, 10, 10 };
133 static const unsigned char usb_pins[4] = { 'A', 'B', 'C', 'D' };
134 static const unsigned char vga_pins[4] = { 'A', 'B', 'C', 'D' };
135 static const unsigned char slot_pins[4] = { 'B', 'C', 'D', 'A' };
136 static const unsigned char ac97_pins[4] = { 'B', 'C', 'D', 'A' };
138 static unsigned char *pin_to_irq(const unsigned char *pin)
140 static unsigned char irqs[4];
142 for (i = 0; i < 4; i++)
143 irqs[i] = pci_irqs[pin[i] - 'A'];
148 static void pci_routing_fixup(struct device *dev)
150 printk_debug("%s: device is %p\n", __FUNCTION__, dev);
152 /* set up PCI IRQ routing */
153 pci_write_config8(dev, 0x55, pci_irqs[0] << 4);
154 pci_write_config8(dev, 0x56, pci_irqs[1] | (pci_irqs[2] << 4));
155 pci_write_config8(dev, 0x57, pci_irqs[3] << 4);
158 printk_debug("Setting up USB interrupts.\n");
159 pci_assign_irqs(0, 0x10, pin_to_irq(usb_pins));
161 printk_debug("Setting up VGA interrupts.\n");
162 pci_assign_irqs(1, 0x00, pin_to_irq(vga_pins));
164 printk_debug("Setting up PCI slot interrupts.\n");
165 pci_assign_irqs(2, 0x04, pin_to_irq(slot_pins));
168 printk_debug("Setting up AC97 interrupts.\n");
169 pci_assign_irqs(0x80, 0x1, pin_to_irq(ac97_pins));
173 * Set up the power management capabilities directly into ACPI mode. This
174 * avoids having to handle any System Management Interrupts (SMI's) which I
175 * can't figure out how to do !!!!
178 void setup_pm(device_t dev)
180 /* Debounce LID and PWRBTN# Inputs for 16ms. */
181 pci_write_config8(dev, 0x80, 0x20);
183 /* Set ACPI base address to IO ACPI_IO_BASE */
184 pci_write_config16(dev, 0x88, ACPI_IO_BASE | 1);
186 /* set ACPI irq to 9 */
187 pci_write_config8(dev, 0x82, 0x49);
189 /* Primary interupt channel, define wake events 0=IRQ0 15=IRQ15 1=en. */
190 pci_write_config16(dev, 0x84, 0x609a);
192 /* SMI output level to low, 7.5us throttle clock */
193 pci_write_config8(dev, 0x8d, 0x18);
195 /* GP Timer Control 1s */
196 pci_write_config8(dev, 0x93, 0x88);
199 pci_write_config8(dev, 0x94, 0x20); // 0x20??
201 /* 7 = stp to sust delay 1msec
202 * 6 = SUSST# Deasserted Before PWRGD for STD
204 pci_write_config8(dev, 0x95, 0xc0); // 0xc1??
206 /* Disable GP2 & GP3 Timer */
207 pci_write_config8(dev, 0x98, 0);
209 /* GP2 Timer Counter */
210 pci_write_config8(dev, 0x99, 0xfb);
211 /* GP3 Timer Counter */
212 //pci_write_config8(dev, 0x9a, 0x20);
214 /* Multi Function Select 1 */
215 pci_write_config8(dev, 0xe4, 0x00);
217 /* Multi Function Select 2 */
218 pci_write_config8(dev, 0xe5, 0x41); //??
220 /* Enable ACPI access (and setup like award) */
221 pci_write_config8(dev, 0x81, 0x84);
223 /* Clear status events. */
224 outw(0xffff, ACPI_IO_BASE + 0x00);
225 outw(0xffff, ACPI_IO_BASE + 0x20);
226 outw(0xffff, ACPI_IO_BASE + 0x28);
227 outl(0xffffffff, ACPI_IO_BASE + 0x30);
229 /* Disable SCI on GPIO. */
230 outw(0x0, ACPI_IO_BASE + 0x22);
232 /* Disable SMI on GPIO. */
233 outw(0x0, ACPI_IO_BASE + 0x24);
235 /* Disable all global enable SMIs. */
236 outw(0x0, ACPI_IO_BASE + 0x2a);
238 /* All SMI off, both IDE buses ON, PSON rising edge. */
239 outw(0x0, ACPI_IO_BASE + 0x2c);
241 /* Primary activity SMI disable. */
242 outl(0x0, ACPI_IO_BASE + 0x34);
244 /* GP timer reload on none. */
245 outl(0x0, ACPI_IO_BASE + 0x38);
247 /* Disable extended IO traps. */
248 outb(0x0, ACPI_IO_BASE + 0x42);
250 /* SCI is generated for RTC/pwrBtn/slpBtn. */
251 outw(0x0001, ACPI_IO_BASE + 0x04);
253 /* Allow SLP# signal to assert LDTSTOP_L.
254 * Will work for C3 and for FID/VID change.
256 outb(0x1, ACPI_IO_BASE + 0x11);
259 static void cx700_set_lpc_registers(struct device *dev)
261 unsigned char enables;
263 printk_debug("VIA CX700 LPC bridge init\n");
265 // enable the internal I/O decode
266 enables = pci_read_config8(dev, 0x6C);
268 pci_write_config8(dev, 0x6C, enables);
270 // Map 4MB of FLASH into the address space
271 // pci_write_config8(dev, 0x41, 0x7f);
273 // Set bit 6 of 0x40, because Award does it (IO recovery time)
274 // IMPORTANT FIX - EISA 0x4d0 decoding must be on so that PCI
275 // interrupts can be properly marked as level triggered.
276 enables = pci_read_config8(dev, 0x40);
278 pci_write_config8(dev, 0x40, enables);
280 /* DMA Line buffer control */
281 enables = pci_read_config8(dev, 0x42);
283 pci_write_config8(dev, 0x42, enables);
285 /* I/O recovery time */
286 pci_write_config8(dev, 0x4c, 0x44);
288 /* ROM memory cycles go to LPC. */
289 pci_write_config8(dev, 0x59, 0x80);
291 /* Enable SM dynamic clock gating */
292 pci_write_config8(dev, 0x5b, 0x01);
294 /* Set Read Pass Write Control Enable */
295 pci_write_config8(dev, 0x48, 0x0c);
297 /* Set SM Misc Control: Enable Internal APIC . */
298 enables = pci_read_config8(dev, 0x58);
300 pci_write_config8(dev, 0x58, enables);
301 enables = pci_read_config8(dev, 0x4d);
303 pci_write_config8(dev, 0x4d, enables);
305 /* Set bit 3 of 0x4f to match award (use INIT# as cpu reset) */
306 enables = pci_read_config8(dev, 0x4f);
308 pci_write_config8(dev, 0x4f, enables);
310 /* enable KBC configuration */
311 pci_write_config8(dev, 0x51, 0x1f);
313 /* enable serial irq */
314 pci_write_config8(dev, 0x52, 0x9);
317 pci_write_config8(dev, 0x53, 0x00);
319 // Power management setup
322 /* set up isa bus -- i/o recovery time, rom write enable, extend-ale */
323 pci_write_config8(dev, 0x40, 0x54);
325 /* Enable HPET timer */
326 pci_write_config32(dev, 0x68, (1 << 31) | (HPET_ADDR >> 8));
330 void cx700_read_resources(device_t dev)
332 struct resource *resource;
334 /* Make sure we call our childrens set/enable functions - these
335 * are not called unless this device has a resource to set.
338 pci_dev_read_resources(dev);
340 resource = new_resource(dev, 1);
342 IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_IO | IORESOURCE_STORED;
344 resource->base = 0x2e;
347 void cx700_set_resources(device_t dev)
349 struct resource *resource;
350 resource = find_resource(dev, 1);
351 resource->flags |= IORESOURCE_STORED;
352 pci_dev_set_resources(dev);
355 void cx700_enable_resources(device_t dev)
357 /* Enable SuperIO decoding */
358 pci_dev_enable_resources(dev);
359 enable_childrens_resources(dev);
362 static void cx700_lpc_init(struct device *dev)
364 cx700_set_lpc_registers(dev);
370 /* Initialize interrupts */
371 pci_routing_fixup(dev);
372 /* make sure interupt controller is configured before keyboard init */
375 /* Start the Real Time Clock */
378 /* Initialize isa dma */
381 /* Initialize keyboard controller */
382 init_pc_keyboard(0x60, 0x64, 0);
385 static struct device_operations cx700_lpc_ops = {
386 .read_resources = cx700_read_resources,
387 .set_resources = cx700_set_resources,
388 .enable_resources = cx700_enable_resources,
389 .init = &cx700_lpc_init,
390 .scan_bus = scan_static_bus,
393 static const struct pci_driver lpc_driver __pci_driver = {
394 .ops = &cx700_lpc_ops,
395 .vendor = PCI_VENDOR_ID_VIA,