cynix

x86 UNIX-like OS
git clone git://git.2f30.org/cynix
Log | Files | Refs | README | LICENSE

pic.c (1005B)


      1 /*
      2  *  core/pic.c
      3  *
      4  *  Copyright (C) 2009 stateless
      5  */
      6 
      7 #include <pic.h>
      8 #include <x86.h>
      9 #include <common.h>
     10 
     11 enum {
     12 	BASE_IRQ_PIC1 = 0x20,
     13 	BASE_IRQ_PIC2 = BASE_IRQ_PIC1 + 8
     14 };
     15 
     16 void
     17 remap_pic(void)
     18 {
     19 	/* ICW 1 */
     20 	outb(0x20, 0x11);
     21 	outb(0xa0, 0x11);
     22 
     23 	/* ICW  2 */
     24 	outb(0x21, BASE_IRQ_PIC1);
     25 	outb(0xa1, BASE_IRQ_PIC2);
     26 
     27 	/* ICW 3 */
     28 	outb(0x21, 0x4);
     29 	outb(0xa1, 0x2);
     30 
     31 	/* ICW 4 */
     32 	outb(0x21, 0x1);
     33 	outb(0xa1, 0x1);
     34 
     35 	/* nullify the data registers */
     36 	outb(0x21, 0x0);
     37 	outb(0xa1, 0x0);
     38 }
     39 
     40 void
     41 enable_irq(uint32_t irq)
     42 {
     43 	uint8_t imr;
     44 
     45 	imr = (irq <= 7) ? inb(0x21) : inb(0xa1);
     46 	irq %= 8;
     47 	imr &= ~(1 << irq);
     48 	if (irq <= 7) {
     49 		outb(0x21, imr);
     50 		return;
     51 	}
     52 	outb(0xa1, imr);
     53 }
     54 
     55 void
     56 disable_irq(uint32_t irq)
     57 {
     58 	uint8_t imr;
     59 
     60 	imr = (irq <= 7) ? inb(0x21) : inb(0xa1);
     61 	irq %= 8;
     62 	imr |= (1 << irq);
     63 	if (irq <= 7) {
     64 		outb(0x21, imr);
     65 		return;
     66 	}
     67 	outb(0xa1, imr);
     68 }
     69 
     70 void
     71 pic_eoi(uint32_t int_no)
     72 {
     73 	if (int_no > 31) {
     74 		if (int_no >= 40)
     75 			outb(0xa0, 0x20);
     76 		outb(0x20, 0x20);
     77 	}
     78 }
     79