Home | History | Annotate | Line # | Download | only in vr
vrkiu.c revision 1.29
      1 /*	$NetBSD: vrkiu.c,v 1.29 2001/09/16 05:32:21 uch 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 static int vrkiumatch(struct device *, struct cfdata *, void *);
     80 static void vrkiuattach(struct device *, struct device *, void *);
     81 
     82 int vrkiu_intr(void *);
     83 
     84 static int vrkiu_init(struct vrkiu_chip *, bus_space_tag_t, bus_space_handle_t);
     85 static void vrkiu_write(struct vrkiu_chip *, int, unsigned short);
     86 static unsigned short vrkiu_read(struct vrkiu_chip *, int);
     87 static int vrkiu_is_console(bus_space_tag_t, bus_space_handle_t);
     88 static void vrkiu_scan(struct vrkiu_chip *);
     89 static int countbits(int);
     90 static void eliminate_phantom_keys(struct vrkiu_chip *, unsigned short *);
     91 static int vrkiu_poll(void*);
     92 static int vrkiu_input_establish(void*, struct hpckbd_if*);
     93 
     94 struct cfattach vrkiu_ca = {
     95 	sizeof(struct vrkiu_softc), vrkiumatch, vrkiuattach
     96 };
     97 
     98 struct vrkiu_chip *vrkiu_consdata = NULL;
     99 
    100 static inline void
    101 vrkiu_write(struct vrkiu_chip *chip, int port, unsigned short val)
    102 {
    103 
    104 	bus_space_write_2(chip->kc_iot, chip->kc_ioh, port, val);
    105 }
    106 
    107 static inline unsigned short
    108 vrkiu_read(struct vrkiu_chip *chip, int port)
    109 {
    110 
    111 	return (bus_space_read_2(chip->kc_iot, chip->kc_ioh, port));
    112 }
    113 
    114 static inline int
    115 vrkiu_is_console(bus_space_tag_t iot, bus_space_handle_t ioh)
    116 {
    117 
    118 	if (vrkiu_consdata &&
    119 	    vrkiu_consdata->kc_iot == iot &&
    120 	    vrkiu_consdata->kc_ioh == ioh) {
    121 		return (1);
    122 	} else {
    123 		return (0);
    124 	}
    125 }
    126 
    127 static int
    128 vrkiumatch(struct device *parent, struct cfdata *cf, void *aux)
    129 {
    130 
    131 	return (1);
    132 }
    133 
    134 static void
    135 vrkiuattach(struct device *parent, struct device *self, void *aux)
    136 {
    137 	struct vrkiu_softc *sc = (struct vrkiu_softc *)self;
    138 	struct vrip_attach_args *va = aux;
    139 	struct hpckbd_attach_args haa;
    140 	int isconsole;
    141 
    142 	bus_space_tag_t iot = va->va_iot;
    143 	bus_space_handle_t ioh;
    144 
    145 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    146 		printf(": can't map bus space\n");
    147 		return;
    148 	}
    149 
    150 	isconsole = vrkiu_is_console(iot, ioh);
    151 	if (isconsole) {
    152 		sc->sc_chip = vrkiu_consdata;
    153 	} else {
    154 		sc->sc_chip = &sc->sc_chip_body;
    155 		vrkiu_init(sc->sc_chip, iot, ioh);
    156 	}
    157 	sc->sc_chip->kc_sc = sc;
    158 
    159 	if (!(sc->sc_handler =
    160 	    vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
    161 		vrkiu_intr, sc))) {
    162 		printf (": can't map interrupt line.\n");
    163 		return;
    164 	}
    165 	/* Level2 register setting */
    166 	vrip_intr_setmask2(va->va_vc, sc->sc_handler, KIUINT_KDATRDY, 1);
    167 
    168 	printf("\n");
    169 
    170 	/* attach hpckbd */
    171 	haa.haa_ic = &sc->sc_chip->kc_if;
    172 	config_found(self, &haa, hpckbd_print);
    173 }
    174 
    175 /*
    176  * initialize device
    177  */
    178 static int
    179 vrkiu_init(struct vrkiu_chip *chip, bus_space_tag_t iot,
    180     bus_space_handle_t ioh)
    181 {
    182 
    183 	memset(chip, 0, sizeof(struct vrkiu_chip));
    184 	chip->kc_iot = iot;
    185 	chip->kc_ioh = ioh;
    186 	chip->kc_enabled = 0;
    187 
    188 	chip->kc_if.hii_ctx		= chip;
    189 	chip->kc_if.hii_establish	= vrkiu_input_establish;
    190 	chip->kc_if.hii_poll		= vrkiu_poll;
    191 
    192 	/* set KIU */
    193 	vrkiu_write(chip, KIURST, 1);   /* reset */
    194 	vrkiu_write(chip, KIUSCANLINE, 0); /* 96keys */
    195 	vrkiu_write(chip, KIUWKS, 0x18a4); /* XXX: scan timing! */
    196 	vrkiu_write(chip, KIUWKI, 450);
    197 	vrkiu_write(chip, KIUSCANREP, 0x8023);
    198 	/* KEYEN | STPREP = 2 | ATSTP | ATSCAN */
    199 
    200 	return (0);
    201 }
    202 
    203 int
    204 vrkiu_intr(void *arg)
    205 {
    206         struct vrkiu_softc *sc = arg;
    207 
    208 	/* When key scan finisshed, this entry is called. */
    209 #if 0
    210 	DPRINTF(("vrkiu_intr: intr=%x scan=%x\n",
    211 	    vrkiu_read(sc->sc_chip, KIUINT) & 7,
    212 	    vrkiu_read(sc->sc_chip, KIUSCANS) & KIUSCANS_SSTAT_MASK));
    213 #endif
    214 
    215 	/*
    216 	 * First, we must clear the interrupt register because
    217 	 * vrkiu_scan() may takes long time if a bitmap screen
    218 	 * scrolls up and it makes us to miss some key release
    219 	 * event.
    220 	 */
    221 	vrkiu_write(sc->sc_chip, KIUINT, 0x7); /* Clear all interrupt */
    222 
    223 #if 1
    224 	/* just return if kiu is scanning keyboard. */
    225 	if ((vrkiu_read(sc->sc_chip, KIUSCANS) & KIUSCANS_SSTAT_MASK) ==
    226 	    KIUSCANS_SSTAT_SCANNING)
    227 		return (0);
    228 #endif
    229 	vrkiu_scan(sc->sc_chip);
    230 
    231 	return (0);
    232 }
    233 
    234 static int
    235 countbits(int d)
    236 {
    237 	int i, n;
    238 
    239 	for (i = 0, n = 0; i < NBBY; i++)
    240 		if (d & (1 << i))
    241 			n++;
    242 	return (n);
    243 }
    244 
    245 static void
    246 eliminate_phantom_keys(struct vrkiu_chip *chip, unsigned short *scandata)
    247 {
    248 	unsigned char inkey[KIU_NSCANLINE], *prevkey, *reskey;
    249 	int i, j;
    250 #ifdef VRKIUDEBUG
    251 	int modified = 0;
    252 	static int prevmod = 0;
    253 #endif
    254 
    255 	reskey = (unsigned char *)scandata;
    256 	for (i = 0; i < KIU_NSCANLINE; i++)
    257 		inkey[i] = reskey[i];
    258 	prevkey = (unsigned char *)chip->kc_scandata;
    259 
    260 	for (i = 0; i < KIU_NSCANLINE; i++) {
    261 		if (countbits(inkey[i]) > 1) {
    262 			for (j = 0; j < KIU_NSCANLINE; j++) {
    263 				if (i != j && (inkey[i] & inkey[j])) {
    264 #ifdef VRKIUDEBUG
    265 					modified = 1;
    266 					if (!prevmod) {
    267 						DPRINTF(("vrkiu_scan: %x:%02x->%02x",
    268 						    i, inkey[i], prevkey[i]));
    269 						DPRINTF(("  %x:%02x->%02x\n",
    270 						    j, inkey[j], prevkey[j]));
    271 					}
    272 #endif
    273 					reskey[i] = prevkey[i];
    274 					reskey[j] = prevkey[j];
    275 				}
    276 			}
    277 		}
    278 	}
    279 #ifdef VRKIUDEBUG
    280 	prevmod = modified;
    281 #endif
    282 }
    283 
    284 static void
    285 vrkiu_scan(struct vrkiu_chip* chip)
    286 {
    287 	int i, j, modified, mask;
    288 	unsigned short scandata[KIU_NSCANLINE/2];
    289 
    290 	if (!chip->kc_enabled)
    291 		return;
    292 
    293 	for (i = 0; i < KIU_NSCANLINE / 2; i++) {
    294 		scandata[i] = vrkiu_read(chip, KIUDATP + i * 2);
    295 	}
    296 	eliminate_phantom_keys(chip, scandata);
    297 
    298 	for (i = 0; i < KIU_NSCANLINE / 2; i++) {
    299 		modified = scandata[i] ^ chip->kc_scandata[i];
    300 		chip->kc_scandata[i] = scandata[i];
    301 		mask = 1;
    302 		for (j = 0; j < 16; j++, mask <<= 1) {
    303 			/*
    304 			 * Simultaneous keypresses are resolved by registering
    305 			 * the one with the lowest bit index first.
    306 			 */
    307 			if (modified & mask) {
    308 				int key = i * 16 + j;
    309 				DPRINTF(("vrkiu_scan: %s(%d,%d)\n",
    310 				    (scandata[i] & mask) ? "down" : "up",
    311 				    i, j));
    312 				hpckbd_input(chip->kc_hpckbd,
    313 				    (scandata[i] & mask), key);
    314 			}
    315 		}
    316 	}
    317 }
    318 
    319 /* called from bicons.c */
    320 int
    321 vrkiu_getc()
    322 {
    323 	static int flag = 1;
    324 
    325 	/*
    326 	 * XXX, currently
    327 	 */
    328 	if (flag) {
    329 		flag = 0;
    330 		printf("%s(%d): vrkiu_getc() is not implemented\n",
    331 		    __FILE__, __LINE__);
    332 	}
    333 	return (0);
    334 }
    335 
    336 /*
    337  * hpckbd interface routines
    338  */
    339 int
    340 vrkiu_input_establish(void *ic, struct hpckbd_if *kbdif)
    341 {
    342 	struct vrkiu_chip *kc = ic;
    343 
    344 	/* save hpckbd interface */
    345 	kc->kc_hpckbd = kbdif;
    346 
    347 	kc->kc_enabled = 1;
    348 
    349 	return (0);
    350 }
    351 
    352 int
    353 vrkiu_poll(void *ic)
    354 {
    355 	struct vrkiu_chip *kc = ic;
    356 
    357 #if 1
    358 	/* wait until kiu completes keyboard scan. */
    359 	while ((vrkiu_read(kc, KIUSCANS) & KIUSCANS_SSTAT_MASK) ==
    360 	    KIUSCANS_SSTAT_SCANNING)
    361 		/* wait until kiu completes keyboard scan */;
    362 #endif
    363 
    364 	vrkiu_scan(kc);
    365 
    366 	return (0);
    367 }
    368 
    369 /*
    370  * console support routine
    371  */
    372 int
    373 vrkiu_cnattach(bus_space_tag_t iot, int iobase)
    374 {
    375 	static struct vrkiu_chip vrkiu_consdata_body;
    376 	bus_space_handle_t ioh;
    377 
    378 	if (vrkiu_consdata) {
    379 		panic("vrkiu is already attached as the console");
    380 	}
    381 	if (bus_space_map(iot, iobase, 1, 0, &ioh)) {
    382 		printf("%s(%d): can't map bus space\n", __FILE__, __LINE__);
    383 		return (-1);
    384 	}
    385 
    386 	if (vrkiu_init(&vrkiu_consdata_body, iot, ioh) != 0) {
    387 		DPRINTF(("%s(%d): vrkiu_init() failed\n", __FILE__, __LINE__));
    388 		return (-1);
    389 	}
    390 	vrkiu_consdata = &vrkiu_consdata_body;
    391 
    392 	hpckbd_cnattach(&vrkiu_consdata_body.kc_if);
    393 
    394 	return (0);
    395 }
    396