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