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