xref: /original-bsd/sys/sparc/dev/zs.c (revision a6d8c59f)
1 /*
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * All advertising materials mentioning features or use of this software
10  * must display the following acknowledgement:
11  *	This product includes software developed by the University of
12  *	California, Lawrence Berkeley Laboratories.
13  *
14  * %sccs.include.redist.c%
15  *
16  *	@(#)zs.c	7.3 (Berkeley) 10/11/92
17  *
18  * from: $Header: zs.c,v 1.24 92/06/30 02:24:21 torek Exp $
19  */
20 
21 /*
22  * Zilog Z8530 (ZSCC) driver.
23  *
24  * Runs two tty ports (ttya and ttyb) on zs0,
25  * and runs a keyboard and mouse on zs1.
26  *
27  * This driver knows far too much about chip to usage mappings.
28  */
29 #define	NZS	2		/* XXX */
30 
31 #include <sys/param.h>
32 #include <sys/proc.h>
33 #include <sys/device.h>
34 #include <sys/conf.h>
35 #include <sys/file.h>
36 #include <sys/ioctl.h>
37 #include <sys/tty.h>
38 #include <sys/time.h>
39 #include <sys/kernel.h>
40 #include <sys/syslog.h>
41 
42 #include <machine/autoconf.h>
43 #include <machine/cpu.h>
44 
45 #include <sparc/dev/kbd.h>
46 #include <sparc/dev/zsreg.h>
47 #include <sparc/dev/zsvar.h>
48 
49 #ifdef KGDB
50 #include <machine/remote-sl.h>
51 #endif
52 
53 #define	ZSMAJOR	12		/* XXX */
54 
55 #define	ZS_KBD		2	/* XXX */
56 #define	ZS_MOUSE	3	/* XXX */
57 
58 /* the magic number below was stolen from the Sprite source. */
59 #define PCLK	(19660800/4)	/* PCLK pin input clock rate */
60 
61 /*
62  * Select software interrupt bit based on TTY ipl.
63  */
64 #if PIL_TTY == 1
65 # define IE_ZSSOFT IE_L1
66 #elif PIL_TTY == 4
67 # define IE_ZSSOFT IE_L4
68 #elif PIL_TTY == 6
69 # define IE_ZSSOFT IE_L6
70 #else
71 # error "no suitable software interrupt bit"
72 #endif
73 
74 /*
75  * Software state per found chip.  This would be called `zs_softc',
76  * but the previous driver had a rather different zs_softc....
77  */
78 struct zsinfo {
79 	struct	device zi_dev;		/* base device */
80 	volatile struct zsdevice *zi_zs;/* chip registers */
81 	struct	zs_chanstate zi_cs[2];	/* channel A and B software state */
82 };
83 
84 struct tty zs_tty[NZS * 2];		/* XXX should be dynamic */
85 
86 /* Definition of the driver for autoconfig. */
87 static int	zsmatch(struct device *, struct cfdata *, void *);
88 static void	zsattach(struct device *, struct device *, void *);
89 struct cfdriver zscd =
90     { NULL, "zs", zsmatch, zsattach, DV_TTY, sizeof(struct zsinfo) };
91 
92 /* Interrupt handlers. */
93 static int	zshard(void *);
94 static struct intrhand levelhard = { zshard };
95 static int	zssoft(void *);
96 static struct intrhand levelsoft = { zssoft };
97 
98 struct zs_chanstate *zslist;
99 
100 /* Routines called from other code. */
101 static void	zsiopen(struct tty *);
102 static void	zsiclose(struct tty *);
103 static void	zsstart(struct tty *);
104 static void	zsstop(struct tty *, int);
105 static int	zsparam(struct tty *, struct termios *);
106 
107 /* Routines purely local to this driver. */
108 static int	zs_getspeed(volatile struct zschan *);
109 static void	zs_reset(volatile struct zschan *, int, int);
110 static void	zs_modem(struct zs_chanstate *, int);
111 static void	zs_loadchannelregs(volatile struct zschan *, u_char *);
112 
113 /* Console stuff. */
114 static struct tty *zs_ctty;	/* console `struct tty *' */
115 static int zs_consin = -1, zs_consout = -1;
116 static int zscnputc(int);	/* console putc function */
117 static volatile struct zschan *zs_conschan;
118 static struct tty *zs_checkcons(struct zsinfo *, int, struct zs_chanstate *);
119 
120 #ifdef KGDB
121 /* KGDB stuff.  Must reboot to change zs_kgdbunit. */
122 extern int kgdb_dev, kgdb_rate;
123 static int zs_kgdb_savedspeed;
124 static void zs_checkkgdb(int, struct zs_chanstate *, struct tty *);
125 #endif
126 
127 extern volatile struct zsdevice *findzs(int);
128 static volatile struct zsdevice *zsaddr[NZS];	/* XXX, but saves work */
129 
130 /*
131  * Console keyboard L1-A processing is done in the hardware interrupt code,
132  * so we need to duplicate some of the console keyboard decode state.  (We
133  * must not use the regular state as the hardware code keeps ahead of the
134  * software state: the software state tracks the most recent ring input but
135  * the hardware state tracks the most recent ZSCC input.)  See also kbd.h.
136  */
137 static struct conk_state {	/* console keyboard state */
138 	char	conk_id;	/* true => ID coming up (console only) */
139 	char	conk_l1;	/* true => L1 pressed (console only) */
140 } zsconk_state;
141 
142 /*
143  * Match slave number to zs unit number, so that misconfiguration will
144  * not set up the keyboard as ttya, etc.
145  */
146 static int
147 zsmatch(struct device *parent, struct cfdata *cf, void *aux)
148 {
149 	struct romaux *ra = aux;
150 
151 	return (getpropint(ra->ra_node, "slave", -2) == cf->cf_unit);
152 }
153 
154 /*
155  * Attach a found zs.
156  *
157  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
158  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
159  */
160 static void
161 zsattach(struct device *parent, struct device *dev, void *aux)
162 {
163 	register int zs = dev->dv_unit, unit;
164 	register struct zsinfo *zi;
165 	register struct zs_chanstate *cs;
166 	register volatile struct zsdevice *addr;
167 	register struct tty *tp, *ctp;
168 	register struct romaux *ra = aux;
169 	int pri, softcar;
170 	static int didintr, prevpri;
171 
172 	if ((addr = zsaddr[zs]) == NULL)
173 		addr = zsaddr[zs] = findzs(zs);
174 	if ((void *)addr != ra->ra_vaddr)
175 		panic("zsattach");
176 	if (ra->ra_nintr != 1) {
177 		printf(": expected 1 interrupt, got %d\n", ra->ra_nintr);
178 		return;
179 	}
180 	pri = ra->ra_intr[0].int_pri;
181 	printf(" pri %d, softpri %d\n", pri, PIL_TTY);
182 	if (!didintr) {
183 		didintr = 1;
184 		prevpri = pri;
185 		intr_establish(pri, &levelhard);
186 		intr_establish(PIL_TTY, &levelsoft);
187 	} else if (pri != prevpri)
188 		panic("broken zs interrupt scheme");
189 	zi = (struct zsinfo *)dev;
190 	zi->zi_zs = addr;
191 	unit = zs * 2;
192 	cs = zi->zi_cs;
193 	tp = &zs_tty[unit];
194 
195 	if (unit == 0) {
196 		/* Get software carrier flags from options node in OPENPROM. */
197 		extern int optionsnode;
198 
199 		softcar = 0;
200 		if (*getpropstring(optionsnode, "ttya-ignore-cd") == 't')
201 			softcar |= 1;
202 		if (*getpropstring(optionsnode, "ttyb-ignore-cd") == 't')
203 			softcar |= 2;
204 	} else
205 		softcar = dev->dv_cfdata->cf_flags;
206 
207 	/* link into interrupt list with order (A,B) (B=A+1) */
208 	cs[0].cs_next = &cs[1];
209 	cs[1].cs_next = zslist;
210 	zslist = cs;
211 
212 	cs->cs_unit = unit;
213 	cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_A]);
214 	cs->cs_softcar = softcar & 1;
215 	cs->cs_zc = &addr->zs_chan[CHAN_A];
216 	tp->t_dev = makedev(ZSMAJOR, unit);
217 	tp->t_oproc = zsstart;
218 	tp->t_param = zsparam;
219 	tp->t_stop = zsstop;
220 	if ((ctp = zs_checkcons(zi, unit, cs)) != NULL)
221 		tp = ctp;
222 	cs->cs_ttyp = tp;
223 #ifdef KGDB
224 	if (ctp == NULL)
225 		zs_checkkgdb(unit, cs, tp);
226 	else
227 #endif
228 		zs_reset(&addr->zs_chan[CHAN_A], 0, cs->cs_speed);
229 	if (unit == ZS_KBD) {
230 		/*
231 		 * Keyboard: tell /dev/kbd driver how to talk to us.
232 		 */
233 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
234 		tp->t_cflag = CS8;
235 		kbd_serial(tp, zsiopen, zsiclose);
236 		cs->cs_conk = 1;		/* do L1-A processing */
237 	}
238 	unit++;
239 	cs++;
240 	tp++;
241 	cs->cs_unit = unit;
242 	cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_B]);
243 	cs->cs_softcar = softcar & 2;
244 	cs->cs_zc = &addr->zs_chan[CHAN_B];
245 	tp->t_dev = makedev(ZSMAJOR, unit);
246 	tp->t_oproc = zsstart;
247 	tp->t_param = zsparam;
248 	tp->t_stop = zsstop;
249 	if ((ctp = zs_checkcons(zi, unit, cs)) != NULL)
250 		tp = ctp;
251 	cs->cs_ttyp = tp;
252 #ifdef KGDB
253 	if (ctp == NULL)
254 		zs_checkkgdb(unit, cs, tp);
255 	else
256 #endif
257 		zs_reset(&addr->zs_chan[CHAN_B], 0, cs->cs_speed);
258 	if (unit == ZS_MOUSE) {
259 		/*
260 		 * Mouse: tell /dev/mouse driver how to talk to us.
261 		 */
262 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
263 		tp->t_cflag = CS8;
264 		ms_serial(tp, zsiopen, zsiclose);
265 	}
266 }
267 
268 /*
269  * Put a channel in a known state.  Interrupts may be left disabled
270  * or enabled, as desired.
271  */
272 static void
273 zs_reset(zc, inten, speed)
274 	volatile struct zschan *zc;
275 	int inten, speed;
276 {
277 	int tconst;
278 	static u_char reg[16] = {
279 		0,
280 		0,
281 		0,
282 		ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
283 		ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
284 		ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
285 		0,
286 		0,
287 		0,
288 		0,
289 		ZSWR10_NRZ,
290 		ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
291 		0,
292 		0,
293 		ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
294 		ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
295 	};
296 
297 	reg[9] = inten ? ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR : ZSWR9_NO_VECTOR;
298 	tconst = BPS_TO_TCONST(PCLK / 16, speed);
299 	reg[12] = tconst;
300 	reg[13] = tconst >> 8;
301 	zs_loadchannelregs(zc, reg);
302 }
303 
304 /*
305  * Declare the given tty (which is in fact &cons) as a console input
306  * or output.  This happens before the zs chip is attached; the hookup
307  * is finished later, in zs_setcons() below.
308  *
309  * This is used only for ports a and b.  The console keyboard is decoded
310  * independently (we always send unit-2 input to /dev/kbd, which will
311  * direct it to /dev/console if appropriate).
312  */
313 void
314 zsconsole(tp, unit, out)
315 	register struct tty *tp;
316 	register int unit;
317 	int out;
318 {
319 	extern int (*v_putc)();
320 	int zs;
321 	volatile struct zsdevice *addr;
322 
323 	if (unit >= ZS_KBD)
324 		panic("zsconsole");
325 	if (out) {
326 		zs_consout = unit;
327 		zs = unit >> 1;
328 		if ((addr = zsaddr[zs]) == NULL)
329 			addr = zsaddr[zs] = findzs(zs);
330 		zs_conschan = (unit & 1) == 0 ? &addr->zs_chan[CHAN_A] :
331 		    &addr->zs_chan[CHAN_B];
332 		v_putc = zscnputc;
333 	} else
334 		zs_consin = unit;
335 	zs_ctty = tp;
336 }
337 
338 /*
339  * Polled console output putchar.
340  */
341 static int
342 zscnputc(c)
343 	int c;
344 {
345 	register volatile struct zschan *zc = zs_conschan;
346 	register int s;
347 
348 	/*
349 	 * Must block output interrupts (i.e., raise to >= splzs) without
350 	 * lowering current ipl.  Need a better way.
351 	 */
352 	s = splhigh();
353 #ifdef sun4c		/* XXX */
354 	if (s <= (12 << 8))
355 		(void) splzs();
356 #endif
357 	while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
358 		continue;
359 	zc->zc_data = c;
360 	splx(s);
361 }
362 
363 /*
364  * Set up the given unit as console input, output, both, or neither, as
365  * needed.  Return console tty if it is to receive console input.
366  */
367 static struct tty *
368 zs_checkcons(struct zsinfo *zi, int unit, struct zs_chanstate *cs)
369 {
370 	register struct tty *tp;
371 	char *i, *o;
372 
373 	if ((tp = zs_ctty) == NULL)
374 		return (0);
375 	i = zs_consin == unit ? "input" : NULL;
376 	o = zs_consout == unit ? "output" : NULL;
377 	if (i == NULL && o == NULL)
378 		return (0);
379 
380 	/* rewire the minor device (gack) */
381 	tp->t_dev = makedev(major(tp->t_dev), unit);
382 
383 	/*
384 	 * Rewire input and/or output.  Note that baud rate reflects
385 	 * input settings, not output settings, but we can do no better
386 	 * if the console is split across two ports.
387 	 */
388 	if (i) {
389 		tp->t_param = zsparam;
390 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
391 		tp->t_cflag = CS8;
392 		ttsetwater(tp);
393 	}
394 	if (o) {
395 		tp->t_oproc = zsstart;
396 		tp->t_stop = zsstop;
397 	}
398 	printf("%s%c: console %s\n",
399 	    zi->zi_dev.dv_xname, (unit & 1) + 'a', i ? (o ? "i/o" : i) : o);
400 	cs->cs_consio = 1;
401 	cs->cs_brkabort = 1;
402 	return (i ? tp : NULL);
403 }
404 
405 #ifdef KGDB
406 /*
407  * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init).
408  * Pick up the current speed and character size and restore the original
409  * speed.
410  */
411 static void
412 zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp)
413 {
414 
415 	if (kgdb_dev == makedev(ZSMAJOR, unit)) {
416 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
417 		tp->t_cflag = CS8;
418 		cs->cs_kgdb = 1;
419 		cs->cs_speed = zs_kgdb_savedspeed;
420 		(void) zsparam(tp, &tp->t_termios);
421 	}
422 }
423 #endif
424 
425 /*
426  * Compute the current baud rate given a ZSCC channel.
427  */
428 static int
429 zs_getspeed(zc)
430 	register volatile struct zschan *zc;
431 {
432 	register int tconst;
433 
434 	tconst = ZS_READ(zc, 12);
435 	tconst |= ZS_READ(zc, 13) << 8;
436 	return (TCONST_TO_BPS(PCLK / 16, tconst));
437 }
438 
439 
440 /*
441  * Do an internal open.
442  */
443 static void
444 zsiopen(struct tty *tp)
445 {
446 
447 	(void) zsparam(tp, &tp->t_termios);
448 	ttsetwater(tp);
449 	tp->t_state = TS_ISOPEN | TS_CARR_ON;
450 }
451 
452 /*
453  * Do an internal close.  Eventually we should shut off the chip when both
454  * ports on it are closed.
455  */
456 static void
457 zsiclose(struct tty *tp)
458 {
459 
460 	ttylclose(tp, 0);	/* ??? */
461 	ttyclose(tp);		/* ??? */
462 	tp->t_state = 0;
463 }
464 
465 
466 /*
467  * Open a zs serial port.  This interface may not be used to open
468  * the keyboard and mouse ports. (XXX)
469  */
470 int
471 zsopen(dev_t dev, int flags, int mode, struct proc *p)
472 {
473 	register struct tty *tp;
474 	register struct zs_chanstate *cs;
475 	struct zsinfo *zi;
476 	int unit = minor(dev), zs = unit >> 1, error, s;
477 
478 	if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL ||
479 	    unit == ZS_KBD || unit == ZS_MOUSE)
480 		return (ENXIO);
481 	cs = &zi->zi_cs[unit & 1];
482 	if (cs->cs_consio)
483 		return (ENXIO);		/* ??? */
484 	tp = cs->cs_ttyp;
485 	s = spltty();
486 	if ((tp->t_state & TS_ISOPEN) == 0) {
487 		ttychars(tp);
488 		if (tp->t_ispeed == 0) {
489 			tp->t_iflag = TTYDEF_IFLAG;
490 			tp->t_oflag = TTYDEF_OFLAG;
491 			tp->t_cflag = TTYDEF_CFLAG;
492 			tp->t_lflag = TTYDEF_LFLAG;
493 			tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
494 		}
495 		(void) zsparam(tp, &tp->t_termios);
496 		ttsetwater(tp);
497 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
498 		splx(s);
499 		return (EBUSY);
500 	}
501 	error = 0;
502 	for (;;) {
503 		/* loop, turning on the device, until carrier present */
504 		zs_modem(cs, 1);
505 		if (cs->cs_softcar)
506 			tp->t_state |= TS_CARR_ON;
507 		if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
508 		    tp->t_state & TS_CARR_ON)
509 			break;
510 		tp->t_state |= TS_WOPEN;
511 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
512 		    ttopen, 0))
513 			break;
514 	}
515 	splx(s);
516 	if (error == 0)
517 		error = (*linesw[tp->t_line].l_open)(dev, tp);
518 	if (error)
519 		zs_modem(cs, 0);
520 	return (error);
521 }
522 
523 /*
524  * Close a zs serial port.
525  */
526 int
527 zsclose(dev_t dev, int flags, int mode, struct proc *p)
528 {
529 	register struct zs_chanstate *cs;
530 	register struct tty *tp;
531 	struct zsinfo *zi;
532 	int unit = minor(dev), s;
533 
534 	zi = zscd.cd_devs[unit >> 1];
535 	cs = &zi->zi_cs[unit & 1];
536 	tp = cs->cs_ttyp;
537 	(*linesw[tp->t_line].l_close)(tp, flags);
538 	if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
539 	    (tp->t_state & TS_ISOPEN) == 0) {
540 		zs_modem(cs, 0);
541 		/* hold low for 1 second */
542 		(void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
543 	}
544 	ttyclose(tp);
545 #ifdef KGDB
546 	/* Reset the speed if we're doing kgdb on this port */
547 	if (cs->cs_kgdb) {
548 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
549 		(void) zsparam(tp, &tp->t_termios);
550 	}
551 #endif
552 	return (0);
553 }
554 
555 /*
556  * Read/write zs serial port.
557  */
558 int
559 zsread(dev_t dev, struct uio *uio, int flags)
560 {
561 	register struct tty *tp = &zs_tty[minor(dev)];
562 
563 	return (linesw[tp->t_line].l_read(tp, uio, flags));
564 }
565 
566 int
567 zswrite(dev_t dev, struct uio *uio, int flags)
568 {
569 	register struct tty *tp = &zs_tty[minor(dev)];
570 
571 	return (linesw[tp->t_line].l_write(tp, uio, flags));
572 }
573 
574 /*
575  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
576  * channels are kept in (A,B) pairs.
577  *
578  * Do just a little, then get out; set a software interrupt if more
579  * work is needed.
580  *
581  * We deliberately ignore the vectoring Zilog gives us, and match up
582  * only the number of `reset interrupt under service' operations, not
583  * the order.
584  */
585 /* ARGSUSED */
586 int
587 zshard(void *intrarg)
588 {
589 	register struct zs_chanstate *a;
590 #define	b (a + 1)
591 	register int rr3, intflags = 0;
592 	static int zsrint(struct zs_chanstate *);
593 	static int zsxint(struct zs_chanstate *);
594 	static int zssint(struct zs_chanstate *);
595 
596 	for (a = zslist; a != NULL; a = b->cs_next) {
597 		rr3 = ZS_READ(a->cs_zc, 3);
598 		if (rr3 == 0)
599 			continue;
600 		intflags |= 2;		/* took an interrupt */
601 		if (rr3 & ZSRR3_IP_A_RX)
602 			intflags |= zsrint(a);
603 		if (rr3 & ZSRR3_IP_B_RX)
604 			intflags |= zsrint(b);
605 		if (rr3 & ZSRR3_IP_A_TX)
606 			intflags |= zsxint(a);
607 		if (rr3 & ZSRR3_IP_B_TX)
608 			intflags |= zsxint(b);
609 		if (rr3 & ZSRR3_IP_A_STAT)
610 			intflags |= zssint(a);
611 		if (rr3 & ZSRR3_IP_B_STAT)
612 			intflags |= zssint(b);
613 	}
614 #undef b
615 	if (intflags & 1) {
616 #if sun4c /* XXX -- but this will go away when zshard moves to locore.s */
617 		struct clockframe *p = intrarg;
618 
619 		if ((p->psr & PSR_PIL) < (PIL_TTY << 8)) {
620 			(void) spltty();
621 			return (zssoft(intrarg));
622 		}
623 #endif
624 		ienab_bis(IE_ZSSOFT);
625 	}
626 	return (intflags & 2);
627 }
628 
629 static int
630 zsrint(register struct zs_chanstate *cs)
631 {
632 	register volatile struct zschan *zc = cs->cs_zc;
633 	register int c = zc->zc_data, i;
634 
635 
636 	if (cs->cs_conk) {
637 		register struct conk_state *conk = &zsconk_state;
638 
639 		/*
640 		 * Check here for console abort function, so that we
641 		 * can abort even when interrupts are locking up the
642 		 * machine.
643 		 */
644 		if (c == KBD_RESET) {
645 			conk->conk_id = 1;	/* ignore next byte */
646 			conk->conk_l1 = 0;
647 		} else if (conk->conk_id)
648 			conk->conk_id = 0;	/* stop ignoring bytes */
649 		else if (c == KBD_L1)
650 			conk->conk_l1 = 1;	/* L1 went down */
651 		else if (c == (KBD_L1|KBD_UP))
652 			conk->conk_l1 = 0;	/* L1 went up */
653 		else if (c == KBD_A && conk->conk_l1) {
654 			zsabort();
655 			conk->conk_l1 = 0;	/* we never see the up */
656 			goto clearit;		/* eat the A after L1-A */
657 		}
658 	}
659 #ifdef KGDB
660 	if (c == FRAME_START && cs->cs_kgdb &&
661 	    (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
662 		zskgdb(cs->cs_unit);
663 		goto clearit;
664 	}
665 #endif
666 	/* store receive character and status into ring */
667 	i = cs->cs_rbput;
668 	cs->cs_rbput = i + 1;
669 	c <<= 8;
670 	c |= ZS_READ(zc, 1);
671 	cs->cs_rbuf[i & ZLRB_RING_MASK] = c;
672 
673 	/* clear receive error & interrupt condition */
674 	zc->zc_csr = ZSWR0_RESET_ERRORS;
675 	zc->zc_csr = ZSWR0_CLR_INTR;
676 	return (1);
677 
678 clearit:
679 	zc->zc_csr = ZSWR0_RESET_ERRORS;
680 	zc->zc_csr = ZSWR0_CLR_INTR;
681 	return (0);
682 }
683 
684 static int
685 zsxint(register struct zs_chanstate *cs)
686 {
687 	register volatile struct zschan *zc = cs->cs_zc;
688 	register int c, i = cs->cs_tbc;
689 
690 	if (i == 0) {
691 		zc->zc_csr = ZSWR0_RESET_TXINT;
692 		zc->zc_csr = ZSWR0_CLR_INTR;
693 		cs->cs_txint = 1;
694 		return (1);
695 	}
696 	cs->cs_tbc = i - 1;
697 	zc->zc_data = *cs->cs_tba++;
698 	zc->zc_csr = ZSWR0_CLR_INTR;
699 	return (0);
700 }
701 
702 static int
703 zssint(register struct zs_chanstate *cs)
704 {
705 	register volatile struct zschan *zc = cs->cs_zc;
706 	register int i;
707 
708 	i = zc->zc_csr;
709 	zc->zc_csr = ZSWR0_RESET_STATUS;
710 	zc->zc_csr = ZSWR0_CLR_INTR;
711 	if ((i & ZSRR0_BREAK) && cs->cs_brkabort)
712 		zsabort();
713 	else if (! cs->cs_softcar) {
714 		cs->cs_rr0 = i | 0x100;
715 		return (1);
716 	}
717 	return (0);
718 }
719 
720 zsabort()
721 {
722 
723 	printf("stopping on keyboard abort\n");
724 	callrom();
725 }
726 
727 #ifdef KGDB
728 /*
729  * KGDB framing character received: enter kernel debugger.  This probably
730  * should time out after a few seconds to avoid hanging on spurious input.
731  */
732 zskgdb(int unit)
733 {
734 
735 	printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
736 	kgdb_connect(1);
737 }
738 #endif
739 
740 /*
741  * Print out a ring or fifo overrun error message.
742  */
743 static void
744 zsoverrun(int unit, long *ptime, char *what)
745 {
746 
747 	if (*ptime != time.tv_sec) {
748 		*ptime = time.tv_sec;
749 		log(LOG_WARNING, "zs%d%c: input %s overrun\n", unit >> 1,
750 		    (unit & 1) + 'a', what);
751 	}
752 }
753 
754 /*
755  * ZS software interrupt.  Scan both ZS chips.
756  */
757 int
758 zssoft(void *arg)
759 {
760 	register struct zs_chanstate *cs;
761 	register volatile struct zschan *zc;
762 	register struct tty *tp;
763 	register int get, n, c, cc, rr0, txint, unit, s;
764 
765 	for (cs = zslist; cs; cs = cs->cs_next) {
766 		unit = cs->cs_unit;
767 		zc = cs->cs_zc;
768 		tp = cs->cs_ttyp;
769 		/*
770 		 * Scan receive ring.  This involves calling ttyinput(),
771 		 * which is quite slow, so we loop until we have caught
772 		 * up with the receiver.  (XXX should test effectiveness)
773 		 * If we are not interested, discard the input right away.
774 		 *
775 		 * XXX this will have to be broken up so that we can get
776 		 * ^S stops out reasonably quickly....
777 		 */
778 		for (;;) {
779 			n = cs->cs_rbput;	/* atomic */
780 			get = cs->cs_rbget;
781 			if (get == n)
782 				break;
783 			/*
784 			 * Compute the number of characters in the receive
785 			 * ring; drain them.  If the count is overlarge, we
786 			 * lost some receive data, and must advance to the
787 			 * first still-extant character.  It may get
788 			 * overwritten if more data are arriving, but this
789 			 * is too expensive to check and gains nothing (we
790 			 * already lost out; all we can do at this point is
791 			 * trade one kind of loss for another).
792 			 *
793 			 * XXX	should do flow control if ring is
794 			 *	getting full ... needs more thought; will
795 			 *	require locking against zshard().
796 			 */
797 			n -= get;
798 			if (n > ZLRB_RING_SIZE) {
799 				zsoverrun(unit, &cs->cs_rotime, "ring");
800 				get += n - ZLRB_RING_SIZE;
801 				n = ZLRB_RING_SIZE;
802 			}
803 			while (--n >= 0) {
804 				/* race to keep ahead of incoming data */
805 				c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
806 				if (c & ZSRR1_DO)
807 					zsoverrun(unit, &cs->cs_fotime, "fifo");
808 				cc = c >> 8;
809 				if (c & ZSRR1_FE)
810 					cc |= TTY_FE;
811 				if (c & ZSRR1_PE)
812 					cc |= TTY_PE;
813 				/*
814 				 * this should be done through
815 				 * bstreams	XXX gag choke
816 				 */
817 				if (unit == ZS_KBD)
818 					kbd_rint(cc);
819 				else if (unit == ZS_MOUSE)
820 					ms_rint(cc);
821 				else
822 					(*linesw[tp->t_line].l_rint)(cc, tp);
823 			}
824 			cs->cs_rbget = get;
825 		}
826 check_xmit:
827 		/*
828 		 * Atomically get and clear transmit and status change
829 		 * interrupts.
830 		 */
831 		s = splzs();
832 		txint = cs->cs_txint;
833 		rr0 = cs->cs_rr0;
834 		if (txint)
835 			cs->cs_txint = 0;
836 		if (rr0 & 0x100)
837 			cs->cs_rr0 = rr0 & 255;
838 		splx(s);
839 
840 		/*
841 		 * Check for status changes.  If carrier has changed,
842 		 * and we want CTS output flow control, we have to fiddle
843 		 * the HFC bit (see zsparam).  If carrier is gone, and
844 		 * linesw l_modem returns 0, drop DTR.
845 		 */
846 		if (rr0 & 0x100) {
847 			if (rr0 & ZSRR0_DCD) {
848 				(void) splzs();
849 				if (tp->t_cflag & CCTS_OFLOW &&
850 				    (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
851 					cs->cs_creg[3] |= ZSWR3_HFC;
852 					ZS_WRITE(zc, 3, cs->cs_creg[3]);
853 				}
854 				splx(s);
855 				(void) (*linesw[tp->t_line].l_modem)(tp, 1);
856 			} else {
857 				(void) splzs();
858 				if (cs->cs_creg[3] & ZSWR3_HFC) {
859 					cs->cs_creg[3] &= ~ZSWR3_HFC;
860 					ZS_WRITE(zc, 3, cs->cs_creg[3]);
861 				}
862 				splx(s);
863 				if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
864 					zs_modem(cs, 0);
865 			}
866 		}
867 
868 		if (txint) {
869 			/*
870 			 * Transmit done: change registers and resume
871 			 * or clear BUSY.
872 			 */
873 			if (cs->cs_heldchange) {
874 				s = splzs();
875 				if ((rr0 & ZSRR0_DCD) == 0)
876 					cs->cs_preg[3] &= ~ZSWR3_HFC;
877 				bcopy((caddr_t)cs->cs_preg,
878 				    (caddr_t)cs->cs_creg, 16);
879 				zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
880 				splx(s);
881 				cs->cs_heldchange = 0;
882 				if (cs->cs_heldtbc &&
883 				    (tp->t_state & TS_TTSTOP) == 0) {
884 					cs->cs_tbc = cs->cs_heldtbc - 1;
885 					zc->zc_data = *cs->cs_tba++;
886 					continue;
887 				}
888 			}
889 			tp->t_state &= ~TS_BUSY;
890 			if (tp->t_state & TS_FLUSH)
891 				tp->t_state &= ~TS_FLUSH;
892 			else
893 				ndflush(&tp->t_outq,
894 				    cs->cs_tba - tp->t_outq.c_cf);
895 			(*linesw[tp->t_line].l_start)(tp);
896 		}
897 	}
898 	return (1);
899 }
900 
901 int
902 zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
903 {
904 	int unit = minor(dev);
905 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
906 	register struct tty *tp = zi->zi_cs[unit & 1].cs_ttyp;
907 	register int error;
908 
909 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
910 	if (error >= 0)
911 		return (error);
912 	error = ttioctl(tp, cmd, data, flag);
913 	if (error >= 0)
914 		return (error);
915 
916 	switch (cmd) {
917 
918 	case TIOCSBRK:
919 		/* FINISH ME ... need implicit TIOCCBRK in zsclose as well */
920 
921 	case TIOCCBRK:
922 
923 	case TIOCSDTR:
924 
925 	case TIOCCDTR:
926 
927 	case TIOCMSET:
928 
929 	case TIOCMBIS:
930 
931 	case TIOCMBIC:
932 
933 	case TIOCMGET:
934 
935 	default:
936 		return (ENOTTY);
937 	}
938 	return (0);
939 }
940 
941 /*
942  * Start or restart transmission.
943  */
944 static void
945 zsstart(register struct tty *tp)
946 {
947 	register struct zs_chanstate *cs;
948 	register int s, nch;
949 	int unit = minor(tp->t_dev);
950 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
951 
952 	cs = &zi->zi_cs[unit & 1];
953 	s = spltty();
954 
955 	/*
956 	 * If currently active or delaying, no need to do anything.
957 	 */
958 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
959 		goto out;
960 
961 	/*
962 	 * If there are sleepers, and output has drained below low
963 	 * water mark, awaken.
964 	 */
965 	if (tp->t_outq.c_cc <= tp->t_lowat) {
966 		if (tp->t_state & TS_ASLEEP) {
967 			tp->t_state &= ~TS_ASLEEP;
968 			wakeup((caddr_t)&tp->t_outq);
969 		}
970 		selwakeup(&tp->t_wsel);
971 	}
972 
973 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
974 	if (nch) {
975 		register char *p = tp->t_outq.c_cf;
976 
977 		/* mark busy, enable tx done interrupts, & send first byte */
978 		tp->t_state |= TS_BUSY;
979 		(void) splzs();
980 		cs->cs_preg[1] |= ZSWR1_TIE;
981 		cs->cs_creg[1] |= ZSWR1_TIE;
982 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
983 		cs->cs_zc->zc_data = *p;
984 		cs->cs_tba = p + 1;
985 		cs->cs_tbc = nch - 1;
986 	} else {
987 		/*
988 		 * Nothing to send, turn off transmit done interrupts.
989 		 * This is useful if something is doing polled output.
990 		 */
991 		(void) splzs();
992 		cs->cs_preg[1] &= ~ZSWR1_TIE;
993 		cs->cs_creg[1] &= ~ZSWR1_TIE;
994 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
995 	}
996 out:
997 	splx(s);
998 }
999 
1000 /*
1001  * Stop output, e.g., for ^S or output flush.
1002  */
1003 static void
1004 zsstop(register struct tty *tp, int flag)
1005 {
1006 	register struct zs_chanstate *cs;
1007 	register int s, unit = minor(tp->t_dev);
1008 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1009 
1010 	cs = &zi->zi_cs[unit & 1];
1011 	s = splzs();
1012 	if (tp->t_state & TS_BUSY) {
1013 		/*
1014 		 * Device is transmitting; must stop it.
1015 		 */
1016 		cs->cs_tbc = 0;
1017 		if ((tp->t_state & TS_TTSTOP) == 0)
1018 			tp->t_state |= TS_FLUSH;
1019 	}
1020 	splx(s);
1021 }
1022 
1023 /*
1024  * Set ZS tty parameters from termios.
1025  *
1026  * This routine makes use of the fact that only registers
1027  * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
1028  */
1029 static int
1030 zsparam(register struct tty *tp, register struct termios *t)
1031 {
1032 	int unit = minor(tp->t_dev);
1033 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1034 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
1035 	register int tmp, tmp5, cflag, s;
1036 
1037 	/*
1038 	 * Because PCLK is only run at 4.9 MHz, the fastest we
1039 	 * can go is 51200 baud (this corresponds to TC=1).
1040 	 * This is somewhat unfortunate as there is no real
1041 	 * reason we should not be able to handle higher rates.
1042 	 */
1043 	tmp = t->c_ospeed;
1044 	if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
1045 		return (EINVAL);
1046 	if (tmp == 0) {
1047 		/* stty 0 => drop DTR and RTS */
1048 		zs_modem(cs, 0);
1049 		return (0);
1050 	}
1051 	tmp = BPS_TO_TCONST(PCLK / 16, tmp);
1052 	if (tmp < 2)
1053 		return (EINVAL);
1054 
1055 	cflag = t->c_cflag;
1056 	tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
1057 	tp->t_cflag = cflag;
1058 
1059 	/*
1060 	 * Block interrupts so that state will not
1061 	 * be altered until we are done setting it up.
1062 	 */
1063 	s = splzs();
1064 	cs->cs_preg[12] = tmp;
1065 	cs->cs_preg[13] = tmp >> 8;
1066 	cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
1067 	switch (cflag & CSIZE) {
1068 	case CS5:
1069 		tmp = ZSWR3_RX_5;
1070 		tmp5 = ZSWR5_TX_5;
1071 		break;
1072 	case CS6:
1073 		tmp = ZSWR3_RX_6;
1074 		tmp5 = ZSWR5_TX_6;
1075 		break;
1076 	case CS7:
1077 		tmp = ZSWR3_RX_7;
1078 		tmp5 = ZSWR5_TX_7;
1079 		break;
1080 	case CS8:
1081 	default:
1082 		tmp = ZSWR3_RX_8;
1083 		tmp5 = ZSWR5_TX_8;
1084 		break;
1085 	}
1086 
1087 	/*
1088 	 * Output hardware flow control on the chip is horrendous: if
1089 	 * carrier detect drops, the receiver is disabled.  Hence we
1090 	 * can only do this when the carrier is on.
1091 	 */
1092 	if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
1093 		tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE;
1094 	else
1095 		tmp |= ZSWR3_RX_ENABLE;
1096 	cs->cs_preg[3] = tmp;
1097 	cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
1098 
1099 	tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
1100 	if ((cflag & PARODD) == 0)
1101 		tmp |= ZSWR4_EVENP;
1102 	if (cflag & PARENB)
1103 		tmp |= ZSWR4_PARENB;
1104 	cs->cs_preg[4] = tmp;
1105 	cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
1106 	cs->cs_preg[10] = ZSWR10_NRZ;
1107 	cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD;
1108 	cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
1109 	cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
1110 
1111 	/*
1112 	 * If nothing is being transmitted, set up new current values,
1113 	 * else mark them as pending.
1114 	 */
1115 	if (cs->cs_heldchange == 0) {
1116 		if (cs->cs_ttyp->t_state & TS_BUSY) {
1117 			cs->cs_heldtbc = cs->cs_tbc;
1118 			cs->cs_tbc = 0;
1119 			cs->cs_heldchange = 1;
1120 		} else {
1121 			bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
1122 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
1123 		}
1124 	}
1125 	splx(s);
1126 	return (0);
1127 }
1128 
1129 /*
1130  * Raise or lower modem control (DTR/RTS) signals.  If a character is
1131  * in transmission, the change is deferred.
1132  */
1133 static void
1134 zs_modem(struct zs_chanstate *cs, int onoff)
1135 {
1136 	int s, bis, and;
1137 
1138 	if (onoff) {
1139 		bis = ZSWR5_DTR | ZSWR5_RTS;
1140 		and = ~0;
1141 	} else {
1142 		bis = 0;
1143 		and = ~(ZSWR5_DTR | ZSWR5_RTS);
1144 	}
1145 	s = splzs();
1146 	cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
1147 	if (cs->cs_heldchange == 0) {
1148 		if (cs->cs_ttyp->t_state & TS_BUSY) {
1149 			cs->cs_heldtbc = cs->cs_tbc;
1150 			cs->cs_tbc = 0;
1151 			cs->cs_heldchange = 1;
1152 		} else {
1153 			cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
1154 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1155 		}
1156 	}
1157 	splx(s);
1158 }
1159 
1160 /*
1161  * Write the given register set to the given zs channel in the proper order.
1162  * The channel must not be transmitting at the time.  The receiver will
1163  * be disabled for the time it takes to write all the registers.
1164  */
1165 static void
1166 zs_loadchannelregs(volatile struct zschan *zc, u_char *reg)
1167 {
1168 	int i;
1169 
1170 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
1171 	i = zc->zc_data;		/* drain fifo */
1172 	i = zc->zc_data;
1173 	i = zc->zc_data;
1174 	ZS_WRITE(zc, 4, reg[4]);
1175 	ZS_WRITE(zc, 10, reg[10]);
1176 	ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
1177 	ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
1178 	ZS_WRITE(zc, 1, reg[1]);
1179 	ZS_WRITE(zc, 9, reg[9]);
1180 	ZS_WRITE(zc, 11, reg[11]);
1181 	ZS_WRITE(zc, 12, reg[12]);
1182 	ZS_WRITE(zc, 13, reg[13]);
1183 	ZS_WRITE(zc, 14, reg[14]);
1184 	ZS_WRITE(zc, 15, reg[15]);
1185 	ZS_WRITE(zc, 3, reg[3]);
1186 	ZS_WRITE(zc, 5, reg[5]);
1187 }
1188 
1189 #ifdef KGDB
1190 /*
1191  * Get a character from the given kgdb channel.  Called at splhigh().
1192  */
1193 static int
1194 zs_kgdb_getc(void *arg)
1195 {
1196 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
1197 
1198 	while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
1199 		continue;
1200 	return (zc->zc_data);
1201 }
1202 
1203 /*
1204  * Put a character to the given kgdb channel.  Called at splhigh().
1205  */
1206 static void
1207 zs_kgdb_putc(void *arg, int c)
1208 {
1209 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
1210 
1211 	while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
1212 		continue;
1213 	zc->zc_data = c;
1214 }
1215 
1216 /*
1217  * Set up for kgdb; called at boot time before configuration.
1218  * KGDB interrupts will be enabled later when zs0 is configured.
1219  */
1220 void
1221 zs_kgdb_init()
1222 {
1223 	volatile struct zsdevice *addr;
1224 	volatile struct zschan *zc;
1225 	int unit, zs;
1226 
1227 	if (major(kgdb_dev) != ZSMAJOR)
1228 		return;
1229 	unit = minor(kgdb_dev);
1230 	/*
1231 	 * Unit must be 0 or 1 (zs0).
1232 	 */
1233 	if ((unsigned)unit >= ZS_KBD) {
1234 		printf("zs_kgdb_init: bad minor dev %d\n", unit);
1235 		return;
1236 	}
1237 	zs = unit >> 1;
1238 	if ((addr = zsaddr[zs]) == NULL)
1239 		addr = zsaddr[zs] = findzs(zs);
1240 	unit &= 1;
1241 	zc = unit == 0 ? &addr->zs_chan[CHAN_A] : &addr->zs_chan[CHAN_B];
1242 	zs_kgdb_savedspeed = zs_getspeed(zc);
1243 	printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
1244 	    zs, unit + 'a', kgdb_rate);
1245 	zs_reset(zc, 1, kgdb_rate);
1246 	kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
1247 }
1248 #endif /* KGDB */
1249