z8530tty.c revision 1.133
1/*	$NetBSD: z8530tty.c,v 1.133 2019/07/21 16:10:37 rin Exp $	*/
2
3/*-
4 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998, 1999
5 *	Charles M. Hannum.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Charles M. Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1992, 1993
35 *	The Regents of the University of California.  All rights reserved.
36 *
37 * This software was developed by the Computer Systems Engineering group
38 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
39 * contributed to Berkeley.
40 *
41 * All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 *	This product includes software developed by the University of
44 *	California, Lawrence Berkeley Laboratory.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 *    notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 *    notice, this list of conditions and the following disclaimer in the
53 *    documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 *    may be used to endorse or promote products derived from this software
56 *    without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 *	@(#)zs.c	8.1 (Berkeley) 7/19/93
71 */
72
73/*
74 * Copyright (c) 1994 Gordon W. Ross
75 *
76 * This software was developed by the Computer Systems Engineering group
77 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
78 * contributed to Berkeley.
79 *
80 * All advertising materials mentioning features or use of this software
81 * must display the following acknowledgement:
82 *	This product includes software developed by the University of
83 *	California, Lawrence Berkeley Laboratory.
84 *
85 * Redistribution and use in source and binary forms, with or without
86 * modification, are permitted provided that the following conditions
87 * are met:
88 * 1. Redistributions of source code must retain the above copyright
89 *    notice, this list of conditions and the following disclaimer.
90 * 2. Redistributions in binary form must reproduce the above copyright
91 *    notice, this list of conditions and the following disclaimer in the
92 *    documentation and/or other materials provided with the distribution.
93 * 3. All advertising materials mentioning features or use of this software
94 *    must display the following acknowledgement:
95 *	This product includes software developed by the University of
96 *	California, Berkeley and its contributors.
97 * 4. Neither the name of the University nor the names of its contributors
98 *    may be used to endorse or promote products derived from this software
99 *    without specific prior written permission.
100 *
101 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
102 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
103 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
104 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
105 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
106 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
107 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
108 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
109 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
110 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
111 * SUCH DAMAGE.
112 *
113 *	@(#)zs.c	8.1 (Berkeley) 7/19/93
114 */
115
116/*
117 * Zilog Z8530 Dual UART driver (tty interface)
118 *
119 * This is the "slave" driver that will be attached to
120 * the "zsc" driver for plain "tty" async. serial lines.
121 *
122 * Credits, history:
123 *
124 * The original version of this code was the sparc/dev/zs.c driver
125 * as distributed with the Berkeley 4.4 Lite release.  Since then,
126 * Gordon Ross reorganized the code into the current parent/child
127 * driver scheme, separating the Sun keyboard and mouse support
128 * into independent child drivers.
129 *
130 * RTS/CTS flow-control support was a collaboration of:
131 *	Gordon Ross <gwr@NetBSD.org>,
132 *	Bill Studenmund <wrstuden@loki.stanford.edu>
133 *	Ian Dall <Ian.Dall@dsto.defence.gov.au>
134 *
135 * The driver was massively overhauled in November 1997 by Charles Hannum,
136 * fixing *many* bugs, and substantially improving performance.
137 */
138
139#include <sys/cdefs.h>
140__KERNEL_RCSID(0, "$NetBSD: z8530tty.c,v 1.133 2019/07/21 16:10:37 rin Exp $");
141
142#include "opt_kgdb.h"
143#include "opt_ntp.h"
144
145#include <sys/param.h>
146#include <sys/systm.h>
147#include <sys/proc.h>
148#include <sys/device.h>
149#include <sys/conf.h>
150#include <sys/file.h>
151#include <sys/ioctl.h>
152#include <sys/malloc.h>
153#include <sys/timepps.h>
154#include <sys/tty.h>
155#include <sys/time.h>
156#include <sys/kernel.h>
157#include <sys/syslog.h>
158#include <sys/kauth.h>
159
160#include <dev/ic/z8530reg.h>
161#include <machine/z8530var.h>
162
163#include <dev/cons.h>
164
165#include "ioconf.h"
166#include "locators.h"
167
168/*
169 * How many input characters we can buffer.
170 * The port-specific var.h may override this.
171 * Note: must be a power of two!
172 */
173#ifndef	ZSTTY_RING_SIZE
174#define	ZSTTY_RING_SIZE	2048
175#endif
176
177static struct cnm_state zstty_cnm_state;
178/*
179 * Make this an option variable one can patch.
180 * But be warned:  this must be a power of 2!
181 */
182u_int zstty_rbuf_size = ZSTTY_RING_SIZE;
183
184/* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
185u_int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE * 1) / 4;
186u_int zstty_rbuf_lowat = (ZSTTY_RING_SIZE * 3) / 4;
187
188struct zstty_softc {
189	device_t zst_dev;		/* required first: base device */
190	struct  tty *zst_tty;
191	struct	zs_chanstate *zst_cs;
192
193	struct callout zst_diag_ch;
194
195	u_int zst_overflows,
196	      zst_floods,
197	      zst_errors;
198
199	int zst_hwflags,	/* see z8530var.h */
200	    zst_swflags;	/* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
201
202	u_int zst_r_hiwat,
203	      zst_r_lowat;
204	uint8_t *volatile zst_rbget,
205	        *volatile zst_rbput;
206	volatile u_int zst_rbavail;
207	uint8_t *zst_rbuf,
208	        *zst_ebuf;
209
210	/*
211	 * The transmit byte count and address are used for pseudo-DMA
212	 * output in the hardware interrupt code.  PDMA can be suspended
213	 * to get pending changes done; heldtbc is used for this.  It can
214	 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
215	 */
216	uint8_t *zst_tba;		/* transmit buffer address */
217	u_int zst_tbc,			/* transmit byte count */
218	      zst_heldtbc;		/* held tbc while xmission stopped */
219
220	/* Flags to communicate with zstty_softint() */
221	volatile uint8_t zst_rx_flags,	/* receiver blocked */
222#define	RX_TTY_BLOCKED		0x01
223#define	RX_TTY_OVERFLOWED	0x02
224#define	RX_IBUF_BLOCKED		0x04
225#define	RX_IBUF_OVERFLOWED	0x08
226#define	RX_ANY_BLOCK		0x0f
227			zst_tx_busy,	/* working on an output chunk */
228			zst_tx_done,	/* done with one output chunk */
229			zst_tx_stopped,	/* H/W level stop (lost CTS) */
230			zst_st_check,	/* got a status interrupt */
231			zst_rx_ready;
232
233	/* PPS signal on DCD, with or without inkernel clock disciplining */
234	uint8_t  zst_ppsmask;			/* pps signal mask */
235	struct pps_state zst_pps_state;
236};
237
238/* Definition of the driver for autoconfig. */
239static int	zstty_match(device_t, cfdata_t, void *);
240static void	zstty_attach(device_t, device_t, void *);
241
242CFATTACH_DECL_NEW(zstty, sizeof(struct zstty_softc),
243    zstty_match, zstty_attach, NULL, NULL);
244
245dev_type_open(zsopen);
246dev_type_close(zsclose);
247dev_type_read(zsread);
248dev_type_write(zswrite);
249dev_type_ioctl(zsioctl);
250dev_type_stop(zsstop);
251dev_type_tty(zstty);
252dev_type_poll(zspoll);
253
254const struct cdevsw zstty_cdevsw = {
255	.d_open = zsopen,
256	.d_close = zsclose,
257	.d_read = zsread,
258	.d_write = zswrite,
259	.d_ioctl = zsioctl,
260	.d_stop = zsstop,
261	.d_tty = zstty,
262	.d_poll = zspoll,
263	.d_mmap = nommap,
264	.d_kqfilter = ttykqfilter,
265	.d_discard = nodiscard,
266	.d_flag = D_TTY
267};
268
269struct zsops zsops_tty;
270
271static void zs_shutdown(struct zstty_softc *);
272static void	zsstart(struct tty *);
273static int	zsparam(struct tty *, struct termios *);
274static void zs_modem(struct zstty_softc *, int);
275static void tiocm_to_zs(struct zstty_softc *, u_long, int);
276static int  zs_to_tiocm(struct zstty_softc *);
277static int    zshwiflow(struct tty *, int);
278static void  zs_hwiflow(struct zstty_softc *);
279static void zs_maskintr(struct zstty_softc *);
280
281/* Low-level routines. */
282static void zstty_rxint  (struct zs_chanstate *);
283static void zstty_stint  (struct zs_chanstate *, int);
284static void zstty_txint  (struct zs_chanstate *);
285static void zstty_softint(struct zs_chanstate *);
286static void zstty_softint1(struct zs_chanstate *);
287
288#define	ZSUNIT(x)	TTUNIT(x)
289#define	ZSDIALOUT(x)	TTDIALOUT(x)
290
291struct tty *zstty_get_tty_from_dev(device_t);
292
293/*
294 * XXX get the (struct tty *) out of a (device_t) we trust to be a
295 * (struct zstty_softc *) - needed by sparc/dev/zs.c, sparc64/dev/zs.c,
296 * sun3/dev/zs.c and sun2/dev/zs.c will probably need it at some point
297 */
298
299struct tty *
300zstty_get_tty_from_dev(device_t dev)
301{
302	struct zstty_softc *sc = device_private(dev);
303
304	return sc->zst_tty;
305}
306
307/*
308 * zstty_match: how is this zs channel configured?
309 */
310int
311zstty_match(device_t parent, cfdata_t cf, void *aux)
312{
313	struct zsc_attach_args *args = aux;
314
315	/* Exact match is better than wildcard. */
316	if (cf->zsccf_channel == args->channel)
317		return 2;
318
319	/* This driver accepts wildcard. */
320	if (cf->zsccf_channel == ZSCCF_CHANNEL_DEFAULT)
321		return 1;
322
323	return 0;
324}
325
326void
327zstty_attach(device_t parent, device_t self, void *aux)
328{
329	struct zstty_softc *zst = device_private(self);
330	struct zsc_softc *zsc = device_private(parent);
331	cfdata_t cf = device_cfdata(self);
332	struct zsc_attach_args *args = aux;
333	struct zs_chanstate *cs;
334	struct tty *tp;
335	int channel, tty_unit;
336	dev_t dev;
337	const char *i, *o;
338	int dtr_on;
339	int resetbit;
340
341	zst->zst_dev = self;
342
343	callout_init(&zst->zst_diag_ch, 0);
344	cn_init_magic(&zstty_cnm_state);
345
346	tty_unit = device_unit(self);
347	channel = args->channel;
348	cs = zsc->zsc_cs[channel];
349	cs->cs_private = zst;
350	cs->cs_ops = &zsops_tty;
351
352	zst->zst_cs = cs;
353	zst->zst_swflags = cf->cf_flags;	/* softcar, etc. */
354	zst->zst_hwflags = args->hwflags;
355	dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), tty_unit);
356
357	if (zst->zst_swflags)
358		aprint_normal(" flags 0x%x", zst->zst_swflags);
359
360	/*
361	 * Check whether we serve as a console device.
362	 * XXX - split console input/output channels aren't
363	 *	 supported yet on /dev/console
364	 */
365	i = o = NULL;
366	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
367		i = "input";
368		if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
369			args->consdev->cn_dev = dev;
370			cn_tab->cn_pollc = args->consdev->cn_pollc;
371			cn_tab->cn_getc = args->consdev->cn_getc;
372		}
373		cn_tab->cn_dev = dev;
374		/* Set console magic to BREAK */
375		cn_set_magic("\047\001");
376	}
377	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
378		o = "output";
379		if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
380			cn_tab->cn_putc = args->consdev->cn_putc;
381		}
382		cn_tab->cn_dev = dev;
383	}
384	if (i != NULL || o != NULL)
385		aprint_normal(" (console %s)", i ? (o ? "i/o" : i) : o);
386
387#ifdef KGDB
388	if (zs_check_kgdb(cs, dev)) {
389		/*
390		 * Allow kgdb to "take over" this port.  Returns true
391		 * if this serial port is in-use by kgdb.
392		 */
393		aprint_normal(" (kgdb)\n");
394		/*
395		 * This is the kgdb port (exclusive use)
396		 * so skip the normal attach code.
397		 */
398		return;
399	}
400#endif
401	aprint_normal("\n");
402
403	tp = tty_alloc();
404	tp->t_dev = dev;
405	tp->t_oproc = zsstart;
406	tp->t_param = zsparam;
407	tp->t_hwiflow = zshwiflow;
408	tty_attach(tp);
409
410	zst->zst_tty = tp;
411	zst->zst_rbuf = malloc(zstty_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
412	if (zst->zst_rbuf == NULL) {
413		aprint_error_dev(zst->zst_dev,
414		    "unable to allocate ring buffer\n");
415		return;
416	}
417	zst->zst_ebuf = zst->zst_rbuf + (zstty_rbuf_size << 1);
418	/* Disable the high water mark. */
419	zst->zst_r_hiwat = 0;
420	zst->zst_r_lowat = 0;
421	zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
422	zst->zst_rbavail = zstty_rbuf_size;
423
424	/* if there are no enable/disable functions, assume the device
425	   is always enabled */
426	if (!cs->enable)
427		cs->enabled = 1;
428
429	/*
430	 * Hardware init
431	 */
432	dtr_on = 0;
433	resetbit = 0;
434	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
435		/* Call zsparam similar to open. */
436		struct termios t;
437
438		/* Wait a while for previous console output to complete */
439		DELAY(10000);
440
441		/* Setup the "new" parameters in t. */
442		t.c_ispeed = 0;
443		t.c_ospeed = cs->cs_defspeed;
444		t.c_cflag = cs->cs_defcflag;
445
446		/*
447		 * Turn on receiver and status interrupts.
448		 * We defer the actual write of the register to zsparam(),
449		 * but we must make sure status interrupts are turned on by
450		 * the time zsparam() reads the initial rr0 state.
451		 */
452		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
453
454		/* Make sure zsparam will see changes. */
455		tp->t_ospeed = 0;
456		(void) zsparam(tp, &t);
457
458		/* Make sure DTR is on now. */
459		dtr_on = 1;
460
461	} else if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_NORESET)) {
462		/* Not the console; may need reset. */
463		resetbit = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
464	}
465
466	mutex_spin_enter(&cs->cs_lock);
467	if (resetbit)
468		zs_write_reg(cs, 9, resetbit);
469	zs_modem(zst, dtr_on);
470	mutex_spin_exit(&cs->cs_lock);
471}
472
473
474/*
475 * Return pointer to our tty.
476 */
477struct tty *
478zstty(dev_t dev)
479{
480	struct zstty_softc *zst;
481
482	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
483
484	return (zst->zst_tty);
485}
486
487
488void
489zs_shutdown(struct zstty_softc *zst)
490{
491	struct zs_chanstate *cs = zst->zst_cs;
492	struct tty *tp = zst->zst_tty;
493
494	mutex_spin_enter(&cs->cs_lock);
495
496	/* If we were asserting flow control, then deassert it. */
497	SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
498	zs_hwiflow(zst);
499
500	/* Clear any break condition set with TIOCSBRK. */
501	zs_break(cs, 0);
502
503	/*
504	 * Hang up if necessary.  Wait a bit, so the other side has time to
505	 * notice even if we immediately open the port again.
506	 */
507	if (ISSET(tp->t_cflag, HUPCL)) {
508		zs_modem(zst, 0);
509		mutex_spin_exit(&cs->cs_lock);
510		/*
511		 * XXX -    another process is not prevented from opening
512		 *	    the device during our sleep.
513		 */
514		(void) tsleep(cs, TTIPRI, ttclos, hz);
515		/* Re-check state in case we were opened during our sleep */
516		if (ISSET(tp->t_state, TS_ISOPEN) || tp->t_wopen != 0)
517			return;
518
519		mutex_spin_enter(&cs->cs_lock);
520	}
521
522	/* Turn off interrupts if not the console. */
523	if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
524		CLR(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
525		cs->cs_creg[1] = cs->cs_preg[1];
526		zs_write_reg(cs, 1, cs->cs_creg[1]);
527	}
528
529	/* Call the power management hook. */
530	if (cs->disable) {
531#ifdef DIAGNOSTIC
532		if (!cs->enabled)
533			panic("%s: not enabled?", __func__);
534#endif
535		(*cs->disable)(zst->zst_cs);
536	}
537
538	mutex_spin_exit(&cs->cs_lock);
539}
540
541/*
542 * Open a zs serial (tty) port.
543 */
544int
545zsopen(dev_t dev, int flags, int mode, struct lwp *l)
546{
547	struct zstty_softc *zst;
548	struct zs_chanstate *cs;
549	struct tty *tp;
550	int error;
551
552	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
553	if (zst == NULL)
554		return (ENXIO);
555
556	tp = zst->zst_tty;
557	cs = zst->zst_cs;
558
559	/* If KGDB took the line, then tp==NULL */
560	if (tp == NULL)
561		return (EBUSY);
562
563	/*
564	 * If the device is exclusively for kernel use, deny userland
565	 * open.
566	 */
567	if (ISSET(tp->t_state, TS_KERN_ONLY))
568		return (EBUSY);
569
570	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
571		return (EBUSY);
572
573	mutex_spin_enter(&tty_lock);
574
575	/*
576	 * Do the following iff this is a first open.
577	 */
578	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
579		struct termios t;
580
581		tp->t_dev = dev;
582
583		/* Call the power management hook. */
584		if (cs->enable) {
585			if ((*cs->enable)(cs)) {
586				mutex_spin_exit(&tty_lock);
587				printf("%s: device enable failed\n",
588				    device_xname(zst->zst_dev));
589				return (EIO);
590			}
591		}
592
593		/*
594		 * Initialize the termios status to the defaults.  Add in the
595		 * sticky bits from TIOCSFLAGS.
596		 */
597		t.c_ispeed = 0;
598		t.c_ospeed = cs->cs_defspeed;
599		t.c_cflag = cs->cs_defcflag;
600		if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
601			SET(t.c_cflag, CLOCAL);
602		if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
603			SET(t.c_cflag, CRTSCTS);
604		if (ISSET(zst->zst_swflags, TIOCFLAG_CDTRCTS))
605			SET(t.c_cflag, CDTRCTS);
606		if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
607			SET(t.c_cflag, MDMBUF);
608
609		mutex_spin_enter(&cs->cs_lock);
610
611		/*
612		 * Turn on receiver and status interrupts.
613		 * We defer the actual write of the register to zsparam(),
614		 * but we must make sure status interrupts are turned on by
615		 * the time zsparam() reads the initial rr0 state.
616		 */
617		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
618
619		/* Clear PPS capture state on first open. */
620		mutex_spin_enter(&timecounter_lock);
621		zst->zst_ppsmask = 0;
622		memset(&zst->zst_pps_state, 0, sizeof(zst->zst_pps_state));
623		zst->zst_pps_state.ppscap =
624		    PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
625		pps_init(&zst->zst_pps_state);
626		mutex_spin_exit(&timecounter_lock);
627
628		mutex_spin_exit(&cs->cs_lock);
629
630		/* Make sure zsparam will see changes. */
631		tp->t_ospeed = 0;
632		mutex_spin_exit(&tty_lock);
633		(void) zsparam(tp, &t);
634		mutex_spin_enter(&tty_lock);
635
636		/*
637		 * Note: zsparam has done: cflag, ispeed, ospeed
638		 * so we just need to do: iflag, oflag, lflag, cc
639		 * For "raw" mode, just leave all zeros.
640		 */
641		if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
642			tp->t_iflag = TTYDEF_IFLAG;
643			tp->t_oflag = TTYDEF_OFLAG;
644			tp->t_lflag = TTYDEF_LFLAG;
645		} else {
646			tp->t_iflag = 0;
647			tp->t_oflag = 0;
648			tp->t_lflag = 0;
649		}
650		ttychars(tp);
651		ttsetwater(tp);
652
653		mutex_spin_enter(&cs->cs_lock);
654
655		/*
656		 * Turn on DTR.  We must always do this, even if carrier is not
657		 * present, because otherwise we'd have to use TIOCSDTR
658		 * immediately after setting CLOCAL, which applications do not
659		 * expect.  We always assert DTR while the device is open
660		 * unless explicitly requested to deassert it.
661		 */
662		zs_modem(zst, 1);
663
664		/* Clear the input ring, and unblock. */
665		zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
666		zst->zst_rbavail = zstty_rbuf_size;
667		zs_iflush(cs);
668		CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
669		zs_hwiflow(zst);
670
671		mutex_spin_exit(&cs->cs_lock);
672	}
673
674	mutex_spin_exit(&tty_lock);
675
676	error = ttyopen(tp, ZSDIALOUT(dev), ISSET(flags, O_NONBLOCK));
677	if (error)
678		goto bad;
679
680	error = (*tp->t_linesw->l_open)(dev, tp);
681	if (error)
682		goto bad;
683
684	return (0);
685
686bad:
687	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
688		/*
689		 * We failed to open the device, and nobody else had it opened.
690		 * Clean up the state as appropriate.
691		 */
692		zs_shutdown(zst);
693	}
694
695	return (error);
696}
697
698/*
699 * Close a zs serial port.
700 */
701int
702zsclose(dev_t dev, int flags, int mode, struct lwp *l)
703{
704	struct zstty_softc *zst;
705	struct tty *tp;
706
707	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
708	tp = zst->zst_tty;
709
710	/* XXX This is for cons.c. */
711	if (!ISSET(tp->t_state, TS_ISOPEN))
712		return 0;
713
714	(*tp->t_linesw->l_close)(tp, flags);
715	ttyclose(tp);
716
717	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
718		/*
719		 * Although we got a last close, the device may still be in
720		 * use; e.g. if this was the dialout node, and there are still
721		 * processes waiting for carrier on the non-dialout node.
722		 */
723		zs_shutdown(zst);
724	}
725
726	return (0);
727}
728
729/*
730 * Read/write zs serial port.
731 */
732int
733zsread(dev_t dev, struct uio *uio, int flags)
734{
735	struct zstty_softc *zst;
736	struct tty *tp;
737
738	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
739	tp = zst->zst_tty;
740
741	return ((*tp->t_linesw->l_read)(tp, uio, flags));
742}
743
744int
745zswrite(dev_t dev, struct uio *uio, int flags)
746{
747	struct zstty_softc *zst;
748	struct tty *tp;
749
750	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
751	tp = zst->zst_tty;
752
753	return ((*tp->t_linesw->l_write)(tp, uio, flags));
754}
755
756int
757zspoll(dev_t dev, int events, struct lwp *l)
758{
759	struct zstty_softc *zst;
760	struct tty *tp;
761
762	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
763	tp = zst->zst_tty;
764
765	return ((*tp->t_linesw->l_poll)(tp, events, l));
766}
767
768int
769zsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
770{
771	struct zstty_softc *zst;
772	struct zs_chanstate *cs;
773	struct tty *tp;
774	int error;
775
776	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
777	cs = zst->zst_cs;
778	tp = zst->zst_tty;
779	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
780	if (error != EPASSTHROUGH)
781		return (error);
782
783	error = ttioctl(tp, cmd, data, flag, l);
784	if (error != EPASSTHROUGH)
785		return (error);
786
787#ifdef	ZS_MD_IOCTL
788	error = ZS_MD_IOCTL(cs, cmd, data);
789	if (error != EPASSTHROUGH)
790		return (error);
791#endif	/* ZS_MD_IOCTL */
792
793	error = 0;
794
795	mutex_spin_enter(&cs->cs_lock);
796
797	switch (cmd) {
798	case TIOCSBRK:
799		zs_break(cs, 1);
800		break;
801
802	case TIOCCBRK:
803		zs_break(cs, 0);
804		break;
805
806	case TIOCGFLAGS:
807		*(int *)data = zst->zst_swflags;
808		break;
809
810	case TIOCSFLAGS:
811		error = kauth_authorize_device_tty(l->l_cred,
812			KAUTH_DEVICE_TTY_PRIVSET, tp);
813		if (error)
814			break;
815		zst->zst_swflags = *(int *)data;
816		break;
817
818	case TIOCSDTR:
819		zs_modem(zst, 1);
820		break;
821
822	case TIOCCDTR:
823		zs_modem(zst, 0);
824		break;
825
826	case TIOCMSET:
827	case TIOCMBIS:
828	case TIOCMBIC:
829		tiocm_to_zs(zst, cmd, *(int *)data);
830		break;
831
832	case TIOCMGET:
833		*(int *)data = zs_to_tiocm(zst);
834		break;
835
836	case PPS_IOC_CREATE:
837	case PPS_IOC_DESTROY:
838	case PPS_IOC_GETPARAMS:
839	case PPS_IOC_SETPARAMS:
840	case PPS_IOC_GETCAP:
841	case PPS_IOC_FETCH:
842#ifdef PPS_SYNC
843	case PPS_IOC_KCBIND:
844#endif
845		mutex_spin_enter(&timecounter_lock);
846		error = pps_ioctl(cmd, data, &zst->zst_pps_state);
847		if (zst->zst_pps_state.ppsparam.mode & PPS_CAPTUREBOTH)
848			zst->zst_ppsmask = ZSRR0_DCD;
849		else
850			zst->zst_ppsmask = 0;
851		mutex_spin_exit(&timecounter_lock);
852		break;
853
854	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
855		if (cs->cs_rr0_pps == 0) {
856			error = EINVAL;
857			break;
858		}
859		mutex_spin_enter(&timecounter_lock);
860#ifndef PPS_TRAILING_EDGE
861		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
862		    &zst->zst_pps_state.ppsinfo.assert_timestamp);
863#else
864		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
865		    &zst->zst_pps_state.ppsinfo.clear_timestamp);
866#endif
867		mutex_spin_exit(&timecounter_lock);
868		/*
869		 * Now update interrupts.
870		 */
871		zs_maskintr(zst);
872		/*
873		 * If nothing is being transmitted, set up new current values,
874		 * else mark them as pending.
875		 */
876		if (!cs->cs_heldchange) {
877			if (zst->zst_tx_busy) {
878				zst->zst_heldtbc = zst->zst_tbc;
879				zst->zst_tbc = 0;
880				cs->cs_heldchange = 1;
881			} else
882				zs_loadchannelregs(cs);
883		}
884
885		break;
886
887	default:
888		error = EPASSTHROUGH;
889		break;
890	}
891
892	mutex_spin_exit(&cs->cs_lock);
893
894	return (error);
895}
896
897/*
898 * Start or restart transmission.
899 */
900static void
901zsstart(struct tty *tp)
902{
903	struct zstty_softc *zst;
904	struct zs_chanstate *cs;
905	u_char *tba;
906	int tbc;
907
908	zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
909	cs = zst->zst_cs;
910
911	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
912		return;
913	if (zst->zst_tx_stopped)
914		return;
915	if (!ttypull(tp))
916		return;
917
918	/* Grab the first contiguous region of buffer space. */
919	tba = tp->t_outq.c_cf;
920	tbc = ndqb(&tp->t_outq, 0);
921
922	mutex_spin_enter(&cs->cs_lock);
923
924	zst->zst_tba = tba;
925	zst->zst_tbc = tbc;
926	SET(tp->t_state, TS_BUSY);
927	zst->zst_tx_busy = 1;
928
929#ifdef ZS_TXDMA
930	if (zst->zst_tbc > 1) {
931		zs_dma_setup(cs, zst->zst_tba, zst->zst_tbc);
932		mutex_spin_exit(&cs->cs_lock);
933		return;
934	}
935#endif
936
937	/* Output the first character of the contiguous buffer. */
938	zs_write_data(cs, *zst->zst_tba);
939	zst->zst_tbc--;
940	zst->zst_tba++;
941
942	mutex_spin_exit(&cs->cs_lock);
943}
944
945/*
946 * Stop output, e.g., for ^S or output flush.
947 */
948void
949zsstop(struct tty *tp, int flag)
950{
951	struct zstty_softc *zst;
952
953	zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
954
955	mutex_spin_enter(&zst->zst_cs->cs_lock);
956	if (ISSET(tp->t_state, TS_BUSY)) {
957		/* Stop transmitting at the next chunk. */
958		zst->zst_tbc = 0;
959		zst->zst_heldtbc = 0;
960		if (!ISSET(tp->t_state, TS_TTSTOP))
961			SET(tp->t_state, TS_FLUSH);
962	}
963	mutex_spin_exit(&zst->zst_cs->cs_lock);
964}
965
966/*
967 * Set ZS tty parameters from termios.
968 * XXX - Should just copy the whole termios after
969 * making sure all the changes could be done.
970 */
971static int
972zsparam(struct tty *tp, struct termios *t)
973{
974	struct zstty_softc *zst;
975	struct zs_chanstate *cs;
976	int ospeed;
977	tcflag_t cflag;
978	uint8_t tmp3, tmp4, tmp5;
979	int error;
980
981	zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
982	cs = zst->zst_cs;
983	ospeed = t->c_ospeed;
984	cflag = t->c_cflag;
985
986	/* Check requested parameters. */
987	if (ospeed < 0)
988		return (EINVAL);
989	if (t->c_ispeed && t->c_ispeed != ospeed)
990		return (EINVAL);
991
992	/*
993	 * For the console, always force CLOCAL and !HUPCL, so that the port
994	 * is always active.
995	 */
996	if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
997	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
998		SET(cflag, CLOCAL);
999		CLR(cflag, HUPCL);
1000	}
1001
1002	/*
1003	 * Only whack the UART when params change.
1004	 * Some callers need to clear tp->t_ospeed
1005	 * to make sure initialization gets done.
1006	 */
1007	if (tp->t_ospeed == ospeed &&
1008	    tp->t_cflag == cflag)
1009		return (0);
1010
1011	/*
1012	 * Call MD functions to deal with changed
1013	 * clock modes or H/W flow control modes.
1014	 * The BRG divisor is set now. (reg 12,13)
1015	 */
1016	error = zs_set_speed(cs, ospeed);
1017	if (error)
1018		return (error);
1019	error = zs_set_modes(cs, cflag);
1020	if (error)
1021		return (error);
1022
1023	/*
1024	 * Block interrupts so that state will not
1025	 * be altered until we are done setting it up.
1026	 *
1027	 * Initial values in cs_preg are set before
1028	 * our attach routine is called.  The master
1029	 * interrupt enable is handled by zsc.c
1030	 *
1031	 */
1032	mutex_spin_enter(&cs->cs_lock);
1033
1034	/*
1035	 * Recalculate which status ints to enable.
1036	 */
1037	zs_maskintr(zst);
1038
1039	/* Recompute character size bits. */
1040	tmp3 = cs->cs_preg[3];
1041	tmp5 = cs->cs_preg[5];
1042	CLR(tmp3, ZSWR3_RXSIZE);
1043	CLR(tmp5, ZSWR5_TXSIZE);
1044	switch (ISSET(cflag, CSIZE)) {
1045	case CS5:
1046		SET(tmp3, ZSWR3_RX_5);
1047		SET(tmp5, ZSWR5_TX_5);
1048		break;
1049	case CS6:
1050		SET(tmp3, ZSWR3_RX_6);
1051		SET(tmp5, ZSWR5_TX_6);
1052		break;
1053	case CS7:
1054		SET(tmp3, ZSWR3_RX_7);
1055		SET(tmp5, ZSWR5_TX_7);
1056		break;
1057	case CS8:
1058		SET(tmp3, ZSWR3_RX_8);
1059		SET(tmp5, ZSWR5_TX_8);
1060		break;
1061	}
1062	cs->cs_preg[3] = tmp3;
1063	cs->cs_preg[5] = tmp5;
1064
1065	/*
1066	 * Recompute the stop bits and parity bits.  Note that
1067	 * zs_set_speed() may have set clock selection bits etc.
1068	 * in wr4, so those must preserved.
1069	 */
1070	tmp4 = cs->cs_preg[4];
1071	CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
1072	if (ISSET(cflag, CSTOPB))
1073		SET(tmp4, ZSWR4_TWOSB);
1074	else
1075		SET(tmp4, ZSWR4_ONESB);
1076	if (!ISSET(cflag, PARODD))
1077		SET(tmp4, ZSWR4_EVENP);
1078	if (ISSET(cflag, PARENB))
1079		SET(tmp4, ZSWR4_PARENB);
1080	cs->cs_preg[4] = tmp4;
1081
1082	/* And copy to tty. */
1083	tp->t_ispeed = 0;
1084	tp->t_ospeed = ospeed;
1085	tp->t_cflag = cflag;
1086
1087	/*
1088	 * If nothing is being transmitted, set up new current values,
1089	 * else mark them as pending.
1090	 */
1091	if (!cs->cs_heldchange) {
1092		if (zst->zst_tx_busy) {
1093			zst->zst_heldtbc = zst->zst_tbc;
1094			zst->zst_tbc = 0;
1095			cs->cs_heldchange = 1;
1096		} else
1097			zs_loadchannelregs(cs);
1098	}
1099
1100	/*
1101	 * If hardware flow control is disabled, turn off the buffer water
1102	 * marks and unblock any soft flow control state.  Otherwise, enable
1103	 * the water marks.
1104	 */
1105	if (!ISSET(cflag, CHWFLOW)) {
1106		zst->zst_r_hiwat = 0;
1107		zst->zst_r_lowat = 0;
1108		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1109			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1110			zst->zst_rx_ready = 1;
1111			cs->cs_softreq = 1;
1112		}
1113		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1114			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1115			zs_hwiflow(zst);
1116		}
1117	} else {
1118		zst->zst_r_hiwat = zstty_rbuf_hiwat;
1119		zst->zst_r_lowat = zstty_rbuf_lowat;
1120	}
1121
1122	/*
1123	 * Force a recheck of the hardware carrier and flow control status,
1124	 * since we may have changed which bits we're looking at.
1125	 */
1126	zstty_stint(cs, 1);
1127
1128	mutex_spin_exit(&cs->cs_lock);
1129
1130	/*
1131	 * If hardware flow control is disabled, unblock any hard flow control
1132	 * state.
1133	 */
1134	if (!ISSET(cflag, CHWFLOW)) {
1135		if (zst->zst_tx_stopped) {
1136			zst->zst_tx_stopped = 0;
1137			zsstart(tp);
1138		}
1139	}
1140
1141	zstty_softint1(cs);
1142
1143	return (0);
1144}
1145
1146/*
1147 * Compute interrupt enable bits and set in the pending bits. Called both
1148 * in zsparam() and when PPS (pulse per second timing) state changes.
1149 * Must be called at splzs().
1150 */
1151static void
1152zs_maskintr(struct zstty_softc *zst)
1153{
1154	struct zs_chanstate *cs = zst->zst_cs;
1155	uint8_t tmp15;
1156
1157	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
1158	if (zst->zst_ppsmask != 0)
1159		cs->cs_rr0_mask |= cs->cs_rr0_pps;
1160	tmp15 = cs->cs_preg[15];
1161	if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
1162		SET(tmp15, ZSWR15_DCD_IE);
1163	else
1164		CLR(tmp15, ZSWR15_DCD_IE);
1165	if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
1166		SET(tmp15, ZSWR15_CTS_IE);
1167	else
1168		CLR(tmp15, ZSWR15_CTS_IE);
1169	cs->cs_preg[15] = tmp15;
1170}
1171
1172
1173/*
1174 * Raise or lower modem control (DTR/RTS) signals.  If a character is
1175 * in transmission, the change is deferred.
1176 * Called at splzs() and with the channel lock held.
1177 */
1178static void
1179zs_modem(struct zstty_softc *zst, int onoff)
1180{
1181	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1182
1183	if (cs->cs_wr5_dtr == 0)
1184		return;
1185
1186	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1187
1188	if (onoff)
1189		SET(ccs->cs_preg[5], cs->cs_wr5_dtr);
1190	else
1191		CLR(ccs->cs_preg[5], cs->cs_wr5_dtr);
1192
1193	if (!cs->cs_heldchange) {
1194		if (zst->zst_tx_busy) {
1195			zst->zst_heldtbc = zst->zst_tbc;
1196			zst->zst_tbc = 0;
1197			cs->cs_heldchange = 1;
1198		} else
1199			zs_loadchannelregs(cs);
1200	}
1201}
1202
1203/*
1204 * Set modem bits.
1205 * Called at splzs() and with the channel lock held.
1206 */
1207static void
1208tiocm_to_zs(struct zstty_softc *zst, u_long how, int ttybits)
1209{
1210	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1211	uint8_t zsbits;
1212
1213	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1214
1215	zsbits = 0;
1216	if (ISSET(ttybits, TIOCM_DTR))
1217		SET(zsbits, ZSWR5_DTR);
1218	if (ISSET(ttybits, TIOCM_RTS))
1219		SET(zsbits, ZSWR5_RTS);
1220
1221	switch (how) {
1222	case TIOCMBIC:
1223		CLR(ccs->cs_preg[5], zsbits);
1224		break;
1225
1226	case TIOCMBIS:
1227		SET(ccs->cs_preg[5], zsbits);
1228		break;
1229
1230	case TIOCMSET:
1231		CLR(ccs->cs_preg[5], ZSWR5_RTS | ZSWR5_DTR);
1232		SET(ccs->cs_preg[5], zsbits);
1233		break;
1234	}
1235
1236	if (!cs->cs_heldchange) {
1237		if (zst->zst_tx_busy) {
1238			zst->zst_heldtbc = zst->zst_tbc;
1239			zst->zst_tbc = 0;
1240			cs->cs_heldchange = 1;
1241		} else
1242			zs_loadchannelregs(cs);
1243	}
1244}
1245
1246/*
1247 * Get modem bits.
1248 * Called at splzs() and with the channel lock held.
1249 */
1250static int
1251zs_to_tiocm(struct zstty_softc *zst)
1252{
1253	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1254	uint8_t zsbits;
1255	int ttybits = 0;
1256
1257	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1258
1259	zsbits = ccs->cs_preg[5];
1260	if (ISSET(zsbits, ZSWR5_DTR))
1261		SET(ttybits, TIOCM_DTR);
1262	if (ISSET(zsbits, ZSWR5_RTS))
1263		SET(ttybits, TIOCM_RTS);
1264
1265	zsbits = cs->cs_rr0;
1266	if (ISSET(zsbits, ZSRR0_DCD))
1267		SET(ttybits, TIOCM_CD);
1268	if (ISSET(zsbits, ZSRR0_CTS))
1269		SET(ttybits, TIOCM_CTS);
1270
1271	return (ttybits);
1272}
1273
1274/*
1275 * Try to block or unblock input using hardware flow-control.
1276 * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
1277 * if this function returns non-zero, the TS_TBLOCK flag will
1278 * be set or cleared according to the "block" arg passed.
1279 */
1280int
1281zshwiflow(struct tty *tp, int block)
1282{
1283	struct zstty_softc *zst;
1284	struct zs_chanstate *cs;
1285
1286	zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
1287	cs = zst->zst_cs;
1288
1289	if (cs->cs_wr5_rts == 0)
1290		return (0);
1291
1292	mutex_spin_enter(&cs->cs_lock);
1293	if (block) {
1294		if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1295			SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
1296			zs_hwiflow(zst);
1297		}
1298	} else {
1299		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1300			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1301			zst->zst_rx_ready = 1;
1302			cs->cs_softreq = 1;
1303		}
1304		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1305			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
1306			zs_hwiflow(zst);
1307		}
1308	}
1309	mutex_spin_exit(&cs->cs_lock);
1310	return (1);
1311}
1312
1313/*
1314 * Internal version of zshwiflow
1315 * Called at splzs() and with the channel lock held.
1316 */
1317static void
1318zs_hwiflow(struct zstty_softc *zst)
1319{
1320	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1321
1322	if (cs->cs_wr5_rts == 0)
1323		return;
1324
1325	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1326
1327	if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
1328		CLR(ccs->cs_preg[5], cs->cs_wr5_rts);
1329		CLR(ccs->cs_creg[5], cs->cs_wr5_rts);
1330	} else {
1331		SET(ccs->cs_preg[5], cs->cs_wr5_rts);
1332		SET(ccs->cs_creg[5], cs->cs_wr5_rts);
1333	}
1334	zs_write_reg(ccs, 5, ccs->cs_creg[5]);
1335}
1336
1337
1338/****************************************************************
1339 * Interface to the lower layer (zscc)
1340 ****************************************************************/
1341
1342#define	integrate	static inline
1343integrate void zstty_rxsoft(struct zstty_softc *, struct tty *);
1344integrate void zstty_txsoft(struct zstty_softc *, struct tty *);
1345integrate void zstty_stsoft(struct zstty_softc *, struct tty *);
1346static void zstty_diag(void *);
1347
1348/*
1349 * Receiver Ready interrupt.
1350 * Called at splzs() and with the channel lock held.
1351 */
1352static void
1353zstty_rxint(struct zs_chanstate *cs)
1354{
1355	struct zstty_softc *zst = cs->cs_private;
1356	uint8_t *put, *end;
1357	u_int cc;
1358	uint8_t rr0, rr1, c;
1359
1360	end = zst->zst_ebuf;
1361	put = zst->zst_rbput;
1362	cc = zst->zst_rbavail;
1363
1364	while (cc > 0) {
1365		/*
1366		 * First read the status, because reading the received char
1367		 * destroys the status of this char.
1368		 */
1369		rr1 = zs_read_reg(cs, 1);
1370		c = zs_read_data(cs);
1371
1372		if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
1373			/* Clear the receive error. */
1374			zs_write_csr(cs, ZSWR0_RESET_ERRORS);
1375		}
1376
1377		cn_check_magic(zst->zst_tty->t_dev, c, zstty_cnm_state);
1378		put[0] = c;
1379		put[1] = rr1;
1380		put += 2;
1381		if (put >= end)
1382			put = zst->zst_rbuf;
1383		cc--;
1384
1385		rr0 = zs_read_csr(cs);
1386		if (!ISSET(rr0, ZSRR0_RX_READY))
1387			break;
1388	}
1389
1390	/*
1391	 * Current string of incoming characters ended because
1392	 * no more data was available or we ran out of space.
1393	 * Schedule a receive event if any data was received.
1394	 * If we're out of space, turn off receive interrupts.
1395	 */
1396	zst->zst_rbput = put;
1397	zst->zst_rbavail = cc;
1398	if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1399		zst->zst_rx_ready = 1;
1400		cs->cs_softreq = 1;
1401	}
1402
1403	/*
1404	 * See if we are in danger of overflowing a buffer. If
1405	 * so, use hardware flow control to ease the pressure.
1406	 */
1407	if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
1408	    cc < zst->zst_r_hiwat) {
1409		SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
1410		zs_hwiflow(zst);
1411	}
1412
1413	/*
1414	 * If we're out of space, disable receive interrupts
1415	 * until the queue has drained a bit.
1416	 */
1417	if (!cc) {
1418		SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
1419		CLR(cs->cs_preg[1], ZSWR1_RIE);
1420		cs->cs_creg[1] = cs->cs_preg[1];
1421		zs_write_reg(cs, 1, cs->cs_creg[1]);
1422	}
1423
1424#if 0
1425	printf("%xH%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
1426#endif
1427}
1428
1429/*
1430 * Transmitter Ready interrupt.
1431 * Called at splzs() and with the channel lock held.
1432 */
1433static void
1434zstty_txint(struct zs_chanstate *cs)
1435{
1436	struct zstty_softc *zst = cs->cs_private;
1437
1438	zs_write_csr(cs, ZSWR0_RESET_TXINT);
1439
1440	/*
1441	 * If we've delayed a parameter change, do it now, and restart
1442	 * output.
1443	 */
1444	if (cs->cs_heldchange) {
1445		zs_loadchannelregs(cs);
1446		cs->cs_heldchange = 0;
1447		zst->zst_tbc = zst->zst_heldtbc;
1448		zst->zst_heldtbc = 0;
1449	}
1450
1451	/* Output the next character in the buffer, if any. */
1452	if (zst->zst_tbc > 0) {
1453		zs_write_data(cs, *zst->zst_tba);
1454		zst->zst_tbc--;
1455		zst->zst_tba++;
1456	} else {
1457		if (zst->zst_tx_busy) {
1458			zst->zst_tx_busy = 0;
1459			zst->zst_tx_done = 1;
1460			cs->cs_softreq = 1;
1461		}
1462	}
1463}
1464
1465/*
1466 * Status Change interrupt.
1467 * Called at splzs() and with the channel lock held.
1468 */
1469static void
1470zstty_stint(struct zs_chanstate *cs, int force)
1471{
1472	struct zstty_softc *zst = cs->cs_private;
1473	uint8_t rr0, delta;
1474
1475	rr0 = zs_read_csr(cs);
1476	zs_write_csr(cs, ZSWR0_RESET_STATUS);
1477
1478	/*
1479	 * Check here for console break, so that we can abort
1480	 * even when interrupts are locking up the machine.
1481	 */
1482	if (ISSET(rr0, ZSRR0_BREAK))
1483		cn_check_magic(zst->zst_tty->t_dev, CNC_BREAK, zstty_cnm_state);
1484
1485	if (!force)
1486		delta = rr0 ^ cs->cs_rr0;
1487	else
1488		delta = cs->cs_rr0_mask;
1489	cs->cs_rr0 = rr0;
1490
1491	if (ISSET(delta, cs->cs_rr0_mask)) {
1492		SET(cs->cs_rr0_delta, delta);
1493
1494		/*
1495		 * Pulse-per-second clock signal on edge of DCD?
1496		 */
1497		if (ISSET(delta, zst->zst_ppsmask)) {
1498			if (zst->zst_pps_state.ppsparam.mode &
1499			    PPS_CAPTUREBOTH) {
1500				mutex_spin_enter(&timecounter_lock);
1501				pps_capture(&zst->zst_pps_state);
1502				pps_event(&zst->zst_pps_state,
1503				    (ISSET(cs->cs_rr0, zst->zst_ppsmask))
1504				    ? PPS_CAPTUREASSERT
1505				    : PPS_CAPTURECLEAR);
1506				mutex_spin_exit(&timecounter_lock);
1507			}
1508		}
1509
1510		/*
1511		 * Stop output immediately if we lose the output
1512		 * flow control signal or carrier detect.
1513		 */
1514		if (ISSET(~rr0, cs->cs_rr0_mask)) {
1515			zst->zst_tbc = 0;
1516			zst->zst_heldtbc = 0;
1517		}
1518
1519		zst->zst_st_check = 1;
1520		cs->cs_softreq = 1;
1521	}
1522}
1523
1524void
1525zstty_diag(void *arg)
1526{
1527	struct zstty_softc *zst = arg;
1528	int overflows, floods;
1529
1530	mutex_spin_enter(&zst->zst_cs->cs_lock);
1531	overflows = zst->zst_overflows;
1532	zst->zst_overflows = 0;
1533	floods = zst->zst_floods;
1534	zst->zst_floods = 0;
1535	zst->zst_errors = 0;
1536	mutex_spin_exit(&zst->zst_cs->cs_lock);
1537
1538	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1539	    device_xname(zst->zst_dev),
1540	    overflows, overflows == 1 ? "" : "s",
1541	    floods, floods == 1 ? "" : "s");
1542}
1543
1544integrate void
1545zstty_rxsoft(struct zstty_softc *zst, struct tty *tp)
1546{
1547	struct zs_chanstate *cs = zst->zst_cs;
1548	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1549	uint8_t *get, *end;
1550	u_int cc, scc;
1551	uint8_t rr1;
1552	int code;
1553
1554	end = zst->zst_ebuf;
1555	get = zst->zst_rbget;
1556	scc = cc = zstty_rbuf_size - zst->zst_rbavail;
1557
1558	if (cc == zstty_rbuf_size) {
1559		zst->zst_floods++;
1560		if (zst->zst_errors++ == 0)
1561			callout_reset(&zst->zst_diag_ch, 60 * hz,
1562			    zstty_diag, zst);
1563	}
1564
1565	/* If not yet open, drop the entire buffer content here */
1566	if (!ISSET(tp->t_state, TS_ISOPEN)) {
1567		get += cc << 1;
1568		if (get >= end)
1569			get -= zstty_rbuf_size << 1;
1570		cc = 0;
1571	}
1572	while (cc) {
1573		code = get[0];
1574		rr1 = get[1];
1575		if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
1576			if (ISSET(rr1, ZSRR1_DO)) {
1577				zst->zst_overflows++;
1578				if (zst->zst_errors++ == 0)
1579					callout_reset(&zst->zst_diag_ch,
1580					    60 * hz, zstty_diag, zst);
1581			}
1582			if (ISSET(rr1, ZSRR1_FE))
1583				SET(code, TTY_FE);
1584			if (ISSET(rr1, ZSRR1_PE))
1585				SET(code, TTY_PE);
1586		}
1587		if ((*rint)(code, tp) == -1) {
1588			/*
1589			 * The line discipline's buffer is out of space.
1590			 */
1591			if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1592				/*
1593				 * We're either not using flow control, or the
1594				 * line discipline didn't tell us to block for
1595				 * some reason.  Either way, we have no way to
1596				 * know when there's more space available, so
1597				 * just drop the rest of the data.
1598				 */
1599				get += cc << 1;
1600				if (get >= end)
1601					get -= zstty_rbuf_size << 1;
1602				cc = 0;
1603			} else {
1604				/*
1605				 * Don't schedule any more receive processing
1606				 * until the line discipline tells us there's
1607				 * space available (through comhwiflow()).
1608				 * Leave the rest of the data in the input
1609				 * buffer.
1610				 */
1611				SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1612			}
1613			break;
1614		}
1615		get += 2;
1616		if (get >= end)
1617			get = zst->zst_rbuf;
1618		cc--;
1619	}
1620
1621	if (cc != scc) {
1622		zst->zst_rbget = get;
1623		mutex_spin_enter(&cs->cs_lock);
1624		cc = zst->zst_rbavail += scc - cc;
1625		/* Buffers should be ok again, release possible block. */
1626		if (cc >= zst->zst_r_lowat) {
1627			if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
1628				CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
1629				SET(cs->cs_preg[1], ZSWR1_RIE);
1630				cs->cs_creg[1] = cs->cs_preg[1];
1631				zs_write_reg(cs, 1, cs->cs_creg[1]);
1632			}
1633			if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
1634				CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
1635				zs_hwiflow(zst);
1636			}
1637		}
1638		mutex_spin_exit(&cs->cs_lock);
1639	}
1640
1641#if 0
1642	printf("%xS%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
1643#endif
1644}
1645
1646integrate void
1647zstty_txsoft(struct zstty_softc *zst, struct tty *tp)
1648{
1649	struct zs_chanstate *cs = zst->zst_cs;
1650
1651	mutex_spin_enter(&cs->cs_lock);
1652	CLR(tp->t_state, TS_BUSY);
1653	if (ISSET(tp->t_state, TS_FLUSH))
1654		CLR(tp->t_state, TS_FLUSH);
1655	else
1656		ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
1657	mutex_spin_exit(&cs->cs_lock);
1658	(*tp->t_linesw->l_start)(tp);
1659}
1660
1661integrate void
1662zstty_stsoft(struct zstty_softc *zst, struct tty *tp)
1663{
1664	struct zs_chanstate *cs = zst->zst_cs;
1665	uint8_t rr0, delta;
1666
1667	mutex_spin_enter(&cs->cs_lock);
1668	rr0 = cs->cs_rr0;
1669	delta = cs->cs_rr0_delta;
1670	cs->cs_rr0_delta = 0;
1671	mutex_spin_exit(&cs->cs_lock);
1672
1673	if (ISSET(delta, cs->cs_rr0_dcd)) {
1674		/*
1675		 * Inform the tty layer that carrier detect changed.
1676		 */
1677		(void) (*tp->t_linesw->l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
1678	}
1679
1680	if (ISSET(delta, cs->cs_rr0_cts)) {
1681		/* Block or unblock output according to flow control. */
1682		if (ISSET(rr0, cs->cs_rr0_cts)) {
1683			zst->zst_tx_stopped = 0;
1684			(*tp->t_linesw->l_start)(tp);
1685		} else {
1686			zst->zst_tx_stopped = 1;
1687		}
1688	}
1689}
1690
1691/*
1692 * Software interrupt.  Called at zssoft
1693 *
1694 * The main job to be done here is to empty the input ring
1695 * by passing its contents up to the tty layer.  The ring is
1696 * always emptied during this operation, therefore the ring
1697 * must not be larger than the space after "high water" in
1698 * the tty layer, or the tty layer might drop our input.
1699 *
1700 * Note: an "input blockage" condition is assumed to exist if
1701 * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
1702 */
1703static void
1704zstty_softint(struct zs_chanstate *cs)
1705{
1706
1707	zstty_softint1(cs);
1708}
1709
1710static void
1711zstty_softint1(struct zs_chanstate *cs)
1712{
1713	struct zstty_softc *zst = cs->cs_private;
1714	struct tty *tp = zst->zst_tty;
1715
1716
1717	if (zst->zst_rx_ready) {
1718		zst->zst_rx_ready = 0;
1719		zstty_rxsoft(zst, tp);
1720	}
1721
1722	if (zst->zst_st_check) {
1723		zst->zst_st_check = 0;
1724		zstty_stsoft(zst, tp);
1725	}
1726
1727	if (zst->zst_tx_done) {
1728		zst->zst_tx_done = 0;
1729		zstty_txsoft(zst, tp);
1730	}
1731}
1732
1733struct zsops zsops_tty = {
1734	zstty_rxint,	/* receive char available */
1735	zstty_stint,	/* external/status */
1736	zstty_txint,	/* xmit buffer empty */
1737	zstty_softint,	/* process software interrupt */
1738};
1739
1740#ifdef ZS_TXDMA
1741void
1742zstty_txdma_int(void *arg)
1743{
1744	struct zs_chanstate *cs = arg;
1745	struct zstty_softc *zst = cs->cs_private;
1746
1747	zst->zst_tba += zst->zst_tbc;
1748	zst->zst_tbc = 0;
1749
1750	if (zst->zst_tx_busy) {
1751		zst->zst_tx_busy = 0;
1752		zst->zst_tx_done = 1;
1753		cs->cs_softreq = 1;
1754	}
1755}
1756#endif
1757