Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.11
      1 /*	$NetBSD: zs.c,v 1.11 1999/04/25 16:16:31 eeh 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/autoconf.h>
     62 #include <machine/openfirm.h>
     63 #include <machine/bsd_openprom.h>
     64 #include <machine/conf.h>
     65 #include <machine/cpu.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 <sparc64/sparc64/vaddrs.h>
     74 #include <sparc64/sparc64/auxreg.h>
     75 #include <sparc64/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()
    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 /* Saved PROM mappings */
    128 static struct zsdevice *zsaddr[NZS];
    129 
    130 /* Flags from cninit() */
    131 static int zs_hwflags[NZS][2];
    132 
    133 /* Default speed for each channel */
    134 static int zs_defspeed[NZS][2] = {
    135 	{ 9600, 	/* ttya */
    136 	  9600 },	/* ttyb */
    137 	{ 1200, 	/* keyboard */
    138 	  1200 },	/* mouse */
    139 	{ 9600, 	/* ttyc */
    140 	  9600 },	/* ttyd */
    141 };
    142 
    143 static u_char zs_init_reg[16] = {
    144 	0,	/* 0: CMD (reset, etc.) */
    145 	0,	/* 1: No interrupts yet. */
    146 	0,	/* 2: IVECT */
    147 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    148 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    149 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    150 	0,	/* 6: TXSYNC/SYNCLO */
    151 	0,	/* 7: RXSYNC/SYNCHI */
    152 	0,	/* 8: alias for data port */
    153 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
    154 	0,	/*10: Misc. TX/RX control bits */
    155 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    156 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    157 	0,			/*13: BAUDHI (default=9600) */
    158 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    159 	ZSWR15_BREAK_IE,
    160 };
    161 
    162 struct zschan *
    163 zs_get_chan_addr(zs_unit, channel)
    164 	int zs_unit, channel;
    165 {
    166 	struct zsdevice	*addr;
    167 	struct zschan	*zc;
    168 
    169 	if (zs_unit >= NZS)
    170 		return (NULL);
    171 	addr = zsaddr[zs_unit];
    172 #ifdef DEBUG
    173 	if (addr == NULL) {
    174 		db_printf("zs_get_chan_addr(): unit %d channel %d not found\n", zs_unit, channel);
    175 		Debugger();
    176 	}
    177 #endif
    178 	if (addr == NULL)
    179 		return (NULL);
    180 	if (channel == 0) {
    181 		zc = &addr->zs_chan_a;
    182 	} else {
    183 		zc = &addr->zs_chan_b;
    184 	}
    185 	return (zc);
    186 }
    187 
    188 
    189 /****************************************************************
    190  * Autoconfig
    191  ****************************************************************/
    192 
    193 /* Definition of the driver for autoconfig. */
    194 static int  zs_match_sbus __P((struct device *, struct cfdata *, void *));
    195 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    196 static int  zs_match_obio __P((struct device *, struct cfdata *, void *));
    197 static void zs_attach_sbus __P((struct device *, struct device *, void *));
    198 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    199 static void zs_attach_obio __P((struct device *, struct device *, void *));
    200 
    201 static void zs_attach __P((struct zsc_softc *, int));
    202 static int  zs_print __P((void *, const char *name));
    203 
    204 struct cfattach zs_ca = {
    205 	sizeof(struct zsc_softc), zs_match_sbus, zs_attach_sbus
    206 };
    207 
    208 struct cfattach zs_mainbus_ca = {
    209 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    210 };
    211 
    212 struct cfattach zs_obio_ca = {
    213 	sizeof(struct zsc_softc), zs_match_obio, zs_attach_obio
    214 };
    215 
    216 extern struct cfdriver zs_cd;
    217 
    218 /* Interrupt handlers. */
    219 static int zshard __P((void *));
    220 static int zssoft __P((void *));
    221 static struct intrhand levelsoft = { zssoft };
    222 
    223 static int zs_get_speed __P((struct zs_chanstate *));
    224 
    225 
    226 /*
    227  * Is the zs chip present?
    228  */
    229 static int
    230 zs_match_mainbus(parent, cf, aux)
    231 	struct device *parent;
    232 	struct cfdata *cf;
    233 	void *aux;
    234 {
    235 	struct mainbus_attach_args *ma = aux;
    236 
    237 	if (strcmp(cf->cf_driver->cd_name, ma->ma_name) != 0)
    238 		return (0);
    239 
    240 	return (getpropint(ma->ma_node, "slave", -2) == cf->cf_unit);
    241 }
    242 
    243 static int
    244 zs_match_sbus(parent, cf, aux)
    245 	struct device *parent;
    246 	struct cfdata *cf;
    247 	void *aux;
    248 {
    249 	struct sbus_attach_args *sa = aux;
    250 
    251 	if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
    252 		return (0);
    253 
    254 	return 1;
    255 }
    256 
    257 static int
    258 zs_match_obio(parent, cf, aux)
    259 	struct device *parent;
    260 	struct cfdata *cf;
    261 	void *aux;
    262 {
    263 #ifdef SUN4U
    264 	return 0;
    265 #else
    266 	union obio_attach_args *uoba = aux;
    267 	struct obio4_attach_args *oba;
    268 
    269 	if (uoba->uoba_isobio4 == 0) {
    270 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    271 
    272 		if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
    273 			return (0);
    274 
    275 		return (getpropint(sa->sa_node, "slave", -2) == cf->cf_unit);
    276 	}
    277 
    278 	oba = &uoba->uoba_oba4;
    279 	return (bus_space_probe(oba->oba_bustag, 0, oba->oba_paddr,
    280 			        1, 0, 0, NULL, NULL));
    281 #endif
    282 }
    283 
    284 static void
    285 zs_attach_mainbus(parent, self, aux)
    286 	struct device *parent;
    287 	struct device *self;
    288 	void *aux;
    289 {
    290 #ifdef SUN4U
    291 	return;
    292 #else
    293 	struct zsc_softc *zsc = (void *) self;
    294 	struct mainbus_attach_args *ma = aux;
    295 	int zs_unit = zsc->zsc_dev.dv_unit;
    296 
    297 	zsc->zsc_bustag = ma->ma_bustag;
    298 	zsc->zsc_dmatag = ma->ma_dmatag;
    299 
    300 	/* Use the mapping setup by the Sun PROM. */
    301 	if (zsaddr[zs_unit] == NULL)
    302 		zsaddr[zs_unit] = findzs(zs_unit);
    303 	if ((void*)zsaddr[zs_unit] != (void*)(u_long)ma->ma_address[0])
    304 		panic("zsattach_mainbus");
    305 	zs_attach(zsc, ma->ma_pri);
    306 #endif
    307 }
    308 
    309 
    310 static void
    311 zs_attach_sbus(parent, self, aux)
    312 	struct device *parent;
    313 	struct device *self;
    314 	void *aux;
    315 {
    316 	struct zsc_softc *zsc = (void *) self;
    317 	struct sbus_attach_args *sa = aux;
    318 	int zs_unit = zsc->zsc_dev.dv_unit;
    319 
    320 	zsc->zsc_bustag = sa->sa_bustag;
    321 	zsc->zsc_dmatag = sa->sa_dmatag;
    322 
    323 	/* Use the mapping setup by the Sun PROM. */
    324 	if (zsaddr[zs_unit] == NULL) {
    325 		if (sa->sa_npromvaddrs) {
    326 			/*
    327 			 * We're converting from a 32-bit pointer to a 64-bit
    328 			 * pointer.  Since the 32-bit entity is negative, but
    329 			 * the kernel is still mapped into the lower 4GB
    330 			 * range, this needs to be zero-extended.
    331 			 *
    332 			 * XXXXX If we map the kernel and devices into the
    333 			 * high 4GB range, this needs to be changed to
    334 			 * sign-extend the address.
    335 			 */
    336 			zsaddr[zs_unit] =
    337 				(struct zsdevice *)
    338 				(unsigned long)sa->sa_promvaddrs[0];
    339 		} else {
    340 			bus_space_handle_t kvaddr;
    341 
    342 			if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    343 					 sa->sa_offset,
    344 					 sa->sa_size,
    345 					 BUS_SPACE_MAP_LINEAR,
    346 					 0, &kvaddr) != 0) {
    347 				printf("%s @ sbus: cannot map registers\n",
    348 				       self->dv_xname);
    349 				return;
    350 			}
    351 			zsaddr[zs_unit] = (struct zsdevice *)
    352 				(long)kvaddr;
    353 		}
    354 	}
    355 	zs_attach(zsc, sa->sa_pri);
    356 }
    357 
    358 static void
    359 zs_attach_obio(parent, self, aux)
    360 	struct device *parent;
    361 	struct device *self;
    362 	void *aux;
    363 {
    364 #ifndef SUN4U
    365 	struct zsc_softc *zsc = (void *) self;
    366 	union obio_attach_args *uoba = aux;
    367 	int zs_unit = zsc->zsc_dev.dv_unit;
    368 
    369 	/* Use the mapping setup by the Sun PROM. */
    370 	if (zsaddr[zs_unit] == NULL)
    371 		zsaddr[zs_unit] = findzs(zs_unit);
    372 
    373 	if (uoba->uoba_isobio4 == 0) {
    374 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    375 		zsc->zsc_bustag = sa->sa_bustag;
    376 		zsc->zsc_dmatag = sa->sa_dmatag;
    377 		zs_attach(zsc, sa->sa_pri);
    378 	} else {
    379 		struct obio4_attach_args *oba = &uoba->uoba_oba4;
    380 		zsc->zsc_bustag = oba->oba_bustag;
    381 		zsc->zsc_dmatag = oba->oba_dmatag;
    382 		zs_attach(zsc, oba->oba_pri);
    383 	}
    384 #endif
    385 }
    386 /*
    387  * Attach a found zs.
    388  *
    389  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    390  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    391  */
    392 static void
    393 zs_attach(zsc, pri)
    394 	struct zsc_softc *zsc;
    395 	int pri;
    396 {
    397 	struct zsc_attach_args zsc_args;
    398 	volatile struct zschan *zc;
    399 	struct zs_chanstate *cs;
    400 	int s, zs_unit, channel;
    401 	static int didintr, prevpri;
    402 
    403 	printf(" softpri %d\n", PIL_TTY);
    404 
    405 	/*
    406 	 * Initialize software state for each channel.
    407 	 */
    408 	zs_unit = zsc->zsc_dev.dv_unit;
    409 	for (channel = 0; channel < 2; channel++) {
    410 		zsc_args.channel = channel;
    411 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    412 		cs = &zsc->zsc_cs_store[channel];
    413 		zsc->zsc_cs[channel] = cs;
    414 
    415 		cs->cs_channel = channel;
    416 		cs->cs_private = NULL;
    417 		cs->cs_ops = &zsops_null;
    418 		cs->cs_brg_clk = PCLK / 16;
    419 
    420 		zc = zs_get_chan_addr(zs_unit, channel);
    421 		if (zs_hwflags[zs_unit][channel] == ZS_HWFLAG_CONSOLE) {
    422 			zs_conschan = (struct zschan *)zc;
    423 		}
    424 		cs->cs_reg_csr  = &zc->zc_csr;
    425 		cs->cs_reg_data = &zc->zc_data;
    426 
    427 		bcopy(zs_init_reg, cs->cs_creg, 16);
    428 		bcopy(zs_init_reg, cs->cs_preg, 16);
    429 
    430 		/* XXX: Get these from the PROM properties! */
    431 		/* XXX: See the mvme167 code.  Better. */
    432 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    433 			cs->cs_defspeed = zs_get_speed(cs);
    434 		else
    435 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
    436 		cs->cs_defcflag = zs_def_cflag;
    437 
    438 		/* Make these correspond to cs_defcflag (-crtscts) */
    439 		cs->cs_rr0_dcd = ZSRR0_DCD;
    440 		cs->cs_rr0_cts = 0;
    441 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    442 		cs->cs_wr5_rts = 0;
    443 
    444 		/*
    445 		 * Clear the master interrupt enable.
    446 		 * The INTENA is common to both channels,
    447 		 * so just do it on the A channel.
    448 		 */
    449 		if (channel == 0) {
    450 			zs_write_reg(cs, 9, 0);
    451 		}
    452 
    453 		/*
    454 		 * Look for a child driver for this channel.
    455 		 * The child attach will setup the hardware.
    456 		 */
    457 		if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
    458 			/* No sub-driver.  Just reset it. */
    459 			u_char reset = (channel == 0) ?
    460 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    461 			s = splzs();
    462 			zs_write_reg(cs,  9, reset);
    463 			splx(s);
    464 		}
    465 	}
    466 
    467 	/*
    468 	 * Now safe to install interrupt handlers.  Note the arguments
    469 	 * to the interrupt handlers aren't used.  Note, we only do this
    470 	 * once since both SCCs interrupt at the same level and vector.
    471 	 */
    472 	if (!didintr) {
    473 		didintr = 1;
    474 		prevpri = pri;
    475 		bus_intr_establish(zsc->zsc_bustag, pri, 0, zshard, NULL);
    476 		intr_establish(PIL_TTY, &levelsoft);
    477 	} else if (pri != prevpri)
    478 		panic("broken zs interrupt scheme");
    479 
    480 	evcnt_attach(&zsc->zsc_dev, "intr", &zsc->zsc_intrcnt);
    481 
    482 	/*
    483 	 * Set the master interrupt enable and interrupt vector.
    484 	 * (common to both channels, do it on A)
    485 	 */
    486 	cs = zsc->zsc_cs[0];
    487 	s = splhigh();
    488 	/* interrupt vector */
    489 	zs_write_reg(cs, 2, zs_init_reg[2]);
    490 	/* master interrupt control (enable) */
    491 	zs_write_reg(cs, 9, zs_init_reg[9]);
    492 	splx(s);
    493 
    494 #if 0
    495 	/*
    496 	 * XXX: L1A hack - We would like to be able to break into
    497 	 * the debugger during the rest of autoconfiguration, so
    498 	 * lower interrupts just enough to let zs interrupts in.
    499 	 * This is done after both zs devices are attached.
    500 	 */
    501 	if (zs_unit == 1) {
    502 		printf("zs1: enabling zs interrupts\n");
    503 		(void)splfd(); /* XXX: splzs - 1 */
    504 	}
    505 #endif
    506 }
    507 
    508 static int
    509 zs_print(aux, name)
    510 	void *aux;
    511 	const char *name;
    512 {
    513 	struct zsc_attach_args *args = aux;
    514 
    515 	if (name != NULL)
    516 		printf("%s: ", name);
    517 
    518 	if (args->channel != -1)
    519 		printf(" channel %d", args->channel);
    520 
    521 	return (UNCONF);
    522 }
    523 
    524 static volatile int zssoftpending;
    525 
    526 /*
    527  * Our ZS chips all share a common, autovectored interrupt,
    528  * so we have to look at all of them on each interrupt.
    529  */
    530 static int
    531 zshard(arg)
    532 	void *arg;
    533 {
    534 	register struct zsc_softc *zsc;
    535 	register int unit, rr3, rval, softreq;
    536 
    537 	rval = softreq = 0;
    538 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    539 		zsc = zs_cd.cd_devs[unit];
    540 		if (zsc == NULL)
    541 			continue;
    542 		rr3 = zsc_intr_hard(zsc);
    543 		/* Count up the interrupts. */
    544 		if (rr3) {
    545 			rval |= rr3;
    546 			zsc->zsc_intrcnt.ev_count++;
    547 		}
    548 		softreq |= zsc->zsc_cs[0]->cs_softreq;
    549 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    550 	}
    551 
    552 	/* We are at splzs here, so no need to lock. */
    553 	if (softreq && (zssoftpending == 0)) {
    554 		zssoftpending = IE_ZSSOFT;
    555 #if defined(SUN4M)
    556 		if (CPU_ISSUN4M)
    557 			raise(0, PIL_TTY);
    558 		else
    559 #endif
    560 			ienab_bis(IE_ZSSOFT);
    561 	}
    562 	return (rval);
    563 }
    564 
    565 /*
    566  * Similar scheme as for zshard (look at all of them)
    567  */
    568 static int
    569 zssoft(arg)
    570 	void *arg;
    571 {
    572 	register struct zsc_softc *zsc;
    573 	register int s, unit;
    574 
    575 	/* This is not the only ISR on this IPL. */
    576 	if (zssoftpending == 0)
    577 		return (0);
    578 
    579 	/*
    580 	 * The soft intr. bit will be set by zshard only if
    581 	 * the variable zssoftpending is zero.  The order of
    582 	 * these next two statements prevents our clearing
    583 	 * the soft intr bit just after zshard has set it.
    584 	 */
    585 	/* ienab_bic(IE_ZSSOFT); */
    586 	zssoftpending = 0;
    587 
    588 	/* Make sure we call the tty layer at spltty. */
    589 	s = spltty();
    590 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    591 		zsc = zs_cd.cd_devs[unit];
    592 		if (zsc == NULL)
    593 			continue;
    594 		(void)zsc_intr_soft(zsc);
    595 	}
    596 	splx(s);
    597 	return (1);
    598 }
    599 
    600 
    601 /*
    602  * Compute the current baud rate given a ZS channel.
    603  */
    604 static int
    605 zs_get_speed(cs)
    606 	struct zs_chanstate *cs;
    607 {
    608 	int tconst;
    609 
    610 	tconst = zs_read_reg(cs, 12);
    611 	tconst |= zs_read_reg(cs, 13) << 8;
    612 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    613 }
    614 
    615 /*
    616  * MD functions for setting the baud rate and control modes.
    617  */
    618 int
    619 zs_set_speed(cs, bps)
    620 	struct zs_chanstate *cs;
    621 	int bps;	/* bits per second */
    622 {
    623 	int tconst, real_bps;
    624 
    625 	if (bps == 0)
    626 		return (0);
    627 
    628 #ifdef	DIAGNOSTIC
    629 	if (cs->cs_brg_clk == 0)
    630 		panic("zs_set_speed");
    631 #endif
    632 
    633 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    634 	if (tconst < 0)
    635 		return (EINVAL);
    636 
    637 	/* Convert back to make sure we can do it. */
    638 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    639 
    640 	/* XXX - Allow some tolerance here? */
    641 	if (real_bps != bps)
    642 		return (EINVAL);
    643 
    644 	cs->cs_preg[12] = tconst;
    645 	cs->cs_preg[13] = tconst >> 8;
    646 
    647 	/* Caller will stuff the pending registers. */
    648 	return (0);
    649 }
    650 
    651 int
    652 zs_set_modes(cs, cflag)
    653 	struct zs_chanstate *cs;
    654 	int cflag;	/* bits per second */
    655 {
    656 	int s;
    657 
    658 	/*
    659 	 * Output hardware flow control on the chip is horrendous:
    660 	 * if carrier detect drops, the receiver is disabled, and if
    661 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    662 	 * Therefore, NEVER set the HFC bit, and instead use the
    663 	 * status interrupt to detect CTS changes.
    664 	 */
    665 	s = splzs();
    666 	cs->cs_rr0_pps = 0;
    667 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    668 		cs->cs_rr0_dcd = 0;
    669 		if ((cflag & MDMBUF) == 0)
    670 			cs->cs_rr0_pps = ZSRR0_DCD;
    671 	} else
    672 		cs->cs_rr0_dcd = ZSRR0_DCD;
    673 	if ((cflag & CRTSCTS) != 0) {
    674 		cs->cs_wr5_dtr = ZSWR5_DTR;
    675 		cs->cs_wr5_rts = ZSWR5_RTS;
    676 		cs->cs_rr0_cts = ZSRR0_CTS;
    677 	} else if ((cflag & CDTRCTS) != 0) {
    678 		cs->cs_wr5_dtr = 0;
    679 		cs->cs_wr5_rts = ZSWR5_DTR;
    680 		cs->cs_rr0_cts = ZSRR0_CTS;
    681 	} else if ((cflag & MDMBUF) != 0) {
    682 		cs->cs_wr5_dtr = 0;
    683 		cs->cs_wr5_rts = ZSWR5_DTR;
    684 		cs->cs_rr0_cts = ZSRR0_DCD;
    685 	} else {
    686 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    687 		cs->cs_wr5_rts = 0;
    688 		cs->cs_rr0_cts = 0;
    689 	}
    690 	splx(s);
    691 
    692 	/* Caller will stuff the pending registers. */
    693 	return (0);
    694 }
    695 
    696 
    697 /*
    698  * Read or write the chip with suitable delays.
    699  */
    700 
    701 u_char
    702 zs_read_reg(cs, reg)
    703 	struct zs_chanstate *cs;
    704 	u_char reg;
    705 {
    706 	u_char val;
    707 
    708 	*cs->cs_reg_csr = reg;
    709 	ZS_DELAY();
    710 	val = *cs->cs_reg_csr;
    711 	ZS_DELAY();
    712 	return (val);
    713 }
    714 
    715 void
    716 zs_write_reg(cs, reg, val)
    717 	struct zs_chanstate *cs;
    718 	u_char reg, val;
    719 {
    720 	*cs->cs_reg_csr = reg;
    721 	ZS_DELAY();
    722 	*cs->cs_reg_csr = val;
    723 	ZS_DELAY();
    724 }
    725 
    726 u_char
    727 zs_read_csr(cs)
    728 	struct zs_chanstate *cs;
    729 {
    730 	register u_char val;
    731 
    732 	val = *cs->cs_reg_csr;
    733 	ZS_DELAY();
    734 	return (val);
    735 }
    736 
    737 void  zs_write_csr(cs, val)
    738 	struct zs_chanstate *cs;
    739 	u_char val;
    740 {
    741 	*cs->cs_reg_csr = val;
    742 	ZS_DELAY();
    743 }
    744 
    745 u_char zs_read_data(cs)
    746 	struct zs_chanstate *cs;
    747 {
    748 	register 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 extern void Debugger __P((void));
    771 void *zs_conschan;
    772 
    773 /*
    774  * Handle user request to enter kernel debugger.
    775  */
    776 void
    777 zs_abort(cs)
    778 	struct zs_chanstate *cs;
    779 {
    780 	register volatile struct zschan *zc = zs_conschan;
    781 	int rr0;
    782 
    783 	/* Wait for end of break to avoid PROM abort. */
    784 	/* XXX - Limit the wait? */
    785 	do {
    786 		rr0 = zc->zc_csr;
    787 		ZS_DELAY();
    788 	} while (rr0 & ZSRR0_BREAK);
    789 
    790 #if defined(KGDB)
    791 	zskgdb(cs);
    792 #elif defined(DDB)
    793 	Debugger();
    794 #else
    795 	printf("stopping on keyboard abort\n");
    796 	callrom();
    797 #endif
    798 }
    799 
    800 /*
    801  * Polled input char.
    802  */
    803 int
    804 zs_getc(arg)
    805 	void *arg;
    806 {
    807 	register volatile struct zschan *zc = arg;
    808 	register 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 	register volatile struct zschan *zc = arg;
    837 	register 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 static void zscninit __P((struct consdev *));
    865 static int  zscngetc __P((dev_t));
    866 static void zscnputc __P((dev_t, int));
    867 static void zscnpollc __P((dev_t, int));
    868 /*
    869  * Console table shared by ttya, ttyb
    870  */
    871 struct consdev consdev_tty = {
    872 	nullcnprobe,
    873 	zscninit,
    874 	zscngetc,
    875 	zscnputc,
    876 	zscnpollc,
    877 };
    878 
    879 static void
    880 zscninit(cn)
    881 	struct consdev *cn;
    882 {
    883 }
    884 
    885 /*
    886  * Polled console input putchar.
    887  */
    888 static int
    889 zscngetc(dev)
    890 	dev_t dev;
    891 {
    892 	return (zs_getc(zs_conschan));
    893 }
    894 
    895 /*
    896  * Polled console output putchar.
    897  */
    898 static void
    899 zscnputc(dev, c)
    900 	dev_t dev;
    901 	int c;
    902 {
    903 	zs_putc(zs_conschan, c);
    904 }
    905 
    906 int swallow_zsintrs;
    907 
    908 static void
    909 zscnpollc(dev, on)
    910 	dev_t dev;
    911 	int on;
    912 {
    913 	/*
    914 	 * Need to tell zs driver to acknowledge all interrupts or we get
    915 	 * annoying spurious interrupt messages.  This is because mucking
    916 	 * with spl() levels during polling does not prevent interrupts from
    917 	 * being generated.
    918 	 */
    919 
    920 	if (on) swallow_zsintrs++;
    921 	else swallow_zsintrs--;
    922 }
    923 
    924 /*****************************************************************/
    925 
    926 static void prom_cninit __P((struct consdev *));
    927 static int  prom_cngetc __P((dev_t));
    928 static void prom_cnputc __P((dev_t, int));
    929 
    930 int stdin = NULL, stdout = NULL;
    931 
    932 /*
    933  * The console is set to this one initially,
    934  * which lets us use the PROM until consinit()
    935  * is called to select a real console.
    936  */
    937 struct consdev consdev_prom = {
    938 	nullcnprobe,
    939 	prom_cninit,
    940 	prom_cngetc,
    941 	prom_cnputc,
    942 	nullcnpollc,
    943 };
    944 
    945 /*
    946  * The console table pointer is statically initialized
    947  * to point to the PROM (output only) table, so that
    948  * early calls to printf will work.
    949  */
    950 struct consdev *cn_tab = &consdev_prom;
    951 
    952 void
    953 nullcnprobe(cn)
    954 	struct consdev *cn;
    955 {
    956 }
    957 
    958 static void
    959 prom_cninit(cn)
    960 	struct consdev *cn;
    961 {
    962 }
    963 
    964 /*
    965  * PROM console input putchar.
    966  * (dummy - this is output only)
    967  */
    968 static int
    969 prom_cngetc(dev)
    970 	dev_t dev;
    971 {
    972 	char c0;
    973 
    974 	if (!stdin) {
    975 		int node = OF_finddevice("/chosen");
    976 		OF_getprop(node, "stdin",  &stdin, sizeof(stdin));
    977 	}
    978 	if (OF_read(stdin, &c0, 1) == 1)
    979 		return (c0 & 0x7f);
    980 	return -1;
    981 }
    982 
    983 /*
    984  * PROM console output putchar.
    985  */
    986 static void
    987 prom_cnputc(dev, c)
    988 	dev_t dev;
    989 	int c;
    990 {
    991 	int s;
    992 	char c0 = (c & 0x7f);
    993 
    994 	if (!stdout) {
    995 		int node = OF_finddevice("/chosen");
    996 		OF_getprop(node, "stdout",  &stdout, sizeof(stdout));
    997 	}
    998 
    999 	s = splhigh();
   1000 	OF_write(stdout, &c0, 1);
   1001 	splx(s);
   1002 }
   1003 
   1004 /*****************************************************************/
   1005 
   1006 extern struct consdev consdev_kd;
   1007 
   1008 static char *prom_inSrc_name[] = {
   1009 	"keyboard/display",
   1010 	"ttya", "ttyb",
   1011 	"ttyc", "ttyd" };
   1012 
   1013 #ifdef	DEBUG
   1014 #define	DBPRINT(x)	printf x
   1015 #else
   1016 #define	DBPRINT(x)
   1017 #endif
   1018 
   1019 /*
   1020  * This function replaces sys/dev/cninit.c
   1021  * Determine which device is the console using
   1022  * the PROM "input source" and "output sink".
   1023  */
   1024 void
   1025 consinit()
   1026 {
   1027 	struct zschan *zc;
   1028 	struct consdev *cn;
   1029 	int channel, zs_unit, zstty_unit;
   1030 	int inSource, outSink;
   1031 	register int node;
   1032 	char buffer[128];
   1033 	register char *cp;
   1034 	extern int fbnode;
   1035 
   1036 	DBPRINT(("consinit()\r\n"));
   1037 	if (cn_tab != &consdev_prom) return;
   1038 
   1039 	inSource = outSink = -1;
   1040 
   1041 	DBPRINT(("setting up stdin\r\n"));
   1042 	node = OF_finddevice("/chosen");
   1043 	OF_getprop(node, "stdin",  &stdin, sizeof(stdin));
   1044 	DBPRINT(("stdin instance = %x\r\n", stdin));
   1045 
   1046 	if ((node = OF_instance_to_package(stdin)) == 0)
   1047 		goto setup_output;
   1048 	DBPRINT(("stdin package = %x\r\n", node));
   1049 	if (OF_getproplen(node,"keyboard") >= 0) {
   1050 		inSource = PROMDEV_KBD;
   1051 	} else if (strcmp(getpropstring(node,"device_type"),"serial") != 0) {
   1052 		/* not a serial, not keyboard. what is it?!? */
   1053 		inSource = -1;
   1054 	}
   1055 
   1056 setup_output:
   1057 	DBPRINT(("setting up stdout\r\n"));
   1058 	node = OF_finddevice("/chosen");
   1059 	OF_getprop(node, "stdout", &stdout, sizeof(stdout));
   1060 
   1061 	DBPRINT(("stdout instance = %x\r\n", stdout));
   1062 
   1063 	node = OF_instance_to_package(stdout);
   1064 	DBPRINT(("stdout package = %x\r\n", node));
   1065 	if (strcmp(getpropstring(node,"device_type"),"display") == 0) {
   1066 		/* frame buffer output */
   1067 		outSink = PROMDEV_SCREEN;
   1068 		fbnode = node;
   1069 	} else if (strcmp(getpropstring(node,"device_type"), "serial")
   1070 		   != 0) {
   1071 		/* not screen, not serial. Whatzit? */
   1072 		outSink = -1;
   1073 	}
   1074 	if (inSource != outSink) {
   1075 		printf("cninit: mismatched PROM output selector\n");
   1076 	}
   1077 
   1078 	switch (inSource) {
   1079 	default:
   1080 		printf("cninit: invalid inSource=%d\n", inSource);
   1081 		callrom();
   1082 		inSource = PROMDEV_KBD;
   1083 		/* fall through */
   1084 
   1085 	case 0:	/* keyboard/display */
   1086 #if NKBD > 0
   1087 		zs_unit = 1;	/* XXX - config info! */
   1088 		channel = 0;
   1089 		cn = &consdev_kd;
   1090 		/* Set cn_dev, cn_pri in kd.c */
   1091 		break;
   1092 #else	/* NKBD */
   1093 		printf("cninit: kdb/display not configured\n");
   1094 		callrom();
   1095 		inSource = PROMDEV_TTYA;
   1096 		/* fall through */
   1097 #endif	/* NKBD */
   1098 
   1099 	case PROMDEV_TTYA:
   1100 	case PROMDEV_TTYB:
   1101 		zstty_unit = inSource - PROMDEV_TTYA;
   1102 		zs_unit = 0;	/* XXX - config info! */
   1103 		channel = zstty_unit & 1;
   1104 		cn = &consdev_tty;
   1105 		cn->cn_dev = makedev(zs_major, zstty_unit);
   1106 		cn->cn_pri = CN_REMOTE;
   1107 		break;
   1108 
   1109 	}
   1110 	/* Now that inSource has been validated, print it. */
   1111 	printf("console is %s\n", prom_inSrc_name[inSource]);
   1112 
   1113 	/*
   1114 	 * We'll just mark this as the future console, but still
   1115 	 * use the PROM until the zs driver attaches.
   1116 	 */
   1117 	zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
   1118 	zs_conschan = NULL;
   1119 	cn_tab = cn;
   1120 
   1121 	(*cn->cn_init)(cn);
   1122 #ifdef	KGDB
   1123 	zs_kgdb_init();
   1124 #endif
   1125 	/* Defer the rest to zs_attach */
   1126 }
   1127