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