Home | History | Annotate | Line # | Download | only in sun
kbd_zs.c revision 1.12
      1 /*	$NetBSD: kbd_zs.c,v 1.12 2002/10/01 01:05:50 thorpej 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/cdefs.h>
     61 __KERNEL_RCSID(0, "$NetBSD: kbd_zs.c,v 1.12 2002/10/01 01:05:50 thorpej Exp $");
     62 
     63 #include <sys/param.h>
     64 #include <sys/systm.h>
     65 #include <sys/conf.h>
     66 #include <sys/device.h>
     67 #include <sys/kernel.h>
     68 #include <sys/malloc.h>
     69 #include <sys/proc.h>
     70 #include <sys/signal.h>
     71 #include <sys/signalvar.h>
     72 #include <sys/time.h>
     73 #include <sys/select.h>
     74 #include <sys/syslog.h>
     75 
     76 #include <dev/ic/z8530reg.h>
     77 #include <machine/z8530var.h>
     78 #include <machine/vuid_event.h>
     79 #include <machine/kbd.h>
     80 #include <dev/sun/event_var.h>
     81 #include <dev/sun/kbd_xlate.h>
     82 #include <dev/sun/kbdvar.h>
     83 
     84 /****************************************************************
     85  * Interface to the lower layer (zscc)
     86  ****************************************************************/
     87 
     88 static void kbd_zs_rxint __P((struct zs_chanstate *));
     89 static void kbd_zs_stint __P((struct zs_chanstate *, int));
     90 static void kbd_zs_txint __P((struct zs_chanstate *));
     91 static void kbd_zs_softint __P((struct zs_chanstate *));
     92 
     93 struct zsops zsops_kbd = {
     94 	kbd_zs_rxint,	/* receive char available */
     95 	kbd_zs_stint,	/* external/status */
     96 	kbd_zs_txint,	/* xmit buffer empty */
     97 	kbd_zs_softint,	/* process software interrupt */
     98 };
     99 
    100 static int	kbd_zs_match(struct device *, struct cfdata *, void *);
    101 static void	kbd_zs_attach(struct device *, struct device *, void *);
    102 static void	kbd_zs_write_data __P((struct kbd_softc *, int));
    103 
    104 CFATTACH_DECL(kbd_zs, sizeof(struct kbd_softc),
    105     kbd_zs_match, kbd_zs_attach, NULL, NULL)
    106 
    107 /* Fall-back baud rate */
    108 int	kbd_zs_bps = KBD_DEFAULT_BPS;
    109 
    110 /*
    111  * kbd_zs_match: how is this zs channel configured?
    112  */
    113 int
    114 kbd_zs_match(parent, cf, aux)
    115 	struct device *parent;
    116 	struct cfdata *cf;
    117 	void   *aux;
    118 {
    119 	struct zsc_attach_args *args = aux;
    120 
    121 	/* Exact match required for keyboard. */
    122 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    123 		return 2;
    124 
    125 	return 0;
    126 }
    127 
    128 void
    129 kbd_zs_attach(parent, self, aux)
    130 	struct device *parent, *self;
    131 	void   *aux;
    132 
    133 {
    134 	struct zsc_softc *zsc = (void *) parent;
    135 	struct kbd_softc *k = (void *) self;
    136 	struct zsc_attach_args *args = aux;
    137 	struct zs_chanstate *cs;
    138 	struct cfdata *cf;
    139 	int channel, kbd_unit;
    140 	int reset, s;
    141 	int bps;
    142 
    143 	cf = k->k_dev.dv_cfdata;
    144 	kbd_unit = k->k_dev.dv_unit;
    145 	channel = args->channel;
    146 	cs = zsc->zsc_cs[channel];
    147 	cs->cs_private = k;
    148 	cs->cs_ops = &zsops_kbd;
    149 	k->k_cs = cs;
    150 	k->k_write_data = kbd_zs_write_data;
    151 	if ((bps = cs->cs_defspeed) == 0)
    152 		bps = kbd_zs_bps;
    153 
    154 	printf(": baud rate %d", bps);
    155 
    156 	if ((args->hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
    157 		/*
    158 		 * Hookup ourselves as the console input channel
    159 		 */
    160 		struct cons_channel *cc;
    161 
    162 		if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
    163 			return;
    164 
    165 		cc->cc_dev = self;
    166 		cc->cc_iopen = kbd_cc_open;
    167 		cc->cc_iclose = kbd_cc_close;
    168 		cc->cc_upstream = NULL;
    169 		cons_attach_input(cc, args->consdev);
    170 		k->k_cc = cc;
    171 		k->k_isconsole = 1;
    172 		printf(" (console input)");
    173 	}
    174 	printf("\n");
    175 
    176 	callout_init(&k->k_repeat_ch);
    177 
    178 	/* Initialize the speed, etc. */
    179 	s = splzs();
    180 	if (k->k_isconsole == 0) {
    181 		/* Not the console; may need reset. */
    182 		reset = (channel == 0) ?
    183 			ZSWR9_A_RESET : ZSWR9_B_RESET;
    184 		zs_write_reg(cs, 9, reset);
    185 	}
    186 	/* These are OK as set by zscc: WR3, WR4, WR5 */
    187 	/* We don't care about status interrupts. */
    188 	cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
    189 	(void) zs_set_speed(cs, bps);
    190 	zs_loadchannelregs(cs);
    191 	splx(s);
    192 
    193 	/* Do this before any calls to kbd_rint(). */
    194 	kbd_xlate_init(&k->k_state);
    195 
    196 	/* XXX - Do this in open? */
    197 	k->k_repeat_start = hz/2;
    198 	k->k_repeat_step = hz/20;
    199 
    200 	/* Magic sequence. */
    201 	k->k_magic1 = KBD_L1;
    202 	k->k_magic2 = KBD_A;
    203 }
    204 
    205 /*
    206  * used by kbd_start_tx();
    207  */
    208 void
    209 kbd_zs_write_data(k, c)
    210 	struct kbd_softc *k;
    211 	int c;
    212 {
    213 	int	s;
    214 
    215 	/* Need splzs to avoid interruption of the delay. */
    216 	s = splzs();
    217 	zs_write_data(k->k_cs, c);
    218 	splx(s);
    219 }
    220 
    221 static void
    222 kbd_zs_rxint(cs)
    223 	struct zs_chanstate *cs;
    224 {
    225 	struct kbd_softc *k;
    226 	int put, put_next;
    227 	u_char c, rr1;
    228 
    229 	k = cs->cs_private;
    230 	put = k->k_rbput;
    231 
    232 	/*
    233 	 * First read the status, because reading the received char
    234 	 * destroys the status of this char.
    235 	 */
    236 	rr1 = zs_read_reg(cs, 1);
    237 	c = zs_read_data(cs);
    238 
    239 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    240 		/* Clear the receive error. */
    241 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    242 	}
    243 
    244 	/*
    245 	 * Check NOW for a console abort sequence, so that we can
    246 	 * abort even when interrupts are locking up the machine.
    247 	 */
    248 	if (k->k_magic1_down) {
    249 		/* The last keycode was "MAGIC1" down. */
    250 		k->k_magic1_down = 0;
    251 		if (c == k->k_magic2) {
    252 			/* Magic "L1-A" sequence; enter debugger. */
    253 			if (k->k_isconsole) {
    254 				zs_abort(cs);
    255 				/* Debugger done.  Fake L1-up to finish it. */
    256 				c = k->k_magic1 | KBD_UP;
    257 			} else {
    258 				printf("kbd: magic sequence, but not console\n");
    259 			}
    260 		}
    261 	}
    262 	if (c == k->k_magic1) {
    263 		k->k_magic1_down = 1;
    264 	}
    265 
    266 	k->k_rbuf[put] = (c << 8) | rr1;
    267 	put_next = (put + 1) & KBD_RX_RING_MASK;
    268 
    269 	/* Would overrun if increment makes (put==get). */
    270 	if (put_next == k->k_rbget) {
    271 		k->k_intr_flags |= INTR_RX_OVERRUN;
    272 	} else {
    273 		/* OK, really increment. */
    274 		put = put_next;
    275 	}
    276 
    277 	/* Done reading. */
    278 	k->k_rbput = put;
    279 
    280 	/* Ask for softint() call. */
    281 	cs->cs_softreq = 1;
    282 }
    283 
    284 
    285 static void
    286 kbd_zs_txint(cs)
    287 	struct zs_chanstate *cs;
    288 {
    289 	struct kbd_softc *k;
    290 
    291 	k = cs->cs_private;
    292 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    293 	k->k_intr_flags |= INTR_TX_EMPTY;
    294 	/* Ask for softint() call. */
    295 	cs->cs_softreq = 1;
    296 }
    297 
    298 
    299 static void
    300 kbd_zs_stint(cs, force)
    301 	struct zs_chanstate *cs;
    302 	int force;
    303 {
    304 	struct kbd_softc *k;
    305 	int rr0;
    306 
    307 	k = cs->cs_private;
    308 
    309 	rr0 = zs_read_csr(cs);
    310 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    311 
    312 #if 0
    313 	if (rr0 & ZSRR0_BREAK) {
    314 		/* Keyboard unplugged? */
    315 		zs_abort(cs);
    316 		return (0);
    317 	}
    318 #endif
    319 
    320 	/*
    321 	 * We have to accumulate status line changes here.
    322 	 * Otherwise, if we get multiple status interrupts
    323 	 * before the softint runs, we could fail to notice
    324 	 * some status line changes in the softint routine.
    325 	 * Fix from Bill Studenmund, October 1996.
    326 	 */
    327 	cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
    328 	cs->cs_rr0 = rr0;
    329 	k->k_intr_flags |= INTR_ST_CHECK;
    330 
    331 	/* Ask for softint() call. */
    332 	cs->cs_softreq = 1;
    333 }
    334 
    335 /*
    336  * Get input from the receive ring and pass it on.
    337  * Note: this is called at splsoftclock()
    338  */
    339 static void
    340 kbd_zs_softint(cs)
    341 	struct zs_chanstate *cs;
    342 {
    343 	struct kbd_softc *k;
    344 	int get, c, s;
    345 	int intr_flags;
    346 	u_short ring_data;
    347 
    348 	k = cs->cs_private;
    349 
    350 	/* Atomically get and clear flags. */
    351 	s = splzs();
    352 	intr_flags = k->k_intr_flags;
    353 	k->k_intr_flags = 0;
    354 
    355 	/* Now lower to spltty for the rest. */
    356 	(void) spltty();
    357 
    358 	/*
    359 	 * Copy data from the receive ring to the event layer.
    360 	 */
    361 	get = k->k_rbget;
    362 	while (get != k->k_rbput) {
    363 		ring_data = k->k_rbuf[get];
    364 		get = (get + 1) & KBD_RX_RING_MASK;
    365 
    366 		/* low byte of ring_data is rr1 */
    367 		c = (ring_data >> 8) & 0xff;
    368 
    369 		if (ring_data & ZSRR1_DO)
    370 			intr_flags |= INTR_RX_OVERRUN;
    371 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
    372 			/*
    373 			 * After garbage, flush pending input, and
    374 			 * send a reset to resync key translation.
    375 			 */
    376 			log(LOG_ERR, "%s: input error (0x%x)\n",
    377 				k->k_dev.dv_xname, ring_data);
    378 			get = k->k_rbput; /* flush */
    379 			goto send_reset;
    380 		}
    381 
    382 		/* Pass this up to the "middle" layer. */
    383 		kbd_input_raw(k, c);
    384 	}
    385 	if (intr_flags & INTR_RX_OVERRUN) {
    386 		log(LOG_ERR, "%s: input overrun\n",
    387 		    k->k_dev.dv_xname);
    388 	send_reset:
    389 		/* Send a reset to resync translation. */
    390 		kbd_output(k, KBD_CMD_RESET);
    391 		kbd_start_tx(k);
    392 	}
    393 	k->k_rbget = get;
    394 
    395 	if (intr_flags & INTR_TX_EMPTY) {
    396 		/*
    397 		 * Transmit done.  Try to send more, or
    398 		 * clear busy and wakeup drain waiters.
    399 		 */
    400 		k->k_txflags &= ~K_TXBUSY;
    401 		kbd_start_tx(k);
    402 	}
    403 
    404 	if (intr_flags & INTR_ST_CHECK) {
    405 		/*
    406 		 * Status line change.  (Not expected.)
    407 		 */
    408 		log(LOG_ERR, "%s: status interrupt?\n",
    409 		    k->k_dev.dv_xname);
    410 		cs->cs_rr0_delta = 0;
    411 	}
    412 
    413 	splx(s);
    414 }
    415