Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.34.8.5
      1  1.34.8.5  nathanw /*	$NetBSD: zs.c,v 1.34.8.5 2002/11/11 21:57:21 nathanw Exp $	*/
      2  1.34.8.2  nathanw 
      3  1.34.8.2  nathanw /*
      4  1.34.8.2  nathanw  * Copyright (c) 1995 L. Weppelman (Atari modifications)
      5  1.34.8.2  nathanw  * Copyright (c) 1992, 1993
      6  1.34.8.2  nathanw  *	The Regents of the University of California.  All rights reserved.
      7  1.34.8.2  nathanw  *
      8  1.34.8.2  nathanw  * This software was developed by the Computer Systems Engineering group
      9  1.34.8.2  nathanw  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     10  1.34.8.2  nathanw  * contributed to Berkeley.
     11  1.34.8.2  nathanw  *
     12  1.34.8.2  nathanw  *
     13  1.34.8.2  nathanw  * All advertising materials mentioning features or use of this software
     14  1.34.8.2  nathanw  * must display the following acknowledgement:
     15  1.34.8.2  nathanw  *	This product includes software developed by the University of
     16  1.34.8.2  nathanw  *	California, Lawrence Berkeley Laboratory.
     17  1.34.8.2  nathanw  *
     18  1.34.8.2  nathanw  * Redistribution and use in source and binary forms, with or without
     19  1.34.8.2  nathanw  * modification, are permitted provided that the following conditions
     20  1.34.8.2  nathanw  * are met:
     21  1.34.8.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     22  1.34.8.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     23  1.34.8.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     24  1.34.8.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     25  1.34.8.2  nathanw  *    documentation and/or other materials provided with the distribution.
     26  1.34.8.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     27  1.34.8.2  nathanw  *    must display the following acknowledgement:
     28  1.34.8.2  nathanw  *	This product includes software developed by the University of
     29  1.34.8.2  nathanw  *	California, Berkeley and its contributors.
     30  1.34.8.2  nathanw  * 4. Neither the name of the University nor the names of its contributors
     31  1.34.8.2  nathanw  *    may be used to endorse or promote products derived from this software
     32  1.34.8.2  nathanw  *    without specific prior written permission.
     33  1.34.8.2  nathanw  *
     34  1.34.8.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     35  1.34.8.2  nathanw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     36  1.34.8.2  nathanw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     37  1.34.8.2  nathanw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     38  1.34.8.2  nathanw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     39  1.34.8.2  nathanw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     40  1.34.8.2  nathanw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41  1.34.8.2  nathanw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     42  1.34.8.2  nathanw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     43  1.34.8.2  nathanw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     44  1.34.8.2  nathanw  * SUCH DAMAGE.
     45  1.34.8.2  nathanw  *
     46  1.34.8.2  nathanw  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
     47  1.34.8.2  nathanw  */
     48  1.34.8.2  nathanw 
     49  1.34.8.2  nathanw /*
     50  1.34.8.2  nathanw  * Zilog Z8530 (ZSCC) driver.
     51  1.34.8.2  nathanw  *
     52  1.34.8.2  nathanw  * Runs two tty ports (modem2 and serial2) on zs0.
     53  1.34.8.2  nathanw  *
     54  1.34.8.2  nathanw  * This driver knows far too much about chip to usage mappings.
     55  1.34.8.2  nathanw  */
     56  1.34.8.2  nathanw #include <sys/param.h>
     57  1.34.8.2  nathanw #include <sys/systm.h>
     58  1.34.8.2  nathanw #include <sys/proc.h>
     59  1.34.8.2  nathanw #include <sys/device.h>
     60  1.34.8.2  nathanw #include <sys/conf.h>
     61  1.34.8.2  nathanw #include <sys/file.h>
     62  1.34.8.2  nathanw #include <sys/ioctl.h>
     63  1.34.8.2  nathanw #include <sys/malloc.h>
     64  1.34.8.2  nathanw #include <sys/tty.h>
     65  1.34.8.2  nathanw #include <sys/time.h>
     66  1.34.8.2  nathanw #include <sys/kernel.h>
     67  1.34.8.2  nathanw #include <sys/syslog.h>
     68  1.34.8.2  nathanw 
     69  1.34.8.2  nathanw #include <machine/cpu.h>
     70  1.34.8.2  nathanw #include <machine/iomap.h>
     71  1.34.8.2  nathanw #include <machine/scu.h>
     72  1.34.8.2  nathanw #include <machine/mfp.h>
     73  1.34.8.2  nathanw #include <atari/dev/ym2149reg.h>
     74  1.34.8.2  nathanw 
     75  1.34.8.2  nathanw #include <dev/ic/z8530reg.h>
     76  1.34.8.2  nathanw #include <atari/dev/zsvar.h>
     77  1.34.8.2  nathanw #include "zs.h"
     78  1.34.8.2  nathanw #if NZS > 1
     79  1.34.8.2  nathanw #error "This driver supports only 1 85C30!"
     80  1.34.8.2  nathanw #endif
     81  1.34.8.2  nathanw 
     82  1.34.8.2  nathanw #if NZS > 0
     83  1.34.8.2  nathanw 
     84  1.34.8.2  nathanw #define PCLK	(8053976)	/* PCLK pin input clock rate */
     85  1.34.8.2  nathanw #define PCLK_HD	(9600 * 1536)	/* PCLK on Hades pin input clock rate */
     86  1.34.8.2  nathanw 
     87  1.34.8.2  nathanw #define splzs	spl5
     88  1.34.8.2  nathanw 
     89  1.34.8.2  nathanw /*
     90  1.34.8.2  nathanw  * Software state per found chip.
     91  1.34.8.2  nathanw  */
     92  1.34.8.2  nathanw struct zs_softc {
     93  1.34.8.2  nathanw     struct	device		zi_dev;    /* base device		  */
     94  1.34.8.2  nathanw     volatile struct zsdevice	*zi_zs;    /* chip registers		  */
     95  1.34.8.2  nathanw     struct	zs_chanstate	zi_cs[2];  /* chan A and B software state */
     96  1.34.8.2  nathanw };
     97  1.34.8.2  nathanw 
     98  1.34.8.2  nathanw static u_char	cb_scheduled = 0;	/* Already asked for callback? */
     99  1.34.8.2  nathanw /*
    100  1.34.8.2  nathanw  * Define the registers for a closed port
    101  1.34.8.2  nathanw  */
    102  1.34.8.2  nathanw static u_char zs_init_regs[16] = {
    103  1.34.8.2  nathanw /*  0 */	0,
    104  1.34.8.2  nathanw /*  1 */	0,
    105  1.34.8.2  nathanw /*  2 */	0x60,
    106  1.34.8.2  nathanw /*  3 */	0,
    107  1.34.8.2  nathanw /*  4 */	0,
    108  1.34.8.2  nathanw /*  5 */	0,
    109  1.34.8.2  nathanw /*  6 */	0,
    110  1.34.8.2  nathanw /*  7 */	0,
    111  1.34.8.2  nathanw /*  8 */	0,
    112  1.34.8.2  nathanw /*  9 */	ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT,
    113  1.34.8.2  nathanw /* 10 */	ZSWR10_NRZ,
    114  1.34.8.2  nathanw /* 11 */	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    115  1.34.8.2  nathanw /* 12 */	0,
    116  1.34.8.2  nathanw /* 13 */	0,
    117  1.34.8.2  nathanw /* 14 */	ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
    118  1.34.8.2  nathanw /* 15 */	0
    119  1.34.8.2  nathanw };
    120  1.34.8.2  nathanw 
    121  1.34.8.2  nathanw /*
    122  1.34.8.2  nathanw  * Define the machine dependant clock frequencies
    123  1.34.8.2  nathanw  * If BRgen feeds sender/receiver we always use a
    124  1.34.8.2  nathanw  * divisor 16, therefor the division by 16 can as
    125  1.34.8.2  nathanw  * well be done here.
    126  1.34.8.2  nathanw  */
    127  1.34.8.2  nathanw static u_long zs_freqs_tt[] = {
    128  1.34.8.2  nathanw 	/*
    129  1.34.8.2  nathanw 	 * Atari TT, RTxCB is generated by TT-MFP timer C,
    130  1.34.8.2  nathanw 	 * which is set to 307.2KHz during initialisation
    131  1.34.8.2  nathanw 	 * and never changed afterwards.
    132  1.34.8.2  nathanw 	 */
    133  1.34.8.2  nathanw 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    134  1.34.8.2  nathanw 	 229500,	/* BRgen, RTxCA, divisor 16	*/
    135  1.34.8.2  nathanw 	3672000,	/* RTxCA, from PCLK4		*/
    136  1.34.8.2  nathanw 	      0,	/* TRxCA, external		*/
    137  1.34.8.2  nathanw 
    138  1.34.8.2  nathanw 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    139  1.34.8.2  nathanw 	  19200,	/* BRgen, RTxCB, divisor 16	*/
    140  1.34.8.2  nathanw 	 307200,	/* RTxCB, from TT-MFP TCO	*/
    141  1.34.8.2  nathanw 	2457600		/* TRxCB, from BCLK		*/
    142  1.34.8.2  nathanw };
    143  1.34.8.2  nathanw 
    144  1.34.8.2  nathanw static u_long zs_freqs_falcon[] = {
    145  1.34.8.2  nathanw 	/*
    146  1.34.8.2  nathanw 	 * Atari Falcon, XXX no specs available, this might be wrong
    147  1.34.8.2  nathanw 	 */
    148  1.34.8.2  nathanw 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    149  1.34.8.2  nathanw 	 229500,	/* BRgen, RTxCA, divisor 16	*/
    150  1.34.8.2  nathanw 	3672000,	/* RTxCA, ???			*/
    151  1.34.8.2  nathanw 	      0,	/* TRxCA, external		*/
    152  1.34.8.2  nathanw 
    153  1.34.8.2  nathanw 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    154  1.34.8.2  nathanw 	 229500,	/* BRgen, RTxCB, divisor 16	*/
    155  1.34.8.2  nathanw 	3672000,	/* RTxCB, ???			*/
    156  1.34.8.2  nathanw 	2457600		/* TRxCB, ???			*/
    157  1.34.8.2  nathanw };
    158  1.34.8.2  nathanw 
    159  1.34.8.2  nathanw static u_long zs_freqs_hades[] = {
    160  1.34.8.2  nathanw 	/*
    161  1.34.8.2  nathanw 	 * XXX: Channel-A unchecked!!!!!
    162  1.34.8.2  nathanw 	 */
    163  1.34.8.2  nathanw      PCLK_HD/16,	/* BRgen, PCLK,  divisor 16	*/
    164  1.34.8.2  nathanw 	 229500,	/* BRgen, RTxCA, divisor 16	*/
    165  1.34.8.2  nathanw 	3672000,	/* RTxCA, from PCLK4		*/
    166  1.34.8.2  nathanw 	      0,	/* TRxCA, external		*/
    167  1.34.8.2  nathanw 
    168  1.34.8.2  nathanw      PCLK_HD/16,	/* BRgen, PCLK,  divisor 16	*/
    169  1.34.8.2  nathanw 	 235550,	/* BRgen, RTxCB, divisor 16	*/
    170  1.34.8.2  nathanw 	3768800,	/* RTxCB, 3.7688MHz		*/
    171  1.34.8.2  nathanw 	3768800		/* TRxCB, 3.7688MHz		*/
    172  1.34.8.2  nathanw };
    173  1.34.8.2  nathanw 
    174  1.34.8.2  nathanw static u_long zs_freqs_generic[] = {
    175  1.34.8.2  nathanw 	/*
    176  1.34.8.2  nathanw 	 * other machines, assume only PCLK is available
    177  1.34.8.2  nathanw 	 */
    178  1.34.8.2  nathanw 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    179  1.34.8.2  nathanw 	      0,	/* BRgen, RTxCA, divisor 16	*/
    180  1.34.8.2  nathanw 	      0,	/* RTxCA, unknown		*/
    181  1.34.8.2  nathanw 	      0,	/* TRxCA, unknown		*/
    182  1.34.8.2  nathanw 
    183  1.34.8.2  nathanw 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    184  1.34.8.2  nathanw 	      0,	/* BRgen, RTxCB, divisor 16	*/
    185  1.34.8.2  nathanw 	      0,	/* RTxCB, unknown		*/
    186  1.34.8.2  nathanw 	      0		/* TRxCB, unknown		*/
    187  1.34.8.2  nathanw };
    188  1.34.8.2  nathanw static u_long *zs_frequencies;
    189  1.34.8.2  nathanw 
    190  1.34.8.2  nathanw /* Definition of the driver for autoconfig. */
    191  1.34.8.2  nathanw static int	zsmatch __P((struct device *, struct cfdata *, void *));
    192  1.34.8.2  nathanw static void	zsattach __P((struct device *, struct device *, void *));
    193  1.34.8.2  nathanw 
    194  1.34.8.4  nathanw CFATTACH_DECL(zs, sizeof(struct zs_softc),
    195  1.34.8.4  nathanw     zsmatch, zsattach, NULL, NULL);
    196  1.34.8.2  nathanw 
    197  1.34.8.2  nathanw extern struct cfdriver zs_cd;
    198  1.34.8.2  nathanw 
    199  1.34.8.2  nathanw /* {b,c}devsw[] function prototypes */
    200  1.34.8.2  nathanw dev_type_open(zsopen);
    201  1.34.8.2  nathanw dev_type_close(zsclose);
    202  1.34.8.2  nathanw dev_type_read(zsread);
    203  1.34.8.2  nathanw dev_type_write(zswrite);
    204  1.34.8.2  nathanw dev_type_ioctl(zsioctl);
    205  1.34.8.3  nathanw dev_type_stop(zsstop);
    206  1.34.8.2  nathanw dev_type_tty(zstty);
    207  1.34.8.3  nathanw dev_type_poll(zspoll);
    208  1.34.8.3  nathanw 
    209  1.34.8.3  nathanw const struct cdevsw zs_cdevsw = {
    210  1.34.8.3  nathanw 	zsopen, zsclose, zsread, zswrite, zsioctl,
    211  1.34.8.5  nathanw 	zsstop, zstty, zspoll, nommap, ttykqfilter, D_TTY
    212  1.34.8.3  nathanw };
    213  1.34.8.2  nathanw 
    214  1.34.8.2  nathanw /* Interrupt handlers. */
    215  1.34.8.2  nathanw int		zshard __P((long));
    216  1.34.8.2  nathanw static int	zssoft __P((long));
    217  1.34.8.2  nathanw static int	zsrint __P((struct zs_chanstate *, volatile struct zschan *));
    218  1.34.8.2  nathanw static int	zsxint __P((struct zs_chanstate *, volatile struct zschan *));
    219  1.34.8.2  nathanw static int	zssint __P((struct zs_chanstate *, volatile struct zschan *));
    220  1.34.8.2  nathanw 
    221  1.34.8.2  nathanw static struct zs_chanstate *zslist;
    222  1.34.8.2  nathanw 
    223  1.34.8.2  nathanw /* Routines called from other code. */
    224  1.34.8.2  nathanw static void	zsstart __P((struct tty *));
    225  1.34.8.2  nathanw 
    226  1.34.8.2  nathanw /* Routines purely local to this driver. */
    227  1.34.8.2  nathanw static void	zsoverrun __P((int, long *, char *));
    228  1.34.8.2  nathanw static int	zsparam __P((struct tty *, struct termios *));
    229  1.34.8.2  nathanw static int	zsbaudrate __P((int, int, int *, int *, int *, int *));
    230  1.34.8.2  nathanw static int	zs_modem __P((struct zs_chanstate *, int, int));
    231  1.34.8.2  nathanw static void	zs_loadchannelregs __P((volatile struct zschan *, u_char *));
    232  1.34.8.2  nathanw static void	zs_shutdown __P((struct zs_chanstate *));
    233  1.34.8.2  nathanw 
    234  1.34.8.2  nathanw static int zsshortcuts;	/* number of "shortcut" software interrupts */
    235  1.34.8.2  nathanw 
    236  1.34.8.2  nathanw static int
    237  1.34.8.2  nathanw zsmatch(pdp, cfp, auxp)
    238  1.34.8.2  nathanw struct device	*pdp;
    239  1.34.8.2  nathanw struct cfdata	*cfp;
    240  1.34.8.2  nathanw void		*auxp;
    241  1.34.8.2  nathanw {
    242  1.34.8.2  nathanw 	static int	zs_matched = 0;
    243  1.34.8.2  nathanw 
    244  1.34.8.2  nathanw 	if(strcmp("zs", auxp) || zs_matched)
    245  1.34.8.2  nathanw 		return(0);
    246  1.34.8.2  nathanw 	zs_matched = 1;
    247  1.34.8.2  nathanw 	return(1);
    248  1.34.8.2  nathanw }
    249  1.34.8.2  nathanw 
    250  1.34.8.2  nathanw /*
    251  1.34.8.2  nathanw  * Attach a found zs.
    252  1.34.8.2  nathanw  */
    253  1.34.8.2  nathanw static void
    254  1.34.8.2  nathanw zsattach(parent, dev, aux)
    255  1.34.8.2  nathanw struct device	*parent;
    256  1.34.8.2  nathanw struct device	*dev;
    257  1.34.8.2  nathanw void		*aux;
    258  1.34.8.2  nathanw {
    259  1.34.8.2  nathanw 	register struct zs_softc		*zi;
    260  1.34.8.2  nathanw 	register struct zs_chanstate		*cs;
    261  1.34.8.2  nathanw 	register volatile struct zsdevice	*addr;
    262  1.34.8.2  nathanw 		 char				tmp;
    263  1.34.8.2  nathanw 
    264  1.34.8.2  nathanw 	addr      = (struct zsdevice *)AD_SCC;
    265  1.34.8.2  nathanw 	zi        = (struct zs_softc *)dev;
    266  1.34.8.2  nathanw 	zi->zi_zs = addr;
    267  1.34.8.2  nathanw 	cs        = zi->zi_cs;
    268  1.34.8.2  nathanw 
    269  1.34.8.2  nathanw 	/*
    270  1.34.8.2  nathanw 	 * Get the command register into a known state.
    271  1.34.8.2  nathanw 	 */
    272  1.34.8.2  nathanw 	tmp = addr->zs_chan[ZS_CHAN_A].zc_csr;
    273  1.34.8.2  nathanw 	tmp = addr->zs_chan[ZS_CHAN_A].zc_csr;
    274  1.34.8.2  nathanw 	tmp = addr->zs_chan[ZS_CHAN_B].zc_csr;
    275  1.34.8.2  nathanw 	tmp = addr->zs_chan[ZS_CHAN_B].zc_csr;
    276  1.34.8.2  nathanw 
    277  1.34.8.2  nathanw 	/*
    278  1.34.8.2  nathanw 	 * Do a hardware reset.
    279  1.34.8.2  nathanw 	 */
    280  1.34.8.2  nathanw 	ZS_WRITE(&addr->zs_chan[ZS_CHAN_A], 9, ZSWR9_HARD_RESET);
    281  1.34.8.2  nathanw 	delay(50000);	/*enough ? */
    282  1.34.8.2  nathanw 	ZS_WRITE(&addr->zs_chan[ZS_CHAN_A], 9, 0);
    283  1.34.8.2  nathanw 
    284  1.34.8.2  nathanw 	/*
    285  1.34.8.2  nathanw 	 * Initialize both channels
    286  1.34.8.2  nathanw 	 */
    287  1.34.8.2  nathanw 	zs_loadchannelregs(&addr->zs_chan[ZS_CHAN_A], zs_init_regs);
    288  1.34.8.2  nathanw 	zs_loadchannelregs(&addr->zs_chan[ZS_CHAN_B], zs_init_regs);
    289  1.34.8.2  nathanw 
    290  1.34.8.2  nathanw 	if(machineid & ATARI_TT) {
    291  1.34.8.2  nathanw 		/*
    292  1.34.8.2  nathanw 		 * ininitialise TT-MFP timer C: 307200Hz
    293  1.34.8.2  nathanw 		 * timer C and D share one control register:
    294  1.34.8.2  nathanw 		 *	bits 0-2 control timer D
    295  1.34.8.2  nathanw 		 *	bits 4-6 control timer C
    296  1.34.8.2  nathanw 		 */
    297  1.34.8.2  nathanw 		int cr = MFP2->mf_tcdcr & 7;
    298  1.34.8.2  nathanw 		MFP2->mf_tcdcr = cr;		/* stop timer C  */
    299  1.34.8.2  nathanw 		MFP2->mf_tcdr  = 1;		/* counter 1     */
    300  1.34.8.2  nathanw 		cr |= T_Q004 << 4;		/* divisor 4     */
    301  1.34.8.2  nathanw 		MFP2->mf_tcdcr = cr;		/* start timer C */
    302  1.34.8.2  nathanw 		/*
    303  1.34.8.2  nathanw 		 * enable scc related interrupts
    304  1.34.8.2  nathanw 		 */
    305  1.34.8.2  nathanw 		SCU->vme_mask |= SCU_SCC;
    306  1.34.8.2  nathanw 
    307  1.34.8.2  nathanw 		zs_frequencies = zs_freqs_tt;
    308  1.34.8.2  nathanw 	} else if (machineid & ATARI_FALCON) {
    309  1.34.8.2  nathanw 		zs_frequencies = zs_freqs_falcon;
    310  1.34.8.2  nathanw 	} else if (machineid & ATARI_HADES) {
    311  1.34.8.2  nathanw 		zs_frequencies = zs_freqs_hades;
    312  1.34.8.2  nathanw 	} else {
    313  1.34.8.2  nathanw 		zs_frequencies = zs_freqs_generic;
    314  1.34.8.2  nathanw 	}
    315  1.34.8.2  nathanw 
    316  1.34.8.2  nathanw 	/* link into interrupt list with order (A,B) (B=A+1) */
    317  1.34.8.2  nathanw 	cs[0].cs_next = &cs[1];
    318  1.34.8.2  nathanw 	cs[1].cs_next = zslist;
    319  1.34.8.2  nathanw 	zslist        = cs;
    320  1.34.8.2  nathanw 
    321  1.34.8.2  nathanw 	cs->cs_unit  = 0;
    322  1.34.8.2  nathanw 	cs->cs_zc    = &addr->zs_chan[ZS_CHAN_A];
    323  1.34.8.2  nathanw 	cs++;
    324  1.34.8.2  nathanw 	cs->cs_unit  = 1;
    325  1.34.8.2  nathanw 	cs->cs_zc    = &addr->zs_chan[ZS_CHAN_B];
    326  1.34.8.2  nathanw 
    327  1.34.8.2  nathanw 	printf(": serial2 on channel a and modem2 on channel b\n");
    328  1.34.8.2  nathanw }
    329  1.34.8.2  nathanw 
    330  1.34.8.2  nathanw /*
    331  1.34.8.2  nathanw  * Open a zs serial port.
    332  1.34.8.2  nathanw  */
    333  1.34.8.2  nathanw int
    334  1.34.8.2  nathanw zsopen(dev, flags, mode, p)
    335  1.34.8.2  nathanw dev_t		dev;
    336  1.34.8.2  nathanw int		flags;
    337  1.34.8.2  nathanw int		mode;
    338  1.34.8.2  nathanw struct proc	*p;
    339  1.34.8.2  nathanw {
    340  1.34.8.2  nathanw 	register struct tty		*tp;
    341  1.34.8.2  nathanw 	register struct zs_chanstate	*cs;
    342  1.34.8.2  nathanw 		 struct zs_softc	*zi;
    343  1.34.8.2  nathanw 		 int			unit = ZS_UNIT(dev);
    344  1.34.8.2  nathanw 		 int			zs = unit >> 1;
    345  1.34.8.2  nathanw 		 int			error, s;
    346  1.34.8.2  nathanw 
    347  1.34.8.2  nathanw 	if(zs >= zs_cd.cd_ndevs || (zi = zs_cd.cd_devs[zs]) == NULL)
    348  1.34.8.2  nathanw 		return (ENXIO);
    349  1.34.8.2  nathanw 	cs = &zi->zi_cs[unit & 1];
    350  1.34.8.2  nathanw 
    351  1.34.8.2  nathanw 	/*
    352  1.34.8.2  nathanw 	 * When port A (ser02) is selected on the TT, make sure
    353  1.34.8.2  nathanw 	 * the port is enabled.
    354  1.34.8.2  nathanw 	 */
    355  1.34.8.2  nathanw 	if((machineid & ATARI_TT) && !(unit & 1))
    356  1.34.8.2  nathanw 		ym2149_ser2(1);
    357  1.34.8.2  nathanw 
    358  1.34.8.2  nathanw 	if (cs->cs_rbuf == NULL) {
    359  1.34.8.2  nathanw 		cs->cs_rbuf = malloc(ZLRB_RING_SIZE * sizeof(int), M_DEVBUF,
    360  1.34.8.2  nathanw 								   M_WAITOK);
    361  1.34.8.2  nathanw 	}
    362  1.34.8.2  nathanw 
    363  1.34.8.2  nathanw 	tp = cs->cs_ttyp;
    364  1.34.8.2  nathanw 	if(tp == NULL) {
    365  1.34.8.2  nathanw 		cs->cs_ttyp = tp = ttymalloc();
    366  1.34.8.2  nathanw 		tty_attach(tp);
    367  1.34.8.2  nathanw 		tp->t_dev   = dev;
    368  1.34.8.2  nathanw 		tp->t_oproc = zsstart;
    369  1.34.8.2  nathanw 		tp->t_param = zsparam;
    370  1.34.8.2  nathanw 	}
    371  1.34.8.2  nathanw 
    372  1.34.8.2  nathanw 	if ((tp->t_state & TS_ISOPEN) &&
    373  1.34.8.2  nathanw 	    (tp->t_state & TS_XCLUDE) &&
    374  1.34.8.2  nathanw 	    p->p_ucred->cr_uid != 0)
    375  1.34.8.2  nathanw 		return (EBUSY);
    376  1.34.8.2  nathanw 
    377  1.34.8.2  nathanw 	s  = spltty();
    378  1.34.8.2  nathanw 
    379  1.34.8.2  nathanw 	/*
    380  1.34.8.2  nathanw 	 * Do the following iff this is a first open.
    381  1.34.8.2  nathanw 	 */
    382  1.34.8.2  nathanw 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    383  1.34.8.2  nathanw 		if(tp->t_ispeed == 0) {
    384  1.34.8.2  nathanw 			tp->t_iflag = TTYDEF_IFLAG;
    385  1.34.8.2  nathanw 			tp->t_oflag = TTYDEF_OFLAG;
    386  1.34.8.2  nathanw 			tp->t_cflag = TTYDEF_CFLAG;
    387  1.34.8.2  nathanw 			tp->t_lflag = TTYDEF_LFLAG;
    388  1.34.8.2  nathanw 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    389  1.34.8.2  nathanw 		}
    390  1.34.8.2  nathanw 		ttychars(tp);
    391  1.34.8.2  nathanw 		ttsetwater(tp);
    392  1.34.8.2  nathanw 
    393  1.34.8.2  nathanw 		(void)zsparam(tp, &tp->t_termios);
    394  1.34.8.2  nathanw 
    395  1.34.8.2  nathanw 		/*
    396  1.34.8.2  nathanw 		 * Turn on DTR.  We must always do this, even if carrier is not
    397  1.34.8.2  nathanw 		 * present, because otherwise we'd have to use TIOCSDTR
    398  1.34.8.2  nathanw 		 * immediately after setting CLOCAL, which applications do not
    399  1.34.8.2  nathanw 		 * expect.  We always assert DTR while the device is open
    400  1.34.8.2  nathanw 		 * unless explicitly requested to deassert it.
    401  1.34.8.2  nathanw 		 */
    402  1.34.8.2  nathanw 		zs_modem(cs, ZSWR5_RTS|ZSWR5_DTR, DMSET);
    403  1.34.8.2  nathanw 		/* May never get a status intr. if DCD already on. -gwr */
    404  1.34.8.2  nathanw 		if((cs->cs_rr0 = cs->cs_zc->zc_csr) & ZSRR0_DCD)
    405  1.34.8.2  nathanw 			tp->t_state |= TS_CARR_ON;
    406  1.34.8.2  nathanw 		if(cs->cs_softcar)
    407  1.34.8.2  nathanw 			tp->t_state |= TS_CARR_ON;
    408  1.34.8.2  nathanw 	}
    409  1.34.8.2  nathanw 
    410  1.34.8.2  nathanw 	splx(s);
    411  1.34.8.2  nathanw 
    412  1.34.8.2  nathanw 	error = ttyopen(tp, ZS_DIALOUT(dev), (flags & O_NONBLOCK));
    413  1.34.8.2  nathanw 	if (error)
    414  1.34.8.2  nathanw 		goto bad;
    415  1.34.8.2  nathanw 
    416  1.34.8.2  nathanw 	error = tp->t_linesw->l_open(dev, tp);
    417  1.34.8.2  nathanw 	if(error)
    418  1.34.8.2  nathanw 		goto bad;
    419  1.34.8.2  nathanw 	return (0);
    420  1.34.8.2  nathanw 
    421  1.34.8.2  nathanw bad:
    422  1.34.8.2  nathanw 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    423  1.34.8.2  nathanw 		/*
    424  1.34.8.2  nathanw 		 * We failed to open the device, and nobody else had it opened.
    425  1.34.8.2  nathanw 		 * Clean up the state as appropriate.
    426  1.34.8.2  nathanw 		 */
    427  1.34.8.2  nathanw 		zs_shutdown(cs);
    428  1.34.8.2  nathanw 	}
    429  1.34.8.2  nathanw 	return(error);
    430  1.34.8.2  nathanw }
    431  1.34.8.2  nathanw 
    432  1.34.8.2  nathanw /*
    433  1.34.8.2  nathanw  * Close a zs serial port.
    434  1.34.8.2  nathanw  */
    435  1.34.8.2  nathanw int
    436  1.34.8.2  nathanw zsclose(dev, flags, mode, p)
    437  1.34.8.2  nathanw dev_t		dev;
    438  1.34.8.2  nathanw int		flags;
    439  1.34.8.2  nathanw int		mode;
    440  1.34.8.2  nathanw struct proc	*p;
    441  1.34.8.2  nathanw {
    442  1.34.8.2  nathanw 	register struct zs_chanstate	*cs;
    443  1.34.8.2  nathanw 	register struct tty		*tp;
    444  1.34.8.2  nathanw 		 struct zs_softc	*zi;
    445  1.34.8.2  nathanw 		 int			unit = ZS_UNIT(dev);
    446  1.34.8.2  nathanw 
    447  1.34.8.2  nathanw 	zi = zs_cd.cd_devs[unit >> 1];
    448  1.34.8.2  nathanw 	cs = &zi->zi_cs[unit & 1];
    449  1.34.8.2  nathanw 	tp = cs->cs_ttyp;
    450  1.34.8.2  nathanw 
    451  1.34.8.2  nathanw 	tp->t_linesw->l_close(tp, flags);
    452  1.34.8.2  nathanw 	ttyclose(tp);
    453  1.34.8.2  nathanw 
    454  1.34.8.2  nathanw 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    455  1.34.8.2  nathanw 		/*
    456  1.34.8.2  nathanw 		 * Although we got a last close, the device may still be in
    457  1.34.8.2  nathanw 		 * use; e.g. if this was the dialout node, and there are still
    458  1.34.8.2  nathanw 		 * processes waiting for carrier on the non-dialout node.
    459  1.34.8.2  nathanw 		 */
    460  1.34.8.2  nathanw 		zs_shutdown(cs);
    461  1.34.8.2  nathanw 	}
    462  1.34.8.2  nathanw 	return (0);
    463  1.34.8.2  nathanw }
    464  1.34.8.2  nathanw 
    465  1.34.8.2  nathanw /*
    466  1.34.8.2  nathanw  * Read/write zs serial port.
    467  1.34.8.2  nathanw  */
    468  1.34.8.2  nathanw int
    469  1.34.8.2  nathanw zsread(dev, uio, flags)
    470  1.34.8.2  nathanw dev_t		dev;
    471  1.34.8.2  nathanw struct uio	*uio;
    472  1.34.8.2  nathanw int		flags;
    473  1.34.8.2  nathanw {
    474  1.34.8.2  nathanw 	register struct zs_chanstate	*cs;
    475  1.34.8.2  nathanw 	register struct zs_softc	*zi;
    476  1.34.8.2  nathanw 	register struct tty		*tp;
    477  1.34.8.2  nathanw 		 int			unit;
    478  1.34.8.2  nathanw 
    479  1.34.8.2  nathanw 	unit = ZS_UNIT(dev);
    480  1.34.8.2  nathanw 	zi   = zs_cd.cd_devs[unit >> 1];
    481  1.34.8.2  nathanw 	cs   = &zi->zi_cs[unit & 1];
    482  1.34.8.2  nathanw 	tp   = cs->cs_ttyp;
    483  1.34.8.2  nathanw 
    484  1.34.8.2  nathanw 	return(tp->t_linesw->l_read(tp, uio, flags));
    485  1.34.8.2  nathanw }
    486  1.34.8.2  nathanw 
    487  1.34.8.2  nathanw int
    488  1.34.8.2  nathanw zswrite(dev, uio, flags)
    489  1.34.8.2  nathanw dev_t		dev;
    490  1.34.8.2  nathanw struct uio	*uio;
    491  1.34.8.2  nathanw int		flags;
    492  1.34.8.2  nathanw {
    493  1.34.8.2  nathanw 	register struct zs_chanstate	*cs;
    494  1.34.8.2  nathanw 	register struct zs_softc	*zi;
    495  1.34.8.2  nathanw 	register struct tty		*tp;
    496  1.34.8.2  nathanw 		 int			unit;
    497  1.34.8.2  nathanw 
    498  1.34.8.2  nathanw 	unit = ZS_UNIT(dev);
    499  1.34.8.2  nathanw 	zi   = zs_cd.cd_devs[unit >> 1];
    500  1.34.8.2  nathanw 	cs   = &zi->zi_cs[unit & 1];
    501  1.34.8.2  nathanw 	tp   = cs->cs_ttyp;
    502  1.34.8.2  nathanw 
    503  1.34.8.2  nathanw 	return(tp->t_linesw->l_write(tp, uio, flags));
    504  1.34.8.2  nathanw }
    505  1.34.8.2  nathanw 
    506  1.34.8.2  nathanw int
    507  1.34.8.2  nathanw zspoll(dev, events, p)
    508  1.34.8.2  nathanw dev_t		dev;
    509  1.34.8.2  nathanw int		events;
    510  1.34.8.2  nathanw struct proc	*p;
    511  1.34.8.2  nathanw {
    512  1.34.8.2  nathanw 	register struct zs_chanstate	*cs;
    513  1.34.8.2  nathanw 	register struct zs_softc	*zi;
    514  1.34.8.2  nathanw 	register struct tty		*tp;
    515  1.34.8.2  nathanw 		 int			unit;
    516  1.34.8.2  nathanw 
    517  1.34.8.2  nathanw 	unit = ZS_UNIT(dev);
    518  1.34.8.2  nathanw 	zi   = zs_cd.cd_devs[unit >> 1];
    519  1.34.8.2  nathanw 	cs   = &zi->zi_cs[unit & 1];
    520  1.34.8.2  nathanw 	tp   = cs->cs_ttyp;
    521  1.34.8.2  nathanw 
    522  1.34.8.2  nathanw 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    523  1.34.8.2  nathanw }
    524  1.34.8.2  nathanw 
    525  1.34.8.2  nathanw struct tty *
    526  1.34.8.2  nathanw zstty(dev)
    527  1.34.8.2  nathanw dev_t	dev;
    528  1.34.8.2  nathanw {
    529  1.34.8.2  nathanw 	register struct zs_chanstate	*cs;
    530  1.34.8.2  nathanw 	register struct zs_softc	*zi;
    531  1.34.8.2  nathanw 		 int			unit;
    532  1.34.8.2  nathanw 
    533  1.34.8.2  nathanw 	unit = ZS_UNIT(dev);
    534  1.34.8.2  nathanw 	zi   = zs_cd.cd_devs[unit >> 1];
    535  1.34.8.2  nathanw 	cs   = &zi->zi_cs[unit & 1];
    536  1.34.8.2  nathanw 	return(cs->cs_ttyp);
    537  1.34.8.2  nathanw }
    538  1.34.8.2  nathanw 
    539  1.34.8.2  nathanw /*
    540  1.34.8.2  nathanw  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
    541  1.34.8.2  nathanw  * channels are kept in (A,B) pairs.
    542  1.34.8.2  nathanw  *
    543  1.34.8.2  nathanw  * Do just a little, then get out; set a software interrupt if more
    544  1.34.8.2  nathanw  * work is needed.
    545  1.34.8.2  nathanw  *
    546  1.34.8.2  nathanw  * We deliberately ignore the vectoring Zilog gives us, and match up
    547  1.34.8.2  nathanw  * only the number of `reset interrupt under service' operations, not
    548  1.34.8.2  nathanw  * the order.
    549  1.34.8.2  nathanw  */
    550  1.34.8.2  nathanw 
    551  1.34.8.2  nathanw int
    552  1.34.8.2  nathanw zshard(sr)
    553  1.34.8.2  nathanw long sr;
    554  1.34.8.2  nathanw {
    555  1.34.8.2  nathanw 	register struct zs_chanstate	*a;
    556  1.34.8.2  nathanw #define	b (a + 1)
    557  1.34.8.2  nathanw 	register volatile struct zschan *zc;
    558  1.34.8.2  nathanw 	register int			rr3, intflags = 0, v, i;
    559  1.34.8.2  nathanw 
    560  1.34.8.2  nathanw 	do {
    561  1.34.8.2  nathanw 	    intflags &= ~4;
    562  1.34.8.2  nathanw 	    for(a = zslist; a != NULL; a = b->cs_next) {
    563  1.34.8.2  nathanw 		rr3 = ZS_READ(a->cs_zc, 3);
    564  1.34.8.2  nathanw 		if(rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
    565  1.34.8.2  nathanw 			intflags |= 4|2;
    566  1.34.8.2  nathanw 			zc = a->cs_zc;
    567  1.34.8.2  nathanw 			i  = a->cs_rbput;
    568  1.34.8.2  nathanw 			if(rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
    569  1.34.8.2  nathanw 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    570  1.34.8.2  nathanw 				intflags |= 1;
    571  1.34.8.2  nathanw 			}
    572  1.34.8.2  nathanw 			if(rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
    573  1.34.8.2  nathanw 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    574  1.34.8.2  nathanw 				intflags |= 1;
    575  1.34.8.2  nathanw 			}
    576  1.34.8.2  nathanw 			if(rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
    577  1.34.8.2  nathanw 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    578  1.34.8.2  nathanw 				intflags |= 1;
    579  1.34.8.2  nathanw 			}
    580  1.34.8.2  nathanw 			a->cs_rbput = i;
    581  1.34.8.2  nathanw 		}
    582  1.34.8.2  nathanw 		if(rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
    583  1.34.8.2  nathanw 			intflags |= 4|2;
    584  1.34.8.2  nathanw 			zc = b->cs_zc;
    585  1.34.8.2  nathanw 			i  = b->cs_rbput;
    586  1.34.8.2  nathanw 			if(rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
    587  1.34.8.2  nathanw 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    588  1.34.8.2  nathanw 				intflags |= 1;
    589  1.34.8.2  nathanw 			}
    590  1.34.8.2  nathanw 			if(rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
    591  1.34.8.2  nathanw 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    592  1.34.8.2  nathanw 				intflags |= 1;
    593  1.34.8.2  nathanw 			}
    594  1.34.8.2  nathanw 			if(rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
    595  1.34.8.2  nathanw 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    596  1.34.8.2  nathanw 				intflags |= 1;
    597  1.34.8.2  nathanw 			}
    598  1.34.8.2  nathanw 			b->cs_rbput = i;
    599  1.34.8.2  nathanw 		}
    600  1.34.8.2  nathanw 	    }
    601  1.34.8.2  nathanw 	} while(intflags & 4);
    602  1.34.8.2  nathanw #undef b
    603  1.34.8.2  nathanw 
    604  1.34.8.2  nathanw 	if(intflags & 1) {
    605  1.34.8.2  nathanw 		if(BASEPRI(sr)) {
    606  1.34.8.2  nathanw 			spl1();
    607  1.34.8.2  nathanw 			zsshortcuts++;
    608  1.34.8.2  nathanw 			return(zssoft(sr));
    609  1.34.8.2  nathanw 		}
    610  1.34.8.2  nathanw 		else if(!cb_scheduled) {
    611  1.34.8.2  nathanw 			cb_scheduled++;
    612  1.34.8.2  nathanw 			add_sicallback((si_farg)zssoft, 0, 0);
    613  1.34.8.2  nathanw 		}
    614  1.34.8.2  nathanw 	}
    615  1.34.8.2  nathanw 	return(intflags & 2);
    616  1.34.8.2  nathanw }
    617  1.34.8.2  nathanw 
    618  1.34.8.2  nathanw static int
    619  1.34.8.2  nathanw zsrint(cs, zc)
    620  1.34.8.2  nathanw register struct zs_chanstate	*cs;
    621  1.34.8.2  nathanw register volatile struct zschan	*zc;
    622  1.34.8.2  nathanw {
    623  1.34.8.2  nathanw 	register int c;
    624  1.34.8.2  nathanw 
    625  1.34.8.2  nathanw 	/*
    626  1.34.8.2  nathanw 	 * First read the status, because read of the received char
    627  1.34.8.2  nathanw 	 * destroy the status of this char.
    628  1.34.8.2  nathanw 	 */
    629  1.34.8.2  nathanw 	c = ZS_READ(zc, 1);
    630  1.34.8.2  nathanw 	c |= (zc->zc_data << 8);
    631  1.34.8.2  nathanw 
    632  1.34.8.2  nathanw 	/* clear receive error & interrupt condition */
    633  1.34.8.2  nathanw 	zc->zc_csr = ZSWR0_RESET_ERRORS;
    634  1.34.8.2  nathanw 	zc->zc_csr = ZSWR0_CLR_INTR;
    635  1.34.8.2  nathanw 
    636  1.34.8.2  nathanw 	return(ZRING_MAKE(ZRING_RINT, c));
    637  1.34.8.2  nathanw }
    638  1.34.8.2  nathanw 
    639  1.34.8.2  nathanw static int
    640  1.34.8.2  nathanw zsxint(cs, zc)
    641  1.34.8.2  nathanw register struct zs_chanstate	*cs;
    642  1.34.8.2  nathanw register volatile struct zschan	*zc;
    643  1.34.8.2  nathanw {
    644  1.34.8.2  nathanw 	register int i = cs->cs_tbc;
    645  1.34.8.2  nathanw 
    646  1.34.8.2  nathanw 	if(i == 0) {
    647  1.34.8.2  nathanw 		zc->zc_csr = ZSWR0_RESET_TXINT;
    648  1.34.8.2  nathanw 		zc->zc_csr = ZSWR0_CLR_INTR;
    649  1.34.8.2  nathanw 		return(ZRING_MAKE(ZRING_XINT, 0));
    650  1.34.8.2  nathanw 	}
    651  1.34.8.2  nathanw 	cs->cs_tbc = i - 1;
    652  1.34.8.2  nathanw 	zc->zc_data = *cs->cs_tba++;
    653  1.34.8.2  nathanw 	zc->zc_csr = ZSWR0_CLR_INTR;
    654  1.34.8.2  nathanw 	return (0);
    655  1.34.8.2  nathanw }
    656  1.34.8.2  nathanw 
    657  1.34.8.2  nathanw static int
    658  1.34.8.2  nathanw zssint(cs, zc)
    659  1.34.8.2  nathanw register struct zs_chanstate	*cs;
    660  1.34.8.2  nathanw register volatile struct zschan	*zc;
    661  1.34.8.2  nathanw {
    662  1.34.8.2  nathanw 	register int rr0;
    663  1.34.8.2  nathanw 
    664  1.34.8.2  nathanw 	rr0 = zc->zc_csr;
    665  1.34.8.2  nathanw 	zc->zc_csr = ZSWR0_RESET_STATUS;
    666  1.34.8.2  nathanw 	zc->zc_csr = ZSWR0_CLR_INTR;
    667  1.34.8.2  nathanw 	/*
    668  1.34.8.2  nathanw 	 * The chip's hardware flow control is, as noted in zsreg.h,
    669  1.34.8.2  nathanw 	 * busted---if the DCD line goes low the chip shuts off the
    670  1.34.8.2  nathanw 	 * receiver (!).  If we want hardware CTS flow control but do
    671  1.34.8.2  nathanw 	 * not have it, and carrier is now on, turn HFC on; if we have
    672  1.34.8.2  nathanw 	 * HFC now but carrier has gone low, turn it off.
    673  1.34.8.2  nathanw 	 */
    674  1.34.8.2  nathanw 	if(rr0 & ZSRR0_DCD) {
    675  1.34.8.2  nathanw 		if(cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
    676  1.34.8.2  nathanw 		    (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
    677  1.34.8.2  nathanw 			cs->cs_creg[3] |= ZSWR3_HFC;
    678  1.34.8.2  nathanw 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    679  1.34.8.2  nathanw 		}
    680  1.34.8.2  nathanw 	}
    681  1.34.8.2  nathanw 	else {
    682  1.34.8.2  nathanw 		if (cs->cs_creg[3] & ZSWR3_HFC) {
    683  1.34.8.2  nathanw 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    684  1.34.8.2  nathanw 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    685  1.34.8.2  nathanw 		}
    686  1.34.8.2  nathanw 	}
    687  1.34.8.2  nathanw 	return(ZRING_MAKE(ZRING_SINT, rr0));
    688  1.34.8.2  nathanw }
    689  1.34.8.2  nathanw 
    690  1.34.8.2  nathanw /*
    691  1.34.8.2  nathanw  * Print out a ring or fifo overrun error message.
    692  1.34.8.2  nathanw  */
    693  1.34.8.2  nathanw static void
    694  1.34.8.2  nathanw zsoverrun(unit, ptime, what)
    695  1.34.8.2  nathanw int	unit;
    696  1.34.8.2  nathanw long	*ptime;
    697  1.34.8.2  nathanw char	*what;
    698  1.34.8.2  nathanw {
    699  1.34.8.2  nathanw 
    700  1.34.8.2  nathanw 	if(*ptime != time.tv_sec) {
    701  1.34.8.2  nathanw 		*ptime = time.tv_sec;
    702  1.34.8.2  nathanw 		log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
    703  1.34.8.2  nathanw 		    (unit & 1) + 'a', what);
    704  1.34.8.2  nathanw 	}
    705  1.34.8.2  nathanw }
    706  1.34.8.2  nathanw 
    707  1.34.8.2  nathanw /*
    708  1.34.8.2  nathanw  * ZS software interrupt.  Scan all channels for deferred interrupts.
    709  1.34.8.2  nathanw  */
    710  1.34.8.2  nathanw int
    711  1.34.8.2  nathanw zssoft(sr)
    712  1.34.8.2  nathanw long sr;
    713  1.34.8.2  nathanw {
    714  1.34.8.2  nathanw     register struct zs_chanstate	*cs;
    715  1.34.8.2  nathanw     register volatile struct zschan	*zc;
    716  1.34.8.2  nathanw     register struct linesw		*line;
    717  1.34.8.2  nathanw     register struct tty			*tp;
    718  1.34.8.2  nathanw     register int			get, n, c, cc, unit, s;
    719  1.34.8.2  nathanw  	     int			retval = 0;
    720  1.34.8.2  nathanw 
    721  1.34.8.2  nathanw     cb_scheduled = 0;
    722  1.34.8.2  nathanw     s = spltty();
    723  1.34.8.2  nathanw     for(cs = zslist; cs != NULL; cs = cs->cs_next) {
    724  1.34.8.2  nathanw 	get = cs->cs_rbget;
    725  1.34.8.2  nathanw again:
    726  1.34.8.2  nathanw 	n = cs->cs_rbput;	/* atomic			*/
    727  1.34.8.2  nathanw 	if(get == n)		/* nothing more on this line	*/
    728  1.34.8.2  nathanw 		continue;
    729  1.34.8.2  nathanw 	retval = 1;
    730  1.34.8.2  nathanw 	unit   = cs->cs_unit;	/* set up to handle interrupts	*/
    731  1.34.8.2  nathanw 	zc     = cs->cs_zc;
    732  1.34.8.2  nathanw 	tp     = cs->cs_ttyp;
    733  1.34.8.2  nathanw 	line   = tp->t_linesw;
    734  1.34.8.2  nathanw 	/*
    735  1.34.8.2  nathanw 	 * Compute the number of interrupts in the receive ring.
    736  1.34.8.2  nathanw 	 * If the count is overlarge, we lost some events, and
    737  1.34.8.2  nathanw 	 * must advance to the first valid one.  It may get
    738  1.34.8.2  nathanw 	 * overwritten if more data are arriving, but this is
    739  1.34.8.2  nathanw 	 * too expensive to check and gains nothing (we already
    740  1.34.8.2  nathanw 	 * lost out; all we can do at this point is trade one
    741  1.34.8.2  nathanw 	 * kind of loss for another).
    742  1.34.8.2  nathanw 	 */
    743  1.34.8.2  nathanw 	n -= get;
    744  1.34.8.2  nathanw 	if(n > ZLRB_RING_SIZE) {
    745  1.34.8.2  nathanw 		zsoverrun(unit, &cs->cs_rotime, "ring");
    746  1.34.8.2  nathanw 		get += n - ZLRB_RING_SIZE;
    747  1.34.8.2  nathanw 		n    = ZLRB_RING_SIZE;
    748  1.34.8.2  nathanw 	}
    749  1.34.8.2  nathanw 	while(--n >= 0) {
    750  1.34.8.2  nathanw 		/* race to keep ahead of incoming interrupts */
    751  1.34.8.2  nathanw 		c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
    752  1.34.8.2  nathanw 		switch (ZRING_TYPE(c)) {
    753  1.34.8.2  nathanw 
    754  1.34.8.2  nathanw 		case ZRING_RINT:
    755  1.34.8.2  nathanw 			c = ZRING_VALUE(c);
    756  1.34.8.2  nathanw 			if(c & ZSRR1_DO)
    757  1.34.8.2  nathanw 				zsoverrun(unit, &cs->cs_fotime, "fifo");
    758  1.34.8.2  nathanw 			cc = c >> 8;
    759  1.34.8.2  nathanw 			if(c & ZSRR1_FE)
    760  1.34.8.2  nathanw 				cc |= TTY_FE;
    761  1.34.8.2  nathanw 			if(c & ZSRR1_PE)
    762  1.34.8.2  nathanw 				cc |= TTY_PE;
    763  1.34.8.2  nathanw 			line->l_rint(cc, tp);
    764  1.34.8.2  nathanw 			break;
    765  1.34.8.2  nathanw 
    766  1.34.8.2  nathanw 		case ZRING_XINT:
    767  1.34.8.2  nathanw 			/*
    768  1.34.8.2  nathanw 			 * Transmit done: change registers and resume,
    769  1.34.8.2  nathanw 			 * or clear BUSY.
    770  1.34.8.2  nathanw 			 */
    771  1.34.8.2  nathanw 			if(cs->cs_heldchange) {
    772  1.34.8.2  nathanw 				int sps;
    773  1.34.8.2  nathanw 
    774  1.34.8.2  nathanw 				sps = splzs();
    775  1.34.8.2  nathanw 				c = zc->zc_csr;
    776  1.34.8.2  nathanw 				if((c & ZSRR0_DCD) == 0)
    777  1.34.8.2  nathanw 					cs->cs_preg[3] &= ~ZSWR3_HFC;
    778  1.34.8.2  nathanw 				bcopy((caddr_t)cs->cs_preg,
    779  1.34.8.2  nathanw 				    (caddr_t)cs->cs_creg, 16);
    780  1.34.8.2  nathanw 				zs_loadchannelregs(zc, cs->cs_creg);
    781  1.34.8.2  nathanw 				splx(sps);
    782  1.34.8.2  nathanw 				cs->cs_heldchange = 0;
    783  1.34.8.2  nathanw 				if(cs->cs_heldtbc
    784  1.34.8.2  nathanw 					&& (tp->t_state & TS_TTSTOP) == 0) {
    785  1.34.8.2  nathanw 					cs->cs_tbc = cs->cs_heldtbc - 1;
    786  1.34.8.2  nathanw 					zc->zc_data = *cs->cs_tba++;
    787  1.34.8.2  nathanw 					goto again;
    788  1.34.8.2  nathanw 				}
    789  1.34.8.2  nathanw 			}
    790  1.34.8.2  nathanw 			tp->t_state &= ~TS_BUSY;
    791  1.34.8.2  nathanw 			if(tp->t_state & TS_FLUSH)
    792  1.34.8.2  nathanw 				tp->t_state &= ~TS_FLUSH;
    793  1.34.8.2  nathanw 			else ndflush(&tp->t_outq,cs->cs_tba
    794  1.34.8.2  nathanw 						- (caddr_t)tp->t_outq.c_cf);
    795  1.34.8.2  nathanw 			line->l_start(tp);
    796  1.34.8.2  nathanw 			break;
    797  1.34.8.2  nathanw 
    798  1.34.8.2  nathanw 		case ZRING_SINT:
    799  1.34.8.2  nathanw 			/*
    800  1.34.8.2  nathanw 			 * Status line change.  HFC bit is run in
    801  1.34.8.2  nathanw 			 * hardware interrupt, to avoid locking
    802  1.34.8.2  nathanw 			 * at splzs here.
    803  1.34.8.2  nathanw 			 */
    804  1.34.8.2  nathanw 			c = ZRING_VALUE(c);
    805  1.34.8.2  nathanw 			if((c ^ cs->cs_rr0) & ZSRR0_DCD) {
    806  1.34.8.2  nathanw 				cc = (c & ZSRR0_DCD) != 0;
    807  1.34.8.2  nathanw 				if(line->l_modem(tp, cc) == 0)
    808  1.34.8.2  nathanw 					zs_modem(cs, ZSWR5_RTS|ZSWR5_DTR,
    809  1.34.8.2  nathanw 							cc ? DMBIS : DMBIC);
    810  1.34.8.2  nathanw 			}
    811  1.34.8.2  nathanw 			cs->cs_rr0 = c;
    812  1.34.8.2  nathanw 			break;
    813  1.34.8.2  nathanw 
    814  1.34.8.2  nathanw 		default:
    815  1.34.8.2  nathanw 			log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
    816  1.34.8.2  nathanw 			    unit >> 1, (unit & 1) + 'a', c);
    817  1.34.8.2  nathanw 			break;
    818  1.34.8.2  nathanw 		}
    819  1.34.8.2  nathanw 	}
    820  1.34.8.2  nathanw 	cs->cs_rbget = get;
    821  1.34.8.2  nathanw 	goto again;
    822  1.34.8.2  nathanw     }
    823  1.34.8.2  nathanw     splx(s);
    824  1.34.8.2  nathanw     return (retval);
    825  1.34.8.2  nathanw }
    826  1.34.8.2  nathanw 
    827  1.34.8.2  nathanw int
    828  1.34.8.2  nathanw zsioctl(dev, cmd, data, flag, p)
    829  1.34.8.2  nathanw dev_t		dev;
    830  1.34.8.2  nathanw u_long		cmd;
    831  1.34.8.2  nathanw caddr_t		data;
    832  1.34.8.2  nathanw int		flag;
    833  1.34.8.2  nathanw struct proc	*p;
    834  1.34.8.2  nathanw {
    835  1.34.8.2  nathanw 		 int			unit = ZS_UNIT(dev);
    836  1.34.8.2  nathanw 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
    837  1.34.8.2  nathanw 	register struct tty		*tp = zi->zi_cs[unit & 1].cs_ttyp;
    838  1.34.8.2  nathanw 	register int			error, s;
    839  1.34.8.2  nathanw 	register struct zs_chanstate	*cs = &zi->zi_cs[unit & 1];
    840  1.34.8.2  nathanw 
    841  1.34.8.2  nathanw 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, p);
    842  1.34.8.2  nathanw 	if(error != EPASSTHROUGH)
    843  1.34.8.2  nathanw 		return(error);
    844  1.34.8.2  nathanw 
    845  1.34.8.2  nathanw 	error = ttioctl(tp, cmd, data, flag, p);
    846  1.34.8.2  nathanw 	if(error !=EPASSTHROUGH)
    847  1.34.8.2  nathanw 		return (error);
    848  1.34.8.2  nathanw 
    849  1.34.8.2  nathanw 	switch (cmd) {
    850  1.34.8.2  nathanw 	case TIOCSBRK:
    851  1.34.8.2  nathanw 		s = splzs();
    852  1.34.8.2  nathanw 		cs->cs_preg[5] |= ZSWR5_BREAK;
    853  1.34.8.2  nathanw 		cs->cs_creg[5] |= ZSWR5_BREAK;
    854  1.34.8.2  nathanw 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    855  1.34.8.2  nathanw 		splx(s);
    856  1.34.8.2  nathanw 		break;
    857  1.34.8.2  nathanw 	case TIOCCBRK:
    858  1.34.8.2  nathanw 		s = splzs();
    859  1.34.8.2  nathanw 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
    860  1.34.8.2  nathanw 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
    861  1.34.8.2  nathanw 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    862  1.34.8.2  nathanw 		splx(s);
    863  1.34.8.2  nathanw 		break;
    864  1.34.8.2  nathanw 	case TIOCGFLAGS: {
    865  1.34.8.2  nathanw 		int bits = 0;
    866  1.34.8.2  nathanw 
    867  1.34.8.2  nathanw 		if(cs->cs_softcar)
    868  1.34.8.2  nathanw 			bits |= TIOCFLAG_SOFTCAR;
    869  1.34.8.2  nathanw 		if(cs->cs_creg[15] & ZSWR15_DCD_IE)
    870  1.34.8.2  nathanw 			bits |= TIOCFLAG_CLOCAL;
    871  1.34.8.2  nathanw 		if(cs->cs_creg[3] & ZSWR3_HFC)
    872  1.34.8.2  nathanw 			bits |= TIOCFLAG_CRTSCTS;
    873  1.34.8.2  nathanw 		*(int *)data = bits;
    874  1.34.8.2  nathanw 		break;
    875  1.34.8.2  nathanw 	}
    876  1.34.8.2  nathanw 	case TIOCSFLAGS: {
    877  1.34.8.2  nathanw 		int userbits = 0;
    878  1.34.8.2  nathanw 
    879  1.34.8.2  nathanw 		error = suser(p->p_ucred, &p->p_acflag);
    880  1.34.8.2  nathanw 		if(error != 0)
    881  1.34.8.2  nathanw 			return (EPERM);
    882  1.34.8.2  nathanw 
    883  1.34.8.2  nathanw 		userbits = *(int *)data;
    884  1.34.8.2  nathanw 
    885  1.34.8.2  nathanw 		/*
    886  1.34.8.2  nathanw 		 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
    887  1.34.8.2  nathanw 		 # defaulting to software flow control.
    888  1.34.8.2  nathanw 		 */
    889  1.34.8.2  nathanw 		if(userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
    890  1.34.8.2  nathanw 			return(EINVAL);
    891  1.34.8.2  nathanw 		if(userbits & TIOCFLAG_MDMBUF)	/* don't support this (yet?) */
    892  1.34.8.2  nathanw 			return(ENODEV);
    893  1.34.8.2  nathanw 
    894  1.34.8.2  nathanw 		s = splzs();
    895  1.34.8.2  nathanw 		if((userbits & TIOCFLAG_SOFTCAR)) {
    896  1.34.8.2  nathanw 			cs->cs_softcar = 1;	/* turn on softcar */
    897  1.34.8.2  nathanw 			cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
    898  1.34.8.2  nathanw 			cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
    899  1.34.8.2  nathanw 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    900  1.34.8.2  nathanw 		}
    901  1.34.8.2  nathanw 		else if(userbits & TIOCFLAG_CLOCAL) {
    902  1.34.8.2  nathanw 			cs->cs_softcar = 0; 	/* turn off softcar */
    903  1.34.8.2  nathanw 			cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
    904  1.34.8.2  nathanw 			cs->cs_creg[15] |= ZSWR15_DCD_IE;
    905  1.34.8.2  nathanw 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    906  1.34.8.2  nathanw 			tp->t_termios.c_cflag |= CLOCAL;
    907  1.34.8.2  nathanw 		}
    908  1.34.8.2  nathanw 		if(userbits & TIOCFLAG_CRTSCTS) {
    909  1.34.8.2  nathanw 			cs->cs_preg[15] |= ZSWR15_CTS_IE;
    910  1.34.8.2  nathanw 			cs->cs_creg[15] |= ZSWR15_CTS_IE;
    911  1.34.8.2  nathanw 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    912  1.34.8.2  nathanw 			cs->cs_preg[3] |= ZSWR3_HFC;
    913  1.34.8.2  nathanw 			cs->cs_creg[3] |= ZSWR3_HFC;
    914  1.34.8.2  nathanw 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
    915  1.34.8.2  nathanw 			tp->t_termios.c_cflag |= CRTSCTS;
    916  1.34.8.2  nathanw 		}
    917  1.34.8.2  nathanw 		else {
    918  1.34.8.2  nathanw 			/* no mdmbuf, so we must want software flow control */
    919  1.34.8.2  nathanw 			cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
    920  1.34.8.2  nathanw 			cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
    921  1.34.8.2  nathanw 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    922  1.34.8.2  nathanw 			cs->cs_preg[3] &= ~ZSWR3_HFC;
    923  1.34.8.2  nathanw 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    924  1.34.8.2  nathanw 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
    925  1.34.8.2  nathanw 			tp->t_termios.c_cflag &= ~CRTSCTS;
    926  1.34.8.2  nathanw 		}
    927  1.34.8.2  nathanw 		splx(s);
    928  1.34.8.2  nathanw 		break;
    929  1.34.8.2  nathanw 	}
    930  1.34.8.2  nathanw 	case TIOCSDTR:
    931  1.34.8.2  nathanw 		zs_modem(cs, ZSWR5_DTR, DMBIS);
    932  1.34.8.2  nathanw 		break;
    933  1.34.8.2  nathanw 	case TIOCCDTR:
    934  1.34.8.2  nathanw 		zs_modem(cs, ZSWR5_DTR, DMBIC);
    935  1.34.8.2  nathanw 		break;
    936  1.34.8.2  nathanw 	case TIOCMGET:
    937  1.34.8.2  nathanw 		zs_modem(cs, 0, DMGET);
    938  1.34.8.2  nathanw 		break;
    939  1.34.8.2  nathanw 	case TIOCMSET:
    940  1.34.8.2  nathanw 	case TIOCMBIS:
    941  1.34.8.2  nathanw 	case TIOCMBIC:
    942  1.34.8.2  nathanw 	default:
    943  1.34.8.2  nathanw 		return (EPASSTHROUGH);
    944  1.34.8.2  nathanw 	}
    945  1.34.8.2  nathanw 	return (0);
    946  1.34.8.2  nathanw }
    947  1.34.8.2  nathanw 
    948  1.34.8.2  nathanw /*
    949  1.34.8.2  nathanw  * Start or restart transmission.
    950  1.34.8.2  nathanw  */
    951  1.34.8.2  nathanw static void
    952  1.34.8.2  nathanw zsstart(tp)
    953  1.34.8.2  nathanw register struct tty *tp;
    954  1.34.8.2  nathanw {
    955  1.34.8.2  nathanw 	register struct zs_chanstate	*cs;
    956  1.34.8.2  nathanw 	register int			s, nch;
    957  1.34.8.2  nathanw 		 int			unit = ZS_UNIT(tp->t_dev);
    958  1.34.8.2  nathanw 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
    959  1.34.8.2  nathanw 
    960  1.34.8.2  nathanw 	cs = &zi->zi_cs[unit & 1];
    961  1.34.8.2  nathanw 	s  = spltty();
    962  1.34.8.2  nathanw 
    963  1.34.8.2  nathanw 	/*
    964  1.34.8.2  nathanw 	 * If currently active or delaying, no need to do anything.
    965  1.34.8.2  nathanw 	 */
    966  1.34.8.2  nathanw 	if(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
    967  1.34.8.2  nathanw 		goto out;
    968  1.34.8.2  nathanw 
    969  1.34.8.2  nathanw 	/*
    970  1.34.8.2  nathanw 	 * If there are sleepers, and output has drained below low
    971  1.34.8.2  nathanw 	 * water mark, awaken.
    972  1.34.8.2  nathanw 	 */
    973  1.34.8.2  nathanw 	if(tp->t_outq.c_cc <= tp->t_lowat) {
    974  1.34.8.2  nathanw 		if(tp->t_state & TS_ASLEEP) {
    975  1.34.8.2  nathanw 			tp->t_state &= ~TS_ASLEEP;
    976  1.34.8.2  nathanw 			wakeup((caddr_t)&tp->t_outq);
    977  1.34.8.2  nathanw 		}
    978  1.34.8.2  nathanw 		selwakeup(&tp->t_wsel);
    979  1.34.8.2  nathanw 	}
    980  1.34.8.2  nathanw 
    981  1.34.8.2  nathanw 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
    982  1.34.8.2  nathanw 	if(nch) {
    983  1.34.8.2  nathanw 		register char *p = tp->t_outq.c_cf;
    984  1.34.8.2  nathanw 
    985  1.34.8.2  nathanw 		/* mark busy, enable tx done interrupts, & send first byte */
    986  1.34.8.2  nathanw 		tp->t_state |= TS_BUSY;
    987  1.34.8.2  nathanw 		(void) splzs();
    988  1.34.8.2  nathanw 		cs->cs_preg[1] |= ZSWR1_TIE;
    989  1.34.8.2  nathanw 		cs->cs_creg[1] |= ZSWR1_TIE;
    990  1.34.8.2  nathanw 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
    991  1.34.8.2  nathanw 		cs->cs_zc->zc_data = *p;
    992  1.34.8.2  nathanw 		cs->cs_tba = p + 1;
    993  1.34.8.2  nathanw 		cs->cs_tbc = nch - 1;
    994  1.34.8.2  nathanw 	} else {
    995  1.34.8.2  nathanw 		/*
    996  1.34.8.2  nathanw 		 * Nothing to send, turn off transmit done interrupts.
    997  1.34.8.2  nathanw 		 * This is useful if something is doing polled output.
    998  1.34.8.2  nathanw 		 */
    999  1.34.8.2  nathanw 		(void) splzs();
   1000  1.34.8.2  nathanw 		cs->cs_preg[1] &= ~ZSWR1_TIE;
   1001  1.34.8.2  nathanw 		cs->cs_creg[1] &= ~ZSWR1_TIE;
   1002  1.34.8.2  nathanw 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1003  1.34.8.2  nathanw 	}
   1004  1.34.8.2  nathanw out:
   1005  1.34.8.2  nathanw 	splx(s);
   1006  1.34.8.2  nathanw }
   1007  1.34.8.2  nathanw 
   1008  1.34.8.2  nathanw /*
   1009  1.34.8.2  nathanw  * Stop output, e.g., for ^S or output flush.
   1010  1.34.8.2  nathanw  */
   1011  1.34.8.2  nathanw void
   1012  1.34.8.2  nathanw zsstop(tp, flag)
   1013  1.34.8.2  nathanw register struct tty	*tp;
   1014  1.34.8.2  nathanw 	 int		flag;
   1015  1.34.8.2  nathanw {
   1016  1.34.8.2  nathanw 	register struct zs_chanstate	*cs;
   1017  1.34.8.2  nathanw 	register int			s, unit = ZS_UNIT(tp->t_dev);
   1018  1.34.8.2  nathanw 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
   1019  1.34.8.2  nathanw 
   1020  1.34.8.2  nathanw 	cs = &zi->zi_cs[unit & 1];
   1021  1.34.8.2  nathanw 	s  = splzs();
   1022  1.34.8.2  nathanw 	if(tp->t_state & TS_BUSY) {
   1023  1.34.8.2  nathanw 		/*
   1024  1.34.8.2  nathanw 		 * Device is transmitting; must stop it.
   1025  1.34.8.2  nathanw 		 */
   1026  1.34.8.2  nathanw 		cs->cs_tbc = 0;
   1027  1.34.8.2  nathanw 		if ((tp->t_state & TS_TTSTOP) == 0)
   1028  1.34.8.2  nathanw 			tp->t_state |= TS_FLUSH;
   1029  1.34.8.2  nathanw 	}
   1030  1.34.8.2  nathanw 	splx(s);
   1031  1.34.8.2  nathanw }
   1032  1.34.8.2  nathanw 
   1033  1.34.8.2  nathanw static void
   1034  1.34.8.2  nathanw zs_shutdown(cs)
   1035  1.34.8.2  nathanw 	struct zs_chanstate	*cs;
   1036  1.34.8.2  nathanw {
   1037  1.34.8.2  nathanw 	struct tty	*tp = cs->cs_ttyp;
   1038  1.34.8.2  nathanw 	int		s;
   1039  1.34.8.2  nathanw 
   1040  1.34.8.2  nathanw 	s = splzs();
   1041  1.34.8.2  nathanw 
   1042  1.34.8.2  nathanw 	/*
   1043  1.34.8.2  nathanw 	 * Hang up if necessary.  Wait a bit, so the other side has time to
   1044  1.34.8.2  nathanw 	 * notice even if we immediately open the port again.
   1045  1.34.8.2  nathanw 	 */
   1046  1.34.8.2  nathanw 	if(tp->t_cflag & HUPCL) {
   1047  1.34.8.2  nathanw 		zs_modem(cs, 0, DMSET);
   1048  1.34.8.2  nathanw 		(void)tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
   1049  1.34.8.2  nathanw 	}
   1050  1.34.8.2  nathanw 
   1051  1.34.8.2  nathanw 	/* Clear any break condition set with TIOCSBRK. */
   1052  1.34.8.2  nathanw 	if(cs->cs_creg[5] & ZSWR5_BREAK) {
   1053  1.34.8.2  nathanw 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
   1054  1.34.8.2  nathanw 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
   1055  1.34.8.2  nathanw 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1056  1.34.8.2  nathanw 	}
   1057  1.34.8.2  nathanw 
   1058  1.34.8.2  nathanw 	/*
   1059  1.34.8.2  nathanw 	 * Drop all lines and cancel interrupts
   1060  1.34.8.2  nathanw 	 */
   1061  1.34.8.2  nathanw 	zs_loadchannelregs(cs->cs_zc, zs_init_regs);
   1062  1.34.8.2  nathanw 	splx(s);
   1063  1.34.8.2  nathanw }
   1064  1.34.8.2  nathanw 
   1065  1.34.8.2  nathanw /*
   1066  1.34.8.2  nathanw  * Set ZS tty parameters from termios.
   1067  1.34.8.2  nathanw  *
   1068  1.34.8.2  nathanw  * This routine makes use of the fact that only registers
   1069  1.34.8.2  nathanw  * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
   1070  1.34.8.2  nathanw  */
   1071  1.34.8.2  nathanw static int
   1072  1.34.8.2  nathanw zsparam(tp, t)
   1073  1.34.8.2  nathanw register struct tty	*tp;
   1074  1.34.8.2  nathanw register struct termios	*t;
   1075  1.34.8.2  nathanw {
   1076  1.34.8.2  nathanw 		 int			unit = ZS_UNIT(tp->t_dev);
   1077  1.34.8.2  nathanw 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
   1078  1.34.8.2  nathanw 	register struct zs_chanstate	*cs = &zi->zi_cs[unit & 1];
   1079  1.34.8.2  nathanw 		 int			cdiv, clkm, brgm, tcon;
   1080  1.34.8.2  nathanw 	register int			tmp, tmp5, cflag, s;
   1081  1.34.8.2  nathanw 
   1082  1.34.8.2  nathanw 	tmp  = t->c_ospeed;
   1083  1.34.8.2  nathanw 	tmp5 = t->c_ispeed;
   1084  1.34.8.2  nathanw 	if(tmp < 0 || (tmp5 && tmp5 != tmp))
   1085  1.34.8.2  nathanw 		return(EINVAL);
   1086  1.34.8.2  nathanw 	if(tmp == 0) {
   1087  1.34.8.2  nathanw 		/* stty 0 => drop DTR and RTS */
   1088  1.34.8.2  nathanw 		zs_modem(cs, 0, DMSET);
   1089  1.34.8.2  nathanw 		return(0);
   1090  1.34.8.2  nathanw 	}
   1091  1.34.8.2  nathanw 	tmp = zsbaudrate(unit, tmp, &cdiv, &clkm, &brgm, &tcon);
   1092  1.34.8.2  nathanw 	if (tmp < 0)
   1093  1.34.8.2  nathanw 		return(EINVAL);
   1094  1.34.8.2  nathanw 	tp->t_ispeed = tp->t_ospeed = tmp;
   1095  1.34.8.2  nathanw 
   1096  1.34.8.2  nathanw 	cflag = tp->t_cflag = t->c_cflag;
   1097  1.34.8.2  nathanw 	if (cflag & CSTOPB)
   1098  1.34.8.2  nathanw 		cdiv |= ZSWR4_TWOSB;
   1099  1.34.8.2  nathanw 	else
   1100  1.34.8.2  nathanw 		cdiv |= ZSWR4_ONESB;
   1101  1.34.8.2  nathanw 	if (!(cflag & PARODD))
   1102  1.34.8.2  nathanw 		cdiv |= ZSWR4_EVENP;
   1103  1.34.8.2  nathanw 	if (cflag & PARENB)
   1104  1.34.8.2  nathanw 		cdiv |= ZSWR4_PARENB;
   1105  1.34.8.2  nathanw 
   1106  1.34.8.2  nathanw 	switch(cflag & CSIZE) {
   1107  1.34.8.2  nathanw 	case CS5:
   1108  1.34.8.2  nathanw 		tmp  = ZSWR3_RX_5;
   1109  1.34.8.2  nathanw 		tmp5 = ZSWR5_TX_5;
   1110  1.34.8.2  nathanw 		break;
   1111  1.34.8.2  nathanw 	case CS6:
   1112  1.34.8.2  nathanw 		tmp  = ZSWR3_RX_6;
   1113  1.34.8.2  nathanw 		tmp5 = ZSWR5_TX_6;
   1114  1.34.8.2  nathanw 		break;
   1115  1.34.8.2  nathanw 	case CS7:
   1116  1.34.8.2  nathanw 		tmp  = ZSWR3_RX_7;
   1117  1.34.8.2  nathanw 		tmp5 = ZSWR5_TX_7;
   1118  1.34.8.2  nathanw 		break;
   1119  1.34.8.2  nathanw 	case CS8:
   1120  1.34.8.2  nathanw 	default:
   1121  1.34.8.2  nathanw 		tmp  = ZSWR3_RX_8;
   1122  1.34.8.2  nathanw 		tmp5 = ZSWR5_TX_8;
   1123  1.34.8.2  nathanw 		break;
   1124  1.34.8.2  nathanw 	}
   1125  1.34.8.2  nathanw 	tmp  |= ZSWR3_RX_ENABLE;
   1126  1.34.8.2  nathanw 	tmp5 |= ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
   1127  1.34.8.2  nathanw 
   1128  1.34.8.2  nathanw 	/*
   1129  1.34.8.2  nathanw 	 * Block interrupts so that state will not
   1130  1.34.8.2  nathanw 	 * be altered until we are done setting it up.
   1131  1.34.8.2  nathanw 	 */
   1132  1.34.8.2  nathanw 	s = splzs();
   1133  1.34.8.2  nathanw 	cs->cs_preg[4]  = cdiv;
   1134  1.34.8.2  nathanw 	cs->cs_preg[11] = clkm;
   1135  1.34.8.2  nathanw 	cs->cs_preg[12] = tcon;
   1136  1.34.8.2  nathanw 	cs->cs_preg[13] = tcon >> 8;
   1137  1.34.8.2  nathanw 	cs->cs_preg[14] = brgm;
   1138  1.34.8.2  nathanw 	cs->cs_preg[1]  = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
   1139  1.34.8.2  nathanw 	cs->cs_preg[9]  = ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT;
   1140  1.34.8.2  nathanw 	cs->cs_preg[10] = ZSWR10_NRZ;
   1141  1.34.8.2  nathanw 	cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
   1142  1.34.8.2  nathanw 
   1143  1.34.8.2  nathanw 	/*
   1144  1.34.8.2  nathanw 	 * Output hardware flow control on the chip is horrendous: if
   1145  1.34.8.2  nathanw 	 * carrier detect drops, the receiver is disabled.  Hence we
   1146  1.34.8.2  nathanw 	 * can only do this when the carrier is on.
   1147  1.34.8.2  nathanw 	 */
   1148  1.34.8.2  nathanw 	if(cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
   1149  1.34.8.2  nathanw 		tmp |= ZSWR3_HFC;
   1150  1.34.8.2  nathanw 	cs->cs_preg[3] = tmp;
   1151  1.34.8.2  nathanw 	cs->cs_preg[5] = tmp5;
   1152  1.34.8.2  nathanw 
   1153  1.34.8.2  nathanw 	/*
   1154  1.34.8.2  nathanw 	 * If nothing is being transmitted, set up new current values,
   1155  1.34.8.2  nathanw 	 * else mark them as pending.
   1156  1.34.8.2  nathanw 	 */
   1157  1.34.8.2  nathanw 	if(cs->cs_heldchange == 0) {
   1158  1.34.8.2  nathanw 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1159  1.34.8.2  nathanw 			cs->cs_heldtbc = cs->cs_tbc;
   1160  1.34.8.2  nathanw 			cs->cs_tbc = 0;
   1161  1.34.8.2  nathanw 			cs->cs_heldchange = 1;
   1162  1.34.8.2  nathanw 		} else {
   1163  1.34.8.2  nathanw 			bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
   1164  1.34.8.2  nathanw 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
   1165  1.34.8.2  nathanw 		}
   1166  1.34.8.2  nathanw 	}
   1167  1.34.8.2  nathanw 	splx(s);
   1168  1.34.8.2  nathanw 	return (0);
   1169  1.34.8.2  nathanw }
   1170  1.34.8.2  nathanw 
   1171  1.34.8.2  nathanw /*
   1172  1.34.8.2  nathanw  * search for the best matching baudrate
   1173  1.34.8.2  nathanw  */
   1174  1.34.8.2  nathanw static int
   1175  1.34.8.2  nathanw zsbaudrate(unit, wanted, divisor, clockmode, brgenmode, timeconst)
   1176  1.34.8.2  nathanw int	unit, wanted, *divisor, *clockmode, *brgenmode, *timeconst;
   1177  1.34.8.2  nathanw {
   1178  1.34.8.2  nathanw 	int	bestdiff, bestbps, source;
   1179  1.34.8.2  nathanw 
   1180  1.34.8.2  nathanw 	bestdiff = bestbps = 0;
   1181  1.34.8.2  nathanw 	unit = (unit & 1) << 2;
   1182  1.34.8.2  nathanw 	for (source = 0; source < 4; ++source) {
   1183  1.34.8.2  nathanw 		long	freq = zs_frequencies[unit + source];
   1184  1.34.8.2  nathanw 		int	diff, bps, div, clkm, brgm, tcon;
   1185  1.34.8.2  nathanw 
   1186  1.34.8.2  nathanw 		bps = div = clkm = brgm = tcon = 0;
   1187  1.34.8.2  nathanw 		switch (source) {
   1188  1.34.8.2  nathanw 			case 0:	/* BRgen, PCLK */
   1189  1.34.8.2  nathanw 				brgm = ZSWR14_BAUD_ENA|ZSWR14_BAUD_FROM_PCLK;
   1190  1.34.8.2  nathanw 				break;
   1191  1.34.8.2  nathanw 			case 1:	/* BRgen, RTxC */
   1192  1.34.8.2  nathanw 				brgm = ZSWR14_BAUD_ENA;
   1193  1.34.8.2  nathanw 				break;
   1194  1.34.8.2  nathanw 			case 2: /* RTxC */
   1195  1.34.8.2  nathanw 				clkm = ZSWR11_RXCLK_RTXC|ZSWR11_TXCLK_RTXC;
   1196  1.34.8.2  nathanw 				break;
   1197  1.34.8.2  nathanw 			case 3: /* TRxC */
   1198  1.34.8.2  nathanw 				clkm = ZSWR11_RXCLK_TRXC|ZSWR11_TXCLK_TRXC;
   1199  1.34.8.2  nathanw 				break;
   1200  1.34.8.2  nathanw 		}
   1201  1.34.8.2  nathanw 		switch (source) {
   1202  1.34.8.2  nathanw 			case 0:
   1203  1.34.8.2  nathanw 			case 1:
   1204  1.34.8.2  nathanw 				div  = ZSWR4_CLK_X16;
   1205  1.34.8.2  nathanw 				clkm = ZSWR11_RXCLK_BAUD|ZSWR11_TXCLK_BAUD;
   1206  1.34.8.2  nathanw 				tcon = BPS_TO_TCONST(freq, wanted);
   1207  1.34.8.2  nathanw 				if (tcon < 0)
   1208  1.34.8.2  nathanw 					tcon = 0;
   1209  1.34.8.2  nathanw 				bps  = TCONST_TO_BPS(freq, tcon);
   1210  1.34.8.2  nathanw 				break;
   1211  1.34.8.2  nathanw 			case 2:
   1212  1.34.8.2  nathanw 			case 3:
   1213  1.34.8.2  nathanw 			{	int	b1 = freq / 16, d1 = abs(b1 - wanted);
   1214  1.34.8.2  nathanw 				int	b2 = freq / 32, d2 = abs(b2 - wanted);
   1215  1.34.8.2  nathanw 				int	b3 = freq / 64, d3 = abs(b3 - wanted);
   1216  1.34.8.2  nathanw 
   1217  1.34.8.2  nathanw 				if (d1 < d2 && d1 < d3) {
   1218  1.34.8.2  nathanw 					div = ZSWR4_CLK_X16;
   1219  1.34.8.2  nathanw 					bps = b1;
   1220  1.34.8.2  nathanw 				} else if (d2 < d3 && d2 < d1) {
   1221  1.34.8.2  nathanw 					div = ZSWR4_CLK_X32;
   1222  1.34.8.2  nathanw 					bps = b2;
   1223  1.34.8.2  nathanw 				} else {
   1224  1.34.8.2  nathanw 					div = ZSWR4_CLK_X64;
   1225  1.34.8.2  nathanw 					bps = b3;
   1226  1.34.8.2  nathanw 				}
   1227  1.34.8.2  nathanw 				brgm = tcon = 0;
   1228  1.34.8.2  nathanw 				break;
   1229  1.34.8.2  nathanw 			}
   1230  1.34.8.2  nathanw 		}
   1231  1.34.8.2  nathanw 		diff = abs(bps - wanted);
   1232  1.34.8.2  nathanw 		if (!source || diff < bestdiff) {
   1233  1.34.8.2  nathanw 			*divisor   = div;
   1234  1.34.8.2  nathanw 			*clockmode = clkm;
   1235  1.34.8.2  nathanw 			*brgenmode = brgm;
   1236  1.34.8.2  nathanw 			*timeconst = tcon;
   1237  1.34.8.2  nathanw 			bestbps    = bps;
   1238  1.34.8.2  nathanw 			bestdiff   = diff;
   1239  1.34.8.2  nathanw 			if (diff == 0)
   1240  1.34.8.2  nathanw 				break;
   1241  1.34.8.2  nathanw 		}
   1242  1.34.8.2  nathanw 	}
   1243  1.34.8.2  nathanw 	/* Allow deviations upto 5% */
   1244  1.34.8.2  nathanw 	if (20 * bestdiff > wanted)
   1245  1.34.8.2  nathanw 		return -1;
   1246  1.34.8.2  nathanw 	return bestbps;
   1247  1.34.8.2  nathanw }
   1248  1.34.8.2  nathanw 
   1249  1.34.8.2  nathanw /*
   1250  1.34.8.2  nathanw  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1251  1.34.8.2  nathanw  * in transmission, the change is deferred.
   1252  1.34.8.2  nathanw  */
   1253  1.34.8.2  nathanw static int
   1254  1.34.8.2  nathanw zs_modem(cs, bits, how)
   1255  1.34.8.2  nathanw struct zs_chanstate	*cs;
   1256  1.34.8.2  nathanw int			bits, how;
   1257  1.34.8.2  nathanw {
   1258  1.34.8.2  nathanw 	int s, mbits;
   1259  1.34.8.2  nathanw 
   1260  1.34.8.2  nathanw 	bits  &= ZSWR5_DTR | ZSWR5_RTS;
   1261  1.34.8.2  nathanw 
   1262  1.34.8.2  nathanw 	s = splzs();
   1263  1.34.8.2  nathanw 	mbits  = cs->cs_preg[5] &  (ZSWR5_DTR | ZSWR5_RTS);
   1264  1.34.8.2  nathanw 
   1265  1.34.8.2  nathanw 	switch(how) {
   1266  1.34.8.2  nathanw 		case DMSET:
   1267  1.34.8.2  nathanw 				mbits  = bits;
   1268  1.34.8.2  nathanw 				break;
   1269  1.34.8.2  nathanw 		case DMBIS:
   1270  1.34.8.2  nathanw 				mbits |= bits;
   1271  1.34.8.2  nathanw 				break;
   1272  1.34.8.2  nathanw 		case DMBIC:
   1273  1.34.8.2  nathanw 				mbits &= ~bits;
   1274  1.34.8.2  nathanw 				break;
   1275  1.34.8.2  nathanw 		case DMGET:
   1276  1.34.8.2  nathanw 				splx(s);
   1277  1.34.8.2  nathanw 				return(mbits);
   1278  1.34.8.2  nathanw 	}
   1279  1.34.8.2  nathanw 
   1280  1.34.8.2  nathanw 	cs->cs_preg[5] = (cs->cs_preg[5] & ~(ZSWR5_DTR | ZSWR5_RTS)) | mbits;
   1281  1.34.8.2  nathanw 	if(cs->cs_heldchange == 0) {
   1282  1.34.8.2  nathanw 		if(cs->cs_ttyp->t_state & TS_BUSY) {
   1283  1.34.8.2  nathanw 			cs->cs_heldtbc = cs->cs_tbc;
   1284  1.34.8.2  nathanw 			cs->cs_tbc = 0;
   1285  1.34.8.2  nathanw 			cs->cs_heldchange = 1;
   1286  1.34.8.2  nathanw 		}
   1287  1.34.8.2  nathanw 		else {
   1288  1.34.8.2  nathanw 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1289  1.34.8.2  nathanw 		}
   1290  1.34.8.2  nathanw 	}
   1291  1.34.8.2  nathanw 	splx(s);
   1292  1.34.8.2  nathanw 	return(0);
   1293  1.34.8.2  nathanw }
   1294  1.34.8.2  nathanw 
   1295  1.34.8.2  nathanw /*
   1296  1.34.8.2  nathanw  * Write the given register set to the given zs channel in the proper order.
   1297  1.34.8.2  nathanw  * The channel must not be transmitting at the time.  The receiver will
   1298  1.34.8.2  nathanw  * be disabled for the time it takes to write all the registers.
   1299  1.34.8.2  nathanw  */
   1300  1.34.8.2  nathanw static void
   1301  1.34.8.2  nathanw zs_loadchannelregs(zc, reg)
   1302  1.34.8.2  nathanw volatile struct zschan	*zc;
   1303  1.34.8.2  nathanw u_char			*reg;
   1304  1.34.8.2  nathanw {
   1305  1.34.8.2  nathanw 	int i;
   1306  1.34.8.2  nathanw 
   1307  1.34.8.2  nathanw 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
   1308  1.34.8.2  nathanw 	i = zc->zc_data;		/* drain fifo */
   1309  1.34.8.2  nathanw 	i = zc->zc_data;
   1310  1.34.8.2  nathanw 	i = zc->zc_data;
   1311  1.34.8.2  nathanw 	ZS_WRITE(zc,  4, reg[4]);
   1312  1.34.8.2  nathanw 	ZS_WRITE(zc, 10, reg[10]);
   1313  1.34.8.2  nathanw 	ZS_WRITE(zc,  3, reg[3] & ~ZSWR3_RX_ENABLE);
   1314  1.34.8.2  nathanw 	ZS_WRITE(zc,  5, reg[5] & ~ZSWR5_TX_ENABLE);
   1315  1.34.8.2  nathanw 	ZS_WRITE(zc,  1, reg[1]);
   1316  1.34.8.2  nathanw 	ZS_WRITE(zc,  9, reg[9]);
   1317  1.34.8.2  nathanw 	ZS_WRITE(zc, 11, reg[11]);
   1318  1.34.8.2  nathanw 	ZS_WRITE(zc, 12, reg[12]);
   1319  1.34.8.2  nathanw 	ZS_WRITE(zc, 13, reg[13]);
   1320  1.34.8.2  nathanw 	ZS_WRITE(zc, 14, reg[14]);
   1321  1.34.8.2  nathanw 	ZS_WRITE(zc, 15, reg[15]);
   1322  1.34.8.2  nathanw 	ZS_WRITE(zc,  3, reg[3]);
   1323  1.34.8.2  nathanw 	ZS_WRITE(zc,  5, reg[5]);
   1324  1.34.8.2  nathanw }
   1325  1.34.8.2  nathanw #endif /* NZS > 1 */
   1326