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