Add header guard around pic.h
[seabios.git] / src / pic.h
1 // Helpers for working with i8259 interrupt controller.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7 #ifndef __PIC_H
8 #define __PIC_H
9
10 #include "ioport.h" // PORT_PIC*
11 #include "util.h" // dprintf
12
13 // PORT_PIC1 bitdefs
14 #define PIC1_IRQ0  (1<<0)
15 #define PIC1_IRQ1  (1<<1)
16 #define PIC1_IRQ2  (1<<2)
17 #define PIC1_IRQ5  (1<<5)
18 #define PIC1_IRQ6  (1<<6)
19 // PORT_PIC2 bitdefs
20 #define PIC2_IRQ8  (1<<0)
21 #define PIC2_IRQ12 (1<<4)
22 #define PIC2_IRQ13 (1<<5)
23 #define PIC2_IRQ14 (1<<6)
24
25 static inline void
26 eoi_pic1()
27 {
28     // Send eoi (select OCW2 + eoi)
29     outb(0x20, PORT_PIC1_CMD);
30 }
31
32 static inline void
33 eoi_pic2()
34 {
35     // Send eoi (select OCW2 + eoi)
36     outb(0x20, PORT_PIC2_CMD);
37     eoi_pic1();
38 }
39
40 static inline void
41 unmask_pic1(u8 irq)
42 {
43     outb(inb(PORT_PIC1_DATA) & ~irq, PORT_PIC1_DATA);
44 }
45
46 static inline void
47 unmask_pic2(u8 irq)
48 {
49     outb(inb(PORT_PIC2_DATA) & ~irq, PORT_PIC2_DATA);
50 }
51
52 static inline u8
53 get_pic1_isr()
54 {
55     // 0x0b == select OCW1 + read ISR
56     outb(0x0b, PORT_PIC1_CMD);
57     return inb(PORT_PIC1_CMD);
58 }
59
60 static inline void
61 pic_setup()
62 {
63     dprintf(3, "init pic\n");
64     // Send ICW1 (select OCW1 + will send ICW4)
65     outb(0x11, PORT_PIC1_CMD);
66     outb(0x11, PORT_PIC2_CMD);
67     // Send ICW2 (base irqs: 0x08-0x0f for irq0-7, 0x70-0x78 for irq8-15)
68     outb(0x08, PORT_PIC1_DATA);
69     outb(0x70, PORT_PIC2_DATA);
70     // Send ICW3 (cascaded pic ids)
71     outb(0x04, PORT_PIC1_DATA);
72     outb(0x02, PORT_PIC2_DATA);
73     // Send ICW4 (enable 8086 mode)
74     outb(0x01, PORT_PIC1_DATA);
75     outb(0x01, PORT_PIC2_DATA);
76     // Mask all irqs (except cascaded PIC2 irq)
77     outb(~PIC1_IRQ2, PORT_PIC1_DATA);
78     outb(~0, PORT_PIC2_DATA);
79 }
80
81 #endif // pic.h