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