2 * This file is part of the coreboot project.
4 * Copyright (C) 2003 Linux Networx
5 * Copyright (C) 2003 SuSE Linux AG
6 * Copyright (C) 2004 Tyan Computer
7 * Written by Yinghai Lu <yhlu@tyan.com> for Tyan Computer.
8 * Copyright (C) 2006,2007 AMD
9 * Written by Yinghai Lu <yinghai.lu@amd.com> for AMD.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <console/console.h>
27 #include <device/device.h>
28 #include <device/pci.h>
29 #include <device/pnp.h>
30 #include <device/pci_ids.h>
31 #include <device/pci_ops.h>
32 #include <pc80/mc146818rtc.h>
33 #include <pc80/isa-dma.h>
36 #include <cpu/x86/lapic.h>
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},
73 { 10, DISABLED, NONE},
74 { 11, DISABLED, NONE},
75 { 12, DISABLED, NONE},
76 { 13, DISABLED, NONE},
77 { 14, DISABLED, NONE},
78 { 15, DISABLED, NONE},
79 { 16, DISABLED, NONE},
80 { 17, DISABLED, NONE},
81 { 18, DISABLED, NONE},
82 { 19, DISABLED, NONE},
83 { 20, DISABLED, NONE},
84 { 21, DISABLED, NONE},
85 { 22, DISABLED, NONE},
86 { 23, DISABLED, NONE},
87 /* Be careful and don't write past the end... */
90 static void setup_ioapic(unsigned long ioapic_base, int master)
93 unsigned long value_low, value_high;
94 // unsigned long ioapic_base = 0xfec00000;
95 volatile unsigned long *l;
96 struct ioapicreg *a = ioapicregvalues;
99 ioapicregvalues[0].value_high = lapicid()<<(56-32);
100 ioapicregvalues[0].value_low = ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT;
103 ioapicregvalues[0].value_high = NONE;
104 ioapicregvalues[0].value_low = DISABLED;
107 l = (unsigned long *) ioapic_base;
109 for (i = 0; i < sizeof(ioapicregvalues) / sizeof(ioapicregvalues[0]);
111 l[0] = (a->reg * 2) + 0x10;
114 l[0] = (a->reg *2) + 0x11;
115 l[4] = a->value_high;
117 if ((i==0) && (value_low == 0xffffffff)) {
118 printk_warning("IO APIC not responding.\n");
121 printk_spew("for IRQ, reg 0x%08x value 0x%08x 0x%08x\n",
122 a->reg, a->value_low, a->value_high);
127 #define PREVIOUS_POWER_STATE 0x7A
129 #define MAINBOARD_POWER_OFF 0
130 #define MAINBOARD_POWER_ON 1
131 #define SLOW_CPU_OFF 0
132 #define SLOW_CPU__ON 1
134 #ifndef MAINBOARD_POWER_ON_AFTER_POWER_FAIL
135 #define MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
138 static void lpc_common_init(device_t dev, int master)
143 /* IO APIC initialization */
144 byte = pci_read_config8(dev, 0x74);
145 byte |= (1<<0); // enable APIC
146 pci_write_config8(dev, 0x74, byte);
147 dword = pci_read_config32(dev, PCI_BASE_ADDRESS_1); // 0x14
149 setup_ioapic(dword, master);
152 static void lpc_slave_init(device_t dev)
154 lpc_common_init(dev, 0);
158 static void enable_hpet(struct device *dev)
160 unsigned long hpet_address;
162 pci_write_config32(dev,0x44, 0xfed00001);
163 hpet_address=pci_read_config32(dev,0x44)& 0xfffffffe;
164 printk_debug("enabling HPET @0x%x\n", hpet_address);
168 static void lpc_init(device_t dev)
175 lpc_common_init(dev, 1);
178 /* posted memory write enable */
179 byte = pci_read_config8(dev, 0x46);
180 pci_write_config8(dev, 0x46, byte | (1<<0));
182 /* power after power fail */
185 on = MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
186 get_option(&on, "power_on_after_fail");
187 byte = pci_read_config8(dev, PREVIOUS_POWER_STATE);
192 pci_write_config8(dev, PREVIOUS_POWER_STATE, byte);
193 printk_info("set power %s after power fail\n", on?"on":"off");
195 /* Throttle the CPU speed down for testing */
197 get_option(&on, "slow_cpu");
201 pm10_bar = (pci_read_config16(dev, 0x60)&0xff00);
202 outl(((on<<1)+0x10) ,(pm10_bar + 0x10));
203 dword = inl(pm10_bar + 0x10);
205 printk_debug("Throttling CPU %2d.%1.1d percent.\n",
206 (on*12)+(on>>1),(on&1)*5);
210 // default is enabled
211 /* Enable Port 92 fast reset */
212 byte = pci_read_config8(dev, 0xe8);
214 pci_write_config8(dev, 0xe8, byte);
217 /* Enable Error reporting */
218 /* Set up sync flood detected */
219 byte = pci_read_config8(dev, 0x47);
221 pci_write_config8(dev, 0x47, byte);
223 /* Set up NMI on errors */
224 byte = inb(0x70); // RTC70
226 nmi_option = NMI_OFF;
227 get_option(&nmi_option, "nmi");
229 byte &= ~(1 << 7); /* set NMI */
231 byte |= ( 1 << 7); // Can not mask NMI from PCI-E and NMI_NOW
233 if( byte != byte_old) {
237 /* Initialize the real time clock */
240 /* Initialize isa dma */
243 /* Initialize the High Precision Event Timers */
248 static void mcp55_lpc_read_resources(device_t dev)
250 struct resource *res;
252 /* Get the normal pci resources of this device */
253 pci_dev_read_resources(dev); // We got one for APIC, or one more for TRAP
255 /* Add an extra subtractive resource for both memory and I/O */
256 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
257 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
259 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
260 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
265 * @brief Enable resources for children devices
267 * @param dev the device whos children's resources are to be enabled
269 * This function is call by the global enable_resources() indirectly via the
270 * device_operation::enable_resources() method of devices.
272 * Indirect mutual recursion:
273 * enable_childrens_resources() -> enable_resources()
274 * enable_resources() -> device_operation::enable_resources()
275 * device_operation::enable_resources() -> enable_children_resources()
277 static void mcp55_lpc_enable_childrens_resources(device_t dev)
280 uint32_t reg, reg_var[4];
284 reg = pci_read_config32(dev, 0xa0);
286 for (link = 0; link < dev->links; link++) {
288 for (child = dev->link[link].children; child; child = child->sibling) {
289 enable_resources(child);
290 if(child->have_resources && (child->path.type == DEVICE_PATH_PNP)) {
291 for(i=0;i<child->resources;i++) {
292 struct resource *res;
293 unsigned long base, end; // don't need long long
294 res = &child->resource[i];
295 if(!(res->flags & IORESOURCE_IO)) continue;
297 end = resource_end(res);
298 printk_debug("mcp55 lpc decode:%s, base=0x%08x, end=0x%08x\n",dev_path(child),base, end);
301 reg |= (1<<0); break;
303 reg |= (1<<1); break;
304 case 0x378: // Parallal 1
305 reg |= (1<<24); break;
307 reg |= (1<<20); break;
308 case 0x220: // Aduio 0
309 reg |= (1<<8); break;
310 case 0x300: // Midi 0
311 reg |= (1<<12); break;
313 if( (base == 0x290) || (base >= 0x400)) {
314 if(var_num>=4) continue; // only 4 var ; compact them ?
315 reg |= (1<<(28+var_num));
316 reg_var[var_num++] = (base & 0xffff)|((end & 0xffff)<<16);
322 pci_write_config32(dev, 0xa0, reg);
323 for(i=0;i<var_num;i++) {
324 pci_write_config32(dev, 0xa8 + i*4, reg_var[i]);
330 static void mcp55_lpc_enable_resources(device_t dev)
332 pci_dev_enable_resources(dev);
333 mcp55_lpc_enable_childrens_resources(dev);
336 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
338 pci_write_config32(dev, 0x40,
339 ((device & 0xffff) << 16) | (vendor & 0xffff));
342 static struct pci_operations lops_pci = {
343 .set_subsystem = lpci_set_subsystem,
346 static struct device_operations lpc_ops = {
347 .read_resources = mcp55_lpc_read_resources,
348 .set_resources = pci_dev_set_resources,
349 .enable_resources = mcp55_lpc_enable_resources,
351 .scan_bus = scan_static_bus,
352 // .enable = mcp55_enable,
353 .ops_pci = &lops_pci,
355 static const struct pci_driver lpc_driver __pci_driver = {
357 .vendor = PCI_VENDOR_ID_NVIDIA,
358 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC,
361 static const struct pci_driver lpc_driver_pro __pci_driver = {
363 .vendor = PCI_VENDOR_ID_NVIDIA,
364 .device = PCI_DEVICE_ID_NVIDIA_MCP55_PRO,
367 static const struct pci_driver lpc_driver_lpc2 __pci_driver = {
369 .vendor = PCI_VENDOR_ID_NVIDIA,
370 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_2,
372 static const struct pci_driver lpc_driver_lpc3 __pci_driver = {
374 .vendor = PCI_VENDOR_ID_NVIDIA,
375 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_3,
377 static const struct pci_driver lpc_driver_lpc4 __pci_driver = {
379 .vendor = PCI_VENDOR_ID_NVIDIA,
380 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_4,
382 static const struct pci_driver lpc_driver_lpc5 __pci_driver = {
384 .vendor = PCI_VENDOR_ID_NVIDIA,
385 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_5,
387 static const struct pci_driver lpc_driver_lpc6 __pci_driver = {
389 .vendor = PCI_VENDOR_ID_NVIDIA,
390 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_6,
393 static struct device_operations lpc_slave_ops = {
394 .read_resources = mcp55_lpc_read_resources,
395 .set_resources = pci_dev_set_resources,
396 .enable_resources = pci_dev_enable_resources,
397 .init = lpc_slave_init,
398 // .enable = mcp55_enable,
399 .ops_pci = &lops_pci,
402 static const struct pci_driver lpc_driver_slave __pci_driver = {
403 .ops = &lpc_slave_ops,
404 .vendor = PCI_VENDOR_ID_NVIDIA,
405 .device = PCI_DEVICE_ID_NVIDIA_MCP55_SLAVE,