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