Home | History | Annotate | Line # | Download | only in vr
vrgiu.c revision 1.31
      1  1.31       uch /*	$NetBSD: vrgiu.c,v 1.31 2002/01/29 18:53:22 uch Exp $	*/
      2   1.1  takemura /*-
      3  1.20  takemura  * Copyright (c) 1999-2001
      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.14      sato #include <sys/queue.h>
     41  1.15      sato #include <sys/reboot.h>
     42   1.1  takemura 
     43   1.1  takemura #include <mips/cpuregs.h>
     44   1.1  takemura #include <machine/bus.h>
     45  1.16      sato #include <machine/config_hook.h>
     46  1.31       uch #include <machine/debug.h>
     47   1.1  takemura 
     48  1.20  takemura #include <dev/hpc/hpciovar.h>
     49  1.20  takemura 
     50  1.18      sato #include "opt_vr41xx.h"
     51  1.18      sato #include <hpcmips/vr/vrcpudef.h>
     52  1.30  takemura #include <hpcmips/vr/vripif.h>
     53   1.1  takemura #include <hpcmips/vr/vripreg.h>
     54   1.1  takemura #include <hpcmips/vr/vrgiureg.h>
     55  1.20  takemura #include <hpcmips/vr/vrgiuvar.h>
     56   1.1  takemura 
     57   1.1  takemura #include "locators.h"
     58   1.1  takemura 
     59  1.27  takemura /*
     60  1.27  takemura  * constant and macro definitions
     61  1.27  takemura  */
     62   1.3  takemura #define VRGIUDEBUG
     63   1.1  takemura #ifdef VRGIUDEBUG
     64   1.3  takemura #define DEBUG_IO	1
     65   1.3  takemura #define DEBUG_INTR	2
     66   1.7      sato #ifndef VRGIUDEBUG_CONF
     67  1.10      sato #define VRGIUDEBUG_CONF 0
     68   1.7      sato #endif /* VRGIUDEBUG_CONF */
     69   1.7      sato int	vrgiu_debug = VRGIUDEBUG_CONF;
     70   1.3  takemura #define	DPRINTF(flag, arg) if (vrgiu_debug & flag) printf arg;
     71   1.9      sato #define DDUMP_IO(flag, sc) if (vrgiu_debug & flag) vrgiu_dump_io(sc);
     72   1.9      sato #define DDUMP_IOSETTING(flag, sc) \
     73  1.10      sato 		if (vrgiu_debug & flag) vrgiu_dump_iosetting(sc);
     74  1.10      sato #define	VPRINTF(flag, arg) \
     75  1.12  jdolecek 		if (bootverbose || vrgiu_debug & flag) printf arg;
     76  1.10      sato #define VDUMP_IO(flag, sc) \
     77  1.12  jdolecek 		if (bootverbose || vrgiu_debug & flag) vrgiu_dump_io(sc);
     78  1.10      sato #define VDUMP_IOSETTING(flag, sc) \
     79  1.12  jdolecek 		if (bootverbose || vrgiu_debug & flag) vrgiu_dump_iosetting(sc);
     80   1.1  takemura #else
     81   1.3  takemura #define	DPRINTF(flag, arg)
     82   1.9      sato #define DDUMP_IO(flag, sc)
     83   1.9      sato #define DDUMP_IOSETTING(flag, sc)
     84  1.12  jdolecek #define	VPRINTF(flag, arg) if (bootverbose) printf arg;
     85  1.12  jdolecek #define VDUMP_IO(flag, sc) if (bootverbose) vrgiu_dump_io(sc);
     86  1.10      sato #define VDUMP_IOSETTING(flag, sc) \
     87  1.12  jdolecek 			if (bootverbose) vrgiu_dump_iosetting(sc);
     88   1.1  takemura #endif
     89   1.1  takemura 
     90  1.16      sato #ifdef VRGIU_INTR_NOLED
     91  1.16      sato int vrgiu_intr_led = 0;
     92  1.16      sato #else /* VRGIU_INTR_NOLED */
     93  1.16      sato int vrgiu_intr_led = 1;
     94  1.16      sato #endif /* VRGIU_INTR_NOLED */
     95  1.16      sato 
     96  1.27  takemura #define MAX_GPIO_OUT 50    /* port 32:49 are output only port */
     97  1.27  takemura #define MAX_GPIO_INOUT 32  /* input/output port(0:31) */
     98  1.27  takemura 
     99   1.1  takemura #define	LEGAL_INTR_PORT(x)	((x) >= 0 && (x) < MAX_GPIO_INOUT)
    100   1.1  takemura #define	LEGAL_OUT_PORT(x)	((x) >= 0 && (x) < MAX_GPIO_OUT)
    101   1.1  takemura 
    102  1.27  takemura /* flags for variant chips */
    103  1.27  takemura #if !defined(VR4122) && !defined(VR4131)
    104  1.27  takemura #define VRGIU_HAVE_PULLUPDNREGS
    105  1.27  takemura #endif
    106  1.27  takemura 
    107  1.27  takemura /*
    108  1.27  takemura  * type declarations
    109  1.27  takemura  */
    110  1.27  takemura struct vrgiu_intr_entry {
    111  1.27  takemura 	int ih_port;
    112  1.27  takemura 	int (*ih_fun)(void *);
    113  1.27  takemura 	void *ih_arg;
    114  1.27  takemura 	TAILQ_ENTRY(vrgiu_intr_entry) ih_link;
    115  1.27  takemura };
    116  1.27  takemura 
    117  1.27  takemura struct vrgiu_softc {
    118  1.27  takemura 	struct	device sc_dev;
    119  1.27  takemura 	bus_space_tag_t sc_iot;
    120  1.27  takemura 	bus_space_handle_t sc_ioh;
    121  1.27  takemura 	/* Interrupt */
    122  1.27  takemura 	vrip_chipset_tag_t sc_vc;
    123  1.27  takemura 	void *sc_ih;
    124  1.27  takemura 	u_int32_t sc_intr_mask;
    125  1.27  takemura 	u_int32_t sc_intr_mode[MAX_GPIO_INOUT];
    126  1.27  takemura 	TAILQ_HEAD(, vrgiu_intr_entry) sc_intr_head[MAX_GPIO_INOUT];
    127  1.27  takemura 	struct hpcio_chip sc_iochip;
    128  1.27  takemura };
    129  1.27  takemura 
    130  1.27  takemura /*
    131  1.27  takemura  * prototypes
    132  1.27  takemura  */
    133  1.20  takemura int vrgiu_match(struct device*, struct cfdata*, void*);
    134  1.20  takemura void vrgiu_attach(struct device*, struct device*, void*);
    135  1.20  takemura int vrgiu_intr(void*);
    136  1.20  takemura int vrgiu_print(void*, const char*);
    137  1.20  takemura void vrgiu_callback(struct device*);
    138  1.20  takemura 
    139  1.20  takemura void	vrgiu_dump_regs(struct vrgiu_softc *);
    140  1.20  takemura void	vrgiu_dump_io(struct vrgiu_softc *);
    141  1.20  takemura void	vrgiu_diff_io(void);
    142  1.20  takemura void	vrgiu_dump_iosetting(struct vrgiu_softc *);
    143  1.20  takemura void	vrgiu_diff_iosetting(void);
    144  1.20  takemura u_int32_t vrgiu_regread_4(struct vrgiu_softc *, bus_addr_t);
    145  1.20  takemura u_int16_t vrgiu_regread(struct vrgiu_softc *, bus_addr_t);
    146  1.20  takemura void	vrgiu_regwrite_4(struct vrgiu_softc *, bus_addr_t, u_int32_t);
    147  1.20  takemura void	vrgiu_regwrite(struct vrgiu_softc *, bus_addr_t, u_int16_t);
    148  1.20  takemura 
    149  1.20  takemura static int vrgiu_port_read(hpcio_chip_t, int);
    150  1.20  takemura static void vrgiu_port_write(hpcio_chip_t, int, int);
    151  1.20  takemura static void *vrgiu_intr_establish(hpcio_chip_t, int, int, int (*)(void *), void*);
    152  1.20  takemura static void vrgiu_intr_disestablish(hpcio_chip_t, void*);
    153  1.20  takemura static void vrgiu_intr_clear(hpcio_chip_t, void*);
    154  1.22  takemura static void vrgiu_register_iochip(hpcio_chip_t, hpcio_chip_t);
    155  1.20  takemura static void vrgiu_update(hpcio_chip_t);
    156  1.20  takemura static void vrgiu_dump(hpcio_chip_t);
    157  1.20  takemura static hpcio_chip_t vrgiu_getchip(void*, int);
    158  1.20  takemura 
    159  1.27  takemura /*
    160  1.27  takemura  * variables
    161  1.27  takemura  */
    162  1.20  takemura static struct hpcio_chip vrgiu_iochip = {
    163  1.20  takemura 	.hc_portread =		vrgiu_port_read,
    164  1.20  takemura 	.hc_portwrite =		vrgiu_port_write,
    165  1.20  takemura 	.hc_intr_establish =	vrgiu_intr_establish,
    166  1.20  takemura 	.hc_intr_disestablish =	vrgiu_intr_disestablish,
    167  1.20  takemura 	.hc_intr_clear =	vrgiu_intr_clear,
    168  1.22  takemura 	.hc_register_iochip =	vrgiu_register_iochip,
    169  1.20  takemura 	.hc_update =		vrgiu_update,
    170  1.20  takemura 	.hc_dump =		vrgiu_dump,
    171   1.1  takemura };
    172   1.1  takemura 
    173   1.1  takemura struct cfattach vrgiu_ca = {
    174   1.1  takemura 	sizeof(struct vrgiu_softc), vrgiu_match, vrgiu_attach
    175   1.1  takemura };
    176   1.1  takemura 
    177  1.14      sato struct vrgiu_softc *this_giu;
    178  1.14      sato 
    179  1.27  takemura /*
    180  1.27  takemura  * function bodies
    181  1.27  takemura  */
    182   1.1  takemura int
    183  1.26       uch vrgiu_match(struct device *parent, struct cfdata *cf, void *aux)
    184   1.1  takemura {
    185  1.26       uch 
    186  1.26       uch 	return (2); /* 1st attach group of vrip */
    187   1.1  takemura }
    188   1.1  takemura 
    189   1.1  takemura void
    190  1.26       uch vrgiu_attach(struct device *parent, struct device *self, void *aux)
    191   1.1  takemura {
    192   1.1  takemura 	struct vrip_attach_args *va = aux;
    193   1.1  takemura 	struct vrgiu_softc *sc = (void*)self;
    194  1.20  takemura 	struct hpcio_attach_args haa;
    195   1.1  takemura 	int i;
    196   1.1  takemura 
    197  1.14      sato 	this_giu = sc;
    198   1.1  takemura 	sc->sc_vc = va->va_vc;
    199   1.1  takemura 	sc->sc_iot = va->va_iot;
    200   1.1  takemura 	bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
    201  1.26       uch 	    0 /* no cache */, &sc->sc_ioh);
    202   1.1  takemura 	/*
    203   1.1  takemura 	 *  Disable all interrupts.
    204   1.1  takemura 	 */
    205   1.1  takemura 	sc->sc_intr_mask = 0;
    206   1.8      shin 	printf("\n");
    207   1.1  takemura #ifdef WINCE_DEFAULT_SETTING
    208   1.1  takemura #warning WINCE_DEFAULT_SETTING
    209   1.1  takemura #else
    210  1.28  takemura 	VPRINTF(DEBUG_IO, ("                                            "
    211  1.28  takemura 			   " 3         2         1\n"));
    212  1.28  takemura 	VPRINTF(DEBUG_IO, ("                                            "
    213  1.28  takemura 			   "10987654321098765432109876543210\n"));
    214  1.10      sato 	VPRINTF(DEBUG_IO, ("WIN setting:                                "));
    215  1.10      sato 	VDUMP_IOSETTING(DEBUG_IO, sc);
    216  1.10      sato 	VPRINTF(DEBUG_IO, ("\n"));
    217   1.1  takemura 	vrgiu_regwrite_4(sc, GIUINTEN_REG, sc->sc_intr_mask);
    218   1.1  takemura #endif
    219   1.1  takemura 
    220   1.1  takemura 	for (i = 0; i < MAX_GPIO_INOUT; i++)
    221   1.1  takemura 		TAILQ_INIT(&sc->sc_intr_head[i]);
    222  1.30  takemura 	if (!(sc->sc_ih = vrip_intr_establish(va->va_vc, va->va_unit, 0,
    223  1.30  takemura 	    IPL_BIO, vrgiu_intr, sc))) {
    224   1.1  takemura 		printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
    225   1.1  takemura 		return;
    226   1.1  takemura 	}
    227   1.1  takemura 	/*
    228  1.20  takemura 	 * fill hpcio_chip structure
    229   1.1  takemura 	 */
    230  1.20  takemura 	sc->sc_iochip = vrgiu_iochip; /* structure copy */
    231  1.20  takemura 	sc->sc_iochip.hc_chipid = VRIP_IOCHIP_VRGIU;
    232  1.20  takemura 	sc->sc_iochip.hc_name = sc->sc_dev.dv_xname;
    233  1.20  takemura 	sc->sc_iochip.hc_sc = sc;
    234  1.20  takemura 	/* Register functions to upper interface */
    235  1.30  takemura 	vrip_register_gpio(va->va_vc, &sc->sc_iochip);
    236   1.9      sato 
    237   1.1  takemura 	/* Display port status (Input/Output) for debugging */
    238  1.10      sato 	VPRINTF(DEBUG_IO, ("I/O setting:                                "));
    239  1.19      sato 	VDUMP_IOSETTING(DEBUG_IO, sc);
    240  1.10      sato 	VPRINTF(DEBUG_IO, ("\n"));
    241  1.10      sato 	VPRINTF(DEBUG_IO, ("       data:"));
    242  1.10      sato 	VDUMP_IO(DEBUG_IO, sc);
    243   1.5      sato 
    244   1.1  takemura 	/*
    245  1.20  takemura 	 *  hpcio I/F
    246   1.1  takemura 	 */
    247  1.21  takemura 	haa.haa_busname = HPCIO_BUSNAME;
    248  1.20  takemura 	haa.haa_sc = sc;
    249  1.20  takemura 	haa.haa_getchip = vrgiu_getchip;
    250  1.22  takemura 	haa.haa_iot = sc->sc_iot;
    251  1.20  takemura 	while (config_found(self, &haa, vrgiu_print)) ;
    252   1.1  takemura 	/*
    253   1.1  takemura 	 * GIU-ISA bridge
    254   1.1  takemura 	 */
    255   1.1  takemura #if 1 /* XXX Sometimes mounting root device failed. Why? XXX*/
    256   1.1  takemura 	config_defer(self, vrgiu_callback);
    257   1.1  takemura #else
    258   1.1  takemura 	vrgiu_callback(self);
    259   1.1  takemura #endif
    260   1.1  takemura }
    261   1.1  takemura 
    262   1.1  takemura void
    263  1.26       uch vrgiu_callback(struct device *self)
    264   1.1  takemura {
    265   1.1  takemura 	struct vrgiu_softc *sc = (void*)self;
    266  1.20  takemura 	struct hpcio_attach_args haa;
    267   1.1  takemura 
    268  1.20  takemura 	haa.haa_busname = "vrisab";
    269  1.20  takemura 	haa.haa_sc = sc;
    270  1.20  takemura 	haa.haa_getchip = vrgiu_getchip;
    271  1.22  takemura 	haa.haa_iot = sc->sc_iot;
    272  1.20  takemura 	config_found(self, &haa, vrgiu_print);
    273   1.1  takemura }
    274   1.1  takemura 
    275   1.1  takemura int
    276  1.26       uch vrgiu_print(void *aux, const char *pnp)
    277   1.1  takemura {
    278   1.1  takemura 	if (pnp)
    279   1.1  takemura 		return (QUIET);
    280   1.1  takemura 	return (UNCONF);
    281   1.1  takemura }
    282   1.1  takemura 
    283   1.1  takemura void
    284  1.26       uch vrgiu_dump_iosetting(struct vrgiu_softc *sc)
    285   1.3  takemura {
    286  1.28  takemura 	long iosel, inten, useupdn, termupdn, edge, hold, level;
    287   1.3  takemura 	u_int32_t m;
    288  1.28  takemura 	char syms[] = "iiiiiiiilhLHeeEEoooooooooooooooo"
    289  1.28  takemura 		      "DDDDDDDDDDDDDDDDUUUUUUUUUUUUUUUU";
    290  1.26       uch 
    291   1.3  takemura 	iosel= vrgiu_regread_4(sc, GIUIOSEL_REG);
    292   1.3  takemura 	inten= vrgiu_regread_4(sc, GIUINTEN_REG);
    293  1.28  takemura 	edge = vrgiu_regread_4(sc, GIUINTTYP_REG);
    294  1.28  takemura 	hold = vrgiu_regread_4(sc, GIUINTHTSEL_REG);
    295  1.28  takemura 	level = vrgiu_regread_4(sc, GIUINTALSEL_REG);
    296  1.27  takemura #ifndef VRGIU_HAVE_PULLUPDNREGS
    297  1.24     enami 	useupdn = termupdn = 0;
    298  1.24     enami #else
    299   1.3  takemura 	useupdn = vrgiu_regread(sc, GIUUSEUPDN_REG_W);
    300   1.3  takemura 	termupdn = vrgiu_regread(sc, GIUTERMUPDN_REG_W);
    301  1.24     enami #endif
    302   1.3  takemura 	for (m = 0x80000000; m; m >>=1)
    303  1.28  takemura 		printf ("%c", syms[
    304  1.28  takemura 			((useupdn&m) ? 32 : 0) +
    305  1.28  takemura 			((iosel&m) ? 16 : 0) + ((termupdn&m) ? 16 : 0) +
    306  1.28  takemura 			((inten&m) ? 8 : 0) +
    307  1.28  takemura 			((edge&m) ? 4 : 0) +
    308  1.28  takemura 			((hold&m) ? 2 : 0) +
    309  1.28  takemura 			((level&m) ? 1 : 0)]);
    310   1.3  takemura }
    311   1.3  takemura 
    312   1.3  takemura void
    313  1.14      sato vrgiu_diff_iosetting()
    314  1.14      sato {
    315  1.14      sato 	struct vrgiu_softc *sc = this_giu;
    316  1.14      sato 	static long oiosel = 0, ointen = 0, ouseupdn = 0, otermupdn = 0;
    317  1.14      sato 	long iosel, inten, useupdn, termupdn;
    318  1.14      sato 	u_int32_t m;
    319  1.14      sato 
    320  1.14      sato 	iosel= vrgiu_regread_4(sc, GIUIOSEL_REG);
    321  1.14      sato 	inten= vrgiu_regread_4(sc, GIUINTEN_REG);
    322  1.27  takemura #ifndef VRGIU_HAVE_PULLUPDNREGS
    323  1.24     enami 	useupdn = termupdn = 0;
    324  1.24     enami #else
    325  1.14      sato 	useupdn = vrgiu_regread(sc, GIUUSEUPDN_REG_W);
    326  1.14      sato 	termupdn = vrgiu_regread(sc, GIUTERMUPDN_REG_W);
    327  1.24     enami #endif
    328  1.14      sato 	if (oiosel != iosel || ointen != inten ||
    329  1.14      sato 	    ouseupdn != useupdn || otermupdn != termupdn) {
    330  1.14      sato 		for (m = 0x80000000; m; m >>=1)
    331  1.14      sato 			printf ("%c" , (useupdn&m) ?
    332  1.26       uch 			    ((termupdn&m) ? 'U' : 'D') :
    333  1.26       uch 			    ((iosel&m) ? 'o' : ((inten&m)?'I':'i')));
    334  1.14      sato 	}
    335  1.14      sato 	oiosel = iosel;
    336  1.14      sato 	ointen = inten;
    337  1.14      sato 	ouseupdn = useupdn;
    338  1.14      sato 	otermupdn = termupdn;
    339  1.14      sato }
    340  1.14      sato 
    341  1.14      sato void
    342  1.26       uch vrgiu_dump_io(struct vrgiu_softc *sc)
    343   1.9      sato {
    344   1.9      sato 
    345  1.31       uch 	dbg_bit_print(vrgiu_regread_4(sc, GIUPIOD_REG));
    346  1.31       uch 	dbg_bit_print(vrgiu_regread_4(sc, GIUPODAT_REG));
    347  1.14      sato }
    348  1.14      sato 
    349  1.14      sato void
    350  1.14      sato vrgiu_diff_io()
    351  1.14      sato {
    352  1.14      sato 	struct vrgiu_softc *sc  = this_giu;
    353  1.14      sato 	static u_int32_t opreg[2] = {0, 0};
    354  1.14      sato 	u_int32_t preg[2];
    355  1.14      sato 
    356  1.14      sato 	preg[0] = vrgiu_regread_4(sc, GIUPIOD_REG);
    357  1.14      sato 	preg[1] = vrgiu_regread_4(sc, GIUPODAT_REG);
    358  1.14      sato 
    359  1.14      sato 	if (opreg[0] != preg[0] || opreg[1] != preg[1]) {
    360  1.14      sato 		printf("giu data: ");
    361  1.31       uch 		dbg_bit_print(preg[0]);
    362  1.31       uch 		dbg_bit_print(preg[1]);
    363  1.14      sato 	}
    364  1.14      sato 	opreg[0] = preg[0];
    365  1.14      sato 	opreg[1] = preg[1];
    366   1.9      sato }
    367   1.9      sato 
    368   1.9      sato void
    369  1.26       uch vrgiu_dump_regs(struct vrgiu_softc *sc)
    370   1.1  takemura {
    371  1.26       uch 
    372   1.1  takemura 	if (sc == NULL) {
    373   1.1  takemura 		panic("%s(%d): VRGIU device not initialized\n",
    374  1.26       uch 		    __FILE__, __LINE__);
    375   1.1  takemura 	}
    376   1.1  takemura 	printf("    IOSEL: %08x\n", vrgiu_regread_4(sc, GIUIOSEL_REG));
    377   1.1  takemura 	printf("     PIOD: %08x\n", vrgiu_regread_4(sc, GIUPIOD_REG));
    378   1.1  takemura 	printf("    PODAT: %08x\n", vrgiu_regread_4(sc, GIUPODAT_REG));
    379   1.1  takemura 	printf("  INTSTAT: %08x\n", vrgiu_regread_4(sc, GIUINTSTAT_REG));
    380   1.1  takemura 	printf("    INTEN: %08x\n", vrgiu_regread_4(sc, GIUINTEN_REG));
    381   1.1  takemura 	printf("   INTTYP: %08x\n", vrgiu_regread_4(sc, GIUINTTYP_REG));
    382   1.1  takemura 	printf(" INTALSEL: %08x\n", vrgiu_regread_4(sc, GIUINTALSEL_REG));
    383   1.1  takemura 	printf(" INTHTSEL: %08x\n", vrgiu_regread_4(sc, GIUINTHTSEL_REG));
    384   1.1  takemura }
    385   1.1  takemura /*
    386   1.1  takemura  * GIU regster access method.
    387   1.1  takemura  */
    388   1.1  takemura u_int32_t
    389  1.26       uch vrgiu_regread_4(struct vrgiu_softc *sc, bus_addr_t offs)
    390   1.1  takemura {
    391   1.1  takemura 	u_int16_t reg[2];
    392  1.26       uch 
    393   1.1  takemura 	bus_space_read_region_2 (sc->sc_iot, sc->sc_ioh, offs, reg, 2);
    394  1.26       uch 
    395  1.26       uch 	return (reg[0] | (reg[1] << 16));
    396   1.1  takemura }
    397   1.1  takemura 
    398   1.3  takemura u_int16_t
    399  1.26       uch vrgiu_regread(struct vrgiu_softc *sc, bus_addr_t off)
    400   1.3  takemura {
    401  1.26       uch 
    402  1.26       uch 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, off));
    403   1.3  takemura }
    404   1.3  takemura 
    405   1.1  takemura void
    406  1.26       uch vrgiu_regwrite_4(struct vrgiu_softc *sc, bus_addr_t offs, u_int32_t data)
    407   1.1  takemura {
    408   1.1  takemura 	u_int16_t reg[2];
    409  1.26       uch 
    410   1.1  takemura 	reg[0] = data & 0xffff;
    411   1.1  takemura 	reg[1] = (data>>16)&0xffff;
    412   1.1  takemura 	bus_space_write_region_2 (sc->sc_iot, sc->sc_ioh, offs, reg, 2);
    413   1.1  takemura }
    414   1.3  takemura 
    415   1.3  takemura void
    416  1.26       uch vrgiu_regwrite(struct vrgiu_softc *sc, bus_addr_t off, u_int16_t data)
    417   1.3  takemura {
    418  1.26       uch 
    419   1.3  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, off, data);
    420   1.3  takemura }
    421   1.4  takemura 
    422   1.1  takemura /*
    423   1.1  takemura  * PORT
    424   1.1  takemura  */
    425   1.1  takemura int
    426  1.26       uch vrgiu_port_read(hpcio_chip_t hc, int port)
    427   1.1  takemura {
    428  1.20  takemura 	struct vrgiu_softc *sc = hc->hc_sc;
    429   1.4  takemura 	int on;
    430   1.4  takemura 
    431   1.4  takemura 	if (!LEGAL_OUT_PORT(port))
    432   1.4  takemura 		panic("vrgiu_port_read: illegal gpio port");
    433   1.4  takemura 
    434   1.4  takemura 	if (port < 32)
    435  1.20  takemura 		on = (vrgiu_regread_4(sc, GIUPIOD_REG) & (1 << port));
    436   1.4  takemura 	else
    437  1.20  takemura 		on = (vrgiu_regread_4(sc, GIUPODAT_REG) & (1 << (port - 32)));
    438   1.4  takemura 
    439   1.4  takemura 	return (on ? 1 : 0);
    440   1.1  takemura }
    441   1.1  takemura 
    442  1.20  takemura void
    443  1.26       uch vrgiu_port_write(hpcio_chip_t hc, int port, int onoff)
    444   1.1  takemura {
    445  1.20  takemura 	struct vrgiu_softc *sc = hc->hc_sc;
    446   1.4  takemura 	u_int32_t reg[2];
    447   1.4  takemura 	int bank;
    448   1.1  takemura 
    449   1.1  takemura 	if (!LEGAL_OUT_PORT(port))
    450   1.1  takemura 		panic("vrgiu_port_write: illegal gpio port");
    451   1.1  takemura 
    452  1.20  takemura 	reg[0] = vrgiu_regread_4(sc, GIUPIOD_REG);
    453  1.20  takemura 	reg[1] = vrgiu_regread_4(sc, GIUPODAT_REG);
    454   1.1  takemura 	bank = port < 32 ? 0 : 1;
    455   1.1  takemura 	if (bank == 1)
    456   1.1  takemura 		port -= 32;
    457   1.1  takemura 
    458   1.1  takemura 	if (onoff)
    459   1.1  takemura 		reg[bank] |= (1<<port);
    460   1.1  takemura 	else
    461   1.1  takemura 		reg[bank] &= ~(1<<port);
    462  1.20  takemura 	vrgiu_regwrite_4(sc, GIUPIOD_REG, reg[0]);
    463  1.20  takemura 	vrgiu_regwrite_4(sc, GIUPODAT_REG, reg[1]);
    464  1.20  takemura }
    465  1.20  takemura 
    466  1.20  takemura static void
    467  1.26       uch vrgiu_update(hpcio_chip_t hc)
    468  1.20  takemura {
    469  1.20  takemura }
    470  1.20  takemura 
    471  1.20  takemura static void
    472  1.26       uch vrgiu_dump(hpcio_chip_t hc)
    473  1.20  takemura {
    474  1.20  takemura }
    475  1.20  takemura 
    476  1.20  takemura static hpcio_chip_t
    477  1.26       uch vrgiu_getchip(void* scx, int chipid)
    478  1.20  takemura {
    479  1.20  takemura 	struct vrgiu_softc *sc = scx;
    480   1.1  takemura 
    481  1.20  takemura 	return (&sc->sc_iochip);
    482   1.1  takemura }
    483  1.20  takemura 
    484   1.1  takemura /*
    485   1.1  takemura  *  For before autoconfiguration.
    486   1.1  takemura  */
    487   1.1  takemura void
    488  1.26       uch __vrgiu_out(int port, int data)
    489   1.1  takemura {
    490   1.1  takemura 	u_int16_t reg;
    491   1.1  takemura 	u_int32_t addr;
    492   1.1  takemura 	int offs;
    493   1.1  takemura 
    494   1.1  takemura 	if (!LEGAL_OUT_PORT(port))
    495   1.1  takemura 		panic("__vrgiu_out: illegal gpio port");
    496   1.1  takemura 	if (port < 16) {
    497   1.1  takemura 		addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPIOD_L_REG_W));
    498   1.1  takemura 		offs = port;
    499   1.1  takemura 	} else if (port < 32) {
    500   1.1  takemura 		addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPIOD_H_REG_W));
    501   1.1  takemura 		offs = port - 16;
    502   1.1  takemura 	} else if (port < 48) {
    503   1.1  takemura 		addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPODAT_L_REG_W));
    504   1.1  takemura 		offs = port - 32;
    505   1.1  takemura 	} else {
    506   1.1  takemura 		addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPODAT_H_REG_W));
    507   1.1  takemura 		offs = port - 48;
    508   1.1  takemura 		panic ("__vrgiu_out: not coded yet.");
    509   1.1  takemura 	}
    510   1.9      sato 	DPRINTF(DEBUG_IO, ("__vrgiu_out: addr %08x bit %d\n", addr, offs));
    511   1.1  takemura 
    512   1.1  takemura 	wbflush();
    513   1.1  takemura 	reg = *((volatile u_int16_t*)addr);
    514   1.1  takemura 	if (data) {
    515   1.1  takemura 		reg |= (1 << offs);
    516   1.1  takemura 	} else {
    517   1.1  takemura 		reg &= ~(1 << offs);
    518   1.1  takemura 	}
    519   1.1  takemura 	*((volatile u_int16_t*)addr) = reg;
    520   1.1  takemura 	wbflush();
    521   1.1  takemura }
    522   1.1  takemura /*
    523   1.1  takemura  * Interrupt staff
    524   1.1  takemura  */
    525   1.1  takemura void *
    526  1.26       uch vrgiu_intr_establish(
    527  1.26       uch 	hpcio_chip_t hc,
    528  1.26       uch 	int port, /* GPIO pin # */
    529  1.26       uch 	int mode, /* GIU trigger setting */
    530  1.26       uch 	int (*ih_fun)(void *),
    531  1.26       uch 	void *ih_arg)
    532   1.1  takemura {
    533  1.20  takemura 	struct vrgiu_softc *sc = hc->hc_sc;
    534   1.1  takemura 	int s;
    535   1.1  takemura 	u_int32_t reg, mask;
    536   1.1  takemura 	struct vrgiu_intr_entry *ih;
    537   1.1  takemura 
    538   1.1  takemura 	if (!LEGAL_INTR_PORT(port))
    539   1.1  takemura 		panic ("vrgiu_intr_establish: bogus interrupt line.");
    540   1.1  takemura 	if (sc->sc_intr_mode[port] && mode != sc->sc_intr_mode[port])
    541   1.1  takemura 		panic ("vrgiu_intr_establish: bogus interrupt type.");
    542   1.1  takemura 	else
    543   1.1  takemura 		sc->sc_intr_mode[port] = mode;
    544   1.1  takemura 	mask = (1 << port);
    545   1.1  takemura 
    546   1.1  takemura 	s = splhigh();
    547   1.1  takemura 
    548   1.1  takemura 	if (!(ih = malloc(sizeof(struct vrgiu_intr_entry), M_DEVBUF, M_NOWAIT)))
    549   1.1  takemura 		panic ("vrgiu_intr_establish: no memory.");
    550   1.1  takemura 
    551  1.28  takemura 	DPRINTF(DEBUG_INTR, ("%s: port %d ", sc->sc_dev.dv_xname, port));
    552   1.1  takemura 	ih->ih_port = port;
    553   1.1  takemura 	ih->ih_fun = ih_fun;
    554   1.1  takemura 	ih->ih_arg = ih_arg;
    555   1.1  takemura 	TAILQ_INSERT_TAIL(&sc->sc_intr_head[port], ih, ih_link);
    556   1.1  takemura #ifdef WINCE_DEFAULT_SETTING
    557   1.1  takemura #warning WINCE_DEFAULT_SETTING
    558   1.1  takemura #else
    559   1.1  takemura 	/*
    560   1.1  takemura 	 *  Setup registers
    561   1.1  takemura 	 */
    562   1.1  takemura 	/* Input mode */
    563   1.1  takemura 	reg = vrgiu_regread_4(sc, GIUIOSEL_REG);
    564   1.1  takemura 	reg &= ~mask;
    565   1.1  takemura 	vrgiu_regwrite_4(sc, GIUIOSEL_REG, reg);
    566   1.1  takemura 
    567   1.1  takemura 	/* interrupt type */
    568   1.1  takemura 	reg = vrgiu_regread_4(sc, GIUINTTYP_REG);
    569   1.3  takemura 	DPRINTF(DEBUG_INTR, ("[%s->",reg & mask ? "edge" : "level"));
    570  1.20  takemura 	if (mode & HPCIO_INTR_EDGE) {
    571   1.3  takemura 		DPRINTF(DEBUG_INTR, ("edge]"));
    572   1.1  takemura 		reg |= mask;	/* edge */
    573   1.1  takemura 	} else {
    574   1.3  takemura 		DPRINTF(DEBUG_INTR, ("level]"));
    575   1.1  takemura 		reg &= ~mask;	/* level */
    576   1.1  takemura 	}
    577   1.1  takemura 	vrgiu_regwrite_4(sc, GIUINTTYP_REG, reg);
    578   1.1  takemura 
    579   1.1  takemura 	/* interrupt level */
    580  1.20  takemura 	if (!(mode & HPCIO_INTR_EDGE)) {
    581   1.1  takemura 		reg = vrgiu_regread_4(sc, GIUINTALSEL_REG);
    582   1.3  takemura 		DPRINTF(DEBUG_INTR, ("[%s->",reg & mask ? "high" : "low"));
    583  1.20  takemura 		if (mode & HPCIO_INTR_HIGH) {
    584   1.3  takemura 			DPRINTF(DEBUG_INTR, ("high]"));
    585   1.1  takemura 			reg |= mask;	/* high */
    586   1.1  takemura 		} else {
    587   1.3  takemura 			DPRINTF(DEBUG_INTR, ("low]"));
    588   1.1  takemura 			reg &= ~mask;	/* low */
    589   1.1  takemura 		}
    590   1.1  takemura 		vrgiu_regwrite_4(sc, GIUINTALSEL_REG, reg);
    591   1.1  takemura 	}
    592   1.1  takemura 	/* hold or through */
    593   1.1  takemura 	reg = vrgiu_regread_4(sc, GIUINTHTSEL_REG);
    594   1.3  takemura 	DPRINTF(DEBUG_INTR, ("[%s->",reg & mask ? "hold" : "through"));
    595  1.20  takemura 	if (mode & HPCIO_INTR_HOLD) {
    596   1.3  takemura 		DPRINTF(DEBUG_INTR, ("hold]"));
    597   1.1  takemura 		reg |= mask;	/* hold */
    598   1.1  takemura 	} else {
    599   1.3  takemura 		DPRINTF(DEBUG_INTR, ("through]"));
    600   1.1  takemura 		reg &= ~mask;	/* through */
    601   1.1  takemura 	}
    602   1.1  takemura 	vrgiu_regwrite_4(sc, GIUINTHTSEL_REG, reg);
    603   1.1  takemura #endif
    604   1.1  takemura 	/*
    605   1.1  takemura 	 *  clear interrupt status
    606   1.1  takemura 	 */
    607   1.1  takemura 	reg = vrgiu_regread_4(sc, GIUINTSTAT_REG);
    608   1.1  takemura 	reg &= ~mask;
    609   1.1  takemura 	vrgiu_regwrite_4(sc, GIUINTSTAT_REG, reg);
    610   1.1  takemura 	/*
    611   1.1  takemura 	 *  enable interrupt
    612   1.1  takemura 	 */
    613   1.1  takemura #ifdef WINCE_DEFAULT_SETTING
    614   1.1  takemura #warning WINCE_DEFAULT_SETTING
    615   1.1  takemura #else
    616   1.1  takemura 	sc->sc_intr_mask |= mask;
    617   1.1  takemura 	vrgiu_regwrite_4(sc, GIUINTEN_REG, sc->sc_intr_mask);
    618   1.1  takemura 	/* Unmask GIU level 2 mask register */
    619   1.1  takemura 	vrip_intr_setmask2(sc->sc_vc, sc->sc_ih, (1<<port), 1);
    620   1.1  takemura #endif
    621   1.1  takemura 	splx(s);
    622   1.1  takemura 
    623   1.3  takemura 	DPRINTF(DEBUG_INTR, ("\n"));
    624   1.1  takemura 
    625  1.26       uch 	return (ih);
    626   1.1  takemura }
    627   1.1  takemura 
    628   1.1  takemura void
    629  1.26       uch vrgiu_intr_disestablish(hpcio_chip_t hc, void *arg)
    630   1.1  takemura {
    631   1.1  takemura 	struct vrgiu_intr_entry *ihe = arg;
    632  1.20  takemura 	struct vrgiu_softc *sc = hc->hc_sc;
    633   1.1  takemura 	int port = ihe->ih_port;
    634   1.1  takemura 	struct vrgiu_intr_entry *ih;
    635   1.1  takemura 	int s;
    636   1.1  takemura 
    637   1.1  takemura 	s = splhigh();
    638   1.1  takemura 	TAILQ_FOREACH(ih, &sc->sc_intr_head[port], ih_link) {
    639   1.1  takemura 		if (ih == ihe) {
    640   1.1  takemura 			TAILQ_REMOVE(&sc->sc_intr_head[port], ih, ih_link);
    641   1.1  takemura 			free(ih, M_DEVBUF);
    642   1.1  takemura 			if (TAILQ_EMPTY(&sc->sc_intr_head[port])) {
    643   1.1  takemura 				/* Disable interrupt */
    644   1.1  takemura #ifdef WINCE_DEFAULT_SETTING
    645   1.1  takemura #warning WINCE_DEFAULT_SETTING
    646   1.1  takemura #else
    647   1.1  takemura 				sc->sc_intr_mask &= ~(1<<port);
    648   1.1  takemura 				vrgiu_regwrite_4(sc, GIUINTEN_REG, sc->sc_intr_mask);
    649   1.1  takemura #endif
    650   1.1  takemura 			}
    651   1.1  takemura 			splx(s);
    652   1.1  takemura 			return;
    653   1.1  takemura 		}
    654   1.1  takemura 	}
    655   1.1  takemura 	panic("vrgiu_intr_disetablish: no such a handle.");
    656   1.1  takemura 	/* NOTREACHED */
    657   1.1  takemura }
    658   1.1  takemura 
    659  1.20  takemura /* Clear interrupt */
    660  1.20  takemura void
    661  1.26       uch vrgiu_intr_clear(hpcio_chip_t hc, void *arg)
    662  1.20  takemura {
    663  1.20  takemura 	struct vrgiu_softc *sc = hc->hc_sc;
    664  1.20  takemura 	struct vrgiu_intr_entry *ihe = arg;
    665  1.20  takemura 	u_int32_t reg;
    666  1.20  takemura 
    667  1.20  takemura 	reg = vrgiu_regread_4(sc, GIUINTSTAT_REG);
    668  1.20  takemura 	vrgiu_regwrite_4(sc, GIUINTSTAT_REG, reg & ~(1 << ihe->ih_port));
    669  1.22  takemura }
    670  1.22  takemura 
    671  1.22  takemura static void
    672  1.26       uch vrgiu_register_iochip(hpcio_chip_t hc, hpcio_chip_t iochip)
    673  1.22  takemura {
    674  1.22  takemura 	struct vrgiu_softc *sc = hc->hc_sc;
    675  1.22  takemura 
    676  1.30  takemura 	vrip_register_gpio(sc->sc_vc, iochip);
    677  1.20  takemura }
    678  1.20  takemura 
    679  1.20  takemura /* interrupt handler */
    680   1.1  takemura int
    681  1.26       uch vrgiu_intr(void *arg)
    682   1.1  takemura {
    683   1.1  takemura #ifdef DUMP_GIU_LEVEL2_INTR
    684   1.1  takemura #warning DUMP_GIU_LEVEL2_INTR
    685   1.1  takemura 	static u_int32_t oreg;
    686   1.1  takemura #endif
    687   1.1  takemura 	struct vrgiu_softc *sc = arg;
    688   1.1  takemura 	int i;
    689   1.1  takemura 	u_int32_t reg;
    690  1.17      sato 	int ledvalue = CONFIG_HOOK_LED_FLASH;
    691  1.17      sato 
    692   1.1  takemura 	/* Get Level 2 interrupt status */
    693  1.30  takemura 	vrip_intr_getstatus2 (sc->sc_vc, sc->sc_ih, &reg);
    694   1.1  takemura #ifdef DUMP_GIU_LEVEL2_INTR
    695   1.1  takemura #warning DUMP_GIU_LEVEL2_INTR
    696   1.1  takemura 	{
    697   1.1  takemura 		u_int32_t uedge, dedge, j;
    698   1.1  takemura 		for (j = 0x80000000; j > 0; j >>=1)
    699   1.1  takemura 			printf ("%c" , reg&j ? '|' : '.');
    700   1.1  takemura 		uedge = (reg ^ oreg) & reg;
    701   1.1  takemura 		dedge = (reg ^ oreg) & ~reg;
    702   1.1  takemura 		if (uedge || dedge) {
    703   1.1  takemura 			for (j = 0; j < 32; j++) {
    704   1.1  takemura 				if (uedge & (1 << j))
    705   1.1  takemura 					printf ("+%d", j);
    706   1.1  takemura 				else if (dedge & (1 << j))
    707   1.1  takemura 					printf ("-%d", j);
    708   1.1  takemura 			}
    709   1.1  takemura 		}
    710   1.1  takemura 		oreg = reg;
    711   1.1  takemura 		printf ("\n");
    712   1.1  takemura 	}
    713   1.1  takemura #endif
    714   1.2       uch 	/* Clear interrupt */
    715   1.2       uch 	vrgiu_regwrite_4(sc, GIUINTSTAT_REG, vrgiu_regread_4(sc, GIUINTSTAT_REG));
    716   1.2       uch 
    717   1.1  takemura 	/* Dispatch handler */
    718   1.1  takemura 	for (i = 0; i < MAX_GPIO_INOUT; i++) {
    719   1.1  takemura 		if (reg & (1 << i)) {
    720   1.1  takemura 			register struct vrgiu_intr_entry *ih;
    721   1.1  takemura 			TAILQ_FOREACH(ih, &sc->sc_intr_head[i], ih_link) {
    722   1.1  takemura 				ih->ih_fun(ih->ih_arg);
    723   1.1  takemura 			}
    724   1.1  takemura 		}
    725   1.1  takemura 	}
    726   1.2       uch 
    727  1.17      sato 	if (vrgiu_intr_led)
    728  1.26       uch 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_LED,
    729  1.26       uch 		    (void *)&ledvalue);
    730  1.26       uch 	return (0);
    731   1.1  takemura }
    732