Home | History | Annotate | Line # | Download | only in s3c2xx0
sscom.c revision 1.10
      1 /*	$NetBSD: sscom.c,v 1.10 2003/08/04 12:28:50 bsh Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002, 2003 Fujitsu Component Limited
      5  * Copyright (c) 2002, 2003 Genetec Corporation
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of The Fujitsu Component Limited nor the name of
     17  *    Genetec corporation may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY FUJITSU COMPONENT LIMITED AND GENETEC
     21  * CORPORATION ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     22  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24  * DISCLAIMED.  IN NO EVENT SHALL FUJITSU COMPONENT LIMITED OR GENETEC
     25  * CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*-
     36  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
     37  * All rights reserved.
     38  *
     39  * This code is derived from software contributed to The NetBSD Foundation
     40  * by Charles M. Hannum.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *        This product includes software developed by the NetBSD
     53  *        Foundation, Inc. and its contributors.
     54  * 4. Neither the name of The NetBSD Foundation nor the names of its
     55  *    contributors may be used to endorse or promote products derived
     56  *    from this software without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     59  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     60  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     61  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     62  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     63  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     65  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     66  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     67  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     68  * POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 
     71 /*
     72  * Copyright (c) 1991 The Regents of the University of California.
     73  * All rights reserved.
     74  *
     75  * Redistribution and use in source and binary forms, with or without
     76  * modification, are permitted provided that the following conditions
     77  * are met:
     78  * 1. Redistributions of source code must retain the above copyright
     79  *    notice, this list of conditions and the following disclaimer.
     80  * 2. Redistributions in binary form must reproduce the above copyright
     81  *    notice, this list of conditions and the following disclaimer in the
     82  *    documentation and/or other materials provided with the distribution.
     83  * 3. All advertising materials mentioning features or use of this software
     84  *    must display the following acknowledgement:
     85  *	This product includes software developed by the University of
     86  *	California, Berkeley and its contributors.
     87  * 4. Neither the name of the University nor the names of its contributors
     88  *    may be used to endorse or promote products derived from this software
     89  *    without specific prior written permission.
     90  *
     91  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     92  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     94  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     95  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     96  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     97  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     98  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     99  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    100  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    101  * SUCH DAMAGE.
    102  *
    103  *	@(#)com.c	7.5 (Berkeley) 5/16/91
    104  */
    105 
    106 /*
    107  * Support integrated UARTs of Samsung S3C2800/2400X/2410X
    108  * Derived from sys/dev/ic/com.c
    109  */
    110 
    111 #include <sys/cdefs.h>
    112 __KERNEL_RCSID(0, "$NetBSD: sscom.c,v 1.10 2003/08/04 12:28:50 bsh Exp $");
    113 
    114 #include "opt_sscom.h"
    115 #include "opt_ddb.h"
    116 #include "opt_kgdb.h"
    117 #include "opt_multiprocessor.h"
    118 #include "opt_lockdebug.h"
    119 
    120 #include "rnd.h"
    121 #if NRND > 0 && defined(RND_COM)
    122 #include <sys/rnd.h>
    123 #endif
    124 
    125 /*
    126  * Override cnmagic(9) macro before including <sys/systm.h>.
    127  * We need to know if cn_check_magic triggered debugger, so set a flag.
    128  * Callers of cn_check_magic must declare int cn_trapped = 0;
    129  * XXX: this is *ugly*!
    130  */
    131 #define cn_trap()				\
    132 	do {					\
    133 		console_debugger();		\
    134 		cn_trapped = 1;			\
    135 	} while (/* CONSTCOND */ 0)
    136 
    137 #include <sys/param.h>
    138 #include <sys/systm.h>
    139 #include <sys/ioctl.h>
    140 #include <sys/select.h>
    141 #include <sys/tty.h>
    142 #include <sys/proc.h>
    143 #include <sys/user.h>
    144 #include <sys/conf.h>
    145 #include <sys/file.h>
    146 #include <sys/uio.h>
    147 #include <sys/kernel.h>
    148 #include <sys/syslog.h>
    149 #include <sys/types.h>
    150 #include <sys/device.h>
    151 #include <sys/malloc.h>
    152 #include <sys/timepps.h>
    153 #include <sys/vnode.h>
    154 
    155 #include <machine/intr.h>
    156 #include <machine/bus.h>
    157 
    158 #include <arm/s3c2xx0/s3c2xx0reg.h>
    159 #include <arm/s3c2xx0/s3c2xx0var.h>
    160 #if defined(SSCOM_S3C2410) || defined(SSCOM_S3C2400)
    161 #include <arm/s3c2xx0/s3c24x0reg.h>
    162 #elif defined(SSCOM_S3C2800)
    163 #include <arm/s3c2xx0/s3c2800reg.h>
    164 #endif
    165 #include <arm/s3c2xx0/sscom_var.h>
    166 #include <dev/cons.h>
    167 
    168 dev_type_open(sscomopen);
    169 dev_type_close(sscomclose);
    170 dev_type_read(sscomread);
    171 dev_type_write(sscomwrite);
    172 dev_type_ioctl(sscomioctl);
    173 dev_type_stop(sscomstop);
    174 dev_type_tty(sscomtty);
    175 dev_type_poll(sscompoll);
    176 
    177 int	sscomcngetc	(dev_t);
    178 void	sscomcnputc	(dev_t, int);
    179 void	sscomcnpollc	(dev_t, int);
    180 
    181 #define	integrate	static inline
    182 void 	sscomsoft	(void *);
    183 
    184 integrate void sscom_rxsoft	(struct sscom_softc *, struct tty *);
    185 integrate void sscom_txsoft	(struct sscom_softc *, struct tty *);
    186 integrate void sscom_stsoft	(struct sscom_softc *, struct tty *);
    187 integrate void sscom_schedrx	(struct sscom_softc *);
    188 static void	sscom_modem(struct sscom_softc *, int);
    189 static void	sscom_break(struct sscom_softc *, int);
    190 static void	sscom_iflush(struct sscom_softc *);
    191 static void	sscom_hwiflow(struct sscom_softc *);
    192 static void	sscom_loadchannelregs(struct sscom_softc *);
    193 static void	tiocm_to_sscom(struct sscom_softc *, u_long, int);
    194 static int	sscom_to_tiocm(struct sscom_softc *);
    195 static void	tiocm_to_sscom(struct sscom_softc *, u_long, int);
    196 static int	sscom_to_tiocm(struct sscom_softc *);
    197 static void	sscom_iflush(struct sscom_softc *);
    198 
    199 static int	sscomhwiflow(struct tty *tp, int block);
    200 static int	sscom_init(bus_space_tag_t, const struct sscom_uart_info *,
    201 		    int, int, tcflag_t, bus_space_handle_t *);
    202 
    203 extern struct cfdriver sscom_cd;
    204 
    205 const struct cdevsw sscom_cdevsw = {
    206 	sscomopen, sscomclose, sscomread, sscomwrite, sscomioctl,
    207 	sscomstop, sscomtty, sscompoll, nommap, ttykqfilter, D_TTY
    208 };
    209 
    210 /*
    211  * Make this an option variable one can patch.
    212  * But be warned:  this must be a power of 2!
    213  */
    214 u_int sscom_rbuf_size = SSCOM_RING_SIZE;
    215 
    216 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    217 u_int sscom_rbuf_hiwat = (SSCOM_RING_SIZE * 1) / 4;
    218 u_int sscom_rbuf_lowat = (SSCOM_RING_SIZE * 3) / 4;
    219 
    220 static int	sscomconsunit = -1;
    221 static bus_space_tag_t sscomconstag;
    222 static bus_space_handle_t sscomconsioh;
    223 static int	sscomconsattached;
    224 static int	sscomconsrate;
    225 static tcflag_t sscomconscflag;
    226 static struct cnm_state sscom_cnm_state;
    227 
    228 #ifdef KGDB
    229 #include <sys/kgdb.h>
    230 
    231 static int sscom_kgdb_unit = -1;
    232 static bus_space_tag_t sscom_kgdb_iot;
    233 static bus_space_handle_t sscom_kgdb_ioh;
    234 static int sscom_kgdb_attached;
    235 
    236 int	sscom_kgdb_getc (void *);
    237 void	sscom_kgdb_putc (void *, int);
    238 #endif /* KGDB */
    239 
    240 #define	SSCOMUNIT_MASK  	0x7f
    241 #define	SSCOMDIALOUT_MASK	0x80
    242 
    243 #define	SSCOMUNIT(x)	(minor(x) & SSCOMUNIT_MASK)
    244 #define	SSCOMDIALOUT(x)	(minor(x) & SSCOMDIALOUT_MASK)
    245 
    246 #if 0
    247 #define	SSCOM_ISALIVE(sc)	((sc)->enabled != 0 && \
    248 			 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
    249 #else
    250 #define	SSCOM_ISALIVE(sc) ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE)
    251 #endif
    252 
    253 #define	BR	BUS_SPACE_BARRIER_READ
    254 #define	BW	BUS_SPACE_BARRIER_WRITE
    255 #define SSCOM_BARRIER(t, h, f) /* no-op */
    256 
    257 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(SSCOM_MPLOCK)
    258 
    259 #define SSCOM_LOCK(sc) simple_lock(&(sc)->sc_lock)
    260 #define SSCOM_UNLOCK(sc) simple_unlock(&(sc)->sc_lock)
    261 
    262 #else
    263 
    264 #define SSCOM_LOCK(sc)
    265 #define SSCOM_UNLOCK(sc)
    266 
    267 #endif
    268 
    269 #ifndef SSCOM_TOLERANCE
    270 #define	SSCOM_TOLERANCE	30	/* XXX: baud rate tolerance, in 0.1% units */
    271 #endif
    272 
    273 /* value for UCON */
    274 #define UCON_RXINT_MASK	  \
    275 	(UCON_RXMODE_MASK|UCON_ERRINT|UCON_TOINT|UCON_RXINT_TYPE)
    276 #define UCON_RXINT_ENABLE \
    277 	(UCON_RXMODE_INT|UCON_ERRINT|UCON_TOINT|UCON_RXINT_TYPE_LEVEL)
    278 #define UCON_TXINT_MASK   (UCON_TXMODE_MASK|UCON_TXINT_TYPE)
    279 #define UCON_TXINT_ENABLE (UCON_TXMODE_INT|UCON_TXINT_TYPE_LEVEL)
    280 
    281 /* we don't want tx interrupt on debug port, but it is needed to
    282    have transmitter active */
    283 #define UCON_DEBUGPORT	  (UCON_RXINT_ENABLE|UCON_TXINT_ENABLE)
    284 
    285 
    286 static __inline void
    287 __sscom_output_chunk(struct sscom_softc *sc, int ufstat)
    288 {
    289 	int n, space;
    290 	bus_space_tag_t iot = sc->sc_iot;
    291 	bus_space_handle_t ioh = sc->sc_ioh;
    292 
    293 	n = sc->sc_tbc;
    294 	space = 16 - ((ufstat & UFSTAT_TXCOUNT) >> UFSTAT_TXCOUNT_SHIFT);
    295 
    296 	if (n > space)
    297 		n = space;
    298 
    299 	if (n > 0) {
    300 		bus_space_write_multi_1(iot, ioh, SSCOM_UTXH, sc->sc_tba, n);
    301 		sc->sc_tbc -= n;
    302 		sc->sc_tba += n;
    303 	}
    304 }
    305 
    306 static void
    307 sscom_output_chunk(struct sscom_softc *sc)
    308 {
    309 	int ufstat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SSCOM_UFSTAT);
    310 
    311 	if (!(ufstat & UFSTAT_TXFULL))
    312 		__sscom_output_chunk(sc, ufstat);
    313 }
    314 
    315 int
    316 sscomspeed(long speed, long frequency)
    317 {
    318 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    319 
    320 	int x, err;
    321 
    322 	if (speed <= 0)
    323 		return -1;
    324 	x = divrnd(frequency / 16, speed);
    325 	if (x <= 0)
    326 		return -1;
    327 	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
    328 	if (err < 0)
    329 		err = -err;
    330 	if (err > SSCOM_TOLERANCE)
    331 		return -1;
    332 	return x-1;
    333 
    334 #undef	divrnd
    335 }
    336 
    337 void sscomstatus (struct sscom_softc *, char *);
    338 
    339 #ifdef SSCOM_DEBUG
    340 int	sscom_debug = 0;
    341 
    342 void
    343 sscomstatus(struct sscom_softc *sc, char *str)
    344 {
    345 	struct tty *tp = sc->sc_tty;
    346 	int umstat = bus_space_read_1(sc->sc_iot, sc->sc_iot, SSCOM_UMSTAT);
    347 	int umcon = bus_space_read_1(sc->sc_iot, sc->sc_iot, SSCOM_UMCON);
    348 
    349 	printf("%s: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    350 	    sc->sc_dev.dv_xname, str,
    351 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    352 	    "+",			/* DCD */
    353 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    354 	    "+",			/* DTR */
    355 	    sc->sc_tx_stopped ? "+" : "-");
    356 
    357 	printf("%s: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
    358 	    sc->sc_dev.dv_xname, str,
    359 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    360 	    ISSET(umstat, UMSTAT_CTS) ? "+" : "-",
    361 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    362 	    ISSET(umcon, UMCON_RTS) ? "+" : "-",
    363 	    sc->sc_rx_flags);
    364 }
    365 #else
    366 #define sscom_debug  0
    367 #endif
    368 
    369 static void
    370 sscom_enable_debugport(struct sscom_softc *sc)
    371 {
    372 	int s;
    373 
    374 	/* Turn on line break interrupt, set carrier. */
    375 	s = splserial();
    376 	SSCOM_LOCK(sc);
    377 	sc->sc_ucon = UCON_DEBUGPORT;
    378 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SSCOM_UCON, sc->sc_ucon);
    379 	sc->sc_umcon = UMCON_RTS|UMCON_DTR;
    380 	sc->set_modem_control(sc);
    381 	sscom_enable_rxint(sc);
    382 	sscom_disable_txint(sc);
    383 	SSCOM_UNLOCK(sc);
    384 	splx(s);
    385 }
    386 
    387 static void
    388 sscom_set_modem_control(struct sscom_softc *sc)
    389 {
    390 	/* flob RTS */
    391 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    392 	    SSCOM_UMCON, sc->sc_umcon & UMCON_HW_MASK);
    393 	/* ignore DTR */
    394 }
    395 
    396 static int
    397 sscom_read_modem_status(struct sscom_softc *sc)
    398 {
    399 	int msts;
    400 
    401 	msts = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SSCOM_UMSTAT);
    402 
    403 	/* DCD and DSR are always on */
    404 	return (msts & UMSTAT_CTS) | MSTS_DCD | MSTS_DSR;
    405 }
    406 
    407 void
    408 sscom_attach_subr(struct sscom_softc *sc)
    409 {
    410 	int unit = sc->sc_unit;
    411 	bus_space_tag_t iot = sc->sc_iot;
    412 	bus_space_handle_t ioh = sc->sc_ioh;
    413 	struct tty *tp;
    414 
    415 	callout_init(&sc->sc_diag_callout);
    416 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(SSCOM_MPLOCK)
    417 	simple_lock_init(&sc->sc_lock);
    418 #endif
    419 
    420 	sc->sc_ucon = UCON_RXINT_ENABLE|UCON_TXINT_ENABLE;
    421 
    422 	/*
    423 	 * set default for modem control hook
    424 	 */
    425 	if (sc->set_modem_control == NULL)
    426 		sc->set_modem_control = sscom_set_modem_control;
    427 	if (sc->read_modem_status == NULL)
    428 		sc->read_modem_status = sscom_read_modem_status;
    429 
    430 	/* Disable interrupts before configuring the device. */
    431 	sscom_disable_txrxint(sc);
    432 
    433 #ifdef KGDB
    434 	/*
    435 	 * Allow kgdb to "take over" this port.  If this is
    436 	 * the kgdb device, it has exclusive use.
    437 	 */
    438 	if (unit == sscom_kgdb_unit) {
    439 		SET(sc->sc_hwflags, SSCOM_HW_KGDB);
    440 		sc->sc_ucon = UCON_DEBUGPORT;
    441 	}
    442 #endif
    443 
    444 	if (unit == sscomconsunit) {
    445 		sscomconsattached = 1;
    446 
    447 		sscomconstag = iot;
    448 		sscomconsioh = ioh;
    449 
    450 		/* Make sure the console is always "hardwired". */
    451 		delay(1000);			/* XXX: wait for output to finish */
    452 		SET(sc->sc_hwflags, SSCOM_HW_CONSOLE);
    453 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    454 
    455 		sc->sc_ucon = UCON_DEBUGPORT;
    456 	}
    457 
    458 	bus_space_write_1(iot, ioh, SSCOM_UFCON,
    459 	    UFCON_TXTRIGGER_8|UFCON_RXTRIGGER_8|UFCON_FIFO_ENABLE|
    460 	    UFCON_TXFIFO_RESET|UFCON_RXFIFO_RESET);
    461 
    462 	bus_space_write_1(iot, ioh, SSCOM_UCON, sc->sc_ucon);
    463 
    464 #ifdef KGDB
    465 	if (ISSET(sc->sc_hwflags, SSCOM_HW_KGDB)) {
    466 		sscom_kgdb_attached = 1;
    467 		printf("%s: kgdb\n", sc->sc_dev.dv_xname);
    468 		sscom_enable_debugport(sc);
    469 		return;
    470 	}
    471 #endif
    472 
    473 
    474 
    475 	tp = ttymalloc();
    476 	tp->t_oproc = sscomstart;
    477 	tp->t_param = sscomparam;
    478 	tp->t_hwiflow = sscomhwiflow;
    479 
    480 	sc->sc_tty = tp;
    481 	sc->sc_rbuf = malloc(sscom_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    482 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    483 	sc->sc_rbavail = sscom_rbuf_size;
    484 	if (sc->sc_rbuf == NULL) {
    485 		printf("%s: unable to allocate ring buffer\n",
    486 		    sc->sc_dev.dv_xname);
    487 		return;
    488 	}
    489 	sc->sc_ebuf = sc->sc_rbuf + (sscom_rbuf_size << 1);
    490 
    491 	tty_attach(tp);
    492 
    493 	if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE)) {
    494 		int maj;
    495 
    496 		/* locate the major number */
    497 		maj = cdevsw_lookup_major(&sscom_cdevsw);
    498 
    499 		cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
    500 
    501 		printf("%s: console (major=%d)\n", sc->sc_dev.dv_xname, maj);
    502 	}
    503 
    504 
    505 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, sscomsoft, sc);
    506 
    507 #if NRND > 0 && defined(RND_COM)
    508 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    509 			  RND_TYPE_TTY, 0);
    510 #endif
    511 
    512 	/* if there are no enable/disable functions, assume the device
    513 	   is always enabled */
    514 
    515 	if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE))
    516 		sscom_enable_debugport(sc);
    517 	else
    518 		sscom_disable_txrxint(sc);
    519 
    520 	SET(sc->sc_hwflags, SSCOM_HW_DEV_OK);
    521 }
    522 
    523 int
    524 sscom_detach(struct device *self, int flags)
    525 {
    526 	return 0;
    527 }
    528 
    529 int
    530 sscom_activate(struct device *self, enum devact act)
    531 {
    532 #ifdef notyet
    533 	struct sscom_softc *sc = (struct sscom_softc *)self;
    534 	int s, rv = 0;
    535 
    536 	s = splserial();
    537 	SSCOM_LOCK(sc);
    538 	switch (act) {
    539 	case DVACT_ACTIVATE:
    540 		rv = EOPNOTSUPP;
    541 		break;
    542 
    543 	case DVACT_DEACTIVATE:
    544 		if (sc->sc_hwflags & (SSCOM_HW_CONSOLE|SSCOM_HW_KGDB)) {
    545 			rv = EBUSY;
    546 			break;
    547 		}
    548 
    549 		sc->enabled = 0;
    550 		break;
    551 	}
    552 
    553 	SSCOM_UNLOCK(sc);
    554 	splx(s);
    555 	return rv;
    556 #else
    557 	return 0;
    558 #endif
    559 }
    560 
    561 void
    562 sscom_shutdown(struct sscom_softc *sc)
    563 {
    564 #ifdef notyet
    565 	struct tty *tp = sc->sc_tty;
    566 	int s;
    567 
    568 	s = splserial();
    569 	SSCOM_LOCK(sc);
    570 
    571 	/* If we were asserting flow control, then deassert it. */
    572 	SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
    573 	sscom_hwiflow(sc);
    574 
    575 	/* Clear any break condition set with TIOCSBRK. */
    576 	sscom_break(sc, 0);
    577 
    578 	/*
    579 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    580 	 * notice even if we immediately open the port again.
    581 	 * Avoid tsleeping above splhigh().
    582 	 */
    583 	if (ISSET(tp->t_cflag, HUPCL)) {
    584 		sscom_modem(sc, 0);
    585 		SSCOM_UNLOCK(sc);
    586 		splx(s);
    587 		/* XXX tsleep will only timeout */
    588 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    589 		s = splserial();
    590 		SSCOM_LOCK(sc);
    591 	}
    592 
    593 	if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE))
    594 		/* interrupt on break */
    595 		sc->sc_ucon = UCON_DEBUGPORT;
    596 	else
    597 		sc->sc_ucon = 0;
    598 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SSCOM_UCON, sc->sc_ucon);
    599 
    600 #ifdef DIAGNOSTIC
    601 	if (!sc->enabled)
    602 		panic("sscom_shutdown: not enabled?");
    603 #endif
    604 	sc->enabled = 0;
    605 	SSCOM_UNLOCK(sc);
    606 	splx(s);
    607 #endif
    608 }
    609 
    610 int
    611 sscomopen(dev_t dev, int flag, int mode, struct proc *p)
    612 {
    613 	struct sscom_softc *sc;
    614 	struct tty *tp;
    615 	int s, s2;
    616 	int error;
    617 
    618 	sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    619 	if (sc == NULL || !ISSET(sc->sc_hwflags, SSCOM_HW_DEV_OK) ||
    620 		sc->sc_rbuf == NULL)
    621 		return ENXIO;
    622 
    623 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    624 		return ENXIO;
    625 
    626 #ifdef KGDB
    627 	/*
    628 	 * If this is the kgdb port, no other use is permitted.
    629 	 */
    630 	if (ISSET(sc->sc_hwflags, SSCOM_HW_KGDB))
    631 		return EBUSY;
    632 #endif
    633 
    634 	tp = sc->sc_tty;
    635 
    636 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    637 	    ISSET(tp->t_state, TS_XCLUDE) &&
    638 		p->p_ucred->cr_uid != 0)
    639 		return EBUSY;
    640 
    641 	s = spltty();
    642 
    643 	/*
    644 	 * Do the following iff this is a first open.
    645 	 */
    646 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    647 		struct termios t;
    648 
    649 		tp->t_dev = dev;
    650 
    651 		s2 = splserial();
    652 		SSCOM_LOCK(sc);
    653 
    654 		/* Turn on interrupts. */
    655 		sscom_enable_txrxint(sc);
    656 
    657 		/* Fetch the current modem control status, needed later. */
    658 		sc->sc_msts = sc->read_modem_status(sc);
    659 
    660 #if 0
    661 		/* Clear PPS capture state on first open. */
    662 		sc->sc_ppsmask = 0;
    663 		sc->ppsparam.mode = 0;
    664 #endif
    665 
    666 		SSCOM_UNLOCK(sc);
    667 		splx(s2);
    668 
    669 		/*
    670 		 * Initialize the termios status to the defaults.  Add in the
    671 		 * sticky bits from TIOCSFLAGS.
    672 		 */
    673 		t.c_ispeed = 0;
    674 		if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE)) {
    675 			t.c_ospeed = sscomconsrate;
    676 			t.c_cflag = sscomconscflag;
    677 		} else {
    678 			t.c_ospeed = TTYDEF_SPEED;
    679 			t.c_cflag = TTYDEF_CFLAG;
    680 		}
    681 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    682 			SET(t.c_cflag, CLOCAL);
    683 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    684 			SET(t.c_cflag, CRTSCTS);
    685 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    686 			SET(t.c_cflag, MDMBUF);
    687 		/* Make sure sscomparam() will do something. */
    688 		tp->t_ospeed = 0;
    689 		(void) sscomparam(tp, &t);
    690 		tp->t_iflag = TTYDEF_IFLAG;
    691 		tp->t_oflag = TTYDEF_OFLAG;
    692 		tp->t_lflag = TTYDEF_LFLAG;
    693 		ttychars(tp);
    694 		ttsetwater(tp);
    695 
    696 		s2 = splserial();
    697 		SSCOM_LOCK(sc);
    698 
    699 		/*
    700 		 * Turn on DTR.  We must always do this, even if carrier is not
    701 		 * present, because otherwise we'd have to use TIOCSDTR
    702 		 * immediately after setting CLOCAL, which applications do not
    703 		 * expect.  We always assert DTR while the device is open
    704 		 * unless explicitly requested to deassert it.
    705 		 */
    706 		sscom_modem(sc, 1);
    707 
    708 		/* Clear the input ring, and unblock. */
    709 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    710 		sc->sc_rbavail = sscom_rbuf_size;
    711 		sscom_iflush(sc);
    712 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    713 		sscom_hwiflow(sc);
    714 
    715 		if (sscom_debug)
    716 			sscomstatus(sc, "sscomopen  ");
    717 
    718 		SSCOM_UNLOCK(sc);
    719 		splx(s2);
    720 	}
    721 
    722 	splx(s);
    723 
    724 	error = ttyopen(tp, SSCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    725 	if (error)
    726 		goto bad;
    727 
    728 	error = (*tp->t_linesw->l_open)(dev, tp);
    729 	if (error)
    730 		goto bad;
    731 
    732 	return 0;
    733 
    734 bad:
    735 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    736 		/*
    737 		 * We failed to open the device, and nobody else had it opened.
    738 		 * Clean up the state as appropriate.
    739 		 */
    740 		sscom_shutdown(sc);
    741 	}
    742 
    743 	return error;
    744 }
    745 
    746 int
    747 sscomclose(dev_t dev, int flag, int mode, struct proc *p)
    748 {
    749 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    750 	struct tty *tp = sc->sc_tty;
    751 
    752 	/* XXX This is for cons.c. */
    753 	if (!ISSET(tp->t_state, TS_ISOPEN))
    754 		return 0;
    755 
    756 	(*tp->t_linesw->l_close)(tp, flag);
    757 	ttyclose(tp);
    758 
    759 	if (SSCOM_ISALIVE(sc) == 0)
    760 		return 0;
    761 
    762 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    763 		/*
    764 		 * Although we got a last close, the device may still be in
    765 		 * use; e.g. if this was the dialout node, and there are still
    766 		 * processes waiting for carrier on the non-dialout node.
    767 		 */
    768 		sscom_shutdown(sc);
    769 	}
    770 
    771 	return 0;
    772 }
    773 
    774 int
    775 sscomread(dev_t dev, struct uio *uio, int flag)
    776 {
    777 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    778 	struct tty *tp = sc->sc_tty;
    779 
    780 	if (SSCOM_ISALIVE(sc) == 0)
    781 		return EIO;
    782 
    783 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    784 }
    785 
    786 int
    787 sscomwrite(dev_t dev, struct uio *uio, int flag)
    788 {
    789 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    790 	struct tty *tp = sc->sc_tty;
    791 
    792 	if (SSCOM_ISALIVE(sc) == 0)
    793 		return EIO;
    794 
    795 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    796 }
    797 
    798 int
    799 sscompoll(dev_t dev, int events, struct proc *p)
    800 {
    801 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    802 	struct tty *tp = sc->sc_tty;
    803 
    804 	if (SSCOM_ISALIVE(sc) == 0)
    805 		return EIO;
    806 
    807 	return (*tp->t_linesw->l_poll)(tp, events, p);
    808 }
    809 
    810 struct tty *
    811 sscomtty(dev_t dev)
    812 {
    813 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    814 	struct tty *tp = sc->sc_tty;
    815 
    816 	return tp;
    817 }
    818 
    819 int
    820 sscomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    821 {
    822 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    823 	struct tty *tp = sc->sc_tty;
    824 	int error;
    825 	int s;
    826 
    827 	if (SSCOM_ISALIVE(sc) == 0)
    828 		return EIO;
    829 
    830 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    831 	if (error != EPASSTHROUGH)
    832 		return error;
    833 
    834 	error = ttioctl(tp, cmd, data, flag, p);
    835 	if (error != EPASSTHROUGH)
    836 		return error;
    837 
    838 	error = 0;
    839 
    840 	s = splserial();
    841 	SSCOM_LOCK(sc);
    842 
    843 	switch (cmd) {
    844 	case TIOCSBRK:
    845 		sscom_break(sc, 1);
    846 		break;
    847 
    848 	case TIOCCBRK:
    849 		sscom_break(sc, 0);
    850 		break;
    851 
    852 	case TIOCSDTR:
    853 		sscom_modem(sc, 1);
    854 		break;
    855 
    856 	case TIOCCDTR:
    857 		sscom_modem(sc, 0);
    858 		break;
    859 
    860 	case TIOCGFLAGS:
    861 		*(int *)data = sc->sc_swflags;
    862 		break;
    863 
    864 	case TIOCSFLAGS:
    865 		error = suser(p->p_ucred, &p->p_acflag);
    866 		if (error)
    867 			break;
    868 		sc->sc_swflags = *(int *)data;
    869 		break;
    870 
    871 	case TIOCMSET:
    872 	case TIOCMBIS:
    873 	case TIOCMBIC:
    874 		tiocm_to_sscom(sc, cmd, *(int *)data);
    875 		break;
    876 
    877 	case TIOCMGET:
    878 		*(int *)data = sscom_to_tiocm(sc);
    879 		break;
    880 
    881 	default:
    882 		error = EPASSTHROUGH;
    883 		break;
    884 	}
    885 
    886 	SSCOM_UNLOCK(sc);
    887 	splx(s);
    888 
    889 	if (sscom_debug)
    890 		sscomstatus(sc, "sscomioctl ");
    891 
    892 	return error;
    893 }
    894 
    895 integrate void
    896 sscom_schedrx(struct sscom_softc *sc)
    897 {
    898 
    899 	sc->sc_rx_ready = 1;
    900 
    901 	/* Wake up the poller. */
    902 	softintr_schedule(sc->sc_si);
    903 }
    904 
    905 static void
    906 sscom_break(struct sscom_softc *sc, int onoff)
    907 {
    908 
    909 	if (onoff)
    910 		SET(sc->sc_ucon, UCON_SBREAK);
    911 	else
    912 		CLR(sc->sc_ucon, UCON_SBREAK);
    913 
    914 	if (!sc->sc_heldchange) {
    915 		if (sc->sc_tx_busy) {
    916 			sc->sc_heldtbc = sc->sc_tbc;
    917 			sc->sc_tbc = 0;
    918 			sc->sc_heldchange = 1;
    919 		} else
    920 			sscom_loadchannelregs(sc);
    921 	}
    922 }
    923 
    924 static void
    925 sscom_modem(struct sscom_softc *sc, int onoff)
    926 {
    927 	if (onoff)
    928 		SET(sc->sc_umcon, UMCON_DTR);
    929 	else
    930 		CLR(sc->sc_umcon, UMCON_DTR);
    931 
    932 	if (!sc->sc_heldchange) {
    933 		if (sc->sc_tx_busy) {
    934 			sc->sc_heldtbc = sc->sc_tbc;
    935 			sc->sc_tbc = 0;
    936 			sc->sc_heldchange = 1;
    937 		} else
    938 			sscom_loadchannelregs(sc);
    939 	}
    940 }
    941 
    942 static void
    943 tiocm_to_sscom(struct sscom_softc *sc, u_long how, int ttybits)
    944 {
    945 	u_char sscombits;
    946 
    947 	sscombits = 0;
    948 	if (ISSET(ttybits, TIOCM_DTR))
    949 		sscombits = UMCON_DTR;
    950 	if (ISSET(ttybits, TIOCM_RTS))
    951 		SET(sscombits, UMCON_RTS);
    952 
    953 	switch (how) {
    954 	case TIOCMBIC:
    955 		CLR(sc->sc_umcon, sscombits);
    956 		break;
    957 
    958 	case TIOCMBIS:
    959 		SET(sc->sc_umcon, sscombits);
    960 		break;
    961 
    962 	case TIOCMSET:
    963 		CLR(sc->sc_umcon, UMCON_DTR);
    964 		SET(sc->sc_umcon, sscombits);
    965 		break;
    966 	}
    967 
    968 	if (!sc->sc_heldchange) {
    969 		if (sc->sc_tx_busy) {
    970 			sc->sc_heldtbc = sc->sc_tbc;
    971 			sc->sc_tbc = 0;
    972 			sc->sc_heldchange = 1;
    973 		} else
    974 			sscom_loadchannelregs(sc);
    975 	}
    976 }
    977 
    978 static int
    979 sscom_to_tiocm(struct sscom_softc *sc)
    980 {
    981 	u_char sscombits;
    982 	int ttybits = 0;
    983 
    984 	sscombits = sc->sc_umcon;
    985 #if 0
    986 	if (ISSET(sscombits, MCR_DTR))
    987 		SET(ttybits, TIOCM_DTR);
    988 #endif
    989 	if (ISSET(sscombits, UMCON_RTS))
    990 		SET(ttybits, TIOCM_RTS);
    991 
    992 	sscombits = sc->sc_msts;
    993 	if (ISSET(sscombits, MSTS_DCD))
    994 		SET(ttybits, TIOCM_CD);
    995 	if (ISSET(sscombits, MSTS_DSR))
    996 		SET(ttybits, TIOCM_DSR);
    997 	if (ISSET(sscombits, MSTS_CTS))
    998 		SET(ttybits, TIOCM_CTS);
    999 
   1000 	if (sc->sc_ucon != 0)
   1001 		SET(ttybits, TIOCM_LE);
   1002 
   1003 	return ttybits;
   1004 }
   1005 
   1006 static int
   1007 cflag2lcr(tcflag_t cflag)
   1008 {
   1009 	u_char lcr = ULCON_PARITY_NONE;
   1010 
   1011 	switch (cflag & (PARENB|PARODD)) {
   1012 	case PARENB|PARODD: lcr = ULCON_PARITY_ODD; break;
   1013 	case PARENB: lcr = ULCON_PARITY_EVEN;
   1014 	}
   1015 
   1016 	switch (ISSET(cflag, CSIZE)) {
   1017 	case CS5:
   1018 		SET(lcr, ULCON_LENGTH_5);
   1019 		break;
   1020 	case CS6:
   1021 		SET(lcr, ULCON_LENGTH_6);
   1022 		break;
   1023 	case CS7:
   1024 		SET(lcr, ULCON_LENGTH_7);
   1025 		break;
   1026 	case CS8:
   1027 		SET(lcr, ULCON_LENGTH_8);
   1028 		break;
   1029 	}
   1030 	if (ISSET(cflag, CSTOPB))
   1031 		SET(lcr, ULCON_STOP);
   1032 
   1033 	return lcr;
   1034 }
   1035 
   1036 int
   1037 sscomparam(struct tty *tp, struct termios *t)
   1038 {
   1039 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
   1040 	int ospeed;
   1041 	u_char lcr;
   1042 	int s;
   1043 
   1044 	if (SSCOM_ISALIVE(sc) == 0)
   1045 		return EIO;
   1046 
   1047 	ospeed = sscomspeed(t->c_ospeed, sc->sc_frequency);
   1048 
   1049 	/* Check requested parameters. */
   1050 	if (ospeed < 0)
   1051 		return EINVAL;
   1052 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1053 		return EINVAL;
   1054 
   1055 	/*
   1056 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1057 	 * is always active.
   1058 	 */
   1059 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
   1060 	    ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE)) {
   1061 		SET(t->c_cflag, CLOCAL);
   1062 		CLR(t->c_cflag, HUPCL);
   1063 	}
   1064 
   1065 	/*
   1066 	 * If there were no changes, don't do anything.  This avoids dropping
   1067 	 * input and improves performance when all we did was frob things like
   1068 	 * VMIN and VTIME.
   1069 	 */
   1070 	if (tp->t_ospeed == t->c_ospeed &&
   1071 	    tp->t_cflag == t->c_cflag)
   1072 		return 0;
   1073 
   1074 	lcr = cflag2lcr(t->c_cflag);
   1075 
   1076 	s = splserial();
   1077 	SSCOM_LOCK(sc);
   1078 
   1079 	sc->sc_ulcon = lcr;
   1080 
   1081 	/*
   1082 	 * If we're not in a mode that assumes a connection is present, then
   1083 	 * ignore carrier changes.
   1084 	 */
   1085 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1086 		sc->sc_msr_dcd = 0;
   1087 	else
   1088 		sc->sc_msr_dcd = MSTS_DCD;
   1089 
   1090 	/*
   1091 	 * Set the flow control pins depending on the current flow control
   1092 	 * mode.
   1093 	 */
   1094 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1095 		sc->sc_mcr_dtr = UMCON_DTR;
   1096 		sc->sc_mcr_rts = UMCON_RTS;
   1097 		sc->sc_msr_cts = MSTS_CTS;
   1098 	}
   1099 	else if (ISSET(t->c_cflag, MDMBUF)) {
   1100 		/*
   1101 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1102 		 * carrier detection.
   1103 		 */
   1104 		sc->sc_mcr_dtr = 0;
   1105 		sc->sc_mcr_rts = UMCON_DTR;
   1106 		sc->sc_msr_cts = MSTS_DCD;
   1107 	}
   1108 	else {
   1109 		/*
   1110 		 * If no flow control, then always set RTS.  This will make
   1111 		 * the other side happy if it mistakenly thinks we're doing
   1112 		 * RTS/CTS flow control.
   1113 		 */
   1114 		sc->sc_mcr_dtr = UMCON_DTR | UMCON_RTS;
   1115 		sc->sc_mcr_rts = 0;
   1116 		sc->sc_msr_cts = 0;
   1117 		if (ISSET(sc->sc_umcon, UMCON_DTR))
   1118 			SET(sc->sc_umcon, UMCON_RTS);
   1119 		else
   1120 			CLR(sc->sc_umcon, UMCON_RTS);
   1121 	}
   1122 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
   1123 
   1124 	if (ospeed == 0)
   1125 		CLR(sc->sc_umcon, sc->sc_mcr_dtr);
   1126 	else
   1127 		SET(sc->sc_umcon, sc->sc_mcr_dtr);
   1128 
   1129 	sc->sc_ubrdiv = ospeed;
   1130 
   1131 	/* And copy to tty. */
   1132 	tp->t_ispeed = 0;
   1133 	tp->t_ospeed = t->c_ospeed;
   1134 	tp->t_cflag = t->c_cflag;
   1135 
   1136 	if (!sc->sc_heldchange) {
   1137 		if (sc->sc_tx_busy) {
   1138 			sc->sc_heldtbc = sc->sc_tbc;
   1139 			sc->sc_tbc = 0;
   1140 			sc->sc_heldchange = 1;
   1141 		} else
   1142 			sscom_loadchannelregs(sc);
   1143 	}
   1144 
   1145 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1146 		/* Disable the high water mark. */
   1147 		sc->sc_r_hiwat = 0;
   1148 		sc->sc_r_lowat = 0;
   1149 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1150 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1151 			sscom_schedrx(sc);
   1152 		}
   1153 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1154 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1155 			sscom_hwiflow(sc);
   1156 		}
   1157 	} else {
   1158 		sc->sc_r_hiwat = sscom_rbuf_hiwat;
   1159 		sc->sc_r_lowat = sscom_rbuf_lowat;
   1160 	}
   1161 
   1162 	SSCOM_UNLOCK(sc);
   1163 	splx(s);
   1164 
   1165 	/*
   1166 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1167 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1168 	 * explicit request.
   1169 	 */
   1170 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msts, MSTS_DCD));
   1171 
   1172 	if (sscom_debug)
   1173 		sscomstatus(sc, "sscomparam ");
   1174 
   1175 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1176 		if (sc->sc_tx_stopped) {
   1177 			sc->sc_tx_stopped = 0;
   1178 			sscomstart(tp);
   1179 		}
   1180 	}
   1181 
   1182 	return 0;
   1183 }
   1184 
   1185 static void
   1186 sscom_iflush(struct sscom_softc *sc)
   1187 {
   1188 	bus_space_tag_t iot = sc->sc_iot;
   1189 	bus_space_handle_t ioh = sc->sc_ioh;
   1190 	int timo;
   1191 
   1192 
   1193 	timo = 50000;
   1194 	/* flush any pending I/O */
   1195 	while ( sscom_rxrdy(iot, ioh) && --timo)
   1196 		(void)sscom_getc(iot,ioh);
   1197 #ifdef DIAGNOSTIC
   1198 	if (!timo)
   1199 		printf("%s: sscom_iflush timeout\n", sc->sc_dev.dv_xname);
   1200 #endif
   1201 }
   1202 
   1203 static void
   1204 sscom_loadchannelregs(struct sscom_softc *sc)
   1205 {
   1206 	bus_space_tag_t iot = sc->sc_iot;
   1207 	bus_space_handle_t ioh = sc->sc_ioh;
   1208 
   1209 	/* XXXXX necessary? */
   1210 	sscom_iflush(sc);
   1211 
   1212 	bus_space_write_2(iot, ioh, SSCOM_UCON, 0);
   1213 
   1214 #if 0
   1215 	if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
   1216 		bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   1217 		bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
   1218 	}
   1219 #endif
   1220 
   1221 	bus_space_write_2(iot, ioh, SSCOM_UBRDIV, sc->sc_ubrdiv);
   1222 	bus_space_write_1(iot, ioh, SSCOM_ULCON, sc->sc_ulcon);
   1223 	sc->set_modem_control(sc);
   1224 	bus_space_write_2(iot, ioh, SSCOM_UCON, sc->sc_ucon);
   1225 }
   1226 
   1227 static int
   1228 sscomhwiflow(struct tty *tp, int block)
   1229 {
   1230 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
   1231 	int s;
   1232 
   1233 	if (SSCOM_ISALIVE(sc) == 0)
   1234 		return 0;
   1235 
   1236 	if (sc->sc_mcr_rts == 0)
   1237 		return 0;
   1238 
   1239 	s = splserial();
   1240 	SSCOM_LOCK(sc);
   1241 
   1242 	if (block) {
   1243 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1244 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1245 			sscom_hwiflow(sc);
   1246 		}
   1247 	} else {
   1248 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1249 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1250 			sscom_schedrx(sc);
   1251 		}
   1252 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1253 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1254 			sscom_hwiflow(sc);
   1255 		}
   1256 	}
   1257 
   1258 	SSCOM_UNLOCK(sc);
   1259 	splx(s);
   1260 	return 1;
   1261 }
   1262 
   1263 /*
   1264  * (un)block input via hw flowcontrol
   1265  */
   1266 static void
   1267 sscom_hwiflow(struct sscom_softc *sc)
   1268 {
   1269 	if (sc->sc_mcr_rts == 0)
   1270 		return;
   1271 
   1272 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
   1273 		CLR(sc->sc_umcon, sc->sc_mcr_rts);
   1274 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
   1275 	} else {
   1276 		SET(sc->sc_umcon, sc->sc_mcr_rts);
   1277 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
   1278 	}
   1279 	sc->set_modem_control(sc);
   1280 }
   1281 
   1282 
   1283 void
   1284 sscomstart(struct tty *tp)
   1285 {
   1286 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
   1287 	int s;
   1288 
   1289 	if (SSCOM_ISALIVE(sc) == 0)
   1290 		return;
   1291 
   1292 	s = spltty();
   1293 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1294 		goto out;
   1295 	if (sc->sc_tx_stopped)
   1296 		goto out;
   1297 
   1298 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1299 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1300 			CLR(tp->t_state, TS_ASLEEP);
   1301 			wakeup(&tp->t_outq);
   1302 		}
   1303 		selwakeup(&tp->t_wsel);
   1304 		if (tp->t_outq.c_cc == 0)
   1305 			goto out;
   1306 	}
   1307 
   1308 	/* Grab the first contiguous region of buffer space. */
   1309 	{
   1310 		u_char *tba;
   1311 		int tbc;
   1312 
   1313 		tba = tp->t_outq.c_cf;
   1314 		tbc = ndqb(&tp->t_outq, 0);
   1315 
   1316 		(void)splserial();
   1317 		SSCOM_LOCK(sc);
   1318 
   1319 		sc->sc_tba = tba;
   1320 		sc->sc_tbc = tbc;
   1321 	}
   1322 
   1323 	SET(tp->t_state, TS_BUSY);
   1324 	sc->sc_tx_busy = 1;
   1325 
   1326 	/* Output the first chunk of the contiguous buffer. */
   1327 	sscom_output_chunk(sc);
   1328 
   1329 	/* Enable transmit completion interrupts if necessary. */
   1330 	if ((sc->sc_hwflags & SSCOM_HW_TXINT) == 0)
   1331 		sscom_enable_txint(sc);
   1332 
   1333 	SSCOM_UNLOCK(sc);
   1334 out:
   1335 	splx(s);
   1336 	return;
   1337 }
   1338 
   1339 /*
   1340  * Stop output on a line.
   1341  */
   1342 void
   1343 sscomstop(struct tty *tp, int flag)
   1344 {
   1345 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
   1346 	int s;
   1347 
   1348 	s = splserial();
   1349 	SSCOM_LOCK(sc);
   1350 	if (ISSET(tp->t_state, TS_BUSY)) {
   1351 		/* Stop transmitting at the next chunk. */
   1352 		sc->sc_tbc = 0;
   1353 		sc->sc_heldtbc = 0;
   1354 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1355 			SET(tp->t_state, TS_FLUSH);
   1356 	}
   1357 	SSCOM_UNLOCK(sc);
   1358 	splx(s);
   1359 }
   1360 
   1361 void
   1362 sscomdiag(void *arg)
   1363 {
   1364 	struct sscom_softc *sc = arg;
   1365 	int overflows, floods;
   1366 	int s;
   1367 
   1368 	s = splserial();
   1369 	SSCOM_LOCK(sc);
   1370 	overflows = sc->sc_overflows;
   1371 	sc->sc_overflows = 0;
   1372 	floods = sc->sc_floods;
   1373 	sc->sc_floods = 0;
   1374 	sc->sc_errors = 0;
   1375 	SSCOM_UNLOCK(sc);
   1376 	splx(s);
   1377 
   1378 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1379 	    sc->sc_dev.dv_xname,
   1380 	    overflows, overflows == 1 ? "" : "s",
   1381 	    floods, floods == 1 ? "" : "s");
   1382 }
   1383 
   1384 integrate void
   1385 sscom_rxsoft(struct sscom_softc *sc, struct tty *tp)
   1386 {
   1387 	int (*rint) (int, struct tty *) = tp->t_linesw->l_rint;
   1388 	u_char *get, *end;
   1389 	u_int cc, scc;
   1390 	u_char rsr;
   1391 	int code;
   1392 	int s;
   1393 
   1394 	end = sc->sc_ebuf;
   1395 	get = sc->sc_rbget;
   1396 	scc = cc = sscom_rbuf_size - sc->sc_rbavail;
   1397 
   1398 	if (cc == sscom_rbuf_size) {
   1399 		sc->sc_floods++;
   1400 		if (sc->sc_errors++ == 0)
   1401 			callout_reset(&sc->sc_diag_callout, 60 * hz,
   1402 			    sscomdiag, sc);
   1403 	}
   1404 
   1405 	while (cc) {
   1406 		code = get[0];
   1407 		rsr = get[1];
   1408 		if (rsr) {
   1409 			if (ISSET(rsr, UERSTAT_OVERRUN)) {
   1410 				sc->sc_overflows++;
   1411 				if (sc->sc_errors++ == 0)
   1412 					callout_reset(&sc->sc_diag_callout,
   1413 					    60 * hz, sscomdiag, sc);
   1414 			}
   1415 			if (ISSET(rsr, UERSTAT_BREAK | UERSTAT_FRAME))
   1416 				SET(code, TTY_FE);
   1417 			if (ISSET(rsr, UERSTAT_PARITY))
   1418 				SET(code, TTY_PE);
   1419 		}
   1420 		if ((*rint)(code, tp) == -1) {
   1421 			/*
   1422 			 * The line discipline's buffer is out of space.
   1423 			 */
   1424 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1425 				/*
   1426 				 * We're either not using flow control, or the
   1427 				 * line discipline didn't tell us to block for
   1428 				 * some reason.  Either way, we have no way to
   1429 				 * know when there's more space available, so
   1430 				 * just drop the rest of the data.
   1431 				 */
   1432 				get += cc << 1;
   1433 				if (get >= end)
   1434 					get -= sscom_rbuf_size << 1;
   1435 				cc = 0;
   1436 			} else {
   1437 				/*
   1438 				 * Don't schedule any more receive processing
   1439 				 * until the line discipline tells us there's
   1440 				 * space available (through sscomhwiflow()).
   1441 				 * Leave the rest of the data in the input
   1442 				 * buffer.
   1443 				 */
   1444 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1445 			}
   1446 			break;
   1447 		}
   1448 		get += 2;
   1449 		if (get >= end)
   1450 			get = sc->sc_rbuf;
   1451 		cc--;
   1452 	}
   1453 
   1454 	if (cc != scc) {
   1455 		sc->sc_rbget = get;
   1456 		s = splserial();
   1457 		SSCOM_LOCK(sc);
   1458 
   1459 		cc = sc->sc_rbavail += scc - cc;
   1460 		/* Buffers should be ok again, release possible block. */
   1461 		if (cc >= sc->sc_r_lowat) {
   1462 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1463 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1464 				sscom_enable_rxint(sc);
   1465 				sc->sc_ucon |= UCON_ERRINT;
   1466 				bus_space_write_2(sc->sc_iot, sc->sc_ioh, SSCOM_UCON,
   1467 						  sc->sc_ucon);
   1468 
   1469 			}
   1470 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1471 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1472 				sscom_hwiflow(sc);
   1473 			}
   1474 		}
   1475 		SSCOM_UNLOCK(sc);
   1476 		splx(s);
   1477 	}
   1478 }
   1479 
   1480 integrate void
   1481 sscom_txsoft(struct sscom_softc *sc, struct tty *tp)
   1482 {
   1483 
   1484 	CLR(tp->t_state, TS_BUSY);
   1485 	if (ISSET(tp->t_state, TS_FLUSH))
   1486 		CLR(tp->t_state, TS_FLUSH);
   1487 	else
   1488 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1489 	(*tp->t_linesw->l_start)(tp);
   1490 }
   1491 
   1492 integrate void
   1493 sscom_stsoft(struct sscom_softc *sc, struct tty *tp)
   1494 {
   1495 	u_char msr, delta;
   1496 	int s;
   1497 
   1498 	s = splserial();
   1499 	SSCOM_LOCK(sc);
   1500 	msr = sc->sc_msts;
   1501 	delta = sc->sc_msr_delta;
   1502 	sc->sc_msr_delta = 0;
   1503 	SSCOM_UNLOCK(sc);
   1504 	splx(s);
   1505 
   1506 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1507 		/*
   1508 		 * Inform the tty layer that carrier detect changed.
   1509 		 */
   1510 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSTS_DCD));
   1511 	}
   1512 
   1513 	if (ISSET(delta, sc->sc_msr_cts)) {
   1514 		/* Block or unblock output according to flow control. */
   1515 		if (ISSET(msr, sc->sc_msr_cts)) {
   1516 			sc->sc_tx_stopped = 0;
   1517 			(*tp->t_linesw->l_start)(tp);
   1518 		} else {
   1519 			sc->sc_tx_stopped = 1;
   1520 		}
   1521 	}
   1522 
   1523 	if (sscom_debug)
   1524 		sscomstatus(sc, "sscom_stsoft");
   1525 }
   1526 
   1527 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1528 void
   1529 sscomsoft(void *arg)
   1530 {
   1531 	struct sscom_softc *sc = arg;
   1532 	struct tty *tp;
   1533 
   1534 	if (SSCOM_ISALIVE(sc) == 0)
   1535 		return;
   1536 
   1537 	{
   1538 		tp = sc->sc_tty;
   1539 
   1540 		if (sc->sc_rx_ready) {
   1541 			sc->sc_rx_ready = 0;
   1542 			sscom_rxsoft(sc, tp);
   1543 		}
   1544 
   1545 		if (sc->sc_st_check) {
   1546 			sc->sc_st_check = 0;
   1547 			sscom_stsoft(sc, tp);
   1548 		}
   1549 
   1550 		if (sc->sc_tx_done) {
   1551 			sc->sc_tx_done = 0;
   1552 			sscom_txsoft(sc, tp);
   1553 		}
   1554 	}
   1555 }
   1556 #else
   1557 #error sscom needs GENERIC_SOFT_INERRUPTS
   1558 #endif
   1559 
   1560 
   1561 int
   1562 sscomrxintr(void *arg)
   1563 {
   1564 	struct sscom_softc *sc = arg;
   1565 	bus_space_tag_t iot = sc->sc_iot;
   1566 	bus_space_handle_t ioh = sc->sc_ioh;
   1567 	u_char *put, *end;
   1568 	u_int cc;
   1569 
   1570 	if (SSCOM_ISALIVE(sc) == 0)
   1571 		return 0;
   1572 
   1573 	SSCOM_LOCK(sc);
   1574 
   1575 	end = sc->sc_ebuf;
   1576 	put = sc->sc_rbput;
   1577 	cc = sc->sc_rbavail;
   1578 
   1579 	do {
   1580 		u_char	msts, delta;
   1581 		u_char  uerstat;
   1582 		uint16_t ufstat;
   1583 
   1584 		ufstat = bus_space_read_2(iot, ioh, SSCOM_UFSTAT);
   1585 
   1586 		/* XXX: break interrupt with no character? */
   1587 
   1588 		if ( (ufstat & (UFSTAT_RXCOUNT|UFSTAT_RXFULL)) &&
   1589 		    !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1590 
   1591 			while (cc > 0) {
   1592 				int cn_trapped = 0;
   1593 
   1594 				/* get status and received character.
   1595 				   read status register first */
   1596 				uerstat = sscom_geterr(iot, ioh);
   1597 				put[0] = sscom_getc(iot, ioh);
   1598 
   1599 				if (ISSET(uerstat, UERSTAT_BREAK)) {
   1600 					int cn_trapped = 0;
   1601 					cn_check_magic(sc->sc_tty->t_dev,
   1602 					    CNC_BREAK, sscom_cnm_state);
   1603 					if (cn_trapped)
   1604 						continue;
   1605 #if defined(KGDB)
   1606 					if (ISSET(sc->sc_hwflags,
   1607 					    SSCOM_HW_KGDB)) {
   1608 						kgdb_connect(1);
   1609 						continue;
   1610 					}
   1611 #endif
   1612 				}
   1613 
   1614 				put[1] = uerstat;
   1615 				cn_check_magic(sc->sc_tty->t_dev,
   1616 					       put[0], sscom_cnm_state);
   1617 				if (!cn_trapped) {
   1618 					put += 2;
   1619 					if (put >= end)
   1620 						put = sc->sc_rbuf;
   1621 					cc--;
   1622 				}
   1623 
   1624 				ufstat = bus_space_read_2(iot, ioh, SSCOM_UFSTAT);
   1625 				if ( (ufstat & (UFSTAT_RXFULL|UFSTAT_RXCOUNT)) == 0 )
   1626 					break;
   1627 			}
   1628 
   1629 			/*
   1630 			 * Current string of incoming characters ended because
   1631 			 * no more data was available or we ran out of space.
   1632 			 * Schedule a receive event if any data was received.
   1633 			 * If we're out of space, turn off receive interrupts.
   1634 			 */
   1635 			sc->sc_rbput = put;
   1636 			sc->sc_rbavail = cc;
   1637 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1638 				sc->sc_rx_ready = 1;
   1639 
   1640 			/*
   1641 			 * See if we are in danger of overflowing a buffer. If
   1642 			 * so, use hardware flow control to ease the pressure.
   1643 			 */
   1644 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   1645 			    cc < sc->sc_r_hiwat) {
   1646 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1647 				sscom_hwiflow(sc);
   1648 			}
   1649 
   1650 			/*
   1651 			 * If we're out of space, disable receive interrupts
   1652 			 * until the queue has drained a bit.
   1653 			 */
   1654 			if (!cc) {
   1655 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1656 				sscom_disable_rxint(sc);
   1657 				sc->sc_ucon &= ~UCON_ERRINT;
   1658 				bus_space_write_2(iot, ioh, SSCOM_UCON, sc->sc_ucon);
   1659 			}
   1660 		}
   1661 
   1662 
   1663 		msts = sc->read_modem_status(sc);
   1664 		delta = msts ^ sc->sc_msts;
   1665 		sc->sc_msts = msts;
   1666 
   1667 #ifdef notyet
   1668 		/*
   1669 		 * Pulse-per-second (PSS) signals on edge of DCD?
   1670 		 * Process these even if line discipline is ignoring DCD.
   1671 		 */
   1672 		if (delta & sc->sc_ppsmask) {
   1673 			struct timeval tv;
   1674 		    	if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
   1675 				/* XXX nanotime() */
   1676 				microtime(&tv);
   1677 				TIMEVAL_TO_TIMESPEC(&tv,
   1678 				    &sc->ppsinfo.assert_timestamp);
   1679 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
   1680 					timespecadd(&sc->ppsinfo.assert_timestamp,
   1681 					    &sc->ppsparam.assert_offset,
   1682 						    &sc->ppsinfo.assert_timestamp);
   1683 				}
   1684 
   1685 #ifdef PPS_SYNC
   1686 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
   1687 					hardpps(&tv, tv.tv_usec);
   1688 #endif
   1689 				sc->ppsinfo.assert_sequence++;
   1690 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   1691 
   1692 			} else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
   1693 				/* XXX nanotime() */
   1694 				microtime(&tv);
   1695 				TIMEVAL_TO_TIMESPEC(&tv,
   1696 				    &sc->ppsinfo.clear_timestamp);
   1697 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
   1698 					timespecadd(&sc->ppsinfo.clear_timestamp,
   1699 					    &sc->ppsparam.clear_offset,
   1700 					    &sc->ppsinfo.clear_timestamp);
   1701 				}
   1702 
   1703 #ifdef PPS_SYNC
   1704 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
   1705 					hardpps(&tv, tv.tv_usec);
   1706 #endif
   1707 				sc->ppsinfo.clear_sequence++;
   1708 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   1709 			}
   1710 		}
   1711 #endif
   1712 
   1713 		/*
   1714 		 * Process normal status changes
   1715 		 */
   1716 		if (ISSET(delta, sc->sc_msr_mask)) {
   1717 			SET(sc->sc_msr_delta, delta);
   1718 
   1719 			/*
   1720 			 * Stop output immediately if we lose the output
   1721 			 * flow control signal or carrier detect.
   1722 			 */
   1723 			if (ISSET(~msts, sc->sc_msr_mask)) {
   1724 				sc->sc_tbc = 0;
   1725 				sc->sc_heldtbc = 0;
   1726 #ifdef SSCOM_DEBUG
   1727 				if (sscom_debug)
   1728 					sscomstatus(sc, "sscomintr  ");
   1729 #endif
   1730 			}
   1731 
   1732 			sc->sc_st_check = 1;
   1733 		}
   1734 
   1735 		/*
   1736 		 * Done handling any receive interrupts.
   1737 		 */
   1738 
   1739 		/*
   1740 		 * If we've delayed a parameter change, do it
   1741 		 * now, and restart * output.
   1742 		 */
   1743 		if ((ufstat & UFSTAT_TXCOUNT) == 0) {
   1744 			/* XXX: we should check transmitter empty also */
   1745 
   1746 			if (sc->sc_heldchange) {
   1747 				sscom_loadchannelregs(sc);
   1748 				sc->sc_heldchange = 0;
   1749 				sc->sc_tbc = sc->sc_heldtbc;
   1750 				sc->sc_heldtbc = 0;
   1751 			}
   1752 		}
   1753 
   1754 
   1755 	} while (0);
   1756 
   1757 	SSCOM_UNLOCK(sc);
   1758 
   1759 	/* Wake up the poller. */
   1760 	softintr_schedule(sc->sc_si);
   1761 
   1762 #if NRND > 0 && defined(RND_COM)
   1763 	rnd_add_uint32(&sc->rnd_source, iir | rsr);
   1764 #endif
   1765 
   1766 	return 1;
   1767 }
   1768 
   1769 int
   1770 sscomtxintr(void *arg)
   1771 {
   1772 	struct sscom_softc *sc = arg;
   1773 	bus_space_tag_t iot = sc->sc_iot;
   1774 	bus_space_handle_t ioh = sc->sc_ioh;
   1775 	uint16_t ufstat;
   1776 
   1777 	if (SSCOM_ISALIVE(sc) == 0)
   1778 		return 0;
   1779 
   1780 	SSCOM_LOCK(sc);
   1781 
   1782 	ufstat = bus_space_read_2(iot, ioh, SSCOM_UFSTAT);
   1783 
   1784 	/*
   1785 	 * If we've delayed a parameter change, do it
   1786 	 * now, and restart * output.
   1787 	 */
   1788 	if (sc->sc_heldchange && (ufstat & UFSTAT_TXCOUNT) == 0) {
   1789 		/* XXX: we should check transmitter empty also */
   1790 		sscom_loadchannelregs(sc);
   1791 		sc->sc_heldchange = 0;
   1792 		sc->sc_tbc = sc->sc_heldtbc;
   1793 		sc->sc_heldtbc = 0;
   1794 	}
   1795 
   1796 	/*
   1797 	 * See if data can be transmitted as well. Schedule tx
   1798 	 * done event if no data left and tty was marked busy.
   1799 	 */
   1800 	if (!ISSET(ufstat,UFSTAT_TXFULL)) {
   1801 		/*
   1802 		 * Output the next chunk of the contiguous
   1803 		 * buffer, if any.
   1804 		 */
   1805 		if (sc->sc_tbc > 0) {
   1806 			__sscom_output_chunk(sc, ufstat);
   1807 		}
   1808 		else {
   1809 			/*
   1810 			 * Disable transmit sscompletion
   1811 			 * interrupts if necessary.
   1812 			 */
   1813 			if (sc->sc_hwflags & SSCOM_HW_TXINT)
   1814 				sscom_disable_txint(sc);
   1815 			if (sc->sc_tx_busy) {
   1816 				sc->sc_tx_busy = 0;
   1817 				sc->sc_tx_done = 1;
   1818 			}
   1819 		}
   1820 	}
   1821 
   1822 	SSCOM_UNLOCK(sc);
   1823 
   1824 	/* Wake up the poller. */
   1825 	softintr_schedule(sc->sc_si);
   1826 
   1827 #if NRND > 0 && defined(RND_COM)
   1828 	rnd_add_uint32(&sc->rnd_source, iir | rsr);
   1829 #endif
   1830 
   1831 	return 1;
   1832 }
   1833 
   1834 
   1835 #if defined(KGDB) || defined(SSCOM0CONSOLE) || defined(SSCOM1CONSOLE)
   1836 /*
   1837  * Initialize UART for use as console or KGDB line.
   1838  */
   1839 static int
   1840 sscom_init(bus_space_tag_t iot, const struct sscom_uart_info *config,
   1841     int rate, int frequency, tcflag_t cflag, bus_space_handle_t *iohp)
   1842 {
   1843 	bus_space_handle_t ioh;
   1844 	bus_addr_t iobase = config->iobase;
   1845 
   1846 	if (bus_space_map(iot, iobase, SSCOM_SIZE, 0, &ioh))
   1847 		return ENOMEM; /* ??? */
   1848 
   1849 	bus_space_write_2(iot, ioh, SSCOM_UCON, 0);
   1850 	bus_space_write_1(iot, ioh, SSCOM_UFCON,
   1851 	    UFCON_TXTRIGGER_8 | UFCON_RXTRIGGER_8 |
   1852 	    UFCON_TXFIFO_RESET | UFCON_RXFIFO_RESET |
   1853 	    UFCON_FIFO_ENABLE );
   1854 	/* tx/rx fifo reset are auto-cleared */
   1855 
   1856 	rate = sscomspeed(rate, frequency);
   1857 	bus_space_write_2(iot, ioh, SSCOM_UBRDIV, rate);
   1858 	bus_space_write_2(iot, ioh, SSCOM_ULCON, cflag2lcr(cflag));
   1859 
   1860 	/* enable UART */
   1861 	bus_space_write_2(iot, ioh, SSCOM_UCON,
   1862 	    UCON_TXMODE_INT|UCON_RXMODE_INT);
   1863 	bus_space_write_2(iot, ioh, SSCOM_UMCON, UMCON_RTS);
   1864 
   1865 	*iohp = ioh;
   1866 	return 0;
   1867 }
   1868 
   1869 #endif
   1870 
   1871 #if defined(SSCOM0CONSOLE) || defined(SSCOM1CONSOLE)
   1872 /*
   1873  * Following are all routines needed for SSCOM to act as console
   1874  */
   1875 struct consdev sscomcons = {
   1876 	NULL, NULL, sscomcngetc, sscomcnputc, sscomcnpollc, NULL,
   1877 	NULL, NULL, NODEV, CN_NORMAL
   1878 };
   1879 
   1880 
   1881 int
   1882 sscom_cnattach(bus_space_tag_t iot, const struct sscom_uart_info *config,
   1883     int rate, int frequency, tcflag_t cflag)
   1884 {
   1885 	int res;
   1886 
   1887 	res = sscom_init(iot, config, rate, frequency, cflag, &sscomconsioh);
   1888 	if (res)
   1889 		return res;
   1890 
   1891 	cn_tab = &sscomcons;
   1892 	cn_init_magic(&sscom_cnm_state);
   1893 	cn_set_magic("\047\001"); /* default magic is BREAK */
   1894 
   1895 	sscomconstag = iot;
   1896 	sscomconsunit = config->unit;
   1897 	sscomconsrate = rate;
   1898 	sscomconscflag = cflag;
   1899 
   1900 	return 0;
   1901 }
   1902 
   1903 void
   1904 sscom_cndetach(void)
   1905 {
   1906 	bus_space_unmap(sscomconstag, sscomconsioh, SSCOM_SIZE);
   1907 	sscomconstag = NULL;
   1908 
   1909 	cn_tab = NULL;
   1910 }
   1911 
   1912 /*
   1913  * The read-ahead code is so that you can detect pending in-band
   1914  * cn_magic in polled mode while doing output rather than having to
   1915  * wait until the kernel decides it needs input.
   1916  */
   1917 
   1918 #define MAX_READAHEAD	20
   1919 static int sscom_readahead[MAX_READAHEAD];
   1920 static int sscom_readaheadcount = 0;
   1921 
   1922 int
   1923 sscomcngetc(dev_t dev)
   1924 {
   1925 	int s = splserial();
   1926 	u_char stat, c;
   1927 
   1928 	/* got a character from reading things earlier */
   1929 	if (sscom_readaheadcount > 0) {
   1930 		int i;
   1931 
   1932 		c = sscom_readahead[0];
   1933 		for (i = 1; i < sscom_readaheadcount; i++) {
   1934 			sscom_readahead[i-1] = sscom_readahead[i];
   1935 		}
   1936 		sscom_readaheadcount--;
   1937 		splx(s);
   1938 		return c;
   1939 	}
   1940 
   1941 	/* block until a character becomes available */
   1942 	while (!sscom_rxrdy(sscomconstag, sscomconsioh))
   1943 		;
   1944 
   1945 	c = sscom_getc(sscomconstag, sscomconsioh);
   1946 	stat = sscom_geterr(sscomconstag, sscomconsioh);
   1947 	{
   1948 		int cn_trapped = 0; /* unused */
   1949 #ifdef DDB
   1950 		extern int db_active;
   1951 		if (!db_active)
   1952 #endif
   1953 			cn_check_magic(dev, c, sscom_cnm_state);
   1954 	}
   1955 	splx(s);
   1956 	return c;
   1957 }
   1958 
   1959 /*
   1960  * Console kernel output character routine.
   1961  */
   1962 void
   1963 sscomcnputc(dev_t dev, int c)
   1964 {
   1965 	int s = splserial();
   1966 	int timo;
   1967 
   1968 	int cin, stat;
   1969 	if (sscom_readaheadcount < MAX_READAHEAD &&
   1970 	    sscom_rxrdy(sscomconstag, sscomconsioh)) {
   1971 
   1972 		int cn_trapped = 0;
   1973 		cin = sscom_getc(sscomconstag, sscomconsioh);
   1974 		stat = sscom_geterr(sscomconstag, sscomconsioh);
   1975 		cn_check_magic(dev, cin, sscom_cnm_state);
   1976 		sscom_readahead[sscom_readaheadcount++] = cin;
   1977 	}
   1978 
   1979 	/* wait for any pending transmission to finish */
   1980 	timo = 150000;
   1981 	while (ISSET(bus_space_read_2(sscomconstag, sscomconsioh, SSCOM_UFSTAT),
   1982 		   UFSTAT_TXFULL) && --timo)
   1983 		continue;
   1984 
   1985 	bus_space_write_1(sscomconstag, sscomconsioh, SSCOM_UTXH, c);
   1986 	SSCOM_BARRIER(sscomconstag, sscomconsioh, BR | BW);
   1987 
   1988 #if 0
   1989 	/* wait for this transmission to complete */
   1990 	timo = 1500000;
   1991 	while (!ISSET(bus_space_read_1(sscomconstag, sscomconsioh, SSCOM_UTRSTAT),
   1992 		   UTRSTAT_TXEMPTY) && --timo)
   1993 		continue;
   1994 #endif
   1995 	splx(s);
   1996 }
   1997 
   1998 void
   1999 sscomcnpollc(dev_t dev, int on)
   2000 {
   2001 
   2002 }
   2003 
   2004 #endif /* SSCOM0CONSOLE||SSCOM1CONSOLE */
   2005 
   2006 #ifdef KGDB
   2007 int
   2008 sscom_kgdb_attach(bus_space_tag_t iot, const struct sscom_uart_info *config,
   2009     int rate, int frequency, tcflag_t cflag)
   2010 {
   2011 	int res;
   2012 
   2013 	if (iot == sscomconstag && config->unit == sscomconsunit) {
   2014 		printf( "console==kgdb_port (%d): kgdb disabled\n", sscomconsunit);
   2015 		return EBUSY; /* cannot share with console */
   2016 	}
   2017 
   2018 	res = sscom_init(iot, config, rate, frequency, cflag, &sscom_kgdb_ioh);
   2019 	if (res)
   2020 		return res;
   2021 
   2022 	kgdb_attach(sscom_kgdb_getc, sscom_kgdb_putc, NULL);
   2023 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   2024 
   2025 	sscom_kgdb_iot = iot;
   2026 	sscom_kgdb_unit = config->unit;
   2027 
   2028 	return 0;
   2029 }
   2030 
   2031 /* ARGSUSED */
   2032 int
   2033 sscom_kgdb_getc(void *arg)
   2034 {
   2035 	int c, stat;
   2036 
   2037 	/* block until a character becomes available */
   2038 	while (!sscom_rxrdy(sscom_kgdb_iot, sscom_kgdb_ioh))
   2039 		;
   2040 
   2041 	c = sscom_getc(sscom_kgdb_iot, sscom_kgdb_ioh);
   2042 	stat = sscom_geterr(sscom_kgdb_iot, sscom_kgdb_ioh);
   2043 
   2044 	return c;
   2045 }
   2046 
   2047 /* ARGSUSED */
   2048 void
   2049 sscom_kgdb_putc(void *arg, int c)
   2050 {
   2051 	int timo;
   2052 
   2053 	/* wait for any pending transmission to finish */
   2054 	timo = 150000;
   2055 	while (ISSET(bus_space_read_2(sscom_kgdb_iot, sscom_kgdb_ioh,
   2056 	    SSCOM_UFSTAT), UFSTAT_TXFULL) && --timo)
   2057 		continue;
   2058 
   2059 	bus_space_write_1(sscom_kgdb_iot, sscom_kgdb_ioh, SSCOM_UTXH, c);
   2060 	SSCOM_BARRIER(sscom_kgdb_iot, sscom_kgdb_ioh, BR | BW);
   2061 
   2062 #if 0
   2063 	/* wait for this transmission to complete */
   2064 	timo = 1500000;
   2065 	while (!ISSET(bus_space_read_1(sscom_kgdb_iot, sscom_kgdb_ioh,
   2066 	    SSCOM_UTRSTAT), UTRSTAT_TXEMPTY) && --timo)
   2067 		continue;
   2068 #endif
   2069 }
   2070 #endif /* KGDB */
   2071 
   2072 /* helper function to identify the sscom ports used by
   2073  console or KGDB (and not yet autoconf attached) */
   2074 int
   2075 sscom_is_console(bus_space_tag_t iot, int unit,
   2076     bus_space_handle_t *ioh)
   2077 {
   2078 	bus_space_handle_t help;
   2079 
   2080 	if (!sscomconsattached &&
   2081 	    iot == sscomconstag && unit == sscomconsunit)
   2082 		help = sscomconsioh;
   2083 #ifdef KGDB
   2084 	else if (!sscom_kgdb_attached &&
   2085 	    iot == sscom_kgdb_iot && unit == sscom_kgdb_unit)
   2086 		help = sscom_kgdb_ioh;
   2087 #endif
   2088 	else
   2089 		return 0;
   2090 
   2091 	if (ioh)
   2092 		*ioh = help;
   2093 	return 1;
   2094 }
   2095