Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2009 coresystems GmbH |
Mike Loptien | ce740c4 | 2014-01-03 16:54:56 -0700 | [diff] [blame^] | 5 | * Copyright (C) 2013 Sage Electronic Engineering, LLC. |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 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 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 Menzel | a46a712 | 2013-02-23 18:37:27 +0100 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Ronald G. Minnich | e5ac295 | 2004-10-14 22:44:26 +0000 | [diff] [blame] | 21 | #include <arch/io.h> |
Stefan Reinauer | a829bfe | 2009-01-20 21:38:17 +0000 | [diff] [blame] | 22 | #include <pc80/i8259.h> |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 23 | #include <console/console.h> |
| 24 | |
Mike Loptien | ce740c4 | 2014-01-03 16:54:56 -0700 | [diff] [blame^] | 25 | /* Read the current PIC IRQ mask */ |
| 26 | u16 pic_read_irq_mask(void) |
| 27 | { |
| 28 | u16 mask; |
| 29 | int i; |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 30 | |
Mike Loptien | ce740c4 | 2014-01-03 16:54:56 -0700 | [diff] [blame^] | 31 | mask = inb(MASTER_PIC_OCW1) | (inb(SLAVE_PIC_OCW1) << 8); |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 32 | |
Mike Loptien | ce740c4 | 2014-01-03 16:54:56 -0700 | [diff] [blame^] | 33 | 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 Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 42 | |
Mike Loptien | ce740c4 | 2014-01-03 16:54:56 -0700 | [diff] [blame^] | 43 | /* |
| 44 | * Write an IRQ mask to the PIC: |
| 45 | * IRQA is bit 0xA in the 16 bit bitmask (OCW1) |
| 46 | */ |
| 47 | void pic_write_irq_mask(u16 mask) |
| 48 | { |
| 49 | outb(mask, MASTER_PIC_OCW1); |
| 50 | outb(mask >> 8, SLAVE_PIC_OCW1); |
| 51 | } |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 52 | |
Mike Loptien | ce740c4 | 2014-01-03 16:54:56 -0700 | [diff] [blame^] | 53 | /* |
| 54 | * The PIC IRQs default to masked off |
| 55 | * Allow specific IRQs to be enabled (1) |
| 56 | * or disabled by (0) the user |
| 57 | */ |
| 58 | void 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. Minnich | e5ac295 | 2004-10-14 22:44:26 +0000 | [diff] [blame] | 63 | |
| 64 | void setup_i8259(void) |
| 65 | { |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 66 | /* 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 Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 83 | /* Now the interrupt controller expects us to write to ICW3. |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 84 | * |
| 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 Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 97 | outb(MICROPROCESSOR_MODE, SLAVE_PIC_ICW2); |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 98 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 99 | /* Now clear the interrupts through OCW1. |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 100 | * 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 Roth | 5688979 | 2013-07-09 21:39:46 -0600 | [diff] [blame] | 102 | * controller. This way the cascading stays alive. |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 103 | */ |
| 104 | outb(ALL_IRQS, SLAVE_PIC_OCW1); |
| 105 | outb(ALL_IRQS & ~IRQ2, MASTER_PIC_OCW1); |
Ronald G. Minnich | e5ac295 | 2004-10-14 22:44:26 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 108 | /** |
| 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 | */ |
| 117 | void i8259_configure_irq_trigger(int int_num, int is_level_triggered) |
| 118 | { |
| 119 | u16 int_bits = inb(ELCR1) | (((u16)inb(ELCR2)) << 8); |
| 120 | |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 121 | if (is_level_triggered) |
| 122 | int_bits |= (1 << int_num); |
| 123 | else |
| 124 | int_bits &= ~(1 << int_num); |
| 125 | |
| 126 | /* Write new values */ |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 127 | 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 Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 133 | printk(BIOS_ERR, "%s: lower order bits are wrong: want 0x%x, got 0x%x\n", |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 134 | __func__, (int_bits & 0xff), inb(ELCR1)); |
| 135 | } |
| 136 | |
| 137 | if (inb(ELCR2) != (int_bits >> 8)) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 138 | printk(BIOS_ERR, "%s: higher order bits are wrong: want 0x%x, got 0x%x\n", |
Stefan Reinauer | 4d933dd | 2009-07-21 21:36:41 +0000 | [diff] [blame] | 139 | __func__, (int_bits>>8), inb(ELCR2)); |
| 140 | } |
| 141 | #endif |
| 142 | } |
| 143 | |
Ronald G. Minnich | e5ac295 | 2004-10-14 22:44:26 +0000 | [diff] [blame] | 144 | |