xref: /openbsd/sys/arch/sparc64/dev/z8530kbd.c (revision 0f9e9ec2)
1 /*	$OpenBSD: z8530kbd.c,v 1.32 2024/05/13 01:15:50 jsg Exp $	*/
2 /*	$NetBSD: z8530tty.c,v 1.77 2001/05/30 15:24:24 lukem Exp $	*/
3 
4 /*-
5  * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998, 1999
6  *	Charles M. Hannum.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Charles M. Hannum.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1994 Gordon W. Ross
36  * Copyright (c) 1992, 1993
37  *	The Regents of the University of California.  All rights reserved.
38  *
39  * This software was developed by the Computer Systems Engineering group
40  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
41  * contributed to Berkeley.
42  *
43  * All advertising materials mentioning features or use of this software
44  * must display the following acknowledgement:
45  *	This product includes software developed by the University of
46  *	California, Lawrence Berkeley Laboratory.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. Neither the name of the University nor the names of its contributors
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
73  */
74 
75 /*
76  * Zilog Z8530 Dual UART driver (tty interface)
77  *
78  * This is the "slave" driver that will be attached to
79  * the "zsc" driver for plain "tty" async. serial lines.
80  *
81  * Credits, history:
82  *
83  * The original version of this code was the sparc/dev/zs.c driver
84  * as distributed with the Berkeley 4.4 Lite release.  Since then,
85  * Gordon Ross reorganized the code into the current parent/child
86  * driver scheme, separating the Sun keyboard and mouse support
87  * into independent child drivers.
88  *
89  * RTS/CTS flow-control support was a collaboration of:
90  *	Gordon Ross <gwr@netbsd.org>,
91  *	Bill Studenmund <wrstuden@loki.stanford.edu>
92  *	Ian Dall <Ian.Dall@dsto.defence.gov.au>
93  *
94  * The driver was massively overhauled in November 1997 by Charles Hannum,
95  * fixing *many* bugs, and substantially improving performance.
96  */
97 
98 #include <sys/param.h>
99 #include <sys/systm.h>
100 #include <sys/proc.h>
101 #include <sys/device.h>
102 #include <sys/conf.h>
103 #include <sys/ioctl.h>
104 #include <sys/malloc.h>
105 #include <sys/tty.h>
106 #include <sys/time.h>
107 #include <sys/kernel.h>
108 #include <sys/syslog.h>
109 
110 #include <machine/autoconf.h>
111 
112 #include <dev/wscons/wsconsio.h>
113 #include <dev/wscons/wskbdvar.h>
114 
115 #include <dev/sun/sunkbdreg.h>
116 #include <dev/sun/sunkbdvar.h>
117 
118 #include <dev/ic/z8530reg.h>
119 #include <machine/z8530var.h>
120 
121 #include <dev/cons.h>
122 
123 /*
124  * How many input characters we can buffer.
125  * The port-specific var.h may override this.
126  * Note: must be a power of two!
127  */
128 #ifndef	ZSKBD_RING_SIZE
129 #define	ZSKBD_RING_SIZE	2048
130 #endif
131 
132 struct cfdriver zskbd_cd = {
133 	NULL, "zskbd", DV_TTY
134 };
135 
136 /*
137  * Make this an option variable one can patch.
138  * But be warned:  this must be a power of 2!
139  */
140 u_int zskbd_rbuf_size = ZSKBD_RING_SIZE;
141 
142 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
143 u_int zskbd_rbuf_hiwat = (ZSKBD_RING_SIZE * 1) / 4;
144 u_int zskbd_rbuf_lowat = (ZSKBD_RING_SIZE * 3) / 4;
145 
146 struct zskbd_softc {
147 	struct sunkbd_softc	sc_base;
148 
149 	struct	zs_chanstate *zst_cs;
150 
151 	struct timeout zst_diag_ch;
152 
153 	u_int zst_overflows,
154 	      zst_floods,
155 	      zst_errors;
156 
157 	int zst_hwflags,	/* see z8530var.h */
158 	    zst_swflags;	/* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
159 
160 	u_int zst_r_hiwat,
161 	      zst_r_lowat;
162 	u_char *volatile zst_rbget,
163 	       *volatile zst_rbput;
164 	volatile u_int zst_rbavail;
165 	u_char *zst_rbuf,
166 	       *zst_ebuf;
167 
168 	/*
169 	 * The transmit byte count and address are used for pseudo-DMA
170 	 * output in the hardware interrupt code.  PDMA can be suspended
171 	 * to get pending changes done; heldtbc is used for this.  It can
172 	 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
173 	 */
174 	u_char *zst_tba;		/* transmit buffer address */
175 	u_int zst_tbc,			/* transmit byte count */
176 	      zst_heldtbc;		/* held tbc while xmission stopped */
177 
178 	u_char zst_tbuf[ZSKBD_RING_SIZE];
179 	u_char *zst_tbeg, *zst_tend, *zst_tbp;
180 
181 	/* Flags to communicate with zskbd_softint() */
182 	volatile u_char zst_rx_flags,	/* receiver blocked */
183 #define	RX_TTY_BLOCKED		0x01
184 #define	RX_TTY_OVERFLOWED	0x02
185 #define	RX_IBUF_BLOCKED		0x04
186 #define	RX_IBUF_OVERFLOWED	0x08
187 #define	RX_ANY_BLOCK		0x0f
188 			zst_tx_busy,	/* working on an output chunk */
189 			zst_tx_done,	/* done with one output chunk */
190 			zst_tx_stopped,	/* H/W level stop (lost CTS) */
191 			zst_st_check,	/* got a status interrupt */
192 			zst_rx_ready;
193 
194 	/* PPS signal on DCD, with or without inkernel clock disciplining */
195 	u_char  zst_ppsmask;			/* pps signal mask */
196 	u_char  zst_ppsassert;			/* pps leading edge */
197 	u_char  zst_ppsclear;			/* pps trailing edge */
198 };
199 
200 /* Definition of the driver for autoconfig. */
201 static int	zskbd_match(struct device *, void *, void *);
202 static void	zskbd_attach(struct device *, struct device *, void *);
203 
204 const struct cfattach zskbd_ca = {
205 	sizeof(struct zskbd_softc), zskbd_match, zskbd_attach
206 };
207 
208 struct zsops zsops_kbd;
209 
210 static void zs_modem(struct zskbd_softc *, int);
211 static void zs_hwiflow(struct zskbd_softc *);
212 static void zs_maskintr(struct zskbd_softc *);
213 
214 /* Low-level routines. */
215 static void zskbd_rxint(struct zs_chanstate *);
216 static void zskbd_stint(struct zs_chanstate *, int);
217 static void zskbd_txint(struct zs_chanstate *);
218 static void zskbd_softint(struct zs_chanstate *);
219 static void zskbd_diag(void *);
220 
221 int zskbd_init(struct zskbd_softc *);
222 void zskbd_putc(struct zskbd_softc *, u_int8_t);
223 
224 /* wskbd glue */
225 void zskbd_cngetc(void *, u_int *, int *);
226 void zskbd_cnpollc(void *, int);
227 
228 void zsstart_tx(struct zskbd_softc *);
229 int zsenqueue_tx(void *, u_int8_t *, u_int);
230 
231 struct wskbd_consops zskbd_consops = {
232 	zskbd_cngetc,
233 	zskbd_cnpollc
234 };
235 
236 #define	ZSKBDUNIT(x)	(minor(x) & 0x7ffff)
237 
238 /*
239  * zskbd_match: how is this zs channel configured?
240  */
241 int
zskbd_match(struct device * parent,void * vcf,void * aux)242 zskbd_match(struct device *parent, void *vcf, void *aux)
243 {
244 	struct cfdata *cf = vcf;
245 	struct zsc_attach_args *args = aux;
246 	int ret;
247 
248 	/* If we're not looking for a keyboard, just exit */
249 	if (strcmp(args->type, "keyboard") != 0)
250 		return (0);
251 
252 	ret = 10;
253 
254 	/* Exact match is better than wildcard. */
255 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
256 		ret += 2;
257 
258 	/* This driver accepts wildcard. */
259 	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
260 		ret += 1;
261 
262 	return (ret);
263 }
264 
265 void
zskbd_attach(struct device * parent,struct device * self,void * aux)266 zskbd_attach(struct device *parent, struct device *self, void *aux)
267 {
268 	struct zsc_softc *zsc = (void *)parent;
269 	struct zskbd_softc *zst = (void *)self;
270 	struct sunkbd_softc *ss = (void *)self;
271 	struct cfdata *cf = self->dv_cfdata;
272 	struct zsc_attach_args *args = aux;
273 	struct wskbddev_attach_args a;
274 	struct zs_chanstate *cs;
275 	int channel, s, tty_unit, console = 0;
276 	dev_t dev;
277 
278 	ss->sc_sendcmd = zsenqueue_tx;
279 	timeout_set(&ss->sc_bellto, sunkbd_bellstop, zst);
280 
281 	timeout_set(&zst->zst_diag_ch, zskbd_diag, zst);
282 
283 	zst->zst_tbp = zst->zst_tba = zst->zst_tbeg = zst->zst_tbuf;
284 	zst->zst_tend = zst->zst_tbeg + ZSKBD_RING_SIZE;
285 
286 	tty_unit = ss->sc_dev.dv_unit;
287 	channel = args->channel;
288 	cs = zsc->zsc_cs[channel];
289 	cs->cs_private = zst;
290 	cs->cs_ops = &zsops_kbd;
291 
292 	zst->zst_cs = cs;
293 	zst->zst_swflags = cf->cf_flags;	/* softcar, etc. */
294 	zst->zst_hwflags = args->hwflags;
295 	dev = makedev(zs_major, tty_unit);
296 
297 	if (zst->zst_swflags)
298 		printf(" flags 0x%x", zst->zst_swflags);
299 
300 	/*
301 	 * Check whether we serve as a console device.
302 	 * XXX - split console input/output channels aren't
303 	 *	 supported yet on /dev/console
304 	 */
305 	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
306 		if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
307 			args->consdev->cn_dev = dev;
308 			cn_tab->cn_pollc = wskbd_cnpollc;
309 			cn_tab->cn_getc = wskbd_cngetc;
310 		}
311 		cn_tab->cn_dev = dev;
312 		console = 1;
313 	}
314 
315 	zst->zst_rbuf = malloc(zskbd_rbuf_size << 1, M_DEVBUF, M_WAITOK);
316 	zst->zst_ebuf = zst->zst_rbuf + (zskbd_rbuf_size << 1);
317 	/* Disable the high water mark. */
318 	zst->zst_r_hiwat = 0;
319 	zst->zst_r_lowat = 0;
320 	zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
321 	zst->zst_rbavail = zskbd_rbuf_size;
322 
323 	/* if there are no enable/disable functions, assume the device
324 	   is always enabled */
325 	if (!cs->enable)
326 		cs->enabled = 1;
327 
328 	/*
329 	 * Hardware init
330 	 */
331 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
332 		/* Call zsparam similar to open. */
333 
334 		/* Wait a while for previous console output to complete */
335 		DELAY(10000);
336 	} else if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_NORESET)) {
337 		/* Not the console; may need reset. */
338 		int reset;
339 
340 		reset = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
341 		s = splzs();
342 		zs_write_reg(cs, 9, reset);
343 		splx(s);
344 	}
345 
346 	/*
347 	 * Probe for a keyboard.
348 	 * If one is found, turn on receiver and status interrupts.
349 	 * We defer the actual write of the register to zsparam(),
350 	 * but we must make sure status interrupts are turned on by
351 	 * the time zsparam() reads the initial rr0 state.
352 	 */
353 	if (zskbd_init(zst)) {
354 		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
355 		zs_write_reg(cs, 1, cs->cs_creg[1]);
356 
357 		/* Make sure DTR is on now. */
358 		s = splzs();
359 		zs_modem(zst, 1);
360 		splx(s);
361 	} else {
362 		/* Will raise DTR in open. */
363 		s = splzs();
364 		zs_modem(zst, 0);
365 		splx(s);
366 
367 		return;
368 	}
369 
370 	ss->sc_click =
371 	    strcmp(getpropstring(optionsnode, "keyboard-click?"), "true") == 0;
372 	sunkbd_setclick(ss, ss->sc_click);
373 
374 	a.console = console;
375 	if (ISTYPE5(ss->sc_layout)) {
376 		a.keymap = &sunkbd5_keymapdata;
377 #ifndef	SUNKBD5_LAYOUT
378 		if (ss->sc_layout < MAXSUNLAYOUT &&
379 		    sunkbd_layouts[ss->sc_layout] != -1)
380 			sunkbd5_keymapdata.layout =
381 			    sunkbd_layouts[ss->sc_layout];
382 #endif
383 	} else {
384 		a.keymap = &sunkbd_keymapdata;
385 #ifndef	SUNKBD_LAYOUT
386 		if (ss->sc_layout < MAXSUNLAYOUT &&
387 		    sunkbd_layouts[ss->sc_layout] != -1)
388 			sunkbd_keymapdata.layout =
389 			    sunkbd_layouts[ss->sc_layout];
390 #endif
391 	}
392 	a.accessops = &sunkbd_accessops;
393 	a.accesscookie = zst;
394 
395 	if (console)
396 		wskbd_cnattach(&zskbd_consops, zst, a.keymap);
397 
398 	sunkbd_attach(ss, &a);
399 }
400 
401 int
zskbd_init(struct zskbd_softc * zst)402 zskbd_init(struct zskbd_softc *zst)
403 {
404 	struct sunkbd_softc *ss = (void *)zst;
405 	struct zs_chanstate *cs = zst->zst_cs;
406 	int s, tries;
407 	u_int8_t v3, v4, v5, rr0;
408 
409 	/* setup for 1200n81 */
410 	if (zs_set_speed(cs, 1200)) {			/* set 1200bps */
411 		printf(": failed to set baudrate\n");
412 		return 0;
413 	}
414 	if (zs_set_modes(cs, CS8 | CLOCAL)) {
415 		printf(": failed to set modes\n");
416 		return 0;
417 	}
418 
419 	s = splzs();
420 
421 	zs_maskintr(zst);
422 
423 	v3 = cs->cs_preg[3];				/* set 8 bit chars */
424 	v5 = cs->cs_preg[5];
425 	CLR(v3, ZSWR3_RXSIZE);
426 	CLR(v5, ZSWR5_TXSIZE);
427 	SET(v3, ZSWR3_RX_8);
428 	SET(v5, ZSWR5_TX_8);
429 	cs->cs_preg[3] = v3;
430 	cs->cs_preg[5] = v5;
431 
432 	v4 = cs->cs_preg[4];				/* no parity 1 stop */
433 	CLR(v4, ZSWR4_SBMASK | ZSWR4_PARMASK);
434 	SET(v4, ZSWR4_ONESB | ZSWR4_EVENP);
435 	cs->cs_preg[4] = v4;
436 
437 	if (!cs->cs_heldchange) {
438 		if (zst->zst_tx_busy) {
439 			zst->zst_heldtbc = zst->zst_tbc;
440 			zst->zst_tbc = 0;
441 			cs->cs_heldchange = 1;
442 		} else
443 			zs_loadchannelregs(cs);
444 	}
445 
446 	/*
447 	 * Hardware flow control is disabled, turn off the buffer water
448 	 * marks and unblock any soft flow control state.  Otherwise, enable
449 	 * the water marks.
450 	 */
451 	zst->zst_r_hiwat = 0;
452 	zst->zst_r_lowat = 0;
453 	if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
454 		CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
455 		zst->zst_rx_ready = 1;
456 		cs->cs_softreq = 1;
457 	}
458 	if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
459 		CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
460 		zs_hwiflow(zst);
461 	}
462 
463 	/*
464 	 * Force a recheck of the hardware carrier and flow control status,
465 	 * since we may have changed which bits we're looking at.
466 	 */
467 	zskbd_stint(cs, 1);
468 
469 	splx(s);
470 
471 	/*
472 	 * Hardware flow control is disabled, unblock any hard flow control
473 	 * state.
474 	 */
475 	if (zst->zst_tx_stopped) {
476 		zst->zst_tx_stopped = 0;
477 		zsstart_tx(zst);
478 	}
479 
480 	zskbd_softint(cs);
481 
482 	/* Ok, start the reset sequence... */
483 
484 	s = splhigh();
485 
486 	for (tries = 5; tries != 0; tries--) {
487 		int ltries;
488 
489 		ss->sc_leds = 0;
490 		ss->sc_layout = -1;
491 
492 		/* Send reset request */
493 		zskbd_putc(zst, SKBD_CMD_RESET);
494 
495 		ltries = 1000;
496 		while (--ltries > 0) {
497 			rr0 = *cs->cs_reg_csr;
498 			if (rr0 & ZSRR0_RX_READY) {
499 				sunkbd_raw(ss, *cs->cs_reg_data);
500 				if (ss->sc_kbdstate == SKBD_STATE_RESET)
501 					break;
502 			}
503 			DELAY(1000);
504 		}
505 		if (ltries == 0)
506 			continue;
507 
508 		/* Wait for reset to finish. */
509 		ltries = 1000;
510 		while (--ltries > 0) {
511 			rr0 = *cs->cs_reg_csr;
512 			if (rr0 & ZSRR0_RX_READY) {
513 				sunkbd_raw(ss, *cs->cs_reg_data);
514 				if (ss->sc_kbdstate == SKBD_STATE_GETKEY)
515 					break;
516 			}
517 			DELAY(1000);
518 		}
519 		if (ltries == 0)
520 			continue;
521 
522 		/* Some Sun<=>PS/2 converters need some delay here */
523 		DELAY(5000);
524 
525 		/* Send layout request */
526 		zskbd_putc(zst, SKBD_CMD_LAYOUT);
527 
528 		ltries = 1000;
529 		while (--ltries > 0) {
530 			rr0 = *cs->cs_reg_csr;
531 			if (rr0 & ZSRR0_RX_READY) {
532 				sunkbd_raw(ss, *cs->cs_reg_data);
533 				if (ss->sc_layout != -1)
534 					break;
535 			}
536 			DELAY(1000);
537 		}
538 		if (ltries == 0)
539 			continue;
540 		break;
541 	}
542 	if (tries == 0)
543 		printf(": no keyboard\n");
544 	else
545 		printf(": layout %d\n", ss->sc_layout);
546 	splx(s);
547 
548 	return tries;
549 }
550 
551 void
zskbd_putc(struct zskbd_softc * zst,u_int8_t c)552 zskbd_putc(struct zskbd_softc *zst, u_int8_t c)
553 {
554 	u_int8_t rr0;
555 	int s;
556 
557 	s = splhigh();
558 	do {
559 		rr0 = *zst->zst_cs->cs_reg_csr;
560 	} while ((rr0 & ZSRR0_TX_READY) == 0);
561 	*zst->zst_cs->cs_reg_data = c;
562 	delay(2);
563 	splx(s);
564 }
565 
566 int
zsenqueue_tx(void * v,u_int8_t * str,u_int len)567 zsenqueue_tx(void *v, u_int8_t *str, u_int len)
568 {
569 	struct zskbd_softc *zst = v;
570 	int s;
571 	u_int i;
572 
573 	s = splzs();
574 	if (zst->zst_tbc + len > ZSKBD_RING_SIZE) {
575 		splx(s);
576 		return (-1);
577 	}
578 	zst->zst_tbc += len;
579 	for (i = 0; i < len; i++) {
580 		*zst->zst_tbp = str[i];
581 		if (++zst->zst_tbp == zst->zst_tend)
582 			zst->zst_tbp = zst->zst_tbeg;
583 	}
584 	splx(s);
585 	zsstart_tx(zst);
586 	return (0);
587 }
588 
589 void
zsstart_tx(struct zskbd_softc * zst)590 zsstart_tx(struct zskbd_softc *zst)
591 {
592 	struct zs_chanstate *cs = zst->zst_cs;
593 	int s, s1;
594 
595 	s = spltty();
596 
597 	if (zst->zst_tx_stopped)
598 		goto out;
599 	if (zst->zst_tbc == 0)
600 		goto out;
601 
602 	s1 = splzs();
603 
604 	zst->zst_tx_busy = 1;
605 
606 	if (!ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
607 		SET(cs->cs_preg[1], ZSWR1_TIE);
608 		cs->cs_creg[1] = cs->cs_preg[1];
609 		zs_write_reg(cs, 1, cs->cs_creg[1]);
610 	}
611 
612 	zs_write_data(cs, *zst->zst_tba);
613 
614 	zst->zst_tbc--;
615 	if (++zst->zst_tba == zst->zst_tend)
616 		zst->zst_tba = zst->zst_tbeg;
617 
618 	splx(s1);
619 
620 out:
621 	splx(s);
622 }
623 
624 /*
625  * Compute interrupt enable bits and set in the pending bits. Called both
626  * in zsparam() and when PPS (pulse per second timing) state changes.
627  * Must be called at splzs().
628  */
629 static void
zs_maskintr(struct zskbd_softc * zst)630 zs_maskintr(struct zskbd_softc *zst)
631 {
632 	struct zs_chanstate *cs = zst->zst_cs;
633 	int tmp15;
634 
635 	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
636 	if (zst->zst_ppsmask != 0)
637 		cs->cs_rr0_mask |= cs->cs_rr0_pps;
638 	tmp15 = cs->cs_preg[15];
639 	if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
640 		SET(tmp15, ZSWR15_DCD_IE);
641 	else
642 		CLR(tmp15, ZSWR15_DCD_IE);
643 	if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
644 		SET(tmp15, ZSWR15_CTS_IE);
645 	else
646 		CLR(tmp15, ZSWR15_CTS_IE);
647 	cs->cs_preg[15] = tmp15;
648 }
649 
650 
651 /*
652  * Raise or lower modem control (DTR/RTS) signals.  If a character is
653  * in transmission, the change is deferred.
654  */
655 static void
zs_modem(struct zskbd_softc * zst,int onoff)656 zs_modem(struct zskbd_softc *zst, int onoff)
657 {
658 	struct zs_chanstate *cs = zst->zst_cs;
659 
660 	if (cs->cs_wr5_dtr == 0)
661 		return;
662 
663 	if (onoff)
664 		SET(cs->cs_preg[5], cs->cs_wr5_dtr);
665 	else
666 		CLR(cs->cs_preg[5], cs->cs_wr5_dtr);
667 
668 	if (!cs->cs_heldchange) {
669 		if (zst->zst_tx_busy) {
670 			zst->zst_heldtbc = zst->zst_tbc;
671 			zst->zst_tbc = 0;
672 			cs->cs_heldchange = 1;
673 		} else
674 			zs_loadchannelregs(cs);
675 	}
676 }
677 
678 /*
679  * Internal version of zshwiflow
680  * called at splzs
681  */
682 static void
zs_hwiflow(struct zskbd_softc * zst)683 zs_hwiflow(struct zskbd_softc *zst)
684 {
685 	struct zs_chanstate *cs = zst->zst_cs;
686 
687 	if (cs->cs_wr5_rts == 0)
688 		return;
689 
690 	if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
691 		CLR(cs->cs_preg[5], cs->cs_wr5_rts);
692 		CLR(cs->cs_creg[5], cs->cs_wr5_rts);
693 	} else {
694 		SET(cs->cs_preg[5], cs->cs_wr5_rts);
695 		SET(cs->cs_creg[5], cs->cs_wr5_rts);
696 	}
697 	zs_write_reg(cs, 5, cs->cs_creg[5]);
698 }
699 
700 
701 /****************************************************************
702  * Interface to the lower layer (zscc)
703  ****************************************************************/
704 
705 #define	integrate
706 integrate void zskbd_rxsoft(struct zskbd_softc *);
707 integrate void zskbd_txsoft(struct zskbd_softc *);
708 integrate void zskbd_stsoft(struct zskbd_softc *);
709 /*
710  * receiver ready interrupt.
711  * called at splzs
712  */
713 static void
zskbd_rxint(struct zs_chanstate * cs)714 zskbd_rxint(struct zs_chanstate *cs)
715 {
716 	struct zskbd_softc *zst = cs->cs_private;
717 	u_char *put, *end;
718 	u_int cc;
719 	u_char rr0, rr1, c;
720 
721 	end = zst->zst_ebuf;
722 	put = zst->zst_rbput;
723 	cc = zst->zst_rbavail;
724 
725 	while (cc > 0) {
726 		/*
727 		 * First read the status, because reading the received char
728 		 * destroys the status of this char.
729 		 */
730 		rr1 = zs_read_reg(cs, 1);
731 		c = zs_read_data(cs);
732 
733 		if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
734 			/* Clear the receive error. */
735 			zs_write_csr(cs, ZSWR0_RESET_ERRORS);
736 		}
737 
738 		put[0] = c;
739 		put[1] = rr1;
740 		put += 2;
741 		if (put >= end)
742 			put = zst->zst_rbuf;
743 		cc--;
744 
745 		rr0 = zs_read_csr(cs);
746 		if (!ISSET(rr0, ZSRR0_RX_READY))
747 			break;
748 	}
749 
750 	/*
751 	 * Current string of incoming characters ended because
752 	 * no more data was available or we ran out of space.
753 	 * Schedule a receive event if any data was received.
754 	 * If we're out of space, turn off receive interrupts.
755 	 */
756 	zst->zst_rbput = put;
757 	zst->zst_rbavail = cc;
758 	if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
759 		zst->zst_rx_ready = 1;
760 		cs->cs_softreq = 1;
761 	}
762 
763 	/*
764 	 * See if we are in danger of overflowing a buffer. If
765 	 * so, use hardware flow control to ease the pressure.
766 	 */
767 	if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
768 	    cc < zst->zst_r_hiwat) {
769 		SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
770 		zs_hwiflow(zst);
771 	}
772 
773 	/*
774 	 * If we're out of space, disable receive interrupts
775 	 * until the queue has drained a bit.
776 	 */
777 	if (!cc) {
778 		SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
779 		CLR(cs->cs_preg[1], ZSWR1_RIE);
780 		cs->cs_creg[1] = cs->cs_preg[1];
781 		zs_write_reg(cs, 1, cs->cs_creg[1]);
782 	}
783 }
784 
785 /*
786  * transmitter ready interrupt.  (splzs)
787  */
788 static void
zskbd_txint(struct zs_chanstate * cs)789 zskbd_txint(struct zs_chanstate *cs)
790 {
791 	struct zskbd_softc *zst = cs->cs_private;
792 
793 	/*
794 	 * If we've delayed a parameter change, do it now, and restart
795 	 * output.
796 	 */
797 	if (cs->cs_heldchange) {
798 		zs_loadchannelregs(cs);
799 		cs->cs_heldchange = 0;
800 		zst->zst_tbc = zst->zst_heldtbc;
801 		zst->zst_heldtbc = 0;
802 	}
803 
804 	/* Output the next character in the buffer, if any. */
805 	if (zst->zst_tbc > 0) {
806 		zs_write_data(cs, *zst->zst_tba);
807 		zst->zst_tbc--;
808 		if (++zst->zst_tba == zst->zst_tend)
809 			zst->zst_tba = zst->zst_tbeg;
810 	} else {
811 		/* Disable transmit completion interrupts if necessary. */
812 		if (ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
813 			CLR(cs->cs_preg[1], ZSWR1_TIE);
814 			cs->cs_creg[1] = cs->cs_preg[1];
815 			zs_write_reg(cs, 1, cs->cs_creg[1]);
816 		}
817 		if (zst->zst_tx_busy) {
818 			zst->zst_tx_busy = 0;
819 			zst->zst_tx_done = 1;
820 			cs->cs_softreq = 1;
821 		}
822 	}
823 }
824 
825 /*
826  * status change interrupt.  (splzs)
827  */
828 static void
zskbd_stint(struct zs_chanstate * cs,int force)829 zskbd_stint(struct zs_chanstate *cs, int force)
830 {
831 	struct zskbd_softc *zst = cs->cs_private;
832 	u_char rr0, delta;
833 
834 	rr0 = zs_read_csr(cs);
835 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
836 
837 	/*
838 	 * Check here for console break, so that we can abort
839 	 * even when interrupts are locking up the machine.
840 	 */
841 	if (!force)
842 		delta = rr0 ^ cs->cs_rr0;
843 	else
844 		delta = cs->cs_rr0_mask;
845 	cs->cs_rr0 = rr0;
846 
847 	if (ISSET(delta, cs->cs_rr0_mask)) {
848 		SET(cs->cs_rr0_delta, delta);
849 
850 		/*
851 		 * Stop output immediately if we lose the output
852 		 * flow control signal or carrier detect.
853 		 */
854 		if (ISSET(~rr0, cs->cs_rr0_mask)) {
855 			zst->zst_tbc = 0;
856 			zst->zst_heldtbc = 0;
857 		}
858 
859 		zst->zst_st_check = 1;
860 		cs->cs_softreq = 1;
861 	}
862 }
863 
864 void
zskbd_diag(void * arg)865 zskbd_diag(void *arg)
866 {
867 	struct zskbd_softc *zst = arg;
868 	struct sunkbd_softc *ss = arg;
869 	int overflows, floods;
870 	int s;
871 
872 	s = splzs();
873 	overflows = zst->zst_overflows;
874 	zst->zst_overflows = 0;
875 	floods = zst->zst_floods;
876 	zst->zst_floods = 0;
877 	zst->zst_errors = 0;
878 	splx(s);
879 
880 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
881 	    ss->sc_dev.dv_xname,
882 	    overflows, overflows == 1 ? "" : "s",
883 	    floods, floods == 1 ? "" : "s");
884 }
885 
886 integrate void
zskbd_rxsoft(struct zskbd_softc * zst)887 zskbd_rxsoft(struct zskbd_softc *zst)
888 {
889 	struct sunkbd_softc *ss = (void *)zst;
890 	struct zs_chanstate *cs = zst->zst_cs;
891 	u_char *get, *end;
892 	u_int cc, scc;
893 	u_char rr1;
894 	int code;
895 	int s;
896 	u_int8_t cbuf[SUNKBD_MAX_INPUT_SIZE], *c;
897 
898 	end = zst->zst_ebuf;
899 	get = zst->zst_rbget;
900 	scc = cc = zskbd_rbuf_size - zst->zst_rbavail;
901 
902 	if (cc == zskbd_rbuf_size) {
903 		zst->zst_floods++;
904 		if (zst->zst_errors++ == 0)
905 			timeout_add_sec(&zst->zst_diag_ch, 60);
906 	}
907 
908 	c = cbuf;
909 	while (cc) {
910 		code = get[0];
911 		rr1 = get[1];
912 		if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
913 			if (ISSET(rr1, ZSRR1_DO)) {
914 				zst->zst_overflows++;
915 				if (zst->zst_errors++ == 0)
916 					timeout_add_sec(&zst->zst_diag_ch, 60);
917 			}
918 			if (ISSET(rr1, ZSRR1_FE))
919 				SET(code, TTY_FE);
920 			if (ISSET(rr1, ZSRR1_PE))
921 				SET(code, TTY_PE);
922 		}
923 
924 		*c++ = code;
925 		if (c - cbuf == sizeof cbuf) {
926 			sunkbd_input(ss, cbuf, c - cbuf);
927 			c = cbuf;
928 		}
929 
930 		get += 2;
931 		if (get >= end)
932 			get = zst->zst_rbuf;
933 		cc--;
934 	}
935 	if (c != cbuf)
936 		sunkbd_input(ss, cbuf, c - cbuf);
937 
938 	if (cc != scc) {
939 		zst->zst_rbget = get;
940 		s = splzs();
941 		cc = zst->zst_rbavail += scc - cc;
942 		/* Buffers should be ok again, release possible block. */
943 		if (cc >= zst->zst_r_lowat) {
944 			if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
945 				CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
946 				SET(cs->cs_preg[1], ZSWR1_RIE);
947 				cs->cs_creg[1] = cs->cs_preg[1];
948 				zs_write_reg(cs, 1, cs->cs_creg[1]);
949 			}
950 			if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
951 				CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
952 				zs_hwiflow(zst);
953 			}
954 		}
955 		splx(s);
956 	}
957 }
958 
959 integrate void
zskbd_txsoft(struct zskbd_softc * zst)960 zskbd_txsoft(struct zskbd_softc *zst)
961 {
962 }
963 
964 integrate void
zskbd_stsoft(struct zskbd_softc * zst)965 zskbd_stsoft(struct zskbd_softc *zst)
966 {
967 	struct zs_chanstate *cs = zst->zst_cs;
968 	u_char rr0, delta;
969 	int s;
970 
971 	s = splzs();
972 	rr0 = cs->cs_rr0;
973 	delta = cs->cs_rr0_delta;
974 	cs->cs_rr0_delta = 0;
975 	splx(s);
976 
977 	if (ISSET(delta, cs->cs_rr0_cts)) {
978 		/* Block or unblock output according to flow control. */
979 		if (ISSET(rr0, cs->cs_rr0_cts))
980 			zst->zst_tx_stopped = 0;
981 		else
982 			zst->zst_tx_stopped = 1;
983 	}
984 }
985 
986 /*
987  * Software interrupt.  Called at zssoft
988  *
989  * The main job to be done here is to empty the input ring
990  * by passing its contents up to the tty layer.  The ring is
991  * always emptied during this operation, therefore the ring
992  * must not be larger than the space after "high water" in
993  * the tty layer, or the tty layer might drop our input.
994  *
995  * Note: an "input blockage" condition is assumed to exist if
996  * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
997  */
998 static void
zskbd_softint(struct zs_chanstate * cs)999 zskbd_softint(struct zs_chanstate *cs)
1000 {
1001 	struct zskbd_softc *zst = cs->cs_private;
1002 	int s;
1003 
1004 	s = spltty();
1005 
1006 	if (zst->zst_rx_ready) {
1007 		zst->zst_rx_ready = 0;
1008 		zskbd_rxsoft(zst);
1009 	}
1010 
1011 	if (zst->zst_st_check) {
1012 		zst->zst_st_check = 0;
1013 		zskbd_stsoft(zst);
1014 	}
1015 
1016 	if (zst->zst_tx_done) {
1017 		zst->zst_tx_done = 0;
1018 		zskbd_txsoft(zst);
1019 	}
1020 
1021 	splx(s);
1022 }
1023 
1024 struct zsops zsops_kbd = {
1025 	zskbd_rxint,	/* receive char available */
1026 	zskbd_stint,	/* external/status */
1027 	zskbd_txint,	/* xmit buffer empty */
1028 	zskbd_softint,	/* process software interrupt */
1029 };
1030 
1031 void
zskbd_cnpollc(void * v,int on)1032 zskbd_cnpollc(void *v, int on)
1033 {
1034 }
1035 
1036 void
zskbd_cngetc(void * v,u_int * type,int * data)1037 zskbd_cngetc(void *v, u_int *type, int *data)
1038 {
1039 	struct zskbd_softc *zst = v;
1040 	int s;
1041 	u_int8_t c, rr0;
1042 
1043 	s = splhigh();
1044 	do {
1045 		rr0 = *zst->zst_cs->cs_reg_csr;
1046 	} while ((rr0 & ZSRR0_RX_READY) == 0);
1047 
1048 	c = *zst->zst_cs->cs_reg_data;
1049 	splx(s);
1050 
1051 	sunkbd_decode(c, type, data);
1052 }
1053