blob: 7c9424c97c0b2ad529dc10d9254ad027aaa8c159 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauer8702ab52010-03-14 17:01:08 +00003
Ronald G. Minnich182615d2004-08-24 16:20:46 +00004#include <console/console.h>
5#include <device/device.h>
6#include <device/pci.h>
7#include <device/pci_ids.h>
8#include <device/pci_ops.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +02009#include <option.h>
Ronald G. Minnich182615d2004-08-24 16:20:46 +000010#include <pc80/mc146818rtc.h>
Steven J. Magnanief792232005-09-21 13:53:44 +000011#include <pc80/isa-dma.h>
12#include <arch/io.h>
Uwe Hermann74d1a6e2010-10-12 17:34:08 +000013#include <arch/ioapic.h>
Kyösti Mälkki12b121c2019-08-18 16:33:39 +030014#include "chip.h"
Stefan Reinauer138be832010-02-27 01:50:21 +000015#include "i82801dx.h"
Ronald G. Minnich182615d2004-08-24 16:20:46 +000016
Ronald G. Minnich182615d2004-08-24 16:20:46 +000017#define NMI_OFF 0
18
Joseph Smith48f3e2b2010-03-17 03:37:18 +000019typedef struct southbridge_intel_i82801dx_config config_t;
20
Kyösti Mälkkie6143532013-02-26 17:24:41 +020021/**
22 * Enable ACPI I/O range.
23 *
24 * @param dev PCI device with ACPI and PM BAR's
25 */
26static void i82801dx_enable_acpi(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000027{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000028 /* Set ACPI base address (I/O space). */
29 pci_write_config32(dev, PMBASE, (PMBASE_ADDR | 1));
Ronald G. Minnich182615d2004-08-24 16:20:46 +000030
Kyösti Mälkkie6143532013-02-26 17:24:41 +020031 /* Enable ACPI I/O range decode and ACPI power management. */
32 pci_write_config8(dev, ACPI_CNTL, ACPI_EN);
33}
34
35/**
36 * Set miscellanous static southbridge features.
37 *
38 * @param dev PCI device with I/O APIC control registers
39 */
40static void i82801dx_enable_ioapic(struct device *dev)
41{
42 u32 reg32;
Joseph Smith48f3e2b2010-03-17 03:37:18 +000043
44 reg32 = pci_read_config32(dev, GEN_CNTL);
Kyösti Mälkkie6143532013-02-26 17:24:41 +020045 reg32 |= (1 << 13); /* Coprocessor error enable (COPR_ERR_EN) */
46 reg32 |= (3 << 7); /* IOAPIC enable (APIC_EN) */
47 reg32 |= (1 << 2); /* DMA collection buffer enable (DCB_EN) */
48 reg32 |= (1 << 1); /* Delayed transaction enable (DTE) */
Joseph Smith48f3e2b2010-03-17 03:37:18 +000049 pci_write_config32(dev, GEN_CNTL, reg32);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000050 printk(BIOS_DEBUG, "IOAPIC Southbridge enabled %x\n", reg32);
Joseph Smith48f3e2b2010-03-17 03:37:18 +000051
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080052 set_ioapic_id(VIO_APIC_VADDR, 0x02);
Kyösti Mälkki83512432013-06-05 07:19:31 +030053
54 /*
55 * Select Boot Configuration register (0x03) and
56 * use Processor System Bus (0x01) to deliver interrupts.
57 */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080058 io_apic_write(VIO_APIC_VADDR, 0x03, 0x01);
Ronald G. Minnich182615d2004-08-24 16:20:46 +000059}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000060
Joseph Smith48f3e2b2010-03-17 03:37:18 +000061static void i82801dx_enable_serial_irqs(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000062{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000063 /* Set packet length and toggle silent mode bit. */
Stefan Reinauer8702ab52010-03-14 17:01:08 +000064 pci_write_config8(dev, SERIRQ_CNTL,
65 (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
Joseph Smith48f3e2b2010-03-17 03:37:18 +000066 pci_write_config8(dev, SERIRQ_CNTL,
67 (1 << 7) | (0 << 6) | ((21 - 17) << 2) | (0 << 0));
Ronald G. Minnich182615d2004-08-24 16:20:46 +000068}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000069
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +020070static void i82801dx_pirq_init(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000071{
Joseph Smith48f3e2b2010-03-17 03:37:18 +000072 /* Get the chip configuration */
73 config_t *config = dev->chip_info;
74
75 pci_write_config8(dev, PIRQA_ROUT, config->pirqa_routing);
76 pci_write_config8(dev, PIRQB_ROUT, config->pirqb_routing);
77 pci_write_config8(dev, PIRQC_ROUT, config->pirqc_routing);
78 pci_write_config8(dev, PIRQD_ROUT, config->pirqd_routing);
79 pci_write_config8(dev, PIRQE_ROUT, config->pirqe_routing);
80 pci_write_config8(dev, PIRQF_ROUT, config->pirqf_routing);
81 pci_write_config8(dev, PIRQG_ROUT, config->pirqg_routing);
82 pci_write_config8(dev, PIRQH_ROUT, config->pirqh_routing);
Ronald G. Minnich182615d2004-08-24 16:20:46 +000083}
Stefan Reinauer8702ab52010-03-14 17:01:08 +000084
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +020085static void i82801dx_power_options(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +000086{
Joseph Smithb5466b02010-03-22 23:10:53 +000087 u8 reg8;
88 u16 reg16, pmbase;
89 u32 reg32;
90 const char *state;
91
Nico Huber9faae2b2018-11-14 00:00:35 +010092 int pwr_on = CONFIG_MAINBOARD_POWER_FAILURE_STATE;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +000093 int nmi_option;
Ronald G. Minnich182615d2004-08-24 16:20:46 +000094
Stefan Reinauer8702ab52010-03-14 17:01:08 +000095 /* Which state do we want to goto after g3 (power restored)?
96 * 0 == S0 Full On
97 * 1 == S5 Soft Off
Joseph Smithb5466b02010-03-22 23:10:53 +000098 *
99 * If the option is not existent (Laptops), use MAINBOARD_POWER_ON.
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000100 */
Varad Gautam06ef0462015-03-11 09:54:41 +0530101 pwr_on = MAINBOARD_POWER_ON;
102 get_option(&pwr_on, "power_on_after_fail");
Joseph Smithb5466b02010-03-22 23:10:53 +0000103
104 reg8 = pci_read_config8(dev, GEN_PMCON_3);
105 reg8 &= 0xfe;
106 switch (pwr_on) {
107 case MAINBOARD_POWER_OFF:
108 reg8 |= 1;
109 state = "off";
110 break;
111 case MAINBOARD_POWER_ON:
112 reg8 &= ~1;
113 state = "on";
114 break;
115 case MAINBOARD_POWER_KEEP:
116 reg8 &= ~1;
117 state = "state keep";
118 break;
119 default:
120 state = "undefined";
121 }
122
123 reg8 &= ~(1 << 3); /* minimum asssertion is 1 to 2 RTCCLK */
124
125 pci_write_config8(dev, GEN_PMCON_3, reg8);
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000126 printk(BIOS_INFO, "Set power %s after power failure.\n", state);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000127
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000128 /* Set up NMI on errors. */
Joseph Smithb5466b02010-03-22 23:10:53 +0000129 reg8 = inb(0x61);
130 reg8 &= 0x0f; /* Higher Nibble must be 0 */
131 reg8 &= ~(1 << 3); /* IOCHK# NMI Enable */
132 // reg8 &= ~(1 << 2); /* PCI SERR# Enable */
133 reg8 |= (1 << 2); /* PCI SERR# Disable for now */
134 outb(reg8, 0x61);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000135
Joseph Smithb5466b02010-03-22 23:10:53 +0000136 reg8 = inb(0x70);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000137 nmi_option = NMI_OFF;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000138 get_option(&nmi_option, "nmi");
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000139 if (nmi_option) {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000140 printk(BIOS_INFO, "NMI sources enabled.\n");
Joseph Smithb5466b02010-03-22 23:10:53 +0000141 reg8 &= ~(1 << 7); /* Set NMI. */
142 } else {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000143 printk(BIOS_INFO, "NMI sources disabled.\n");
Elyes HAOUAS9c5d4632018-04-26 22:21:21 +0200144 reg8 |= (1 << 7); /* Disable NMI. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000145 }
Joseph Smithb5466b02010-03-22 23:10:53 +0000146 outb(reg8, 0x70);
147
148 /* Set SMI# rate down and enable CPU_SLP# */
149 reg16 = pci_read_config16(dev, GEN_PMCON_1);
150 reg16 &= ~(3 << 0); // SMI# rate 1 minute
151 reg16 |= (1 << 5); // CPUSLP_EN Desktop only
152 pci_write_config16(dev, GEN_PMCON_1, reg16);
153
154 pmbase = pci_read_config16(dev, 0x40) & 0xfffe;
155
156 /* Set up power management block and determine sleep mode */
157 reg32 = inl(pmbase + 0x04); // PM1_CNT
158
159 reg32 &= ~(7 << 10); // SLP_TYP
160 reg32 |= (1 << 0); // SCI_EN
161 outl(reg32, pmbase + 0x04);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000162}
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000163
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +0200164static void gpio_init(struct device *dev)
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000165{
166 /* This should be done in romstage.c already */
167 pci_write_config32(dev, GPIO_BASE, (GPIOBASE_ADDR | 1));
168 pci_write_config8(dev, GPIO_CNTL, 0x10);
169}
170
171static void i82801dx_rtc_init(struct device *dev)
172{
173 u8 reg8;
174 u32 reg32;
175 int rtc_failed;
176
177 reg8 = pci_read_config8(dev, GEN_PMCON_3);
178 rtc_failed = reg8 & RTC_BATTERY_DEAD;
179 if (rtc_failed) {
180 reg8 &= ~(1 << 1); /* Preserve the power fail state. */
181 pci_write_config8(dev, GEN_PMCON_3, reg8);
182 }
183 reg32 = pci_read_config32(dev, GEN_STS);
184 rtc_failed |= reg32 & (1 << 2);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700185 cmos_init(rtc_failed);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000186
187 /* Enable access to the upper 128 byte bank of CMOS RAM. */
188 pci_write_config8(dev, RTC_CONF, 0x04);
189}
190
191static void i82801dx_lpc_route_dma(struct device *dev, u8 mask)
192{
193 u16 reg16;
194 int i;
195
196 reg16 = pci_read_config16(dev, PCI_DMA_CFG);
197 reg16 &= 0x300;
198 for (i = 0; i < 8; i++) {
199 if (i == 4)
200 continue;
201 reg16 |= ((mask & (1 << i)) ? 3 : 1) << (i * 2);
202 }
203 pci_write_config16(dev, PCI_DMA_CFG, reg16);
204}
205
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +0200206static void i82801dx_lpc_decode_en(struct device *dev)
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000207{
208 /* Decode 0x3F8-0x3FF (COM1) for COMA port, 0x2F8-0x2FF (COM2) for COMB.
209 * LPT decode defaults to 0x378-0x37F and 0x778-0x77F.
210 * Floppy decode defaults to 0x3F0-0x3F5, 0x3F7.
211 * We also need to set the value for LPC I/F Enables Register.
212 */
213 pci_write_config8(dev, COM_DEC, 0x10);
214 pci_write_config16(dev, LPC_EN, 0x300F);
215}
216
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000217/* ICH4 does not mention HPET in the docs, but
218 * all ICH3 and ICH4 do have HPETs built in.
219 */
220static void enable_hpet(struct device *dev)
221{
Joseph Smithb5466b02010-03-22 23:10:53 +0000222 u32 reg32, hpet, val;
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000223
Joseph Smithb5466b02010-03-22 23:10:53 +0000224 /* Set HPET base address and enable it */
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200225 printk(BIOS_DEBUG, "Enabling HPET at 0x%x\n", CONFIG_HPET_ADDRESS);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000226 reg32 = pci_read_config32(dev, GEN_CNTL);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000227 /*
Joseph Smithb5466b02010-03-22 23:10:53 +0000228 * Bit 17 is HPET enable bit.
229 * Bit 16:15 control the HPET base address.
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000230 */
231 reg32 &= ~(3 << 15); /* Clear it */
Joseph Smithb5466b02010-03-22 23:10:53 +0000232
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200233 hpet = CONFIG_HPET_ADDRESS >> 12;
Joseph Smithb5466b02010-03-22 23:10:53 +0000234 hpet &= 0x3;
235
236 reg32 |= (hpet << 15);
237 reg32 |= (1 << 17); /* Enable HPET. */
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000238 pci_write_config32(dev, GEN_CNTL, reg32);
239
Joseph Smithb5466b02010-03-22 23:10:53 +0000240 /* Check to see whether it took */
241 reg32 = pci_read_config32(dev, GEN_CNTL);
242 val = reg32 >> 15;
243 val &= 0x7;
244
245 if ((val & 0x4) && (hpet == (val & 0x3))) {
Patrick Georgi9aeb6942012-10-05 21:54:38 +0200246 printk(BIOS_INFO, "HPET enabled at 0x%x\n", CONFIG_HPET_ADDRESS);
Joseph Smithb5466b02010-03-22 23:10:53 +0000247 } else {
Stefan Reinauerf0aa09b2010-03-23 13:23:40 +0000248 printk(BIOS_WARNING, "HPET was not enabled correctly\n");
Joseph Smithb5466b02010-03-22 23:10:53 +0000249 reg32 &= ~(1 << 17); /* Clear Enable */
250 pci_write_config32(dev, GEN_CNTL, reg32);
251 }
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000252}
253
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000254static void lpc_init(struct device *dev)
255{
256 /* Set the value for PCI command register. */
257 pci_write_config16(dev, PCI_COMMAND, 0x000f);
258
Kyösti Mälkkie6143532013-02-26 17:24:41 +0200259 i82801dx_enable_acpi(dev);
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000260 /* IO APIC initialization. */
261 i82801dx_enable_ioapic(dev);
262
263 i82801dx_enable_serial_irqs(dev);
264
265 /* Setup the PIRQ. */
266 i82801dx_pirq_init(dev);
267
268 /* Setup power options. */
269 i82801dx_power_options(dev);
270
271 /* Set the state of the GPIO lines. */
272 gpio_init(dev);
273
274 /* Initialize the real time clock. */
Stefan Reinauer138be832010-02-27 01:50:21 +0000275 i82801dx_rtc_init(dev);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000276
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000277 /* Route DMA. */
Stefan Reinauer138be832010-02-27 01:50:21 +0000278 i82801dx_lpc_route_dma(dev, 0xff);
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000279
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000280 /* Initialize ISA DMA. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000281 isa_dma_init();
282
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000283 /* Setup decode ports and LPC I/F enables. */
284 i82801dx_lpc_decode_en(dev);
Stefan Reinauer527aedc2010-03-17 22:08:51 +0000285
286 /* Initialize the High Precision Event Timers */
287 enable_hpet(dev);
Kyösti Mälkki55b72632019-07-08 22:36:38 +0300288
289 /* Don't allow evil boot loaders, kernels, or
290 * userspace applications to deceive us:
291 */
Kyösti Mälkkicd0b67b2019-10-09 07:52:40 +0300292 if (CONFIG(HAVE_SMI_HANDLER) && !CONFIG(PARALLEL_MP))
Kyösti Mälkki55b72632019-07-08 22:36:38 +0300293 aseg_smm_lock();
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000294}
295
Elyes HAOUAS66faf0c2018-05-13 13:32:56 +0200296static void i82801dx_lpc_read_resources(struct device *dev)
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000297{
Eric Biederman4f9265f2004-10-22 02:33:51 +0000298 struct resource *res;
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000299
Myles Watson29cc9ed2009-07-02 18:56:24 +0000300 /* Get the normal PCI resources of this device. */
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000301 pci_dev_read_resources(dev);
302
Myles Watson29cc9ed2009-07-02 18:56:24 +0000303 /* Add an extra subtractive resource for both memory and I/O. */
Eric Biederman4f9265f2004-10-22 02:33:51 +0000304 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000305 res->base = 0;
306 res->size = 0x1000;
307 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000308 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Eric Biederman4f9265f2004-10-22 02:33:51 +0000309
310 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000311 res->base = 0xff800000;
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000312 res->size = 0x00800000; /* 8 MB for flash */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000313 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000314 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000315
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000316 res = new_resource(dev, 3); /* IOAPIC */
Uwe Hermann74d1a6e2010-10-12 17:34:08 +0000317 res->base = IO_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000318 res->size = 0x00001000;
319 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Eric Biederman4f9265f2004-10-22 02:33:51 +0000320}
321
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000322static struct device_operations lpc_ops = {
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000323 .read_resources = i82801dx_lpc_read_resources,
324 .set_resources = pci_dev_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +0000325 .enable_resources = pci_dev_enable_resources,
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000326 .init = lpc_init,
Nico Huber51b75ae2019-03-14 16:02:05 +0100327 .scan_bus = scan_static_bus,
Joseph Smith48f3e2b2010-03-17 03:37:18 +0000328 .enable = i82801dx_enable,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000329};
330
Stefan Reinauer8702ab52010-03-14 17:01:08 +0000331/* 82801DB/DBL */
332static const struct pci_driver lpc_driver_db __pci_driver = {
333 .ops = &lpc_ops,
334 .vendor = PCI_VENDOR_ID_INTEL,
335 .device = PCI_DEVICE_ID_INTEL_82801DB_LPC,
336};
337
338/* 82801DBM */
339static const struct pci_driver lpc_driver_dbm __pci_driver = {
340 .ops = &lpc_ops,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000341 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermanna29ec062007-11-04 03:21:37 +0000342 .device = PCI_DEVICE_ID_INTEL_82801DBM_LPC,
Ronald G. Minnich182615d2004-08-24 16:20:46 +0000343};