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