blob: 8f698ee53d0b2b564f79170be939232c95831003 [file] [log] [blame]
Ronald G. Minnich182615d2004-08-24 16:20:46 +00001/*
Stefan Reinauer8702ab52010-03-14 17:01:08 +00002 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2003 Linux Networx
5 * Copyright (C) 2004 SuSE Linux AG
6 * Copyright (C) 2004 Tyan Computer
Joseph Smith48f3e2b2010-03-17 03:37:18 +00007 * Copyright (C) 2010 Joseph Smith <joe@settoplinux.org>
Stefan Reinauer8702ab52010-03-14 17:01:08 +00008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; version 2 of
12 * the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Ronald G. Minnich182615d2004-08-24 16:20:46 +000022 */
Stefan Reinauer8702ab52010-03-14 17:01:08 +000023
Ronald G. Minnich182615d2004-08-24 16:20:46 +000024#include <console/console.h>
25#include <device/device.h>
26#include <device/pci.h>
27#include <device/pci_ids.h>
28#include <device/pci_ops.h>
Ronald G. Minnich182615d2004-08-24 16:20:46 +000029#include <pc80/mc146818rtc.h>
Steven J. Magnanief792232005-09-21 13:53:44 +000030#include <pc80/isa-dma.h>
31#include <arch/io.h>
Stefan Reinauer138be832010-02-27 01:50:21 +000032#include "i82801dx.h"
Ronald G. Minnich182615d2004-08-24 16:20:46 +000033
Ronald G. Minnich182615d2004-08-24 16:20:46 +000034#define NMI_OFF 0
35
Joseph Smith48f3e2b2010-03-17 03:37:18 +000036typedef struct southbridge_intel_i82801dx_config config_t;
37
38static void i82801dx_enable_ioapic(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000039{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000040 u32 reg32;
Stefan Reinauer5c32d242010-03-17 03:40:23 +000041 volatile u32 *ioapic_index = (volatile u32 *)(IO_APIC_ADDR);
42 volatile u32 *ioapic_data = (volatile u32 *)(IO_APIC_ADDR + 0x10);
Ronald G. Minnich182615d2004-08-24 16:20:46 +000043
Joseph Smith48f3e2b2010-03-17 03:37:18 +000044 /* Set ACPI base address (I/O space). */
45 pci_write_config32(dev, PMBASE, (PMBASE_ADDR | 1));
Ronald G. Minnich182615d2004-08-24 16:20:46 +000046
Joseph Smith48f3e2b2010-03-17 03:37:18 +000047 /* Enable ACPI I/O and power management. */
48 pci_write_config8(dev, ACPI_CNTL, 0x10);
49
50 reg32 = pci_read_config32(dev, GEN_CNTL);
51 reg32 |= (3 << 7); /* Enable IOAPIC */
52 reg32 |= (1 << 13); /* Coprocessor error enable */
53 reg32 |= (1 << 1); /* Delayed transaction enable */
54 reg32 |= (1 << 2); /* DMA collection buffer enable */
55 pci_write_config32(dev, GEN_CNTL, reg32);
56 printk_debug("IOAPIC Southbridge enabled %x\n", reg32);
57
58 *ioapic_index = 0;
59 *ioapic_data = (1 << 25);
60
61 *ioapic_index = 0;
62 reg32 = *ioapic_data;
63 printk_debug("Southbridge APIC ID = %x\n", reg32);
64 if (reg32 != (1 << 25))
65 die("APIC Error\n");
66
67 /* TODO: From i82801ca, needed/useful on other ICH? */
68 *ioapic_index = 3; /* Select Boot Configuration register. */
69 *ioapic_data = 1; /* Use Processor System Bus to deliver interrupts. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +000070}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000071
Joseph Smith48f3e2b2010-03-17 03:37:18 +000072static void i82801dx_enable_serial_irqs(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000073{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000074 /* Set packet length and toggle silent mode bit. */
Stefan Reinauer8702ab52010-03-14 17:01:08 +000075 pci_write_config8(dev, SERIRQ_CNTL,
76 (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
Joseph Smith48f3e2b2010-03-17 03:37:18 +000077 pci_write_config8(dev, SERIRQ_CNTL,
78 (1 << 7) | (0 << 6) | ((21 - 17) << 2) | (0 << 0));
Ronald G. Minnich182615d2004-08-24 16:20:46 +000079}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000080
Joseph Smith48f3e2b2010-03-17 03:37:18 +000081static void i82801dx_pirq_init(device_t dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000082{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000083 /* Get the chip configuration */
84 config_t *config = dev->chip_info;
85
86 pci_write_config8(dev, PIRQA_ROUT, config->pirqa_routing);
87 pci_write_config8(dev, PIRQB_ROUT, config->pirqb_routing);
88 pci_write_config8(dev, PIRQC_ROUT, config->pirqc_routing);
89 pci_write_config8(dev, PIRQD_ROUT, config->pirqd_routing);
90 pci_write_config8(dev, PIRQE_ROUT, config->pirqe_routing);
91 pci_write_config8(dev, PIRQF_ROUT, config->pirqf_routing);
92 pci_write_config8(dev, PIRQG_ROUT, config->pirqg_routing);
93 pci_write_config8(dev, PIRQH_ROUT, config->pirqh_routing);
94
Ronald G. Minnich182615d2004-08-24 16:20:46 +000095}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000096
Joseph Smith48f3e2b2010-03-17 03:37:18 +000097static void i82801dx_power_options(device_t dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000098{
Stefan Reinauer8702ab52010-03-14 17:01:08 +000099 u8 byte;
100 int pwr_on = -1;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000101 int nmi_option;
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000102
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000103 /* power after power fail */
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000104 /* FIXME this doesn't work! */
105 /* Which state do we want to goto after g3 (power restored)?
106 * 0 == S0 Full On
107 * 1 == S5 Soft Off
108 */
109 pci_write_config8(dev, GEN_PMCON_3, pwr_on ? 0 : 1);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000110 printk_info("Set power %s if power fails\n", pwr_on ? "on" : "off");
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000111
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000112 /* Set up NMI on errors. */
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000113 byte = inb(0x61);
114 byte &= ~(1 << 3); /* IOCHK# NMI Enable */
115 byte &= ~(1 << 2); /* PCI SERR# Enable */
116 outb(byte, 0x61);
117 byte = inb(0x70);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000118
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000119 nmi_option = NMI_OFF;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000120 get_option(&nmi_option, "nmi");
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000121 if (nmi_option) {
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000122 byte &= ~(1 << 7); /* Set NMI. */
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000123 outb(byte, 0x70);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000124 }
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000125}
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000126
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000127static void gpio_init(device_t dev)
128{
129 /* This should be done in romstage.c already */
130 pci_write_config32(dev, GPIO_BASE, (GPIOBASE_ADDR | 1));
131 pci_write_config8(dev, GPIO_CNTL, 0x10);
132}
133
134static void i82801dx_rtc_init(struct device *dev)
135{
136 u8 reg8;
137 u32 reg32;
138 int rtc_failed;
139
140 reg8 = pci_read_config8(dev, GEN_PMCON_3);
141 rtc_failed = reg8 & RTC_BATTERY_DEAD;
142 if (rtc_failed) {
143 reg8 &= ~(1 << 1); /* Preserve the power fail state. */
144 pci_write_config8(dev, GEN_PMCON_3, reg8);
145 }
146 reg32 = pci_read_config32(dev, GEN_STS);
147 rtc_failed |= reg32 & (1 << 2);
148 rtc_init(rtc_failed);
149
150 /* Enable access to the upper 128 byte bank of CMOS RAM. */
151 pci_write_config8(dev, RTC_CONF, 0x04);
152}
153
154static void i82801dx_lpc_route_dma(struct device *dev, u8 mask)
155{
156 u16 reg16;
157 int i;
158
159 reg16 = pci_read_config16(dev, PCI_DMA_CFG);
160 reg16 &= 0x300;
161 for (i = 0; i < 8; i++) {
162 if (i == 4)
163 continue;
164 reg16 |= ((mask & (1 << i)) ? 3 : 1) << (i * 2);
165 }
166 pci_write_config16(dev, PCI_DMA_CFG, reg16);
167}
168
169static void i82801dx_lpc_decode_en(device_t dev)
170{
171 /* Decode 0x3F8-0x3FF (COM1) for COMA port, 0x2F8-0x2FF (COM2) for COMB.
172 * LPT decode defaults to 0x378-0x37F and 0x778-0x77F.
173 * Floppy decode defaults to 0x3F0-0x3F5, 0x3F7.
174 * We also need to set the value for LPC I/F Enables Register.
175 */
176 pci_write_config8(dev, COM_DEC, 0x10);
177 pci_write_config16(dev, LPC_EN, 0x300F);
178}
179
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000180static void lpc_init(struct device *dev)
181{
182 /* Set the value for PCI command register. */
183 pci_write_config16(dev, PCI_COMMAND, 0x000f);
184
185 /* IO APIC initialization. */
186 i82801dx_enable_ioapic(dev);
187
188 i82801dx_enable_serial_irqs(dev);
189
190 /* Setup the PIRQ. */
191 i82801dx_pirq_init(dev);
192
193 /* Setup power options. */
194 i82801dx_power_options(dev);
195
196 /* Set the state of the GPIO lines. */
197 gpio_init(dev);
198
199 /* Initialize the real time clock. */
Stefan Reinauer138be832010-02-27 01:50:21 +0000200 i82801dx_rtc_init(dev);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000201
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000202 /* Route DMA. */
Stefan Reinauer138be832010-02-27 01:50:21 +0000203 i82801dx_lpc_route_dma(dev, 0xff);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000204
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000205 /* Initialize ISA DMA. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000206 isa_dma_init();
207
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000208 /* Setup decode ports and LPC I/F enables. */
209 i82801dx_lpc_decode_en(dev);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000210}
211
Stefan Reinauer138be832010-02-27 01:50:21 +0000212static void i82801dx_lpc_read_resources(device_t dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000213{
Eric Biederman4f9265f2004-10-22 02:33:51 +0000214 struct resource *res;
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000215
Myles Watson29cc9ed2009-07-02 18:56:24 +0000216 /* Get the normal PCI resources of this device. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000217 pci_dev_read_resources(dev);
218
Myles Watson29cc9ed2009-07-02 18:56:24 +0000219 /* Add an extra subtractive resource for both memory and I/O. */
Eric Biederman4f9265f2004-10-22 02:33:51 +0000220 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000221 res->base = 0;
222 res->size = 0x1000;
223 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000224 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Eric Biederman4f9265f2004-10-22 02:33:51 +0000225
226 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000227 res->base = 0xff800000;
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000228 res->size = 0x00800000; /* 8 MB for flash */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000229 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000230 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000231
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000232 res = new_resource(dev, 3); /* IOAPIC */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000233 res->base = 0xfec00000;
234 res->size = 0x00001000;
235 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Eric Biederman4f9265f2004-10-22 02:33:51 +0000236}
237
Stefan Reinauer138be832010-02-27 01:50:21 +0000238static void i82801dx_lpc_enable_resources(device_t dev)
Eric Biederman4f9265f2004-10-22 02:33:51 +0000239{
240 pci_dev_enable_resources(dev);
241 enable_childrens_resources(dev);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000242}
243
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000244static struct device_operations lpc_ops = {
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000245 .read_resources = i82801dx_lpc_read_resources,
246 .set_resources = pci_dev_set_resources,
247 .enable_resources = i82801dx_lpc_enable_resources,
248 .init = lpc_init,
249 .scan_bus = scan_static_bus,
250 .enable = i82801dx_enable,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000251};
252
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000253/* 82801DB/DBL */
254static const struct pci_driver lpc_driver_db __pci_driver = {
255 .ops = &lpc_ops,
256 .vendor = PCI_VENDOR_ID_INTEL,
257 .device = PCI_DEVICE_ID_INTEL_82801DB_LPC,
258};
259
260/* 82801DBM */
261static const struct pci_driver lpc_driver_dbm __pci_driver = {
262 .ops = &lpc_ops,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000263 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermanna29ec062007-11-04 03:21:37 +0000264 .device = PCI_DEVICE_ID_INTEL_82801DBM_LPC,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000265};