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