Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.59
      1 /*	$NetBSD: zs.c,v 1.59 1998/03/30 02:41:21 mycroft 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 <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/conf.h>
     50 #include <sys/device.h>
     51 #include <sys/file.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/kernel.h>
     54 #include <sys/proc.h>
     55 #include <sys/tty.h>
     56 #include <sys/time.h>
     57 #include <sys/syslog.h>
     58 
     59 #include <machine/autoconf.h>
     60 #include <machine/bsd_openprom.h>
     61 #include <machine/conf.h>
     62 #include <machine/cpu.h>
     63 #include <machine/eeprom.h>
     64 #include <machine/psl.h>
     65 #include <machine/z8530var.h>
     66 
     67 #include <dev/cons.h>
     68 #include <dev/ic/z8530reg.h>
     69 
     70 #include <sparc/sparc/vaddrs.h>
     71 #include <sparc/sparc/auxreg.h>
     72 #include <sparc/dev/cons.h>
     73 
     74 #include "kbd.h"	/* NKBD */
     75 #include "zs.h" 	/* NZS */
     76 
     77 /* Make life easier for the initialized arrays here. */
     78 #if NZS < 3
     79 #undef  NZS
     80 #define NZS 3
     81 #endif
     82 
     83 /*
     84  * Some warts needed by z8530tty.c -
     85  * The default parity REALLY needs to be the same as the PROM uses,
     86  * or you can not see messages done with printf during boot-up...
     87  */
     88 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     89 int zs_major = 12;
     90 
     91 /*
     92  * The Sun provides a 4.9152 MHz clock to the ZS chips.
     93  */
     94 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     95 
     96 /*
     97  * Select software interrupt bit based on TTY ipl.
     98  */
     99 #if PIL_TTY == 1
    100 # define IE_ZSSOFT IE_L1
    101 #elif PIL_TTY == 4
    102 # define IE_ZSSOFT IE_L4
    103 #elif PIL_TTY == 6
    104 # define IE_ZSSOFT IE_L6
    105 #else
    106 # error "no suitable software interrupt bit"
    107 #endif
    108 
    109 #define	ZS_DELAY()		(CPU_ISSUN4C ? (0) : delay(2))
    110 
    111 /* The layout of this is hardware-dependent (padding, order). */
    112 struct zschan {
    113 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    114 	u_char		zc_xxx0;
    115 	volatile u_char	zc_data;	/* data */
    116 	u_char		zc_xxx1;
    117 };
    118 struct zsdevice {
    119 	/* Yes, they are backwards. */
    120 	struct	zschan zs_chan_b;
    121 	struct	zschan zs_chan_a;
    122 };
    123 
    124 /* Saved PROM mappings */
    125 static struct zsdevice *zsaddr[NZS];
    126 
    127 /* Flags from cninit() */
    128 static int zs_hwflags[NZS][2];
    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 	14,	/*12: BAUDLO (default=9600) */
    154 	0,	/*13: BAUDHI (default=9600) */
    155 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    156 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
    157 };
    158 
    159 struct zschan *
    160 zs_get_chan_addr(zs_unit, channel)
    161 	int zs_unit, channel;
    162 {
    163 	struct zsdevice	*addr;
    164 	struct zschan	*zc;
    165 
    166 	if (zs_unit >= NZS)
    167 		return (NULL);
    168 	addr = zsaddr[zs_unit];
    169 	if (addr == NULL)
    170 		addr = zsaddr[zs_unit] = findzs(zs_unit);
    171 	if (addr == NULL)
    172 		return (NULL);
    173 	if (channel == 0) {
    174 		zc = &addr->zs_chan_a;
    175 	} else {
    176 		zc = &addr->zs_chan_b;
    177 	}
    178 	return (zc);
    179 }
    180 
    181 
    182 /****************************************************************
    183  * Autoconfig
    184  ****************************************************************/
    185 
    186 /* Definition of the driver for autoconfig. */
    187 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    188 static int  zs_match_obio __P((struct device *, struct cfdata *, void *));
    189 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    190 static void zs_attach_obio __P((struct device *, struct device *, void *));
    191 
    192 static void zs_attach __P((struct zsc_softc *, int));
    193 static int  zs_print __P((void *, const char *name));
    194 
    195 struct cfattach zs_mainbus_ca = {
    196 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    197 };
    198 
    199 struct cfattach zs_obio_ca = {
    200 	sizeof(struct zsc_softc), zs_match_obio, zs_attach_obio
    201 };
    202 
    203 extern struct cfdriver zs_cd;
    204 
    205 /* Interrupt handlers. */
    206 static int zshard __P((void *));
    207 static int zssoft __P((void *));
    208 static struct intrhand levelsoft = { zssoft };
    209 
    210 static int zs_get_speed __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 (getpropint(ma->ma_node, "slave", -2) == cf->cf_unit);
    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 (getpropint(sa->sa_node, "slave", -2) == cf->cf_unit);
    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 	int zs_unit = zsc->zsc_dev.dv_unit;
    262 
    263 	zsc->zsc_bustag = ma->ma_bustag;
    264 	zsc->zsc_dmatag = ma->ma_dmatag;
    265 
    266 	/* Use the mapping setup by the Sun PROM. */
    267 	if (zsaddr[zs_unit] == NULL)
    268 		zsaddr[zs_unit] = findzs(zs_unit);
    269 	if ((void*)zsaddr[zs_unit] != ma->ma_promvaddr)
    270 		panic("zsattach_mainbus");
    271 
    272 	zs_attach(zsc, ma->ma_pri);
    273 }
    274 
    275 static void
    276 zs_attach_obio(parent, self, aux)
    277 	struct device *parent;
    278 	struct device *self;
    279 	void *aux;
    280 {
    281 	struct zsc_softc *zsc = (void *) self;
    282 	union obio_attach_args *uoba = aux;
    283 	int zs_unit = zsc->zsc_dev.dv_unit;
    284 
    285 	/* Use the mapping setup by the Sun PROM. */
    286 	if (zsaddr[zs_unit] == NULL)
    287 		zsaddr[zs_unit] = findzs(zs_unit);
    288 
    289 	if (uoba->uoba_isobio4 == 0) {
    290 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    291 		zsc->zsc_bustag = sa->sa_bustag;
    292 		zsc->zsc_dmatag = sa->sa_dmatag;
    293 		zs_attach(zsc, sa->sa_pri);
    294 	} else {
    295 		struct obio4_attach_args *oba = &uoba->uoba_oba4;
    296 		zsc->zsc_bustag = oba->oba_bustag;
    297 		zsc->zsc_dmatag = oba->oba_dmatag;
    298 		zs_attach(zsc, oba->oba_pri);
    299 	}
    300 }
    301 /*
    302  * Attach a found zs.
    303  *
    304  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    305  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    306  */
    307 static void
    308 zs_attach(zsc, pri)
    309 	struct zsc_softc *zsc;
    310 	int pri;
    311 {
    312 	struct zsc_attach_args zsc_args;
    313 	volatile struct zschan *zc;
    314 	struct zs_chanstate *cs;
    315 	int s, zs_unit, channel;
    316 	static int didintr, prevpri;
    317 
    318 	printf(" softpri %d\n", PIL_TTY);
    319 
    320 	/*
    321 	 * Initialize software state for each channel.
    322 	 */
    323 	zs_unit = zsc->zsc_dev.dv_unit;
    324 	for (channel = 0; channel < 2; channel++) {
    325 		zsc_args.channel = channel;
    326 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    327 		cs = &zsc->zsc_cs_store[channel];
    328 		zsc->zsc_cs[channel] = cs;
    329 
    330 		cs->cs_channel = channel;
    331 		cs->cs_private = NULL;
    332 		cs->cs_ops = &zsops_null;
    333 		cs->cs_brg_clk = PCLK / 16;
    334 
    335 		zc = zs_get_chan_addr(zs_unit, channel);
    336 		cs->cs_reg_csr  = &zc->zc_csr;
    337 		cs->cs_reg_data = &zc->zc_data;
    338 
    339 		bcopy(zs_init_reg, cs->cs_creg, 16);
    340 		bcopy(zs_init_reg, cs->cs_preg, 16);
    341 
    342 		/* XXX: Get these from the PROM properties! */
    343 		/* XXX: See the mvme167 code.  Better. */
    344 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    345 			cs->cs_defspeed = zs_get_speed(cs);
    346 		else
    347 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
    348 		cs->cs_defcflag = zs_def_cflag;
    349 
    350 		/* Make these correspond to cs_defcflag (-crtscts) */
    351 		cs->cs_rr0_dcd = ZSRR0_DCD;
    352 		cs->cs_rr0_cts = 0;
    353 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    354 		cs->cs_wr5_rts = 0;
    355 
    356 		/*
    357 		 * Clear the master interrupt enable.
    358 		 * The INTENA is common to both channels,
    359 		 * so just do it on the A channel.
    360 		 */
    361 		if (channel == 0) {
    362 			zs_write_reg(cs, 9, 0);
    363 		}
    364 
    365 		/*
    366 		 * Look for a child driver for this channel.
    367 		 * The child attach will setup the hardware.
    368 		 */
    369 		if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
    370 			/* No sub-driver.  Just reset it. */
    371 			u_char reset = (channel == 0) ?
    372 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    373 			s = splzs();
    374 			zs_write_reg(cs,  9, reset);
    375 			splx(s);
    376 		}
    377 	}
    378 
    379 	/*
    380 	 * Now safe to install interrupt handlers.  Note the arguments
    381 	 * to the interrupt handlers aren't used.  Note, we only do this
    382 	 * once since both SCCs interrupt at the same level and vector.
    383 	 */
    384 	if (!didintr) {
    385 		didintr = 1;
    386 		prevpri = pri;
    387 		bus_intr_establish(zsc->zsc_bustag, pri, 0, zshard, NULL);
    388 		intr_establish(PIL_TTY, &levelsoft);
    389 	} else if (pri != prevpri)
    390 		panic("broken zs interrupt scheme");
    391 
    392 	evcnt_attach(&zsc->zsc_dev, "intr", &zsc->zsc_intrcnt);
    393 
    394 	/*
    395 	 * Set the master interrupt enable and interrupt vector.
    396 	 * (common to both channels, do it on A)
    397 	 */
    398 	cs = zsc->zsc_cs[0];
    399 	s = splhigh();
    400 	/* interrupt vector */
    401 	zs_write_reg(cs, 2, zs_init_reg[2]);
    402 	/* master interrupt control (enable) */
    403 	zs_write_reg(cs, 9, zs_init_reg[9]);
    404 	splx(s);
    405 
    406 #if 0
    407 	/*
    408 	 * XXX: L1A hack - We would like to be able to break into
    409 	 * the debugger during the rest of autoconfiguration, so
    410 	 * lower interrupts just enough to let zs interrupts in.
    411 	 * This is done after both zs devices are attached.
    412 	 */
    413 	if (zs_unit == 1) {
    414 		printf("zs1: enabling zs interrupts\n");
    415 		(void)splfd(); /* XXX: splzs - 1 */
    416 	}
    417 #endif
    418 }
    419 
    420 static int
    421 zs_print(aux, name)
    422 	void *aux;
    423 	const char *name;
    424 {
    425 	struct zsc_attach_args *args = aux;
    426 
    427 	if (name != NULL)
    428 		printf("%s: ", name);
    429 
    430 	if (args->channel != -1)
    431 		printf(" channel %d", args->channel);
    432 
    433 	return (UNCONF);
    434 }
    435 
    436 static volatile int zssoftpending;
    437 
    438 /*
    439  * Our ZS chips all share a common, autovectored interrupt,
    440  * so we have to look at all of them on each interrupt.
    441  */
    442 static int
    443 zshard(arg)
    444 	void *arg;
    445 {
    446 	register struct zsc_softc *zsc;
    447 	register int unit, rr3, rval, softreq;
    448 
    449 	rval = softreq = 0;
    450 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    451 		zsc = zs_cd.cd_devs[unit];
    452 		if (zsc == NULL)
    453 			continue;
    454 		rr3 = zsc_intr_hard(zsc);
    455 		/* Count up the interrupts. */
    456 		if (rr3) {
    457 			rval |= rr3;
    458 			zsc->zsc_intrcnt.ev_count++;
    459 		}
    460 		softreq |= zsc->zsc_cs[0]->cs_softreq;
    461 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    462 	}
    463 
    464 	/* We are at splzs here, so no need to lock. */
    465 	if (softreq && (zssoftpending == 0)) {
    466 		zssoftpending = IE_ZSSOFT;
    467 #if defined(SUN4M)
    468 		if (CPU_ISSUN4M)
    469 			raise(0, PIL_TTY);
    470 		else
    471 #endif
    472 			ienab_bis(IE_ZSSOFT);
    473 	}
    474 	return (rval);
    475 }
    476 
    477 /*
    478  * Similar scheme as for zshard (look at all of them)
    479  */
    480 static int
    481 zssoft(arg)
    482 	void *arg;
    483 {
    484 	register struct zsc_softc *zsc;
    485 	register int s, unit;
    486 
    487 	/* This is not the only ISR on this IPL. */
    488 	if (zssoftpending == 0)
    489 		return (0);
    490 
    491 	/*
    492 	 * The soft intr. bit will be set by zshard only if
    493 	 * the variable zssoftpending is zero.  The order of
    494 	 * these next two statements prevents our clearing
    495 	 * the soft intr bit just after zshard has set it.
    496 	 */
    497 	/* ienab_bic(IE_ZSSOFT); */
    498 	zssoftpending = 0;
    499 
    500 	/* Make sure we call the tty layer at spltty. */
    501 	s = spltty();
    502 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    503 		zsc = zs_cd.cd_devs[unit];
    504 		if (zsc == NULL)
    505 			continue;
    506 		(void)zsc_intr_soft(zsc);
    507 	}
    508 	splx(s);
    509 	return (1);
    510 }
    511 
    512 
    513 /*
    514  * Compute the current baud rate given a ZS channel.
    515  */
    516 static int
    517 zs_get_speed(cs)
    518 	struct zs_chanstate *cs;
    519 {
    520 	int tconst;
    521 
    522 	tconst = zs_read_reg(cs, 12);
    523 	tconst |= zs_read_reg(cs, 13) << 8;
    524 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    525 }
    526 
    527 /*
    528  * MD functions for setting the baud rate and control modes.
    529  */
    530 int
    531 zs_set_speed(cs, bps)
    532 	struct zs_chanstate *cs;
    533 	int bps;	/* bits per second */
    534 {
    535 	int tconst, real_bps;
    536 
    537 	if (bps == 0)
    538 		return (0);
    539 
    540 #ifdef	DIAGNOSTIC
    541 	if (cs->cs_brg_clk == 0)
    542 		panic("zs_set_speed");
    543 #endif
    544 
    545 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    546 	if (tconst < 0)
    547 		return (EINVAL);
    548 
    549 	/* Convert back to make sure we can do it. */
    550 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    551 
    552 	/* XXX - Allow some tolerance here? */
    553 	if (real_bps != bps)
    554 		return (EINVAL);
    555 
    556 	cs->cs_preg[12] = tconst;
    557 	cs->cs_preg[13] = tconst >> 8;
    558 
    559 	/* Caller will stuff the pending registers. */
    560 	return (0);
    561 }
    562 
    563 int
    564 zs_set_modes(cs, cflag)
    565 	struct zs_chanstate *cs;
    566 	int cflag;	/* bits per second */
    567 {
    568 	int s;
    569 
    570 	/*
    571 	 * Output hardware flow control on the chip is horrendous:
    572 	 * if carrier detect drops, the receiver is disabled, and if
    573 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    574 	 * Therefore, NEVER set the HFC bit, and instead use the
    575 	 * status interrupt to detect CTS changes.
    576 	 */
    577 	s = splzs();
    578 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
    579 		cs->cs_rr0_dcd = 0;
    580 	else
    581 		cs->cs_rr0_dcd = ZSRR0_DCD;
    582 	if ((cflag & CRTSCTS) != 0) {
    583 		cs->cs_wr5_dtr = ZSWR5_DTR;
    584 		cs->cs_wr5_rts = ZSWR5_RTS;
    585 		cs->cs_rr0_cts = ZSRR0_CTS;
    586 	} else if ((cflag & CDTRCTS) != 0) {
    587 		cs->cs_wr5_dtr = 0;
    588 		cs->cs_wr5_rts = ZSWR5_DTR;
    589 		cs->cs_rr0_cts = ZSRR0_CTS;
    590 	} else if ((cflag & MDMBUF) != 0) {
    591 		cs->cs_wr5_dtr = 0;
    592 		cs->cs_wr5_rts = ZSWR5_DTR;
    593 		cs->cs_rr0_cts = ZSRR0_DCD;
    594 	} else {
    595 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    596 		cs->cs_wr5_rts = 0;
    597 		cs->cs_rr0_cts = 0;
    598 	}
    599 	splx(s);
    600 
    601 	/* Caller will stuff the pending registers. */
    602 	return (0);
    603 }
    604 
    605 
    606 /*
    607  * Read or write the chip with suitable delays.
    608  */
    609 
    610 u_char
    611 zs_read_reg(cs, reg)
    612 	struct zs_chanstate *cs;
    613 	u_char reg;
    614 {
    615 	u_char val;
    616 
    617 	*cs->cs_reg_csr = reg;
    618 	ZS_DELAY();
    619 	val = *cs->cs_reg_csr;
    620 	ZS_DELAY();
    621 	return (val);
    622 }
    623 
    624 void
    625 zs_write_reg(cs, reg, val)
    626 	struct zs_chanstate *cs;
    627 	u_char reg, val;
    628 {
    629 	*cs->cs_reg_csr = reg;
    630 	ZS_DELAY();
    631 	*cs->cs_reg_csr = val;
    632 	ZS_DELAY();
    633 }
    634 
    635 u_char
    636 zs_read_csr(cs)
    637 	struct zs_chanstate *cs;
    638 {
    639 	register u_char val;
    640 
    641 	val = *cs->cs_reg_csr;
    642 	ZS_DELAY();
    643 	return (val);
    644 }
    645 
    646 void  zs_write_csr(cs, val)
    647 	struct zs_chanstate *cs;
    648 	u_char val;
    649 {
    650 	*cs->cs_reg_csr = val;
    651 	ZS_DELAY();
    652 }
    653 
    654 u_char zs_read_data(cs)
    655 	struct zs_chanstate *cs;
    656 {
    657 	register u_char val;
    658 
    659 	val = *cs->cs_reg_data;
    660 	ZS_DELAY();
    661 	return (val);
    662 }
    663 
    664 void  zs_write_data(cs, val)
    665 	struct zs_chanstate *cs;
    666 	u_char val;
    667 {
    668 	*cs->cs_reg_data = val;
    669 	ZS_DELAY();
    670 }
    671 
    672 /****************************************************************
    673  * Console support functions (Sun specific!)
    674  * Note: this code is allowed to know about the layout of
    675  * the chip registers, and uses that to keep things simple.
    676  * XXX - I think I like the mvme167 code better. -gwr
    677  ****************************************************************/
    678 
    679 extern void Debugger __P((void));
    680 void *zs_conschan;
    681 
    682 /*
    683  * Handle user request to enter kernel debugger.
    684  */
    685 void
    686 zs_abort(cs)
    687 	struct zs_chanstate *cs;
    688 {
    689 	register volatile struct zschan *zc = zs_conschan;
    690 	int rr0;
    691 
    692 	/* Wait for end of break to avoid PROM abort. */
    693 	/* XXX - Limit the wait? */
    694 	do {
    695 		rr0 = zc->zc_csr;
    696 		ZS_DELAY();
    697 	} while (rr0 & ZSRR0_BREAK);
    698 
    699 #if defined(KGDB)
    700 	zskgdb(cs);
    701 #elif defined(DDB)
    702 	Debugger();
    703 #else
    704 	printf("stopping on keyboard abort\n");
    705 	callrom();
    706 #endif
    707 }
    708 
    709 /*
    710  * Polled input char.
    711  */
    712 int
    713 zs_getc(arg)
    714 	void *arg;
    715 {
    716 	register volatile struct zschan *zc = arg;
    717 	register int s, c, rr0;
    718 
    719 	s = splhigh();
    720 	/* Wait for a character to arrive. */
    721 	do {
    722 		rr0 = zc->zc_csr;
    723 		ZS_DELAY();
    724 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    725 
    726 	c = zc->zc_data;
    727 	ZS_DELAY();
    728 	splx(s);
    729 
    730 	/*
    731 	 * This is used by the kd driver to read scan codes,
    732 	 * so don't translate '\r' ==> '\n' here...
    733 	 */
    734 	return (c);
    735 }
    736 
    737 /*
    738  * Polled output char.
    739  */
    740 void
    741 zs_putc(arg, c)
    742 	void *arg;
    743 	int c;
    744 {
    745 	register volatile struct zschan *zc = arg;
    746 	register int s, rr0;
    747 
    748 	s = splhigh();
    749 
    750 	/* Wait for transmitter to become ready. */
    751 	do {
    752 		rr0 = zc->zc_csr;
    753 		ZS_DELAY();
    754 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    755 
    756 	zc->zc_data = c;
    757 	ZS_DELAY();
    758 
    759 	splx(s);
    760 }
    761 
    762 /*****************************************************************/
    763 
    764 static void zscninit __P((struct consdev *));
    765 static int  zscngetc __P((dev_t));
    766 static void zscnputc __P((dev_t, int));
    767 
    768 /*
    769  * Console table shared by ttya, ttyb
    770  */
    771 struct consdev consdev_tty = {
    772 	nullcnprobe,
    773 	zscninit,
    774 	zscngetc,
    775 	zscnputc,
    776 	nullcnpollc,
    777 };
    778 
    779 static void
    780 zscninit(cn)
    781 	struct consdev *cn;
    782 {
    783 }
    784 
    785 /*
    786  * Polled console input putchar.
    787  */
    788 static int
    789 zscngetc(dev)
    790 	dev_t dev;
    791 {
    792 	return (zs_getc(zs_conschan));
    793 }
    794 
    795 /*
    796  * Polled console output putchar.
    797  */
    798 static void
    799 zscnputc(dev, c)
    800 	dev_t dev;
    801 	int c;
    802 {
    803 	zs_putc(zs_conschan, c);
    804 }
    805 
    806 /*****************************************************************/
    807 
    808 static void prom_cninit __P((struct consdev *));
    809 static int  prom_cngetc __P((dev_t));
    810 static void prom_cnputc __P((dev_t, int));
    811 
    812 /*
    813  * The console is set to this one initially,
    814  * which lets us use the PROM until consinit()
    815  * is called to select a real console.
    816  */
    817 struct consdev consdev_prom = {
    818 	nullcnprobe,
    819 	prom_cninit,
    820 	prom_cngetc,
    821 	prom_cnputc,
    822 	nullcnpollc,
    823 };
    824 
    825 /*
    826  * The console table pointer is statically initialized
    827  * to point to the PROM (output only) table, so that
    828  * early calls to printf will work.
    829  */
    830 struct consdev *cn_tab = &consdev_prom;
    831 
    832 void
    833 nullcnprobe(cn)
    834 	struct consdev *cn;
    835 {
    836 }
    837 
    838 static void
    839 prom_cninit(cn)
    840 	struct consdev *cn;
    841 {
    842 }
    843 
    844 /*
    845  * PROM console input putchar.
    846  * (dummy - this is output only)
    847  */
    848 static int
    849 prom_cngetc(dev)
    850 	dev_t dev;
    851 {
    852 	return (0);
    853 }
    854 
    855 /*
    856  * PROM console output putchar.
    857  */
    858 static void
    859 prom_cnputc(dev, c)
    860 	dev_t dev;
    861 	int c;
    862 {
    863 	char c0 = (c & 0x7f);
    864 
    865 	if (promvec->pv_romvec_vers > 2)
    866 		(*promvec->pv_v2devops.v2_write)
    867 			(*promvec->pv_v2bootargs.v2_fd1, &c0, 1);
    868 	else
    869 		(*promvec->pv_putchar)(c);
    870 }
    871 
    872 /*****************************************************************/
    873 
    874 extern struct consdev consdev_kd;
    875 
    876 static char *prom_inSrc_name[] = {
    877 	"keyboard/display",
    878 	"ttya", "ttyb",
    879 	"ttyc", "ttyd" };
    880 
    881 /*
    882  * This function replaces sys/dev/cninit.c
    883  * Determine which device is the console using
    884  * the PROM "input source" and "output sink".
    885  */
    886 void
    887 consinit()
    888 {
    889 	struct zschan *zc;
    890 	struct consdev *cn;
    891 	int channel, zs_unit, zstty_unit;
    892 	int inSource, outSink;
    893 
    894 	if (promvec->pv_romvec_vers > 2) {
    895 		/* We need to probe the PROM device tree */
    896 		register int node,fd;
    897 		char buffer[128];
    898 		register struct nodeops *no;
    899 		register struct v2devops *op;
    900 		register char *cp;
    901 		extern int fbnode;
    902 
    903 		inSource = outSink = -1;
    904 		no = promvec->pv_nodeops;
    905 		op = &promvec->pv_v2devops;
    906 
    907 		node = findroot();
    908 		if (no->no_proplen(node, "stdin-path") >= sizeof(buffer)) {
    909 			printf("consinit: increase buffer size and recompile\n");
    910 			goto setup_output;
    911 		}
    912 		/* XXX: fix above */
    913 
    914 		no->no_getprop(node, "stdin-path",buffer);
    915 
    916 		/*
    917 		 * Open an "instance" of this device.
    918 		 * You'd think it would be appropriate to call v2_close()
    919 		 * on the handle when we're done with it. But that seems
    920 		 * to cause the device to shut down somehow; for the moment,
    921 		 * we simply leave it open...
    922 		 */
    923 		if ((fd = op->v2_open(buffer)) == 0 ||
    924 		     (node = op->v2_fd_phandle(fd)) == 0) {
    925 			printf("consinit: bogus stdin path %s.\n",buffer);
    926 			goto setup_output;
    927 		}
    928 		if (no->no_proplen(node,"keyboard") >= 0) {
    929 			inSource = PROMDEV_KBD;
    930 			goto setup_output;
    931 		}
    932 		if (strcmp(getpropstring(node,"device_type"),"serial") != 0) {
    933 			/* not a serial, not keyboard. what is it?!? */
    934 			inSource = -1;
    935 			goto setup_output;
    936 		}
    937 		/*
    938 		 * At this point we assume the device path is in the form
    939 		 *   ....device@x,y:a for ttya and ...device@x,y:b for ttyb.
    940 		 * If it isn't, we defer to the ROM
    941 		 */
    942 		cp = buffer;
    943 		while (*cp)
    944 		    cp++;
    945 		cp -= 2;
    946 #ifdef DEBUG
    947 		if (cp < buffer)
    948 		    panic("consinit: bad stdin path %s",buffer);
    949 #endif
    950 		/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
    951 		if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
    952 		    inSource = PROMDEV_TTYA + (cp[1] - 'a');
    953 		/* else use rom */
    954 setup_output:
    955 		node = findroot();
    956 		if (no->no_proplen(node, "stdout-path") >= sizeof(buffer)) {
    957 			printf("consinit: increase buffer size and recompile\n");
    958 			goto setup_console;
    959 		}
    960 		/* XXX: fix above */
    961 
    962 		no->no_getprop(node, "stdout-path", buffer);
    963 
    964 		if ((fd = op->v2_open(buffer)) == 0 ||
    965 		     (node = op->v2_fd_phandle(fd)) == 0) {
    966 			printf("consinit: bogus stdout path %s.\n",buffer);
    967 			goto setup_output;
    968 		}
    969 		if (strcmp(getpropstring(node,"device_type"),"display") == 0) {
    970 			/* frame buffer output */
    971 			outSink = PROMDEV_SCREEN;
    972 			fbnode = node;
    973 		} else if (strcmp(getpropstring(node,"device_type"), "serial")
    974 			   != 0) {
    975 			/* not screen, not serial. Whatzit? */
    976 			outSink = -1;
    977 		} else { /* serial console. which? */
    978 			/*
    979 			 * At this point we assume the device path is in the
    980 			 * form:
    981 			 * ....device@x,y:a for ttya, etc.
    982 			 * If it isn't, we defer to the ROM
    983 			 */
    984 			cp = buffer;
    985 			while (*cp)
    986 			    cp++;
    987 			cp -= 2;
    988 #ifdef DEBUG
    989 			if (cp < buffer)
    990 				panic("consinit: bad stdout path %s",buffer);
    991 #endif
    992 			/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
    993 			if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
    994 			    outSink = PROMDEV_TTYA + (cp[1] - 'a');
    995 			else outSink = -1;
    996 		}
    997 	} else {
    998 		inSource = *promvec->pv_stdin;
    999 		outSink  = *promvec->pv_stdout;
   1000 	}
   1001 
   1002 setup_console:
   1003 
   1004 	if (inSource != outSink) {
   1005 		printf("cninit: mismatched PROM output selector\n");
   1006 	}
   1007 
   1008 	switch (inSource) {
   1009 	default:
   1010 		printf("cninit: invalid inSource=%d\n", inSource);
   1011 		callrom();
   1012 		inSource = PROMDEV_KBD;
   1013 		/* fall through */
   1014 
   1015 	case 0:	/* keyboard/display */
   1016 #if NKBD > 0
   1017 		zs_unit = 1;	/* XXX - config info! */
   1018 		channel = 0;
   1019 		cn = &consdev_kd;
   1020 		/* Set cn_dev, cn_pri in kd.c */
   1021 		break;
   1022 #else	/* NKBD */
   1023 		printf("cninit: kdb/display not configured\n");
   1024 		callrom();
   1025 		inSource = PROMDEV_TTYA;
   1026 		/* fall through */
   1027 #endif	/* NKBD */
   1028 
   1029 	case PROMDEV_TTYA:
   1030 	case PROMDEV_TTYB:
   1031 		zstty_unit = inSource - PROMDEV_TTYA;
   1032 		zs_unit = 0;	/* XXX - config info! */
   1033 		channel = zstty_unit & 1;
   1034 		cn = &consdev_tty;
   1035 		cn->cn_dev = makedev(zs_major, zstty_unit);
   1036 		cn->cn_pri = CN_REMOTE;
   1037 		break;
   1038 
   1039 	}
   1040 	/* Now that inSource has been validated, print it. */
   1041 	printf("console is %s\n", prom_inSrc_name[inSource]);
   1042 
   1043 	zc = zs_get_chan_addr(zs_unit, channel);
   1044 	if (zc == NULL) {
   1045 		printf("cninit: zs not mapped.\n");
   1046 		return;
   1047 	}
   1048 	zs_conschan = zc;
   1049 	zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
   1050 	cn_tab = cn;
   1051 	(*cn->cn_init)(cn);
   1052 #ifdef	KGDB
   1053 	zs_kgdb_init();
   1054 #endif
   1055 }
   1056