eefd7bb9f15ffa900c8572e5aa7d2eb3fd093579
[coreboot.git] / src / southbridge / intel / i82801xx / i82801xx_lpc.c
1 /*
2  * This file is part of the LinuxBIOS 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 /* From 82801DBM, needs to be fixed to support everything the 82801ER does. */
25
26 #include <console/console.h>
27 #include <device/device.h>
28 #include <device/pci.h>
29 #include <device/pci_ids.h>
30 #include <pc80/mc146818rtc.h>
31 #include <pc80/isa-dma.h>
32 #include <arch/io.h>
33 #include "i82801xx.h"
34
35 #define PMBASE_ADDR     0x00000400 /* ACPI Base Address Register */
36 #define GPIO_BASE_ADDR  0x00000500 /* GPIO Base Address Register */
37
38 #define NMI_OFF 0
39
40 /* PIRQ[n]_ROUT[3:0] - PIRQ Routing Control
41  * 0x00 - 0000 = Reserved
42  * 0x01 - 0001 = Reserved
43  * 0x02 - 0010 = Reserved
44  * 0x03 - 0011 = IRQ3
45  * 0x04 - 0100 = IRQ4
46  * 0x05 - 0101 = IRQ5
47  * 0x06 - 0110 = IRQ6
48  * 0x07 - 0111 = IRQ7
49  * 0x08 - 1000 = Reserved
50  * 0x09 - 1001 = IRQ9
51  * 0x0A - 1010 = IRQ10
52  * 0x0B - 1011 = IRQ11
53  * 0x0C - 1100 = IRQ12
54  * 0x0D - 1101 = Reserved
55  * 0x0E - 1110 = IRQ14
56  * 0x0F - 1111 = IRQ15
57  * PIRQ[n]_ROUT[7] - PIRQ Routing Control
58  * 0x80 - The PIRQ is not routed.
59  */
60
61 #define PIRQA 0x03
62 #define PIRQB 0x05
63 #define PIRQC 0x06
64 #define PIRQD 0x07
65 #define PIRQE 0x09
66 #define PIRQF 0x0A
67 #define PIRQG 0x0B
68 #define PIRQH 0x0C
69
70 void i82801xx_enable_apic(struct device *dev)
71 {
72         uint32_t reg32;
73         volatile uint32_t *ioapic_index = (volatile uint32_t *)0xfec00000;
74         volatile uint32_t *ioapic_data = (volatile uint32_t *)0xfec00010;
75
76         /* Set ACPI base address (I/O space). */
77         pci_write_config32(dev, PMBASE, (PMBASE_ADDR | 1));
78
79         /* Enable ACPI I/O and power management. */
80         pci_write_config8(dev, ACPI_CNTL, 0x10);
81
82         reg32 = pci_read_config32(dev, GEN_CNTL);
83         reg32 |= (3 << 7);      /* Enable IOAPIC */
84         reg32 |= (1 << 13);     /* Coprocessor error enable */
85         reg32 |= (1 << 1);      /* Delayed transaction enable */
86         reg32 |= (1 << 2);      /* DMA collection buffer enable */
87         pci_write_config32(dev, GEN_CNTL, reg32);
88         printk_debug("IOAPIC Southbridge enabled %x\n", reg32);
89
90         *ioapic_index = 0;
91         *ioapic_data = (1 << 25);
92
93         *ioapic_index = 0;
94         reg32 = *ioapic_data;
95         printk_debug("Southbridge APIC ID = %x\n", reg32);
96         if (reg32 != (1 << 25))
97                 die("APIC Error\n");
98
99         /* TODO: From i82801ca, needed/useful on other ICH? */
100         *ioapic_index = 3; /* Select Boot Configuration register. */
101         *ioapic_data = 1; /* Use Processor System Bus to deliver interrupts. */
102 }
103
104 void i82801xx_enable_serial_irqs(struct device *dev)
105 {
106         /* Set packet length and toggle silent mode bit. */
107         pci_write_config8(dev, SERIRQ_CNTL,
108                           (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0));
109         pci_write_config8(dev, SERIRQ_CNTL,
110                           (1 << 7) | (0 << 6) | ((21 - 17) << 2) | (0 << 0));
111         /* TODO: Explain/#define the real meaning of these magic numbers. */
112 }
113
114 static void i82801xx_pirq_init(device_t dev, uint16_t ich_model)
115 {
116         /* Route PIRQA - PIRQD. */
117         pci_write_config8(dev, PIRQA_ROUT, PIRQA);
118         pci_write_config8(dev, PIRQB_ROUT, PIRQB);
119         pci_write_config8(dev, PIRQC_ROUT, PIRQC);
120         pci_write_config8(dev, PIRQD_ROUT, PIRQD);
121
122         /* Route PIRQE - PIRQH (for ICH2-ICH9). */
123         if (ich_model >= 0x2440) {
124                 pci_write_config8(dev, PIRQE_ROUT, PIRQE);
125                 pci_write_config8(dev, PIRQF_ROUT, PIRQF);
126                 pci_write_config8(dev, PIRQG_ROUT, PIRQG);
127                 pci_write_config8(dev, PIRQH_ROUT, PIRQH);
128         }
129 }
130
131 static void i82801xx_power_options(device_t dev)
132 {
133         uint8_t byte;
134         int pwr_on = -1;
135         int nmi_option;
136
137         /* power after power fail */
138         /* FIXME this doesn't work! */
139         /* Which state do we want to goto after g3 (power restored)?
140          * 0 == S0 Full On
141          * 1 == S5 Soft Off
142          */
143         pci_write_config8(dev, GEN_PMCON_3, pwr_on ? 0 : 1);
144         printk_info("Set power %s if power fails\n", pwr_on ? "on" : "off");
145
146         /* Set up NMI on errors. */
147         byte = inb(0x61);
148         byte &= ~(1 << 3);      /* IOCHK# NMI Enable */
149         byte &= ~(1 << 2);      /* PCI SERR# Enable */
150         outb(byte, 0x61);
151         byte = inb(0x70);
152
153         nmi_option = NMI_OFF;
154         get_option(&nmi_option, "nmi");
155         if (nmi_option) {
156                 byte &= ~(1 << 7);      /* Set NMI. */
157                 outb(byte, 0x70);
158         }
159 }
160
161 static void gpio_init(device_t dev, uint16_t ich_model)
162 {
163         /* Set the value for GPIO base address register and enable GPIO.
164          * Note: ICH-ICH5 registers differ from ICH6-ICH9.
165          */
166         if (ich_model <= 0x24D0) {
167                 pci_write_config32(dev, GPIO_BASE_ICH0_5, (GPIO_BASE_ADDR | 1));
168                 pci_write_config8(dev, GPIO_CNTL_ICH0_5, 0x10);
169         } else if (ich_model >= 0x2640) {
170                 pci_write_config32(dev, GPIO_BASE_ICH6_9, (GPIO_BASE_ADDR | 1));
171                 pci_write_config8(dev, GPIO_CNTL_ICH6_9, 0x10);
172         }
173 }
174
175 void i82801xx_rtc_init(struct device *dev)
176 {
177         uint8_t reg8;
178         uint32_t reg32;
179         int rtc_failed;
180
181         reg8 = pci_read_config8(dev, GEN_PMCON_3);
182         rtc_failed = reg8 & RTC_BATTERY_DEAD;
183         if (rtc_failed) {
184                 reg8 &= ~(1 << 1);      /* Preserve the power fail state. */
185                 pci_write_config8(dev, GEN_PMCON_3, reg8);
186         }
187         reg32 = pci_read_config32(dev, GEN_STS);
188         rtc_failed |= reg32 & (1 << 2);
189         rtc_init(rtc_failed);
190
191         /* Enable access to the upper 128 byte bank of CMOS RAM. */
192         pci_write_config8(dev, RTC_CONF, 0x04);
193 }
194
195 void i82801xx_lpc_route_dma(struct device *dev, uint8_t mask)
196 {
197         uint16_t reg16;
198         int i;
199
200         reg16 = pci_read_config16(dev, PCI_DMA_CFG);
201         reg16 &= 0x300;
202         for (i = 0; i < 8; i++) {
203                 if (i == 4)
204                         continue;
205                 reg16 |= ((mask & (1 << i)) ? 3 : 1) << (i * 2);
206         }
207         pci_write_config16(dev, PCI_DMA_CFG, reg16);
208 }
209
210 static void i82801xx_lpc_decode_en(device_t dev, uint16_t ich_model)
211 {
212         /* Decode 0x3F8-0x3FF (COM1) for COMA port, 0x2F8-0x2FF (COM2) for COMB.
213          * LPT decode defaults to 0x378-0x37F and 0x778-0x77F.
214          * Floppy decode defaults to 0x3F0-0x3F5, 0x3F7.
215          * We also need to set the value for LPC I/F Enables Register.
216          * Note: ICH-ICH5 registers differ from ICH6-ICH9.
217          */
218         if (ich_model <= 0x24D0) {
219                 pci_write_config8(dev, COM_DEC, 0x10);
220                 pci_write_config16(dev, LPC_EN_ICH0_5, 0x300F);
221         } else if (ich_model >= 0x2640) {
222                 pci_write_config8(dev, LPC_IO_DEC, 0x10);
223                 pci_write_config16(dev, LPC_EN_ICH6_9, 0x300F);
224         }
225 }
226
227 static void enable_hpet(struct device *dev)
228 {
229 #ifdef HPET_PRESENT
230         uint32_t reg32;
231         uint32_t code = (0 & 0x3);
232
233         reg32 = pci_read_config32(dev, GEN_CNTL);
234         reg32 |= (1 << 17);     /* Enable HPET. */
235         /*
236          * Bits [16:15] Memory Address Range
237          * 00           FED0_0000h - FED0_03FFh
238          * 01           FED0_1000h - FED0_13FFh
239          * 10           FED0_2000h - FED0_23FFh
240          * 11           FED0_3000h - FED0_33FFh
241          */
242         reg32 &= ~(3 << 15);    /* Clear it */
243         reg32 |= (code << 15);
244         /* TODO: reg32 is never written to anywhere? */
245         printk_debug("Enabling HPET @0x%x\n", HPET_ADDR | (code << 12));
246 #endif
247 }
248
249 static void lpc_init(struct device *dev)
250 {
251         uint16_t ich_model = pci_read_config16(dev, PCI_DEVICE_ID);
252
253         /* Set the value for PCI command register. */
254         pci_write_config16(dev, PCI_COMMAND, 0x000f);
255
256         /* IO APIC initialization. */
257         i82801xx_enable_apic(dev);
258
259         i82801xx_enable_serial_irqs(dev);
260
261         /* Setup the PIRQ. */
262         i82801xx_pirq_init(dev, ich_model);
263
264         /* Setup power options. */
265         i82801xx_power_options(dev);
266
267         /* Set the state of the GPIO lines. */
268         gpio_init(dev, ich_model);
269
270         /* Initialize the real time clock. */
271         i82801xx_rtc_init(dev);
272
273         /* Route DMA. */
274         i82801xx_lpc_route_dma(dev, 0xff);
275
276         /* Initialize ISA DMA. */
277         isa_dma_init();
278
279         /* Setup decode ports and LPC I/F enables. */
280         i82801xx_lpc_decode_en(dev, ich_model);
281
282         /* Initialize the High Precision Event Timers, if present. */
283         enable_hpet(dev);
284 }
285
286 static void i82801xx_lpc_read_resources(device_t dev)
287 {
288         struct resource *res;
289
290         /* Get the normal PCI resources of this device. */
291         pci_dev_read_resources(dev);
292
293         /* Add an extra subtractive resource for both memory and I/O. */
294         res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
295         res->flags =
296             IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
297
298         res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
299         res->flags =
300             IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
301 }
302
303 static void i82801xx_lpc_enable_resources(device_t dev)
304 {
305         pci_dev_enable_resources(dev);
306         enable_childrens_resources(dev);
307 }
308
309 static struct device_operations lpc_ops = {
310         .read_resources         = i82801xx_lpc_read_resources,
311         .set_resources          = pci_dev_set_resources,
312         .enable_resources       = i82801xx_lpc_enable_resources,
313         .init                   = lpc_init,
314         .scan_bus               = scan_static_bus,
315         .enable                 = i82801xx_enable,
316 };
317
318 static const struct pci_driver i82801aa_lpc __pci_driver = {
319         .ops    = &lpc_ops,
320         .vendor = PCI_VENDOR_ID_INTEL,
321         .device = 0x2410,
322 };
323
324 static const struct pci_driver i82801ab_lpc __pci_driver = {
325         .ops    = &lpc_ops,
326         .vendor = PCI_VENDOR_ID_INTEL,
327         .device = 0x2420,
328 };
329
330 static const struct pci_driver i82801ba_lpc __pci_driver = {
331         .ops    = &lpc_ops,
332         .vendor = PCI_VENDOR_ID_INTEL,
333         .device = 0x2440,
334 };
335
336 static const struct pci_driver i82801ca_lpc __pci_driver = {
337         .ops    = &lpc_ops,
338         .vendor = PCI_VENDOR_ID_INTEL,
339         .device = 0x2480,
340 };
341
342 static const struct pci_driver i82801db_lpc __pci_driver = {
343         .ops    = &lpc_ops,
344         .vendor = PCI_VENDOR_ID_INTEL,
345         .device = 0x24c0,
346 };
347
348 static const struct pci_driver i82801dbm_lpc __pci_driver = {
349         .ops    = &lpc_ops,
350         .vendor = PCI_VENDOR_ID_INTEL,
351         .device = 0x24cc,
352 };
353
354 /* 82801EB and 82801ER */
355 static const struct pci_driver i82801ex_lpc __pci_driver = {
356         .ops    = &lpc_ops,
357         .vendor = PCI_VENDOR_ID_INTEL,
358         .device = 0x24d0,
359 };