z8530tty.c revision 1.132
1/*	$NetBSD: z8530tty.c,v 1.132 2017/10/31 10:45:19 martin 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.132 2017/10/31 10:45:19 martin 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		(void) zsparam(tp, &t);
633
634		/*
635		 * Note: zsparam has done: cflag, ispeed, ospeed
636		 * so we just need to do: iflag, oflag, lflag, cc
637		 * For "raw" mode, just leave all zeros.
638		 */
639		if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
640			tp->t_iflag = TTYDEF_IFLAG;
641			tp->t_oflag = TTYDEF_OFLAG;
642			tp->t_lflag = TTYDEF_LFLAG;
643		} else {
644			tp->t_iflag = 0;
645			tp->t_oflag = 0;
646			tp->t_lflag = 0;
647		}
648		ttychars(tp);
649		ttsetwater(tp);
650
651		mutex_spin_enter(&cs->cs_lock);
652
653		/*
654		 * Turn on DTR.  We must always do this, even if carrier is not
655		 * present, because otherwise we'd have to use TIOCSDTR
656		 * immediately after setting CLOCAL, which applications do not
657		 * expect.  We always assert DTR while the device is open
658		 * unless explicitly requested to deassert it.
659		 */
660		zs_modem(zst, 1);
661
662		/* Clear the input ring, and unblock. */
663		zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
664		zst->zst_rbavail = zstty_rbuf_size;
665		zs_iflush(cs);
666		CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
667		zs_hwiflow(zst);
668
669		mutex_spin_exit(&cs->cs_lock);
670	}
671
672	mutex_spin_exit(&tty_lock);
673
674	error = ttyopen(tp, ZSDIALOUT(dev), ISSET(flags, O_NONBLOCK));
675	if (error)
676		goto bad;
677
678	error = (*tp->t_linesw->l_open)(dev, tp);
679	if (error)
680		goto bad;
681
682	return (0);
683
684bad:
685	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
686		/*
687		 * We failed to open the device, and nobody else had it opened.
688		 * Clean up the state as appropriate.
689		 */
690		zs_shutdown(zst);
691	}
692
693	return (error);
694}
695
696/*
697 * Close a zs serial port.
698 */
699int
700zsclose(dev_t dev, int flags, int mode, struct lwp *l)
701{
702	struct zstty_softc *zst;
703	struct tty *tp;
704
705	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
706	tp = zst->zst_tty;
707
708	/* XXX This is for cons.c. */
709	if (!ISSET(tp->t_state, TS_ISOPEN))
710		return 0;
711
712	(*tp->t_linesw->l_close)(tp, flags);
713	ttyclose(tp);
714
715	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
716		/*
717		 * Although we got a last close, the device may still be in
718		 * use; e.g. if this was the dialout node, and there are still
719		 * processes waiting for carrier on the non-dialout node.
720		 */
721		zs_shutdown(zst);
722	}
723
724	return (0);
725}
726
727/*
728 * Read/write zs serial port.
729 */
730int
731zsread(dev_t dev, struct uio *uio, int flags)
732{
733	struct zstty_softc *zst;
734	struct tty *tp;
735
736	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
737	tp = zst->zst_tty;
738
739	return ((*tp->t_linesw->l_read)(tp, uio, flags));
740}
741
742int
743zswrite(dev_t dev, struct uio *uio, int flags)
744{
745	struct zstty_softc *zst;
746	struct tty *tp;
747
748	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
749	tp = zst->zst_tty;
750
751	return ((*tp->t_linesw->l_write)(tp, uio, flags));
752}
753
754int
755zspoll(dev_t dev, int events, struct lwp *l)
756{
757	struct zstty_softc *zst;
758	struct tty *tp;
759
760	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
761	tp = zst->zst_tty;
762
763	return ((*tp->t_linesw->l_poll)(tp, events, l));
764}
765
766int
767zsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
768{
769	struct zstty_softc *zst;
770	struct zs_chanstate *cs;
771	struct tty *tp;
772	int error;
773
774	zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
775	cs = zst->zst_cs;
776	tp = zst->zst_tty;
777	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
778	if (error != EPASSTHROUGH)
779		return (error);
780
781	error = ttioctl(tp, cmd, data, flag, l);
782	if (error != EPASSTHROUGH)
783		return (error);
784
785#ifdef	ZS_MD_IOCTL
786	error = ZS_MD_IOCTL(cs, cmd, data);
787	if (error != EPASSTHROUGH)
788		return (error);
789#endif	/* ZS_MD_IOCTL */
790
791	error = 0;
792
793	mutex_spin_enter(&cs->cs_lock);
794
795	switch (cmd) {
796	case TIOCSBRK:
797		zs_break(cs, 1);
798		break;
799
800	case TIOCCBRK:
801		zs_break(cs, 0);
802		break;
803
804	case TIOCGFLAGS:
805		*(int *)data = zst->zst_swflags;
806		break;
807
808	case TIOCSFLAGS:
809		error = kauth_authorize_device_tty(l->l_cred,
810			KAUTH_DEVICE_TTY_PRIVSET, tp);
811		if (error)
812			break;
813		zst->zst_swflags = *(int *)data;
814		break;
815
816	case TIOCSDTR:
817		zs_modem(zst, 1);
818		break;
819
820	case TIOCCDTR:
821		zs_modem(zst, 0);
822		break;
823
824	case TIOCMSET:
825	case TIOCMBIS:
826	case TIOCMBIC:
827		tiocm_to_zs(zst, cmd, *(int *)data);
828		break;
829
830	case TIOCMGET:
831		*(int *)data = zs_to_tiocm(zst);
832		break;
833
834	case PPS_IOC_CREATE:
835	case PPS_IOC_DESTROY:
836	case PPS_IOC_GETPARAMS:
837	case PPS_IOC_SETPARAMS:
838	case PPS_IOC_GETCAP:
839	case PPS_IOC_FETCH:
840#ifdef PPS_SYNC
841	case PPS_IOC_KCBIND:
842#endif
843		mutex_spin_enter(&timecounter_lock);
844		error = pps_ioctl(cmd, data, &zst->zst_pps_state);
845		if (zst->zst_pps_state.ppsparam.mode & PPS_CAPTUREBOTH)
846			zst->zst_ppsmask = ZSRR0_DCD;
847		else
848			zst->zst_ppsmask = 0;
849		mutex_spin_exit(&timecounter_lock);
850		break;
851
852	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
853		if (cs->cs_rr0_pps == 0) {
854			error = EINVAL;
855			break;
856		}
857		mutex_spin_enter(&timecounter_lock);
858#ifndef PPS_TRAILING_EDGE
859		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
860		    &zst->zst_pps_state.ppsinfo.assert_timestamp);
861#else
862		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
863		    &zst->zst_pps_state.ppsinfo.clear_timestamp);
864#endif
865		mutex_spin_exit(&timecounter_lock);
866		/*
867		 * Now update interrupts.
868		 */
869		zs_maskintr(zst);
870		/*
871		 * If nothing is being transmitted, set up new current values,
872		 * else mark them as pending.
873		 */
874		if (!cs->cs_heldchange) {
875			if (zst->zst_tx_busy) {
876				zst->zst_heldtbc = zst->zst_tbc;
877				zst->zst_tbc = 0;
878				cs->cs_heldchange = 1;
879			} else
880				zs_loadchannelregs(cs);
881		}
882
883		break;
884
885	default:
886		error = EPASSTHROUGH;
887		break;
888	}
889
890	mutex_spin_exit(&cs->cs_lock);
891
892	return (error);
893}
894
895/*
896 * Start or restart transmission.
897 */
898static void
899zsstart(struct tty *tp)
900{
901	struct zstty_softc *zst;
902	struct zs_chanstate *cs;
903	u_char *tba;
904	int tbc;
905
906	zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
907	cs = zst->zst_cs;
908
909	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
910		return;
911	if (zst->zst_tx_stopped)
912		return;
913	if (!ttypull(tp))
914		return;
915
916	/* Grab the first contiguous region of buffer space. */
917	tba = tp->t_outq.c_cf;
918	tbc = ndqb(&tp->t_outq, 0);
919
920	mutex_spin_enter(&cs->cs_lock);
921
922	zst->zst_tba = tba;
923	zst->zst_tbc = tbc;
924	SET(tp->t_state, TS_BUSY);
925	zst->zst_tx_busy = 1;
926
927#ifdef ZS_TXDMA
928	if (zst->zst_tbc > 1) {
929		zs_dma_setup(cs, zst->zst_tba, zst->zst_tbc);
930		mutex_spin_exit(&cs->cs_lock);
931		return;
932	}
933#endif
934
935	/* Output the first character of the contiguous buffer. */
936	zs_write_data(cs, *zst->zst_tba);
937	zst->zst_tbc--;
938	zst->zst_tba++;
939
940	mutex_spin_exit(&cs->cs_lock);
941}
942
943/*
944 * Stop output, e.g., for ^S or output flush.
945 */
946void
947zsstop(struct tty *tp, int flag)
948{
949	struct zstty_softc *zst;
950
951	zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
952
953	mutex_spin_enter(&zst->zst_cs->cs_lock);
954	if (ISSET(tp->t_state, TS_BUSY)) {
955		/* Stop transmitting at the next chunk. */
956		zst->zst_tbc = 0;
957		zst->zst_heldtbc = 0;
958		if (!ISSET(tp->t_state, TS_TTSTOP))
959			SET(tp->t_state, TS_FLUSH);
960	}
961	mutex_spin_exit(&zst->zst_cs->cs_lock);
962}
963
964/*
965 * Set ZS tty parameters from termios.
966 * XXX - Should just copy the whole termios after
967 * making sure all the changes could be done.
968 */
969static int
970zsparam(struct tty *tp, struct termios *t)
971{
972	struct zstty_softc *zst;
973	struct zs_chanstate *cs;
974	int ospeed;
975	tcflag_t cflag;
976	uint8_t tmp3, tmp4, tmp5;
977	int error;
978
979	zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
980	cs = zst->zst_cs;
981	ospeed = t->c_ospeed;
982	cflag = t->c_cflag;
983
984	/* Check requested parameters. */
985	if (ospeed < 0)
986		return (EINVAL);
987	if (t->c_ispeed && t->c_ispeed != ospeed)
988		return (EINVAL);
989
990	/*
991	 * For the console, always force CLOCAL and !HUPCL, so that the port
992	 * is always active.
993	 */
994	if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
995	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
996		SET(cflag, CLOCAL);
997		CLR(cflag, HUPCL);
998	}
999
1000	/*
1001	 * Only whack the UART when params change.
1002	 * Some callers need to clear tp->t_ospeed
1003	 * to make sure initialization gets done.
1004	 */
1005	if (tp->t_ospeed == ospeed &&
1006	    tp->t_cflag == cflag)
1007		return (0);
1008
1009	/*
1010	 * Call MD functions to deal with changed
1011	 * clock modes or H/W flow control modes.
1012	 * The BRG divisor is set now. (reg 12,13)
1013	 */
1014	error = zs_set_speed(cs, ospeed);
1015	if (error)
1016		return (error);
1017	error = zs_set_modes(cs, cflag);
1018	if (error)
1019		return (error);
1020
1021	/*
1022	 * Block interrupts so that state will not
1023	 * be altered until we are done setting it up.
1024	 *
1025	 * Initial values in cs_preg are set before
1026	 * our attach routine is called.  The master
1027	 * interrupt enable is handled by zsc.c
1028	 *
1029	 */
1030	mutex_spin_enter(&cs->cs_lock);
1031
1032	/*
1033	 * Recalculate which status ints to enable.
1034	 */
1035	zs_maskintr(zst);
1036
1037	/* Recompute character size bits. */
1038	tmp3 = cs->cs_preg[3];
1039	tmp5 = cs->cs_preg[5];
1040	CLR(tmp3, ZSWR3_RXSIZE);
1041	CLR(tmp5, ZSWR5_TXSIZE);
1042	switch (ISSET(cflag, CSIZE)) {
1043	case CS5:
1044		SET(tmp3, ZSWR3_RX_5);
1045		SET(tmp5, ZSWR5_TX_5);
1046		break;
1047	case CS6:
1048		SET(tmp3, ZSWR3_RX_6);
1049		SET(tmp5, ZSWR5_TX_6);
1050		break;
1051	case CS7:
1052		SET(tmp3, ZSWR3_RX_7);
1053		SET(tmp5, ZSWR5_TX_7);
1054		break;
1055	case CS8:
1056		SET(tmp3, ZSWR3_RX_8);
1057		SET(tmp5, ZSWR5_TX_8);
1058		break;
1059	}
1060	cs->cs_preg[3] = tmp3;
1061	cs->cs_preg[5] = tmp5;
1062
1063	/*
1064	 * Recompute the stop bits and parity bits.  Note that
1065	 * zs_set_speed() may have set clock selection bits etc.
1066	 * in wr4, so those must preserved.
1067	 */
1068	tmp4 = cs->cs_preg[4];
1069	CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
1070	if (ISSET(cflag, CSTOPB))
1071		SET(tmp4, ZSWR4_TWOSB);
1072	else
1073		SET(tmp4, ZSWR4_ONESB);
1074	if (!ISSET(cflag, PARODD))
1075		SET(tmp4, ZSWR4_EVENP);
1076	if (ISSET(cflag, PARENB))
1077		SET(tmp4, ZSWR4_PARENB);
1078	cs->cs_preg[4] = tmp4;
1079
1080	/* And copy to tty. */
1081	tp->t_ispeed = 0;
1082	tp->t_ospeed = ospeed;
1083	tp->t_cflag = cflag;
1084
1085	/*
1086	 * If nothing is being transmitted, set up new current values,
1087	 * else mark them as pending.
1088	 */
1089	if (!cs->cs_heldchange) {
1090		if (zst->zst_tx_busy) {
1091			zst->zst_heldtbc = zst->zst_tbc;
1092			zst->zst_tbc = 0;
1093			cs->cs_heldchange = 1;
1094		} else
1095			zs_loadchannelregs(cs);
1096	}
1097
1098	/*
1099	 * If hardware flow control is disabled, turn off the buffer water
1100	 * marks and unblock any soft flow control state.  Otherwise, enable
1101	 * the water marks.
1102	 */
1103	if (!ISSET(cflag, CHWFLOW)) {
1104		zst->zst_r_hiwat = 0;
1105		zst->zst_r_lowat = 0;
1106		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1107			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1108			zst->zst_rx_ready = 1;
1109			cs->cs_softreq = 1;
1110		}
1111		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1112			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1113			zs_hwiflow(zst);
1114		}
1115	} else {
1116		zst->zst_r_hiwat = zstty_rbuf_hiwat;
1117		zst->zst_r_lowat = zstty_rbuf_lowat;
1118	}
1119
1120	/*
1121	 * Force a recheck of the hardware carrier and flow control status,
1122	 * since we may have changed which bits we're looking at.
1123	 */
1124	zstty_stint(cs, 1);
1125
1126	mutex_spin_exit(&cs->cs_lock);
1127
1128	/*
1129	 * If hardware flow control is disabled, unblock any hard flow control
1130	 * state.
1131	 */
1132	if (!ISSET(cflag, CHWFLOW)) {
1133		if (zst->zst_tx_stopped) {
1134			zst->zst_tx_stopped = 0;
1135			zsstart(tp);
1136		}
1137	}
1138
1139	zstty_softint1(cs);
1140
1141	return (0);
1142}
1143
1144/*
1145 * Compute interrupt enable bits and set in the pending bits. Called both
1146 * in zsparam() and when PPS (pulse per second timing) state changes.
1147 * Must be called at splzs().
1148 */
1149static void
1150zs_maskintr(struct zstty_softc *zst)
1151{
1152	struct zs_chanstate *cs = zst->zst_cs;
1153	uint8_t tmp15;
1154
1155	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
1156	if (zst->zst_ppsmask != 0)
1157		cs->cs_rr0_mask |= cs->cs_rr0_pps;
1158	tmp15 = cs->cs_preg[15];
1159	if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
1160		SET(tmp15, ZSWR15_DCD_IE);
1161	else
1162		CLR(tmp15, ZSWR15_DCD_IE);
1163	if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
1164		SET(tmp15, ZSWR15_CTS_IE);
1165	else
1166		CLR(tmp15, ZSWR15_CTS_IE);
1167	cs->cs_preg[15] = tmp15;
1168}
1169
1170
1171/*
1172 * Raise or lower modem control (DTR/RTS) signals.  If a character is
1173 * in transmission, the change is deferred.
1174 * Called at splzs() and with the channel lock held.
1175 */
1176static void
1177zs_modem(struct zstty_softc *zst, int onoff)
1178{
1179	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1180
1181	if (cs->cs_wr5_dtr == 0)
1182		return;
1183
1184	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1185
1186	if (onoff)
1187		SET(ccs->cs_preg[5], cs->cs_wr5_dtr);
1188	else
1189		CLR(ccs->cs_preg[5], cs->cs_wr5_dtr);
1190
1191	if (!cs->cs_heldchange) {
1192		if (zst->zst_tx_busy) {
1193			zst->zst_heldtbc = zst->zst_tbc;
1194			zst->zst_tbc = 0;
1195			cs->cs_heldchange = 1;
1196		} else
1197			zs_loadchannelregs(cs);
1198	}
1199}
1200
1201/*
1202 * Set modem bits.
1203 * Called at splzs() and with the channel lock held.
1204 */
1205static void
1206tiocm_to_zs(struct zstty_softc *zst, u_long how, int ttybits)
1207{
1208	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1209	uint8_t zsbits;
1210
1211	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1212
1213	zsbits = 0;
1214	if (ISSET(ttybits, TIOCM_DTR))
1215		SET(zsbits, ZSWR5_DTR);
1216	if (ISSET(ttybits, TIOCM_RTS))
1217		SET(zsbits, ZSWR5_RTS);
1218
1219	switch (how) {
1220	case TIOCMBIC:
1221		CLR(ccs->cs_preg[5], zsbits);
1222		break;
1223
1224	case TIOCMBIS:
1225		SET(ccs->cs_preg[5], zsbits);
1226		break;
1227
1228	case TIOCMSET:
1229		CLR(ccs->cs_preg[5], ZSWR5_RTS | ZSWR5_DTR);
1230		SET(ccs->cs_preg[5], zsbits);
1231		break;
1232	}
1233
1234	if (!cs->cs_heldchange) {
1235		if (zst->zst_tx_busy) {
1236			zst->zst_heldtbc = zst->zst_tbc;
1237			zst->zst_tbc = 0;
1238			cs->cs_heldchange = 1;
1239		} else
1240			zs_loadchannelregs(cs);
1241	}
1242}
1243
1244/*
1245 * Get modem bits.
1246 * Called at splzs() and with the channel lock held.
1247 */
1248static int
1249zs_to_tiocm(struct zstty_softc *zst)
1250{
1251	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1252	uint8_t zsbits;
1253	int ttybits = 0;
1254
1255	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1256
1257	zsbits = ccs->cs_preg[5];
1258	if (ISSET(zsbits, ZSWR5_DTR))
1259		SET(ttybits, TIOCM_DTR);
1260	if (ISSET(zsbits, ZSWR5_RTS))
1261		SET(ttybits, TIOCM_RTS);
1262
1263	zsbits = cs->cs_rr0;
1264	if (ISSET(zsbits, ZSRR0_DCD))
1265		SET(ttybits, TIOCM_CD);
1266	if (ISSET(zsbits, ZSRR0_CTS))
1267		SET(ttybits, TIOCM_CTS);
1268
1269	return (ttybits);
1270}
1271
1272/*
1273 * Try to block or unblock input using hardware flow-control.
1274 * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
1275 * if this function returns non-zero, the TS_TBLOCK flag will
1276 * be set or cleared according to the "block" arg passed.
1277 */
1278int
1279zshwiflow(struct tty *tp, int block)
1280{
1281	struct zstty_softc *zst;
1282	struct zs_chanstate *cs;
1283
1284	zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
1285	cs = zst->zst_cs;
1286
1287	if (cs->cs_wr5_rts == 0)
1288		return (0);
1289
1290	mutex_spin_enter(&cs->cs_lock);
1291	if (block) {
1292		if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1293			SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
1294			zs_hwiflow(zst);
1295		}
1296	} else {
1297		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1298			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1299			zst->zst_rx_ready = 1;
1300			cs->cs_softreq = 1;
1301		}
1302		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1303			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
1304			zs_hwiflow(zst);
1305		}
1306	}
1307	mutex_spin_exit(&cs->cs_lock);
1308	return (1);
1309}
1310
1311/*
1312 * Internal version of zshwiflow
1313 * Called at splzs() and with the channel lock held.
1314 */
1315static void
1316zs_hwiflow(struct zstty_softc *zst)
1317{
1318	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1319
1320	if (cs->cs_wr5_rts == 0)
1321		return;
1322
1323	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1324
1325	if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
1326		CLR(ccs->cs_preg[5], cs->cs_wr5_rts);
1327		CLR(ccs->cs_creg[5], cs->cs_wr5_rts);
1328	} else {
1329		SET(ccs->cs_preg[5], cs->cs_wr5_rts);
1330		SET(ccs->cs_creg[5], cs->cs_wr5_rts);
1331	}
1332	zs_write_reg(ccs, 5, ccs->cs_creg[5]);
1333}
1334
1335
1336/****************************************************************
1337 * Interface to the lower layer (zscc)
1338 ****************************************************************/
1339
1340#define	integrate	static inline
1341integrate void zstty_rxsoft(struct zstty_softc *, struct tty *);
1342integrate void zstty_txsoft(struct zstty_softc *, struct tty *);
1343integrate void zstty_stsoft(struct zstty_softc *, struct tty *);
1344static void zstty_diag(void *);
1345
1346/*
1347 * Receiver Ready interrupt.
1348 * Called at splzs() and with the channel lock held.
1349 */
1350static void
1351zstty_rxint(struct zs_chanstate *cs)
1352{
1353	struct zstty_softc *zst = cs->cs_private;
1354	uint8_t *put, *end;
1355	u_int cc;
1356	uint8_t rr0, rr1, c;
1357
1358	end = zst->zst_ebuf;
1359	put = zst->zst_rbput;
1360	cc = zst->zst_rbavail;
1361
1362	while (cc > 0) {
1363		/*
1364		 * First read the status, because reading the received char
1365		 * destroys the status of this char.
1366		 */
1367		rr1 = zs_read_reg(cs, 1);
1368		c = zs_read_data(cs);
1369
1370		if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
1371			/* Clear the receive error. */
1372			zs_write_csr(cs, ZSWR0_RESET_ERRORS);
1373		}
1374
1375		cn_check_magic(zst->zst_tty->t_dev, c, zstty_cnm_state);
1376		put[0] = c;
1377		put[1] = rr1;
1378		put += 2;
1379		if (put >= end)
1380			put = zst->zst_rbuf;
1381		cc--;
1382
1383		rr0 = zs_read_csr(cs);
1384		if (!ISSET(rr0, ZSRR0_RX_READY))
1385			break;
1386	}
1387
1388	/*
1389	 * Current string of incoming characters ended because
1390	 * no more data was available or we ran out of space.
1391	 * Schedule a receive event if any data was received.
1392	 * If we're out of space, turn off receive interrupts.
1393	 */
1394	zst->zst_rbput = put;
1395	zst->zst_rbavail = cc;
1396	if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1397		zst->zst_rx_ready = 1;
1398		cs->cs_softreq = 1;
1399	}
1400
1401	/*
1402	 * See if we are in danger of overflowing a buffer. If
1403	 * so, use hardware flow control to ease the pressure.
1404	 */
1405	if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
1406	    cc < zst->zst_r_hiwat) {
1407		SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
1408		zs_hwiflow(zst);
1409	}
1410
1411	/*
1412	 * If we're out of space, disable receive interrupts
1413	 * until the queue has drained a bit.
1414	 */
1415	if (!cc) {
1416		SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
1417		CLR(cs->cs_preg[1], ZSWR1_RIE);
1418		cs->cs_creg[1] = cs->cs_preg[1];
1419		zs_write_reg(cs, 1, cs->cs_creg[1]);
1420	}
1421
1422#if 0
1423	printf("%xH%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
1424#endif
1425}
1426
1427/*
1428 * Transmitter Ready interrupt.
1429 * Called at splzs() and with the channel lock held.
1430 */
1431static void
1432zstty_txint(struct zs_chanstate *cs)
1433{
1434	struct zstty_softc *zst = cs->cs_private;
1435
1436	zs_write_csr(cs, ZSWR0_RESET_TXINT);
1437
1438	/*
1439	 * If we've delayed a parameter change, do it now, and restart
1440	 * output.
1441	 */
1442	if (cs->cs_heldchange) {
1443		zs_loadchannelregs(cs);
1444		cs->cs_heldchange = 0;
1445		zst->zst_tbc = zst->zst_heldtbc;
1446		zst->zst_heldtbc = 0;
1447	}
1448
1449	/* Output the next character in the buffer, if any. */
1450	if (zst->zst_tbc > 0) {
1451		zs_write_data(cs, *zst->zst_tba);
1452		zst->zst_tbc--;
1453		zst->zst_tba++;
1454	} else {
1455		if (zst->zst_tx_busy) {
1456			zst->zst_tx_busy = 0;
1457			zst->zst_tx_done = 1;
1458			cs->cs_softreq = 1;
1459		}
1460	}
1461}
1462
1463/*
1464 * Status Change interrupt.
1465 * Called at splzs() and with the channel lock held.
1466 */
1467static void
1468zstty_stint(struct zs_chanstate *cs, int force)
1469{
1470	struct zstty_softc *zst = cs->cs_private;
1471	uint8_t rr0, delta;
1472
1473	rr0 = zs_read_csr(cs);
1474	zs_write_csr(cs, ZSWR0_RESET_STATUS);
1475
1476	/*
1477	 * Check here for console break, so that we can abort
1478	 * even when interrupts are locking up the machine.
1479	 */
1480	if (ISSET(rr0, ZSRR0_BREAK))
1481		cn_check_magic(zst->zst_tty->t_dev, CNC_BREAK, zstty_cnm_state);
1482
1483	if (!force)
1484		delta = rr0 ^ cs->cs_rr0;
1485	else
1486		delta = cs->cs_rr0_mask;
1487	cs->cs_rr0 = rr0;
1488
1489	if (ISSET(delta, cs->cs_rr0_mask)) {
1490		SET(cs->cs_rr0_delta, delta);
1491
1492		/*
1493		 * Pulse-per-second clock signal on edge of DCD?
1494		 */
1495		if (ISSET(delta, zst->zst_ppsmask)) {
1496			if (zst->zst_pps_state.ppsparam.mode &
1497			    PPS_CAPTUREBOTH) {
1498				mutex_spin_enter(&timecounter_lock);
1499				pps_capture(&zst->zst_pps_state);
1500				pps_event(&zst->zst_pps_state,
1501				    (ISSET(cs->cs_rr0, zst->zst_ppsmask))
1502				    ? PPS_CAPTUREASSERT
1503				    : PPS_CAPTURECLEAR);
1504				mutex_spin_exit(&timecounter_lock);
1505			}
1506		}
1507
1508		/*
1509		 * Stop output immediately if we lose the output
1510		 * flow control signal or carrier detect.
1511		 */
1512		if (ISSET(~rr0, cs->cs_rr0_mask)) {
1513			zst->zst_tbc = 0;
1514			zst->zst_heldtbc = 0;
1515		}
1516
1517		zst->zst_st_check = 1;
1518		cs->cs_softreq = 1;
1519	}
1520}
1521
1522void
1523zstty_diag(void *arg)
1524{
1525	struct zstty_softc *zst = arg;
1526	int overflows, floods;
1527
1528	mutex_spin_enter(&zst->zst_cs->cs_lock);
1529	overflows = zst->zst_overflows;
1530	zst->zst_overflows = 0;
1531	floods = zst->zst_floods;
1532	zst->zst_floods = 0;
1533	zst->zst_errors = 0;
1534	mutex_spin_exit(&zst->zst_cs->cs_lock);
1535
1536	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1537	    device_xname(zst->zst_dev),
1538	    overflows, overflows == 1 ? "" : "s",
1539	    floods, floods == 1 ? "" : "s");
1540}
1541
1542integrate void
1543zstty_rxsoft(struct zstty_softc *zst, struct tty *tp)
1544{
1545	struct zs_chanstate *cs = zst->zst_cs;
1546	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1547	uint8_t *get, *end;
1548	u_int cc, scc;
1549	uint8_t rr1;
1550	int code;
1551
1552	end = zst->zst_ebuf;
1553	get = zst->zst_rbget;
1554	scc = cc = zstty_rbuf_size - zst->zst_rbavail;
1555
1556	if (cc == zstty_rbuf_size) {
1557		zst->zst_floods++;
1558		if (zst->zst_errors++ == 0)
1559			callout_reset(&zst->zst_diag_ch, 60 * hz,
1560			    zstty_diag, zst);
1561	}
1562
1563	/* If not yet open, drop the entire buffer content here */
1564	if (!ISSET(tp->t_state, TS_ISOPEN)) {
1565		get += cc << 1;
1566		if (get >= end)
1567			get -= zstty_rbuf_size << 1;
1568		cc = 0;
1569	}
1570	while (cc) {
1571		code = get[0];
1572		rr1 = get[1];
1573		if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
1574			if (ISSET(rr1, ZSRR1_DO)) {
1575				zst->zst_overflows++;
1576				if (zst->zst_errors++ == 0)
1577					callout_reset(&zst->zst_diag_ch,
1578					    60 * hz, zstty_diag, zst);
1579			}
1580			if (ISSET(rr1, ZSRR1_FE))
1581				SET(code, TTY_FE);
1582			if (ISSET(rr1, ZSRR1_PE))
1583				SET(code, TTY_PE);
1584		}
1585		if ((*rint)(code, tp) == -1) {
1586			/*
1587			 * The line discipline's buffer is out of space.
1588			 */
1589			if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1590				/*
1591				 * We're either not using flow control, or the
1592				 * line discipline didn't tell us to block for
1593				 * some reason.  Either way, we have no way to
1594				 * know when there's more space available, so
1595				 * just drop the rest of the data.
1596				 */
1597				get += cc << 1;
1598				if (get >= end)
1599					get -= zstty_rbuf_size << 1;
1600				cc = 0;
1601			} else {
1602				/*
1603				 * Don't schedule any more receive processing
1604				 * until the line discipline tells us there's
1605				 * space available (through comhwiflow()).
1606				 * Leave the rest of the data in the input
1607				 * buffer.
1608				 */
1609				SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1610			}
1611			break;
1612		}
1613		get += 2;
1614		if (get >= end)
1615			get = zst->zst_rbuf;
1616		cc--;
1617	}
1618
1619	if (cc != scc) {
1620		zst->zst_rbget = get;
1621		mutex_spin_enter(&cs->cs_lock);
1622		cc = zst->zst_rbavail += scc - cc;
1623		/* Buffers should be ok again, release possible block. */
1624		if (cc >= zst->zst_r_lowat) {
1625			if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
1626				CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
1627				SET(cs->cs_preg[1], ZSWR1_RIE);
1628				cs->cs_creg[1] = cs->cs_preg[1];
1629				zs_write_reg(cs, 1, cs->cs_creg[1]);
1630			}
1631			if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
1632				CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
1633				zs_hwiflow(zst);
1634			}
1635		}
1636		mutex_spin_exit(&cs->cs_lock);
1637	}
1638
1639#if 0
1640	printf("%xS%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
1641#endif
1642}
1643
1644integrate void
1645zstty_txsoft(struct zstty_softc *zst, struct tty *tp)
1646{
1647	struct zs_chanstate *cs = zst->zst_cs;
1648
1649	mutex_spin_enter(&cs->cs_lock);
1650	CLR(tp->t_state, TS_BUSY);
1651	if (ISSET(tp->t_state, TS_FLUSH))
1652		CLR(tp->t_state, TS_FLUSH);
1653	else
1654		ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
1655	mutex_spin_exit(&cs->cs_lock);
1656	(*tp->t_linesw->l_start)(tp);
1657}
1658
1659integrate void
1660zstty_stsoft(struct zstty_softc *zst, struct tty *tp)
1661{
1662	struct zs_chanstate *cs = zst->zst_cs;
1663	uint8_t rr0, delta;
1664
1665	mutex_spin_enter(&cs->cs_lock);
1666	rr0 = cs->cs_rr0;
1667	delta = cs->cs_rr0_delta;
1668	cs->cs_rr0_delta = 0;
1669	mutex_spin_exit(&cs->cs_lock);
1670
1671	if (ISSET(delta, cs->cs_rr0_dcd)) {
1672		/*
1673		 * Inform the tty layer that carrier detect changed.
1674		 */
1675		mutex_spin_exit(&tty_lock);
1676		(void) (*tp->t_linesw->l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
1677		mutex_spin_enter(&tty_lock);
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