2 * This file is part of the LinuxBIOS 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)
93 unsigned long value_low, value_high;
94 // unsigned long ioapic_base = 0xfec00000;
95 volatile unsigned long *l;
96 struct ioapicreg *a = ioapicregvalues;
98 ioapicregvalues[0].value_high = lapicid()<<(56-32);
100 l = (unsigned long *) ioapic_base;
102 for (i = 0; i < sizeof(ioapicregvalues) / sizeof(ioapicregvalues[0]);
104 l[0] = (a->reg * 2) + 0x10;
107 l[0] = (a->reg *2) + 0x11;
108 l[4] = a->value_high;
110 if ((i==0) && (value_low == 0xffffffff)) {
111 printk_warning("IO APIC not responding.\n");
114 printk_spew("for IRQ, reg 0x%08x value 0x%08x 0x%08x\n",
115 a->reg, a->value_low, a->value_high);
120 #define PREVIOUS_POWER_STATE 0x7A
122 #define MAINBOARD_POWER_OFF 0
123 #define MAINBOARD_POWER_ON 1
124 #define SLOW_CPU_OFF 0
125 #define SLOW_CPU__ON 1
127 #ifndef MAINBOARD_POWER_ON_AFTER_POWER_FAIL
128 #define MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
131 static void lpc_common_init(device_t dev)
136 /* IO APIC initialization */
137 byte = pci_read_config8(dev, 0x74);
138 byte |= (1<<0); // enable APIC
139 pci_write_config8(dev, 0x74, byte);
140 dword = pci_read_config32(dev, PCI_BASE_ADDRESS_1); // 0x14
146 static void lpc_slave_init(device_t dev)
148 lpc_common_init(dev);
152 static void enable_hpet(struct device *dev)
154 unsigned long hpet_address;
156 pci_write_config32(dev,0x44, 0xfed00001);
157 hpet_address=pci_read_config32(dev,0x44)& 0xfffffffe;
158 printk_debug("enabling HPET @0x%x\n", hpet_address);
162 static void lpc_init(device_t dev)
169 lpc_common_init(dev);
172 /* posted memory write enable */
173 byte = pci_read_config8(dev, 0x46);
174 pci_write_config8(dev, 0x46, byte | (1<<0));
177 /* power after power fail */
180 on = MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
181 get_option(&on, "power_on_after_fail");
182 byte = pci_read_config8(dev, PREVIOUS_POWER_STATE);
187 pci_write_config8(dev, PREVIOUS_POWER_STATE, byte);
188 printk_info("set power %s after power fail\n", on?"on":"off");
190 /* Throttle the CPU speed down for testing */
192 get_option(&on, "slow_cpu");
196 pm10_bar = (pci_read_config16(dev, 0x60)&0xff00);
197 outl(((on<<1)+0x10) ,(pm10_bar + 0x10));
198 dword = inl(pm10_bar + 0x10);
200 printk_debug("Throttling CPU %2d.%1.1d percent.\n",
201 (on*12)+(on>>1),(on&1)*5);
205 // default is enabled
206 /* Enable Port 92 fast reset */
207 byte = pci_read_config8(dev, 0xe8);
209 pci_write_config8(dev, 0xe8, byte);
212 /* Enable Error reporting */
213 /* Set up sync flood detected */
214 byte = pci_read_config8(dev, 0x47);
216 pci_write_config8(dev, 0x47, byte);
218 /* Set up NMI on errors */
219 byte = inb(0x70); // RTC70
221 nmi_option = NMI_OFF;
222 get_option(&nmi_option, "nmi");
224 byte &= ~(1 << 7); /* set NMI */
226 byte |= ( 1 << 7); // Can not mask NMI from PCI-E and NMI_NOW
228 if( byte != byte_old) {
232 /* Initialize the real time clock */
235 /* Initialize isa dma */
238 /* Initialize the High Precision Event Timers */
243 static void mcp55_lpc_read_resources(device_t dev)
245 struct resource *res;
248 /* Get the normal pci resources of this device */
249 pci_dev_read_resources(dev); // We got one for APIC, or one more for TRAP
251 /* Add an extra subtractive resource for both memory and I/O */
252 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
253 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
255 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
256 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
261 * @brief Enable resources for children devices
263 * @param dev the device whos children's resources are to be enabled
265 * This function is call by the global enable_resources() indirectly via the
266 * device_operation::enable_resources() method of devices.
268 * Indirect mutual recursion:
269 * enable_childrens_resources() -> enable_resources()
270 * enable_resources() -> device_operation::enable_resources()
271 * device_operation::enable_resources() -> enable_children_resources()
273 static void mcp55_lpc_enable_childrens_resources(device_t dev)
276 uint32_t reg, reg_var[4];
280 reg = pci_read_config32(dev, 0xa0);
282 for (link = 0; link < dev->links; link++) {
284 for (child = dev->link[link].children; child; child = child->sibling) {
285 enable_resources(child);
286 if(child->have_resources && (child->path.type == DEVICE_PATH_PNP)) {
287 for(i=0;i<child->resources;i++) {
288 struct resource *res;
289 unsigned long base, end; // don't need long long
290 res = &child->resource[i];
291 if(!(res->flags & IORESOURCE_IO)) continue;
293 end = resource_end(res);
294 printk_debug("mcp55 lpc decode:%s, base=0x%08x, end=0x%08x\n",dev_path(child),base, end);
297 reg |= (1<<0); break;
299 reg |= (1<<1); break;
300 case 0x378: // Parallal 1
301 reg |= (1<<24); break;
303 reg |= (1<<20); break;
304 case 0x220: // Aduio 0
305 reg |= (1<<8); break;
306 case 0x300: // Midi 0
307 reg |= (1<<12); break;
309 if( (base == 0x290) || (base >= 0x400)) {
310 if(var_num>=4) continue; // only 4 var ; compact them ?
311 reg |= (1<<(28+var_num));
312 reg_var[var_num++] = (base & 0xffff)|((end & 0xffff)<<16);
318 pci_write_config32(dev, 0xa0, reg);
319 for(i=0;i<var_num;i++) {
320 pci_write_config32(dev, 0xa8 + i*4, reg_var[i]);
326 static void mcp55_lpc_enable_resources(device_t dev)
328 pci_dev_enable_resources(dev);
329 mcp55_lpc_enable_childrens_resources(dev);
332 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
334 pci_write_config32(dev, 0x40,
335 ((device & 0xffff) << 16) | (vendor & 0xffff));
338 static struct pci_operations lops_pci = {
339 .set_subsystem = lpci_set_subsystem,
342 static struct device_operations lpc_ops = {
343 .read_resources = mcp55_lpc_read_resources,
344 .set_resources = pci_dev_set_resources,
345 .enable_resources = mcp55_lpc_enable_resources,
347 .scan_bus = scan_static_bus,
348 // .enable = mcp55_enable,
349 .ops_pci = &lops_pci,
351 static struct pci_driver lpc_driver __pci_driver = {
353 .vendor = PCI_VENDOR_ID_NVIDIA,
354 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC,
357 static struct pci_driver lpc_driver_pro __pci_driver = {
359 .vendor = PCI_VENDOR_ID_NVIDIA,
360 .device = PCI_DEVICE_ID_NVIDIA_MCP55_PRO,
363 static struct pci_driver lpc_driver_lpc2 __pci_driver = {
365 .vendor = PCI_VENDOR_ID_NVIDIA,
366 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_2,
368 static struct pci_driver lpc_driver_lpc3 __pci_driver = {
370 .vendor = PCI_VENDOR_ID_NVIDIA,
371 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_3,
373 static struct pci_driver lpc_driver_lpc4 __pci_driver = {
375 .vendor = PCI_VENDOR_ID_NVIDIA,
376 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_4,
378 static struct pci_driver lpc_driver_lpc5 __pci_driver = {
380 .vendor = PCI_VENDOR_ID_NVIDIA,
381 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_5,
383 static struct pci_driver lpc_driver_lpc6 __pci_driver = {
385 .vendor = PCI_VENDOR_ID_NVIDIA,
386 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_6,
389 static struct device_operations lpc_slave_ops = {
390 .read_resources = mcp55_lpc_read_resources,
391 .set_resources = pci_dev_set_resources,
392 .enable_resources = pci_dev_enable_resources,
393 .init = lpc_slave_init,
394 // .enable = mcp55_enable,
395 .ops_pci = &lops_pci,
398 static struct pci_driver lpc_driver_slave __pci_driver = {
399 .ops = &lpc_slave_ops,
400 .vendor = PCI_VENDOR_ID_NVIDIA,
401 .device = PCI_DEVICE_ID_NVIDIA_MCP55_SLAVE,