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