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