Convert some comments to proper Doxygen syntax.
[coreboot.git] / src / southbridge / intel / i82801cx / i82801cx_lpc.c
1 /*
2  * (C) 2003 Linux Networx, SuSE Linux AG
3  * (C) 2004 Tyan Computer
4  * (c) 2005 Digital Design Corporation
5  */
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 <pc80/mc146818rtc.h>
12 #include <pc80/isa-dma.h>
13 #include <arch/io.h>
14 #include <arch/ioapic.h>
15 #include "i82801cx.h"
16
17 #define NMI_OFF 0
18
19 #ifndef CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL
20 #define CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
21 #endif
22
23 #define MAINBOARD_POWER_OFF 0
24 #define MAINBOARD_POWER_ON  1
25
26
27 static void i82801cx_enable_ioapic( struct device *dev)
28 {
29         uint32_t dword;
30     volatile uint32_t* ioapic_index = (volatile uint32_t*)IO_APIC_ADDR;
31     volatile uint32_t* ioapic_data = (volatile uint32_t*)(IO_APIC_ADDR + 0x10);
32
33     dword = pci_read_config32(dev, GEN_CNTL);
34     dword |= (3 << 7); /* enable ioapic & disable SMBus interrupts */
35     dword |= (1 <<13); /* coprocessor error enable */
36     dword |= (1 << 1); /* delay transaction enable */
37     dword |= (1 << 2); /* DMA collection buf enable */
38     pci_write_config32(dev, GEN_CNTL, dword);
39     printk(BIOS_DEBUG, "ioapic southbridge enabled %x\n",dword);
40
41     // Must program the APIC's ID before using it
42
43     *ioapic_index = 0;          // Select APIC ID register
44     *ioapic_data = (2<<24);
45
46     // Hang if the ID didn't take (chip not present?)
47     *ioapic_index = 0;
48     dword = *ioapic_data;
49     printk(BIOS_DEBUG, "Southbridge apic id = %x\n", (dword>>24) & 0xF);
50     if(dword != (2<<24))
51                 die("");
52
53         *ioapic_index = 3;              // Select Boot Configuration register
54         *ioapic_data = 1;               // Use Processor System Bus to deliver interrupts
55 }
56
57 // This is how interrupts are received from the Super I/O chip
58 static void i82801cx_enable_serial_irqs( struct device *dev)
59 {
60         // Recognize serial IRQs, continuous mode, frame size 21, 4 clock start frame pulse width
61     pci_write_config8(dev, SERIRQ_CNTL, (1 << 7)|(1 << 6)|((21 - 17) << 2)|(0<< 0));
62 }
63
64 /**
65  * Route all DMA channels to either PCI or LPC.
66  *
67  * @param dev TODO
68  * @param mask Identifies whether each channel should be used for PCI DMA
69  *             (bit = 0) or LPC DMA (bit = 1). The LSb controls channel 0.
70  *             Channel 4 is not used (reserved).
71  */
72 static void i82801cx_lpc_route_dma( struct device *dev, uint8_t mask)
73 {
74     uint16_t dmaConfig;
75     int channelIndex;
76
77     dmaConfig = pci_read_config16(dev, PCI_DMA_CFG);
78     dmaConfig &= 0x300;                         // Preserve reserved bits
79     for(channelIndex = 0; channelIndex < 8; channelIndex++) {
80         if (channelIndex == 4)
81                 continue;               // Register doesn't support channel 4
82         dmaConfig |= ((mask & (1 << channelIndex))? 3:1) << (channelIndex*2);
83     }
84     pci_write_config16(dev, PCI_DMA_CFG, dmaConfig);
85 }
86
87 static void i82801cx_rtc_init(struct device *dev)
88 {
89     uint32_t dword;
90     int rtc_failed;
91         int pwr_on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
92     uint8_t pmcon3 = pci_read_config8(dev, GEN_PMCON_3);
93
94     rtc_failed = pmcon3 & RTC_BATTERY_DEAD;
95     if (rtc_failed) {
96         // Clear the RTC_BATTERY_DEAD bit, but preserve
97         // the RTC_POWER_FAILED, G3 state, and reserved bits
98                 // NOTE: RTC_BATTERY_DEAD and RTC_POWER_FAILED are "write-1-clear" bits
99         pmcon3 &= ~RTC_POWER_FAILED;
100     }
101
102     get_option(&pwr_on, "power_on_after_fail");
103         pmcon3 &= ~SLEEP_AFTER_POWER_FAIL;
104         if (!pwr_on) {
105                 pmcon3 |= SLEEP_AFTER_POWER_FAIL;
106         }
107         pci_write_config8(dev, GEN_PMCON_3, pmcon3);
108         printk(BIOS_INFO, "set power %s after power fail\n",
109                                  pwr_on ? "on" : "off");
110
111     // See if the Safe Mode jumper is set
112     dword = pci_read_config32(dev, GEN_STS);
113     rtc_failed |= dword & (1 << 2);
114
115     rtc_init(rtc_failed);
116 }
117
118
119 static void i82801cx_1f0_misc(struct device *dev)
120 {
121         // Prevent LPC disabling, enable parity errors, and SERR# (System Error)
122     pci_write_config16(dev, PCI_COMMAND, 0x014f);
123
124     // Set ACPI base address to 0x1100 (I/O space)
125     pci_write_config32(dev, PMBASE, 0x00001101);
126
127     // Enable ACPI I/O and power management
128     pci_write_config8(dev, ACPI_CNTL, 0x10);
129
130     // Set GPIO base address to 0x1180 (I/O space)
131     pci_write_config32(dev, GPIO_BASE, 0x00001181);
132
133     // Enable GPIO
134     pci_write_config8(dev, GPIO_CNTL, 0x10);
135
136     // Route PIRQA to IRQ11, PIRQB to IRQ3, PIRQC to IRQ5, PIRQD to IRQ10
137     pci_write_config32(dev, PIRQA_ROUT, 0x0A05030B);
138
139     // Route PIRQE to IRQ7. Leave PIRQF - PIRQH unrouted.
140     pci_write_config8(dev, PIRQE_ROUT, 0x07);
141
142     // Enable access to the upper 128 byte bank of CMOS RAM
143     pci_write_config8(dev, RTC_CONF, 0x04);
144
145     // Decode 0x3F8-0x3FF (COM1) for COMA port,
146         //                0x2F8-0x2FF (COM2) for COMB
147     pci_write_config8(dev, COM_DEC, 0x10);
148
149         // LPT decode defaults to 0x378-0x37F and 0x778-0x77F
150         // Floppy decode defaults to 0x3F0-0x3F5, 0x3F7
151
152     // Enable COMA, COMB, LPT, floppy;
153         // disable microcontroller, Super I/O, sound, gameport
154     pci_write_config16(dev, LPC_EN, 0x000F);
155 }
156
157 static void lpc_init(struct device *dev)
158 {
159         uint8_t byte;
160         int pwr_on=-1;
161         int nmi_option;
162
163         /* IO APIC initialization */
164         i82801cx_enable_ioapic(dev);
165
166         i82801cx_enable_serial_irqs(dev);
167
168         /* power after power fail */
169                 /* FIXME this doesn't work! */
170         /* Which state do we want to goto after g3 (power restored)?
171          * 0 == S0 Full On
172          * 1 == S5 Soft Off
173          */
174     byte = pci_read_config8(dev, GEN_PMCON_3);
175     if (pwr_on)
176         byte &= ~1;             // Return to S0 (boot) after power is re-applied
177     else
178         byte |= 1;              // Return to S5
179     pci_write_config8(dev, GEN_PMCON_3, byte);
180     printk(BIOS_INFO, "set power %s after power fail\n", pwr_on?"on":"off");
181
182     /* Set up NMI on errors */
183     byte = inb(0x61);
184     byte &= ~(1 << 3); /* IOCHK# NMI Enable */
185     byte &= ~(1 << 2); /* PCI SERR# Enable */
186     outb(byte, 0x61);
187     byte = inb(0x70);
188     nmi_option = NMI_OFF;
189     get_option(&nmi_option, "nmi");
190     if (nmi_option) {
191         byte &= ~(1 << 7); /* set NMI */
192         outb(byte, 0x70);
193     }
194
195         /* Initialize the real time clock */
196         i82801cx_rtc_init(dev);
197
198         i82801cx_lpc_route_dma(dev, 0xff);
199
200         /* Initialize isa dma */
201         isa_dma_init();
202
203         i82801cx_1f0_misc(dev);
204 }
205
206 static void i82801cx_lpc_read_resources(device_t dev)
207 {
208         struct resource *res;
209
210         /* Get the normal PCI resources of this device. */
211         pci_dev_read_resources(dev);
212
213         /* Add an extra subtractive resource for both memory and I/O. */
214         res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
215         res->base = 0;
216         res->size = 0x1000;
217         res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
218                      IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
219
220         res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
221         res->base = 0xff800000;
222         res->size = 0x00800000; /* 8 MB for flash */
223         res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
224                      IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
225
226         res = new_resource(dev, 3); /* IOAPIC */
227         res->base = IO_APIC_ADDR;
228         res->size = 0x00001000;
229         res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
230 }
231
232 static struct device_operations lpc_ops  = {
233         .read_resources   = i82801cx_lpc_read_resources,
234         .set_resources    = pci_dev_set_resources,
235         .enable_resources = pci_dev_enable_resources,
236         .init             = lpc_init,
237         .scan_bus         = scan_static_bus,
238         .enable           = 0,
239 };
240
241 static const struct pci_driver lpc_driver __pci_driver = {
242         .ops    = &lpc_ops,
243         .vendor = PCI_VENDOR_ID_INTEL,
244         .device = PCI_DEVICE_ID_INTEL_82801CA_LPC,
245 };