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