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