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