- Better memory I/O space distinguishing in amd_mtrr.c
[coreboot.git] / src / southbridge / amd / amd8111 / amd8111_lpc.c
1 /*
2  * (C) 2003 Linux Networx, SuSE Linux AG
3  */
4 #include <console/console.h>
5 #include <device/device.h>
6 #include <device/pci.h>
7 #include <device/pci_ids.h>
8 #include <device/pci_ops.h>
9 #include <pc80/mc146818rtc.h>
10 #include <pc80/isa-dma.h>
11 #include "amd8111.h"
12
13 #define NMI_OFF 0
14
15 struct ioapicreg {
16         unsigned int reg;
17         unsigned int value_low, value_high;
18 };
19
20 static struct ioapicreg ioapicregvalues[] = {
21 #define ALL             (0xff << 24)
22 #define NONE            (0)
23 #define DISABLED        (1 << 16)
24 #define ENABLED         (0 << 16)
25 #define TRIGGER_EDGE    (0 << 15)
26 #define TRIGGER_LEVEL   (1 << 15)
27 #define POLARITY_HIGH   (0 << 13)
28 #define POLARITY_LOW    (1 << 13)
29 #define PHYSICAL_DEST   (0 << 11)
30 #define LOGICAL_DEST    (1 << 11)
31 #define ExtINT          (7 << 8)
32 #define NMI             (4 << 8)
33 #define SMI             (2 << 8)
34 #define INT             (1 << 8)
35         /* IO-APIC virtual wire mode configuration */
36         /* mask, trigger, polarity, destination, delivery, vector */
37         {   0, ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT, NONE},
38         {   1, DISABLED, NONE},
39         {   2, DISABLED, NONE},
40         {   3, DISABLED, NONE},
41         {   4, DISABLED, NONE},
42         {   5, DISABLED, NONE},
43         {   6, DISABLED, NONE},
44         {   7, DISABLED, NONE},
45         {   8, DISABLED, NONE},
46         {   9, DISABLED, NONE},
47         {  10, DISABLED, NONE},
48         {  11, DISABLED, NONE},
49         {  12, DISABLED, NONE},
50         {  13, DISABLED, NONE},
51         {  14, DISABLED, NONE},
52         {  15, DISABLED, NONE},
53         {  16, DISABLED, NONE},
54         {  17, DISABLED, NONE},
55         {  18, DISABLED, NONE},
56         {  19, DISABLED, NONE},
57         {  20, DISABLED, NONE},
58         {  21, DISABLED, NONE},
59         {  22, DISABLED, NONE},
60         {  23, DISABLED, NONE},
61         /* Be careful and don't write past the end... */
62 };
63
64 static void setup_ioapic(void)
65 {
66         int i;
67         unsigned long value_low, value_high;
68         unsigned long ioapic_base = 0xfec00000;
69         volatile unsigned long *l;
70         struct ioapicreg *a = ioapicregvalues;
71
72         l = (unsigned long *) ioapic_base;
73
74         for (i = 0; i < sizeof(ioapicregvalues) / sizeof(ioapicregvalues[0]);
75              i++, a++) {
76                 l[0] = (a->reg * 2) + 0x10;
77                 l[4] = a->value_low;
78                 value_low = l[4];
79                 l[0] = (a->reg *2) + 0x11;
80                 l[4] = a->value_high;
81                 value_high = l[4];
82                 if ((i==0) && (value_low == 0xffffffff)) {
83                         printk_warning("IO APIC not responding.\n");
84                         return;
85                 }
86                 printk_spew("for IRQ, reg 0x%08x value 0x%08x 0x%08x\n", 
87                         a->reg, a->value_low, a->value_high);
88         }
89 }
90
91 static void enable_hpet(struct device *dev)
92 {
93         unsigned long hpet_address;
94         
95         pci_write_config32(dev,0xa0, 0xfed00001);
96         hpet_address=pci_read_config32(dev,0xa0)& 0xfffffffe;
97         printk_debug("enabling HPET @0x%x\n", hpet_address);
98 }
99
100 static void lpc_init(struct device *dev)
101 {
102         uint8_t byte;
103         int pwr_on=-1;
104         int nmi_option;
105
106         /* IO APIC initialization */
107         byte = pci_read_config8(dev, 0x4B);
108         byte |= 1;
109         pci_write_config8(dev, 0x4B, byte);
110         setup_ioapic();
111
112         /* posted memory write enable */
113         byte = pci_read_config8(dev, 0x46);
114         pci_write_config8(dev, 0x46, byte | (1<<0)); 
115
116         /* power after power fail */
117         byte = pci_read_config8(dev, 0x43);
118         if (pwr_on) { 
119                 byte &= ~(1<<6);
120         } else {
121                 byte |= (1<<6);
122         }
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
180 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
181 {
182         pci_write_config32(dev, 0x70, 
183                 ((device & 0xffff) << 16) | (vendor & 0xffff));
184 }
185 static struct pci_operations lops_pci = {
186         .set_subsystem = lpci_set_subsystem,
187 };
188 static struct device_operations lpc_ops  = {
189         .read_resources   = amd8111_lpc_read_resources,
190         .set_resources    = pci_dev_set_resources,
191         .enable_resources = amd8111_lpc_enable_resources,
192         .init             = lpc_init,
193         .scan_bus         = scan_static_bus,
194         .enable           = amd8111_enable,
195         .ops_pci          = &lops_pci,
196 };
197
198 static struct pci_driver lpc_driver __pci_driver = {
199         .ops    = &lpc_ops,
200         .vendor = PCI_VENDOR_ID_AMD,
201         .device = PCI_DEVICE_ID_AMD_8111_ISA,
202 };