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