Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 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> |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 38 | #include <arch/ioapic.h> |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 39 | #include <cpu/x86/lapic.h> |
Carl-Daniel Hailfinger | 2ee6779 | 2008-10-01 12:52:52 +0000 | [diff] [blame] | 40 | #include <stdlib.h> |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 41 | #include "sis966.h" |
| 42 | #include <pc80/keyboard.h> |
| 43 | |
| 44 | #define NMI_OFF 0 |
| 45 | |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 46 | // 0x7a or e3 |
| 47 | #define PREVIOUS_POWER_STATE 0x7A |
| 48 | |
| 49 | #define MAINBOARD_POWER_OFF 0 |
| 50 | #define MAINBOARD_POWER_ON 1 |
| 51 | #define SLOW_CPU_OFF 0 |
| 52 | #define SLOW_CPU__ON 1 |
| 53 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 54 | #ifndef CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL |
| 55 | #define CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Stefan Reinauer | eea66b7 | 2010-04-07 15:32:52 +0000 | [diff] [blame^] | 58 | #undef SLAVE_INIT |
| 59 | |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 60 | static void lpc_common_init(device_t dev) |
| 61 | { |
| 62 | uint8_t byte; |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 63 | uint32_t ioapic_base; |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 64 | |
| 65 | /* IO APIC initialization */ |
| 66 | byte = pci_read_config8(dev, 0x74); |
| 67 | byte |= (1<<0); // enable APIC |
| 68 | pci_write_config8(dev, 0x74, byte); |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 69 | ioapic_base = pci_read_config32(dev, PCI_BASE_ADDRESS_1); // 0x14 |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 70 | |
Stefan Reinauer | 0401bd8 | 2010-01-16 18:31:34 +0000 | [diff] [blame] | 71 | setup_ioapic(ioapic_base, 0); // Don't rename IO APIC ID |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Stefan Reinauer | eea66b7 | 2010-04-07 15:32:52 +0000 | [diff] [blame^] | 74 | #ifdef SLAVE_INIT |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 75 | static void lpc_slave_init(device_t dev) |
| 76 | { |
| 77 | lpc_common_init(dev); |
| 78 | } |
Stefan Reinauer | eea66b7 | 2010-04-07 15:32:52 +0000 | [diff] [blame^] | 79 | #endif |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 80 | |
| 81 | static void lpc_usb_legacy_init(device_t dev) |
| 82 | { |
| 83 | uint16_t acpi_base; |
| 84 | |
| 85 | acpi_base = (pci_read_config8(dev,0x75) << 8); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 86 | |
| 87 | outb(inb(acpi_base + 0xbb) |0x80, acpi_base + 0xbb); |
| 88 | outb(inb(acpi_base + 0xba) |0x80, acpi_base + 0xba); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void lpc_init(device_t dev) |
| 92 | { |
Morgan Tsai | 218c265 | 2007-11-02 16:09:58 +0000 | [diff] [blame] | 93 | uint8_t byte; |
| 94 | uint8_t byte_old; |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 95 | int on; |
| 96 | int nmi_option; |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 97 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 98 | printk(BIOS_DEBUG, "LPC_INIT -------->\n"); |
Stefan Reinauer | 740b587 | 2010-02-23 20:31:37 +0000 | [diff] [blame] | 99 | pc_keyboard_init(0); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 100 | |
Morgan Tsai | 218c265 | 2007-11-02 16:09:58 +0000 | [diff] [blame] | 101 | lpc_usb_legacy_init(dev); |
| 102 | lpc_common_init(dev); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 103 | |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 104 | /* power after power fail */ |
| 105 | |
Morgan Tsai | 218c265 | 2007-11-02 16:09:58 +0000 | [diff] [blame] | 106 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 107 | on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL; |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 108 | get_option(&on, "power_on_after_fail"); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 109 | byte = pci_read_config8(dev, PREVIOUS_POWER_STATE); |
| 110 | byte &= ~0x40; |
| 111 | if (!on) { |
| 112 | byte |= 0x40; |
| 113 | } |
| 114 | pci_write_config8(dev, PREVIOUS_POWER_STATE, byte); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 115 | printk(BIOS_INFO, "set power %s after power fail\n", on?"on":"off"); |
Morgan Tsai | 218c265 | 2007-11-02 16:09:58 +0000 | [diff] [blame] | 116 | |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 117 | /* Throttle the CPU speed down for testing */ |
| 118 | on = SLOW_CPU_OFF; |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 119 | get_option(&on, "slow_cpu"); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 120 | if(on) { |
| 121 | uint16_t pm10_bar; |
| 122 | uint32_t dword; |
| 123 | pm10_bar = (pci_read_config16(dev, 0x60)&0xff00); |
| 124 | outl(((on<<1)+0x10) ,(pm10_bar + 0x10)); |
| 125 | dword = inl(pm10_bar + 0x10); |
| 126 | on = 8-on; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 127 | printk(BIOS_DEBUG, "Throttling CPU %2d.%1.1d percent.\n", |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 128 | (on*12)+(on>>1),(on&1)*5); |
| 129 | } |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 130 | |
Morgan Tsai | 218c265 | 2007-11-02 16:09:58 +0000 | [diff] [blame] | 131 | /* Enable Error reporting */ |
| 132 | /* Set up sync flood detected */ |
| 133 | byte = pci_read_config8(dev, 0x47); |
| 134 | byte |= (1 << 1); |
| 135 | pci_write_config8(dev, 0x47, byte); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 136 | |
Morgan Tsai | 218c265 | 2007-11-02 16:09:58 +0000 | [diff] [blame] | 137 | /* Set up NMI on errors */ |
| 138 | byte = inb(0x70); // RTC70 |
| 139 | byte_old = byte; |
| 140 | nmi_option = NMI_OFF; |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 141 | get_option(&nmi_option, "nmi"); |
Morgan Tsai | 218c265 | 2007-11-02 16:09:58 +0000 | [diff] [blame] | 142 | if (nmi_option) { |
| 143 | byte &= ~(1 << 7); /* set NMI */ |
| 144 | } else { |
| 145 | byte |= ( 1 << 7); // Can not mask NMI from PCI-E and NMI_NOW |
| 146 | } |
| 147 | if( byte != byte_old) { |
Ed Swierk | e42e142 | 2009-07-10 15:05:35 +0000 | [diff] [blame] | 148 | outb(byte, 0x70); |
Morgan Tsai | 218c265 | 2007-11-02 16:09:58 +0000 | [diff] [blame] | 149 | } |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 150 | |
Morgan Tsai | 218c265 | 2007-11-02 16:09:58 +0000 | [diff] [blame] | 151 | /* Initialize the real time clock */ |
| 152 | rtc_init(0); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 153 | |
Morgan Tsai | 218c265 | 2007-11-02 16:09:58 +0000 | [diff] [blame] | 154 | /* Initialize isa dma */ |
| 155 | isa_dma_init(); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 156 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 157 | printk(BIOS_DEBUG, "LPC_INIT <--------\n"); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void sis966_lpc_read_resources(device_t dev) |
| 161 | { |
| 162 | struct resource *res; |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 163 | |
| 164 | /* Get the normal pci resources of this device */ |
| 165 | pci_dev_read_resources(dev); // We got one for APIC, or one more for TRAP |
| 166 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 167 | /* Add an extra subtractive resource for both memory and I/O. */ |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 168 | res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 169 | res->base = 0; |
| 170 | res->size = 0x1000; |
| 171 | res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | |
| 172 | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 173 | |
| 174 | res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 175 | res->base = 0xff800000; |
| 176 | res->size = 0x00800000; /* 8 MB for flash */ |
| 177 | res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | |
| 178 | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 179 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 180 | res = new_resource(dev, 3); /* IOAPIC */ |
| 181 | res->base = 0xfec00000; |
| 182 | res->size = 0x00001000; |
| 183 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @brief Enable resources for children devices |
| 188 | * |
| 189 | * @param dev the device whos children's resources are to be enabled |
| 190 | * |
| 191 | * This function is call by the global enable_resources() indirectly via the |
| 192 | * device_operation::enable_resources() method of devices. |
| 193 | * |
| 194 | * Indirect mutual recursion: |
| 195 | * enable_childrens_resources() -> enable_resources() |
| 196 | * enable_resources() -> device_operation::enable_resources() |
| 197 | * device_operation::enable_resources() -> enable_children_resources() |
| 198 | */ |
| 199 | static void sis966_lpc_enable_childrens_resources(device_t dev) |
| 200 | { |
| 201 | unsigned link; |
| 202 | uint32_t reg, reg_var[4]; |
| 203 | int i; |
| 204 | int var_num = 0; |
| 205 | |
| 206 | reg = pci_read_config32(dev, 0xa0); |
| 207 | |
| 208 | for (link = 0; link < dev->links; link++) { |
| 209 | device_t child; |
| 210 | for (child = dev->link[link].children; child; child = child->sibling) { |
| 211 | enable_resources(child); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 212 | if(child->enabled && (child->path.type == DEVICE_PATH_PNP)) { |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 213 | for(i=0;i<child->resources;i++) { |
| 214 | struct resource *res; |
| 215 | unsigned long base, end; // don't need long long |
| 216 | res = &child->resource[i]; |
| 217 | if(!(res->flags & IORESOURCE_IO)) continue; |
| 218 | base = res->base; |
| 219 | end = resource_end(res); |
Myles Watson | 08e0fb8 | 2010-03-22 16:33:25 +0000 | [diff] [blame] | 220 | printk(BIOS_DEBUG, "sis966 lpc decode:%s, base=0x%08lx, end=0x%08lx\n",dev_path(child),base, end); |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 221 | switch(base) { |
| 222 | case 0x3f8: // COM1 |
| 223 | reg |= (1<<0); break; |
| 224 | case 0x2f8: // COM2 |
| 225 | reg |= (1<<1); break; |
| 226 | case 0x378: // Parallal 1 |
| 227 | reg |= (1<<24); break; |
| 228 | case 0x3f0: // FD0 |
| 229 | reg |= (1<<20); break; |
| 230 | case 0x220: // Aduio 0 |
| 231 | reg |= (1<<8); break; |
| 232 | case 0x300: // Midi 0 |
| 233 | reg |= (1<<12); break; |
| 234 | } |
| 235 | if( (base == 0x290) || (base >= 0x400)) { |
| 236 | if(var_num>=4) continue; // only 4 var ; compact them ? |
| 237 | reg |= (1<<(28+var_num)); |
| 238 | reg_var[var_num++] = (base & 0xffff)|((end & 0xffff)<<16); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | pci_write_config32(dev, 0xa0, reg); |
| 245 | for(i=0;i<var_num;i++) { |
| 246 | pci_write_config32(dev, 0xa8 + i*4, reg_var[i]); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | } |
| 251 | |
| 252 | static void sis966_lpc_enable_resources(device_t dev) |
| 253 | { |
| 254 | pci_dev_enable_resources(dev); |
| 255 | sis966_lpc_enable_childrens_resources(dev); |
| 256 | } |
| 257 | |
| 258 | static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device) |
| 259 | { |
| 260 | pci_write_config32(dev, 0x40, |
| 261 | ((device & 0xffff) << 16) | (vendor & 0xffff)); |
| 262 | } |
| 263 | |
| 264 | static struct pci_operations lops_pci = { |
| 265 | .set_subsystem = lpci_set_subsystem, |
| 266 | }; |
| 267 | |
| 268 | static struct device_operations lpc_ops = { |
| 269 | .read_resources = sis966_lpc_read_resources, |
| 270 | .set_resources = pci_dev_set_resources, |
| 271 | .enable_resources = sis966_lpc_enable_resources, |
| 272 | .init = lpc_init, |
| 273 | .scan_bus = scan_static_bus, |
| 274 | // .enable = sis966_enable, |
| 275 | .ops_pci = &lops_pci, |
| 276 | }; |
Stefan Reinauer | eea66b7 | 2010-04-07 15:32:52 +0000 | [diff] [blame^] | 277 | |
Stefan Reinauer | 83b52e7 | 2007-10-30 02:17:49 +0000 | [diff] [blame] | 278 | static const struct pci_driver lpc_driver __pci_driver = { |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 279 | .ops = &lpc_ops, |
| 280 | .vendor = PCI_VENDOR_ID_SIS, |
| 281 | .device = PCI_DEVICE_ID_SIS_SIS966_LPC, |
| 282 | }; |
| 283 | |
Stefan Reinauer | eea66b7 | 2010-04-07 15:32:52 +0000 | [diff] [blame^] | 284 | #ifdef SLAVE_INIT // No device? |
Morgan Tsai | 1602dd5 | 2007-10-29 21:00:14 +0000 | [diff] [blame] | 285 | static struct device_operations lpc_slave_ops = { |
| 286 | .read_resources = sis966_lpc_read_resources, |
| 287 | .set_resources = pci_dev_set_resources, |
| 288 | .enable_resources = pci_dev_enable_resources, |
| 289 | .init = lpc_slave_init, |
| 290 | // .enable = sis966_enable, |
| 291 | .ops_pci = &lops_pci, |
| 292 | }; |
Stefan Reinauer | eea66b7 | 2010-04-07 15:32:52 +0000 | [diff] [blame^] | 293 | #endif |