Contiki 3.x
pic.h
1 /*
2  * Copyright (C) 2015, Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef PIC_H
32 #define PIC_H
33 
34 #include "helpers.h"
35 
36 #define PIC1_CMD_PORT 0x20
37 #define PIC1_DATA_PORT 0x21
38 #define PIC2_CMD_PORT 0xA0
39 #define PIC2_DATA_PORT 0xA1
40 #define PIC1_OFFSET 0x20
41 #define PIC2_OFFSET PIC1_OFFSET + 8
42 
43 /*
44  * Returns the actual interrupt number of a given IRQ,
45  * no matter which PIC it is part of.
46  */
47 #define PIC_INT(a) (a + PIC1_OFFSET)
48 
49 void pic_unmask_irq(unsigned int num);
50 
51 /* This function initializes the daisy-chained Master and Slave 8259 PICs.
52  * It is only called once, so let's give the compiler the option to inline it.
53  * For more information about the ICWs, please refer to http://stanislavs.org/helppc/8259.html.
54  */
55 static inline void
56 pic_init(void)
57 {
58  /* ICW1: Initialization. */
59  outb(PIC1_CMD_PORT, 0x11);
60  outb(PIC2_CMD_PORT, 0x11);
61 
62  /* ICW2: Remap IRQs by setting an IDT Offset for each PIC. */
63  outb(PIC1_DATA_PORT, PIC1_OFFSET);
64  outb(PIC2_DATA_PORT, PIC2_OFFSET);
65 
66  /* ICW3: Setup Slave to Master's IRQ2. */
67  outb(PIC1_DATA_PORT, 0x04);
68  outb(PIC2_DATA_PORT, 0x02);
69 
70  /* ICW4: Environment setup. Set PIC1 as master and PIC2 as slave. */
71  outb(PIC1_DATA_PORT, 0x01);
72  outb(PIC2_DATA_PORT, 0x01);
73 
74  /* Set the IMR register, masking all hardware interrupts but IRQ 2.
75  * We will have to unmask each IRQ when registering them. */
76  outb(PIC1_DATA_PORT, 0xfb);
77  outb(PIC2_DATA_PORT, 0xff);
78 }
79 
80 /*
81  * This function sends an end-of-interrupt (EOI) to the correct PIC according
82  * to the IRQ line number.
83  */
84 void pic_eoi(unsigned int irq);
85 
86 #endif /* PIC_H */