blob: 940844b4421be28c0a00af7962fcbe7261333323 [file] [log] [blame]
Morgan Tsai1602dd52007-10-29 21:00:14 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Morgan Tsai1602dd52007-10-29 21:00:14 +00003 *
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.
Morgan Tsai1602dd52007-10-29 21:00:14 +000022 */
23
24#include <console/console.h>
25#include <device/device.h>
26#include <device/pci.h>
27#include <device/pnp.h>
28#include <device/pci_ids.h>
29#include <device/pci_ops.h>
30#include <pc80/mc146818rtc.h>
31#include <pc80/isa-dma.h>
Morgan Tsai1602dd52007-10-29 21:00:14 +000032#include <arch/io.h>
Stefan Reinauer0401bd82010-01-16 18:31:34 +000033#include <arch/ioapic.h>
Morgan Tsai1602dd52007-10-29 21:00:14 +000034#include <cpu/x86/lapic.h>
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +000035#include <stdlib.h>
Morgan Tsai1602dd52007-10-29 21:00:14 +000036#include "sis966.h"
37#include <pc80/keyboard.h>
38
39#define NMI_OFF 0
40
Morgan Tsai1602dd52007-10-29 21:00:14 +000041// 0x7a or e3
42#define PREVIOUS_POWER_STATE 0x7A
43
44#define MAINBOARD_POWER_OFF 0
45#define MAINBOARD_POWER_ON 1
46#define SLOW_CPU_OFF 0
47#define SLOW_CPU__ON 1
48
Stefan Reinauer08670622009-06-30 15:17:49 +000049#ifndef CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL
50#define CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
Morgan Tsai1602dd52007-10-29 21:00:14 +000051#endif
52
Stefan Reinauereea66b72010-04-07 15:32:52 +000053#undef SLAVE_INIT
54
Morgan Tsai1602dd52007-10-29 21:00:14 +000055static void lpc_common_init(device_t dev)
56{
57 uint8_t byte;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080058 void *ioapic_base;
Morgan Tsai1602dd52007-10-29 21:00:14 +000059
60 /* IO APIC initialization */
61 byte = pci_read_config8(dev, 0x74);
62 byte |= (1<<0); // enable APIC
63 pci_write_config8(dev, 0x74, byte);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080064 ioapic_base = (void *)pci_read_config32(dev, PCI_BASE_ADDRESS_1); // 0x14
Morgan Tsai1602dd52007-10-29 21:00:14 +000065
Stefan Reinauer0401bd82010-01-16 18:31:34 +000066 setup_ioapic(ioapic_base, 0); // Don't rename IO APIC ID
Morgan Tsai1602dd52007-10-29 21:00:14 +000067}
68
Stefan Reinauereea66b72010-04-07 15:32:52 +000069#ifdef SLAVE_INIT
Morgan Tsai1602dd52007-10-29 21:00:14 +000070static void lpc_slave_init(device_t dev)
71{
72 lpc_common_init(dev);
73}
Stefan Reinauereea66b72010-04-07 15:32:52 +000074#endif
Morgan Tsai1602dd52007-10-29 21:00:14 +000075
76static void lpc_usb_legacy_init(device_t dev)
77{
78 uint16_t acpi_base;
79
80 acpi_base = (pci_read_config8(dev,0x75) << 8);
Morgan Tsai1602dd52007-10-29 21:00:14 +000081
82 outb(inb(acpi_base + 0xbb) |0x80, acpi_base + 0xbb);
83 outb(inb(acpi_base + 0xba) |0x80, acpi_base + 0xba);
Morgan Tsai1602dd52007-10-29 21:00:14 +000084}
85
86static void lpc_init(device_t dev)
87{
Edward O'Callaghandef00be2014-04-30 05:01:52 +100088 uint8_t byte;
89 uint8_t byte_old;
90 int on;
91 int nmi_option;
Morgan Tsai1602dd52007-10-29 21:00:14 +000092
Edward O'Callaghandef00be2014-04-30 05:01:52 +100093 printk(BIOS_DEBUG, "LPC_INIT -------->\n");
94 pc_keyboard_init();
Morgan Tsai1602dd52007-10-29 21:00:14 +000095
Edward O'Callaghandef00be2014-04-30 05:01:52 +100096 lpc_usb_legacy_init(dev);
97 lpc_common_init(dev);
Morgan Tsai1602dd52007-10-29 21:00:14 +000098
Morgan Tsai1602dd52007-10-29 21:00:14 +000099 /* power after power fail */
100
Morgan Tsai218c2652007-11-02 16:09:58 +0000101
Stefan Reinauer08670622009-06-30 15:17:49 +0000102 on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000103 get_option(&on, "power_on_after_fail");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000104 byte = pci_read_config8(dev, PREVIOUS_POWER_STATE);
105 byte &= ~0x40;
106 if (!on) {
107 byte |= 0x40;
108 }
109 pci_write_config8(dev, PREVIOUS_POWER_STATE, byte);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000110 printk(BIOS_INFO, "set power %s after power fail\n", on?"on":"off");
Morgan Tsai218c2652007-11-02 16:09:58 +0000111
Morgan Tsai1602dd52007-10-29 21:00:14 +0000112 /* Throttle the CPU speed down for testing */
113 on = SLOW_CPU_OFF;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000114 get_option(&on, "slow_cpu");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000115 if(on) {
116 uint16_t pm10_bar;
117 uint32_t dword;
118 pm10_bar = (pci_read_config16(dev, 0x60)&0xff00);
119 outl(((on<<1)+0x10) ,(pm10_bar + 0x10));
120 dword = inl(pm10_bar + 0x10);
121 on = 8-on;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000122 printk(BIOS_DEBUG, "Throttling CPU %2d.%1.1d percent.\n",
Morgan Tsai1602dd52007-10-29 21:00:14 +0000123 (on*12)+(on>>1),(on&1)*5);
124 }
Morgan Tsai1602dd52007-10-29 21:00:14 +0000125
Morgan Tsai218c2652007-11-02 16:09:58 +0000126 /* Enable Error reporting */
127 /* Set up sync flood detected */
128 byte = pci_read_config8(dev, 0x47);
129 byte |= (1 << 1);
130 pci_write_config8(dev, 0x47, byte);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000131
Morgan Tsai218c2652007-11-02 16:09:58 +0000132 /* Set up NMI on errors */
133 byte = inb(0x70); // RTC70
134 byte_old = byte;
135 nmi_option = NMI_OFF;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000136 get_option(&nmi_option, "nmi");
Morgan Tsai218c2652007-11-02 16:09:58 +0000137 if (nmi_option) {
138 byte &= ~(1 << 7); /* set NMI */
139 } else {
140 byte |= ( 1 << 7); // Can not mask NMI from PCI-E and NMI_NOW
141 }
142 if( byte != byte_old) {
Ed Swierke42e1422009-07-10 15:05:35 +0000143 outb(byte, 0x70);
Morgan Tsai218c2652007-11-02 16:09:58 +0000144 }
Morgan Tsai1602dd52007-10-29 21:00:14 +0000145
Morgan Tsai218c2652007-11-02 16:09:58 +0000146 /* Initialize the real time clock */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700147 cmos_init(0);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000148
Morgan Tsai218c2652007-11-02 16:09:58 +0000149 /* Initialize isa dma */
150 isa_dma_init();
Morgan Tsai1602dd52007-10-29 21:00:14 +0000151
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000152 printk(BIOS_DEBUG, "LPC_INIT <--------\n");
Morgan Tsai1602dd52007-10-29 21:00:14 +0000153}
154
155static void sis966_lpc_read_resources(device_t dev)
156{
157 struct resource *res;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000158
159 /* Get the normal pci resources of this device */
160 pci_dev_read_resources(dev); // We got one for APIC, or one more for TRAP
161
Myles Watson29cc9ed2009-07-02 18:56:24 +0000162 /* Add an extra subtractive resource for both memory and I/O. */
Morgan Tsai1602dd52007-10-29 21:00:14 +0000163 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000164 res->base = 0;
165 res->size = 0x1000;
166 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
167 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000168
169 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000170 res->base = 0xff800000;
171 res->size = 0x00800000; /* 8 MB for flash */
172 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
173 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000174
Myles Watson29cc9ed2009-07-02 18:56:24 +0000175 res = new_resource(dev, 3); /* IOAPIC */
Uwe Hermann74d1a6e2010-10-12 17:34:08 +0000176 res->base = IO_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000177 res->size = 0x00001000;
178 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000179}
180
181/**
Uwe Hermannb69cb5a2010-10-26 22:46:43 +0000182 * Enable resources for children devices.
Morgan Tsai1602dd52007-10-29 21:00:14 +0000183 *
Martin Roth226db052014-12-09 13:51:49 -0700184 * @param dev The device whose children's resources are to be enabled.
Morgan Tsai1602dd52007-10-29 21:00:14 +0000185 */
186static void sis966_lpc_enable_childrens_resources(device_t dev)
187{
Myles Watson894a3472010-06-09 22:41:35 +0000188 struct bus *link;
Morgan Tsai1602dd52007-10-29 21:00:14 +0000189 uint32_t reg, reg_var[4];
190 int i;
191 int var_num = 0;
192
193 reg = pci_read_config32(dev, 0xa0);
194
Myles Watson894a3472010-06-09 22:41:35 +0000195 for (link = dev->link_list; link; link = link->next) {
Morgan Tsai1602dd52007-10-29 21:00:14 +0000196 device_t child;
Myles Watson894a3472010-06-09 22:41:35 +0000197 for (child = link->children; child; child = child->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000198 if(child->enabled && (child->path.type == DEVICE_PATH_PNP)) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000199 struct resource *res;
200 for(res = child->resource_list; res; res = res->next) {
Morgan Tsai1602dd52007-10-29 21:00:14 +0000201 unsigned long base, end; // don't need long long
Morgan Tsai1602dd52007-10-29 21:00:14 +0000202 if(!(res->flags & IORESOURCE_IO)) continue;
203 base = res->base;
204 end = resource_end(res);
Myles Watson08e0fb82010-03-22 16:33:25 +0000205 printk(BIOS_DEBUG, "sis966 lpc decode:%s, base=0x%08lx, end=0x%08lx\n",dev_path(child),base, end);
Morgan Tsai1602dd52007-10-29 21:00:14 +0000206 switch(base) {
207 case 0x3f8: // COM1
208 reg |= (1<<0); break;
209 case 0x2f8: // COM2
210 reg |= (1<<1); break;
Martin Roth226db052014-12-09 13:51:49 -0700211 case 0x378: // Parallel 1
Morgan Tsai1602dd52007-10-29 21:00:14 +0000212 reg |= (1<<24); break;
213 case 0x3f0: // FD0
214 reg |= (1<<20); break;
Martin Roth226db052014-12-09 13:51:49 -0700215 case 0x220: // Audio 0
Morgan Tsai1602dd52007-10-29 21:00:14 +0000216 reg |= (1<<8); break;
217 case 0x300: // Midi 0
218 reg |= (1<<12); break;
219 }
220 if( (base == 0x290) || (base >= 0x400)) {
221 if(var_num>=4) continue; // only 4 var ; compact them ?
222 reg |= (1<<(28+var_num));
223 reg_var[var_num++] = (base & 0xffff)|((end & 0xffff)<<16);
224 }
225 }
226 }
227 }
228 }
229 pci_write_config32(dev, 0xa0, reg);
230 for(i=0;i<var_num;i++) {
231 pci_write_config32(dev, 0xa8 + i*4, reg_var[i]);
232 }
233
234
235}
236
237static void sis966_lpc_enable_resources(device_t dev)
238{
239 pci_dev_enable_resources(dev);
240 sis966_lpc_enable_childrens_resources(dev);
241}
242
243static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
244{
245 pci_write_config32(dev, 0x40,
246 ((device & 0xffff) << 16) | (vendor & 0xffff));
247}
248
249static struct pci_operations lops_pci = {
250 .set_subsystem = lpci_set_subsystem,
251};
252
253static struct device_operations lpc_ops = {
254 .read_resources = sis966_lpc_read_resources,
255 .set_resources = pci_dev_set_resources,
256 .enable_resources = sis966_lpc_enable_resources,
257 .init = lpc_init,
Kyösti Mälkkid0e212c2015-02-26 20:47:47 +0200258 .scan_bus = scan_lpc_bus,
Morgan Tsai1602dd52007-10-29 21:00:14 +0000259// .enable = sis966_enable,
260 .ops_pci = &lops_pci,
261};
Stefan Reinauereea66b72010-04-07 15:32:52 +0000262
Stefan Reinauer83b52e72007-10-30 02:17:49 +0000263static const struct pci_driver lpc_driver __pci_driver = {
Morgan Tsai1602dd52007-10-29 21:00:14 +0000264 .ops = &lpc_ops,
265 .vendor = PCI_VENDOR_ID_SIS,
266 .device = PCI_DEVICE_ID_SIS_SIS966_LPC,
267};
268
Stefan Reinauer14e22772010-04-27 06:56:47 +0000269#ifdef SLAVE_INIT // No device?
Morgan Tsai1602dd52007-10-29 21:00:14 +0000270static struct device_operations lpc_slave_ops = {
271 .read_resources = sis966_lpc_read_resources,
272 .set_resources = pci_dev_set_resources,
273 .enable_resources = pci_dev_enable_resources,
274 .init = lpc_slave_init,
275// .enable = sis966_enable,
276 .ops_pci = &lops_pci,
277};
Stefan Reinauereea66b72010-04-07 15:32:52 +0000278#endif