Home | History | Annotate | Line # | Download | only in sun
kbd_zs.c revision 1.21
      1 /*	$NetBSD: kbd_zs.c,v 1.21 2006/03/30 16:12:10 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. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)kbd.c	8.2 (Berkeley) 10/30/93
     41  */
     42 
     43 /*
     44  * /dev/kbd lower layer for sun keyboard off a zs channel.
     45  * This driver uses kbdsun middle layer to hook up to /dev/kbd.
     46  */
     47 
     48 /*
     49  * Zilog Z8530 Dual UART driver (keyboard interface)
     50  *
     51  * This is the 8530 portion of the driver that will be attached to
     52  * the "zsc" driver for a Sun keyboard.
     53  */
     54 
     55 #include <sys/cdefs.h>
     56 __KERNEL_RCSID(0, "$NetBSD: kbd_zs.c,v 1.21 2006/03/30 16:12:10 thorpej Exp $");
     57 
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/conf.h>
     61 #include <sys/device.h>
     62 #include <sys/kernel.h>
     63 #include <sys/malloc.h>
     64 #include <sys/proc.h>
     65 #include <sys/signal.h>
     66 #include <sys/signalvar.h>
     67 #include <sys/time.h>
     68 #include <sys/select.h>
     69 #include <sys/syslog.h>
     70 
     71 #include <dev/ic/z8530reg.h>
     72 #include <machine/z8530var.h>
     73 #include <dev/sun/vuid_event.h>
     74 #include <dev/sun/event_var.h>
     75 #include <dev/sun/kbd_reg.h>
     76 #include <dev/sun/kbd_xlate.h>
     77 #include <dev/sun/kbdvar.h>
     78 #include <dev/sun/kbdsunvar.h>
     79 
     80 #if NWSKBD > 0
     81 void kbd_wskbd_attach(struct kbd_softc *k, int isconsole);
     82 #endif
     83 
     84 /****************************************************************
     85  * Interface to the lower layer (zscc)
     86  ****************************************************************/
     87 
     88 static void kbd_zs_rxint(struct zs_chanstate *);
     89 static void kbd_zs_stint(struct zs_chanstate *, int);
     90 static void kbd_zs_txint(struct zs_chanstate *);
     91 static void kbd_zs_softint(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(struct kbd_sun_softc *, int);
    103 
    104 CFATTACH_DECL(kbd_zs, sizeof(struct kbd_sun_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 = device_private(parent);
    135 	struct kbd_sun_softc *k = device_private(self);
    136 	struct zsc_attach_args *args = aux;
    137 	struct zs_chanstate *cs;
    138 	int channel;
    139 	int reset, s;
    140 	int bps;
    141 
    142 	/* provide upper layer with a link to the middle layer */
    143 	k->k_kbd.k_ops = &kbd_ops_sun;
    144 
    145 	/* provide middle layer with a link to the lower layer (i.e. us) */
    146 	channel = args->channel;
    147 	cs = zsc->zsc_cs[channel];
    148 	cs->cs_private = k;
    149 	cs->cs_ops = &zsops_kbd;
    150 	k->k_cs = cs;
    151 	k->k_write_data = kbd_zs_write_data;
    152 
    153 	if ((bps = cs->cs_defspeed) == 0)
    154 		bps = kbd_zs_bps;
    155 
    156 	printf(": baud rate %d", bps);
    157 
    158 	if ((args->hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
    159 		/*
    160 		 * Hookup ourselves as the console input channel
    161 		 */
    162 		struct cons_channel *cc = kbd_cc_alloc(&k->k_kbd);
    163 
    164 		if (cc == NULL)
    165 			return;
    166 
    167 		cons_attach_input(cc, args->consdev);
    168 		k->k_kbd.k_isconsole = 1;
    169 		printf(" (console input)");
    170 	}
    171 	printf("\n");
    172 
    173 	/* Initialize the speed, etc. */
    174 	s = splzs();
    175 	if (k->k_kbd.k_isconsole == 0) {
    176 		/* Not the console; may need reset. */
    177 		reset = (channel == 0) ?
    178 			ZSWR9_A_RESET : ZSWR9_B_RESET;
    179 		zs_write_reg(cs, 9, reset);
    180 	}
    181 	/* These are OK as set by zscc: WR3, WR4, WR5 */
    182 	/* We don't care about status interrupts. */
    183 	cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
    184 	(void) zs_set_speed(cs, bps);
    185 	zs_loadchannelregs(cs);
    186 	splx(s);
    187 
    188 	/* Do this before any calls to kbd_rint(). */
    189 	kbd_xlate_init(&k->k_kbd.k_state);
    190 
    191 	/* Magic sequence. */
    192 	k->k_magic1 = KBD_L1;
    193 	k->k_magic2 = KBD_A;
    194 #if NWSKBD > 0
    195 	kbd_wskbd_attach(&k->k_kbd, k->k_kbd.k_isconsole);
    196 #endif
    197 }
    198 
    199 /*
    200  * used by kbd_sun_start_tx();
    201  */
    202 void
    203 kbd_zs_write_data(k, c)
    204 	struct kbd_sun_softc *k;
    205 	int c;
    206 {
    207 	int	s;
    208 
    209 	/* Need splzs to avoid interruption of the delay. */
    210 	s = splzs();
    211 	zs_write_data(k->k_cs, c);
    212 	splx(s);
    213 }
    214 
    215 static void
    216 kbd_zs_rxint(cs)
    217 	struct zs_chanstate *cs;
    218 {
    219 	struct kbd_sun_softc *k;
    220 	int put, put_next;
    221 	u_char c, rr1;
    222 
    223 	k = cs->cs_private;
    224 	put = k->k_rbput;
    225 
    226 	/*
    227 	 * First read the status, because reading the received char
    228 	 * destroys the status of this char.
    229 	 */
    230 	rr1 = zs_read_reg(cs, 1);
    231 	c = zs_read_data(cs);
    232 
    233 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    234 		/* Clear the receive error. */
    235 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    236 	}
    237 
    238 	/*
    239 	 * Check NOW for a console abort sequence, so that we can
    240 	 * abort even when interrupts are locking up the machine.
    241 	 */
    242 	if (k->k_magic1_down) {
    243 		/* The last keycode was "MAGIC1" down. */
    244 		k->k_magic1_down = 0;
    245 		if (c == k->k_magic2) {
    246 			/* Magic "L1-A" sequence; enter debugger. */
    247 			if (k->k_kbd.k_isconsole) {
    248 				zs_abort(cs);
    249 				/* Debugger done.  Fake L1-up to finish it. */
    250 				c = k->k_magic1 | KBD_UP;
    251 			} else {
    252 				printf("kbd: magic sequence, but not console\n");
    253 			}
    254 		}
    255 	}
    256 	if (c == k->k_magic1) {
    257 		k->k_magic1_down = 1;
    258 	}
    259 
    260 	k->k_rbuf[put] = (c << 8) | rr1;
    261 	put_next = (put + 1) & KBD_RX_RING_MASK;
    262 
    263 	/* Would overrun if increment makes (put==get). */
    264 	if (put_next == k->k_rbget) {
    265 		k->k_intr_flags |= INTR_RX_OVERRUN;
    266 	} else {
    267 		/* OK, really increment. */
    268 		put = put_next;
    269 	}
    270 
    271 	/* Done reading. */
    272 	k->k_rbput = put;
    273 
    274 	/* Ask for softint() call. */
    275 	cs->cs_softreq = 1;
    276 }
    277 
    278 
    279 static void
    280 kbd_zs_txint(cs)
    281 	struct zs_chanstate *cs;
    282 {
    283 	struct kbd_sun_softc *k;
    284 
    285 	k = cs->cs_private;
    286 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    287 	k->k_intr_flags |= INTR_TX_EMPTY;
    288 	/* Ask for softint() call. */
    289 	cs->cs_softreq = 1;
    290 }
    291 
    292 
    293 static void
    294 kbd_zs_stint(cs, force)
    295 	struct zs_chanstate *cs;
    296 	int force;
    297 {
    298 	struct kbd_sun_softc *k;
    299 	int rr0;
    300 
    301 	k = cs->cs_private;
    302 
    303 	rr0 = zs_read_csr(cs);
    304 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    305 
    306 #if 0
    307 	if (rr0 & ZSRR0_BREAK) {
    308 		/* Keyboard unplugged? */
    309 		zs_abort(cs);
    310 		return (0);
    311 	}
    312 #endif
    313 
    314 	/*
    315 	 * We have to accumulate status line changes here.
    316 	 * Otherwise, if we get multiple status interrupts
    317 	 * before the softint runs, we could fail to notice
    318 	 * some status line changes in the softint routine.
    319 	 * Fix from Bill Studenmund, October 1996.
    320 	 */
    321 	cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
    322 	cs->cs_rr0 = rr0;
    323 	k->k_intr_flags |= INTR_ST_CHECK;
    324 
    325 	/* Ask for softint() call. */
    326 	cs->cs_softreq = 1;
    327 }
    328 
    329 /*
    330  * Get input from the receive ring and pass it on.
    331  * Note: this is called at splsoftclock()
    332  */
    333 static void
    334 kbd_zs_softint(cs)
    335 	struct zs_chanstate *cs;
    336 {
    337 	struct kbd_sun_softc *k;
    338 	int get, c, s;
    339 	int intr_flags;
    340 	u_short ring_data;
    341 
    342 	k = cs->cs_private;
    343 
    344 	/* Atomically get and clear flags. */
    345 	s = splzs();
    346 	intr_flags = k->k_intr_flags;
    347 	k->k_intr_flags = 0;
    348 
    349 	/* Now lower to spltty for the rest. */
    350 	(void) spltty();
    351 
    352 	/*
    353 	 * Copy data from the receive ring to the event layer.
    354 	 */
    355 	get = k->k_rbget;
    356 	while (get != k->k_rbput) {
    357 		ring_data = k->k_rbuf[get];
    358 		get = (get + 1) & KBD_RX_RING_MASK;
    359 
    360 		/* low byte of ring_data is rr1 */
    361 		c = (ring_data >> 8) & 0xff;
    362 
    363 		if (ring_data & ZSRR1_DO)
    364 			intr_flags |= INTR_RX_OVERRUN;
    365 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
    366 			/*
    367 			 * After garbage, flush pending input, and
    368 			 * send a reset to resync key translation.
    369 			 */
    370 			log(LOG_ERR, "%s: input error (0x%x)\n",
    371 				k->k_kbd.k_dev.dv_xname, ring_data);
    372 			get = k->k_rbput; /* flush */
    373 			goto send_reset;
    374 		}
    375 
    376 		/* Pass this up to the "middle" layer. */
    377 		kbd_sun_input(k, c);
    378 	}
    379 	if (intr_flags & INTR_RX_OVERRUN) {
    380 		log(LOG_ERR, "%s: input overrun\n",
    381 		    k->k_kbd.k_dev.dv_xname);
    382 	send_reset:
    383 		/* Send a reset to resync translation. */
    384 		kbd_sun_output(k, KBD_CMD_RESET);
    385 		kbd_sun_start_tx(k);
    386 	}
    387 	k->k_rbget = get;
    388 
    389 	if (intr_flags & INTR_TX_EMPTY) {
    390 		/*
    391 		 * Transmit done.  Try to send more, or
    392 		 * clear busy and wakeup drain waiters.
    393 		 */
    394 		k->k_txflags &= ~K_TXBUSY;
    395 		kbd_sun_start_tx(k);
    396 	}
    397 
    398 	if (intr_flags & INTR_ST_CHECK) {
    399 		/*
    400 		 * Status line change.  (Not expected.)
    401 		 */
    402 		log(LOG_ERR, "%s: status interrupt?\n",
    403 		    k->k_kbd.k_dev.dv_xname);
    404 		cs->cs_rr0_delta = 0;
    405 	}
    406 
    407 	splx(s);
    408 }
    409