1 | #include <unistd.h>
|
---|
2 | #include <ddi.h>
|
---|
3 | #include <libarch/ddi.h>
|
---|
4 | #include <stdio.h>
|
---|
5 |
|
---|
6 | #include "pic.h"
|
---|
7 |
|
---|
8 | #define PIC1 0x20 /* IO base address for master PIC */
|
---|
9 | #define PIC2 0xA0 /* IO base address for slave PIC */
|
---|
10 | #define REG_COUNT 2 /* command and data */
|
---|
11 |
|
---|
12 | #define NAME "PIC"
|
---|
13 |
|
---|
14 | static ioport8_t *pic1_cmd, *pic1_data, *pic2_cmd, *pic2_data;
|
---|
15 |
|
---|
16 | static int pic_enable_ports(void *base_phys_addr, ioport8_t **pic_cmd, ioport8_t **pic_data);
|
---|
17 |
|
---|
18 |
|
---|
19 | int pic_init()
|
---|
20 | {
|
---|
21 | printf(NAME ": enabling ports 0x%x - 0x%x.\n", PIC1, PIC1 + 1);
|
---|
22 | if (!pic_enable_ports((void *)PIC1, &pic1_cmd, &pic1_data)) {
|
---|
23 | printf(NAME ": Error - master PIC initialization failed.\n");
|
---|
24 | return 0;
|
---|
25 | }
|
---|
26 |
|
---|
27 | printf(NAME ": enabling ports 0x%x - 0x%x.\n", PIC2, PIC2 + 1);
|
---|
28 | if (!pic_enable_ports((void *)PIC2, &pic2_cmd, &pic2_data)) {
|
---|
29 | printf(NAME ": Error - slave PIC initialization failed.");
|
---|
30 | return 0;
|
---|
31 | }
|
---|
32 |
|
---|
33 | printf(NAME ": initialization was successful.\n");
|
---|
34 | return 1;
|
---|
35 | }
|
---|
36 |
|
---|
37 | static int pic_enable_ports(void *base_phys_addr, ioport8_t **pic_cmd, ioport8_t **pic_data)
|
---|
38 | {
|
---|
39 | if (pio_enable(base_phys_addr, REG_COUNT, (void **)(pic_cmd))) { // Gain control over port's registers.
|
---|
40 | printf(NAME ": Error - cannot gain the port %lx.\n", base_phys_addr);
|
---|
41 | return 0;
|
---|
42 | }
|
---|
43 |
|
---|
44 | *pic_data = *pic_cmd + 1;
|
---|
45 | return 1;
|
---|
46 | }
|
---|
47 |
|
---|
48 | void pic_enable_interrupt(int irq)
|
---|
49 | {
|
---|
50 | printf(NAME ": pic_enable_interrupt %d.", irq);
|
---|
51 | pic_enable_irqs(1 << irq);
|
---|
52 | printf(NAME ": interrupt %d enabled.", irq);
|
---|
53 | }
|
---|
54 |
|
---|
55 | void pic_enable_irqs(uint16_t irqmask)
|
---|
56 | {
|
---|
57 | uint8_t x;
|
---|
58 |
|
---|
59 | if (irqmask & 0xff) {
|
---|
60 | x = pio_read_8(pic1_data);
|
---|
61 | pio_write_8(pic1_data, (uint8_t) (x & (~(irqmask & 0xff))));
|
---|
62 | }
|
---|
63 | if (irqmask >> 8) {
|
---|
64 | x = pio_read_8(pic2_data);
|
---|
65 | pio_write_8(pic2_data, (uint8_t) (x & (~(irqmask >> 8))));
|
---|
66 | }
|
---|
67 | }
|
---|