blob: 0bba26a82fba38d2ac2402406f869bd53c852122 [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);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000056 printk(BIOS_DEBUG, "IOAPIC Southbridge enabled %x\n", reg32);
Joseph Smith48f3e2b2010-03-17 03:37:18 +000057
58 *ioapic_index = 0;
59 *ioapic_data = (1 << 25);
60
61 *ioapic_index = 0;
62 reg32 = *ioapic_data;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000063 printk(BIOS_DEBUG, "Southbridge APIC ID = %x\n", reg32);
Joseph Smith48f3e2b2010-03-17 03:37:18 +000064 if (reg32 != (1 << 25))
65 die("APIC Error\n");
66
Joseph Smith48f3e2b2010-03-17 03:37:18 +000067 *ioapic_index = 3; /* Select Boot Configuration register. */
68 *ioapic_data = 1; /* Use Processor System Bus to deliver interrupts. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +000069}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000070
Joseph Smith48f3e2b2010-03-17 03:37:18 +000071static void i82801dx_enable_serial_irqs(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000072{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000073 /* Set packet length and toggle silent mode bit. */
Stefan Reinauer8702ab52010-03-14 17:01:08 +000074 pci_write_config8(dev, SERIRQ_CNTL,
75 (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
Joseph Smith48f3e2b2010-03-17 03:37:18 +000076 pci_write_config8(dev, SERIRQ_CNTL,
77 (1 << 7) | (0 << 6) | ((21 - 17) << 2) | (0 << 0));
Ronald G. Minnich182615d2004-08-24 16:20:46 +000078}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000079
Joseph Smith48f3e2b2010-03-17 03:37:18 +000080static void i82801dx_pirq_init(device_t dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000081{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000082 /* Get the chip configuration */
83 config_t *config = dev->chip_info;
84
85 pci_write_config8(dev, PIRQA_ROUT, config->pirqa_routing);
86 pci_write_config8(dev, PIRQB_ROUT, config->pirqb_routing);
87 pci_write_config8(dev, PIRQC_ROUT, config->pirqc_routing);
88 pci_write_config8(dev, PIRQD_ROUT, config->pirqd_routing);
89 pci_write_config8(dev, PIRQE_ROUT, config->pirqe_routing);
90 pci_write_config8(dev, PIRQF_ROUT, config->pirqf_routing);
91 pci_write_config8(dev, PIRQG_ROUT, config->pirqg_routing);
92 pci_write_config8(dev, PIRQH_ROUT, config->pirqh_routing);
Ronald G. Minnich182615d2004-08-24 16:20:46 +000093}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000094
Joseph Smith48f3e2b2010-03-17 03:37:18 +000095static void i82801dx_power_options(device_t dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000096{
Joseph Smithb5466b02010-03-22 23:10:53 +000097 u8 reg8;
98 u16 reg16, pmbase;
99 u32 reg32;
100 const char *state;
101
102 int pwr_on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000103 int nmi_option;
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000104
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000105 /* Which state do we want to goto after g3 (power restored)?
106 * 0 == S0 Full On
107 * 1 == S5 Soft Off
Joseph Smithb5466b02010-03-22 23:10:53 +0000108 *
109 * If the option is not existent (Laptops), use MAINBOARD_POWER_ON.
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000110 */
Joseph Smithb5466b02010-03-22 23:10:53 +0000111 if (get_option(&pwr_on, "power_on_after_fail") < 0)
112 pwr_on = MAINBOARD_POWER_ON;
113
114 reg8 = pci_read_config8(dev, GEN_PMCON_3);
115 reg8 &= 0xfe;
116 switch (pwr_on) {
117 case MAINBOARD_POWER_OFF:
118 reg8 |= 1;
119 state = "off";
120 break;
121 case MAINBOARD_POWER_ON:
122 reg8 &= ~1;
123 state = "on";
124 break;
125 case MAINBOARD_POWER_KEEP:
126 reg8 &= ~1;
127 state = "state keep";
128 break;
129 default:
130 state = "undefined";
131 }
132
133 reg8 &= ~(1 << 3); /* minimum asssertion is 1 to 2 RTCCLK */
134
135 pci_write_config8(dev, GEN_PMCON_3, reg8);
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000136 printk(BIOS_INFO, "Set power %s after power failure.\n", state);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000137
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000138 /* Set up NMI on errors. */
Joseph Smithb5466b02010-03-22 23:10:53 +0000139 reg8 = inb(0x61);
140 reg8 &= 0x0f; /* Higher Nibble must be 0 */
141 reg8 &= ~(1 << 3); /* IOCHK# NMI Enable */
142 // reg8 &= ~(1 << 2); /* PCI SERR# Enable */
143 reg8 |= (1 << 2); /* PCI SERR# Disable for now */
144 outb(reg8, 0x61);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000145
Joseph Smithb5466b02010-03-22 23:10:53 +0000146 reg8 = inb(0x70);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000147 nmi_option = NMI_OFF;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000148 get_option(&nmi_option, "nmi");
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000149 if (nmi_option) {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000150 printk(BIOS_INFO, "NMI sources enabled.\n");
Joseph Smithb5466b02010-03-22 23:10:53 +0000151 reg8 &= ~(1 << 7); /* Set NMI. */
152 } else {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000153 printk(BIOS_INFO, "NMI sources disabled.\n");
Joseph Smithb5466b02010-03-22 23:10:53 +0000154 reg8 |= ( 1 << 7); /* Disable NMI. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000155 }
Joseph Smithb5466b02010-03-22 23:10:53 +0000156 outb(reg8, 0x70);
157
158 /* Set SMI# rate down and enable CPU_SLP# */
159 reg16 = pci_read_config16(dev, GEN_PMCON_1);
160 reg16 &= ~(3 << 0); // SMI# rate 1 minute
161 reg16 |= (1 << 5); // CPUSLP_EN Desktop only
162 pci_write_config16(dev, GEN_PMCON_1, reg16);
163
164 pmbase = pci_read_config16(dev, 0x40) & 0xfffe;
165
166 /* Set up power management block and determine sleep mode */
167 reg32 = inl(pmbase + 0x04); // PM1_CNT
168
169 reg32 &= ~(7 << 10); // SLP_TYP
170 reg32 |= (1 << 0); // SCI_EN
171 outl(reg32, pmbase + 0x04);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000172}
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000173
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000174static void gpio_init(device_t dev)
175{
176 /* This should be done in romstage.c already */
177 pci_write_config32(dev, GPIO_BASE, (GPIOBASE_ADDR | 1));
178 pci_write_config8(dev, GPIO_CNTL, 0x10);
179}
180
181static void i82801dx_rtc_init(struct device *dev)
182{
183 u8 reg8;
184 u32 reg32;
185 int rtc_failed;
186
187 reg8 = pci_read_config8(dev, GEN_PMCON_3);
188 rtc_failed = reg8 & RTC_BATTERY_DEAD;
189 if (rtc_failed) {
190 reg8 &= ~(1 << 1); /* Preserve the power fail state. */
191 pci_write_config8(dev, GEN_PMCON_3, reg8);
192 }
193 reg32 = pci_read_config32(dev, GEN_STS);
194 rtc_failed |= reg32 & (1 << 2);
195 rtc_init(rtc_failed);
196
197 /* Enable access to the upper 128 byte bank of CMOS RAM. */
198 pci_write_config8(dev, RTC_CONF, 0x04);
199}
200
201static void i82801dx_lpc_route_dma(struct device *dev, u8 mask)
202{
203 u16 reg16;
204 int i;
205
206 reg16 = pci_read_config16(dev, PCI_DMA_CFG);
207 reg16 &= 0x300;
208 for (i = 0; i < 8; i++) {
209 if (i == 4)
210 continue;
211 reg16 |= ((mask & (1 << i)) ? 3 : 1) << (i * 2);
212 }
213 pci_write_config16(dev, PCI_DMA_CFG, reg16);
214}
215
216static void i82801dx_lpc_decode_en(device_t dev)
217{
218 /* Decode 0x3F8-0x3FF (COM1) for COMA port, 0x2F8-0x2FF (COM2) for COMB.
219 * LPT decode defaults to 0x378-0x37F and 0x778-0x77F.
220 * Floppy decode defaults to 0x3F0-0x3F5, 0x3F7.
221 * We also need to set the value for LPC I/F Enables Register.
222 */
223 pci_write_config8(dev, COM_DEC, 0x10);
224 pci_write_config16(dev, LPC_EN, 0x300F);
225}
226
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000227/* ICH4 does not mention HPET in the docs, but
228 * all ICH3 and ICH4 do have HPETs built in.
229 */
230static void enable_hpet(struct device *dev)
231{
Joseph Smithb5466b02010-03-22 23:10:53 +0000232 u32 reg32, hpet, val;
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000233
Joseph Smithb5466b02010-03-22 23:10:53 +0000234 /* Set HPET base address and enable it */
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000235 printk(BIOS_DEBUG, "Enabling HPET at 0x%x\n", HPET_ADDR);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000236 reg32 = pci_read_config32(dev, GEN_CNTL);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000237 /*
Joseph Smithb5466b02010-03-22 23:10:53 +0000238 * Bit 17 is HPET enable bit.
239 * Bit 16:15 control the HPET base address.
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000240 */
241 reg32 &= ~(3 << 15); /* Clear it */
Joseph Smithb5466b02010-03-22 23:10:53 +0000242
243 hpet = HPET_ADDR >> 12;
244 hpet &= 0x3;
245
246 reg32 |= (hpet << 15);
247 reg32 |= (1 << 17); /* Enable HPET. */
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000248 pci_write_config32(dev, GEN_CNTL, reg32);
249
Joseph Smithb5466b02010-03-22 23:10:53 +0000250 /* Check to see whether it took */
251 reg32 = pci_read_config32(dev, GEN_CNTL);
252 val = reg32 >> 15;
253 val &= 0x7;
254
255 if ((val & 0x4) && (hpet == (val & 0x3))) {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000256 printk(BIOS_INFO, "HPET enabled at 0x%x\n", HPET_ADDR);
Joseph Smithb5466b02010-03-22 23:10:53 +0000257 } else {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000258 printk(BIOS_WARNING, "HPET was not enabled correctly\n");
Joseph Smithb5466b02010-03-22 23:10:53 +0000259 reg32 &= ~(1 << 17); /* Clear Enable */
260 pci_write_config32(dev, GEN_CNTL, reg32);
261 }
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000262}
263
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000264static void lpc_init(struct device *dev)
265{
266 /* Set the value for PCI command register. */
267 pci_write_config16(dev, PCI_COMMAND, 0x000f);
268
269 /* IO APIC initialization. */
270 i82801dx_enable_ioapic(dev);
271
272 i82801dx_enable_serial_irqs(dev);
273
274 /* Setup the PIRQ. */
275 i82801dx_pirq_init(dev);
276
277 /* Setup power options. */
278 i82801dx_power_options(dev);
279
280 /* Set the state of the GPIO lines. */
281 gpio_init(dev);
282
283 /* Initialize the real time clock. */
Stefan Reinauer138be832010-02-27 01:50:21 +0000284 i82801dx_rtc_init(dev);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000285
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000286 /* Route DMA. */
Stefan Reinauer138be832010-02-27 01:50:21 +0000287 i82801dx_lpc_route_dma(dev, 0xff);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000288
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000289 /* Initialize ISA DMA. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000290 isa_dma_init();
291
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000292 /* Setup decode ports and LPC I/F enables. */
293 i82801dx_lpc_decode_en(dev);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000294
295 /* Initialize the High Precision Event Timers */
296 enable_hpet(dev);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000297}
298
Stefan Reinauer138be832010-02-27 01:50:21 +0000299static void i82801dx_lpc_read_resources(device_t dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000300{
Eric Biederman4f9265f2004-10-22 02:33:51 +0000301 struct resource *res;
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000302
Myles Watson29cc9ed2009-07-02 18:56:24 +0000303 /* Get the normal PCI resources of this device. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000304 pci_dev_read_resources(dev);
305
Myles Watson29cc9ed2009-07-02 18:56:24 +0000306 /* Add an extra subtractive resource for both memory and I/O. */
Eric Biederman4f9265f2004-10-22 02:33:51 +0000307 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000308 res->base = 0;
309 res->size = 0x1000;
310 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000311 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Eric Biederman4f9265f2004-10-22 02:33:51 +0000312
313 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000314 res->base = 0xff800000;
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000315 res->size = 0x00800000; /* 8 MB for flash */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000316 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000317 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000318
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000319 res = new_resource(dev, 3); /* IOAPIC */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000320 res->base = 0xfec00000;
321 res->size = 0x00001000;
322 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Eric Biederman4f9265f2004-10-22 02:33:51 +0000323}
324
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000325static struct device_operations lpc_ops = {
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000326 .read_resources = i82801dx_lpc_read_resources,
327 .set_resources = pci_dev_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +0000328 .enable_resources = pci_dev_enable_resources,
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000329 .init = lpc_init,
330 .scan_bus = scan_static_bus,
331 .enable = i82801dx_enable,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000332};
333
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000334/* 82801DB/DBL */
335static const struct pci_driver lpc_driver_db __pci_driver = {
336 .ops = &lpc_ops,
337 .vendor = PCI_VENDOR_ID_INTEL,
338 .device = PCI_DEVICE_ID_INTEL_82801DB_LPC,
339};
340
341/* 82801DBM */
342static const struct pci_driver lpc_driver_dbm __pci_driver = {
343 .ops = &lpc_ops,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000344 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermanna29ec062007-11-04 03:21:37 +0000345 .device = PCI_DEVICE_ID_INTEL_82801DBM_LPC,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000346};