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