Home | History | Annotate | Line # | Download | only in jensenio
jensenio_intr.c revision 1.2
      1 /* $NetBSD: jensenio_intr.c,v 1.2 2000/08/14 05:38:23 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      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>			/* RCS ID & Copyright macro defns */
     40 
     41 __KERNEL_RCSID(0, "$NetBSD: jensenio_intr.c,v 1.2 2000/08/14 05:38:23 thorpej Exp $");
     42 
     43 #include <sys/types.h>
     44 #include <sys/param.h>
     45 #include <sys/time.h>
     46 #include <sys/systm.h>
     47 #include <sys/errno.h>
     48 #include <sys/malloc.h>
     49 #include <sys/device.h>
     50 #include <sys/syslog.h>
     51 
     52 #include <machine/autoconf.h>
     53 
     54 #include <dev/eisa/eisavar.h>
     55 
     56 #include <dev/isa/isareg.h>
     57 #include <dev/isa/isavar.h>
     58 
     59 #include <alpha/jensenio/jenseniovar.h>
     60 
     61 static bus_space_tag_t pic_iot;
     62 static bus_space_handle_t pic_ioh[2];
     63 static bus_space_handle_t pic_elcr_ioh;
     64 
     65 static const int irq_to_vector[] = {
     66 	0x900,		/* com0 */
     67 	0x920,		/* com1 */
     68 	0x980,		/* keyboard */
     69 	0x990,		/* mouse */
     70 };
     71 
     72 int	jensenio_eisa_intr_map(void *, u_int, eisa_intr_handle_t *);
     73 const char *jensenio_eisa_intr_string(void *, int);
     74 const struct evcnt *jensenio_eisa_intr_evcnt(void *, int);
     75 void	*jensenio_eisa_intr_establish(void *, int, int, int,
     76 	    int (*)(void *), void *);
     77 void	jensenio_eisa_intr_disestablish(void *, void *);
     78 int	jensenio_eisa_intr_alloc(void *, int, int, int *);
     79 
     80 /*
     81  * We have 16 (E)ISA IRQs, plus 4 hard-wired vectors which we
     82  * assign to "virtual" IRQs.
     83  */
     84 #define	JENSEN_MAX_IRQ		20
     85 #define	JENSEN_VECT_IRQ_BASE	16
     86 #define	JENSEN_IRQ_IS_EISA(x)	((x) < 16)
     87 
     88 struct alpha_shared_intr *jensenio_eisa_intr;
     89 
     90 void	jensenio_iointr(void *, u_long);
     91 
     92 void	jensenio_enable_intr(int, int);
     93 void	jensenio_setlevel(int, int);
     94 void	jensenio_pic_init(void);
     95 
     96 const int jensenio_intr_deftype[JENSEN_MAX_IRQ] = {
     97 	IST_EDGE,		/*  0: interval timer 0 output */
     98 	IST_EDGE,		/*  1: line printer */
     99 	IST_UNUSABLE,		/*  2: (cascade) */
    100 	IST_NONE,		/*  3: EISA pin B25 */
    101 	IST_NONE,		/*  4: EISA pin B24 */
    102 	IST_NONE,		/*  5: EISA pin B23 */
    103 	IST_NONE,		/*  6: EISA pin B22 (floppy) */
    104 	IST_NONE,		/*  7: EISA pin B21 */
    105 	IST_EDGE,		/*  8: RTC */
    106 	IST_NONE,		/*  9: EISA pin B04 */
    107 	IST_NONE,		/* 10: EISA pin D03 */
    108 	IST_NONE,		/* 11: EISA pin D04 */
    109 	IST_NONE,		/* 12: EISA pin D05 */
    110 	IST_UNUSABLE,		/* 13: not connected */
    111 	IST_NONE,		/* 14: EISA pin D07 (SCSI) */
    112 	IST_NONE,		/* 15: EISA pin D06 */
    113 	IST_EDGE,		/* 16: com0 (vector 0x900) */
    114 	IST_EDGE,		/* 17: com1 (vector 0x920) */
    115 	IST_EDGE,		/* 18: keyboard (vector 0x980) */
    116 	IST_EDGE,		/* 19: mouse (vector 0x990) */
    117 };
    118 
    119 static __inline void
    120 jensenio_specific_eoi(int irq)
    121 {
    122 
    123 	if (irq > 7)
    124 		bus_space_write_1(pic_iot, pic_ioh[1],
    125 		    0, 0x20 | (irq & 0x07));
    126 	bus_space_write_1(pic_iot, pic_ioh[0],
    127 	    0, 0x20 | (irq > 7 ? 2 : irq));
    128 }
    129 
    130 void
    131 jensenio_intr_init(struct jensenio_config *jcp)
    132 {
    133 	eisa_chipset_tag_t ec = &jcp->jc_ec;
    134 	isa_chipset_tag_t ic = &jcp->jc_ic;
    135 	char *cp;
    136 	int i;
    137 
    138 	pic_iot = &jcp->jc_eisa_iot;
    139 
    140 	jensenio_pic_init();
    141 
    142 	jensenio_eisa_intr = alpha_shared_intr_alloc(JENSEN_MAX_IRQ, 16);
    143 	for (i = 0; i < JENSEN_MAX_IRQ; i++) {
    144 		alpha_shared_intr_set_dfltsharetype(jensenio_eisa_intr,
    145 		    i, jensenio_intr_deftype[i]);
    146 		/* Don't bother with stray interrupts. */
    147 		alpha_shared_intr_set_maxstrays(jensenio_eisa_intr,
    148 		    i, 0);
    149 
    150 		cp = alpha_shared_intr_string(jensenio_eisa_intr, i);
    151 		if (JENSEN_IRQ_IS_EISA(i)) {
    152 			sprintf(cp, "irq %d", i);
    153 			evcnt_attach_dynamic(alpha_shared_intr_evcnt(
    154 			    jensenio_eisa_intr, i), EVCNT_TYPE_INTR,
    155 			    NULL, "eisa", cp);
    156 		} else {
    157 			sprintf(cp, "0x%03x",
    158 			    irq_to_vector[i - JENSEN_VECT_IRQ_BASE]);
    159 			evcnt_attach_dynamic(alpha_shared_intr_evcnt(
    160 			    jensenio_eisa_intr, i), EVCNT_TYPE_INTR,
    161 			    NULL, "vector", cp);
    162 		}
    163 	}
    164 
    165 	/*
    166 	 * The cascasde interrupt must be edge triggered and always enabled.
    167 	 */
    168 	jensenio_setlevel(2, 0);
    169 	jensenio_enable_intr(2, 1);
    170 
    171 	/*
    172 	 * Initialize the EISA chipset.
    173 	 */
    174 	ec->ec_v = jcp;
    175 	ec->ec_intr_map = jensenio_eisa_intr_map;
    176 	ec->ec_intr_string = jensenio_eisa_intr_string;
    177 	ec->ec_intr_evcnt = jensenio_eisa_intr_evcnt;
    178 	ec->ec_intr_establish = jensenio_eisa_intr_establish;
    179 	ec->ec_intr_disestablish = jensenio_eisa_intr_disestablish;
    180 
    181 	/*
    182 	 * Initialize the ISA chipset.
    183 	 */
    184 	ic->ic_v = jcp;
    185 	ic->ic_intr_establish = jensenio_eisa_intr_establish;
    186 	ic->ic_intr_disestablish = jensenio_eisa_intr_disestablish;
    187 	ic->ic_intr_alloc = jensenio_eisa_intr_alloc;
    188 	ic->ic_intr_evcnt = jensenio_eisa_intr_evcnt;
    189 
    190 	set_iointr(jensenio_iointr);
    191 }
    192 
    193 int
    194 jensenio_eisa_intr_map(void *v, u_int eirq, eisa_intr_handle_t *ihp)
    195 {
    196 
    197 	if (JENSEN_IRQ_IS_EISA(eirq) == 0) {
    198 		printf("jensenio_eisa_intr_map: bad EISA IRQ %d\n",
    199 		    eirq);
    200 		*ihp = -1;
    201 		return (1);
    202 	}
    203 
    204 	if (jensenio_intr_deftype[eirq] == IST_UNUSABLE) {
    205 		printf("jensenio_eisa_intr_map: unusable irq %d\n",
    206 		    eirq);
    207 		*ihp = -1;
    208 		return (1);
    209 	}
    210 
    211 	*ihp = eirq;
    212 	return (0);
    213 }
    214 
    215 const char *
    216 jensenio_eisa_intr_string(void *v, int eirq)
    217 {
    218 	static char irqstr[64];
    219 
    220 	if (eirq >= JENSEN_MAX_IRQ)
    221 		panic("jensenio_eisa_intr_string: bogus IRQ %d\n", eirq);
    222 
    223 	if (JENSEN_IRQ_IS_EISA(eirq) == 0)
    224 		sprintf(irqstr, "vector 0x%03x",
    225 		    irq_to_vector[eirq - JENSEN_VECT_IRQ_BASE]);
    226 	else
    227 		sprintf(irqstr, "eisa irq %d", eirq);
    228 
    229 	return (irqstr);
    230 }
    231 
    232 const struct evcnt *
    233 jensenio_eisa_intr_evcnt(void *v, int eirq)
    234 {
    235 
    236 	if (eirq > 19)
    237 		panic("jensenio_eisa_intr_evcnt: bogus IRQ %d\n", eirq);
    238 
    239 	return (alpha_shared_intr_evcnt(jensenio_eisa_intr, eirq));
    240 }
    241 
    242 void *
    243 jensenio_eisa_intr_establish(void *v, int irq, int type, int level,
    244     int (*fn)(void *), void *arg)
    245 {
    246 	void *cookie;
    247 
    248 	if (irq > 19 || type == IST_NONE)
    249 		panic("jensenio_eisa_intr_establish: bogus irq or type");
    250 
    251 	if (jensenio_intr_deftype[irq] == IST_UNUSABLE) {
    252 		printf("jensenio_eisa_intr_establish: IRQ %d not usable\n",
    253 		    irq);
    254 		return (NULL);
    255 	}
    256 
    257 	cookie = alpha_shared_intr_establish(jensenio_eisa_intr, irq,
    258 	    type, level, fn, arg, "eisa irq");
    259 
    260 	if (JENSEN_IRQ_IS_EISA(irq) == 0)
    261 		return (cookie);
    262 
    263 	if (cookie != NULL &&
    264 	    alpha_shared_intr_isactive(jensenio_eisa_intr, irq)) {
    265 		jensenio_setlevel(irq,
    266 		    alpha_shared_intr_get_sharetype(jensenio_eisa_intr,
    267 						    irq) == IST_LEVEL);
    268 		jensenio_enable_intr(irq, 1);
    269 	}
    270 
    271 	return (cookie);
    272 }
    273 
    274 void
    275 jensenio_eisa_intr_disestablish(void *v, void *cookie)
    276 {
    277 	struct alpha_shared_intrhand *ih = cookie;
    278 	int s, irq = ih->ih_num;
    279 
    280 	s = splhigh();
    281 
    282 	/* Remove it from the link. */
    283 	alpha_shared_intr_disestablish(jensenio_eisa_intr, cookie,
    284 	    "eisa irq");
    285 
    286 	if (JENSEN_IRQ_IS_EISA(irq) == 0) {
    287 		splx(s);
    288 		return;
    289 	}
    290 
    291 	if (alpha_shared_intr_isactive(jensenio_eisa_intr, irq) == 0) {
    292 		jensenio_enable_intr(irq, 0);
    293 		alpha_shared_intr_set_dfltsharetype(jensenio_eisa_intr,
    294 		    irq, jensenio_intr_deftype[irq]);
    295 	}
    296 
    297 	splx(s);
    298 }
    299 
    300 int
    301 jensenio_eisa_intr_alloc(void *v, int mask, int type, int *rqp)
    302 {
    303 
    304 	/* XXX Not supported right now. */
    305 	return (1);
    306 }
    307 
    308 void
    309 jensenio_iointr(void *framep, u_long vec)
    310 {
    311 	int irq;
    312 	int need_eoi = 0;
    313 
    314 	switch (vec) {
    315 	case 0x900: irq = 16; break;		/* com0 */
    316 	case 0x920: irq = 17; break;		/* com1 */
    317 	case 0x980: irq = 18; break;		/* keyboard */
    318 	case 0x990: irq = 19; break;		/* mouse */
    319 	default:
    320 		if (vec >= 0x800) {
    321 			if (vec >= 0x800 + (JENSEN_VECT_IRQ_BASE << 4))
    322 				panic("jensenio_iointr: vec 0x%lx out of "
    323 				    "range\n", vec);
    324 			irq = (vec - 0x800) >> 4;
    325 			need_eoi = 1;
    326 		} else
    327 			panic("jensenio_iointr: wierd vec 0x%lx\n", vec);
    328 	}
    329 
    330 	if (!alpha_shared_intr_dispatch(jensenio_eisa_intr, irq))
    331 		alpha_shared_intr_stray(jensenio_eisa_intr, irq, "eisa irq");
    332 
    333 	if (need_eoi)
    334 		jensenio_specific_eoi(irq);
    335 }
    336 
    337 void
    338 jensenio_enable_intr(int irq, int onoff)
    339 {
    340 	int pic;
    341 	u_int8_t bit, mask;
    342 
    343 	pic = irq >> 3;
    344 	bit = 1 << (irq & 0x7);
    345 
    346 	mask = bus_space_read_1(pic_iot, pic_ioh[pic], 1);
    347 	if (onoff)
    348 		mask &= ~bit;
    349 	else
    350 		mask |= bit;
    351 	bus_space_write_1(pic_iot, pic_ioh[pic], 1, mask);
    352 }
    353 
    354 void
    355 jensenio_setlevel(int irq, int level)
    356 {
    357 	int elcr;
    358 	u_int8_t bit, mask;
    359 
    360 	elcr = irq >> 3;
    361 	bit = 1 << (irq & 0x7);
    362 
    363 	mask = bus_space_read_1(pic_iot, pic_elcr_ioh, elcr);
    364 	if (level)
    365 		mask |= bit;
    366 	else
    367 		mask &= ~bit;
    368 	bus_space_write_1(pic_iot, pic_elcr_ioh, elcr, mask);
    369 }
    370 
    371 void
    372 jensenio_pic_init(void)
    373 {
    374 	static const int picaddr[2] = { IO_ICU1, IO_ICU2 };
    375 	int pic;
    376 
    377 	/*
    378 	 * Map the PICs and mask off the interrupts on them.
    379 	 */
    380 	for (pic = 0; pic < 2; pic++) {
    381 		if (bus_space_map(pic_iot, picaddr[pic], 2, 0, &pic_ioh[pic]))
    382 			panic("jensenio_init_intr: unable to map PIC %d", pic);
    383 		bus_space_write_1(pic_iot, pic_ioh[pic], 1, 0xff);
    384 	}
    385 
    386 	/*
    387 	 * Map the ELCR registers.
    388 	 */
    389 	if (bus_space_map(pic_iot, 0x4d0, 2, 0, &pic_elcr_ioh))
    390 		panic("jensenio_init_intr: unable to map ELCR registers");
    391 }
    392