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