enable bsp apic id lifting regarding ioapic setup
[coreboot.git] / src / southbridge / amd / amd8111 / amd8111_lpc.c
1 /*
2  * (C) 2003 Linux Networx, SuSE Linux AG
3  *  2006.1 yhlu add dest apicid for IRQ0
4  */
5 #include <console/console.h>
6 #include <device/device.h>
7 #include <device/pci.h>
8 #include <device/pci_ids.h>
9 #include <device/pci_ops.h>
10 #include <pc80/mc146818rtc.h>
11 #include <pc80/isa-dma.h>
12 #include <cpu/x86/lapic.h>
13 #include "amd8111.h"
14
15 #define NMI_OFF 0
16
17 struct ioapicreg {
18         unsigned int reg;
19         unsigned int value_low, value_high;
20 };
21
22 static struct ioapicreg ioapicregvalues[] = {
23 #define ALL             (0xff << 24)
24 #define NONE            (0)
25 #define DISABLED        (1 << 16)
26 #define ENABLED         (0 << 16)
27 #define TRIGGER_EDGE    (0 << 15)
28 #define TRIGGER_LEVEL   (1 << 15)
29 #define POLARITY_HIGH   (0 << 13)
30 #define POLARITY_LOW    (1 << 13)
31 #define PHYSICAL_DEST   (0 << 11)
32 #define LOGICAL_DEST    (1 << 11)
33 #define ExtINT          (7 << 8)
34 #define NMI             (4 << 8)
35 #define SMI             (2 << 8)
36 #define INT             (1 << 8)
37         /* IO-APIC virtual wire mode configuration */
38         /* mask, trigger, polarity, destination, delivery, vector */
39         {   0, ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT, NONE},
40         {   1, DISABLED, NONE},
41         {   2, DISABLED, NONE},
42         {   3, DISABLED, NONE},
43         {   4, DISABLED, NONE},
44         {   5, DISABLED, NONE},
45         {   6, DISABLED, NONE},
46         {   7, DISABLED, NONE},
47         {   8, DISABLED, NONE},
48         {   9, DISABLED, NONE},
49         {  10, DISABLED, NONE},
50         {  11, DISABLED, NONE},
51         {  12, DISABLED, NONE},
52         {  13, DISABLED, NONE},
53         {  14, DISABLED, NONE},
54         {  15, DISABLED, NONE},
55         {  16, DISABLED, NONE},
56         {  17, DISABLED, NONE},
57         {  18, DISABLED, NONE},
58         {  19, DISABLED, NONE},
59         {  20, DISABLED, NONE},
60         {  21, DISABLED, NONE},
61         {  22, DISABLED, NONE},
62         {  23, DISABLED, NONE},
63         /* Be careful and don't write past the end... */
64 };
65
66 static void setup_ioapic(void)
67 {
68         int i;
69         unsigned long value_low, value_high;
70         unsigned long ioapic_base = 0xfec00000;
71         volatile unsigned long *l;
72         struct ioapicreg *a = ioapicregvalues;
73
74         l = (unsigned long *) ioapic_base;
75
76         ioapicregvalues[0].value_high = lapicid()<<(56-32); 
77         
78         for (i = 0; i < sizeof(ioapicregvalues) / sizeof(ioapicregvalues[0]);
79              i++, a++) {
80                 l[0] = (a->reg * 2) + 0x10;
81                 l[4] = a->value_low;
82                 value_low = l[4];
83                 l[0] = (a->reg *2) + 0x11;
84                 l[4] = a->value_high;
85                 value_high = l[4];
86                 if ((i==0) && (value_low == 0xffffffff)) {
87                         printk_warning("IO APIC not responding.\n");
88                         return;
89                 }
90                 printk_spew("for IRQ, reg 0x%08x value 0x%08x 0x%08x\n", 
91                         a->reg, a->value_low, a->value_high);
92         }
93 }
94
95 static void enable_hpet(struct device *dev)
96 {
97         unsigned long hpet_address;
98         
99         pci_write_config32(dev,0xa0, 0xfed00001);
100         hpet_address = pci_read_config32(dev,0xa0)& 0xfffffffe;
101         printk_debug("enabling HPET @0x%x\n", hpet_address);
102         
103 }
104
105 static void lpc_init(struct device *dev)
106 {
107         uint8_t byte;
108         int nmi_option;
109
110         /* IO APIC initialization */
111         byte = pci_read_config8(dev, 0x4B);
112         byte |= 1;
113         pci_write_config8(dev, 0x4B, byte);
114         setup_ioapic();
115
116         /* posted memory write enable */
117         byte = pci_read_config8(dev, 0x46);
118         pci_write_config8(dev, 0x46, byte | (1<<0)); 
119
120         /* Enable 5Mib Rom window */
121         byte = pci_read_config8(dev, 0x43);
122         byte |= 0xc0;
123         pci_write_config8(dev, 0x43, byte);
124
125         /* Enable Port 92 fast reset */
126         byte = pci_read_config8(dev, 0x41);
127         byte |= (1 << 5);
128         pci_write_config8(dev, 0x41, byte);
129
130         /* Enable Error reporting */
131         /* Set up sync flood detected */
132         byte = pci_read_config8(dev, 0x47);
133         byte |= (1 << 1);
134         pci_write_config8(dev, 0x47, byte);
135
136         /* Set up NMI on errors */
137         byte = pci_read_config8(dev, 0x40);
138         byte |= (1 << 1); /* clear PW2LPC error */
139         byte |= (1 << 6); /* clear LPCERR */
140         pci_write_config8(dev, 0x40, byte);
141         nmi_option = NMI_OFF;
142         get_option(&nmi_option, "nmi");
143         if (nmi_option) {                       
144                 byte |= (1 << 7); /* set NMI */
145                 pci_write_config8(dev, 0x40, byte);
146         }
147         
148         /* Initialize the real time clock */
149         rtc_init(0);
150
151         /* Initialize isa dma */
152         isa_dma_init();
153
154         /* Initialize the High Precision Event Timers */
155         enable_hpet(dev);
156 }
157
158 static void amd8111_lpc_read_resources(device_t dev)
159 {
160         struct resource *res;
161
162         /* Get the normal pci resources of this device */
163         pci_dev_read_resources(dev);
164
165         /* Add an extra subtractive resource for both memory and I/O */
166         res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
167         res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
168         
169         res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
170         res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
171 }
172
173 static void amd8111_lpc_enable_resources(device_t dev)
174 {
175         pci_dev_enable_resources(dev);
176         enable_childrens_resources(dev);
177 }
178
179 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
180 {
181         pci_write_config32(dev, 0x70, 
182                 ((device & 0xffff) << 16) | (vendor & 0xffff));
183 }
184
185 static struct pci_operations lops_pci = {
186         .set_subsystem = lpci_set_subsystem,
187 };
188
189 static struct device_operations lpc_ops  = {
190         .read_resources   = amd8111_lpc_read_resources,
191         .set_resources    = pci_dev_set_resources,
192         .enable_resources = amd8111_lpc_enable_resources,
193         .init             = lpc_init,
194         .scan_bus         = scan_static_bus,
195         .enable           = amd8111_enable,
196         .ops_pci          = &lops_pci,
197 };
198
199 static struct pci_driver lpc_driver __pci_driver = {
200         .ops    = &lpc_ops,
201         .vendor = PCI_VENDOR_ID_AMD,
202         .device = PCI_DEVICE_ID_AMD_8111_ISA,
203 };