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