i3100: configure pci irqs
[coreboot.git] / src / southbridge / intel / i3100 / lpc.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2004 Linux Networx
5  * Copyright (C) 2008 Arastra, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  */
21
22 /* This code is based on src/southbridge/intel/esb6300/esb6300_lpc.c */
23
24 #include <console/console.h>
25 #include <device/device.h>
26 #include <device/pci.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 <pc80/i8259.h>
32 #include <arch/io.h>
33 #include <arch/ioapic.h>
34 #include "i3100.h"
35
36 #define ACPI_BAR 0x40
37 #define GPIO_BAR 0x48
38 #define RCBA 0xf0
39
40 #define SERIRQ_CNTL 0x64
41
42 #define GEN_PMCON_1 0xA0
43 #define GEN_PMCON_2 0xA2
44 #define GEN_PMCON_3 0xA4
45
46 #define NMI_OFF 0
47 #define MAINBOARD_POWER_OFF 0
48 #define MAINBOARD_POWER_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 i3100_enable_serial_irqs(device_t dev)
55 {
56         /* set packet length and toggle silent mode bit */
57         pci_write_config8(dev, SERIRQ_CNTL, (1 << 7)|(1 << 6)|((21 - 17) << 2)|(0 << 0));
58         pci_write_config8(dev, SERIRQ_CNTL, (1 << 7)|(0 << 6)|((21 - 17) << 2)|(0 << 0));
59 }
60
61 typedef struct southbridge_intel_i3100_config config_t;
62
63 static void set_i3100_gpio_use_sel(
64         device_t dev, struct resource *res, config_t *config)
65 {
66         u32 gpio_use_sel, gpio_use_sel2;
67         int i;
68
69         gpio_use_sel = inl(res->base + 0x00) | 0x0000c603;
70         gpio_use_sel2 = inl(res->base + 0x30) | 0x00000100;
71         for (i = 0; i < 64; i++) {
72                 int val;
73                 switch (config->gpio[i] & I3100_GPIO_USE_MASK) {
74                 case I3100_GPIO_USE_AS_NATIVE:
75                         val = 0;
76                         break;
77                 case I3100_GPIO_USE_AS_GPIO:
78                         val = 1;
79                         break;
80                 default:
81                         continue;
82                 }
83                 /* The caller is responsible for not playing with unimplemented bits */
84                 if (i < 32) {
85                         gpio_use_sel &= ~(1 << i);
86                         gpio_use_sel |= (val << i);
87                 } else {
88                         gpio_use_sel2 &= ~(1 << (i - 32));
89                         gpio_use_sel2 |= (val << (i - 32));
90                 }
91         }
92         outl(gpio_use_sel, res->base + 0x00);
93         outl(gpio_use_sel2, res->base + 0x30);
94 }
95
96 static void set_i3100_gpio_direction(
97         device_t dev, struct resource *res, config_t *config)
98 {
99         u32 gpio_io_sel, gpio_io_sel2;
100         int i;
101
102         gpio_io_sel = inl(res->base + 0x04);
103         gpio_io_sel2 = inl(res->base + 0x34);
104         for (i = 0; i < 64; i++) {
105                 int val;
106                 switch (config->gpio[i] & I3100_GPIO_SEL_MASK) {
107                 case I3100_GPIO_SEL_OUTPUT:
108                         val = 0;
109                         break;
110                 case I3100_GPIO_SEL_INPUT:
111                         val = 1;
112                         break;
113                 default:
114                         continue;
115                 }
116                 /* The caller is responsible for not playing with unimplemented bits */
117                 if (i < 32) {
118                         gpio_io_sel &= ~(1 << i);
119                         gpio_io_sel |= (val << i);
120                 } else {
121                         gpio_io_sel2 &= ~(1 << (i - 32));
122                         gpio_io_sel2 |= (val << (i - 32));
123                 }
124         }
125         outl(gpio_io_sel, res->base + 0x04);
126         outl(gpio_io_sel2, res->base + 0x34);
127 }
128
129 static void set_i3100_gpio_level(
130         device_t dev, struct resource *res, config_t *config)
131 {
132         u32 gpio_lvl, gpio_lvl2;
133         u32 gpio_blink;
134         int i;
135
136         gpio_lvl = inl(res->base + 0x0c);
137         gpio_blink = inl(res->base + 0x18);
138         gpio_lvl2 = inl(res->base + 0x38);
139         for (i = 0; i < 64; i++) {
140                 int val, blink;
141                 switch (config->gpio[i] & I3100_GPIO_LVL_MASK) {
142                 case I3100_GPIO_LVL_LOW:
143                         val = 0;
144                         blink = 0;
145                         break;
146                 case I3100_GPIO_LVL_HIGH:
147                         val = 1;
148                         blink = 0;
149                         break;
150                 case I3100_GPIO_LVL_BLINK:
151                         val = 1;
152                         blink = 1;
153                         break;
154                 default:
155                         continue;
156                 }
157                 /* The caller is responsible for not playing with unimplemented bits */
158                 if (i < 32) {
159                         gpio_lvl &= ~(1 << i);
160                         gpio_blink &= ~(1 << i);
161                         gpio_lvl |= (val << i);
162                         gpio_blink |= (blink << i);
163                 } else {
164                         gpio_lvl2 &= ~(1 << (i - 32));
165                         gpio_lvl2 |= (val << (i - 32));
166                 }
167         }
168         outl(gpio_lvl, res->base + 0x0c);
169         outl(gpio_blink, res->base + 0x18);
170         outl(gpio_lvl2, res->base + 0x38);
171 }
172
173 static void set_i3100_gpio_inv(
174         device_t dev, struct resource *res, config_t *config)
175 {
176         u32 gpio_inv;
177         int i;
178
179         gpio_inv = inl(res->base + 0x2c);
180         for (i = 0; i < 32; i++) {
181                 int val;
182                 switch (config->gpio[i] & I3100_GPIO_INV_MASK) {
183                 case I3100_GPIO_INV_OFF:
184                         val = 0;
185                         break;
186                 case I3100_GPIO_INV_ON:
187                         val = 1;
188                         break;
189                 default:
190                         continue;
191                 }
192                 gpio_inv &= ~(1 << i);
193                 gpio_inv |= (val << i);
194         }
195         outl(gpio_inv, res->base + 0x2c);
196 }
197
198 static void i3100_pirq_init(device_t dev)
199 {
200         device_t irq_dev;
201         config_t *config;
202
203         /* Get the chip configuration */
204         config = dev->chip_info;
205
206         if(config->pirq_a_d)
207                 pci_write_config32(dev, 0x60, config->pirq_a_d);
208
209         if(config->pirq_e_h)
210                 pci_write_config32(dev, 0x68, config->pirq_e_h);
211
212         for(irq_dev = all_devices; irq_dev; irq_dev = irq_dev->next) {
213                 u8 int_pin=0, int_line=0;
214
215                 if (!irq_dev->enabled || irq_dev->path.type != DEVICE_PATH_PCI)
216                         continue;
217
218                 int_pin = pci_read_config8(irq_dev, PCI_INTERRUPT_PIN);
219                 switch (int_pin) {
220                 case 1: /* INTA# */
221                         int_line = config->pirq_a_d & 0xff;
222                         break;
223
224                 case 2: /* INTB# */
225                         int_line = (config->pirq_a_d >> 8) & 0xff;
226                         break;
227
228                 case 3: /* INTC# */
229                         int_line = (config->pirq_a_d >> 16) & 0xff;
230                         break;
231
232                 case 4: /* INTD# */
233                         int_line = (config->pirq_a_d >> 24) & 0xff;
234                         break;
235                 }
236
237                 if (!int_line)
238                         continue;
239
240                 printk(BIOS_DEBUG, "%s: irq pin %d, irq line %d\n", dev_path(irq_dev), int_pin, int_line);
241                 pci_write_config8(irq_dev, PCI_INTERRUPT_LINE, int_line);
242         }
243
244
245 }
246
247 static void i3100_power_options(device_t dev) {
248   u8 reg8;
249   u16 reg16;
250   int pwr_on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
251   int nmi_option;
252
253   /* Which state do we want to goto after g3 (power restored)?
254    * 0 == S0 Full On
255    * 1 == S5 Soft Off
256    */
257   get_option(&pwr_on, "power_on_after_fail");
258   reg8 = pci_read_config8(dev, GEN_PMCON_3);
259   reg8 &= 0xfe;
260   if (pwr_on) {
261     reg8 &= ~1;
262   } else {
263     reg8 |= 1;
264   }
265   /* avoid #S4 assertions */
266   reg8 |= (3 << 4);
267   /* minimum asssertion is 1 to 2 RTCCLK */
268   reg8 &= ~(1 << 3);
269   pci_write_config8(dev, GEN_PMCON_3, reg8);
270   printk(BIOS_INFO, "set power %s after power fail\n", pwr_on ? "on" : "off");
271
272   /* Set up NMI on errors. */
273   reg8 = inb(0x61);
274   /* Higher Nibble must be 0 */
275   reg8 &= 0x0f;
276   /* IOCHK# NMI Enable */
277   reg8 &= ~(1 << 3);
278   /* PCI SERR# Enable */
279   // reg8 &= ~(1 << 2);
280   /* PCI SERR# Disable for now */
281   reg8 |= (1 << 2);
282   outb(reg8, 0x61);
283
284   reg8 = inb(0x70);
285   nmi_option = NMI_OFF;
286   get_option(&nmi_option, "nmi");
287   if (nmi_option) {
288     /* Set NMI. */
289     printk(BIOS_INFO, "NMI sources enabled.\n");
290     reg8 &= ~(1 << 7);
291   } else {
292     /* Can't mask NMI from PCI-E and NMI_NOW */
293     printk(BIOS_INFO, "NMI sources disabled.\n");
294     reg8 |= ( 1 << 7);
295   }
296   outb(reg8, 0x70);
297
298   // Enable CPU_SLP# and Intel Speedstep, set SMI# rate down
299   reg16 = pci_read_config16(dev, GEN_PMCON_1);
300   reg16 &= ~((3 << 0) | (1 << 10));
301   reg16 |= (1 << 3) | (1 << 5);
302   /* CLKRUN_EN */
303   // reg16 |= (1 << 2);
304   pci_write_config16(dev, GEN_PMCON_1, reg16);
305
306   // Set the board's GPI routing.
307   // i82801gx_gpi_routing(dev);
308 }
309
310 static void i3100_gpio_init(device_t dev)
311 {
312         struct resource *res;
313         config_t *config;
314
315         /* Skip if I don't have any configuration */
316         if (!dev->chip_info) {
317                 return;
318         }
319         /* The programmer is responsible for ensuring
320          * a valid gpio configuration.
321          */
322
323         /* Get the chip configuration */
324         config = dev->chip_info;
325         /* Find the GPIO bar */
326         res = find_resource(dev, GPIO_BAR);
327         if (!res) {
328                 return;
329         }
330
331         /* Set the use selects */
332         set_i3100_gpio_use_sel(dev, res, config);
333
334         /* Set the IO direction */
335         set_i3100_gpio_direction(dev, res, config);
336
337         /* Setup the input inverters */
338         set_i3100_gpio_inv(dev, res, config);
339
340         /* Set the value on the GPIO output pins */
341         set_i3100_gpio_level(dev, res, config);
342
343 }
344
345
346 static void lpc_init(struct device *dev)
347 {
348         struct resource *res;
349
350         /* Enable IO APIC */
351         res = find_resource(dev, RCBA);
352         if (!res) {
353                 return;
354         }
355         *((u8 *)((u32)res->base + 0x31ff)) |= (1 << 0);
356
357         // TODO this code sets int 0 of the IOAPIC in Virtual Wire Mode
358         // (register 0x10/0x11) while the old code used int 1 (register 0x12)
359         // ... Why?
360         setup_ioapic(IO_APIC_ADDR, 0); // Don't rename IOAPIC ID
361
362         /* Decode 0xffc00000 - 0xffffffff to fwh idsel 0 */
363         pci_write_config32(dev, 0xd0, 0x00000000);
364
365         i3100_enable_serial_irqs(dev);
366
367         /* Set up the PIRQ */
368         i3100_pirq_init(dev);
369
370         /* Setup power options */
371         i3100_power_options(dev);
372
373         /* Set the state of the gpio lines */
374         i3100_gpio_init(dev);
375
376         /* Initialize the real time clock */
377         rtc_init(0);
378
379         /* Initialize isa dma */
380         isa_dma_init();
381
382         setup_i8259();
383         i8259_configure_irq_trigger(9, 1);
384 }
385
386 static void i3100_lpc_read_resources(device_t dev)
387 {
388         struct resource *res;
389
390         /* Get the normal pci resources of this device */
391         pci_dev_read_resources(dev);
392
393         /* Add the ACPI BAR */
394         res = pci_get_resource(dev, ACPI_BAR);
395
396         /* Add the GPIO BAR */
397         res = pci_get_resource(dev, GPIO_BAR);
398
399         /* Add an extra subtractive resource for both memory and I/O. */
400         res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
401         res->base = 0;
402         res->size = 0x1000;
403         res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
404                      IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
405
406         res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
407         res->base = 0xff800000;
408         res->size = 0x00800000; /* 8 MB for flash */
409         res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
410                      IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
411
412         res = new_resource(dev, 3); /* IOAPIC */
413         res->base = IO_APIC_ADDR;
414         res->size = 0x00001000;
415         res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
416
417         /* Add resource for RCBA */
418         res = new_resource(dev, RCBA);
419         res->size = 0x4000;
420         res->limit = 0xffffc000;
421         res->align = 14;
422         res->gran = 14;
423         res->flags = IORESOURCE_MEM;
424 }
425
426 static void i3100_lpc_enable_resources(device_t dev)
427 {
428         u8 acpi_cntl, gpio_cntl;
429
430         /* Enable the normal pci resources */
431         pci_dev_enable_resources(dev);
432
433         /* Enable the ACPI bar */
434         acpi_cntl = pci_read_config8(dev, 0x44);
435         acpi_cntl |= (1 << 7);
436         pci_write_config8(dev, 0x44, acpi_cntl);
437
438         /* Enable the GPIO bar */
439         gpio_cntl = pci_read_config8(dev, 0x4c);
440         gpio_cntl |= (1 << 4);
441         pci_write_config8(dev, 0x4c, gpio_cntl);
442
443         /* Enable the RCBA */
444         pci_write_config32(dev, RCBA, pci_read_config32(dev, RCBA) | (1 << 0));
445 }
446
447 static struct pci_operations lops_pci = {
448         .set_subsystem = 0,
449 };
450
451 static struct device_operations lpc_ops  = {
452         .read_resources   = i3100_lpc_read_resources,
453         .set_resources    = pci_dev_set_resources,
454         .enable_resources = i3100_lpc_enable_resources,
455         .init             = lpc_init,
456         .scan_bus         = scan_static_bus,
457         .enable           = i3100_enable,
458         .ops_pci          = &lops_pci,
459 };
460
461 static const struct pci_driver lpc_driver __pci_driver = {
462         .ops    = &lpc_ops,
463         .vendor = PCI_VENDOR_ID_INTEL,
464         .device = PCI_DEVICE_ID_INTEL_3100_LPC,
465 };
466
467 static const struct pci_driver lpc_driver_ep80579 __pci_driver = {
468         .ops    = &lpc_ops,
469         .vendor = PCI_VENDOR_ID_INTEL,
470         .device = PCI_DEVICE_ID_INTEL_EP80579_LPC,
471 };