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