Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.232
      1 /*	$NetBSD: com.c,v 1.232 2005/02/27 00:27:01 perry Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1991 The Regents of the University of California.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     68  */
     69 
     70 /*
     71  * COM driver, uses National Semiconductor NS16450/NS16550AF UART
     72  * Supports automatic hardware flow control on StarTech ST16C650A UART
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: com.c,v 1.232 2005/02/27 00:27:01 perry Exp $");
     77 
     78 #include "opt_com.h"
     79 #include "opt_ddb.h"
     80 #include "opt_kgdb.h"
     81 #include "opt_lockdebug.h"
     82 #include "opt_multiprocessor.h"
     83 #include "opt_ntp.h"
     84 
     85 #include "rnd.h"
     86 #if NRND > 0 && defined(RND_COM)
     87 #include <sys/rnd.h>
     88 #endif
     89 
     90 /* The COM16650 option was renamed to COM_16650. */
     91 #ifdef COM16650
     92 #error Obsolete COM16650 option; use COM_16650 instead.
     93 #endif
     94 
     95 /*
     96  * Override cnmagic(9) macro before including <sys/systm.h>.
     97  * We need to know if cn_check_magic triggered debugger, so set a flag.
     98  * Callers of cn_check_magic must declare int cn_trapped = 0;
     99  * XXX: this is *ugly*!
    100  */
    101 #define cn_trap()				\
    102 	do {					\
    103 		console_debugger();		\
    104 		cn_trapped = 1;			\
    105 	} while (/* CONSTCOND */ 0)
    106 
    107 #include <sys/param.h>
    108 #include <sys/systm.h>
    109 #include <sys/ioctl.h>
    110 #include <sys/select.h>
    111 #include <sys/tty.h>
    112 #include <sys/proc.h>
    113 #include <sys/user.h>
    114 #include <sys/conf.h>
    115 #include <sys/file.h>
    116 #include <sys/uio.h>
    117 #include <sys/kernel.h>
    118 #include <sys/syslog.h>
    119 #include <sys/device.h>
    120 #include <sys/malloc.h>
    121 #include <sys/timepps.h>
    122 #include <sys/vnode.h>
    123 
    124 #include <machine/intr.h>
    125 #include <machine/bus.h>
    126 
    127 #include <dev/ic/comreg.h>
    128 #include <dev/ic/comvar.h>
    129 #include <dev/ic/ns16550reg.h>
    130 #include <dev/ic/st16650reg.h>
    131 #ifdef COM_HAYESP
    132 #include <dev/ic/hayespreg.h>
    133 #endif
    134 #define	com_lcr	com_cfcr
    135 #include <dev/cons.h>
    136 
    137 #ifdef COM_HAYESP
    138 int comprobeHAYESP(bus_space_handle_t hayespioh, struct com_softc *sc);
    139 #endif
    140 
    141 static void com_enable_debugport(struct com_softc *);
    142 
    143 void	com_config(struct com_softc *);
    144 void	com_shutdown(struct com_softc *);
    145 int	comspeed(long, long, int);
    146 static	u_char	cflag2lcr(tcflag_t);
    147 int	comparam(struct tty *, struct termios *);
    148 void	comstart(struct tty *);
    149 int	comhwiflow(struct tty *, int);
    150 
    151 void	com_loadchannelregs(struct com_softc *);
    152 void	com_hwiflow(struct com_softc *);
    153 void	com_break(struct com_softc *, int);
    154 void	com_modem(struct com_softc *, int);
    155 void	tiocm_to_com(struct com_softc *, u_long, int);
    156 int	com_to_tiocm(struct com_softc *);
    157 void	com_iflush(struct com_softc *);
    158 
    159 int	com_common_getc(dev_t, bus_space_tag_t, bus_space_handle_t);
    160 void	com_common_putc(dev_t, bus_space_tag_t, bus_space_handle_t, int);
    161 
    162 int	cominit(bus_space_tag_t, bus_addr_t, int, int, int, tcflag_t,
    163 	    bus_space_handle_t *);
    164 
    165 int	comcngetc(dev_t);
    166 void	comcnputc(dev_t, int);
    167 void	comcnpollc(dev_t, int);
    168 
    169 #define	integrate	static inline
    170 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    171 void 	comsoft(void *);
    172 #else
    173 #ifndef __NO_SOFT_SERIAL_INTERRUPT
    174 void 	comsoft(void);
    175 #else
    176 void 	comsoft(void *);
    177 static struct callout comsoft_callout = CALLOUT_INITIALIZER;
    178 #endif
    179 #endif
    180 integrate void com_rxsoft(struct com_softc *, struct tty *);
    181 integrate void com_txsoft(struct com_softc *, struct tty *);
    182 integrate void com_stsoft(struct com_softc *, struct tty *);
    183 integrate void com_schedrx(struct com_softc *);
    184 void	comdiag(void *);
    185 
    186 extern struct cfdriver com_cd;
    187 
    188 dev_type_open(comopen);
    189 dev_type_close(comclose);
    190 dev_type_read(comread);
    191 dev_type_write(comwrite);
    192 dev_type_ioctl(comioctl);
    193 dev_type_stop(comstop);
    194 dev_type_tty(comtty);
    195 dev_type_poll(compoll);
    196 
    197 const struct cdevsw com_cdevsw = {
    198 	comopen, comclose, comread, comwrite, comioctl,
    199 	comstop, comtty, compoll, nommap, ttykqfilter, D_TTY
    200 };
    201 
    202 /*
    203  * Make this an option variable one can patch.
    204  * But be warned:  this must be a power of 2!
    205  */
    206 u_int com_rbuf_size = COM_RING_SIZE;
    207 
    208 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    209 u_int com_rbuf_hiwat = (COM_RING_SIZE * 1) / 4;
    210 u_int com_rbuf_lowat = (COM_RING_SIZE * 3) / 4;
    211 
    212 static bus_addr_t	comconsaddr;
    213 static bus_space_tag_t comconstag;
    214 static bus_space_handle_t comconsioh;
    215 static int	comconsattached;
    216 static int comconsrate;
    217 static tcflag_t comconscflag;
    218 static struct cnm_state com_cnm_state;
    219 
    220 static int ppscap =
    221 	PPS_TSFMT_TSPEC |
    222 	PPS_CAPTUREASSERT |
    223 	PPS_CAPTURECLEAR |
    224 	PPS_OFFSETASSERT | PPS_OFFSETCLEAR;
    225 
    226 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
    227 #ifdef __NO_SOFT_SERIAL_INTERRUPT
    228 volatile int	com_softintr_scheduled;
    229 #endif
    230 #endif
    231 
    232 #ifdef KGDB
    233 #include <sys/kgdb.h>
    234 
    235 static bus_addr_t com_kgdb_addr;
    236 static bus_space_tag_t com_kgdb_iot;
    237 static bus_space_handle_t com_kgdb_ioh;
    238 static int com_kgdb_attached;
    239 
    240 int	com_kgdb_getc(void *);
    241 void	com_kgdb_putc(void *, int);
    242 #endif /* KGDB */
    243 
    244 #define	COMUNIT_MASK	0x7ffff
    245 #define	COMDIALOUT_MASK	0x80000
    246 
    247 #define	COMUNIT(x)	(minor(x) & COMUNIT_MASK)
    248 #define	COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
    249 
    250 #define	COM_ISALIVE(sc)	((sc)->enabled != 0 && \
    251 			 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
    252 
    253 #define	BR	BUS_SPACE_BARRIER_READ
    254 #define	BW	BUS_SPACE_BARRIER_WRITE
    255 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
    256 
    257 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(COM_MPLOCK)
    258 
    259 #define COM_LOCK(sc) simple_lock(&(sc)->sc_lock)
    260 #define COM_UNLOCK(sc) simple_unlock(&(sc)->sc_lock)
    261 
    262 #else
    263 
    264 #define COM_LOCK(sc)
    265 #define COM_UNLOCK(sc)
    266 
    267 #endif
    268 
    269 /*ARGSUSED*/
    270 int
    271 comspeed(long speed, long frequency, int type)
    272 {
    273 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    274 
    275 	int x, err;
    276 
    277 #if 0
    278 	if (speed == 0)
    279 		return (0);
    280 #endif
    281 	if (speed <= 0)
    282 		return (-1);
    283 	x = divrnd(frequency / 16, speed);
    284 	if (x <= 0)
    285 		return (-1);
    286 	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
    287 	if (err < 0)
    288 		err = -err;
    289 	if (err > COM_TOLERANCE)
    290 		return (-1);
    291 	return (x);
    292 
    293 #undef	divrnd
    294 }
    295 
    296 #ifdef COM_DEBUG
    297 int	com_debug = 0;
    298 
    299 void comstatus(struct com_softc *, char *);
    300 void
    301 comstatus(struct com_softc *sc, char *str)
    302 {
    303 	struct tty *tp = sc->sc_tty;
    304 
    305 	printf("%s: %s %cclocal  %cdcd %cts_carr_on %cdtr %ctx_stopped\n",
    306 	    sc->sc_dev.dv_xname, str,
    307 	    ISSET(tp->t_cflag, CLOCAL) ? '+' : '-',
    308 	    ISSET(sc->sc_msr, MSR_DCD) ? '+' : '-',
    309 	    ISSET(tp->t_state, TS_CARR_ON) ? '+' : '-',
    310 	    ISSET(sc->sc_mcr, MCR_DTR) ? '+' : '-',
    311 	    sc->sc_tx_stopped ? '+' : '-');
    312 
    313 	printf("%s: %s %ccrtscts %ccts %cts_ttstop  %crts rx_flags=0x%x\n",
    314 	    sc->sc_dev.dv_xname, str,
    315 	    ISSET(tp->t_cflag, CRTSCTS) ? '+' : '-',
    316 	    ISSET(sc->sc_msr, MSR_CTS) ? '+' : '-',
    317 	    ISSET(tp->t_state, TS_TTSTOP) ? '+' : '-',
    318 	    ISSET(sc->sc_mcr, MCR_RTS) ? '+' : '-',
    319 	    sc->sc_rx_flags);
    320 }
    321 #endif
    322 
    323 int
    324 comprobe1(bus_space_tag_t iot, bus_space_handle_t ioh)
    325 {
    326 
    327 	/* force access to id reg */
    328 	bus_space_write_1(iot, ioh, com_lcr, LCR_8BITS);
    329 	bus_space_write_1(iot, ioh, com_iir, 0);
    330 	if ((bus_space_read_1(iot, ioh, com_lcr) != LCR_8BITS) ||
    331 	    (bus_space_read_1(iot, ioh, com_iir) & 0x38))
    332 		return (0);
    333 
    334 	return (1);
    335 }
    336 
    337 #ifdef COM_HAYESP
    338 int
    339 comprobeHAYESP(bus_space_handle_t hayespioh, struct com_softc *sc)
    340 {
    341 	char	val, dips;
    342 	int	combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
    343 	bus_space_tag_t iot = sc->sc_iot;
    344 
    345 	/*
    346 	 * Hayes ESP cards have two iobases.  One is for compatibility with
    347 	 * 16550 serial chips, and at the same ISA PC base addresses.  The
    348 	 * other is for ESP-specific enhanced features, and lies at a
    349 	 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
    350 	 */
    351 
    352 	/* Test for ESP signature */
    353 	if ((bus_space_read_1(iot, hayespioh, 0) & 0xf3) == 0)
    354 		return (0);
    355 
    356 	/*
    357 	 * ESP is present at ESP enhanced base address; unknown com port
    358 	 */
    359 
    360 	/* Get the dip-switch configurations */
    361 	bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
    362 	dips = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1);
    363 
    364 	/* Determine which com port this ESP card services: bits 0,1 of  */
    365 	/*  dips is the port # (0-3); combaselist[val] is the com_iobase */
    366 	if (sc->sc_iobase != combaselist[dips & 0x03])
    367 		return (0);
    368 
    369 	printf(": ESP");
    370 
    371  	/* Check ESP Self Test bits. */
    372 	/* Check for ESP version 2.0: bits 4,5,6 == 010 */
    373 	bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
    374 	val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1); /* Clear reg1 */
    375 	val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS2);
    376 	if ((val & 0x70) < 0x20) {
    377 		printf("-old (%o)", val & 0x70);
    378 		/* we do not support the necessary features */
    379 		return (0);
    380 	}
    381 
    382 	/* Check for ability to emulate 16550: bit 8 == 1 */
    383 	if ((dips & 0x80) == 0) {
    384 		printf(" slave");
    385 		/* XXX Does slave really mean no 16550 support?? */
    386 		return (0);
    387 	}
    388 
    389 	/*
    390 	 * If we made it this far, we are a full-featured ESP v2.0 (or
    391 	 * better), at the correct com port address.
    392 	 */
    393 
    394 	sc->sc_type = COM_TYPE_HAYESP;
    395 	printf(", 1024 byte fifo\n");
    396 	return (1);
    397 }
    398 #endif
    399 
    400 static void
    401 com_enable_debugport(struct com_softc *sc)
    402 {
    403 	int s;
    404 
    405 	/* Turn on line break interrupt, set carrier. */
    406 	s = splserial();
    407 	COM_LOCK(sc);
    408 	sc->sc_ier = IER_ERXRDY;
    409 #ifdef COM_PXA2X0
    410 	if (sc->sc_type == COM_TYPE_PXA2x0)
    411 		sc->sc_ier |= IER_EUART | IER_ERXTOUT;
    412 #endif
    413 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
    414 	SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
    415 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
    416 	COM_UNLOCK(sc);
    417 	splx(s);
    418 }
    419 
    420 void
    421 com_attach_subr(struct com_softc *sc)
    422 {
    423 	bus_addr_t iobase = sc->sc_iobase;
    424 	bus_space_tag_t iot = sc->sc_iot;
    425 	bus_space_handle_t ioh = sc->sc_ioh;
    426 	struct tty *tp;
    427 #ifdef COM_16650
    428 	u_int8_t lcr;
    429 #endif
    430 #ifdef COM_HAYESP
    431 	int	hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
    432 	int	*hayespp;
    433 #endif
    434 	const char *fifo_msg = NULL;
    435 
    436 	callout_init(&sc->sc_diag_callout);
    437 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(COM_MPLOCK)
    438 	simple_lock_init(&sc->sc_lock);
    439 #endif
    440 
    441 	/* Disable interrupts before configuring the device. */
    442 #ifdef COM_PXA2X0
    443 	if (sc->sc_type == COM_TYPE_PXA2x0)
    444 		sc->sc_ier = IER_EUART;
    445 	else
    446 #endif
    447 		sc->sc_ier = 0;
    448 	bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
    449 
    450 	if (iot == comconstag && iobase == comconsaddr) {
    451 		comconsattached = 1;
    452 
    453 		/* Make sure the console is always "hardwired". */
    454 		delay(10000);			/* wait for output to finish */
    455 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    456 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    457 	}
    458 
    459 #ifdef COM_HAYESP
    460 	sc->sc_prescaler = 0;			/* set prescaler to x1. */
    461 
    462 	/* Look for a Hayes ESP board. */
    463 	for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
    464 		bus_space_handle_t hayespioh;
    465 
    466 #define	HAYESP_NPORTS	8			/* XXX XXX XXX ??? ??? ??? */
    467 		if (bus_space_map(iot, *hayespp, HAYESP_NPORTS, 0, &hayespioh))
    468 			continue;
    469 		if (comprobeHAYESP(hayespioh, sc)) {
    470 			sc->sc_hayespioh = hayespioh;
    471 			sc->sc_fifolen = 1024;
    472 
    473 			break;
    474 		}
    475 		bus_space_unmap(iot, hayespioh, HAYESP_NPORTS);
    476 	}
    477 	/* No ESP; look for other things. */
    478 	if (sc->sc_type != COM_TYPE_HAYESP) {
    479 #endif
    480 	sc->sc_fifolen = 1;
    481 	/* look for a NS 16550AF UART with FIFOs */
    482 	bus_space_write_1(iot, ioh, com_fifo,
    483 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
    484 	delay(100);
    485 	if (ISSET(bus_space_read_1(iot, ioh, com_iir), IIR_FIFO_MASK)
    486 	    == IIR_FIFO_MASK)
    487 		if (ISSET(bus_space_read_1(iot, ioh, com_fifo), FIFO_TRIGGER_14)
    488 		    == FIFO_TRIGGER_14) {
    489 			SET(sc->sc_hwflags, COM_HW_FIFO);
    490 
    491 #ifdef COM_16650
    492 			/*
    493 			 * IIR changes into the EFR if LCR is set to LCR_EERS
    494 			 * on 16650s. We also know IIR != 0 at this point.
    495 			 * Write 0 into the EFR, and read it. If the result
    496 			 * is 0, we have a 16650.
    497 			 *
    498 			 * Older 16650s were broken; the test to detect them
    499 			 * is taken from the Linux driver. Apparently
    500 			 * setting DLAB enable gives access to the EFR on
    501 			 * these chips.
    502 			 */
    503 			lcr = bus_space_read_1(iot, ioh, com_lcr);
    504 			bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
    505 			bus_space_write_1(iot, ioh, com_efr, 0);
    506 			if (bus_space_read_1(iot, ioh, com_efr) == 0) {
    507 				bus_space_write_1(iot, ioh, com_lcr,
    508 				    lcr | LCR_DLAB);
    509 				if (bus_space_read_1(iot, ioh, com_efr) == 0) {
    510 					CLR(sc->sc_hwflags, COM_HW_FIFO);
    511 					sc->sc_fifolen = 0;
    512 				} else {
    513 					SET(sc->sc_hwflags, COM_HW_FLOW);
    514 					sc->sc_fifolen = 32;
    515 				}
    516 			} else
    517 #endif
    518 				sc->sc_fifolen = 16;
    519 
    520 #ifdef COM_16650
    521 			bus_space_write_1(iot, ioh, com_lcr, lcr);
    522 			if (sc->sc_fifolen == 0)
    523 				fifo_msg = "st16650, broken fifo";
    524 			else if (sc->sc_fifolen == 32)
    525 				fifo_msg = "st16650a, working fifo";
    526 			else
    527 #endif
    528 				fifo_msg = "ns16550a, working fifo";
    529 		} else
    530 			fifo_msg = "ns16550, broken fifo";
    531 	else
    532 		fifo_msg = "ns8250 or ns16450, no fifo";
    533 	bus_space_write_1(iot, ioh, com_fifo, 0);
    534 	/*
    535 	 * Some chips will clear down both Tx and Rx FIFOs when zero is
    536 	 * written to com_fifo. If this chip is the console, writing zero
    537 	 * results in some of the chip/FIFO description being lost, so delay
    538 	 * printing it until now.
    539 	 */
    540 	delay(10);
    541 	aprint_normal(": %s\n", fifo_msg);
    542 	if (ISSET(sc->sc_hwflags, COM_HW_TXFIFO_DISABLE)) {
    543 		sc->sc_fifolen = 1;
    544 		aprint_normal("%s: txfifo disabled\n", sc->sc_dev.dv_xname);
    545 	}
    546 #ifdef COM_HAYESP
    547 	}
    548 #endif
    549 
    550 	tp = ttymalloc();
    551 	tp->t_oproc = comstart;
    552 	tp->t_param = comparam;
    553 	tp->t_hwiflow = comhwiflow;
    554 
    555 	sc->sc_tty = tp;
    556 	sc->sc_rbuf = malloc(com_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    557 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    558 	sc->sc_rbavail = com_rbuf_size;
    559 	if (sc->sc_rbuf == NULL) {
    560 		aprint_error("%s: unable to allocate ring buffer\n",
    561 		    sc->sc_dev.dv_xname);
    562 		return;
    563 	}
    564 	sc->sc_ebuf = sc->sc_rbuf + (com_rbuf_size << 1);
    565 
    566 	tty_attach(tp);
    567 
    568 	if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
    569 		SET(sc->sc_mcr, MCR_IENABLE);
    570 
    571 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    572 		int maj;
    573 
    574 		/* locate the major number */
    575 		maj = cdevsw_lookup_major(&com_cdevsw);
    576 
    577 		tp->t_dev = cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
    578 
    579 		aprint_normal("%s: console\n", sc->sc_dev.dv_xname);
    580 	}
    581 
    582 #ifdef KGDB
    583 	/*
    584 	 * Allow kgdb to "take over" this port.  If this is
    585 	 * not the console and is the kgdb device, it has
    586 	 * exclusive use.  If it's the console _and_ the
    587 	 * kgdb device, it doesn't.
    588 	 */
    589 	if (iot == com_kgdb_iot && iobase == com_kgdb_addr) {
    590 		if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    591 			com_kgdb_attached = 1;
    592 
    593 			SET(sc->sc_hwflags, COM_HW_KGDB);
    594 		}
    595 		aprint_normal("%s: kgdb\n", sc->sc_dev.dv_xname);
    596 	}
    597 #endif
    598 
    599 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    600 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, comsoft, sc);
    601 #endif
    602 
    603 #if NRND > 0 && defined(RND_COM)
    604 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    605 			  RND_TYPE_TTY, 0);
    606 #endif
    607 
    608 	/* if there are no enable/disable functions, assume the device
    609 	   is always enabled */
    610 	if (!sc->enable)
    611 		sc->enabled = 1;
    612 
    613 	com_config(sc);
    614 
    615 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    616 }
    617 
    618 void
    619 com_config(struct com_softc *sc)
    620 {
    621 	bus_space_tag_t iot = sc->sc_iot;
    622 	bus_space_handle_t ioh = sc->sc_ioh;
    623 
    624 	/* Disable interrupts before configuring the device. */
    625 #ifdef COM_PXA2X0
    626 	if (sc->sc_type == COM_TYPE_PXA2x0)
    627 		sc->sc_ier = IER_EUART;
    628 	else
    629 #endif
    630 		sc->sc_ier = 0;
    631 	bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
    632 	(void) bus_space_read_1(iot, ioh, com_iir);
    633 
    634 #ifdef COM_HAYESP
    635 	/* Look for a Hayes ESP board. */
    636 	if (sc->sc_type == COM_TYPE_HAYESP) {
    637 		sc->sc_fifolen = 1024;
    638 
    639 		/* Set 16550 compatibility mode */
    640 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
    641 				  HAYESP_SETMODE);
    642 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    643 				  HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
    644 				  HAYESP_MODE_SCALE);
    645 
    646 		/* Set RTS/CTS flow control */
    647 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
    648 				  HAYESP_SETFLOWTYPE);
    649 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    650 				  HAYESP_FLOW_RTS);
    651 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    652 				  HAYESP_FLOW_CTS);
    653 
    654 		/* Set flow control levels */
    655 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
    656 				  HAYESP_SETRXFLOW);
    657 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    658 				  HAYESP_HIBYTE(HAYESP_RXHIWMARK));
    659 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    660 				  HAYESP_LOBYTE(HAYESP_RXHIWMARK));
    661 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    662 				  HAYESP_HIBYTE(HAYESP_RXLOWMARK));
    663 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    664 				  HAYESP_LOBYTE(HAYESP_RXLOWMARK));
    665 	}
    666 #endif
    667 
    668 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE|COM_HW_KGDB))
    669 		com_enable_debugport(sc);
    670 }
    671 
    672 int
    673 com_detach(struct device *self, int flags)
    674 {
    675 	struct com_softc *sc = (struct com_softc *)self;
    676 	int maj, mn;
    677 
    678 	/* locate the major number */
    679 	maj = cdevsw_lookup_major(&com_cdevsw);
    680 
    681 	/* Nuke the vnodes for any open instances. */
    682 	mn = self->dv_unit;
    683 	vdevgone(maj, mn, mn, VCHR);
    684 
    685 	mn |= COMDIALOUT_MASK;
    686 	vdevgone(maj, mn, mn, VCHR);
    687 
    688 	if (sc->sc_rbuf == NULL) {
    689 		/*
    690 		 * Ring buffer allocation failed in the com_attach_subr,
    691 		 * only the tty is allocated, and nothing else.
    692 		 */
    693 		ttyfree(sc->sc_tty);
    694 		return 0;
    695 	}
    696 
    697 	/* Free the receive buffer. */
    698 	free(sc->sc_rbuf, M_DEVBUF);
    699 
    700 	/* Detach and free the tty. */
    701 	tty_detach(sc->sc_tty);
    702 	ttyfree(sc->sc_tty);
    703 
    704 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    705 	/* Unhook the soft interrupt handler. */
    706 	softintr_disestablish(sc->sc_si);
    707 #endif
    708 
    709 #if NRND > 0 && defined(RND_COM)
    710 	/* Unhook the entropy source. */
    711 	rnd_detach_source(&sc->rnd_source);
    712 #endif
    713 
    714 	return (0);
    715 }
    716 
    717 int
    718 com_activate(struct device *self, enum devact act)
    719 {
    720 	struct com_softc *sc = (struct com_softc *)self;
    721 	int s, rv = 0;
    722 
    723 	s = splserial();
    724 	COM_LOCK(sc);
    725 	switch (act) {
    726 	case DVACT_ACTIVATE:
    727 		rv = EOPNOTSUPP;
    728 		break;
    729 
    730 	case DVACT_DEACTIVATE:
    731 		if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
    732 			rv = EBUSY;
    733 			break;
    734 		}
    735 
    736 		if (sc->disable != NULL && sc->enabled != 0) {
    737 			(*sc->disable)(sc);
    738 			sc->enabled = 0;
    739 		}
    740 		break;
    741 	}
    742 
    743 	COM_UNLOCK(sc);
    744 	splx(s);
    745 	return (rv);
    746 }
    747 
    748 void
    749 com_shutdown(struct com_softc *sc)
    750 {
    751 	struct tty *tp = sc->sc_tty;
    752 	int s;
    753 
    754 	s = splserial();
    755 	COM_LOCK(sc);
    756 
    757 	/* If we were asserting flow control, then deassert it. */
    758 	SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
    759 	com_hwiflow(sc);
    760 
    761 	/* Clear any break condition set with TIOCSBRK. */
    762 	com_break(sc, 0);
    763 
    764 	/* Turn off PPS capture on last close. */
    765 	sc->sc_ppsmask = 0;
    766 	sc->ppsparam.mode = 0;
    767 
    768 	/*
    769 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    770 	 * notice even if we immediately open the port again.
    771 	 * Avoid tsleeping above splhigh().
    772 	 */
    773 	if (ISSET(tp->t_cflag, HUPCL)) {
    774 		com_modem(sc, 0);
    775 		COM_UNLOCK(sc);
    776 		splx(s);
    777 		/* XXX tsleep will only timeout */
    778 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    779 		s = splserial();
    780 		COM_LOCK(sc);
    781 	}
    782 
    783 	/* Turn off interrupts. */
    784 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    785 		sc->sc_ier = IER_ERXRDY; /* interrupt on break */
    786 #ifdef COM_PXA2X0
    787 		if (sc->sc_type == COM_TYPE_PXA2x0)
    788 			sc->sc_ier |= IER_ERXTOUT;
    789 #endif
    790 	} else
    791 		sc->sc_ier = 0;
    792 
    793 #ifdef COM_PXA2X0
    794 	if (sc->sc_type == COM_TYPE_PXA2x0)
    795 		sc->sc_ier |= IER_EUART;
    796 #endif
    797 
    798 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
    799 
    800 	if (sc->disable) {
    801 #ifdef DIAGNOSTIC
    802 		if (!sc->enabled)
    803 			panic("com_shutdown: not enabled?");
    804 #endif
    805 		(*sc->disable)(sc);
    806 		sc->enabled = 0;
    807 	}
    808 	COM_UNLOCK(sc);
    809 	splx(s);
    810 }
    811 
    812 int
    813 comopen(dev_t dev, int flag, int mode, struct proc *p)
    814 {
    815 	struct com_softc *sc;
    816 	struct tty *tp;
    817 	int s, s2;
    818 	int error;
    819 
    820 	sc = device_lookup(&com_cd, COMUNIT(dev));
    821 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    822 		sc->sc_rbuf == NULL)
    823 		return (ENXIO);
    824 
    825 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    826 		return (ENXIO);
    827 
    828 #ifdef KGDB
    829 	/*
    830 	 * If this is the kgdb port, no other use is permitted.
    831 	 */
    832 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    833 		return (EBUSY);
    834 #endif
    835 
    836 	tp = sc->sc_tty;
    837 
    838 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    839 	    ISSET(tp->t_state, TS_XCLUDE) &&
    840 		p->p_ucred->cr_uid != 0)
    841 		return (EBUSY);
    842 
    843 	s = spltty();
    844 
    845 	/*
    846 	 * Do the following iff this is a first open.
    847 	 */
    848 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    849 		struct termios t;
    850 
    851 		tp->t_dev = dev;
    852 
    853 		s2 = splserial();
    854 		COM_LOCK(sc);
    855 
    856 		if (sc->enable) {
    857 			if ((*sc->enable)(sc)) {
    858 				COM_UNLOCK(sc);
    859 				splx(s2);
    860 				splx(s);
    861 				printf("%s: device enable failed\n",
    862 				       sc->sc_dev.dv_xname);
    863 				return (EIO);
    864 			}
    865 			sc->enabled = 1;
    866 			com_config(sc);
    867 		}
    868 
    869 		/* Turn on interrupts. */
    870 		sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
    871 #ifdef COM_PXA2X0
    872 		if (sc->sc_type == COM_TYPE_PXA2x0)
    873 			sc->sc_ier |= IER_EUART | IER_ERXTOUT;
    874 #endif
    875 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
    876 
    877 		/* Fetch the current modem control status, needed later. */
    878 		sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
    879 
    880 		/* Clear PPS capture state on first open. */
    881 		sc->sc_ppsmask = 0;
    882 		sc->ppsparam.mode = 0;
    883 
    884 		COM_UNLOCK(sc);
    885 		splx(s2);
    886 
    887 		/*
    888 		 * Initialize the termios status to the defaults.  Add in the
    889 		 * sticky bits from TIOCSFLAGS.
    890 		 */
    891 		t.c_ispeed = 0;
    892 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    893 			t.c_ospeed = comconsrate;
    894 			t.c_cflag = comconscflag;
    895 		} else {
    896 			t.c_ospeed = TTYDEF_SPEED;
    897 			t.c_cflag = TTYDEF_CFLAG;
    898 		}
    899 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    900 			SET(t.c_cflag, CLOCAL);
    901 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    902 			SET(t.c_cflag, CRTSCTS);
    903 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    904 			SET(t.c_cflag, MDMBUF);
    905 		/* Make sure comparam() will do something. */
    906 		tp->t_ospeed = 0;
    907 		(void) comparam(tp, &t);
    908 		tp->t_iflag = TTYDEF_IFLAG;
    909 		tp->t_oflag = TTYDEF_OFLAG;
    910 		tp->t_lflag = TTYDEF_LFLAG;
    911 		ttychars(tp);
    912 		ttsetwater(tp);
    913 
    914 		s2 = splserial();
    915 		COM_LOCK(sc);
    916 
    917 		/*
    918 		 * Turn on DTR.  We must always do this, even if carrier is not
    919 		 * present, because otherwise we'd have to use TIOCSDTR
    920 		 * immediately after setting CLOCAL, which applications do not
    921 		 * expect.  We always assert DTR while the device is open
    922 		 * unless explicitly requested to deassert it.
    923 		 */
    924 		com_modem(sc, 1);
    925 
    926 		/* Clear the input ring, and unblock. */
    927 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    928 		sc->sc_rbavail = com_rbuf_size;
    929 		com_iflush(sc);
    930 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    931 		com_hwiflow(sc);
    932 
    933 #ifdef COM_DEBUG
    934 		if (com_debug)
    935 			comstatus(sc, "comopen  ");
    936 #endif
    937 
    938 		COM_UNLOCK(sc);
    939 		splx(s2);
    940 	}
    941 
    942 	splx(s);
    943 
    944 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    945 	if (error)
    946 		goto bad;
    947 
    948 	error = (*tp->t_linesw->l_open)(dev, tp);
    949 	if (error)
    950 		goto bad;
    951 
    952 	return (0);
    953 
    954 bad:
    955 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    956 		/*
    957 		 * We failed to open the device, and nobody else had it opened.
    958 		 * Clean up the state as appropriate.
    959 		 */
    960 		com_shutdown(sc);
    961 	}
    962 
    963 	return (error);
    964 }
    965 
    966 int
    967 comclose(dev_t dev, int flag, int mode, struct proc *p)
    968 {
    969 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
    970 	struct tty *tp = sc->sc_tty;
    971 
    972 	/* XXX This is for cons.c. */
    973 	if (!ISSET(tp->t_state, TS_ISOPEN))
    974 		return (0);
    975 
    976 	(*tp->t_linesw->l_close)(tp, flag);
    977 	ttyclose(tp);
    978 
    979 	if (COM_ISALIVE(sc) == 0)
    980 		return (0);
    981 
    982 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    983 		/*
    984 		 * Although we got a last close, the device may still be in
    985 		 * use; e.g. if this was the dialout node, and there are still
    986 		 * processes waiting for carrier on the non-dialout node.
    987 		 */
    988 		com_shutdown(sc);
    989 	}
    990 
    991 	return (0);
    992 }
    993 
    994 int
    995 comread(dev_t dev, struct uio *uio, int flag)
    996 {
    997 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
    998 	struct tty *tp = sc->sc_tty;
    999 
   1000 	if (COM_ISALIVE(sc) == 0)
   1001 		return (EIO);
   1002 
   1003 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
   1004 }
   1005 
   1006 int
   1007 comwrite(dev_t dev, struct uio *uio, int flag)
   1008 {
   1009 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
   1010 	struct tty *tp = sc->sc_tty;
   1011 
   1012 	if (COM_ISALIVE(sc) == 0)
   1013 		return (EIO);
   1014 
   1015 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
   1016 }
   1017 
   1018 int
   1019 compoll(dev_t dev, int events, struct proc *p)
   1020 {
   1021 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
   1022 	struct tty *tp = sc->sc_tty;
   1023 
   1024 	if (COM_ISALIVE(sc) == 0)
   1025 		return (EIO);
   1026 
   1027 	return ((*tp->t_linesw->l_poll)(tp, events, p));
   1028 }
   1029 
   1030 struct tty *
   1031 comtty(dev_t dev)
   1032 {
   1033 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
   1034 	struct tty *tp = sc->sc_tty;
   1035 
   1036 	return (tp);
   1037 }
   1038 
   1039 int
   1040 comioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
   1041 {
   1042 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
   1043 	struct tty *tp = sc->sc_tty;
   1044 	int error;
   1045 	int s;
   1046 
   1047 	if (COM_ISALIVE(sc) == 0)
   1048 		return (EIO);
   1049 
   1050 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
   1051 	if (error != EPASSTHROUGH)
   1052 		return (error);
   1053 
   1054 	error = ttioctl(tp, cmd, data, flag, p);
   1055 	if (error != EPASSTHROUGH)
   1056 		return (error);
   1057 
   1058 	error = 0;
   1059 
   1060 	s = splserial();
   1061 	COM_LOCK(sc);
   1062 
   1063 	switch (cmd) {
   1064 	case TIOCSBRK:
   1065 		com_break(sc, 1);
   1066 		break;
   1067 
   1068 	case TIOCCBRK:
   1069 		com_break(sc, 0);
   1070 		break;
   1071 
   1072 	case TIOCSDTR:
   1073 		com_modem(sc, 1);
   1074 		break;
   1075 
   1076 	case TIOCCDTR:
   1077 		com_modem(sc, 0);
   1078 		break;
   1079 
   1080 	case TIOCGFLAGS:
   1081 		*(int *)data = sc->sc_swflags;
   1082 		break;
   1083 
   1084 	case TIOCSFLAGS:
   1085 		error = suser(p->p_ucred, &p->p_acflag);
   1086 		if (error)
   1087 			break;
   1088 		sc->sc_swflags = *(int *)data;
   1089 		break;
   1090 
   1091 	case TIOCMSET:
   1092 	case TIOCMBIS:
   1093 	case TIOCMBIC:
   1094 		tiocm_to_com(sc, cmd, *(int *)data);
   1095 		break;
   1096 
   1097 	case TIOCMGET:
   1098 		*(int *)data = com_to_tiocm(sc);
   1099 		break;
   1100 
   1101 	case PPS_IOC_CREATE:
   1102 		break;
   1103 
   1104 	case PPS_IOC_DESTROY:
   1105 		break;
   1106 
   1107 	case PPS_IOC_GETPARAMS: {
   1108 		pps_params_t *pp;
   1109 		pp = (pps_params_t *)data;
   1110 		*pp = sc->ppsparam;
   1111 		break;
   1112 	}
   1113 
   1114 	case PPS_IOC_SETPARAMS: {
   1115 	  	pps_params_t *pp;
   1116 		int mode;
   1117 		pp = (pps_params_t *)data;
   1118 		if (pp->mode & ~ppscap) {
   1119 			error = EINVAL;
   1120 			break;
   1121 		}
   1122 		sc->ppsparam = *pp;
   1123 	 	/*
   1124 		 * Compute msr masks from user-specified timestamp state.
   1125 		 */
   1126 		mode = sc->ppsparam.mode;
   1127 		switch (mode & PPS_CAPTUREBOTH) {
   1128 		case 0:
   1129 			sc->sc_ppsmask = 0;
   1130 			break;
   1131 
   1132 		case PPS_CAPTUREASSERT:
   1133 			sc->sc_ppsmask = MSR_DCD;
   1134 			sc->sc_ppsassert = MSR_DCD;
   1135 			sc->sc_ppsclear = -1;
   1136 			break;
   1137 
   1138 		case PPS_CAPTURECLEAR:
   1139 			sc->sc_ppsmask = MSR_DCD;
   1140 			sc->sc_ppsassert = -1;
   1141 			sc->sc_ppsclear = 0;
   1142 			break;
   1143 
   1144 		case PPS_CAPTUREBOTH:
   1145 			sc->sc_ppsmask = MSR_DCD;
   1146 			sc->sc_ppsassert = MSR_DCD;
   1147 			sc->sc_ppsclear = 0;
   1148 			break;
   1149 
   1150 		default:
   1151 			error = EINVAL;
   1152 			break;
   1153 		}
   1154 		break;
   1155 	}
   1156 
   1157 	case PPS_IOC_GETCAP:
   1158 		*(int*)data = ppscap;
   1159 		break;
   1160 
   1161 	case PPS_IOC_FETCH: {
   1162 		pps_info_t *pi;
   1163 		pi = (pps_info_t *)data;
   1164 		*pi = sc->ppsinfo;
   1165 		break;
   1166 	}
   1167 
   1168 #ifdef PPS_SYNC
   1169 	case PPS_IOC_KCBIND: {
   1170 		int edge = (*(int *)data) & PPS_CAPTUREBOTH;
   1171 
   1172 		if (edge == 0) {
   1173 			/*
   1174 			 * remove binding for this source; ignore
   1175 			 * the request if this is not the current
   1176 			 * hardpps source
   1177 			 */
   1178 			if (pps_kc_hardpps_source == sc) {
   1179 				pps_kc_hardpps_source = NULL;
   1180 				pps_kc_hardpps_mode = 0;
   1181 			}
   1182 		} else {
   1183 			/*
   1184 			 * bind hardpps to this source, replacing any
   1185 			 * previously specified source or edges
   1186 			 */
   1187 			pps_kc_hardpps_source = sc;
   1188 			pps_kc_hardpps_mode = edge;
   1189 		}
   1190 		break;
   1191 	}
   1192 #endif /* PPS_SYNC */
   1193 
   1194 	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
   1195 		/*
   1196 		 * Some GPS clocks models use the falling rather than
   1197 		 * rising edge as the on-the-second signal.
   1198 		 * The old API has no way to specify PPS polarity.
   1199 		 */
   1200 		sc->sc_ppsmask = MSR_DCD;
   1201 #ifndef PPS_TRAILING_EDGE
   1202 		sc->sc_ppsassert = MSR_DCD;
   1203 		sc->sc_ppsclear = -1;
   1204 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1205 		    &sc->ppsinfo.assert_timestamp);
   1206 #else
   1207 		sc->sc_ppsassert = -1;
   1208 		sc->sc_ppsclear = 0;
   1209 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1210 		    &sc->ppsinfo.clear_timestamp);
   1211 #endif
   1212 		break;
   1213 
   1214 	default:
   1215 		error = EPASSTHROUGH;
   1216 		break;
   1217 	}
   1218 
   1219 	COM_UNLOCK(sc);
   1220 	splx(s);
   1221 
   1222 #ifdef COM_DEBUG
   1223 	if (com_debug)
   1224 		comstatus(sc, "comioctl ");
   1225 #endif
   1226 
   1227 	return (error);
   1228 }
   1229 
   1230 integrate void
   1231 com_schedrx(struct com_softc *sc)
   1232 {
   1233 
   1234 	sc->sc_rx_ready = 1;
   1235 
   1236 	/* Wake up the poller. */
   1237 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1238 	softintr_schedule(sc->sc_si);
   1239 #else
   1240 #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1241 	setsoftserial();
   1242 #else
   1243 	if (!com_softintr_scheduled) {
   1244 		com_softintr_scheduled = 1;
   1245 		callout_reset(&comsoft_callout, 1, comsoft, NULL);
   1246 	}
   1247 #endif
   1248 #endif
   1249 }
   1250 
   1251 void
   1252 com_break(struct com_softc *sc, int onoff)
   1253 {
   1254 
   1255 	if (onoff)
   1256 		SET(sc->sc_lcr, LCR_SBREAK);
   1257 	else
   1258 		CLR(sc->sc_lcr, LCR_SBREAK);
   1259 
   1260 	if (!sc->sc_heldchange) {
   1261 		if (sc->sc_tx_busy) {
   1262 			sc->sc_heldtbc = sc->sc_tbc;
   1263 			sc->sc_tbc = 0;
   1264 			sc->sc_heldchange = 1;
   1265 		} else
   1266 			com_loadchannelregs(sc);
   1267 	}
   1268 }
   1269 
   1270 void
   1271 com_modem(struct com_softc *sc, int onoff)
   1272 {
   1273 
   1274 	if (sc->sc_mcr_dtr == 0)
   1275 		return;
   1276 
   1277 	if (onoff)
   1278 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1279 	else
   1280 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1281 
   1282 	if (!sc->sc_heldchange) {
   1283 		if (sc->sc_tx_busy) {
   1284 			sc->sc_heldtbc = sc->sc_tbc;
   1285 			sc->sc_tbc = 0;
   1286 			sc->sc_heldchange = 1;
   1287 		} else
   1288 			com_loadchannelregs(sc);
   1289 	}
   1290 }
   1291 
   1292 void
   1293 tiocm_to_com(struct com_softc *sc, u_long how, int ttybits)
   1294 {
   1295 	u_char combits;
   1296 
   1297 	combits = 0;
   1298 	if (ISSET(ttybits, TIOCM_DTR))
   1299 		SET(combits, MCR_DTR);
   1300 	if (ISSET(ttybits, TIOCM_RTS))
   1301 		SET(combits, MCR_RTS);
   1302 
   1303 	switch (how) {
   1304 	case TIOCMBIC:
   1305 		CLR(sc->sc_mcr, combits);
   1306 		break;
   1307 
   1308 	case TIOCMBIS:
   1309 		SET(sc->sc_mcr, combits);
   1310 		break;
   1311 
   1312 	case TIOCMSET:
   1313 		CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
   1314 		SET(sc->sc_mcr, combits);
   1315 		break;
   1316 	}
   1317 
   1318 	if (!sc->sc_heldchange) {
   1319 		if (sc->sc_tx_busy) {
   1320 			sc->sc_heldtbc = sc->sc_tbc;
   1321 			sc->sc_tbc = 0;
   1322 			sc->sc_heldchange = 1;
   1323 		} else
   1324 			com_loadchannelregs(sc);
   1325 	}
   1326 }
   1327 
   1328 int
   1329 com_to_tiocm(struct com_softc *sc)
   1330 {
   1331 	u_char combits;
   1332 	int ttybits = 0;
   1333 
   1334 	combits = sc->sc_mcr;
   1335 	if (ISSET(combits, MCR_DTR))
   1336 		SET(ttybits, TIOCM_DTR);
   1337 	if (ISSET(combits, MCR_RTS))
   1338 		SET(ttybits, TIOCM_RTS);
   1339 
   1340 	combits = sc->sc_msr;
   1341 	if (ISSET(combits, MSR_DCD))
   1342 		SET(ttybits, TIOCM_CD);
   1343 	if (ISSET(combits, MSR_CTS))
   1344 		SET(ttybits, TIOCM_CTS);
   1345 	if (ISSET(combits, MSR_DSR))
   1346 		SET(ttybits, TIOCM_DSR);
   1347 	if (ISSET(combits, MSR_RI | MSR_TERI))
   1348 		SET(ttybits, TIOCM_RI);
   1349 
   1350 	if (ISSET(sc->sc_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC))
   1351 		SET(ttybits, TIOCM_LE);
   1352 
   1353 	return (ttybits);
   1354 }
   1355 
   1356 static u_char
   1357 cflag2lcr(tcflag_t cflag)
   1358 {
   1359 	u_char lcr = 0;
   1360 
   1361 	switch (ISSET(cflag, CSIZE)) {
   1362 	case CS5:
   1363 		SET(lcr, LCR_5BITS);
   1364 		break;
   1365 	case CS6:
   1366 		SET(lcr, LCR_6BITS);
   1367 		break;
   1368 	case CS7:
   1369 		SET(lcr, LCR_7BITS);
   1370 		break;
   1371 	case CS8:
   1372 		SET(lcr, LCR_8BITS);
   1373 		break;
   1374 	}
   1375 	if (ISSET(cflag, PARENB)) {
   1376 		SET(lcr, LCR_PENAB);
   1377 		if (!ISSET(cflag, PARODD))
   1378 			SET(lcr, LCR_PEVEN);
   1379 	}
   1380 	if (ISSET(cflag, CSTOPB))
   1381 		SET(lcr, LCR_STOPB);
   1382 
   1383 	return (lcr);
   1384 }
   1385 
   1386 int
   1387 comparam(struct tty *tp, struct termios *t)
   1388 {
   1389 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
   1390 	int ospeed;
   1391 	u_char lcr;
   1392 	int s;
   1393 
   1394 	if (COM_ISALIVE(sc) == 0)
   1395 		return (EIO);
   1396 
   1397 #ifdef COM_HAYESP
   1398 	if (sc->sc_type == COM_TYPE_HAYESP) {
   1399 		int prescaler, speed;
   1400 
   1401 		/*
   1402 		 * Calculate UART clock prescaler.  It should be in
   1403 		 * range of 0 .. 3.
   1404 		 */
   1405 		for (prescaler = 0, speed = t->c_ospeed; prescaler < 4;
   1406 		    prescaler++, speed /= 2)
   1407 			if ((ospeed = comspeed(speed, sc->sc_frequency,
   1408 					       sc->sc_type)) > 0)
   1409 				break;
   1410 
   1411 		if (prescaler == 4)
   1412 			return (EINVAL);
   1413 		sc->sc_prescaler = prescaler;
   1414 	} else
   1415 #endif
   1416 	ospeed = comspeed(t->c_ospeed, sc->sc_frequency, sc->sc_type);
   1417 
   1418 	/* Check requested parameters. */
   1419 	if (ospeed < 0)
   1420 		return (EINVAL);
   1421 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1422 		return (EINVAL);
   1423 
   1424 	/*
   1425 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1426 	 * is always active.
   1427 	 */
   1428 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
   1429 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1430 		SET(t->c_cflag, CLOCAL);
   1431 		CLR(t->c_cflag, HUPCL);
   1432 	}
   1433 
   1434 	/*
   1435 	 * If there were no changes, don't do anything.  This avoids dropping
   1436 	 * input and improves performance when all we did was frob things like
   1437 	 * VMIN and VTIME.
   1438 	 */
   1439 	if (tp->t_ospeed == t->c_ospeed &&
   1440 	    tp->t_cflag == t->c_cflag)
   1441 		return (0);
   1442 
   1443 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
   1444 
   1445 	s = splserial();
   1446 	COM_LOCK(sc);
   1447 
   1448 	sc->sc_lcr = lcr;
   1449 
   1450 	/*
   1451 	 * If we're not in a mode that assumes a connection is present, then
   1452 	 * ignore carrier changes.
   1453 	 */
   1454 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1455 		sc->sc_msr_dcd = 0;
   1456 	else
   1457 		sc->sc_msr_dcd = MSR_DCD;
   1458 	/*
   1459 	 * Set the flow control pins depending on the current flow control
   1460 	 * mode.
   1461 	 */
   1462 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1463 		sc->sc_mcr_dtr = MCR_DTR;
   1464 		sc->sc_mcr_rts = MCR_RTS;
   1465 		sc->sc_msr_cts = MSR_CTS;
   1466 		sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
   1467 	} else if (ISSET(t->c_cflag, MDMBUF)) {
   1468 		/*
   1469 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1470 		 * carrier detection.
   1471 		 */
   1472 		sc->sc_mcr_dtr = 0;
   1473 		sc->sc_mcr_rts = MCR_DTR;
   1474 		sc->sc_msr_cts = MSR_DCD;
   1475 		sc->sc_efr = 0;
   1476 	} else {
   1477 		/*
   1478 		 * If no flow control, then always set RTS.  This will make
   1479 		 * the other side happy if it mistakenly thinks we're doing
   1480 		 * RTS/CTS flow control.
   1481 		 */
   1482 		sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
   1483 		sc->sc_mcr_rts = 0;
   1484 		sc->sc_msr_cts = 0;
   1485 		sc->sc_efr = 0;
   1486 		if (ISSET(sc->sc_mcr, MCR_DTR))
   1487 			SET(sc->sc_mcr, MCR_RTS);
   1488 		else
   1489 			CLR(sc->sc_mcr, MCR_RTS);
   1490 	}
   1491 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
   1492 
   1493 #if 0
   1494 	if (ospeed == 0)
   1495 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1496 	else
   1497 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1498 #endif
   1499 
   1500 	sc->sc_dlbl = ospeed;
   1501 	sc->sc_dlbh = ospeed >> 8;
   1502 
   1503 	/*
   1504 	 * Set the FIFO threshold based on the receive speed.
   1505 	 *
   1506 	 *  * If it's a low speed, it's probably a mouse or some other
   1507 	 *    interactive device, so set the threshold low.
   1508 	 *  * If it's a high speed, trim the trigger level down to prevent
   1509 	 *    overflows.
   1510 	 *  * Otherwise set it a bit higher.
   1511 	 */
   1512 	if (sc->sc_type == COM_TYPE_HAYESP)
   1513 		sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
   1514 	else if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
   1515 		sc->sc_fifo = FIFO_ENABLE |
   1516 		    (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8);
   1517 	else
   1518 		sc->sc_fifo = 0;
   1519 
   1520 	/* And copy to tty. */
   1521 	tp->t_ispeed = 0;
   1522 	tp->t_ospeed = t->c_ospeed;
   1523 	tp->t_cflag = t->c_cflag;
   1524 
   1525 	if (!sc->sc_heldchange) {
   1526 		if (sc->sc_tx_busy) {
   1527 			sc->sc_heldtbc = sc->sc_tbc;
   1528 			sc->sc_tbc = 0;
   1529 			sc->sc_heldchange = 1;
   1530 		} else
   1531 			com_loadchannelregs(sc);
   1532 	}
   1533 
   1534 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1535 		/* Disable the high water mark. */
   1536 		sc->sc_r_hiwat = 0;
   1537 		sc->sc_r_lowat = 0;
   1538 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1539 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1540 			com_schedrx(sc);
   1541 		}
   1542 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1543 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1544 			com_hwiflow(sc);
   1545 		}
   1546 	} else {
   1547 		sc->sc_r_hiwat = com_rbuf_hiwat;
   1548 		sc->sc_r_lowat = com_rbuf_lowat;
   1549 	}
   1550 
   1551 	COM_UNLOCK(sc);
   1552 	splx(s);
   1553 
   1554 	/*
   1555 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1556 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1557 	 * explicit request.
   1558 	 */
   1559 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
   1560 
   1561 #ifdef COM_DEBUG
   1562 	if (com_debug)
   1563 		comstatus(sc, "comparam ");
   1564 #endif
   1565 
   1566 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1567 		if (sc->sc_tx_stopped) {
   1568 			sc->sc_tx_stopped = 0;
   1569 			comstart(tp);
   1570 		}
   1571 	}
   1572 
   1573 	return (0);
   1574 }
   1575 
   1576 void
   1577 com_iflush(struct com_softc *sc)
   1578 {
   1579 	bus_space_tag_t iot = sc->sc_iot;
   1580 	bus_space_handle_t ioh = sc->sc_ioh;
   1581 #ifdef DIAGNOSTIC
   1582 	int reg;
   1583 #endif
   1584 	int timo;
   1585 
   1586 #ifdef DIAGNOSTIC
   1587 	reg = 0xffff;
   1588 #endif
   1589 	timo = 50000;
   1590 	/* flush any pending I/O */
   1591 	while (ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY)
   1592 	    && --timo)
   1593 #ifdef DIAGNOSTIC
   1594 		reg =
   1595 #else
   1596 		    (void)
   1597 #endif
   1598 		    bus_space_read_1(iot, ioh, com_data);
   1599 #ifdef DIAGNOSTIC
   1600 	if (!timo)
   1601 		printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
   1602 		       reg);
   1603 #endif
   1604 }
   1605 
   1606 void
   1607 com_loadchannelregs(struct com_softc *sc)
   1608 {
   1609 	bus_space_tag_t iot = sc->sc_iot;
   1610 	bus_space_handle_t ioh = sc->sc_ioh;
   1611 
   1612 	/* XXXXX necessary? */
   1613 	com_iflush(sc);
   1614 
   1615 #ifdef COM_PXA2X0
   1616 	if (sc->sc_type == COM_TYPE_PXA2x0)
   1617 		bus_space_write_1(iot, ioh, com_ier, IER_EUART);
   1618 	else
   1619 #endif
   1620 		bus_space_write_1(iot, ioh, com_ier, 0);
   1621 
   1622 	if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
   1623 		bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   1624 		bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
   1625 	}
   1626 	bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr | LCR_DLAB);
   1627 	bus_space_write_1(iot, ioh, com_dlbl, sc->sc_dlbl);
   1628 	bus_space_write_1(iot, ioh, com_dlbh, sc->sc_dlbh);
   1629 	bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
   1630 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active = sc->sc_mcr);
   1631 	bus_space_write_1(iot, ioh, com_fifo, sc->sc_fifo);
   1632 #ifdef COM_HAYESP
   1633 	if (sc->sc_type == COM_TYPE_HAYESP) {
   1634 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
   1635 		    HAYESP_SETPRESCALER);
   1636 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
   1637 		    sc->sc_prescaler);
   1638 	}
   1639 #endif
   1640 
   1641 	bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1642 }
   1643 
   1644 int
   1645 comhwiflow(struct tty *tp, int block)
   1646 {
   1647 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
   1648 	int s;
   1649 
   1650 	if (COM_ISALIVE(sc) == 0)
   1651 		return (0);
   1652 
   1653 	if (sc->sc_mcr_rts == 0)
   1654 		return (0);
   1655 
   1656 	s = splserial();
   1657 	COM_LOCK(sc);
   1658 
   1659 	if (block) {
   1660 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1661 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1662 			com_hwiflow(sc);
   1663 		}
   1664 	} else {
   1665 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1666 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1667 			com_schedrx(sc);
   1668 		}
   1669 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1670 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1671 			com_hwiflow(sc);
   1672 		}
   1673 	}
   1674 
   1675 	COM_UNLOCK(sc);
   1676 	splx(s);
   1677 	return (1);
   1678 }
   1679 
   1680 /*
   1681  * (un)block input via hw flowcontrol
   1682  */
   1683 void
   1684 com_hwiflow(struct com_softc *sc)
   1685 {
   1686 	bus_space_tag_t iot = sc->sc_iot;
   1687 	bus_space_handle_t ioh = sc->sc_ioh;
   1688 
   1689 	if (sc->sc_mcr_rts == 0)
   1690 		return;
   1691 
   1692 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
   1693 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
   1694 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
   1695 	} else {
   1696 		SET(sc->sc_mcr, sc->sc_mcr_rts);
   1697 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
   1698 	}
   1699 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
   1700 }
   1701 
   1702 
   1703 void
   1704 comstart(struct tty *tp)
   1705 {
   1706 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
   1707 	bus_space_tag_t iot = sc->sc_iot;
   1708 	bus_space_handle_t ioh = sc->sc_ioh;
   1709 	int s;
   1710 
   1711 	if (COM_ISALIVE(sc) == 0)
   1712 		return;
   1713 
   1714 	s = spltty();
   1715 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1716 		goto out;
   1717 	if (sc->sc_tx_stopped)
   1718 		goto out;
   1719 
   1720 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1721 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1722 			CLR(tp->t_state, TS_ASLEEP);
   1723 			wakeup(&tp->t_outq);
   1724 		}
   1725 		selwakeup(&tp->t_wsel);
   1726 		if (tp->t_outq.c_cc == 0)
   1727 			goto out;
   1728 	}
   1729 
   1730 	/* Grab the first contiguous region of buffer space. */
   1731 	{
   1732 		u_char *tba;
   1733 		int tbc;
   1734 
   1735 		tba = tp->t_outq.c_cf;
   1736 		tbc = ndqb(&tp->t_outq, 0);
   1737 
   1738 		(void)splserial();
   1739 		COM_LOCK(sc);
   1740 
   1741 		sc->sc_tba = tba;
   1742 		sc->sc_tbc = tbc;
   1743 	}
   1744 
   1745 	SET(tp->t_state, TS_BUSY);
   1746 	sc->sc_tx_busy = 1;
   1747 
   1748 	/* Enable transmit completion interrupts if necessary. */
   1749 	if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
   1750 		SET(sc->sc_ier, IER_ETXRDY);
   1751 		bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1752 	}
   1753 
   1754 #if 0
   1755 	/* Output the first chunk of the contiguous buffer. */
   1756 	if (!ISSET(sc->sc_hwflags, COM_HW_NO_TXPRELOAD)) {
   1757 		u_int n;
   1758 
   1759 		n = sc->sc_tbc;
   1760 		if (n > sc->sc_fifolen)
   1761 			n = sc->sc_fifolen;
   1762 		bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
   1763 		sc->sc_tbc -= n;
   1764 		sc->sc_tba += n;
   1765 	}
   1766 #endif
   1767 	COM_UNLOCK(sc);
   1768 out:
   1769 	splx(s);
   1770 	return;
   1771 }
   1772 
   1773 /*
   1774  * Stop output on a line.
   1775  */
   1776 void
   1777 comstop(struct tty *tp, int flag)
   1778 {
   1779 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
   1780 	int s;
   1781 
   1782 	s = splserial();
   1783 	COM_LOCK(sc);
   1784 	if (ISSET(tp->t_state, TS_BUSY)) {
   1785 		/* Stop transmitting at the next chunk. */
   1786 		sc->sc_tbc = 0;
   1787 		sc->sc_heldtbc = 0;
   1788 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1789 			SET(tp->t_state, TS_FLUSH);
   1790 	}
   1791 	COM_UNLOCK(sc);
   1792 	splx(s);
   1793 }
   1794 
   1795 void
   1796 comdiag(void *arg)
   1797 {
   1798 	struct com_softc *sc = arg;
   1799 	int overflows, floods;
   1800 	int s;
   1801 
   1802 	s = splserial();
   1803 	COM_LOCK(sc);
   1804 	overflows = sc->sc_overflows;
   1805 	sc->sc_overflows = 0;
   1806 	floods = sc->sc_floods;
   1807 	sc->sc_floods = 0;
   1808 	sc->sc_errors = 0;
   1809 	COM_UNLOCK(sc);
   1810 	splx(s);
   1811 
   1812 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1813 	    sc->sc_dev.dv_xname,
   1814 	    overflows, overflows == 1 ? "" : "s",
   1815 	    floods, floods == 1 ? "" : "s");
   1816 }
   1817 
   1818 integrate void
   1819 com_rxsoft(struct com_softc *sc, struct tty *tp)
   1820 {
   1821 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
   1822 	u_char *get, *end;
   1823 	u_int cc, scc;
   1824 	u_char lsr;
   1825 	int code;
   1826 	int s;
   1827 
   1828 	end = sc->sc_ebuf;
   1829 	get = sc->sc_rbget;
   1830 	scc = cc = com_rbuf_size - sc->sc_rbavail;
   1831 
   1832 	if (cc == com_rbuf_size) {
   1833 		sc->sc_floods++;
   1834 		if (sc->sc_errors++ == 0)
   1835 			callout_reset(&sc->sc_diag_callout, 60 * hz,
   1836 			    comdiag, sc);
   1837 	}
   1838 
   1839 	/* If not yet open, drop the entire buffer content here */
   1840 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
   1841 		get += cc << 1;
   1842 		if (get >= end)
   1843 			get -= com_rbuf_size << 1;
   1844 		cc = 0;
   1845 	}
   1846 	while (cc) {
   1847 		code = get[0];
   1848 		lsr = get[1];
   1849 		if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
   1850 			if (ISSET(lsr, LSR_OE)) {
   1851 				sc->sc_overflows++;
   1852 				if (sc->sc_errors++ == 0)
   1853 					callout_reset(&sc->sc_diag_callout,
   1854 					    60 * hz, comdiag, sc);
   1855 			}
   1856 			if (ISSET(lsr, LSR_BI | LSR_FE))
   1857 				SET(code, TTY_FE);
   1858 			if (ISSET(lsr, LSR_PE))
   1859 				SET(code, TTY_PE);
   1860 		}
   1861 		if ((*rint)(code, tp) == -1) {
   1862 			/*
   1863 			 * The line discipline's buffer is out of space.
   1864 			 */
   1865 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1866 				/*
   1867 				 * We're either not using flow control, or the
   1868 				 * line discipline didn't tell us to block for
   1869 				 * some reason.  Either way, we have no way to
   1870 				 * know when there's more space available, so
   1871 				 * just drop the rest of the data.
   1872 				 */
   1873 				get += cc << 1;
   1874 				if (get >= end)
   1875 					get -= com_rbuf_size << 1;
   1876 				cc = 0;
   1877 			} else {
   1878 				/*
   1879 				 * Don't schedule any more receive processing
   1880 				 * until the line discipline tells us there's
   1881 				 * space available (through comhwiflow()).
   1882 				 * Leave the rest of the data in the input
   1883 				 * buffer.
   1884 				 */
   1885 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1886 			}
   1887 			break;
   1888 		}
   1889 		get += 2;
   1890 		if (get >= end)
   1891 			get = sc->sc_rbuf;
   1892 		cc--;
   1893 	}
   1894 
   1895 	if (cc != scc) {
   1896 		sc->sc_rbget = get;
   1897 		s = splserial();
   1898 		COM_LOCK(sc);
   1899 
   1900 		cc = sc->sc_rbavail += scc - cc;
   1901 		/* Buffers should be ok again, release possible block. */
   1902 		if (cc >= sc->sc_r_lowat) {
   1903 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1904 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1905 				SET(sc->sc_ier, IER_ERXRDY);
   1906 #ifdef COM_PXA2X0
   1907 				if (sc->sc_type == COM_TYPE_PXA2x0)
   1908 					SET(sc->sc_ier, IER_ERXTOUT);
   1909 #endif
   1910 				bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1911 				    com_ier, sc->sc_ier);
   1912 			}
   1913 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1914 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1915 				com_hwiflow(sc);
   1916 			}
   1917 		}
   1918 		COM_UNLOCK(sc);
   1919 		splx(s);
   1920 	}
   1921 }
   1922 
   1923 integrate void
   1924 com_txsoft(struct com_softc *sc, struct tty *tp)
   1925 {
   1926 
   1927 	CLR(tp->t_state, TS_BUSY);
   1928 	if (ISSET(tp->t_state, TS_FLUSH))
   1929 		CLR(tp->t_state, TS_FLUSH);
   1930 	else
   1931 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1932 	(*tp->t_linesw->l_start)(tp);
   1933 }
   1934 
   1935 integrate void
   1936 com_stsoft(struct com_softc *sc, struct tty *tp)
   1937 {
   1938 	u_char msr, delta;
   1939 	int s;
   1940 
   1941 	s = splserial();
   1942 	COM_LOCK(sc);
   1943 	msr = sc->sc_msr;
   1944 	delta = sc->sc_msr_delta;
   1945 	sc->sc_msr_delta = 0;
   1946 	COM_UNLOCK(sc);
   1947 	splx(s);
   1948 
   1949 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1950 		/*
   1951 		 * Inform the tty layer that carrier detect changed.
   1952 		 */
   1953 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
   1954 	}
   1955 
   1956 	if (ISSET(delta, sc->sc_msr_cts)) {
   1957 		/* Block or unblock output according to flow control. */
   1958 		if (ISSET(msr, sc->sc_msr_cts)) {
   1959 			sc->sc_tx_stopped = 0;
   1960 			(*tp->t_linesw->l_start)(tp);
   1961 		} else {
   1962 			sc->sc_tx_stopped = 1;
   1963 		}
   1964 	}
   1965 
   1966 #ifdef COM_DEBUG
   1967 	if (com_debug)
   1968 		comstatus(sc, "com_stsoft");
   1969 #endif
   1970 }
   1971 
   1972 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1973 void
   1974 comsoft(void *arg)
   1975 {
   1976 	struct com_softc *sc = arg;
   1977 	struct tty *tp;
   1978 
   1979 	if (COM_ISALIVE(sc) == 0)
   1980 		return;
   1981 
   1982 	{
   1983 #else
   1984 void
   1985 #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1986 comsoft(void)
   1987 #else
   1988 comsoft(void *arg)
   1989 #endif
   1990 {
   1991 	struct com_softc	*sc;
   1992 	struct tty	*tp;
   1993 	int	unit;
   1994 #ifdef __NO_SOFT_SERIAL_INTERRUPT
   1995 	int s;
   1996 
   1997 	s = splsoftserial();
   1998 	com_softintr_scheduled = 0;
   1999 #endif
   2000 
   2001 	for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
   2002 		sc = device_lookup(&com_cd, unit);
   2003 		if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
   2004 			continue;
   2005 
   2006 		if (COM_ISALIVE(sc) == 0)
   2007 			continue;
   2008 
   2009 		tp = sc->sc_tty;
   2010 		if (tp == NULL)
   2011 			continue;
   2012 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   2013 			continue;
   2014 #endif
   2015 		tp = sc->sc_tty;
   2016 
   2017 		if (sc->sc_rx_ready) {
   2018 			sc->sc_rx_ready = 0;
   2019 			com_rxsoft(sc, tp);
   2020 		}
   2021 
   2022 		if (sc->sc_st_check) {
   2023 			sc->sc_st_check = 0;
   2024 			com_stsoft(sc, tp);
   2025 		}
   2026 
   2027 		if (sc->sc_tx_done) {
   2028 			sc->sc_tx_done = 0;
   2029 			com_txsoft(sc, tp);
   2030 		}
   2031 	}
   2032 
   2033 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
   2034 #ifdef __NO_SOFT_SERIAL_INTERRUPT
   2035 	splx(s);
   2036 #endif
   2037 #endif
   2038 }
   2039 
   2040 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
   2041 	/* there has got to be a better way to do comsoft() */
   2042 }}
   2043 #endif
   2044 
   2045 int
   2046 comintr(void *arg)
   2047 {
   2048 	struct com_softc *sc = arg;
   2049 	bus_space_tag_t iot = sc->sc_iot;
   2050 	bus_space_handle_t ioh = sc->sc_ioh;
   2051 	u_char *put, *end;
   2052 	u_int cc;
   2053 	u_char lsr, iir;
   2054 
   2055 	if (COM_ISALIVE(sc) == 0)
   2056 		return (0);
   2057 
   2058 	COM_LOCK(sc);
   2059 	iir = bus_space_read_1(iot, ioh, com_iir);
   2060 	if (ISSET(iir, IIR_NOPEND)) {
   2061 		COM_UNLOCK(sc);
   2062 		return (0);
   2063 	}
   2064 
   2065 	end = sc->sc_ebuf;
   2066 	put = sc->sc_rbput;
   2067 	cc = sc->sc_rbavail;
   2068 
   2069 again:	do {
   2070 		u_char	msr, delta;
   2071 
   2072 		lsr = bus_space_read_1(iot, ioh, com_lsr);
   2073 		if (ISSET(lsr, LSR_BI)) {
   2074 			int cn_trapped = 0;
   2075 
   2076 			cn_check_magic(sc->sc_tty->t_dev,
   2077 				       CNC_BREAK, com_cnm_state);
   2078 			if (cn_trapped)
   2079 				continue;
   2080 #if defined(KGDB) && !defined(DDB)
   2081 			if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
   2082 				kgdb_connect(1);
   2083 				continue;
   2084 			}
   2085 #endif
   2086 		}
   2087 
   2088 		if (ISSET(lsr, LSR_RCV_MASK) &&
   2089 		    !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   2090 			while (cc > 0) {
   2091 				int cn_trapped = 0;
   2092 				put[0] = bus_space_read_1(iot, ioh, com_data);
   2093 				put[1] = lsr;
   2094 				cn_check_magic(sc->sc_tty->t_dev,
   2095 					       put[0], com_cnm_state);
   2096 				if (cn_trapped)
   2097 					goto next;
   2098 				put += 2;
   2099 				if (put >= end)
   2100 					put = sc->sc_rbuf;
   2101 				cc--;
   2102 			next:
   2103 				lsr = bus_space_read_1(iot, ioh, com_lsr);
   2104 				if (!ISSET(lsr, LSR_RCV_MASK))
   2105 					break;
   2106 			}
   2107 
   2108 			/*
   2109 			 * Current string of incoming characters ended because
   2110 			 * no more data was available or we ran out of space.
   2111 			 * Schedule a receive event if any data was received.
   2112 			 * If we're out of space, turn off receive interrupts.
   2113 			 */
   2114 			sc->sc_rbput = put;
   2115 			sc->sc_rbavail = cc;
   2116 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   2117 				sc->sc_rx_ready = 1;
   2118 
   2119 			/*
   2120 			 * See if we are in danger of overflowing a buffer. If
   2121 			 * so, use hardware flow control to ease the pressure.
   2122 			 */
   2123 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   2124 			    cc < sc->sc_r_hiwat) {
   2125 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   2126 				com_hwiflow(sc);
   2127 			}
   2128 
   2129 			/*
   2130 			 * If we're out of space, disable receive interrupts
   2131 			 * until the queue has drained a bit.
   2132 			 */
   2133 			if (!cc) {
   2134 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   2135 #ifdef COM_PXA2X0
   2136 				if (sc->sc_type == COM_TYPE_PXA2x0)
   2137 					CLR(sc->sc_ier, IER_ERXRDY|IER_ERXTOUT);
   2138 				else
   2139 #endif
   2140 					CLR(sc->sc_ier, IER_ERXRDY);
   2141 				bus_space_write_1(iot, ioh, com_ier,
   2142 				    sc->sc_ier);
   2143 			}
   2144 		} else {
   2145 			if ((iir & (IIR_RXRDY|IIR_TXRDY)) == IIR_RXRDY) {
   2146 				(void) bus_space_read_1(iot, ioh, com_data);
   2147 				continue;
   2148 			}
   2149 		}
   2150 
   2151 		msr = bus_space_read_1(iot, ioh, com_msr);
   2152 		delta = msr ^ sc->sc_msr;
   2153 		sc->sc_msr = msr;
   2154 		/*
   2155 		 * Pulse-per-second (PSS) signals on edge of DCD?
   2156 		 * Process these even if line discipline is ignoring DCD.
   2157 		 */
   2158 		if (delta & sc->sc_ppsmask) {
   2159 			struct timeval tv;
   2160 		    	if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
   2161 				/* XXX nanotime() */
   2162 				microtime(&tv);
   2163 				TIMEVAL_TO_TIMESPEC(&tv,
   2164 				    &sc->ppsinfo.assert_timestamp);
   2165 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
   2166 					timespecadd(&sc->ppsinfo.assert_timestamp,
   2167 					    &sc->ppsparam.assert_offset,
   2168 						    &sc->ppsinfo.assert_timestamp);
   2169 				}
   2170 
   2171 #ifdef PPS_SYNC
   2172 				if (pps_kc_hardpps_source == sc &&
   2173 				    pps_kc_hardpps_mode & PPS_CAPTUREASSERT) {
   2174 					hardpps(&tv, tv.tv_usec);
   2175 				}
   2176 #endif
   2177 				sc->ppsinfo.assert_sequence++;
   2178 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   2179 
   2180 			} else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
   2181 				/* XXX nanotime() */
   2182 				microtime(&tv);
   2183 				TIMEVAL_TO_TIMESPEC(&tv,
   2184 				    &sc->ppsinfo.clear_timestamp);
   2185 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
   2186 					timespecadd(&sc->ppsinfo.clear_timestamp,
   2187 					    &sc->ppsparam.clear_offset,
   2188 					    &sc->ppsinfo.clear_timestamp);
   2189 				}
   2190 
   2191 #ifdef PPS_SYNC
   2192 				if (pps_kc_hardpps_source == sc &&
   2193 				    pps_kc_hardpps_mode & PPS_CAPTURECLEAR) {
   2194 					hardpps(&tv, tv.tv_usec);
   2195 				}
   2196 #endif
   2197 				sc->ppsinfo.clear_sequence++;
   2198 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   2199 			}
   2200 		}
   2201 
   2202 		/*
   2203 		 * Process normal status changes
   2204 		 */
   2205 		if (ISSET(delta, sc->sc_msr_mask)) {
   2206 			SET(sc->sc_msr_delta, delta);
   2207 
   2208 			/*
   2209 			 * Stop output immediately if we lose the output
   2210 			 * flow control signal or carrier detect.
   2211 			 */
   2212 			if (ISSET(~msr, sc->sc_msr_mask)) {
   2213 				sc->sc_tbc = 0;
   2214 				sc->sc_heldtbc = 0;
   2215 #ifdef COM_DEBUG
   2216 				if (com_debug)
   2217 					comstatus(sc, "comintr  ");
   2218 #endif
   2219 			}
   2220 
   2221 			sc->sc_st_check = 1;
   2222 		}
   2223 	} while (!ISSET((iir =
   2224 	    bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND) &&
   2225 	    /*
   2226 	     * Since some device (e.g., ST16C1550) doesn't clear IIR_TXRDY
   2227 	     * by IIR read, so we can't do this way: `process all interrupts,
   2228 	     * then do TX if possble'.
   2229 	     */
   2230 	    (iir & IIR_IMASK) != IIR_TXRDY);
   2231 
   2232 	/*
   2233 	 * Read LSR again, since there may be an interrupt between
   2234 	 * the last LSR read and IIR read above.
   2235 	 */
   2236 	lsr = bus_space_read_1(iot, ioh, com_lsr);
   2237 
   2238 	/*
   2239 	 * See if data can be transmitted as well.
   2240 	 * Schedule tx done event if no data left
   2241 	 * and tty was marked busy.
   2242 	 */
   2243 	if (ISSET(lsr, LSR_TXRDY)) {
   2244 		/*
   2245 		 * If we've delayed a parameter change, do it now, and restart
   2246 		 * output.
   2247 		 */
   2248 		if (sc->sc_heldchange) {
   2249 			com_loadchannelregs(sc);
   2250 			sc->sc_heldchange = 0;
   2251 			sc->sc_tbc = sc->sc_heldtbc;
   2252 			sc->sc_heldtbc = 0;
   2253 		}
   2254 
   2255 		/* Output the next chunk of the contiguous buffer, if any. */
   2256 		if (sc->sc_tbc > 0) {
   2257 			u_int n;
   2258 
   2259 			n = sc->sc_tbc;
   2260 			if (n > sc->sc_fifolen)
   2261 				n = sc->sc_fifolen;
   2262 			bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
   2263 			sc->sc_tbc -= n;
   2264 			sc->sc_tba += n;
   2265 		} else {
   2266 			/* Disable transmit completion interrupts if necessary. */
   2267 			if (ISSET(sc->sc_ier, IER_ETXRDY)) {
   2268 				CLR(sc->sc_ier, IER_ETXRDY);
   2269 				bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   2270 			}
   2271 			if (sc->sc_tx_busy) {
   2272 				sc->sc_tx_busy = 0;
   2273 				sc->sc_tx_done = 1;
   2274 			}
   2275 		}
   2276 	}
   2277 
   2278 	if (!ISSET((iir = bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND))
   2279 		goto again;
   2280 
   2281 	COM_UNLOCK(sc);
   2282 
   2283 	/* Wake up the poller. */
   2284 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   2285 	softintr_schedule(sc->sc_si);
   2286 #else
   2287 #ifndef __NO_SOFT_SERIAL_INTERRUPT
   2288 	setsoftserial();
   2289 #else
   2290 	if (!com_softintr_scheduled) {
   2291 		com_softintr_scheduled = 1;
   2292 		callout_reset(&comsoft_callout, 1, comsoft, NULL);
   2293 	}
   2294 #endif
   2295 #endif
   2296 
   2297 #if NRND > 0 && defined(RND_COM)
   2298 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   2299 #endif
   2300 
   2301 	return (1);
   2302 }
   2303 
   2304 /*
   2305  * The following functions are polled getc and putc routines, shared
   2306  * by the console and kgdb glue.
   2307  *
   2308  * The read-ahead code is so that you can detect pending in-band
   2309  * cn_magic in polled mode while doing output rather than having to
   2310  * wait until the kernel decides it needs input.
   2311  */
   2312 
   2313 #define MAX_READAHEAD	20
   2314 static int com_readahead[MAX_READAHEAD];
   2315 static int com_readaheadcount = 0;
   2316 
   2317 int
   2318 com_common_getc(dev_t dev, bus_space_tag_t iot, bus_space_handle_t ioh)
   2319 {
   2320 	int s = splserial();
   2321 	u_char stat, c;
   2322 
   2323 	/* got a character from reading things earlier */
   2324 	if (com_readaheadcount > 0) {
   2325 		int i;
   2326 
   2327 		c = com_readahead[0];
   2328 		for (i = 1; i < com_readaheadcount; i++) {
   2329 			com_readahead[i-1] = com_readahead[i];
   2330 		}
   2331 		com_readaheadcount--;
   2332 		splx(s);
   2333 		return (c);
   2334 	}
   2335 
   2336 	/* block until a character becomes available */
   2337 	while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
   2338 		;
   2339 
   2340 	c = bus_space_read_1(iot, ioh, com_data);
   2341 	stat = bus_space_read_1(iot, ioh, com_iir);
   2342 	{
   2343 		int cn_trapped = 0; /* unused */
   2344 #ifdef DDB
   2345 		extern int db_active;
   2346 		if (!db_active)
   2347 #endif
   2348 			cn_check_magic(dev, c, com_cnm_state);
   2349 	}
   2350 	splx(s);
   2351 	return (c);
   2352 }
   2353 
   2354 void
   2355 com_common_putc(dev_t dev, bus_space_tag_t iot, bus_space_handle_t ioh, int c)
   2356 {
   2357 	int s = splserial();
   2358 	int cin, stat, timo;
   2359 
   2360 	if (com_readaheadcount < MAX_READAHEAD
   2361 	     && ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY)) {
   2362 		int cn_trapped = 0;
   2363 		cin = bus_space_read_1(iot, ioh, com_data);
   2364 		stat = bus_space_read_1(iot, ioh, com_iir);
   2365 		cn_check_magic(dev, cin, com_cnm_state);
   2366 		com_readahead[com_readaheadcount++] = cin;
   2367 	}
   2368 
   2369 	/* wait for any pending transmission to finish */
   2370 	timo = 150000;
   2371 	while (!ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY) && --timo)
   2372 		continue;
   2373 
   2374 	bus_space_write_1(iot, ioh, com_data, c);
   2375 	COM_BARRIER(iot, ioh, BR | BW);
   2376 
   2377 	splx(s);
   2378 }
   2379 
   2380 /*
   2381  * Initialize UART for use as console or KGDB line.
   2382  */
   2383 int
   2384 cominit(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
   2385     int type, tcflag_t cflag, bus_space_handle_t *iohp)
   2386 {
   2387 	bus_space_handle_t ioh;
   2388 
   2389 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
   2390 		return (ENOMEM); /* ??? */
   2391 
   2392 	bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   2393 	bus_space_write_1(iot, ioh, com_efr, 0);
   2394 	bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
   2395 	rate = comspeed(rate, frequency, type);
   2396 	bus_space_write_1(iot, ioh, com_dlbl, rate);
   2397 	bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
   2398 	bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
   2399 	bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS);
   2400 	bus_space_write_1(iot, ioh, com_fifo,
   2401 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
   2402 #ifdef COM_PXA2X0
   2403 	if (type == COM_TYPE_PXA2x0)
   2404 		bus_space_write_1(iot, ioh, com_ier, IER_EUART);
   2405 	else
   2406 #endif
   2407 		bus_space_write_1(iot, ioh, com_ier, 0);
   2408 
   2409 	*iohp = ioh;
   2410 	return (0);
   2411 }
   2412 
   2413 /*
   2414  * Following are all routines needed for COM to act as console
   2415  */
   2416 struct consdev comcons = {
   2417 	NULL, NULL, comcngetc, comcnputc, comcnpollc, NULL, NULL, NULL,
   2418 	NODEV, CN_NORMAL
   2419 };
   2420 
   2421 
   2422 int
   2423 comcnattach(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
   2424     int type, tcflag_t cflag)
   2425 {
   2426 	int res;
   2427 
   2428 	res = cominit(iot, iobase, rate, frequency, type, cflag, &comconsioh);
   2429 	if (res)
   2430 		return (res);
   2431 
   2432 	cn_tab = &comcons;
   2433 	cn_init_magic(&com_cnm_state);
   2434 	cn_set_magic("\047\001"); /* default magic is BREAK */
   2435 
   2436 	comconstag = iot;
   2437 	comconsaddr = iobase;
   2438 	comconsrate = rate;
   2439 	comconscflag = cflag;
   2440 
   2441 	return (0);
   2442 }
   2443 
   2444 int
   2445 comcngetc(dev_t dev)
   2446 {
   2447 
   2448 	return (com_common_getc(dev, comconstag, comconsioh));
   2449 }
   2450 
   2451 /*
   2452  * Console kernel output character routine.
   2453  */
   2454 void
   2455 comcnputc(dev_t dev, int c)
   2456 {
   2457 
   2458 	com_common_putc(dev, comconstag, comconsioh, c);
   2459 }
   2460 
   2461 void
   2462 comcnpollc(dev_t dev, int on)
   2463 {
   2464 
   2465 }
   2466 
   2467 #ifdef KGDB
   2468 int
   2469 com_kgdb_attach(bus_space_tag_t iot, bus_addr_t iobase, int rate,
   2470     int frequency, int type, tcflag_t cflag)
   2471 {
   2472 	int res;
   2473 
   2474 	if (iot == comconstag && iobase == comconsaddr) {
   2475 #if !defined(DDB)
   2476 		return (EBUSY); /* cannot share with console */
   2477 #else
   2478 		com_kgdb_ioh = comconsioh;
   2479 #endif
   2480 	} else {
   2481 		res = cominit(iot, iobase, rate, frequency, type, cflag,
   2482 			      &com_kgdb_ioh);
   2483 		if (res)
   2484 			return (res);
   2485 
   2486 		/*
   2487 		 * XXXfvdl this shouldn't be needed, but the cn_magic goo
   2488 		 * expects this to be initialized
   2489 		 */
   2490 		cn_init_magic(&com_cnm_state);
   2491 		cn_set_magic("\047\001");
   2492 	}
   2493 
   2494 	kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
   2495 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   2496 
   2497 	com_kgdb_iot = iot;
   2498 	com_kgdb_addr = iobase;
   2499 
   2500 	return (0);
   2501 }
   2502 
   2503 /* ARGSUSED */
   2504 int
   2505 com_kgdb_getc(void *arg)
   2506 {
   2507 
   2508 	return (com_common_getc(NODEV, com_kgdb_iot, com_kgdb_ioh));
   2509 }
   2510 
   2511 /* ARGSUSED */
   2512 void
   2513 com_kgdb_putc(void *arg, int c)
   2514 {
   2515 
   2516 	com_common_putc(NODEV, com_kgdb_iot, com_kgdb_ioh, c);
   2517 }
   2518 #endif /* KGDB */
   2519 
   2520 /* helper function to identify the com ports used by
   2521  console or KGDB (and not yet autoconf attached) */
   2522 int
   2523 com_is_console(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t *ioh)
   2524 {
   2525 	bus_space_handle_t help;
   2526 
   2527 	if (!comconsattached &&
   2528 	    iot == comconstag && iobase == comconsaddr)
   2529 		help = comconsioh;
   2530 #ifdef KGDB
   2531 	else if (!com_kgdb_attached &&
   2532 	    iot == com_kgdb_iot && iobase == com_kgdb_addr)
   2533 		help = com_kgdb_ioh;
   2534 #endif
   2535 	else
   2536 		return (0);
   2537 
   2538 	if (ioh)
   2539 		*ioh = help;
   2540 	return (1);
   2541 }
   2542