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