040ab622736de23bc9448f8d376fca0baa653611
[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 <arch/ioapic.h>
37 #include <cpu/x86/lapic.h>
38 #include <stdlib.h>
39 #include "mcp55.h"
40
41 #define NMI_OFF 0
42
43 // 0x7a or e3
44 #define PREVIOUS_POWER_STATE    0x7A
45
46 #define MAINBOARD_POWER_OFF     0
47 #define MAINBOARD_POWER_ON      1
48 #define SLOW_CPU_OFF            0
49 #define SLOW_CPU__ON            1
50
51 #ifndef CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL
52 #define CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
53 #endif
54
55 static void lpc_common_init(device_t dev, int master)
56 {
57         uint8_t byte;
58         uint32_t ioapic_base;
59
60         /* IO APIC initialization */
61         byte = pci_read_config8(dev, 0x74);
62         byte |= (1<<0); // enable APIC
63         pci_write_config8(dev, 0x74, byte);
64         ioapic_base = pci_read_config32(dev, PCI_BASE_ADDRESS_1); // 0x14
65
66         if (master)
67                 setup_ioapic(ioapic_base, 0);
68         else
69                 clear_ioapic(ioapic_base);
70 }
71
72 static void lpc_slave_init(device_t dev)
73 {
74         lpc_common_init(dev, 0);
75 }
76
77 static void enable_hpet(struct device *dev)
78 {
79         unsigned long hpet_address;
80
81         pci_write_config32(dev,0x44, 0xfed00001);
82         hpet_address=pci_read_config32(dev,0x44)& 0xfffffffe;
83         printk(BIOS_DEBUG, "enabling HPET @0x%lx\n", hpet_address);
84 }
85
86 static void lpc_init(device_t dev)
87 {
88         uint8_t byte;
89         uint8_t byte_old;
90         int on;
91         int nmi_option;
92
93         lpc_common_init(dev, 1);
94
95 #if 0
96         /* posted memory write enable */
97         byte = pci_read_config8(dev, 0x46);
98         pci_write_config8(dev, 0x46, byte | (1<<0));
99 #endif
100         /* power after power fail */
101
102 #if 1
103         on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
104         get_option(&on, "power_on_after_fail");
105         byte = pci_read_config8(dev, PREVIOUS_POWER_STATE);
106         byte &= ~0x40;
107         if (!on) {
108                 byte |= 0x40;
109         }
110         pci_write_config8(dev, PREVIOUS_POWER_STATE, byte);
111         printk(BIOS_INFO, "set power %s after power fail\n", on?"on":"off");
112 #endif
113         /* Throttle the CPU speed down for testing */
114         on = SLOW_CPU_OFF;
115         get_option(&on, "slow_cpu");
116         if(on) {
117                 uint16_t pm10_bar;
118                 uint32_t dword;
119                 pm10_bar = (pci_read_config16(dev, 0x60)&0xff00);
120                 outl(((on<<1)+0x10)  ,(pm10_bar + 0x10));
121                 dword = inl(pm10_bar + 0x10);
122                 on = 8-on;
123                 printk(BIOS_DEBUG, "Throttling CPU %2d.%1.1d percent.\n",
124                              (on*12)+(on>>1),(on&1)*5);
125         }
126
127 #if 0
128 // default is enabled
129         /* Enable Port 92 fast reset */
130         byte = pci_read_config8(dev, 0xe8);
131         byte |= ~(1 << 3);
132         pci_write_config8(dev, 0xe8, byte);
133 #endif
134
135         /* Enable Error reporting */
136         /* Set up sync flood detected */
137         byte = pci_read_config8(dev, 0x47);
138         byte |= (1 << 1);
139         pci_write_config8(dev, 0x47, byte);
140
141         /* Set up NMI on errors */
142         byte = inb(0x70); // RTC70
143         byte_old = byte;
144         nmi_option = NMI_OFF;
145         get_option(&nmi_option, "nmi");
146         if (nmi_option) {
147                 byte &= ~(1 << 7); /* set NMI */
148         } else {
149                 byte |= ( 1 << 7); // Can not mask NMI from PCI-E and NMI_NOW
150         }
151         if( byte != byte_old) {
152                 outb(byte, 0x70);
153         }
154
155         /* Initialize the real time clock */
156         rtc_init(0);
157
158         /* Initialize isa dma */
159         isa_dma_init();
160
161         /* Initialize the High Precision Event Timers */
162         enable_hpet(dev);
163
164 }
165
166 static void mcp55_lpc_read_resources(device_t dev)
167 {
168         struct resource *res;
169
170         /* Get the normal PCI resources of this device. */
171         /* We got one for APIC, or one more for TRAP. */
172         pci_dev_read_resources(dev);
173
174         /* Add an extra subtractive resource for both memory and I/O. */
175         res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
176         res->base = 0;
177         res->size = 0x1000;
178         res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
179                      IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
180
181         res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
182         res->base = 0xff800000;
183         res->size = 0x00800000; /* 8 MB for flash */
184         res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
185                      IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
186
187         res = new_resource(dev, 3); /* IOAPIC */
188         res->base = 0xfec00000;
189         res->size = 0x00001000;
190         res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
191 }
192
193 /**
194  * @brief Enable resources for children devices
195  *
196  * @param dev the device whos children's resources are to be enabled
197  *
198  */
199 static void mcp55_lpc_enable_childrens_resources(device_t dev)
200 {
201         uint32_t reg, reg_var[4];
202         int i;
203         int var_num = 0;
204         struct bus *link;
205
206         reg = pci_read_config32(dev, 0xa0);
207
208         for (link = dev->link_list; link; link = link->next) {
209                 device_t child;
210                 for (child = link->children; child; child = child->sibling) {
211                         if(child->enabled && (child->path.type == DEVICE_PATH_PNP)) {
212                                 struct resource *res;
213                                 for(res = child->resource_list; res; res = res->next) {
214                                         unsigned long base, end; // don't need long long
215                                         if(!(res->flags & IORESOURCE_IO)) continue;
216                                         base = res->base;
217                                         end = resource_end(res);
218                                         printk(BIOS_DEBUG, "mcp55 lpc decode:%s, base=0x%08lx, end=0x%08lx\n",dev_path(child),base, end);
219                                         switch(base) {
220                                         case 0x3f8: // COM1
221                                                 reg |= (1<<0);  break;
222                                         case 0x2f8: // COM2
223                                                 reg |= (1<<1);  break;
224                                         case 0x378: // Parallal 1
225                                                 reg |= (1<<24); break;
226                                         case 0x3f0: // FD0
227                                                 reg |= (1<<20); break;
228                                         case 0x220:  // Aduio 0
229                                                 reg |= (1<<8);  break;
230                                         case 0x300:  // Midi 0
231                                                 reg |= (1<<12); break;
232                                         }
233                                         if( (base == 0x290) || (base >= 0x400)) {
234                                                 if(var_num>=4) continue; // only 4 var ; compact them ?
235                                                 reg |= (1<<(28+var_num));
236                                                 reg_var[var_num++] = (base & 0xffff)|((end & 0xffff)<<16);
237                                         }
238                                 }
239                         }
240                 }
241         }
242         pci_write_config32(dev, 0xa0, reg);
243         for(i=0;i<var_num;i++) {
244                 pci_write_config32(dev, 0xa8 + i*4, reg_var[i]);
245         }
246
247
248 }
249
250 static void mcp55_lpc_enable_resources(device_t dev)
251 {
252         pci_dev_enable_resources(dev);
253         mcp55_lpc_enable_childrens_resources(dev);
254 }
255
256 static void lpci_set_subsystem(device_t dev, unsigned vendor, unsigned device)
257 {
258         pci_write_config32(dev, 0x40,
259                 ((device & 0xffff) << 16) | (vendor & 0xffff));
260 }
261
262 static struct pci_operations lops_pci = {
263         .set_subsystem  = lpci_set_subsystem,
264 };
265
266 static struct device_operations lpc_ops  = {
267         .read_resources = mcp55_lpc_read_resources,
268         .set_resources  = pci_dev_set_resources,
269         .enable_resources       = mcp55_lpc_enable_resources,
270         .init           = lpc_init,
271         .scan_bus       = scan_static_bus,
272 //      .enable         = mcp55_enable,
273         .ops_pci        = &lops_pci,
274 };
275 static const struct pci_driver lpc_driver __pci_driver = {
276         .ops    = &lpc_ops,
277         .vendor = PCI_VENDOR_ID_NVIDIA,
278         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC,
279 };
280
281 static const struct pci_driver lpc_driver_pro __pci_driver = {
282         .ops    = &lpc_ops,
283         .vendor = PCI_VENDOR_ID_NVIDIA,
284         .device = PCI_DEVICE_ID_NVIDIA_MCP55_PRO,
285 };
286
287 static const struct pci_driver lpc_driver_lpc2 __pci_driver = {
288         .ops    = &lpc_ops,
289         .vendor = PCI_VENDOR_ID_NVIDIA,
290         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_2,
291 };
292 static const struct pci_driver lpc_driver_lpc3 __pci_driver = {
293         .ops    = &lpc_ops,
294         .vendor = PCI_VENDOR_ID_NVIDIA,
295         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_3,
296 };
297 static const struct pci_driver lpc_driver_lpc4 __pci_driver = {
298         .ops    = &lpc_ops,
299         .vendor = PCI_VENDOR_ID_NVIDIA,
300         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_4,
301 };
302 static const struct pci_driver lpc_driver_lpc5 __pci_driver = {
303         .ops    = &lpc_ops,
304         .vendor = PCI_VENDOR_ID_NVIDIA,
305         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_5,
306 };
307 static const struct pci_driver lpc_driver_lpc6 __pci_driver = {
308         .ops    = &lpc_ops,
309         .vendor = PCI_VENDOR_ID_NVIDIA,
310         .device = PCI_DEVICE_ID_NVIDIA_MCP55_LPC_6,
311 };
312
313 static struct device_operations lpc_slave_ops  = {
314         .read_resources = mcp55_lpc_read_resources,
315         .set_resources  = pci_dev_set_resources,
316         .enable_resources       = pci_dev_enable_resources,
317         .init           = lpc_slave_init,
318 //      .enable         = mcp55_enable,
319         .ops_pci        = &lops_pci,
320 };
321
322 static const struct pci_driver lpc_driver_slave __pci_driver = {
323         .ops    = &lpc_slave_ops,
324         .vendor = PCI_VENDOR_ID_NVIDIA,
325         .device = PCI_DEVICE_ID_NVIDIA_MCP55_SLAVE,
326 };