1 /* $NetBSD: i8259_common.c,v 1.6 2011/06/20 06:21:45 matt Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Tim Rightnour 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: i8259_common.c,v 1.6 2011/06/20 06:21:45 matt Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/malloc.h> 37 #include <sys/kernel.h> 38 #include <sys/intr.h> 39 40 #include <uvm/uvm_extern.h> 41 42 #include <machine/pio.h> 43 44 #include <powerpc/pic/picvar.h> 45 46 #include <dev/isa/isareg.h> 47 #include <dev/isa/isavar.h> 48 49 void 50 i8259_initialize(void) 51 { 52 isa_outb(IO_ICU1, 0x11); /* program device, four bytes */ 53 isa_outb(IO_ICU1+1, 0); /* starting at this vector */ 54 isa_outb(IO_ICU1+1, 1 << IRQ_SLAVE); /* slave on line 2 */ 55 isa_outb(IO_ICU1+1, 1); /* 8086 mode */ 56 isa_outb(IO_ICU1+1, 0xff); /* leave interrupts masked */ 57 58 isa_outb(IO_ICU2, 0x11); /* program device, four bytes */ 59 isa_outb(IO_ICU2+1, 8); /* starting at this vector */ 60 isa_outb(IO_ICU2+1, IRQ_SLAVE); 61 isa_outb(IO_ICU2+1, 1); /* 8086 mode */ 62 isa_outb(IO_ICU2+1, 0xff); /* leave interrupts masked */ 63 } 64 65 void 66 i8259_enable_irq(struct pic_ops *pic, int irq, int type) 67 { 68 struct i8259_ops *i8259 = (struct i8259_ops *)pic; 69 70 i8259->irqs |= 1 << irq; 71 if (i8259->irqs >= 0x100) /* IRQS >= 8 in use? */ 72 i8259->irqs |= 1 << IRQ_SLAVE; 73 74 i8259->enable_mask = ~i8259->irqs; 75 isa_outb(IO_ICU1+1, i8259->enable_mask); 76 isa_outb(IO_ICU2+1, i8259->enable_mask >> 8); 77 } 78 79 void 80 i8259_disable_irq(struct pic_ops *pic, int irq) 81 { 82 struct i8259_ops *i8259 = (struct i8259_ops *)pic; 83 uint32_t mask = 1 << irq; 84 85 i8259->enable_mask |= mask; 86 isa_outb(IO_ICU1+1, i8259->enable_mask); 87 isa_outb(IO_ICU2+1, i8259->enable_mask >> 8); 88 } 89 90 void 91 i8259_ack_irq(struct pic_ops *pic, int irq) 92 { 93 if (irq < 8) { 94 isa_outb(IO_ICU1, 0xe0 | irq); 95 } else { 96 isa_outb(IO_ICU2, 0xe0 | (irq & 7)); 97 isa_outb(IO_ICU1, 0xe0 | IRQ_SLAVE); 98 } 99 } 100 101 int 102 i8259_get_irq(struct pic_ops *pic, int mode) 103 { 104 int irq; 105 106 isa_outb(IO_ICU1, 0x0c); 107 irq = isa_inb(IO_ICU1) & 0x07; 108 if (irq == IRQ_SLAVE) { 109 isa_outb(IO_ICU2, 0x0c); 110 irq = (isa_inb(IO_ICU2) & 0x07) + 8; 111 } 112 113 if (irq == 0 && mode == PIC_GET_IRQ) 114 return 255; 115 if (irq == 7 && mode == PIC_GET_RECHECK) 116 return 255; 117 118 return irq; 119 } 120