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