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