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