2 * (C) 2003 Linux Networx, SuSE Linux AG
3 * Copyright 2004 Tyan Computer
5 * 2006.1 yhlu add dest apicid for IRQ0
8 #include <console/console.h>
9 #include <device/device.h>
10 #include <device/pci.h>
11 #include <device/pnp.h>
12 #include <device/pci_ids.h>
13 #include <device/pci_ops.h>
14 #include <pc80/mc146818rtc.h>
15 #include <pc80/isa-dma.h>
18 #include <cpu/x86/lapic.h>
22 #define CK804_CHIP_REV 2
28 unsigned int value_low, value_high;
31 static struct ioapicreg ioapicregvalues[] = {
32 #define ALL (0xff << 24)
34 #define DISABLED (1 << 16)
35 #define ENABLED (0 << 16)
36 #define TRIGGER_EDGE (0 << 15)
37 #define TRIGGER_LEVEL (1 << 15)
38 #define POLARITY_HIGH (0 << 13)
39 #define POLARITY_LOW (1 << 13)
40 #define PHYSICAL_DEST (0 << 11)
41 #define LOGICAL_DEST (1 << 11)
42 #define ExtINT (7 << 8)
46 /* IO-APIC virtual wire mode configuration */
47 /* mask, trigger, polarity, destination, delivery, vector */
48 {0, ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT, NONE},
72 /* Be careful and don't write past the end... */
75 static void setup_ioapic(unsigned long ioapic_base)
78 unsigned long value_low, value_high;
79 /* unsigned long ioapic_base = 0xfec00000; */
80 volatile unsigned long *l;
81 struct ioapicreg *a = ioapicregvalues;
83 ioapicregvalues[0].value_high = lapicid() << (56 - 32);
85 l = (unsigned long *)ioapic_base;
87 for (i = 0; i < ARRAY_SIZE(ioapicregvalues); i++, a++) {
88 l[0] = (a->reg * 2) + 0x10;
91 l[0] = (a->reg * 2) + 0x11;
94 if ((i == 0) && (value_low == 0xffffffff)) {
95 printk_warning("IO APIC not responding.\n");
98 printk_spew("for IRQ, reg 0x%08x value 0x%08x 0x%08x\n",
99 a->reg, a->value_low, a->value_high);
104 #define PREVIOUS_POWER_STATE 0x7A
106 #define MAINBOARD_POWER_OFF 0
107 #define MAINBOARD_POWER_ON 1
108 #define SLOW_CPU_OFF 0
109 #define SLOW_CPU__ON 1
111 #ifndef MAINBOARD_POWER_ON_AFTER_POWER_FAIL
112 #define MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
115 static void lpc_common_init(device_t dev)
120 /* I/O APIC initialization */
121 byte = pci_read_config8(dev, 0x74);
122 byte |= (1 << 0); /* Enable APIC. */
123 pci_write_config8(dev, 0x74, byte);
124 dword = pci_read_config32(dev, PCI_BASE_ADDRESS_1); /* 0x14 */
129 dword = pci_read_config32(dev, 0xe4);
131 pci_write_config32(dev, 0xe4, dword);
135 static void lpc_slave_init(device_t dev)
137 lpc_common_init(dev);
140 static void rom_dummy_write(device_t dev)
145 old = pci_read_config8(dev, 0x88);
148 pci_write_config8(dev, 0x88, new);
150 old = pci_read_config8(dev, 0x6d);
153 pci_write_config8(dev, 0x6d, new);
156 p = (uint8_t *) 0xffffffe0;
162 old = pci_read_config8(dev, 0x6d);
165 pci_write_config8(dev, 0x6d, new);
168 static void enable_hpet(struct device *dev)
170 unsigned long hpet_address;
172 pci_write_config32(dev, 0x44, 0xfed00001);
173 hpet_address = pci_read_config32(dev, 0x44) & 0xfffffffe;
174 printk_debug("Enabling HPET @0x%lx\n", hpet_address);
179 static void lpc_init(device_t dev)
181 uint8_t byte, byte_old;
184 lpc_common_init(dev);
186 pm_base = pci_read_config32(dev, 0x60) & 0xff00;
187 printk_info("%s: pm_base = %x \n", __func__, pm_base);
189 #if CK804_CHIP_REV==1
190 if (dev->bus->secondary != 1)
195 /* Posted memory write enable */
196 byte = pci_read_config8(dev, 0x46);
197 pci_write_config8(dev, 0x46, byte | (1 << 0));
200 /* power after power fail */
201 on = MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
202 get_option(&on, "power_on_after_fail");
203 byte = pci_read_config8(dev, PREVIOUS_POWER_STATE);
207 pci_write_config8(dev, PREVIOUS_POWER_STATE, byte);
208 printk_info("set power %s after power fail\n", on ? "on" : "off");
210 /* Throttle the CPU speed down for testing. */
212 get_option(&on, "slow_cpu");
216 pm10_bar = (pci_read_config16(dev, 0x60) & 0xff00);
217 outl(((on << 1) + 0x10), (pm10_bar + 0x10));
218 dword = inl(pm10_bar + 0x10);
220 printk_debug("Throttling CPU %2d.%1.1d percent.\n",
221 (on * 12) + (on >> 1), (on & 1) * 5);
224 // default is enabled
225 /* Enable Port 92 fast reset. */
226 byte = pci_read_config8(dev, 0xe8);
228 pci_write_config8(dev, 0xe8, byte);
231 /* Enable Error reporting. */
232 /* Set up sync flood detected. */
233 byte = pci_read_config8(dev, 0x47);
235 pci_write_config8(dev, 0x47, byte);
237 /* Set up NMI on errors. */
238 byte = inb(0x70); /* RTC70 */
240 nmi_option = NMI_OFF;
241 get_option(&nmi_option, "nmi");
243 byte &= ~(1 << 7); /* Set NMI. */
245 byte |= (1 << 7); /* Can't mask NMI from PCI-E and NMI_NOW. */
247 if (byte != byte_old)
250 /* Initialize the real time clock (RTC). */
253 /* Initialize ISA DMA. */
256 /* Initialize the High Precision Event Timers (HPET). */
259 rom_dummy_write(dev);
262 static void ck804_lpc_read_resources(device_t dev)
264 struct resource *res;
267 /* Get the normal PCI resources of this device. */
268 /* We got one for APIC, or one more for TRAP. */
269 pci_dev_read_resources(dev);
271 /* Get resource for ACPI, SYSTEM_CONTROL, ANALOG_CONTROL. */
272 for (index = 0x60; index <= 0x68; index += 4) /* We got another 3. */
273 pci_get_resource(dev, index);
274 compact_resources(dev);
276 /* Add an extra subtractive resource for both memory and I/O. */
277 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
279 IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
281 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
283 IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
287 * Enable resources for children devices.
289 * This function is called by the global enable_resources() indirectly via the
290 * device_operation::enable_resources() method of devices.
292 * Indirect mutual recursion:
293 * enable_childrens_resources() -> enable_resources()
294 * enable_resources() -> device_operation::enable_resources()
295 * device_operation::enable_resources() -> enable_children_resources()
297 * @param dev The device whose children's resources are to be enabled.
299 static void ck804_lpc_enable_childrens_resources(device_t dev)
302 uint32_t reg, reg_var[4];
305 reg = pci_read_config32(dev, 0xa0);
307 for (link = 0; link < dev->links; link++) {
309 for (child = dev->link[link].children; child; child = child->sibling) {
310 enable_resources(child);
311 if (child->have_resources && (child->path.type == DEVICE_PATH_PNP)) {
312 for (i = 0; i < child->resources; i++) {
313 struct resource *res;
314 unsigned long base, end; // don't need long long
315 res = &child->resource[i];
316 if (!(res->flags & IORESOURCE_IO))
319 end = resource_end(res);
320 printk_debug("ck804 lpc decode:%s, base=0x%08lx, end=0x%08lx\r\n", dev_path(child), base, end);
328 case 0x378: // Parallel 1
334 case 0x220: // Audio 0
337 case 0x300: // Midi 0
341 if (base == 0x290 || base >= 0x400) {
343 continue; // only 4 var ; compact them ?
344 reg |= (1 << (28 + var_num));
345 reg_var[var_num++] = (base & 0xffff) | ((end & 0xffff) << 16);
351 pci_write_config32(dev, 0xa0, reg);
352 for (i = 0; i < var_num; i++)
353 pci_write_config32(dev, 0xa8 + i * 4, reg_var[i]);
356 static void ck804_lpc_enable_resources(device_t dev)
358 pci_dev_enable_resources(dev);
359 ck804_lpc_enable_childrens_resources(dev);
362 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
364 pci_write_config32(dev, 0x40,
365 ((device & 0xffff) << 16) | (vendor & 0xffff));
368 static struct pci_operations lops_pci = {
369 .set_subsystem = lpci_set_subsystem,
372 static struct device_operations lpc_ops = {
373 .read_resources = ck804_lpc_read_resources,
374 .set_resources = pci_dev_set_resources,
375 .enable_resources = ck804_lpc_enable_resources,
377 .scan_bus = scan_static_bus,
378 // .enable = ck804_enable,
379 .ops_pci = &lops_pci,
382 static const struct pci_driver lpc_driver __pci_driver = {
384 .vendor = PCI_VENDOR_ID_NVIDIA,
385 .device = PCI_DEVICE_ID_NVIDIA_CK804_LPC,
388 static const struct pci_driver lpc_driver_pro __pci_driver = {
390 .vendor = PCI_VENDOR_ID_NVIDIA,
391 .device = PCI_DEVICE_ID_NVIDIA_CK804_PRO,
394 #if CK804_CHIP_REV == 1
395 static const struct pci_driver lpc_driver_slave __pci_driver = {
397 .vendor = PCI_VENDOR_ID_NVIDIA,
398 .device = PCI_DEVICE_ID_NVIDIA_CK804_SLAVE,
401 static struct device_operations lpc_slave_ops = {
402 .read_resources = ck804_lpc_read_resources,
403 .set_resources = pci_dev_set_resources,
404 .enable_resources = pci_dev_enable_resources,
405 .init = lpc_slave_init,
406 // .enable = ck804_enable,
407 .ops_pci = &lops_pci,
410 static const struct pci_driver lpc_driver_slave __pci_driver = {
411 .ops = &lpc_slave_ops,
412 .vendor = PCI_VENDOR_ID_NVIDIA,
413 .device = PCI_DEVICE_ID_NVIDIA_CK804_SLAVE,