Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.43
      1 /*	$NetBSD: zs.c,v 1.43 1996/12/17 20:52:12 gwr 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 REGENTS OR CONTRIBUTORS BE
     30  * 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 <dev/cons.h>
     60 #include <dev/ic/z8530reg.h>
     61 #include <machine/z8530var.h>
     62 
     63 #include <machine/autoconf.h>
     64 #include <machine/cpu.h>
     65 #include <machine/obio.h>
     66 #include <machine/mon.h>
     67 
     68 #include "zs_cons.h"
     69 
     70 /*
     71  * XXX: Hard code this to make console init easier...
     72  */
     73 #define	NZSC	2		/* XXX */
     74 
     75 /*
     76  * Some warts needed by z8530tty.c -
     77  * The default parity REALLY needs to be the same as the PROM uses,
     78  * or you can not see messages done with printf during boot-up...
     79  */
     80 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     81 int zs_major = 12;
     82 
     83 /*
     84  * The Sun3 provides a 4.9152 MHz clock to the ZS chips.
     85  */
     86 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     87 
     88 /*
     89  * Define interrupt levels.
     90  */
     91 #define ZSHARD_PRI	6	/* Wired on the CPU board... */
     92 #define ZSSOFT_PRI	3	/* Want tty pri (4) but this is OK. */
     93 
     94 #define ZS_DELAY()			delay(2)
     95 
     96 /* The layout of this is hardware-dependent (padding, order). */
     97 struct zschan {
     98 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
     99 	u_char		zc_xxx0;
    100 	volatile u_char	zc_data;	/* data */
    101 	u_char		zc_xxx1;
    102 };
    103 struct zsdevice {
    104 	/* Yes, they are backwards. */
    105 	struct	zschan zs_chan_b;
    106 	struct	zschan zs_chan_a;
    107 };
    108 
    109 
    110 /* Default OBIO addresses. */
    111 static int zs_physaddr[NZSC] = { OBIO_KEYBD_MS, OBIO_ZS };
    112 
    113 /* Saved PROM mappings */
    114 static struct zsdevice *zsaddr[NZSC];	/* See zs_init() */
    115 
    116 /* Flags from cninit() */
    117 static int zs_hwflags[NZSC][2];
    118 
    119 /* Default speed for each channel */
    120 static int zs_defspeed[NZSC][2] = {
    121 	{ 1200, 	/* keyboard */
    122 	  1200 },	/* mouse */
    123 	{ 9600, 	/* ttya */
    124 	  9600 },	/* ttyb */
    125 };
    126 
    127 static u_char zs_init_reg[16] = {
    128 	0,	/* 0: CMD (reset, etc.) */
    129 	0,	/* 1: No interrupts yet. */
    130 	0x18 + ZSHARD_PRI,	/* IVECT */
    131 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    132 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    133 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    134 	0,	/* 6: TXSYNC/SYNCLO */
    135 	0,	/* 7: RXSYNC/SYNCHI */
    136 	0,	/* 8: alias for data port */
    137 	ZSWR9_MASTER_IE,
    138 	0,	/*10: Misc. TX/RX control bits */
    139 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    140 	14,	/*12: BAUDLO (default=9600) */
    141 	0,	/*13: BAUDHI (default=9600) */
    142 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    143 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
    144 };
    145 
    146 static struct zschan *
    147 zs_get_chan_addr __P((int zsc_unit, int channel));
    148 
    149 
    150 /* Find PROM mappings (for console support). */
    151 void
    152 zs_init()
    153 {
    154 	int i;
    155 
    156 	for (i = 0; i < NZSC; i++) {
    157 		zsaddr[i] = (struct zsdevice *)
    158 			obio_find_mapping(zs_physaddr[i], OBIO_ZS_SIZE);
    159 	}
    160 }
    161 
    162 static struct zschan *
    163 zs_get_chan_addr(zsc_unit, channel)
    164 	int zsc_unit, channel;
    165 {
    166 	struct zsdevice *addr;
    167 	struct zschan *zc;
    168 
    169 	if (zsc_unit >= NZSC)
    170 		return NULL;
    171 	addr = zsaddr[zsc_unit];
    172 	if (addr == NULL)
    173 		return NULL;
    174 	if (channel == 0) {
    175 		zc = &addr->zs_chan_a;
    176 	} else {
    177 		zc = &addr->zs_chan_b;
    178 	}
    179 	return (zc);
    180 }
    181 
    182 
    183 /****************************************************************
    184  * Autoconfig
    185  ****************************************************************/
    186 
    187 /* Definition of the driver for autoconfig. */
    188 static int	zsc_match __P((struct device *, struct cfdata *, void *));
    189 static void	zsc_attach __P((struct device *, struct device *, void *));
    190 static int  zsc_print __P((void *, const char *name));
    191 
    192 struct cfattach zsc_ca = {
    193 	sizeof(struct zsc_softc), zsc_match, zsc_attach
    194 };
    195 
    196 struct cfdriver zsc_cd = {
    197 	NULL, "zsc", DV_DULL
    198 };
    199 
    200 static int zshard __P((void *));
    201 static int zssoft __P((void *));
    202 static int zs_get_speed __P((struct zs_chanstate *));
    203 
    204 
    205 /*
    206  * Is the zs chip present?
    207  */
    208 static int
    209 zsc_match(parent, cf, aux)
    210 	struct device *parent;
    211 	struct cfdata *cf;
    212 	void *aux;
    213 {
    214 	struct confargs *ca = aux;
    215 	int unit;
    216 	void *va;
    217 
    218 	/* We have arrays sized with NZSC so validate. */
    219 	unit = cf->cf_unit;
    220 	if (unit < 0 || unit >= NZSC)
    221 		return (0);
    222 
    223 	/*
    224 	 * This driver only supports its wired-in mappings,
    225 	 * because the console support depends on those.
    226 	 */
    227 	if (ca->ca_paddr != zs_physaddr[unit])
    228 		return (0);
    229 
    230 	/* Make sure zs_init() found mappings. */
    231 	va = zsaddr[unit];
    232 	if (va == NULL)
    233 		return (0);
    234 
    235 	/* This returns -1 on a fault (bus error). */
    236 	if (peek_byte(va) == -1)
    237 		return (0);
    238 
    239 	/* Default interrupt priority (always splbio==2) */
    240 	if (ca->ca_intpri == -1)
    241 		ca->ca_intpri = ZSHARD_PRI;
    242 
    243 	return (1);
    244 }
    245 
    246 /*
    247  * Attach a found zs.
    248  *
    249  * Match slave number to zs unit number, so that misconfiguration will
    250  * not set up the keyboard as ttya, etc.
    251  */
    252 static void
    253 zsc_attach(parent, self, aux)
    254 	struct device *parent;
    255 	struct device *self;
    256 	void *aux;
    257 {
    258 	struct zsc_softc *zsc = (void *) self;
    259 	struct confargs *ca = aux;
    260 	struct zsc_attach_args zsc_args;
    261 	volatile struct zschan *zc;
    262 	struct zs_chanstate *cs;
    263 	int s, zsc_unit, channel;
    264 	static int didintr;
    265 
    266 	zsc_unit = zsc->zsc_dev.dv_unit;
    267 
    268 	printf(": (softpri %d)\n", ZSSOFT_PRI);
    269 
    270 	/* Use the mapping setup by the Sun PROM. */
    271 	if (zsaddr[zsc_unit] == NULL)
    272 		panic("zs_attach: zs%d not mapped\n", zsc_unit);
    273 
    274 	/*
    275 	 * Initialize software state for each channel.
    276 	 */
    277 	for (channel = 0; channel < 2; channel++) {
    278 		zsc_args.channel = channel;
    279 		zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
    280 		cs = &zsc->zsc_cs_store[channel];
    281 		zsc->zsc_cs[channel] = cs;
    282 
    283 		cs->cs_channel = channel;
    284 		cs->cs_private = NULL;
    285 		cs->cs_ops = &zsops_null;
    286 		cs->cs_brg_clk = PCLK / 16;
    287 
    288 		zc = zs_get_chan_addr(zsc_unit, channel);
    289 		cs->cs_reg_csr  = &zc->zc_csr;
    290 		cs->cs_reg_data = &zc->zc_data;
    291 
    292 		bcopy(zs_init_reg, cs->cs_creg, 16);
    293 		bcopy(zs_init_reg, cs->cs_preg, 16);
    294 
    295 		/* XXX: Get these from the EEPROM instead? */
    296 		/* XXX: See the mvme167 code.  Better. */
    297 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    298 			cs->cs_defspeed = zs_get_speed(cs);
    299 		else
    300 			cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
    301 		cs->cs_defcflag = zs_def_cflag;
    302 
    303 		/*
    304 		 * Clear the master interrupt enable.
    305 		 * The INTENA is common to both channels,
    306 		 * so just do it on the A channel.
    307 		 */
    308 		if (channel == 0) {
    309 			zs_write_reg(cs, 9, 0);
    310 		}
    311 
    312 		/*
    313 		 * Look for a child driver for this channel.
    314 		 * The child attach will setup the hardware.
    315 		 */
    316 		if (!config_found(self, (void *)&zsc_args, zsc_print)) {
    317 			/* No sub-driver.  Just reset it. */
    318 			u_char reset = (channel == 0) ?
    319 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    320 			s = splzs();
    321 			zs_write_reg(cs,  9, reset);
    322 			splx(s);
    323 		}
    324 	}
    325 
    326 	/*
    327 	 * Now safe to install interrupt handlers.  Note the arguments
    328 	 * to the interrupt handlers aren't used.  Note, we only do this
    329 	 * once since both SCCs interrupt at the same level and vector.
    330 	 */
    331 	if (!didintr) {
    332 		didintr = 1;
    333 		isr_add_autovect(zssoft, NULL, ZSSOFT_PRI);
    334 		isr_add_autovect(zshard, NULL, ca->ca_intpri);
    335 	}
    336 
    337 	/*
    338 	 * Set the master interrupt enable and interrupt vector.
    339 	 * (common to both channels, do it on A)
    340 	 */
    341 	cs = zsc->zsc_cs[0];
    342 	s = splzs();
    343 	/* interrupt vector */
    344 	zs_write_reg(cs, 2, zs_init_reg[2]);
    345 	/* master interrupt control (enable) */
    346 	zs_write_reg(cs, 9, zs_init_reg[9]);
    347 	splx(s);
    348 }
    349 
    350 static int
    351 zsc_print(aux, name)
    352 	void *aux;
    353 	const char *name;
    354 {
    355 	struct zsc_attach_args *args = aux;
    356 
    357 	if (name != NULL)
    358 		printf("%s: ", name);
    359 
    360 	if (args->channel != -1)
    361 		printf(" channel %d", args->channel);
    362 
    363 	return UNCONF;
    364 }
    365 
    366 static int zssoftpending;
    367 
    368 /*
    369  * Our ZS chips all share a common, autovectored interrupt,
    370  * so we have to look at all of them on each interrupt.
    371  */
    372 static int
    373 zshard(arg)
    374 	void *arg;
    375 {
    376 	register struct zsc_softc *zsc;
    377 	register int unit, rval;
    378 
    379 	/* Do ttya/ttyb first, because they go faster. */
    380 	rval = 0;
    381 	unit = zsc_cd.cd_ndevs;
    382 	while (--unit >= 0) {
    383 		zsc = zsc_cd.cd_devs[unit];
    384 		if (zsc == NULL)
    385 			continue;
    386 		rval |= zsc_intr_hard(zsc);
    387 		if ((zsc->zsc_cs[0]->cs_softreq) ||
    388 			(zsc->zsc_cs[1]->cs_softreq))
    389 		{
    390 			/* zsc_req_softint(zsc); */
    391 			/* We are at splzs here, so no need to lock. */
    392 			if (zssoftpending == 0) {
    393 				zssoftpending = ZSSOFT_PRI;
    394 				isr_soft_request(ZSSOFT_PRI);
    395 			}
    396 		}
    397 	}
    398 	return (rval);
    399 }
    400 
    401 /*
    402  * Similar scheme as for zshard (look at all of them)
    403  */
    404 static int
    405 zssoft(arg)
    406 	void *arg;
    407 {
    408 	register struct zsc_softc *zsc;
    409 	register int unit;
    410 
    411 	/* This is not the only ISR on this IPL. */
    412 	if (zssoftpending == 0)
    413 		return (0);
    414 
    415 	/*
    416 	 * The soft intr. bit will be set by zshard only if
    417 	 * the variable zssoftpending is zero.  The order of
    418 	 * these next two statements prevents our clearing
    419 	 * the soft intr bit just after zshard has set it.
    420 	 */
    421 	isr_soft_clear(ZSSOFT_PRI);
    422 	zssoftpending = 0;
    423 
    424 	/* Do ttya/ttyb first, because they go faster. */
    425 	unit = zsc_cd.cd_ndevs;
    426 	while (--unit >= 0) {
    427 		zsc = zsc_cd.cd_devs[unit];
    428 		if (zsc == NULL)
    429 			continue;
    430 		(void) zsc_intr_soft(zsc);
    431 	}
    432 	return (1);
    433 }
    434 
    435 
    436 /*
    437  * Compute the current baud rate given a ZSCC channel.
    438  */
    439 static int
    440 zs_get_speed(cs)
    441 	struct zs_chanstate *cs;
    442 {
    443 	int tconst;
    444 
    445 	tconst = zs_read_reg(cs, 12);
    446 	tconst |= zs_read_reg(cs, 13) << 8;
    447 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    448 }
    449 
    450 /*
    451  * MD functions for setting the baud rate and control modes.
    452  */
    453 int
    454 zs_set_speed(cs, bps)
    455 	struct zs_chanstate *cs;
    456 	int bps;	/* bits per second */
    457 {
    458 	int tconst, real_bps;
    459 
    460 	if (bps == 0)
    461 		return (0);
    462 
    463 #ifdef	DIAGNOSTIC
    464 	if (cs->cs_brg_clk == 0)
    465 		panic("zs_set_speed");
    466 #endif
    467 
    468 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    469 	if (tconst < 0)
    470 		return (EINVAL);
    471 
    472 	/* Convert back to make sure we can do it. */
    473 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    474 
    475 	/* XXX - Allow some tolerance here? */
    476 	if (real_bps != bps)
    477 		return (EINVAL);
    478 
    479 	cs->cs_preg[12] = tconst;
    480 	cs->cs_preg[13] = tconst >> 8;
    481 
    482 	/* Caller will stuff the pending registers. */
    483 	return (0);
    484 }
    485 
    486 int
    487 zs_set_modes(cs, cflag)
    488 	struct zs_chanstate *cs;
    489 	int cflag;	/* bits per second */
    490 {
    491 	int s;
    492 
    493 	/*
    494 	 * Output hardware flow control on the chip is horrendous:
    495 	 * if carrier detect drops, the receiver is disabled, and if
    496 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    497 	 * Therefore, NEVER set the HFC bit, and instead use the
    498 	 * status interrupt to detect CTS changes.
    499 	 */
    500 	s = splzs();
    501 	if (cflag & CLOCAL) {
    502 		cs->cs_rr0_dcd = 0;
    503 		cs->cs_preg[15] &= ~ZSWR15_DCD_IE;
    504 	} else {
    505 		cs->cs_rr0_dcd = ZSRR0_DCD;
    506 		cs->cs_preg[15] |= ZSWR15_DCD_IE;
    507 	}
    508 	if (cflag & CRTSCTS) {
    509 		cs->cs_wr5_dtr = ZSWR5_DTR;
    510 		cs->cs_wr5_rts = ZSWR5_RTS;
    511 		cs->cs_rr0_cts = ZSRR0_CTS;
    512 		cs->cs_preg[15] |= ZSWR15_CTS_IE;
    513 	} else {
    514 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    515 		cs->cs_wr5_rts = 0;
    516 		cs->cs_rr0_cts = 0;
    517 		cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
    518 	}
    519 	splx(s);
    520 
    521 	/* Caller will stuff the pending registers. */
    522 	return (0);
    523 }
    524 
    525 
    526 /*
    527  * Read or write the chip with suitable delays.
    528  */
    529 
    530 u_char
    531 zs_read_reg(cs, reg)
    532 	struct zs_chanstate *cs;
    533 	u_char reg;
    534 {
    535 	u_char val;
    536 
    537 	*cs->cs_reg_csr = reg;
    538 	ZS_DELAY();
    539 	val = *cs->cs_reg_csr;
    540 	ZS_DELAY();
    541 	return val;
    542 }
    543 
    544 void
    545 zs_write_reg(cs, reg, val)
    546 	struct zs_chanstate *cs;
    547 	u_char reg, val;
    548 {
    549 	*cs->cs_reg_csr = reg;
    550 	ZS_DELAY();
    551 	*cs->cs_reg_csr = val;
    552 	ZS_DELAY();
    553 }
    554 
    555 u_char zs_read_csr(cs)
    556 	struct zs_chanstate *cs;
    557 {
    558 	register u_char val;
    559 
    560 	val = *cs->cs_reg_csr;
    561 	ZS_DELAY();
    562 	return val;
    563 }
    564 
    565 void  zs_write_csr(cs, val)
    566 	struct zs_chanstate *cs;
    567 	u_char val;
    568 {
    569 	*cs->cs_reg_csr = val;
    570 	ZS_DELAY();
    571 }
    572 
    573 u_char zs_read_data(cs)
    574 	struct zs_chanstate *cs;
    575 {
    576 	register u_char val;
    577 
    578 	val = *cs->cs_reg_data;
    579 	ZS_DELAY();
    580 	return val;
    581 }
    582 
    583 void  zs_write_data(cs, val)
    584 	struct zs_chanstate *cs;
    585 	u_char val;
    586 {
    587 	*cs->cs_reg_data = val;
    588 	ZS_DELAY();
    589 }
    590 
    591 /****************************************************************
    592  * Console support functions (Sun3 specific!)
    593  * Note: this code is allowed to know about the layout of
    594  * the chip registers, and uses that to keep things simple.
    595  * XXX - I think I like the mvme167 code better. -gwr
    596  ****************************************************************/
    597 
    598 void *zs_conschan;
    599 
    600 /*
    601  * Polled input char.
    602  */
    603 int
    604 zs_getc(arg)
    605 	void *arg;
    606 {
    607 	register volatile struct zschan *zc = arg;
    608 	register int s, c, rr0;
    609 
    610 	s = splhigh();
    611 	/* Wait for a character to arrive. */
    612 	do {
    613 		rr0 = zc->zc_csr;
    614 		ZS_DELAY();
    615 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    616 
    617 	c = zc->zc_data;
    618 	ZS_DELAY();
    619 	splx(s);
    620 
    621 	/*
    622 	 * This is used by the kd driver to read scan codes,
    623 	 * so don't translate '\r' ==> '\n' here...
    624 	 */
    625 	return (c);
    626 }
    627 
    628 /*
    629  * Polled output char.
    630  */
    631 void
    632 zs_putc(arg, c)
    633 	void *arg;
    634 	int c;
    635 {
    636 	register volatile struct zschan *zc = arg;
    637 	register int s, rr0;
    638 
    639 	s = splhigh();
    640 	/* Wait for transmitter to become ready. */
    641 	do {
    642 		rr0 = zc->zc_csr;
    643 		ZS_DELAY();
    644 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    645 
    646 	zc->zc_data = c;
    647 	ZS_DELAY();
    648 	splx(s);
    649 }
    650 
    651 extern struct consdev consdev_kd;	/* keyboard/display */
    652 extern struct consdev consdev_tty;
    653 extern struct consdev *cn_tab;	/* physical console device info */
    654 
    655 static int  zscngetc __P((dev_t));
    656 static void zscnputc __P((dev_t, int));
    657 static void zscninit __P((struct consdev *));
    658 
    659 static struct {
    660 	int zsc_unit, channel;
    661 } zstty_conf[NZSC*2] = {
    662 	/* XXX: knowledge from the config file here... */
    663 	{ 1, 0 },	/* ttya */
    664 	{ 1, 1 },	/* ttyb */
    665 	{ 0, 0 },	/* ttyc */
    666 	{ 0, 1 },	/* ttyd */
    667 };
    668 
    669 /*
    670  * This function replaces sys/dev/cninit.c
    671  * Determine which device is the console using
    672  * the PROM "input source" and "output sink".
    673  */
    674 void
    675 cninit()
    676 {
    677 	MachMonRomVector *v;
    678 	struct zschan *zc;
    679 	struct consdev *cn;
    680 	int channel, zsc_unit, zstty_unit;
    681 	u_char inSource;
    682 
    683 	v = romVectorPtr;
    684 	inSource = *(v->inSource);
    685 
    686 	if (inSource != *(v->outSink)) {
    687 		mon_printf("cninit: mismatched PROM output selector\n");
    688 	}
    689 
    690 	switch (inSource) {
    691 
    692 	case 1:	/* ttya */
    693 	case 2:	/* ttyb */
    694 	case 3:	/* ttyc (rewired keyboard connector) */
    695 	case 4:	/* ttyd (rewired mouse connector)   */
    696 		zstty_unit = inSource - 1;
    697 		zsc_unit = zstty_conf[zstty_unit].zsc_unit;
    698 		channel  = zstty_conf[zstty_unit].channel;
    699 		cn = &consdev_tty;
    700 		cn->cn_dev = makedev(zs_major, zstty_unit);
    701 		cn->cn_pri = CN_REMOTE;
    702 		break;
    703 
    704 	default:
    705 		mon_printf("cninit: invalid PROM console selector\n");
    706 		/* assume keyboard/display */
    707 		/* fallthrough */
    708 	case 0:	/* keyboard/display */
    709 		zsc_unit = 0;
    710 		channel = 0;
    711 		cn = &consdev_kd;
    712 		/* Set cn_dev, cn_pri in kd.c */
    713 		break;
    714 	}
    715 
    716 	zc = zs_get_chan_addr(zsc_unit, channel);
    717 	if (zc == NULL) {
    718 		mon_printf("cninit: zs not mapped.\n");
    719 		return;
    720 	}
    721 	zs_conschan = zc;
    722 	zs_hwflags[zsc_unit][channel] = ZS_HWFLAG_CONSOLE;
    723 	cn_tab = cn;
    724 	(*cn->cn_init)(cn);
    725 }
    726 
    727 /* We never call this. */
    728 void
    729 nullcnprobe(cn)
    730 	struct consdev *cn;
    731 {
    732 }
    733 
    734 void
    735 zscninit(cn)
    736 	struct consdev *cn;
    737 {
    738 	int unit = minor(cn->cn_dev) & 1;
    739 
    740 	mon_printf("console is zstty%d (tty%c)\n",
    741 		   unit, unit + 'a');
    742 }
    743 
    744 /*
    745  * Polled console input putchar.
    746  */
    747 static int
    748 zscngetc(dev)
    749 	dev_t dev;
    750 {
    751 	register int c;
    752 
    753 	c = zs_getc(zs_conschan);
    754 	return (c);
    755 }
    756 
    757 /*
    758  * Polled console output putchar.
    759  */
    760 static void
    761 zscnputc(dev, c)
    762 	dev_t dev;
    763 	int c;
    764 {
    765 
    766 	zs_putc(zs_conschan, c);
    767 }
    768 
    769 
    770 struct consdev consdev_tty = {
    771 	nullcnprobe,
    772 	zscninit,
    773 	zscngetc,
    774 	zscnputc,
    775 	nullcnpollc,
    776 };
    777 
    778 
    779 /*
    780  * Handle user request to enter kernel debugger.
    781  */
    782 void
    783 zs_abort(cs)
    784 	struct zs_chanstate *cs;
    785 {
    786 	register volatile struct zschan *zc = zs_conschan;
    787 	int rr0;
    788 
    789 	/* Wait for end of break to avoid PROM abort. */
    790 	/* XXX - Limit the wait? */
    791 	do {
    792 		rr0 = zc->zc_csr;
    793 		ZS_DELAY();
    794 	} while (rr0 & ZSRR0_BREAK);
    795 
    796 	/* XXX - Always available, but may be the PROM monitor. */
    797 	Debugger();
    798 }
    799