Home | History | Annotate | Line # | Download | only in vr
vrkiu.c revision 1.28
      1 /*	$NetBSD: vrkiu.c,v 1.28 2001/06/11 06:11:01 enami Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 SASAKI Takesi All rights reserved.
      5  * Copyright (c) 1999,2000 TAKEMRUA, Shin All rights reserved.
      6  * Copyright (c) 1999 PocketBSD Project. All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the PocketBSD project
     19  *	and its contributors.
     20  * 4. Neither the name of the project nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/tty.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 #include <sys/conf.h>
     43 #include <sys/kernel.h>
     44 #include <sys/proc.h>
     45 
     46 #include <machine/intr.h>
     47 #include <machine/cpu.h>
     48 #include <machine/bus.h>
     49 #include <machine/platid.h>
     50 #include <machine/platid_mask.h>
     51 
     52 #include <dev/hpc/hpckbdvar.h>
     53 
     54 #include <hpcmips/vr/vr.h>
     55 #include <hpcmips/vr/vripvar.h>
     56 #include <hpcmips/vr/vrkiureg.h>
     57 #include <hpcmips/vr/vrkiuvar.h>
     58 #include <hpcmips/vr/icureg.h>
     59 
     60 #include "opt_wsdisplay_compat.h"
     61 #include "opt_pckbd_layout.h"
     62 #include <dev/wscons/wsconsio.h>
     63 #include <dev/wscons/wskbdvar.h>
     64 #include <dev/wscons/wsksymdef.h>
     65 #include <dev/wscons/wsksymvar.h>
     66 #include <dev/pckbc/wskbdmap_mfii.h>
     67 #ifdef WSDISPLAY_COMPAT_RAWKBD
     68 #include <dev/hpc/pckbd_encode.h>
     69 #endif
     70 
     71 #define VRKIUDEBUG
     72 #ifdef VRKIUDEBUG
     73 int vrkiu_debug = 0;
     74 #define DPRINTF(arg) if (vrkiu_debug) printf arg;
     75 #else
     76 #define	DPRINTF(arg)
     77 #endif
     78 
     79 /*
     80  * function prototypes
     81  */
     82 static int vrkiumatch __P((struct device *, struct cfdata *, void *));
     83 static void vrkiuattach __P((struct device *, struct device *, void *));
     84 
     85 int vrkiu_intr __P((void *));
     86 
     87 static int vrkiu_init(struct vrkiu_chip*, bus_space_tag_t, bus_space_handle_t);
     88 static void vrkiu_write __P((struct vrkiu_chip *, int, unsigned short));
     89 static unsigned short vrkiu_read __P((struct vrkiu_chip *, int));
     90 static int vrkiu_is_console(bus_space_tag_t, bus_space_handle_t);
     91 static void vrkiu_scan __P((struct vrkiu_chip *));
     92 static int countbits(int);
     93 static void eliminate_phantom_keys(struct vrkiu_chip*, unsigned short *);
     94 static int vrkiu_poll __P((void*));
     95 static int vrkiu_input_establish __P((void*, struct hpckbd_if*));
     96 
     97 /*
     98  * global/static data
     99  */
    100 struct cfattach vrkiu_ca = {
    101 	sizeof(struct vrkiu_softc), vrkiumatch, vrkiuattach
    102 };
    103 
    104 struct vrkiu_chip *vrkiu_consdata = NULL;
    105 
    106 /*
    107  * utilities
    108  */
    109 static inline void
    110 vrkiu_write(chip, port, val)
    111 	struct vrkiu_chip *chip;
    112 	int port;
    113 	unsigned short val;
    114 {
    115 	bus_space_write_2(chip->kc_iot, chip->kc_ioh, port, val);
    116 }
    117 
    118 static inline unsigned short
    119 vrkiu_read(chip, port)
    120 	struct vrkiu_chip *chip;
    121 	int port;
    122 {
    123 	return bus_space_read_2(chip->kc_iot, chip->kc_ioh, port);
    124 }
    125 
    126 static inline int
    127 vrkiu_is_console(iot, ioh)
    128 	bus_space_tag_t iot;
    129 	bus_space_handle_t ioh;
    130 {
    131 	if (vrkiu_consdata &&
    132 	    vrkiu_consdata->kc_iot == iot &&
    133 	    vrkiu_consdata->kc_ioh == ioh) {
    134 		return 1;
    135 	} else {
    136 		return 0;
    137 	}
    138 }
    139 
    140 /*
    141  * initialize device
    142  */
    143 static int
    144 vrkiu_init(chip, iot, ioh)
    145 	struct vrkiu_chip* chip;
    146 	bus_space_tag_t iot;
    147 	bus_space_handle_t ioh;
    148 {
    149 	memset(chip, 0, sizeof(struct vrkiu_chip));
    150 	chip->kc_iot = iot;
    151 	chip->kc_ioh = ioh;
    152 	chip->kc_enabled = 0;
    153 
    154 	chip->kc_if.hii_ctx		= chip;
    155 	chip->kc_if.hii_establish	= vrkiu_input_establish;
    156 	chip->kc_if.hii_poll		= vrkiu_poll;
    157 
    158 	/* set KIU */
    159 	vrkiu_write(chip, KIURST, 1);   /* reset */
    160 	vrkiu_write(chip, KIUSCANLINE, 0); /* 96keys */
    161 	vrkiu_write(chip, KIUWKS, 0x18a4); /* XXX: scan timing! */
    162 	vrkiu_write(chip, KIUWKI, 450);
    163 	vrkiu_write(chip, KIUSCANREP, 0x8023);
    164 				/* KEYEN | STPREP = 2 | ATSTP | ATSCAN */
    165 
    166 	return 0;
    167 }
    168 
    169 /*
    170  * probe
    171  */
    172 static int
    173 vrkiumatch(parent, cf, aux)
    174 	struct device *parent;
    175 	struct cfdata *cf;
    176 	void *aux;
    177 {
    178 	return 1;
    179 }
    180 
    181 /*
    182  * attach
    183  */
    184 static void
    185 vrkiuattach(parent, self, aux)
    186 	struct device *parent;
    187 	struct device *self;
    188 	void *aux;
    189 {
    190 	struct vrkiu_softc *sc = (struct vrkiu_softc *)self;
    191 	struct vrip_attach_args *va = aux;
    192 	struct hpckbd_attach_args haa;
    193 	int isconsole;
    194 
    195 	bus_space_tag_t iot = va->va_iot;
    196 	bus_space_handle_t ioh;
    197 
    198 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    199 		printf(": can't map bus space\n");
    200 		return;
    201 	}
    202 
    203 	isconsole = vrkiu_is_console(iot, ioh);
    204 	if (isconsole) {
    205 		sc->sc_chip = vrkiu_consdata;
    206 	} else {
    207 		sc->sc_chip = &sc->sc_chip_body;
    208 		vrkiu_init(sc->sc_chip, iot, ioh);
    209 	}
    210 	sc->sc_chip->kc_sc = sc;
    211 
    212 	if (!(sc->sc_handler =
    213 	      vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
    214 				  vrkiu_intr, sc))) {
    215 		printf (": can't map interrupt line.\n");
    216 		return;
    217 	}
    218 	/* Level2 register setting */
    219 	vrip_intr_setmask2(va->va_vc, sc->sc_handler, KIUINT_KDATRDY, 1);
    220 
    221 	printf("\n");
    222 
    223 	/* attach hpckbd */
    224 	haa.haa_ic = &sc->sc_chip->kc_if;
    225 	config_found(self, &haa, hpckbd_print);
    226 }
    227 
    228 int
    229 vrkiu_intr(arg)
    230 	void *arg;
    231 {
    232         struct vrkiu_softc *sc = arg;
    233 
    234 	/* When key scan finisshed, this entry is called. */
    235 #if 0
    236 	DPRINTF(("vrkiu_intr: intr=%x scan=%x\n",
    237 		 vrkiu_read(sc->sc_chip, KIUINT) & 7,
    238 		 vrkiu_read(sc->sc_chip, KIUSCANS) & KIUSCANS_SSTAT_MASK));
    239 #endif
    240 
    241 	/*
    242 	 * First, we must clear the interrupt register because
    243 	 * vrkiu_scan() may takes long time if a bitmap screen
    244 	 * scrolls up and it makes us to miss some key release
    245 	 * event.
    246 	 */
    247 	vrkiu_write(sc->sc_chip, KIUINT, 0x7); /* Clear all interrupt */
    248 
    249 #if 1
    250 	/* just return if kiu is scanning keyboard. */
    251 	if ((vrkiu_read(sc->sc_chip, KIUSCANS) & KIUSCANS_SSTAT_MASK) ==
    252 	       KIUSCANS_SSTAT_SCANNING)
    253 		return (0);
    254 #endif
    255 	vrkiu_scan(sc->sc_chip);
    256 
    257 	return (0);
    258 }
    259 
    260 static int
    261 countbits(d)
    262 	int d;
    263 {
    264 	int i, n;
    265 
    266 	for (i = 0, n = 0; i < NBBY; i++)
    267 		if (d & (1 << i))
    268 			n++;
    269 	return n;
    270 }
    271 
    272 static void
    273 eliminate_phantom_keys(chip, scandata)
    274 	struct vrkiu_chip* chip;
    275 	unsigned short *scandata;
    276 {
    277 	unsigned char inkey[KIU_NSCANLINE], *prevkey, *reskey;
    278 	int i, j;
    279 #ifdef VRKIUDEBUG
    280 	int modified = 0;
    281 	static int prevmod = 0;
    282 #endif
    283 
    284 	reskey = (unsigned char *)scandata;
    285 	for (i = 0; i < KIU_NSCANLINE; i++)
    286 		inkey[i] = reskey[i];
    287 	prevkey = (unsigned char *)chip->kc_scandata;
    288 
    289 	for (i = 0; i < KIU_NSCANLINE; i++) {
    290 	    if (countbits(inkey[i]) > 1) {
    291 		for (j = 0; j < KIU_NSCANLINE; j++) {
    292 		    if (i != j && (inkey[i] & inkey[j])) {
    293 #ifdef VRKIUDEBUG
    294 			    modified = 1;
    295 			    if (!prevmod) {
    296 				DPRINTF(("vrkiu_scan: %x:%02x->%02x",
    297 					 i, inkey[i], prevkey[i]));
    298 				DPRINTF(("  %x:%02x->%02x\n",
    299 					 j, inkey[j], prevkey[j]));
    300 			    }
    301 #endif
    302 			    reskey[i] = prevkey[i];
    303 			    reskey[j] = prevkey[j];
    304 		    }
    305 		}
    306 	    }
    307 	}
    308 #ifdef VRKIUDEBUG
    309 	prevmod = modified;
    310 #endif
    311 }
    312 
    313 static void
    314 vrkiu_scan(chip)
    315 	struct vrkiu_chip* chip;
    316 {
    317 	int i, j, modified, mask;
    318 	unsigned short scandata[KIU_NSCANLINE/2];
    319 
    320 	if (!chip->kc_enabled)
    321 		return;
    322 
    323 	for (i = 0; i < KIU_NSCANLINE / 2; i++) {
    324 		scandata[i] = vrkiu_read(chip, KIUDATP + i * 2);
    325 	}
    326 	eliminate_phantom_keys(chip, scandata);
    327 
    328 	for (i = 0; i < KIU_NSCANLINE / 2; i++) {
    329 		modified = scandata[i] ^ chip->kc_scandata[i];
    330 		chip->kc_scandata[i] = scandata[i];
    331 		mask = 1;
    332 		for (j = 0; j < 16; j++, mask <<= 1) {
    333 			/*
    334 			 * Simultaneous keypresses are resolved by registering
    335 			 * the one with the lowest bit index first.
    336 			 */
    337 			if (modified & mask) {
    338 				int key = i * 16 + j;
    339 				DPRINTF(("vrkiu_scan: %s(%d,%d)\n",
    340 					 (scandata[i] & mask) ? "down" : "up",
    341 					 i, j));
    342 				hpckbd_input(chip->kc_hpckbd,
    343 					     (scandata[i] & mask), key);
    344 			}
    345 		}
    346 	}
    347 }
    348 
    349 /* called from bicons.c */
    350 int
    351 vrkiu_getc()
    352 {
    353 	static int flag = 1;
    354 
    355 	/*
    356 	 * XXX, currently
    357 	 */
    358 	if (flag) {
    359 		flag = 0;
    360 		printf("%s(%d): vrkiu_getc() is not implemented\n",
    361 		       __FILE__, __LINE__);
    362 	}
    363 	return 0;
    364 }
    365 
    366 /*
    367  * hpckbd interface routines
    368  */
    369 int
    370 vrkiu_input_establish(ic, kbdif)
    371 	void *ic;
    372 	struct hpckbd_if *kbdif;
    373 {
    374 	struct vrkiu_chip *kc = ic;
    375 
    376 	/* save hpckbd interface */
    377 	kc->kc_hpckbd = kbdif;
    378 
    379 	kc->kc_enabled = 1;
    380 
    381 	return 0;
    382 }
    383 
    384 int
    385 vrkiu_poll(ic)
    386 	void *ic;
    387 {
    388 	struct vrkiu_chip *kc = ic;
    389 
    390 #if 1
    391 	/* wait until kiu completes keyboard scan. */
    392 	while ((vrkiu_read(kc, KIUSCANS) & KIUSCANS_SSTAT_MASK) ==
    393 	       KIUSCANS_SSTAT_SCANNING)
    394 		/* wait until kiu completes keyboard scan */;
    395 #endif
    396 
    397 	vrkiu_scan(kc);
    398 
    399 	return 0;
    400 }
    401 
    402 /*
    403  * console support routine
    404  */
    405 int
    406 vrkiu_cnattach(iot, iobase)
    407 	bus_space_tag_t iot;
    408 	int iobase;
    409 {
    410 	static struct vrkiu_chip vrkiu_consdata_body;
    411 	bus_space_handle_t ioh;
    412 
    413 	if (vrkiu_consdata) {
    414 		panic("vrkiu is already attached as the console");
    415 	}
    416 	if (bus_space_map(iot, iobase, 1, 0, &ioh)) {
    417 		printf("%s(%d): can't map bus space\n", __FILE__, __LINE__);
    418 		return -1;
    419 	}
    420 
    421 	if (vrkiu_init(&vrkiu_consdata_body, iot, ioh) != 0) {
    422 		DPRINTF(("%s(%d): vrkiu_init() failed\n", __FILE__, __LINE__));
    423 		return -1;
    424 	}
    425 	vrkiu_consdata = &vrkiu_consdata_body;
    426 
    427 	hpckbd_cnattach(&vrkiu_consdata_body.kc_if);
    428 
    429 	return (0);
    430 }
    431