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