Home | History | Annotate | Line # | Download | only in pic
pic_prepivr.c revision 1.1.2.4
      1 /* $NetBSD: pic_prepivr.c,v 1.1.2.4 2007/05/04 00:57:24 garbled 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: pic_prepivr.c,v 1.1.2.4 2007/05/04 00:57:24 garbled Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/malloc.h>
     44 #include <sys/kernel.h>
     45 
     46 #include <uvm/uvm_extern.h>
     47 
     48 #include <machine/pio.h>
     49 #include <machine/intr.h>
     50 
     51 #include <arch/powerpc/pic/picvar.h>
     52 
     53 #include <dev/isa/isareg.h>
     54 #include <dev/isa/isavar.h>
     55 
     56 void prepivr_initialize(void);
     57 static int  prepivr_irq_is_enabled(struct pic_ops *, int);
     58 static void prepivr_enable_irq(struct pic_ops *, int, int);
     59 static void prepivr_disable_irq(struct pic_ops *, int);
     60 static void prepivr_clear_irq(struct pic_ops *, int);
     61 static int  prepivr_get_irq(struct pic_ops *);
     62 static void prepivr_ack_irq(struct pic_ops *, int);
     63 static void prepivr_establish_irq(struct pic_ops *pic, int irq, int type);
     64 
     65 struct prepivr_ops {
     66 	struct pic_ops pic;
     67 	uint32_t pending_events;
     68 	uint32_t enable_mask;
     69 	uint32_t irqs;
     70 };
     71 
     72 vaddr_t prep_intr_reg;		/* PReP interrupt vector register */
     73 uint32_t prep_intr_reg_off;	/* IVR offset within the mapped page */
     74 
     75 #define IO_ELCR1	0x4d0
     76 #define IO_ELCR2	0x4d1
     77 
     78 /*
     79  * Prior to calling init_prepivr, the port is expected to have mapped the
     80  * page containing the IVR with mapiodev to prep_intr_reg and set up the
     81  * prep_intr_reg_off.
     82  */
     83 
     84 struct pic_ops *
     85 setup_prepivr(void)
     86 {
     87 	struct prepivr_ops *prepivr;
     88 	struct pic_ops *pic;
     89 	uint32_t pivr;
     90 
     91 	prepivr = malloc(sizeof(struct prepivr_ops), M_DEVBUF, M_NOWAIT);
     92 	KASSERT(prepivr != NULL);
     93 	pic = &prepivr->pic;
     94 
     95 	pivr = prep_intr_reg + prep_intr_reg_off;
     96 	pic->pic_numintrs = 16;
     97 	pic->pic_cookie = (void *)pivr;
     98 	pic->pic_irq_is_enabled = prepivr_irq_is_enabled;
     99 	pic->pic_enable_irq = prepivr_enable_irq;
    100 	pic->pic_reenable_irq = prepivr_enable_irq;
    101 	pic->pic_disable_irq = prepivr_disable_irq;
    102 	pic->pic_clear_irq = prepivr_clear_irq;
    103 	pic->pic_get_irq = prepivr_get_irq;
    104 	pic->pic_ack_irq = prepivr_ack_irq;
    105 	pic->pic_establish_irq = prepivr_establish_irq;
    106 	strcpy(pic->pic_name, "prepivr");
    107 	pic_add(pic);
    108 	prepivr->pending_events = 0;
    109 	prepivr->enable_mask = 0xffffffff;
    110 	prepivr->irqs = 0;
    111 
    112 	prepivr_initialize();
    113 
    114 	return pic;
    115 }
    116 
    117 void
    118 prepivr_initialize(void)
    119 {
    120 	isa_outb(IO_ICU1, 0x11);		/* program device, four bytes */
    121 	isa_outb(IO_ICU1+1, 0);			/* starting at this vector */
    122 	isa_outb(IO_ICU1+1, 1 << IRQ_SLAVE);	/* slave on line 2 */
    123 	isa_outb(IO_ICU1+1, 1);			/* 8086 mode */
    124 	isa_outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    125 
    126 	isa_outb(IO_ICU2, 0x11);		/* program device, four bytes */
    127 	isa_outb(IO_ICU2+1, 8);			/* starting at this vector */
    128 	isa_outb(IO_ICU2+1, IRQ_SLAVE);
    129 	isa_outb(IO_ICU2+1, 1);			/* 8086 mode */
    130 	isa_outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    131 }
    132 
    133 static int
    134 prepivr_irq_is_enabled(struct pic_ops *pic, int irq)
    135 {
    136 	return 1;
    137 }
    138 
    139 static void
    140 prepivr_establish_irq(struct pic_ops *pic, int irq, int type)
    141 {
    142 	u_int8_t elcr[2];
    143 	int icu, bit;
    144 
    145 	icu = irq / 8;
    146 	bit = irq % 8;
    147 	elcr[0] = isa_inb(IO_ELCR1);
    148 	elcr[1] = isa_inb(IO_ELCR2);
    149 
    150 	if (type == IST_LEVEL)
    151 		elcr[icu] |= 1 << bit;
    152 	else
    153 		elcr[icu] &= ~(1 << bit);
    154 
    155 	isa_outb(IO_ELCR1, elcr[0]);
    156 	isa_outb(IO_ELCR2, elcr[1]);
    157 }
    158 
    159 static void
    160 prepivr_enable_irq(struct pic_ops *pic, int irq, int type)
    161 {
    162 	struct prepivr_ops *prepivr = (struct prepivr_ops *)pic;
    163 
    164 	prepivr->irqs |= 1 << irq;
    165 	if (prepivr->irqs >= 0x100) /* IRQS >= 8 in use? */
    166 		prepivr->irqs |= 1 << IRQ_SLAVE;
    167 
    168 	prepivr->enable_mask = ~prepivr->irqs;
    169 	isa_outb(IO_ICU1+1, prepivr->enable_mask);
    170 	isa_outb(IO_ICU2+1, prepivr->enable_mask >> 8);
    171 }
    172 
    173 static void
    174 prepivr_disable_irq(struct pic_ops *pic, int irq)
    175 {
    176 	struct prepivr_ops *prepivr = (struct prepivr_ops *)pic;
    177 	uint32_t mask = 1 << irq;
    178 
    179 	prepivr->enable_mask |= mask;
    180 	isa_outb(IO_ICU1+1, prepivr->enable_mask);
    181 	isa_outb(IO_ICU2+1, prepivr->enable_mask >> 8);
    182 }
    183 
    184 static void
    185 prepivr_clear_irq(struct pic_ops *pic, int irq)
    186 {
    187 	/* do nothing */
    188 }
    189 
    190 static int
    191 prepivr_get_irq(struct pic_ops *pic)
    192 {
    193 	static int lirq;
    194 	int irq;
    195 
    196 	irq = inb(pic->pic_cookie);
    197 	if (lirq == 7 && irq == lirq)
    198 		return 255;
    199 
    200 	lirq = irq;
    201 	if (irq == 0)
    202 		return 255;
    203 
    204 	return irq;
    205 }
    206 
    207 static void
    208 prepivr_ack_irq(struct pic_ops *pic, int irq)
    209 {
    210 	if (irq < 8) {
    211 		isa_outb(IO_ICU1, 0xe0 | irq);
    212 	} else {
    213 		isa_outb(IO_ICU2, 0xe0 | (irq & 7));
    214 		isa_outb(IO_ICU1, 0xe0 | IRQ_SLAVE);
    215 	}
    216 }
    217