xref: /openbsd-src/sys/arch/sparc64/dev/zs.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: zs.c,v 1.22 2008/06/26 05:42:13 ray 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 /* Deprecate this? */
478 static volatile int zssoftpending;
479 
480 static int
481 zshard(arg)
482 	void *arg;
483 {
484 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
485 	int rr3, rval;
486 
487 	rval = 0;
488 	while ((rr3 = zsc_intr_hard(zsc))) {
489 		/* Count up the interrupts. */
490 		rval |= rr3;
491 	}
492 	if (((zsc->zsc_cs[0] && zsc->zsc_cs[0]->cs_softreq) ||
493 	     (zsc->zsc_cs[1] && zsc->zsc_cs[1]->cs_softreq)) &&
494 	    zsc->zsc_softintr) {
495 		zssoftpending = PIL_TTY;
496 		softintr_schedule(zsc->zsc_softintr);
497 	}
498 	return (rval);
499 }
500 
501 int
502 zscheckintr(arg)
503 	void *arg;
504 {
505 	struct zsc_softc *zsc;
506 	int unit, rval;
507 
508 	rval = 0;
509 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
510 
511 		zsc = zs_cd.cd_devs[unit];
512 		if (zsc == NULL)
513 			continue;
514 		rval = (zshard((void *)zsc) || rval);
515 	}
516 	return (rval);
517 }
518 
519 
520 /*
521  * We need this only for TTY_DEBUG purposes.
522  */
523 static void
524 zssoft(arg)
525 	void *arg;
526 {
527 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
528 	int s;
529 
530 	/* Make sure we call the tty layer at spltty. */
531 	s = spltty();
532 	zssoftpending = 0;
533 	(void)zsc_intr_soft(zsc);
534 #ifdef TTY_DEBUG
535 	{
536 		struct zstty_softc *zst0 = zsc->zsc_cs[0]->cs_private;
537 		struct zstty_softc *zst1 = zsc->zsc_cs[1]->cs_private;
538 		if (zst0->zst_overflows || zst1->zst_overflows ) {
539 			struct trapframe *frame = (struct trapframe *)arg;
540 
541 			printf("zs silo overflow from %p\n",
542 			       (long)frame->tf_pc);
543 		}
544 	}
545 #endif
546 	splx(s);
547 }
548 
549 
550 /*
551  * Compute the current baud rate given a ZS channel.
552  */
553 static int
554 zs_get_speed(cs)
555 	struct zs_chanstate *cs;
556 {
557 	int tconst;
558 
559 	tconst = zs_read_reg(cs, 12);
560 	tconst |= zs_read_reg(cs, 13) << 8;
561 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
562 }
563 
564 /*
565  * MD functions for setting the baud rate and control modes.
566  */
567 int
568 zs_set_speed(cs, bps)
569 	struct zs_chanstate *cs;
570 	int bps;	/* bits per second */
571 {
572 	int tconst, real_bps;
573 
574 	if (bps == 0)
575 		return (0);
576 
577 #ifdef	DIAGNOSTIC
578 	if (cs->cs_brg_clk == 0)
579 		panic("zs_set_speed");
580 #endif
581 
582 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
583 	if (tconst < 0)
584 		return (EINVAL);
585 
586 	/* Convert back to make sure we can do it. */
587 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
588 
589 	/* XXX - Allow some tolerance here? */
590 	if (real_bps != bps)
591 		return (EINVAL);
592 
593 	cs->cs_preg[12] = tconst;
594 	cs->cs_preg[13] = tconst >> 8;
595 
596 	/* Caller will stuff the pending registers. */
597 	return (0);
598 }
599 
600 int
601 zs_set_modes(cs, cflag)
602 	struct zs_chanstate *cs;
603 	int cflag;	/* bits per second */
604 {
605 	int s;
606 
607 	/*
608 	 * Output hardware flow control on the chip is horrendous:
609 	 * if carrier detect drops, the receiver is disabled, and if
610 	 * CTS drops, the transmitter is stopped IN MID CHARACTER!
611 	 * Therefore, NEVER set the HFC bit, and instead use the
612 	 * status interrupt to detect CTS changes.
613 	 */
614 	s = splzs();
615 	cs->cs_rr0_pps = 0;
616 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
617 		cs->cs_rr0_dcd = 0;
618 		if ((cflag & MDMBUF) == 0)
619 			cs->cs_rr0_pps = ZSRR0_DCD;
620 	} else
621 		cs->cs_rr0_dcd = ZSRR0_DCD;
622 	if ((cflag & CRTSCTS) != 0) {
623 		cs->cs_wr5_dtr = ZSWR5_DTR;
624 		cs->cs_wr5_rts = ZSWR5_RTS;
625 		cs->cs_rr0_cts = ZSRR0_CTS;
626 #if 0 /* JLW */
627 	} else if ((cflag & CDTRCTS) != 0) {
628 		cs->cs_wr5_dtr = 0;
629 		cs->cs_wr5_rts = ZSWR5_DTR;
630 		cs->cs_rr0_cts = ZSRR0_CTS;
631 #endif
632 	} else if ((cflag & MDMBUF) != 0) {
633 		cs->cs_wr5_dtr = 0;
634 		cs->cs_wr5_rts = ZSWR5_DTR;
635 		cs->cs_rr0_cts = ZSRR0_DCD;
636 	} else {
637 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
638 		cs->cs_wr5_rts = 0;
639 		cs->cs_rr0_cts = 0;
640 	}
641 	splx(s);
642 
643 	/* Caller will stuff the pending registers. */
644 	return (0);
645 }
646 
647 
648 /*
649  * Read or write the chip with suitable delays.
650  */
651 
652 u_char
653 zs_read_reg(cs, reg)
654 	struct zs_chanstate *cs;
655 	u_char reg;
656 {
657 	u_char val;
658 
659 	*cs->cs_reg_csr = reg;
660 	ZS_DELAY();
661 	val = *cs->cs_reg_csr;
662 	ZS_DELAY();
663 	return (val);
664 }
665 
666 void
667 zs_write_reg(cs, reg, val)
668 	struct zs_chanstate *cs;
669 	u_char reg, val;
670 {
671 	*cs->cs_reg_csr = reg;
672 	ZS_DELAY();
673 	*cs->cs_reg_csr = val;
674 	ZS_DELAY();
675 }
676 
677 u_char
678 zs_read_csr(cs)
679 	struct zs_chanstate *cs;
680 {
681 	u_char val;
682 
683 	val = *cs->cs_reg_csr;
684 	ZS_DELAY();
685 	return (val);
686 }
687 
688 void  zs_write_csr(cs, val)
689 	struct zs_chanstate *cs;
690 	u_char val;
691 {
692 	*cs->cs_reg_csr = val;
693 	ZS_DELAY();
694 }
695 
696 u_char zs_read_data(cs)
697 	struct zs_chanstate *cs;
698 {
699 	u_char val;
700 
701 	val = *cs->cs_reg_data;
702 	ZS_DELAY();
703 	return (val);
704 }
705 
706 void  zs_write_data(cs, val)
707 	struct zs_chanstate *cs;
708 	u_char val;
709 {
710 	*cs->cs_reg_data = val;
711 	ZS_DELAY();
712 }
713 
714 /****************************************************************
715  * Console support functions (Sun specific!)
716  * Note: this code is allowed to know about the layout of
717  * the chip registers, and uses that to keep things simple.
718  * XXX - I think I like the mvme167 code better. -gwr
719  ****************************************************************/
720 
721 extern void Debugger(void);
722 
723 /*
724  * Handle user request to enter kernel debugger.
725  */
726 void
727 zs_abort(cs)
728 	struct zs_chanstate *cs;
729 {
730 	volatile struct zschan *zc = zs_conschan_get;
731 	int rr0;
732 
733 	/* Wait for end of break to avoid PROM abort. */
734 	/* XXX - Limit the wait? */
735 	do {
736 		rr0 = zc->zc_csr;
737 		ZS_DELAY();
738 	} while (rr0 & ZSRR0_BREAK);
739 
740 #if defined(KGDB)
741 	zskgdb(cs);
742 #elif defined(DDB)
743 	{
744 		extern int db_active;
745 
746 		if (!db_active)
747 			Debugger();
748 		else
749 			/* Debugger is probably hozed */
750 			callrom();
751 	}
752 #else
753 	printf("stopping on keyboard abort\n");
754 	callrom();
755 #endif
756 }
757 
758 
759 /*
760  * Polled input char.
761  */
762 int
763 zs_getc(arg)
764 	void *arg;
765 {
766 	volatile struct zschan *zc = arg;
767 	int s, c, rr0;
768 
769 	s = splhigh();
770 	/* Wait for a character to arrive. */
771 	do {
772 		rr0 = zc->zc_csr;
773 		ZS_DELAY();
774 	} while ((rr0 & ZSRR0_RX_READY) == 0);
775 
776 	c = zc->zc_data;
777 	ZS_DELAY();
778 	splx(s);
779 
780 	return (c);
781 }
782 
783 /*
784  * Polled output char.
785  */
786 void
787 zs_putc(arg, c)
788 	void *arg;
789 	int c;
790 {
791 	volatile struct zschan *zc = arg;
792 	int s, rr0;
793 
794 	s = splhigh();
795 
796 	/* Wait for transmitter to become ready. */
797 	do {
798 		rr0 = zc->zc_csr;
799 		ZS_DELAY();
800 	} while ((rr0 & ZSRR0_TX_READY) == 0);
801 
802 	/*
803 	 * Send the next character.
804 	 * Now you'd think that this could be followed by a ZS_DELAY()
805 	 * just like all the other chip accesses, but it turns out that
806 	 * the `transmit-ready' interrupt isn't de-asserted until
807 	 * some period of time after the register write completes
808 	 * (more than a couple instructions).  So to avoid stray
809 	 * interrupts we put in the 2us delay regardless of cpu model.
810 	 */
811 	zc->zc_data = c;
812 	delay(2);
813 
814 	splx(s);
815 }
816 
817 /*****************************************************************/
818 
819 
820 
821 
822 /*
823  * Polled console input putchar.
824  */
825 static int
826 zscngetc(dev)
827 	dev_t dev;
828 {
829 	return (zs_getc(zs_conschan_get));
830 }
831 
832 /*
833  * Polled console output putchar.
834  */
835 static void
836 zscnputc(dev, c)
837 	dev_t dev;
838 	int c;
839 {
840 	zs_putc(zs_conschan_put, c);
841 }
842 
843 int swallow_zsintrs;
844 
845 static void
846 zscnpollc(dev, on)
847 	dev_t dev;
848 	int on;
849 {
850 	/*
851 	 * Need to tell zs driver to acknowledge all interrupts or we get
852 	 * annoying spurious interrupt messages.  This is because mucking
853 	 * with spl() levels during polling does not prevent interrupts from
854 	 * being generated.
855 	 */
856 
857 	if (on) swallow_zsintrs++;
858 	else swallow_zsintrs--;
859 }
860 
861 int
862 zs_console_flags(promunit, node, channel)
863 	int promunit;
864 	int node;
865 	int channel;
866 {
867 	int cookie, flags = 0;
868 	u_int options;
869 	char buf[255];
870 
871 	/*
872 	 * We'll just to the OBP grovelling down here since that's
873 	 * the only type of firmware we support.
874 	 */
875 	options = OF_finddevice("/options");
876 
877 	/* Default to channel 0 if there are no explicit prom args */
878 	cookie = 0;
879 
880 	if (node == OF_instance_to_package(OF_stdin())) {
881 		if (OF_getprop(options, "input-device",
882 		    buf, sizeof(buf)) != -1) {
883 			if (strncmp("ttyb", buf, strlen("ttyb")) == 0)
884 				cookie = 1;
885 		}
886 
887 		if (channel == cookie)
888 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
889 	}
890 
891 	if (node == OF_instance_to_package(OF_stdout())) {
892 		if (OF_getprop(options, "output-device",
893 		    buf, sizeof(buf)) != -1) {
894 			if (strncmp("ttyb", buf, strlen("ttyb")) == 0)
895 				cookie = 1;
896 		}
897 
898 		if (channel == cookie)
899 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
900 	}
901 
902 	return (flags);
903 }
904 
905