Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the LinuxBIOS project. |
| 3 | * |
| 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. |
| 10 | * Copyright (C) 2007 Silicon Integrated Systems Corp. (SiS) |
| 11 | * Written by Morgan Tsai <my_tsai@sis.com> for SiS. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 | */ |
| 27 | |
| 28 | #include <console/console.h> |
| 29 | #include <device/device.h> |
| 30 | #include <device/pci.h> |
| 31 | #include <device/pnp.h> |
| 32 | #include <device/pci_ids.h> |
| 33 | #include <device/pci_ops.h> |
| 34 | #include <pc80/mc146818rtc.h> |
| 35 | #include <pc80/isa-dma.h> |
| 36 | #include <bitops.h> |
| 37 | #include <arch/io.h> |
| 38 | #include <cpu/x86/lapic.h> |
| 39 | #include "sis966.h" |
| 40 | #include <pc80/keyboard.h> |
| 41 | |
| 42 | #define NMI_OFF 0 |
| 43 | |
| 44 | struct ioapicreg { |
| 45 | unsigned int reg; |
| 46 | unsigned int value_low, value_high; |
| 47 | }; |
| 48 | |
| 49 | static struct ioapicreg ioapicregvalues[] = { |
| 50 | #define ALL (0xff << 24) |
| 51 | #define NONE (0) |
| 52 | #define DISABLED (1 << 16) |
| 53 | #define ENABLED (0 << 16) |
| 54 | #define TRIGGER_EDGE (0 << 15) |
| 55 | #define TRIGGER_LEVEL (1 << 15) |
| 56 | #define POLARITY_HIGH (0 << 13) |
| 57 | #define POLARITY_LOW (1 << 13) |
| 58 | #define PHYSICAL_DEST (0 << 11) |
| 59 | #define LOGICAL_DEST (1 << 11) |
| 60 | #define ExtINT (7 << 8) |
| 61 | #define NMI (4 << 8) |
| 62 | #define SMI (2 << 8) |
| 63 | #define INT (1 << 8) |
| 64 | /* IO-APIC virtual wire mode configuration */ |
| 65 | /* mask, trigger, polarity, destination, delivery, vector */ |
| 66 | { 0, ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT, NONE}, |
| 67 | { 1, DISABLED, NONE}, |
| 68 | { 2, DISABLED, NONE}, |
| 69 | { 3, DISABLED, NONE}, |
| 70 | { 4, DISABLED, NONE}, |
| 71 | { 5, DISABLED, NONE}, |
| 72 | { 6, DISABLED, NONE}, |
| 73 | { 7, DISABLED, NONE}, |
| 74 | { 8, DISABLED, NONE}, |
| 75 | { 9, DISABLED, NONE}, |
| 76 | { 10, DISABLED, NONE}, |
| 77 | { 11, DISABLED, NONE}, |
| 78 | { 12, DISABLED, NONE}, |
| 79 | { 13, DISABLED, NONE}, |
| 80 | { 14, DISABLED, NONE}, |
| 81 | { 15, DISABLED, NONE}, |
| 82 | { 16, DISABLED, NONE}, |
| 83 | { 17, DISABLED, NONE}, |
| 84 | { 18, DISABLED, NONE}, |
| 85 | { 19, DISABLED, NONE}, |
| 86 | { 20, DISABLED, NONE}, |
| 87 | { 21, DISABLED, NONE}, |
| 88 | { 22, DISABLED, NONE}, |
| 89 | { 23, DISABLED, NONE}, |
| 90 | /* Be careful and don't write past the end... */ |
| 91 | }; |
| 92 | |
| 93 | static void setup_ioapic(unsigned long ioapic_base) |
| 94 | { |
| 95 | int i; |
| 96 | unsigned long value_low, value_high; |
| 97 | // unsigned long ioapic_base = 0xfec00000; |
| 98 | volatile unsigned long *l; |
| 99 | struct ioapicreg *a = ioapicregvalues; |
| 100 | |
| 101 | ioapicregvalues[0].value_high = lapicid()<<(56-32); |
| 102 | |
| 103 | l = (unsigned long *) ioapic_base; |
| 104 | |
| 105 | for (i = 0; i < sizeof(ioapicregvalues) / sizeof(ioapicregvalues[0]); |
| 106 | i++, a++) { |
| 107 | l[0] = (a->reg * 2) + 0x10; |
| 108 | l[4] = a->value_low; |
| 109 | value_low = l[4]; |
| 110 | l[0] = (a->reg *2) + 0x11; |
| 111 | l[4] = a->value_high; |
| 112 | value_high = l[4]; |
| 113 | if ((i==0) && (value_low == 0xffffffff)) { |
| 114 | printk_warning("IO APIC not responding.\n"); |
| 115 | return; |
| 116 | } |
| 117 | printk_spew("for IRQ, reg 0x%08x value 0x%08x 0x%08x\n", |
| 118 | a->reg, a->value_low, a->value_high); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // 0x7a or e3 |
| 123 | #define PREVIOUS_POWER_STATE 0x7A |
| 124 | |
| 125 | #define MAINBOARD_POWER_OFF 0 |
| 126 | #define MAINBOARD_POWER_ON 1 |
| 127 | #define SLOW_CPU_OFF 0 |
| 128 | #define SLOW_CPU__ON 1 |
| 129 | |
| 130 | #ifndef MAINBOARD_POWER_ON_AFTER_POWER_FAIL |
| 131 | #define MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON |
| 132 | #endif |
| 133 | |
| 134 | static void lpc_common_init(device_t dev) |
| 135 | { |
| 136 | uint8_t byte; |
| 137 | uint32_t dword; |
| 138 | |
| 139 | /* IO APIC initialization */ |
| 140 | byte = pci_read_config8(dev, 0x74); |
| 141 | byte |= (1<<0); // enable APIC |
| 142 | pci_write_config8(dev, 0x74, byte); |
| 143 | dword = pci_read_config32(dev, PCI_BASE_ADDRESS_1); // 0x14 |
| 144 | |
| 145 | setup_ioapic(dword); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | static void lpc_slave_init(device_t dev) |
| 150 | { |
| 151 | lpc_common_init(dev); |
| 152 | } |
| 153 | |
| 154 | #if 0 |
| 155 | static void enable_hpet(struct device *dev) |
| 156 | { |
| 157 | unsigned long hpet_address; |
| 158 | |
| 159 | pci_write_config32(dev,0x44, 0xfed00001); |
| 160 | hpet_address=pci_read_config32(dev,0x44)& 0xfffffffe; |
| 161 | printk_debug("enabling HPET @0x%x\n", hpet_address); |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | static void lpc_usb_legacy_init(device_t dev) |
| 166 | { |
| 167 | uint16_t acpi_base; |
| 168 | |
| 169 | acpi_base = (pci_read_config8(dev,0x75) << 8); |
| 170 | //printk_debug("ACPI Base Addr=0x%4.4x\n",acpi_base); |
| 171 | |
| 172 | //printk_debug("acpi_base + 0xbb=%.2x\n", inb(acpi_base + 0xbb)); |
| 173 | //printk_debug("acpi_base + 0xba=%.2x\n", inb(acpi_base + 0xba)); |
| 174 | |
| 175 | outb(inb(acpi_base + 0xbb) |0x80, acpi_base + 0xbb); |
| 176 | outb(inb(acpi_base + 0xba) |0x80, acpi_base + 0xba); |
| 177 | |
| 178 | //printk_debug("acpi_base + 0xbb=%.2x\n", inb(acpi_base + 0xbb)); |
| 179 | //printk_debug("acpi_base + 0xba=%.2x\n", inb(acpi_base + 0xba)); |
| 180 | |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | static void lpc_init(device_t dev) |
| 185 | { |
| 186 | uint8_t byte; |
| 187 | uint8_t byte_old; |
| 188 | int on; |
| 189 | int nmi_option; |
| 190 | |
| 191 | printk_debug("lpc_init -------->\n"); |
| 192 | init_pc_keyboard(0x60, 0x64, 0); |
| 193 | |
| 194 | #if 0 |
| 195 | { |
| 196 | int i; |
| 197 | printk_debug("LPC PCI config \n"); |
| 198 | for(i=0;i<0xFF;i+=4) |
| 199 | { |
| 200 | if((i%16)==0) |
| 201 | { |
| 202 | print_debug("\r\n"); |
| 203 | print_debug_hex8(i); |
| 204 | print_debug(" ");} |
| 205 | print_debug_hex32(pci_read_config32(dev,i)); |
| 206 | print_debug(" "); |
| 207 | } |
| 208 | print_debug("\r\n"); |
| 209 | } |
| 210 | #endif |
| 211 | printk_debug("lpc_init <--------\n"); |
| 212 | lpc_usb_legacy_init(dev); |
| 213 | return; |
| 214 | |
| 215 | printk_debug("lpc_init\r\n"); |
| 216 | lpc_common_init(dev); |
| 217 | printk_debug("lpc_init2\r\n"); |
| 218 | |
| 219 | |
| 220 | #if 0 |
| 221 | /* posted memory write enable */ |
| 222 | byte = pci_read_config8(dev, 0x46); |
| 223 | pci_write_config8(dev, 0x46, byte | (1<<0)); |
| 224 | |
| 225 | #endif |
| 226 | /* power after power fail */ |
| 227 | |
| 228 | #if 1 |
| 229 | on = MAINBOARD_POWER_ON_AFTER_POWER_FAIL; |
| 230 | get_option(&on, "power_on_after_fail"); |
| 231 | byte = pci_read_config8(dev, PREVIOUS_POWER_STATE); |
| 232 | byte &= ~0x40; |
| 233 | if (!on) { |
| 234 | byte |= 0x40; |
| 235 | } |
| 236 | pci_write_config8(dev, PREVIOUS_POWER_STATE, byte); |
| 237 | printk_info("set power %s after power fail\n", on?"on":"off"); |
| 238 | #endif |
| 239 | /* Throttle the CPU speed down for testing */ |
| 240 | on = SLOW_CPU_OFF; |
| 241 | get_option(&on, "slow_cpu"); |
| 242 | if(on) { |
| 243 | uint16_t pm10_bar; |
| 244 | uint32_t dword; |
| 245 | pm10_bar = (pci_read_config16(dev, 0x60)&0xff00); |
| 246 | outl(((on<<1)+0x10) ,(pm10_bar + 0x10)); |
| 247 | dword = inl(pm10_bar + 0x10); |
| 248 | on = 8-on; |
| 249 | printk_debug("Throttling CPU %2d.%1.1d percent.\n", |
| 250 | (on*12)+(on>>1),(on&1)*5); |
| 251 | } |
| 252 | |
| 253 | #if 0 |
| 254 | // default is enabled |
| 255 | /* Enable Port 92 fast reset */ |
| 256 | byte = pci_read_config8(dev, 0xe8); |
| 257 | byte |= ~(1 << 3); |
| 258 | pci_write_config8(dev, 0xe8, byte); |
| 259 | #endif |
| 260 | |
| 261 | /* Enable Error reporting */ |
| 262 | /* Set up sync flood detected */ |
| 263 | byte = pci_read_config8(dev, 0x47); |
| 264 | byte |= (1 << 1); |
| 265 | pci_write_config8(dev, 0x47, byte); |
| 266 | |
| 267 | /* Set up NMI on errors */ |
| 268 | byte = inb(0x70); // RTC70 |
| 269 | byte_old = byte; |
| 270 | nmi_option = NMI_OFF; |
| 271 | get_option(&nmi_option, "nmi"); |
| 272 | if (nmi_option) { |
| 273 | byte &= ~(1 << 7); /* set NMI */ |
| 274 | } else { |
| 275 | byte |= ( 1 << 7); // Can not mask NMI from PCI-E and NMI_NOW |
| 276 | } |
| 277 | if( byte != byte_old) { |
| 278 | outb(0x70, byte); |
| 279 | } |
| 280 | |
| 281 | /* Initialize the real time clock */ |
| 282 | rtc_init(0); |
| 283 | |
| 284 | /* Initialize isa dma */ |
| 285 | isa_dma_init(); |
| 286 | |
| 287 | /* Initialize the High Precision Event Timers */ |
| 288 | // enable_hpet(dev); |
| 289 | |
| 290 | } |
| 291 | |
| 292 | static void sis966_lpc_read_resources(device_t dev) |
| 293 | { |
| 294 | struct resource *res; |
| 295 | unsigned long index; |
| 296 | |
| 297 | /* Get the normal pci resources of this device */ |
| 298 | pci_dev_read_resources(dev); // We got one for APIC, or one more for TRAP |
| 299 | |
| 300 | /* Add an extra subtractive resource for both memory and I/O */ |
| 301 | res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0)); |
| 302 | res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED; |
| 303 | |
| 304 | res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0)); |
| 305 | res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED; |
| 306 | |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * @brief Enable resources for children devices |
| 311 | * |
| 312 | * @param dev the device whos children's resources are to be enabled |
| 313 | * |
| 314 | * This function is call by the global enable_resources() indirectly via the |
| 315 | * device_operation::enable_resources() method of devices. |
| 316 | * |
| 317 | * Indirect mutual recursion: |
| 318 | * enable_childrens_resources() -> enable_resources() |
| 319 | * enable_resources() -> device_operation::enable_resources() |
| 320 | * device_operation::enable_resources() -> enable_children_resources() |
| 321 | */ |
| 322 | static void sis966_lpc_enable_childrens_resources(device_t dev) |
| 323 | { |
| 324 | unsigned link; |
| 325 | uint32_t reg, reg_var[4]; |
| 326 | int i; |
| 327 | int var_num = 0; |
| 328 | |
| 329 | reg = pci_read_config32(dev, 0xa0); |
| 330 | |
| 331 | for (link = 0; link < dev->links; link++) { |
| 332 | device_t child; |
| 333 | for (child = dev->link[link].children; child; child = child->sibling) { |
| 334 | enable_resources(child); |
| 335 | if(child->have_resources && (child->path.type == DEVICE_PATH_PNP)) { |
| 336 | for(i=0;i<child->resources;i++) { |
| 337 | struct resource *res; |
| 338 | unsigned long base, end; // don't need long long |
| 339 | res = &child->resource[i]; |
| 340 | if(!(res->flags & IORESOURCE_IO)) continue; |
| 341 | base = res->base; |
| 342 | end = resource_end(res); |
| 343 | printk_debug("sis966 lpc decode:%s, base=0x%08x, end=0x%08x\n",dev_path(child),base, end); |
| 344 | switch(base) { |
| 345 | case 0x3f8: // COM1 |
| 346 | reg |= (1<<0); break; |
| 347 | case 0x2f8: // COM2 |
| 348 | reg |= (1<<1); break; |
| 349 | case 0x378: // Parallal 1 |
| 350 | reg |= (1<<24); break; |
| 351 | case 0x3f0: // FD0 |
| 352 | reg |= (1<<20); break; |
| 353 | case 0x220: // Aduio 0 |
| 354 | reg |= (1<<8); break; |
| 355 | case 0x300: // Midi 0 |
| 356 | reg |= (1<<12); break; |
| 357 | } |
| 358 | if( (base == 0x290) || (base >= 0x400)) { |
| 359 | if(var_num>=4) continue; // only 4 var ; compact them ? |
| 360 | reg |= (1<<(28+var_num)); |
| 361 | reg_var[var_num++] = (base & 0xffff)|((end & 0xffff)<<16); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | pci_write_config32(dev, 0xa0, reg); |
| 368 | for(i=0;i<var_num;i++) { |
| 369 | pci_write_config32(dev, 0xa8 + i*4, reg_var[i]); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | } |
| 374 | |
| 375 | static void sis966_lpc_enable_resources(device_t dev) |
| 376 | { |
| 377 | pci_dev_enable_resources(dev); |
| 378 | sis966_lpc_enable_childrens_resources(dev); |
| 379 | } |
| 380 | |
| 381 | static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device) |
| 382 | { |
| 383 | pci_write_config32(dev, 0x40, |
| 384 | ((device & 0xffff) << 16) | (vendor & 0xffff)); |
| 385 | } |
| 386 | |
| 387 | static struct pci_operations lops_pci = { |
| 388 | .set_subsystem = lpci_set_subsystem, |
| 389 | }; |
| 390 | |
| 391 | static struct device_operations lpc_ops = { |
| 392 | .read_resources = sis966_lpc_read_resources, |
| 393 | .set_resources = pci_dev_set_resources, |
| 394 | .enable_resources = sis966_lpc_enable_resources, |
| 395 | .init = lpc_init, |
| 396 | .scan_bus = scan_static_bus, |
| 397 | // .enable = sis966_enable, |
| 398 | .ops_pci = &lops_pci, |
| 399 | }; |
Stefan Reinauer | 83b52e7 | 2007-10-30 02:17:49 +0000 | [diff] [blame^] | 400 | static const struct pci_driver lpc_driver __pci_driver = { |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 401 | .ops = &lpc_ops, |
| 402 | .vendor = PCI_VENDOR_ID_SIS, |
| 403 | .device = PCI_DEVICE_ID_SIS_SIS966_LPC, |
| 404 | }; |
| 405 | |
Stefan Reinauer | 83b52e7 | 2007-10-30 02:17:49 +0000 | [diff] [blame^] | 406 | static const struct pci_driver lpc_driver_pro __pci_driver = { |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 407 | .ops = &lpc_ops, |
| 408 | .vendor = PCI_VENDOR_ID_SIS, |
| 409 | .device = PCI_DEVICE_ID_SIS_SIS966_PRO, |
| 410 | }; |
| 411 | |
Stefan Reinauer | 83b52e7 | 2007-10-30 02:17:49 +0000 | [diff] [blame^] | 412 | static const struct pci_driver lpc_driver_lpc2 __pci_driver = { |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 413 | .ops = &lpc_ops, |
| 414 | .vendor = PCI_VENDOR_ID_SIS, |
| 415 | .device = PCI_DEVICE_ID_SIS_SIS966_LPC_2, |
| 416 | }; |
Stefan Reinauer | 83b52e7 | 2007-10-30 02:17:49 +0000 | [diff] [blame^] | 417 | static const struct pci_driver lpc_driver_lpc3 __pci_driver = { |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 418 | .ops = &lpc_ops, |
| 419 | .vendor = PCI_VENDOR_ID_SIS, |
| 420 | .device = PCI_DEVICE_ID_SIS_SIS966_LPC_3, |
| 421 | }; |
Stefan Reinauer | 83b52e7 | 2007-10-30 02:17:49 +0000 | [diff] [blame^] | 422 | static const struct pci_driver lpc_driver_lpc4 __pci_driver = { |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 423 | .ops = &lpc_ops, |
| 424 | .vendor = PCI_VENDOR_ID_SIS, |
| 425 | .device = PCI_DEVICE_ID_SIS_SIS966_LPC_4, |
| 426 | }; |
Stefan Reinauer | 83b52e7 | 2007-10-30 02:17:49 +0000 | [diff] [blame^] | 427 | static const struct pci_driver lpc_driver_lpc5 __pci_driver = { |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 428 | .ops = &lpc_ops, |
| 429 | .vendor = PCI_VENDOR_ID_SIS, |
| 430 | .device = PCI_DEVICE_ID_SIS_SIS966_LPC_5, |
| 431 | }; |
Stefan Reinauer | 83b52e7 | 2007-10-30 02:17:49 +0000 | [diff] [blame^] | 432 | static const struct pci_driver lpc_driver_lpc6 __pci_driver = { |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 433 | .ops = &lpc_ops, |
| 434 | .vendor = PCI_VENDOR_ID_SIS, |
| 435 | .device = PCI_DEVICE_ID_SIS_SIS966_LPC_6, |
| 436 | }; |
| 437 | |
| 438 | static struct device_operations lpc_slave_ops = { |
| 439 | .read_resources = sis966_lpc_read_resources, |
| 440 | .set_resources = pci_dev_set_resources, |
| 441 | .enable_resources = pci_dev_enable_resources, |
| 442 | .init = lpc_slave_init, |
| 443 | // .enable = sis966_enable, |
| 444 | .ops_pci = &lops_pci, |
| 445 | }; |
| 446 | |
Stefan Reinauer | 83b52e7 | 2007-10-30 02:17:49 +0000 | [diff] [blame^] | 447 | static const struct pci_driver lpc_driver_slave __pci_driver = { |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 448 | .ops = &lpc_slave_ops, |
| 449 | .vendor = PCI_VENDOR_ID_SIS, |
| 450 | .device = PCI_DEVICE_ID_SIS_SIS966_SLAVE, |
| 451 | }; |