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