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