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