xref: /openbsd/sys/arch/sparc64/dev/zs.c (revision 91f110e0)
1 /*	$OpenBSD: zs.c,v 1.26 2013/11/01 21:22:31 miod Exp $	*/
2 /*	$NetBSD: zs.c,v 1.29 2001/05/30 15:24:24 lukem Exp $	*/
3 
4 /*-
5  * Copyright (c) 1996 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Gordon W. Ross.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Zilog Z8530 Dual UART driver (machine-dependent part)
35  *
36  * Runs two serial lines per chip using slave drivers.
37  * Plain tty/async lines use the zstty slave.
38  * Sun keyboard/mouse uses the zskbd/zsms slaves.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/device.h>
45 #include <sys/file.h>
46 #include <sys/ioctl.h>
47 #include <sys/kernel.h>
48 #include <sys/proc.h>
49 #include <sys/tty.h>
50 #include <sys/time.h>
51 #include <sys/syslog.h>
52 
53 #include <machine/autoconf.h>
54 #include <machine/openfirm.h>
55 #include <machine/conf.h>
56 #include <machine/cpu.h>
57 #include <machine/psl.h>
58 #include <machine/z8530var.h>
59 
60 #include <dev/cons.h>
61 #include <dev/ic/z8530reg.h>
62 #include <sparc64/dev/fhcvar.h>
63 #include <ddb/db_output.h>
64 
65 #include <sparc64/dev/cons.h>
66 
67 struct cfdriver zs_cd = {
68 	NULL, "zs", DV_TTY
69 };
70 
71 /*
72  * Some warts needed by z8530tty.c -
73  * The default parity REALLY needs to be the same as the PROM uses,
74  * or you can not see messages done with printf during boot-up...
75  */
76 int zs_def_cflag = (CREAD | CS8 | HUPCL);
77 int zs_major = 12;
78 
79 /*
80  * The Sun provides a 4.9152 MHz clock to the ZS chips.
81  */
82 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
83 
84 #define	ZS_DELAY()
85 
86 /* The layout of this is hardware-dependent (padding, order). */
87 struct zschan {
88 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
89 	u_char		zc_xxx0;
90 	volatile u_char	zc_data;	/* data */
91 	u_char		zc_xxx1;
92 };
93 struct zsdevice {
94 	/* Yes, they are backwards. */
95 	struct	zschan zs_chan_b;
96 	struct	zschan zs_chan_a;
97 };
98 
99 /* ZS channel used as the console device (if any) */
100 void *zs_conschan_get, *zs_conschan_put;
101 
102 static u_char zs_init_reg[16] = {
103 	0,	/* 0: CMD (reset, etc.) */
104 	0,	/* 1: No interrupts yet. */
105 	0,	/* 2: IVECT */
106 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
107 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
108 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
109 	0,	/* 6: TXSYNC/SYNCLO */
110 	0,	/* 7: RXSYNC/SYNCHI */
111 	0,	/* 8: alias for data port */
112 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
113 	0,	/*10: Misc. TX/RX control bits */
114 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
115 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
116 	0,			/*13: BAUDHI (default=9600) */
117 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
118 	ZSWR15_BREAK_IE,
119 };
120 
121 /* Console ops */
122 static int  zscngetc(dev_t);
123 static void zscnputc(dev_t, int);
124 static void zscnpollc(dev_t, int);
125 
126 struct consdev zs_consdev = {
127 	NULL,
128 	NULL,
129 	zscngetc,
130 	zscnputc,
131 	zscnpollc,
132 	NULL,
133 };
134 
135 
136 /****************************************************************
137  * Autoconfig
138  ****************************************************************/
139 
140 /* Definition of the driver for autoconfig. */
141 static int  zs_match_sbus(struct device *, void *, void *);
142 static void zs_attach_sbus(struct device *, struct device *, void *);
143 
144 static int  zs_match_fhc(struct device *, void *, void *);
145 static void zs_attach_fhc(struct device *, struct device *, void *);
146 
147 static void zs_attach(struct zsc_softc *, struct zsdevice *, int);
148 static int  zs_print(void *, const char *name);
149 
150 struct cfattach zs_sbus_ca = {
151 	sizeof(struct zsc_softc), zs_match_sbus, zs_attach_sbus
152 };
153 
154 struct cfattach zs_fhc_ca = {
155 	sizeof(struct zsc_softc), zs_match_fhc, zs_attach_fhc
156 };
157 
158 extern int stdinnode;
159 extern int fbnode;
160 
161 /* Interrupt handlers. */
162 static int zshard(void *);
163 static void zssoft(void *);
164 
165 static int zs_get_speed(struct zs_chanstate *);
166 
167 /* Console device support */
168 static int zs_console_flags(int, int, int);
169 
170 /*
171  * Is the zs chip present?
172  */
173 static int
174 zs_match_sbus(parent, vcf, aux)
175 	struct device *parent;
176 	void *vcf;
177 	void *aux;
178 {
179 	struct cfdata *cf = vcf;
180 	struct sbus_attach_args *sa = aux;
181 
182 	if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
183 		return (0);
184 
185 	return (1);
186 }
187 
188 static int
189 zs_match_fhc(parent, vcf, aux)
190 	struct device *parent;
191 	void *vcf;
192 	void *aux;
193 {
194 	struct cfdata *cf = vcf;
195 	struct fhc_attach_args *fa = aux;
196 
197 	if (strcmp(cf->cf_driver->cd_name, fa->fa_name) != 0)
198 		return (0);
199 	return (1);
200 }
201 
202 static void
203 zs_attach_sbus(parent, self, aux)
204 	struct device *parent;
205 	struct device *self;
206 	void *aux;
207 {
208 	struct zsc_softc *zsc = (void *) self;
209 	struct sbus_attach_args *sa = aux;
210 	struct zsdevice *zsaddr;
211 	bus_space_handle_t kvaddr;
212 
213 	if (sa->sa_nintr == 0) {
214 		printf(" no interrupt lines\n");
215 		return;
216 	}
217 
218 	/* Only map registers once. */
219 	if (sa->sa_npromvaddrs) {
220 		/*
221 		 * We're converting from a 32-bit pointer to a 64-bit
222 		 * pointer.  Since the 32-bit entity is negative, but
223 		 * the kernel is still mapped into the lower 4GB
224 		 * range, this needs to be zero-extended.
225 		 *
226 		 * XXXXX If we map the kernel and devices into the
227 		 * high 4GB range, this needs to be changed to
228 		 * sign-extend the address.
229 		 */
230 		zsaddr = (struct zsdevice *)
231 		    (unsigned long int)sa->sa_promvaddrs[0];
232 	} else {
233 		if (sbus_bus_map(sa->sa_bustag, sa->sa_slot, sa->sa_offset,
234 		    sa->sa_size, BUS_SPACE_MAP_LINEAR, 0, &kvaddr) != 0) {
235 			printf("%s @ sbus: cannot map registers\n",
236 			       self->dv_xname);
237 			return;
238 		}
239 		zsaddr = (struct zsdevice *)
240 		    bus_space_vaddr(sa->sa_bustag, kvaddr);
241 	}
242 
243 	zsc->zsc_bustag = sa->sa_bustag;
244 	zsc->zsc_dmatag = sa->sa_dmatag;
245 	zsc->zsc_promunit = getpropint(sa->sa_node, "slave", -2);
246 	zsc->zsc_node = sa->sa_node;
247 
248 	zs_attach(zsc, zsaddr, sa->sa_pri);
249 }
250 
251 static void
252 zs_attach_fhc(parent, self, aux)
253 	struct device *parent;
254 	struct device *self;
255 	void *aux;
256 {
257 	struct zsc_softc *zsc = (void *) self;
258 	struct fhc_attach_args *fa = aux;
259 	struct zsdevice *zsaddr;
260 	bus_space_handle_t kvaddr;
261 
262 	if (fa->fa_nreg < 1 && fa->fa_npromvaddrs < 1) {
263 		printf(": no registers\n");
264 		return;
265 	}
266 
267 	if (fa->fa_nintr < 1) {
268 		printf(": no interrupts\n");
269 		return;
270 	}
271 
272 	if (fa->fa_npromvaddrs) {
273 		/*
274 		 * We're converting from a 32-bit pointer to a 64-bit
275 		 * pointer.  Since the 32-bit entity is negative, but
276 		 * the kernel is still mapped into the lower 4GB
277 		 * range, this needs to be zero-extended.
278 		 *
279 		 * XXXXX If we map the kernel and devices into the
280 		 * high 4GB range, this needs to be changed to
281 		 * sign-extend the address.
282 		 */
283 		zsaddr = (struct zsdevice *)
284 		    (unsigned long int)fa->fa_promvaddrs[0];
285 	} else {
286 		if (fhc_bus_map(fa->fa_bustag, fa->fa_reg[0].fbr_slot,
287 		    fa->fa_reg[0].fbr_offset, fa->fa_reg[0].fbr_size,
288 		    BUS_SPACE_MAP_LINEAR, &kvaddr) != 0) {
289 			printf("%s @ fhc: cannot map registers\n",
290 			    self->dv_xname);
291 			return;
292 		}
293 		zsaddr = (struct zsdevice *)
294 		    bus_space_vaddr(fa->fa_bustag, kvaddr);
295 	}
296 
297 	zsc->zsc_bustag = fa->fa_bustag;
298 	zsc->zsc_dmatag = NULL;
299 	zsc->zsc_promunit = getpropint(fa->fa_node, "slave", -2);
300 	zsc->zsc_node = fa->fa_node;
301 
302 	zs_attach(zsc, zsaddr, fa->fa_intr[0]);
303 }
304 
305 /*
306  * Attach a found zs.
307  *
308  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
309  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
310  */
311 static void
312 zs_attach(zsc, zsd, pri)
313 	struct zsc_softc *zsc;
314 	struct zsdevice *zsd;
315 	int pri;
316 {
317 	struct zsc_attach_args zsc_args;
318 	struct zs_chanstate *cs;
319 	int s, channel, softpri = PIL_TTY;
320 
321 	if (zsd == NULL) {
322 		printf("configuration incomplete\n");
323 		return;
324 	}
325 
326 	printf(" softpri %d\n", softpri);
327 
328 	/*
329 	 * Initialize software state for each channel.
330 	 */
331 	for (channel = 0; channel < 2; channel++) {
332 		struct zschan *zc;
333 		struct device *child;
334 
335 		zsc_args.type = "serial";
336 		if (getproplen(zsc->zsc_node, "keyboard") == 0) {
337 			if (channel == 0)
338 				zsc_args.type = "keyboard";
339 			if (channel == 1)
340 				zsc_args.type = "mouse";
341 		}
342 
343 		zsc_args.channel = channel;
344 		cs = &zsc->zsc_cs_store[channel];
345 		zsc->zsc_cs[channel] = cs;
346 
347 		cs->cs_channel = channel;
348 		cs->cs_private = NULL;
349 		cs->cs_ops = &zsops_null;
350 		cs->cs_brg_clk = PCLK / 16;
351 
352 		zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
353 
354 		zsc_args.consdev = NULL;
355 		zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit,
356 						    zsc->zsc_node,
357 						    channel);
358 
359 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
360 			zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
361 			zsc_args.consdev = &zs_consdev;
362 		}
363 
364 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
365 			zs_conschan_get = zc;
366 		}
367 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
368 			zs_conschan_put = zc;
369 		}
370 		/* Childs need to set cn_dev, etc */
371 
372 		cs->cs_reg_csr  = &zc->zc_csr;
373 		cs->cs_reg_data = &zc->zc_data;
374 
375 		bcopy(zs_init_reg, cs->cs_creg, 16);
376 		bcopy(zs_init_reg, cs->cs_preg, 16);
377 
378 		/* XXX: Consult PROM properties for this?! */
379 		cs->cs_defspeed = zs_get_speed(cs);
380 		cs->cs_defcflag = zs_def_cflag;
381 
382 		/* Make these correspond to cs_defcflag (-crtscts) */
383 		cs->cs_rr0_dcd = ZSRR0_DCD;
384 		cs->cs_rr0_cts = 0;
385 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
386 		cs->cs_wr5_rts = 0;
387 
388 		/*
389 		 * Clear the master interrupt enable.
390 		 * The INTENA is common to both channels,
391 		 * so just do it on the A channel.
392 		 */
393 		if (channel == 0) {
394 			zs_write_reg(cs, 9, 0);
395 		}
396 
397 		/*
398 		 * Look for a child driver for this channel.
399 		 * The child attach will setup the hardware.
400 		 */
401 		if (!(child =
402 		      config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print))) {
403 			/* No sub-driver.  Just reset it. */
404 			u_char reset = (channel == 0) ?
405 				ZSWR9_A_RESET : ZSWR9_B_RESET;
406 			s = splzs();
407 			zs_write_reg(cs,  9, reset);
408 			splx(s);
409 		}
410 	}
411 
412 	/*
413 	 * Now safe to install interrupt handlers.
414 	 */
415 	if (bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, 0, zshard,
416 	    zsc, zsc->zsc_dev.dv_xname) == NULL)
417 		panic("zsattach: could not establish interrupt");
418 	if (!(zsc->zsc_softintr = softintr_establish(softpri, zssoft, zsc)))
419 		panic("zsattach: could not establish soft interrupt");
420 
421 	/*
422 	 * Set the master interrupt enable and interrupt vector.
423 	 * (common to both channels, do it on A)
424 	 */
425 	cs = zsc->zsc_cs[0];
426 	s = splhigh();
427 	/* interrupt vector */
428 	zs_write_reg(cs, 2, zs_init_reg[2]);
429 	/* master interrupt control (enable) */
430 	zs_write_reg(cs, 9, zs_init_reg[9]);
431 	splx(s);
432 
433 }
434 
435 static int
436 zs_print(aux, name)
437 	void *aux;
438 	const char *name;
439 {
440 	struct zsc_attach_args *args = aux;
441 
442 	if (name != NULL)
443 		printf("%s: ", name);
444 
445 	if (args->channel != -1)
446 		printf(" channel %d", args->channel);
447 
448 	return (UNCONF);
449 }
450 
451 static int
452 zshard(arg)
453 	void *arg;
454 {
455 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
456 	int rval = 0;
457 
458 	while (zsc_intr_hard(zsc))
459 		rval = 1;
460 	if ((zsc->zsc_cs[0] && zsc->zsc_cs[0]->cs_softreq) ||
461 	    (zsc->zsc_cs[1] && zsc->zsc_cs[1]->cs_softreq))
462 		softintr_schedule(zsc->zsc_softintr);
463 	return (rval);
464 }
465 
466 /*
467  * We need this only for TTY_DEBUG purposes.
468  */
469 static void
470 zssoft(arg)
471 	void *arg;
472 {
473 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
474 	int s;
475 
476 	/* Make sure we call the tty layer at spltty. */
477 	s = spltty();
478 	(void)zsc_intr_soft(zsc);
479 #ifdef TTY_DEBUG
480 	{
481 		struct zstty_softc *zst0 = zsc->zsc_cs[0]->cs_private;
482 		struct zstty_softc *zst1 = zsc->zsc_cs[1]->cs_private;
483 		if (zst0->zst_overflows || zst1->zst_overflows ) {
484 			struct trapframe *frame = (struct trapframe *)arg;
485 
486 			printf("zs silo overflow from %p\n",
487 			       (long)frame->tf_pc);
488 		}
489 	}
490 #endif
491 	splx(s);
492 }
493 
494 
495 /*
496  * Compute the current baud rate given a ZS channel.
497  */
498 static int
499 zs_get_speed(cs)
500 	struct zs_chanstate *cs;
501 {
502 	int tconst;
503 
504 	tconst = zs_read_reg(cs, 12);
505 	tconst |= zs_read_reg(cs, 13) << 8;
506 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
507 }
508 
509 /*
510  * MD functions for setting the baud rate and control modes.
511  */
512 int
513 zs_set_speed(cs, bps)
514 	struct zs_chanstate *cs;
515 	int bps;	/* bits per second */
516 {
517 	int tconst, real_bps;
518 
519 	if (bps == 0)
520 		return (0);
521 
522 #ifdef	DIAGNOSTIC
523 	if (cs->cs_brg_clk == 0)
524 		panic("zs_set_speed");
525 #endif
526 
527 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
528 	if (tconst < 0)
529 		return (EINVAL);
530 
531 	/* Convert back to make sure we can do it. */
532 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
533 
534 	/* XXX - Allow some tolerance here? */
535 	if (real_bps != bps)
536 		return (EINVAL);
537 
538 	cs->cs_preg[12] = tconst;
539 	cs->cs_preg[13] = tconst >> 8;
540 
541 	/* Caller will stuff the pending registers. */
542 	return (0);
543 }
544 
545 int
546 zs_set_modes(cs, cflag)
547 	struct zs_chanstate *cs;
548 	int cflag;
549 {
550 	int s;
551 
552 	/*
553 	 * Output hardware flow control on the chip is horrendous:
554 	 * if carrier detect drops, the receiver is disabled, and if
555 	 * CTS drops, the transmitter is stopped IN MID CHARACTER!
556 	 * Therefore, NEVER set the HFC bit, and instead use the
557 	 * status interrupt to detect CTS changes.
558 	 */
559 	s = splzs();
560 	cs->cs_rr0_pps = 0;
561 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
562 		cs->cs_rr0_dcd = 0;
563 		if ((cflag & MDMBUF) == 0)
564 			cs->cs_rr0_pps = ZSRR0_DCD;
565 	} else
566 		cs->cs_rr0_dcd = ZSRR0_DCD;
567 	if ((cflag & CRTSCTS) != 0) {
568 		cs->cs_wr5_dtr = ZSWR5_DTR;
569 		cs->cs_wr5_rts = ZSWR5_RTS;
570 		cs->cs_rr0_cts = ZSRR0_CTS;
571 #if 0 /* JLW */
572 	} else if ((cflag & CDTRCTS) != 0) {
573 		cs->cs_wr5_dtr = 0;
574 		cs->cs_wr5_rts = ZSWR5_DTR;
575 		cs->cs_rr0_cts = ZSRR0_CTS;
576 #endif
577 	} else if ((cflag & MDMBUF) != 0) {
578 		cs->cs_wr5_dtr = 0;
579 		cs->cs_wr5_rts = ZSWR5_DTR;
580 		cs->cs_rr0_cts = ZSRR0_DCD;
581 	} else {
582 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
583 		cs->cs_wr5_rts = 0;
584 		cs->cs_rr0_cts = 0;
585 	}
586 	splx(s);
587 
588 	/* Caller will stuff the pending registers. */
589 	return (0);
590 }
591 
592 
593 /*
594  * Read or write the chip with suitable delays.
595  */
596 
597 u_char
598 zs_read_reg(cs, reg)
599 	struct zs_chanstate *cs;
600 	u_char reg;
601 {
602 	u_char val;
603 
604 	*cs->cs_reg_csr = reg;
605 	ZS_DELAY();
606 	val = *cs->cs_reg_csr;
607 	ZS_DELAY();
608 	return (val);
609 }
610 
611 void
612 zs_write_reg(cs, reg, val)
613 	struct zs_chanstate *cs;
614 	u_char reg, val;
615 {
616 	*cs->cs_reg_csr = reg;
617 	ZS_DELAY();
618 	*cs->cs_reg_csr = val;
619 	ZS_DELAY();
620 }
621 
622 u_char
623 zs_read_csr(cs)
624 	struct zs_chanstate *cs;
625 {
626 	u_char val;
627 
628 	val = *cs->cs_reg_csr;
629 	ZS_DELAY();
630 	return (val);
631 }
632 
633 void
634 zs_write_csr(cs, val)
635 	struct zs_chanstate *cs;
636 	u_char val;
637 {
638 	*cs->cs_reg_csr = val;
639 	ZS_DELAY();
640 }
641 
642 u_char
643 zs_read_data(cs)
644 	struct zs_chanstate *cs;
645 {
646 	u_char val;
647 
648 	val = *cs->cs_reg_data;
649 	ZS_DELAY();
650 	return (val);
651 }
652 
653 void
654 zs_write_data(cs, val)
655 	struct zs_chanstate *cs;
656 	u_char val;
657 {
658 	*cs->cs_reg_data = val;
659 	ZS_DELAY();
660 }
661 
662 /****************************************************************
663  * Console support functions (Sun specific!)
664  * Note: this code is allowed to know about the layout of
665  * the chip registers, and uses that to keep things simple.
666  * XXX - I think I like the mvme167 code better. -gwr
667  ****************************************************************/
668 
669 extern void Debugger(void);
670 
671 /*
672  * Handle user request to enter kernel debugger.
673  */
674 void
675 zs_abort(cs)
676 	struct zs_chanstate *cs;
677 {
678 	volatile struct zschan *zc = zs_conschan_get;
679 	int rr0;
680 
681 	/* Wait for end of break to avoid PROM abort. */
682 	/* XXX - Limit the wait? */
683 	do {
684 		rr0 = zc->zc_csr;
685 		ZS_DELAY();
686 	} while (rr0 & ZSRR0_BREAK);
687 
688 #if defined(KGDB)
689 	zskgdb(cs);
690 #elif defined(DDB)
691 	{
692 		extern int db_active;
693 
694 		if (!db_active)
695 			Debugger();
696 		else
697 			/* Debugger is probably hozed */
698 			callrom();
699 	}
700 #else
701 	printf("stopping on keyboard abort\n");
702 	callrom();
703 #endif
704 }
705 
706 
707 /*
708  * Polled input char.
709  */
710 int
711 zs_getc(arg)
712 	void *arg;
713 {
714 	volatile struct zschan *zc = arg;
715 	int s, c, rr0;
716 
717 	s = splhigh();
718 	/* Wait for a character to arrive. */
719 	do {
720 		rr0 = zc->zc_csr;
721 		ZS_DELAY();
722 	} while ((rr0 & ZSRR0_RX_READY) == 0);
723 
724 	c = zc->zc_data;
725 	ZS_DELAY();
726 	splx(s);
727 
728 	return (c);
729 }
730 
731 /*
732  * Polled output char.
733  */
734 void
735 zs_putc(arg, c)
736 	void *arg;
737 	int c;
738 {
739 	volatile struct zschan *zc = arg;
740 	int s, rr0;
741 
742 	s = splhigh();
743 
744 	/* Wait for transmitter to become ready. */
745 	do {
746 		rr0 = zc->zc_csr;
747 		ZS_DELAY();
748 	} while ((rr0 & ZSRR0_TX_READY) == 0);
749 
750 	/*
751 	 * Send the next character.
752 	 * Now you'd think that this could be followed by a ZS_DELAY()
753 	 * just like all the other chip accesses, but it turns out that
754 	 * the `transmit-ready' interrupt isn't de-asserted until
755 	 * some period of time after the register write completes
756 	 * (more than a couple instructions).  So to avoid stray
757 	 * interrupts we put in the 2us delay regardless of cpu model.
758 	 */
759 	zc->zc_data = c;
760 	delay(2);
761 
762 	splx(s);
763 }
764 
765 /*****************************************************************/
766 
767 
768 
769 
770 /*
771  * Polled console input putchar.
772  */
773 static int
774 zscngetc(dev)
775 	dev_t dev;
776 {
777 	return (zs_getc(zs_conschan_get));
778 }
779 
780 /*
781  * Polled console output putchar.
782  */
783 static void
784 zscnputc(dev, c)
785 	dev_t dev;
786 	int c;
787 {
788 	zs_putc(zs_conschan_put, c);
789 }
790 
791 int swallow_zsintrs;
792 
793 static void
794 zscnpollc(dev, on)
795 	dev_t dev;
796 	int on;
797 {
798 	/*
799 	 * Need to tell zs driver to acknowledge all interrupts or we get
800 	 * annoying spurious interrupt messages.  This is because mucking
801 	 * with spl() levels during polling does not prevent interrupts from
802 	 * being generated.
803 	 */
804 
805 	if (on)
806 		swallow_zsintrs++;
807 	else
808 		swallow_zsintrs--;
809 }
810 
811 int
812 zs_console_flags(promunit, node, channel)
813 	int promunit;
814 	int node;
815 	int channel;
816 {
817 	int cookie, flags = 0;
818 	u_int options;
819 	char buf[255];
820 
821 	/*
822 	 * We'll just to the OBP grovelling down here since that's
823 	 * the only type of firmware we support.
824 	 */
825 	options = OF_finddevice("/options");
826 
827 	/* Default to channel 0 if there are no explicit prom args */
828 	cookie = 0;
829 
830 	if (node == OF_instance_to_package(OF_stdin())) {
831 		if (OF_getprop(options, "input-device",
832 		    buf, sizeof(buf)) != -1) {
833 			if (strncmp("ttyb", buf, strlen("ttyb")) == 0)
834 				cookie = 1;
835 		}
836 
837 		if (channel == cookie)
838 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
839 	}
840 
841 	if (node == OF_instance_to_package(OF_stdout())) {
842 		if (OF_getprop(options, "output-device",
843 		    buf, sizeof(buf)) != -1) {
844 			if (strncmp("ttyb", buf, strlen("ttyb")) == 0)
845 				cookie = 1;
846 		}
847 
848 		if (channel == cookie)
849 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
850 	}
851 
852 	return (flags);
853 }
854