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