Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.18
      1  1.18       wiz /*	$NetBSD: zs.c,v 1.18 2001/12/27 02:23:26 wiz Exp $	*/
      2   1.1       oki 
      3  1.12   minoura /*-
      4  1.12   minoura  * Copyright (c) 1998 Minoura Makoto
      5  1.12   minoura  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      6  1.12   minoura  * All rights reserved.
      7   1.1       oki  *
      8  1.12   minoura  * This code is derived from software contributed to The NetBSD Foundation
      9  1.12   minoura  * by Gordon W. Ross.
     10   1.1       oki  *
     11   1.1       oki  * Redistribution and use in source and binary forms, with or without
     12   1.1       oki  * modification, are permitted provided that the following conditions
     13   1.1       oki  * are met:
     14   1.1       oki  * 1. Redistributions of source code must retain the above copyright
     15   1.1       oki  *    notice, this list of conditions and the following disclaimer.
     16   1.1       oki  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       oki  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       oki  *    documentation and/or other materials provided with the distribution.
     19   1.1       oki  * 3. All advertising materials mentioning features or use of this software
     20   1.1       oki  *    must display the following acknowledgement:
     21  1.12   minoura  *        This product includes software developed by the NetBSD
     22  1.12   minoura  *        Foundation, Inc. and its contributors.
     23  1.12   minoura  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.12   minoura  *    contributors may be used to endorse or promote products derived
     25  1.12   minoura  *    from this software without specific prior written permission.
     26  1.12   minoura  *
     27  1.12   minoura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.12   minoura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.12   minoura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.12   minoura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.12   minoura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.12   minoura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.12   minoura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.12   minoura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.12   minoura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.12   minoura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.12   minoura  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1       oki  */
     39   1.1       oki 
     40   1.1       oki /*
     41  1.12   minoura  * Zilog Z8530 Dual UART driver (machine-dependent part)
     42  1.12   minoura  *
     43  1.12   minoura  * X68k uses one Z8530 built-in. Channel A is for RS-232C serial port;
     44  1.12   minoura  * while channel B is dedicated to the mouse.
     45  1.15   minoura  * Extra Z8530's can be installed for serial ports.  This driver
     46  1.15   minoura  * supports up to 5 chips including the built-in one.
     47   1.1       oki  */
     48  1.10  jonathan 
     49   1.1       oki #include <sys/param.h>
     50   1.1       oki #include <sys/systm.h>
     51  1.12   minoura #include <sys/conf.h>
     52   1.1       oki #include <sys/device.h>
     53   1.1       oki #include <sys/file.h>
     54   1.1       oki #include <sys/ioctl.h>
     55  1.12   minoura #include <sys/kernel.h>
     56  1.12   minoura #include <sys/proc.h>
     57   1.1       oki #include <sys/tty.h>
     58   1.1       oki #include <sys/time.h>
     59   1.1       oki #include <sys/syslog.h>
     60   1.1       oki 
     61   1.1       oki #include <machine/cpu.h>
     62  1.15   minoura #include <machine/bus.h>
     63  1.15   minoura #include <arch/x68k/dev/intiovar.h>
     64  1.12   minoura #include <machine/z8530var.h>
     65   1.1       oki 
     66   1.1       oki #include <dev/ic/z8530reg.h>
     67   1.1       oki 
     68  1.12   minoura #include "zsc.h"	/* NZSC */
     69  1.15   minoura #include "opt_zsc.h"
     70  1.15   minoura #ifndef ZSCN_SPEED
     71  1.15   minoura #define ZSCN_SPEED 9600
     72  1.15   minoura #endif
     73  1.12   minoura #include "zstty.h"
     74   1.1       oki 
     75   1.1       oki 
     76  1.12   minoura extern void Debugger __P((void));
     77   1.1       oki 
     78  1.12   minoura /*
     79  1.12   minoura  * Some warts needed by z8530tty.c -
     80  1.12   minoura  * The default parity REALLY needs to be the same as the PROM uses,
     81  1.12   minoura  * or you can not see messages done with printf during boot-up...
     82  1.12   minoura  */
     83  1.12   minoura int zs_def_cflag = (CREAD | CS8 | HUPCL);
     84  1.15   minoura int zscn_def_cflag = (CREAD | CS8 | HUPCL);
     85  1.12   minoura int zs_major = 12;
     86  1.12   minoura 
     87  1.12   minoura /*
     88  1.12   minoura  * X68k provides a 5.0 MHz clock to the ZS chips.
     89  1.12   minoura  */
     90  1.15   minoura #define PCLK	(5 * 1000 * 1000)	/* PCLK pin input clock rate */
     91  1.15   minoura 
     92  1.15   minoura 
     93  1.15   minoura /* Default physical addresses. */
     94  1.15   minoura #define ZS_MAXDEV 5
     95  1.15   minoura static bus_addr_t zs_physaddr[ZS_MAXDEV] = {
     96  1.15   minoura 	0x00e98000,
     97  1.15   minoura 	0x00eafc00,
     98  1.15   minoura 	0x00eafc10,
     99  1.15   minoura 	0x00eafc20,
    100  1.15   minoura 	0x00eafc30
    101  1.15   minoura };
    102  1.12   minoura 
    103  1.12   minoura static u_char zs_init_reg[16] = {
    104  1.12   minoura 	0,	/* 0: CMD (reset, etc.) */
    105  1.12   minoura 	0,	/* 1: No interrupts yet. */
    106  1.12   minoura 	0x70,	/* 2: XXX: IVECT */
    107  1.12   minoura 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    108  1.12   minoura 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    109  1.12   minoura 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    110  1.12   minoura 	0,	/* 6: TXSYNC/SYNCLO */
    111  1.12   minoura 	0,	/* 7: RXSYNC/SYNCHI */
    112  1.12   minoura 	0,	/* 8: alias for data port */
    113  1.12   minoura 	ZSWR9_MASTER_IE,
    114  1.12   minoura 	ZSWR10_NRZ,	/*10: Misc. TX/RX control bits */
    115  1.12   minoura 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    116  1.14   mycroft 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    117  1.14   mycroft 	0,			/*13: BAUDHI (default=9600) */
    118  1.12   minoura 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    119  1.13   mycroft 	ZSWR15_BREAK_IE,
    120  1.12   minoura };
    121   1.1       oki 
    122  1.12   minoura static volatile struct zschan *conschan = 0;
    123   1.1       oki 
    124   1.1       oki 
    125  1.12   minoura /****************************************************************
    126  1.12   minoura  * Autoconfig
    127  1.12   minoura  ****************************************************************/
    128   1.1       oki 
    129   1.1       oki /* Definition of the driver for autoconfig. */
    130  1.12   minoura static int	zs_match __P((struct device *, struct cfdata *, void *));
    131  1.12   minoura static void	zs_attach __P((struct device *, struct device *, void *));
    132  1.12   minoura static int  zs_print __P((void *, const char *name));
    133   1.1       oki 
    134  1.12   minoura struct cfattach zsc_ca = {
    135  1.12   minoura 	sizeof(struct zsc_softc), zs_match, zs_attach
    136   1.1       oki };
    137   1.1       oki 
    138  1.12   minoura extern struct cfdriver zsc_cd;
    139   1.1       oki 
    140  1.15   minoura static int zshard __P((void *));
    141  1.12   minoura int zssoft __P((void *));
    142  1.12   minoura static int zs_get_speed __P((struct zs_chanstate *));
    143   1.1       oki 
    144   1.1       oki 
    145   1.1       oki /*
    146  1.12   minoura  * Is the zs chip present?
    147   1.1       oki  */
    148   1.1       oki static int
    149  1.15   minoura zs_match(parent, cf, aux)
    150   1.1       oki 	struct device *parent;
    151  1.15   minoura 	struct cfdata *cf;
    152  1.11   minoura 	void *aux;
    153   1.1       oki {
    154  1.15   minoura 	struct intio_attach_args *ia = aux;
    155  1.15   minoura 	struct zsdevice *zsaddr = (void*) ia->ia_addr;
    156  1.15   minoura 	int i;
    157   1.1       oki 
    158  1.15   minoura 	if (strcmp (ia->ia_name, "zsc") != 0)
    159   1.1       oki 		return 0;
    160  1.15   minoura 
    161  1.15   minoura 	for (i = 0; i < ZS_MAXDEV; i++)
    162  1.15   minoura 		if (zsaddr == (void*) zs_physaddr[i]) /* XXX */
    163  1.15   minoura 			break;
    164  1.15   minoura 
    165  1.15   minoura 	ia->ia_size = 8;
    166  1.15   minoura 	if (intio_map_allocate_region (parent, ia, INTIO_MAP_TESTONLY))
    167  1.15   minoura 		return 0;
    168  1.15   minoura 
    169  1.15   minoura 	if (zsaddr != (void*) zs_physaddr[i])
    170  1.15   minoura 		return 0;
    171  1.16   minoura 	if (badaddr((caddr_t)INTIO_ADDR(zsaddr)))
    172  1.15   minoura 		return 0;
    173  1.15   minoura 
    174  1.15   minoura 	return (1);
    175   1.1       oki }
    176   1.1       oki 
    177   1.1       oki /*
    178   1.1       oki  * Attach a found zs.
    179   1.1       oki  */
    180   1.1       oki static void
    181  1.12   minoura zs_attach(parent, self, aux)
    182   1.1       oki 	struct device *parent;
    183  1.12   minoura 	struct device *self;
    184   1.1       oki 	void *aux;
    185   1.1       oki {
    186  1.12   minoura 	struct zsc_softc *zsc = (void *) self;
    187  1.15   minoura 	struct intio_attach_args *ia = aux;
    188  1.12   minoura 	struct zsc_attach_args zsc_args;
    189   1.1       oki 	volatile struct zschan *zc;
    190   1.1       oki 	struct zs_chanstate *cs;
    191  1.15   minoura 	int r, s, zs_unit, channel;
    192   1.1       oki 
    193  1.12   minoura 	zs_unit = zsc->zsc_dev.dv_unit;
    194  1.15   minoura 	zsc->zsc_addr = (void*) ia->ia_addr;
    195  1.15   minoura 
    196  1.15   minoura 	ia->ia_size = 8;
    197  1.15   minoura 	r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
    198  1.15   minoura #ifdef DIAGNOSTIC
    199  1.15   minoura 	if (r)
    200  1.15   minoura 		panic ("zs: intio IO map corruption");
    201  1.15   minoura #endif
    202   1.1       oki 
    203  1.12   minoura 	printf("\n");
    204   1.1       oki 
    205   1.1       oki 	/*
    206  1.12   minoura 	 * Initialize software state for each channel.
    207   1.1       oki 	 */
    208  1.12   minoura 	for (channel = 0; channel < 2; channel++) {
    209  1.12   minoura 		struct device *child;
    210   1.1       oki 
    211  1.12   minoura 		zsc_args.channel = channel;
    212  1.12   minoura 		zsc_args.hwflags = 0;
    213  1.12   minoura 		cs = &zsc->zsc_cs_store[channel];
    214  1.12   minoura 		zsc->zsc_cs[channel] = cs;
    215  1.12   minoura 
    216  1.12   minoura 		cs->cs_channel = channel;
    217  1.12   minoura 		cs->cs_private = NULL;
    218  1.12   minoura 		cs->cs_ops = &zsops_null;
    219  1.12   minoura 		cs->cs_brg_clk = PCLK / 16;
    220  1.12   minoura 
    221  1.12   minoura 		if (channel == 0)
    222  1.15   minoura 			zc = (void*) INTIO_ADDR(&zsc->zsc_addr->zs_chan_a);
    223  1.12   minoura 		else
    224  1.15   minoura 			zc = (void*) INTIO_ADDR(&zsc->zsc_addr->zs_chan_b);
    225  1.12   minoura 		cs->cs_reg_csr  = &zc->zc_csr;
    226  1.12   minoura 		cs->cs_reg_data = &zc->zc_data;
    227  1.12   minoura 
    228  1.15   minoura 		zs_init_reg[2] = ia->ia_intr;
    229  1.18       wiz 		memcpy(cs->cs_creg, zs_init_reg, 16);
    230  1.18       wiz 		memcpy(cs->cs_preg, zs_init_reg, 16);
    231  1.12   minoura 
    232  1.15   minoura 		if (zc == conschan) {
    233  1.15   minoura 			zsc_args.hwflags |= ZS_HWFLAG_CONSOLE;
    234  1.15   minoura 			cs->cs_defspeed = zs_get_speed(cs);
    235  1.15   minoura 			cs->cs_defcflag = zscn_def_cflag;
    236  1.15   minoura 		} else {
    237  1.15   minoura 			cs->cs_defspeed = 9600;
    238  1.15   minoura 			cs->cs_defcflag = zs_def_cflag;
    239  1.15   minoura 		}
    240  1.12   minoura 
    241  1.12   minoura 		/* Make these correspond to cs_defcflag (-crtscts) */
    242  1.12   minoura 		cs->cs_rr0_dcd = ZSRR0_DCD;
    243  1.12   minoura 		cs->cs_rr0_cts = 0;
    244  1.12   minoura 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    245  1.12   minoura 		cs->cs_wr5_rts = 0;
    246   1.1       oki 
    247   1.9   msaitoh 		/*
    248  1.12   minoura 		 * Clear the master interrupt enable.
    249  1.12   minoura 		 * The INTENA is common to both channels,
    250  1.12   minoura 		 * so just do it on the A channel.
    251   1.9   msaitoh 		 */
    252  1.12   minoura 		if (channel == 0) {
    253  1.12   minoura 			s = splzs();
    254  1.12   minoura 			zs_write_reg(cs, 9, 0);
    255  1.12   minoura 			splx(s);
    256   1.1       oki 		}
    257   1.1       oki 
    258   1.1       oki 		/*
    259  1.12   minoura 		 * Look for a child driver for this channel.
    260  1.12   minoura 		 * The child attach will setup the hardware.
    261   1.1       oki 		 */
    262  1.12   minoura 		child = config_found(self, (void *)&zsc_args, zs_print);
    263  1.15   minoura #if ZSTTY > 0
    264  1.15   minoura 		if (zc == conschan &&
    265  1.15   minoura 		    ((child && strcmp (child->dv_xname, "zstty0")) ||
    266  1.15   minoura 		     child == NULL)) /* XXX */
    267  1.15   minoura 			panic ("zs_attach: console device mismatch");
    268  1.15   minoura #endif
    269  1.12   minoura 		if (child == NULL) {
    270  1.12   minoura 			/* No sub-driver.  Just reset it. */
    271  1.12   minoura 			u_char reset = (channel == 0) ?
    272  1.12   minoura 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    273  1.12   minoura 			s = splzs();
    274  1.12   minoura 			zs_write_reg(cs,  9, reset);
    275  1.12   minoura 			splx(s);
    276   1.1       oki 		}
    277   1.1       oki 	}
    278   1.1       oki 
    279  1.12   minoura 	/*
    280  1.15   minoura 	 * Now safe to install interrupt handlers.
    281  1.15   minoura 	 */
    282  1.15   minoura 	if (intio_intr_establish(ia->ia_intr, "zs", zshard, zsc))
    283  1.15   minoura 		panic("zs_attach: interrupt vector busy");
    284  1.15   minoura 	/* XXX; evcnt_attach() ? */
    285  1.15   minoura 
    286  1.15   minoura 	/*
    287  1.12   minoura 	 * Set the master interrupt enable and interrupt vector.
    288  1.12   minoura 	 * (common to both channels, do it on A)
    289  1.12   minoura 	 */
    290  1.12   minoura 	cs = zsc->zsc_cs[0];
    291  1.12   minoura 	s = splzs();
    292  1.12   minoura 	/* interrupt vector */
    293  1.15   minoura 	zs_write_reg(cs, 2, ia->ia_intr);
    294  1.12   minoura 	/* master interrupt control (enable) */
    295  1.12   minoura 	zs_write_reg(cs, 9, zs_init_reg[9]);
    296  1.12   minoura 	splx(s);
    297   1.1       oki }
    298   1.1       oki 
    299   1.1       oki static int
    300  1.12   minoura zs_print(aux, name)
    301  1.12   minoura 	void *aux;
    302  1.12   minoura 	const char *name;
    303   1.1       oki {
    304  1.12   minoura 	struct zsc_attach_args *args = aux;
    305   1.1       oki 
    306  1.12   minoura 	if (name != NULL)
    307  1.12   minoura 		printf("%s: ", name);
    308   1.1       oki 
    309  1.12   minoura 	if (args->channel != -1)
    310  1.12   minoura 		printf(" channel %d", args->channel);
    311   1.1       oki 
    312  1.12   minoura 	return UNCONF;
    313   1.1       oki }
    314   1.1       oki 
    315   1.1       oki 
    316   1.1       oki /*
    317  1.15   minoura  * For x68k-port, we don't use autovectored interrupt.
    318  1.15   minoura  * We do not need to look at all of the zs chips.
    319   1.1       oki  */
    320  1.15   minoura static int
    321  1.15   minoura zshard(arg)
    322  1.15   minoura 	void *arg;
    323   1.1       oki {
    324  1.15   minoura 	register struct zsc_softc *zsc = arg;
    325  1.15   minoura 	register int rval;
    326  1.15   minoura 	int s;
    327   1.1       oki 
    328  1.15   minoura 	/*
    329  1.15   minoura 	 * Actually, zs hardware ipl is 5.
    330  1.15   minoura 	 * Here we disable all interrupts to shorten the zshard
    331  1.15   minoura 	 * handling time.  Otherwise, too many characters are
    332  1.15   minoura 	 * dropped.
    333  1.15   minoura 	 */
    334  1.15   minoura 	s = splhigh();
    335  1.15   minoura 	rval = zsc_intr_hard(zsc);
    336   1.1       oki 
    337  1.12   minoura 	/* We are at splzs here, so no need to lock. */
    338  1.15   minoura 	if (zsc->zsc_cs[0]->cs_softreq || zsc->zsc_cs[1]->cs_softreq)
    339  1.12   minoura 		setsoftserial();
    340  1.15   minoura 
    341  1.12   minoura 	return (rval);
    342   1.1       oki }
    343   1.1       oki 
    344   1.1       oki /*
    345  1.15   minoura  * Shared among the all chips. We have to look at all of them.
    346   1.1       oki  */
    347   1.1       oki int
    348   1.1       oki zssoft(arg)
    349   1.1       oki 	void *arg;
    350   1.1       oki {
    351  1.12   minoura 	register struct zsc_softc *zsc;
    352  1.12   minoura 	register int s, unit;
    353   1.1       oki 
    354  1.12   minoura 	/* Make sure we call the tty layer at spltty. */
    355   1.1       oki 	s = spltty();
    356  1.12   minoura 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    357  1.12   minoura 		zsc = zsc_cd.cd_devs[unit];
    358  1.12   minoura 		if (zsc == NULL)
    359  1.12   minoura 			continue;
    360  1.12   minoura 		(void) zsc_intr_soft(zsc);
    361   1.1       oki 	}
    362   1.1       oki 	splx(s);
    363  1.15   minoura 
    364  1.12   minoura 	return (1);
    365   1.1       oki }
    366   1.1       oki 
    367  1.12   minoura 
    368   1.1       oki /*
    369  1.12   minoura  * Compute the current baud rate given a ZS channel.
    370   1.1       oki  */
    371  1.12   minoura static int
    372  1.12   minoura zs_get_speed(cs)
    373  1.12   minoura 	struct zs_chanstate *cs;
    374   1.1       oki {
    375  1.12   minoura 	int tconst;
    376   1.1       oki 
    377  1.12   minoura 	tconst = zs_read_reg(cs, 12);
    378  1.12   minoura 	tconst |= zs_read_reg(cs, 13) << 8;
    379  1.12   minoura 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    380   1.1       oki }
    381   1.1       oki 
    382   1.1       oki /*
    383  1.12   minoura  * MD functions for setting the baud rate and control modes.
    384   1.1       oki  */
    385  1.12   minoura int
    386  1.12   minoura zs_set_speed(cs, bps)
    387  1.12   minoura 	struct zs_chanstate *cs;
    388  1.12   minoura 	int bps;	/* bits per second */
    389   1.1       oki {
    390  1.12   minoura 	int tconst, real_bps;
    391   1.1       oki 
    392  1.12   minoura 	if (bps == 0)
    393   1.1       oki 		return (0);
    394  1.12   minoura 
    395  1.12   minoura #ifdef	DIAGNOSTIC
    396  1.12   minoura 	if (cs->cs_brg_clk == 0)
    397  1.12   minoura 		panic("zs_set_speed");
    398  1.12   minoura #endif
    399  1.12   minoura 
    400  1.12   minoura 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    401  1.12   minoura 	if (tconst < 0)
    402   1.1       oki 		return (EINVAL);
    403   1.1       oki 
    404  1.12   minoura 	/* Convert back to make sure we can do it. */
    405  1.12   minoura 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    406   1.1       oki 
    407  1.15   minoura #if 0				/* XXX */
    408  1.12   minoura 	/* XXX - Allow some tolerance here? */
    409  1.12   minoura 	if (real_bps != bps)
    410  1.12   minoura 		return (EINVAL);
    411  1.15   minoura #else
    412  1.15   minoura 	/*
    413  1.15   minoura 	 * Since our PCLK has somewhat strange value,
    414  1.15   minoura 	 * we have to allow tolerance here.
    415  1.15   minoura 	 */
    416  1.15   minoura 	if (BPS_TO_TCONST(cs->cs_brg_clk, real_bps) != tconst)
    417  1.15   minoura 		return (EINVAL);
    418  1.15   minoura #endif
    419  1.12   minoura 
    420  1.12   minoura 	cs->cs_preg[12] = tconst;
    421  1.12   minoura 	cs->cs_preg[13] = tconst >> 8;
    422   1.1       oki 
    423  1.12   minoura 	/* Caller will stuff the pending registers. */
    424  1.12   minoura 	return (0);
    425  1.12   minoura }
    426   1.1       oki 
    427  1.12   minoura int
    428  1.12   minoura zs_set_modes(cs, cflag)
    429  1.12   minoura 	struct zs_chanstate *cs;
    430  1.12   minoura 	int cflag;	/* bits per second */
    431  1.12   minoura {
    432  1.12   minoura 	int s;
    433   1.1       oki 
    434   1.1       oki 	/*
    435  1.12   minoura 	 * Output hardware flow control on the chip is horrendous:
    436  1.12   minoura 	 * if carrier detect drops, the receiver is disabled, and if
    437  1.12   minoura 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    438  1.12   minoura 	 * Therefore, NEVER set the HFC bit, and instead use the
    439  1.12   minoura 	 * status interrupt to detect CTS changes.
    440   1.1       oki 	 */
    441  1.12   minoura 	s = splzs();
    442  1.17  wrstuden 	cs->cs_rr0_pps = 0;
    443  1.17  wrstuden 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    444  1.12   minoura 		cs->cs_rr0_dcd = 0;
    445  1.17  wrstuden 		if ((cflag & MDMBUF) == 0)
    446  1.17  wrstuden 			cs->cs_rr0_pps = ZSRR0_DCD;
    447  1.17  wrstuden 	} else
    448  1.12   minoura 		cs->cs_rr0_dcd = ZSRR0_DCD;
    449  1.12   minoura 	if ((cflag & CRTSCTS) != 0) {
    450  1.12   minoura 		cs->cs_wr5_dtr = ZSWR5_DTR;
    451  1.12   minoura 		cs->cs_wr5_rts = ZSWR5_RTS;
    452  1.12   minoura 		cs->cs_rr0_cts = ZSRR0_CTS;
    453  1.12   minoura 	} else if ((cflag & MDMBUF) != 0) {
    454  1.12   minoura 		cs->cs_wr5_dtr = 0;
    455  1.12   minoura 		cs->cs_wr5_rts = ZSWR5_DTR;
    456  1.12   minoura 		cs->cs_rr0_cts = ZSRR0_DCD;
    457  1.12   minoura 	} else {
    458  1.12   minoura 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    459  1.12   minoura 		cs->cs_wr5_rts = 0;
    460  1.12   minoura 		cs->cs_rr0_cts = 0;
    461   1.1       oki 	}
    462   1.1       oki 	splx(s);
    463  1.12   minoura 
    464  1.12   minoura 	/* Caller will stuff the pending registers. */
    465   1.1       oki 	return (0);
    466   1.1       oki }
    467   1.1       oki 
    468  1.12   minoura 
    469   1.1       oki /*
    470  1.12   minoura  * Read or write the chip with suitable delays.
    471   1.1       oki  */
    472  1.12   minoura 
    473  1.12   minoura u_char
    474  1.12   minoura zs_read_reg(cs, reg)
    475   1.1       oki 	struct zs_chanstate *cs;
    476  1.12   minoura 	u_char reg;
    477   1.1       oki {
    478  1.12   minoura 	u_char val;
    479   1.1       oki 
    480  1.12   minoura 	*cs->cs_reg_csr = reg;
    481  1.12   minoura 	ZS_DELAY();
    482  1.12   minoura 	val = *cs->cs_reg_csr;
    483  1.12   minoura 	ZS_DELAY();
    484  1.12   minoura 	return val;
    485   1.1       oki }
    486   1.1       oki 
    487  1.12   minoura void
    488  1.12   minoura zs_write_reg(cs, reg, val)
    489  1.12   minoura 	struct zs_chanstate *cs;
    490  1.12   minoura 	u_char reg, val;
    491   1.1       oki {
    492  1.12   minoura 	*cs->cs_reg_csr = reg;
    493  1.12   minoura 	ZS_DELAY();
    494  1.12   minoura 	*cs->cs_reg_csr = val;
    495  1.12   minoura 	ZS_DELAY();
    496   1.1       oki }
    497   1.1       oki 
    498  1.12   minoura u_char zs_read_csr(cs)
    499  1.12   minoura 	struct zs_chanstate *cs;
    500   1.1       oki {
    501  1.12   minoura 	register u_char val;
    502   1.1       oki 
    503  1.12   minoura 	val = *cs->cs_reg_csr;
    504   1.1       oki 	ZS_DELAY();
    505  1.12   minoura 	return val;
    506   1.1       oki }
    507   1.1       oki 
    508  1.12   minoura void  zs_write_csr(cs, val)
    509  1.12   minoura 	struct zs_chanstate *cs;
    510  1.12   minoura 	u_char val;
    511   1.1       oki {
    512  1.12   minoura 	*cs->cs_reg_csr = val;
    513  1.12   minoura 	ZS_DELAY();
    514   1.1       oki }
    515   1.1       oki 
    516  1.12   minoura u_char zs_read_data(cs)
    517  1.12   minoura 	struct zs_chanstate *cs;
    518   1.1       oki {
    519  1.12   minoura 	register u_char val;
    520   1.1       oki 
    521  1.12   minoura 	val = *cs->cs_reg_data;
    522  1.12   minoura 	ZS_DELAY();
    523  1.12   minoura 	return val;
    524   1.1       oki }
    525   1.1       oki 
    526  1.12   minoura void  zs_write_data(cs, val)
    527  1.12   minoura 	struct zs_chanstate *cs;
    528  1.12   minoura 	u_char val;
    529   1.1       oki {
    530  1.12   minoura 	*cs->cs_reg_data = val;
    531   1.1       oki 	ZS_DELAY();
    532   1.1       oki }
    533   1.1       oki 
    534  1.15   minoura 
    535  1.15   minoura static struct zs_chanstate zscn_cs;
    536  1.15   minoura 
    537  1.15   minoura /****************************************************************
    538  1.15   minoura  * Console support functions (x68k specific!)
    539  1.15   minoura  * Note: this code is allowed to know about the layout of
    540  1.15   minoura  * the chip registers, and uses that to keep things simple.
    541  1.15   minoura  * XXX - I think I like the mvme167 code better. -gwr
    542  1.15   minoura  ****************************************************************/
    543  1.15   minoura 
    544   1.1       oki /*
    545  1.12   minoura  * Handle user request to enter kernel debugger.
    546   1.1       oki  */
    547   1.1       oki void
    548  1.12   minoura zs_abort(cs)
    549  1.12   minoura 	struct zs_chanstate *cs;
    550   1.1       oki {
    551  1.12   minoura 	int rr0;
    552  1.12   minoura 
    553  1.12   minoura 	/* Wait for end of break to avoid PROM abort. */
    554  1.12   minoura 	/* XXX - Limit the wait? */
    555  1.12   minoura 	do {
    556  1.12   minoura 		rr0 = *cs->cs_reg_csr;
    557  1.12   minoura 		ZS_DELAY();
    558  1.12   minoura 	} while (rr0 & ZSRR0_BREAK);
    559   1.1       oki 
    560  1.12   minoura #ifdef DDB
    561  1.12   minoura 	Debugger();
    562  1.12   minoura #else
    563  1.12   minoura 	printf ("BREAK!!\n");
    564  1.12   minoura #endif
    565   1.1       oki }
    566  1.15   minoura 
    567  1.15   minoura 
    568  1.15   minoura #if NZSTTY > 0
    569  1.15   minoura 
    570  1.15   minoura #include <dev/cons.h>
    571  1.15   minoura cons_decl(zs);
    572  1.15   minoura 
    573  1.15   minoura static int zs_getc __P((void));
    574  1.15   minoura static void zs_putc __P((int));
    575  1.15   minoura 
    576  1.15   minoura /*
    577  1.15   minoura  * Polled input char.
    578  1.15   minoura  */
    579  1.15   minoura static int
    580  1.15   minoura zs_getc(void)
    581  1.15   minoura {
    582  1.15   minoura 	register int s, c, rr0;
    583  1.15   minoura 
    584  1.15   minoura 	s = splzs();
    585  1.15   minoura 	/* Wait for a character to arrive. */
    586  1.15   minoura 	do {
    587  1.15   minoura 		rr0 = zs_read_csr(&zscn_cs);
    588  1.15   minoura 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    589  1.15   minoura 
    590  1.15   minoura 	c = zs_read_data (&zscn_cs);
    591  1.15   minoura 	splx(s);
    592  1.15   minoura 
    593  1.15   minoura 	/*
    594  1.15   minoura 	 * This is used by the kd driver to read scan codes,
    595  1.15   minoura 	 * so don't translate '\r' ==> '\n' here...
    596  1.15   minoura 	 */
    597  1.15   minoura 	return (c);
    598  1.15   minoura }
    599  1.15   minoura 
    600  1.15   minoura /*
    601  1.15   minoura  * Polled output char.
    602  1.15   minoura  */
    603  1.15   minoura static void
    604  1.15   minoura zs_putc(c)
    605  1.15   minoura 	int c;
    606  1.15   minoura {
    607  1.15   minoura 	register int s, rr0;
    608  1.15   minoura 
    609  1.15   minoura 	s = splzs();
    610  1.15   minoura 	/* Wait for transmitter to become ready. */
    611  1.15   minoura 	do {
    612  1.15   minoura 		rr0 = zs_read_csr (&zscn_cs);
    613  1.15   minoura 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    614  1.15   minoura 
    615  1.15   minoura 	zs_write_data(&zscn_cs, c);
    616  1.15   minoura 	splx(s);
    617  1.15   minoura }
    618  1.15   minoura 
    619  1.15   minoura void
    620  1.15   minoura zscninit(cn)
    621  1.15   minoura 	struct consdev *cn;
    622  1.15   minoura {
    623  1.15   minoura 	volatile struct zschan *cnchan = (void*) INTIO_ADDR(ZSCN_PHYSADDR);
    624  1.15   minoura 	int s;
    625  1.15   minoura 
    626  1.18       wiz 	memset(&zscn_cs, 0, sizeof (struct zs_chanstate));
    627  1.15   minoura 	zscn_cs.cs_reg_csr = &cnchan->zc_csr;
    628  1.15   minoura 	zscn_cs.cs_reg_data = &cnchan->zc_data;
    629  1.15   minoura 	zscn_cs.cs_channel = 0;
    630  1.15   minoura 	zscn_cs.cs_brg_clk = PCLK / 16;
    631  1.18       wiz 	memcpy(zscn_cs.cs_preg, zs_init_reg, 16);
    632  1.15   minoura 	zscn_cs.cs_preg[4] = ZSWR4_CLK_X16 | ZSWR4_ONESB; /* XXX */
    633  1.15   minoura 	zscn_cs.cs_preg[9] = 0;
    634  1.15   minoura 	zs_set_speed(&zscn_cs, ZSCN_SPEED);
    635  1.15   minoura 	s = splzs();
    636  1.15   minoura 	zs_loadchannelregs(&zscn_cs);
    637  1.15   minoura 	splx(s);
    638  1.15   minoura 	conschan = cnchan;
    639  1.15   minoura }
    640  1.15   minoura 
    641  1.15   minoura /*
    642  1.15   minoura  * Polled console input putchar.
    643  1.15   minoura  */
    644  1.15   minoura int
    645  1.15   minoura zscngetc(dev)
    646  1.15   minoura 	dev_t dev;
    647  1.15   minoura {
    648  1.15   minoura 	return (zs_getc());
    649  1.15   minoura }
    650  1.15   minoura 
    651  1.15   minoura /*
    652  1.15   minoura  * Polled console output putchar.
    653  1.15   minoura  */
    654  1.15   minoura void
    655  1.15   minoura zscnputc(dev, c)
    656  1.15   minoura 	dev_t dev;
    657  1.15   minoura 	int c;
    658  1.15   minoura {
    659  1.15   minoura 	zs_putc(c);
    660  1.15   minoura }
    661  1.15   minoura 
    662  1.15   minoura extern int zsopen(dev_t, int, int, struct proc *);
    663  1.15   minoura 
    664  1.15   minoura void
    665  1.15   minoura zscnprobe(cd)
    666  1.15   minoura 	struct consdev *cd;
    667  1.15   minoura {
    668  1.15   minoura 	int maj;
    669  1.15   minoura 
    670  1.15   minoura 	/* locate the major number */
    671  1.15   minoura 	for (maj = 0; maj < nchrdev; maj++)
    672  1.15   minoura 		if (cdevsw[maj].d_open == zsopen)
    673  1.15   minoura 			break;
    674  1.15   minoura 	/* XXX: minor number is 0 */
    675  1.15   minoura 
    676  1.15   minoura 	if (cdevsw[maj].d_open != zsopen)
    677  1.15   minoura 		cd->cn_pri = CN_DEAD;
    678  1.15   minoura 	else {
    679  1.15   minoura #ifdef ZSCONSOLE
    680  1.15   minoura 		cd->cn_pri = CN_REMOTE;	/* higher than ITE (CN_INTERNAL) */
    681  1.15   minoura #else
    682  1.15   minoura 		cd->cn_pri = CN_NORMAL;
    683  1.15   minoura #endif
    684  1.15   minoura 		cd->cn_dev = makedev(maj, 0);
    685  1.15   minoura 	}
    686  1.15   minoura }
    687  1.15   minoura 
    688  1.15   minoura void
    689  1.15   minoura zscnpollc(dev, on)
    690  1.15   minoura 	dev_t dev;
    691  1.15   minoura 	int on;
    692  1.15   minoura {
    693  1.15   minoura }
    694  1.15   minoura 
    695  1.15   minoura #endif
    696