blob: 46c731ac94cc269cb58cbbc3dd1b68d3b44c8c3b [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer8702ab52010-03-14 17:01:08 +00002
Felix Held972d9f22022-02-23 16:32:20 +01003#include <arch/hpet.h>
Kyösti Mälkkib1968342022-01-17 13:57:55 +02004#include <arch/io.h>
5#include <arch/ioapic.h>
Ronald G. Minnich182615d2004-08-24 16:20:46 +00006#include <console/console.h>
7#include <device/device.h>
8#include <device/pci.h>
9#include <device/pci_ids.h>
10#include <device/pci_ops.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020011#include <option.h>
Ronald G. Minnich182615d2004-08-24 16:20:46 +000012#include <pc80/mc146818rtc.h>
Steven J. Magnanief792232005-09-21 13:53:44 +000013#include <pc80/isa-dma.h>
Kyösti Mälkkib1968342022-01-17 13:57:55 +020014#include <pc80/i8259.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030015#include "chip.h"
Stefan Reinauer138be832010-02-27 01:50:21 +000016#include "i82801dx.h"
Ronald G. Minnich182615d2004-08-24 16:20:46 +000017
Ronald G. Minnich182615d2004-08-24 16:20:46 +000018#define NMI_OFF 0
19
Joseph Smith48f3e2b2010-03-17 03:37:18 +000020typedef struct southbridge_intel_i82801dx_config config_t;
21
Kyösti Mälkkie6143532013-02-26 17:24:41 +020022/**
23 * Enable ACPI I/O range.
24 *
25 * @param dev PCI device with ACPI and PM BAR's
26 */
27static void i82801dx_enable_acpi(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000028{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000029 /* Set ACPI base address (I/O space). */
30 pci_write_config32(dev, PMBASE, (PMBASE_ADDR | 1));
Ronald G. Minnich182615d2004-08-24 16:20:46 +000031
Kyösti Mälkkie6143532013-02-26 17:24:41 +020032 /* Enable ACPI I/O range decode and ACPI power management. */
33 pci_write_config8(dev, ACPI_CNTL, ACPI_EN);
34}
35
36/**
Martin Roth26f97f92021-10-01 14:53:22 -060037 * Set miscellaneous static southbridge features.
Kyösti Mälkkie6143532013-02-26 17:24:41 +020038 *
39 * @param dev PCI device with I/O APIC control registers
40 */
41static void i82801dx_enable_ioapic(struct device *dev)
42{
43 u32 reg32;
Joseph Smith48f3e2b2010-03-17 03:37:18 +000044
45 reg32 = pci_read_config32(dev, GEN_CNTL);
Kyösti Mälkkie6143532013-02-26 17:24:41 +020046 reg32 |= (1 << 13); /* Coprocessor error enable (COPR_ERR_EN) */
47 reg32 |= (3 << 7); /* IOAPIC enable (APIC_EN) */
48 reg32 |= (1 << 2); /* DMA collection buffer enable (DCB_EN) */
49 reg32 |= (1 << 1); /* Delayed transaction enable (DTE) */
Joseph Smith48f3e2b2010-03-17 03:37:18 +000050 pci_write_config32(dev, GEN_CNTL, reg32);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000051 printk(BIOS_DEBUG, "IOAPIC Southbridge enabled %x\n", reg32);
Joseph Smith48f3e2b2010-03-17 03:37:18 +000052
Kyösti Mälkki682613f2021-06-08 11:31:19 +030053 setup_ioapic(VIO_APIC_VADDR, 0x02);
Kyösti Mälkki83512432013-06-05 07:19:31 +030054
Kyösti Mälkki8c9a89d2021-06-06 08:14:57 +030055 ioapic_set_boot_config(VIO_APIC_VADDR, true);
Ronald G. Minnich182615d2004-08-24 16:20:46 +000056}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000057
Joseph Smith48f3e2b2010-03-17 03:37:18 +000058static void i82801dx_enable_serial_irqs(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000059{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000060 /* Set packet length and toggle silent mode bit. */
Stefan Reinauer8702ab52010-03-14 17:01:08 +000061 pci_write_config8(dev, SERIRQ_CNTL,
62 (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
Joseph Smith48f3e2b2010-03-17 03:37:18 +000063 pci_write_config8(dev, SERIRQ_CNTL,
64 (1 << 7) | (0 << 6) | ((21 - 17) << 2) | (0 << 0));
Ronald G. Minnich182615d2004-08-24 16:20:46 +000065}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000066
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +020067static void i82801dx_pirq_init(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000068{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000069 /* Get the chip configuration */
70 config_t *config = dev->chip_info;
71
72 pci_write_config8(dev, PIRQA_ROUT, config->pirqa_routing);
73 pci_write_config8(dev, PIRQB_ROUT, config->pirqb_routing);
74 pci_write_config8(dev, PIRQC_ROUT, config->pirqc_routing);
75 pci_write_config8(dev, PIRQD_ROUT, config->pirqd_routing);
76 pci_write_config8(dev, PIRQE_ROUT, config->pirqe_routing);
77 pci_write_config8(dev, PIRQF_ROUT, config->pirqf_routing);
78 pci_write_config8(dev, PIRQG_ROUT, config->pirqg_routing);
79 pci_write_config8(dev, PIRQH_ROUT, config->pirqh_routing);
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_power_options(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000083{
Joseph Smithb5466b02010-03-22 23:10:53 +000084 u8 reg8;
85 u16 reg16, pmbase;
86 u32 reg32;
87 const char *state;
88
Stefan Reinauer8702ab52010-03-14 17:01:08 +000089 /* Which state do we want to goto after g3 (power restored)?
90 * 0 == S0 Full On
91 * 1 == S5 Soft Off
Joseph Smithb5466b02010-03-22 23:10:53 +000092 *
93 * If the option is not existent (Laptops), use MAINBOARD_POWER_ON.
Stefan Reinauer8702ab52010-03-14 17:01:08 +000094 */
Angel Pons88dcb312021-04-26 17:10:28 +020095 const unsigned int pwr_on = get_uint_option("power_on_after_fail", MAINBOARD_POWER_ON);
Joseph Smithb5466b02010-03-22 23:10:53 +000096
97 reg8 = pci_read_config8(dev, GEN_PMCON_3);
98 reg8 &= 0xfe;
99 switch (pwr_on) {
100 case MAINBOARD_POWER_OFF:
101 reg8 |= 1;
102 state = "off";
103 break;
104 case MAINBOARD_POWER_ON:
105 reg8 &= ~1;
106 state = "on";
107 break;
108 case MAINBOARD_POWER_KEEP:
109 reg8 &= ~1;
110 state = "state keep";
111 break;
112 default:
113 state = "undefined";
114 }
115
Elyes HAOUASdc413712021-02-07 20:54:53 +0100116 reg8 &= ~(1 << 3); /* minimum assertion is 1 to 2 RTCCLK */
Joseph Smithb5466b02010-03-22 23:10:53 +0000117
118 pci_write_config8(dev, GEN_PMCON_3, reg8);
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000119 printk(BIOS_INFO, "Set power %s after power failure.\n", state);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000120
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000121 /* Set up NMI on errors. */
Joseph Smithb5466b02010-03-22 23:10:53 +0000122 reg8 = inb(0x61);
123 reg8 &= 0x0f; /* Higher Nibble must be 0 */
124 reg8 &= ~(1 << 3); /* IOCHK# NMI Enable */
125 // reg8 &= ~(1 << 2); /* PCI SERR# Enable */
126 reg8 |= (1 << 2); /* PCI SERR# Disable for now */
127 outb(reg8, 0x61);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000128
Joseph Smithb5466b02010-03-22 23:10:53 +0000129 reg8 = inb(0x70);
Angel Pons88dcb312021-04-26 17:10:28 +0200130 const unsigned int nmi_option = get_uint_option("nmi", NMI_OFF);
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000131 if (nmi_option) {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000132 printk(BIOS_INFO, "NMI sources enabled.\n");
Joseph Smithb5466b02010-03-22 23:10:53 +0000133 reg8 &= ~(1 << 7); /* Set NMI. */
134 } else {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000135 printk(BIOS_INFO, "NMI sources disabled.\n");
Elyes HAOUAS9c5d4632018-04-26 22:21:21 +0200136 reg8 |= (1 << 7); /* Disable NMI. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000137 }
Joseph Smithb5466b02010-03-22 23:10:53 +0000138 outb(reg8, 0x70);
139
140 /* Set SMI# rate down and enable CPU_SLP# */
141 reg16 = pci_read_config16(dev, GEN_PMCON_1);
142 reg16 &= ~(3 << 0); // SMI# rate 1 minute
143 reg16 |= (1 << 5); // CPUSLP_EN Desktop only
144 pci_write_config16(dev, GEN_PMCON_1, reg16);
145
146 pmbase = pci_read_config16(dev, 0x40) & 0xfffe;
147
148 /* Set up power management block and determine sleep mode */
149 reg32 = inl(pmbase + 0x04); // PM1_CNT
150
151 reg32 &= ~(7 << 10); // SLP_TYP
152 reg32 |= (1 << 0); // SCI_EN
153 outl(reg32, pmbase + 0x04);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000154}
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000155
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +0200156static void gpio_init(struct device *dev)
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000157{
158 /* This should be done in romstage.c already */
159 pci_write_config32(dev, GPIO_BASE, (GPIOBASE_ADDR | 1));
160 pci_write_config8(dev, GPIO_CNTL, 0x10);
161}
162
163static void i82801dx_rtc_init(struct device *dev)
164{
165 u8 reg8;
166 u32 reg32;
167 int rtc_failed;
168
169 reg8 = pci_read_config8(dev, GEN_PMCON_3);
170 rtc_failed = reg8 & RTC_BATTERY_DEAD;
171 if (rtc_failed) {
172 reg8 &= ~(1 << 1); /* Preserve the power fail state. */
173 pci_write_config8(dev, GEN_PMCON_3, reg8);
174 }
175 reg32 = pci_read_config32(dev, GEN_STS);
176 rtc_failed |= reg32 & (1 << 2);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700177 cmos_init(rtc_failed);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000178
179 /* Enable access to the upper 128 byte bank of CMOS RAM. */
180 pci_write_config8(dev, RTC_CONF, 0x04);
181}
182
183static void i82801dx_lpc_route_dma(struct device *dev, u8 mask)
184{
185 u16 reg16;
186 int i;
187
188 reg16 = pci_read_config16(dev, PCI_DMA_CFG);
189 reg16 &= 0x300;
190 for (i = 0; i < 8; i++) {
191 if (i == 4)
192 continue;
193 reg16 |= ((mask & (1 << i)) ? 3 : 1) << (i * 2);
194 }
195 pci_write_config16(dev, PCI_DMA_CFG, reg16);
196}
197
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +0200198static void i82801dx_lpc_decode_en(struct device *dev)
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000199{
200 /* Decode 0x3F8-0x3FF (COM1) for COMA port, 0x2F8-0x2FF (COM2) for COMB.
201 * LPT decode defaults to 0x378-0x37F and 0x778-0x77F.
202 * Floppy decode defaults to 0x3F0-0x3F5, 0x3F7.
203 * We also need to set the value for LPC I/F Enables Register.
204 */
205 pci_write_config8(dev, COM_DEC, 0x10);
206 pci_write_config16(dev, LPC_EN, 0x300F);
207}
208
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000209/* ICH4 does not mention HPET in the docs, but
210 * all ICH3 and ICH4 do have HPETs built in.
211 */
212static void enable_hpet(struct device *dev)
213{
Joseph Smithb5466b02010-03-22 23:10:53 +0000214 u32 reg32, hpet, val;
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000215
Joseph Smithb5466b02010-03-22 23:10:53 +0000216 /* Set HPET base address and enable it */
Felix Held972d9f22022-02-23 16:32:20 +0100217 printk(BIOS_DEBUG, "Enabling HPET at 0x%x\n", HPET_BASE_ADDRESS);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000218 reg32 = pci_read_config32(dev, GEN_CNTL);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000219 /*
Joseph Smithb5466b02010-03-22 23:10:53 +0000220 * Bit 17 is HPET enable bit.
221 * Bit 16:15 control the HPET base address.
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000222 */
223 reg32 &= ~(3 << 15); /* Clear it */
Joseph Smithb5466b02010-03-22 23:10:53 +0000224
Felix Held972d9f22022-02-23 16:32:20 +0100225 hpet = HPET_BASE_ADDRESS >> 12;
Joseph Smithb5466b02010-03-22 23:10:53 +0000226 hpet &= 0x3;
227
228 reg32 |= (hpet << 15);
229 reg32 |= (1 << 17); /* Enable HPET. */
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000230 pci_write_config32(dev, GEN_CNTL, reg32);
231
Joseph Smithb5466b02010-03-22 23:10:53 +0000232 /* Check to see whether it took */
233 reg32 = pci_read_config32(dev, GEN_CNTL);
234 val = reg32 >> 15;
235 val &= 0x7;
236
237 if ((val & 0x4) && (hpet == (val & 0x3))) {
Felix Held972d9f22022-02-23 16:32:20 +0100238 printk(BIOS_INFO, "HPET enabled at 0x%x\n", HPET_BASE_ADDRESS);
Joseph Smithb5466b02010-03-22 23:10:53 +0000239 } else {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000240 printk(BIOS_WARNING, "HPET was not enabled correctly\n");
Joseph Smithb5466b02010-03-22 23:10:53 +0000241 reg32 &= ~(1 << 17); /* Clear Enable */
242 pci_write_config32(dev, GEN_CNTL, reg32);
243 }
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000244}
245
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000246static void lpc_init(struct device *dev)
247{
Kyösti Mälkkie6143532013-02-26 17:24:41 +0200248 i82801dx_enable_acpi(dev);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000249 /* IO APIC initialization. */
250 i82801dx_enable_ioapic(dev);
251
252 i82801dx_enable_serial_irqs(dev);
253
254 /* Setup the PIRQ. */
255 i82801dx_pirq_init(dev);
256
257 /* Setup power options. */
258 i82801dx_power_options(dev);
259
260 /* Set the state of the GPIO lines. */
261 gpio_init(dev);
262
263 /* Initialize the real time clock. */
Stefan Reinauer138be832010-02-27 01:50:21 +0000264 i82801dx_rtc_init(dev);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000265
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000266 /* Route DMA. */
Stefan Reinauer138be832010-02-27 01:50:21 +0000267 i82801dx_lpc_route_dma(dev, 0xff);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000268
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000269 /* Initialize ISA DMA. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000270 isa_dma_init();
271
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000272 /* Setup decode ports and LPC I/F enables. */
273 i82801dx_lpc_decode_en(dev);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000274
275 /* Initialize the High Precision Event Timers */
276 enable_hpet(dev);
Kyösti Mälkki55b72632019-07-08 22:36:38 +0300277
Kyösti Mälkkib1968342022-01-17 13:57:55 +0200278 setup_i8259();
279
Kyösti Mälkki55b72632019-07-08 22:36:38 +0300280 /* Don't allow evil boot loaders, kernels, or
281 * userspace applications to deceive us:
282 */
Kyösti Mälkki894f6f82022-01-29 14:05:58 +0200283 if (CONFIG(SMM_LEGACY_ASEG))
Kyösti Mälkki55b72632019-07-08 22:36:38 +0300284 aseg_smm_lock();
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000285}
286
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +0200287static void i82801dx_lpc_read_resources(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000288{
Eric Biederman4f9265f2004-10-22 02:33:51 +0000289 struct resource *res;
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000290
Myles Watson29cc9ed2009-07-02 18:56:24 +0000291 /* Get the normal PCI resources of this device. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000292 pci_dev_read_resources(dev);
293
Myles Watson29cc9ed2009-07-02 18:56:24 +0000294 /* Add an extra subtractive resource for both memory and I/O. */
Eric Biederman4f9265f2004-10-22 02:33:51 +0000295 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000296 res->base = 0;
297 res->size = 0x1000;
298 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000299 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Eric Biederman4f9265f2004-10-22 02:33:51 +0000300
301 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000302 res->base = 0xff800000;
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000303 res->size = 0x00800000; /* 8 MB for flash */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000304 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000305 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000306
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000307 res = new_resource(dev, 3); /* IOAPIC */
Uwe Hermann74d1a6e2010-10-12 17:34:08 +0000308 res->base = IO_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000309 res->size = 0x00001000;
310 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Eric Biederman4f9265f2004-10-22 02:33:51 +0000311}
312
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000313static struct device_operations lpc_ops = {
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000314 .read_resources = i82801dx_lpc_read_resources,
315 .set_resources = pci_dev_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +0000316 .enable_resources = pci_dev_enable_resources,
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000317 .init = lpc_init,
Nico Huber51b75ae2019-03-14 16:02:05 +0100318 .scan_bus = scan_static_bus,
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000319 .enable = i82801dx_enable,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000320};
321
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000322/* 82801DB/DBL */
323static const struct pci_driver lpc_driver_db __pci_driver = {
324 .ops = &lpc_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100325 .vendor = PCI_VID_INTEL,
326 .device = PCI_DID_INTEL_82801DB_LPC,
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000327};
328
329/* 82801DBM */
330static const struct pci_driver lpc_driver_dbm __pci_driver = {
331 .ops = &lpc_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100332 .vendor = PCI_VID_INTEL,
333 .device = PCI_DID_INTEL_82801DBM_LPC,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000334};