Home | History | Annotate | Line # | Download | only in tx
tx39icu.c revision 1.15
      1 /*	$NetBSD: tx39icu.c,v 1.15 2002/01/02 13:13:20 uch Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999-2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      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 "opt_vr41xx.h"
     40 #include "opt_tx39xx.h"
     41 
     42 #include "opt_tx39_debug.h"
     43 #include "opt_use_poll.h"
     44 #include "opt_tx39icudebug.h"
     45 #include "opt_tx39_watchdogtimer.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/device.h>
     50 #include <sys/malloc.h>
     51 #include <sys/queue.h>
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <mips/cpuregs.h>
     56 #include <machine/bus.h>
     57 
     58 #include <hpcmips/tx/tx39var.h>
     59 #include <hpcmips/tx/tx39icureg.h>
     60 #include <hpcmips/tx/tx39clockvar.h>
     61 
     62 #include <machine/cpu.h>
     63 #include <dev/dec/clockvar.h>
     64 
     65 #undef TX39ICUDEBUG_PRINT_PENDING_INTERRUPT /* For explorer. good luck! */
     66 
     67 #if defined(VR41XX) && defined(TX39XX)
     68 #define	TX_INTR	tx_intr
     69 #else
     70 #define	TX_INTR	cpu_intr	/* locore_mips3 directly call this */
     71 #endif
     72 void TX_INTR(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
     73 
     74 #ifdef TX39ICUDEBUG
     75 #define	DPRINTF(arg) printf arg
     76 #else
     77 #define	DPRINTF(arg)
     78 #endif
     79 u_int32_t tx39intrvec;
     80 
     81 /*
     82  * This is a mask of bits to clear in the SR when we go to a
     83  * given interrupt priority level.
     84  */
     85 const u_int32_t __ipl_sr_bits_tx[_IPL_N] = {
     86 	0,					/* IPL_NONE */
     87 
     88 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFT */
     89 
     90 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFTCLOCK */
     91 
     92 	MIPS_SOFT_INT_MASK_0|
     93 		MIPS_SOFT_INT_MASK_1,		/* IPL_SOFTNET */
     94 
     95 	MIPS_SOFT_INT_MASK_0|
     96 		MIPS_SOFT_INT_MASK_1,		/* IPL_SOFTSERIAL */
     97 
     98 	MIPS_SOFT_INT_MASK_0|
     99 		MIPS_SOFT_INT_MASK_1|
    100 		MIPS_INT_MASK_2|
    101 		MIPS_INT_MASK_4,		/* IPL_BIO */
    102 
    103 	MIPS_SOFT_INT_MASK_0|
    104 		MIPS_SOFT_INT_MASK_1|
    105 		MIPS_INT_MASK_2|
    106 		MIPS_INT_MASK_4,		/* IPL_NET */
    107 
    108 	MIPS_SOFT_INT_MASK_0|
    109 		MIPS_SOFT_INT_MASK_1|
    110 		MIPS_INT_MASK_2|
    111 		MIPS_INT_MASK_4,		/* IPL_{TTY,SERIAL} */
    112 
    113 	MIPS_SOFT_INT_MASK_0|
    114 		MIPS_SOFT_INT_MASK_1|
    115 		MIPS_INT_MASK_2|
    116 		MIPS_INT_MASK_4,		/* IPL_{CLOCK,HIGH} */
    117 };
    118 
    119 /* IRQHIGH lines list */
    120 static const struct irqhigh_list {
    121 	int qh_pri; /* IRQHIGH priority */
    122 	int qh_set; /* Register set */
    123 	int qh_bit; /* bit offset in the register set */
    124 } irqhigh_list[] = {
    125 	{15,	5,	25},	/* POSPWROKINT */
    126 	{15,	5,	24},	/* NEGPWROKINT */
    127 	{14,	5,	30},	/* ALARMINT*/
    128 	{13,	5,	29},	/* PERINT */
    129 #ifdef TX391X
    130 	{12,	2,	3},	/* MBUSPOSINT */
    131 	{12,	2,	2},	/* MBUSNEGINT */
    132 	{11,	2,	31},	/* UARTARXINT */
    133 	{10,	2,	21},	/* UARTBRXINT */
    134 	{9,	3,	19},	/* MFIOPOSINT19 */
    135 	{9,	3,	18},	/* MFIOPOSINT18 */
    136 	{9,	3,	17},	/* MFIOPOSINT17 */
    137 	{9,	3,	16},	/* MFIOPOSINT16 */
    138 	{8,	3,	1},	/* MFIOPOSINT1 */
    139 	{8,	3,	0},	/* MFIOPOSINT0 */
    140 	{8,	5,	13},	/* IOPOSINT6 */
    141 	{8,	5,	12},	/* IOPOSINT5 */
    142 	{7,	4,	19},	/* MFIONEGINT19 */
    143 	{7,	4,	18},	/* MFIONEGINT18 */
    144 	{7,	4,	17},	/* MFIONEGINT17 */
    145 	{7,	4,	16},	/* MFIONEGINT16 */
    146 	{6,	4,	1},	/* MFIONEGINT1 */
    147 	{6,	4,	0},	/* MFIONEGINT0 */
    148 	{6,	5,	6},	/* IONEGINT6 */
    149 	{6,	5,	5},	/* IONEGINT5 */
    150 	{5,	2,	5},	/* MBUSDMAFULLINT */
    151 #endif /* TX391X */
    152 #ifdef TX392X
    153 	{12,	2,	31},	/* UARTARXINT */
    154 	{12,	2,	21},	/* UARTBRXINT */
    155 	{11,	3,	19},	/* MFIOPOSINT19 */
    156 	{11,	3,	18},	/* MFIOPOSINT18 */
    157 	{11,	3,	17},	/* MFIOPOSINT17 */
    158 	{11,	3,	16},	/* MFIOPOSINT16 */
    159 	{10,	3,	1},	/* MFIOPOSINT1 */
    160 	{10,	3,	0},	/* MFIOPOSINT0 */
    161 	{10,	5,	13},	/* IOPOSINT6 */
    162 	{10,	5,	12},	/* IOPOSINT5 */
    163 	{9,	4,	19},	/* MFIONEGINT19 */
    164 	{9,	4,	18},	/* MFIONEGINT18 */
    165 	{9,	4,	17},	/* MFIONEGINT17 */
    166 	{9,	4,	16},	/* MFIONEGINT16 */
    167 	{8,	4,	1},	/* MFIONEGINT1 */
    168 	{8,	4,	0},	/* MFIONEGINT0 */
    169 	{8,	5,	6},	/* IONEGINT6 */
    170 	{8,	5,	5},	/* IONEGINT5 */
    171 	{5,	7,	19},	/* IRRXCINT */
    172 	{5,	7,	17},	/* IRRXEINT */
    173 #endif /* TX392X */
    174 	{4,	1,	18},	/* SNDDMACNTINT */
    175 	{3,	1,	17},	/* TELDMACNTINT */
    176 	{2,	1,	27},	/* CHIDMACNTINT */
    177 	{1,	5,	7},	/* IOPOSINT0 */
    178 	{1,	5,	0}	/* IONEGINT0 */
    179 };
    180 
    181 struct txintr_high_entry {
    182 	int	he_set;
    183 	txreg_t	he_mask;
    184 	int	(*he_fun)(void *);
    185 	void	*he_arg;
    186 	TAILQ_ENTRY(txintr_high_entry) he_link;
    187 };
    188 
    189 #ifdef USE_POLL
    190 struct txpoll_entry{
    191 	int	p_cnt; /* dispatch interval */
    192 	int	p_desc;
    193 	int	(*p_fun)(void *);
    194 	void	*p_arg;
    195 	TAILQ_ENTRY(txpoll_entry) p_link;
    196 };
    197 int	tx39_poll_intr(void *);
    198 #endif /* USE_POLL */
    199 
    200 struct tx39icu_softc {
    201 	struct	device sc_dev;
    202 	tx_chipset_tag_t sc_tc;
    203 	/* IRQLOW */
    204 	txreg_t	sc_le_mask[TX39_INTRSET_MAX + 1];
    205 	int	(*sc_le_fun[TX39_INTRSET_MAX + 1][32])(void *);
    206 	void	*sc_le_arg[TX39_INTRSET_MAX + 1][32];
    207 	/* IRQHIGH */
    208 	TAILQ_HEAD(, txintr_high_entry) sc_he_head[TX39_IRQHIGH_MAX];
    209 	/* Register */
    210 	txreg_t sc_regs[TX39_INTRSET_MAX + 1];
    211 #ifdef USE_POLL
    212 	unsigned sc_pollcnt;
    213 	int	sc_polling;
    214 	void	*sc_poll_ih;
    215 	TAILQ_HEAD(, txpoll_entry) sc_p_head;
    216 #endif /* USE_POLL */
    217 };
    218 
    219 int	tx39icu_match(struct device *, struct cfdata *, void *);
    220 void	tx39icu_attach(struct device *, struct device *, void *);
    221 int	tx39icu_intr(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
    222 
    223 void	tx39_intr_dump(struct tx39icu_softc *);
    224 void	tx39_intr_decode(int, int *, int *);
    225 void	tx39_irqhigh_disestablish(tx_chipset_tag_t, int, int, int);
    226 void	tx39_irqhigh_establish(tx_chipset_tag_t, int, int, int,
    227 	    int (*)(void *), void *);
    228 void	tx39_irqhigh_intr(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
    229 int	tx39_irqhigh(int, int);
    230 
    231 struct cfattach tx39icu_ca = {
    232 	sizeof(struct tx39icu_softc), tx39icu_match, tx39icu_attach
    233 };
    234 
    235 int
    236 tx39icu_match(struct device *parent, struct cfdata *cf, void *aux)
    237 {
    238 
    239 	return (ATTACH_FIRST);
    240 }
    241 
    242 void
    243 tx39icu_attach(struct device *parent, struct device *self, void *aux)
    244 {
    245 	struct txsim_attach_args *ta = aux;
    246 	struct tx39icu_softc *sc = (void *)self;
    247 	tx_chipset_tag_t tc = ta->ta_tc;
    248 	txreg_t reg, *regs;
    249 	int i;
    250 
    251 	printf("\n");
    252 	sc->sc_tc = ta->ta_tc;
    253 
    254 	regs = sc->sc_regs;
    255 	regs[0] = tx_conf_read(tc, TX39_INTRSTATUS6_REG);
    256 	regs[1] = tx_conf_read(tc, TX39_INTRSTATUS1_REG);
    257 	regs[2] = tx_conf_read(tc, TX39_INTRSTATUS2_REG);
    258 	regs[3] = tx_conf_read(tc, TX39_INTRSTATUS3_REG);
    259 	regs[4] = tx_conf_read(tc, TX39_INTRSTATUS4_REG);
    260 	regs[5] = tx_conf_read(tc, TX39_INTRSTATUS5_REG);
    261 #ifdef TX392X
    262 	regs[7] = tx_conf_read(tc, TX39_INTRSTATUS7_REG);
    263 	regs[8] = tx_conf_read(tc, TX39_INTRSTATUS8_REG);
    264 #endif
    265 #ifdef TX39ICUDEBUG
    266 	printf("\t[Windows CE setting]\n");
    267 	tx39_intr_dump(sc);
    268 #endif /* TX39ICUDEBUG */
    269 
    270 #ifdef WINCE_DEFAULT_SETTING
    271 #warning WINCE_DEFAULT_SETTING
    272 #else /* WINCE_DEFAULT_SETTING */
    273 	/* Disable IRQLOW */
    274 	tx_conf_write(tc, TX39_INTRENABLE1_REG, 0);
    275 	tx_conf_write(tc, TX39_INTRENABLE2_REG, 0);
    276 	tx_conf_write(tc, TX39_INTRENABLE3_REG, 0);
    277 	tx_conf_write(tc, TX39_INTRENABLE4_REG, 0);
    278 	tx_conf_write(tc, TX39_INTRENABLE5_REG, 0);
    279 #ifdef TX392X
    280 	tx_conf_write(tc, TX39_INTRENABLE7_REG, 0);
    281 	tx_conf_write(tc, TX39_INTRENABLE8_REG, 0);
    282 #endif /* TX392X */
    283 
    284 	/* Disable IRQHIGH */
    285 	reg = tx_conf_read(tc, TX39_INTRENABLE6_REG);
    286 	reg &= ~TX39_INTRENABLE6_PRIORITYMASK_MASK;
    287 	tx_conf_write(tc, TX39_INTRENABLE6_REG, reg);
    288 #endif /* WINCE_DEFAULT_SETTING */
    289 
    290 	/* Clear all pending interrupts */
    291 	tx_conf_write(tc, TX39_INTRCLEAR1_REG,
    292 	    tx_conf_read(tc, TX39_INTRSTATUS1_REG));
    293 	tx_conf_write(tc, TX39_INTRCLEAR2_REG,
    294 	    tx_conf_read(tc, TX39_INTRSTATUS2_REG));
    295 	tx_conf_write(tc, TX39_INTRCLEAR3_REG,
    296 	    tx_conf_read(tc, TX39_INTRSTATUS3_REG));
    297 	tx_conf_write(tc, TX39_INTRCLEAR4_REG,
    298 	    tx_conf_read(tc, TX39_INTRSTATUS4_REG));
    299 	tx_conf_write(tc, TX39_INTRCLEAR5_REG,
    300 	    tx_conf_read(tc, TX39_INTRSTATUS5_REG));
    301 #ifdef TX392X
    302 	tx_conf_write(tc, TX39_INTRCLEAR7_REG,
    303 	    tx_conf_read(tc, TX39_INTRSTATUS7_REG));
    304 	tx_conf_write(tc, TX39_INTRCLEAR8_REG,
    305 	    tx_conf_read(tc, TX39_INTRSTATUS8_REG));
    306 #endif /* TX392X */
    307 
    308 	/* Enable global interrupts */
    309 	reg = tx_conf_read(tc, TX39_INTRENABLE6_REG);
    310 	reg |= TX39_INTRENABLE6_GLOBALEN;
    311 	tx_conf_write(tc, TX39_INTRENABLE6_REG, reg);
    312 
    313 	/* Initialize IRQHIGH interrupt handler holder*/
    314 	for (i = 0; i < TX39_IRQHIGH_MAX; i++) {
    315 		TAILQ_INIT(&sc->sc_he_head[i]);
    316 	}
    317 #ifdef USE_POLL
    318 	/* Initialize polling handler holder */
    319 	TAILQ_INIT(&sc->sc_p_head);
    320 #endif /* USE_POLL */
    321 
    322 	/* Register interrupt module myself */
    323 	tx_conf_register_intr(tc, self);
    324 }
    325 
    326 void
    327 TX_INTR(u_int32_t status, u_int32_t cause, u_int32_t pc, u_int32_t ipending)
    328 {
    329 	struct tx39icu_softc *sc;
    330 	tx_chipset_tag_t tc;
    331 	txreg_t reg, pend, *regs;
    332 	int i, j;
    333 
    334 	uvmexp.intrs++;
    335 
    336 	if ((ipending & MIPS_HARD_INT_MASK) == 0)
    337 		goto softintr;
    338 
    339 	tc = tx_conf_get_tag();
    340 	sc = tc->tc_intrt;
    341 	/*
    342 	 * Read regsiter ASAP
    343 	 */
    344 	regs = sc->sc_regs;
    345 	regs[0] = tx_conf_read(tc, TX39_INTRSTATUS6_REG);
    346 	regs[1] = tx_conf_read(tc, TX39_INTRSTATUS1_REG);
    347 	regs[2] = tx_conf_read(tc, TX39_INTRSTATUS2_REG);
    348 	regs[3] = tx_conf_read(tc, TX39_INTRSTATUS3_REG);
    349 	regs[4] = tx_conf_read(tc, TX39_INTRSTATUS4_REG);
    350 	regs[5] = tx_conf_read(tc, TX39_INTRSTATUS5_REG);
    351 #ifdef TX392X
    352 	regs[7] = tx_conf_read(tc, TX39_INTRSTATUS7_REG);
    353 	regs[8] = tx_conf_read(tc, TX39_INTRSTATUS8_REG);
    354 #endif
    355 
    356 #ifdef TX39ICUDEBUG
    357 	if (!(ipending & MIPS_INT_MASK_4) && !(ipending & MIPS_INT_MASK_2)) {
    358 		bitdisp(ipending);
    359 		panic("bogus HwInt");
    360 	}
    361 #ifdef TX39_DEBUG
    362 	if (tx39debugflag) {
    363 		tx39_intr_dump(sc);
    364 	}
    365 #endif
    366 #endif /* TX39ICUDEBUG */
    367 
    368 	/* IRQHIGH */
    369 	if (ipending & MIPS_INT_MASK_4) {
    370 		tx39_irqhigh_intr(ipending, pc, status, cause);
    371 
    372 		goto softintr;
    373 	}
    374 
    375 	/* IRQLOW */
    376 	if (ipending & MIPS_INT_MASK_2) {
    377 		for (i = 1; i <= TX39_INTRSET_MAX; i++) {
    378 			int ofs;
    379 #ifdef TX392X
    380 			if (i == 6)
    381 				continue;
    382 #endif /* TX392X */
    383 			ofs = TX39_INTRSTATUS_REG(i);
    384 			pend = sc->sc_regs[i];
    385 			reg = sc->sc_le_mask[i] & pend;
    386 			/* Clear interrupts */
    387 			tx_conf_write(tc, ofs, reg);
    388 			/* Dispatch handler */
    389 			for (j = 0 ; j < 32; j++) {
    390 				if ((reg & (1 << j)) &&
    391 				    sc->sc_le_fun[i][j]) {
    392 #ifdef TX39_DEBUG
    393 					tx39intrvec = (i << 16) | j;
    394 					if (tx39debugflag) {
    395 						DPRINTF(("IRQLOW %d:%d\n",
    396 						    i, j));
    397 					}
    398 #endif /* TX39_DEBUG */
    399 					(*sc->sc_le_fun[i][j])
    400 					    (sc->sc_le_arg[i][j]);
    401 
    402 				}
    403 			}
    404 #ifdef TX39ICUDEBUG_PRINT_PENDING_INTERRUPT
    405 			pend &= ~reg;
    406 			if (pend) {
    407 				printf("%d pending:", i);
    408 				__bitdisp(pend, 0, 31, 0, 1);
    409 			}
    410 #endif
    411 
    412 		}
    413 	}
    414 #ifdef TX39_WATCHDOGTIMER
    415 	{
    416 		extern int	tx39biu_intr(void *);
    417 		/* Bus error (If watch dog timer is enabled)*/
    418 		if (ipending & MIPS_INT_MASK_1) {
    419 			tx39biu_intr(0); /* Clear bus error */
    420 		}
    421 	}
    422 #endif
    423 #if 0
    424 	/* reset priority mask */
    425 	reg = tx_conf_read(tc, TX39_INTRENABLE6_REG);
    426 	reg = TX39_INTRENABLE6_PRIORITYMASK_SET(reg, 0xffff);
    427 	tx_conf_write(tc, TX39_INTRENABLE6_REG, reg);
    428 #endif
    429 
    430  softintr:
    431 	_splset((status & ~cause & MIPS_HARD_INT_MASK) | MIPS_SR_INT_IE);
    432 
    433 	softintr(ipending);
    434 }
    435 
    436 int
    437 tx39_irqhigh(int set, int bit)
    438 {
    439 	int i, n;
    440 
    441 	n = sizeof irqhigh_list / sizeof (struct irqhigh_list);
    442 	for (i = 0; i < n; i++) {
    443 		if (irqhigh_list[i].qh_set == set &&
    444 		    irqhigh_list[i].qh_bit == bit)
    445 			return (irqhigh_list[i].qh_pri);
    446 	}
    447 
    448 	return (0);
    449 }
    450 
    451 void
    452 tx39_irqhigh_intr(u_int32_t ipending, u_int32_t pc, u_int32_t status,
    453     u_int32_t cause)
    454 {
    455 	struct txintr_high_entry *he;
    456 	struct tx39icu_softc *sc;
    457 	struct clockframe cf;
    458 	tx_chipset_tag_t tc;
    459 	int i, pri, ofs, set;
    460 	txreg_t he_mask;
    461 
    462 	tc = tx_conf_get_tag();
    463 	sc = tc->tc_intrt;
    464 	pri = TX39_INTRSTATUS6_INTVECT(sc->sc_regs[0]);
    465 
    466 	if (pri == TX39_INTRPRI13_TIMER_PERIODIC) {
    467 		tx_conf_write(tc, TX39_INTRCLEAR5_REG,
    468 		    TX39_INTRSTATUS5_PERINT);
    469 		cf.pc = pc;
    470 		cf.sr = status;
    471 		hardclock(&cf);
    472 		intrcnt[HARDCLOCK]++;
    473 
    474 		return;
    475 	}
    476 
    477 	/* Handle all pending IRQHIGH interrupts */
    478 	for (i = pri; i > 0; i--) {
    479 		TAILQ_FOREACH(he, &sc->sc_he_head[i], he_link) {
    480 			set = he->he_set;
    481 			he_mask = he->he_mask;
    482 			if (he_mask & (sc->sc_regs[set])) {
    483 				ofs = TX39_INTRSTATUS_REG(set);
    484 				/* Clear interrupt */
    485 				tx_conf_write(tc, ofs, he_mask);
    486 #ifdef TX39_DEBUG
    487 				tx39intrvec = (set << 16) |
    488 				    (ffs(he_mask) - 1);
    489 				if (tx39debugflag) {
    490 					DPRINTF(("IRQHIGH: %d:%d\n",
    491 					    set, ffs(he_mask) - 1));
    492 				}
    493 #endif /* TX39_DEBUG */
    494 				/* Dispatch handler */
    495 				(*he->he_fun)(he->he_arg);
    496 			}
    497 		}
    498 	}
    499 }
    500 
    501 void
    502 tx39_intr_decode(int intr, int *set, int *bit)
    503 {
    504 	if (!intr || intr >= (TX39_INTRSET_MAX + 1) * 32
    505 #ifdef TX392X
    506 	    || intr == 6
    507 #endif /* TX392X */
    508 	    ) {
    509 		panic("tx39icu_decode: bogus intrrupt line. %d", intr);
    510 	}
    511 	*set = intr / 32;
    512 	*bit = intr % 32;
    513 }
    514 
    515 void
    516 tx39_irqhigh_establish(tx_chipset_tag_t tc, int set, int bit, int pri,
    517     int (*ih_fun)(void *), void *ih_arg)
    518 {
    519 	struct tx39icu_softc *sc;
    520 	struct txintr_high_entry *he;
    521 	txreg_t reg;
    522 
    523 	sc = tc->tc_intrt;
    524 	/*
    525 	 *	Add new entry to `pri' priority
    526 	 */
    527 	if (!(he = malloc(sizeof(struct txintr_high_entry),
    528 	    M_DEVBUF, M_NOWAIT))) {
    529 		panic ("tx39_irqhigh_establish: no memory.");
    530 	}
    531 	memset(he, 0, sizeof(struct txintr_high_entry));
    532 	he->he_set = set;
    533 	he->he_mask= (1 << bit);
    534 	he->he_fun = ih_fun;
    535 	he->he_arg = ih_arg;
    536 	TAILQ_INSERT_TAIL(&sc->sc_he_head[pri], he, he_link);
    537 	/*
    538 	 *	Enable interrupt on this priority.
    539 	 */
    540 	reg = tx_conf_read(tc, TX39_INTRENABLE6_REG);
    541 	reg = TX39_INTRENABLE6_PRIORITYMASK_SET(reg, (1 << pri));
    542 	tx_conf_write(tc, TX39_INTRENABLE6_REG, reg);
    543 }
    544 
    545 void
    546 tx39_irqhigh_disestablish(tx_chipset_tag_t tc, int set, int bit, int pri)
    547 {
    548 	struct tx39icu_softc *sc;
    549 	struct txintr_high_entry *he;
    550 	txreg_t reg;
    551 
    552 	sc = tc->tc_intrt;
    553 	TAILQ_FOREACH(he, &sc->sc_he_head[pri], he_link) {
    554 		if (he->he_set == set && he->he_mask == (1 << bit)) {
    555 			TAILQ_REMOVE(&sc->sc_he_head[pri], he, he_link);
    556 			free(he, M_DEVBUF);
    557 			break;
    558 		}
    559 	}
    560 
    561 	if (TAILQ_EMPTY(&sc->sc_he_head[pri])) {
    562 		reg = tx_conf_read(tc, TX39_INTRENABLE6_REG);
    563 		reg &= ~(1 << pri);
    564 		tx_conf_write(tc, TX39_INTRENABLE6_REG, reg);
    565 	}
    566 }
    567 
    568 
    569 void *
    570 tx_intr_establish(tx_chipset_tag_t tc, int line, int mode, int level,
    571     int (*ih_fun)(void *), void *ih_arg)
    572 {
    573 	struct tx39icu_softc *sc;
    574 	txreg_t reg;
    575 	int bit, set, highpri, ofs;
    576 
    577 	sc = tc->tc_intrt;
    578 
    579 	tx39_intr_decode(line, &set, &bit);
    580 
    581 	sc->sc_le_fun[set][bit] = ih_fun;
    582 	sc->sc_le_arg[set][bit] = ih_arg;
    583 	DPRINTF(("tx_intr_establish: %d:%d", set, bit));
    584 
    585 	if ((highpri = tx39_irqhigh(set, bit))) {
    586 		tx39_irqhigh_establish(tc, set, bit, highpri,
    587 		    ih_fun, ih_arg);
    588 		DPRINTF(("(high)\n"));
    589 	} else {
    590 		/* Set mask for acknowledge. */
    591 		sc->sc_le_mask[set] |= (1 << bit);
    592 		/* Enable interrupt */
    593 		ofs = TX39_INTRENABLE_REG(set);
    594 		reg = tx_conf_read(tc, ofs);
    595 		reg |= (1 << bit);
    596 		tx_conf_write(tc, ofs, reg);
    597 		DPRINTF(("(low)\n"));
    598 	}
    599 
    600 	return ((void *)line);
    601 }
    602 
    603 void
    604 tx_intr_disestablish(tx_chipset_tag_t tc, void *arg)
    605 {
    606 	struct tx39icu_softc *sc;
    607 	int set, bit, highpri, ofs;
    608 	txreg_t reg;
    609 
    610 	sc = tc->tc_intrt;
    611 
    612 	tx39_intr_decode((int)arg, &set, &bit);
    613 	DPRINTF(("tx_intr_disestablish: %d:%d", set, bit));
    614 
    615 	if ((highpri = tx39_irqhigh(set, bit))) {
    616 		tx39_irqhigh_disestablish(tc, set, bit, highpri);
    617 		DPRINTF(("(high)\n"));
    618 	} else {
    619 		sc->sc_le_fun[set][bit] = 0;
    620 		sc->sc_le_arg[set][bit] = 0;
    621 		sc->sc_le_mask[set] &= ~(1 << bit);
    622 		ofs = TX39_INTRENABLE_REG(set);
    623 		reg = tx_conf_read(tc, ofs);
    624 		reg &= ~(1 << bit);
    625 		tx_conf_write(tc, ofs, reg);
    626 		DPRINTF(("(low)\n"));
    627 	}
    628 }
    629 
    630 u_int32_t
    631 tx_intr_status(tx_chipset_tag_t tc, int r)
    632 {
    633 	struct tx39icu_softc *sc = tc->tc_intrt;
    634 
    635 	if (r < 0 || r >= TX39_INTRSET_MAX + 1)
    636 		panic("tx_intr_status: invalid index %d", r);
    637 
    638 	return (u_int32_t)(sc->sc_regs[r]);
    639 }
    640 
    641 #ifdef USE_POLL
    642 void *
    643 tx39_poll_establish(tx_chipset_tag_t tc, int interval, int level,
    644     int (*ih_fun)(void *), void *ih_arg)
    645 {
    646 	struct tx39icu_softc *sc;
    647 	struct txpoll_entry *p;
    648 	int s;
    649 	void *ret;
    650 
    651 	s = splhigh();
    652 	sc = tc->tc_intrt;
    653 
    654 	if (!(p = malloc(sizeof(struct txpoll_entry),
    655 	    M_DEVBUF, M_NOWAIT))) {
    656 		panic ("tx39_poll_establish: no memory.");
    657 	}
    658 	memset(p, 0, sizeof(struct txpoll_entry));
    659 
    660 	p->p_fun = ih_fun;
    661 	p->p_arg = ih_arg;
    662 	p->p_cnt = interval;
    663 
    664 	if (!sc->sc_polling) {
    665 		tx39clock_alarm_set(tc, 33); /* 33 msec */
    666 
    667 		if (!(sc->sc_poll_ih =
    668 		    tx_intr_establish(
    669 			    tc, MAKEINTR(5, TX39_INTRSTATUS5_ALARMINT),
    670 			    IST_EDGE, level, tx39_poll_intr, sc)))  {
    671 			printf("tx39_poll_establish: can't hook\n");
    672 
    673 			splx(s);
    674 			return (0);
    675 		}
    676 	}
    677 
    678 	sc->sc_polling++;
    679 	p->p_desc = sc->sc_polling;
    680 	TAILQ_INSERT_TAIL(&sc->sc_p_head, p, p_link);
    681 	ret = (void *)p->p_desc;
    682 
    683 	splx(s);
    684 	return (ret);
    685 }
    686 
    687 void
    688 tx39_poll_disestablish(tx_chipset_tag_t tc, void *arg)
    689 {
    690 	struct tx39icu_softc *sc;
    691 	struct txpoll_entry *p;
    692 	int s, desc;
    693 
    694 	s = splhigh();
    695 	sc = tc->tc_intrt;
    696 
    697 	desc = (int)arg;
    698 	TAILQ_FOREACH(p, &sc->sc_p_head, p_link) {
    699 		if (p->p_desc == desc) {
    700 			TAILQ_REMOVE(&sc->sc_p_head, p, p_link);
    701 			free(p, M_DEVBUF);
    702 			break;
    703 		}
    704 	}
    705 
    706 	if (TAILQ_EMPTY(&sc->sc_p_head)) {
    707 		sc->sc_polling = 0;
    708 		tx_intr_disestablish(tc, sc->sc_poll_ih);
    709 	}
    710 
    711 	splx(s);
    712 	return;
    713 }
    714 
    715 int
    716 tx39_poll_intr(void *arg)
    717 {
    718 	struct tx39icu_softc *sc = arg;
    719 	struct txpoll_entry *p;
    720 
    721 	tx39clock_alarm_refill(sc->sc_tc);
    722 
    723 	if (!sc->sc_polling) {
    724 		return (0);
    725 	}
    726 	sc->sc_pollcnt++;
    727 	TAILQ_FOREACH(p, &sc->sc_p_head, p_link) {
    728 		if (sc->sc_pollcnt % p->p_cnt == 0) {
    729 			if ((*p->p_fun)(p->p_arg) == POLL_END)
    730 				goto disestablish;
    731 		}
    732 	}
    733 
    734 	return (0);
    735 
    736  disestablish:
    737 	TAILQ_REMOVE(&sc->sc_p_head, p, p_link);
    738 	free(p, M_DEVBUF);
    739 	if (TAILQ_EMPTY(&sc->sc_p_head)) {
    740 		sc->sc_polling = 0;
    741 		tx_intr_disestablish(sc->sc_tc, sc->sc_poll_ih);
    742 	}
    743 
    744 	return (0);
    745 }
    746 #endif /* USE_POLL */
    747 
    748 void
    749 tx39_intr_dump(struct tx39icu_softc *sc)
    750 {
    751 	tx_chipset_tag_t tc = sc->sc_tc;
    752 	int i, j, ofs;
    753 	txreg_t reg;
    754 	char msg[16];
    755 
    756 	for (i = 1; i <= TX39_INTRSET_MAX; i++) {
    757 #ifdef TX392X
    758 		if (i == 6)
    759 			continue;
    760 #endif /* TX392X */
    761 		for (reg = j = 0; j < 32; j++) {
    762 			if (tx39_irqhigh(i, j)) {
    763 				reg |= (1 << j);
    764 			}
    765 		}
    766 		sprintf(msg, "%d high", i);
    767 		__bitdisp(reg, 32, 0, msg, 1);
    768 		sprintf(msg, "%d status", i);
    769 		__bitdisp(sc->sc_regs[i], 0, 31, msg, 1);
    770 		ofs = TX39_INTRENABLE_REG(i);
    771 		reg = tx_conf_read(tc, ofs);
    772 		sprintf(msg, "%d enable", i);
    773 		__bitdisp(reg, 0, 31, msg, 1);
    774 	}
    775 	reg = sc->sc_regs[0];
    776 	printf("<%s><%s> vector=%2d\t\t[6 status]\n",
    777 	    reg & TX39_INTRSTATUS6_IRQHIGH ? "HI" : "--",
    778 	    reg & TX39_INTRSTATUS6_IRQLOW ? "LO" : "--",
    779 	    TX39_INTRSTATUS6_INTVECT(reg));
    780 	reg = tx_conf_read(tc, TX39_INTRENABLE6_REG);
    781 	__bitdisp(reg, 0, 18, "6 enable", 1);
    782 
    783 }
    784