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