blob: b7eba17da666a1a1249cfd79ef85c9cdb08d4808 [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 *
Stefan Reinauer8702ab52010-03-14 17:01:08 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; version 2 of
8 * the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Ronald G. Minnich182615d2004-08-24 16:20:46 +000014 */
Stefan Reinauer8702ab52010-03-14 17:01:08 +000015
Ronald G. Minnich182615d2004-08-24 16:20:46 +000016#include <console/console.h>
17#include <device/device.h>
18#include <device/pci.h>
19#include <device/pci_ids.h>
20#include <device/pci_ops.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020021#include <option.h>
Ronald G. Minnich182615d2004-08-24 16:20:46 +000022#include <pc80/mc146818rtc.h>
Steven J. Magnanief792232005-09-21 13:53:44 +000023#include <pc80/isa-dma.h>
24#include <arch/io.h>
Uwe Hermann74d1a6e2010-10-12 17:34:08 +000025#include <arch/ioapic.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030026#include "chip.h"
Stefan Reinauer138be832010-02-27 01:50:21 +000027#include "i82801dx.h"
Ronald G. Minnich182615d2004-08-24 16:20:46 +000028
Ronald G. Minnich182615d2004-08-24 16:20:46 +000029#define NMI_OFF 0
30
Joseph Smith48f3e2b2010-03-17 03:37:18 +000031typedef struct southbridge_intel_i82801dx_config config_t;
32
Kyösti Mälkkie6143532013-02-26 17:24:41 +020033/**
34 * Enable ACPI I/O range.
35 *
36 * @param dev PCI device with ACPI and PM BAR's
37 */
38static void i82801dx_enable_acpi(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000039{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000040 /* Set ACPI base address (I/O space). */
41 pci_write_config32(dev, PMBASE, (PMBASE_ADDR | 1));
Ronald G. Minnich182615d2004-08-24 16:20:46 +000042
Kyösti Mälkkie6143532013-02-26 17:24:41 +020043 /* Enable ACPI I/O range decode and ACPI power management. */
44 pci_write_config8(dev, ACPI_CNTL, ACPI_EN);
45}
46
47/**
48 * Set miscellanous static southbridge features.
49 *
50 * @param dev PCI device with I/O APIC control registers
51 */
52static void i82801dx_enable_ioapic(struct device *dev)
53{
54 u32 reg32;
Joseph Smith48f3e2b2010-03-17 03:37:18 +000055
56 reg32 = pci_read_config32(dev, GEN_CNTL);
Kyösti Mälkkie6143532013-02-26 17:24:41 +020057 reg32 |= (1 << 13); /* Coprocessor error enable (COPR_ERR_EN) */
58 reg32 |= (3 << 7); /* IOAPIC enable (APIC_EN) */
59 reg32 |= (1 << 2); /* DMA collection buffer enable (DCB_EN) */
60 reg32 |= (1 << 1); /* Delayed transaction enable (DTE) */
Joseph Smith48f3e2b2010-03-17 03:37:18 +000061 pci_write_config32(dev, GEN_CNTL, reg32);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000062 printk(BIOS_DEBUG, "IOAPIC Southbridge enabled %x\n", reg32);
Joseph Smith48f3e2b2010-03-17 03:37:18 +000063
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080064 set_ioapic_id(VIO_APIC_VADDR, 0x02);
Kyösti Mälkki83512432013-06-05 07:19:31 +030065
66 /*
67 * Select Boot Configuration register (0x03) and
68 * use Processor System Bus (0x01) to deliver interrupts.
69 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080070 io_apic_write(VIO_APIC_VADDR, 0x03, 0x01);
Ronald G. Minnich182615d2004-08-24 16:20:46 +000071}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000072
Joseph Smith48f3e2b2010-03-17 03:37:18 +000073static void i82801dx_enable_serial_irqs(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000074{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000075 /* Set packet length and toggle silent mode bit. */
Stefan Reinauer8702ab52010-03-14 17:01:08 +000076 pci_write_config8(dev, SERIRQ_CNTL,
77 (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
Joseph Smith48f3e2b2010-03-17 03:37:18 +000078 pci_write_config8(dev, SERIRQ_CNTL,
79 (1 << 7) | (0 << 6) | ((21 - 17) << 2) | (0 << 0));
Ronald G. Minnich182615d2004-08-24 16:20:46 +000080}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000081
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +020082static void i82801dx_pirq_init(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000083{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000084 /* Get the chip configuration */
85 config_t *config = dev->chip_info;
86
87 pci_write_config8(dev, PIRQA_ROUT, config->pirqa_routing);
88 pci_write_config8(dev, PIRQB_ROUT, config->pirqb_routing);
89 pci_write_config8(dev, PIRQC_ROUT, config->pirqc_routing);
90 pci_write_config8(dev, PIRQD_ROUT, config->pirqd_routing);
91 pci_write_config8(dev, PIRQE_ROUT, config->pirqe_routing);
92 pci_write_config8(dev, PIRQF_ROUT, config->pirqf_routing);
93 pci_write_config8(dev, PIRQG_ROUT, config->pirqg_routing);
94 pci_write_config8(dev, PIRQH_ROUT, config->pirqh_routing);
Ronald G. Minnich182615d2004-08-24 16:20:46 +000095}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000096
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +020097static void i82801dx_power_options(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000098{
Joseph Smithb5466b02010-03-22 23:10:53 +000099 u8 reg8;
100 u16 reg16, pmbase;
101 u32 reg32;
102 const char *state;
103
Nico Huber9faae2b2018-11-14 00:00:35 +0100104 int pwr_on = CONFIG_MAINBOARD_POWER_FAILURE_STATE;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000105 int nmi_option;
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000106
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000107 /* Which state do we want to goto after g3 (power restored)?
108 * 0 == S0 Full On
109 * 1 == S5 Soft Off
Joseph Smithb5466b02010-03-22 23:10:53 +0000110 *
111 * If the option is not existent (Laptops), use MAINBOARD_POWER_ON.
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000112 */
Varad Gautam06ef0462015-03-11 09:54:41 +0530113 pwr_on = MAINBOARD_POWER_ON;
114 get_option(&pwr_on, "power_on_after_fail");
Joseph Smithb5466b02010-03-22 23:10:53 +0000115
116 reg8 = pci_read_config8(dev, GEN_PMCON_3);
117 reg8 &= 0xfe;
118 switch (pwr_on) {
119 case MAINBOARD_POWER_OFF:
120 reg8 |= 1;
121 state = "off";
122 break;
123 case MAINBOARD_POWER_ON:
124 reg8 &= ~1;
125 state = "on";
126 break;
127 case MAINBOARD_POWER_KEEP:
128 reg8 &= ~1;
129 state = "state keep";
130 break;
131 default:
132 state = "undefined";
133 }
134
135 reg8 &= ~(1 << 3); /* minimum asssertion is 1 to 2 RTCCLK */
136
137 pci_write_config8(dev, GEN_PMCON_3, reg8);
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000138 printk(BIOS_INFO, "Set power %s after power failure.\n", state);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000139
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000140 /* Set up NMI on errors. */
Joseph Smithb5466b02010-03-22 23:10:53 +0000141 reg8 = inb(0x61);
142 reg8 &= 0x0f; /* Higher Nibble must be 0 */
143 reg8 &= ~(1 << 3); /* IOCHK# NMI Enable */
144 // reg8 &= ~(1 << 2); /* PCI SERR# Enable */
145 reg8 |= (1 << 2); /* PCI SERR# Disable for now */
146 outb(reg8, 0x61);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000147
Joseph Smithb5466b02010-03-22 23:10:53 +0000148 reg8 = inb(0x70);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000149 nmi_option = NMI_OFF;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000150 get_option(&nmi_option, "nmi");
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000151 if (nmi_option) {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000152 printk(BIOS_INFO, "NMI sources enabled.\n");
Joseph Smithb5466b02010-03-22 23:10:53 +0000153 reg8 &= ~(1 << 7); /* Set NMI. */
154 } else {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000155 printk(BIOS_INFO, "NMI sources disabled.\n");
Elyes HAOUAS9c5d4632018-04-26 22:21:21 +0200156 reg8 |= (1 << 7); /* Disable NMI. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000157 }
Joseph Smithb5466b02010-03-22 23:10:53 +0000158 outb(reg8, 0x70);
159
160 /* Set SMI# rate down and enable CPU_SLP# */
161 reg16 = pci_read_config16(dev, GEN_PMCON_1);
162 reg16 &= ~(3 << 0); // SMI# rate 1 minute
163 reg16 |= (1 << 5); // CPUSLP_EN Desktop only
164 pci_write_config16(dev, GEN_PMCON_1, reg16);
165
166 pmbase = pci_read_config16(dev, 0x40) & 0xfffe;
167
168 /* Set up power management block and determine sleep mode */
169 reg32 = inl(pmbase + 0x04); // PM1_CNT
170
171 reg32 &= ~(7 << 10); // SLP_TYP
172 reg32 |= (1 << 0); // SCI_EN
173 outl(reg32, pmbase + 0x04);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000174}
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000175
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +0200176static void gpio_init(struct device *dev)
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000177{
178 /* This should be done in romstage.c already */
179 pci_write_config32(dev, GPIO_BASE, (GPIOBASE_ADDR | 1));
180 pci_write_config8(dev, GPIO_CNTL, 0x10);
181}
182
183static void i82801dx_rtc_init(struct device *dev)
184{
185 u8 reg8;
186 u32 reg32;
187 int rtc_failed;
188
189 reg8 = pci_read_config8(dev, GEN_PMCON_3);
190 rtc_failed = reg8 & RTC_BATTERY_DEAD;
191 if (rtc_failed) {
192 reg8 &= ~(1 << 1); /* Preserve the power fail state. */
193 pci_write_config8(dev, GEN_PMCON_3, reg8);
194 }
195 reg32 = pci_read_config32(dev, GEN_STS);
196 rtc_failed |= reg32 & (1 << 2);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700197 cmos_init(rtc_failed);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000198
199 /* Enable access to the upper 128 byte bank of CMOS RAM. */
200 pci_write_config8(dev, RTC_CONF, 0x04);
201}
202
203static void i82801dx_lpc_route_dma(struct device *dev, u8 mask)
204{
205 u16 reg16;
206 int i;
207
208 reg16 = pci_read_config16(dev, PCI_DMA_CFG);
209 reg16 &= 0x300;
210 for (i = 0; i < 8; i++) {
211 if (i == 4)
212 continue;
213 reg16 |= ((mask & (1 << i)) ? 3 : 1) << (i * 2);
214 }
215 pci_write_config16(dev, PCI_DMA_CFG, reg16);
216}
217
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +0200218static void i82801dx_lpc_decode_en(struct device *dev)
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000219{
220 /* Decode 0x3F8-0x3FF (COM1) for COMA port, 0x2F8-0x2FF (COM2) for COMB.
221 * LPT decode defaults to 0x378-0x37F and 0x778-0x77F.
222 * Floppy decode defaults to 0x3F0-0x3F5, 0x3F7.
223 * We also need to set the value for LPC I/F Enables Register.
224 */
225 pci_write_config8(dev, COM_DEC, 0x10);
226 pci_write_config16(dev, LPC_EN, 0x300F);
227}
228
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000229/* ICH4 does not mention HPET in the docs, but
230 * all ICH3 and ICH4 do have HPETs built in.
231 */
232static void enable_hpet(struct device *dev)
233{
Joseph Smithb5466b02010-03-22 23:10:53 +0000234 u32 reg32, hpet, val;
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000235
Joseph Smithb5466b02010-03-22 23:10:53 +0000236 /* Set HPET base address and enable it */
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200237 printk(BIOS_DEBUG, "Enabling HPET at 0x%x\n", CONFIG_HPET_ADDRESS);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000238 reg32 = pci_read_config32(dev, GEN_CNTL);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000239 /*
Joseph Smithb5466b02010-03-22 23:10:53 +0000240 * Bit 17 is HPET enable bit.
241 * Bit 16:15 control the HPET base address.
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000242 */
243 reg32 &= ~(3 << 15); /* Clear it */
Joseph Smithb5466b02010-03-22 23:10:53 +0000244
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200245 hpet = CONFIG_HPET_ADDRESS >> 12;
Joseph Smithb5466b02010-03-22 23:10:53 +0000246 hpet &= 0x3;
247
248 reg32 |= (hpet << 15);
249 reg32 |= (1 << 17); /* Enable HPET. */
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000250 pci_write_config32(dev, GEN_CNTL, reg32);
251
Joseph Smithb5466b02010-03-22 23:10:53 +0000252 /* Check to see whether it took */
253 reg32 = pci_read_config32(dev, GEN_CNTL);
254 val = reg32 >> 15;
255 val &= 0x7;
256
257 if ((val & 0x4) && (hpet == (val & 0x3))) {
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200258 printk(BIOS_INFO, "HPET enabled at 0x%x\n", CONFIG_HPET_ADDRESS);
Joseph Smithb5466b02010-03-22 23:10:53 +0000259 } else {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000260 printk(BIOS_WARNING, "HPET was not enabled correctly\n");
Joseph Smithb5466b02010-03-22 23:10:53 +0000261 reg32 &= ~(1 << 17); /* Clear Enable */
262 pci_write_config32(dev, GEN_CNTL, reg32);
263 }
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000264}
265
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000266static void lpc_init(struct device *dev)
267{
268 /* Set the value for PCI command register. */
269 pci_write_config16(dev, PCI_COMMAND, 0x000f);
270
Kyösti Mälkkie6143532013-02-26 17:24:41 +0200271 i82801dx_enable_acpi(dev);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000272 /* IO APIC initialization. */
273 i82801dx_enable_ioapic(dev);
274
275 i82801dx_enable_serial_irqs(dev);
276
277 /* Setup the PIRQ. */
278 i82801dx_pirq_init(dev);
279
280 /* Setup power options. */
281 i82801dx_power_options(dev);
282
283 /* Set the state of the GPIO lines. */
284 gpio_init(dev);
285
286 /* Initialize the real time clock. */
Stefan Reinauer138be832010-02-27 01:50:21 +0000287 i82801dx_rtc_init(dev);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000288
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000289 /* Route DMA. */
Stefan Reinauer138be832010-02-27 01:50:21 +0000290 i82801dx_lpc_route_dma(dev, 0xff);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000291
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000292 /* Initialize ISA DMA. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000293 isa_dma_init();
294
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000295 /* Setup decode ports and LPC I/F enables. */
296 i82801dx_lpc_decode_en(dev);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000297
298 /* Initialize the High Precision Event Timers */
299 enable_hpet(dev);
Kyösti Mälkki55b72632019-07-08 22:36:38 +0300300
301 /* Don't allow evil boot loaders, kernels, or
302 * userspace applications to deceive us:
303 */
Kyösti Mälkkicd0b67b2019-10-09 07:52:40 +0300304 if (CONFIG(HAVE_SMI_HANDLER) && !CONFIG(PARALLEL_MP))
Kyösti Mälkki55b72632019-07-08 22:36:38 +0300305 aseg_smm_lock();
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000306}
307
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +0200308static void i82801dx_lpc_read_resources(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000309{
Eric Biederman4f9265f2004-10-22 02:33:51 +0000310 struct resource *res;
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000311
Myles Watson29cc9ed2009-07-02 18:56:24 +0000312 /* Get the normal PCI resources of this device. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000313 pci_dev_read_resources(dev);
314
Myles Watson29cc9ed2009-07-02 18:56:24 +0000315 /* Add an extra subtractive resource for both memory and I/O. */
Eric Biederman4f9265f2004-10-22 02:33:51 +0000316 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000317 res->base = 0;
318 res->size = 0x1000;
319 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000320 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Eric Biederman4f9265f2004-10-22 02:33:51 +0000321
322 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000323 res->base = 0xff800000;
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000324 res->size = 0x00800000; /* 8 MB for flash */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000325 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000326 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000327
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000328 res = new_resource(dev, 3); /* IOAPIC */
Uwe Hermann74d1a6e2010-10-12 17:34:08 +0000329 res->base = IO_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000330 res->size = 0x00001000;
331 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Eric Biederman4f9265f2004-10-22 02:33:51 +0000332}
333
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000334static struct device_operations lpc_ops = {
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000335 .read_resources = i82801dx_lpc_read_resources,
336 .set_resources = pci_dev_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +0000337 .enable_resources = pci_dev_enable_resources,
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000338 .init = lpc_init,
Nico Huber51b75ae2019-03-14 16:02:05 +0100339 .scan_bus = scan_static_bus,
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000340 .enable = i82801dx_enable,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000341};
342
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000343/* 82801DB/DBL */
344static const struct pci_driver lpc_driver_db __pci_driver = {
345 .ops = &lpc_ops,
346 .vendor = PCI_VENDOR_ID_INTEL,
347 .device = PCI_DEVICE_ID_INTEL_82801DB_LPC,
348};
349
350/* 82801DBM */
351static const struct pci_driver lpc_driver_dbm __pci_driver = {
352 .ops = &lpc_ops,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000353 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermanna29ec062007-11-04 03:21:37 +0000354 .device = PCI_DEVICE_ID_INTEL_82801DBM_LPC,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000355};