Home | History | Annotate | Line # | Download | only in dev
tc5165buf.c revision 1.5
      1 /*	$NetBSD: tc5165buf.c,v 1.5 2000/03/23 06:38:02 thorpej 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/callout.h>
     40 #include <sys/device.h>
     41 
     42 #include <machine/bus.h>
     43 #include <machine/intr.h>
     44 
     45 #include <hpcmips/tx/tx39var.h>
     46 #include <hpcmips/tx/txcsbusvar.h>
     47 
     48 #include <hpcmips/dev/tc5165bufvar.h>
     49 #include <hpcmips/dev/skbdvar.h>
     50 
     51 #define TC5165_ROW_MAX		16
     52 #define TC5165_COLUMN_MAX	8
     53 
     54 #ifdef TC5165DEBUG
     55 #define	DPRINTF(arg) printf arg
     56 #else
     57 #define	DPRINTF(arg)
     58 #endif
     59 
     60 struct tc5165buf_chip {
     61 	bus_space_tag_t		scc_cst;
     62 	bus_space_handle_t	scc_csh;
     63 	u_int16_t		scc_buf[TC5165_COLUMN_MAX];
     64 	int			scc_enabled;
     65 	int			scc_queued;
     66 	struct callout		scc_soft_ch;
     67 
     68 	struct skbd_controller	scc_controller;
     69 };
     70 
     71 struct tc5165buf_softc {
     72 	struct device		sc_dev;
     73 	struct tc5165buf_chip	*sc_chip;
     74 	tx_chipset_tag_t	sc_tc;
     75 	void			*sc_ih;
     76 };
     77 
     78 int	tc5165buf_match	__P((struct device*, struct cfdata*, void*));
     79 void	tc5165buf_attach __P((struct device*, struct device*, void*));
     80 int	tc5165buf_intr __P((void*));
     81 int	tc5165buf_poll __P((void*));
     82 void	tc5165buf_soft __P((void*));
     83 void	tc5165buf_ifsetup __P((struct tc5165buf_chip*));
     84 
     85 int	tc5165buf_input_establish __P((void*, int (*) __P((void*, int, int)),
     86 				      void (*) __P((void*)), void*));
     87 
     88 struct tc5165buf_chip tc5165buf_chip;
     89 
     90 struct cfattach tc5165buf_ca = {
     91 	sizeof(struct tc5165buf_softc), tc5165buf_match, tc5165buf_attach
     92 };
     93 
     94 int
     95 tc5165buf_match(parent, cf, aux)
     96 	struct device *parent;
     97 	struct cfdata *cf;
     98 	void *aux;
     99 {
    100 	return 1;
    101 }
    102 
    103 void
    104 tc5165buf_attach(parent, self, aux)
    105 	struct device *parent;
    106 	struct device *self;
    107 	void *aux;
    108 {
    109 	struct cs_attach_args *ca = aux;
    110 	struct tc5165buf_softc *sc = (void*)self;
    111 	struct skbd_attach_args saa;
    112 
    113 	printf(": ");
    114 	sc->sc_tc = ca->ca_tc;
    115 	sc->sc_chip = &tc5165buf_chip;
    116 
    117 	callout_init(&sc->sc_chip->scc_soft_ch);
    118 
    119 	sc->sc_chip->scc_cst = ca->ca_csio.cstag;
    120 
    121 	if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
    122 			  ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
    123 		printf("can't map i/o space\n");
    124 		return;
    125 	}
    126 
    127 	sc->sc_chip->scc_enabled = 0;
    128 
    129 	if (ca->ca_irq1 != -1) {
    130 		sc->sc_ih = tx_intr_establish(sc->sc_tc, ca->ca_irq1,
    131 					      IST_EDGE, IPL_TTY,
    132 					      tc5165buf_intr, sc);
    133 		printf("interrupt mode");
    134 	} else {
    135 		sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1, IPL_TTY,
    136 						tc5165buf_intr, sc);
    137 		printf("polling mode");
    138 	}
    139 
    140 	if (!sc->sc_ih) {
    141 		printf(" can't establish interrupt\n");
    142 		return;
    143 	}
    144 
    145 	printf("\n");
    146 
    147 	/* setup upper interface */
    148 	tc5165buf_ifsetup(sc->sc_chip);
    149 
    150 	saa.saa_ic = &sc->sc_chip->scc_controller;
    151 
    152 	config_found(self, &saa, skbd_print);
    153 }
    154 
    155 void
    156 tc5165buf_ifsetup(scc)
    157 	struct tc5165buf_chip *scc;
    158 {
    159 	scc->scc_controller.skif_v		= scc;
    160 
    161 	scc->scc_controller.skif_establish	= tc5165buf_input_establish;
    162 	scc->scc_controller.skif_poll		= tc5165buf_poll;
    163 }
    164 
    165 int
    166 tc5165buf_cnattach(addr)
    167 	paddr_t addr;
    168 {
    169 	struct tc5165buf_chip *scc = &tc5165buf_chip;
    170 
    171 	scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
    172 
    173 	tc5165buf_ifsetup(scc);
    174 
    175 	skbd_cnattach(&scc->scc_controller);
    176 
    177 	return 0;
    178 }
    179 
    180 int
    181 tc5165buf_input_establish(ic, inputfunc, inputhookfunc, arg)
    182 	void *ic;
    183 	int (*inputfunc) __P((void*, int, int));
    184 	void (*inputhookfunc) __P((void*));
    185 	void *arg;
    186 {
    187 	struct tc5165buf_chip *scc = ic;
    188 
    189 	/* setup lower interface */
    190 
    191 	scc->scc_controller.sk_v = arg;
    192 
    193 	scc->scc_controller.sk_input = inputfunc;
    194 	scc->scc_controller.sk_input_hook = inputhookfunc;
    195 
    196 	scc->scc_enabled = 1;
    197 
    198 	return 0;
    199 }
    200 
    201 int
    202 tc5165buf_intr(arg)
    203 	void *arg;
    204 {
    205 	struct tc5165buf_softc *sc = arg;
    206 	struct tc5165buf_chip *scc = sc->sc_chip;
    207 
    208 	if (!scc->scc_enabled || scc->scc_queued)
    209 		return 0;
    210 
    211 	scc->scc_queued = 1;
    212 	callout_reset(&scc->scc_soft_ch, 1, tc5165buf_soft, scc);
    213 
    214 	return 0;
    215 }
    216 
    217 int
    218 tc5165buf_poll(arg)
    219 	void *arg;
    220 {
    221 	struct tc5165buf_chip *scc = arg;
    222 
    223 	if (!scc->scc_enabled)
    224 		return POLL_CONT;
    225 
    226 	tc5165buf_soft(arg);
    227 
    228 	return POLL_CONT;
    229 }
    230 
    231 void
    232 tc5165buf_soft(arg)
    233 	void *arg;
    234 {
    235 	struct tc5165buf_chip *scc = arg;
    236 	bus_space_tag_t t = scc->scc_cst;
    237 	bus_space_handle_t h = scc->scc_csh;
    238 	u_int16_t mask, rpat, edge;
    239 	int i, j, type, val;
    240 	skbd_tag_t controller;
    241 	int s;
    242 
    243 	controller = &scc->scc_controller;
    244 	skbd_input_hook(controller);
    245 
    246 	/* clear scanlines */
    247 	(void)bus_space_read_2(t, h, 0);
    248 	delay(3);
    249 
    250 	for (i = 0; i < TC5165_COLUMN_MAX; i++) {
    251 		rpat = bus_space_read_2(t, h, 2 << i);
    252 		delay(3);
    253 		(void)bus_space_read_2(t, h, 0);
    254 		delay(3);
    255 		if ((edge = (rpat ^ scc->scc_buf[i]))) {
    256 			scc->scc_buf[i] = rpat;
    257 			for (j = 0, mask = 1; j < TC5165_ROW_MAX;
    258 			     j++, mask <<= 1) {
    259 				if (mask & edge) {
    260 					type = mask & rpat ? 1 : 0;
    261 					val = j * TC5165_COLUMN_MAX + i;
    262 					DPRINTF(("%d %d\n", j, i));
    263 					skbd_input(controller, type, val);
    264 				}
    265 			}
    266 		}
    267 	}
    268 
    269 	s = spltty();
    270 	scc->scc_queued = 0;
    271 	splx(s);
    272 }
    273