blob: 483f19e76272b95a8819bf4e7981acda8b9b79bc [file] [log] [blame]
Stefan Reinaueraeba92a2009-04-17 08:37:18 +00001/*
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 Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000018 */
19
20#include <arch/io.h>
21#include <console/console.h>
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000022#include <device/device.h>
23#include <device/pci.h>
24#include <device/pci_ops.h>
25#include <device/pci_ids.h>
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000026#include <pc80/mc146818rtc.h>
27#include <pc80/i8259.h>
28#include <pc80/keyboard.h>
29#include <pc80/isa-dma.h>
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000030#include <cpu/x86/lapic.h>
Stefan Reinauer0401bd82010-01-16 18:31:34 +000031#include <arch/ioapic.h>
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000032#include <stdlib.h>
33
34#define ACPI_IO_BASE 0x400
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000035
36static const unsigned char pci_irqs[4] = { 11, 11, 10, 10 };
37
38static const unsigned char usb_pins[4] = { 'A', 'B', 'C', 'D' };
39static const unsigned char vga_pins[4] = { 'A', 'B', 'C', 'D' };
40static const unsigned char slot_pins[4] = { 'B', 'C', 'D', 'A' };
41static const unsigned char ac97_pins[4] = { 'B', 'C', 'D', 'A' };
42
43static 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
53static void pci_routing_fixup(struct device *dev)
54{
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000055 printk(BIOS_DEBUG, "%s: device is %p\n", __FUNCTION__, dev);
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000056
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 Reinauerc02b4fc2010-03-22 11:42:32 +000063 printk(BIOS_DEBUG, "Setting up USB interrupts.\n");
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000064 pci_assign_irqs(0, 0x10, pin_to_irq(usb_pins));
65
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000066 printk(BIOS_DEBUG, "Setting up VGA interrupts.\n");
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000067 pci_assign_irqs(1, 0x00, pin_to_irq(vga_pins));
68
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000069 printk(BIOS_DEBUG, "Setting up PCI slot interrupts.\n");
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000070 pci_assign_irqs(2, 0x04, pin_to_irq(slot_pins));
71 // more?
72
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000073 printk(BIOS_DEBUG, "Setting up AC97 interrupts.\n");
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000074 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 Reinauerc65666f2010-04-03 12:41:41 +000083static void setup_pm(device_t dev)
Stefan Reinaueraeba92a2009-04-17 08:37:18 +000084{
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
164static void cx700_set_lpc_registers(struct device *dev)
165{
166 unsigned char enables;
167
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000168 printk(BIOS_DEBUG, "VIA CX700 LPC bridge init\n");
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000169
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 Georgi9aeb6942012-10-05 21:54:38 +0200231 pci_write_config32(dev, 0x68, (1 << 31) | (CONFIG_HPET_ADDRESS >> 8));
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000232
233}
234
Stefan Reinauerc65666f2010-04-03 12:41:41 +0000235static void cx700_read_resources(device_t dev)
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000236{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000237 struct resource *res;
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000238
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 Watson29cc9ed2009-07-02 18:56:24 +0000245 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 Hermann74d1a6e2010-10-12 17:34:08 +0000252 res->base = IO_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000253 res->size = 0x00001000;
254 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000255}
256
Stefan Reinauerc65666f2010-04-03 12:41:41 +0000257static void cx700_set_resources(device_t dev)
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000258{
259 struct resource *resource;
260 resource = find_resource(dev, 1);
261 resource->flags |= IORESOURCE_STORED;
262 pci_dev_set_resources(dev);
263}
264
Stefan Reinauerc65666f2010-04-03 12:41:41 +0000265static void cx700_enable_resources(device_t dev)
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000266{
267 /* Enable SuperIO decoding */
268 pci_dev_enable_resources(dev);
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000269}
270
271static void cx700_lpc_init(struct device *dev)
272{
273 cx700_set_lpc_registers(dev);
274
Myles Watsonec0ee642009-10-19 16:21:30 +0000275#if CONFIG_IOAPIC
Stefan Reinauer0401bd82010-01-16 18:31:34 +0000276#define IO_APIC_ID 2
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800277 setup_ioapic(VIO_APIC_VADDR, IO_APIC_ID);
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000278#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 Blackb3f08c62014-04-30 17:12:25 -0700286 cmos_init(0);
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000287
288 /* Initialize isa dma */
289 isa_dma_init();
290
291 /* Initialize keyboard controller */
Edward O'Callaghandef00be2014-04-30 05:01:52 +1000292 pc_keyboard_init();
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000293}
294
295static 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'Callaghane408dce2014-10-31 08:54:41 +1100299 .init = cx700_lpc_init,
Kyösti Mälkkid0e212c2015-02-26 20:47:47 +0200300 .scan_bus = scan_lpc_bus,
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000301};
302
303static const struct pci_driver lpc_driver __pci_driver = {
Edward O'Callaghane408dce2014-10-31 08:54:41 +1100304 .ops = &cx700_lpc_ops,
Stefan Reinaueraeba92a2009-04-17 08:37:18 +0000305 .vendor = PCI_VENDOR_ID_VIA,
306 .device = 0x8324,
307};