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