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