blob: c8447352a85ccff47dee3d3f544377a43d598950 [file] [log] [blame]
Kyösti Mälkki7b73e8522022-11-08 04:43:41 +00001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <arch/hpet.h>
4#include <arch/io.h>
5#include <arch/ioapic.h>
6#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>
11#include <option.h>
Kyösti Mälkki7b73e8522022-11-08 04:43:41 +000012#include <pc80/i8259.h>
Elyes Haouas357c2292022-11-10 08:50:22 +010013#include <pc80/isa-dma.h>
14#include <pc80/mc146818rtc.h>
15#include <types.h>
16
Kyösti Mälkki7b73e8522022-11-08 04:43:41 +000017#include "chip.h"
18#include "i82801dx.h"
19
20#define NMI_OFF 0
21
22typedef struct southbridge_intel_i82801dx_config config_t;
23
24/**
25 * Enable ACPI I/O range.
26 *
27 * @param dev PCI device with ACPI and PM BAR's
28 */
29static void i82801dx_enable_acpi(struct device *dev)
30{
31 /* Set ACPI base address (I/O space). */
32 pci_write_config32(dev, PMBASE, (PMBASE_ADDR | 1));
33
34 /* Enable ACPI I/O range decode and ACPI power management. */
35 pci_write_config8(dev, ACPI_CNTL, ACPI_EN);
36}
37
38/**
39 * Set miscellaneous static southbridge features.
40 *
41 * @param dev PCI device with I/O APIC control registers
42 */
43static void i82801dx_enable_ioapic(struct device *dev)
44{
45 u32 reg32;
46
47 reg32 = pci_read_config32(dev, GEN_CNTL);
48 reg32 |= (1 << 13); /* Coprocessor error enable (COPR_ERR_EN) */
49 reg32 |= (3 << 7); /* IOAPIC enable (APIC_EN) */
50 reg32 |= (1 << 2); /* DMA collection buffer enable (DCB_EN) */
51 reg32 |= (1 << 1); /* Delayed transaction enable (DTE) */
52 pci_write_config32(dev, GEN_CNTL, reg32);
53 printk(BIOS_DEBUG, "IOAPIC Southbridge enabled %x\n", reg32);
54
Kyösti Mälkkid1653572021-06-08 11:31:19 +030055 register_new_ioapic_gsi0(VIO_APIC_VADDR);
Kyösti Mälkki7b73e8522022-11-08 04:43:41 +000056
57 ioapic_set_boot_config(VIO_APIC_VADDR, true);
58}
59
60static void i82801dx_enable_serial_irqs(struct device *dev)
61{
62 /* Set packet length and toggle silent mode bit. */
63 pci_write_config8(dev, SERIRQ_CNTL,
64 (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
65 pci_write_config8(dev, SERIRQ_CNTL,
66 (1 << 7) | (0 << 6) | ((21 - 17) << 2) | (0 << 0));
67}
68
69static void i82801dx_pirq_init(struct device *dev)
70{
71 /* Get the chip configuration */
72 config_t *config = dev->chip_info;
73
74 pci_write_config8(dev, PIRQA_ROUT, config->pirqa_routing);
75 pci_write_config8(dev, PIRQB_ROUT, config->pirqb_routing);
76 pci_write_config8(dev, PIRQC_ROUT, config->pirqc_routing);
77 pci_write_config8(dev, PIRQD_ROUT, config->pirqd_routing);
78 pci_write_config8(dev, PIRQE_ROUT, config->pirqe_routing);
79 pci_write_config8(dev, PIRQF_ROUT, config->pirqf_routing);
80 pci_write_config8(dev, PIRQG_ROUT, config->pirqg_routing);
81 pci_write_config8(dev, PIRQH_ROUT, config->pirqh_routing);
82}
83
84static void i82801dx_power_options(struct device *dev)
85{
86 u8 reg8;
87 u16 reg16, pmbase;
88 u32 reg32;
89 const char *state;
90
91 /* Which state do we want to goto after g3 (power restored)?
92 * 0 == S0 Full On
93 * 1 == S5 Soft Off
94 *
95 * If the option is not existent (Laptops), use MAINBOARD_POWER_ON.
96 */
97 const unsigned int pwr_on = get_uint_option("power_on_after_fail", MAINBOARD_POWER_ON);
98
99 reg8 = pci_read_config8(dev, GEN_PMCON_3);
100 reg8 &= 0xfe;
101 switch (pwr_on) {
102 case MAINBOARD_POWER_OFF:
103 reg8 |= 1;
104 state = "off";
105 break;
106 case MAINBOARD_POWER_ON:
107 reg8 &= ~1;
108 state = "on";
109 break;
110 case MAINBOARD_POWER_KEEP:
111 reg8 &= ~1;
112 state = "state keep";
113 break;
114 default:
115 state = "undefined";
116 }
117
118 reg8 &= ~(1 << 3); /* minimum assertion is 1 to 2 RTCCLK */
119
120 pci_write_config8(dev, GEN_PMCON_3, reg8);
121 printk(BIOS_INFO, "Set power %s after power failure.\n", state);
122
123 /* Set up NMI on errors. */
124 reg8 = inb(0x61);
125 reg8 &= 0x0f; /* Higher Nibble must be 0 */
126 reg8 &= ~(1 << 3); /* IOCHK# NMI Enable */
127 // reg8 &= ~(1 << 2); /* PCI SERR# Enable */
128 reg8 |= (1 << 2); /* PCI SERR# Disable for now */
129 outb(reg8, 0x61);
130
131 reg8 = inb(0x70);
132 const unsigned int nmi_option = get_uint_option("nmi", NMI_OFF);
133 if (nmi_option) {
134 printk(BIOS_INFO, "NMI sources enabled.\n");
135 reg8 &= ~(1 << 7); /* Set NMI. */
136 } else {
137 printk(BIOS_INFO, "NMI sources disabled.\n");
138 reg8 |= (1 << 7); /* Disable NMI. */
139 }
140 outb(reg8, 0x70);
141
142 /* Set SMI# rate down and enable CPU_SLP# */
143 reg16 = pci_read_config16(dev, GEN_PMCON_1);
144 reg16 &= ~(3 << 0); // SMI# rate 1 minute
145 reg16 |= (1 << 5); // CPUSLP_EN Desktop only
146 pci_write_config16(dev, GEN_PMCON_1, reg16);
147
148 pmbase = pci_read_config16(dev, 0x40) & 0xfffe;
149
150 /* Set up power management block and determine sleep mode */
151 reg32 = inl(pmbase + 0x04); // PM1_CNT
152
153 reg32 &= ~(7 << 10); // SLP_TYP
154 reg32 |= (1 << 0); // SCI_EN
155 outl(reg32, pmbase + 0x04);
156}
157
158static void gpio_init(struct device *dev)
159{
160 /* This should be done in romstage.c already */
161 pci_write_config32(dev, GPIO_BASE, (GPIOBASE_ADDR | 1));
162 pci_write_config8(dev, GPIO_CNTL, 0x10);
163}
164
165static void i82801dx_rtc_init(struct device *dev)
166{
167 u8 reg8;
168 u32 reg32;
169 int rtc_failed;
170
171 reg8 = pci_read_config8(dev, GEN_PMCON_3);
172 rtc_failed = reg8 & RTC_BATTERY_DEAD;
173 if (rtc_failed) {
174 reg8 &= ~(1 << 1); /* Preserve the power fail state. */
175 pci_write_config8(dev, GEN_PMCON_3, reg8);
176 }
177 reg32 = pci_read_config32(dev, GEN_STS);
178 rtc_failed |= reg32 & (1 << 2);
179 cmos_init(rtc_failed);
180
181 /* Enable access to the upper 128 byte bank of CMOS RAM. */
182 pci_write_config8(dev, RTC_CONF, 0x04);
183}
184
185static void i82801dx_lpc_route_dma(struct device *dev, u8 mask)
186{
187 u16 reg16;
188 int i;
189
190 reg16 = pci_read_config16(dev, PCI_DMA_CFG);
191 reg16 &= 0x300;
192 for (i = 0; i < 8; i++) {
193 if (i == 4)
194 continue;
195 reg16 |= ((mask & (1 << i)) ? 3 : 1) << (i * 2);
196 }
197 pci_write_config16(dev, PCI_DMA_CFG, reg16);
198}
199
200static void i82801dx_lpc_decode_en(struct device *dev)
201{
202 /* Decode 0x3F8-0x3FF (COM1) for COMA port, 0x2F8-0x2FF (COM2) for COMB.
203 * LPT decode defaults to 0x378-0x37F and 0x778-0x77F.
204 * Floppy decode defaults to 0x3F0-0x3F5, 0x3F7.
205 * We also need to set the value for LPC I/F Enables Register.
206 */
207 pci_write_config8(dev, COM_DEC, 0x10);
208 pci_write_config16(dev, LPC_EN, 0x300F);
209}
210
211/* ICH4 does not mention HPET in the docs, but
212 * all ICH3 and ICH4 do have HPETs built in.
213 */
214static void enable_hpet(struct device *dev)
215{
216 u32 reg32, hpet, val;
217
218 /* Set HPET base address and enable it */
219 printk(BIOS_DEBUG, "Enabling HPET at 0x%x\n", HPET_BASE_ADDRESS);
220 reg32 = pci_read_config32(dev, GEN_CNTL);
221 /*
222 * Bit 17 is HPET enable bit.
223 * Bit 16:15 control the HPET base address.
224 */
225 reg32 &= ~(3 << 15); /* Clear it */
226
227 hpet = HPET_BASE_ADDRESS >> 12;
228 hpet &= 0x3;
229
230 reg32 |= (hpet << 15);
231 reg32 |= (1 << 17); /* Enable HPET. */
232 pci_write_config32(dev, GEN_CNTL, reg32);
233
234 /* Check to see whether it took */
235 reg32 = pci_read_config32(dev, GEN_CNTL);
236 val = reg32 >> 15;
237 val &= 0x7;
238
239 if ((val & 0x4) && (hpet == (val & 0x3))) {
240 printk(BIOS_INFO, "HPET enabled at 0x%x\n", HPET_BASE_ADDRESS);
241 } else {
242 printk(BIOS_WARNING, "HPET was not enabled correctly\n");
243 reg32 &= ~(1 << 17); /* Clear Enable */
244 pci_write_config32(dev, GEN_CNTL, reg32);
245 }
246}
247
248static void lpc_init(struct device *dev)
249{
250 i82801dx_enable_acpi(dev);
251 /* IO APIC initialization. */
252 i82801dx_enable_ioapic(dev);
253
254 i82801dx_enable_serial_irqs(dev);
255
256 /* Setup the PIRQ. */
257 i82801dx_pirq_init(dev);
258
259 /* Setup power options. */
260 i82801dx_power_options(dev);
261
262 /* Set the state of the GPIO lines. */
263 gpio_init(dev);
264
265 /* Initialize the real time clock. */
266 i82801dx_rtc_init(dev);
267
268 /* Route DMA. */
269 i82801dx_lpc_route_dma(dev, 0xff);
270
271 /* Initialize ISA DMA. */
272 isa_dma_init();
273
274 /* Setup decode ports and LPC I/F enables. */
275 i82801dx_lpc_decode_en(dev);
276
277 /* Initialize the High Precision Event Timers */
278 enable_hpet(dev);
279
280 setup_i8259();
281}
282
283static void i82801dx_lpc_read_resources(struct device *dev)
284{
285 struct resource *res;
286
287 /* Get the normal PCI resources of this device. */
288 pci_dev_read_resources(dev);
289
290 /* Add an extra subtractive resource for both memory and I/O. */
291 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
292 res->base = 0;
293 res->size = 0x1000;
294 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
295 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
296
297 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
298 res->base = 0xff800000;
299 res->size = 0x00800000; /* 8 MB for flash */
300 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
301 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
302
303 res = new_resource(dev, 3); /* IOAPIC */
304 res->base = IO_APIC_ADDR;
305 res->size = 0x00001000;
306 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
307}
308
309static struct device_operations lpc_ops = {
310 .read_resources = i82801dx_lpc_read_resources,
311 .set_resources = pci_dev_set_resources,
312 .enable_resources = pci_dev_enable_resources,
313 .init = lpc_init,
314 .scan_bus = scan_static_bus,
315 .enable = i82801dx_enable,
316};
317
318/* 82801DB/DBL */
319static const struct pci_driver lpc_driver_db __pci_driver = {
320 .ops = &lpc_ops,
321 .vendor = PCI_VID_INTEL,
322 .device = PCI_DID_INTEL_82801DB_LPC,
323};
324
325/* 82801DBM */
326static const struct pci_driver lpc_driver_dbm __pci_driver = {
327 .ops = &lpc_ops,
328 .vendor = PCI_VID_INTEL,
329 .device = PCI_DID_INTEL_82801DBM_LPC,
330};