Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.25
      1 /*	$NetBSD: zs.c,v 1.25 2000/07/10 01:11:14 eeh 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 int zscheckintr __P((void *));
    179 static int zshard __P((void *));
    180 static int zssoft __P((void *));
    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, softpri = PIL_TTY;
    278 
    279 	if (zsd == NULL) {
    280 		printf("configuration incomplete\n");
    281 		return;
    282 	}
    283 
    284 	printf(" softpri %d\n", softpri);
    285 
    286 	/*
    287 	 * Initialize software state for each channel.
    288 	 */
    289 	for (channel = 0; channel < 2; channel++) {
    290 		struct zschan *zc;
    291 
    292 		zsc_args.channel = channel;
    293 		cs = &zsc->zsc_cs_store[channel];
    294 		zsc->zsc_cs[channel] = cs;
    295 
    296 		cs->cs_channel = channel;
    297 		cs->cs_private = NULL;
    298 		cs->cs_ops = &zsops_null;
    299 		cs->cs_brg_clk = PCLK / 16;
    300 
    301 		zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    302 
    303 		zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit,
    304 						    zsc->zsc_node,
    305 						    channel);
    306 
    307 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
    308 			zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
    309 			zsc_args.consdev = &zs_consdev;
    310 		}
    311 
    312 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
    313 			zs_conschan_get = zc;
    314 		}
    315 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
    316 			zs_conschan_put = zc;
    317 		}
    318 		/* Childs need to set cn_dev, etc */
    319 
    320 		cs->cs_reg_csr  = &zc->zc_csr;
    321 		cs->cs_reg_data = &zc->zc_data;
    322 
    323 		bcopy(zs_init_reg, cs->cs_creg, 16);
    324 		bcopy(zs_init_reg, cs->cs_preg, 16);
    325 
    326 		/* XXX: Consult PROM properties for this?! */
    327 		cs->cs_defspeed = zs_get_speed(cs);
    328 		cs->cs_defcflag = zs_def_cflag;
    329 
    330 		/* Make these correspond to cs_defcflag (-crtscts) */
    331 		cs->cs_rr0_dcd = ZSRR0_DCD;
    332 		cs->cs_rr0_cts = 0;
    333 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    334 		cs->cs_wr5_rts = 0;
    335 
    336 		/*
    337 		 * Clear the master interrupt enable.
    338 		 * The INTENA is common to both channels,
    339 		 * so just do it on the A channel.
    340 		 */
    341 		if (channel == 0) {
    342 			zs_write_reg(cs, 9, 0);
    343 		}
    344 
    345 		/*
    346 		 * Look for a child driver for this channel.
    347 		 * The child attach will setup the hardware.
    348 		 */
    349 		if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
    350 			/* No sub-driver.  Just reset it. */
    351 			u_char reset = (channel == 0) ?
    352 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    353 			s = splzs();
    354 			zs_write_reg(cs,  9, reset);
    355 			splx(s);
    356 		}
    357 	}
    358 
    359 	/*
    360 	 * Now safe to install interrupt handlers.  Note the arguments
    361 	 * to the interrupt handlers aren't used.  Note, we only do this
    362 	 * once since both SCCs interrupt at the same level and vector.
    363 	 */
    364 	bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, 0, zshard, zsc);
    365 	if (!(zsc->zsc_softintr = softintr_establish(softpri, zssoft, zsc)))
    366 		panic("zsattach: could not establish soft interrupt\n");
    367 
    368 	evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
    369 	    zsc->zsc_dev.dv_xname, "intr");
    370 
    371 
    372 	/*
    373 	 * Set the master interrupt enable and interrupt vector.
    374 	 * (common to both channels, do it on A)
    375 	 */
    376 	cs = zsc->zsc_cs[0];
    377 	s = splhigh();
    378 	/* interrupt vector */
    379 	zs_write_reg(cs, 2, zs_init_reg[2]);
    380 	/* master interrupt control (enable) */
    381 	zs_write_reg(cs, 9, zs_init_reg[9]);
    382 	splx(s);
    383 
    384 #if 0
    385 	/*
    386 	 * XXX: L1A hack - We would like to be able to break into
    387 	 * the debugger during the rest of autoconfiguration, so
    388 	 * lower interrupts just enough to let zs interrupts in.
    389 	 * This is done after both zs devices are attached.
    390 	 */
    391 	if (zsc->zsc_promunit == 1) {
    392 		printf("zs1: enabling zs interrupts\n");
    393 		(void)splfd(); /* XXX: splzs - 1 */
    394 	}
    395 #endif
    396 }
    397 
    398 static int
    399 zs_print(aux, name)
    400 	void *aux;
    401 	const char *name;
    402 {
    403 	struct zsc_attach_args *args = aux;
    404 
    405 	if (name != NULL)
    406 		printf("%s: ", name);
    407 
    408 	if (args->channel != -1)
    409 		printf(" channel %d", args->channel);
    410 
    411 	return (UNCONF);
    412 }
    413 
    414 /* Deprecate this? */
    415 static volatile int zssoftpending;
    416 
    417 static int
    418 zshard(arg)
    419 	void *arg;
    420 {
    421 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    422 	int rr3, rval;
    423 
    424 	rval = 0;
    425 	while ((rr3 = zsc_intr_hard(zsc))) {
    426 		/* Count up the interrupts. */
    427 		rval |= rr3;
    428 		zsc->zsc_intrcnt.ev_count++;
    429 	}
    430 	if (((zsc->zsc_cs[0] && zsc->zsc_cs[0]->cs_softreq) ||
    431 	     (zsc->zsc_cs[1] && zsc->zsc_cs[1]->cs_softreq)) &&
    432 	    zsc->zsc_softintr) {
    433 		zssoftpending = PIL_TTY;
    434 		softintr_schedule(zsc->zsc_softintr);
    435 	}
    436 	return (rval);
    437 }
    438 
    439 int
    440 zscheckintr(arg)
    441 	void *arg;
    442 {
    443 	struct zsc_softc *zsc;
    444 	int unit, rval;
    445 
    446 	rval = 0;
    447 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    448 
    449 		zsc = zs_cd.cd_devs[unit];
    450 		if (zsc == NULL)
    451 			continue;
    452 		rval = (zshard((void *)zsc) || rval);
    453 	}
    454 	return (rval);
    455 }
    456 
    457 
    458 /*
    459  * We need this only for TTY_DEBUG purposes.
    460  */
    461 static int
    462 zssoft(arg)
    463 	void *arg;
    464 {
    465 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    466 	int s;
    467 
    468 	/* Make sure we call the tty layer at spltty. */
    469 	s = spltty();
    470 	zssoftpending = 0;
    471 	(void)zsc_intr_soft(zsc);
    472 #ifdef TTY_DEBUG
    473 	{
    474 		struct zstty_softc *zst0 = zsc->zsc_cs[0]->cs_private;
    475 		struct zstty_softc *zst1 = zsc->zsc_cs[1]->cs_private;
    476 		if (zst0->zst_overflows || zst1->zst_overflows ) {
    477 			struct trapframe *frame = (struct trapframe *)arg;
    478 
    479 			printf("zs silo overflow from %p\n",
    480 			       (long)frame->tf_pc);
    481 		}
    482 	}
    483 #endif
    484 	splx(s);
    485 	return (1);
    486 }
    487 
    488 
    489 /*
    490  * Compute the current baud rate given a ZS channel.
    491  */
    492 static int
    493 zs_get_speed(cs)
    494 	struct zs_chanstate *cs;
    495 {
    496 	int tconst;
    497 
    498 	tconst = zs_read_reg(cs, 12);
    499 	tconst |= zs_read_reg(cs, 13) << 8;
    500 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    501 }
    502 
    503 /*
    504  * MD functions for setting the baud rate and control modes.
    505  */
    506 int
    507 zs_set_speed(cs, bps)
    508 	struct zs_chanstate *cs;
    509 	int bps;	/* bits per second */
    510 {
    511 	int tconst, real_bps;
    512 
    513 	if (bps == 0)
    514 		return (0);
    515 
    516 #ifdef	DIAGNOSTIC
    517 	if (cs->cs_brg_clk == 0)
    518 		panic("zs_set_speed");
    519 #endif
    520 
    521 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    522 	if (tconst < 0)
    523 		return (EINVAL);
    524 
    525 	/* Convert back to make sure we can do it. */
    526 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    527 
    528 	/* XXX - Allow some tolerance here? */
    529 	if (real_bps != bps)
    530 		return (EINVAL);
    531 
    532 	cs->cs_preg[12] = tconst;
    533 	cs->cs_preg[13] = tconst >> 8;
    534 
    535 	/* Caller will stuff the pending registers. */
    536 	return (0);
    537 }
    538 
    539 int
    540 zs_set_modes(cs, cflag)
    541 	struct zs_chanstate *cs;
    542 	int cflag;	/* bits per second */
    543 {
    544 	int s;
    545 
    546 	/*
    547 	 * Output hardware flow control on the chip is horrendous:
    548 	 * if carrier detect drops, the receiver is disabled, and if
    549 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    550 	 * Therefore, NEVER set the HFC bit, and instead use the
    551 	 * status interrupt to detect CTS changes.
    552 	 */
    553 	s = splzs();
    554 	cs->cs_rr0_pps = 0;
    555 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    556 		cs->cs_rr0_dcd = 0;
    557 		if ((cflag & MDMBUF) == 0)
    558 			cs->cs_rr0_pps = ZSRR0_DCD;
    559 	} else
    560 		cs->cs_rr0_dcd = ZSRR0_DCD;
    561 	if ((cflag & CRTSCTS) != 0) {
    562 		cs->cs_wr5_dtr = ZSWR5_DTR;
    563 		cs->cs_wr5_rts = ZSWR5_RTS;
    564 		cs->cs_rr0_cts = ZSRR0_CTS;
    565 	} else if ((cflag & CDTRCTS) != 0) {
    566 		cs->cs_wr5_dtr = 0;
    567 		cs->cs_wr5_rts = ZSWR5_DTR;
    568 		cs->cs_rr0_cts = ZSRR0_CTS;
    569 	} else if ((cflag & MDMBUF) != 0) {
    570 		cs->cs_wr5_dtr = 0;
    571 		cs->cs_wr5_rts = ZSWR5_DTR;
    572 		cs->cs_rr0_cts = ZSRR0_DCD;
    573 	} else {
    574 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    575 		cs->cs_wr5_rts = 0;
    576 		cs->cs_rr0_cts = 0;
    577 	}
    578 	splx(s);
    579 
    580 	/* Caller will stuff the pending registers. */
    581 	return (0);
    582 }
    583 
    584 
    585 /*
    586  * Read or write the chip with suitable delays.
    587  */
    588 
    589 u_char
    590 zs_read_reg(cs, reg)
    591 	struct zs_chanstate *cs;
    592 	u_char reg;
    593 {
    594 	u_char val;
    595 
    596 	*cs->cs_reg_csr = reg;
    597 	ZS_DELAY();
    598 	val = *cs->cs_reg_csr;
    599 	ZS_DELAY();
    600 	return (val);
    601 }
    602 
    603 void
    604 zs_write_reg(cs, reg, val)
    605 	struct zs_chanstate *cs;
    606 	u_char reg, val;
    607 {
    608 	*cs->cs_reg_csr = reg;
    609 	ZS_DELAY();
    610 	*cs->cs_reg_csr = val;
    611 	ZS_DELAY();
    612 }
    613 
    614 u_char
    615 zs_read_csr(cs)
    616 	struct zs_chanstate *cs;
    617 {
    618 	u_char val;
    619 
    620 	val = *cs->cs_reg_csr;
    621 	ZS_DELAY();
    622 	return (val);
    623 }
    624 
    625 void  zs_write_csr(cs, val)
    626 	struct zs_chanstate *cs;
    627 	u_char val;
    628 {
    629 	*cs->cs_reg_csr = val;
    630 	ZS_DELAY();
    631 }
    632 
    633 u_char zs_read_data(cs)
    634 	struct zs_chanstate *cs;
    635 {
    636 	u_char val;
    637 
    638 	val = *cs->cs_reg_data;
    639 	ZS_DELAY();
    640 	return (val);
    641 }
    642 
    643 void  zs_write_data(cs, val)
    644 	struct zs_chanstate *cs;
    645 	u_char val;
    646 {
    647 	*cs->cs_reg_data = val;
    648 	ZS_DELAY();
    649 }
    650 
    651 /****************************************************************
    652  * Console support functions (Sun specific!)
    653  * Note: this code is allowed to know about the layout of
    654  * the chip registers, and uses that to keep things simple.
    655  * XXX - I think I like the mvme167 code better. -gwr
    656  ****************************************************************/
    657 
    658 extern void Debugger __P((void));
    659 
    660 /*
    661  * Handle user request to enter kernel debugger.
    662  */
    663 void
    664 zs_abort(cs)
    665 	struct zs_chanstate *cs;
    666 {
    667 	volatile struct zschan *zc = zs_conschan_get;
    668 	int rr0;
    669 
    670 	/* Wait for end of break to avoid PROM abort. */
    671 	/* XXX - Limit the wait? */
    672 	do {
    673 		rr0 = zc->zc_csr;
    674 		ZS_DELAY();
    675 	} while (rr0 & ZSRR0_BREAK);
    676 
    677 #if defined(KGDB)
    678 	zskgdb(cs);
    679 #elif defined(DDB)
    680 	{
    681 		extern int db_active;
    682 
    683 		if (!db_active)
    684 			Debugger();
    685 		else
    686 			/* Debugger is probably hozed */
    687 			callrom();
    688 	}
    689 #else
    690 	printf("stopping on keyboard abort\n");
    691 	callrom();
    692 #endif
    693 }
    694 
    695 
    696 /*
    697  * Polled input char.
    698  */
    699 int
    700 zs_getc(arg)
    701 	void *arg;
    702 {
    703 	volatile struct zschan *zc = arg;
    704 	int s, c, rr0;
    705 
    706 	s = splhigh();
    707 	/* Wait for a character to arrive. */
    708 	do {
    709 		rr0 = zc->zc_csr;
    710 		ZS_DELAY();
    711 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    712 
    713 	c = zc->zc_data;
    714 	ZS_DELAY();
    715 	splx(s);
    716 
    717 	/*
    718 	 * This is used by the kd driver to read scan codes,
    719 	 * so don't translate '\r' ==> '\n' here...
    720 	 */
    721 	return (c);
    722 }
    723 
    724 /*
    725  * Polled output char.
    726  */
    727 void
    728 zs_putc(arg, c)
    729 	void *arg;
    730 	int c;
    731 {
    732 	volatile struct zschan *zc = arg;
    733 	int s, rr0;
    734 
    735 	s = splhigh();
    736 
    737 	/* Wait for transmitter to become ready. */
    738 	do {
    739 		rr0 = zc->zc_csr;
    740 		ZS_DELAY();
    741 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    742 
    743 	/*
    744 	 * Send the next character.
    745 	 * Now you'd think that this could be followed by a ZS_DELAY()
    746 	 * just like all the other chip accesses, but it turns out that
    747 	 * the `transmit-ready' interrupt isn't de-asserted until
    748 	 * some period of time after the register write completes
    749 	 * (more than a couple instructions).  So to avoid stray
    750 	 * interrupts we put in the 2us delay regardless of cpu model.
    751 	 */
    752 	zc->zc_data = c;
    753 	delay(2);
    754 
    755 	splx(s);
    756 }
    757 
    758 /*****************************************************************/
    759 
    760 
    761 
    762 
    763 /*
    764  * Polled console input putchar.
    765  */
    766 static int
    767 zscngetc(dev)
    768 	dev_t dev;
    769 {
    770 	return (zs_getc(zs_conschan_get));
    771 }
    772 
    773 /*
    774  * Polled console output putchar.
    775  */
    776 static void
    777 zscnputc(dev, c)
    778 	dev_t dev;
    779 	int c;
    780 {
    781 	zs_putc(zs_conschan_put, c);
    782 }
    783 
    784 int swallow_zsintrs;
    785 
    786 static void
    787 zscnpollc(dev, on)
    788 	dev_t dev;
    789 	int on;
    790 {
    791 	/*
    792 	 * Need to tell zs driver to acknowledge all interrupts or we get
    793 	 * annoying spurious interrupt messages.  This is because mucking
    794 	 * with spl() levels during polling does not prevent interrupts from
    795 	 * being generated.
    796 	 */
    797 
    798 	if (on) swallow_zsintrs++;
    799 	else swallow_zsintrs--;
    800 }
    801 
    802 int
    803 zs_console_flags(promunit, node, channel)
    804 	int promunit;
    805 	int node;
    806 	int channel;
    807 {
    808 	int cookie, flags = 0;
    809 	u_int chosen;
    810 	char buf[255];
    811 
    812 	/*
    813 	 * We'll just to the OBP grovelling down here since that's
    814 	 * the only type of firmware we support.
    815 	 */
    816 	chosen = OF_finddevice("/chosen");
    817 
    818 	/* Default to channel 0 if there are no explicit prom args */
    819 	cookie = 0;
    820 	if (node == OF_instance_to_package(OF_stdin())) {
    821 		if (OF_getprop(chosen, "input-device", buf, sizeof(buf)) != -1) {
    822 
    823 			if (!strcmp("ttyb", buf))
    824 				cookie = 1;
    825 		}
    826 
    827 		if (channel == cookie)
    828 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
    829 	}
    830 
    831 	if (node == OF_instance_to_package(OF_stdout())) {
    832 		if (OF_getprop(chosen, "output-device", buf, sizeof(buf)) != -1) {
    833 
    834 			if (!strcmp("ttyb", buf))
    835 				cookie = 1;
    836 		}
    837 
    838 		if (channel == cookie)
    839 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
    840 	}
    841 
    842 	return (flags);
    843 }
    844 
    845