Round 2 of i82801AX fixes to get it into a usable shape.
[coreboot.git] / src / southbridge / intel / i82801ax / i82801ax_lpc.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2003 Linux Networx
5  * Copyright (C) 2003 SuSE Linux AG
6  * Copyright (C) 2005 Tyan Computer
7  * (Written by Yinghai Lu <yinghailu@gmail.com> for Tyan Computer)
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23
24 #include <console/console.h>
25 #include <device/device.h>
26 #include <device/pci.h>
27 #include <device/pci_ids.h>
28 #include <pc80/mc146818rtc.h>
29 #include <pc80/isa-dma.h>
30 #include <arch/io.h>
31 #include "i82801ax.h"
32
33 #define GPIO_BASE_ADDR  0x00000500 /* GPIO Base Address Register */
34
35 #define NMI_OFF 0
36
37 typedef struct southbridge_intel_i82801ax_config config_t;
38
39 /* PIRQ[n]_ROUT[3:0] - IRQ Routing (ISA compatible)
40  * 0x00 - 0000 = Reserved
41  * 0x01 - 0001 = Reserved
42  * 0x02 - 0010 = Reserved
43  * 0x03 - 0011 = IRQ3
44  * 0x04 - 0100 = IRQ4
45  * 0x05 - 0101 = IRQ5
46  * 0x06 - 0110 = IRQ6
47  * 0x07 - 0111 = IRQ7
48  * 0x08 - 1000 = Reserved
49  * 0x09 - 1001 = IRQ9
50  * 0x0A - 1010 = IRQ10
51  * 0x0B - 1011 = IRQ11
52  * 0x0C - 1100 = IRQ12
53  * 0x0D - 1101 = Reserved
54  * 0x0E - 1110 = IRQ14
55  * 0x0F - 1111 = IRQ15
56  *
57  * PIRQ[n]_ROUT[7] - Interrupt Routing Enable (IRQEN)
58  * 0 - The PIRQ is routed to the ISA-compatible interrupt specified above.
59  * 1 - The PIRQ is not routed to the 8259.
60  */
61
62 #define PIRQA 0x03
63 #define PIRQB 0x04
64 #define PIRQC 0x05
65 #define PIRQD 0x06
66
67 /*
68  * Use 0x0ef8 for a bitmap to cover all these IRQ's.
69  * Use the defined IRQ values above or set mainboard
70  * specific IRQ values in your mainboards Config.lb.
71  */
72 static void i82801ax_enable_apic(struct device *dev)
73 {
74         u32 reg32;
75         volatile u32 *ioapic_index = (volatile u32 *)0xfec00000;
76         volatile u32 *ioapic_data = (volatile u32 *)0xfec00010;
77
78         /* Set ACPI base address (I/O space). */
79         pci_write_config32(dev, PMBASE, (PMBASE_ADDR | 1));
80
81         /* Enable ACPI I/O range decode and ACPI power management. */
82         pci_write_config8(dev, ACPI_CNTL, ACPI_EN);
83
84         reg32 = pci_read_config32(dev, GEN_CNTL);
85         reg32 |= (1 << 13);     /* Coprocessor error enable (COPR_ERR_EN) */
86         reg32 |= (3 << 7);      /* IOAPIC enable (APIC_EN) */
87         reg32 |= (1 << 2);      /* DMA collection buffer enable (DCB_EN) */
88         reg32 |= (1 << 1);      /* Delayed transaction enable (DTE) */
89         pci_write_config32(dev, GEN_CNTL, reg32);
90         printk(BIOS_DEBUG, "IOAPIC Southbridge enabled %x\n", reg32);
91
92         *ioapic_index = 0;
93         *ioapic_data = (1 << 25);
94
95         *ioapic_index = 0;
96         reg32 = *ioapic_data;
97         printk(BIOS_DEBUG, "Southbridge APIC ID = %x\n", reg32);
98         if (reg32 != (1 << 25))
99                 die("APIC Error\n");
100
101         /* TODO: From i82801ca, needed/useful on other ICH? */
102         *ioapic_index = 3; /* Select Boot Configuration register. */
103         *ioapic_data = 1; /* Use Processor System Bus to deliver interrupts. */
104 }
105
106 static void i82801ax_enable_serial_irqs(struct device *dev)
107 {
108         /* Set packet length and toggle silent mode bit. */
109         pci_write_config8(dev, SERIRQ_CNTL,
110                           (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
111         pci_write_config8(dev, SERIRQ_CNTL,
112                           (1 << 7) | (0 << 6) | ((21 - 17) << 2) | (0 << 0));
113         /* TODO: Explain/#define the real meaning of these magic numbers. */
114 }
115
116 static void i82801ax_pirq_init(device_t dev)
117 {
118         u8 reg8;
119         config_t *config = dev->chip_info;
120
121         reg8 = (config->pirqa_routing) ? config->pirqa_routing : PIRQA;
122         pci_write_config8(dev, PIRQA_ROUT, reg8);
123
124         reg8 = (config->pirqb_routing) ? config->pirqb_routing : PIRQB;
125         pci_write_config8(dev, PIRQB_ROUT, reg8);
126
127         reg8 = (config->pirqc_routing) ? config->pirqc_routing : PIRQC;
128         pci_write_config8(dev, PIRQC_ROUT, reg8);
129
130         reg8 = (config->pirqd_routing) ? config->pirqd_routing : PIRQD;
131         pci_write_config8(dev, PIRQD_ROUT, reg8);
132 }
133
134 static void i82801ax_power_options(device_t dev)
135 {
136         uint8_t byte;
137         int pwr_on = -1;
138         int nmi_option;
139
140         /* power after power fail */
141         /* FIXME this doesn't work! */
142         /* Which state do we want to goto after g3 (power restored)?
143          * 0 == S0 Full On
144          * 1 == S5 Soft Off
145          */
146         pci_write_config8(dev, GEN_PMCON_3, pwr_on ? 0 : 1);
147         printk(BIOS_INFO, "Set power %s if power fails\n", pwr_on ? "on" : "off");
148
149         /* Set up NMI on errors. */
150         byte = inb(0x61);
151         byte &= ~(1 << 3);      /* IOCHK# NMI Enable */
152         byte &= ~(1 << 2);      /* PCI SERR# Enable */
153         outb(byte, 0x61);
154         byte = inb(0x70);
155
156         nmi_option = NMI_OFF;
157         get_option(&nmi_option, "nmi");
158         if (nmi_option) {
159                 byte &= ~(1 << 7);      /* Set NMI. */
160                 outb(byte, 0x70);
161         }
162 }
163
164 static void gpio_init(device_t dev)
165 {
166         pci_write_config32(dev, GPIO_BASE, (GPIO_BASE_ADDR | 1));
167         pci_write_config8(dev, GPIO_CNTL, GPIO_EN);
168 }
169
170 static void i82801ax_rtc_init(struct device *dev)
171 {
172         uint8_t reg8;
173         uint32_t reg32;
174         int rtc_failed;
175
176         reg8 = pci_read_config8(dev, GEN_PMCON_3);
177         rtc_failed = reg8 & RTC_BATTERY_DEAD;
178         if (rtc_failed) {
179                 reg8 &= ~(1 << 1);      /* Preserve the power fail state. */
180                 pci_write_config8(dev, GEN_PMCON_3, reg8);
181         }
182         reg32 = pci_read_config32(dev, GEN_STA);
183         rtc_failed |= reg32 & (1 << 2);
184         rtc_init(rtc_failed);
185
186         /* Enable access to the upper 128 byte bank of CMOS RAM. */
187         pci_write_config8(dev, RTC_CONF, 0x04);
188 }
189
190 static void i82801ax_lpc_route_dma(struct device *dev, uint8_t mask)
191 {
192         uint16_t reg16;
193         int i;
194
195         reg16 = pci_read_config16(dev, PCI_DMA_CFG);
196         reg16 &= 0x300;
197         for (i = 0; i < 8; i++) {
198                 if (i == 4)
199                         continue;
200                 reg16 |= ((mask & (1 << i)) ? 3 : 1) << (i * 2);
201         }
202         pci_write_config16(dev, PCI_DMA_CFG, reg16);
203 }
204
205 static void i82801ax_lpc_decode_en(device_t dev)
206 {
207         /* Decode 0x3F8-0x3FF (COM1) for COMA port, 0x2F8-0x2FF (COM2) for COMB.
208          * LPT decode defaults to 0x378-0x37F and 0x778-0x77F.
209          * Floppy decode defaults to 0x3F0-0x3F5, 0x3F7.
210          * We also need to set the value for LPC I/F Enables Register.
211          */
212         pci_write_config8(dev, COM_DEC, 0x10);
213         pci_write_config16(dev, LPC_EN, 0x300F);
214 }
215
216 static void lpc_init(struct device *dev)
217 {
218         /* Set the value for PCI command register. */
219         pci_write_config16(dev, PCI_COMMAND, 0x000f);
220
221         /* IO APIC initialization. */
222         i82801ax_enable_apic(dev);
223
224         i82801ax_enable_serial_irqs(dev);
225
226         /* Setup the PIRQ. */
227         i82801ax_pirq_init(dev);
228
229         /* Setup power options. */
230         i82801ax_power_options(dev);
231
232         /* Set the state of the GPIO lines. */
233         gpio_init(dev);
234
235         /* Initialize the real time clock. */
236         i82801ax_rtc_init(dev);
237
238         /* Route DMA. */
239         i82801ax_lpc_route_dma(dev, 0xff);
240
241         /* Initialize ISA DMA. */
242         isa_dma_init();
243
244         /* Setup decode ports and LPC I/F enables. */
245         i82801ax_lpc_decode_en(dev);
246 }
247
248 static void i82801ax_lpc_read_resources(device_t dev)
249 {
250         struct resource *res;
251
252         /* Get the normal PCI resources of this device. */
253         pci_dev_read_resources(dev);
254
255         /* Add an extra subtractive resource for both memory and I/O. */
256         res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
257         res->base = 0;
258         res->size = 0x1000;
259         res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
260                      IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
261
262         res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
263         res->base = 0xff800000;
264         res->size = 0x00800000; /* 8 MB for flash */
265         res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
266                      IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
267
268         res = new_resource(dev, 3); /* IOAPIC */
269         res->base = 0xfec00000;
270         res->size = 0x00001000;
271         res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
272 }
273
274 static struct device_operations lpc_ops = {
275         .read_resources         = i82801ax_lpc_read_resources,
276         .set_resources          = pci_dev_set_resources,
277         .enable_resources       = pci_dev_enable_resources,
278         .init                   = lpc_init,
279         .scan_bus               = scan_static_bus,
280         .enable                 = i82801ax_enable,
281 };
282
283 /* 82801AA (ICH) */
284 static const struct pci_driver i82801aa_lpc __pci_driver = {
285         .ops    = &lpc_ops,
286         .vendor = PCI_VENDOR_ID_INTEL,
287         .device = 0x2410,
288 };
289
290 /* 82801AB (ICH0) */
291 static const struct pci_driver i82801ab_lpc __pci_driver = {
292         .ops    = &lpc_ops,
293         .vendor = PCI_VENDOR_ID_INTEL,
294         .device = 0x2420,
295 };