Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.77
      1 /*	$NetBSD: zs.c,v 1.77 2000/03/19 14:58:02 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/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_get, *zs_conschan_put;
    129 
    130 static u_char zs_init_reg[16] = {
    131 	0,	/* 0: CMD (reset, etc.) */
    132 	0,	/* 1: No interrupts yet. */
    133 	0,	/* 2: IVECT */
    134 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    135 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    136 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    137 	0,	/* 6: TXSYNC/SYNCLO */
    138 	0,	/* 7: RXSYNC/SYNCHI */
    139 	0,	/* 8: alias for data port */
    140 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
    141 	0,	/*10: Misc. TX/RX control bits */
    142 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    143 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    144 	0,			/*13: BAUDHI (default=9600) */
    145 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    146 	ZSWR15_BREAK_IE,
    147 };
    148 
    149 /* Console ops */
    150 static int  zscngetc __P((dev_t));
    151 static void zscnputc __P((dev_t, int));
    152 static void zscnpollc __P((dev_t, int));
    153 
    154 struct consdev zs_consdev = {
    155 	NULL,
    156 	NULL,
    157 	zscngetc,
    158 	zscnputc,
    159 	zscnpollc,
    160 	NULL,
    161 };
    162 
    163 
    164 /****************************************************************
    165  * Autoconfig
    166  ****************************************************************/
    167 
    168 /* Definition of the driver for autoconfig. */
    169 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    170 static int  zs_match_obio __P((struct device *, struct cfdata *, void *));
    171 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    172 static void zs_attach_obio __P((struct device *, struct device *, void *));
    173 
    174 
    175 static void zs_attach __P((struct zsc_softc *, struct zsdevice *, int));
    176 static int  zs_print __P((void *, const char *name));
    177 
    178 struct cfattach zs_mainbus_ca = {
    179 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    180 };
    181 
    182 struct cfattach zs_obio_ca = {
    183 	sizeof(struct zsc_softc), zs_match_obio, zs_attach_obio
    184 };
    185 
    186 extern struct cfdriver zs_cd;
    187 
    188 /* Interrupt handlers. */
    189 static int zshard __P((void *));
    190 static int zssoft __P((void *));
    191 static struct intrhand levelsoft = { zssoft };
    192 
    193 static int zs_get_speed __P((struct zs_chanstate *));
    194 
    195 /* Console device support */
    196 static int zs_console_flags __P((int, int, int));
    197 
    198 /* Power management hooks */
    199 int  zs_enable __P((struct zs_chanstate *));
    200 void zs_disable __P((struct zs_chanstate *));
    201 
    202 
    203 /*
    204  * Is the zs chip present?
    205  */
    206 static int
    207 zs_match_mainbus(parent, cf, aux)
    208 	struct device *parent;
    209 	struct cfdata *cf;
    210 	void *aux;
    211 {
    212 	struct mainbus_attach_args *ma = aux;
    213 
    214 	if (strcmp(cf->cf_driver->cd_name, ma->ma_name) != 0)
    215 		return (0);
    216 
    217 	return (1);
    218 }
    219 
    220 static int
    221 zs_match_obio(parent, cf, aux)
    222 	struct device *parent;
    223 	struct cfdata *cf;
    224 	void *aux;
    225 {
    226 	union obio_attach_args *uoba = aux;
    227 	struct obio4_attach_args *oba;
    228 
    229 	if (uoba->uoba_isobio4 == 0) {
    230 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    231 
    232 		if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
    233 			return (0);
    234 
    235 		return (1);
    236 	}
    237 
    238 	oba = &uoba->uoba_oba4;
    239 	return (bus_space_probe(oba->oba_bustag, 0, oba->oba_paddr,
    240 			        1, 0, 0, NULL, NULL));
    241 }
    242 
    243 static void
    244 zs_attach_mainbus(parent, self, aux)
    245 	struct device *parent;
    246 	struct device *self;
    247 	void *aux;
    248 {
    249 	struct zsc_softc *zsc = (void *) self;
    250 	struct mainbus_attach_args *ma = aux;
    251 
    252 	zsc->zsc_bustag = ma->ma_bustag;
    253 	zsc->zsc_dmatag = ma->ma_dmatag;
    254 	zsc->zsc_promunit = getpropint(ma->ma_node, "slave", -2);
    255 	zsc->zsc_node = ma->ma_node;
    256 
    257 	/*
    258 	 * For machines with zs on mainbus (all sun4c models), we expect
    259 	 * the device registers to be mapped by the PROM.
    260 	 */
    261 	zs_attach(zsc, ma->ma_promvaddr, ma->ma_pri);
    262 }
    263 
    264 static void
    265 zs_attach_obio(parent, self, aux)
    266 	struct device *parent;
    267 	struct device *self;
    268 	void *aux;
    269 {
    270 	struct zsc_softc *zsc = (void *) self;
    271 	union obio_attach_args *uoba = aux;
    272 
    273 	if (uoba->uoba_isobio4 == 0) {
    274 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    275 		void *va;
    276 		struct zs_chanstate *cs;
    277 		int channel;
    278 
    279 		if (sa->sa_nintr == 0) {
    280 			printf(" no interrupt lines\n");
    281 			return;
    282 		}
    283 
    284 		/*
    285 		 * Some sun4m models (Javastations) may not map the zs device.
    286 		 */
    287 		if (sa->sa_npromvaddrs > 0)
    288 			va = (void *)sa->sa_promvaddr;
    289 		else {
    290 			bus_space_handle_t bh;
    291 
    292 			if (sbus_bus_map(sa->sa_bustag,
    293 					  sa->sa_slot,
    294 					  sa->sa_offset,
    295 					  sa->sa_size,
    296 					  BUS_SPACE_MAP_LINEAR,
    297 					  0, &bh) != 0) {
    298 				printf(" cannot map zs registers\n");
    299 				return;
    300 			}
    301 			va = (void *)bh;
    302 		}
    303 
    304 		/*
    305 		 * Check if power state can be set, e.g. Tadpole 3GX
    306 		 */
    307 		if (getpropint(sa->sa_node, "pwr-on-auxio2", 0))
    308 		{
    309 			printf (" powered via auxio2");
    310 			for (channel = 0; channel < 2; channel++) {
    311 				cs = &zsc->zsc_cs_store[channel];
    312 				cs->enable = zs_enable;
    313 				cs->disable = zs_disable;
    314 			}
    315 		}
    316 
    317 		zsc->zsc_bustag = sa->sa_bustag;
    318 		zsc->zsc_dmatag = sa->sa_dmatag;
    319 		zsc->zsc_promunit = getpropint(sa->sa_node, "slave", -2);
    320 		zsc->zsc_node = sa->sa_node;
    321 		zs_attach(zsc, va, sa->sa_pri);
    322 	} else {
    323 		struct obio4_attach_args *oba = &uoba->uoba_oba4;
    324 		bus_space_handle_t bh;
    325 		bus_addr_t paddr = oba->oba_paddr;
    326 
    327 		/*
    328 		 * As for zs on mainbus, we require a PROM mapping.
    329 		 */
    330 		if (bus_space_map(oba->oba_bustag,
    331 				  paddr,
    332 				  sizeof(struct zsdevice),
    333 				  BUS_SPACE_MAP_LINEAR | OBIO_BUS_MAP_USE_ROM,
    334 				  &bh) != 0) {
    335 			printf(" cannot map zs registers\n");
    336 			return;
    337 		}
    338 		zsc->zsc_bustag = oba->oba_bustag;
    339 		zsc->zsc_dmatag = oba->oba_dmatag;
    340 		/* Find prom unit by physical address */
    341 		zsc->zsc_promunit =
    342 			(paddr == 0xf1000000) ? 0 :
    343 			(paddr == 0xf0000000) ? 1 :
    344 			(paddr == 0xe0000000) ? 2 : -2;
    345 
    346 		zs_attach(zsc, (void *)bh, oba->oba_pri);
    347 	}
    348 }
    349 /*
    350  * Attach a found zs.
    351  *
    352  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    353  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    354  */
    355 static void
    356 zs_attach(zsc, zsd, pri)
    357 	struct zsc_softc *zsc;
    358 	struct zsdevice *zsd;
    359 	int pri;
    360 {
    361 	struct zsc_attach_args zsc_args;
    362 	struct zs_chanstate *cs;
    363 	int s, channel;
    364 	static int didintr, prevpri;
    365 
    366 	if (zsd == NULL) {
    367 		printf("configuration incomplete\n");
    368 		return;
    369 	}
    370 
    371 	printf(" softpri %d\n", PIL_TTY);
    372 
    373 	/*
    374 	 * Initialize software state for each channel.
    375 	 */
    376 	for (channel = 0; channel < 2; channel++) {
    377 		struct zschan *zc;
    378 
    379 		zsc_args.channel = channel;
    380 		cs = &zsc->zsc_cs_store[channel];
    381 		zsc->zsc_cs[channel] = cs;
    382 
    383 		cs->cs_channel = channel;
    384 		cs->cs_private = NULL;
    385 		cs->cs_ops = &zsops_null;
    386 		cs->cs_brg_clk = PCLK / 16;
    387 
    388 		zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    389 
    390 		zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit,
    391 						    zsc->zsc_node,
    392 						    channel);
    393 
    394 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
    395 			zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
    396 			zsc_args.consdev = &zs_consdev;
    397 		}
    398 
    399 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
    400 			zs_conschan_get = zc;
    401 		}
    402 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
    403 			zs_conschan_put = zc;
    404 		}
    405 		/* Childs need to set cn_dev, etc */
    406 
    407 		cs->cs_reg_csr  = &zc->zc_csr;
    408 		cs->cs_reg_data = &zc->zc_data;
    409 
    410 		bcopy(zs_init_reg, cs->cs_creg, 16);
    411 		bcopy(zs_init_reg, cs->cs_preg, 16);
    412 
    413 		/* XXX: Consult PROM properties for this?! */
    414 		cs->cs_defspeed = zs_get_speed(cs);
    415 		cs->cs_defcflag = zs_def_cflag;
    416 
    417 		/* Make these correspond to cs_defcflag (-crtscts) */
    418 		cs->cs_rr0_dcd = ZSRR0_DCD;
    419 		cs->cs_rr0_cts = 0;
    420 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    421 		cs->cs_wr5_rts = 0;
    422 
    423 		/*
    424 		 * Clear the master interrupt enable.
    425 		 * The INTENA is common to both channels,
    426 		 * so just do it on the A channel.
    427 		 */
    428 		if (channel == 0) {
    429 			zs_write_reg(cs, 9, 0);
    430 		}
    431 
    432 		/*
    433 		 * Look for a child driver for this channel.
    434 		 * The child attach will setup the hardware.
    435 		 */
    436 		if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
    437 			/* No sub-driver.  Just reset it. */
    438 			u_char reset = (channel == 0) ?
    439 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    440 			s = splzs();
    441 			zs_write_reg(cs,  9, reset);
    442 			splx(s);
    443 		}
    444 	}
    445 
    446 	/*
    447 	 * Now safe to install interrupt handlers.  Note the arguments
    448 	 * to the interrupt handlers aren't used.  Note, we only do this
    449 	 * once since both SCCs interrupt at the same level and vector.
    450 	 */
    451 	if (!didintr) {
    452 		didintr = 1;
    453 		prevpri = pri;
    454 		bus_intr_establish(zsc->zsc_bustag, pri, 0, zshard, NULL);
    455 		intr_establish(PIL_TTY, &levelsoft);
    456 	} else if (pri != prevpri)
    457 		panic("broken zs interrupt scheme");
    458 
    459 	evcnt_attach(&zsc->zsc_dev, "intr", &zsc->zsc_intrcnt);
    460 
    461 	/*
    462 	 * Set the master interrupt enable and interrupt vector.
    463 	 * (common to both channels, do it on A)
    464 	 */
    465 	cs = zsc->zsc_cs[0];
    466 	s = splhigh();
    467 	/* interrupt vector */
    468 	zs_write_reg(cs, 2, zs_init_reg[2]);
    469 	/* master interrupt control (enable) */
    470 	zs_write_reg(cs, 9, zs_init_reg[9]);
    471 	splx(s);
    472 
    473 #if 0
    474 	/*
    475 	 * XXX: L1A hack - We would like to be able to break into
    476 	 * the debugger during the rest of autoconfiguration, so
    477 	 * lower interrupts just enough to let zs interrupts in.
    478 	 * This is done after both zs devices are attached.
    479 	 */
    480 	if (zsc->zsc_promunit == 1) {
    481 		printf("zs1: enabling zs interrupts\n");
    482 		(void)splfd(); /* XXX: splzs - 1 */
    483 	}
    484 #endif
    485 }
    486 
    487 static int
    488 zs_print(aux, name)
    489 	void *aux;
    490 	const char *name;
    491 {
    492 	struct zsc_attach_args *args = aux;
    493 
    494 	if (name != NULL)
    495 		printf("%s: ", name);
    496 
    497 	if (args->channel != -1)
    498 		printf(" channel %d", args->channel);
    499 
    500 	return (UNCONF);
    501 }
    502 
    503 static volatile int zssoftpending;
    504 
    505 /*
    506  * Our ZS chips all share a common, autovectored interrupt,
    507  * so we have to look at all of them on each interrupt.
    508  */
    509 static int
    510 zshard(arg)
    511 	void *arg;
    512 {
    513 	struct zsc_softc *zsc;
    514 	int unit, rr3, rval, softreq;
    515 
    516 	rval = softreq = 0;
    517 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    518 		struct zs_chanstate *cs;
    519 
    520 		zsc = zs_cd.cd_devs[unit];
    521 		if (zsc == NULL)
    522 			continue;
    523 		rr3 = zsc_intr_hard(zsc);
    524 		/* Count up the interrupts. */
    525 		if (rr3) {
    526 			rval |= rr3;
    527 			zsc->zsc_intrcnt.ev_count++;
    528 		}
    529 		if ((cs = zsc->zsc_cs[0]) != NULL)
    530 			softreq |= cs->cs_softreq;
    531 		if ((cs = zsc->zsc_cs[1]) != NULL)
    532 			softreq |= cs->cs_softreq;
    533 	}
    534 
    535 	/* We are at splzs here, so no need to lock. */
    536 	if (softreq && (zssoftpending == 0)) {
    537 		zssoftpending = IE_ZSSOFT;
    538 #if defined(SUN4M)
    539 		if (CPU_ISSUN4M)
    540 			raise(0, PIL_TTY);
    541 		else
    542 #endif
    543 			ienab_bis(IE_ZSSOFT);
    544 	}
    545 	return (rval);
    546 }
    547 
    548 /*
    549  * Similar scheme as for zshard (look at all of them)
    550  */
    551 static int
    552 zssoft(arg)
    553 	void *arg;
    554 {
    555 	struct zsc_softc *zsc;
    556 	int s, unit;
    557 
    558 	/* This is not the only ISR on this IPL. */
    559 	if (zssoftpending == 0)
    560 		return (0);
    561 
    562 	/*
    563 	 * The soft intr. bit will be set by zshard only if
    564 	 * the variable zssoftpending is zero.  The order of
    565 	 * these next two statements prevents our clearing
    566 	 * the soft intr bit just after zshard has set it.
    567 	 */
    568 	/* ienab_bic(IE_ZSSOFT); */
    569 	zssoftpending = 0;
    570 
    571 	/* Make sure we call the tty layer at spltty. */
    572 	s = spltty();
    573 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    574 		zsc = zs_cd.cd_devs[unit];
    575 		if (zsc == NULL)
    576 			continue;
    577 		(void)zsc_intr_soft(zsc);
    578 	}
    579 	splx(s);
    580 	return (1);
    581 }
    582 
    583 
    584 /*
    585  * Compute the current baud rate given a ZS channel.
    586  */
    587 static int
    588 zs_get_speed(cs)
    589 	struct zs_chanstate *cs;
    590 {
    591 	int tconst;
    592 
    593 	tconst = zs_read_reg(cs, 12);
    594 	tconst |= zs_read_reg(cs, 13) << 8;
    595 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    596 }
    597 
    598 /*
    599  * MD functions for setting the baud rate and control modes.
    600  */
    601 int
    602 zs_set_speed(cs, bps)
    603 	struct zs_chanstate *cs;
    604 	int bps;	/* bits per second */
    605 {
    606 	int tconst, real_bps;
    607 
    608 	if (bps == 0)
    609 		return (0);
    610 
    611 #ifdef	DIAGNOSTIC
    612 	if (cs->cs_brg_clk == 0)
    613 		panic("zs_set_speed");
    614 #endif
    615 
    616 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    617 	if (tconst < 0)
    618 		return (EINVAL);
    619 
    620 	/* Convert back to make sure we can do it. */
    621 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    622 
    623 	/* XXX - Allow some tolerance here? */
    624 	if (real_bps != bps)
    625 		return (EINVAL);
    626 
    627 	cs->cs_preg[12] = tconst;
    628 	cs->cs_preg[13] = tconst >> 8;
    629 
    630 	/* Caller will stuff the pending registers. */
    631 	return (0);
    632 }
    633 
    634 int
    635 zs_set_modes(cs, cflag)
    636 	struct zs_chanstate *cs;
    637 	int cflag;	/* bits per second */
    638 {
    639 	int s;
    640 
    641 	/*
    642 	 * Output hardware flow control on the chip is horrendous:
    643 	 * if carrier detect drops, the receiver is disabled, and if
    644 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    645 	 * Therefore, NEVER set the HFC bit, and instead use the
    646 	 * status interrupt to detect CTS changes.
    647 	 */
    648 	s = splzs();
    649 	cs->cs_rr0_pps = 0;
    650 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    651 		cs->cs_rr0_dcd = 0;
    652 		if ((cflag & MDMBUF) == 0)
    653 			cs->cs_rr0_pps = ZSRR0_DCD;
    654 	} else
    655 		cs->cs_rr0_dcd = ZSRR0_DCD;
    656 	if ((cflag & CRTSCTS) != 0) {
    657 		cs->cs_wr5_dtr = ZSWR5_DTR;
    658 		cs->cs_wr5_rts = ZSWR5_RTS;
    659 		cs->cs_rr0_cts = ZSRR0_CTS;
    660 	} else if ((cflag & CDTRCTS) != 0) {
    661 		cs->cs_wr5_dtr = 0;
    662 		cs->cs_wr5_rts = ZSWR5_DTR;
    663 		cs->cs_rr0_cts = ZSRR0_CTS;
    664 	} else if ((cflag & MDMBUF) != 0) {
    665 		cs->cs_wr5_dtr = 0;
    666 		cs->cs_wr5_rts = ZSWR5_DTR;
    667 		cs->cs_rr0_cts = ZSRR0_DCD;
    668 	} else {
    669 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    670 		cs->cs_wr5_rts = 0;
    671 		cs->cs_rr0_cts = 0;
    672 	}
    673 	splx(s);
    674 
    675 	/* Caller will stuff the pending registers. */
    676 	return (0);
    677 }
    678 
    679 
    680 /*
    681  * Read or write the chip with suitable delays.
    682  */
    683 
    684 u_char
    685 zs_read_reg(cs, reg)
    686 	struct zs_chanstate *cs;
    687 	u_char reg;
    688 {
    689 	u_char val;
    690 
    691 	*cs->cs_reg_csr = reg;
    692 	ZS_DELAY();
    693 	val = *cs->cs_reg_csr;
    694 	ZS_DELAY();
    695 	return (val);
    696 }
    697 
    698 void
    699 zs_write_reg(cs, reg, val)
    700 	struct zs_chanstate *cs;
    701 	u_char reg, val;
    702 {
    703 	*cs->cs_reg_csr = reg;
    704 	ZS_DELAY();
    705 	*cs->cs_reg_csr = val;
    706 	ZS_DELAY();
    707 }
    708 
    709 u_char
    710 zs_read_csr(cs)
    711 	struct zs_chanstate *cs;
    712 {
    713 	u_char val;
    714 
    715 	val = *cs->cs_reg_csr;
    716 	ZS_DELAY();
    717 	return (val);
    718 }
    719 
    720 void
    721 zs_write_csr(cs, val)
    722 	struct zs_chanstate *cs;
    723 	u_char val;
    724 {
    725 	*cs->cs_reg_csr = val;
    726 	ZS_DELAY();
    727 }
    728 
    729 u_char
    730 zs_read_data(cs)
    731 	struct zs_chanstate *cs;
    732 {
    733 	u_char val;
    734 
    735 	val = *cs->cs_reg_data;
    736 	ZS_DELAY();
    737 	return (val);
    738 }
    739 
    740 void  zs_write_data(cs, val)
    741 	struct zs_chanstate *cs;
    742 	u_char val;
    743 {
    744 	*cs->cs_reg_data = val;
    745 	ZS_DELAY();
    746 }
    747 
    748 /****************************************************************
    749  * Console support functions (Sun specific!)
    750  * Note: this code is allowed to know about the layout of
    751  * the chip registers, and uses that to keep things simple.
    752  * XXX - I think I like the mvme167 code better. -gwr
    753  ****************************************************************/
    754 
    755 /*
    756  * Handle user request to enter kernel debugger.
    757  */
    758 void
    759 zs_abort(cs)
    760 	struct zs_chanstate *cs;
    761 {
    762 	struct zschan *zc = zs_conschan_get;
    763 	int rr0;
    764 
    765 	/* Wait for end of break to avoid PROM abort. */
    766 	/* XXX - Limit the wait? */
    767 	do {
    768 		rr0 = zc->zc_csr;
    769 		ZS_DELAY();
    770 	} while (rr0 & ZSRR0_BREAK);
    771 
    772 #if defined(KGDB)
    773 	zskgdb(cs);
    774 #elif defined(DDB)
    775 	Debugger();
    776 #else
    777 	printf("stopping on keyboard abort\n");
    778 	callrom();
    779 #endif
    780 }
    781 
    782 static int  zs_getc __P((void *arg));
    783 static void zs_putc __P((void *arg, int c));
    784 
    785 /*
    786  * Polled input char.
    787  */
    788 int
    789 zs_getc(arg)
    790 	void *arg;
    791 {
    792 	struct zschan *zc = arg;
    793 	int s, c, rr0;
    794 
    795 	s = splhigh();
    796 	/* Wait for a character to arrive. */
    797 	do {
    798 		rr0 = zc->zc_csr;
    799 		ZS_DELAY();
    800 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    801 
    802 	c = zc->zc_data;
    803 	ZS_DELAY();
    804 	splx(s);
    805 
    806 	/*
    807 	 * This is used by the kd driver to read scan codes,
    808 	 * so don't translate '\r' ==> '\n' here...
    809 	 */
    810 	return (c);
    811 }
    812 
    813 /*
    814  * Polled output char.
    815  */
    816 void
    817 zs_putc(arg, c)
    818 	void *arg;
    819 	int c;
    820 {
    821 	struct zschan *zc = arg;
    822 	int s, rr0;
    823 
    824 	s = splhigh();
    825 
    826 	/* Wait for transmitter to become ready. */
    827 	do {
    828 		rr0 = zc->zc_csr;
    829 		ZS_DELAY();
    830 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    831 
    832 	/*
    833 	 * Send the next character.
    834 	 * Now you'd think that this could be followed by a ZS_DELAY()
    835 	 * just like all the other chip accesses, but it turns out that
    836 	 * the `transmit-ready' interrupt isn't de-asserted until
    837 	 * some period of time after the register write completes
    838 	 * (more than a couple instructions).  So to avoid stray
    839 	 * interrupts we put in the 2us delay regardless of cpu model.
    840 	 */
    841 	zc->zc_data = c;
    842 	delay(2);
    843 
    844 	splx(s);
    845 }
    846 
    847 /*****************************************************************/
    848 /*
    849  * Polled console input putchar.
    850  */
    851 int
    852 zscngetc(dev)
    853 	dev_t dev;
    854 {
    855 	return (zs_getc(zs_conschan_get));
    856 }
    857 
    858 /*
    859  * Polled console output putchar.
    860  */
    861 void
    862 zscnputc(dev, c)
    863 	dev_t dev;
    864 	int c;
    865 {
    866 	zs_putc(zs_conschan_put, c);
    867 }
    868 
    869 void
    870 zscnpollc(dev, on)
    871 	dev_t dev;
    872 	int on;
    873 {
    874 	/* No action needed */
    875 }
    876 
    877 int
    878 zs_console_flags(promunit, node, channel)
    879 	int promunit;
    880 	int node;
    881 	int channel;
    882 {
    883 	int cookie, flags = 0;
    884 
    885 	switch (prom_version()) {
    886 	case PROM_OLDMON:
    887 	case PROM_OBP_V0:
    888 		/*
    889 		 * Use `promunit' and `channel' to derive the PROM
    890 		 * stdio handles that correspond to this device.
    891 		 */
    892 		if (promunit == 0)
    893 			cookie = PROMDEV_TTYA + channel;
    894 		else if (promunit == 1 && channel == 0)
    895 			cookie = PROMDEV_KBD;
    896 		else
    897 			cookie = -1;
    898 
    899 		if (cookie == prom_stdin())
    900 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
    901 
    902 		/*
    903 		 * Prevent the keyboard from matching the output device
    904 		 * (note that PROMDEV_KBD == PROMDEV_SCREEN == 0!).
    905 		 */
    906 		if (cookie != PROMDEV_KBD && cookie == prom_stdout())
    907 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
    908 
    909 		break;
    910 
    911 	case PROM_OBP_V2:
    912 	case PROM_OBP_V3:
    913 	case PROM_OPENFIRM:
    914 
    915 		/*
    916 		 * Match the nodes and device arguments prepared by
    917 		 * consinit() against our device node and channel.
    918 		 * (The device argument is the part of the OBP path
    919 		 * following the colon, as in `/obio/zs@0,100000:a')
    920 		 */
    921 
    922 		/* Default to channel 0 if there are no explicit prom args */
    923 		cookie = 0;
    924 
    925 		if (node == prom_stdin_node) {
    926 			if (prom_stdin_args[0] != '\0')
    927 				/* Translate (a,b) -> (0,1) */
    928 				cookie = prom_stdin_args[0] - 'a';
    929 
    930 			if (channel == cookie)
    931 				flags |= ZS_HWFLAG_CONSOLE_INPUT;
    932 		}
    933 
    934 		if (node == prom_stdout_node) {
    935 			if (prom_stdout_args[0] != '\0')
    936 				/* Translate (a,b) -> (0,1) */
    937 				cookie = prom_stdout_args[0] - 'a';
    938 
    939 			if (channel == cookie)
    940 				flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
    941 		}
    942 
    943 		break;
    944 
    945 	default:
    946 		break;
    947 	}
    948 
    949 prom_printf("zs_console flags: %x\n", flags);
    950 	return (flags);
    951 }
    952 
    953 /*
    954  * Power management hooks for zsopen() and zsclose().
    955  * We use them to power on/off the ports, if necessary.
    956  */
    957 int
    958 zs_enable(cs)
    959 	struct zs_chanstate *cs;
    960 {
    961 	auxiotwoserialendis (ZS_ENABLE);
    962 	cs->enabled = 1;
    963 	return(0);
    964 }
    965 
    966 void
    967 zs_disable(cs)
    968 	struct zs_chanstate *cs;
    969 {
    970 	auxiotwoserialendis (ZS_DISABLE);
    971 	cs->enabled = 0;
    972 }
    973