blob: 1c96543a5101eb1184378296adbd015f5ba931fd [file] [log] [blame]
swissChili9b3584b2021-02-18 13:57:27 -08001#include "pic.h"
2#include "io.h"
swissChili825d46b2021-02-21 10:14:16 -08003#include "log.h"
swissChili9b3584b2021-02-18 13:57:27 -08004
swissChili825d46b2021-02-21 10:14:16 -08005#define ICW1_ICW4 0x01 /* ICW4 (not) needed */
6#define ICW1_SINGLE 0x02 /* Single (cascade) mode */
7#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */
8#define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */
9#define ICW1_INIT 0x10 /* Initialization - required! */
swissChilidefeb0d2021-02-18 15:28:36 -080010
swissChili825d46b2021-02-21 10:14:16 -080011#define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */
12#define ICW4_AUTO 0x02 /* Auto (normal) EOI */
13#define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */
14#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
15#define ICW4_SFNM 0x10 /* Special fully nested (not) */
swissChilidefeb0d2021-02-18 15:28:36 -080016
swissChili825d46b2021-02-21 10:14:16 -080017void (*interrupt_handlers[256])(struct registers *);
swissChili9b3584b2021-02-18 13:57:27 -080018
19void pic_send_eoi(uchar interrupt)
20{
swissChilidefeb0d2021-02-18 15:28:36 -080021 if (interrupt >= IRQ_TO_INT(8))
swissChili9b3584b2021-02-18 13:57:27 -080022 outb(PIC2_COMMAND, PIC_EOI);
swissChilidefeb0d2021-02-18 15:28:36 -080023
swissChili9b3584b2021-02-18 13:57:27 -080024 outb(PIC1_COMMAND, PIC_EOI);
25}
26
27void irq_handler(struct registers regs)
28{
29 pic_send_eoi(regs.interrupt_number);
30
31 if (interrupt_handlers[regs.interrupt_number])
swissChili825d46b2021-02-21 10:14:16 -080032 interrupt_handlers[regs.interrupt_number](&regs);
33 else
34 kprintf("Unhandled hardware interrupt: %d, called from %d\n", regs.interrupt_number, regs.eip);
swissChili9b3584b2021-02-18 13:57:27 -080035}
36
swissChili825d46b2021-02-21 10:14:16 -080037void isr_handler(struct registers regs)
38{
39 if (interrupt_handlers[regs.interrupt_number])
40 interrupt_handlers[regs.interrupt_number](&regs);
41 else
42 kprintf("Unhandled interrupt: %d, called from %d\n", regs.interrupt_number, regs.eip);
43}
44
45void add_interrupt_handler(uchar interrupt, void (*handler)(struct registers *))
swissChili9b3584b2021-02-18 13:57:27 -080046{
47 interrupt_handlers[interrupt] = handler;
48}
swissChilidefeb0d2021-02-18 15:28:36 -080049
50void pic_remap()
51{
swissChili825d46b2021-02-21 10:14:16 -080052 outb(0x20, 0x11);
53 outb(0xA0, 0x11);
54 outb(0x21, 0x20);
55 outb(0xA1, 0x28);
56 outb(0x21, 0x04);
57 outb(0xA1, 0x02);
58 outb(0x21, 0x01);
59 outb(0xA1, 0x01);
60 outb(0x21, 0x0);
61 outb(0xA1, 0x0);
swissChilidefeb0d2021-02-18 15:28:36 -080062
63 return;
64}