blob: 2ad46e40705a1542772eb64dce5574826d7a2d5a [file] [log] [blame]
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2009 coresystems GmbH
Mike Loptience740c42014-01-03 16:54:56 -07005 * Copyright (C) 2013 Sage Electronic Engineering, LLC.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
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
Paul Menzela46a7122013-02-23 18:37:27 +010018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000019 */
20
Ronald G. Minniche5ac2952004-10-14 22:44:26 +000021#include <arch/io.h>
Stefan Reinauera829bfe2009-01-20 21:38:17 +000022#include <pc80/i8259.h>
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000023#include <console/console.h>
24
Mike Loptience740c42014-01-03 16:54:56 -070025/* Read the current PIC IRQ mask */
26u16 pic_read_irq_mask(void)
27{
28 u16 mask;
29 int i;
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000030
Mike Loptience740c42014-01-03 16:54:56 -070031 mask = inb(MASTER_PIC_OCW1) | (inb(SLAVE_PIC_OCW1) << 8);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000032
Mike Loptience740c42014-01-03 16:54:56 -070033 printk(BIOS_DEBUG, "8259 PIC: OCW1 IRQ Mask: 0x%x\n", mask);
34 printk(BIOS_SPEW, "\tEnabled IRQs (0 = Unmasked, 1 = Masked off):\n"
35 "\t\tMaster\t\tSlave\n");
36 for(i = 0; i <= 7; i++) {
37 printk(BIOS_SPEW, "\t\tIRQ%X: %x\t\tIRQ%X: %x\n",
38 i, (mask >> i) & 1, i + 8, (mask >> (i + 8)) & 1);
39 }
40 return mask;
41}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000042
Mike Loptience740c42014-01-03 16:54:56 -070043/*
44 * Write an IRQ mask to the PIC:
45 * IRQA is bit 0xA in the 16 bit bitmask (OCW1)
46 */
47void pic_write_irq_mask(u16 mask)
48{
49 outb(mask, MASTER_PIC_OCW1);
50 outb(mask >> 8, SLAVE_PIC_OCW1);
51}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000052
Mike Loptience740c42014-01-03 16:54:56 -070053/*
54 * The PIC IRQs default to masked off
55 * Allow specific IRQs to be enabled (1)
56 * or disabled by (0) the user
57 */
58void pic_irq_enable(u8 int_num, u8 mask)
59{
60 pic_write_irq_mask(pic_read_irq_mask() & ~(mask << int_num));
61 pic_read_irq_mask();
62}
Ronald G. Minniche5ac2952004-10-14 22:44:26 +000063
64void setup_i8259(void)
65{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000066 /* A write to ICW1 starts the Interrupt Controller Initialization
67 * Sequence. This implicitly causes the following to happen:
68 * - Interrupt Mask register is cleared
69 * - Priority 7 is assigned to IRQ7 input
70 * - Slave mode address is set to 7
71 * - Special mask mode is cleared
72 *
73 * We send the initialization sequence to both the master and
74 * slave i8259 controller.
75 */
76 outb(ICW_SELECT|IC4, MASTER_PIC_ICW1);
77 outb(ICW_SELECT|IC4, SLAVE_PIC_ICW1);
78
79 /* Now the interrupt controller expects us to write to ICW2. */
80 outb(INT_VECTOR_MASTER | IRQ0, MASTER_PIC_ICW2);
81 outb(INT_VECTOR_SLAVE | IRQ8, SLAVE_PIC_ICW2);
82
Stefan Reinauer14e22772010-04-27 06:56:47 +000083 /* Now the interrupt controller expects us to write to ICW3.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000084 *
85 * The normal scenario is to set up cascading on IRQ2 on the master
86 * i8259 and assign the slave ID 2 to the slave i8259.
87 */
88 outb(CASCADED_PIC, MASTER_PIC_ICW3);
89 outb(SLAVE_ID, SLAVE_PIC_ICW3);
90
91 /* Now the interrupt controller expects us to write to ICW4.
92 *
93 * We switch both i8259 to microprocessor mode because they're
94 * operating as part of an x86 architecture based chipset
95 */
96 outb(MICROPROCESSOR_MODE, MASTER_PIC_ICW2);
Stefan Reinauer14e22772010-04-27 06:56:47 +000097 outb(MICROPROCESSOR_MODE, SLAVE_PIC_ICW2);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000098
Stefan Reinauer14e22772010-04-27 06:56:47 +000099 /* Now clear the interrupts through OCW1.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000100 * First we mask off all interrupts on the slave interrupt controller
101 * then we mask off all interrupts but interrupt 2 on the master
Martin Roth56889792013-07-09 21:39:46 -0600102 * controller. This way the cascading stays alive.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000103 */
104 outb(ALL_IRQS, SLAVE_PIC_OCW1);
105 outb(ALL_IRQS & ~IRQ2, MASTER_PIC_OCW1);
Ronald G. Minniche5ac2952004-10-14 22:44:26 +0000106}
107
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000108/**
109 * @brief Configure IRQ triggering in the i8259 compatible Interrupt Controller.
110 *
111 * Switch a certain interrupt to be level / edge triggered.
112 *
113 * @param int_num legacy interrupt number (3-7, 9-15)
114 * @param is_level_triggered 1 for level triggered interrupt, 0 for edge
115 * triggered interrupt
116 */
117void i8259_configure_irq_trigger(int int_num, int is_level_triggered)
118{
119 u16 int_bits = inb(ELCR1) | (((u16)inb(ELCR2)) << 8);
120
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000121 if (is_level_triggered)
122 int_bits |= (1 << int_num);
123 else
124 int_bits &= ~(1 << int_num);
125
126 /* Write new values */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000127 outb((u8)(int_bits & 0xff), ELCR1);
128 outb((u8)(int_bits >> 8), ELCR2);
129
130#ifdef PARANOID_IRQ_TRIGGERS
131 /* Try reading back the new values. This seems like an error but is not ... */
132 if (inb(ELCR1) != (int_bits & 0xff)) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000133 printk(BIOS_ERR, "%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000134 __func__, (int_bits & 0xff), inb(ELCR1));
135 }
136
137 if (inb(ELCR2) != (int_bits >> 8)) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000138 printk(BIOS_ERR, "%s: higher order bits are wrong: want 0x%x, got 0x%x\n",
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000139 __func__, (int_bits>>8), inb(ELCR2));
140 }
141#endif
142}
143
Ronald G. Minniche5ac2952004-10-14 22:44:26 +0000144