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