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