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