Home | History | Annotate | Line # | Download | only in apbus
kb_ap.c revision 1.1
      1 /*	$NetBSD: kb_ap.c,v 1.1 2000/11/15 14:04:05 tsubai Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/param.h>
     30 #include <sys/systm.h>
     31 #include <sys/device.h>
     32 
     33 #include <dev/wscons/wsconsio.h>
     34 #include <dev/wscons/wskbdvar.h>
     35 #include <dev/wscons/wsksymdef.h>
     36 
     37 #include <machine/adrsmap.h>
     38 #include <newsmips/apbus/apbusvar.h>
     39 
     40 struct kbreg {
     41 	u_int kb_rx_data;
     42 	u_int kb_rx_stat;
     43 	u_int kb_rx_intr_en;
     44 	u_int kb_rx_reset;
     45 	u_int kb_rx_speed;
     46 
     47 	u_int ms_rx_data;
     48 	u_int ms_rx_stat;
     49 	u_int ms_rx_intr_en;
     50 	u_int ms_rx_reset;
     51 	u_int ms_rx_speed;
     52 
     53 	u_int kb_buzzf;
     54 	u_int kb_buzz;
     55 
     56 	u_int kb_tx_data;
     57 	u_int kb_tx_stat;
     58 	u_int kb_tx_intr_en;
     59 	u_int kb_tx_reset;
     60 	u_int kb_tx_speed;
     61 };
     62 
     63 struct kb_ap_softc {
     64 	struct device sc_dev;
     65 	volatile struct kbreg *sc_reg;
     66 	struct device *sc_wskbddev;
     67 };
     68 
     69 int kb_ap_match(struct device *, struct cfdata *, void *);
     70 void kb_ap_attach(struct device *, struct device *, void *);
     71 int kb_ap_intr(void *);
     72 
     73 void kb_ap_cnattach(void);
     74 void kb_ap_cngetc(void *, u_int *, int *);
     75 void kb_ap_cnpollc(void *, int);
     76 
     77 int kb_ap_enable(void *, int);
     78 void kb_ap_setleds(void *, int);
     79 int kb_ap_ioctl(void *, u_long, caddr_t, int, struct proc *);
     80 
     81 extern struct wscons_keydesc newskb_keydesctab[];
     82 
     83 struct cfattach kb_ap_ca = {
     84 	sizeof(struct kb_ap_softc), kb_ap_match, kb_ap_attach
     85 };
     86 
     87 struct wskbd_accessops kb_ap_accessops = {
     88 	kb_ap_enable,
     89 	kb_ap_setleds,
     90 	kb_ap_ioctl,
     91 };
     92 
     93 struct wskbd_consops kb_ap_consops = {
     94 	kb_ap_cngetc,
     95 	kb_ap_cnpollc,
     96 };
     97 
     98 struct wskbd_mapdata kb_ap_keymapdata = {
     99 	newskb_keydesctab,
    100 	KB_JP,
    101 };
    102 
    103 int
    104 kb_ap_match(parent, cf, aux)
    105 	struct device *parent;
    106 	struct cfdata *cf;
    107 	void *aux;
    108 {
    109 	struct apbus_attach_args *apa = aux;
    110 
    111 	if (strcmp(apa->apa_name, "kb") == 0)
    112 		return 1;
    113 
    114 	return 0;
    115 }
    116 
    117 void
    118 kb_ap_attach(parent, self, aux)
    119 	struct device *parent, *self;
    120 	void *aux;
    121 {
    122 	struct kb_ap_softc *sc = (void *)self;
    123 	struct apbus_attach_args *apa = aux;
    124 	volatile struct kbreg *reg = (void *)apa->apa_hwbase;
    125 	volatile int *dipsw = (void *)NEWS5000_DIP_SWITCH;
    126 	struct wskbddev_attach_args waa;
    127 	int cons = 0;
    128 
    129 	printf(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
    130 
    131 	sc->sc_reg = reg;
    132 
    133 	if (*dipsw & 7) {
    134 		printf(" (console)");
    135 		cons = 1;
    136 	}
    137 	printf("\n");
    138 
    139 	reg->kb_rx_reset = 0x03;
    140 	reg->kb_tx_reset = 0x03;
    141 
    142 	reg->kb_rx_speed = 0x04;
    143 	reg->kb_tx_speed = 0x04;
    144 
    145 	reg->kb_rx_intr_en = 1;
    146 	reg->kb_tx_intr_en = 0;
    147 
    148 	apbus_intr_establish(1, NEWS5000_INT1_KBD, 0, kb_ap_intr, sc,
    149 	    "", apa->apa_ctlnum);
    150 
    151 	waa.console = cons;
    152 	waa.keymap = &kb_ap_keymapdata;
    153 	waa.accessops = &kb_ap_accessops;
    154 	waa.accesscookie = sc;
    155 
    156 	sc->sc_wskbddev = config_found(self, &waa, wskbddevprint);
    157 }
    158 
    159 int
    160 kb_ap_intr(v)
    161 	void *v;
    162 {
    163 	struct kb_ap_softc *sc = v;
    164 	volatile struct kbreg *reg = sc->sc_reg;
    165 	int key, val, type, release;
    166 	int rv = 0;
    167 
    168 	while (reg->kb_rx_stat & RX_KBRDY) {
    169 		key = reg->kb_rx_data;
    170 		val = key & 0x7f;
    171 		release = key & 0x80;
    172 		type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    173 
    174 		if (sc->sc_wskbddev)
    175 			wskbd_input(sc->sc_wskbddev, type, val);
    176 
    177 		rv = 1;
    178 	}
    179 
    180 	return rv;
    181 }
    182 
    183 void
    184 kb_ap_cnattach()
    185 {
    186 	wskbd_cnattach(&kb_ap_consops, (void *)0xbf900000, &kb_ap_keymapdata);
    187 }
    188 
    189 void
    190 kb_ap_cngetc(v, type, data)
    191 	void *v;
    192 	u_int *type;
    193 	int *data;
    194 {
    195 	volatile struct kbreg *reg = v;
    196 	int key, release, ointr;
    197 
    198 	/* Disable keyboard interrupt. */
    199 	ointr = reg->kb_rx_intr_en;
    200 	reg->kb_rx_intr_en = 0;
    201 
    202 	/* Wait for key data. */
    203 	while ((reg->kb_rx_stat & RX_KBRDY) == 0);
    204 
    205 	key = reg->kb_rx_data;
    206 	release = key & 0x80;
    207 	*data = key & 0x7f;
    208 	*type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    209 
    210 	reg->kb_rx_intr_en = ointr;
    211 }
    212 
    213 void
    214 kb_ap_cnpollc(v, on)
    215 	void *v;
    216 	int on;
    217 {
    218 }
    219 
    220 int
    221 kb_ap_enable(v, on)
    222 	void *v;
    223 	int on;
    224 {
    225 	return 0;
    226 }
    227 
    228 void
    229 kb_ap_setleds(v, on)
    230 	void *v;
    231 	int on;
    232 {
    233 }
    234 
    235 int
    236 kb_ap_ioctl(v, cmd, data, flag, p)
    237 	void *v;
    238 	u_long cmd;
    239 	caddr_t data;
    240 	int flag;
    241 	struct proc *p;
    242 {
    243 	switch (cmd) {
    244 	case WSKBDIO_GTYPE:
    245 		*(int *)data = 0;	/* XXX */
    246 		return 0;
    247 	case WSKBDIO_SETLEDS:
    248 		return 0;
    249 	case WSKBDIO_GETLEDS:
    250 		*(int *)data = 0;
    251 		return 0;
    252 	}
    253 
    254 	return -1;
    255 }
    256