VIA VT8237R cleanups (trivial).
[coreboot.git] / src / southbridge / via / vt8237r / vt8237r_lpc.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007, 2008 Rudolf Marek <r.marek@assembler.cz>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License v2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 /* Inspiration from other VIA SB code. */
21
22 #include <arch/io.h>
23 #include <console/console.h>
24 #include <device/device.h>
25 #include <device/pci.h>
26 #include <device/pci_ids.h>
27 #include <pc80/mc146818rtc.h>
28 #include <cpu/x86/lapic.h>
29 #include <stdlib.h>
30 #include "vt8237r.h"
31 #include "chip.h"
32
33 #define ALL             (0xff << 24)
34 #define NONE            (0)
35 #define DISABLED        (1 << 16)
36 #define ENABLED         (0 << 16)
37 #define TRIGGER_EDGE    (0 << 15)
38 #define TRIGGER_LEVEL   (1 << 15)
39 #define POLARITY_HIGH   (0 << 13)
40 #define POLARITY_LOW    (1 << 13)
41 #define PHYSICAL_DEST   (0 << 11)
42 #define LOGICAL_DEST    (1 << 11)
43 #define ExtINT          (7 << 8)
44 #define NMI             (4 << 8)
45 #define SMI             (2 << 8)
46 #define INT             (1 << 8)
47
48 extern void dump_south(device_t dev);
49
50 static struct ioapicreg {
51         u32 reg;
52         u32 value_low;
53         u32 value_high;
54 } ioapic_table[] = {
55         /* IO-APIC virtual wire mode configuration. */
56         /* mask, trigger, polarity, destination, delivery, vector */
57         {0, ENABLED | TRIGGER_EDGE | POLARITY_HIGH | PHYSICAL_DEST |
58                     ExtINT, NONE}, {
59         {1,  DISABLED, NONE},
60         {2,  DISABLED, NONE},
61         {3,  DISABLED, NONE},
62         {4,  DISABLED, NONE},
63         {5,  DISABLED, NONE},
64         {6,  DISABLED, NONE},
65         {7,  DISABLED, NONE},
66         {8,  DISABLED, NONE},
67         {9,  DISABLED, NONE},
68         {10, DISABLED, NONE},
69         {11, DISABLED, NONE},
70         {12, DISABLED, NONE},
71         {13, DISABLED, NONE},
72         {14, DISABLED, NONE},
73         {15, DISABLED, NONE},
74         {16, DISABLED, NONE},
75         {17, DISABLED, NONE},
76         {18, DISABLED, NONE},
77         {19, DISABLED, NONE},
78         {20, DISABLED, NONE},
79         {21, DISABLED, NONE},
80         {22, DISABLED, NONE},
81         {23, DISABLED, NONE},
82 };
83
84 static void setup_ioapic(u32 ioapic_base)
85 {
86         u32 value_low, value_high, val;
87         volatile u32 *l;
88         int i;
89
90         /* All delivered to CPU0. */
91         ioapic_table[0].value_high = (lapicid()) << (56 - 32);
92         l = (unsigned long *)ioapic_base;
93
94         /* Set APIC to FSB message bus. */
95         l[0] = 0x3;
96         val = l[4];
97         l[4] = (val & 0xFFFFFE) | 1;
98
99         /* Set APIC ADDR - this will be VT8237R_APIC_ID. */
100         l[0] = 0;
101         val = l[4];
102         l[4] = (val & 0xF0FFFF) | (VT8237R_APIC_ID << 24);
103
104         for (i = 0; i < ARRAY_SIZE(ioapic_table); i++) {
105                 l[0] = (ioapic_table[i].reg * 2) + 0x10;
106                 l[4] = ioapic_table[i].value_low;
107                 value_low = l[4];
108                 l[0] = (ioapic_table[i].reg * 2) + 0x11;
109                 l[4] = ioapic_table[i].value_high;
110                 value_high = l[4];
111
112                 if ((i == 0) && (value_low == 0xffffffff)) {
113                         printk_warning("IO APIC not responding.\n");
114                         return;
115                 }
116         }
117 }
118
119 static void southbridge_init_common(struct device *dev);
120
121 /** Set up PCI IRQ routing, route everything through APIC. */
122 static void pci_routing_fixup(struct device *dev)
123 {
124         /* PCI PNP Interrupt Routing INTE/F - disable */
125         pci_write_config8(dev, 0x44, 0x00);
126
127         /* PCI PNP Interrupt Routing INTG/H - disable */
128         pci_write_config8(dev, 0x45, 0x00);
129
130         /* Route INTE-INTH through registers above, no map to INTA-INTD. */
131         pci_write_config8(dev, 0x46, 0x10);
132
133         /* PCI Interrupt Polarity */
134         pci_write_config8(dev, 0x54, 0x00);
135
136         /* PCI INTA# Routing */
137         pci_write_config8(dev, 0x55, 0x00);
138
139         /* PCI INTB#/C# Routing */
140         pci_write_config8(dev, 0x56, 0x00);
141
142         /* PCI INTD# Routing */
143         pci_write_config8(dev, 0x57, 0x00);
144 }
145
146 /**
147  * Set up the power management capabilities directly into ACPI mode.
148  * This avoids having to handle any System Management Interrupts (SMIs).
149  */
150 static void setup_pm(device_t dev)
151 {
152         /* Debounce LID and PWRBTN# Inputs for 16ms. */
153         pci_write_config8(dev, 0x80, 0x20);
154
155         /* Set ACPI base address to I/O VT8237R_ACPI_IO_BASE. */
156         pci_write_config16(dev, 0x88, VT8237R_ACPI_IO_BASE | 0x1);
157
158         /* Set ACPI to 9, must set IRQ 9 override to level! Set PSON gating. */
159         pci_write_config8(dev, 0x82, 0x40 | VT8237R_ACPI_IRQ);
160
161         /* Primary interupt channel, define wake events 0=IRQ0 15=IRQ15 1=en. */
162         pci_write_config16(dev, 0x84, 0x30b2);
163
164         /* SMI output level to low, 7.5us throttle clock */
165         pci_write_config8(dev, 0x8d, 0x18);
166
167         /* GP Timer Control 1s */
168         pci_write_config8(dev, 0x93, 0x88);
169
170         /*
171          * 7 = SMBus clock from RTC 32.768KHz
172          * 5 = Internal PLL reset from susp
173          * 2 = GPO2 is GPIO
174          */
175         pci_write_config8(dev, 0x94, 0xa4);
176
177         /*
178          * 7 = stp to sust delay 1msec
179          * 6 = SUSST# Deasserted Before PWRGD for STD
180          * 4 = PWRGOOD reset on VT8237A/S
181          * 3 = GPO26/GPO27 is GPO 
182          * 2 = Disable Alert on Lan
183          */
184         pci_write_config8(dev, 0x95, 0xcc);
185
186         /* Disable GP3 timer. */
187         pci_write_config8(dev, 0x98, 0);
188
189         /* Enable ACPI accessm RTC signal gated with PSON. */
190         pci_write_config8(dev, 0x81, 0x84);
191
192         /* Clear status events. */
193         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x00);
194         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x20);
195         outw(0xffff, VT8237R_ACPI_IO_BASE + 0x28);
196         outl(0xffffffff, VT8237R_ACPI_IO_BASE + 0x30);
197
198         /* Disable SCI on GPIO. */
199         outw(0x0, VT8237R_ACPI_IO_BASE + 0x22);
200
201         /* Disable SMI on GPIO. */
202         outw(0x0, VT8237R_ACPI_IO_BASE + 0x24);
203
204         /* Disable all global enable SMIs. */
205         outw(0x0, VT8237R_ACPI_IO_BASE + 0x2a);
206
207         /* All SMI off, both IDE buses ON, PSON rising edge. */
208         outw(0x0, VT8237R_ACPI_IO_BASE + 0x2c);
209
210         /* Primary activity SMI disable. */
211         outl(0x0, VT8237R_ACPI_IO_BASE + 0x34);
212
213         /* GP timer reload on none. */
214         outl(0x0, VT8237R_ACPI_IO_BASE + 0x38);
215
216         /* Disable extended IO traps. */
217         outb(0x0, VT8237R_ACPI_IO_BASE + 0x42);
218
219         /* SCI is generated for RTC/pwrBtn/slpBtn. */
220         outw(0x001, VT8237R_ACPI_IO_BASE + 0x04);
221 }
222
223 static void vt8237r_init(struct device *dev)
224 {
225         u8 enables;
226
227         /*
228          * Enable SATA LED, disable special CPU Frequency Change -
229          * GPIO28 GPIO22 GPIO29 GPIO23 are GPIOs.
230          */
231         pci_write_config8(dev, 0xe5, 0x9);
232
233         /* REQ5 as PCI request input - should be together with INTE-INTH. */
234         pci_write_config8(dev, 0xe4, 0x4);
235
236         /* Set bit 3 of 0x4f (use INIT# as CPU reset). */
237         enables = pci_read_config8(dev, 0x4f);
238         enables |= 0x08;
239         pci_write_config8(dev, 0x4f, enables);
240
241         /*
242          * Set Read Pass Write Control Enable
243          * (force A2 from APIC FSB to low).
244          */
245         pci_write_config8(dev, 0x48, 0x8c);
246
247         southbridge_init_common(dev);
248
249         /* FIXME: Intel needs more bit set for C2/C3. */
250
251         /*
252          * Allow SLP# signal to assert LDTSTOP_L.
253          * Will work for C3 and for FID/VID change.
254          */
255         outb(0x1, VT8237R_ACPI_IO_BASE + 0x11);
256 }
257
258 static void vt8237s_init(struct device *dev)
259 {
260         u32 tmp;
261
262         /* Put SPI base VT8237S_SPI_MEM_BASE. */
263         tmp = pci_read_config32(dev, 0xbc);
264         pci_write_config32(dev, 0xbc,
265                            (VT8237S_SPI_MEM_BASE >> 8) | (tmp & 0xFF000000));
266
267         /* Enable SATA LED, VR timer = 100us, VR timer should be fixed. */
268         pci_write_config8(dev, 0xe5, 0x69);
269
270         /*
271          * REQ5 as PCI request input - should be together with INTE-INTH. 
272          * Fast VR timer disable - need for LDTSTOP_L signal.
273          */
274         pci_write_config8(dev, 0xe4, 0xa5);
275
276         /* Reduce further the STPCLK/LDTSTP signal to 5us. */
277         pci_write_config8(dev, 0xec, 0x4);
278
279         /* Host Bus Power Management Control, maybe not needed */
280         pci_write_config8(dev, 0x8c, 0x5);
281
282         /* Enable HPET at VT8237R_HPET_ADDR., does not work correctly on R. */
283         pci_write_config32(dev, 0x68, (VT8237R_HPET_ADDR | 0x80));
284
285         southbridge_init_common(dev);
286
287         /* FIXME: Intel needs more bit set for C2/C3. */
288
289         /*
290          * Allow SLP# signal to assert LDTSTOP_L.
291          * Will work for C3 and for FID/VID change. FIXME FIXME, pre rev A2.
292          */
293         outb(0xff, VT8237R_ACPI_IO_BASE + 0x50);
294
295         dump_south(dev);
296 }
297
298 static void vt8237_common_init(struct device *dev)
299 {
300         u8 enables, byte;
301
302         /* Enable addr/data stepping. */
303         byte = pci_read_config8(dev, PCI_COMMAND);
304         byte |= PCI_COMMAND_WAIT;
305         pci_write_config8(dev, PCI_COMMAND, byte);
306
307         /* Enable the internal I/O decode. */
308         enables = pci_read_config8(dev, 0x6C);
309         enables |= 0x80;
310         pci_write_config8(dev, 0x6C, enables);
311
312         /*
313          * ROM decode
314          * bit range
315          *   7 000E0000h-000EFFFFh
316          *   6 FFF00000h-FFF7FFFFh
317          *   5 FFE80000h-FFEFFFFFh
318          *   4 FFE00000h-FFE7FFFFh
319          *   3 FFD80000h-FFDFFFFFh
320          *   2 FFD00000h-FFD7FFFFh
321          *   1 FFC80000h-FFCFFFFFh
322          *   0 FFC00000h-FFC7FFFFh
323          * So 0x7f here sets ROM decode to FFC00000-FFFFFFFF or 4Mbyte.
324          */
325         pci_write_config8(dev, 0x41, 0x7f);
326
327         /*
328          * Set bit 6 of 0x40 (I/O recovery time).
329          * IMPORTANT FIX - EISA = ECLR reg at 0x4d0! Decoding must be on so
330          * that PCI interrupts can be properly marked as level triggered.
331          */
332         enables = pci_read_config8(dev, 0x40);
333         enables |= 0x44;
334         pci_write_config8(dev, 0x40, enables);
335
336         /* Line buffer control */
337         enables = pci_read_config8(dev, 0x42);
338         enables |= 0xf8;
339         pci_write_config8(dev, 0x42, enables);
340
341         /* Delay transaction control */
342         pci_write_config8(dev, 0x43, 0xb);
343
344         /* I/O recovery time, default IDE routing */
345         pci_write_config8(dev, 0x4c, 0x44);
346
347         /* ROM memory cycles go to LPC. */
348         pci_write_config8(dev, 0x59, 0x80);
349
350         /*
351          * Bit | Meaning
352          * -------------
353          *   3 | Bypass APIC De-Assert Message (1=Enable)
354          *   1 | possibly "INTE#, INTF#, INTG#, INTH# as PCI"
355          *     | bit 1=1 works for Aaron at VIA, bit 1=0 works for jakllsch
356          *   0 | Dynamic Clock Gating Main Switch (1=Enable)
357          */
358         pci_write_config8(dev, 0x5b, 0xb);
359
360         /* Set 0x58 to 0x43 APIC and RTC. */
361         pci_write_config8(dev, 0x58, 0x43);
362
363         /* Enable serial IRQ, 6PCI clocks. */
364         pci_write_config8(dev, 0x52, 0x9);
365
366         /* Power management setup */
367         setup_pm(dev);
368
369         /* Start the RTC. */
370         rtc_init(0);
371 }
372
373 static void vt8237r_read_resources(device_t dev)
374 {
375         struct resource *res;
376
377         pci_dev_read_resources(dev);
378         /* Fixed APIC resource */
379         res = new_resource(dev, 0x44);
380         res->base = VT8237R_APIC_BASE;
381         res->size = 256;
382         res->limit = res->base + res->size - 1;
383         res->align = 8;
384         res->gran = 8;
385         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
386                      IORESOURCE_STORED | IORESOURCE_ASSIGNED;
387 }
388
389 /**
390  * The VT8237R is not a PCI bridge and has no resources of its own (other
391  * than standard PC I/O addresses), however it does control the ISA bus
392  * and so we need to manually call enable childrens resources on that bus.
393  */
394 static void vt8237r_enable_resources(device_t dev)
395 {
396         pci_dev_enable_resources(dev);
397         enable_childrens_resources(dev);
398 }
399
400 static void init_keyboard(struct device *dev)
401 {
402         u8 regval = pci_read_config8(dev, 0x51);
403         if (regval & 0x1)
404                 init_pc_keyboard(0x60, 0x64, 0);
405 }
406
407 static void southbridge_init_common(struct device *dev)
408 {
409         vt8237_common_init(dev);
410         pci_routing_fixup(dev);
411         setup_ioapic(VT8237R_APIC_BASE);
412         setup_i8259();
413         init_keyboard(dev);
414 }
415
416 static const struct device_operations vt8237r_lpc_ops_s = {
417         .read_resources         = vt8237r_read_resources,
418         .set_resources          = pci_dev_set_resources,
419         .enable_resources       = vt8237r_enable_resources,
420         .init                   = &vt8237s_init,
421         .scan_bus               = scan_static_bus,
422 };
423
424 static const struct device_operations vt8237r_lpc_ops_r = {
425         .read_resources         = vt8237r_read_resources,
426         .set_resources          = pci_dev_set_resources,
427         .enable_resources       = vt8237r_enable_resources,
428         .init                   = &vt8237r_init,
429         .scan_bus               = scan_static_bus,
430 };
431
432 static const struct pci_driver lpc_driver_r __pci_driver = {
433         .ops    = &vt8237r_lpc_ops_r,
434         .vendor = PCI_VENDOR_ID_VIA,
435         .device = PCI_DEVICE_ID_VIA_VT8237R_LPC,
436 };
437
438 static const struct pci_driver lpc_driver_s __pci_driver = {
439         .ops    = &vt8237r_lpc_ops_s,
440         .vendor = PCI_VENDOR_ID_VIA,
441         .device = PCI_DEVICE_ID_VIA_VT8237S_LPC,
442 };