z8530tty.c revision 1.31
1/*	$NetBSD: z8530tty.c,v 1.31 1997/11/02 08:50:31 mycroft Exp $	*/
2
3/*-
4 * Copyright (c) 1993, 1994, 1995, 1996, 1997
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) 1994 Gordon W. Ross
35 * Copyright (c) 1992, 1993
36 *	The Regents of the University of California.  All rights reserved.
37 *
38 * This software was developed by the Computer Systems Engineering group
39 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
40 * contributed to Berkeley.
41 *
42 * All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 *	This product includes software developed by the University of
45 *	California, Lawrence Berkeley Laboratory.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 *    notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 *    notice, this list of conditions and the following disclaimer in the
54 *    documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 *    must display the following acknowledgement:
57 *	This product includes software developed by the University of
58 *	California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 *    may be used to endorse or promote products derived from this software
61 *    without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 *	@(#)zs.c	8.1 (Berkeley) 7/19/93
76 */
77
78/*
79 * Zilog Z8530 Dual UART driver (tty interface)
80 *
81 * This is the "slave" driver that will be attached to
82 * the "zsc" driver for plain "tty" async. serial lines.
83 *
84 * Credits, history:
85 *
86 * The original version of this code was the sparc/dev/zs.c driver
87 * as distributed with the Berkeley 4.4 Lite release.  Since then,
88 * Gordon Ross reorganized the code into the current parent/child
89 * driver scheme, separating the Sun keyboard and mouse support
90 * into independent child drivers.
91 *
92 * RTS/CTS flow-control support was a collaboration of:
93 *	Gordon Ross <gwr@netbsd.org>,
94 *	Bill Studenmund <wrstuden@loki.stanford.edu>
95 *	Ian Dall <Ian.Dall@dsto.defence.gov.au>
96 */
97
98#include <sys/param.h>
99#include <sys/systm.h>
100#include <sys/proc.h>
101#include <sys/device.h>
102#include <sys/conf.h>
103#include <sys/file.h>
104#include <sys/ioctl.h>
105#include <sys/malloc.h>
106#include <sys/tty.h>
107#include <sys/time.h>
108#include <sys/kernel.h>
109#include <sys/syslog.h>
110
111#include <dev/ic/z8530reg.h>
112#include <machine/z8530var.h>
113
114#include "locators.h"
115
116/*
117 * How many input characters we can buffer.
118 * The port-specific var.h may override this.
119 * Note: must be a power of two!
120 */
121#ifndef	ZSTTY_RING_SIZE
122#define	ZSTTY_RING_SIZE	2048
123#endif
124
125/*
126 * Make this an option variable one can patch.
127 * But be warned:  this must be a power of 2!
128 */
129int zstty_rbuf_size = ZSTTY_RING_SIZE;
130
131/* This should usually be 3/4 of ZSTTY_RING_SIZE */
132int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE - (ZSTTY_RING_SIZE >> 2));
133
134struct zstty_softc {
135	struct	device zst_dev;		/* required first: base device */
136	struct  tty *zst_tty;
137	struct	zs_chanstate *zst_cs;
138
139	int zst_hwflags;	/* see z8530var.h */
140	int zst_swflags;	/* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
141
142	/*
143	 * Printing an overrun error message often takes long enough to
144	 * cause another overrun, so we only print one per second.
145	 */
146	long	zst_rotime;		/* time of last ring overrun */
147	long	zst_fotime;		/* time of last fifo overrun */
148
149	/*
150	 * The receive ring buffer.
151	 */
152	int	zst_rbget;	/* ring buffer `get' index */
153	volatile int	zst_rbput;	/* ring buffer `put' index */
154	int	zst_ringmask;
155	int	zst_rbhiwat;
156
157	u_short	*zst_rbuf; /* rr1, data pairs */
158
159	/*
160	 * The transmit byte count and address are used for pseudo-DMA
161	 * output in the hardware interrupt code.  PDMA can be suspended
162	 * to get pending changes done; heldtbc is used for this.  It can
163	 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
164	 */
165	int 	zst_tbc;			/* transmit byte count */
166	u_char *zst_tba;			/* transmit buffer address */
167	int 	zst_heldtbc;		/* held tbc while xmission stopped */
168
169	/* Flags to communicate with zstty_softint() */
170	volatile char zst_rx_blocked;	/* input block at ring */
171	volatile char zst_rx_overrun;	/* ring overrun */
172	volatile char zst_tx_busy;	/* working on an output chunk */
173	volatile char zst_tx_done;	/* done with one output chunk */
174	volatile char zst_tx_stopped;	/* H/W level stop (lost CTS) */
175	volatile char zst_st_check;	/* got a status interrupt */
176	char pad[2];
177};
178
179
180/* Definition of the driver for autoconfig. */
181#ifdef	__BROKEN_INDIRECT_CONFIG
182static int	zstty_match(struct device *, void *, void *);
183#else
184static int	zstty_match(struct device *, struct cfdata *, void *);
185#endif
186static void	zstty_attach(struct device *, struct device *, void *);
187
188struct cfattach zstty_ca = {
189	sizeof(struct zstty_softc), zstty_match, zstty_attach
190};
191
192struct cfdriver zstty_cd = {
193	NULL, "zstty", DV_TTY
194};
195
196struct zsops zsops_tty;
197
198/* Routines called from other code. */
199cdev_decl(zs);	/* open, close, read, write, ioctl, stop, ... */
200
201static void	zsstart __P((struct tty *));
202static int	zsparam __P((struct tty *, struct termios *));
203static void zs_modem __P((struct zstty_softc *zst, int onoff));
204static int	zshwiflow __P((struct tty *, int));
205static void zs_hwiflow __P((struct zstty_softc *));
206
207/*
208 * zstty_match: how is this zs channel configured?
209 */
210#ifdef	__BROKEN_INDIRECT_CONFIG
211int
212zstty_match(parent, vcf, aux)
213	struct device *parent;
214	void   *vcf, *aux;
215{
216	struct cfdata *cf = vcf;
217	struct zsc_attach_args *args = aux;
218
219	/* Exact match is better than wildcard. */
220	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
221		return 2;
222
223	/* This driver accepts wildcard. */
224	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
225		return 1;
226
227	return 0;
228}
229#else	/* __BROKEN_INDIRECT_CONFIG */
230int
231zstty_match(parent, cf, aux)
232	struct device *parent;
233	struct cfdata *cf;
234	void   *aux;
235{
236	struct zsc_attach_args *args = aux;
237
238	/* Exact match is better than wildcard. */
239	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
240		return 2;
241
242	/* This driver accepts wildcard. */
243	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
244		return 1;
245
246	return 0;
247}
248#endif	/* __BROKEN_INDIRECT_CONFIG */
249
250void
251zstty_attach(parent, self, aux)
252	struct device *parent, *self;
253	void   *aux;
254
255{
256	struct zsc_softc *zsc = (void *) parent;
257	struct zstty_softc *zst = (void *) self;
258	struct cfdata *cf = self->dv_cfdata;
259	struct zsc_attach_args *args = aux;
260	struct zs_chanstate *cs;
261	struct tty *tp;
262	int channel, tty_unit;
263	dev_t dev;
264
265	tty_unit = zst->zst_dev.dv_unit;
266	channel = args->channel;
267	cs = zsc->zsc_cs[channel];
268	cs->cs_private = zst;
269	cs->cs_ops = &zsops_tty;
270
271	zst->zst_cs = cs;
272	zst->zst_swflags = cf->cf_flags;	/* softcar, etc. */
273	zst->zst_hwflags = args->hwflags;
274	dev = makedev(zs_major, tty_unit);
275
276	if (zst->zst_swflags)
277		printf(" flags 0x%x", zst->zst_swflags);
278
279	if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE)
280		printf(" (console)");
281	else {
282#ifdef KGDB
283		/*
284		 * Allow kgdb to "take over" this port.  Returns true
285		 * if this serial port is in-use by kgdb.
286		 */
287		if (zs_check_kgdb(cs, dev)) {
288			printf(" (kgdb)\n");
289			/*
290			 * This is the kgdb port (exclusive use)
291			 * so skip the normal attach code.
292			 */
293			return;
294		}
295#endif
296	}
297	printf("\n");
298
299	tp = ttymalloc();
300	tp->t_dev = dev;
301	tp->t_oproc = zsstart;
302	tp->t_param = zsparam;
303	tp->t_hwiflow = zshwiflow;
304	tty_attach(tp);
305
306	zst->zst_tty = tp;
307	zst->zst_rbhiwat =  zstty_rbuf_size;	/* impossible value */
308	zst->zst_ringmask = zstty_rbuf_size - 1;
309	zst->zst_rbuf = malloc(zstty_rbuf_size * sizeof(zst->zst_rbuf[0]),
310			      M_DEVBUF, M_WAITOK);
311
312	/* XXX - Do we need an MD hook here? */
313
314	/*
315	 * Hardware init
316	 */
317	if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE) {
318		/* Call zsparam similar to open. */
319		struct termios t;
320
321		/* Make console output work while closed. */
322		zst->zst_swflags |= TIOCFLAG_SOFTCAR;
323		/* Setup the "new" parameters in t. */
324		bzero((void*)&t, sizeof(t));
325		t.c_cflag  = cs->cs_defcflag;
326		t.c_ospeed = cs->cs_defspeed;
327		/* Enable interrupts. */
328		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
329		/* Make sure zsparam will see changes. */
330		tp->t_ospeed = 0;
331		(void)zsparam(tp, &t);
332	} else {
333		/* Not the console; may need reset. */
334		int reset, s;
335		reset = (channel == 0) ?
336			ZSWR9_A_RESET : ZSWR9_B_RESET;
337		s = splzs();
338		zs_write_reg(cs, 9, reset);
339		splx(s);
340	}
341
342	/*
343	 * Initialize state of modem control lines (DTR).
344	 * If softcar is set, turn on DTR now and leave it.
345	 * otherwise, turn off DTR now, and raise in open.
346	 * (Keeps modem from answering too early.)
347	 */
348	zs_modem(zst, (zst->zst_swflags & TIOCFLAG_SOFTCAR) ? 1 : 0);
349}
350
351
352/*
353 * Return pointer to our tty.
354 */
355struct tty *
356zstty(dev)
357	dev_t dev;
358{
359	struct zstty_softc *zst;
360	int unit = minor(dev);
361
362#ifdef	DIAGNOSTIC
363	if (unit >= zstty_cd.cd_ndevs)
364		panic("zstty");
365#endif
366	zst = zstty_cd.cd_devs[unit];
367	return (zst->zst_tty);
368}
369
370
371/*
372 * Open a zs serial (tty) port.
373 */
374int
375zsopen(dev, flags, mode, p)
376	dev_t dev;
377	int flags;
378	int mode;
379	struct proc *p;
380{
381	register struct tty *tp;
382	register struct zs_chanstate *cs;
383	struct zstty_softc *zst;
384	int error, s, s2, unit;
385
386	unit = minor(dev);
387	if (unit >= zstty_cd.cd_ndevs)
388		return (ENXIO);
389	zst = zstty_cd.cd_devs[unit];
390	if (zst == NULL)
391		return (ENXIO);
392	tp = zst->zst_tty;
393	cs = zst->zst_cs;
394
395	/* If KGDB took the line, then tp==NULL */
396	if (tp == NULL)
397		return (EBUSY);
398
399	if ((tp->t_state & TS_ISOPEN) != 0 &&
400	    (tp->t_state & TS_XCLUDE) != 0 &&
401	    p->p_ucred->cr_uid != 0)
402		return (EBUSY);
403
404	s = spltty();
405
406	if ((tp->t_state & TS_ISOPEN) == 0) {
407		/* First open. */
408		struct termios t;
409
410		s2 = splzs();
411
412		/* Turn on interrupts. */
413		cs->cs_creg[1] = cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
414		zs_write_reg(cs, 1, cs->cs_creg[1]);
415
416		/* Fetch the current modem control status, needed later. */
417		cs->cs_rr0 = zs_read_csr(cs);
418
419		splx(s2);
420
421		/*
422		 * Setup the "new" parameters in t.
423		 * Can not use tp->t because zsparam
424		 * deals only with what has changed.
425		 */
426		t.c_ispeed = 0;
427		t.c_ospeed = cs->cs_defspeed;
428		t.c_cflag = cs->cs_defcflag;
429		if (zst->zst_swflags & TIOCFLAG_CLOCAL)
430			t.c_cflag |= CLOCAL;
431		if (zst->zst_swflags & TIOCFLAG_CRTSCTS)
432			t.c_cflag |= CRTSCTS;
433		if (zst->zst_swflags & TIOCFLAG_MDMBUF)
434			t.c_cflag |= MDMBUF;
435		/* Make sure zsparam will see changes. */
436		tp->t_ospeed = 0;
437		(void) zsparam(tp, &t);
438		/*
439		 * Note: zsparam has done: cflag, ispeed, ospeed
440		 * so we just need to do: iflag, oflag, lflag, cc
441		 * For "raw" mode, just leave all zeros.
442		 */
443		if ((zst->zst_hwflags & ZS_HWFLAG_RAW) == 0) {
444			tp->t_iflag = TTYDEF_IFLAG;
445			tp->t_oflag = TTYDEF_OFLAG;
446			tp->t_lflag = TTYDEF_LFLAG;
447		}
448		ttychars(tp);
449		ttsetwater(tp);
450
451		/*
452		 * Turn on DTR.  We must always do this, even if carrier is not
453		 * present, because otherwise we'd have to use TIOCSDTR
454		 * immediately after setting CLOCAL, which applications do not
455		 * expect.  We always assert DTR while the device is open
456		 * unless explicitly requested to deassert it.
457		 */
458		zs_modem(zst, 1);
459
460		s2 = splzs();
461
462		/* Clear the input ring, and unblock. */
463		zst->zst_rbget = zst->zst_rbput;
464		zs_iflush(cs);
465		zst->zst_rx_blocked = 0;
466		zs_hwiflow(zst);
467
468		splx(s2);
469	}
470	error = 0;
471
472	/* If we're doing a blocking open... */
473	if ((flags & O_NONBLOCK) == 0)
474		/* ...then wait for carrier. */
475		while ((tp->t_state & TS_CARR_ON) == 0 &&
476		    (tp->t_cflag & (CLOCAL | MDMBUF)) == 0) {
477			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
478			    ttopen, 0);
479			if (error) {
480				/*
481				 * If the open was interrupted and nobody
482				 * else has the device open, then hang up.
483				 */
484				if ((tp->t_state & TS_ISOPEN) == 0) {
485					zs_modem(zst, 0);
486					tp->t_state &= ~TS_WOPEN;
487					ttwakeup(tp);
488				}
489				break;
490			}
491			tp->t_state |= TS_WOPEN;
492		}
493
494	splx(s);
495	if (error == 0)
496		error = (*linesw[tp->t_line].l_open)(dev, tp);
497	return (error);
498}
499
500/*
501 * Close a zs serial port.
502 */
503int
504zsclose(dev, flags, mode, p)
505	dev_t dev;
506	int flags;
507	int mode;
508	struct proc *p;
509{
510	struct zstty_softc *zst;
511	register struct zs_chanstate *cs;
512	register struct tty *tp;
513	int s;
514
515	zst = zstty_cd.cd_devs[minor(dev)];
516	cs = zst->zst_cs;
517	tp = zst->zst_tty;
518
519	/* XXX This is for cons.c. */
520	if ((tp->t_state & TS_ISOPEN) == 0)
521		return 0;
522
523	(*linesw[tp->t_line].l_close)(tp, flags);
524	ttyclose(tp);
525
526	s = splzs();
527
528	/* If we were asserting flow control, then deassert it. */
529	zst->zst_rx_blocked = 1;
530	zs_hwiflow(zst);
531
532	splx(s);
533
534	/* Clear any break condition set with TIOCSBRK. */
535	zs_break(cs, 0);
536
537	/*
538	 * Hang up if necessary.  Wait a bit, so the other side has time to
539	 * notice even if we immediately open the port again.
540	 */
541	if ((tp->t_cflag & HUPCL) != 0) {
542		zs_modem(zst, 0);
543		(void) tsleep(cs, TTIPRI, ttclos, hz);
544	}
545
546	s = splzs();
547
548	/* Turn off interrupts. */
549	cs->cs_creg[1] = cs->cs_preg[1] = 0;
550	zs_write_reg(cs, 1, cs->cs_creg[1]);
551
552	splx(s);
553
554	return (0);
555}
556
557/*
558 * Read/write zs serial port.
559 */
560int
561zsread(dev, uio, flags)
562	dev_t dev;
563	struct uio *uio;
564	int flags;
565{
566	register struct zstty_softc *zst;
567	register struct tty *tp;
568
569	zst = zstty_cd.cd_devs[minor(dev)];
570	tp = zst->zst_tty;
571	return (linesw[tp->t_line].l_read(tp, uio, flags));
572}
573
574int
575zswrite(dev, uio, flags)
576	dev_t dev;
577	struct uio *uio;
578	int flags;
579{
580	register struct zstty_softc *zst;
581	register struct tty *tp;
582
583	zst = zstty_cd.cd_devs[minor(dev)];
584	tp = zst->zst_tty;
585	return (linesw[tp->t_line].l_write(tp, uio, flags));
586}
587
588int
589zsioctl(dev, cmd, data, flag, p)
590	dev_t dev;
591	u_long cmd;
592	caddr_t data;
593	int flag;
594	struct proc *p;
595{
596	register struct zstty_softc *zst;
597	register struct zs_chanstate *cs;
598	register struct tty *tp;
599	register struct linesw *line;
600	register int error;
601
602	zst = zstty_cd.cd_devs[minor(dev)];
603	cs = zst->zst_cs;
604	tp = zst->zst_tty;
605	line = &linesw[tp->t_line];
606
607	error = (*line->l_ioctl)(tp, cmd, data, flag, p);
608	if (error >= 0)
609		return (error);
610
611	error = ttioctl(tp, cmd, data, flag, p);
612	if (error >= 0)
613		return (error);
614
615#ifdef	ZS_MD_IOCTL
616	error = ZS_MD_IOCTL;
617	if (error >= 0)
618		return (error);
619#endif	/* ZS_MD_IOCTL */
620
621	switch (cmd) {
622	case TIOCSBRK:
623		zs_break(cs, 1);
624		break;
625
626	case TIOCCBRK:
627		zs_break(cs, 0);
628		break;
629
630	case TIOCGFLAGS:
631		*(int *)data = zst->zst_swflags;
632		break;
633
634	case TIOCSFLAGS:
635		error = suser(p->p_ucred, &p->p_acflag);
636		if (error)
637			return (error);
638		zst->zst_swflags = *(int *)data;
639		break;
640
641	case TIOCSDTR:
642		zs_modem(zst, 1);
643		break;
644
645	case TIOCCDTR:
646		zs_modem(zst, 0);
647		break;
648
649	case TIOCMSET:
650	case TIOCMBIS:
651	case TIOCMBIC:
652	case TIOCMGET:
653	default:
654		return (ENOTTY);
655	}
656	return (0);
657}
658
659/*
660 * Start or restart transmission.
661 */
662static void
663zsstart(tp)
664	register struct tty *tp;
665{
666	register struct zstty_softc *zst;
667	register struct zs_chanstate *cs;
668	register int s;
669
670	zst = zstty_cd.cd_devs[minor(tp->t_dev)];
671	cs = zst->zst_cs;
672
673	s = spltty();
674	if ((tp->t_state & TS_BUSY) != 0)
675		goto out;
676	if ((tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) != 0)
677		goto stopped;
678
679	if (zst->zst_tx_stopped)
680		goto stopped;
681
682	if (tp->t_outq.c_cc <= tp->t_lowat) {
683		if ((tp->t_state & TS_ASLEEP) != 0) {
684			tp->t_state &= ~TS_ASLEEP;
685			wakeup((caddr_t)&tp->t_outq);
686		}
687		selwakeup(&tp->t_wsel);
688		if (tp->t_outq.c_cc == 0)
689			goto stopped;
690	}
691
692	/* Grab the first contiguous region of buffer space. */
693	{
694		u_char *tba;
695		int tbc;
696
697		tba = tp->t_outq.c_cf;
698		tbc = ndqb(&tp->t_outq, 0);
699
700		(void) splzs();
701
702		zst->zst_tba = tba;
703		zst->zst_tbc = tbc;
704	}
705
706	tp->t_state |= TS_BUSY;
707	zst->zst_tx_busy = 1;
708
709	/* Enable transmit completion interrupts if necessary. */
710	if ((cs->cs_preg[1] & ZSWR1_TIE) == 0) {
711		cs->cs_preg[1] |= ZSWR1_TIE;
712		cs->cs_creg[1] = cs->cs_preg[1];
713		zs_write_reg(cs, 1, cs->cs_creg[1]);
714	}
715
716	/* Output the first character of the contiguous buffer. */
717	zs_write_data(cs, *zst->zst_tba);
718	zst->zst_tbc--;
719	zst->zst_tba++;
720	splx(s);
721	return;
722
723stopped:
724	/* Disable transmit completion interrupts if necessary. */
725	if ((cs->cs_preg[1] & ZSWR1_TIE) != 0) {
726		cs->cs_preg[1] &= ~ZSWR1_TIE;
727		cs->cs_creg[1] = cs->cs_preg[1];
728		zs_write_reg(cs, 1, cs->cs_creg[1]);
729	}
730out:
731	splx(s);
732	return;
733}
734
735/*
736 * Stop output, e.g., for ^S or output flush.
737 */
738void
739zsstop(tp, flag)
740	struct tty *tp;
741	int flag;
742{
743	register struct zstty_softc *zst;
744	register struct zs_chanstate *cs;
745	register int s;
746
747	zst = zstty_cd.cd_devs[minor(tp->t_dev)];
748	cs = zst->zst_cs;
749
750	s = splzs();
751	if (tp->t_state & TS_BUSY) {
752		/*
753		 * Device is transmitting; must stop it.
754		 * Also clear _heldtbc to prevent any
755		 * flow-control event from resuming.
756		 */
757		zst->zst_tbc = 0;
758		zst->zst_heldtbc = 0;
759		if ((tp->t_state & TS_TTSTOP) == 0)
760			tp->t_state |= TS_FLUSH;
761	}
762	splx(s);
763}
764
765/*
766 * Set ZS tty parameters from termios.
767 * XXX - Should just copy the whole termios after
768 * making sure all the changes could be done.
769 */
770static int
771zsparam(tp, t)
772	register struct tty *tp;
773	register struct termios *t;
774{
775	struct zstty_softc *zst;
776	struct zs_chanstate *cs;
777	register struct linesw *line;
778	int s, bps, cflag, error;
779	u_char tmp3, tmp4, tmp5;
780
781	zst = zstty_cd.cd_devs[minor(tp->t_dev)];
782	cs = zst->zst_cs;
783	line = &linesw[tp->t_line];
784	bps = t->c_ospeed;
785	cflag = t->c_cflag;
786
787	if (bps < 0 || (t->c_ispeed && t->c_ispeed != bps))
788		return (EINVAL);
789
790	/*
791	 * For the console, always force CLOCAL and !HUPCL, so that the port
792	 * is always active.
793	 */
794	if ((zst->zst_swflags & TIOCFLAG_SOFTCAR) != 0 ||
795	    (zst->zst_hwflags & (ZS_HWFLAG_NO_DCD | ZS_HWFLAG_CONSOLE)) != 0) {
796		t->c_cflag |= CLOCAL;
797		t->c_cflag &= ~HUPCL;
798	}
799
800	/*
801	 * Only whack the UART when params change.
802	 * Some callers need to clear tp->t_ospeed
803	 * to make sure initialization gets done.
804	 */
805	if (tp->t_ospeed == bps &&
806	    tp->t_cflag == cflag)
807		return (0);
808
809	/*
810	 * Call MD functions to deal with changed
811	 * clock modes or H/W flow control modes.
812	 * The BRG divisor is set now. (reg 12,13)
813	 */
814	error = zs_set_speed(cs, bps);
815	if (error)
816		return (error);
817	error = zs_set_modes(cs, cflag);
818	if (error)
819		return (error);
820
821	/* OK, we are now committed to do it. */
822	tp->t_cflag = cflag;
823	tp->t_ospeed = bps;
824	tp->t_ispeed = bps;
825
826	/*
827	 * Block interrupts so that state will not
828	 * be altered until we are done setting it up.
829	 *
830	 * Initial values in cs_preg are set before
831	 * our attach routine is called.  The master
832	 * interrupt enable is handled by zsc.c
833	 *
834	 */
835	s = splzs();
836
837	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
838	if ((cs->cs_rr0_mask & ZSRR0_DCD) != 0)
839		cs->cs_preg[15] |= ZSWR15_DCD_IE;
840	else
841		cs->cs_preg[15] &= ~ZSWR15_DCD_IE;
842	if ((cs->cs_rr0_mask & ZSRR0_CTS) != 0)
843		cs->cs_preg[15] |= ZSWR15_CTS_IE;
844	else
845		cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
846
847	/* Recompute character size bits. */
848	tmp3 = cs->cs_preg[3] & ~ZSWR3_RXSIZE;
849	tmp5 = cs->cs_preg[5] & ~ZSWR5_TXSIZE;
850	switch (cflag & CSIZE) {
851	case CS5:
852		/* These are |= 0 but let the optimizer deal with it. */
853		tmp3 |= ZSWR3_RX_5;
854		tmp5 |= ZSWR5_TX_5;
855		break;
856	case CS6:
857		tmp3 |= ZSWR3_RX_6;
858		tmp5 |= ZSWR5_TX_6;
859		break;
860	case CS7:
861		tmp3 |= ZSWR3_RX_7;
862		tmp5 |= ZSWR5_TX_7;
863		break;
864	case CS8:
865	default:
866		tmp3 |= ZSWR3_RX_8;
867		tmp5 |= ZSWR5_TX_8;
868		break;
869	}
870
871#if 0
872	/* Raise or lower DTR and RTS as appropriate. */
873	if (bps) {
874		/* Raise DTR and RTS */
875		tmp5 |= cs->cs_wr5_dtr;
876	} else {
877		/* Drop DTR and RTS */
878		/* XXX: Should SOFTCAR prevent this? */
879		tmp5 &= ~cs->cs_wr5_dtr;
880	}
881#endif
882
883	cs->cs_preg[3] = tmp3;
884	cs->cs_preg[5] = tmp5;
885
886	/*
887	 * Recompute the stop bits and parity bits.  Note that
888	 * zs_set_speed() may have set clock selection bits etc.
889	 * in wr4, so those must preserved.
890	 */
891	tmp4 = cs->cs_preg[4];
892	/* Recompute stop bits. */
893	tmp4 &= ~ZSWR4_SBMASK;
894	tmp4 |= (cflag & CSTOPB) ?
895		ZSWR4_TWOSB : ZSWR4_ONESB;
896	/* Recompute parity bits. */
897	tmp4 &= ~ZSWR4_PARMASK;
898	if ((cflag & PARODD) == 0)
899		tmp4 |= ZSWR4_EVENP;
900	if (cflag & PARENB)
901		tmp4 |= ZSWR4_PARENB;
902	cs->cs_preg[4] = tmp4;
903
904	/* The MD function zs_set_modes handled CRTSCTS, etc. */
905
906	/*
907	 * If nothing is being transmitted, set up new current values,
908	 * else mark them as pending.
909	 */
910	if (!cs->cs_heldchange) {
911		if (zst->zst_tx_busy) {
912			zst->zst_heldtbc = zst->zst_tbc;
913			zst->zst_tbc = 0;
914			cs->cs_heldchange = 1;
915		} else
916			zs_loadchannelregs(cs);
917	}
918
919	splx(s);
920
921	/*
922	 * Update the tty layer's idea of the carrier bit, in case we changed
923	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that if we
924	 * lose carrier while carrier detection is on.
925	 */
926	(void) (*line->l_modem)(tp, (cs->cs_rr0 & cs->cs_rr0_dcd) != 0);
927
928	/* If we can throttle input, enable "high water" detection. */
929	if (cflag & CHWFLOW) {
930		zst->zst_rbhiwat = zstty_rbuf_hiwat;
931	} else {
932		/* This impossible value prevents a "high water" trigger. */
933		zst->zst_rbhiwat = zstty_rbuf_size;
934		if (zst->zst_rx_blocked) {
935			zst->zst_rx_blocked = 0;
936			zs_hwiflow(zst);
937		}
938		if (zst->zst_tx_stopped) {
939			zst->zst_tx_stopped = 0;
940			zsstart(tp);
941		}
942	}
943
944	return (0);
945}
946
947/*
948 * Raise or lower modem control (DTR/RTS) signals.  If a character is
949 * in transmission, the change is deferred.
950 */
951static void
952zs_modem(zst, onoff)
953	struct zstty_softc *zst;
954	int onoff;
955{
956	struct zs_chanstate *cs;
957	int s;
958
959	cs = zst->zst_cs;
960	if (cs->cs_wr5_dtr == 0)
961		return;
962
963	s = splzs();
964	if (onoff)
965		cs->cs_preg[5] |= cs->cs_wr5_dtr;
966	else
967		cs->cs_preg[5] &= ~cs->cs_wr5_dtr;
968
969	if (!cs->cs_heldchange) {
970		if (zst->zst_tx_busy) {
971			zst->zst_heldtbc = zst->zst_tbc;
972			zst->zst_tbc = 0;
973			cs->cs_heldchange = 1;
974		} else
975			zs_loadchannelregs(cs);
976	}
977	splx(s);
978}
979
980/*
981 * Try to block or unblock input using hardware flow-control.
982 * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
983 * if this function returns non-zero, the TS_TBLOCK flag will
984 * be set or cleared according to the "block" arg passed.
985 */
986int
987zshwiflow(tp, block)
988	struct tty *tp;
989	int block;
990{
991	register struct zstty_softc *zst;
992	register struct zs_chanstate *cs;
993	int s;
994
995	zst = zstty_cd.cd_devs[minor(tp->t_dev)];
996	cs = zst->zst_cs;
997	if (cs->cs_wr5_rts == 0)
998		return (0);
999
1000	s = splzs();
1001	if (block) {
1002		if (!zst->zst_rx_blocked) {
1003			zst->zst_rx_blocked = 1;
1004			zs_hwiflow(zst);
1005		}
1006	} else {
1007		if (zst->zst_rx_blocked) {
1008			zst->zst_rx_blocked = 0;
1009			zs_hwiflow(zst);
1010		}
1011	}
1012	splx(s);
1013	return 1;
1014}
1015
1016/*
1017 * Internal version of zshwiflow
1018 * called at splzs
1019 */
1020static void
1021zs_hwiflow(zst)
1022	register struct zstty_softc *zst;
1023{
1024	register struct zs_chanstate *cs;
1025
1026	cs = zst->zst_cs;
1027	if (cs->cs_wr5_rts == 0)
1028		return;
1029
1030	if (zst->zst_rx_blocked) {
1031		cs->cs_preg[5] &= ~cs->cs_wr5_rts;
1032		cs->cs_creg[5] &= ~cs->cs_wr5_rts;
1033	} else {
1034		cs->cs_preg[5] |= cs->cs_wr5_rts;
1035		cs->cs_creg[5] |= cs->cs_wr5_rts;
1036	}
1037	zs_write_reg(cs, 5, cs->cs_creg[5]);
1038}
1039
1040
1041/****************************************************************
1042 * Interface to the lower layer (zscc)
1043 ****************************************************************/
1044
1045static void zstty_rxint __P((struct zs_chanstate *));
1046static void zstty_txint __P((struct zs_chanstate *));
1047static void zstty_stint __P((struct zs_chanstate *));
1048static void zstty_softint  __P((struct zs_chanstate *));
1049
1050static void zsoverrun __P((struct zstty_softc *, long *, char *));
1051
1052/*
1053 * receiver ready interrupt.
1054 * called at splzs
1055 */
1056static void
1057zstty_rxint(cs)
1058	register struct zs_chanstate *cs;
1059{
1060	register struct zstty_softc *zst;
1061	register int cc, put, put_next, ringmask;
1062	register u_char c, rr0, rr1;
1063	register u_short ch_rr1;
1064
1065	zst = cs->cs_private;
1066	put = zst->zst_rbput;
1067	ringmask = zst->zst_ringmask;
1068
1069nextchar:
1070
1071	/*
1072	 * First read the status, because reading the received char
1073	 * destroys the status of this char.
1074	 */
1075	rr1 = zs_read_reg(cs, 1);
1076	c = zs_read_data(cs);
1077	ch_rr1 = (c << 8) | rr1;
1078
1079	if (ch_rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
1080		/* Clear the receive error. */
1081		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
1082	}
1083
1084	/* XXX: Check for the stop character? */
1085
1086	zst->zst_rbuf[put] = ch_rr1;
1087	put_next = (put + 1) & ringmask;
1088
1089	/* Would overrun if increment makes (put==get). */
1090	if (put_next == zst->zst_rbget) {
1091		zst->zst_rx_overrun = 1;
1092	} else {
1093		/* OK, really increment. */
1094		put = put_next;
1095	}
1096
1097	/* Keep reading until the FIFO is empty. */
1098	rr0 = zs_read_csr(cs);
1099	if (rr0 & ZSRR0_RX_READY)
1100		goto nextchar;
1101
1102	/* Done reading. */
1103	zst->zst_rbput = put;
1104
1105	/*
1106	 * If ring is getting too full, try to block input.
1107	 */
1108	cc = put - zst->zst_rbget;
1109	if (cc < 0)
1110		cc += zstty_rbuf_size;
1111	if ((cc > zst->zst_rbhiwat) && (zst->zst_rx_blocked == 0)) {
1112		zst->zst_rx_blocked = 1;
1113		zs_hwiflow(zst);
1114	}
1115
1116	/* Ask for softint() call. */
1117	cs->cs_softreq = 1;
1118}
1119
1120/*
1121 * transmitter ready interrupt.  (splzs)
1122 */
1123static void
1124zstty_txint(cs)
1125	register struct zs_chanstate *cs;
1126{
1127	register struct zstty_softc *zst;
1128	register int count;
1129
1130	zst = cs->cs_private;
1131
1132	/*
1133	 * If we suspended output for a "held" change,
1134	 * then handle that now and resume.
1135	 * Do flow-control changes ASAP.
1136	 * When the only change is for flow control,
1137	 * avoid hitting other registers, because that
1138	 * often makes the stupid zs drop input...
1139	 */
1140	if (cs->cs_heldchange) {
1141		zs_loadchannelregs(cs);
1142		cs->cs_heldchange = 0;
1143		count = zst->zst_heldtbc;
1144	} else
1145		count = zst->zst_tbc;
1146
1147	/*
1148	 * If our transmit buffer still has data,
1149	 * just send the next character.
1150	 */
1151	if (count > 0) {
1152		/* Send the next char. */
1153		zst->zst_tbc = --count;
1154		zs_write_data(cs, *zst->zst_tba);
1155		zst->zst_tba++;
1156		return;
1157	}
1158
1159	zs_write_csr(cs, ZSWR0_RESET_TXINT);
1160
1161	/* Ask the softint routine for more output. */
1162	zst->zst_tx_busy = 0;
1163	zst->zst_tx_done = 1;
1164	cs->cs_softreq = 1;
1165}
1166
1167/*
1168 * status change interrupt.  (splzs)
1169 */
1170static void
1171zstty_stint(cs)
1172	register struct zs_chanstate *cs;
1173{
1174	register struct zstty_softc *zst;
1175	register u_char rr0, delta;
1176
1177	zst = cs->cs_private;
1178
1179	rr0 = zs_read_csr(cs);
1180	zs_write_csr(cs, ZSWR0_RESET_STATUS);
1181
1182	/*
1183	 * Check here for console break, so that we can abort
1184	 * even when interrupts are locking up the machine.
1185	 */
1186	if ((rr0 & ZSRR0_BREAK) &&
1187		(zst->zst_hwflags & ZS_HWFLAG_CONSOLE))
1188	{
1189		zs_abort(cs);
1190		return;
1191	}
1192
1193	delta = rr0 ^ cs->cs_rr0;
1194	cs->cs_rr0 = rr0;
1195	if ((delta & cs->cs_rr0_mask) != 0) {
1196		cs->cs_rr0_delta |= delta;
1197
1198		/*
1199		 * Stop output immediately if we lose the output
1200		 * flow control signal or carrier detect.
1201		 */
1202		if ((~rr0 & cs->cs_rr0_mask) != 0) {
1203			zst->zst_tbc = 0;
1204			zst->zst_heldtbc = 0;
1205		}
1206
1207		zst->zst_st_check = 1;
1208	}
1209
1210	/* Ask for softint() call. */
1211	cs->cs_softreq = 1;
1212}
1213
1214/*
1215 * Print out a ring or fifo overrun error message.
1216 */
1217static void
1218zsoverrun(zst, ptime, what)
1219	struct zstty_softc *zst;
1220	long *ptime;
1221	char *what;
1222{
1223
1224	if (*ptime != time.tv_sec) {
1225		*ptime = time.tv_sec;
1226		log(LOG_WARNING, "%s: %s overrun\n",
1227			zst->zst_dev.dv_xname, what);
1228	}
1229}
1230
1231/*
1232 * Software interrupt.  Called at zssoft
1233 *
1234 * The main job to be done here is to empty the input ring
1235 * by passing its contents up to the tty layer.  The ring is
1236 * always emptied during this operation, therefore the ring
1237 * must not be larger than the space after "high water" in
1238 * the tty layer, or the tty layer might drop our input.
1239 *
1240 * Note: an "input blockage" condition is assumed to exist if
1241 * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
1242 */
1243static void
1244zstty_softint(cs)
1245	struct zs_chanstate *cs;
1246{
1247	register struct zstty_softc *zst;
1248	register struct tty *tp;
1249	register struct linesw *line;
1250	register int get, c, s, t;
1251	int ringmask, overrun;
1252	register u_short ring_data;
1253	register u_char rr0, delta;
1254
1255	zst = cs->cs_private;
1256	tp = zst->zst_tty;
1257	line = &linesw[tp->t_line];
1258	ringmask = zst->zst_ringmask;
1259	overrun = 0;
1260
1261	/*
1262	 * Raise to tty priority while servicing the ring.
1263	 */
1264	s = spltty();
1265
1266	if (zst->zst_rx_overrun) {
1267		zst->zst_rx_overrun = 0;
1268		zsoverrun(zst, &zst->zst_rotime, "ring");
1269	}
1270
1271	/*
1272	 * Copy data from the receive ring into the tty layer.
1273	 */
1274	get = zst->zst_rbget;
1275	while (get != zst->zst_rbput) {
1276		ring_data = zst->zst_rbuf[get];
1277		get = (get + 1) & ringmask;
1278
1279		if (ring_data & ZSRR1_DO)
1280			overrun++;
1281		/* low byte of ring_data is rr1 */
1282		c = (ring_data >> 8) & 0xff;
1283		if (ring_data & ZSRR1_FE)
1284			c |= TTY_FE;
1285		if (ring_data & ZSRR1_PE)
1286			c |= TTY_PE;
1287
1288		(*line->l_rint)(c, tp);
1289	}
1290	zst->zst_rbget = get;
1291
1292	/*
1293	 * If the overrun flag is set now, it was set while
1294	 * copying char/status pairs from the ring, which
1295	 * means this was a hardware (fifo) overrun.
1296	 */
1297	if (overrun) {
1298		zsoverrun(zst, &zst->zst_fotime, "fifo");
1299	}
1300
1301	/*
1302	 * We have emptied the input ring.  Maybe unblock input.
1303	 * Note: an "input blockage" condition is assumed to exist
1304	 * when EITHER zst_rx_blocked or the TS_TBLOCK flag is set,
1305	 * so unblock here ONLY if TS_TBLOCK has not been set.
1306	 */
1307	if (zst->zst_rx_blocked && ((tp->t_state & TS_TBLOCK) == 0)) {
1308		t = splzs();
1309		zst->zst_rx_blocked = 0;
1310		zs_hwiflow(zst);
1311		splx(t);
1312	}
1313
1314	/*
1315	 * Do any deferred work for status interrupts.
1316	 * The rr0 was saved in the h/w interrupt to
1317	 * avoid another splzs in here.
1318	 */
1319	if (zst->zst_st_check) {
1320		zst->zst_st_check = 0;
1321
1322		t = splzs();
1323		rr0 = cs->cs_rr0;
1324		delta = cs->cs_rr0_delta;
1325		cs->cs_rr0_delta = 0;
1326		splx(t);
1327
1328		if ((delta & cs->cs_rr0_dcd) != 0) {
1329			/*
1330			 * Inform the tty layer that carrier detect changed.
1331			 */
1332			(void) (*line->l_modem)(tp, (rr0 & cs->cs_rr0_dcd) != 0);
1333		}
1334
1335		if ((delta & cs->cs_rr0_cts) != 0) {
1336			/* Block or unblock output according to flow control. */
1337			if ((rr0 & cs->cs_rr0_cts) != 0) {
1338				zst->zst_tx_stopped = 0;
1339				(*line->l_start)(tp);
1340			} else {
1341				zst->zst_tx_stopped = 1;
1342			}
1343		}
1344	}
1345
1346	if (zst->zst_tx_done) {
1347		zst->zst_tx_done = 0;
1348
1349		tp->t_state &= ~TS_BUSY;
1350		if (tp->t_state & TS_FLUSH)
1351			tp->t_state &= ~TS_FLUSH;
1352		else
1353			ndflush(&tp->t_outq,
1354			    (int)(zst->zst_tba - tp->t_outq.c_cf));
1355		(*line->l_start)(tp);
1356	}
1357
1358	splx(s);
1359}
1360
1361struct zsops zsops_tty = {
1362	zstty_rxint,	/* receive char available */
1363	zstty_stint,	/* external/status */
1364	zstty_txint,	/* xmit buffer empty */
1365	zstty_softint,	/* process software interrupt */
1366};
1367
1368