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