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