Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.39
      1 /*	$NetBSD: zs.c,v 1.39 2002/09/27 02:24:25 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 "opt_ddb.h"
     48 #include "opt_kgdb.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/conf.h>
     53 #include <sys/device.h>
     54 #include <sys/file.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/kernel.h>
     57 #include <sys/proc.h>
     58 #include <sys/tty.h>
     59 #include <sys/time.h>
     60 #include <sys/syslog.h>
     61 
     62 #include <machine/autoconf.h>
     63 #include <machine/openfirm.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 #include <dev/sun/kbd_ms_ttyvar.h>
     72 #include <ddb/db_output.h>
     73 
     74 #include <sparc64/dev/cons.h>
     75 
     76 #include "kbd.h"	/* NKBD */
     77 #include "ms.h"		/* NMS */
     78 #include "zs.h" 	/* NZS */
     79 
     80 /* Make life easier for the initialized arrays here. */
     81 #if NZS < 3
     82 #undef  NZS
     83 #define NZS 3
     84 #endif
     85 
     86 /*
     87  * Some warts needed by z8530tty.c -
     88  * The default parity REALLY needs to be the same as the PROM uses,
     89  * or you can not see messages done with printf during boot-up...
     90  */
     91 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     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 #define	ZS_DELAY()
     99 
    100 /* The layout of this is hardware-dependent (padding, order). */
    101 struct zschan {
    102 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    103 	u_char		zc_xxx0;
    104 	volatile u_char	zc_data;	/* data */
    105 	u_char		zc_xxx1;
    106 };
    107 struct zsdevice {
    108 	/* Yes, they are backwards. */
    109 	struct	zschan zs_chan_b;
    110 	struct	zschan zs_chan_a;
    111 };
    112 
    113 /* ZS channel used as the console device (if any) */
    114 void *zs_conschan_get, *zs_conschan_put;
    115 
    116 /* Saved PROM mappings */
    117 static struct zsdevice *zsaddr[NZS];
    118 
    119 static u_char zs_init_reg[16] = {
    120 	0,	/* 0: CMD (reset, etc.) */
    121 	0,	/* 1: No interrupts yet. */
    122 	0,	/* 2: IVECT */
    123 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    124 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    125 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    126 	0,	/* 6: TXSYNC/SYNCLO */
    127 	0,	/* 7: RXSYNC/SYNCHI */
    128 	0,	/* 8: alias for data port */
    129 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
    130 	0,	/*10: Misc. TX/RX control bits */
    131 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    132 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    133 	0,			/*13: BAUDHI (default=9600) */
    134 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    135 	ZSWR15_BREAK_IE,
    136 };
    137 
    138 /* Console ops */
    139 static int  zscngetc __P((dev_t));
    140 static void zscnputc __P((dev_t, int));
    141 static void zscnpollc __P((dev_t, int));
    142 
    143 struct consdev zs_consdev = {
    144 	NULL,
    145 	NULL,
    146 	zscngetc,
    147 	zscnputc,
    148 	zscnpollc,
    149 	NULL,
    150 };
    151 
    152 
    153 /****************************************************************
    154  * Autoconfig
    155  ****************************************************************/
    156 
    157 /* Definition of the driver for autoconfig. */
    158 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    159 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    160 
    161 static void zs_attach __P((struct zsc_softc *, struct zsdevice *, int));
    162 static int  zs_print __P((void *, const char *name));
    163 
    164 /* Do we really need this ? */
    165 struct cfattach zs_ca = {
    166 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    167 };
    168 
    169 struct cfattach zs_mainbus_ca = {
    170 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    171 };
    172 
    173 extern struct cfdriver zs_cd;
    174 extern int stdinnode;
    175 extern int fbnode;
    176 
    177 /* Interrupt handlers. */
    178 int zscheckintr __P((void *));
    179 static int zshard __P((void *));
    180 static void zssoft __P((void *));
    181 
    182 static int zs_get_speed __P((struct zs_chanstate *));
    183 
    184 /* Console device support */
    185 static int zs_console_flags __P((int, int, int));
    186 
    187 /* Power management hooks */
    188 int  zs_enable __P((struct zs_chanstate *));
    189 void zs_disable __P((struct zs_chanstate *));
    190 
    191 /*
    192  * Is the zs chip present?
    193  */
    194 static int
    195 zs_match_mainbus(parent, cf, aux)
    196 	struct device *parent;
    197 	struct cfdata *cf;
    198 	void *aux;
    199 {
    200 	struct sbus_attach_args *sa = aux;
    201 
    202 	if (strcmp(cf->cf_name, sa->sa_name) != 0)
    203 		return (0);
    204 
    205 	return (1);
    206 }
    207 
    208 static void
    209 zs_attach_mainbus(parent, self, aux)
    210 	struct device *parent;
    211 	struct device *self;
    212 	void *aux;
    213 {
    214 	struct zsc_softc *zsc = (void *) self;
    215 	struct sbus_attach_args *sa = aux;
    216 	bus_space_handle_t bh;
    217 	int zs_unit = zsc->zsc_dev.dv_unit;
    218 
    219 	if (sa->sa_nintr == 0) {
    220 		printf(" no interrupt lines\n");
    221 		return;
    222 	}
    223 
    224 	/* Use the mapping setup by the Sun PROM if possible. */
    225 	if (zsaddr[zs_unit] == NULL) {
    226 		/* Only map registers once. */
    227 		if (sa->sa_npromvaddrs) {
    228 			/*
    229 			 * We're converting from a 32-bit pointer to a 64-bit
    230 			 * pointer.  Since the 32-bit entity is negative, but
    231 			 * the kernel is still mapped into the lower 4GB
    232 			 * range, this needs to be zero-extended.
    233 			 *
    234 			 * XXXXX If we map the kernel and devices into the
    235 			 * high 4GB range, this needs to be changed to
    236 			 * sign-extend the address.
    237 			 */
    238 			sparc_promaddr_to_handle(sa->sa_bustag,
    239 				sa->sa_promvaddrs[0], &bh);
    240 
    241 		} else {
    242 
    243 			if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    244 					 sa->sa_offset,
    245 					 sa->sa_size,
    246 					 BUS_SPACE_MAP_LINEAR,
    247 					 &bh) != 0) {
    248 				printf("%s @ sbus: cannot map registers\n",
    249 				       self->dv_xname);
    250 				return;
    251 			}
    252 		}
    253 		zsaddr[zs_unit] = (struct zsdevice *)
    254 			bus_space_vaddr(sa->sa_bustag, bh);
    255 	}
    256 	zsc->zsc_bustag = sa->sa_bustag;
    257 	zsc->zsc_dmatag = sa->sa_dmatag;
    258 	zsc->zsc_promunit = PROM_getpropint(sa->sa_node, "slave", -2);
    259 	zsc->zsc_node = sa->sa_node;
    260 	zs_attach(zsc, zsaddr[zs_unit], sa->sa_pri);
    261 }
    262 
    263 /*
    264  * Attach a found zs.
    265  *
    266  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    267  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    268  */
    269 static void
    270 zs_attach(zsc, zsd, pri)
    271 	struct zsc_softc *zsc;
    272 	struct zsdevice *zsd;
    273 	int pri;
    274 {
    275 	struct zsc_attach_args zsc_args;
    276 	struct zs_chanstate *cs;
    277 	int s, channel, softpri = PIL_TTY;
    278 
    279 	if (zsd == NULL) {
    280 		printf("configuration incomplete\n");
    281 		return;
    282 	}
    283 
    284 	printf(" softpri %d\n", softpri);
    285 
    286 	/*
    287 	 * Initialize software state for each channel.
    288 	 */
    289 	for (channel = 0; channel < 2; channel++) {
    290 		struct zschan *zc;
    291 		struct device *child;
    292 
    293 		zsc_args.channel = channel;
    294 		cs = &zsc->zsc_cs_store[channel];
    295 		zsc->zsc_cs[channel] = cs;
    296 
    297 		cs->cs_channel = channel;
    298 		cs->cs_private = NULL;
    299 		cs->cs_ops = &zsops_null;
    300 		cs->cs_brg_clk = PCLK / 16;
    301 
    302 		zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    303 
    304 		zsc_args.consdev = NULL;
    305 		zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit,
    306 						    zsc->zsc_node,
    307 						    channel);
    308 
    309 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
    310 			zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
    311 			zsc_args.consdev = &zs_consdev;
    312 		}
    313 
    314 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
    315 			zs_conschan_get = zc;
    316 		}
    317 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
    318 			zs_conschan_put = zc;
    319 		}
    320 
    321 		/* Children need to set cn_dev, etc */
    322 		cs->cs_reg_csr  = &zc->zc_csr;
    323 		cs->cs_reg_data = &zc->zc_data;
    324 
    325 		bcopy(zs_init_reg, cs->cs_creg, 16);
    326 		bcopy(zs_init_reg, cs->cs_preg, 16);
    327 
    328 		/* XXX: Consult PROM properties for this?! */
    329 		cs->cs_defspeed = zs_get_speed(cs);
    330 		cs->cs_defcflag = zs_def_cflag;
    331 
    332 		/* Make these correspond to cs_defcflag (-crtscts) */
    333 		cs->cs_rr0_dcd = ZSRR0_DCD;
    334 		cs->cs_rr0_cts = 0;
    335 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    336 		cs->cs_wr5_rts = 0;
    337 
    338 		/*
    339 		 * Clear the master interrupt enable.
    340 		 * The INTENA is common to both channels,
    341 		 * so just do it on the A channel.
    342 		 */
    343 		if (channel == 0) {
    344 			zs_write_reg(cs, 9, 0);
    345 		}
    346 
    347 		/*
    348 		 * Look for a child driver for this channel.
    349 		 * The child attach will setup the hardware.
    350 		 */
    351 		if (!(child =
    352 		      config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print))) {
    353 			/* No sub-driver.  Just reset it. */
    354 			u_char reset = (channel == 0) ?
    355 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    356 			s = splzs();
    357 			zs_write_reg(cs,  9, reset);
    358 			splx(s);
    359 		}
    360 #if (NKBD > 0) || (NMS > 0)
    361 		/*
    362 		 * If this was a zstty it has a keyboard
    363 		 * property on it we need to attach the
    364 		 * sunkbd and sunms line disciplines.
    365 		 */
    366 		if (child
    367 		    && (!strcmp(child->dv_cfdata->cf_name, "zstty"))
    368 		    && (PROM_getproplen(zsc->zsc_node, "keyboard") == 0)) {
    369 			struct kbd_ms_tty_attach_args kma;
    370 			struct zstty_softc {
    371 				/* The following are the only fields we need here */
    372 				struct	device zst_dev;
    373 				struct  tty *zst_tty;
    374 				struct	zs_chanstate *zst_cs;
    375 			} *zst = (struct zstty_softc *)child;
    376 			struct tty *tp;
    377 
    378 			kma.kmta_tp = tp = zst->zst_tty;
    379 			kma.kmta_dev = tp->t_dev;
    380 			kma.kmta_consdev = zsc_args.consdev;
    381 
    382 			/* Attach 'em if we got 'em. */
    383 #if (NKBD > 0)
    384 			if (channel == 0) {
    385 				kma.kmta_name = "keyboard";
    386 				config_found(child, (void *)&kma, NULL);
    387 			}
    388 #endif
    389 #if (NMS > 0)
    390 			if (channel == 1) {
    391 				kma.kmta_name = "mouse";
    392 				config_found(child, (void *)&kma, NULL);
    393 			}
    394 #endif
    395 		}
    396 #endif
    397 	}
    398 
    399 	/*
    400 	 * Now safe to install interrupt handlers.  Note the arguments
    401 	 * to the interrupt handlers aren't used.  Note, we only do this
    402 	 * once since both SCCs interrupt at the same level and vector.
    403 	 */
    404 	bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, 0, zshard, zsc);
    405 	if (!(zsc->zsc_softintr = softintr_establish(softpri, zssoft, zsc)))
    406 		panic("zsattach: could not establish soft interrupt\n");
    407 
    408 	evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
    409 	    zsc->zsc_dev.dv_xname, "intr");
    410 
    411 
    412 	/*
    413 	 * Set the master interrupt enable and interrupt vector.
    414 	 * (common to both channels, do it on A)
    415 	 */
    416 	cs = zsc->zsc_cs[0];
    417 	s = splhigh();
    418 	/* interrupt vector */
    419 	zs_write_reg(cs, 2, zs_init_reg[2]);
    420 	/* master interrupt control (enable) */
    421 	zs_write_reg(cs, 9, zs_init_reg[9]);
    422 	splx(s);
    423 
    424 }
    425 
    426 static int
    427 zs_print(aux, name)
    428 	void *aux;
    429 	const char *name;
    430 {
    431 	struct zsc_attach_args *args = aux;
    432 
    433 	if (name != NULL)
    434 		printf("%s: ", name);
    435 
    436 	if (args->channel != -1)
    437 		printf(" channel %d", args->channel);
    438 
    439 	return (UNCONF);
    440 }
    441 
    442 /* Deprecate this? */
    443 static volatile int zssoftpending;
    444 
    445 static int
    446 zshard(arg)
    447 	void *arg;
    448 {
    449 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    450 	int rr3, rval;
    451 
    452 	rval = 0;
    453 	while ((rr3 = zsc_intr_hard(zsc))) {
    454 		/* Count up the interrupts. */
    455 		rval |= rr3;
    456 		zsc->zsc_intrcnt.ev_count++;
    457 	}
    458 	if (((zsc->zsc_cs[0] && zsc->zsc_cs[0]->cs_softreq) ||
    459 	     (zsc->zsc_cs[1] && zsc->zsc_cs[1]->cs_softreq)) &&
    460 	    zsc->zsc_softintr) {
    461 		zssoftpending = PIL_TTY;
    462 		softintr_schedule(zsc->zsc_softintr);
    463 	}
    464 	return (rval);
    465 }
    466 
    467 int
    468 zscheckintr(arg)
    469 	void *arg;
    470 {
    471 	struct zsc_softc *zsc;
    472 	int unit, rval;
    473 
    474 	rval = 0;
    475 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    476 
    477 		zsc = zs_cd.cd_devs[unit];
    478 		if (zsc == NULL)
    479 			continue;
    480 		rval = (zshard((void *)zsc) || rval);
    481 	}
    482 	return (rval);
    483 }
    484 
    485 
    486 /*
    487  * We need this only for TTY_DEBUG purposes.
    488  */
    489 static void
    490 zssoft(arg)
    491 	void *arg;
    492 {
    493 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    494 	int s;
    495 
    496 	/* Make sure we call the tty layer at spltty. */
    497 	s = spltty();
    498 	zssoftpending = 0;
    499 	(void)zsc_intr_soft(zsc);
    500 #ifdef TTY_DEBUG
    501 	{
    502 		struct zstty_softc *zst0 = zsc->zsc_cs[0]->cs_private;
    503 		struct zstty_softc *zst1 = zsc->zsc_cs[1]->cs_private;
    504 		if (zst0->zst_overflows || zst1->zst_overflows ) {
    505 			struct trapframe *frame = (struct trapframe *)arg;
    506 
    507 			printf("zs silo overflow from %p\n",
    508 			       (long)frame->tf_pc);
    509 		}
    510 	}
    511 #endif
    512 	splx(s);
    513 }
    514 
    515 
    516 /*
    517  * Compute the current baud rate given a ZS channel.
    518  */
    519 static int
    520 zs_get_speed(cs)
    521 	struct zs_chanstate *cs;
    522 {
    523 	int tconst;
    524 
    525 	tconst = zs_read_reg(cs, 12);
    526 	tconst |= zs_read_reg(cs, 13) << 8;
    527 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    528 }
    529 
    530 /*
    531  * MD functions for setting the baud rate and control modes.
    532  */
    533 int
    534 zs_set_speed(cs, bps)
    535 	struct zs_chanstate *cs;
    536 	int bps;	/* bits per second */
    537 {
    538 	int tconst, real_bps;
    539 
    540 	if (bps == 0)
    541 		return (0);
    542 
    543 #ifdef	DIAGNOSTIC
    544 	if (cs->cs_brg_clk == 0)
    545 		panic("zs_set_speed");
    546 #endif
    547 
    548 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    549 	if (tconst < 0)
    550 		return (EINVAL);
    551 
    552 	/* Convert back to make sure we can do it. */
    553 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    554 
    555 	/* XXX - Allow some tolerance here? */
    556 	if (real_bps != bps)
    557 		return (EINVAL);
    558 
    559 	cs->cs_preg[12] = tconst;
    560 	cs->cs_preg[13] = tconst >> 8;
    561 
    562 	/* Caller will stuff the pending registers. */
    563 	return (0);
    564 }
    565 
    566 int
    567 zs_set_modes(cs, cflag)
    568 	struct zs_chanstate *cs;
    569 	int cflag;	/* bits per second */
    570 {
    571 	int s;
    572 
    573 	/*
    574 	 * Output hardware flow control on the chip is horrendous:
    575 	 * if carrier detect drops, the receiver is disabled, and if
    576 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    577 	 * Therefore, NEVER set the HFC bit, and instead use the
    578 	 * status interrupt to detect CTS changes.
    579 	 */
    580 	s = splzs();
    581 	cs->cs_rr0_pps = 0;
    582 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    583 		cs->cs_rr0_dcd = 0;
    584 		if ((cflag & MDMBUF) == 0)
    585 			cs->cs_rr0_pps = ZSRR0_DCD;
    586 	} else
    587 		cs->cs_rr0_dcd = ZSRR0_DCD;
    588 	if ((cflag & CRTSCTS) != 0) {
    589 		cs->cs_wr5_dtr = ZSWR5_DTR;
    590 		cs->cs_wr5_rts = ZSWR5_RTS;
    591 		cs->cs_rr0_cts = ZSRR0_CTS;
    592 	} else if ((cflag & CDTRCTS) != 0) {
    593 		cs->cs_wr5_dtr = 0;
    594 		cs->cs_wr5_rts = ZSWR5_DTR;
    595 		cs->cs_rr0_cts = ZSRR0_CTS;
    596 	} else if ((cflag & MDMBUF) != 0) {
    597 		cs->cs_wr5_dtr = 0;
    598 		cs->cs_wr5_rts = ZSWR5_DTR;
    599 		cs->cs_rr0_cts = ZSRR0_DCD;
    600 	} else {
    601 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    602 		cs->cs_wr5_rts = 0;
    603 		cs->cs_rr0_cts = 0;
    604 	}
    605 	splx(s);
    606 
    607 	/* Caller will stuff the pending registers. */
    608 	return (0);
    609 }
    610 
    611 
    612 /*
    613  * Read or write the chip with suitable delays.
    614  */
    615 
    616 u_char
    617 zs_read_reg(cs, reg)
    618 	struct zs_chanstate *cs;
    619 	u_char reg;
    620 {
    621 	u_char val;
    622 
    623 	*cs->cs_reg_csr = reg;
    624 	ZS_DELAY();
    625 	val = *cs->cs_reg_csr;
    626 	ZS_DELAY();
    627 	return (val);
    628 }
    629 
    630 void
    631 zs_write_reg(cs, reg, val)
    632 	struct zs_chanstate *cs;
    633 	u_char reg, val;
    634 {
    635 	*cs->cs_reg_csr = reg;
    636 	ZS_DELAY();
    637 	*cs->cs_reg_csr = val;
    638 	ZS_DELAY();
    639 }
    640 
    641 u_char
    642 zs_read_csr(cs)
    643 	struct zs_chanstate *cs;
    644 {
    645 	u_char val;
    646 
    647 	val = *cs->cs_reg_csr;
    648 	ZS_DELAY();
    649 	return (val);
    650 }
    651 
    652 void  zs_write_csr(cs, val)
    653 	struct zs_chanstate *cs;
    654 	u_char val;
    655 {
    656 	*cs->cs_reg_csr = val;
    657 	ZS_DELAY();
    658 }
    659 
    660 u_char zs_read_data(cs)
    661 	struct zs_chanstate *cs;
    662 {
    663 	u_char val;
    664 
    665 	val = *cs->cs_reg_data;
    666 	ZS_DELAY();
    667 	return (val);
    668 }
    669 
    670 void  zs_write_data(cs, val)
    671 	struct zs_chanstate *cs;
    672 	u_char val;
    673 {
    674 	*cs->cs_reg_data = val;
    675 	ZS_DELAY();
    676 }
    677 
    678 /****************************************************************
    679  * Console support functions (Sun specific!)
    680  * Note: this code is allowed to know about the layout of
    681  * the chip registers, and uses that to keep things simple.
    682  * XXX - I think I like the mvme167 code better. -gwr
    683  ****************************************************************/
    684 
    685 extern void Debugger __P((void));
    686 
    687 /*
    688  * Handle user request to enter kernel debugger.
    689  */
    690 void
    691 zs_abort(cs)
    692 	struct zs_chanstate *cs;
    693 {
    694 	volatile struct zschan *zc = zs_conschan_get;
    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 	{
    708 		extern int db_active;
    709 
    710 		if (!db_active)
    711 			Debugger();
    712 		else
    713 			/* Debugger is probably hozed */
    714 			callrom();
    715 	}
    716 #else
    717 	printf("stopping on keyboard abort\n");
    718 	callrom();
    719 #endif
    720 }
    721 
    722 
    723 /*
    724  * Polled input char.
    725  */
    726 int
    727 zs_getc(arg)
    728 	void *arg;
    729 {
    730 	volatile struct zschan *zc = arg;
    731 	int s, c, rr0;
    732 
    733 	s = splhigh();
    734 	/* Wait for a character to arrive. */
    735 	do {
    736 		rr0 = zc->zc_csr;
    737 		ZS_DELAY();
    738 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    739 
    740 	c = zc->zc_data;
    741 	ZS_DELAY();
    742 	splx(s);
    743 
    744 	/*
    745 	 * This is used by the kd driver to read scan codes,
    746 	 * so don't translate '\r' ==> '\n' here...
    747 	 */
    748 	return (c);
    749 }
    750 
    751 /*
    752  * Polled output char.
    753  */
    754 void
    755 zs_putc(arg, c)
    756 	void *arg;
    757 	int c;
    758 {
    759 	volatile struct zschan *zc = arg;
    760 	int s, rr0;
    761 
    762 	s = splhigh();
    763 
    764 	/* Wait for transmitter to become ready. */
    765 	do {
    766 		rr0 = zc->zc_csr;
    767 		ZS_DELAY();
    768 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    769 
    770 	/*
    771 	 * Send the next character.
    772 	 * Now you'd think that this could be followed by a ZS_DELAY()
    773 	 * just like all the other chip accesses, but it turns out that
    774 	 * the `transmit-ready' interrupt isn't de-asserted until
    775 	 * some period of time after the register write completes
    776 	 * (more than a couple instructions).  So to avoid stray
    777 	 * interrupts we put in the 2us delay regardless of cpu model.
    778 	 */
    779 	zc->zc_data = c;
    780 	delay(2);
    781 
    782 	splx(s);
    783 }
    784 
    785 /*****************************************************************/
    786 
    787 
    788 
    789 
    790 /*
    791  * Polled console input putchar.
    792  */
    793 static int
    794 zscngetc(dev)
    795 	dev_t dev;
    796 {
    797 	return (zs_getc(zs_conschan_get));
    798 }
    799 
    800 /*
    801  * Polled console output putchar.
    802  */
    803 static void
    804 zscnputc(dev, c)
    805 	dev_t dev;
    806 	int c;
    807 {
    808 	zs_putc(zs_conschan_put, c);
    809 }
    810 
    811 int swallow_zsintrs;
    812 
    813 static void
    814 zscnpollc(dev, on)
    815 	dev_t dev;
    816 	int on;
    817 {
    818 	/*
    819 	 * Need to tell zs driver to acknowledge all interrupts or we get
    820 	 * annoying spurious interrupt messages.  This is because mucking
    821 	 * with spl() levels during polling does not prevent interrupts from
    822 	 * being generated.
    823 	 */
    824 
    825 	if (on) swallow_zsintrs++;
    826 	else swallow_zsintrs--;
    827 }
    828 
    829 int
    830 zs_console_flags(promunit, node, channel)
    831 	int promunit;
    832 	int node;
    833 	int channel;
    834 {
    835 	int cookie, flags = 0;
    836 	u_int options;
    837 	char buf[255];
    838 
    839 	/*
    840 	 * We'll just to the OBP grovelling down here since that's
    841 	 * the only type of firmware we support.
    842 	 */
    843 	options = OF_finddevice("/options");
    844 
    845 	/* Default to channel 0 if there are no explicit prom args */
    846 	cookie = 0;
    847 	if (node == OF_instance_to_package(OF_stdin())) {
    848 		if (OF_getprop(options, "input-device", buf, sizeof(buf)) != -1) {
    849 
    850 			if (!strcmp("ttyb", buf))
    851 				cookie = 1;
    852 		}
    853 
    854 		if (channel == cookie)
    855 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
    856 	}
    857 
    858 	if (node == OF_instance_to_package(OF_stdout())) {
    859 		if (OF_getprop(options, "output-device", buf, sizeof(buf)) != -1) {
    860 
    861 			if (!strcmp("ttyb", buf))
    862 				cookie = 1;
    863 		}
    864 
    865 		if (channel == cookie)
    866 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
    867 	}
    868 
    869 	return (flags);
    870 }
    871 
    872