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