Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.52
      1 /*	$NetBSD: zs.c,v 1.52 1997/11/02 08:05:11 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 __P((struct device *, struct cfdata *, void *));
    188 static void	zs_attach __P((struct device *, struct device *, void *));
    189 static int  zs_print __P((void *, const char *name));
    190 
    191 struct cfattach zs_ca = {
    192 	sizeof(struct zsc_softc), zs_match, zs_attach
    193 };
    194 
    195 struct cfdriver zs_cd = {
    196 	NULL, "zs", DV_DULL
    197 };
    198 
    199 /* Interrupt handlers. */
    200 static int zshard __P((void *));
    201 static int zssoft __P((void *));
    202 static struct intrhand levelhard = { zshard };
    203 static struct intrhand levelsoft = { zssoft };
    204 
    205 static int zs_get_speed __P((struct zs_chanstate *));
    206 
    207 
    208 /*
    209  * Is the zs chip present?
    210  */
    211 static int
    212 zs_match(parent, cf, aux)
    213 	struct device *parent;
    214 	struct cfdata *cf;
    215 	void *aux;
    216 {
    217 	struct confargs *ca = aux;
    218 	struct romaux *ra = &ca->ca_ra;
    219 
    220 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
    221 		return (0);
    222 	if ((ca->ca_bustype == BUS_MAIN && !CPU_ISSUN4) ||
    223 	    (ca->ca_bustype == BUS_OBIO && CPU_ISSUN4M))
    224 		return (getpropint(ra->ra_node, "slave", -2) == cf->cf_unit);
    225 	ra->ra_len = NBPG;
    226 	return (probeget(ra->ra_vaddr, 1) != -1);
    227 }
    228 
    229 /*
    230  * Attach a found zs.
    231  *
    232  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    233  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    234  */
    235 static void
    236 zs_attach(parent, self, aux)
    237 	struct device *parent;
    238 	struct device *self;
    239 	void *aux;
    240 {
    241 	struct zsc_softc *zsc = (void *) self;
    242 	struct confargs *ca = aux;
    243 	struct romaux *ra = &ca->ca_ra;
    244 	struct zsc_attach_args zsc_args;
    245 	volatile struct zschan *zc;
    246 	struct zs_chanstate *cs;
    247 	int pri, s, zs_unit, channel;
    248 	static int didintr, prevpri;
    249 
    250 	zs_unit = zsc->zsc_dev.dv_unit;
    251 
    252 	/* Use the mapping setup by the Sun PROM. */
    253 	if (zsaddr[zs_unit] == NULL)
    254 		zsaddr[zs_unit] = findzs(zs_unit);
    255 
    256 	if (ca->ca_bustype==BUS_MAIN)
    257 		if ((void*)zsaddr[zs_unit] != ra->ra_vaddr)
    258 			panic("zsattach");
    259 	if (ra->ra_nintr != 1) {
    260 		printf(": expected 1 interrupt, got %d\n", ra->ra_nintr);
    261 		return;
    262 	}
    263 	pri = ra->ra_intr[0].int_pri;
    264 	printf(" pri %d, softpri %d\n", pri, PIL_TTY);
    265 
    266 	/*
    267 	 * Initialize software state for each channel.
    268 	 */
    269 	for (channel = 0; channel < 2; channel++) {
    270 		zsc_args.channel = channel;
    271 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    272 		cs = &zsc->zsc_cs_store[channel];
    273 		zsc->zsc_cs[channel] = cs;
    274 
    275 		cs->cs_channel = channel;
    276 		cs->cs_private = NULL;
    277 		cs->cs_ops = &zsops_null;
    278 		cs->cs_brg_clk = PCLK / 16;
    279 
    280 		zc = zs_get_chan_addr(zs_unit, channel);
    281 		cs->cs_reg_csr  = &zc->zc_csr;
    282 		cs->cs_reg_data = &zc->zc_data;
    283 
    284 		bcopy(zs_init_reg, cs->cs_creg, 16);
    285 		bcopy(zs_init_reg, cs->cs_preg, 16);
    286 
    287 		/* XXX: Get these from the PROM properties! */
    288 		/* XXX: See the mvme167 code.  Better. */
    289 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    290 			cs->cs_defspeed = zs_get_speed(cs);
    291 		else
    292 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
    293 		cs->cs_defcflag = zs_def_cflag;
    294 
    295 		/* Make these correspond to cs_defcflag (-crtscts) */
    296 		cs->cs_rr0_dcd = ZSRR0_DCD;
    297 		cs->cs_rr0_cts = 0;
    298 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    299 		cs->cs_wr5_rts = 0;
    300 
    301 		/*
    302 		 * Clear the master interrupt enable.
    303 		 * The INTENA is common to both channels,
    304 		 * so just do it on the A channel.
    305 		 */
    306 		if (channel == 0) {
    307 			zs_write_reg(cs, 9, 0);
    308 		}
    309 
    310 		/*
    311 		 * Look for a child driver for this channel.
    312 		 * The child attach will setup the hardware.
    313 		 */
    314 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
    315 			/* No sub-driver.  Just reset it. */
    316 			u_char reset = (channel == 0) ?
    317 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    318 			s = splhigh();
    319 			zs_write_reg(cs,  9, reset);
    320 			splx(s);
    321 		}
    322 	}
    323 
    324 	/*
    325 	 * Now safe to install interrupt handlers.  Note the arguments
    326 	 * to the interrupt handlers aren't used.  Note, we only do this
    327 	 * once since both SCCs interrupt at the same level and vector.
    328 	 */
    329 	if (!didintr) {
    330 		didintr = 1;
    331 		prevpri = pri;
    332 		intr_establish(pri, &levelhard);
    333 		intr_establish(PIL_TTY, &levelsoft);
    334 	} else if (pri != prevpri)
    335 		panic("broken zs interrupt scheme");
    336 	evcnt_attach(&zsc->zsc_dev, "intr", &zsc->zsc_intrcnt);
    337 
    338 	/*
    339 	 * Set the master interrupt enable and interrupt vector.
    340 	 * (common to both channels, do it on A)
    341 	 */
    342 	cs = zsc->zsc_cs[0];
    343 	s = splhigh();
    344 	/* interrupt vector */
    345 	zs_write_reg(cs, 2, zs_init_reg[2]);
    346 	/* master interrupt control (enable) */
    347 	zs_write_reg(cs, 9, zs_init_reg[9]);
    348 	splx(s);
    349 
    350 #if 0
    351 	/*
    352 	 * XXX: L1A hack - We would like to be able to break into
    353 	 * the debugger during the rest of autoconfiguration, so
    354 	 * lower interrupts just enough to let zs interrupts in.
    355 	 * This is done after both zs devices are attached.
    356 	 */
    357 	if (zs_unit == 1) {
    358 		printf("zs1: enabling zs interrupts\n");
    359 		(void)splfd(); /* XXX: splzs - 1 */
    360 	}
    361 #endif
    362 }
    363 
    364 static int
    365 zs_print(aux, name)
    366 	void *aux;
    367 	const char *name;
    368 {
    369 	struct zsc_attach_args *args = aux;
    370 
    371 	if (name != NULL)
    372 		printf("%s: ", name);
    373 
    374 	if (args->channel != -1)
    375 		printf(" channel %d", args->channel);
    376 
    377 	return UNCONF;
    378 }
    379 
    380 static volatile int zssoftpending;
    381 
    382 /*
    383  * Our ZS chips all share a common, autovectored interrupt,
    384  * so we have to look at all of them on each interrupt.
    385  */
    386 static int
    387 zshard(arg)
    388 	void *arg;
    389 {
    390 	register struct zsc_softc *zsc;
    391 	register int unit, rr3, rval, softreq;
    392 
    393 	rval = softreq = 0;
    394 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    395 		zsc = zs_cd.cd_devs[unit];
    396 		if (zsc == NULL)
    397 			continue;
    398 		rr3 = zsc_intr_hard(zsc);
    399 		/* Count up the interrupts. */
    400 		if (rr3) {
    401 			rval |= rr3;
    402 			zsc->zsc_intrcnt.ev_count++;
    403 		}
    404 		softreq |= zsc->zsc_cs[0]->cs_softreq;
    405 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    406 	}
    407 
    408 	/* We are at splzs here, so no need to lock. */
    409 	if (softreq && (zssoftpending == 0)) {
    410 		zssoftpending = IE_ZSSOFT;
    411 #if defined(SUN4M)
    412 		if (CPU_ISSUN4M)
    413 			raise(0, PIL_TTY);
    414 		else
    415 #endif
    416 		ienab_bis(IE_ZSSOFT);
    417 	}
    418 	return (rval);
    419 }
    420 
    421 /*
    422  * Similar scheme as for zshard (look at all of them)
    423  */
    424 static int
    425 zssoft(arg)
    426 	void *arg;
    427 {
    428 	register struct zsc_softc *zsc;
    429 	register int s, unit;
    430 
    431 	/* This is not the only ISR on this IPL. */
    432 	if (zssoftpending == 0)
    433 		return (0);
    434 
    435 	/*
    436 	 * The soft intr. bit will be set by zshard only if
    437 	 * the variable zssoftpending is zero.  The order of
    438 	 * these next two statements prevents our clearing
    439 	 * the soft intr bit just after zshard has set it.
    440 	 */
    441 	/* ienab_bic(IE_ZSSOFT); */
    442 	zssoftpending = 0;
    443 
    444 	/* Make sure we call the tty layer at spltty. */
    445 	s = spltty();
    446 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    447 		zsc = zs_cd.cd_devs[unit];
    448 		if (zsc == NULL)
    449 			continue;
    450 		(void) zsc_intr_soft(zsc);
    451 	}
    452 	splx(s);
    453 	return (1);
    454 }
    455 
    456 
    457 /*
    458  * Compute the current baud rate given a ZS channel.
    459  */
    460 static int
    461 zs_get_speed(cs)
    462 	struct zs_chanstate *cs;
    463 {
    464 	int tconst;
    465 
    466 	tconst = zs_read_reg(cs, 12);
    467 	tconst |= zs_read_reg(cs, 13) << 8;
    468 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    469 }
    470 
    471 /*
    472  * MD functions for setting the baud rate and control modes.
    473  */
    474 int
    475 zs_set_speed(cs, bps)
    476 	struct zs_chanstate *cs;
    477 	int bps;	/* bits per second */
    478 {
    479 	int tconst, real_bps;
    480 
    481 	if (bps == 0)
    482 		return (0);
    483 
    484 #ifdef	DIAGNOSTIC
    485 	if (cs->cs_brg_clk == 0)
    486 		panic("zs_set_speed");
    487 #endif
    488 
    489 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    490 	if (tconst < 0)
    491 		return (EINVAL);
    492 
    493 	/* Convert back to make sure we can do it. */
    494 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    495 
    496 	/* XXX - Allow some tolerance here? */
    497 	if (real_bps != bps)
    498 		return (EINVAL);
    499 
    500 	cs->cs_preg[12] = tconst;
    501 	cs->cs_preg[13] = tconst >> 8;
    502 
    503 	/* Caller will stuff the pending registers. */
    504 	return (0);
    505 }
    506 
    507 int
    508 zs_set_modes(cs, cflag)
    509 	struct zs_chanstate *cs;
    510 	int cflag;	/* bits per second */
    511 {
    512 	int s;
    513 
    514 	/*
    515 	 * Output hardware flow control on the chip is horrendous:
    516 	 * if carrier detect drops, the receiver is disabled, and if
    517 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    518 	 * Therefore, NEVER set the HFC bit, and instead use the
    519 	 * status interrupt to detect CTS changes.
    520 	 */
    521 	s = splzs();
    522 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
    523 		cs->cs_rr0_dcd = 0;
    524 	else
    525 		cs->cs_rr0_dcd = ZSRR0_DCD;
    526 	if ((cflag & CRTSCTS) != 0) {
    527 		cs->cs_wr5_dtr = ZSWR5_DTR;
    528 		cs->cs_wr5_rts = ZSWR5_RTS;
    529 		cs->cs_rr0_cts = ZSRR0_CTS;
    530 	} else if ((cflag & MDMBUF) != 0) {
    531 		cs->cs_wr5_dtr = 0;
    532 		cs->cs_wr5_rts = ZSWR5_DTR;
    533 		cs->cs_rr0_cts = ZSRR0_DCD;
    534 	} else {
    535 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    536 		cs->cs_wr5_rts = 0;
    537 		cs->cs_rr0_cts = 0;
    538 	}
    539 	splx(s);
    540 
    541 	/* Caller will stuff the pending registers. */
    542 	return (0);
    543 }
    544 
    545 
    546 /*
    547  * Read or write the chip with suitable delays.
    548  */
    549 
    550 u_char
    551 zs_read_reg(cs, reg)
    552 	struct zs_chanstate *cs;
    553 	u_char reg;
    554 {
    555 	u_char val;
    556 
    557 	*cs->cs_reg_csr = reg;
    558 	ZS_DELAY();
    559 	val = *cs->cs_reg_csr;
    560 	ZS_DELAY();
    561 	return val;
    562 }
    563 
    564 void
    565 zs_write_reg(cs, reg, val)
    566 	struct zs_chanstate *cs;
    567 	u_char reg, val;
    568 {
    569 	*cs->cs_reg_csr = reg;
    570 	ZS_DELAY();
    571 	*cs->cs_reg_csr = val;
    572 	ZS_DELAY();
    573 }
    574 
    575 u_char zs_read_csr(cs)
    576 	struct zs_chanstate *cs;
    577 {
    578 	register u_char val;
    579 
    580 	val = *cs->cs_reg_csr;
    581 	ZS_DELAY();
    582 	return val;
    583 }
    584 
    585 void  zs_write_csr(cs, val)
    586 	struct zs_chanstate *cs;
    587 	u_char val;
    588 {
    589 	*cs->cs_reg_csr = val;
    590 	ZS_DELAY();
    591 }
    592 
    593 u_char zs_read_data(cs)
    594 	struct zs_chanstate *cs;
    595 {
    596 	register u_char val;
    597 
    598 	val = *cs->cs_reg_data;
    599 	ZS_DELAY();
    600 	return val;
    601 }
    602 
    603 void  zs_write_data(cs, val)
    604 	struct zs_chanstate *cs;
    605 	u_char val;
    606 {
    607 	*cs->cs_reg_data = val;
    608 	ZS_DELAY();
    609 }
    610 
    611 /****************************************************************
    612  * Console support functions (Sun specific!)
    613  * Note: this code is allowed to know about the layout of
    614  * the chip registers, and uses that to keep things simple.
    615  * XXX - I think I like the mvme167 code better. -gwr
    616  ****************************************************************/
    617 
    618 extern void Debugger __P((void));
    619 void *zs_conschan;
    620 
    621 /*
    622  * Handle user request to enter kernel debugger.
    623  */
    624 void
    625 zs_abort(cs)
    626 	struct zs_chanstate *cs;
    627 {
    628 	register volatile struct zschan *zc = zs_conschan;
    629 	int rr0;
    630 
    631 	/* Wait for end of break to avoid PROM abort. */
    632 	/* XXX - Limit the wait? */
    633 	do {
    634 		rr0 = zc->zc_csr;
    635 		ZS_DELAY();
    636 	} while (rr0 & ZSRR0_BREAK);
    637 
    638 #if defined(KGDB)
    639 	zskgdb(cs);
    640 #elif defined(DDB)
    641 	Debugger();
    642 #else
    643 	printf("stopping on keyboard abort\n");
    644 	callrom();
    645 #endif
    646 }
    647 
    648 /*
    649  * Polled input char.
    650  */
    651 int
    652 zs_getc(arg)
    653 	void *arg;
    654 {
    655 	register volatile struct zschan *zc = arg;
    656 	register int s, c, rr0;
    657 
    658 	s = splhigh();
    659 	/* Wait for a character to arrive. */
    660 	do {
    661 		rr0 = zc->zc_csr;
    662 		ZS_DELAY();
    663 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    664 
    665 	c = zc->zc_data;
    666 	ZS_DELAY();
    667 	splx(s);
    668 
    669 	/*
    670 	 * This is used by the kd driver to read scan codes,
    671 	 * so don't translate '\r' ==> '\n' here...
    672 	 */
    673 	return (c);
    674 }
    675 
    676 /*
    677  * Polled output char.
    678  */
    679 void
    680 zs_putc(arg, c)
    681 	void *arg;
    682 	int c;
    683 {
    684 	register volatile struct zschan *zc = arg;
    685 	register int s, rr0;
    686 
    687 	s = splhigh();
    688 	/* Wait for transmitter to become ready. */
    689 	do {
    690 		rr0 = zc->zc_csr;
    691 		ZS_DELAY();
    692 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    693 
    694 	zc->zc_data = c;
    695 	ZS_DELAY();
    696 	splx(s);
    697 }
    698 
    699 /*****************************************************************/
    700 
    701 static void zscninit __P((struct consdev *));
    702 static int  zscngetc __P((dev_t));
    703 static void zscnputc __P((dev_t, int));
    704 
    705 /*
    706  * Console table shared by ttya, ttyb
    707  */
    708 struct consdev consdev_tty = {
    709 	nullcnprobe,
    710 	zscninit,
    711 	zscngetc,
    712 	zscnputc,
    713 	nullcnpollc,
    714 };
    715 
    716 static void
    717 zscninit(cn)
    718 	struct consdev *cn;
    719 {
    720 }
    721 
    722 /*
    723  * Polled console input putchar.
    724  */
    725 static int
    726 zscngetc(dev)
    727 	dev_t dev;
    728 {
    729 	return (zs_getc(zs_conschan));
    730 }
    731 
    732 /*
    733  * Polled console output putchar.
    734  */
    735 static void
    736 zscnputc(dev, c)
    737 	dev_t dev;
    738 	int c;
    739 {
    740 	zs_putc(zs_conschan, c);
    741 }
    742 
    743 /*****************************************************************/
    744 
    745 static void prom_cninit __P((struct consdev *));
    746 static int  prom_cngetc __P((dev_t));
    747 static void prom_cnputc __P((dev_t, int));
    748 
    749 /*
    750  * The console is set to this one initially,
    751  * which lets us use the PROM until consinit()
    752  * is called to select a real console.
    753  */
    754 struct consdev consdev_prom = {
    755 	nullcnprobe,
    756 	prom_cninit,
    757 	prom_cngetc,
    758 	prom_cnputc,
    759 	nullcnpollc,
    760 };
    761 
    762 /*
    763  * The console table pointer is statically initialized
    764  * to point to the PROM (output only) table, so that
    765  * early calls to printf will work.
    766  */
    767 struct consdev *cn_tab = &consdev_prom;
    768 
    769 void
    770 nullcnprobe(cn)
    771 	struct consdev *cn;
    772 {
    773 }
    774 
    775 static void
    776 prom_cninit(cn)
    777 	struct consdev *cn;
    778 {
    779 }
    780 
    781 /*
    782  * PROM console input putchar.
    783  * (dummy - this is output only)
    784  */
    785 static int
    786 prom_cngetc(dev)
    787 	dev_t dev;
    788 {
    789 	return (0);
    790 }
    791 
    792 /*
    793  * PROM console output putchar.
    794  */
    795 static void
    796 prom_cnputc(dev, c)
    797 	dev_t dev;
    798 	int c;
    799 {
    800 	char c0 = (c & 0x7f);
    801 
    802 	if (promvec->pv_romvec_vers > 2)
    803 		(*promvec->pv_v2devops.v2_write)
    804 			(*promvec->pv_v2bootargs.v2_fd1, &c0, 1);
    805 	else
    806 		(*promvec->pv_putchar)(c);
    807 }
    808 
    809 /*****************************************************************/
    810 
    811 extern struct consdev consdev_kd;
    812 
    813 static char *prom_inSrc_name[] = {
    814 	"keyboard/display",
    815 	"ttya", "ttyb",
    816 	"ttyc", "ttyd" };
    817 
    818 /*
    819  * This function replaces sys/dev/cninit.c
    820  * Determine which device is the console using
    821  * the PROM "input source" and "output sink".
    822  */
    823 void
    824 consinit()
    825 {
    826 	struct zschan *zc;
    827 	struct consdev *cn;
    828 	int channel, zs_unit, zstty_unit;
    829 	int inSource, outSink;
    830 
    831 	if (promvec->pv_romvec_vers > 2) {
    832 		/* We need to probe the PROM device tree */
    833 		register int node,fd;
    834 		char buffer[128];
    835 		register struct nodeops *no;
    836 		register struct v2devops *op;
    837 		register char *cp;
    838 		extern int fbnode;
    839 
    840 		inSource = outSink = -1;
    841 		no = promvec->pv_nodeops;
    842 		op = &promvec->pv_v2devops;
    843 
    844 		node = findroot();
    845 		if (no->no_proplen(node, "stdin-path") >= sizeof(buffer)) {
    846 			printf("consinit: increase buffer size and recompile\n");
    847 			goto setup_output;
    848 		}
    849 		/* XXX: fix above */
    850 
    851 		no->no_getprop(node, "stdin-path",buffer);
    852 
    853 		/*
    854 		 * Open an "instance" of this device.
    855 		 * You'd think it would be appropriate to call v2_close()
    856 		 * on the handle when we're done with it. But that seems
    857 		 * to cause the device to shut down somehow; for the moment,
    858 		 * we simply leave it open...
    859 		 */
    860 		if ((fd = op->v2_open(buffer)) == 0 ||
    861 		     (node = op->v2_fd_phandle(fd)) == 0) {
    862 			printf("consinit: bogus stdin path %s.\n",buffer);
    863 			goto setup_output;
    864 		}
    865 		if (no->no_proplen(node,"keyboard") >= 0) {
    866 			inSource = PROMDEV_KBD;
    867 			goto setup_output;
    868 		}
    869 		if (strcmp(getpropstring(node,"device_type"),"serial") != 0) {
    870 			/* not a serial, not keyboard. what is it?!? */
    871 			inSource = -1;
    872 			goto setup_output;
    873 		}
    874 		/*
    875 		 * At this point we assume the device path is in the form
    876 		 *   ....device@x,y:a for ttya and ...device@x,y:b for ttyb.
    877 		 * If it isn't, we defer to the ROM
    878 		 */
    879 		cp = buffer;
    880 		while (*cp)
    881 		    cp++;
    882 		cp -= 2;
    883 #ifdef DEBUG
    884 		if (cp < buffer)
    885 		    panic("consinit: bad stdin path %s",buffer);
    886 #endif
    887 		/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
    888 		if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
    889 		    inSource = PROMDEV_TTYA + (cp[1] - 'a');
    890 		/* else use rom */
    891 setup_output:
    892 		node = findroot();
    893 		if (no->no_proplen(node, "stdout-path") >= sizeof(buffer)) {
    894 			printf("consinit: increase buffer size and recompile\n");
    895 			goto setup_console;
    896 		}
    897 		/* XXX: fix above */
    898 
    899 		no->no_getprop(node, "stdout-path", buffer);
    900 
    901 		if ((fd = op->v2_open(buffer)) == 0 ||
    902 		     (node = op->v2_fd_phandle(fd)) == 0) {
    903 			printf("consinit: bogus stdout path %s.\n",buffer);
    904 			goto setup_output;
    905 		}
    906 		if (strcmp(getpropstring(node,"device_type"),"display") == 0) {
    907 			/* frame buffer output */
    908 			outSink = PROMDEV_SCREEN;
    909 			fbnode = node;
    910 		} else if (strcmp(getpropstring(node,"device_type"), "serial")
    911 			   != 0) {
    912 			/* not screen, not serial. Whatzit? */
    913 			outSink = -1;
    914 		} else { /* serial console. which? */
    915 			/*
    916 			 * At this point we assume the device path is in the
    917 			 * form:
    918 			 * ....device@x,y:a for ttya, etc.
    919 			 * If it isn't, we defer to the ROM
    920 			 */
    921 			cp = buffer;
    922 			while (*cp)
    923 			    cp++;
    924 			cp -= 2;
    925 #ifdef DEBUG
    926 			if (cp < buffer)
    927 				panic("consinit: bad stdout path %s",buffer);
    928 #endif
    929 			/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
    930 			if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
    931 			    outSink = PROMDEV_TTYA + (cp[1] - 'a');
    932 			else outSink = -1;
    933 		}
    934 	} else {
    935 		inSource = *promvec->pv_stdin;
    936 		outSink  = *promvec->pv_stdout;
    937 	}
    938 
    939 setup_console:
    940 
    941 	if (inSource != outSink) {
    942 		printf("cninit: mismatched PROM output selector\n");
    943 	}
    944 
    945 	switch (inSource) {
    946 	default:
    947 		printf("cninit: invalid inSource=%d\n", inSource);
    948 		callrom();
    949 		inSource = PROMDEV_KBD;
    950 		/* fall through */
    951 
    952 	case 0:	/* keyboard/display */
    953 #if NKBD > 0
    954 		zs_unit = 1;	/* XXX - config info! */
    955 		channel = 0;
    956 		cn = &consdev_kd;
    957 		/* Set cn_dev, cn_pri in kd.c */
    958 		break;
    959 #else	/* NKBD */
    960 		printf("cninit: kdb/display not configured\n");
    961 		callrom();
    962 		inSource = PROMDEV_TTYA;
    963 		/* fall through */
    964 #endif	/* NKBD */
    965 
    966 	case PROMDEV_TTYA:
    967 	case PROMDEV_TTYB:
    968 		zstty_unit = inSource - PROMDEV_TTYA;
    969 		zs_unit = 0;	/* XXX - config info! */
    970 		channel = zstty_unit & 1;
    971 		cn = &consdev_tty;
    972 		cn->cn_dev = makedev(zs_major, zstty_unit);
    973 		cn->cn_pri = CN_REMOTE;
    974 		break;
    975 
    976 	}
    977 	/* Now that inSource has been validated, print it. */
    978 	printf("console is %s\n", prom_inSrc_name[inSource]);
    979 
    980 	zc = zs_get_chan_addr(zs_unit, channel);
    981 	if (zc == NULL) {
    982 		printf("cninit: zs not mapped.\n");
    983 		return;
    984 	}
    985 	zs_conschan = zc;
    986 	zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
    987 	cn_tab = cn;
    988 	(*cn->cn_init)(cn);
    989 #ifdef	KGDB
    990 	zs_kgdb_init();
    991 #endif
    992 }
    993