Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.85.6.1
      1 /*	$NetBSD: zs.c,v 1.85.6.1 2002/12/07 21:55:32 he Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Gordon W. Ross.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Zilog Z8530 Dual UART driver (machine-dependent part)
     41  *
     42  * Runs two serial lines per chip using slave drivers.
     43  * Plain tty/async lines use the zs_async slave.
     44  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
     45  */
     46 
     47 #include "opt_ddb.h"
     48 #include "opt_kgdb.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/conf.h>
     53 #include <sys/device.h>
     54 #include <sys/file.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/kernel.h>
     57 #include <sys/proc.h>
     58 #include <sys/tty.h>
     59 #include <sys/time.h>
     60 #include <sys/syslog.h>
     61 
     62 #include <machine/bsd_openprom.h>
     63 #include <machine/autoconf.h>
     64 #include <machine/intr.h>
     65 #include <machine/conf.h>
     66 #include <machine/eeprom.h>
     67 #include <machine/psl.h>
     68 #include <machine/z8530var.h>
     69 
     70 #include <dev/cons.h>
     71 #include <dev/ic/z8530reg.h>
     72 
     73 #include <sparc/sparc/vaddrs.h>
     74 #include <sparc/sparc/auxreg.h>
     75 #include <sparc/sparc/auxiotwo.h>
     76 #include <sparc/dev/cons.h>
     77 
     78 #include "kbd.h"	/* NKBD */
     79 #include "zs.h" 	/* NZS */
     80 
     81 /* Make life easier for the initialized arrays here. */
     82 #if NZS < 3
     83 #undef  NZS
     84 #define NZS 3
     85 #endif
     86 
     87 /*
     88  * Some warts needed by z8530tty.c -
     89  * The default parity REALLY needs to be the same as the PROM uses,
     90  * or you can not see messages done with printf during boot-up...
     91  */
     92 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     93 int zs_major = 12;
     94 
     95 /*
     96  * The Sun provides a 4.9152 MHz clock to the ZS chips.
     97  */
     98 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     99 
    100 /*
    101  * Select software interrupt bit based on TTY ipl.
    102  */
    103 #if PIL_TTY == 1
    104 # define IE_ZSSOFT IE_L1
    105 #elif PIL_TTY == 4
    106 # define IE_ZSSOFT IE_L4
    107 #elif PIL_TTY == 6
    108 # define IE_ZSSOFT IE_L6
    109 #else
    110 # error "no suitable software interrupt bit"
    111 #endif
    112 
    113 #define	ZS_DELAY()		(CPU_ISSUN4C ? (0) : delay(2))
    114 
    115 /* The layout of this is hardware-dependent (padding, order). */
    116 struct zschan {
    117 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    118 	u_char		zc_xxx0;
    119 	volatile u_char	zc_data;	/* data */
    120 	u_char		zc_xxx1;
    121 };
    122 struct zsdevice {
    123 	/* Yes, they are backwards. */
    124 	struct	zschan zs_chan_b;
    125 	struct	zschan zs_chan_a;
    126 };
    127 
    128 /* ZS channel used as the console device (if any) */
    129 void *zs_conschan_get, *zs_conschan_put;
    130 
    131 static u_char zs_init_reg[16] = {
    132 	0,	/* 0: CMD (reset, etc.) */
    133 	0,	/* 1: No interrupts yet. */
    134 	0,	/* 2: IVECT */
    135 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    136 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    137 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    138 	0,	/* 6: TXSYNC/SYNCLO */
    139 	0,	/* 7: RXSYNC/SYNCHI */
    140 	0,	/* 8: alias for data port */
    141 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
    142 	0,	/*10: Misc. TX/RX control bits */
    143 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    144 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    145 	0,			/*13: BAUDHI (default=9600) */
    146 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    147 	ZSWR15_BREAK_IE,
    148 };
    149 
    150 /* Console ops */
    151 static int  zscngetc __P((dev_t));
    152 static void zscnputc __P((dev_t, int));
    153 static void zscnpollc __P((dev_t, int));
    154 
    155 struct consdev zs_consdev = {
    156 	NULL,
    157 	NULL,
    158 	zscngetc,
    159 	zscnputc,
    160 	zscnpollc,
    161 	NULL,
    162 };
    163 
    164 
    165 /****************************************************************
    166  * Autoconfig
    167  ****************************************************************/
    168 
    169 /* Definition of the driver for autoconfig. */
    170 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    171 static int  zs_match_obio __P((struct device *, struct cfdata *, void *));
    172 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    173 static void zs_attach_obio __P((struct device *, struct device *, void *));
    174 
    175 
    176 static void zs_attach __P((struct zsc_softc *, struct zsdevice *, int));
    177 static int  zs_print __P((void *, const char *name));
    178 
    179 struct cfattach zs_mainbus_ca = {
    180 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    181 };
    182 
    183 struct cfattach zs_obio_ca = {
    184 	sizeof(struct zsc_softc), zs_match_obio, zs_attach_obio
    185 };
    186 
    187 extern struct cfdriver zs_cd;
    188 
    189 /* Interrupt handlers. */
    190 static int zshard __P((void *));
    191 static int zssoft __P((void *));
    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, 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 = PROM_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, &bh) != 0) {
    297 				printf(" cannot map zs registers\n");
    298 				return;
    299 			}
    300 			va = (void *)bh;
    301 		}
    302 
    303 		/*
    304 		 * Check if power state can be set, e.g. Tadpole 3GX
    305 		 */
    306 		if (PROM_getpropint(sa->sa_node, "pwr-on-auxio2", 0))
    307 		{
    308 			printf (" powered via auxio2");
    309 			for (channel = 0; channel < 2; channel++) {
    310 				cs = &zsc->zsc_cs_store[channel];
    311 				cs->enable = zs_enable;
    312 				cs->disable = zs_disable;
    313 			}
    314 		}
    315 
    316 		zsc->zsc_bustag = sa->sa_bustag;
    317 		zsc->zsc_dmatag = sa->sa_dmatag;
    318 		zsc->zsc_promunit = PROM_getpropint(sa->sa_node, "slave", -2);
    319 		zsc->zsc_node = sa->sa_node;
    320 		zs_attach(zsc, va, sa->sa_pri);
    321 	} else {
    322 		struct obio4_attach_args *oba = &uoba->uoba_oba4;
    323 		bus_space_handle_t bh;
    324 		bus_addr_t paddr = oba->oba_paddr;
    325 
    326 		/*
    327 		 * As for zs on mainbus, we require a PROM mapping.
    328 		 */
    329 		if (bus_space_map(oba->oba_bustag,
    330 				  paddr,
    331 				  sizeof(struct zsdevice),
    332 				  BUS_SPACE_MAP_LINEAR | OBIO_BUS_MAP_USE_ROM,
    333 				  &bh) != 0) {
    334 			printf(" cannot map zs registers\n");
    335 			return;
    336 		}
    337 		zsc->zsc_bustag = oba->oba_bustag;
    338 		zsc->zsc_dmatag = oba->oba_dmatag;
    339 		/*
    340 		 * Find prom unit by physical address
    341 		 * We're just comparing the address (not the iospace) here
    342 		 */
    343 		paddr = BUS_ADDR_PADDR(paddr);
    344 		if (cpuinfo.cpu_type == CPUTYP_4_100)
    345 			/*
    346 			 * On the sun4/100, the top-most 4 bits are zero
    347 			 * on obio addresses; force them to 1's for the
    348 			 * sake of the comparison here.
    349 			 */
    350 			paddr |= 0xf0000000;
    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: Consult PROM properties for this?! */
    424 		cs->cs_defspeed = zs_get_speed(cs);
    425 		cs->cs_defcflag = zs_def_cflag;
    426 
    427 		/* Make these correspond to cs_defcflag (-crtscts) */
    428 		cs->cs_rr0_dcd = ZSRR0_DCD;
    429 		cs->cs_rr0_cts = 0;
    430 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    431 		cs->cs_wr5_rts = 0;
    432 
    433 		/*
    434 		 * Clear the master interrupt enable.
    435 		 * The INTENA is common to both channels,
    436 		 * so just do it on the A channel.
    437 		 */
    438 		if (channel == 0) {
    439 			zs_write_reg(cs, 9, 0);
    440 		}
    441 
    442 		/*
    443 		 * Look for a child driver for this channel.
    444 		 * The child attach will setup the hardware.
    445 		 */
    446 		if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
    447 			/* No sub-driver.  Just reset it. */
    448 			u_char reset = (channel == 0) ?
    449 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    450 			s = splzs();
    451 			zs_write_reg(cs,  9, reset);
    452 			splx(s);
    453 		}
    454 	}
    455 
    456 	/*
    457 	 * Now safe to install interrupt handlers.  Note the arguments
    458 	 * to the interrupt handlers aren't used.  Note, we only do this
    459 	 * once since both SCCs interrupt at the same level and vector.
    460 	 */
    461 	if (!didintr) {
    462 		didintr = 1;
    463 		prevpri = pri;
    464 		bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, 0,
    465 				   zshard, NULL);
    466 		bus_intr_establish(zsc->zsc_bustag, PIL_TTY,
    467 				   IPL_SOFTSERIAL,
    468 				   BUS_INTR_ESTABLISH_SOFTINTR,
    469 				   zssoft, NULL);
    470 	} else if (pri != prevpri)
    471 		panic("broken zs interrupt scheme");
    472 
    473 	evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
    474 	    zsc->zsc_dev.dv_xname, "intr");
    475 
    476 	/*
    477 	 * Set the master interrupt enable and interrupt vector.
    478 	 * (common to both channels, do it on A)
    479 	 */
    480 	cs = zsc->zsc_cs[0];
    481 	s = splhigh();
    482 	/* interrupt vector */
    483 	zs_write_reg(cs, 2, zs_init_reg[2]);
    484 	/* master interrupt control (enable) */
    485 	zs_write_reg(cs, 9, zs_init_reg[9]);
    486 	splx(s);
    487 
    488 #if 0
    489 	/*
    490 	 * XXX: L1A hack - We would like to be able to break into
    491 	 * the debugger during the rest of autoconfiguration, so
    492 	 * lower interrupts just enough to let zs interrupts in.
    493 	 * This is done after both zs devices are attached.
    494 	 */
    495 	if (zsc->zsc_promunit == 1) {
    496 		printf("zs1: enabling zs interrupts\n");
    497 		(void)splfd(); /* XXX: splzs - 1 */
    498 	}
    499 #endif
    500 }
    501 
    502 static int
    503 zs_print(aux, name)
    504 	void *aux;
    505 	const char *name;
    506 {
    507 	struct zsc_attach_args *args = aux;
    508 
    509 	if (name != NULL)
    510 		printf("%s: ", name);
    511 
    512 	if (args->channel != -1)
    513 		printf(" channel %d", args->channel);
    514 
    515 	return (UNCONF);
    516 }
    517 
    518 static volatile int zssoftpending;
    519 
    520 /*
    521  * Our ZS chips all share a common, autovectored interrupt,
    522  * so we have to look at all of them on each interrupt.
    523  */
    524 static int
    525 zshard(arg)
    526 	void *arg;
    527 {
    528 	struct zsc_softc *zsc;
    529 	int unit, rr3, rval, softreq;
    530 
    531 	rval = softreq = 0;
    532 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    533 		struct zs_chanstate *cs;
    534 
    535 		zsc = zs_cd.cd_devs[unit];
    536 		if (zsc == NULL)
    537 			continue;
    538 		rr3 = zsc_intr_hard(zsc);
    539 		/* Count up the interrupts. */
    540 		if (rr3) {
    541 			rval |= rr3;
    542 			zsc->zsc_intrcnt.ev_count++;
    543 		}
    544 		if ((cs = zsc->zsc_cs[0]) != NULL)
    545 			softreq |= cs->cs_softreq;
    546 		if ((cs = zsc->zsc_cs[1]) != NULL)
    547 			softreq |= cs->cs_softreq;
    548 	}
    549 
    550 	/* We are at splzs here, so no need to lock. */
    551 	if (softreq && (zssoftpending == 0)) {
    552 		zssoftpending = IE_ZSSOFT;
    553 #if defined(SUN4M)
    554 		if (CPU_ISSUN4M)
    555 			raise(0, PIL_TTY);
    556 		else
    557 #endif
    558 			ienab_bis(IE_ZSSOFT);
    559 	}
    560 	return (rval);
    561 }
    562 
    563 /*
    564  * Similar scheme as for zshard (look at all of them)
    565  */
    566 static int
    567 zssoft(arg)
    568 	void *arg;
    569 {
    570 	struct zsc_softc *zsc;
    571 	int s, unit;
    572 
    573 	/* This is not the only ISR on this IPL. */
    574 	if (zssoftpending == 0)
    575 		return (0);
    576 
    577 	/*
    578 	 * The soft intr. bit will be set by zshard only if
    579 	 * the variable zssoftpending is zero.  The order of
    580 	 * these next two statements prevents our clearing
    581 	 * the soft intr bit just after zshard has set it.
    582 	 */
    583 	/* ienab_bic(IE_ZSSOFT); */
    584 	zssoftpending = 0;
    585 
    586 	/* Make sure we call the tty layer at spltty. */
    587 	s = spltty();
    588 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    589 		zsc = zs_cd.cd_devs[unit];
    590 		if (zsc == NULL)
    591 			continue;
    592 		(void)zsc_intr_soft(zsc);
    593 	}
    594 	splx(s);
    595 	return (1);
    596 }
    597 
    598 
    599 /*
    600  * Compute the current baud rate given a ZS channel.
    601  */
    602 static int
    603 zs_get_speed(cs)
    604 	struct zs_chanstate *cs;
    605 {
    606 	int tconst;
    607 
    608 	tconst = zs_read_reg(cs, 12);
    609 	tconst |= zs_read_reg(cs, 13) << 8;
    610 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    611 }
    612 
    613 /*
    614  * MD functions for setting the baud rate and control modes.
    615  */
    616 int
    617 zs_set_speed(cs, bps)
    618 	struct zs_chanstate *cs;
    619 	int bps;	/* bits per second */
    620 {
    621 	int tconst, real_bps;
    622 
    623 	if (bps == 0)
    624 		return (0);
    625 
    626 #ifdef	DIAGNOSTIC
    627 	if (cs->cs_brg_clk == 0)
    628 		panic("zs_set_speed");
    629 #endif
    630 
    631 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    632 	if (tconst < 0)
    633 		return (EINVAL);
    634 
    635 	/* Convert back to make sure we can do it. */
    636 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    637 
    638 	/* XXX - Allow some tolerance here? */
    639 	if (real_bps != bps)
    640 		return (EINVAL);
    641 
    642 	cs->cs_preg[12] = tconst;
    643 	cs->cs_preg[13] = tconst >> 8;
    644 
    645 	/* Caller will stuff the pending registers. */
    646 	return (0);
    647 }
    648 
    649 int
    650 zs_set_modes(cs, cflag)
    651 	struct zs_chanstate *cs;
    652 	int cflag;	/* bits per second */
    653 {
    654 	int s;
    655 
    656 	/*
    657 	 * Output hardware flow control on the chip is horrendous:
    658 	 * if carrier detect drops, the receiver is disabled, and if
    659 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    660 	 * Therefore, NEVER set the HFC bit, and instead use the
    661 	 * status interrupt to detect CTS changes.
    662 	 */
    663 	s = splzs();
    664 	cs->cs_rr0_pps = 0;
    665 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    666 		cs->cs_rr0_dcd = 0;
    667 		if ((cflag & MDMBUF) == 0)
    668 			cs->cs_rr0_pps = ZSRR0_DCD;
    669 	} else
    670 		cs->cs_rr0_dcd = ZSRR0_DCD;
    671 	if ((cflag & CRTSCTS) != 0) {
    672 		cs->cs_wr5_dtr = ZSWR5_DTR;
    673 		cs->cs_wr5_rts = ZSWR5_RTS;
    674 		cs->cs_rr0_cts = ZSRR0_CTS;
    675 	} else if ((cflag & CDTRCTS) != 0) {
    676 		cs->cs_wr5_dtr = 0;
    677 		cs->cs_wr5_rts = ZSWR5_DTR;
    678 		cs->cs_rr0_cts = ZSRR0_CTS;
    679 	} else if ((cflag & MDMBUF) != 0) {
    680 		cs->cs_wr5_dtr = 0;
    681 		cs->cs_wr5_rts = ZSWR5_DTR;
    682 		cs->cs_rr0_cts = ZSRR0_DCD;
    683 	} else {
    684 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    685 		cs->cs_wr5_rts = 0;
    686 		cs->cs_rr0_cts = 0;
    687 	}
    688 	splx(s);
    689 
    690 	/* Caller will stuff the pending registers. */
    691 	return (0);
    692 }
    693 
    694 
    695 /*
    696  * Read or write the chip with suitable delays.
    697  */
    698 
    699 u_char
    700 zs_read_reg(cs, reg)
    701 	struct zs_chanstate *cs;
    702 	u_char reg;
    703 {
    704 	u_char val;
    705 
    706 	*cs->cs_reg_csr = reg;
    707 	ZS_DELAY();
    708 	val = *cs->cs_reg_csr;
    709 	ZS_DELAY();
    710 	return (val);
    711 }
    712 
    713 void
    714 zs_write_reg(cs, reg, val)
    715 	struct zs_chanstate *cs;
    716 	u_char reg, val;
    717 {
    718 	*cs->cs_reg_csr = reg;
    719 	ZS_DELAY();
    720 	*cs->cs_reg_csr = val;
    721 	ZS_DELAY();
    722 }
    723 
    724 u_char
    725 zs_read_csr(cs)
    726 	struct zs_chanstate *cs;
    727 {
    728 	u_char val;
    729 
    730 	val = *cs->cs_reg_csr;
    731 	ZS_DELAY();
    732 	return (val);
    733 }
    734 
    735 void
    736 zs_write_csr(cs, val)
    737 	struct zs_chanstate *cs;
    738 	u_char val;
    739 {
    740 	*cs->cs_reg_csr = val;
    741 	ZS_DELAY();
    742 }
    743 
    744 u_char
    745 zs_read_data(cs)
    746 	struct zs_chanstate *cs;
    747 {
    748 	u_char val;
    749 
    750 	val = *cs->cs_reg_data;
    751 	ZS_DELAY();
    752 	return (val);
    753 }
    754 
    755 void  zs_write_data(cs, val)
    756 	struct zs_chanstate *cs;
    757 	u_char val;
    758 {
    759 	*cs->cs_reg_data = val;
    760 	ZS_DELAY();
    761 }
    762 
    763 /****************************************************************
    764  * Console support functions (Sun specific!)
    765  * Note: this code is allowed to know about the layout of
    766  * the chip registers, and uses that to keep things simple.
    767  * XXX - I think I like the mvme167 code better. -gwr
    768  ****************************************************************/
    769 
    770 /*
    771  * Handle user request to enter kernel debugger.
    772  */
    773 void
    774 zs_abort(cs)
    775 	struct zs_chanstate *cs;
    776 {
    777 	struct zschan *zc = zs_conschan_get;
    778 	int rr0;
    779 
    780 	/* Wait for end of break to avoid PROM abort. */
    781 	/* XXX - Limit the wait? */
    782 	do {
    783 		rr0 = zc->zc_csr;
    784 		ZS_DELAY();
    785 	} while (rr0 & ZSRR0_BREAK);
    786 
    787 #if defined(KGDB)
    788 	zskgdb(cs);
    789 #elif defined(DDB)
    790 	Debugger();
    791 #else
    792 	printf("stopping on keyboard abort\n");
    793 	callrom();
    794 #endif
    795 }
    796 
    797 int  zs_getc __P((void *arg));
    798 void zs_putc __P((void *arg, int c));
    799 
    800 /*
    801  * Polled input char.
    802  */
    803 int
    804 zs_getc(arg)
    805 	void *arg;
    806 {
    807 	struct zschan *zc = arg;
    808 	int s, c, rr0;
    809 
    810 	s = splhigh();
    811 	/* Wait for a character to arrive. */
    812 	do {
    813 		rr0 = zc->zc_csr;
    814 		ZS_DELAY();
    815 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    816 
    817 	c = zc->zc_data;
    818 	ZS_DELAY();
    819 	splx(s);
    820 
    821 	/*
    822 	 * This is used by the kd driver to read scan codes,
    823 	 * so don't translate '\r' ==> '\n' here...
    824 	 */
    825 	return (c);
    826 }
    827 
    828 /*
    829  * Polled output char.
    830  */
    831 void
    832 zs_putc(arg, c)
    833 	void *arg;
    834 	int c;
    835 {
    836 	struct zschan *zc = arg;
    837 	int s, rr0;
    838 
    839 	s = splhigh();
    840 
    841 	/* Wait for transmitter to become ready. */
    842 	do {
    843 		rr0 = zc->zc_csr;
    844 		ZS_DELAY();
    845 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    846 
    847 	/*
    848 	 * Send the next character.
    849 	 * Now you'd think that this could be followed by a ZS_DELAY()
    850 	 * just like all the other chip accesses, but it turns out that
    851 	 * the `transmit-ready' interrupt isn't de-asserted until
    852 	 * some period of time after the register write completes
    853 	 * (more than a couple instructions).  So to avoid stray
    854 	 * interrupts we put in the 2us delay regardless of cpu model.
    855 	 */
    856 	zc->zc_data = c;
    857 	delay(2);
    858 
    859 	splx(s);
    860 }
    861 
    862 /*****************************************************************/
    863 /*
    864  * Polled console input putchar.
    865  */
    866 int
    867 zscngetc(dev)
    868 	dev_t dev;
    869 {
    870 	return (zs_getc(zs_conschan_get));
    871 }
    872 
    873 /*
    874  * Polled console output putchar.
    875  */
    876 void
    877 zscnputc(dev, c)
    878 	dev_t dev;
    879 	int c;
    880 {
    881 	zs_putc(zs_conschan_put, c);
    882 }
    883 
    884 void
    885 zscnpollc(dev, on)
    886 	dev_t dev;
    887 	int on;
    888 {
    889 	/* No action needed */
    890 }
    891 
    892 int
    893 zs_console_flags(promunit, node, channel)
    894 	int promunit;
    895 	int node;
    896 	int channel;
    897 {
    898 	int cookie, flags = 0;
    899 
    900 	switch (prom_version()) {
    901 	case PROM_OLDMON:
    902 	case PROM_OBP_V0:
    903 		/*
    904 		 * Use `promunit' and `channel' to derive the PROM
    905 		 * stdio handles that correspond to this device.
    906 		 */
    907 		if (promunit == 0)
    908 			cookie = PROMDEV_TTYA + channel;
    909 		else if (promunit == 1 && channel == 0)
    910 			cookie = PROMDEV_KBD;
    911 		else
    912 			cookie = -1;
    913 
    914 		if (cookie == prom_stdin())
    915 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
    916 
    917 		/*
    918 		 * Prevent the keyboard from matching the output device
    919 		 * (note that PROMDEV_KBD == PROMDEV_SCREEN == 0!).
    920 		 */
    921 		if (cookie != PROMDEV_KBD && cookie == prom_stdout())
    922 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
    923 
    924 		break;
    925 
    926 	case PROM_OBP_V2:
    927 	case PROM_OBP_V3:
    928 	case PROM_OPENFIRM:
    929 
    930 		/*
    931 		 * Match the nodes and device arguments prepared by
    932 		 * consinit() against our device node and channel.
    933 		 * (The device argument is the part of the OBP path
    934 		 * following the colon, as in `/obio/zs@0,100000:a')
    935 		 */
    936 
    937 		/* Default to channel 0 if there are no explicit prom args */
    938 		cookie = 0;
    939 
    940 		if (node == prom_stdin_node) {
    941 			if (prom_stdin_args[0] != '\0')
    942 				/* Translate (a,b) -> (0,1) */
    943 				cookie = prom_stdin_args[0] - 'a';
    944 
    945 			if (channel == cookie)
    946 				flags |= ZS_HWFLAG_CONSOLE_INPUT;
    947 		}
    948 
    949 		if (node == prom_stdout_node) {
    950 			if (prom_stdout_args[0] != '\0')
    951 				/* Translate (a,b) -> (0,1) */
    952 				cookie = prom_stdout_args[0] - 'a';
    953 
    954 			if (channel == cookie)
    955 				flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
    956 		}
    957 
    958 		break;
    959 
    960 	default:
    961 		break;
    962 	}
    963 
    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