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