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