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