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