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