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