Home | History | Annotate | Line # | Download | only in dev
scif.c revision 1.8
      1  1.8  thorpej /* $NetBSD: scif.c,v 1.8 2000/03/23 06:43:52 thorpej Exp $ */
      2  1.1   itojun 
      3  1.1   itojun /*-
      4  1.1   itojun  * Copyright (C) 1999 T.Horiuchi and SAITOH Masanobu.  All rights reserved.
      5  1.1   itojun  *
      6  1.1   itojun  * Redistribution and use in source and binary forms, with or without
      7  1.1   itojun  * modification, are permitted provided that the following conditions
      8  1.1   itojun  * are met:
      9  1.1   itojun  * 1. Redistributions of source code must retain the above copyright
     10  1.1   itojun  *    notice, this list of conditions and the following disclaimer.
     11  1.1   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   itojun  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   itojun  *    documentation and/or other materials provided with the distribution.
     14  1.1   itojun  * 3. The name of the author may not be used to endorse or promote products
     15  1.1   itojun  *    derived from this software without specific prior written permission.
     16  1.1   itojun  *
     17  1.1   itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1   itojun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1   itojun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1   itojun  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1   itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1   itojun  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1   itojun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1   itojun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1   itojun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  1.1   itojun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1   itojun  */
     28  1.1   itojun 
     29  1.2  msaitoh /*-
     30  1.2  msaitoh  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
     31  1.2  msaitoh  * All rights reserved.
     32  1.2  msaitoh  *
     33  1.2  msaitoh  * This code is derived from software contributed to The NetBSD Foundation
     34  1.2  msaitoh  * by Charles M. Hannum.
     35  1.2  msaitoh  *
     36  1.2  msaitoh  * Redistribution and use in source and binary forms, with or without
     37  1.2  msaitoh  * modification, are permitted provided that the following conditions
     38  1.2  msaitoh  * are met:
     39  1.2  msaitoh  * 1. Redistributions of source code must retain the above copyright
     40  1.2  msaitoh  *    notice, this list of conditions and the following disclaimer.
     41  1.2  msaitoh  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.2  msaitoh  *    notice, this list of conditions and the following disclaimer in the
     43  1.2  msaitoh  *    documentation and/or other materials provided with the distribution.
     44  1.2  msaitoh  * 3. All advertising materials mentioning features or use of this software
     45  1.2  msaitoh  *    must display the following acknowledgement:
     46  1.2  msaitoh  *        This product includes software developed by the NetBSD
     47  1.2  msaitoh  *        Foundation, Inc. and its contributors.
     48  1.2  msaitoh  * 4. Neither the name of The NetBSD Foundation nor the names of its
     49  1.2  msaitoh  *    contributors may be used to endorse or promote products derived
     50  1.2  msaitoh  *    from this software without specific prior written permission.
     51  1.2  msaitoh  *
     52  1.2  msaitoh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     53  1.2  msaitoh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     54  1.2  msaitoh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     55  1.2  msaitoh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     56  1.2  msaitoh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     57  1.2  msaitoh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     58  1.2  msaitoh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     59  1.2  msaitoh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     60  1.2  msaitoh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     61  1.2  msaitoh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     62  1.2  msaitoh  * POSSIBILITY OF SUCH DAMAGE.
     63  1.2  msaitoh  */
     64  1.2  msaitoh 
     65  1.2  msaitoh /*
     66  1.2  msaitoh  * Copyright (c) 1991 The Regents of the University of California.
     67  1.2  msaitoh  * All rights reserved.
     68  1.2  msaitoh  *
     69  1.2  msaitoh  * Redistribution and use in source and binary forms, with or without
     70  1.2  msaitoh  * modification, are permitted provided that the following conditions
     71  1.2  msaitoh  * are met:
     72  1.2  msaitoh  * 1. Redistributions of source code must retain the above copyright
     73  1.2  msaitoh  *    notice, this list of conditions and the following disclaimer.
     74  1.2  msaitoh  * 2. Redistributions in binary form must reproduce the above copyright
     75  1.2  msaitoh  *    notice, this list of conditions and the following disclaimer in the
     76  1.2  msaitoh  *    documentation and/or other materials provided with the distribution.
     77  1.2  msaitoh  * 3. All advertising materials mentioning features or use of this software
     78  1.2  msaitoh  *    must display the following acknowledgement:
     79  1.2  msaitoh  *	This product includes software developed by the University of
     80  1.2  msaitoh  *	California, Berkeley and its contributors.
     81  1.2  msaitoh  * 4. Neither the name of the University nor the names of its contributors
     82  1.2  msaitoh  *    may be used to endorse or promote products derived from this software
     83  1.2  msaitoh  *    without specific prior written permission.
     84  1.2  msaitoh  *
     85  1.2  msaitoh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86  1.2  msaitoh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87  1.2  msaitoh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88  1.2  msaitoh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89  1.2  msaitoh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90  1.2  msaitoh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91  1.2  msaitoh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  1.2  msaitoh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  1.2  msaitoh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  1.2  msaitoh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  1.2  msaitoh  * SUCH DAMAGE.
     96  1.2  msaitoh  *
     97  1.2  msaitoh  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     98  1.2  msaitoh  */
     99  1.2  msaitoh 
    100  1.2  msaitoh /*
    101  1.2  msaitoh  * SH internal serial driver
    102  1.2  msaitoh  *
    103  1.2  msaitoh  * This code is derived from both z8530tty.c and com.c
    104  1.2  msaitoh  */
    105  1.2  msaitoh 
    106  1.1   itojun #include "opt_pclock.h"
    107  1.1   itojun #include "opt_scif.h"
    108  1.1   itojun 
    109  1.1   itojun #include <sys/param.h>
    110  1.1   itojun #include <sys/systm.h>
    111  1.1   itojun #include <sys/tty.h>
    112  1.1   itojun #include <sys/proc.h>
    113  1.1   itojun #include <sys/conf.h>
    114  1.1   itojun #include <sys/file.h>
    115  1.1   itojun #include <sys/syslog.h>
    116  1.1   itojun #include <sys/kernel.h>
    117  1.1   itojun #include <sys/device.h>
    118  1.1   itojun #include <sys/malloc.h>
    119  1.1   itojun 
    120  1.1   itojun #include <dev/cons.h>
    121  1.1   itojun 
    122  1.1   itojun #include <machine/cpu.h>
    123  1.1   itojun #include <sh3/scifreg.h>
    124  1.1   itojun #include <sh3/tmureg.h>
    125  1.1   itojun 
    126  1.1   itojun #include <machine/shbvar.h>
    127  1.1   itojun 
    128  1.1   itojun static void	scifstart __P((struct tty *));
    129  1.1   itojun static int	scifparam __P((struct tty *, struct termios *));
    130  1.1   itojun 
    131  1.1   itojun void scifcnprobe __P((struct consdev *));
    132  1.1   itojun void scifcninit __P((struct consdev *));
    133  1.1   itojun void scifcnputc __P((dev_t, int));
    134  1.1   itojun int scifcngetc __P((dev_t));
    135  1.1   itojun void scifcnpoolc __P((dev_t, int));
    136  1.1   itojun void scif_intr_init __P((void));
    137  1.1   itojun int scifintr __P((void *));
    138  1.1   itojun 
    139  1.1   itojun struct scif_softc {
    140  1.1   itojun 	struct device sc_dev;		/* boilerplate */
    141  1.1   itojun 	struct tty *sc_tty;
    142  1.1   itojun 	void *sc_ih;
    143  1.1   itojun 
    144  1.8  thorpej 	struct callout sc_diag_ch;
    145  1.8  thorpej 
    146  1.1   itojun #if 0
    147  1.1   itojun 	bus_space_tag_t sc_iot;		/* ISA i/o space identifier */
    148  1.1   itojun 	bus_space_handle_t   sc_ioh;	/* ISA io handle */
    149  1.1   itojun 
    150  1.1   itojun 	int sc_drq;
    151  1.1   itojun 
    152  1.1   itojun 	int sc_frequency;
    153  1.1   itojun #endif
    154  1.1   itojun 
    155  1.1   itojun 	u_int sc_overflows,
    156  1.1   itojun 	      sc_floods,
    157  1.1   itojun 	      sc_errors;		/* number of retries so far */
    158  1.1   itojun 	u_char sc_status[7];		/* copy of registers */
    159  1.1   itojun 
    160  1.1   itojun 	int sc_hwflags;
    161  1.1   itojun 	int sc_swflags;
    162  1.1   itojun 	u_int sc_fifolen;
    163  1.1   itojun 
    164  1.1   itojun 	u_int sc_r_hiwat,
    165  1.1   itojun 	      sc_r_lowat;
    166  1.1   itojun 	u_char *volatile sc_rbget,
    167  1.1   itojun 	       *volatile sc_rbput;
    168  1.1   itojun  	volatile u_int sc_rbavail;
    169  1.1   itojun 	u_char *sc_rbuf,
    170  1.1   itojun 	       *sc_ebuf;
    171  1.1   itojun 
    172  1.1   itojun  	u_char *sc_tba;			/* transmit buffer address */
    173  1.1   itojun  	u_int sc_tbc,			/* transmit byte count */
    174  1.1   itojun 	      sc_heldtbc;
    175  1.1   itojun 
    176  1.1   itojun 	volatile u_char sc_rx_flags,
    177  1.1   itojun #define	RX_TTY_BLOCKED		0x01
    178  1.1   itojun #define	RX_TTY_OVERFLOWED	0x02
    179  1.1   itojun #define	RX_IBUF_BLOCKED		0x04
    180  1.1   itojun #define	RX_IBUF_OVERFLOWED	0x08
    181  1.1   itojun #define	RX_ANY_BLOCK		0x0f
    182  1.3  msaitoh 			sc_tx_busy,	/* working on an output chunk */
    183  1.3  msaitoh 			sc_tx_done,	/* done with one output chunk */
    184  1.2  msaitoh 			sc_tx_stopped,	/* H/W level stop (lost CTS) */
    185  1.2  msaitoh 			sc_st_check,	/* got a status interrupt */
    186  1.1   itojun 			sc_rx_ready;
    187  1.1   itojun 
    188  1.1   itojun 	volatile u_char sc_heldchange;
    189  1.1   itojun };
    190  1.1   itojun 
    191  1.1   itojun /* controller driver configuration */
    192  1.1   itojun static int scif_match __P((struct device *, struct cfdata *, void *));
    193  1.1   itojun static void scif_attach __P((struct device *, struct device *, void *));
    194  1.1   itojun 
    195  1.6  msaitoh void	scif_break	__P((struct scif_softc *, int));
    196  1.1   itojun void	scif_iflush	__P((struct scif_softc *));
    197  1.1   itojun 
    198  1.1   itojun #define	integrate	static inline
    199  1.1   itojun #ifdef __GENERIC_SOFT_INTERRUPTS
    200  1.1   itojun void 	scifsoft	__P((void *));
    201  1.1   itojun #else
    202  1.1   itojun #ifndef __NO_SOFT_SERIAL_INTERRUPT
    203  1.1   itojun void 	scifsoft	__P((void));
    204  1.1   itojun #else
    205  1.1   itojun void 	scifsoft	__P((void *));
    206  1.1   itojun #endif
    207  1.1   itojun #endif
    208  1.1   itojun integrate void scif_rxsoft	__P((struct scif_softc *, struct tty *));
    209  1.1   itojun integrate void scif_txsoft	__P((struct scif_softc *, struct tty *));
    210  1.1   itojun integrate void scif_stsoft	__P((struct scif_softc *, struct tty *));
    211  1.1   itojun integrate void scif_schedrx	__P((struct scif_softc *));
    212  1.1   itojun void	scifdiag		__P((void *));
    213  1.1   itojun 
    214  1.1   itojun 
    215  1.1   itojun #define	SCIFUNIT_MASK		0x7ffff
    216  1.1   itojun #define	SCIFDIALOUT_MASK	0x80000
    217  1.1   itojun 
    218  1.1   itojun #define	SCIFUNIT(x)	(minor(x) & SCIFUNIT_MASK)
    219  1.1   itojun #define	SCIFDIALOUT(x)	(minor(x) & SCIFDIALOUT_MASK)
    220  1.1   itojun 
    221  1.1   itojun /* Macros to clear/set/test flags. */
    222  1.1   itojun #define SET(t, f)	(t) |= (f)
    223  1.1   itojun #define CLR(t, f)	(t) &= ~(f)
    224  1.1   itojun #define ISSET(t, f)	((t) & (f))
    225  1.1   itojun 
    226  1.1   itojun /* Hardware flag masks */
    227  1.1   itojun #define	SCIF_HW_NOIEN	0x01
    228  1.1   itojun #define	SCIF_HW_FIFO	0x02
    229  1.1   itojun #define	SCIF_HW_FLOW	0x08
    230  1.1   itojun #define	SCIF_HW_DEV_OK	0x20
    231  1.1   itojun #define	SCIF_HW_CONSOLE	0x40
    232  1.1   itojun #define	SCIF_HW_KGDB	0x80
    233  1.1   itojun 
    234  1.1   itojun /* Buffer size for character buffer */
    235  1.1   itojun #define	SCIF_RING_SIZE	2048
    236  1.1   itojun 
    237  1.1   itojun /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    238  1.1   itojun u_int scif_rbuf_hiwat = (SCIF_RING_SIZE * 1) / 4;
    239  1.1   itojun u_int scif_rbuf_lowat = (SCIF_RING_SIZE * 3) / 4;
    240  1.1   itojun 
    241  1.1   itojun #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    242  1.1   itojun int scifconscflag = CONMODE;
    243  1.1   itojun 
    244  1.7  msaitoh #ifdef SCIFCN_SPEED
    245  1.7  msaitoh unsigned int scifcn_speed = SCIFCN_SPEED;
    246  1.7  msaitoh #else
    247  1.7  msaitoh unsigned int scifcn_speed = 9600;
    248  1.7  msaitoh #endif
    249  1.7  msaitoh 
    250  1.1   itojun #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    251  1.1   itojun 
    252  1.1   itojun #ifndef __GENERIC_SOFT_INTERRUPTS
    253  1.1   itojun #ifdef __NO_SOFT_SERIAL_INTERRUPT
    254  1.1   itojun volatile int	scif_softintr_scheduled;
    255  1.8  thorpej struct callout scif_soft_ch = CALLOUT_INITIALIZER;
    256  1.1   itojun #endif
    257  1.1   itojun #endif
    258  1.1   itojun 
    259  1.1   itojun u_int scif_rbuf_size = SCIF_RING_SIZE;
    260  1.1   itojun 
    261  1.1   itojun struct cfattach scif_ca = {
    262  1.1   itojun 	sizeof(struct scif_softc), scif_match, scif_attach
    263  1.1   itojun };
    264  1.1   itojun 
    265  1.1   itojun extern struct cfdriver scif_cd;
    266  1.1   itojun 
    267  1.1   itojun cdev_decl(scif);
    268  1.1   itojun 
    269  1.1   itojun void InitializeScif  __P((unsigned int));
    270  1.1   itojun 
    271  1.1   itojun /*
    272  1.1   itojun  * following functions are debugging prupose only
    273  1.1   itojun  */
    274  1.1   itojun #define CR      0x0D
    275  1.1   itojun #define USART_ON (unsigned int)~0x08
    276  1.1   itojun 
    277  1.1   itojun static void WaitFor __P((int));
    278  1.1   itojun void PutcScif __P((unsigned char));
    279  1.1   itojun void PutStrScif __P((unsigned char *));
    280  1.1   itojun int ScifErrCheck __P((void));
    281  1.1   itojun unsigned char GetcScif __P((void));
    282  1.1   itojun int GetStrScif __P((unsigned char *, int));
    283  1.1   itojun 
    284  1.1   itojun /*
    285  1.1   itojun  * WaitFor
    286  1.1   itojun  * : int mSec;
    287  1.1   itojun  */
    288  1.1   itojun static void
    289  1.1   itojun WaitFor(mSec)
    290  1.1   itojun 	int mSec;
    291  1.1   itojun {
    292  1.1   itojun 
    293  1.1   itojun 	/* Disable Under Flow interrupt, rising edge, 1/4 */
    294  1.6  msaitoh 	SHREG_TCR2 = 0x0000;
    295  1.1   itojun 
    296  1.1   itojun 	/* Set counter value (count down with 4 KHz) */
    297  1.6  msaitoh 	SHREG_TCNT2 = mSec * 4;
    298  1.1   itojun 
    299  1.6  msaitoh 	/* start Channel2 */
    300  1.6  msaitoh 	SHREG_TSTR |= TSTR_STR2;
    301  1.1   itojun 
    302  1.6  msaitoh 	/* wait for under flag ON of channel2 */
    303  1.7  msaitoh 	while ((SHREG_TCR2 & TCR_UNF) == 0)
    304  1.1   itojun 		;
    305  1.1   itojun 
    306  1.6  msaitoh 	/* stop channel2 */
    307  1.6  msaitoh 	SHREG_TSTR &= ~TSTR_STR2;
    308  1.1   itojun }
    309  1.1   itojun 
    310  1.1   itojun /*
    311  1.1   itojun  * InitializeScif
    312  1.1   itojun  * : unsigned int bps;
    313  1.1   itojun  * : SCIF(Serial Communication Interface)
    314  1.1   itojun  */
    315  1.1   itojun 
    316  1.1   itojun void
    317  1.1   itojun InitializeScif(bps)
    318  1.1   itojun 	unsigned int bps;
    319  1.1   itojun {
    320  1.1   itojun 
    321  1.1   itojun 	/* Initialize SCR */
    322  1.3  msaitoh 	SHREG_SCSCR2 = 0x00;
    323  1.1   itojun 
    324  1.6  msaitoh #if 0
    325  1.6  msaitoh 	SHREG_SCFCR2 = SCFCR2_TFRST | SCFCR2_RFRST | SCFCR2_MCE;
    326  1.6  msaitoh #else
    327  1.1   itojun 	SHREG_SCFCR2 = SCFCR2_TFRST | SCFCR2_RFRST;
    328  1.6  msaitoh #endif
    329  1.7  msaitoh 	/* Serial Mode Register */
    330  1.3  msaitoh 	SHREG_SCSMR2 = 0x00;	/* 8bit,NonParity,Even,1Stop */
    331  1.1   itojun 
    332  1.7  msaitoh 	/* Bit Rate Register */
    333  1.7  msaitoh 	SHREG_SCBRR2 = divrnd(PCLOCK, 32 * bps) - 1;
    334  1.1   itojun 
    335  1.7  msaitoh 	/*
    336  1.7  msaitoh 	 * wait 1mSec, because Send/Recv must begin 1 bit period after
    337  1.7  msaitoh 	 * BRR is set.
    338  1.7  msaitoh 	 */
    339  1.1   itojun 	WaitFor(1);
    340  1.1   itojun 
    341  1.6  msaitoh #if 0
    342  1.6  msaitoh 	SHREG_SCFCR2 = FIFO_RCV_TRIGGER_14 | FIFO_XMT_TRIGGER_1 | SCFCR2_MCE;
    343  1.6  msaitoh #else
    344  1.1   itojun 	SHREG_SCFCR2 = FIFO_RCV_TRIGGER_14 | FIFO_XMT_TRIGGER_1;
    345  1.6  msaitoh #endif
    346  1.1   itojun 
    347  1.1   itojun 	/* Send permission, Recieve permission ON */
    348  1.3  msaitoh 	SHREG_SCSCR2 = SCSCR2_TE | SCSCR2_RE;
    349  1.1   itojun 
    350  1.7  msaitoh 	/* Serial Status Register */
    351  1.3  msaitoh 	SHREG_SCSSR2 &= SCSSR2_TDFE;	/* Clear Status */
    352  1.1   itojun }
    353  1.1   itojun 
    354  1.1   itojun 
    355  1.1   itojun /*
    356  1.1   itojun  * PutcScif
    357  1.1   itojun  *  : unsigned char c;
    358  1.1   itojun  */
    359  1.1   itojun 
    360  1.1   itojun void
    361  1.1   itojun PutcScif(c)
    362  1.1   itojun 	unsigned char c;
    363  1.1   itojun {
    364  1.1   itojun 
    365  1.1   itojun 	/* wait for ready */
    366  1.1   itojun 	while ((SHREG_SCFDR2 & SCFDR2_TXCNT) == SCFDR2_TXF_FULL)
    367  1.1   itojun 		;
    368  1.1   itojun 
    369  1.1   itojun 	/* write send data to send register */
    370  1.1   itojun 	SHREG_SCFTDR2 = c;
    371  1.1   itojun 
    372  1.1   itojun 	/* clear ready flag */
    373  1.1   itojun 	SHREG_SCSSR2 &= ~(SCSSR2_TDFE | SCSSR2_TEND);
    374  1.1   itojun 
    375  1.7  msaitoh 	if (c == '\n') {
    376  1.1   itojun 		while ((SHREG_SCFDR2 & SCFDR2_TXCNT) == SCFDR2_TXF_FULL)
    377  1.1   itojun 			;
    378  1.1   itojun 
    379  1.1   itojun 		SHREG_SCFTDR2 = '\r';
    380  1.1   itojun 
    381  1.1   itojun 		SHREG_SCSSR2 &= ~(SCSSR2_TDFE | SCSSR2_TEND);
    382  1.1   itojun 	}
    383  1.1   itojun }
    384  1.1   itojun 
    385  1.1   itojun /*
    386  1.1   itojun  * PutStrScif
    387  1.1   itojun  * : unsigned char *s;
    388  1.1   itojun  */
    389  1.1   itojun void
    390  1.1   itojun PutStrScif(s)
    391  1.1   itojun 	unsigned char *s;
    392  1.1   itojun {
    393  1.1   itojun 
    394  1.1   itojun 	while (*s)
    395  1.1   itojun 		PutcScif(*s++);
    396  1.1   itojun }
    397  1.1   itojun 
    398  1.1   itojun /*
    399  1.1   itojun  * : ScifErrCheck
    400  1.1   itojun  *	0x80 = error
    401  1.1   itojun  *	0x08 = frame error
    402  1.1   itojun  *	0x04 = parity error
    403  1.1   itojun  */
    404  1.1   itojun int
    405  1.1   itojun ScifErrCheck(void)
    406  1.1   itojun {
    407  1.1   itojun 
    408  1.1   itojun 	return(SHREG_SCSSR2 & (SCSSR2_ER | SCSSR2_FER | SCSSR2_PER));
    409  1.1   itojun }
    410  1.1   itojun 
    411  1.1   itojun /*
    412  1.1   itojun  * GetcScif
    413  1.1   itojun  */
    414  1.1   itojun #if 0
    415  1.1   itojun /* Old code */
    416  1.1   itojun unsigned char
    417  1.1   itojun GetcScif(void)
    418  1.1   itojun {
    419  1.1   itojun 	unsigned char c, err_c;
    420  1.1   itojun 
    421  1.1   itojun 	while (((err_c = SHREG_SCSSR2)
    422  1.1   itojun 		& (SCSSR2_RDF | SCSSR2_ER | SCSSR2_FER | SCSSR2_PER | SCSSR2_DR)) == 0)
    423  1.1   itojun 		;
    424  1.1   itojun 	if ((err_c & (SCSSR2_ER | SCSSR2_FER | SCSSR2_PER)) != 0)
    425  1.1   itojun 		return(err_c |= 0x80);
    426  1.1   itojun 
    427  1.1   itojun 	c = SHREG_SCFRDR2;
    428  1.1   itojun 
    429  1.1   itojun 	SHREG_SCSSR2 &= ~(SCSSR2_RDF | SCSSR2_DR);
    430  1.1   itojun 
    431  1.1   itojun 	return(c);
    432  1.1   itojun }
    433  1.1   itojun #else
    434  1.1   itojun unsigned char
    435  1.1   itojun GetcScif(void)
    436  1.1   itojun {
    437  1.1   itojun 	unsigned char c, err_c;
    438  1.1   itojun 
    439  1.1   itojun 	/* wait for ready */
    440  1.1   itojun 	while ((SHREG_SCFDR2 & SCFDR2_RECVCNT) == 0)
    441  1.1   itojun 		;
    442  1.1   itojun 	err_c = SHREG_SCSSR2;
    443  1.6  msaitoh 	if ((err_c & (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER | SCSSR2_PER)) != 0)
    444  1.1   itojun 		return(err_c |= 0x80);
    445  1.1   itojun 
    446  1.1   itojun 	c = SHREG_SCFRDR2;
    447  1.1   itojun 
    448  1.6  msaitoh 	SHREG_SCSSR2 &= ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR);
    449  1.1   itojun 
    450  1.1   itojun 	return(c);
    451  1.1   itojun }
    452  1.1   itojun #endif
    453  1.1   itojun 
    454  1.1   itojun /*
    455  1.1   itojun  * GetStrScif
    456  1.1   itojun  *  : unsigned char *s;
    457  1.1   itojun  *  : int size;
    458  1.1   itojun  */
    459  1.1   itojun int
    460  1.1   itojun GetStrScif(s, size)
    461  1.1   itojun 	unsigned char *s;
    462  1.1   itojun 	int size;
    463  1.1   itojun {
    464  1.1   itojun 
    465  1.7  msaitoh 	for (; size ; size--) {
    466  1.1   itojun 		*s = GetcScif();
    467  1.1   itojun 		if (*s & 0x80)
    468  1.1   itojun 			return -1;
    469  1.7  msaitoh 		if (*s == CR) {
    470  1.1   itojun 			*s = 0;
    471  1.1   itojun 			break;
    472  1.1   itojun 		}
    473  1.1   itojun 		s++;
    474  1.1   itojun 	}
    475  1.1   itojun 	if (size == 0)
    476  1.1   itojun 		*s = 0;
    477  1.1   itojun 	return 0;
    478  1.1   itojun }
    479  1.1   itojun 
    480  1.1   itojun #if 0
    481  1.1   itojun #define SCIF_MAX_UNITS 2
    482  1.1   itojun #else
    483  1.1   itojun #define SCIF_MAX_UNITS 1
    484  1.1   itojun #endif
    485  1.1   itojun 
    486  1.1   itojun 
    487  1.1   itojun static int
    488  1.1   itojun scif_match(parent, cfp, aux)
    489  1.1   itojun 	struct device *parent;
    490  1.1   itojun 	struct cfdata *cfp;
    491  1.1   itojun 	void *aux;
    492  1.1   itojun {
    493  1.1   itojun 	struct shb_attach_args *sa = aux;
    494  1.1   itojun 
    495  1.1   itojun 	if (strcmp(cfp->cf_driver->cd_name, "scif")
    496  1.1   itojun 	    || cfp->cf_unit >= SCIF_MAX_UNITS)
    497  1.1   itojun 		return 0;
    498  1.1   itojun 
    499  1.1   itojun 	sa->ia_iosize = 0x10;
    500  1.1   itojun 	return 1;
    501  1.1   itojun }
    502  1.1   itojun 
    503  1.1   itojun static void
    504  1.1   itojun scif_attach(parent, self, aux)
    505  1.1   itojun 	struct device	*parent, *self;
    506  1.1   itojun 	void		*aux;
    507  1.1   itojun {
    508  1.1   itojun 	struct scif_softc *sc = (struct scif_softc *)self;
    509  1.1   itojun 	struct tty *tp;
    510  1.1   itojun 	int irq;
    511  1.1   itojun 	struct shb_attach_args *ia = aux;
    512  1.1   itojun 
    513  1.1   itojun 	sc->sc_hwflags = 0;	/* XXX */
    514  1.1   itojun 	sc->sc_swflags = 0;	/* XXX */
    515  1.1   itojun 	sc->sc_fifolen = 16;
    516  1.1   itojun 
    517  1.1   itojun 	irq = ia->ia_irq;
    518  1.1   itojun 
    519  1.1   itojun 	SET(sc->sc_hwflags, SCIF_HW_DEV_OK);
    520  1.1   itojun 	SET(sc->sc_hwflags, SCIF_HW_CONSOLE);
    521  1.1   itojun 
    522  1.8  thorpej 	callout_init(&sc->sc_diag_ch);
    523  1.8  thorpej 
    524  1.1   itojun #if 0
    525  1.1   itojun 	if (irq != IRQUNK) {
    526  1.1   itojun 		sc->sc_ih = shb_intr_establish(irq,
    527  1.1   itojun 		    IST_EDGE, IPL_SERIAL, scifintr, sc);
    528  1.1   itojun 	}
    529  1.1   itojun #else
    530  1.1   itojun 	if (irq != IRQUNK) {
    531  1.1   itojun 		sc->sc_ih = shb_intr_establish(SCIF_IRQ,
    532  1.1   itojun 		    IST_EDGE, IPL_SERIAL, scifintr, sc);
    533  1.1   itojun 	}
    534  1.1   itojun #endif
    535  1.1   itojun 
    536  1.1   itojun 	printf("\n");
    537  1.1   itojun 
    538  1.1   itojun 	printf("%s: console\n", sc->sc_dev.dv_xname);
    539  1.1   itojun 
    540  1.1   itojun 	tp = ttymalloc();
    541  1.1   itojun 	tp->t_oproc = scifstart;
    542  1.1   itojun 	tp->t_param = scifparam;
    543  1.1   itojun 	tp->t_hwiflow = NULL;
    544  1.1   itojun 
    545  1.1   itojun 	sc->sc_tty = tp;
    546  1.1   itojun 	sc->sc_rbuf = malloc(scif_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    547  1.1   itojun 	if (sc->sc_rbuf == NULL) {
    548  1.1   itojun 		printf("%s: unable to allocate ring buffer\n",
    549  1.1   itojun 		    sc->sc_dev.dv_xname);
    550  1.1   itojun 		return;
    551  1.1   itojun 	}
    552  1.1   itojun 	sc->sc_ebuf = sc->sc_rbuf + (scif_rbuf_size << 1);
    553  1.1   itojun 
    554  1.1   itojun 	tty_attach(tp);
    555  1.1   itojun }
    556  1.1   itojun 
    557  1.1   itojun /*
    558  1.1   itojun  * Start or restart transmission.
    559  1.1   itojun  */
    560  1.1   itojun static void
    561  1.1   itojun scifstart(tp)
    562  1.1   itojun 	struct tty *tp;
    563  1.1   itojun {
    564  1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(tp->t_dev)];
    565  1.1   itojun 	int s;
    566  1.1   itojun 
    567  1.1   itojun 	s = spltty();
    568  1.1   itojun 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    569  1.1   itojun 		goto out;
    570  1.1   itojun 	if (sc->sc_tx_stopped)
    571  1.1   itojun 		goto out;
    572  1.1   itojun 
    573  1.1   itojun 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    574  1.1   itojun 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    575  1.1   itojun 			CLR(tp->t_state, TS_ASLEEP);
    576  1.1   itojun 			wakeup(&tp->t_outq);
    577  1.1   itojun 		}
    578  1.1   itojun 		selwakeup(&tp->t_wsel);
    579  1.1   itojun 		if (tp->t_outq.c_cc == 0)
    580  1.1   itojun 			goto out;
    581  1.1   itojun 	}
    582  1.1   itojun 
    583  1.1   itojun 	/* Grab the first contiguous region of buffer space. */
    584  1.1   itojun 	{
    585  1.1   itojun 		u_char *tba;
    586  1.1   itojun 		int tbc;
    587  1.1   itojun 
    588  1.1   itojun 		tba = tp->t_outq.c_cf;
    589  1.1   itojun 		tbc = ndqb(&tp->t_outq, 0);
    590  1.1   itojun 
    591  1.1   itojun 		(void)splserial();
    592  1.1   itojun 
    593  1.1   itojun 		sc->sc_tba = tba;
    594  1.1   itojun 		sc->sc_tbc = tbc;
    595  1.1   itojun 	}
    596  1.1   itojun 
    597  1.1   itojun 	SET(tp->t_state, TS_BUSY);
    598  1.1   itojun 	sc->sc_tx_busy = 1;
    599  1.1   itojun 
    600  1.1   itojun 	/* Enable transmit completion interrupts if necessary. */
    601  1.1   itojun 	SHREG_SCSCR2 |= SCSCR2_TIE | SCSCR2_RIE;
    602  1.1   itojun 
    603  1.1   itojun 	/* Output the first chunk of the contiguous buffer. */
    604  1.1   itojun 	{
    605  1.1   itojun 		int n;
    606  1.1   itojun 		int max;
    607  1.1   itojun 		int i;
    608  1.1   itojun 
    609  1.1   itojun 		n = sc->sc_tbc;
    610  1.1   itojun 		max = sc->sc_fifolen - ((SHREG_SCFDR2 & SCFDR2_TXCNT) >> 8);
    611  1.1   itojun 		if (n > max)
    612  1.1   itojun 			n = max;
    613  1.1   itojun 
    614  1.1   itojun 		for (i = 0; i < n; i++) {
    615  1.1   itojun 			PutcScif(*(sc->sc_tba));
    616  1.1   itojun 			sc->sc_tba++;
    617  1.1   itojun 		}
    618  1.1   itojun 		sc->sc_tbc -= n;
    619  1.1   itojun 	}
    620  1.1   itojun out:
    621  1.1   itojun 	splx(s);
    622  1.1   itojun 	return;
    623  1.1   itojun }
    624  1.1   itojun 
    625  1.1   itojun /*
    626  1.1   itojun  * Set SCIF tty parameters from termios.
    627  1.1   itojun  * XXX - Should just copy the whole termios after
    628  1.1   itojun  * making sure all the changes could be done.
    629  1.1   itojun  */
    630  1.1   itojun static int
    631  1.1   itojun scifparam(tp, t)
    632  1.1   itojun 	struct tty *tp;
    633  1.1   itojun 	struct termios *t;
    634  1.1   itojun {
    635  1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(tp->t_dev)];
    636  1.1   itojun 	int ospeed = t->c_ospeed;
    637  1.1   itojun 	int s;
    638  1.1   itojun 
    639  1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    640  1.1   itojun 		return (EIO);
    641  1.1   itojun 
    642  1.1   itojun 	/* Check requested parameters. */
    643  1.1   itojun 	if (ospeed < 0)
    644  1.1   itojun 		return (EINVAL);
    645  1.1   itojun 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    646  1.1   itojun 		return (EINVAL);
    647  1.1   itojun 
    648  1.1   itojun 	/*
    649  1.1   itojun 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    650  1.1   itojun 	 * is always active.
    651  1.1   itojun 	 */
    652  1.1   itojun 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    653  1.1   itojun 	    ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
    654  1.1   itojun 		SET(t->c_cflag, CLOCAL);
    655  1.1   itojun 		CLR(t->c_cflag, HUPCL);
    656  1.1   itojun 	}
    657  1.1   itojun 
    658  1.1   itojun 	/*
    659  1.1   itojun 	 * If there were no changes, don't do anything.  This avoids dropping
    660  1.1   itojun 	 * input and improves performance when all we did was frob things like
    661  1.1   itojun 	 * VMIN and VTIME.
    662  1.1   itojun 	 */
    663  1.1   itojun 	if (tp->t_ospeed == t->c_ospeed &&
    664  1.1   itojun 	    tp->t_cflag == t->c_cflag)
    665  1.1   itojun 		return (0);
    666  1.1   itojun 
    667  1.1   itojun #if 0
    668  1.1   itojun /* XXX (msaitoh) */
    669  1.1   itojun 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
    670  1.1   itojun #endif
    671  1.1   itojun 
    672  1.1   itojun 	s = splserial();
    673  1.1   itojun 
    674  1.1   itojun 	/*
    675  1.1   itojun 	 * Set the flow control pins depending on the current flow control
    676  1.1   itojun 	 * mode.
    677  1.1   itojun 	 */
    678  1.1   itojun 	if (ISSET(t->c_cflag, CRTSCTS)) {
    679  1.1   itojun 		SHREG_SCFCR2 |= SCFCR2_MCE;
    680  1.1   itojun 	} else {
    681  1.1   itojun 		SHREG_SCFCR2 &= ~SCFCR2_MCE;
    682  1.1   itojun 	}
    683  1.1   itojun 
    684  1.1   itojun 	SHREG_SCBRR2 = divrnd(PCLOCK, 32 * ospeed) -1;
    685  1.1   itojun 
    686  1.1   itojun 	/*
    687  1.1   itojun 	 * Set the FIFO threshold based on the receive speed.
    688  1.1   itojun 	 *
    689  1.1   itojun 	 *  * If it's a low speed, it's probably a mouse or some other
    690  1.1   itojun 	 *    interactive device, so set the threshold low.
    691  1.1   itojun 	 *  * If it's a high speed, trim the trigger level down to prevent
    692  1.1   itojun 	 *    overflows.
    693  1.1   itojun 	 *  * Otherwise set it a bit higher.
    694  1.1   itojun 	 */
    695  1.1   itojun #if 0
    696  1.1   itojun /* XXX (msaitoh) */
    697  1.1   itojun 	if (ISSET(sc->sc_hwflags, SCIF_HW_HAYESP))
    698  1.1   itojun 		sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
    699  1.1   itojun 	else if (ISSET(sc->sc_hwflags, SCIF_HW_FIFO))
    700  1.1   itojun 		sc->sc_fifo = FIFO_ENABLE |
    701  1.1   itojun 		    (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
    702  1.1   itojun 		     t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
    703  1.1   itojun 	else
    704  1.1   itojun 		sc->sc_fifo = 0;
    705  1.1   itojun #endif
    706  1.1   itojun 
    707  1.1   itojun 	/* And copy to tty. */
    708  1.1   itojun 	tp->t_ispeed = 0;
    709  1.1   itojun 	tp->t_ospeed = t->c_ospeed;
    710  1.1   itojun 	tp->t_cflag = t->c_cflag;
    711  1.1   itojun 
    712  1.1   itojun 	if (!sc->sc_heldchange) {
    713  1.1   itojun 		if (sc->sc_tx_busy) {
    714  1.1   itojun 			sc->sc_heldtbc = sc->sc_tbc;
    715  1.1   itojun 			sc->sc_tbc = 0;
    716  1.1   itojun 			sc->sc_heldchange = 1;
    717  1.1   itojun 		}
    718  1.1   itojun #if 0
    719  1.1   itojun /* XXX (msaitoh) */
    720  1.1   itojun 		else
    721  1.1   itojun 			scif_loadchannelregs(sc);
    722  1.1   itojun #endif
    723  1.1   itojun 	}
    724  1.1   itojun 
    725  1.1   itojun 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    726  1.1   itojun 		/* Disable the high water mark. */
    727  1.1   itojun 		sc->sc_r_hiwat = 0;
    728  1.1   itojun 		sc->sc_r_lowat = 0;
    729  1.1   itojun 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
    730  1.1   itojun 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
    731  1.1   itojun 			scif_schedrx(sc);
    732  1.1   itojun 		}
    733  1.1   itojun 	} else {
    734  1.1   itojun 		sc->sc_r_hiwat = scif_rbuf_hiwat;
    735  1.1   itojun 		sc->sc_r_lowat = scif_rbuf_lowat;
    736  1.1   itojun 	}
    737  1.1   itojun 
    738  1.1   itojun 	splx(s);
    739  1.1   itojun 
    740  1.1   itojun #ifdef SCIF_DEBUG
    741  1.1   itojun 	if (scif_debug)
    742  1.1   itojun 		scifstatus(sc, "scifparam ");
    743  1.1   itojun #endif
    744  1.1   itojun 
    745  1.1   itojun 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    746  1.1   itojun 		if (sc->sc_tx_stopped) {
    747  1.1   itojun 			sc->sc_tx_stopped = 0;
    748  1.1   itojun 			scifstart(tp);
    749  1.1   itojun 		}
    750  1.1   itojun 	}
    751  1.1   itojun 
    752  1.1   itojun 	return (0);
    753  1.1   itojun }
    754  1.1   itojun 
    755  1.1   itojun void
    756  1.1   itojun scif_iflush(sc)
    757  1.1   itojun 	struct scif_softc *sc;
    758  1.1   itojun {
    759  1.1   itojun 	int i;
    760  1.1   itojun 	unsigned char c;
    761  1.1   itojun 
    762  1.1   itojun 	i = SHREG_SCFDR2 & SCFDR2_RECVCNT;
    763  1.1   itojun 
    764  1.1   itojun 	while (i > 0) {
    765  1.1   itojun 		c = SHREG_SCFRDR2;
    766  1.1   itojun 		SHREG_SCSSR2 &= ~(SCSSR2_RDF | SCSSR2_DR);
    767  1.1   itojun 		i--;
    768  1.1   itojun 	}
    769  1.1   itojun }
    770  1.1   itojun 
    771  1.1   itojun int scif_getc __P((void));
    772  1.1   itojun void scif_putc __P((int));
    773  1.1   itojun 
    774  1.1   itojun int
    775  1.1   itojun scif_getc()
    776  1.1   itojun {
    777  1.1   itojun 
    778  1.1   itojun 	return (GetcScif());
    779  1.1   itojun }
    780  1.1   itojun 
    781  1.1   itojun void
    782  1.1   itojun scif_putc(int c)
    783  1.1   itojun {
    784  1.1   itojun 
    785  1.1   itojun 	PutcScif(c);
    786  1.1   itojun }
    787  1.1   itojun 
    788  1.1   itojun int
    789  1.1   itojun scifopen(dev, flag, mode, p)
    790  1.1   itojun 	dev_t dev;
    791  1.1   itojun 	int flag, mode;
    792  1.1   itojun 	struct proc *p;
    793  1.1   itojun {
    794  1.1   itojun 	int unit = SCIFUNIT(dev);
    795  1.1   itojun 	struct scif_softc *sc;
    796  1.1   itojun 	struct tty *tp;
    797  1.1   itojun 	int s, s2;
    798  1.1   itojun 	int error;
    799  1.1   itojun 
    800  1.1   itojun 	if (unit >= scif_cd.cd_ndevs)
    801  1.1   itojun 		return (ENXIO);
    802  1.1   itojun 	sc = scif_cd.cd_devs[unit];
    803  1.1   itojun 	if (sc == 0 || !ISSET(sc->sc_hwflags, SCIF_HW_DEV_OK) ||
    804  1.1   itojun 	    sc->sc_rbuf == NULL)
    805  1.1   itojun 		return (ENXIO);
    806  1.1   itojun 
    807  1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    808  1.1   itojun 		return (ENXIO);
    809  1.1   itojun 
    810  1.1   itojun #ifdef KGDB
    811  1.1   itojun 	/*
    812  1.1   itojun 	 * If this is the kgdb port, no other use is permitted.
    813  1.1   itojun 	 */
    814  1.1   itojun 	if (ISSET(sc->sc_hwflags, SCIF_HW_KGDB))
    815  1.1   itojun 		return (EBUSY);
    816  1.1   itojun #endif
    817  1.1   itojun 
    818  1.1   itojun 	tp = sc->sc_tty;
    819  1.1   itojun 
    820  1.1   itojun 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    821  1.1   itojun 	    ISSET(tp->t_state, TS_XCLUDE) &&
    822  1.1   itojun 	    p->p_ucred->cr_uid != 0)
    823  1.1   itojun 		return (EBUSY);
    824  1.1   itojun 
    825  1.1   itojun 	s = spltty();
    826  1.1   itojun 
    827  1.1   itojun 	/*
    828  1.1   itojun 	 * Do the following iff this is a first open.
    829  1.1   itojun 	 */
    830  1.1   itojun 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    831  1.1   itojun 		struct termios t;
    832  1.1   itojun 
    833  1.1   itojun 		tp->t_dev = dev;
    834  1.1   itojun 
    835  1.1   itojun 		s2 = splserial();
    836  1.1   itojun 
    837  1.1   itojun 		/* Turn on interrupts. */
    838  1.1   itojun 		SHREG_SCSCR2 |= SCSCR2_TIE | SCSCR2_RIE;
    839  1.1   itojun 
    840  1.1   itojun 		splx(s2);
    841  1.1   itojun 
    842  1.1   itojun 		/*
    843  1.1   itojun 		 * Initialize the termios status to the defaults.  Add in the
    844  1.1   itojun 		 * sticky bits from TIOCSFLAGS.
    845  1.1   itojun 		 */
    846  1.1   itojun 		t.c_ispeed = 0;
    847  1.1   itojun 		if (ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
    848  1.7  msaitoh 			t.c_ospeed = scifcn_speed;	/* XXX (msaitoh) */
    849  1.1   itojun 			t.c_cflag = scifconscflag;
    850  1.1   itojun 		} else {
    851  1.1   itojun 			t.c_ospeed = TTYDEF_SPEED;
    852  1.1   itojun 			t.c_cflag = TTYDEF_CFLAG;
    853  1.1   itojun 		}
    854  1.1   itojun 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    855  1.1   itojun 			SET(t.c_cflag, CLOCAL);
    856  1.1   itojun 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    857  1.1   itojun 			SET(t.c_cflag, CRTSCTS);
    858  1.1   itojun 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    859  1.1   itojun 			SET(t.c_cflag, MDMBUF);
    860  1.1   itojun 		/* Make sure scifparam() will do something. */
    861  1.1   itojun 		tp->t_ospeed = 0;
    862  1.1   itojun 		(void) scifparam(tp, &t);
    863  1.1   itojun 		tp->t_iflag = TTYDEF_IFLAG;
    864  1.1   itojun 		tp->t_oflag = TTYDEF_OFLAG;
    865  1.1   itojun 		tp->t_lflag = TTYDEF_LFLAG;
    866  1.1   itojun 		ttychars(tp);
    867  1.1   itojun 		ttsetwater(tp);
    868  1.1   itojun 
    869  1.1   itojun 		s2 = splserial();
    870  1.1   itojun 
    871  1.1   itojun 		/* Clear the input ring, and unblock. */
    872  1.1   itojun 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    873  1.1   itojun 		sc->sc_rbavail = scif_rbuf_size;
    874  1.1   itojun 		scif_iflush(sc);
    875  1.1   itojun 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    876  1.1   itojun #if 0
    877  1.1   itojun /* XXX (msaitoh) */
    878  1.1   itojun 		scif_hwiflow(sc);
    879  1.1   itojun #endif
    880  1.1   itojun 
    881  1.1   itojun #ifdef SCIF_DEBUG
    882  1.1   itojun 		if (scif_debug)
    883  1.1   itojun 			scifstatus(sc, "scifopen  ");
    884  1.1   itojun #endif
    885  1.1   itojun 
    886  1.1   itojun 		splx(s2);
    887  1.1   itojun 	}
    888  1.1   itojun 
    889  1.1   itojun 	splx(s);
    890  1.1   itojun 
    891  1.1   itojun 	error = ttyopen(tp, SCIFDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    892  1.1   itojun 	if (error)
    893  1.1   itojun 		goto bad;
    894  1.1   itojun 
    895  1.1   itojun 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    896  1.1   itojun 	if (error)
    897  1.1   itojun 		goto bad;
    898  1.1   itojun 
    899  1.1   itojun 	return (0);
    900  1.1   itojun 
    901  1.1   itojun bad:
    902  1.1   itojun 
    903  1.1   itojun 	return (error);
    904  1.1   itojun }
    905  1.1   itojun 
    906  1.1   itojun int
    907  1.1   itojun scifclose(dev, flag, mode, p)
    908  1.1   itojun 	dev_t dev;
    909  1.1   itojun 	int flag, mode;
    910  1.1   itojun 	struct proc *p;
    911  1.1   itojun {
    912  1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    913  1.1   itojun 	struct tty *tp = sc->sc_tty;
    914  1.1   itojun 
    915  1.1   itojun 	/* XXX This is for cons.c. */
    916  1.1   itojun 	if (!ISSET(tp->t_state, TS_ISOPEN))
    917  1.1   itojun 		return (0);
    918  1.1   itojun 
    919  1.1   itojun 	(*linesw[tp->t_line].l_close)(tp, flag);
    920  1.1   itojun 	ttyclose(tp);
    921  1.1   itojun 
    922  1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    923  1.1   itojun 		return (0);
    924  1.1   itojun 
    925  1.1   itojun 	return (0);
    926  1.1   itojun }
    927  1.1   itojun 
    928  1.1   itojun int
    929  1.1   itojun scifread(dev, uio, flag)
    930  1.1   itojun 	dev_t dev;
    931  1.1   itojun 	struct uio *uio;
    932  1.1   itojun 	int flag;
    933  1.1   itojun {
    934  1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    935  1.1   itojun 	struct tty *tp = sc->sc_tty;
    936  1.1   itojun 
    937  1.1   itojun 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    938  1.1   itojun }
    939  1.1   itojun 
    940  1.1   itojun int
    941  1.1   itojun scifwrite(dev, uio, flag)
    942  1.1   itojun 	dev_t dev;
    943  1.1   itojun 	struct uio *uio;
    944  1.1   itojun 	int flag;
    945  1.1   itojun {
    946  1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    947  1.1   itojun 	struct tty *tp = sc->sc_tty;
    948  1.1   itojun 
    949  1.1   itojun 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    950  1.1   itojun }
    951  1.1   itojun 
    952  1.1   itojun struct tty *
    953  1.1   itojun sciftty(dev)
    954  1.1   itojun 	dev_t dev;
    955  1.1   itojun {
    956  1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    957  1.1   itojun 	struct tty *tp = sc->sc_tty;
    958  1.1   itojun 
    959  1.1   itojun 	return (tp);
    960  1.1   itojun }
    961  1.1   itojun 
    962  1.1   itojun int
    963  1.1   itojun scifioctl(dev, cmd, data, flag, p)
    964  1.1   itojun 	dev_t dev;
    965  1.1   itojun 	u_long cmd;
    966  1.1   itojun 	caddr_t data;
    967  1.1   itojun 	int flag;
    968  1.1   itojun 	struct proc *p;
    969  1.1   itojun {
    970  1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    971  1.1   itojun 	struct tty *tp = sc->sc_tty;
    972  1.1   itojun 	int error;
    973  1.1   itojun 	int s;
    974  1.1   itojun 
    975  1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    976  1.1   itojun 		return (EIO);
    977  1.1   itojun 
    978  1.1   itojun 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    979  1.1   itojun 	if (error >= 0)
    980  1.1   itojun 		return (error);
    981  1.1   itojun 
    982  1.1   itojun 	error = ttioctl(tp, cmd, data, flag, p);
    983  1.1   itojun 	if (error >= 0)
    984  1.1   itojun 		return (error);
    985  1.1   itojun 
    986  1.1   itojun 	error = 0;
    987  1.1   itojun 
    988  1.1   itojun 	s = splserial();
    989  1.1   itojun 
    990  1.1   itojun 	switch (cmd) {
    991  1.1   itojun 	case TIOCSBRK:
    992  1.1   itojun 		scif_break(sc, 1);
    993  1.1   itojun 		break;
    994  1.1   itojun 
    995  1.1   itojun 	case TIOCCBRK:
    996  1.1   itojun 		scif_break(sc, 0);
    997  1.1   itojun 		break;
    998  1.6  msaitoh 
    999  1.1   itojun 	case TIOCGFLAGS:
   1000  1.1   itojun 		*(int *)data = sc->sc_swflags;
   1001  1.1   itojun 		break;
   1002  1.1   itojun 
   1003  1.1   itojun 	case TIOCSFLAGS:
   1004  1.1   itojun 		error = suser(p->p_ucred, &p->p_acflag);
   1005  1.1   itojun 		if (error)
   1006  1.1   itojun 			break;
   1007  1.1   itojun 		sc->sc_swflags = *(int *)data;
   1008  1.1   itojun 		break;
   1009  1.1   itojun 
   1010  1.1   itojun 	default:
   1011  1.1   itojun 		error = ENOTTY;
   1012  1.1   itojun 		break;
   1013  1.1   itojun 	}
   1014  1.1   itojun 
   1015  1.1   itojun 	splx(s);
   1016  1.1   itojun 
   1017  1.1   itojun 	return (error);
   1018  1.1   itojun }
   1019  1.1   itojun 
   1020  1.1   itojun integrate void
   1021  1.1   itojun scif_schedrx(sc)
   1022  1.1   itojun 	struct scif_softc *sc;
   1023  1.1   itojun {
   1024  1.1   itojun 
   1025  1.1   itojun 	sc->sc_rx_ready = 1;
   1026  1.1   itojun 
   1027  1.1   itojun 	/* Wake up the poller. */
   1028  1.1   itojun #ifdef __GENERIC_SOFT_INTERRUPTS
   1029  1.1   itojun 	softintr_schedule(sc->sc_si);
   1030  1.1   itojun #else
   1031  1.1   itojun #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1032  1.1   itojun 	setsoftserial();
   1033  1.1   itojun #else
   1034  1.1   itojun 	if (!scif_softintr_scheduled) {
   1035  1.1   itojun 		scif_softintr_scheduled = 1;
   1036  1.8  thorpej 		callout_reset(&scif_soft_ch, 1, scifsoft, NULL);
   1037  1.1   itojun 	}
   1038  1.1   itojun #endif
   1039  1.1   itojun #endif
   1040  1.1   itojun }
   1041  1.1   itojun 
   1042  1.6  msaitoh void
   1043  1.6  msaitoh scif_break(sc, onoff)
   1044  1.6  msaitoh 	struct scif_softc *sc;
   1045  1.6  msaitoh 	int onoff;
   1046  1.6  msaitoh {
   1047  1.6  msaitoh 
   1048  1.6  msaitoh 	if (onoff)
   1049  1.6  msaitoh 		SHREG_SCSSR2 &= ~SCSSR2_TDFE;
   1050  1.6  msaitoh 	else
   1051  1.6  msaitoh 		SHREG_SCSSR2 |= SCSSR2_TDFE;
   1052  1.6  msaitoh 
   1053  1.6  msaitoh #if 0	/* XXX */
   1054  1.6  msaitoh 	if (!sc->sc_heldchange) {
   1055  1.6  msaitoh 		if (sc->sc_tx_busy) {
   1056  1.6  msaitoh 			sc->sc_heldtbc = sc->sc_tbc;
   1057  1.6  msaitoh 			sc->sc_tbc = 0;
   1058  1.6  msaitoh 			sc->sc_heldchange = 1;
   1059  1.6  msaitoh 		} else
   1060  1.6  msaitoh 			scif_loadchannelregs(sc);
   1061  1.6  msaitoh 	}
   1062  1.6  msaitoh #endif
   1063  1.6  msaitoh }
   1064  1.6  msaitoh 
   1065  1.1   itojun /*
   1066  1.1   itojun  * Stop output, e.g., for ^S or output flush.
   1067  1.1   itojun  */
   1068  1.1   itojun void
   1069  1.1   itojun scifstop(tp, flag)
   1070  1.1   itojun 	struct tty *tp;
   1071  1.1   itojun 	int flag;
   1072  1.1   itojun {
   1073  1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(tp->t_dev)];
   1074  1.1   itojun 	int s;
   1075  1.1   itojun 
   1076  1.1   itojun 	s = splserial();
   1077  1.1   itojun 	if (ISSET(tp->t_state, TS_BUSY)) {
   1078  1.1   itojun 		/* Stop transmitting at the next chunk. */
   1079  1.1   itojun 		sc->sc_tbc = 0;
   1080  1.1   itojun 		sc->sc_heldtbc = 0;
   1081  1.1   itojun 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1082  1.1   itojun 			SET(tp->t_state, TS_FLUSH);
   1083  1.1   itojun 	}
   1084  1.1   itojun 	splx(s);
   1085  1.1   itojun }
   1086  1.1   itojun 
   1087  1.1   itojun void
   1088  1.1   itojun scif_intr_init()
   1089  1.1   itojun {
   1090  1.1   itojun 	/* XXX */
   1091  1.1   itojun }
   1092  1.1   itojun 
   1093  1.1   itojun void
   1094  1.1   itojun scifdiag(arg)
   1095  1.1   itojun 	void *arg;
   1096  1.1   itojun {
   1097  1.1   itojun 	struct scif_softc *sc = arg;
   1098  1.1   itojun 	int overflows, floods;
   1099  1.1   itojun 	int s;
   1100  1.1   itojun 
   1101  1.1   itojun 	s = splserial();
   1102  1.1   itojun 	overflows = sc->sc_overflows;
   1103  1.1   itojun 	sc->sc_overflows = 0;
   1104  1.1   itojun 	floods = sc->sc_floods;
   1105  1.1   itojun 	sc->sc_floods = 0;
   1106  1.1   itojun 	sc->sc_errors = 0;
   1107  1.1   itojun 	splx(s);
   1108  1.1   itojun 
   1109  1.1   itojun 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1110  1.1   itojun 	    sc->sc_dev.dv_xname,
   1111  1.1   itojun 	    overflows, overflows == 1 ? "" : "s",
   1112  1.1   itojun 	    floods, floods == 1 ? "" : "s");
   1113  1.1   itojun }
   1114  1.1   itojun 
   1115  1.1   itojun integrate void
   1116  1.1   itojun scif_rxsoft(sc, tp)
   1117  1.1   itojun 	struct scif_softc *sc;
   1118  1.1   itojun 	struct tty *tp;
   1119  1.1   itojun {
   1120  1.1   itojun 	int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
   1121  1.1   itojun 	u_char *get, *end;
   1122  1.1   itojun 	u_int cc, scc;
   1123  1.1   itojun 	u_char ssr2;
   1124  1.1   itojun 	int code;
   1125  1.1   itojun 	int s;
   1126  1.1   itojun 
   1127  1.1   itojun 	end = sc->sc_ebuf;
   1128  1.1   itojun 	get = sc->sc_rbget;
   1129  1.1   itojun 	scc = cc = scif_rbuf_size - sc->sc_rbavail;
   1130  1.1   itojun 
   1131  1.1   itojun 	if (cc == scif_rbuf_size) {
   1132  1.1   itojun 		sc->sc_floods++;
   1133  1.1   itojun 		if (sc->sc_errors++ == 0)
   1134  1.8  thorpej 			callout(&sc->sc_diag_ch, 60 * hz, scifdiag, sc);
   1135  1.1   itojun 	}
   1136  1.1   itojun 
   1137  1.1   itojun 	while (cc) {
   1138  1.1   itojun 		code = get[0];
   1139  1.1   itojun 		ssr2 = get[1];
   1140  1.6  msaitoh 		if (ISSET(ssr2, SCSSR2_BRK | SCSSR2_FER | SCSSR2_PER)) {
   1141  1.6  msaitoh 			if (ISSET(ssr2, SCSSR2_BRK | SCSSR2_FER))
   1142  1.1   itojun 				SET(code, TTY_FE);
   1143  1.1   itojun 			if (ISSET(ssr2, SCSSR2_PER))
   1144  1.1   itojun 				SET(code, TTY_PE);
   1145  1.1   itojun 		}
   1146  1.1   itojun 		if ((*rint)(code, tp) == -1) {
   1147  1.1   itojun 			/*
   1148  1.1   itojun 			 * The line discipline's buffer is out of space.
   1149  1.1   itojun 			 */
   1150  1.1   itojun 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1151  1.1   itojun 				/*
   1152  1.1   itojun 				 * We're either not using flow control, or the
   1153  1.1   itojun 				 * line discipline didn't tell us to block for
   1154  1.1   itojun 				 * some reason.  Either way, we have no way to
   1155  1.1   itojun 				 * know when there's more space available, so
   1156  1.1   itojun 				 * just drop the rest of the data.
   1157  1.1   itojun 				 */
   1158  1.1   itojun 				get += cc << 1;
   1159  1.1   itojun 				if (get >= end)
   1160  1.1   itojun 					get -= scif_rbuf_size << 1;
   1161  1.1   itojun 				cc = 0;
   1162  1.1   itojun 			} else {
   1163  1.1   itojun 				/*
   1164  1.1   itojun 				 * Don't schedule any more receive processing
   1165  1.1   itojun 				 * until the line discipline tells us there's
   1166  1.1   itojun 				 * space available (through scifhwiflow()).
   1167  1.1   itojun 				 * Leave the rest of the data in the input
   1168  1.1   itojun 				 * buffer.
   1169  1.1   itojun 				 */
   1170  1.1   itojun 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1171  1.1   itojun 			}
   1172  1.1   itojun 			break;
   1173  1.1   itojun 		}
   1174  1.1   itojun 		get += 2;
   1175  1.1   itojun 		if (get >= end)
   1176  1.1   itojun 			get = sc->sc_rbuf;
   1177  1.1   itojun 		cc--;
   1178  1.1   itojun 	}
   1179  1.1   itojun 
   1180  1.1   itojun 	if (cc != scc) {
   1181  1.1   itojun 		sc->sc_rbget = get;
   1182  1.1   itojun 		s = splserial();
   1183  1.1   itojun 		cc = sc->sc_rbavail += scc - cc;
   1184  1.1   itojun 		/* Buffers should be ok again, release possible block. */
   1185  1.1   itojun 		if (cc >= sc->sc_r_lowat) {
   1186  1.1   itojun 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1187  1.1   itojun 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1188  1.1   itojun 				SHREG_SCSCR2 |= SCSCR2_RIE;
   1189  1.1   itojun 			}
   1190  1.1   itojun #if 0
   1191  1.1   itojun 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1192  1.1   itojun 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1193  1.1   itojun 				scif_hwiflow(sc);
   1194  1.1   itojun 			}
   1195  1.1   itojun #endif
   1196  1.1   itojun 		}
   1197  1.1   itojun 		splx(s);
   1198  1.1   itojun 	}
   1199  1.1   itojun }
   1200  1.1   itojun 
   1201  1.1   itojun integrate void
   1202  1.1   itojun scif_txsoft(sc, tp)
   1203  1.1   itojun 	struct scif_softc *sc;
   1204  1.1   itojun 	struct tty *tp;
   1205  1.1   itojun {
   1206  1.1   itojun 
   1207  1.1   itojun 	CLR(tp->t_state, TS_BUSY);
   1208  1.1   itojun 	if (ISSET(tp->t_state, TS_FLUSH))
   1209  1.1   itojun 		CLR(tp->t_state, TS_FLUSH);
   1210  1.1   itojun 	else
   1211  1.1   itojun 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1212  1.1   itojun 	(*linesw[tp->t_line].l_start)(tp);
   1213  1.1   itojun }
   1214  1.1   itojun 
   1215  1.1   itojun integrate void
   1216  1.1   itojun scif_stsoft(sc, tp)
   1217  1.1   itojun 	struct scif_softc *sc;
   1218  1.1   itojun 	struct tty *tp;
   1219  1.1   itojun {
   1220  1.1   itojun #if 0
   1221  1.1   itojun /* XXX (msaitoh) */
   1222  1.1   itojun 	u_char msr, delta;
   1223  1.1   itojun 	int s;
   1224  1.1   itojun 
   1225  1.1   itojun 	s = splserial();
   1226  1.1   itojun 	msr = sc->sc_msr;
   1227  1.1   itojun 	delta = sc->sc_msr_delta;
   1228  1.1   itojun 	sc->sc_msr_delta = 0;
   1229  1.1   itojun 	splx(s);
   1230  1.1   itojun 
   1231  1.1   itojun 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1232  1.1   itojun 		/*
   1233  1.1   itojun 		 * Inform the tty layer that carrier detect changed.
   1234  1.1   itojun 		 */
   1235  1.1   itojun 		(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD));
   1236  1.1   itojun 	}
   1237  1.1   itojun 
   1238  1.1   itojun 	if (ISSET(delta, sc->sc_msr_cts)) {
   1239  1.1   itojun 		/* Block or unblock output according to flow control. */
   1240  1.1   itojun 		if (ISSET(msr, sc->sc_msr_cts)) {
   1241  1.1   itojun 			sc->sc_tx_stopped = 0;
   1242  1.1   itojun 			(*linesw[tp->t_line].l_start)(tp);
   1243  1.1   itojun 		} else {
   1244  1.1   itojun 			sc->sc_tx_stopped = 1;
   1245  1.1   itojun 		}
   1246  1.1   itojun 	}
   1247  1.1   itojun 
   1248  1.1   itojun #ifdef SCIF_DEBUG
   1249  1.1   itojun 	if (scif_debug)
   1250  1.1   itojun 		scifstatus(sc, "scif_stsoft");
   1251  1.1   itojun #endif
   1252  1.1   itojun #endif
   1253  1.1   itojun }
   1254  1.1   itojun 
   1255  1.1   itojun #ifdef __GENERIC_SOFT_INTERRUPTS
   1256  1.1   itojun void
   1257  1.1   itojun scifsoft(arg)
   1258  1.1   itojun 	void *arg;
   1259  1.1   itojun {
   1260  1.1   itojun 	struct scif_softc *sc = arg;
   1261  1.1   itojun 	struct tty *tp;
   1262  1.1   itojun 
   1263  1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
   1264  1.1   itojun 		return;
   1265  1.1   itojun 
   1266  1.1   itojun 	{
   1267  1.1   itojun #else
   1268  1.1   itojun void
   1269  1.1   itojun #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1270  1.1   itojun scifsoft()
   1271  1.1   itojun #else
   1272  1.1   itojun scifsoft(arg)
   1273  1.1   itojun 	void *arg;
   1274  1.1   itojun #endif
   1275  1.1   itojun {
   1276  1.1   itojun 	struct scif_softc	*sc;
   1277  1.1   itojun 	struct tty	*tp;
   1278  1.1   itojun 	int	unit;
   1279  1.1   itojun #ifdef __NO_SOFT_SERIAL_INTERRUPT
   1280  1.1   itojun 	int s;
   1281  1.1   itojun 
   1282  1.1   itojun 	s = splsoftserial();
   1283  1.1   itojun 	scif_softintr_scheduled = 0;
   1284  1.1   itojun #endif
   1285  1.1   itojun 
   1286  1.1   itojun 	for (unit = 0; unit < scif_cd.cd_ndevs; unit++) {
   1287  1.1   itojun 		sc = scif_cd.cd_devs[unit];
   1288  1.1   itojun 		if (sc == NULL || !ISSET(sc->sc_hwflags, SCIF_HW_DEV_OK))
   1289  1.1   itojun 			continue;
   1290  1.1   itojun 
   1291  1.1   itojun 		if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
   1292  1.1   itojun 			continue;
   1293  1.1   itojun 
   1294  1.1   itojun 		tp = sc->sc_tty;
   1295  1.1   itojun 		if (tp == NULL)
   1296  1.1   itojun 			continue;
   1297  1.1   itojun 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1298  1.1   itojun 			continue;
   1299  1.1   itojun #endif
   1300  1.1   itojun 		tp = sc->sc_tty;
   1301  1.1   itojun 
   1302  1.1   itojun 		if (sc->sc_rx_ready) {
   1303  1.1   itojun 			sc->sc_rx_ready = 0;
   1304  1.1   itojun 			scif_rxsoft(sc, tp);
   1305  1.1   itojun 		}
   1306  1.1   itojun 
   1307  1.1   itojun #if 0
   1308  1.1   itojun 		if (sc->sc_st_check) {
   1309  1.1   itojun 			sc->sc_st_check = 0;
   1310  1.1   itojun 			scif_stsoft(sc, tp);
   1311  1.1   itojun 		}
   1312  1.1   itojun #endif
   1313  1.1   itojun 
   1314  1.1   itojun 		if (sc->sc_tx_done) {
   1315  1.1   itojun 			sc->sc_tx_done = 0;
   1316  1.1   itojun 			scif_txsoft(sc, tp);
   1317  1.1   itojun 		}
   1318  1.1   itojun 	}
   1319  1.1   itojun 
   1320  1.1   itojun #ifndef __GENERIC_SOFT_INTERRUPTS
   1321  1.1   itojun #ifdef __NO_SOFT_SERIAL_INTERRUPT
   1322  1.1   itojun 	splx(s);
   1323  1.1   itojun #endif
   1324  1.1   itojun #endif
   1325  1.1   itojun }
   1326  1.1   itojun 
   1327  1.1   itojun int
   1328  1.1   itojun scifintr(arg)
   1329  1.1   itojun 	void *arg;
   1330  1.1   itojun {
   1331  1.1   itojun 	struct scif_softc *sc = arg;
   1332  1.1   itojun 	u_char *put, *end;
   1333  1.1   itojun 	u_int cc;
   1334  1.1   itojun 	u_short ssr2;
   1335  1.1   itojun 	int count;
   1336  1.1   itojun 
   1337  1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
   1338  1.1   itojun 		return (0);
   1339  1.1   itojun 
   1340  1.1   itojun 	end = sc->sc_ebuf;
   1341  1.1   itojun 	put = sc->sc_rbput;
   1342  1.1   itojun 	cc = sc->sc_rbavail;
   1343  1.1   itojun 
   1344  1.7  msaitoh 	ssr2 = SHREG_SCSSR2;
   1345  1.7  msaitoh 	if (ISSET(ssr2, SCSSR2_BRK)) {
   1346  1.7  msaitoh 		SHREG_SCSSR2 &= ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_DR);
   1347  1.1   itojun #if defined(DDB) || defined(KGDB)
   1348  1.1   itojun #ifdef DDB
   1349  1.7  msaitoh 		if (ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
   1350  1.7  msaitoh 			console_debugger();
   1351  1.7  msaitoh 		}
   1352  1.1   itojun #endif
   1353  1.1   itojun #ifdef KGDB
   1354  1.7  msaitoh 		if (ISSET(sc->sc_hwflags, SCIF_HW_KGDB)) {
   1355  1.7  msaitoh 			kgdb_connect(1);
   1356  1.7  msaitoh 		}
   1357  1.1   itojun #endif
   1358  1.6  msaitoh #endif /* DDB || KGDB */
   1359  1.7  msaitoh 	}
   1360  1.7  msaitoh 	count = SHREG_SCFDR2 & SCFDR2_RECVCNT;
   1361  1.7  msaitoh 	if (count != 0) {
   1362  1.7  msaitoh 		while ((cc > 0) && (count > 0)) {
   1363  1.7  msaitoh 			put[0] = SHREG_SCFRDR2;
   1364  1.7  msaitoh 			put[1] = (u_char)(SHREG_SCSSR2 & 0x00ff);
   1365  1.7  msaitoh 
   1366  1.7  msaitoh 			SHREG_SCSSR2 &= ~(SCSSR2_RDF | SCSSR2_DR);
   1367  1.7  msaitoh 
   1368  1.7  msaitoh 			put += 2;
   1369  1.7  msaitoh 			if (put >= end)
   1370  1.7  msaitoh 				put = sc->sc_rbuf;
   1371  1.7  msaitoh 			cc--;
   1372  1.7  msaitoh 			count--;
   1373  1.1   itojun 		}
   1374  1.1   itojun 
   1375  1.7  msaitoh 		/*
   1376  1.7  msaitoh 		 * Current string of incoming characters ended because
   1377  1.7  msaitoh 		 * no more data was available or we ran out of space.
   1378  1.7  msaitoh 		 * Schedule a receive event if any data was received.
   1379  1.7  msaitoh 		 * If we're out of space, turn off receive interrupts.
   1380  1.7  msaitoh 		 */
   1381  1.7  msaitoh 		sc->sc_rbput = put;
   1382  1.7  msaitoh 		sc->sc_rbavail = cc;
   1383  1.7  msaitoh 		if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1384  1.7  msaitoh 			sc->sc_rx_ready = 1;
   1385  1.1   itojun 
   1386  1.7  msaitoh 		/*
   1387  1.7  msaitoh 		 * See if we are in danger of overflowing a buffer. If
   1388  1.7  msaitoh 		 * so, use hardware flow control to ease the pressure.
   1389  1.7  msaitoh 		 */
   1390  1.7  msaitoh 		if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   1391  1.7  msaitoh 		    cc < sc->sc_r_hiwat) {
   1392  1.7  msaitoh 			SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1393  1.1   itojun #if 0
   1394  1.7  msaitoh 			scif_hwiflow(sc);
   1395  1.1   itojun #endif
   1396  1.7  msaitoh 		}
   1397  1.1   itojun 
   1398  1.7  msaitoh 		/*
   1399  1.7  msaitoh 		 * If we're out of space, disable receive interrupts
   1400  1.7  msaitoh 		 * until the queue has drained a bit.
   1401  1.7  msaitoh 		 */
   1402  1.7  msaitoh 		if (!cc) {
   1403  1.7  msaitoh 			SHREG_SCSCR2 &= ~SCSCR2_RIE;
   1404  1.7  msaitoh 		}
   1405  1.7  msaitoh 	} else {
   1406  1.7  msaitoh 		if (SHREG_SCSSR2 & (SCSSR2_RDF | SCSSR2_DR)) {
   1407  1.7  msaitoh 			SHREG_SCSCR2 &= ~(SCSCR2_TIE | SCSCR2_RIE);
   1408  1.1   itojun 		}
   1409  1.7  msaitoh 	}
   1410  1.1   itojun 
   1411  1.1   itojun #if 0
   1412  1.7  msaitoh 	msr = bus_space_read_1(iot, ioh, scif_msr);
   1413  1.7  msaitoh 	delta = msr ^ sc->sc_msr;
   1414  1.7  msaitoh 	sc->sc_msr = msr;
   1415  1.7  msaitoh 	if (ISSET(delta, sc->sc_msr_mask)) {
   1416  1.7  msaitoh 		SET(sc->sc_msr_delta, delta);
   1417  1.1   itojun 
   1418  1.7  msaitoh 		/*
   1419  1.7  msaitoh 		 * Pulse-per-second clock signal on edge of DCD?
   1420  1.7  msaitoh 		 */
   1421  1.7  msaitoh 		if (ISSET(delta, sc->sc_ppsmask)) {
   1422  1.7  msaitoh 			struct timeval tv;
   1423  1.7  msaitoh 			if (ISSET(msr, sc->sc_ppsmask) ==
   1424  1.7  msaitoh 			    sc->sc_ppsassert) {
   1425  1.7  msaitoh 				/* XXX nanotime() */
   1426  1.7  msaitoh 				microtime(&tv);
   1427  1.7  msaitoh 				TIMEVAL_TO_TIMESPEC(&tv,
   1428  1.7  msaitoh 						    &sc->ppsinfo.assert_timestamp);
   1429  1.7  msaitoh 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
   1430  1.7  msaitoh 					timespecadd(&sc->ppsinfo.assert_timestamp,
   1431  1.1   itojun 						    &sc->ppsparam.assert_offset,
   1432  1.1   itojun 						    &sc->ppsinfo.assert_timestamp);
   1433  1.7  msaitoh 					TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp);
   1434  1.7  msaitoh 				}
   1435  1.1   itojun 
   1436  1.1   itojun #ifdef PPS_SYNC
   1437  1.7  msaitoh 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
   1438  1.7  msaitoh 					hardpps(&tv, tv.tv_usec);
   1439  1.1   itojun #endif
   1440  1.7  msaitoh 				sc->ppsinfo.assert_sequence++;
   1441  1.7  msaitoh 				sc->ppsinfo.current_mode =
   1442  1.7  msaitoh 					sc->ppsparam.mode;
   1443  1.7  msaitoh 
   1444  1.7  msaitoh 			} else if (ISSET(msr, sc->sc_ppsmask) ==
   1445  1.7  msaitoh 				   sc->sc_ppsclear) {
   1446  1.7  msaitoh 				/* XXX nanotime() */
   1447  1.7  msaitoh 				microtime(&tv);
   1448  1.7  msaitoh 				TIMEVAL_TO_TIMESPEC(&tv,
   1449  1.7  msaitoh 						    &sc->ppsinfo.clear_timestamp);
   1450  1.7  msaitoh 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
   1451  1.7  msaitoh 					timespecadd(&sc->ppsinfo.clear_timestamp,
   1452  1.1   itojun 						    &sc->ppsparam.clear_offset,
   1453  1.1   itojun 						    &sc->ppsinfo.clear_timestamp);
   1454  1.7  msaitoh 					TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp);
   1455  1.7  msaitoh 				}
   1456  1.1   itojun 
   1457  1.1   itojun #ifdef PPS_SYNC
   1458  1.7  msaitoh 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
   1459  1.7  msaitoh 					hardpps(&tv, tv.tv_usec);
   1460  1.1   itojun #endif
   1461  1.7  msaitoh 				sc->ppsinfo.clear_sequence++;
   1462  1.7  msaitoh 				sc->ppsinfo.current_mode =
   1463  1.7  msaitoh 					sc->ppsparam.mode;
   1464  1.1   itojun 			}
   1465  1.7  msaitoh 		}
   1466  1.1   itojun 
   1467  1.7  msaitoh 		/*
   1468  1.7  msaitoh 		 * Stop output immediately if we lose the output
   1469  1.7  msaitoh 		 * flow control signal or carrier detect.
   1470  1.7  msaitoh 		 */
   1471  1.7  msaitoh 		if (ISSET(~msr, sc->sc_msr_mask)) {
   1472  1.7  msaitoh 			sc->sc_tbc = 0;
   1473  1.7  msaitoh 			sc->sc_heldtbc = 0;
   1474  1.1   itojun #ifdef SCIF_DEBUG
   1475  1.7  msaitoh 			if (scif_debug)
   1476  1.7  msaitoh 				scifstatus(sc, "scifintr  ");
   1477  1.1   itojun #endif
   1478  1.7  msaitoh 		}
   1479  1.1   itojun 
   1480  1.7  msaitoh 		sc->sc_st_check = 1;
   1481  1.7  msaitoh 	}
   1482  1.1   itojun #endif
   1483  1.1   itojun 
   1484  1.1   itojun 	/*
   1485  1.1   itojun 	 * Done handling any receive interrupts. See if data can be
   1486  1.1   itojun 	 * transmitted as well. Schedule tx done event if no data left
   1487  1.1   itojun 	 * and tty was marked busy.
   1488  1.1   itojun 	 */
   1489  1.7  msaitoh 	if (((SHREG_SCFDR2 & SCFDR2_TXCNT) >> 8) != 16) { /* XXX (msaitoh) */
   1490  1.1   itojun 		/*
   1491  1.1   itojun 		 * If we've delayed a parameter change, do it now, and restart
   1492  1.1   itojun 		 * output.
   1493  1.1   itojun 		 */
   1494  1.1   itojun 		if (sc->sc_heldchange) {
   1495  1.1   itojun 			sc->sc_heldchange = 0;
   1496  1.1   itojun 			sc->sc_tbc = sc->sc_heldtbc;
   1497  1.1   itojun 			sc->sc_heldtbc = 0;
   1498  1.1   itojun 		}
   1499  1.1   itojun 
   1500  1.1   itojun 		/* Output the next chunk of the contiguous buffer, if any. */
   1501  1.1   itojun 		if (sc->sc_tbc > 0) {
   1502  1.1   itojun 			int n;
   1503  1.1   itojun 			int max;
   1504  1.1   itojun 			int i;
   1505  1.1   itojun 
   1506  1.1   itojun 			n = sc->sc_tbc;
   1507  1.1   itojun 			max = sc->sc_fifolen -
   1508  1.1   itojun 				((SHREG_SCFDR2 & SCFDR2_TXCNT) >> 8);
   1509  1.1   itojun 			if (n > max)
   1510  1.1   itojun 				n = max;
   1511  1.1   itojun 
   1512  1.1   itojun 			for (i = 0; i < n; i++) {
   1513  1.1   itojun 				PutcScif(*(sc->sc_tba));
   1514  1.1   itojun 				sc->sc_tba++;
   1515  1.1   itojun 			}
   1516  1.1   itojun 			sc->sc_tbc -= n;
   1517  1.1   itojun 		} else {
   1518  1.1   itojun 			/* Disable transmit completion interrupts if necessary. */
   1519  1.1   itojun #if 0
   1520  1.1   itojun 			if (ISSET(sc->sc_ier, IER_ETXRDY))
   1521  1.1   itojun #endif
   1522  1.1   itojun 				SHREG_SCSCR2 &= ~SCSCR2_TIE;
   1523  1.1   itojun 
   1524  1.1   itojun 			if (sc->sc_tx_busy) {
   1525  1.1   itojun 				sc->sc_tx_busy = 0;
   1526  1.1   itojun 				sc->sc_tx_done = 1;
   1527  1.1   itojun 			}
   1528  1.1   itojun 		}
   1529  1.1   itojun 	}
   1530  1.1   itojun 
   1531  1.1   itojun 	/* Wake up the poller. */
   1532  1.1   itojun #ifdef __GENERIC_SOFT_INTERRUPTS
   1533  1.1   itojun 	softintr_schedule(sc->sc_si);
   1534  1.1   itojun #else
   1535  1.1   itojun #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1536  1.1   itojun 	setsoftserial();
   1537  1.1   itojun #else
   1538  1.1   itojun 	if (!scif_softintr_scheduled) {
   1539  1.1   itojun 		scif_softintr_scheduled = 1;
   1540  1.8  thorpej 		callout_reset(&scif_soft_ch, 1, scifsoft, NULL);
   1541  1.1   itojun 	}
   1542  1.1   itojun #endif
   1543  1.1   itojun #endif
   1544  1.1   itojun 
   1545  1.1   itojun #if NRND > 0 && defined(RND_SCIF)
   1546  1.1   itojun 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1547  1.1   itojun #endif
   1548  1.1   itojun 
   1549  1.1   itojun 	return (1);
   1550  1.1   itojun }
   1551  1.1   itojun 
   1552  1.1   itojun void
   1553  1.1   itojun scifcnprobe(cp)
   1554  1.1   itojun 	struct consdev *cp;
   1555  1.1   itojun {
   1556  1.1   itojun 	int maj;
   1557  1.1   itojun 
   1558  1.1   itojun 	/* locate the major number */
   1559  1.1   itojun 	for (maj = 0; maj < nchrdev; maj++)
   1560  1.1   itojun 		if (cdevsw[maj].d_open == scifopen)
   1561  1.1   itojun 			break;
   1562  1.1   itojun 
   1563  1.1   itojun 	/* Initialize required fields. */
   1564  1.1   itojun 	cp->cn_dev = makedev(maj, 0);
   1565  1.4  msaitoh #ifdef SCIFCONSOLE
   1566  1.4  msaitoh 	cp->cn_pri = CN_REMOTE;
   1567  1.4  msaitoh #else
   1568  1.1   itojun 	cp->cn_pri = CN_NORMAL;
   1569  1.1   itojun #endif
   1570  1.1   itojun }
   1571  1.1   itojun 
   1572  1.1   itojun #define scif_gets GetStrScif
   1573  1.1   itojun #define scif_puts PutStrScif
   1574  1.1   itojun 
   1575  1.1   itojun void
   1576  1.1   itojun scifcninit(cp)
   1577  1.1   itojun 	struct consdev *cp;
   1578  1.1   itojun {
   1579  1.1   itojun 
   1580  1.7  msaitoh 	InitializeScif(scifcn_speed);
   1581  1.1   itojun }
   1582  1.1   itojun 
   1583  1.1   itojun #define scif_getc GetcScif
   1584  1.1   itojun #define scif_putc PutcScif
   1585  1.1   itojun 
   1586  1.1   itojun int
   1587  1.1   itojun scifcngetc(dev)
   1588  1.1   itojun 	dev_t dev;
   1589  1.1   itojun {
   1590  1.1   itojun 	int c;
   1591  1.1   itojun 	int s;
   1592  1.1   itojun 
   1593  1.1   itojun 	s = splserial();
   1594  1.1   itojun 	c = scif_getc();
   1595  1.1   itojun 	splx(s);
   1596  1.1   itojun 
   1597  1.1   itojun 	return (c);
   1598  1.1   itojun }
   1599  1.1   itojun 
   1600  1.1   itojun void
   1601  1.1   itojun scifcnputc(dev, c)
   1602  1.1   itojun 	dev_t dev;
   1603  1.1   itojun 	int c;
   1604  1.1   itojun {
   1605  1.1   itojun 	int s;
   1606  1.1   itojun 
   1607  1.1   itojun 	s = splserial();
   1608  1.1   itojun 	scif_putc(c);
   1609  1.1   itojun 	splx(s);
   1610  1.1   itojun }
   1611