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