xref: /netbsd-src/sys/arch/sun3/dev/zs.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: zs.c,v 1.23 1995/04/11 02:41:42 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Gordon W. Ross
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This software was developed by the Computer Systems Engineering group
9  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10  * contributed to Berkeley.
11  *
12  * All advertising materials mentioning features or use of this software
13  * must display the following acknowledgement:
14  *	This product includes software developed by the University of
15  *	California, Lawrence Berkeley Laboratory.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. All advertising materials mentioning features or use of this software
26  *    must display the following acknowledgement:
27  *	This product includes software developed by the University of
28  *	California, Berkeley and its contributors.
29  * 4. Neither the name of the University nor the names of its contributors
30  *    may be used to endorse or promote products derived from this software
31  *    without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  *
45  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
46  */
47 
48 /*
49  * Zilog Z8530 (ZSCC) driver.
50  *
51  * Runs two tty ports (ttya and ttyb) on zs0,
52  * and runs a keyboard and mouse on zs1.
53  *
54  * This driver knows far too much about chip to usage mappings.
55  */
56 #define	NZS	2		/* XXX */
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/proc.h>
61 #include <sys/device.h>
62 #include <sys/conf.h>
63 #include <sys/file.h>
64 #include <sys/ioctl.h>
65 #include <sys/tty.h>
66 #include <sys/time.h>
67 #include <sys/kernel.h>
68 #include <sys/syslog.h>
69 
70 #include <machine/autoconf.h>
71 #include <machine/cpu.h>
72 #include <machine/isr.h>
73 #include <machine/obio.h>
74 #include <machine/mon.h>
75 #include <machine/eeprom.h>
76 #include <machine/kbd.h>
77 
78 #include <dev/cons.h>
79 
80 #include <dev/ic/z8530.h>
81 #include <sun3/dev/zsvar.h>
82 
83 /*
84  * The default parity REALLY needs to be the same as the PROM uses,
85  * or you can not see messages done with printf during boot-up...
86  */
87 #undef	TTYDEF_CFLAG
88 #define	TTYDEF_CFLAG	(CREAD | CS8 | HUPCL)
89 
90 #ifdef KGDB
91 #include <machine/remote-sl.h>
92 #endif
93 
94 #define	ZSMAJOR	12		/* XXX */
95 
96 #define	ZS_KBD		2	/* XXX */
97 #define	ZS_MOUSE	3	/* XXX */
98 
99 /* The Sun3 provides a 4.9152 MHz clock to the ZS chips. */
100 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
101 
102 /*
103  * Define interrupt levels.
104  */
105 #define ZSHARD_PRI	6	/* Wired on the CPU board... */
106 #define ZSSOFT_PRI	3	/* Want tty pri (4) but this is OK. */
107 
108 /*
109  * Software state per found chip.  This would be called `zs_softc',
110  * but the previous driver had a rather different zs_softc....
111  */
112 struct zsinfo {
113 	struct	device zi_dev;		/* base device */
114 	volatile struct zsdevice *zi_zs;/* chip registers */
115 	struct	zs_chanstate zi_cs[2];	/* channel A and B software state */
116 };
117 
118 struct tty *zs_tty[NZS * 2];		/* XXX should be dynamic */
119 
120 /* Definition of the driver for autoconfig. */
121 static int	zs_match(struct device *, void *, void *);
122 static void	zs_attach(struct device *, struct device *, void *);
123 
124 struct cfdriver zscd = {
125 	NULL, "zs", zs_match, zs_attach,
126 	DV_TTY, sizeof(struct zsinfo) };
127 
128 /* Interrupt handlers. */
129 static int	zshard(int);
130 static int	zssoft(int);
131 
132 struct zs_chanstate *zslist;
133 
134 /* Routines called from other code. */
135 int zsopen(dev_t, int, int, struct proc *);
136 int zsclose(dev_t, int, int, struct proc *);
137 static void	zsiopen(struct tty *);
138 static void	zsiclose(struct tty *);
139 static void	zsstart(struct tty *);
140 void		zsstop(struct tty *, int);
141 static int	zsparam(struct tty *, struct termios *);
142 
143 /* Routines purely local to this driver. */
144 static int	zs_getspeed(volatile struct zschan *);
145 static void	zs_reset(volatile struct zschan *, int, int);
146 static void	zs_modem(struct zs_chanstate *, int);
147 static void	zs_loadchannelregs(volatile struct zschan *, u_char *);
148 static u_char zs_read(volatile struct zschan *, u_char);
149 static u_char zs_write(volatile struct zschan *, u_char, u_char);
150 
151 /* Console stuff. */
152 static volatile struct zschan *zs_conschan;
153 
154 #ifdef KGDB
155 /* KGDB stuff.  Must reboot to change zs_kgdbunit. */
156 extern int kgdb_dev, kgdb_rate;
157 static int zs_kgdb_savedspeed;
158 static void zs_checkkgdb(int, struct zs_chanstate *, struct tty *);
159 #endif
160 
161 /*
162  * Console keyboard L1-A processing is done in the hardware interrupt code,
163  * so we need to duplicate some of the console keyboard decode state.  (We
164  * must not use the regular state as the hardware code keeps ahead of the
165  * software state: the software state tracks the most recent ring input but
166  * the hardware state tracks the most recent ZSCC input.)  See also kbd.h.
167  */
168 static struct conk_state {	/* console keyboard state */
169 	char	conk_id;	/* true => ID coming up (console only) */
170 	char	conk_l1;	/* true => L1 pressed (console only) */
171 } zsconk_state;
172 
173 int zshardscope;
174 int zsshortcuts;		/* number of "shortcut" software interrupts */
175 
176 int zssoftpending;		/* We have done isr_soft_request() */
177 
178 static struct zsdevice *zsaddr[NZS];	/* XXX, but saves work */
179 
180 /* Default OBIO addresses. */
181 static int zs_physaddr[NZS] = { OBIO_ZS, OBIO_KEYBD_MS };
182 
183 static u_char zs_init_reg[16] = {
184 	0,	/* 0: CMD (reset, etc.) */
185 	ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE,
186 	0x18 + ZSHARD_PRI,	/* IVECT */
187 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
188 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
189 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
190 	0,	/* 6: TXSYNC/SYNCLO */
191 	0,	/* 7: RXSYNC/SYNCHI */
192 	0,	/* 8: alias for data port */
193 	0,	/* 9: ZSWR9_MASTER_IE (later) */
194 	0,	/*10: Misc. TX/RX control bits */
195 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
196 	0,	/*12: BAUDLO (later) */
197 	0,	/*13: BAUDHI (later) */
198 	ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
199 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
200 };
201 
202 /* Find PROM mappings (for console support). */
203 void zs_init()
204 {
205 	int i;
206 
207 	for (i = 0; i < NZS; i++) {
208 		zsaddr[i] = (struct zsdevice *)
209 			obio_find_mapping(zs_physaddr[i], OBIO_ZS_SIZE);
210 	}
211 }
212 
213 /*
214  * Match slave number to zs unit number, so that misconfiguration will
215  * not set up the keyboard as ttya, etc.
216  */
217 static int
218 zs_match(struct device *parent, void *vcf, void *args)
219 {
220 	struct cfdata *cf = vcf;
221 	struct confargs *ca = args;
222 	int unit, x;
223 	void *zsva;
224 
225 	unit = cf->cf_unit;
226 	if (unit < 0 || unit >= NZS)
227 		return (0);
228 
229 	zsva = zsaddr[unit];
230 	if (zsva == NULL)
231 		return (0);
232 
233 	if (ca->ca_paddr == -1)
234 		ca->ca_paddr = zs_physaddr[unit];
235 	if (ca->ca_intpri == -1)
236 		ca->ca_intpri = ZSHARD_PRI;
237 
238 	/* This returns -1 on a fault (bus error). */
239 	x = peek_byte(zsva);
240 	return (x != -1);
241 }
242 
243 /*
244  * Attach a found zs.
245  *
246  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
247  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
248  */
249 static void
250 zs_attach(struct device *parent, struct device *self, void *args)
251 {
252 	struct cfdata *cf;
253 	struct confargs *ca;
254 	register int zs, unit;
255 	register struct zsinfo *zi;
256 	register struct zs_chanstate *cs;
257 	register volatile struct zsdevice *addr;
258 	register struct tty *tp, *ctp;
259 	int softcar;
260 	static int didintr;
261 
262 	cf = self->dv_cfdata;
263 	zs = self->dv_unit;
264 	ca = args;
265 
266 	printf(" softpri %d\n", ZSSOFT_PRI);
267 
268 	if (zsaddr[zs] == NULL)
269 		panic("zs_attach: zs%d not mapped\n", zs);
270 	addr = zsaddr[zs];
271 
272 	if (!didintr) {
273 		didintr = 1;
274 		isr_add_autovect(zssoft, NULL, ZSSOFT_PRI);
275 		isr_add_autovect(zshard, NULL, ZSHARD_PRI);
276 	}
277 
278 	zi = (struct zsinfo *)self;
279 	zi->zi_zs = addr;
280 	unit = zs * 2;
281 	cs = zi->zi_cs;
282 	softcar = cf->cf_flags;
283 
284 	if(!zs_tty[unit])
285 		zs_tty[unit] = ttymalloc();
286 	if(!zs_tty[unit+1])
287 		zs_tty[unit+1] = ttymalloc();
288 
289 	/* link into interrupt list with order (A,B) (B=A+1) */
290 	cs[0].cs_next = &cs[1];
291 	cs[1].cs_next = zslist;
292 	zslist = cs;
293 
294 	tp = zs_tty[unit];
295 	cs->cs_unit = unit;
296 	cs->cs_zc = &addr->zs_chan[ZS_CHAN_A];
297 	cs->cs_speed = zs_getspeed(cs->cs_zc);
298 #ifdef	DEBUG
299 	mon_printf("zs%da speed %d ",  zs, cs->cs_speed);
300 #endif
301 	cs->cs_softcar = softcar & 1;
302 	cs->cs_ttyp = tp;
303 	tp->t_dev = makedev(ZSMAJOR, unit);
304 	tp->t_oproc = zsstart;
305 	tp->t_param = zsparam;
306 	if (cs->cs_zc == zs_conschan) {
307 		/* This unit is the console. */
308 		cs->cs_consio = 1;
309 		cs->cs_brkabort = 1;
310 		cs->cs_softcar = 1;
311 		/* Call zsparam so interrupts get enabled. */
312 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
313 		tp->t_cflag = TTYDEF_CFLAG;
314 		(void) zsparam(tp, &tp->t_termios);
315 	} else {
316 		/* Can not run kgdb on the console? */
317 #ifdef KGDB
318 		zs_checkkgdb(unit, cs, tp);
319 #endif
320 	}
321 #if 0
322 	/* XXX - Drop carrier here? -gwr */
323 	zs_modem(cs, cs->cs_softcar ? 1 : 0);
324 #endif
325 
326 	if (unit == ZS_KBD) {
327 		/*
328 		 * Keyboard: tell /dev/kbd driver how to talk to us.
329 		 */
330 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
331 		tp->t_cflag = CS8;
332 		/* zsparam called by zsiopen */
333 		kbd_serial(tp, zsiopen, zsiclose);
334 		cs->cs_conk = 1;		/* do L1-A processing */
335 	}
336 	unit++;
337 	cs++;
338 	tp = zs_tty[unit];
339 
340 	cs->cs_unit = unit;
341 	cs->cs_zc = &addr->zs_chan[ZS_CHAN_B];
342 	cs->cs_speed = zs_getspeed(cs->cs_zc);
343 #ifdef	DEBUG
344 	mon_printf("zs%db speed %d\n", zs, cs->cs_speed);
345 #endif
346 	cs->cs_softcar = softcar & 2;
347 	cs->cs_ttyp = tp;
348 	tp->t_dev = makedev(ZSMAJOR, unit);
349 	tp->t_oproc = zsstart;
350 	tp->t_param = zsparam;
351 	if (cs->cs_zc == zs_conschan) {
352 		/* This unit is the console. */
353 		cs->cs_consio = 1;
354 		cs->cs_brkabort = 1;
355 		cs->cs_softcar = 1;
356 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
357 		tp->t_cflag = TTYDEF_CFLAG;
358 		(void) zsparam(tp, &tp->t_termios);
359 	} else {
360 		/* Can not run kgdb on the console? */
361 #ifdef KGDB
362 		zs_checkkgdb(unit, cs, tp);
363 #endif
364 	}
365 #if 0
366 	/* XXX - Drop carrier here? -gwr */
367 	zs_modem(cs, cs->cs_softcar ? 1 : 0);
368 #endif
369 
370 	if (unit == ZS_MOUSE) {
371 		/*
372 		 * Mouse: tell /dev/mouse driver how to talk to us.
373 		 */
374 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
375 		tp->t_cflag = CS8;
376 		/* zsparam called by zsiopen */
377 		ms_serial(tp, zsiopen, zsiclose);
378 	}
379 }
380 
381 /*
382  * Put a channel in a known state.  Interrupts may be left disabled
383  * or enabled, as desired.  (Used only by kgdb)
384  */
385 static void
386 zs_reset(zc, inten, speed)
387 	volatile struct zschan *zc;
388 	int inten, speed;
389 {
390 	int tconst;
391 	u_char reg[16];
392 
393 	bcopy(zs_init_reg, reg, 16);
394 	if (inten)
395 		reg[9] |= ZSWR9_MASTER_IE;
396 
397 	tconst = BPS_TO_TCONST(PCLK / 16, speed);
398 	reg[12] = tconst;
399 	reg[13] = tconst >> 8;
400 	zs_loadchannelregs(zc, reg);
401 }
402 
403 /*
404  * Console support
405  */
406 
407 /*
408  * Used by the kd driver to find out if it can work.
409  */
410 int
411 zscnprobe_kbd()
412 {
413 	if (zsaddr[1] == NULL) {
414 		mon_printf("zscnprobe_kbd: zs1 not yet mapped\n");
415 		return CN_DEAD;
416 	}
417 	return CN_INTERNAL;
418 }
419 
420 /*
421  * This is the console probe routine for ttya and ttyb.
422  */
423 static int
424 zscnprobe(struct consdev *cn, int unit)
425 {
426 	int maj;
427 
428 	if (zsaddr[0] == NULL) {
429 		mon_printf("zscnprobe: zs0 not mapped\n");
430 		cn->cn_pri = CN_DEAD;
431 		return 0;
432 	}
433 	/* XXX - Also try to make sure it exists? */
434 
435 	/* locate the major number */
436 	for (maj = 0; maj < nchrdev; maj++)
437 		if (cdevsw[maj].d_open == (void*)zsopen)
438 			break;
439 
440 	cn->cn_dev = makedev(maj, unit);
441 
442 	/* Use EEPROM console setting to decide "remote" console. */
443 	/* Note: EE_CONS_TTYA + 1 == EE_CONS_TTYB */
444 	if (ee_console == (EE_CONS_TTYA + unit)) {
445 		cn->cn_pri = CN_REMOTE;
446 	} else {
447 		cn->cn_pri = CN_NORMAL;
448 	}
449 	return (0);
450 }
451 
452 /* This is the constab entry for TTYA. */
453 int
454 zscnprobe_a(struct consdev *cn)
455 {
456 	return (zscnprobe(cn, 0));
457 }
458 
459 /* This is the constab entry for TTYB. */
460 int
461 zscnprobe_b(struct consdev *cn)
462 {
463 	return (zscnprobe(cn, 1));
464 }
465 
466 /* Called by kdcninit() or below. */
467 void
468 zs_set_conschan(unit, ab)
469 	int unit, ab;
470 {
471 	volatile struct zsdevice *addr;
472 
473 	addr = zsaddr[unit];
474 	zs_conschan = ((ab == 0) ?
475 				   &addr->zs_chan[ZS_CHAN_A] :
476 				   &addr->zs_chan[ZS_CHAN_B] );
477 }
478 
479 /* Attach as console.  Also set zs_conschan */
480 int
481 zscninit(struct consdev *cn)
482 {
483 	int ab = minor(cn->cn_dev) & 1;
484 	zs_set_conschan(0, ab);
485 	mon_printf("console on zs0 (tty%c)\n", 'a' + ab);
486 }
487 
488 
489 /*
490  * Polled console input putchar.
491  */
492 int
493 zscngetc(dev)
494 	dev_t dev;
495 {
496 	register volatile struct zschan *zc = zs_conschan;
497 	register int s, c;
498 
499 	if (zc == NULL)
500 		return (0);
501 
502 	s = splhigh();
503 
504 	/* Wait for a character to arrive. */
505 	while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
506 		ZS_DELAY();
507 	ZS_DELAY();
508 
509 	c = zc->zc_data;
510 	ZS_DELAY();
511 
512 	splx(s);
513 
514 	/*
515 	 * This is used by the kd driver to read scan codes,
516 	 * so don't translate '\r' ==> '\n' here...
517 	 */
518 	return (c);
519 }
520 
521 /*
522  * Polled console output putchar.
523  */
524 int
525 zscnputc(dev, c)
526 	dev_t dev;
527 	int c;
528 {
529 	register volatile struct zschan *zc = zs_conschan;
530 	register int s;
531 
532 	if (zc == NULL) {
533 		s = splhigh();
534 		mon_putchar(c);
535 		splx(s);
536 		return (0);
537 	}
538 	s = splhigh();
539 
540 	/* Wait for transmitter to become ready. */
541 	while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
542 		ZS_DELAY();
543 	ZS_DELAY();
544 
545 	zc->zc_data = c;
546 	ZS_DELAY();
547 	splx(s);
548 }
549 
550 #ifdef KGDB
551 /*
552  * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init).
553  * Pick up the current speed and character size and restore the original
554  * speed.
555  */
556 static void
557 zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp)
558 {
559 
560 	if (kgdb_dev == makedev(ZSMAJOR, unit)) {
561 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
562 		tp->t_cflag = CS8;
563 		cs->cs_kgdb = 1;
564 		cs->cs_speed = zs_kgdb_savedspeed;
565 		(void) zsparam(tp, &tp->t_termios);
566 	}
567 }
568 #endif
569 
570 /*
571  * Compute the current baud rate given a ZSCC channel.
572  */
573 static int
574 zs_getspeed(zc)
575 	register volatile struct zschan *zc;
576 {
577 	register int tconst;
578 
579 	tconst = ZS_READ(zc, 12);
580 	tconst |= ZS_READ(zc, 13) << 8;
581 	return (TCONST_TO_BPS(PCLK / 16, tconst));
582 }
583 
584 
585 /*
586  * Do an internal open.
587  */
588 static void
589 zsiopen(struct tty *tp)
590 {
591 
592 	(void) zsparam(tp, &tp->t_termios);
593 	ttsetwater(tp);
594 	tp->t_state = TS_ISOPEN | TS_CARR_ON;
595 }
596 
597 /*
598  * Do an internal close.  Eventually we should shut off the chip when both
599  * ports on it are closed.
600  */
601 static void
602 zsiclose(struct tty *tp)
603 {
604 
605 	ttylclose(tp, 0);	/* ??? */
606 	ttyclose(tp);		/* ??? */
607 	tp->t_state = 0;
608 }
609 
610 
611 /*
612  * Open a zs serial port.  This interface may not be used to open
613  * the keyboard and mouse ports. (XXX)
614  */
615 int
616 zsopen(dev_t dev, int flags, int mode, struct proc *p)
617 {
618 	register struct tty *tp;
619 	register struct zs_chanstate *cs;
620 	struct zsinfo *zi;
621 	int unit = minor(dev), zs = unit >> 1, error, s;
622 
623 #ifdef	DEBUG
624 	mon_printf("zs_open\n");
625 #endif
626 	if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL ||
627 	    unit == ZS_KBD || unit == ZS_MOUSE)
628 		return (ENXIO);
629 	cs = &zi->zi_cs[unit & 1];
630 	tp = cs->cs_ttyp;
631 	s = spltty();
632 	if ((tp->t_state & TS_ISOPEN) == 0) {
633 		ttychars(tp);
634 		tp->t_iflag = TTYDEF_IFLAG;
635 		tp->t_oflag = TTYDEF_OFLAG;
636 		tp->t_cflag = TTYDEF_CFLAG;
637 		tp->t_lflag = TTYDEF_LFLAG;
638 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
639 		(void) zsparam(tp, &tp->t_termios);
640 		ttsetwater(tp);
641 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
642 		splx(s);
643 		return (EBUSY);
644 	}
645 	error = 0;
646 #ifdef	DEBUG
647 	mon_printf("wait for carrier...\n");
648 #endif
649 	for (;;) {
650 		/* loop, turning on the device, until carrier present */
651 		zs_modem(cs, 1);
652 		/* May never get status intr if carrier already on. -gwr */
653 		if (cs->cs_zc->zc_csr & ZSRR0_DCD)
654 			tp->t_state |= TS_CARR_ON;
655 		if (cs->cs_softcar)
656 			tp->t_state |= TS_CARR_ON;
657 		if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
658 		    tp->t_state & TS_CARR_ON)
659 			break;
660 		tp->t_state |= TS_WOPEN;
661 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
662 		    ttopen, 0))
663 			break;
664 	}
665 #ifdef	DEBUG
666 	mon_printf("...carrier %s\n",
667 			   (tp->t_state & TS_CARR_ON) ? "on" : "off");
668 #endif
669 	splx(s);
670 	if (error == 0)
671 		error = linesw[tp->t_line].l_open(dev, tp);
672 	if (error)
673 		zs_modem(cs, 0);
674 	return (error);
675 }
676 
677 /*
678  * Close a zs serial port.
679  */
680 int
681 zsclose(dev_t dev, int flags, int mode, struct proc *p)
682 {
683 	register struct zs_chanstate *cs;
684 	register struct tty *tp;
685 	struct zsinfo *zi;
686 	int unit = minor(dev), s;
687 
688 #ifdef	DEBUG
689 	mon_printf("zs_close\n");
690 #endif
691 	zi = zscd.cd_devs[unit >> 1];
692 	cs = &zi->zi_cs[unit & 1];
693 	tp = cs->cs_ttyp;
694 	linesw[tp->t_line].l_close(tp, flags);
695 	if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
696 	    (tp->t_state & TS_ISOPEN) == 0) {
697 		zs_modem(cs, 0);
698 		/* hold low for 1 second */
699 		(void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
700 	}
701 	if (cs->cs_creg[5] & ZSWR5_BREAK)
702 	{
703 		s = splzs();
704 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
705 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
706 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
707 		splx(s);
708 	}
709 	ttyclose(tp);
710 #ifdef KGDB
711 	/* Reset the speed if we're doing kgdb on this port */
712 	if (cs->cs_kgdb) {
713 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
714 		(void) zsparam(tp, &tp->t_termios);
715 	}
716 #endif
717 	return (0);
718 }
719 
720 /*
721  * Read/write zs serial port.
722  */
723 int
724 zsread(dev_t dev, struct uio *uio, int flags)
725 {
726 	register struct tty *tp = zs_tty[minor(dev)];
727 
728 	return (linesw[tp->t_line].l_read(tp, uio, flags));
729 }
730 
731 int
732 zswrite(dev_t dev, struct uio *uio, int flags)
733 {
734 	register struct tty *tp = zs_tty[minor(dev)];
735 
736 	return (linesw[tp->t_line].l_write(tp, uio, flags));
737 }
738 
739 /*
740  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
741  * channels are kept in (A,B) pairs.
742  *
743  * Do just a little, then get out; set a software interrupt if more
744  * work is needed.
745  *
746  * We deliberately ignore the vectoring Zilog gives us, and match up
747  * only the number of `reset interrupt under service' operations, not
748  * the order.
749  */
750 /* ARGSUSED */
751 int
752 zshard(int intrarg)
753 {
754 	register struct zs_chanstate *a;
755 #define	b (a + 1)
756 	register volatile struct zschan *zc;
757 	register int rr3, intflags = 0, v, i;
758 	static int zsrint(struct zs_chanstate *, volatile struct zschan *);
759 	static int zsxint(struct zs_chanstate *, volatile struct zschan *);
760 	static int zssint(struct zs_chanstate *, volatile struct zschan *);
761 
762 	for (a = zslist; a != NULL; a = b->cs_next) {
763 		rr3 = ZS_READ(a->cs_zc, 3);
764 
765 		/* XXX - This should loop to empty the on-chip fifo. */
766 		if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
767 			intflags |= 2;
768 			zc = a->cs_zc;
769 			i = a->cs_rbput;
770 			if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
771 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
772 				intflags |= 1;
773 			}
774 			if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
775 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
776 				intflags |= 1;
777 			}
778 			if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
779 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
780 				intflags |= 1;
781 			}
782 			a->cs_rbput = i;
783 		}
784 
785 		/* XXX - This should loop to empty the on-chip fifo. */
786 		if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
787 			intflags |= 2;
788 			zc = b->cs_zc;
789 			i = b->cs_rbput;
790 			if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
791 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
792 				intflags |= 1;
793 			}
794 			if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
795 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
796 				intflags |= 1;
797 			}
798 			if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
799 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
800 				intflags |= 1;
801 			}
802 			b->cs_rbput = i;
803 		}
804 	}
805 #undef b
806 	if (intflags & 1) {
807 		if (zssoftpending == 0) {
808 			/* We are at splzs here, so no need to lock. */
809 			zssoftpending = ZSSOFT_PRI;
810 			isr_soft_request(ZSSOFT_PRI);
811 		}
812 	}
813 	return (intflags & 2);
814 }
815 
816 static int
817 zsrint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
818 {
819 	register int c = zc->zc_data;
820 
821 	if (cs->cs_conk) {
822 		register struct conk_state *conk = &zsconk_state;
823 
824 		/*
825 		 * Check here for console abort function, so that we
826 		 * can abort even when interrupts are locking up the
827 		 * machine.
828 		 */
829 		if (c == KBD_RESET) {
830 			conk->conk_id = 1;	/* ignore next byte */
831 			conk->conk_l1 = 0;
832 		} else if (conk->conk_id)
833 			conk->conk_id = 0;	/* stop ignoring bytes */
834 		else if (c == KBD_L1)
835 			conk->conk_l1 = 1;	/* L1 went down */
836 		else if (c == (KBD_L1|KBD_UP))
837 			conk->conk_l1 = 0;	/* L1 went up */
838 		else if (c == KBD_A && conk->conk_l1) {
839 			zsabort();
840 			/* Debugger done.  Send L1-up in case X is running. */
841 			conk->conk_l1 = 0;
842 			c = (KBD_L1|KBD_UP);
843 		}
844 	}
845 #ifdef KGDB
846 	if (c == FRAME_START && cs->cs_kgdb &&
847 	    (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
848 		zskgdb(cs->cs_unit);
849 		goto clearit;
850 	}
851 #endif
852 	/* compose receive character and status */
853 	c <<= 8;
854 	c |= ZS_READ(zc, 1);
855 
856 	/* clear receive error & interrupt condition */
857 	zc->zc_csr = ZSWR0_RESET_ERRORS;
858 	zc->zc_csr = ZSWR0_CLR_INTR;
859 
860 	return (ZRING_MAKE(ZRING_RINT, c));
861 
862 clearit:
863 	zc->zc_csr = ZSWR0_RESET_ERRORS;
864 	zc->zc_csr = ZSWR0_CLR_INTR;
865 	return (0);
866 }
867 
868 static int
869 zsxint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
870 {
871 	register int i = cs->cs_tbc;
872 
873 	if (i == 0) {
874 		zc->zc_csr = ZSWR0_RESET_TXINT;
875 		zc->zc_csr = ZSWR0_CLR_INTR;
876 		return (ZRING_MAKE(ZRING_XINT, 0));
877 	}
878 	cs->cs_tbc = i - 1;
879 	zc->zc_data = *cs->cs_tba++;
880 	zc->zc_csr = ZSWR0_CLR_INTR;
881 	return (0);
882 }
883 
884 static int
885 zssint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
886 {
887 	register int rr0;
888 
889 	rr0 = zc->zc_csr;
890 	zc->zc_csr = ZSWR0_RESET_STATUS;
891 	zc->zc_csr = ZSWR0_CLR_INTR;
892 	/*
893 	 * The chip's hardware flow control is, as noted in zsreg.h,
894 	 * busted---if the DCD line goes low the chip shuts off the
895 	 * receiver (!).  If we want hardware CTS flow control but do
896 	 * not have it, and carrier is now on, turn HFC on; if we have
897 	 * HFC now but carrier has gone low, turn it off.
898 	 */
899 	if (rr0 & ZSRR0_DCD) {
900 		if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
901 		    (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
902 			cs->cs_creg[3] |= ZSWR3_HFC;
903 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
904 		}
905 	} else {
906 		if (cs->cs_creg[3] & ZSWR3_HFC) {
907 			cs->cs_creg[3] &= ~ZSWR3_HFC;
908 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
909 		}
910 	}
911 	if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
912 		/* Wait for end of break to avoid PROM abort. */
913 		while (zc->zc_csr & ZSRR0_BREAK)
914 			ZS_DELAY();
915 		zsabort();
916 		return (0);
917 	}
918 	return (ZRING_MAKE(ZRING_SINT, rr0));
919 }
920 
921 zsabort()
922 {
923 #ifdef DDB
924 	Debugger();
925 #else
926 	printf("stopping on keyboard abort\n");
927 	sun3_rom_abort();
928 #endif
929 }
930 
931 #ifdef KGDB
932 /*
933  * KGDB framing character received: enter kernel debugger.  This probably
934  * should time out after a few seconds to avoid hanging on spurious input.
935  */
936 zskgdb(int unit)
937 {
938 
939 	printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
940 	kgdb_connect(1);
941 }
942 #endif
943 
944 /*
945  * Print out a ring or fifo overrun error message.
946  */
947 static void
948 zsoverrun(int unit, long *ptime, char *what)
949 {
950 
951 	if (*ptime != time.tv_sec) {
952 		*ptime = time.tv_sec;
953 		log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
954 		    (unit & 1) + 'a', what);
955 	}
956 }
957 
958 /*
959  * ZS software interrupt.  Scan all channels for deferred interrupts.
960  */
961 int
962 zssoft(int arg)
963 {
964 	register struct zs_chanstate *cs;
965 	register volatile struct zschan *zc;
966 	register struct linesw *line;
967 	register struct tty *tp;
968 	register int get, n, c, cc, unit, s;
969 
970 	/* This is not the only ISR on this IPL. */
971 	if (zssoftpending == 0)
972 		return (0);
973 
974 	/*
975 	 * The soft intr. bit will be set by zshard only if
976 	 * the variable zssoftpending is zero.  The order of
977 	 * these next two statements prevents our clearing
978 	 * the soft intr bit just after zshard has set it.
979 	 */
980 	isr_soft_clear(ZSSOFT_PRI);
981 	zssoftpending = 0;	/* Now zshard may set it again. */
982 
983 	for (cs = zslist; cs != NULL; cs = cs->cs_next) {
984 		get = cs->cs_rbget;
985 again:
986 		n = cs->cs_rbput;	/* atomic */
987 		if (get == n)		/* nothing more on this line */
988 			continue;
989 		unit = cs->cs_unit;	/* set up to handle interrupts */
990 		zc = cs->cs_zc;
991 		tp = cs->cs_ttyp;
992 		line = &linesw[tp->t_line];
993 		/*
994 		 * Compute the number of interrupts in the receive ring.
995 		 * If the count is overlarge, we lost some events, and
996 		 * must advance to the first valid one.  It may get
997 		 * overwritten if more data are arriving, but this is
998 		 * too expensive to check and gains nothing (we already
999 		 * lost out; all we can do at this point is trade one
1000 		 * kind of loss for another).
1001 		 */
1002 		n -= get;
1003 		if (n > ZLRB_RING_SIZE) {
1004 			zsoverrun(unit, &cs->cs_rotime, "ring");
1005 			get += n - ZLRB_RING_SIZE;
1006 			n = ZLRB_RING_SIZE;
1007 		}
1008 		while (--n >= 0) {
1009 			/* race to keep ahead of incoming interrupts */
1010 			c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
1011 			switch (ZRING_TYPE(c)) {
1012 
1013 			case ZRING_RINT:
1014 				c = ZRING_VALUE(c);
1015 				if (c & ZSRR1_DO)
1016 					zsoverrun(unit, &cs->cs_fotime, "fifo");
1017 				cc = c >> 8;
1018 				if (c & ZSRR1_FE)
1019 					cc |= TTY_FE;
1020 				if (c & ZSRR1_PE)
1021 					cc |= TTY_PE;
1022 				/*
1023 				 * this should be done through
1024 				 * bstreams	XXX gag choke
1025 				 */
1026 				if (unit == ZS_KBD)
1027 					kbd_rint(cc);
1028 				else if (unit == ZS_MOUSE)
1029 					ms_rint(cc);
1030 				else
1031 					line->l_rint(cc, tp);
1032 				break;
1033 
1034 			case ZRING_XINT:
1035 				/*
1036 				 * Transmit done: change registers and resume,
1037 				 * or clear BUSY.
1038 				 */
1039 				if (cs->cs_heldchange) {
1040 					s = splzs();
1041 					c = zc->zc_csr;
1042 					if ((c & ZSRR0_DCD) == 0)
1043 						cs->cs_preg[3] &= ~ZSWR3_HFC;
1044 					bcopy((caddr_t)cs->cs_preg,
1045 					    (caddr_t)cs->cs_creg, 16);
1046 					zs_loadchannelregs(zc, cs->cs_creg);
1047 					splx(s);
1048 					cs->cs_heldchange = 0;
1049 					if (cs->cs_heldtbc &&
1050 					    (tp->t_state & TS_TTSTOP) == 0) {
1051 						cs->cs_tbc = cs->cs_heldtbc - 1;
1052 						zc->zc_data = *cs->cs_tba++;
1053 						goto again;
1054 					}
1055 				}
1056 				tp->t_state &= ~TS_BUSY;
1057 				if (tp->t_state & TS_FLUSH)
1058 					tp->t_state &= ~TS_FLUSH;
1059 				else
1060 					ndflush(&tp->t_outq, cs->cs_tba -
1061 						(caddr_t) tp->t_outq.c_cf);
1062 				line->l_start(tp);
1063 				break;
1064 
1065 			case ZRING_SINT:
1066 				/*
1067 				 * Status line change.  HFC bit is run in
1068 				 * hardware interrupt, to avoid locking
1069 				 * at splzs here.
1070 				 */
1071 				c = ZRING_VALUE(c);
1072 				if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
1073 					cc = (c & ZSRR0_DCD) != 0;
1074 					if (line->l_modem(tp, cc) == 0)
1075 						zs_modem(cs, cc);
1076 				}
1077 				cs->cs_rr0 = c;
1078 				break;
1079 
1080 			default:
1081 				log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
1082 				    unit >> 1, (unit & 1) + 'a', c);
1083 				break;
1084 			}
1085 		}
1086 		cs->cs_rbget = get;
1087 		goto again;
1088 	}
1089 	return (1);
1090 }
1091 
1092 int
1093 zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
1094 {
1095 	int unit = minor(dev);
1096 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1097 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
1098 	register struct tty *tp = cs->cs_ttyp;
1099 	register int error, s;
1100 
1101 	error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
1102 	if (error >= 0)
1103 		return (error);
1104 	error = ttioctl(tp, cmd, data, flag, p);
1105 	if (error >= 0)
1106 		return (error);
1107 
1108 	switch (cmd) {
1109 
1110 	case TIOCSBRK:
1111 		s = splzs();
1112 		cs->cs_preg[5] |= ZSWR5_BREAK;
1113 		cs->cs_creg[5] |= ZSWR5_BREAK;
1114 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1115 		splx(s);
1116 		break;
1117 
1118 	case TIOCCBRK:
1119 		s = splzs();
1120 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
1121 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
1122 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1123 		splx(s);
1124 		break;
1125 
1126 	case TIOCGFLAGS: {
1127 		int bits = 0;
1128 
1129 		if (cs->cs_softcar)
1130 			bits |= TIOCFLAG_SOFTCAR;
1131 		if (cs->cs_creg[15] & ZSWR15_DCD_IE)
1132 			bits |= TIOCFLAG_CLOCAL;
1133 		if (cs->cs_creg[3] & ZSWR3_HFC)
1134 			bits |= TIOCFLAG_CRTSCTS;
1135 		*(int *)data = bits;
1136 		break;
1137 	}
1138 
1139 	case TIOCSFLAGS: {
1140 		int userbits, driverbits = 0;
1141 
1142 		error = suser(p->p_ucred, &p->p_acflag);
1143 		if (error != 0)
1144 			return (EPERM);
1145 
1146 		userbits = *(int *)data;
1147 
1148 		/*
1149 		 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
1150 		 * defaulting to software flow control.
1151 		 */
1152 		if (userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
1153 			return(EINVAL);
1154 		if (userbits & TIOCFLAG_MDMBUF)	/* don't support this (yet?) */
1155 			return(ENXIO);
1156 
1157 		s = splzs();
1158 		if ((userbits & TIOCFLAG_SOFTCAR) ||
1159 			(cs->cs_zc == zs_conschan))
1160 		{
1161 			cs->cs_softcar = 1;	/* turn on softcar */
1162 			cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
1163 			cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
1164 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1165 		} else if (userbits & TIOCFLAG_CLOCAL) {
1166 			cs->cs_softcar = 0; 	/* turn off softcar */
1167 			cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
1168 			cs->cs_creg[15] |= ZSWR15_DCD_IE;
1169 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1170 			tp->t_termios.c_cflag |= CLOCAL;
1171 		}
1172 		if (userbits & TIOCFLAG_CRTSCTS) {
1173 			cs->cs_preg[15] |= ZSWR15_CTS_IE;
1174 			cs->cs_creg[15] |= ZSWR15_CTS_IE;
1175 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1176 			cs->cs_preg[3] |= ZSWR3_HFC;
1177 			cs->cs_creg[3] |= ZSWR3_HFC;
1178 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
1179 			tp->t_termios.c_cflag |= CRTSCTS;
1180 		} else {
1181 			/* no mdmbuf, so we must want software flow control */
1182 			cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
1183 			cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
1184 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1185 			cs->cs_preg[3] &= ~ZSWR3_HFC;
1186 			cs->cs_creg[3] &= ~ZSWR3_HFC;
1187 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
1188 			tp->t_termios.c_cflag &= ~CRTSCTS;
1189 		}
1190 		splx(s);
1191 		break;
1192 	}
1193 
1194 	case TIOCSDTR:
1195 	case TIOCCDTR:
1196 	case TIOCMSET:
1197 	case TIOCMBIS:
1198 	case TIOCMBIC:
1199 	case TIOCMGET:
1200 	default:
1201 		return (ENOTTY);
1202 	}
1203 	return (0);
1204 }
1205 
1206 /*
1207  * Start or restart transmission.
1208  */
1209 static void
1210 zsstart(register struct tty *tp)
1211 {
1212 	register struct zs_chanstate *cs;
1213 	register int s, nch;
1214 	int unit = minor(tp->t_dev);
1215 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1216 
1217 	cs = &zi->zi_cs[unit & 1];
1218 	s = spltty();
1219 
1220 	/*
1221 	 * If currently active or delaying, no need to do anything.
1222 	 */
1223 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
1224 		goto out;
1225 
1226 	/*
1227 	 * If there are sleepers, and output has drained below low
1228 	 * water mark, awaken.
1229 	 */
1230 	if (tp->t_outq.c_cc <= tp->t_lowat) {
1231 		if (tp->t_state & TS_ASLEEP) {
1232 			tp->t_state &= ~TS_ASLEEP;
1233 			wakeup((caddr_t)&tp->t_outq);
1234 		}
1235 		selwakeup(&tp->t_wsel);
1236 	}
1237 
1238 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
1239 	if (nch) {
1240 		register char *p = tp->t_outq.c_cf;
1241 
1242 		/* mark busy, enable tx done interrupts, & send first byte */
1243 		tp->t_state |= TS_BUSY;
1244 		(void) splzs();
1245 		cs->cs_preg[1] |= ZSWR1_TIE;
1246 		cs->cs_creg[1] |= ZSWR1_TIE;
1247 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1248 		cs->cs_zc->zc_data = *p;
1249 		cs->cs_tba = p + 1;
1250 		cs->cs_tbc = nch - 1;
1251 	} else {
1252 		/*
1253 		 * Nothing to send, turn off transmit done interrupts.
1254 		 * This is useful if something is doing polled output.
1255 		 */
1256 		(void) splzs();
1257 		cs->cs_preg[1] &= ~ZSWR1_TIE;
1258 		cs->cs_creg[1] &= ~ZSWR1_TIE;
1259 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1260 	}
1261 out:
1262 	splx(s);
1263 }
1264 
1265 /*
1266  * Stop output, e.g., for ^S or output flush.
1267  */
1268 void
1269 zsstop(register struct tty *tp, int flag)
1270 {
1271 	register struct zs_chanstate *cs;
1272 	register int s, unit = minor(tp->t_dev);
1273 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1274 
1275 	cs = &zi->zi_cs[unit & 1];
1276 	s = splzs();
1277 	if (tp->t_state & TS_BUSY) {
1278 		/*
1279 		 * Device is transmitting; must stop it.
1280 		 */
1281 		cs->cs_tbc = 0;
1282 		if ((tp->t_state & TS_TTSTOP) == 0)
1283 			tp->t_state |= TS_FLUSH;
1284 	}
1285 	splx(s);
1286 }
1287 
1288 /*
1289  * Set ZS tty parameters from termios.
1290  */
1291 static int
1292 zsparam(register struct tty *tp, register struct termios *t)
1293 {
1294 	int unit = minor(tp->t_dev);
1295 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1296 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
1297 	register int tmp, tmp5, cflag, s;
1298 
1299 	/*
1300 	 * Because PCLK is only run at 4.9 MHz, the fastest we
1301 	 * can go is 51200 baud (this corresponds to TC=1).
1302 	 * This is somewhat unfortunate as there is no real
1303 	 * reason we should not be able to handle higher rates.
1304 	 */
1305 	tmp = t->c_ospeed;
1306 	if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
1307 		return (EINVAL);
1308 	if (tmp == 0) {
1309 		/* stty 0 => drop DTR and RTS */
1310 		zs_modem(cs, 0);
1311 		return (0);
1312 	}
1313 	tmp = BPS_TO_TCONST(PCLK / 16, tmp);
1314 	if (tmp < 2)
1315 		return (EINVAL);
1316 
1317 	cflag = t->c_cflag;
1318 	tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
1319 	tp->t_cflag = cflag;
1320 
1321 	/*
1322 	 * Block interrupts so that state will not
1323 	 * be altered until we are done setting it up.
1324 	 */
1325 	s = splzs();
1326 	bcopy(zs_init_reg, cs->cs_preg, 16);
1327 	cs->cs_preg[12] = tmp;
1328 	cs->cs_preg[13] = tmp >> 8;
1329 	cs->cs_preg[9] |= ZSWR9_MASTER_IE;
1330 	switch (cflag & CSIZE) {
1331 	case CS5:
1332 		tmp = ZSWR3_RX_5;
1333 		tmp5 = ZSWR5_TX_5;
1334 		break;
1335 	case CS6:
1336 		tmp = ZSWR3_RX_6;
1337 		tmp5 = ZSWR5_TX_6;
1338 		break;
1339 	case CS7:
1340 		tmp = ZSWR3_RX_7;
1341 		tmp5 = ZSWR5_TX_7;
1342 		break;
1343 	case CS8:
1344 	default:
1345 		tmp = ZSWR3_RX_8;
1346 		tmp5 = ZSWR5_TX_8;
1347 		break;
1348 	}
1349 
1350 	/*
1351 	 * Output hardware flow control on the chip is horrendous: if
1352 	 * carrier detect drops, the receiver is disabled.  Hence we
1353 	 * can only do this when the carrier is on.
1354 	 */
1355 	if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
1356 		tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE;
1357 	else
1358 		tmp |= ZSWR3_RX_ENABLE;
1359 	cs->cs_preg[3] = tmp;
1360 	cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
1361 
1362 	tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
1363 	if ((cflag & PARODD) == 0)
1364 		tmp |= ZSWR4_EVENP;
1365 	if (cflag & PARENB)
1366 		tmp |= ZSWR4_PARENB;
1367 	cs->cs_preg[4] = tmp;
1368 
1369 	/*
1370 	 * If nothing is being transmitted, set up new current values,
1371 	 * else mark them as pending.
1372 	 */
1373 	if (cs->cs_heldchange == 0) {
1374 		if (cs->cs_ttyp->t_state & TS_BUSY) {
1375 			cs->cs_heldtbc = cs->cs_tbc;
1376 			cs->cs_tbc = 0;
1377 			cs->cs_heldchange = 1;
1378 		} else {
1379 			bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
1380 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
1381 		}
1382 	}
1383 	splx(s);
1384 	return (0);
1385 }
1386 
1387 /*
1388  * Raise or lower modem control (DTR/RTS) signals.  If a character is
1389  * in transmission, the change is deferred.
1390  */
1391 static void
1392 zs_modem(struct zs_chanstate *cs, int onoff)
1393 {
1394 	int s, bis, and;
1395 
1396 	if (onoff) {
1397 		bis = ZSWR5_DTR | ZSWR5_RTS;
1398 		and = ~0;
1399 	} else {
1400 		bis = 0;
1401 		and = ~(ZSWR5_DTR | ZSWR5_RTS);
1402 	}
1403 	s = splzs();
1404 	cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
1405 	if (cs->cs_heldchange == 0) {
1406 		if (cs->cs_ttyp->t_state & TS_BUSY) {
1407 			cs->cs_heldtbc = cs->cs_tbc;
1408 			cs->cs_tbc = 0;
1409 			cs->cs_heldchange = 1;
1410 		} else {
1411 			cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
1412 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1413 		}
1414 	}
1415 	splx(s);
1416 }
1417 
1418 /*
1419  * Write the given register set to the given zs channel in the proper order.
1420  * The channel must not be transmitting at the time.  The receiver will
1421  * be disabled for the time it takes to write all the registers.
1422  */
1423 static void
1424 zs_loadchannelregs(volatile struct zschan *zc, u_char *reg)
1425 {
1426 	int i;
1427 
1428 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
1429 	ZS_DELAY();
1430 
1431 #if 1	/* XXX - Is this really a good idea? -gwr */
1432 	i = zc->zc_data;		/* drain fifo */
1433 	ZS_DELAY();
1434 	i = zc->zc_data;
1435 	ZS_DELAY();
1436 	i = zc->zc_data;
1437 	ZS_DELAY();
1438 #endif
1439 
1440 	/* baud clock divisor, stop bits, parity */
1441 	ZS_WRITE(zc, 4, reg[4]);
1442 
1443 	/* misc. TX/RX control bits */
1444 	ZS_WRITE(zc, 10, reg[10]);
1445 
1446 	/* char size, enable (RX/TX) */
1447 	ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
1448 	ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
1449 
1450 	/* interrupt enables: TX, TX, STATUS */
1451 	ZS_WRITE(zc, 1, reg[1]);
1452 
1453 	/* interrupt vector */
1454 	ZS_WRITE(zc, 2, reg[2]);
1455 
1456 	/* master interrupt control */
1457 	ZS_WRITE(zc, 9, reg[9]);
1458 
1459 	/* clock mode control */
1460 	ZS_WRITE(zc, 11, reg[11]);
1461 
1462 	/* baud rate (lo/hi) */
1463 	ZS_WRITE(zc, 12, reg[12]);
1464 	ZS_WRITE(zc, 13, reg[13]);
1465 
1466 	/* Misc. control bits */
1467 	ZS_WRITE(zc, 14, reg[14]);
1468 
1469 	/* which lines cause status interrupts */
1470 	ZS_WRITE(zc, 15, reg[15]);
1471 
1472 	/* char size, enable (RX/TX)*/
1473 	ZS_WRITE(zc, 3, reg[3]);
1474 	ZS_WRITE(zc, 5, reg[5]);
1475 }
1476 
1477 static u_char
1478 zs_read(zc, reg)
1479 	volatile struct zschan *zc;
1480 	u_char reg;
1481 {
1482 	u_char val;
1483 
1484 	zc->zc_csr = reg;
1485 	ZS_DELAY();
1486 	val = zc->zc_csr;
1487 	ZS_DELAY();
1488 	return val;
1489 }
1490 
1491 static u_char
1492 zs_write(zc, reg, val)
1493 	volatile struct zschan *zc;
1494 	u_char reg, val;
1495 {
1496 	zc->zc_csr = reg;
1497 	ZS_DELAY();
1498 	zc->zc_csr = val;
1499 	ZS_DELAY();
1500 	return val;
1501 }
1502 
1503 #ifdef KGDB
1504 /*
1505  * Get a character from the given kgdb channel.  Called at splhigh().
1506  * XXX - Add delays, or combine with zscngetc()...
1507  */
1508 static int
1509 zs_kgdb_getc(void *arg)
1510 {
1511 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
1512 
1513 	while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
1514 		continue;
1515 	return (zc->zc_data);
1516 }
1517 
1518 /*
1519  * Put a character to the given kgdb channel.  Called at splhigh().
1520  */
1521 static void
1522 zs_kgdb_putc(void *arg, int c)
1523 {
1524 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
1525 
1526 	while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
1527 		continue;
1528 	zc->zc_data = c;
1529 }
1530 
1531 /*
1532  * Set up for kgdb; called at boot time before configuration.
1533  * KGDB interrupts will be enabled later when zs0 is configured.
1534  */
1535 void
1536 zs_kgdb_init()
1537 {
1538 	volatile struct zsdevice *addr;
1539 	volatile struct zschan *zc;
1540 	int unit, zs;
1541 
1542 	if (major(kgdb_dev) != ZSMAJOR)
1543 		return;
1544 	unit = minor(kgdb_dev);
1545 	/*
1546 	 * Unit must be 0 or 1 (zs0).
1547 	 */
1548 	if ((unsigned)unit >= ZS_KBD) {
1549 		printf("zs_kgdb_init: bad minor dev %d\n", unit);
1550 		return;
1551 	}
1552 	zs = unit >> 1;
1553 	unit &= 1;
1554 
1555 	if (zsaddr[0] == NULL)
1556 		panic("kbdb_attach: zs0 not yet mapped");
1557 	addr = zsaddr[0];
1558 
1559 	zc = unit == 0 ? &addr->zs_chan[ZS_CHAN_A] : &addr->zs_chan[ZS_CHAN_B];
1560 	zs_kgdb_savedspeed = zs_getspeed(zc);
1561 	printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
1562 	    zs, unit + 'a', kgdb_rate);
1563 	zs_reset(zc, 1, kgdb_rate);
1564 	kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
1565 }
1566 #endif /* KGDB */
1567