CK804: Cosmetic fixes, switch to u8 et al.
[coreboot.git] / src / southbridge / nvidia / ck804 / 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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; version 2 of the License.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22
23 #include <console/console.h>
24 #include <device/device.h>
25 #include <device/pci.h>
26 #include <device/pnp.h>
27 #include <device/pci_ids.h>
28 #include <device/pci_ops.h>
29 #include <pc80/mc146818rtc.h>
30 #include <pc80/isa-dma.h>
31 #include <bitops.h>
32 #include <arch/io.h>
33 #include <arch/ioapic.h>
34 #include <cpu/x86/lapic.h>
35 #include <stdlib.h>
36 #include "ck804.h"
37
38 #define CK804_CHIP_REV 2
39
40 #define NMI_OFF 0
41
42 // 0x7a or e3
43 #define PREVIOUS_POWER_STATE 0x7A
44
45 #define MAINBOARD_POWER_OFF 0
46 #define MAINBOARD_POWER_ON 1
47 #define SLOW_CPU_OFF 0
48 #define SLOW_CPU__ON 1
49
50 #ifndef CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL
51 #define CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
52 #endif
53
54 static void lpc_common_init(device_t dev)
55 {
56         u8 byte;
57         u32 dword;
58
59         /* I/O APIC initialization. */
60         byte = pci_read_config8(dev, 0x74);
61         byte |= (1 << 0);       /* Enable APIC. */
62         pci_write_config8(dev, 0x74, byte);
63         dword = pci_read_config32(dev, PCI_BASE_ADDRESS_1);     /* 0x14 */
64
65         setup_ioapic(dword, 0); /* Don't rename IOAPIC ID. */
66
67 #if 1
68         dword = pci_read_config32(dev, 0xe4);
69         dword |= (1 << 23);
70         pci_write_config32(dev, 0xe4, dword);
71 #endif
72 }
73
74 static void lpc_slave_init(device_t dev)
75 {
76         lpc_common_init(dev);
77 }
78
79 static void rom_dummy_write(device_t dev)
80 {
81         u8 old, new;
82         u8 *p;
83
84         old = pci_read_config8(dev, 0x88);
85         new = old | 0xc0;
86         if (new != old)
87                 pci_write_config8(dev, 0x88, new);
88         /* Enable write. */
89         old = pci_read_config8(dev, 0x6d);
90         new = old | 0x01;
91         if (new != old)
92                 pci_write_config8(dev, 0x6d, new);
93
94         /* Dummy write. */
95         p = (u8 *) 0xffffffe0;
96         old = 0;
97         *p = old;
98         old = *p;
99
100         /* Disable write. */
101         old = pci_read_config8(dev, 0x6d);
102         new = old & 0xfe;
103         if (new != old)
104                 pci_write_config8(dev, 0x6d, new);
105 }
106
107 static void enable_hpet(struct device *dev)
108 {
109         unsigned long hpet_address;
110
111         pci_write_config32(dev, 0x44, 0xfed00001);
112         hpet_address = pci_read_config32(dev, 0x44) & 0xfffffffe;
113         printk(BIOS_DEBUG, "Enabling HPET @0x%lx\n", hpet_address);
114 }
115
116 unsigned pm_base = 0;
117
118 static void lpc_init(device_t dev)
119 {
120         u8 byte, byte_old;
121         int on, nmi_option;
122
123         lpc_common_init(dev);
124
125         pm_base = pci_read_config32(dev, 0x60) & 0xff00;
126         printk(BIOS_INFO, "%s: pm_base = %x \n", __func__, pm_base);
127
128 #if CK804_CHIP_REV == 1
129         if (dev->bus->secondary != 1)
130                 return;
131 #endif
132
133 #if 0
134         /* Posted memory write enable */
135         byte = pci_read_config8(dev, 0x46);
136         pci_write_config8(dev, 0x46, byte | (1 << 0));
137 #endif
138
139         /* Power after power fail */
140         on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
141         get_option(&on, "power_on_after_fail");
142         byte = pci_read_config8(dev, PREVIOUS_POWER_STATE);
143         byte &= ~0x40;
144         if (!on)
145                 byte |= 0x40;
146         pci_write_config8(dev, PREVIOUS_POWER_STATE, byte);
147         printk(BIOS_INFO, "set power %s after power fail\n", on ? "on" : "off");
148
149         /* Throttle the CPU speed down for testing. */
150         on = SLOW_CPU_OFF;
151         get_option(&on, "slow_cpu");
152         if (on) {
153                 u16 pm10_bar;
154                 u32 dword;
155                 pm10_bar = (pci_read_config16(dev, 0x60) & 0xff00);
156                 outl(((on << 1) + 0x10), (pm10_bar + 0x10));
157                 dword = inl(pm10_bar + 0x10);
158                 on = 8 - on;
159                 printk(BIOS_DEBUG, "Throttling CPU %2d.%1.1d percent.\n",
160                        (on * 12) + (on >> 1), (on & 1) * 5);
161         }
162 #if 0
163         /* Enable Port 92 fast reset (default is enabled). */
164         byte = pci_read_config8(dev, 0xe8);
165         byte |= ~(1 << 3);
166         pci_write_config8(dev, 0xe8, byte);
167 #endif
168
169         /* Enable Error reporting. */
170         /* Set up sync flood detected. */
171         byte = pci_read_config8(dev, 0x47);
172         byte |= (1 << 1);
173         pci_write_config8(dev, 0x47, byte);
174
175         /* Set up NMI on errors. */
176         byte = inb(0x70);               /* RTC70 */
177         byte_old = byte;
178         nmi_option = NMI_OFF;
179         get_option(&nmi_option, "nmi");
180         if (nmi_option)
181                 byte &= ~(1 << 7); /* Set NMI. */
182         else
183                 byte |= (1 << 7); /* Can't mask NMI from PCI-E and NMI_NOW. */
184         if (byte != byte_old)
185                 outb(byte, 0x70);
186
187         /* Initialize the real time clock (RTC). */
188         rtc_init(0);
189
190         /* Initialize ISA DMA. */
191         isa_dma_init();
192
193         /* Initialize the High Precision Event Timers (HPET). */
194         enable_hpet(dev);
195
196         rom_dummy_write(dev);
197 }
198
199 static void ck804_lpc_read_resources(device_t dev)
200 {
201         struct resource *res;
202         unsigned long index;
203
204         /* Get the normal PCI resources of this device. */
205         /* We got one for APIC, or one more for TRAP. */
206         pci_dev_read_resources(dev);
207
208         /* Get resource for ACPI, SYSTEM_CONTROL, ANALOG_CONTROL. */
209         for (index = 0x60; index <= 0x68; index += 4)   /* We got another 3. */
210                 pci_get_resource(dev, index);
211         compact_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 /**
233  * Enable resources for children devices.
234  *
235  * This function is called by the global enable_resources() indirectly via the
236  * device_operation::enable_resources() method of devices.
237  */
238 static void ck804_lpc_enable_childrens_resources(device_t dev)
239 {
240         struct bus *link;
241         u32 reg, reg_var[4];
242         int i, var_num = 0;
243
244         reg = pci_read_config32(dev, 0xa0);
245
246         for (link = dev->link_list; link; link = link->next) {
247                 device_t child;
248                 for (child = link->children; child; child = child->sibling) {
249                         if (child->enabled && (child->path.type == DEVICE_PATH_PNP)) {
250                                 struct resource *res;
251                                 for (res = child->resource_list; res; res = res->next) {
252                                         unsigned long base, end; /* Don't need long long. */
253                                         if (!(res->flags & IORESOURCE_IO))
254                                                 continue;
255                                         base = res->base;
256                                         end = resource_end(res);
257                                         printk(BIOS_DEBUG, "ck804 lpc decode:%s, base=0x%08lx, end=0x%08lx\n", dev_path(child), base, end);
258                                         switch (base) {
259                                         case 0x3f8:     // COM1
260                                                 reg |= (1 << 0);
261                                                 break;
262                                         case 0x2f8:     // COM2
263                                                 reg |= (1 << 1);
264                                                 break;
265                                         case 0x378:     // Parallel 1
266                                                 reg |= (1 << 24);
267                                                 break;
268                                         case 0x3f0:     // FD0
269                                                 reg |= (1 << 20);
270                                                 break;
271                                         case 0x220:     // Audio 0
272                                                 reg |= (1 << 8);
273                                                 break;
274                                         case 0x300:     // Midi 0
275                                                 reg |= (1 << 12);
276                                                 break;
277                                         }
278                                         if (base == 0x290 || base >= 0x400) {
279                                                 /* Only 4 var; compact them? */
280                                                 if (var_num >= 4)
281                                                         continue;
282                                                 reg |= (1 << (28 + var_num));
283                                                 reg_var[var_num++] = (base & 0xffff) | ((end & 0xffff) << 16);
284                                         }
285                                 }
286                         }
287                 }
288         }
289         pci_write_config32(dev, 0xa0, reg);
290         for (i = 0; i < var_num; i++)
291                 pci_write_config32(dev, 0xa8 + i * 4, reg_var[i]);
292 }
293
294 static void ck804_lpc_enable_resources(device_t dev)
295 {
296         pci_dev_enable_resources(dev);
297         ck804_lpc_enable_childrens_resources(dev);
298 }
299
300 static struct device_operations lpc_ops = {
301         .read_resources   = ck804_lpc_read_resources,
302         .set_resources    = pci_dev_set_resources,
303         .enable_resources = ck804_lpc_enable_resources,
304         .init             = lpc_init,
305         .scan_bus         = scan_static_bus,
306         // .enable        = ck804_enable,
307         .ops_pci          = &ck804_pci_ops,
308 };
309
310 static const struct pci_driver lpc_driver __pci_driver = {
311         .ops    = &lpc_ops,
312         .vendor = PCI_VENDOR_ID_NVIDIA,
313         .device = PCI_DEVICE_ID_NVIDIA_CK804_LPC,
314 };
315
316 static const struct pci_driver lpc_driver_pro __pci_driver = {
317         .ops    = &lpc_ops,
318         .vendor = PCI_VENDOR_ID_NVIDIA,
319         .device = PCI_DEVICE_ID_NVIDIA_CK804_PRO,
320 };
321
322 #if CK804_CHIP_REV == 1
323 static const struct pci_driver lpc_driver_slave __pci_driver = {
324         .ops    = &lpc_ops,
325         .vendor = PCI_VENDOR_ID_NVIDIA,
326         .device = PCI_DEVICE_ID_NVIDIA_CK804_SLAVE,
327 };
328 #else
329 static struct device_operations lpc_slave_ops = {
330         .read_resources   = ck804_lpc_read_resources,
331         .set_resources    = pci_dev_set_resources,
332         .enable_resources = pci_dev_enable_resources,
333         .init             = lpc_slave_init,
334         // .enable        = ck804_enable,
335         .ops_pci          = &ck804_pci_ops,
336 };
337
338 static const struct pci_driver lpc_driver_slave __pci_driver = {
339         .ops    = &lpc_slave_ops,
340         .vendor = PCI_VENDOR_ID_NVIDIA,
341         .device = PCI_DEVICE_ID_NVIDIA_CK804_SLAVE,
342 };
343 #endif