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