Apparently I'm not the only one who forgets which way the outb and
[coreboot.git] / src / southbridge / nvidia / mcp55 / mcp55_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) 2004 Tyan Computer
7  * Written by Yinghai Lu <yhlu@tyan.com> for Tyan Computer.
8  * Copyright (C) 2006,2007 AMD
9  * Written by Yinghai Lu <yinghai.lu@amd.com> for AMD.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
24  */
25
26 #include <console/console.h>
27 #include <device/device.h>
28 #include <device/pci.h>
29 #include <device/pnp.h>
30 #include <device/pci_ids.h>
31 #include <device/pci_ops.h>
32 #include <pc80/mc146818rtc.h>
33 #include <pc80/isa-dma.h>
34 #include <bitops.h>
35 #include <arch/io.h>
36 #include <cpu/x86/lapic.h>
37 #include <stdlib.h>
38 #include "mcp55.h"
39
40 #define NMI_OFF 0
41
42 struct ioapicreg {
43         unsigned int reg;
44         unsigned int value_low, value_high;
45 };
46
47 static struct ioapicreg ioapicregvalues[] = {
48 #define ALL             (0xff << 24)
49 #define NONE            (0)
50 #define DISABLED        (1 << 16)
51 #define ENABLED         (0 << 16)
52 #define TRIGGER_EDGE    (0 << 15)
53 #define TRIGGER_LEVEL   (1 << 15)
54 #define POLARITY_HIGH   (0 << 13)
55 #define POLARITY_LOW    (1 << 13)
56 #define PHYSICAL_DEST   (0 << 11)
57 #define LOGICAL_DEST    (1 << 11)
58 #define ExtINT          (7 << 8)
59 #define NMI             (4 << 8)
60 #define SMI             (2 << 8)
61 #define INT             (1 << 8)
62         /* IO-APIC virtual wire mode configuration */
63         /* mask, trigger, polarity, destination, delivery, vector */
64         {   0, ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT, NONE},
65         {   1, DISABLED, NONE},
66         {   2, DISABLED, NONE},
67         {   3, DISABLED, NONE},
68         {   4, DISABLED, NONE},
69         {   5, DISABLED, NONE},
70         {   6, DISABLED, NONE},
71         {   7, DISABLED, NONE},
72         {   8, DISABLED, NONE},
73         {   9, DISABLED, NONE},
74         {  10, DISABLED, NONE},
75         {  11, DISABLED, NONE},
76         {  12, DISABLED, NONE},
77         {  13, DISABLED, NONE},
78         {  14, DISABLED, NONE},
79         {  15, DISABLED, NONE},
80         {  16, DISABLED, NONE},
81         {  17, DISABLED, NONE},
82         {  18, DISABLED, NONE},
83         {  19, DISABLED, NONE},
84         {  20, DISABLED, NONE},
85         {  21, DISABLED, NONE},
86         {  22, DISABLED, NONE},
87         {  23, DISABLED, NONE},
88         /* Be careful and don't write past the end... */
89 };
90
91 static void setup_ioapic(unsigned long ioapic_base, int master)
92 {
93         int i;
94         unsigned long value_low, value_high;
95 //      unsigned long ioapic_base = 0xfec00000;
96         volatile unsigned long *l;
97         struct ioapicreg *a = ioapicregvalues;
98
99         if (master) {
100                 ioapicregvalues[0].value_high = lapicid()<<(56-32);
101                 ioapicregvalues[0].value_low = ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST | ExtINT;
102         }
103         else {
104                 ioapicregvalues[0].value_high = NONE;
105                 ioapicregvalues[0].value_low = DISABLED;
106         }
107
108         l = (unsigned long *) ioapic_base;
109
110         for (i = 0; i < ARRAY_SIZE(ioapicregvalues);
111              i++, a++) {
112                 l[0] = (a->reg * 2) + 0x10;
113                 l[4] = a->value_low;
114                 value_low = l[4];
115                 l[0] = (a->reg *2) + 0x11;
116                 l[4] = a->value_high;
117                 value_high = l[4];
118                 if ((i==0) && (value_low == 0xffffffff)) {
119                         printk_warning("IO APIC not responding.\n");
120                         return;
121                 }
122                 printk_spew("for IRQ, reg 0x%08x value 0x%08x 0x%08x\n",
123                             a->reg, a->value_low, a->value_high);
124         }
125 }
126
127 // 0x7a or e3
128 #define PREVIOUS_POWER_STATE    0x7A
129
130 #define MAINBOARD_POWER_OFF     0
131 #define MAINBOARD_POWER_ON      1
132 #define SLOW_CPU_OFF            0
133 #define SLOW_CPU__ON            1
134
135 #ifndef CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL
136 #define CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
137 #endif
138
139 static void lpc_common_init(device_t dev, int master)
140 {
141         uint8_t byte;
142         uint32_t dword;
143
144         /* IO APIC initialization */
145         byte = pci_read_config8(dev, 0x74);
146         byte |= (1<<0); // enable APIC
147         pci_write_config8(dev, 0x74, byte);
148         dword = pci_read_config32(dev, PCI_BASE_ADDRESS_1); // 0x14
149
150         setup_ioapic(dword, master);
151 }
152
153 static void lpc_slave_init(device_t dev)
154 {
155         lpc_common_init(dev, 0);
156 }
157
158 static void enable_hpet(struct device *dev)
159 {
160         unsigned long hpet_address;
161
162         pci_write_config32(dev,0x44, 0xfed00001);
163         hpet_address=pci_read_config32(dev,0x44)& 0xfffffffe;
164         printk_debug("enabling HPET @0x%x\n", hpet_address);
165 }
166
167 static void lpc_init(device_t dev)
168 {
169         uint8_t byte;
170         uint8_t byte_old;
171         int on;
172         int nmi_option;
173
174         lpc_common_init(dev, 1);
175
176 #if 0
177         /* posted memory write enable */
178         byte = pci_read_config8(dev, 0x46);
179         pci_write_config8(dev, 0x46, byte | (1<<0));
180 #endif
181         /* power after power fail */
182
183 #if 1
184         on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
185         get_option(&on, "power_on_after_fail");
186         byte = pci_read_config8(dev, PREVIOUS_POWER_STATE);
187         byte &= ~0x40;
188         if (!on) {
189                 byte |= 0x40;
190         }
191         pci_write_config8(dev, PREVIOUS_POWER_STATE, byte);
192         printk_info("set power %s after power fail\n", on?"on":"off");
193 #endif
194         /* Throttle the CPU speed down for testing */
195         on = SLOW_CPU_OFF;
196         get_option(&on, "slow_cpu");
197         if(on) {
198                 uint16_t pm10_bar;
199                 uint32_t dword;
200                 pm10_bar = (pci_read_config16(dev, 0x60)&0xff00);
201                 outl(((on<<1)+0x10)  ,(pm10_bar + 0x10));
202                 dword = inl(pm10_bar + 0x10);
203                 on = 8-on;
204                 printk_debug("Throttling CPU %2d.%1.1d percent.\n",
205                              (on*12)+(on>>1),(on&1)*5);
206         }
207
208 #if 0
209 // default is enabled
210         /* Enable Port 92 fast reset */
211         byte = pci_read_config8(dev, 0xe8);
212         byte |= ~(1 << 3);
213         pci_write_config8(dev, 0xe8, byte);
214 #endif
215
216         /* Enable Error reporting */
217         /* Set up sync flood detected */
218         byte = pci_read_config8(dev, 0x47);
219         byte |= (1 << 1);
220         pci_write_config8(dev, 0x47, byte);
221
222         /* Set up NMI on errors */
223         byte = inb(0x70); // RTC70
224         byte_old = byte;
225         nmi_option = NMI_OFF;
226         get_option(&nmi_option, "nmi");
227         if (nmi_option) {
228                 byte &= ~(1 << 7); /* set NMI */
229         } else {
230                 byte |= ( 1 << 7); // Can not mask NMI from PCI-E and NMI_NOW
231         }
232         if( byte != byte_old) {
233                 outb(byte, 0x70);
234         }
235
236         /* Initialize the real time clock */
237         rtc_init(0);
238
239         /* Initialize isa dma */
240         isa_dma_init();
241
242         /* Initialize the High Precision Event Timers */
243         enable_hpet(dev);
244
245 }
246
247 static void mcp55_lpc_read_resources(device_t dev)
248 {
249         struct resource *res;
250
251         /* Get the normal PCI resources of this device. */
252         /* We got one for APIC, or one more for TRAP. */
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 /**
275  * @brief Enable resources for children devices
276  *
277  * @param dev the device whos children's resources are to be enabled
278  *
279  * This function is called by the global enable_resources() indirectly via the
280  * device_operation::enable_resources() method of devices.
281  *
282  * Indirect mutual recursion:
283  *      enable_childrens_resources() -> enable_resources()
284  *      enable_resources() -> device_operation::enable_resources()
285  *      device_operation::enable_resources() -> enable_children_resources()
286  */
287 static void mcp55_lpc_enable_childrens_resources(device_t dev)
288 {
289         unsigned link;
290         uint32_t reg, reg_var[4];
291         int i;
292         int var_num = 0;
293
294         reg = pci_read_config32(dev, 0xa0);
295
296         for (link = 0; link < dev->links; link++) {
297                 device_t child;
298                 for (child = dev->link[link].children; child; child = child->sibling) {
299                         enable_resources(child);
300                         if(child->enabled && (child->path.type == DEVICE_PATH_PNP)) {
301                                 for(i=0;i<child->resources;i++) {
302                                         struct resource *res;
303                                         unsigned long base, end; // don't need long long
304                                         res = &child->resource[i];
305                                         if(!(res->flags & IORESOURCE_IO)) continue;
306                                         base = res->base;
307                                         end = resource_end(res);
308                                         printk_debug("mcp55 lpc decode:%s, base=0x%08x, end=0x%08x\n",dev_path(child),base, end);
309                                         switch(base) {
310                                         case 0x3f8: // COM1
311                                                 reg |= (1<<0);  break;
312                                         case 0x2f8: // COM2
313                                                 reg |= (1<<1);  break;
314                                         case 0x378: // Parallal 1
315                                                 reg |= (1<<24); break;
316                                         case 0x3f0: // FD0
317                                                 reg |= (1<<20); break;
318                                         case 0x220:  // Aduio 0
319                                                 reg |= (1<<8);  break;
320                                         case 0x300:  // Midi 0
321                                                 reg |= (1<<12); break;
322                                         }
323                                         if( (base == 0x290) || (base >= 0x400)) {
324                                                 if(var_num>=4) continue; // only 4 var ; compact them ?
325                                                 reg |= (1<<(28+var_num));
326                                                 reg_var[var_num++] = (base & 0xffff)|((end & 0xffff)<<16);
327                                         }
328                                 }
329                         }
330                 }
331         }
332         pci_write_config32(dev, 0xa0, reg);
333         for(i=0;i<var_num;i++) {
334                 pci_write_config32(dev, 0xa8 + i*4, reg_var[i]);
335         }
336
337
338 }
339
340 static void mcp55_lpc_enable_resources(device_t dev)
341 {
342         pci_dev_enable_resources(dev);
343         mcp55_lpc_enable_childrens_resources(dev);
344 }
345
346 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
347 {
348         pci_write_config32(dev, 0x40,
349                 ((device & 0xffff) << 16) | (vendor & 0xffff));
350 }
351
352 static struct pci_operations lops_pci = {
353         .set_subsystem  = lpci_set_subsystem,
354 };
355
356 static struct device_operations lpc_ops  = {
357         .read_resources = mcp55_lpc_read_resources,
358         .set_resources  = pci_dev_set_resources,
359         .enable_resources       = mcp55_lpc_enable_resources,
360         .init           = lpc_init,
361         .scan_bus       = scan_static_bus,
362 //      .enable         = mcp55_enable,
363         .ops_pci        = &lops_pci,
364 };
365 static const struct pci_driver lpc_driver __pci_driver = {
366         .ops    = &lpc_ops,
367         .vendor = PCI_VENDOR_ID_NVIDIA,
368         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC,
369 };
370
371 static const struct pci_driver lpc_driver_pro __pci_driver = {
372         .ops    = &lpc_ops,
373         .vendor = PCI_VENDOR_ID_NVIDIA,
374         .device = PCI_DEVICE_ID_NVIDIA_MCP55_PRO,
375 };
376
377 static const struct pci_driver lpc_driver_lpc2 __pci_driver = {
378         .ops    = &lpc_ops,
379         .vendor = PCI_VENDOR_ID_NVIDIA,
380         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_2,
381 };
382 static const struct pci_driver lpc_driver_lpc3 __pci_driver = {
383         .ops    = &lpc_ops,
384         .vendor = PCI_VENDOR_ID_NVIDIA,
385         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_3,
386 };
387 static const struct pci_driver lpc_driver_lpc4 __pci_driver = {
388         .ops    = &lpc_ops,
389         .vendor = PCI_VENDOR_ID_NVIDIA,
390         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_4,
391 };
392 static const struct pci_driver lpc_driver_lpc5 __pci_driver = {
393         .ops    = &lpc_ops,
394         .vendor = PCI_VENDOR_ID_NVIDIA,
395         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_5,
396 };
397 static const struct pci_driver lpc_driver_lpc6 __pci_driver = {
398         .ops    = &lpc_ops,
399         .vendor = PCI_VENDOR_ID_NVIDIA,
400         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_6,
401 };
402
403 static struct device_operations lpc_slave_ops  = {
404         .read_resources = mcp55_lpc_read_resources,
405         .set_resources  = pci_dev_set_resources,
406         .enable_resources       = pci_dev_enable_resources,
407         .init           = lpc_slave_init,
408 //      .enable         = mcp55_enable,
409         .ops_pci        = &lops_pci,
410 };
411
412 static const struct pci_driver lpc_driver_slave __pci_driver = {
413         .ops    = &lpc_slave_ops,
414         .vendor = PCI_VENDOR_ID_NVIDIA,
415         .device = PCI_DEVICE_ID_NVIDIA_MCP55_SLAVE,
416 };