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>
44 unsigned int value_low, value_high;
47 static struct ioapicreg ioapicregvalues[] = {
48 #define ALL (0xff << 24)
50 #define DISABLED (1 << 16)
51 #define ENABLED (0 << 16)
52 #define TRIGGER_EDGE (0 << 15)
53 #define TRIGGER_LEVEL (1 << 15)
54 #define POLARITY_HIGH (0 << 13)
55 #define POLARITY_LOW (1 << 13)
56 #define PHYSICAL_DEST (0 << 11)
57 #define LOGICAL_DEST (1 << 11)
58 #define ExtINT (7 << 8)
62 /* IO-APIC virtual wire mode configuration */
63 /* mask, trigger, polarity, destination, delivery, vector */
64 { 0, ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT, NONE},
74 { 10, DISABLED, NONE},
75 { 11, DISABLED, NONE},
76 { 12, DISABLED, NONE},
77 { 13, DISABLED, NONE},
78 { 14, DISABLED, NONE},
79 { 15, DISABLED, NONE},
80 { 16, DISABLED, NONE},
81 { 17, DISABLED, NONE},
82 { 18, DISABLED, NONE},
83 { 19, DISABLED, NONE},
84 { 20, DISABLED, NONE},
85 { 21, DISABLED, NONE},
86 { 22, DISABLED, NONE},
87 { 23, DISABLED, NONE},
88 /* Be careful and don't write past the end... */
91 static void setup_ioapic(unsigned long ioapic_base, int master)
94 unsigned long value_low, value_high;
95 // unsigned long ioapic_base = 0xfec00000;
96 volatile unsigned long *l;
97 struct ioapicreg *a = ioapicregvalues;
100 ioapicregvalues[0].value_high = lapicid()<<(56-32);
101 ioapicregvalues[0].value_low = ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT;
104 ioapicregvalues[0].value_high = NONE;
105 ioapicregvalues[0].value_low = DISABLED;
108 l = (unsigned long *) ioapic_base;
110 for (i = 0; i < ARRAY_SIZE(ioapicregvalues);
112 l[0] = (a->reg * 2) + 0x10;
115 l[0] = (a->reg *2) + 0x11;
116 l[4] = a->value_high;
118 if ((i==0) && (value_low == 0xffffffff)) {
119 printk_warning("IO APIC not responding.\n");
122 printk_spew("for IRQ, reg 0x%08x value 0x%08x 0x%08x\n",
123 a->reg, a->value_low, a->value_high);
128 #define PREVIOUS_POWER_STATE 0x7A
130 #define MAINBOARD_POWER_OFF 0
131 #define MAINBOARD_POWER_ON 1
132 #define SLOW_CPU_OFF 0
133 #define SLOW_CPU__ON 1
135 #ifndef MAINBOARD_POWER_ON_AFTER_POWER_FAIL
136 #define MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
139 static void lpc_common_init(device_t dev, int master)
144 /* IO APIC initialization */
145 byte = pci_read_config8(dev, 0x74);
146 byte |= (1<<0); // enable APIC
147 pci_write_config8(dev, 0x74, byte);
148 dword = pci_read_config32(dev, PCI_BASE_ADDRESS_1); // 0x14
150 setup_ioapic(dword, master);
153 static void lpc_slave_init(device_t dev)
155 lpc_common_init(dev, 0);
159 static void enable_hpet(struct device *dev)
161 unsigned long hpet_address;
163 pci_write_config32(dev,0x44, 0xfed00001);
164 hpet_address=pci_read_config32(dev,0x44)& 0xfffffffe;
165 printk_debug("enabling HPET @0x%x\n", hpet_address);
169 static void lpc_init(device_t dev)
176 lpc_common_init(dev, 1);
179 /* posted memory write enable */
180 byte = pci_read_config8(dev, 0x46);
181 pci_write_config8(dev, 0x46, byte | (1<<0));
183 /* power after power fail */
186 on = MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
187 get_option("power_on_after_fail", &on);
188 byte = pci_read_config8(dev, PREVIOUS_POWER_STATE);
193 pci_write_config8(dev, PREVIOUS_POWER_STATE, byte);
194 printk_info("set power %s after power fail\n", on?"on":"off");
196 /* Throttle the CPU speed down for testing */
198 get_option("slow_cpu", &on);
202 pm10_bar = (pci_read_config16(dev, 0x60)&0xff00);
203 outl(((on<<1)+0x10) ,(pm10_bar + 0x10));
204 dword = inl(pm10_bar + 0x10);
206 printk_debug("Throttling CPU %2d.%1.1d percent.\n",
207 (on*12)+(on>>1),(on&1)*5);
211 // default is enabled
212 /* Enable Port 92 fast reset */
213 byte = pci_read_config8(dev, 0xe8);
215 pci_write_config8(dev, 0xe8, byte);
218 /* Enable Error reporting */
219 /* Set up sync flood detected */
220 byte = pci_read_config8(dev, 0x47);
222 pci_write_config8(dev, 0x47, byte);
224 /* Set up NMI on errors */
225 byte = inb(0x70); // RTC70
227 nmi_option = NMI_OFF;
228 get_option("nmi", &nmi_option);
230 byte &= ~(1 << 7); /* set NMI */
232 byte |= ( 1 << 7); // Can not mask NMI from PCI-E and NMI_NOW
234 if( byte != byte_old) {
238 /* Initialize the real time clock */
241 /* Initialize isa dma */
244 /* Initialize the High Precision Event Timers */
249 static void mcp55_lpc_read_resources(device_t dev)
251 struct resource *res;
253 /* Get the normal pci resources of this device */
254 pci_dev_read_resources(dev); // We got one for APIC, or one more for TRAP
256 /* Add an extra subtractive resource for both memory and I/O */
257 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
258 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
260 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
261 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
266 * @brief Enable resources for children devices
268 * @param dev the device whos children's resources are to be enabled
270 * This function is call by the global enable_resources() indirectly via the
271 * device_operation::enable_resources() method of devices.
273 * Indirect mutual recursion:
274 * enable_childrens_resources() -> enable_resources()
275 * enable_resources() -> device_operation::enable_resources()
276 * device_operation::enable_resources() -> enable_children_resources()
278 static void mcp55_lpc_enable_childrens_resources(device_t dev)
281 uint32_t reg, reg_var[4];
285 reg = pci_read_config32(dev, 0xa0);
287 for (link = 0; link < dev->links; link++) {
289 for (child = dev->link[link].children; child; child = child->sibling) {
290 enable_resources(child);
291 if(child->have_resources && (child->path.type == DEVICE_PATH_PNP)) {
292 for(i=0;i<child->resources;i++) {
293 struct resource *res;
294 unsigned long base, end; // don't need long long
295 res = &child->resource[i];
296 if(!(res->flags & IORESOURCE_IO)) continue;
298 end = resource_end(res);
299 printk_debug("mcp55 lpc decode:%s, base=0x%08x, end=0x%08x\n",dev_path(child),base, end);
302 reg |= (1<<0); break;
304 reg |= (1<<1); break;
305 case 0x378: // Parallal 1
306 reg |= (1<<24); break;
308 reg |= (1<<20); break;
309 case 0x220: // Aduio 0
310 reg |= (1<<8); break;
311 case 0x300: // Midi 0
312 reg |= (1<<12); break;
314 if( (base == 0x290) || (base >= 0x400)) {
315 if(var_num>=4) continue; // only 4 var ; compact them ?
316 reg |= (1<<(28+var_num));
317 reg_var[var_num++] = (base & 0xffff)|((end & 0xffff)<<16);
323 pci_write_config32(dev, 0xa0, reg);
324 for(i=0;i<var_num;i++) {
325 pci_write_config32(dev, 0xa8 + i*4, reg_var[i]);
331 static void mcp55_lpc_enable_resources(device_t dev)
333 pci_dev_enable_resources(dev);
334 mcp55_lpc_enable_childrens_resources(dev);
337 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
339 pci_write_config32(dev, 0x40,
340 ((device & 0xffff) << 16) | (vendor & 0xffff));
343 static struct pci_operations lops_pci = {
344 .set_subsystem = lpci_set_subsystem,
347 static struct device_operations lpc_ops = {
348 .read_resources = mcp55_lpc_read_resources,
349 .set_resources = pci_dev_set_resources,
350 .enable_resources = mcp55_lpc_enable_resources,
352 .scan_bus = scan_static_bus,
353 // .enable = mcp55_enable,
354 .ops_pci = &lops_pci,
356 static const struct pci_driver lpc_driver __pci_driver = {
358 .vendor = PCI_VENDOR_ID_NVIDIA,
359 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC,
362 static const struct pci_driver lpc_driver_pro __pci_driver = {
364 .vendor = PCI_VENDOR_ID_NVIDIA,
365 .device = PCI_DEVICE_ID_NVIDIA_MCP55_PRO,
368 static const struct pci_driver lpc_driver_lpc2 __pci_driver = {
370 .vendor = PCI_VENDOR_ID_NVIDIA,
371 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_2,
373 static const struct pci_driver lpc_driver_lpc3 __pci_driver = {
375 .vendor = PCI_VENDOR_ID_NVIDIA,
376 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_3,
378 static const struct pci_driver lpc_driver_lpc4 __pci_driver = {
380 .vendor = PCI_VENDOR_ID_NVIDIA,
381 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_4,
383 static const struct pci_driver lpc_driver_lpc5 __pci_driver = {
385 .vendor = PCI_VENDOR_ID_NVIDIA,
386 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_5,
388 static const struct pci_driver lpc_driver_lpc6 __pci_driver = {
390 .vendor = PCI_VENDOR_ID_NVIDIA,
391 .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_6,
394 static struct device_operations lpc_slave_ops = {
395 .read_resources = mcp55_lpc_read_resources,
396 .set_resources = pci_dev_set_resources,
397 .enable_resources = pci_dev_enable_resources,
398 .init = lpc_slave_init,
399 // .enable = mcp55_enable,
400 .ops_pci = &lops_pci,
403 static const struct pci_driver lpc_driver_slave __pci_driver = {
404 .ops = &lpc_slave_ops,
405 .vendor = PCI_VENDOR_ID_NVIDIA,
406 .device = PCI_DEVICE_ID_NVIDIA_MCP55_SLAVE,