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