Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.55
      1 /*	$NetBSD: zs.c,v 1.55 1998/01/12 20:24:02 thorpej 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 = 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 (!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 zs_read_csr(cs)
    590 	struct zs_chanstate *cs;
    591 {
    592 	register u_char val;
    593 
    594 	val = *cs->cs_reg_csr;
    595 	ZS_DELAY();
    596 	return val;
    597 }
    598 
    599 void  zs_write_csr(cs, val)
    600 	struct zs_chanstate *cs;
    601 	u_char val;
    602 {
    603 	*cs->cs_reg_csr = val;
    604 	ZS_DELAY();
    605 }
    606 
    607 u_char zs_read_data(cs)
    608 	struct zs_chanstate *cs;
    609 {
    610 	register u_char val;
    611 
    612 	val = *cs->cs_reg_data;
    613 	ZS_DELAY();
    614 	return val;
    615 }
    616 
    617 void  zs_write_data(cs, val)
    618 	struct zs_chanstate *cs;
    619 	u_char val;
    620 {
    621 	*cs->cs_reg_data = val;
    622 	ZS_DELAY();
    623 }
    624 
    625 /****************************************************************
    626  * Console support functions (Sun specific!)
    627  * Note: this code is allowed to know about the layout of
    628  * the chip registers, and uses that to keep things simple.
    629  * XXX - I think I like the mvme167 code better. -gwr
    630  ****************************************************************/
    631 
    632 extern void Debugger __P((void));
    633 void *zs_conschan;
    634 
    635 /*
    636  * Handle user request to enter kernel debugger.
    637  */
    638 void
    639 zs_abort(cs)
    640 	struct zs_chanstate *cs;
    641 {
    642 	register volatile struct zschan *zc = zs_conschan;
    643 	int rr0;
    644 
    645 	/* Wait for end of break to avoid PROM abort. */
    646 	/* XXX - Limit the wait? */
    647 	do {
    648 		rr0 = zc->zc_csr;
    649 		ZS_DELAY();
    650 	} while (rr0 & ZSRR0_BREAK);
    651 
    652 #if defined(KGDB)
    653 	zskgdb(cs);
    654 #elif defined(DDB)
    655 	Debugger();
    656 #else
    657 	printf("stopping on keyboard abort\n");
    658 	callrom();
    659 #endif
    660 }
    661 
    662 /*
    663  * Polled input char.
    664  */
    665 int
    666 zs_getc(arg)
    667 	void *arg;
    668 {
    669 	register volatile struct zschan *zc = arg;
    670 	register int s, c, rr0;
    671 
    672 	s = splhigh();
    673 	/* Wait for a character to arrive. */
    674 	do {
    675 		rr0 = zc->zc_csr;
    676 		ZS_DELAY();
    677 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    678 
    679 	c = zc->zc_data;
    680 	ZS_DELAY();
    681 	splx(s);
    682 
    683 	/*
    684 	 * This is used by the kd driver to read scan codes,
    685 	 * so don't translate '\r' ==> '\n' here...
    686 	 */
    687 	return (c);
    688 }
    689 
    690 /*
    691  * Polled output char.
    692  */
    693 void
    694 zs_putc(arg, c)
    695 	void *arg;
    696 	int c;
    697 {
    698 	register volatile struct zschan *zc = arg;
    699 	register int s, rr0;
    700 
    701 	s = splhigh();
    702 	/* Wait for transmitter to become ready. */
    703 	do {
    704 		rr0 = zc->zc_csr;
    705 		ZS_DELAY();
    706 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    707 
    708 	/*
    709 	 * If the transmitter was busy doing regular tty I/O (ZSWR1_TIE on),
    710 	 * defer our output until the transmit interrupt runs. We still
    711 	 * sync with TX_READY so we can get by with a single-char "queue".
    712 	 */
    713 	if (zs_conschanstate && (zs_conschanstate->cs_preg[1] & ZSWR1_TIE)) {
    714 		/*
    715 		 * If a previous held character has not yet gone out, we can
    716 		 * send it now;  zsxint() will field the interrupt for our
    717 		 * char, but doesn't care. We're running at sufficiently
    718 		 * high spl for this to work.
    719 		 */
    720 		if (zs_conschanstate->cs_heldchar != 0)
    721 			zc->zc_data = zs_conschanstate->cs_heldchar;
    722 		zs_conschanstate->cs_heldchar = c;
    723 		ZS_DELAY();
    724 		splx(s);
    725 		return;
    726 	}
    727 
    728 	zc->zc_data = c;
    729 	ZS_DELAY();
    730 	splx(s);
    731 }
    732 
    733 /*****************************************************************/
    734 
    735 static void zscninit __P((struct consdev *));
    736 static int  zscngetc __P((dev_t));
    737 static void zscnputc __P((dev_t, int));
    738 
    739 /*
    740  * Console table shared by ttya, ttyb
    741  */
    742 struct consdev consdev_tty = {
    743 	nullcnprobe,
    744 	zscninit,
    745 	zscngetc,
    746 	zscnputc,
    747 	nullcnpollc,
    748 };
    749 
    750 static void
    751 zscninit(cn)
    752 	struct consdev *cn;
    753 {
    754 }
    755 
    756 /*
    757  * Polled console input putchar.
    758  */
    759 static int
    760 zscngetc(dev)
    761 	dev_t dev;
    762 {
    763 	return (zs_getc(zs_conschan));
    764 }
    765 
    766 /*
    767  * Polled console output putchar.
    768  */
    769 static void
    770 zscnputc(dev, c)
    771 	dev_t dev;
    772 	int c;
    773 {
    774 	zs_putc(zs_conschan, c);
    775 }
    776 
    777 /*****************************************************************/
    778 
    779 static void prom_cninit __P((struct consdev *));
    780 static int  prom_cngetc __P((dev_t));
    781 static void prom_cnputc __P((dev_t, int));
    782 
    783 /*
    784  * The console is set to this one initially,
    785  * which lets us use the PROM until consinit()
    786  * is called to select a real console.
    787  */
    788 struct consdev consdev_prom = {
    789 	nullcnprobe,
    790 	prom_cninit,
    791 	prom_cngetc,
    792 	prom_cnputc,
    793 	nullcnpollc,
    794 };
    795 
    796 /*
    797  * The console table pointer is statically initialized
    798  * to point to the PROM (output only) table, so that
    799  * early calls to printf will work.
    800  */
    801 struct consdev *cn_tab = &consdev_prom;
    802 
    803 void
    804 nullcnprobe(cn)
    805 	struct consdev *cn;
    806 {
    807 }
    808 
    809 static void
    810 prom_cninit(cn)
    811 	struct consdev *cn;
    812 {
    813 }
    814 
    815 /*
    816  * PROM console input putchar.
    817  * (dummy - this is output only)
    818  */
    819 static int
    820 prom_cngetc(dev)
    821 	dev_t dev;
    822 {
    823 	return (0);
    824 }
    825 
    826 /*
    827  * PROM console output putchar.
    828  */
    829 static void
    830 prom_cnputc(dev, c)
    831 	dev_t dev;
    832 	int c;
    833 {
    834 	char c0 = (c & 0x7f);
    835 
    836 	if (promvec->pv_romvec_vers > 2)
    837 		(*promvec->pv_v2devops.v2_write)
    838 			(*promvec->pv_v2bootargs.v2_fd1, &c0, 1);
    839 	else
    840 		(*promvec->pv_putchar)(c);
    841 }
    842 
    843 /*****************************************************************/
    844 
    845 extern struct consdev consdev_kd;
    846 
    847 static char *prom_inSrc_name[] = {
    848 	"keyboard/display",
    849 	"ttya", "ttyb",
    850 	"ttyc", "ttyd" };
    851 
    852 /*
    853  * This function replaces sys/dev/cninit.c
    854  * Determine which device is the console using
    855  * the PROM "input source" and "output sink".
    856  */
    857 void
    858 consinit()
    859 {
    860 	struct zschan *zc;
    861 	struct consdev *cn;
    862 	int channel, zs_unit, zstty_unit;
    863 	int inSource, outSink;
    864 
    865 	if (promvec->pv_romvec_vers > 2) {
    866 		/* We need to probe the PROM device tree */
    867 		register int node,fd;
    868 		char buffer[128];
    869 		register struct nodeops *no;
    870 		register struct v2devops *op;
    871 		register char *cp;
    872 		extern int fbnode;
    873 
    874 		inSource = outSink = -1;
    875 		no = promvec->pv_nodeops;
    876 		op = &promvec->pv_v2devops;
    877 
    878 		node = findroot();
    879 		if (no->no_proplen(node, "stdin-path") >= sizeof(buffer)) {
    880 			printf("consinit: increase buffer size and recompile\n");
    881 			goto setup_output;
    882 		}
    883 		/* XXX: fix above */
    884 
    885 		no->no_getprop(node, "stdin-path",buffer);
    886 
    887 		/*
    888 		 * Open an "instance" of this device.
    889 		 * You'd think it would be appropriate to call v2_close()
    890 		 * on the handle when we're done with it. But that seems
    891 		 * to cause the device to shut down somehow; for the moment,
    892 		 * we simply leave it open...
    893 		 */
    894 		if ((fd = op->v2_open(buffer)) == 0 ||
    895 		     (node = op->v2_fd_phandle(fd)) == 0) {
    896 			printf("consinit: bogus stdin path %s.\n",buffer);
    897 			goto setup_output;
    898 		}
    899 		if (no->no_proplen(node,"keyboard") >= 0) {
    900 			inSource = PROMDEV_KBD;
    901 			goto setup_output;
    902 		}
    903 		if (strcmp(getpropstring(node,"device_type"),"serial") != 0) {
    904 			/* not a serial, not keyboard. what is it?!? */
    905 			inSource = -1;
    906 			goto setup_output;
    907 		}
    908 		/*
    909 		 * At this point we assume the device path is in the form
    910 		 *   ....device@x,y:a for ttya and ...device@x,y:b for ttyb.
    911 		 * If it isn't, we defer to the ROM
    912 		 */
    913 		cp = buffer;
    914 		while (*cp)
    915 		    cp++;
    916 		cp -= 2;
    917 #ifdef DEBUG
    918 		if (cp < buffer)
    919 		    panic("consinit: bad stdin path %s",buffer);
    920 #endif
    921 		/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
    922 		if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
    923 		    inSource = PROMDEV_TTYA + (cp[1] - 'a');
    924 		/* else use rom */
    925 setup_output:
    926 		node = findroot();
    927 		if (no->no_proplen(node, "stdout-path") >= sizeof(buffer)) {
    928 			printf("consinit: increase buffer size and recompile\n");
    929 			goto setup_console;
    930 		}
    931 		/* XXX: fix above */
    932 
    933 		no->no_getprop(node, "stdout-path", buffer);
    934 
    935 		if ((fd = op->v2_open(buffer)) == 0 ||
    936 		     (node = op->v2_fd_phandle(fd)) == 0) {
    937 			printf("consinit: bogus stdout path %s.\n",buffer);
    938 			goto setup_output;
    939 		}
    940 		if (strcmp(getpropstring(node,"device_type"),"display") == 0) {
    941 			/* frame buffer output */
    942 			outSink = PROMDEV_SCREEN;
    943 			fbnode = node;
    944 		} else if (strcmp(getpropstring(node,"device_type"), "serial")
    945 			   != 0) {
    946 			/* not screen, not serial. Whatzit? */
    947 			outSink = -1;
    948 		} else { /* serial console. which? */
    949 			/*
    950 			 * At this point we assume the device path is in the
    951 			 * form:
    952 			 * ....device@x,y:a for ttya, etc.
    953 			 * If it isn't, we defer to the ROM
    954 			 */
    955 			cp = buffer;
    956 			while (*cp)
    957 			    cp++;
    958 			cp -= 2;
    959 #ifdef DEBUG
    960 			if (cp < buffer)
    961 				panic("consinit: bad stdout path %s",buffer);
    962 #endif
    963 			/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
    964 			if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
    965 			    outSink = PROMDEV_TTYA + (cp[1] - 'a');
    966 			else outSink = -1;
    967 		}
    968 	} else {
    969 		inSource = *promvec->pv_stdin;
    970 		outSink  = *promvec->pv_stdout;
    971 	}
    972 
    973 setup_console:
    974 
    975 	if (inSource != outSink) {
    976 		printf("cninit: mismatched PROM output selector\n");
    977 	}
    978 
    979 	switch (inSource) {
    980 	default:
    981 		printf("cninit: invalid inSource=%d\n", inSource);
    982 		callrom();
    983 		inSource = PROMDEV_KBD;
    984 		/* fall through */
    985 
    986 	case 0:	/* keyboard/display */
    987 #if NKBD > 0
    988 		zs_unit = 1;	/* XXX - config info! */
    989 		channel = 0;
    990 		cn = &consdev_kd;
    991 		/* Set cn_dev, cn_pri in kd.c */
    992 		break;
    993 #else	/* NKBD */
    994 		printf("cninit: kdb/display not configured\n");
    995 		callrom();
    996 		inSource = PROMDEV_TTYA;
    997 		/* fall through */
    998 #endif	/* NKBD */
    999 
   1000 	case PROMDEV_TTYA:
   1001 	case PROMDEV_TTYB:
   1002 		zstty_unit = inSource - PROMDEV_TTYA;
   1003 		zs_unit = 0;	/* XXX - config info! */
   1004 		channel = zstty_unit & 1;
   1005 		cn = &consdev_tty;
   1006 		cn->cn_dev = makedev(zs_major, zstty_unit);
   1007 		cn->cn_pri = CN_REMOTE;
   1008 		zs_console_unit = zs_unit;
   1009 		zs_console_channel = channel;
   1010 		break;
   1011 
   1012 	}
   1013 	/* Now that inSource has been validated, print it. */
   1014 	printf("console is %s\n", prom_inSrc_name[inSource]);
   1015 
   1016 	zc = zs_get_chan_addr(zs_unit, channel);
   1017 	if (zc == NULL) {
   1018 		printf("cninit: zs not mapped.\n");
   1019 		return;
   1020 	}
   1021 	zs_conschan = zc;
   1022 	zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
   1023 	cn_tab = cn;
   1024 	(*cn->cn_init)(cn);
   1025 #ifdef	KGDB
   1026 	zs_kgdb_init();
   1027 #endif
   1028 }
   1029