tx39io.c revision 1.19       1 /*	$NetBSD: tx39io.c,v 1.19 2006/03/07 23:26:24 he 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 <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: tx39io.c,v 1.19 2006/03/07 23:26:24 he Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 
     46 #include <machine/bus.h>
     47 
     48 #include <hpcmips/tx/tx39var.h>
     49 #include <hpcmips/tx/tx39icureg.h>
     50 #define	__TX39IO_PRIVATE
     51 #include <hpcmips/tx/tx39iovar.h>
     52 #include <hpcmips/tx/tx39ioreg.h>
     53 
     54 #ifdef	TX39IO_DEBUG
     55 #define	DPRINTF_ENABLE
     56 #define	DPRINTF_DEBUG	tx39io_debug
     57 #endif
     58 #include <machine/debug.h>
     59 
     60 #ifdef ISSET
     61 #undef ISSET
     62 #endif
     63 #define	ISSET(x, s)	((x) & (1 << (s)))
     64 
     65 int	tx39io_match(struct device *, struct cfdata *, void *);
     66 void	tx39io_attach(struct device *, struct device *, void *);
     67 
     68 CFATTACH_DECL(tx39io, sizeof(struct tx39io_softc),
     69     tx39io_match, tx39io_attach, NULL, NULL);
     70 
     71 /* IO/MFIO common */
     72 static void port_intr_disestablish(hpcio_chip_t, hpcio_intr_handle_t);
     73 static void port_intr_clear(hpcio_chip_t, hpcio_intr_handle_t);
     74 /* MFIO */
     75 static void *mfio_intr_establish(hpcio_chip_t, int, int, int (*)(void *),
     76     void *);
     77 static int mfio_in(hpcio_chip_t, int);
     78 static void mfio_out(hpcio_chip_t, int, int);
     79 static int mfio_intr_map(int *, int, int);
     80 static void mfio_dump(hpcio_chip_t);
     81 static void mfio_update(hpcio_chip_t);
     82 /* IO */
     83 static void *io_intr_establish(hpcio_chip_t, int, int, int (*)(void *),
     84     void *);
     85 #ifdef TX391X
     86 static int tx391x_io_in(hpcio_chip_t, int);
     87 static void tx391x_io_out(hpcio_chip_t, int, int);
     88 static void tx391x_io_update(hpcio_chip_t);
     89 static int tx391x_io_intr_map(int *, int, int);
     90 #endif
     91 #ifdef TX392X
     92 static int tx392x_io_in(hpcio_chip_t, int);
     93 static void tx392x_io_out(hpcio_chip_t, int, int);
     94 static void tx392x_io_update(hpcio_chip_t);
     95 static int tx392x_io_intr_map(int *, int, int);
     96 #endif
     97 #if defined TX391X && defined TX392X
     98 #define	tx39_io_intr_map(t, s, p, m)					\
     99 	(IS_TX391X(t)
    100 	    ? tx391x_io_intr_map(s, p, m) : tx392x_io_intr_map(s, p, m))
    101 #elif defined TX391X
    102 #define	tx39io_intr_map(t, s, p, m)	tx391x_io_intr_map(s, p, m)
    103 #elif defined TX392X
    104 #define	tx39io_intr_map(t, s, p, m)	tx392x_io_intr_map(s, p, m)
    105 #endif
    106 static void io_dump(hpcio_chip_t);
    107 
    108 static void __print_port_status(struct tx39io_port_status *, int);
    109 
    110 int
    111 tx39io_match(struct device *parent, struct cfdata *cf, void *aux)
    112 {
    113 	return (ATTACH_FIRST); /* 1st attach group of txsim */
    114 }
    115 
    116 void
    117 tx39io_attach(struct device *parent, struct device *self, void *aux)
    118 {
    119 	struct txsim_attach_args *ta = aux;
    120 	struct tx39io_softc *sc = (void *)self;
    121 	tx_chipset_tag_t tc;
    122 	struct hpcio_chip *io_hc = &sc->sc_io_ops;
    123 	struct hpcio_chip *mfio_hc = &sc->sc_mfio_ops;
    124 
    125 	tc = sc->sc_tc = ta->ta_tc;
    126 
    127 	printf("\n");
    128 	sc->sc_stat_io_mask = ~(1 << 5); /* exclude Plum2 INT */
    129 	sc->sc_stat_mfio_mask = ~(0x3|(0x3 << 23));
    130 
    131 	/* IO */
    132 	io_hc->hc_chipid		= IO;
    133 	io_hc->hc_name			= "IO";
    134 	io_hc->hc_sc			= sc;
    135 	io_hc->hc_intr_establish	= io_intr_establish;
    136 	io_hc->hc_intr_disestablish	= port_intr_disestablish;
    137 	io_hc->hc_intr_clear		= port_intr_clear;
    138 	io_hc->hc_dump			= io_dump;
    139 	if (IS_TX391X(tc)) {
    140 #ifdef TX391X
    141 		io_hc->hc_portread	= tx391x_io_in;
    142 		io_hc->hc_portwrite	= tx391x_io_out;
    143 		io_hc->hc_update	= tx391x_io_update;
    144 #endif
    145 	} else if (IS_TX392X(tc)) {
    146 #ifdef TX392X
    147 		io_hc->hc_portread	= tx392x_io_in;
    148 		io_hc->hc_portwrite	= tx392x_io_out;
    149 		io_hc->hc_update	= tx392x_io_update;
    150 #endif
    151 	}
    152 	tx_conf_register_ioman(tc, io_hc);
    153 
    154 	/* MFIO */
    155 	mfio_hc->hc_chipid		= MFIO;
    156 	mfio_hc->hc_name		= "MFIO";
    157 	mfio_hc->hc_sc			= sc;
    158 	mfio_hc->hc_portread		= mfio_in;
    159 	mfio_hc->hc_portwrite		= mfio_out;
    160 	mfio_hc->hc_intr_establish	= mfio_intr_establish;
    161 	mfio_hc->hc_intr_disestablish	= port_intr_disestablish;
    162 	mfio_hc->hc_update		= mfio_update;
    163 	mfio_hc->hc_dump		= mfio_dump;
    164 
    165 	tx_conf_register_ioman(tc, mfio_hc);
    166 
    167 	hpcio_update(io_hc);
    168 	hpcio_update(mfio_hc);
    169 
    170 #ifdef TX39IO_DEBUG
    171 	hpcio_dump(io_hc);
    172 	hpcio_dump(mfio_hc);
    173 	printf("IO i0x%08x o0x%08x MFIO i0x%08x o0x%08x\n",
    174 	       sc->sc_stat_io.in, sc->sc_stat_io.out,
    175 	       sc->sc_stat_mfio.in, sc->sc_stat_mfio.out);
    176 #endif /* TX39IO_DEBUG */
    177 }
    178 
    179 /*
    180  * TX391X, TX392X common
    181  */
    182 static void *
    183 io_intr_establish(hpcio_chip_t arg, int port, int mode, int (*func)(void *),
    184     void *func_arg)
    185 {
    186 	struct tx39io_softc *sc = arg->hc_sc;
    187 	int src;
    188 
    189 	if (tx39io_intr_map(sc->sc_tc, &src, port, mode) != 0)
    190 		return (0);
    191 
    192 	return (tx_intr_establish(sc->sc_tc, src, IST_EDGE, IPL_CLOCK, func,
    193 	    func_arg));
    194 }
    195 
    196 static void *
    197 mfio_intr_establish(hpcio_chip_t arg, int port, int mode, int (*func)(void *),
    198     void *func_arg)
    199 {
    200 	struct tx39io_softc *sc = arg->hc_sc;
    201 	int src;
    202 
    203 	if (mfio_intr_map(&src, port, mode) != 0)
    204 		return (0);
    205 
    206 	return (tx_intr_establish(sc->sc_tc, src, IST_EDGE, IPL_CLOCK, func,
    207 	    func_arg));
    208 }
    209 
    210 static void
    211 port_intr_disestablish(hpcio_chip_t arg, void *ih)
    212 {
    213 	struct tx39io_softc *sc = arg->hc_sc;
    214 	tx_intr_disestablish(sc->sc_tc, ih);
    215 }
    216 
    217 static void
    218 port_intr_clear(hpcio_chip_t arg, void *ih)
    219 {
    220 }
    221 
    222 static void
    223 mfio_out(hpcio_chip_t arg, int port, int onoff)
    224 {
    225 	struct tx39io_softc *sc = arg->hc_sc;
    226 	tx_chipset_tag_t tc;
    227 	txreg_t reg, pos;
    228 
    229 	DPRINTF("port #%d\n", port);
    230 	tc = sc->sc_tc;
    231 	/* MFIO */
    232 	pos = 1 << port;
    233 #ifdef DIAGNOSTIC
    234 	if (!(sc->sc_stat_mfio.dir & pos)) {
    235 		panic("%s: MFIO%d is not output port.",
    236 		      sc->sc_dev.dv_xname, port);
    237 	}
    238 #endif
    239 	reg = tx_conf_read(tc, TX39_IOMFIODATAOUT_REG);
    240 	if (onoff)
    241 		reg |= pos;
    242 	else
    243 		reg &= ~pos;
    244 	tx_conf_write(tc, TX39_IOMFIODATAOUT_REG, reg);
    245 }
    246 
    247 static int
    248 mfio_in(hpcio_chip_t arg, int port)
    249 {
    250 	struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc ;
    251 
    252 	DPRINTF("port #%d\n", port);
    253 	return (tx_conf_read(sc->sc_tc, TX39_IOMFIODATAIN_REG) & (1 << port));
    254 }
    255 
    256 static int
    257 mfio_intr_map(int *src, int port, int mode)
    258 {
    259 
    260 	if (mode & HPCIO_INTR_POSEDGE) {
    261 		*src = MAKEINTR(3, (1 << port));
    262 		return (0);
    263 	} else if (mode & HPCIO_INTR_NEGEDGE) {
    264 		*src = MAKEINTR(4, (1 << port));
    265 		return (0);
    266 	}
    267 
    268 	DPRINTF("invalid interrupt mode.\n");
    269 
    270 	return (1);
    271 }
    272 
    273 static void
    274 mfio_update(hpcio_chip_t arg)
    275 {
    276 	struct tx39io_softc *sc = arg->hc_sc;
    277 	tx_chipset_tag_t tc = sc->sc_tc;
    278 	struct tx39io_port_status *stat_mfio = &sc->sc_stat_mfio;
    279 
    280 	sc->sc_ostat_mfio = *stat_mfio; /* save old status */
    281 	stat_mfio->dir		= tx_conf_read(tc, TX39_IOMFIODATADIR_REG);
    282 	stat_mfio->in		= tx_conf_read(tc, TX39_IOMFIODATAIN_REG);
    283 	stat_mfio->out		= tx_conf_read(tc, TX39_IOMFIODATAOUT_REG);
    284 	stat_mfio->power	= tx_conf_read(tc, TX39_IOMFIOPOWERDWN_REG);
    285 	stat_mfio->u.select	= tx_conf_read(tc, TX39_IOMFIODATASEL_REG);
    286 }
    287 
    288 #ifdef TX391X
    289 /*
    290  * TMPR3912 specific
    291  */
    292 int
    293 tx391x_io_in(hpcio_chip_t arg, int port)
    294 {
    295 	struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc;
    296 	txreg_t reg = tx_conf_read(sc->sc_tc, TX39_IOCTRL_REG);
    297 
    298 	DPRINTF("port #%d\n", port);
    299 	return  (TX391X_IOCTRL_IODIN(reg) & (1 << port));
    300 }
    301 
    302 void
    303 tx391x_io_out(hpcio_chip_t arg, int port, int onoff)
    304 {
    305 	struct tx39io_softc *sc = arg->hc_sc;
    306 	tx_chipset_tag_t tc;
    307 	txreg_t reg, pos, iostat;
    308 
    309 	KASSERT(sc);
    310 	DPRINTF("port #%d\n", port);
    311 
    312 	tc = sc->sc_tc;
    313 
    314 	/* IO [0:6] */
    315 	pos = 1 << port;
    316 #ifdef DIAGNOSTIC
    317 	if (!(sc->sc_stat_io.dir & pos))
    318 		panic("%s: IO%d is not output port.", sc->sc_dev.dv_xname,
    319 		      port);
    320 #endif
    321 	reg = tx_conf_read(tc, TX39_IOCTRL_REG);
    322 	iostat = TX391X_IOCTRL_IODOUT(reg);
    323 	if (onoff)
    324 		iostat |= pos;
    325 	else
    326 		iostat &= ~pos;
    327 	TX391X_IOCTRL_IODOUT_CLR(reg);
    328 	reg = TX391X_IOCTRL_IODOUT_SET(reg, iostat);
    329 	tx_conf_write(tc, TX39_IOCTRL_REG, reg);
    330 }
    331 
    332 void
    333 tx391x_io_update(hpcio_chip_t arg)
    334 {
    335 	struct tx39io_softc *sc = arg->hc_sc;
    336 	struct tx39io_port_status *stat_io = &sc->sc_stat_io;
    337 	tx_chipset_tag_t tc = sc->sc_tc;
    338 	txreg_t reg;
    339 
    340 	/* IO [0:6] */
    341 	sc->sc_ostat_io = *stat_io; /* save old status */
    342 	reg = tx_conf_read(tc, TX39_IOCTRL_REG);
    343 	stat_io->dir		= TX391X_IOCTRL_IODIREC(reg);
    344 	stat_io->in		= TX391X_IOCTRL_IODIN(reg);
    345 	stat_io->out		= TX391X_IOCTRL_IODOUT(reg);
    346 	stat_io->u.debounce	= TX391X_IOCTRL_IODEBSEL(reg);
    347 	reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
    348 	stat_io->power = TX391X_IOIOPOWERDWN_IOPD(reg);
    349 }
    350 
    351 int
    352 tx391x_io_intr_map(int *src, int port, int mode)
    353 {
    354 
    355 	if (mode & HPCIO_INTR_POSEDGE) {
    356 		*src = MAKEINTR(5, (1 << (port + 7)));
    357 		return (0);
    358 	} else if (mode & HPCIO_INTR_NEGEDGE) {
    359 		*src = MAKEINTR(5, (1 << port));
    360 		return (0);
    361 	}
    362 
    363 	DPRINTF("invalid interrupt mode.\n");
    364 
    365 	return (1);
    366 }
    367 #endif /* TX391X */
    368 
    369 #ifdef TX392X
    370 /*
    371  * TMPR3922 specific
    372  */
    373 int
    374 tx392x_io_in(hpcio_chip_t arg, int port)
    375 {
    376 	struct tx39io_softc *sc __attribute__((__unused__)) = arg->hc_sc;
    377 	txreg_t reg = tx_conf_read(sc->sc_tc, TX392X_IODATAINOUT_REG);
    378 
    379 	DPRINTF("port #%d\n", port);
    380 
    381 	return (TX392X_IODATAINOUT_DIN(reg) & (1 << port));
    382 }
    383 
    384 void
    385 tx392x_io_out(hpcio_chip_t arg, int port, int onoff)
    386 {
    387 	struct tx39io_softc *sc = arg->hc_sc;
    388 #ifdef DIAGNOSTIC
    389 	const char *devname =  sc->sc_dev.dv_xname;
    390 #endif
    391 	tx_chipset_tag_t tc = sc->sc_tc;
    392 	txreg_t reg, pos, iostat;
    393 
    394 	DPRINTF("port #%d\n", port);
    395 	/* IO [0:15] */
    396 	pos = 1 << port;
    397 #ifdef DIAGNOSTIC
    398 	if (!(sc->sc_stat_io.dir & pos))
    399 		panic("%s: IO%d is not output port.", devname, port);
    400 #endif
    401 	reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
    402 	iostat = TX392X_IODATAINOUT_DOUT(reg);
    403 	if (onoff)
    404 		iostat |= pos;
    405 	else
    406 		iostat &= ~pos;
    407 	TX392X_IODATAINOUT_DOUT_CLR(reg);
    408 	reg = TX392X_IODATAINOUT_DOUT_SET(reg, iostat);
    409 	tx_conf_write(tc, TX392X_IODATAINOUT_REG, reg);
    410 }
    411 
    412 int
    413 tx392x_io_intr_map(int *src, int port, int mode)
    414 {
    415 
    416 	if (mode & HPCIO_INTR_POSEDGE) {
    417 		*src = MAKEINTR(8, (1 << (port + 16)));
    418 		return (0);
    419 	} else if (mode & HPCIO_INTR_NEGEDGE) {
    420 		*src = MAKEINTR(8, (1 << port));
    421 		return (0);
    422 	}
    423 
    424 	DPRINTF("invalid interrupt mode.\n");
    425 
    426 	return (1);
    427 }
    428 
    429 void
    430 tx392x_io_update(hpcio_chip_t arg)
    431 {
    432 	struct tx39io_softc *sc = arg->hc_sc;
    433 	struct tx39io_port_status *stat_io = &sc->sc_stat_io;
    434 	tx_chipset_tag_t tc = sc->sc_tc;
    435 	txreg_t reg;
    436 
    437 	sc->sc_ostat_io = *stat_io; /* save old status */
    438 	/* IO [0:15] */
    439 	reg = tx_conf_read(tc, TX39_IOCTRL_REG);
    440 	stat_io->dir		= TX392X_IOCTRL_IODIREC(reg);
    441 	stat_io->u.debounce	= TX392X_IOCTRL_IODEBSEL(reg);
    442 	reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
    443 	stat_io->in		= TX392X_IODATAINOUT_DIN(reg);
    444 	stat_io->out		= TX392X_IODATAINOUT_DOUT(reg);
    445 	reg = tx_conf_read(tc, TX39_IOIOPOWERDWN_REG);
    446 	stat_io->power = TX392X_IOIOPOWERDWN_IOPD(reg);
    447 }
    448 
    449 #endif /* TX392X */
    450 
    451 static const char *line = "--------------------------------------------------"
    452 "------------\n";
    453 static void
    454 mfio_dump(hpcio_chip_t arg)
    455 {
    456 	struct tx39io_softc *sc = arg->hc_sc;
    457 	const struct tx39io_mfio_map *map = tx39io_get_mfio_map(tc);
    458 	struct tx39io_port_status *stat;
    459 	int i;
    460 
    461 	printf("%s", line);
    462 	stat = &sc->sc_stat_mfio;
    463 	for (i = TX39_IO_MFIO_MAX - 1; i >= 0 ; i--) {
    464 		/* MFIO port has power down state */
    465 		printf("MFIO %2d:     -       ", i);
    466 		__print_port_status(stat, i);
    467 		printf(ISSET(stat->u.select, i) ? "  MFIO(%s)\n" : "  %s\n",
    468 		       map[i].std_pin_name);
    469 	}
    470 	printf("%s", line);
    471 }
    472 
    473 static void
    474 io_dump(hpcio_chip_t arg)
    475 {
    476 	struct tx39io_softc *sc = arg->hc_sc;
    477 	struct tx39io_port_status *stat;
    478 	int i;
    479 
    480 	printf("%s	 Debounce Direction DataOut DataIn PowerDown Select"
    481 	       "\n%s", line, line);
    482 	stat = &sc->sc_stat_io;
    483 	for (i = tx39io_get_io_max(tc) - 1; i >= 0 ; i--) {
    484 		/* IO port has debouncer */
    485 		printf("IO   %2d:    %s      ", i,
    486 		       ISSET(stat->u.debounce, i) ? "On " : "Off");
    487 		__print_port_status(stat, i);
    488 		printf("    -\n");
    489 	}
    490 }
    491 
    492 static void
    493 __print_port_status(struct tx39io_port_status *stat, int i)
    494 {
    495 	printf("%s       %d       %d     %s",
    496 	       ISSET(stat->dir, i) ? "Out" : "In ",
    497 	       ISSET(stat->out, i) ? 1 : 0,
    498 	       ISSET(stat->in, i) ? 1 : 0,
    499 	       ISSET(stat->power, i) ? "Down  ": "Active");
    500 }
    501