Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.75
      1 /*	$NetBSD: zs.c,v 1.75 2000/03/14 21:20:51 jdc 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/bsd_openprom.h>
     62 #include <machine/autoconf.h>
     63 #include <machine/conf.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 
     72 #include <sparc/sparc/vaddrs.h>
     73 #include <sparc/sparc/auxreg.h>
     74 #include <sparc/sparc/auxiotwo.h>
     75 #include <sparc/dev/cons.h>
     76 
     77 #include "kbd.h"	/* NKBD */
     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 int zs_major = 12;
     93 
     94 /*
     95  * The Sun provides a 4.9152 MHz clock to the ZS chips.
     96  */
     97 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     98 
     99 /*
    100  * Select software interrupt bit based on TTY ipl.
    101  */
    102 #if PIL_TTY == 1
    103 # define IE_ZSSOFT IE_L1
    104 #elif PIL_TTY == 4
    105 # define IE_ZSSOFT IE_L4
    106 #elif PIL_TTY == 6
    107 # define IE_ZSSOFT IE_L6
    108 #else
    109 # error "no suitable software interrupt bit"
    110 #endif
    111 
    112 #define	ZS_DELAY()		(CPU_ISSUN4C ? (0) : delay(2))
    113 
    114 /* The layout of this is hardware-dependent (padding, order). */
    115 struct zschan {
    116 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    117 	u_char		zc_xxx0;
    118 	volatile u_char	zc_data;	/* data */
    119 	u_char		zc_xxx1;
    120 };
    121 struct zsdevice {
    122 	/* Yes, they are backwards. */
    123 	struct	zschan zs_chan_b;
    124 	struct	zschan zs_chan_a;
    125 };
    126 
    127 /* ZS channel used as the console device (if any) */
    128 void *zs_conschan;
    129 
    130 /* Default speed for each channel */
    131 static int zs_defspeed[NZS][2] = {
    132 	{ 9600, 	/* ttya */
    133 	  9600 },	/* ttyb */
    134 	{ 1200, 	/* keyboard */
    135 	  1200 },	/* mouse */
    136 	{ 9600, 	/* ttyc */
    137 	  9600 },	/* ttyd */
    138 };
    139 
    140 static u_char zs_init_reg[16] = {
    141 	0,	/* 0: CMD (reset, etc.) */
    142 	0,	/* 1: No interrupts yet. */
    143 	0,	/* 2: IVECT */
    144 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    145 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    146 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    147 	0,	/* 6: TXSYNC/SYNCLO */
    148 	0,	/* 7: RXSYNC/SYNCHI */
    149 	0,	/* 8: alias for data port */
    150 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
    151 	0,	/*10: Misc. TX/RX control bits */
    152 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    153 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    154 	0,			/*13: BAUDHI (default=9600) */
    155 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    156 	ZSWR15_BREAK_IE,
    157 };
    158 
    159 
    160 /****************************************************************
    161  * Autoconfig
    162  ****************************************************************/
    163 
    164 /* Definition of the driver for autoconfig. */
    165 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    166 static int  zs_match_obio __P((struct device *, struct cfdata *, void *));
    167 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    168 static void zs_attach_obio __P((struct device *, struct device *, void *));
    169 
    170 static void zs_attach __P((struct zsc_softc *, struct zsdevice *, int));
    171 static int  zs_print __P((void *, const char *name));
    172 
    173 struct cfattach zs_mainbus_ca = {
    174 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    175 };
    176 
    177 struct cfattach zs_obio_ca = {
    178 	sizeof(struct zsc_softc), zs_match_obio, zs_attach_obio
    179 };
    180 
    181 extern struct cfdriver zs_cd;
    182 
    183 /* Interrupt handlers. */
    184 static int zshard __P((void *));
    185 static int zssoft __P((void *));
    186 static struct intrhand levelsoft = { zssoft };
    187 
    188 static int zs_get_speed __P((struct zs_chanstate *));
    189 
    190 /* Power management hooks */
    191 int  zs_enable __P((struct zs_chanstate *));
    192 void zs_disable __P((struct zs_chanstate *));
    193 
    194 
    195 /*
    196  * Is the zs chip present?
    197  */
    198 static int
    199 zs_match_mainbus(parent, cf, aux)
    200 	struct device *parent;
    201 	struct cfdata *cf;
    202 	void *aux;
    203 {
    204 	struct mainbus_attach_args *ma = aux;
    205 
    206 	if (strcmp(cf->cf_driver->cd_name, ma->ma_name) != 0)
    207 		return (0);
    208 
    209 	return (1);
    210 }
    211 
    212 static int
    213 zs_match_obio(parent, cf, aux)
    214 	struct device *parent;
    215 	struct cfdata *cf;
    216 	void *aux;
    217 {
    218 	union obio_attach_args *uoba = aux;
    219 	struct obio4_attach_args *oba;
    220 
    221 	if (uoba->uoba_isobio4 == 0) {
    222 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    223 
    224 		if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
    225 			return (0);
    226 
    227 		return (1);
    228 	}
    229 
    230 	oba = &uoba->uoba_oba4;
    231 	return (bus_space_probe(oba->oba_bustag, 0, oba->oba_paddr,
    232 			        1, 0, 0, NULL, NULL));
    233 }
    234 
    235 static void
    236 zs_attach_mainbus(parent, self, aux)
    237 	struct device *parent;
    238 	struct device *self;
    239 	void *aux;
    240 {
    241 	struct zsc_softc *zsc = (void *) self;
    242 	struct mainbus_attach_args *ma = aux;
    243 
    244 	zsc->zsc_bustag = ma->ma_bustag;
    245 	zsc->zsc_dmatag = ma->ma_dmatag;
    246 
    247 	/*
    248 	 * For machines with zs on mainbus (all sun4c models), we expect
    249 	 * the device registers to be mapped by the PROM.
    250 	 */
    251 	zs_attach(zsc, ma->ma_promvaddr, ma->ma_pri);
    252 }
    253 
    254 static void
    255 zs_attach_obio(parent, self, aux)
    256 	struct device *parent;
    257 	struct device *self;
    258 	void *aux;
    259 {
    260 	struct zsc_softc *zsc = (void *) self;
    261 	union obio_attach_args *uoba = aux;
    262 
    263 	if (uoba->uoba_isobio4 == 0) {
    264 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    265 		void *va;
    266 		struct zs_chanstate *cs;
    267 		int channel;
    268 
    269 		if (sa->sa_nintr == 0) {
    270 			printf(" no interrupt lines\n");
    271 			return;
    272 		}
    273 
    274 		/*
    275 		 * Some sun4m models (Javastations) may not map the zs device.
    276 		 */
    277 		if (sa->sa_npromvaddrs > 0)
    278 			va = (void *)sa->sa_promvaddr;
    279 		else {
    280 			bus_space_handle_t bh;
    281 
    282 			if (sbus_bus_map(sa->sa_bustag,
    283 					  sa->sa_slot,
    284 					  sa->sa_offset,
    285 					  sa->sa_size,
    286 					  BUS_SPACE_MAP_LINEAR,
    287 					  0, &bh) != 0) {
    288 				printf(" cannot map zs registers\n");
    289 				return;
    290 			}
    291 			va = (void *)bh;
    292 		}
    293 
    294 		/*
    295 		 * Check if power state can be set, e.g. Tadpole 3GX
    296 		 */
    297 		if (getpropint(sa->sa_node, "pwr-on-auxio2", 0))
    298 		{
    299 			printf (" powered via auxio2");
    300 			for (channel = 0; channel < 2; channel++) {
    301 				cs = &zsc->zsc_cs_store[channel];
    302 				cs->enable = zs_enable;
    303 				cs->disable = zs_disable;
    304 			}
    305 		}
    306 
    307 		zsc->zsc_bustag = sa->sa_bustag;
    308 		zsc->zsc_dmatag = sa->sa_dmatag;
    309 		zs_attach(zsc, va, sa->sa_pri);
    310 	} else {
    311 		struct obio4_attach_args *oba = &uoba->uoba_oba4;
    312 		bus_space_handle_t bh;
    313 
    314 		/*
    315 		 * As for zs on mainbus, we require a PROM mapping.
    316 		 */
    317 		if (bus_space_map(oba->oba_bustag,
    318 				  oba->oba_paddr,
    319 				  sizeof(struct zsdevice),
    320 				  BUS_SPACE_MAP_LINEAR | OBIO_BUS_MAP_USE_ROM,
    321 				  &bh) != 0) {
    322 			printf(" cannot map zs registers\n");
    323 			return;
    324 		}
    325 		zsc->zsc_bustag = oba->oba_bustag;
    326 		zsc->zsc_dmatag = oba->oba_dmatag;
    327 		zs_attach(zsc, (void *)bh, oba->oba_pri);
    328 	}
    329 }
    330 /*
    331  * Attach a found zs.
    332  *
    333  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    334  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    335  */
    336 static void
    337 zs_attach(zsc, zsd, pri)
    338 	struct zsc_softc *zsc;
    339 	struct zsdevice *zsd;
    340 	int pri;
    341 {
    342 	struct zsc_attach_args zsc_args;
    343 	struct zs_chanstate *cs;
    344 	int s, zs_unit, channel;
    345 	static int didintr, prevpri;
    346 
    347 	if (zsd == NULL) {
    348 		printf("configuration incomplete\n");
    349 		return;
    350 	}
    351 
    352 	printf(" softpri %d\n", PIL_TTY);
    353 
    354 	/*
    355 	 * Initialize software state for each channel.
    356 	 */
    357 	zs_unit = zsc->zsc_dev.dv_unit;
    358 	for (channel = 0; channel < 2; channel++) {
    359 		volatile struct zschan *zc;
    360 
    361 		zsc_args.channel = channel;
    362 		zsc_args.hwflags = 0;
    363 		cs = &zsc->zsc_cs_store[channel];
    364 		zsc->zsc_cs[channel] = cs;
    365 
    366 		cs->cs_channel = channel;
    367 		cs->cs_private = NULL;
    368 		cs->cs_ops = &zsops_null;
    369 		cs->cs_brg_clk = PCLK / 16;
    370 
    371 		zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    372 		if (zc == zs_conschan)
    373 			zsc_args.hwflags |= ZS_HWFLAG_CONSOLE;
    374 
    375 		cs->cs_reg_csr  = &zc->zc_csr;
    376 		cs->cs_reg_data = &zc->zc_data;
    377 
    378 		bcopy(zs_init_reg, cs->cs_creg, 16);
    379 		bcopy(zs_init_reg, cs->cs_preg, 16);
    380 
    381 		/* XXX: Get these from the PROM properties! */
    382 		/* XXX: See the mvme167 code.  Better. */
    383 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    384 			cs->cs_defspeed = zs_get_speed(cs);
    385 		else
    386 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
    387 		cs->cs_defcflag = zs_def_cflag;
    388 
    389 		/* Make these correspond to cs_defcflag (-crtscts) */
    390 		cs->cs_rr0_dcd = ZSRR0_DCD;
    391 		cs->cs_rr0_cts = 0;
    392 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    393 		cs->cs_wr5_rts = 0;
    394 
    395 		/*
    396 		 * Clear the master interrupt enable.
    397 		 * The INTENA is common to both channels,
    398 		 * so just do it on the A channel.
    399 		 */
    400 		if (channel == 0) {
    401 			zs_write_reg(cs, 9, 0);
    402 		}
    403 
    404 		/*
    405 		 * Look for a child driver for this channel.
    406 		 * The child attach will setup the hardware.
    407 		 */
    408 		if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
    409 			/* No sub-driver.  Just reset it. */
    410 			u_char reset = (channel == 0) ?
    411 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    412 			s = splzs();
    413 			zs_write_reg(cs,  9, reset);
    414 			splx(s);
    415 		}
    416 	}
    417 
    418 	/*
    419 	 * Now safe to install interrupt handlers.  Note the arguments
    420 	 * to the interrupt handlers aren't used.  Note, we only do this
    421 	 * once since both SCCs interrupt at the same level and vector.
    422 	 */
    423 	if (!didintr) {
    424 		didintr = 1;
    425 		prevpri = pri;
    426 		bus_intr_establish(zsc->zsc_bustag, pri, 0, zshard, NULL);
    427 		intr_establish(PIL_TTY, &levelsoft);
    428 	} else if (pri != prevpri)
    429 		panic("broken zs interrupt scheme");
    430 
    431 	evcnt_attach(&zsc->zsc_dev, "intr", &zsc->zsc_intrcnt);
    432 
    433 	/*
    434 	 * Set the master interrupt enable and interrupt vector.
    435 	 * (common to both channels, do it on A)
    436 	 */
    437 	cs = zsc->zsc_cs[0];
    438 	s = splhigh();
    439 	/* interrupt vector */
    440 	zs_write_reg(cs, 2, zs_init_reg[2]);
    441 	/* master interrupt control (enable) */
    442 	zs_write_reg(cs, 9, zs_init_reg[9]);
    443 	splx(s);
    444 
    445 #if 0
    446 	/*
    447 	 * XXX: L1A hack - We would like to be able to break into
    448 	 * the debugger during the rest of autoconfiguration, so
    449 	 * lower interrupts just enough to let zs interrupts in.
    450 	 * This is done after both zs devices are attached.
    451 	 */
    452 	if (zs_unit == 1) {
    453 		printf("zs1: enabling zs interrupts\n");
    454 		(void)splfd(); /* XXX: splzs - 1 */
    455 	}
    456 #endif
    457 }
    458 
    459 static int
    460 zs_print(aux, name)
    461 	void *aux;
    462 	const char *name;
    463 {
    464 	struct zsc_attach_args *args = aux;
    465 
    466 	if (name != NULL)
    467 		printf("%s: ", name);
    468 
    469 	if (args->channel != -1)
    470 		printf(" channel %d", args->channel);
    471 
    472 	return (UNCONF);
    473 }
    474 
    475 static volatile int zssoftpending;
    476 
    477 /*
    478  * Our ZS chips all share a common, autovectored interrupt,
    479  * so we have to look at all of them on each interrupt.
    480  */
    481 static int
    482 zshard(arg)
    483 	void *arg;
    484 {
    485 	register struct zsc_softc *zsc;
    486 	register int unit, rr3, rval, softreq;
    487 
    488 	rval = softreq = 0;
    489 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    490 		zsc = zs_cd.cd_devs[unit];
    491 		if (zsc == NULL)
    492 			continue;
    493 		rr3 = zsc_intr_hard(zsc);
    494 		/* Count up the interrupts. */
    495 		if (rr3) {
    496 			rval |= rr3;
    497 			zsc->zsc_intrcnt.ev_count++;
    498 		}
    499 		softreq |= zsc->zsc_cs[0]->cs_softreq;
    500 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    501 	}
    502 
    503 	/* We are at splzs here, so no need to lock. */
    504 	if (softreq && (zssoftpending == 0)) {
    505 		zssoftpending = IE_ZSSOFT;
    506 #if defined(SUN4M)
    507 		if (CPU_ISSUN4M)
    508 			raise(0, PIL_TTY);
    509 		else
    510 #endif
    511 			ienab_bis(IE_ZSSOFT);
    512 	}
    513 	return (rval);
    514 }
    515 
    516 /*
    517  * Similar scheme as for zshard (look at all of them)
    518  */
    519 static int
    520 zssoft(arg)
    521 	void *arg;
    522 {
    523 	register struct zsc_softc *zsc;
    524 	register int s, unit;
    525 
    526 	/* This is not the only ISR on this IPL. */
    527 	if (zssoftpending == 0)
    528 		return (0);
    529 
    530 	/*
    531 	 * The soft intr. bit will be set by zshard only if
    532 	 * the variable zssoftpending is zero.  The order of
    533 	 * these next two statements prevents our clearing
    534 	 * the soft intr bit just after zshard has set it.
    535 	 */
    536 	/* ienab_bic(IE_ZSSOFT); */
    537 	zssoftpending = 0;
    538 
    539 	/* Make sure we call the tty layer at spltty. */
    540 	s = spltty();
    541 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    542 		zsc = zs_cd.cd_devs[unit];
    543 		if (zsc == NULL)
    544 			continue;
    545 		(void)zsc_intr_soft(zsc);
    546 	}
    547 	splx(s);
    548 	return (1);
    549 }
    550 
    551 
    552 /*
    553  * Compute the current baud rate given a ZS channel.
    554  */
    555 static int
    556 zs_get_speed(cs)
    557 	struct zs_chanstate *cs;
    558 {
    559 	int tconst;
    560 
    561 	tconst = zs_read_reg(cs, 12);
    562 	tconst |= zs_read_reg(cs, 13) << 8;
    563 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    564 }
    565 
    566 /*
    567  * MD functions for setting the baud rate and control modes.
    568  */
    569 int
    570 zs_set_speed(cs, bps)
    571 	struct zs_chanstate *cs;
    572 	int bps;	/* bits per second */
    573 {
    574 	int tconst, real_bps;
    575 
    576 	if (bps == 0)
    577 		return (0);
    578 
    579 #ifdef	DIAGNOSTIC
    580 	if (cs->cs_brg_clk == 0)
    581 		panic("zs_set_speed");
    582 #endif
    583 
    584 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    585 	if (tconst < 0)
    586 		return (EINVAL);
    587 
    588 	/* Convert back to make sure we can do it. */
    589 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    590 
    591 	/* XXX - Allow some tolerance here? */
    592 	if (real_bps != bps)
    593 		return (EINVAL);
    594 
    595 	cs->cs_preg[12] = tconst;
    596 	cs->cs_preg[13] = tconst >> 8;
    597 
    598 	/* Caller will stuff the pending registers. */
    599 	return (0);
    600 }
    601 
    602 int
    603 zs_set_modes(cs, cflag)
    604 	struct zs_chanstate *cs;
    605 	int cflag;	/* bits per second */
    606 {
    607 	int s;
    608 
    609 	/*
    610 	 * Output hardware flow control on the chip is horrendous:
    611 	 * if carrier detect drops, the receiver is disabled, and if
    612 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    613 	 * Therefore, NEVER set the HFC bit, and instead use the
    614 	 * status interrupt to detect CTS changes.
    615 	 */
    616 	s = splzs();
    617 	cs->cs_rr0_pps = 0;
    618 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    619 		cs->cs_rr0_dcd = 0;
    620 		if ((cflag & MDMBUF) == 0)
    621 			cs->cs_rr0_pps = ZSRR0_DCD;
    622 	} else
    623 		cs->cs_rr0_dcd = ZSRR0_DCD;
    624 	if ((cflag & CRTSCTS) != 0) {
    625 		cs->cs_wr5_dtr = ZSWR5_DTR;
    626 		cs->cs_wr5_rts = ZSWR5_RTS;
    627 		cs->cs_rr0_cts = ZSRR0_CTS;
    628 	} else if ((cflag & CDTRCTS) != 0) {
    629 		cs->cs_wr5_dtr = 0;
    630 		cs->cs_wr5_rts = ZSWR5_DTR;
    631 		cs->cs_rr0_cts = ZSRR0_CTS;
    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 	register 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 	register 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 /*
    722  * Handle user request to enter kernel debugger.
    723  */
    724 void
    725 zs_abort(cs)
    726 	struct zs_chanstate *cs;
    727 {
    728 	register volatile struct zschan *zc = zs_conschan;
    729 	int rr0;
    730 
    731 	/* Wait for end of break to avoid PROM abort. */
    732 	/* XXX - Limit the wait? */
    733 	do {
    734 		rr0 = zc->zc_csr;
    735 		ZS_DELAY();
    736 	} while (rr0 & ZSRR0_BREAK);
    737 
    738 #if defined(KGDB)
    739 	zskgdb(cs);
    740 #elif defined(DDB)
    741 	Debugger();
    742 #else
    743 	printf("stopping on keyboard abort\n");
    744 	callrom();
    745 #endif
    746 }
    747 
    748 /*
    749  * Polled input char.
    750  */
    751 int
    752 zs_getc(arg)
    753 	void *arg;
    754 {
    755 	register volatile struct zschan *zc = arg;
    756 	register int s, c, rr0;
    757 
    758 	s = splhigh();
    759 	/* Wait for a character to arrive. */
    760 	do {
    761 		rr0 = zc->zc_csr;
    762 		ZS_DELAY();
    763 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    764 
    765 	c = zc->zc_data;
    766 	ZS_DELAY();
    767 	splx(s);
    768 
    769 	/*
    770 	 * This is used by the kd driver to read scan codes,
    771 	 * so don't translate '\r' ==> '\n' here...
    772 	 */
    773 	return (c);
    774 }
    775 
    776 /*
    777  * Polled output char.
    778  */
    779 void
    780 zs_putc(arg, c)
    781 	void *arg;
    782 	int c;
    783 {
    784 	register volatile struct zschan *zc = arg;
    785 	register int s, rr0;
    786 
    787 	s = splhigh();
    788 
    789 	/* Wait for transmitter to become ready. */
    790 	do {
    791 		rr0 = zc->zc_csr;
    792 		ZS_DELAY();
    793 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    794 
    795 	/*
    796 	 * Send the next character.
    797 	 * Now you'd think that this could be followed by a ZS_DELAY()
    798 	 * just like all the other chip accesses, but it turns out that
    799 	 * the `transmit-ready' interrupt isn't de-asserted until
    800 	 * some period of time after the register write completes
    801 	 * (more than a couple instructions).  So to avoid stray
    802 	 * interrupts we put in the 2us delay regardless of cpu model.
    803 	 */
    804 	zc->zc_data = c;
    805 	delay(2);
    806 
    807 	splx(s);
    808 }
    809 
    810 /*****************************************************************/
    811 
    812 static void zscninit __P((struct consdev *));
    813 static int  zscngetc __P((dev_t));
    814 static void zscnputc __P((dev_t, int));
    815 
    816 /*
    817  * Console table shared by ttya, ttyb
    818  */
    819 struct consdev consdev_tty = {
    820 	nullcnprobe,
    821 	zscninit,
    822 	zscngetc,
    823 	zscnputc,
    824 	nullcnpollc,
    825 	NULL,
    826 };
    827 
    828 static void
    829 zscninit(cn)
    830 	struct consdev *cn;
    831 {
    832 }
    833 
    834 /*
    835  * Polled console input putchar.
    836  */
    837 static int
    838 zscngetc(dev)
    839 	dev_t dev;
    840 {
    841 	return (zs_getc(zs_conschan));
    842 }
    843 
    844 /*
    845  * Polled console output putchar.
    846  */
    847 static void
    848 zscnputc(dev, c)
    849 	dev_t dev;
    850 	int c;
    851 {
    852 	zs_putc(zs_conschan, c);
    853 }
    854 
    855 /*****************************************************************/
    856 
    857 static void prom_cninit __P((struct consdev *));
    858 static int  prom_cngetc __P((dev_t));
    859 static void prom_cnputc __P((dev_t, int));
    860 
    861 /*
    862  * The console is set to this one initially,
    863  * which lets us use the PROM until consinit()
    864  * is called to select a real console.
    865  */
    866 struct consdev consdev_prom = {
    867 	nullcnprobe,
    868 	prom_cninit,
    869 	prom_cngetc,
    870 	prom_cnputc,
    871 	nullcnpollc,
    872 };
    873 
    874 /*
    875  * The console table pointer is statically initialized
    876  * to point to the PROM (output only) table, so that
    877  * early calls to printf will work.
    878  */
    879 struct consdev *cn_tab = &consdev_prom;
    880 
    881 void
    882 nullcnprobe(cn)
    883 	struct consdev *cn;
    884 {
    885 }
    886 
    887 static void
    888 prom_cninit(cn)
    889 	struct consdev *cn;
    890 {
    891 }
    892 
    893 /*
    894  * PROM console input putchar.
    895  * (dummy - this is output only) (WHY?????!)
    896  */
    897 static int
    898 prom_cngetc(dev)
    899 	dev_t dev;
    900 {
    901 	return (prom_getchar());
    902 }
    903 
    904 /*
    905  * PROM console output putchar.
    906  */
    907 static void
    908 prom_cnputc(dev, c)
    909 	dev_t dev;
    910 	int c;
    911 {
    912 
    913 	prom_putchar(c);
    914 }
    915 
    916 /*****************************************************************/
    917 
    918 extern struct consdev consdev_kd;
    919 
    920 static char *prom_inSrc_name[] = {
    921 	"keyboard/display",
    922 	"ttya", "ttyb",
    923 	"ttyc", "ttyd" };
    924 
    925 
    926 static int get_serial_promdev __P((int));
    927 
    928 int
    929 get_serial_promdev(io)
    930 	int io;
    931 {
    932 	char *prop, *cp, buffer[128];
    933 	int node;
    934 
    935 	node = findroot();
    936 	prop = (io == 0) ? "stdin-path" : "stdout-path";
    937 
    938 	cp = getpropstringA(node, prop, buffer, sizeof buffer);
    939 
    940 	/*
    941 	 * At this point we assume the device path is in the form
    942 	 *   ....device@x,y:a for ttya and ...device@x,y:b for ttyb, etc.
    943 	 */
    944 	if (cp[0] != '\0' && cp[1] != '\0') {
    945 		while (*cp != '\0')
    946 			cp++;
    947 		cp -= 2;
    948 	} else {
    949 		/*
    950 		 * If don't have at least a 2 character string at cp, then
    951 		 *  we default to using using the string ":a" for ttya.
    952 		 */
    953 		cp[0] = ':';
    954 		cp[1] = 'a';
    955 		cp[2] = '\0';
    956 	}
    957 
    958 	if (cp >= buffer) {
    959 		/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
    960 		if (cp[0] == ':' && cp[1] >= 'a' && cp[1] <= 'z')
    961 			return (PROMDEV_TTYA + (cp[1] - 'a'));
    962 	}
    963 
    964 	printf("Warning: unparseable %s property\n", prop);
    965 	return (-1);
    966 }
    967 
    968 /*
    969  * This function replaces sys/dev/cninit.c
    970  * Determine which device is the console using
    971  * the PROM "input source" and "output sink".
    972  */
    973 void
    974 consinit()
    975 {
    976 	struct zschan *zc;
    977 	struct zsdevice *zsd;
    978 	struct consdev *cn;
    979 	int channel, promzs_unit, zstty_unit;
    980 	int inSource, outSink;
    981 	int node;
    982 	char *devtype;
    983 	extern int fbnode;
    984 
    985 	switch (prom_version()) {
    986 	case PROM_OLDMON:
    987 	case PROM_OBP_V0:
    988 		/* The stdio handles identify the device type */
    989 		inSource = prom_stdin();
    990 		outSink  = prom_stdout();
    991 		break;
    992 	case PROM_OBP_V2:
    993 	case PROM_OBP_V3:
    994 	case PROM_OPENFIRM:
    995 		/*
    996 		 * We need to probe the PROM device tree.
    997 		 *
    998 		 * Translate the STDIO package instance (`ihandle') -- that
    999 		 * the PROM has already opened for us -- to a device tree
   1000 		 * node (i.e. a `phandle').
   1001 		 */
   1002 
   1003 		if ((node = prom_instance_to_package(prom_stdin())) == 0) {
   1004 			printf("consinit: cannot convert stdin ihandle\n");
   1005 			inSource = -1;
   1006 			goto setup_output;
   1007 		}
   1008 
   1009 		if (prom_node_has_property(node, "keyboard")) {
   1010 			inSource = PROMDEV_KBD;
   1011 		} else if (strcmp(getpropstring(node, "device_type"),
   1012 				  "serial") == 0) {
   1013 			inSource = get_serial_promdev(0);
   1014 		} else {
   1015 			/* not serial, not keyboard. what is it?!? */
   1016 			inSource = -1;
   1017 		}
   1018 
   1019 setup_output:
   1020 		if ((node = prom_instance_to_package(prom_stdout())) == 0) {
   1021 			printf("consinit: cannot convert stdout ihandle\n");
   1022 			outSink = -1;
   1023 			goto setup_console;
   1024 		}
   1025 		devtype = getpropstring(node, "device_type");
   1026 		if (strcmp(devtype, "display") == 0) {
   1027 			/* frame buffer output */
   1028 			outSink = PROMDEV_SCREEN;
   1029 			fbnode = node;
   1030 		} else if (strcmp(devtype, "serial") == 0) {
   1031 			outSink = get_serial_promdev(1);
   1032 		} else {
   1033 			/* not screen, not serial. Whatzit? */
   1034 			outSink = -1;
   1035 		}
   1036 		break;
   1037 
   1038 	default:
   1039 		inSource = -1;
   1040 		outSink = -1;
   1041 	}
   1042 
   1043 setup_console:
   1044 	if (inSource != outSink) {
   1045 		printf("cninit: mismatched PROM output selector\n");
   1046 		printf("inSource=%x; Sink=%x\n", inSource, outSink);
   1047 	}
   1048 
   1049 	switch (inSource) {
   1050 	default:
   1051 		printf("cninit: invalid inSource=0x%x\n", inSource);
   1052 		prom_abort();
   1053 		inSource = PROMDEV_KBD;
   1054 		/* fall through */
   1055 
   1056 	case 0:	/* keyboard/display */
   1057 #if NKBD > 0
   1058 		promzs_unit = 1;	/* XXX - config info! */
   1059 		channel = 0;
   1060 		cn = &consdev_kd;
   1061 		/* Set cn_dev, cn_pri in kd.c */
   1062 		break;
   1063 #else	/* NKBD */
   1064 		printf("cninit: kdb/display not configured\n");
   1065 		callrom();
   1066 		inSource = PROMDEV_TTYA;
   1067 		/* fall through */
   1068 #endif	/* NKBD */
   1069 
   1070 	case PROMDEV_TTYA:
   1071 	case PROMDEV_TTYB:
   1072 		zstty_unit = inSource - PROMDEV_TTYA;
   1073 		promzs_unit = 0;	/* XXX - config info! */
   1074 		channel = zstty_unit & 1;
   1075 		cn = &consdev_tty;
   1076 		cn->cn_dev = makedev(zs_major, zstty_unit);
   1077 		cn->cn_pri = CN_REMOTE;
   1078 		break;
   1079 
   1080 	}
   1081 	/* Now that inSource has been validated, print it. */
   1082 	printf("console is %s\n", prom_inSrc_name[inSource]);
   1083 
   1084 	zsd = findzs(promzs_unit);
   1085 	if (zsd == NULL) {
   1086 		printf("cninit: zs not mapped.\n");
   1087 		return;
   1088 	}
   1089 	zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
   1090 	zs_conschan = zc;
   1091 	cn_tab = cn;
   1092 	(*cn->cn_init)(cn);
   1093 #ifdef	KGDB
   1094 	zs_kgdb_init();
   1095 #endif
   1096 }
   1097 
   1098 /*
   1099  * Power management hooks for zsopen() and zsclose().
   1100  * We use them to power on/off the ports, if necessary.
   1101  */
   1102 int
   1103 zs_enable(cs)
   1104 	struct zs_chanstate *cs;
   1105 {
   1106 	auxiotwoserialendis (ZS_ENABLE);
   1107 	cs->enabled = 1;
   1108 	return(0);
   1109 }
   1110 
   1111 void
   1112 zs_disable(cs)
   1113 	struct zs_chanstate *cs;
   1114 {
   1115 	auxiotwoserialendis (ZS_DISABLE);
   1116 	cs->enabled = 0;
   1117 }
   1118