Home | History | Annotate | Line # | Download | only in vr
vrgiu.c revision 1.22
      1 /*	$NetBSD: vrgiu.c,v 1.22 2001/05/06 14:25:16 takemura Exp $	*/
      2 /*-
      3  * Copyright (c) 1999-2001
      4  *         Shin Takemura and PocketBSD Project. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the PocketBSD project
     17  *	and its contributors.
     18  * 4. Neither the name of the project nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 #include <sys/malloc.h>
     40 #include <sys/queue.h>
     41 #include <sys/reboot.h>
     42 
     43 #define	TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
     44 
     45 #include <mips/cpuregs.h>
     46 #include <machine/bus.h>
     47 #include <machine/config_hook.h>
     48 
     49 #include <dev/hpc/hpciovar.h>
     50 
     51 #include "opt_vr41xx.h"
     52 #include <hpcmips/vr/vrcpudef.h>
     53 #include <hpcmips/vr/vripreg.h>
     54 #include <hpcmips/vr/vripvar.h>
     55 #include <hpcmips/vr/vrgiureg.h>
     56 #include <hpcmips/vr/vrgiuvar.h>
     57 
     58 #include "locators.h"
     59 
     60 #define VRGIUDEBUG
     61 #ifdef VRGIUDEBUG
     62 #define DEBUG_IO	1
     63 #define DEBUG_INTR	2
     64 #ifndef VRGIUDEBUG_CONF
     65 #define VRGIUDEBUG_CONF 0
     66 #endif /* VRGIUDEBUG_CONF */
     67 int	vrgiu_debug = VRGIUDEBUG_CONF;
     68 #define	DPRINTF(flag, arg) if (vrgiu_debug & flag) printf arg;
     69 #define DDUMP_IO(flag, sc) if (vrgiu_debug & flag) vrgiu_dump_io(sc);
     70 #define DDUMP_IOSETTING(flag, sc) \
     71 		if (vrgiu_debug & flag) vrgiu_dump_iosetting(sc);
     72 #define	VPRINTF(flag, arg) \
     73 		if (bootverbose || vrgiu_debug & flag) printf arg;
     74 #define VDUMP_IO(flag, sc) \
     75 		if (bootverbose || vrgiu_debug & flag) vrgiu_dump_io(sc);
     76 #define VDUMP_IOSETTING(flag, sc) \
     77 		if (bootverbose || vrgiu_debug & flag) vrgiu_dump_iosetting(sc);
     78 #else
     79 #define	DPRINTF(flag, arg)
     80 #define DDUMP_IO(flag, sc)
     81 #define DDUMP_IOSETTING(flag, sc)
     82 #define	VPRINTF(flag, arg) if (bootverbose) printf arg;
     83 #define VDUMP_IO(flag, sc) if (bootverbose) vrgiu_dump_io(sc);
     84 #define VDUMP_IOSETTING(flag, sc) \
     85 			if (bootverbose) vrgiu_dump_iosetting(sc);
     86 #endif
     87 
     88 #ifdef VRGIU_INTR_NOLED
     89 int vrgiu_intr_led = 0;
     90 #else /* VRGIU_INTR_NOLED */
     91 int vrgiu_intr_led = 1;
     92 #endif /* VRGIU_INTR_NOLED */
     93 
     94 #define	LEGAL_INTR_PORT(x)	((x) >= 0 && (x) < MAX_GPIO_INOUT)
     95 #define	LEGAL_OUT_PORT(x)	((x) >= 0 && (x) < MAX_GPIO_OUT)
     96 
     97 int vrgiu_match(struct device*, struct cfdata*, void*);
     98 void vrgiu_attach(struct device*, struct device*, void*);
     99 int vrgiu_intr(void*);
    100 int vrgiu_print(void*, const char*);
    101 void vrgiu_callback(struct device*);
    102 
    103 void	vrgiu_dump_regs(struct vrgiu_softc *);
    104 void	vrgiu_dump_io(struct vrgiu_softc *);
    105 void	vrgiu_diff_io(void);
    106 void	vrgiu_dump_iosetting(struct vrgiu_softc *);
    107 void	vrgiu_diff_iosetting(void);
    108 u_int32_t vrgiu_regread_4(struct vrgiu_softc *, bus_addr_t);
    109 u_int16_t vrgiu_regread(struct vrgiu_softc *, bus_addr_t);
    110 void	vrgiu_regwrite_4(struct vrgiu_softc *, bus_addr_t, u_int32_t);
    111 void	vrgiu_regwrite(struct vrgiu_softc *, bus_addr_t, u_int16_t);
    112 
    113 static int vrgiu_port_read(hpcio_chip_t, int);
    114 static void vrgiu_port_write(hpcio_chip_t, int, int);
    115 static void *vrgiu_intr_establish(hpcio_chip_t, int, int, int (*)(void *), void*);
    116 static void vrgiu_intr_disestablish(hpcio_chip_t, void*);
    117 static void vrgiu_intr_clear(hpcio_chip_t, void*);
    118 static void vrgiu_register_iochip(hpcio_chip_t, hpcio_chip_t);
    119 static void vrgiu_update(hpcio_chip_t);
    120 static void vrgiu_dump(hpcio_chip_t);
    121 static hpcio_chip_t vrgiu_getchip(void*, int);
    122 
    123 static struct hpcio_chip vrgiu_iochip = {
    124 	.hc_portread =		vrgiu_port_read,
    125 	.hc_portwrite =		vrgiu_port_write,
    126 	.hc_intr_establish =	vrgiu_intr_establish,
    127 	.hc_intr_disestablish =	vrgiu_intr_disestablish,
    128 	.hc_intr_clear =	vrgiu_intr_clear,
    129 	.hc_register_iochip =	vrgiu_register_iochip,
    130 	.hc_update =		vrgiu_update,
    131 	.hc_dump =		vrgiu_dump,
    132 };
    133 
    134 struct cfattach vrgiu_ca = {
    135 	sizeof(struct vrgiu_softc), vrgiu_match, vrgiu_attach
    136 };
    137 
    138 struct vrgiu_softc *this_giu;
    139 
    140 int
    141 vrgiu_match(parent, cf, aux)
    142 	struct device *parent;
    143 	struct cfdata *cf;
    144 	void *aux;
    145 {
    146 	return 2; /* 1st attach group of vrip */
    147 }
    148 
    149 void
    150 vrgiu_attach(parent, self, aux)
    151 	struct device *parent;
    152 	struct device *self;
    153 	void *aux;
    154 {
    155 	struct vrip_attach_args *va = aux;
    156 	struct vrgiu_softc *sc = (void*)self;
    157 	struct hpcio_attach_args haa;
    158 	int i;
    159 
    160 	this_giu = sc;
    161 	sc->sc_vc = va->va_vc;
    162 	sc->sc_iot = va->va_iot;
    163 	bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
    164 		      0 /* no cache */, &sc->sc_ioh);
    165 	/*
    166 	 *  Disable all interrupts.
    167 	 */
    168 	sc->sc_intr_mask = 0;
    169 	printf("\n");
    170 #ifdef WINCE_DEFAULT_SETTING
    171 #warning WINCE_DEFAULT_SETTING
    172 #else
    173 	VPRINTF(DEBUG_IO, ("WIN setting:                                "));
    174 	VDUMP_IOSETTING(DEBUG_IO, sc);
    175 	VPRINTF(DEBUG_IO, ("\n"));
    176 	vrgiu_regwrite_4(sc, GIUINTEN_REG, sc->sc_intr_mask);
    177 #endif
    178 
    179 	for (i = 0; i < MAX_GPIO_INOUT; i++)
    180 		TAILQ_INIT(&sc->sc_intr_head[i]);
    181 	if (!(sc->sc_ih = vrip_intr_establish(va->va_vc, va->va_intr, IPL_BIO,
    182 					      vrgiu_intr, sc))) {
    183 		printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
    184 		return;
    185 	}
    186 	/*
    187 	 * fill hpcio_chip structure
    188 	 */
    189 	sc->sc_iochip = vrgiu_iochip; /* structure copy */
    190 	sc->sc_iochip.hc_chipid = VRIP_IOCHIP_VRGIU;
    191 	sc->sc_iochip.hc_name = sc->sc_dev.dv_xname;
    192 	sc->sc_iochip.hc_sc = sc;
    193 	/* Register functions to upper interface */
    194 	vrip_gpio_register(va->va_vc, &sc->sc_iochip);
    195 
    196 	/* Display port status (Input/Output) for debugging */
    197 	VPRINTF(DEBUG_IO, ("I/O setting:                                "));
    198 	VDUMP_IOSETTING(DEBUG_IO, sc);
    199 	VPRINTF(DEBUG_IO, ("\n"));
    200 	VPRINTF(DEBUG_IO, ("       data:"));
    201 	VDUMP_IO(DEBUG_IO, sc);
    202 
    203 	/*
    204 	 *  hpcio I/F
    205 	 */
    206 	haa.haa_busname = HPCIO_BUSNAME;
    207 	haa.haa_sc = sc;
    208 	haa.haa_getchip = vrgiu_getchip;
    209 	haa.haa_iot = sc->sc_iot;
    210 	while (config_found(self, &haa, vrgiu_print)) ;
    211 	/*
    212 	 * GIU-ISA bridge
    213 	 */
    214 #if 1 /* XXX Sometimes mounting root device failed. Why? XXX*/
    215 	config_defer(self, vrgiu_callback);
    216 #else
    217 	vrgiu_callback(self);
    218 #endif
    219 }
    220 
    221 void
    222 vrgiu_callback(self)
    223 	struct device *self;
    224 {
    225 	struct vrgiu_softc *sc = (void*)self;
    226 	struct hpcio_attach_args haa;
    227 
    228 	haa.haa_busname = "vrisab";
    229 	haa.haa_sc = sc;
    230 	haa.haa_getchip = vrgiu_getchip;
    231 	haa.haa_iot = sc->sc_iot;
    232 	config_found(self, &haa, vrgiu_print);
    233 }
    234 
    235 int
    236 vrgiu_print(aux, pnp)
    237 	void *aux;
    238 	const char *pnp;
    239 {
    240 	if (pnp)
    241 		return (QUIET);
    242 	return (UNCONF);
    243 }
    244 
    245 void
    246 vrgiu_dump_iosetting(sc)
    247 	struct vrgiu_softc *sc;
    248 {
    249 	long iosel, inten, useupdn, termupdn;
    250 	u_int32_t m;
    251 	iosel= vrgiu_regread_4(sc, GIUIOSEL_REG);
    252 	inten= vrgiu_regread_4(sc, GIUINTEN_REG);
    253 	useupdn = vrgiu_regread(sc, GIUUSEUPDN_REG_W);
    254 	termupdn = vrgiu_regread(sc, GIUTERMUPDN_REG_W);
    255 	for (m = 0x80000000; m; m >>=1)
    256 		printf ("%c" , (useupdn&m) ?
    257 			((termupdn&m) ? 'U' : 'D') :
    258 			((iosel&m) ? 'o' : ((inten&m)?'I':'i')));
    259 }
    260 
    261 void
    262 vrgiu_diff_iosetting()
    263 {
    264 	struct vrgiu_softc *sc = this_giu;
    265 	static long oiosel = 0, ointen = 0, ouseupdn = 0, otermupdn = 0;
    266 	long iosel, inten, useupdn, termupdn;
    267 	u_int32_t m;
    268 
    269 	iosel= vrgiu_regread_4(sc, GIUIOSEL_REG);
    270 	inten= vrgiu_regread_4(sc, GIUINTEN_REG);
    271 	useupdn = vrgiu_regread(sc, GIUUSEUPDN_REG_W);
    272 	termupdn = vrgiu_regread(sc, GIUTERMUPDN_REG_W);
    273 	if (oiosel != iosel || ointen != inten ||
    274 	    ouseupdn != useupdn || otermupdn != termupdn) {
    275 		for (m = 0x80000000; m; m >>=1)
    276 			printf ("%c" , (useupdn&m) ?
    277 				((termupdn&m) ? 'U' : 'D') :
    278 				((iosel&m) ? 'o' : ((inten&m)?'I':'i')));
    279 	}
    280 	oiosel = iosel;
    281 	ointen = inten;
    282 	ouseupdn = useupdn;
    283 	otermupdn = termupdn;
    284 }
    285 
    286 void
    287 vrgiu_dump_io(sc)
    288 	struct vrgiu_softc *sc;
    289 {
    290 	u_int32_t preg[2];
    291 
    292 	preg[0] = vrgiu_regread_4(sc, GIUPIOD_REG);
    293 	preg[1] = vrgiu_regread_4(sc, GIUPODAT_REG);
    294 
    295 	bitdisp64(preg);
    296 }
    297 
    298 void
    299 vrgiu_diff_io()
    300 {
    301 	struct vrgiu_softc *sc  = this_giu;
    302 	static u_int32_t opreg[2] = {0, 0};
    303 	u_int32_t preg[2];
    304 
    305 	preg[0] = vrgiu_regread_4(sc, GIUPIOD_REG);
    306 	preg[1] = vrgiu_regread_4(sc, GIUPODAT_REG);
    307 
    308 	if (opreg[0] != preg[0] || opreg[1] != preg[1]) {
    309 		printf("giu data: ");
    310 		bitdisp64(preg);
    311 	}
    312 	opreg[0] = preg[0];
    313 	opreg[1] = preg[1];
    314 }
    315 
    316 void
    317 vrgiu_dump_regs(sc)
    318 	struct vrgiu_softc *sc;
    319 {
    320 	if (sc == NULL) {
    321 		panic("%s(%d): VRGIU device not initialized\n",
    322 		      __FILE__, __LINE__);
    323 	}
    324 	printf("    IOSEL: %08x\n", vrgiu_regread_4(sc, GIUIOSEL_REG));
    325 	printf("     PIOD: %08x\n", vrgiu_regread_4(sc, GIUPIOD_REG));
    326 	printf("    PODAT: %08x\n", vrgiu_regread_4(sc, GIUPODAT_REG));
    327 	printf("  INTSTAT: %08x\n", vrgiu_regread_4(sc, GIUINTSTAT_REG));
    328 	printf("    INTEN: %08x\n", vrgiu_regread_4(sc, GIUINTEN_REG));
    329 	printf("   INTTYP: %08x\n", vrgiu_regread_4(sc, GIUINTTYP_REG));
    330 	printf(" INTALSEL: %08x\n", vrgiu_regread_4(sc, GIUINTALSEL_REG));
    331 	printf(" INTHTSEL: %08x\n", vrgiu_regread_4(sc, GIUINTHTSEL_REG));
    332 }
    333 /*
    334  * GIU regster access method.
    335  */
    336 u_int32_t
    337 vrgiu_regread_4(sc, offs)
    338 	struct vrgiu_softc *sc;
    339 	bus_addr_t offs;
    340 {
    341 	u_int16_t reg[2];
    342 	bus_space_read_region_2 (sc->sc_iot, sc->sc_ioh, offs, reg, 2);
    343 	return reg[0]|(reg[1]<<16);
    344 }
    345 
    346 u_int16_t
    347 vrgiu_regread(sc, off)
    348 	struct vrgiu_softc *sc;
    349 	bus_addr_t off;
    350 {
    351 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, off);
    352 }
    353 
    354 void
    355 vrgiu_regwrite_4(sc, offs, data)
    356 	struct vrgiu_softc *sc;
    357 	bus_addr_t offs;
    358 	u_int32_t data;
    359 {
    360 	u_int16_t reg[2];
    361 	reg[0] = data & 0xffff;
    362 	reg[1] = (data>>16)&0xffff;
    363 	bus_space_write_region_2 (sc->sc_iot, sc->sc_ioh, offs, reg, 2);
    364 }
    365 
    366 void
    367 vrgiu_regwrite(sc, off, data)
    368 	struct vrgiu_softc *sc;
    369 	bus_addr_t off;
    370 	u_int16_t data;
    371 {
    372 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, off, data);
    373 }
    374 
    375 /*
    376  * PORT
    377  */
    378 int
    379 vrgiu_port_read(hc, port)
    380 	hpcio_chip_t hc;
    381 	int port;
    382 {
    383 	struct vrgiu_softc *sc = hc->hc_sc;
    384 	int on;
    385 
    386 	if (!LEGAL_OUT_PORT(port))
    387 		panic("vrgiu_port_read: illegal gpio port");
    388 
    389 	if (port < 32)
    390 		on = (vrgiu_regread_4(sc, GIUPIOD_REG) & (1 << port));
    391 	else
    392 		on = (vrgiu_regread_4(sc, GIUPODAT_REG) & (1 << (port - 32)));
    393 
    394 	return (on ? 1 : 0);
    395 }
    396 
    397 void
    398 vrgiu_port_write(hc, port, onoff)
    399 	hpcio_chip_t hc;
    400 	int port;
    401 	int onoff;
    402 {
    403 	struct vrgiu_softc *sc = hc->hc_sc;
    404 	u_int32_t reg[2];
    405 	int bank;
    406 
    407 	if (!LEGAL_OUT_PORT(port))
    408 		panic("vrgiu_port_write: illegal gpio port");
    409 
    410 	reg[0] = vrgiu_regread_4(sc, GIUPIOD_REG);
    411 	reg[1] = vrgiu_regread_4(sc, GIUPODAT_REG);
    412 	bank = port < 32 ? 0 : 1;
    413 	if (bank == 1)
    414 		port -= 32;
    415 
    416 	if (onoff)
    417 		reg[bank] |= (1<<port);
    418 	else
    419 		reg[bank] &= ~(1<<port);
    420 	vrgiu_regwrite_4(sc, GIUPIOD_REG, reg[0]);
    421 	vrgiu_regwrite_4(sc, GIUPODAT_REG, reg[1]);
    422 }
    423 
    424 static void
    425 vrgiu_update(hc)
    426 	hpcio_chip_t hc;
    427 {
    428 }
    429 
    430 static void
    431 vrgiu_dump(hc)
    432 	hpcio_chip_t hc;
    433 {
    434 }
    435 
    436 static hpcio_chip_t
    437 vrgiu_getchip(scx, chipid)
    438 	void* scx;
    439 	int chipid;
    440 {
    441 	struct vrgiu_softc *sc = scx;
    442 
    443 	return (&sc->sc_iochip);
    444 }
    445 
    446 /*
    447  *  For before autoconfiguration.
    448  */
    449 void
    450 __vrgiu_out(port, data)
    451 	int port;
    452 	int data;
    453 {
    454 	u_int16_t reg;
    455 	u_int32_t addr;
    456 	int offs;
    457 
    458 	if (!LEGAL_OUT_PORT(port))
    459 		panic("__vrgiu_out: illegal gpio port");
    460 	if (port < 16) {
    461 		addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPIOD_L_REG_W));
    462 		offs = port;
    463 	} else if (port < 32) {
    464 		addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPIOD_H_REG_W));
    465 		offs = port - 16;
    466 	} else if (port < 48) {
    467 		addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPODAT_L_REG_W));
    468 		offs = port - 32;
    469 	} else {
    470 		addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPODAT_H_REG_W));
    471 		offs = port - 48;
    472 		panic ("__vrgiu_out: not coded yet.");
    473 	}
    474 	DPRINTF(DEBUG_IO, ("__vrgiu_out: addr %08x bit %d\n", addr, offs));
    475 
    476 	wbflush();
    477 	reg = *((volatile u_int16_t*)addr);
    478 	if (data) {
    479 		reg |= (1 << offs);
    480 	} else {
    481 		reg &= ~(1 << offs);
    482 	}
    483 	*((volatile u_int16_t*)addr) = reg;
    484 	wbflush();
    485 }
    486 /*
    487  * Interrupt staff
    488  */
    489 void *
    490 vrgiu_intr_establish(hc, port, mode, ih_fun, ih_arg)
    491 	hpcio_chip_t hc;
    492 	int port; /* GPIO pin # */
    493 	int mode; /* GIU trigger setting */
    494 	int (*ih_fun) __P((void*));
    495 	void *ih_arg;
    496 {
    497 	struct vrgiu_softc *sc = hc->hc_sc;
    498 	int s;
    499 	u_int32_t reg, mask;
    500 	struct vrgiu_intr_entry *ih;
    501 
    502 	if (!LEGAL_INTR_PORT(port))
    503 		panic ("vrgiu_intr_establish: bogus interrupt line.");
    504 	if (sc->sc_intr_mode[port] && mode != sc->sc_intr_mode[port])
    505 		panic ("vrgiu_intr_establish: bogus interrupt type.");
    506 	else
    507 		sc->sc_intr_mode[port] = mode;
    508 	mask = (1 << port);
    509 
    510 	s = splhigh();
    511 
    512 	if (!(ih = malloc(sizeof(struct vrgiu_intr_entry), M_DEVBUF, M_NOWAIT)))
    513 		panic ("vrgiu_intr_establish: no memory.");
    514 
    515 	ih->ih_port = port;
    516 	ih->ih_fun = ih_fun;
    517 	ih->ih_arg = ih_arg;
    518 	TAILQ_INSERT_TAIL(&sc->sc_intr_head[port], ih, ih_link);
    519 #ifdef WINCE_DEFAULT_SETTING
    520 #warning WINCE_DEFAULT_SETTING
    521 #else
    522 	/*
    523 	 *  Setup registers
    524 	 */
    525 	/* Input mode */
    526 	reg = vrgiu_regread_4(sc, GIUIOSEL_REG);
    527 	reg &= ~mask;
    528 	vrgiu_regwrite_4(sc, GIUIOSEL_REG, reg);
    529 
    530 	/* interrupt type */
    531 	reg = vrgiu_regread_4(sc, GIUINTTYP_REG);
    532 	DPRINTF(DEBUG_INTR, ("[%s->",reg & mask ? "edge" : "level"));
    533 	if (mode & HPCIO_INTR_EDGE) {
    534 		DPRINTF(DEBUG_INTR, ("edge]"));
    535 		reg |= mask;	/* edge */
    536 	} else {
    537 		DPRINTF(DEBUG_INTR, ("level]"));
    538 		reg &= ~mask;	/* level */
    539 	}
    540 	vrgiu_regwrite_4(sc, GIUINTTYP_REG, reg);
    541 
    542 	/* interrupt level */
    543 	if (!(mode & HPCIO_INTR_EDGE)) {
    544 		reg = vrgiu_regread_4(sc, GIUINTALSEL_REG);
    545 		DPRINTF(DEBUG_INTR, ("[%s->",reg & mask ? "high" : "low"));
    546 		if (mode & HPCIO_INTR_HIGH) {
    547 			DPRINTF(DEBUG_INTR, ("high]"));
    548 			reg |= mask;	/* high */
    549 		} else {
    550 			DPRINTF(DEBUG_INTR, ("low]"));
    551 			reg &= ~mask;	/* low */
    552 		}
    553 		vrgiu_regwrite_4(sc, GIUINTALSEL_REG, reg);
    554 	}
    555 	/* hold or through */
    556 	reg = vrgiu_regread_4(sc, GIUINTHTSEL_REG);
    557 	DPRINTF(DEBUG_INTR, ("[%s->",reg & mask ? "hold" : "through"));
    558 	if (mode & HPCIO_INTR_HOLD) {
    559 		DPRINTF(DEBUG_INTR, ("hold]"));
    560 		reg |= mask;	/* hold */
    561 	} else {
    562 		DPRINTF(DEBUG_INTR, ("through]"));
    563 		reg &= ~mask;	/* through */
    564 	}
    565 	vrgiu_regwrite_4(sc, GIUINTHTSEL_REG, reg);
    566 #endif
    567 	/*
    568 	 *  clear interrupt status
    569 	 */
    570 	reg = vrgiu_regread_4(sc, GIUINTSTAT_REG);
    571 	reg &= ~mask;
    572 	vrgiu_regwrite_4(sc, GIUINTSTAT_REG, reg);
    573 	/*
    574 	 *  enable interrupt
    575 	 */
    576 #ifdef WINCE_DEFAULT_SETTING
    577 #warning WINCE_DEFAULT_SETTING
    578 #else
    579 	sc->sc_intr_mask |= mask;
    580 	vrgiu_regwrite_4(sc, GIUINTEN_REG, sc->sc_intr_mask);
    581 	/* Unmask GIU level 2 mask register */
    582 	vrip_intr_setmask2(sc->sc_vc, sc->sc_ih, (1<<port), 1);
    583 #endif
    584 	splx(s);
    585 
    586 	DPRINTF(DEBUG_INTR, ("\n"));
    587 
    588 	return ih;
    589 }
    590 
    591 void
    592 vrgiu_intr_disestablish(hc, arg)
    593 	hpcio_chip_t hc;
    594 	void *arg;
    595 {
    596 	struct vrgiu_intr_entry *ihe = arg;
    597 	struct vrgiu_softc *sc = hc->hc_sc;
    598 	int port = ihe->ih_port;
    599 	struct vrgiu_intr_entry *ih;
    600 	int s;
    601 
    602 	s = splhigh();
    603 	TAILQ_FOREACH(ih, &sc->sc_intr_head[port], ih_link) {
    604 		if (ih == ihe) {
    605 			TAILQ_REMOVE(&sc->sc_intr_head[port], ih, ih_link);
    606 			free(ih, M_DEVBUF);
    607 			if (TAILQ_EMPTY(&sc->sc_intr_head[port])) {
    608 				/* Disable interrupt */
    609 #ifdef WINCE_DEFAULT_SETTING
    610 #warning WINCE_DEFAULT_SETTING
    611 #else
    612 				sc->sc_intr_mask &= ~(1<<port);
    613 				vrgiu_regwrite_4(sc, GIUINTEN_REG, sc->sc_intr_mask);
    614 #endif
    615 			}
    616 			splx(s);
    617 			return;
    618 		}
    619 	}
    620 	panic("vrgiu_intr_disetablish: no such a handle.");
    621 	/* NOTREACHED */
    622 }
    623 
    624 /* Clear interrupt */
    625 void
    626 vrgiu_intr_clear(hc, arg)
    627 	hpcio_chip_t hc;
    628 	void *arg;
    629 {
    630 	struct vrgiu_softc *sc = hc->hc_sc;
    631 	struct vrgiu_intr_entry *ihe = arg;
    632 	u_int32_t reg;
    633 
    634 	reg = vrgiu_regread_4(sc, GIUINTSTAT_REG);
    635 	vrgiu_regwrite_4(sc, GIUINTSTAT_REG, reg & ~(1 << ihe->ih_port));
    636 }
    637 
    638 static void
    639 vrgiu_register_iochip(hc, iochip)
    640 	hpcio_chip_t hc;
    641 	hpcio_chip_t iochip;
    642 {
    643 	struct vrgiu_softc *sc = hc->hc_sc;
    644 
    645 	vrip_gpio_register(sc->sc_vc, iochip);
    646 }
    647 
    648 /* interrupt handler */
    649 int
    650 vrgiu_intr(arg)
    651 	void *arg;
    652 {
    653 #ifdef DUMP_GIU_LEVEL2_INTR
    654 #warning DUMP_GIU_LEVEL2_INTR
    655 	static u_int32_t oreg;
    656 #endif
    657 	struct vrgiu_softc *sc = arg;
    658 	int i;
    659 	u_int32_t reg;
    660 	int ledvalue = CONFIG_HOOK_LED_FLASH;
    661 
    662 	/* Get Level 2 interrupt status */
    663 	vrip_intr_get_status2 (sc->sc_vc, sc->sc_ih, &reg);
    664 #ifdef DUMP_GIU_LEVEL2_INTR
    665 #warning DUMP_GIU_LEVEL2_INTR
    666 	{
    667 		u_int32_t uedge, dedge, j;
    668 		for (j = 0x80000000; j > 0; j >>=1)
    669 			printf ("%c" , reg&j ? '|' : '.');
    670 		uedge = (reg ^ oreg) & reg;
    671 		dedge = (reg ^ oreg) & ~reg;
    672 		if (uedge || dedge) {
    673 			for (j = 0; j < 32; j++) {
    674 				if (uedge & (1 << j))
    675 					printf ("+%d", j);
    676 				else if (dedge & (1 << j))
    677 					printf ("-%d", j);
    678 			}
    679 		}
    680 		oreg = reg;
    681 		printf ("\n");
    682 	}
    683 #endif
    684 	/* Clear interrupt */
    685 	vrgiu_regwrite_4(sc, GIUINTSTAT_REG, vrgiu_regread_4(sc, GIUINTSTAT_REG));
    686 
    687 	/* Dispatch handler */
    688 	for (i = 0; i < MAX_GPIO_INOUT; i++) {
    689 		if (reg & (1 << i)) {
    690 			register struct vrgiu_intr_entry *ih;
    691 			TAILQ_FOREACH(ih, &sc->sc_intr_head[i], ih_link) {
    692 				ih->ih_fun(ih->ih_arg);
    693 			}
    694 		}
    695 	}
    696 
    697 	if (vrgiu_intr_led)
    698 		config_hook_call(CONFIG_HOOK_SET,
    699 				 CONFIG_HOOK_LED,
    700 				(void *)&ledvalue);
    701 	return 0;
    702 }
    703