Home | History | Annotate | Line # | Download | only in dev
tc5165buf.c revision 1.4
      1 /*	$NetBSD: tc5165buf.c,v 1.4 2000/01/16 21:47:01 uch Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999, 2000, by UCHIYAMA Yasushi
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. The name of the developer may NOT be used to endorse or promote products
     13  *    derived from this software without specific prior written permission.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  *
     27  */
     28 
     29 /*
     30  * Device driver for TOSHIBA TC5165BFTS, PHILIPS 74ALVC16241/245
     31  * buffer chip
     32  */
     33 
     34 #include "opt_tx39_debug.h"
     35 #include "opt_use_poll.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 
     41 #include <machine/bus.h>
     42 #include <machine/intr.h>
     43 
     44 #include <hpcmips/tx/tx39var.h>
     45 #include <hpcmips/tx/txcsbusvar.h>
     46 
     47 #include <hpcmips/dev/tc5165bufvar.h>
     48 #include <hpcmips/dev/skbdvar.h>
     49 
     50 #define TC5165_ROW_MAX		16
     51 #define TC5165_COLUMN_MAX	8
     52 
     53 #ifdef TC5165DEBUG
     54 #define	DPRINTF(arg) printf arg
     55 #else
     56 #define	DPRINTF(arg)
     57 #endif
     58 
     59 struct tc5165buf_chip {
     60 	bus_space_tag_t		scc_cst;
     61 	bus_space_handle_t	scc_csh;
     62 	u_int16_t		scc_buf[TC5165_COLUMN_MAX];
     63 	int			scc_enabled;
     64 	int			scc_queued;
     65 
     66 	struct skbd_controller	scc_controller;
     67 };
     68 
     69 struct tc5165buf_softc {
     70 	struct device		sc_dev;
     71 	struct tc5165buf_chip	*sc_chip;
     72 	tx_chipset_tag_t	sc_tc;
     73 	void			*sc_ih;
     74 };
     75 
     76 int	tc5165buf_match	__P((struct device*, struct cfdata*, void*));
     77 void	tc5165buf_attach __P((struct device*, struct device*, void*));
     78 int	tc5165buf_intr __P((void*));
     79 int	tc5165buf_poll __P((void*));
     80 void	tc5165buf_soft __P((void*));
     81 void	tc5165buf_ifsetup __P((struct tc5165buf_chip*));
     82 
     83 int	tc5165buf_input_establish __P((void*, int (*) __P((void*, int, int)),
     84 				      void (*) __P((void*)), void*));
     85 
     86 struct tc5165buf_chip tc5165buf_chip;
     87 
     88 struct cfattach tc5165buf_ca = {
     89 	sizeof(struct tc5165buf_softc), tc5165buf_match, tc5165buf_attach
     90 };
     91 
     92 int
     93 tc5165buf_match(parent, cf, aux)
     94 	struct device *parent;
     95 	struct cfdata *cf;
     96 	void *aux;
     97 {
     98 	return 1;
     99 }
    100 
    101 void
    102 tc5165buf_attach(parent, self, aux)
    103 	struct device *parent;
    104 	struct device *self;
    105 	void *aux;
    106 {
    107 	struct cs_attach_args *ca = aux;
    108 	struct tc5165buf_softc *sc = (void*)self;
    109 	struct skbd_attach_args saa;
    110 
    111 	printf(": ");
    112 	sc->sc_tc = ca->ca_tc;
    113 	sc->sc_chip = &tc5165buf_chip;
    114 
    115 	sc->sc_chip->scc_cst = ca->ca_csio.cstag;
    116 
    117 	if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
    118 			  ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
    119 		printf("can't map i/o space\n");
    120 		return;
    121 	}
    122 
    123 	sc->sc_chip->scc_enabled = 0;
    124 
    125 	if (ca->ca_irq1 != -1) {
    126 		sc->sc_ih = tx_intr_establish(sc->sc_tc, ca->ca_irq1,
    127 					      IST_EDGE, IPL_TTY,
    128 					      tc5165buf_intr, sc);
    129 		printf("interrupt mode");
    130 	} else {
    131 		sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1, IPL_TTY,
    132 						tc5165buf_intr, sc);
    133 		printf("polling mode");
    134 	}
    135 
    136 	if (!sc->sc_ih) {
    137 		printf(" can't establish interrupt\n");
    138 		return;
    139 	}
    140 
    141 	printf("\n");
    142 
    143 	/* setup upper interface */
    144 	tc5165buf_ifsetup(sc->sc_chip);
    145 
    146 	saa.saa_ic = &sc->sc_chip->scc_controller;
    147 
    148 	config_found(self, &saa, skbd_print);
    149 }
    150 
    151 void
    152 tc5165buf_ifsetup(scc)
    153 	struct tc5165buf_chip *scc;
    154 {
    155 	scc->scc_controller.skif_v		= scc;
    156 
    157 	scc->scc_controller.skif_establish	= tc5165buf_input_establish;
    158 	scc->scc_controller.skif_poll		= tc5165buf_poll;
    159 }
    160 
    161 int
    162 tc5165buf_cnattach(addr)
    163 	paddr_t addr;
    164 {
    165 	struct tc5165buf_chip *scc = &tc5165buf_chip;
    166 
    167 	scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
    168 
    169 	tc5165buf_ifsetup(scc);
    170 
    171 	skbd_cnattach(&scc->scc_controller);
    172 
    173 	return 0;
    174 }
    175 
    176 int
    177 tc5165buf_input_establish(ic, inputfunc, inputhookfunc, arg)
    178 	void *ic;
    179 	int (*inputfunc) __P((void*, int, int));
    180 	void (*inputhookfunc) __P((void*));
    181 	void *arg;
    182 {
    183 	struct tc5165buf_chip *scc = ic;
    184 
    185 	/* setup lower interface */
    186 
    187 	scc->scc_controller.sk_v = arg;
    188 
    189 	scc->scc_controller.sk_input = inputfunc;
    190 	scc->scc_controller.sk_input_hook = inputhookfunc;
    191 
    192 	scc->scc_enabled = 1;
    193 
    194 	return 0;
    195 }
    196 
    197 int
    198 tc5165buf_intr(arg)
    199 	void *arg;
    200 {
    201 	struct tc5165buf_softc *sc = arg;
    202 	struct tc5165buf_chip *scc = sc->sc_chip;
    203 
    204 	if (!scc->scc_enabled || scc->scc_queued)
    205 		return 0;
    206 
    207 	scc->scc_queued = 1;
    208 	timeout(tc5165buf_soft, scc, 1);
    209 
    210 	return 0;
    211 }
    212 
    213 int
    214 tc5165buf_poll(arg)
    215 	void *arg;
    216 {
    217 	struct tc5165buf_chip *scc = arg;
    218 
    219 	if (!scc->scc_enabled)
    220 		return POLL_CONT;
    221 
    222 	tc5165buf_soft(arg);
    223 
    224 	return POLL_CONT;
    225 }
    226 
    227 void
    228 tc5165buf_soft(arg)
    229 	void *arg;
    230 {
    231 	struct tc5165buf_chip *scc = arg;
    232 	bus_space_tag_t t = scc->scc_cst;
    233 	bus_space_handle_t h = scc->scc_csh;
    234 	u_int16_t mask, rpat, edge;
    235 	int i, j, type, val;
    236 	skbd_tag_t controller;
    237 	int s;
    238 
    239 	controller = &scc->scc_controller;
    240 	skbd_input_hook(controller);
    241 
    242 	/* clear scanlines */
    243 	(void)bus_space_read_2(t, h, 0);
    244 	delay(3);
    245 
    246 	for (i = 0; i < TC5165_COLUMN_MAX; i++) {
    247 		rpat = bus_space_read_2(t, h, 2 << i);
    248 		delay(3);
    249 		(void)bus_space_read_2(t, h, 0);
    250 		delay(3);
    251 		if ((edge = (rpat ^ scc->scc_buf[i]))) {
    252 			scc->scc_buf[i] = rpat;
    253 			for (j = 0, mask = 1; j < TC5165_ROW_MAX;
    254 			     j++, mask <<= 1) {
    255 				if (mask & edge) {
    256 					type = mask & rpat ? 1 : 0;
    257 					val = j * TC5165_COLUMN_MAX + i;
    258 					DPRINTF(("%d %d\n", j, i));
    259 					skbd_input(controller, type, val);
    260 				}
    261 			}
    262 		}
    263 	}
    264 
    265 	s = spltty();
    266 	scc->scc_queued = 0;
    267 	splx(s);
    268 }
    269