1 // Helpers for working with i8259 interrupt controller.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "biosvar.h" // SET_IVT
9 #include "config.h" // CONFIG_*
10 #include "output.h" // dprintf
11 #include "pic.h" // pic_*
14 pic_irqmask_read(void)
16 if (!CONFIG_HARDWARE_IRQ)
18 return inb(PORT_PIC1_DATA) | (inb(PORT_PIC2_DATA) << 8);
22 pic_irqmask_write(u16 mask)
24 if (!CONFIG_HARDWARE_IRQ)
26 outb(mask, PORT_PIC1_DATA);
27 outb(mask >> 8, PORT_PIC2_DATA);
31 pic_irqmask_mask(u16 off, u16 on)
33 if (!CONFIG_HARDWARE_IRQ)
35 u8 pic1off = off, pic1on = on, pic2off = off>>8, pic2on = on>>8;
36 outb((inb(PORT_PIC1_DATA) & ~pic1off) | pic1on, PORT_PIC1_DATA);
37 outb((inb(PORT_PIC2_DATA) & ~pic2off) | pic2on, PORT_PIC2_DATA);
41 pic_reset(u8 irq0, u8 irq8)
43 if (!CONFIG_HARDWARE_IRQ)
45 // Send ICW1 (select OCW1 + will send ICW4)
46 outb(0x11, PORT_PIC1_CMD);
47 outb(0x11, PORT_PIC2_CMD);
48 // Send ICW2 (base irqs: 0x08-0x0f for irq0-7, 0x70-0x77 for irq8-15)
49 outb(irq0, PORT_PIC1_DATA);
50 outb(irq8, PORT_PIC2_DATA);
51 // Send ICW3 (cascaded pic ids)
52 outb(0x04, PORT_PIC1_DATA);
53 outb(0x02, PORT_PIC2_DATA);
54 // Send ICW4 (enable 8086 mode)
55 outb(0x01, PORT_PIC1_DATA);
56 outb(0x01, PORT_PIC2_DATA);
57 // Mask all irqs (except cascaded PIC2 irq)
58 pic_irqmask_write(PIC_IRQMASK_DEFAULT);
64 dprintf(3, "init pic\n");
65 pic_reset(BIOS_HWIRQ0_VECTOR, BIOS_HWIRQ8_VECTOR);
69 enable_hwirq(int hwirq, struct segoff_s func)
71 if (!CONFIG_HARDWARE_IRQ)
73 pic_irqmask_mask(1 << hwirq, 0);
76 vector = BIOS_HWIRQ0_VECTOR + hwirq;
78 vector = BIOS_HWIRQ8_VECTOR + hwirq - 8;
79 SET_IVT(vector, func);
85 if (!CONFIG_HARDWARE_IRQ)
87 // 0x0b == select OCW1 + read ISR
88 outb(0x0b, PORT_PIC1_CMD);
89 return inb(PORT_PIC1_CMD);
95 if (!CONFIG_HARDWARE_IRQ)
97 // 0x0b == select OCW1 + read ISR
98 outb(0x0b, PORT_PIC2_CMD);
99 return inb(PORT_PIC2_CMD);
102 // Handler for otherwise unused hardware irqs.
104 handle_hwpic1(struct bregs *regs)
106 dprintf(DEBUG_ISR_hwpic1, "handle_hwpic1 irq=%x\n", pic_isr1_read());
111 handle_hwpic2(struct bregs *regs)
113 dprintf(DEBUG_ISR_hwpic2, "handle_hwpic2 irq=%x\n", pic_isr2_read());