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