Home | History | Annotate | Line # | Download | only in sun
kbd_zs.c revision 1.8
      1 /*	$NetBSD: kbd_zs.c,v 1.8 2001/09/28 14:34:49 pk Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by the University of
     27  *	California, Berkeley and its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *	@(#)kbd.c	8.2 (Berkeley) 10/30/93
     45  */
     46 
     47 /*
     48  * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
     49  * [yet?]).  Translates incoming bytes to ASCII or to `firm_events' and
     50  * passes them up to the appropriate reader.
     51  */
     52 
     53 /*
     54  * Zilog Z8530 Dual UART driver (keyboard interface)
     55  *
     56  * This is the 8530 portion of the driver that will be attached to
     57  * the "zsc" driver for a Sun keyboard.
     58  */
     59 
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/conf.h>
     63 #include <sys/device.h>
     64 #include <sys/kernel.h>
     65 #include <sys/malloc.h>
     66 #include <sys/proc.h>
     67 #include <sys/signal.h>
     68 #include <sys/signalvar.h>
     69 #include <sys/time.h>
     70 #include <sys/select.h>
     71 #include <sys/syslog.h>
     72 
     73 #include <dev/ic/z8530reg.h>
     74 #include <machine/z8530var.h>
     75 #include <machine/vuid_event.h>
     76 #include <machine/kbd.h>
     77 #include <dev/sun/event_var.h>
     78 #include <dev/sun/kbd_xlate.h>
     79 #include <dev/sun/kbdvar.h>
     80 
     81 /****************************************************************
     82  * Interface to the lower layer (zscc)
     83  ****************************************************************/
     84 
     85 static void kbd_zs_rxint __P((struct zs_chanstate *));
     86 static void kbd_zs_stint __P((struct zs_chanstate *, int));
     87 static void kbd_zs_txint __P((struct zs_chanstate *));
     88 static void kbd_zs_softint __P((struct zs_chanstate *));
     89 
     90 struct zsops zsops_kbd = {
     91 	kbd_zs_rxint,	/* receive char available */
     92 	kbd_zs_stint,	/* external/status */
     93 	kbd_zs_txint,	/* xmit buffer empty */
     94 	kbd_zs_softint,	/* process software interrupt */
     95 };
     96 
     97 static int	kbd_zs_match(struct device *, struct cfdata *, void *);
     98 static void	kbd_zs_attach(struct device *, struct device *, void *);
     99 static void	kbd_zs_write_data __P((struct kbd_softc *, int));
    100 
    101 struct cfattach kbd_zs_ca = {
    102 	sizeof(struct kbd_softc), kbd_zs_match, kbd_zs_attach
    103 };
    104 
    105 /* Fall-back baud rate */
    106 int	kbd_zs_bps = KBD_BPS;
    107 
    108 /*
    109  * kbd_zs_match: how is this zs channel configured?
    110  */
    111 int
    112 kbd_zs_match(parent, cf, aux)
    113 	struct device *parent;
    114 	struct cfdata *cf;
    115 	void   *aux;
    116 {
    117 	struct zsc_attach_args *args = aux;
    118 
    119 	/* Exact match required for keyboard. */
    120 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    121 		return 2;
    122 
    123 	return 0;
    124 }
    125 
    126 void
    127 kbd_zs_attach(parent, self, aux)
    128 	struct device *parent, *self;
    129 	void   *aux;
    130 
    131 {
    132 	struct zsc_softc *zsc = (void *) parent;
    133 	struct kbd_softc *k = (void *) self;
    134 	struct zsc_attach_args *args = aux;
    135 	struct zs_chanstate *cs;
    136 	struct cfdata *cf;
    137 	int channel, kbd_unit;
    138 	int reset, s;
    139 	int bps;
    140 
    141 	cf = k->k_dev.dv_cfdata;
    142 	kbd_unit = k->k_dev.dv_unit;
    143 	channel = args->channel;
    144 	cs = zsc->zsc_cs[channel];
    145 	cs->cs_private = k;
    146 	cs->cs_ops = &zsops_kbd;
    147 	k->k_cs = cs;
    148 	k->k_write_data = kbd_zs_write_data;
    149 	if ((bps = cs->cs_defspeed) == 0)
    150 		bps = kbd_zs_bps;
    151 
    152 	printf(": baud rate %d", bps);
    153 
    154 	if ((args->hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
    155 		/*
    156 		 * Hookup ourselves as the console input channel
    157 		 */
    158 		struct cons_channel *cc;
    159 
    160 		if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
    161 			return;
    162 
    163 		cc->cc_dev = self;
    164 		cc->cc_iopen = kbd_cc_open;
    165 		cc->cc_iclose = kbd_cc_close;
    166 		cc->cc_upstream = NULL;
    167 		cons_attach_input(cc, args->consdev);
    168 		k->k_cc = cc;
    169 		k->k_isconsole = 1;
    170 		printf(" (console input)");
    171 	}
    172 	printf("\n");
    173 
    174 	callout_init(&k->k_repeat_ch);
    175 
    176 	/* Initialize the speed, etc. */
    177 	s = splzs();
    178 	if (k->k_isconsole == 0) {
    179 		/* Not the console; may need reset. */
    180 		reset = (channel == 0) ?
    181 			ZSWR9_A_RESET : ZSWR9_B_RESET;
    182 		zs_write_reg(cs, 9, reset);
    183 	}
    184 	/* These are OK as set by zscc: WR3, WR4, WR5 */
    185 	/* We don't care about status interrupts. */
    186 	cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
    187 	(void) zs_set_speed(cs, bps);
    188 	zs_loadchannelregs(cs);
    189 	splx(s);
    190 
    191 	/* Do this before any calls to kbd_rint(). */
    192 	kbd_xlate_init(&k->k_state);
    193 
    194 	/* XXX - Do this in open? */
    195 	k->k_repeat_start = hz/2;
    196 	k->k_repeat_step = hz/20;
    197 
    198 	/* Magic sequence. */
    199 	k->k_magic1 = KBD_L1;
    200 	k->k_magic2 = KBD_A;
    201 }
    202 
    203 /*
    204  * used by kbd_start_tx();
    205  */
    206 void
    207 kbd_zs_write_data(k, c)
    208 	struct kbd_softc *k;
    209 	int c;
    210 {
    211 	int	s;
    212 
    213 	/* Need splzs to avoid interruption of the delay. */
    214 	s = splzs();
    215 	zs_write_data(k->k_cs, c);
    216 	splx(s);
    217 }
    218 
    219 static void
    220 kbd_zs_rxint(cs)
    221 	struct zs_chanstate *cs;
    222 {
    223 	struct kbd_softc *k;
    224 	int put, put_next;
    225 	u_char c, rr1;
    226 
    227 	k = cs->cs_private;
    228 	put = k->k_rbput;
    229 
    230 	/*
    231 	 * First read the status, because reading the received char
    232 	 * destroys the status of this char.
    233 	 */
    234 	rr1 = zs_read_reg(cs, 1);
    235 	c = zs_read_data(cs);
    236 
    237 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    238 		/* Clear the receive error. */
    239 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    240 	}
    241 
    242 	/*
    243 	 * Check NOW for a console abort sequence, so that we can
    244 	 * abort even when interrupts are locking up the machine.
    245 	 */
    246 	if (k->k_magic1_down) {
    247 		/* The last keycode was "MAGIC1" down. */
    248 		k->k_magic1_down = 0;
    249 		if (c == k->k_magic2) {
    250 			/* Magic "L1-A" sequence; enter debugger. */
    251 			if (k->k_isconsole) {
    252 				zs_abort(cs);
    253 				/* Debugger done.  Fake L1-up to finish it. */
    254 				c = k->k_magic1 | KBD_UP;
    255 			} else {
    256 				printf("kbd: magic sequence, but not console\n");
    257 			}
    258 		}
    259 	}
    260 	if (c == k->k_magic1) {
    261 		k->k_magic1_down = 1;
    262 	}
    263 
    264 	k->k_rbuf[put] = (c << 8) | rr1;
    265 	put_next = (put + 1) & KBD_RX_RING_MASK;
    266 
    267 	/* Would overrun if increment makes (put==get). */
    268 	if (put_next == k->k_rbget) {
    269 		k->k_intr_flags |= INTR_RX_OVERRUN;
    270 	} else {
    271 		/* OK, really increment. */
    272 		put = put_next;
    273 	}
    274 
    275 	/* Done reading. */
    276 	k->k_rbput = put;
    277 
    278 	/* Ask for softint() call. */
    279 	cs->cs_softreq = 1;
    280 }
    281 
    282 
    283 static void
    284 kbd_zs_txint(cs)
    285 	struct zs_chanstate *cs;
    286 {
    287 	struct kbd_softc *k;
    288 
    289 	k = cs->cs_private;
    290 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    291 	k->k_intr_flags |= INTR_TX_EMPTY;
    292 	/* Ask for softint() call. */
    293 	cs->cs_softreq = 1;
    294 }
    295 
    296 
    297 static void
    298 kbd_zs_stint(cs, force)
    299 	struct zs_chanstate *cs;
    300 	int force;
    301 {
    302 	struct kbd_softc *k;
    303 	int rr0;
    304 
    305 	k = cs->cs_private;
    306 
    307 	rr0 = zs_read_csr(cs);
    308 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    309 
    310 #if 0
    311 	if (rr0 & ZSRR0_BREAK) {
    312 		/* Keyboard unplugged? */
    313 		zs_abort(cs);
    314 		return (0);
    315 	}
    316 #endif
    317 
    318 	/*
    319 	 * We have to accumulate status line changes here.
    320 	 * Otherwise, if we get multiple status interrupts
    321 	 * before the softint runs, we could fail to notice
    322 	 * some status line changes in the softint routine.
    323 	 * Fix from Bill Studenmund, October 1996.
    324 	 */
    325 	cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
    326 	cs->cs_rr0 = rr0;
    327 	k->k_intr_flags |= INTR_ST_CHECK;
    328 
    329 	/* Ask for softint() call. */
    330 	cs->cs_softreq = 1;
    331 }
    332 
    333 /*
    334  * Get input from the receive ring and pass it on.
    335  * Note: this is called at splsoftclock()
    336  */
    337 static void
    338 kbd_zs_softint(cs)
    339 	struct zs_chanstate *cs;
    340 {
    341 	struct kbd_softc *k;
    342 	int get, c, s;
    343 	int intr_flags;
    344 	u_short ring_data;
    345 
    346 	k = cs->cs_private;
    347 
    348 	/* Atomically get and clear flags. */
    349 	s = splzs();
    350 	intr_flags = k->k_intr_flags;
    351 	k->k_intr_flags = 0;
    352 
    353 	/* Now lower to spltty for the rest. */
    354 	(void) spltty();
    355 
    356 	/*
    357 	 * Copy data from the receive ring to the event layer.
    358 	 */
    359 	get = k->k_rbget;
    360 	while (get != k->k_rbput) {
    361 		ring_data = k->k_rbuf[get];
    362 		get = (get + 1) & KBD_RX_RING_MASK;
    363 
    364 		/* low byte of ring_data is rr1 */
    365 		c = (ring_data >> 8) & 0xff;
    366 
    367 		if (ring_data & ZSRR1_DO)
    368 			intr_flags |= INTR_RX_OVERRUN;
    369 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
    370 			/*
    371 			 * After garbage, flush pending input, and
    372 			 * send a reset to resync key translation.
    373 			 */
    374 			log(LOG_ERR, "%s: input error (0x%x)\n",
    375 				k->k_dev.dv_xname, ring_data);
    376 			get = k->k_rbput; /* flush */
    377 			goto send_reset;
    378 		}
    379 
    380 		/* Pass this up to the "middle" layer. */
    381 		kbd_input_raw(k, c);
    382 	}
    383 	if (intr_flags & INTR_RX_OVERRUN) {
    384 		log(LOG_ERR, "%s: input overrun\n",
    385 		    k->k_dev.dv_xname);
    386 	send_reset:
    387 		/* Send a reset to resync translation. */
    388 		kbd_output(k, KBD_CMD_RESET);
    389 		kbd_start_tx(k);
    390 	}
    391 	k->k_rbget = get;
    392 
    393 	if (intr_flags & INTR_TX_EMPTY) {
    394 		/*
    395 		 * Transmit done.  Try to send more, or
    396 		 * clear busy and wakeup drain waiters.
    397 		 */
    398 		k->k_txflags &= ~K_TXBUSY;
    399 		kbd_start_tx(k);
    400 	}
    401 
    402 	if (intr_flags & INTR_ST_CHECK) {
    403 		/*
    404 		 * Status line change.  (Not expected.)
    405 		 */
    406 		log(LOG_ERR, "%s: status interrupt?\n",
    407 		    k->k_dev.dv_xname);
    408 		cs->cs_rr0_delta = 0;
    409 	}
    410 
    411 	splx(s);
    412 }
    413