Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.44
      1 /*	$NetBSD: zs.c,v 1.44 2002/12/10 13:44:52 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 #include "opt_kgdb.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/conf.h>
     53 #include <sys/device.h>
     54 #include <sys/file.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/kernel.h>
     57 #include <sys/proc.h>
     58 #include <sys/tty.h>
     59 #include <sys/time.h>
     60 #include <sys/syslog.h>
     61 
     62 #include <machine/autoconf.h>
     63 #include <machine/openfirm.h>
     64 #include <machine/cpu.h>
     65 #include <machine/eeprom.h>
     66 #include <machine/psl.h>
     67 #include <machine/z8530var.h>
     68 
     69 #include <dev/cons.h>
     70 #include <dev/ic/z8530reg.h>
     71 #include <dev/sun/kbd_ms_ttyvar.h>
     72 #include <ddb/db_output.h>
     73 
     74 #include <sparc64/dev/cons.h>
     75 
     76 #include "kbd.h"	/* NKBD */
     77 #include "ms.h"		/* NMS */
     78 #include "zs.h" 	/* NZS */
     79 
     80 /* Make life easier for the initialized arrays here. */
     81 #if NZS < 3
     82 #undef  NZS
     83 #define NZS 3
     84 #endif
     85 
     86 /*
     87  * Some warts needed by z8530tty.c -
     88  * The default parity REALLY needs to be the same as the PROM uses,
     89  * or you can not see messages done with printf during boot-up...
     90  */
     91 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     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 CFATTACH_DECL(zs, sizeof(struct zsc_softc),
    166     zs_match_mainbus, zs_attach_mainbus, NULL, NULL);
    167 
    168 CFATTACH_DECL(zs_mainbus, sizeof(struct zsc_softc),
    169     zs_match_mainbus, zs_attach_mainbus, NULL, NULL);
    170 
    171 extern struct cfdriver zs_cd;
    172 extern int stdinnode;
    173 extern int fbnode;
    174 
    175 /* Interrupt handlers. */
    176 int zscheckintr __P((void *));
    177 static int zshard __P((void *));
    178 static void zssoft __P((void *));
    179 
    180 static int zs_get_speed __P((struct zs_chanstate *));
    181 
    182 /* Console device support */
    183 static int zs_console_flags __P((int, int, int));
    184 
    185 /* Power management hooks */
    186 int  zs_enable __P((struct zs_chanstate *));
    187 void zs_disable __P((struct zs_chanstate *));
    188 
    189 /*
    190  * Is the zs chip present?
    191  */
    192 static int
    193 zs_match_mainbus(parent, cf, aux)
    194 	struct device *parent;
    195 	struct cfdata *cf;
    196 	void *aux;
    197 {
    198 	struct sbus_attach_args *sa = aux;
    199 
    200 	if (strcmp(cf->cf_name, sa->sa_name) != 0)
    201 		return (0);
    202 
    203 	return (1);
    204 }
    205 
    206 static void
    207 zs_attach_mainbus(parent, self, aux)
    208 	struct device *parent;
    209 	struct device *self;
    210 	void *aux;
    211 {
    212 	struct zsc_softc *zsc = (void *) self;
    213 	struct sbus_attach_args *sa = aux;
    214 	bus_space_handle_t bh;
    215 	int zs_unit = zsc->zsc_dev.dv_unit;
    216 
    217 	if (sa->sa_nintr == 0) {
    218 		printf(" no interrupt lines\n");
    219 		return;
    220 	}
    221 
    222 	/* Use the mapping setup by the Sun PROM if possible. */
    223 	if (zsaddr[zs_unit] == NULL) {
    224 		/* Only map registers once. */
    225 		if (sa->sa_npromvaddrs) {
    226 			/*
    227 			 * We're converting from a 32-bit pointer to a 64-bit
    228 			 * pointer.  Since the 32-bit entity is negative, but
    229 			 * the kernel is still mapped into the lower 4GB
    230 			 * range, this needs to be zero-extended.
    231 			 *
    232 			 * XXXXX If we map the kernel and devices into the
    233 			 * high 4GB range, this needs to be changed to
    234 			 * sign-extend the address.
    235 			 */
    236 			sparc_promaddr_to_handle(sa->sa_bustag,
    237 				sa->sa_promvaddrs[0], &bh);
    238 
    239 		} else {
    240 
    241 			if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    242 					 sa->sa_offset,
    243 					 sa->sa_size,
    244 					 BUS_SPACE_MAP_LINEAR,
    245 					 &bh) != 0) {
    246 				printf("%s @ sbus: cannot map registers\n",
    247 				       self->dv_xname);
    248 				return;
    249 			}
    250 		}
    251 		zsaddr[zs_unit] = (struct zsdevice *)
    252 			bus_space_vaddr(sa->sa_bustag, bh);
    253 	}
    254 	zsc->zsc_bustag = sa->sa_bustag;
    255 	zsc->zsc_dmatag = sa->sa_dmatag;
    256 	zsc->zsc_promunit = PROM_getpropint(sa->sa_node, "slave", -2);
    257 	zsc->zsc_node = sa->sa_node;
    258 	zs_attach(zsc, zsaddr[zs_unit], sa->sa_pri);
    259 }
    260 
    261 /*
    262  * Attach a found zs.
    263  *
    264  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    265  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    266  */
    267 static void
    268 zs_attach(zsc, zsd, pri)
    269 	struct zsc_softc *zsc;
    270 	struct zsdevice *zsd;
    271 	int pri;
    272 {
    273 	struct zsc_attach_args zsc_args;
    274 	struct zs_chanstate *cs;
    275 	int s, channel, softpri = PIL_TTY;
    276 
    277 	if (zsd == NULL) {
    278 		printf("configuration incomplete\n");
    279 		return;
    280 	}
    281 
    282 	printf(" softpri %d\n", softpri);
    283 
    284 	/*
    285 	 * Initialize software state for each channel.
    286 	 */
    287 	for (channel = 0; channel < 2; channel++) {
    288 		struct zschan *zc;
    289 		struct device *child;
    290 
    291 		zsc_args.channel = channel;
    292 		cs = &zsc->zsc_cs_store[channel];
    293 		zsc->zsc_cs[channel] = cs;
    294 
    295 		cs->cs_channel = channel;
    296 		cs->cs_private = NULL;
    297 		cs->cs_ops = &zsops_null;
    298 		cs->cs_brg_clk = PCLK / 16;
    299 
    300 		zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    301 
    302 		zsc_args.consdev = NULL;
    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 
    319 		/* Children need to set cn_dev, etc */
    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 (!(child =
    350 		      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 #if (NKBD > 0) || (NMS > 0)
    359 		/*
    360 		 * If this was a zstty it has a keyboard
    361 		 * property on it we need to attach the
    362 		 * sunkbd and sunms line disciplines.
    363 		 */
    364 		if (child
    365 		    && (!strcmp(child->dv_cfdata->cf_name, "zstty"))
    366 		    && (PROM_getproplen(zsc->zsc_node, "keyboard") == 0)) {
    367 			struct kbd_ms_tty_attach_args kma;
    368 			struct zstty_softc {
    369 				/* The following are the only fields we need here */
    370 				struct	device zst_dev;
    371 				struct  tty *zst_tty;
    372 				struct	zs_chanstate *zst_cs;
    373 			} *zst = (struct zstty_softc *)child;
    374 			struct tty *tp;
    375 
    376 			kma.kmta_tp = tp = zst->zst_tty;
    377 			kma.kmta_dev = tp->t_dev;
    378 			kma.kmta_consdev = zsc_args.consdev;
    379 
    380 			/* Attach 'em if we got 'em. */
    381 #if (NKBD > 0)
    382 			if (channel == 0) {
    383 				kma.kmta_name = "keyboard";
    384 				config_found(child, (void *)&kma, NULL);
    385 			}
    386 #endif
    387 #if (NMS > 0)
    388 			if (channel == 1) {
    389 				kma.kmta_name = "mouse";
    390 				config_found(child, (void *)&kma, NULL);
    391 			}
    392 #endif
    393 		}
    394 #endif
    395 	}
    396 
    397 	/*
    398 	 * Now safe to install interrupt handlers.  Note the arguments
    399 	 * to the interrupt handlers aren't used.  Note, we only do this
    400 	 * once since both SCCs interrupt at the same level and vector.
    401 	 */
    402 	bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, zshard, zsc);
    403 	if (!(zsc->zsc_softintr = softintr_establish(softpri, zssoft, zsc)))
    404 		panic("zsattach: could not establish soft interrupt");
    405 
    406 	evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
    407 	    zsc->zsc_dev.dv_xname, "intr");
    408 
    409 
    410 	/*
    411 	 * Set the master interrupt enable and interrupt vector.
    412 	 * (common to both channels, do it on A)
    413 	 */
    414 	cs = zsc->zsc_cs[0];
    415 	s = splhigh();
    416 	/* interrupt vector */
    417 	zs_write_reg(cs, 2, zs_init_reg[2]);
    418 	/* master interrupt control (enable) */
    419 	zs_write_reg(cs, 9, zs_init_reg[9]);
    420 	splx(s);
    421 
    422 }
    423 
    424 static int
    425 zs_print(aux, name)
    426 	void *aux;
    427 	const char *name;
    428 {
    429 	struct zsc_attach_args *args = aux;
    430 
    431 	if (name != NULL)
    432 		printf("%s: ", name);
    433 
    434 	if (args->channel != -1)
    435 		printf(" channel %d", args->channel);
    436 
    437 	return (UNCONF);
    438 }
    439 
    440 /* Deprecate this? */
    441 static volatile int zssoftpending;
    442 
    443 static int
    444 zshard(arg)
    445 	void *arg;
    446 {
    447 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    448 	int rr3, rval;
    449 
    450 	rval = 0;
    451 	while ((rr3 = zsc_intr_hard(zsc))) {
    452 		/* Count up the interrupts. */
    453 		rval |= rr3;
    454 		zsc->zsc_intrcnt.ev_count++;
    455 	}
    456 	if (((zsc->zsc_cs[0] && zsc->zsc_cs[0]->cs_softreq) ||
    457 	     (zsc->zsc_cs[1] && zsc->zsc_cs[1]->cs_softreq)) &&
    458 	    zsc->zsc_softintr) {
    459 		zssoftpending = PIL_TTY;
    460 		softintr_schedule(zsc->zsc_softintr);
    461 	}
    462 	return (rval);
    463 }
    464 
    465 int
    466 zscheckintr(arg)
    467 	void *arg;
    468 {
    469 	struct zsc_softc *zsc;
    470 	int unit, rval;
    471 
    472 	rval = 0;
    473 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    474 
    475 		zsc = zs_cd.cd_devs[unit];
    476 		if (zsc == NULL)
    477 			continue;
    478 		rval = (zshard((void *)zsc) || rval);
    479 	}
    480 	return (rval);
    481 }
    482 
    483 
    484 /*
    485  * We need this only for TTY_DEBUG purposes.
    486  */
    487 static void
    488 zssoft(arg)
    489 	void *arg;
    490 {
    491 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    492 	int s;
    493 
    494 	/* Make sure we call the tty layer at spltty. */
    495 	s = spltty();
    496 	zssoftpending = 0;
    497 	(void)zsc_intr_soft(zsc);
    498 #ifdef TTY_DEBUG
    499 	{
    500 		struct zstty_softc *zst0 = zsc->zsc_cs[0]->cs_private;
    501 		struct zstty_softc *zst1 = zsc->zsc_cs[1]->cs_private;
    502 		if (zst0->zst_overflows || zst1->zst_overflows ) {
    503 			struct trapframe *frame = (struct trapframe *)arg;
    504 
    505 			printf("zs silo overflow from %p\n",
    506 			       (long)frame->tf_pc);
    507 		}
    508 	}
    509 #endif
    510 	splx(s);
    511 }
    512 
    513 
    514 /*
    515  * Compute the current baud rate given a ZS channel.
    516  */
    517 static int
    518 zs_get_speed(cs)
    519 	struct zs_chanstate *cs;
    520 {
    521 	int tconst;
    522 
    523 	tconst = zs_read_reg(cs, 12);
    524 	tconst |= zs_read_reg(cs, 13) << 8;
    525 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    526 }
    527 
    528 /*
    529  * MD functions for setting the baud rate and control modes.
    530  */
    531 int
    532 zs_set_speed(cs, bps)
    533 	struct zs_chanstate *cs;
    534 	int bps;	/* bits per second */
    535 {
    536 	int tconst, real_bps;
    537 
    538 	if (bps == 0)
    539 		return (0);
    540 
    541 #ifdef	DIAGNOSTIC
    542 	if (cs->cs_brg_clk == 0)
    543 		panic("zs_set_speed");
    544 #endif
    545 
    546 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    547 	if (tconst < 0)
    548 		return (EINVAL);
    549 
    550 	/* Convert back to make sure we can do it. */
    551 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    552 
    553 	/* XXX - Allow some tolerance here? */
    554 	if (real_bps != bps)
    555 		return (EINVAL);
    556 
    557 	cs->cs_preg[12] = tconst;
    558 	cs->cs_preg[13] = tconst >> 8;
    559 
    560 	/* Caller will stuff the pending registers. */
    561 	return (0);
    562 }
    563 
    564 int
    565 zs_set_modes(cs, cflag)
    566 	struct zs_chanstate *cs;
    567 	int cflag;	/* bits per second */
    568 {
    569 	int s;
    570 
    571 	/*
    572 	 * Output hardware flow control on the chip is horrendous:
    573 	 * if carrier detect drops, the receiver is disabled, and if
    574 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    575 	 * Therefore, NEVER set the HFC bit, and instead use the
    576 	 * status interrupt to detect CTS changes.
    577 	 */
    578 	s = splzs();
    579 	cs->cs_rr0_pps = 0;
    580 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    581 		cs->cs_rr0_dcd = 0;
    582 		if ((cflag & MDMBUF) == 0)
    583 			cs->cs_rr0_pps = ZSRR0_DCD;
    584 	} else
    585 		cs->cs_rr0_dcd = ZSRR0_DCD;
    586 	if ((cflag & CRTSCTS) != 0) {
    587 		cs->cs_wr5_dtr = ZSWR5_DTR;
    588 		cs->cs_wr5_rts = ZSWR5_RTS;
    589 		cs->cs_rr0_cts = ZSRR0_CTS;
    590 	} else if ((cflag & CDTRCTS) != 0) {
    591 		cs->cs_wr5_dtr = 0;
    592 		cs->cs_wr5_rts = ZSWR5_DTR;
    593 		cs->cs_rr0_cts = ZSRR0_CTS;
    594 	} else if ((cflag & MDMBUF) != 0) {
    595 		cs->cs_wr5_dtr = 0;
    596 		cs->cs_wr5_rts = ZSWR5_DTR;
    597 		cs->cs_rr0_cts = ZSRR0_DCD;
    598 	} else {
    599 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    600 		cs->cs_wr5_rts = 0;
    601 		cs->cs_rr0_cts = 0;
    602 	}
    603 	splx(s);
    604 
    605 	/* Caller will stuff the pending registers. */
    606 	return (0);
    607 }
    608 
    609 
    610 /*
    611  * Read or write the chip with suitable delays.
    612  */
    613 
    614 u_char
    615 zs_read_reg(cs, reg)
    616 	struct zs_chanstate *cs;
    617 	u_char reg;
    618 {
    619 	u_char val;
    620 
    621 	*cs->cs_reg_csr = reg;
    622 	ZS_DELAY();
    623 	val = *cs->cs_reg_csr;
    624 	ZS_DELAY();
    625 	return (val);
    626 }
    627 
    628 void
    629 zs_write_reg(cs, reg, val)
    630 	struct zs_chanstate *cs;
    631 	u_char reg, val;
    632 {
    633 	*cs->cs_reg_csr = reg;
    634 	ZS_DELAY();
    635 	*cs->cs_reg_csr = val;
    636 	ZS_DELAY();
    637 }
    638 
    639 u_char
    640 zs_read_csr(cs)
    641 	struct zs_chanstate *cs;
    642 {
    643 	u_char val;
    644 
    645 	val = *cs->cs_reg_csr;
    646 	ZS_DELAY();
    647 	return (val);
    648 }
    649 
    650 void  zs_write_csr(cs, val)
    651 	struct zs_chanstate *cs;
    652 	u_char val;
    653 {
    654 	*cs->cs_reg_csr = val;
    655 	ZS_DELAY();
    656 }
    657 
    658 u_char zs_read_data(cs)
    659 	struct zs_chanstate *cs;
    660 {
    661 	u_char val;
    662 
    663 	val = *cs->cs_reg_data;
    664 	ZS_DELAY();
    665 	return (val);
    666 }
    667 
    668 void  zs_write_data(cs, val)
    669 	struct zs_chanstate *cs;
    670 	u_char val;
    671 {
    672 	*cs->cs_reg_data = val;
    673 	ZS_DELAY();
    674 }
    675 
    676 /****************************************************************
    677  * Console support functions (Sun specific!)
    678  * Note: this code is allowed to know about the layout of
    679  * the chip registers, and uses that to keep things simple.
    680  * XXX - I think I like the mvme167 code better. -gwr
    681  ****************************************************************/
    682 
    683 extern void Debugger __P((void));
    684 
    685 /*
    686  * Handle user request to enter kernel debugger.
    687  */
    688 void
    689 zs_abort(cs)
    690 	struct zs_chanstate *cs;
    691 {
    692 	volatile struct zschan *zc = zs_conschan_get;
    693 	int rr0;
    694 
    695 	/* Wait for end of break to avoid PROM abort. */
    696 	/* XXX - Limit the wait? */
    697 	do {
    698 		rr0 = zc->zc_csr;
    699 		ZS_DELAY();
    700 	} while (rr0 & ZSRR0_BREAK);
    701 
    702 #if defined(KGDB)
    703 	zskgdb(cs);
    704 #elif defined(DDB)
    705 	{
    706 		extern int db_active;
    707 
    708 		if (!db_active)
    709 			Debugger();
    710 		else
    711 			/* Debugger is probably hozed */
    712 			callrom();
    713 	}
    714 #else
    715 	printf("stopping on keyboard abort\n");
    716 	callrom();
    717 #endif
    718 }
    719 
    720 
    721 /*
    722  * Polled input char.
    723  */
    724 int
    725 zs_getc(arg)
    726 	void *arg;
    727 {
    728 	volatile struct zschan *zc = arg;
    729 	int s, c, rr0;
    730 
    731 	s = splhigh();
    732 	/* Wait for a character to arrive. */
    733 	do {
    734 		rr0 = zc->zc_csr;
    735 		ZS_DELAY();
    736 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    737 
    738 	c = zc->zc_data;
    739 	ZS_DELAY();
    740 	splx(s);
    741 
    742 	/*
    743 	 * This is used by the kd driver to read scan codes,
    744 	 * so don't translate '\r' ==> '\n' here...
    745 	 */
    746 	return (c);
    747 }
    748 
    749 /*
    750  * Polled output char.
    751  */
    752 void
    753 zs_putc(arg, c)
    754 	void *arg;
    755 	int c;
    756 {
    757 	volatile struct zschan *zc = arg;
    758 	int s, rr0;
    759 
    760 	s = splhigh();
    761 
    762 	/* Wait for transmitter to become ready. */
    763 	do {
    764 		rr0 = zc->zc_csr;
    765 		ZS_DELAY();
    766 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    767 
    768 	/*
    769 	 * Send the next character.
    770 	 * Now you'd think that this could be followed by a ZS_DELAY()
    771 	 * just like all the other chip accesses, but it turns out that
    772 	 * the `transmit-ready' interrupt isn't de-asserted until
    773 	 * some period of time after the register write completes
    774 	 * (more than a couple instructions).  So to avoid stray
    775 	 * interrupts we put in the 2us delay regardless of cpu model.
    776 	 */
    777 	zc->zc_data = c;
    778 	delay(2);
    779 
    780 	splx(s);
    781 }
    782 
    783 /*****************************************************************/
    784 
    785 
    786 
    787 
    788 /*
    789  * Polled console input putchar.
    790  */
    791 static int
    792 zscngetc(dev)
    793 	dev_t dev;
    794 {
    795 	return (zs_getc(zs_conschan_get));
    796 }
    797 
    798 /*
    799  * Polled console output putchar.
    800  */
    801 static void
    802 zscnputc(dev, c)
    803 	dev_t dev;
    804 	int c;
    805 {
    806 	zs_putc(zs_conschan_put, c);
    807 }
    808 
    809 int swallow_zsintrs;
    810 
    811 static void
    812 zscnpollc(dev, on)
    813 	dev_t dev;
    814 	int on;
    815 {
    816 	/*
    817 	 * Need to tell zs driver to acknowledge all interrupts or we get
    818 	 * annoying spurious interrupt messages.  This is because mucking
    819 	 * with spl() levels during polling does not prevent interrupts from
    820 	 * being generated.
    821 	 */
    822 
    823 	if (on) swallow_zsintrs++;
    824 	else swallow_zsintrs--;
    825 }
    826 
    827 int
    828 zs_console_flags(promunit, node, channel)
    829 	int promunit;
    830 	int node;
    831 	int channel;
    832 {
    833 	int cookie, flags = 0;
    834 	u_int options;
    835 	char buf[255];
    836 
    837 	/*
    838 	 * We'll just to the OBP grovelling down here since that's
    839 	 * the only type of firmware we support.
    840 	 */
    841 	options = OF_finddevice("/options");
    842 
    843 	/* Default to channel 0 if there are no explicit prom args */
    844 	cookie = 0;
    845 	if (node == OF_instance_to_package(OF_stdin())) {
    846 		if (OF_getprop(options, "input-device", buf, sizeof(buf)) != -1) {
    847 
    848 			if (!strcmp("ttyb", buf))
    849 				cookie = 1;
    850 		}
    851 
    852 		if (channel == cookie)
    853 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
    854 	}
    855 
    856 	if (node == OF_instance_to_package(OF_stdout())) {
    857 		if (OF_getprop(options, "output-device", buf, sizeof(buf)) != -1) {
    858 
    859 			if (!strcmp("ttyb", buf))
    860 				cookie = 1;
    861 		}
    862 
    863 		if (channel == cookie)
    864 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
    865 	}
    866 
    867 	return (flags);
    868 }
    869 
    870