Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.130
      1 /*	$NetBSD: com.c,v 1.130 1998/01/12 09:23:19 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994, 1995, 1996, 1997
      5  *	Charles M. Hannum.  All rights reserved.
      6  *
      7  * Interrupt processing and hardware flow control partly based on code from
      8  * Onno van der Linden and Gordon Ross.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by Charles M. Hannum.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Copyright (c) 1991 The Regents of the University of California.
     38  * All rights reserved.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the University of
     51  *	California, Berkeley and its contributors.
     52  * 4. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  *
     68  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     69  */
     70 
     71 /*
     72  * COM driver, uses National Semiconductor NS16450/NS16550AF UART
     73  * Supports automatic hardware flow control on StarTech ST16C650A UART
     74  */
     75 
     76 #include "rnd.h"
     77 #if NRND > 0 && defined(RND_COM)
     78 #include <sys/rnd.h>
     79 #endif
     80 
     81 #include <sys/param.h>
     82 #include <sys/systm.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/select.h>
     85 #include <sys/tty.h>
     86 #include <sys/proc.h>
     87 #include <sys/user.h>
     88 #include <sys/conf.h>
     89 #include <sys/file.h>
     90 #include <sys/uio.h>
     91 #include <sys/kernel.h>
     92 #include <sys/syslog.h>
     93 #include <sys/types.h>
     94 #include <sys/device.h>
     95 #include <sys/malloc.h>
     96 
     97 #include <machine/intr.h>
     98 #include <machine/bus.h>
     99 
    100 #include <dev/ic/comreg.h>
    101 #include <dev/ic/comvar.h>
    102 #include <dev/ic/ns16550reg.h>
    103 #include <dev/ic/st16650reg.h>
    104 #ifdef COM_HAYESP
    105 #include <dev/ic/hayespreg.h>
    106 #endif
    107 #define	com_lcr	com_cfcr
    108 #include <dev/cons.h>
    109 
    110 #include "com.h"
    111 
    112 #ifdef COM_HAYESP
    113 int comprobeHAYESP __P((bus_space_handle_t hayespioh, struct com_softc *sc));
    114 #endif
    115 
    116 #if defined(DDB) || defined(KGDB)
    117 static void com_enable_debugport __P((struct com_softc *));
    118 #endif
    119 void	com_attach_subr	__P((struct com_softc *sc));
    120 int	comspeed	__P((long, long));
    121 static	u_char	cflag2lcr __P((tcflag_t));
    122 int	comparam	__P((struct tty *, struct termios *));
    123 void	comstart	__P((struct tty *));
    124 void	comstop		__P((struct tty *, int));
    125 int	comhwiflow	__P((struct tty *, int));
    126 
    127 void	com_loadchannelregs __P((struct com_softc *));
    128 void	com_hwiflow	__P((struct com_softc *));
    129 void	com_break	__P((struct com_softc *, int));
    130 void	com_modem	__P((struct com_softc *, int));
    131 void	com_iflush	__P((struct com_softc *));
    132 
    133 int	com_common_getc	__P((bus_space_tag_t, bus_space_handle_t));
    134 void	com_common_putc	__P((bus_space_tag_t, bus_space_handle_t, int));
    135 
    136 /* XXX: These belong elsewhere */
    137 cdev_decl(com);
    138 bdev_decl(com);
    139 
    140 int	comcngetc	__P((dev_t));
    141 void	comcnputc	__P((dev_t, int));
    142 void	comcnpollc	__P((dev_t, int));
    143 
    144 #define	integrate	static inline
    145 #ifdef __GENERIC_SOFT_INTERRUPTS
    146 void 	comsoft		__P((void *));
    147 #else
    148 #ifndef alpha
    149 void 	comsoft		__P((void));
    150 #else
    151 void 	comsoft		__P((void *));
    152 #endif
    153 #endif
    154 integrate void com_rxsoft	__P((struct com_softc *, struct tty *));
    155 integrate void com_txsoft	__P((struct com_softc *, struct tty *));
    156 integrate void com_stsoft	__P((struct com_softc *, struct tty *));
    157 integrate void com_schedrx	__P((struct com_softc *));
    158 void	comdiag		__P((void *));
    159 
    160 extern struct cfdriver com_cd;
    161 
    162 /*
    163  * Make this an option variable one can patch.
    164  * But be warned:  this must be a power of 2!
    165  */
    166 u_int com_rbuf_size = COM_RING_SIZE;
    167 
    168 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    169 u_int com_rbuf_hiwat = (COM_RING_SIZE * 1) / 4;
    170 u_int com_rbuf_lowat = (COM_RING_SIZE * 3) / 4;
    171 
    172 static int	comconsaddr;
    173 static bus_space_tag_t comconstag;
    174 static bus_space_handle_t comconsioh;
    175 static int	comconsattached;
    176 static int comconsrate;
    177 static tcflag_t comconscflag;
    178 
    179 static u_char tiocm_xxx2mcr __P((int));
    180 
    181 #ifndef __GENERIC_SOFT_INTERRUPTS
    182 #ifdef alpha
    183 volatile int	com_softintr_scheduled;
    184 #endif
    185 #endif
    186 
    187 #ifdef KGDB
    188 #include <sys/kgdb.h>
    189 
    190 static int com_kgdb_addr;
    191 static bus_space_tag_t com_kgdb_iot;
    192 static bus_space_handle_t com_kgdb_ioh;
    193 static int com_kgdb_attached;
    194 
    195 int	com_kgdb_getc __P((void *));
    196 void	com_kgdb_putc __P((void *, int));
    197 #endif /* KGDB */
    198 
    199 #define	COMUNIT(x)	(minor(x))
    200 
    201 int
    202 comspeed(speed, frequency)
    203 	long speed, frequency;
    204 {
    205 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    206 
    207 	int x, err;
    208 
    209 #if 0
    210 	if (speed == 0)
    211 		return (0);
    212 #endif
    213 	if (speed <= 0)
    214 		return (-1);
    215 	x = divrnd(frequency / 16, speed);
    216 	if (x <= 0)
    217 		return (-1);
    218 	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
    219 	if (err < 0)
    220 		err = -err;
    221 	if (err > COM_TOLERANCE)
    222 		return (-1);
    223 	return (x);
    224 
    225 #undef	divrnd(n, q)
    226 }
    227 
    228 #ifdef COM_DEBUG
    229 int	com_debug = 0;
    230 
    231 void comstatus __P((struct com_softc *, char *));
    232 void
    233 comstatus(sc, str)
    234 	struct com_softc *sc;
    235 	char *str;
    236 {
    237 	struct tty *tp = sc->sc_tty;
    238 
    239 	printf("%s: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    240 	    sc->sc_dev.dv_xname, str,
    241 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    242 	    ISSET(sc->sc_msr, MSR_DCD) ? "+" : "-",
    243 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    244 	    ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
    245 	    sc->sc_tx_stopped ? "+" : "-");
    246 
    247 	printf("%s: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
    248 	    sc->sc_dev.dv_xname, str,
    249 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    250 	    ISSET(sc->sc_msr, MSR_CTS) ? "+" : "-",
    251 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    252 	    ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
    253 	    sc->sc_rx_flags);
    254 }
    255 #endif
    256 
    257 int
    258 comprobe1(iot, ioh, iobase)
    259 	bus_space_tag_t iot;
    260 	bus_space_handle_t ioh;
    261 	int iobase;
    262 {
    263 
    264 	/* force access to id reg */
    265 	bus_space_write_1(iot, ioh, com_lcr, 0);
    266 	bus_space_write_1(iot, ioh, com_iir, 0);
    267 	if (bus_space_read_1(iot, ioh, com_iir) & 0x38)
    268 		return (0);
    269 
    270 	return (1);
    271 }
    272 
    273 #ifdef COM_HAYESP
    274 int
    275 comprobeHAYESP(hayespioh, sc)
    276 	bus_space_handle_t hayespioh;
    277 	struct com_softc *sc;
    278 {
    279 	char	val, dips;
    280 	int	combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
    281 	bus_space_tag_t iot = sc->sc_iot;
    282 
    283 	/*
    284 	 * Hayes ESP cards have two iobases.  One is for compatibility with
    285 	 * 16550 serial chips, and at the same ISA PC base addresses.  The
    286 	 * other is for ESP-specific enhanced features, and lies at a
    287 	 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
    288 	 */
    289 
    290 	/* Test for ESP signature */
    291 	if ((bus_space_read_1(iot, hayespioh, 0) & 0xf3) == 0)
    292 		return (0);
    293 
    294 	/*
    295 	 * ESP is present at ESP enhanced base address; unknown com port
    296 	 */
    297 
    298 	/* Get the dip-switch configurations */
    299 	bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
    300 	dips = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1);
    301 
    302 	/* Determine which com port this ESP card services: bits 0,1 of  */
    303 	/*  dips is the port # (0-3); combaselist[val] is the com_iobase */
    304 	if (sc->sc_iobase != combaselist[dips & 0x03])
    305 		return (0);
    306 
    307 	printf(": ESP");
    308 
    309  	/* Check ESP Self Test bits. */
    310 	/* Check for ESP version 2.0: bits 4,5,6 == 010 */
    311 	bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
    312 	val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1); /* Clear reg1 */
    313 	val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS2);
    314 	if ((val & 0x70) < 0x20) {
    315 		printf("-old (%o)", val & 0x70);
    316 		/* we do not support the necessary features */
    317 		return (0);
    318 	}
    319 
    320 	/* Check for ability to emulate 16550: bit 8 == 1 */
    321 	if ((dips & 0x80) == 0) {
    322 		printf(" slave");
    323 		/* XXX Does slave really mean no 16550 support?? */
    324 		return (0);
    325 	}
    326 
    327 	/*
    328 	 * If we made it this far, we are a full-featured ESP v2.0 (or
    329 	 * better), at the correct com port address.
    330 	 */
    331 
    332 	SET(sc->sc_hwflags, COM_HW_HAYESP);
    333 	printf(", 1024 byte fifo\n");
    334 	return (1);
    335 }
    336 #endif
    337 
    338 #if defined(DDB) || defined(KGDB)
    339 static void
    340 com_enable_debugport(sc)
    341 	struct com_softc *sc;
    342 {
    343 	int s;
    344 
    345 	/* Turn on line break interrupt, set carrier. */
    346 	s = splserial();
    347 	sc->sc_ier = IER_ERXRDY;
    348 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
    349 	SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
    350 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
    351 	splx(s);
    352 }
    353 #endif
    354 
    355 void
    356 com_attach_subr(sc)
    357 	struct com_softc *sc;
    358 {
    359 	int iobase = sc->sc_iobase;
    360 	bus_space_tag_t iot = sc->sc_iot;
    361 	bus_space_handle_t ioh = sc->sc_ioh;
    362 	struct tty *tp;
    363 #ifdef COM16650
    364 	u_int8_t lcr;
    365 #endif
    366 #ifdef COM_HAYESP
    367 	int	hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
    368 	int	*hayespp;
    369 #endif
    370 
    371 	/* Disable interrupts before configuring the device. */
    372 	sc->sc_ier = 0;
    373 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
    374 
    375 	if (iot == comconstag && iobase == comconsaddr) {
    376 		comconsattached = 1;
    377 
    378 		/* Make sure the console is always "hardwired". */
    379 		delay(1000);			/* wait for output to finish */
    380 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    381 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    382 	}
    383 
    384 #ifdef COM_HAYESP
    385 	/* Look for a Hayes ESP board. */
    386 	for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
    387 		bus_space_handle_t hayespioh;
    388 
    389 #define	HAYESP_NPORTS	8			/* XXX XXX XXX ??? ??? ??? */
    390 		if (bus_space_map(iot, *hayespp, HAYESP_NPORTS, 0, &hayespioh))
    391 			continue;
    392 		if (comprobeHAYESP(hayespioh, sc)) {
    393 			sc->sc_hayespioh = hayespioh;
    394 			sc->sc_fifolen = 1024;
    395 
    396 			/* Set 16550 compatibility mode */
    397 			bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_SETMODE);
    398 			bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
    399 			     HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
    400 			     HAYESP_MODE_SCALE);
    401 
    402 			/* Set RTS/CTS flow control */
    403 			bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_SETFLOWTYPE);
    404 			bus_space_write_1(iot, hayespioh, HAYESP_CMD2, HAYESP_FLOW_RTS);
    405 			bus_space_write_1(iot, hayespioh, HAYESP_CMD2, HAYESP_FLOW_CTS);
    406 
    407 			/* Set flow control levels */
    408 			bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_SETRXFLOW);
    409 			bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
    410 			     HAYESP_HIBYTE(HAYESP_RXHIWMARK));
    411 			bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
    412 			     HAYESP_LOBYTE(HAYESP_RXHIWMARK));
    413 			bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
    414 			     HAYESP_HIBYTE(HAYESP_RXLOWMARK));
    415 			bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
    416 			     HAYESP_LOBYTE(HAYESP_RXLOWMARK));
    417 
    418 			break;
    419 		}
    420 		bus_space_unmap(iot, hayespioh, HAYESP_NPORTS);
    421 	}
    422 	/* No ESP; look for other things. */
    423 	if (*hayespp == 0) {
    424 #endif
    425 	sc->sc_fifolen = 1;
    426 	/* look for a NS 16550AF UART with FIFOs */
    427 	bus_space_write_1(iot, ioh, com_fifo,
    428 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
    429 	delay(100);
    430 	if (ISSET(bus_space_read_1(iot, ioh, com_iir), IIR_FIFO_MASK)
    431 	    == IIR_FIFO_MASK)
    432 		if (ISSET(bus_space_read_1(iot, ioh, com_fifo), FIFO_TRIGGER_14)
    433 		    == FIFO_TRIGGER_14) {
    434 			SET(sc->sc_hwflags, COM_HW_FIFO);
    435 
    436 #ifdef COM16650
    437 			/*
    438 			 * IIR changes into the EFR if LCR is set to LCR_EERS
    439 			 * on 16650s. We also know IIR != 0 at this point.
    440 			 * Write 0 into the EFR, and read it. If the result
    441 			 * is 0, we have a 16650.
    442 			 *
    443 			 * Older 16650s were broken; the test to detect them
    444 			 * is taken from the Linux driver. Apparently
    445 			 * setting DLAB enable gives access to the EFR on
    446 			 * these chips.
    447 			 */
    448 			lcr = bus_space_read_1(iot, ioh, com_lcr);
    449 			bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
    450 			bus_space_write_1(iot, ioh, com_efr, 0);
    451 			if (bus_space_read_1(iot, ioh, com_efr) == 0) {
    452 				bus_space_write_1(iot, ioh, com_lcr,
    453 				    lcr | LCR_DLAB);
    454 				if (bus_space_read_1(iot, ioh, com_efr) == 0) {
    455 					CLR(sc->sc_hwflags, COM_HW_FIFO);
    456 					sc->sc_fifolen = 0;
    457 				} else {
    458 					SET(sc->sc_hwflags, COM_HW_FLOW);
    459 					sc->sc_fifolen = 32;
    460 				}
    461 			} else
    462 #endif
    463 				sc->sc_fifolen = 16;
    464 
    465 #ifdef COM16650
    466 			bus_space_write_1(iot, ioh, com_lcr, lcr);
    467 			if (sc->sc_fifolen == 0)
    468 				printf(": st16650, broken fifo\n");
    469 			else if (sc->sc_fifolen == 32)
    470 				printf(": st16650a, working fifo\n");
    471 			else
    472 #endif
    473 				printf(": ns16550a, working fifo\n");
    474 		} else
    475 			printf(": ns16550, broken fifo\n");
    476 	else
    477 		printf(": ns8250 or ns16450, no fifo\n");
    478 	bus_space_write_1(iot, ioh, com_fifo, 0);
    479 #ifdef COM_HAYESP
    480 	}
    481 #endif
    482 
    483 	tp = ttymalloc();
    484 	tp->t_oproc = comstart;
    485 	tp->t_param = comparam;
    486 	tp->t_hwiflow = comhwiflow;
    487 	tty_attach(tp);
    488 
    489 	sc->sc_tty = tp;
    490 	sc->sc_rbuf = malloc(com_rbuf_size << 1, M_DEVBUF, M_WAITOK);
    491 	sc->sc_ebuf = sc->sc_rbuf + (com_rbuf_size << 1);
    492 
    493 	if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
    494 		SET(sc->sc_mcr, MCR_IENABLE);
    495 
    496 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    497 		int maj;
    498 
    499 		/* locate the major number */
    500 		for (maj = 0; maj < nchrdev; maj++)
    501 			if (cdevsw[maj].d_open == comopen)
    502 				break;
    503 
    504 		cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
    505 #ifdef DDB
    506 		com_enable_debugport(sc);
    507 #endif
    508 		printf("%s: console\n", sc->sc_dev.dv_xname);
    509 	}
    510 
    511 #ifdef KGDB
    512 	/*
    513 	 * Allow kgdb to "take over" this port.  If this is
    514 	 * the kgdb device, it has exclusive use.
    515 	 */
    516 	if (iot == com_kgdb_iot && iobase == com_kgdb_addr) {
    517 		com_kgdb_attached = 1;
    518 
    519 		SET(sc->sc_hwflags, COM_HW_KGDB);
    520 		com_enable_debugport(sc);
    521 		printf("%s: kgdb\n", sc->sc_dev.dv_xname);
    522 	}
    523 #endif
    524 
    525 #ifdef __GENERIC_SOFT_INTERRUPTS
    526 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, comsoft, sc);
    527 #endif
    528 
    529 #if NRND > 0 && defined(RND_COM)
    530 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    531 			  RND_TYPE_TTY);
    532 #endif
    533 }
    534 
    535 int
    536 comopen(dev, flag, mode, p)
    537 	dev_t dev;
    538 	int flag, mode;
    539 	struct proc *p;
    540 {
    541 	int unit = COMUNIT(dev);
    542 	struct com_softc *sc;
    543 	struct tty *tp;
    544 	int s, s2;
    545 	int error = 0;
    546 
    547 	if (unit >= com_cd.cd_ndevs)
    548 		return (ENXIO);
    549 	sc = com_cd.cd_devs[unit];
    550 	if (!sc)
    551 		return (ENXIO);
    552 
    553 #ifdef KGDB
    554 	/*
    555 	 * If this is the kgdb port, no other use is permitted.
    556 	 */
    557 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    558 		return (EBUSY);
    559 #endif
    560 
    561 	tp = sc->sc_tty;
    562 
    563 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    564 	    ISSET(tp->t_state, TS_XCLUDE) &&
    565 	    p->p_ucred->cr_uid != 0)
    566 		return (EBUSY);
    567 
    568 	s = spltty();
    569 
    570 	/* We need to set this early for the benefit of comsoft(). */
    571 	SET(tp->t_state, TS_WOPEN);
    572 
    573 	/*
    574 	 * Do the following iff this is a first open.
    575 	 */
    576 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    577 		struct termios t;
    578 
    579 		tp->t_dev = dev;
    580 
    581 		s2 = splserial();
    582 
    583 		/* Turn on interrupts. */
    584 		sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
    585 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
    586 
    587 		/* Fetch the current modem control status, needed later. */
    588 		sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
    589 
    590 		splx(s2);
    591 
    592 		/*
    593 		 * Initialize the termios status to the defaults.  Add in the
    594 		 * sticky bits from TIOCSFLAGS.
    595 		 */
    596 		t.c_ispeed = 0;
    597 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    598 			t.c_ospeed = comconsrate;
    599 			t.c_cflag = comconscflag;
    600 		} else {
    601 			t.c_ospeed = TTYDEF_SPEED;
    602 			t.c_cflag = TTYDEF_CFLAG;
    603 		}
    604 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    605 			SET(t.c_cflag, CLOCAL);
    606 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    607 			SET(t.c_cflag, CRTSCTS);
    608 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    609 			SET(t.c_cflag, MDMBUF);
    610 		/* Make sure comparam() will do something. */
    611 		tp->t_ospeed = 0;
    612 		(void) comparam(tp, &t);
    613 		tp->t_iflag = TTYDEF_IFLAG;
    614 		tp->t_oflag = TTYDEF_OFLAG;
    615 		tp->t_lflag = TTYDEF_LFLAG;
    616 		ttychars(tp);
    617 		ttsetwater(tp);
    618 
    619 		/*
    620 		 * Turn on DTR.  We must always do this, even if carrier is not
    621 		 * present, because otherwise we'd have to use TIOCSDTR
    622 		 * immediately after setting CLOCAL, which applications do not
    623 		 * expect.  We always assert DTR while the device is open
    624 		 * unless explicitly requested to deassert it.
    625 		 */
    626 		com_modem(sc, 1);
    627 
    628 		s2 = splserial();
    629 
    630 		/* Clear the input ring, and unblock. */
    631 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    632 		sc->sc_rbavail = com_rbuf_size;
    633 		com_iflush(sc);
    634 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    635 		com_hwiflow(sc);
    636 
    637 #ifdef COM_DEBUG
    638 		if (com_debug)
    639 			comstatus(sc, "comopen  ");
    640 #endif
    641 
    642 		splx(s2);
    643 	}
    644 	error = 0;
    645 
    646 	/* If we're doing a blocking open... */
    647 	if (!ISSET(flag, O_NONBLOCK))
    648 		/* ...then wait for carrier. */
    649 		while (!ISSET(tp->t_state, TS_CARR_ON) &&
    650 		    !ISSET(tp->t_cflag, CLOCAL | MDMBUF)) {
    651 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    652 			    ttopen, 0);
    653 			if (error) {
    654 				/*
    655 				 * If the open was interrupted and nobody
    656 				 * else has the device open, then hang up.
    657 				 */
    658 				if (!ISSET(tp->t_state, TS_ISOPEN)) {
    659 					com_modem(sc, 0);
    660 					CLR(tp->t_state, TS_WOPEN);
    661 					ttwakeup(tp);
    662 				}
    663 				break;
    664 			}
    665 			SET(tp->t_state, TS_WOPEN);
    666 		}
    667 
    668 	splx(s);
    669 	if (error == 0)
    670 		error = (*linesw[tp->t_line].l_open)(dev, tp);
    671 	return (error);
    672 }
    673 
    674 int
    675 comclose(dev, flag, mode, p)
    676 	dev_t dev;
    677 	int flag, mode;
    678 	struct proc *p;
    679 {
    680 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    681 	struct tty *tp = sc->sc_tty;
    682 	int s;
    683 
    684 	/* XXX This is for cons.c. */
    685 	if (!ISSET(tp->t_state, TS_ISOPEN))
    686 		return (0);
    687 
    688 	(*linesw[tp->t_line].l_close)(tp, flag);
    689 	ttyclose(tp);
    690 
    691 	s = splserial();
    692 
    693 	/* If we were asserting flow control, then deassert it. */
    694 	SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
    695 	com_hwiflow(sc);
    696 
    697 	splx(s);
    698 
    699 	/* Clear any break condition set with TIOCSBRK. */
    700 	com_break(sc, 0);
    701 
    702 	/*
    703 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    704 	 * notice even if we immediately open the port again.
    705 	 */
    706 	if (ISSET(tp->t_cflag, HUPCL)) {
    707 		com_modem(sc, 0);
    708 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    709 	}
    710 
    711 	s = splserial();
    712 
    713 	/* Turn off interrupts. */
    714 #ifdef DDB
    715 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    716 		sc->sc_ier = IER_ERXRDY; /* interrupt on break */
    717 	else
    718 #endif
    719 		sc->sc_ier = 0;
    720 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
    721 
    722 	splx(s);
    723 
    724 	return (0);
    725 }
    726 
    727 int
    728 comread(dev, uio, flag)
    729 	dev_t dev;
    730 	struct uio *uio;
    731 	int flag;
    732 {
    733 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    734 	struct tty *tp = sc->sc_tty;
    735 
    736 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    737 }
    738 
    739 int
    740 comwrite(dev, uio, flag)
    741 	dev_t dev;
    742 	struct uio *uio;
    743 	int flag;
    744 {
    745 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    746 	struct tty *tp = sc->sc_tty;
    747 
    748 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    749 }
    750 
    751 struct tty *
    752 comtty(dev)
    753 	dev_t dev;
    754 {
    755 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    756 	struct tty *tp = sc->sc_tty;
    757 
    758 	return (tp);
    759 }
    760 
    761 static u_char
    762 tiocm_xxx2mcr(data)
    763 	int data;
    764 {
    765 	u_char m = 0;
    766 
    767 	if (ISSET(data, TIOCM_DTR))
    768 		SET(m, MCR_DTR);
    769 	if (ISSET(data, TIOCM_RTS))
    770 		SET(m, MCR_RTS);
    771 	return m;
    772 }
    773 
    774 int
    775 comioctl(dev, cmd, data, flag, p)
    776 	dev_t dev;
    777 	u_long cmd;
    778 	caddr_t data;
    779 	int flag;
    780 	struct proc *p;
    781 {
    782 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    783 	struct tty *tp = sc->sc_tty;
    784 	int error;
    785 
    786 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    787 	if (error >= 0)
    788 		return (error);
    789 
    790 	error = ttioctl(tp, cmd, data, flag, p);
    791 	if (error >= 0)
    792 		return (error);
    793 
    794 	switch (cmd) {
    795 	case TIOCSBRK:
    796 		com_break(sc, 1);
    797 		break;
    798 
    799 	case TIOCCBRK:
    800 		com_break(sc, 0);
    801 		break;
    802 
    803 	case TIOCSDTR:
    804 		com_modem(sc, 1);
    805 		break;
    806 
    807 	case TIOCCDTR:
    808 		com_modem(sc, 0);
    809 		break;
    810 
    811 	case TIOCGFLAGS:
    812 		*(int *)data = sc->sc_swflags;
    813 		break;
    814 
    815 	case TIOCSFLAGS:
    816 		error = suser(p->p_ucred, &p->p_acflag);
    817 		if (error)
    818 			return (error);
    819 		sc->sc_swflags = *(int *)data;
    820 		break;
    821 
    822 	case TIOCMSET:
    823 		CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
    824 		/*FALLTHROUGH*/
    825 
    826 	case TIOCMBIS:
    827 		SET(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
    828 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
    829 		break;
    830 
    831 	case TIOCMBIC:
    832 		CLR(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
    833 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
    834 		break;
    835 
    836 	case TIOCMGET: {
    837 		u_char m;
    838 		int bits = 0;
    839 
    840 		m = sc->sc_mcr;
    841 		if (ISSET(m, MCR_DTR))
    842 			SET(bits, TIOCM_DTR);
    843 		if (ISSET(m, MCR_RTS))
    844 			SET(bits, TIOCM_RTS);
    845 		m = sc->sc_msr;
    846 		if (ISSET(m, MSR_DCD))
    847 			SET(bits, TIOCM_CD);
    848 		if (ISSET(m, MSR_CTS))
    849 			SET(bits, TIOCM_CTS);
    850 		if (ISSET(m, MSR_DSR))
    851 			SET(bits, TIOCM_DSR);
    852 		if (ISSET(m, MSR_RI | MSR_TERI))
    853 			SET(bits, TIOCM_RI);
    854 		if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_ier))
    855 			SET(bits, TIOCM_LE);
    856 		*(int *)data = bits;
    857 		break;
    858 	}
    859 	default:
    860 		return (ENOTTY);
    861 	}
    862 
    863 #ifdef COM_DEBUG
    864 	if (com_debug)
    865 		comstatus(sc, "comioctl ");
    866 #endif
    867 
    868 	return (0);
    869 }
    870 
    871 integrate void
    872 com_schedrx(sc)
    873 	struct com_softc *sc;
    874 {
    875 
    876 	sc->sc_rx_ready = 1;
    877 
    878 	/* Wake up the poller. */
    879 #ifdef __GENERIC_SOFT_INTERRUPTS
    880 	softintr_schedule(sc->sc_si);
    881 #else
    882 #ifndef alpha
    883 	setsoftserial();
    884 #else
    885 	if (!com_softintr_scheduled) {
    886 		com_softintr_scheduled = 1;
    887 		timeout(comsoft, NULL, 1);
    888 	}
    889 #endif
    890 #endif
    891 }
    892 
    893 void
    894 com_break(sc, onoff)
    895 	struct com_softc *sc;
    896 	int onoff;
    897 {
    898 	int s;
    899 
    900 	s = splserial();
    901 	if (onoff)
    902 		SET(sc->sc_lcr, LCR_SBREAK);
    903 	else
    904 		CLR(sc->sc_lcr, LCR_SBREAK);
    905 
    906 	if (!sc->sc_heldchange) {
    907 		if (sc->sc_tx_busy) {
    908 			sc->sc_heldtbc = sc->sc_tbc;
    909 			sc->sc_tbc = 0;
    910 			sc->sc_heldchange = 1;
    911 		} else
    912 			com_loadchannelregs(sc);
    913 	}
    914 	splx(s);
    915 }
    916 
    917 void
    918 com_modem(sc, onoff)
    919 	struct com_softc *sc;
    920 	int onoff;
    921 {
    922 	int s;
    923 
    924 	s = splserial();
    925 	if (onoff)
    926 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
    927 	else
    928 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
    929 
    930 	if (!sc->sc_heldchange) {
    931 		if (sc->sc_tx_busy) {
    932 			sc->sc_heldtbc = sc->sc_tbc;
    933 			sc->sc_tbc = 0;
    934 			sc->sc_heldchange = 1;
    935 		} else
    936 			com_loadchannelregs(sc);
    937 	}
    938 	splx(s);
    939 }
    940 
    941 static u_char
    942 cflag2lcr(cflag)
    943 	tcflag_t cflag;
    944 {
    945 	u_char lcr = 0;
    946 
    947 	switch (ISSET(cflag, CSIZE)) {
    948 	case CS5:
    949 		SET(lcr, LCR_5BITS);
    950 		break;
    951 	case CS6:
    952 		SET(lcr, LCR_6BITS);
    953 		break;
    954 	case CS7:
    955 		SET(lcr, LCR_7BITS);
    956 		break;
    957 	case CS8:
    958 		SET(lcr, LCR_8BITS);
    959 		break;
    960 	}
    961 	if (ISSET(cflag, PARENB)) {
    962 		SET(lcr, LCR_PENAB);
    963 		if (!ISSET(cflag, PARODD))
    964 			SET(lcr, LCR_PEVEN);
    965 	}
    966 	if (ISSET(cflag, CSTOPB))
    967 		SET(lcr, LCR_STOPB);
    968 
    969 	return (lcr);
    970 }
    971 
    972 int
    973 comparam(tp, t)
    974 	struct tty *tp;
    975 	struct termios *t;
    976 {
    977 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
    978 	int ospeed = comspeed(t->c_ospeed, sc->sc_frequency);
    979 	u_char lcr;
    980 	int s;
    981 
    982 	/* Check requested parameters. */
    983 	if (ospeed < 0)
    984 		return (EINVAL);
    985 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    986 		return (EINVAL);
    987 
    988 	/*
    989 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    990 	 * is always active.
    991 	 */
    992 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    993 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    994 		SET(t->c_cflag, CLOCAL);
    995 		CLR(t->c_cflag, HUPCL);
    996 	}
    997 
    998 	/*
    999 	 * If there were no changes, don't do anything.  This avoids dropping
   1000 	 * input and improves performance when all we did was frob things like
   1001 	 * VMIN and VTIME.
   1002 	 */
   1003 	if (tp->t_ospeed == t->c_ospeed &&
   1004 	    tp->t_cflag == t->c_cflag)
   1005 		return (0);
   1006 
   1007 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
   1008 
   1009 	s = splserial();
   1010 
   1011 	sc->sc_lcr = lcr;
   1012 
   1013 	/*
   1014 	 * If we're not in a mode that assumes a connection is present, then
   1015 	 * ignore carrier changes.
   1016 	 */
   1017 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1018 		sc->sc_msr_dcd = 0;
   1019 	else
   1020 		sc->sc_msr_dcd = MSR_DCD;
   1021 	/*
   1022 	 * Set the flow control pins depending on the current flow control
   1023 	 * mode.
   1024 	 */
   1025 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1026 		sc->sc_mcr_dtr = MCR_DTR;
   1027 		sc->sc_mcr_rts = MCR_RTS;
   1028 		sc->sc_msr_cts = MSR_CTS;
   1029 		sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
   1030 	} else if (ISSET(t->c_cflag, MDMBUF)) {
   1031 		/*
   1032 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1033 		 * carrier detection.
   1034 		 */
   1035 		sc->sc_mcr_dtr = 0;
   1036 		sc->sc_mcr_rts = MCR_DTR;
   1037 		sc->sc_msr_cts = MSR_DCD;
   1038 		sc->sc_efr = 0;
   1039 	} else {
   1040 		/*
   1041 		 * If no flow control, then always set RTS.  This will make
   1042 		 * the other side happy if it mistakenly thinks we're doing
   1043 		 * RTS/CTS flow control.
   1044 		 */
   1045 		sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
   1046 		sc->sc_mcr_rts = 0;
   1047 		sc->sc_msr_cts = 0;
   1048 		sc->sc_efr = 0;
   1049 		if (ISSET(sc->sc_mcr, MCR_DTR))
   1050 			SET(sc->sc_mcr, MCR_RTS);
   1051 		else
   1052 			CLR(sc->sc_mcr, MCR_RTS);
   1053 	}
   1054 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
   1055 
   1056 #if 0
   1057 	if (ospeed == 0)
   1058 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1059 	else
   1060 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1061 #endif
   1062 
   1063 	sc->sc_dlbl = ospeed;
   1064 	sc->sc_dlbh = ospeed >> 8;
   1065 
   1066 	/*
   1067 	 * Set the FIFO threshold based on the receive speed.
   1068 	 *
   1069 	 *  * If it's a low speed, it's probably a mouse or some other
   1070 	 *    interactive device, so set the threshold low.
   1071 	 *  * If it's a high speed, trim the trigger level down to prevent
   1072 	 *    overflows.
   1073 	 *  * Otherwise set it a bit higher.
   1074 	 */
   1075 	if (ISSET(sc->sc_hwflags, COM_HW_HAYESP))
   1076 		sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
   1077 	else if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
   1078 		sc->sc_fifo = FIFO_ENABLE |
   1079 		    (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
   1080 		     t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
   1081 	else
   1082 		sc->sc_fifo = 0;
   1083 
   1084 	/* And copy to tty. */
   1085 	tp->t_ispeed = 0;
   1086 	tp->t_ospeed = t->c_ospeed;
   1087 	tp->t_cflag = t->c_cflag;
   1088 
   1089 	if (!sc->sc_heldchange) {
   1090 		if (sc->sc_tx_busy) {
   1091 			sc->sc_heldtbc = sc->sc_tbc;
   1092 			sc->sc_tbc = 0;
   1093 			sc->sc_heldchange = 1;
   1094 		} else
   1095 			com_loadchannelregs(sc);
   1096 	}
   1097 
   1098 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1099 		/* Disable the high water mark. */
   1100 		sc->sc_r_hiwat = 0;
   1101 		sc->sc_r_lowat = 0;
   1102 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1103 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1104 			com_schedrx(sc);
   1105 		}
   1106 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1107 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1108 			com_hwiflow(sc);
   1109 		}
   1110 	} else {
   1111 		sc->sc_r_hiwat = com_rbuf_hiwat;
   1112 		sc->sc_r_lowat = com_rbuf_lowat;
   1113 	}
   1114 
   1115 	splx(s);
   1116 
   1117 	/*
   1118 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1119 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1120 	 * explicit request.
   1121 	 */
   1122 	(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
   1123 
   1124 #ifdef COM_DEBUG
   1125 	if (com_debug)
   1126 		comstatus(sc, "comparam ");
   1127 #endif
   1128 
   1129 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1130 		if (sc->sc_tx_stopped) {
   1131 			sc->sc_tx_stopped = 0;
   1132 			comstart(tp);
   1133 		}
   1134 	}
   1135 
   1136 	return (0);
   1137 }
   1138 
   1139 void
   1140 com_iflush(sc)
   1141 	struct com_softc *sc;
   1142 {
   1143 	bus_space_tag_t iot = sc->sc_iot;
   1144 	bus_space_handle_t ioh = sc->sc_ioh;
   1145 
   1146 	/* flush any pending I/O */
   1147 	while (ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
   1148 		(void) bus_space_read_1(iot, ioh, com_data);
   1149 }
   1150 
   1151 void
   1152 com_loadchannelregs(sc)
   1153 	struct com_softc *sc;
   1154 {
   1155 	bus_space_tag_t iot = sc->sc_iot;
   1156 	bus_space_handle_t ioh = sc->sc_ioh;
   1157 
   1158 	/* XXXXX necessary? */
   1159 	com_iflush(sc);
   1160 
   1161 	bus_space_write_1(iot, ioh, com_ier, 0);
   1162 
   1163 	if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
   1164 		bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   1165 		bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
   1166 	}
   1167 	bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr | LCR_DLAB);
   1168 	bus_space_write_1(iot, ioh, com_dlbl, sc->sc_dlbl);
   1169 	bus_space_write_1(iot, ioh, com_dlbh, sc->sc_dlbh);
   1170 	bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
   1171 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active = sc->sc_mcr);
   1172 	bus_space_write_1(iot, ioh, com_fifo, sc->sc_fifo);
   1173 
   1174 	bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1175 }
   1176 
   1177 int
   1178 comhwiflow(tp, block)
   1179 	struct tty *tp;
   1180 	int block;
   1181 {
   1182 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1183 	int s;
   1184 
   1185 	if (sc->sc_mcr_rts == 0)
   1186 		return (0);
   1187 
   1188 	s = splserial();
   1189 	if (block) {
   1190 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1191 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1192 			com_hwiflow(sc);
   1193 		}
   1194 	} else {
   1195 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1196 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1197 			com_schedrx(sc);
   1198 		}
   1199 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1200 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1201 			com_hwiflow(sc);
   1202 		}
   1203 	}
   1204 	splx(s);
   1205 	return (1);
   1206 }
   1207 
   1208 /*
   1209  * (un)block input via hw flowcontrol
   1210  */
   1211 void
   1212 com_hwiflow(sc)
   1213 	struct com_softc *sc;
   1214 {
   1215 	bus_space_tag_t iot = sc->sc_iot;
   1216 	bus_space_handle_t ioh = sc->sc_ioh;
   1217 
   1218 	if (sc->sc_mcr_rts == 0)
   1219 		return;
   1220 
   1221 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
   1222 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
   1223 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
   1224 	} else {
   1225 		SET(sc->sc_mcr, sc->sc_mcr_rts);
   1226 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
   1227 	}
   1228 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
   1229 }
   1230 
   1231 
   1232 void
   1233 comstart(tp)
   1234 	struct tty *tp;
   1235 {
   1236 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1237 	bus_space_tag_t iot = sc->sc_iot;
   1238 	bus_space_handle_t ioh = sc->sc_ioh;
   1239 	int s;
   1240 
   1241 	s = spltty();
   1242 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1243 		goto out;
   1244 	if (sc->sc_tx_stopped)
   1245 		goto out;
   1246 
   1247 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1248 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1249 			CLR(tp->t_state, TS_ASLEEP);
   1250 			wakeup(&tp->t_outq);
   1251 		}
   1252 		selwakeup(&tp->t_wsel);
   1253 		if (tp->t_outq.c_cc == 0)
   1254 			goto out;
   1255 	}
   1256 
   1257 	/* Grab the first contiguous region of buffer space. */
   1258 	{
   1259 		u_char *tba;
   1260 		int tbc;
   1261 
   1262 		tba = tp->t_outq.c_cf;
   1263 		tbc = ndqb(&tp->t_outq, 0);
   1264 
   1265 		(void)splserial();
   1266 
   1267 		sc->sc_tba = tba;
   1268 		sc->sc_tbc = tbc;
   1269 	}
   1270 
   1271 	SET(tp->t_state, TS_BUSY);
   1272 	sc->sc_tx_busy = 1;
   1273 
   1274 	/* Enable transmit completion interrupts if necessary. */
   1275 	if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
   1276 		SET(sc->sc_ier, IER_ETXRDY);
   1277 		bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1278 	}
   1279 
   1280 	/* Output the first chunk of the contiguous buffer. */
   1281 	{
   1282 		int n;
   1283 
   1284 		n = sc->sc_tbc;
   1285 		if (n > sc->sc_fifolen)
   1286 			n = sc->sc_fifolen;
   1287 		bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
   1288 		sc->sc_tbc -= n;
   1289 		sc->sc_tba += n;
   1290 	}
   1291 out:
   1292 	splx(s);
   1293 	return;
   1294 }
   1295 
   1296 /*
   1297  * Stop output on a line.
   1298  */
   1299 void
   1300 comstop(tp, flag)
   1301 	struct tty *tp;
   1302 	int flag;
   1303 {
   1304 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1305 	int s;
   1306 
   1307 	s = splserial();
   1308 	if (ISSET(tp->t_state, TS_BUSY)) {
   1309 		/* Stop transmitting at the next chunk. */
   1310 		sc->sc_tbc = 0;
   1311 		sc->sc_heldtbc = 0;
   1312 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1313 			SET(tp->t_state, TS_FLUSH);
   1314 	}
   1315 	splx(s);
   1316 }
   1317 
   1318 void
   1319 comdiag(arg)
   1320 	void *arg;
   1321 {
   1322 	struct com_softc *sc = arg;
   1323 	int overflows, floods;
   1324 	int s;
   1325 
   1326 	s = splserial();
   1327 	overflows = sc->sc_overflows;
   1328 	sc->sc_overflows = 0;
   1329 	floods = sc->sc_floods;
   1330 	sc->sc_floods = 0;
   1331 	sc->sc_errors = 0;
   1332 	splx(s);
   1333 
   1334 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1335 	    sc->sc_dev.dv_xname,
   1336 	    overflows, overflows == 1 ? "" : "s",
   1337 	    floods, floods == 1 ? "" : "s");
   1338 }
   1339 
   1340 integrate void
   1341 com_rxsoft(sc, tp)
   1342 	struct com_softc *sc;
   1343 	struct tty *tp;
   1344 {
   1345 	int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
   1346 	u_char *get, *end;
   1347 	u_int cc, scc;
   1348 	u_char lsr;
   1349 	int code;
   1350 	int s;
   1351 
   1352 	end = sc->sc_ebuf;
   1353 	get = sc->sc_rbget;
   1354 	scc = cc = com_rbuf_size - sc->sc_rbavail;
   1355 
   1356 	if (cc == com_rbuf_size) {
   1357 		sc->sc_floods++;
   1358 		if (sc->sc_errors++ == 0)
   1359 			timeout(comdiag, sc, 60 * hz);
   1360 	}
   1361 
   1362 	while (cc) {
   1363 		code = get[0];
   1364 		lsr = get[1];
   1365 		if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
   1366 			if (ISSET(lsr, LSR_OE)) {
   1367 				sc->sc_overflows++;
   1368 				if (sc->sc_errors++ == 0)
   1369 					timeout(comdiag, sc, 60 * hz);
   1370 			}
   1371 			if (ISSET(lsr, LSR_BI | LSR_FE))
   1372 				SET(code, TTY_FE);
   1373 			if (ISSET(lsr, LSR_PE))
   1374 				SET(code, TTY_PE);
   1375 		}
   1376 		if ((*rint)(code, tp) == -1) {
   1377 			/*
   1378 			 * The line discipline's buffer is out of space.
   1379 			 */
   1380 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1381 				/*
   1382 				 * We're either not using flow control, or the
   1383 				 * line discipline didn't tell us to block for
   1384 				 * some reason.  Either way, we have no way to
   1385 				 * know when there's more space available, so
   1386 				 * just drop the rest of the data.
   1387 				 */
   1388 				get += cc << 1;
   1389 				if (get >= end)
   1390 					get -= com_rbuf_size << 1;
   1391 				cc = 0;
   1392 			} else {
   1393 				/*
   1394 				 * Don't schedule any more receive processing
   1395 				 * until the line discipline tells us there's
   1396 				 * space available (through comhwiflow()).
   1397 				 * Leave the rest of the data in the input
   1398 				 * buffer.
   1399 				 */
   1400 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1401 			}
   1402 			break;
   1403 		}
   1404 		get += 2;
   1405 		if (get >= end)
   1406 			get = sc->sc_rbuf;
   1407 		cc--;
   1408 	}
   1409 
   1410 	if (cc != scc) {
   1411 		sc->sc_rbget = get;
   1412 		s = splserial();
   1413 		cc = sc->sc_rbavail += scc - cc;
   1414 		/* Buffers should be ok again, release possible block. */
   1415 		if (cc >= sc->sc_r_lowat) {
   1416 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1417 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1418 				SET(sc->sc_ier, IER_ERXRDY);
   1419 				bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
   1420 			}
   1421 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1422 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1423 				com_hwiflow(sc);
   1424 			}
   1425 		}
   1426 		splx(s);
   1427 	}
   1428 }
   1429 
   1430 integrate void
   1431 com_txsoft(sc, tp)
   1432 	struct com_softc *sc;
   1433 	struct tty *tp;
   1434 {
   1435 
   1436 	CLR(tp->t_state, TS_BUSY);
   1437 	if (ISSET(tp->t_state, TS_FLUSH))
   1438 		CLR(tp->t_state, TS_FLUSH);
   1439 	else
   1440 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1441 	(*linesw[tp->t_line].l_start)(tp);
   1442 }
   1443 
   1444 integrate void
   1445 com_stsoft(sc, tp)
   1446 	struct com_softc *sc;
   1447 	struct tty *tp;
   1448 {
   1449 	u_char msr, delta;
   1450 	int s;
   1451 
   1452 	s = splserial();
   1453 	msr = sc->sc_msr;
   1454 	delta = sc->sc_msr_delta;
   1455 	sc->sc_msr_delta = 0;
   1456 	splx(s);
   1457 
   1458 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1459 		/*
   1460 		 * Inform the tty layer that carrier detect changed.
   1461 		 */
   1462 		(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD));
   1463 	}
   1464 
   1465 	if (ISSET(delta, sc->sc_msr_cts)) {
   1466 		/* Block or unblock output according to flow control. */
   1467 		if (ISSET(msr, sc->sc_msr_cts)) {
   1468 			sc->sc_tx_stopped = 0;
   1469 			(*linesw[tp->t_line].l_start)(tp);
   1470 		} else {
   1471 			sc->sc_tx_stopped = 1;
   1472 		}
   1473 	}
   1474 
   1475 #ifdef COM_DEBUG
   1476 	if (com_debug)
   1477 		comstatus(sc, "com_stsoft");
   1478 #endif
   1479 }
   1480 
   1481 #ifdef __GENERIC_SOFT_INTERRUPTS
   1482 void
   1483 comsoft(arg)
   1484 	void *arg;
   1485 {
   1486 	struct com_softc *sc = arg;
   1487 	struct tty *tp;
   1488 
   1489 	{
   1490 #else
   1491 void
   1492 #ifndef alpha
   1493 comsoft()
   1494 #else
   1495 comsoft(arg)
   1496 	void *arg;
   1497 #endif
   1498 {
   1499 	struct com_softc	*sc;
   1500 	struct tty	*tp;
   1501 	int	unit;
   1502 #ifdef alpha
   1503 	int s;
   1504 
   1505 	s = splsoftserial();
   1506 	com_softintr_scheduled = 0;
   1507 #endif
   1508 
   1509 	for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
   1510 		sc = com_cd.cd_devs[unit];
   1511 		if (sc == NULL)
   1512 			continue;
   1513 
   1514 		tp = sc->sc_tty;
   1515 		if (tp == NULL || !ISSET(tp->t_state, TS_ISOPEN | TS_WOPEN))
   1516 			continue;
   1517 #endif
   1518 		tp = sc->sc_tty;
   1519 
   1520 		if (sc->sc_rx_ready) {
   1521 			sc->sc_rx_ready = 0;
   1522 			com_rxsoft(sc, tp);
   1523 		}
   1524 
   1525 		if (sc->sc_st_check) {
   1526 			sc->sc_st_check = 0;
   1527 			com_stsoft(sc, tp);
   1528 		}
   1529 
   1530 		if (sc->sc_tx_done) {
   1531 			sc->sc_tx_done = 0;
   1532 			com_txsoft(sc, tp);
   1533 		}
   1534 	}
   1535 
   1536 #ifndef __GENERIC_SOFT_INTERRUPTS
   1537 #ifdef alpha
   1538 	splx(s);
   1539 #endif
   1540 #endif
   1541 }
   1542 
   1543 int
   1544 comintr(arg)
   1545 	void *arg;
   1546 {
   1547 	struct com_softc *sc = arg;
   1548 	bus_space_tag_t iot = sc->sc_iot;
   1549 	bus_space_handle_t ioh = sc->sc_ioh;
   1550 	u_char *put, *end;
   1551 	u_int cc;
   1552 	u_char lsr, iir;
   1553 
   1554 	iir = bus_space_read_1(iot, ioh, com_iir);
   1555 	if (ISSET(iir, IIR_NOPEND))
   1556 		return (0);
   1557 
   1558 	end = sc->sc_ebuf;
   1559 	put = sc->sc_rbput;
   1560 	cc = sc->sc_rbavail;
   1561 
   1562 	do {
   1563 		u_char	msr, delta;
   1564 
   1565 		lsr = bus_space_read_1(iot, ioh, com_lsr);
   1566 #if defined(DDB) || defined(KGDB)
   1567 		if (ISSET(lsr, LSR_BI)) {
   1568 #ifdef DDB
   1569 			if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1570 				Debugger();
   1571 				continue;
   1572 			}
   1573 #endif
   1574 #ifdef KGDB
   1575 			if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
   1576 				kgdb_connect(1);
   1577 				continue;
   1578 			}
   1579 #endif
   1580 		}
   1581 #endif /* DDB || KGDB */
   1582 
   1583 		if (ISSET(lsr, LSR_RCV_MASK) &&
   1584 		    !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1585 			while (cc > 0) {
   1586 				put[0] = bus_space_read_1(iot, ioh, com_data);
   1587 				put[1] = lsr;
   1588 				put += 2;
   1589 				if (put >= end)
   1590 					put = sc->sc_rbuf;
   1591 				cc--;
   1592 
   1593 				lsr = bus_space_read_1(iot, ioh, com_lsr);
   1594 				if (!ISSET(lsr, LSR_RCV_MASK))
   1595 					break;
   1596 			}
   1597 
   1598 			/*
   1599 			 * Current string of incoming characters ended because
   1600 			 * no more data was available or we ran out of space.
   1601 			 * Schedule a receive event if any data was received.
   1602 			 * If we're out of space, turn off receive interrupts.
   1603 			 */
   1604 			sc->sc_rbput = put;
   1605 			sc->sc_rbavail = cc;
   1606 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1607 				sc->sc_rx_ready = 1;
   1608 
   1609 			/*
   1610 			 * See if we are in danger of overflowing a buffer. If
   1611 			 * so, use hardware flow control to ease the pressure.
   1612 			 */
   1613 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   1614 			    cc < sc->sc_r_hiwat) {
   1615 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1616 				com_hwiflow(sc);
   1617 			}
   1618 
   1619 			/*
   1620 			 * If we're out of space, disable receive interrupts
   1621 			 * until the queue has drained a bit.
   1622 			 */
   1623 			if (!cc) {
   1624 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1625 				CLR(sc->sc_ier, IER_ERXRDY);
   1626 				bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1627 			}
   1628 		} else {
   1629 			if ((iir & IIR_IMASK) == IIR_RXRDY) {
   1630 				bus_space_write_1(iot, ioh, com_ier, 0);
   1631 				delay(10);
   1632 				bus_space_write_1(iot, ioh, com_ier,sc->sc_ier);
   1633 				iir = IIR_NOPEND;
   1634 				continue;
   1635 			}
   1636 		}
   1637 
   1638 		msr = bus_space_read_1(iot, ioh, com_msr);
   1639 		delta = msr ^ sc->sc_msr;
   1640 		sc->sc_msr = msr;
   1641 		if (ISSET(delta, sc->sc_msr_mask)) {
   1642 			SET(sc->sc_msr_delta, delta);
   1643 
   1644 			/*
   1645 			 * Stop output immediately if we lose the output
   1646 			 * flow control signal or carrier detect.
   1647 			 */
   1648 			if (ISSET(~msr, sc->sc_msr_mask)) {
   1649 				sc->sc_tbc = 0;
   1650 				sc->sc_heldtbc = 0;
   1651 #ifdef COM_DEBUG
   1652 				if (com_debug)
   1653 					comstatus(sc, "comintr  ");
   1654 #endif
   1655 			}
   1656 
   1657 			sc->sc_st_check = 1;
   1658 		}
   1659 	} while (!ISSET((iir = bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND));
   1660 
   1661 	/*
   1662 	 * Done handling any receive interrupts. See if data can be
   1663 	 * transmitted as well. Schedule tx done event if no data left
   1664 	 * and tty was marked busy.
   1665 	 */
   1666 	if (ISSET(lsr, LSR_TXRDY)) {
   1667 		/*
   1668 		 * If we've delayed a parameter change, do it now, and restart
   1669 		 * output.
   1670 		 */
   1671 		if (sc->sc_heldchange) {
   1672 			com_loadchannelregs(sc);
   1673 			sc->sc_heldchange = 0;
   1674 			sc->sc_tbc = sc->sc_heldtbc;
   1675 			sc->sc_heldtbc = 0;
   1676 		}
   1677 
   1678 		/* Output the next chunk of the contiguous buffer, if any. */
   1679 		if (sc->sc_tbc > 0) {
   1680 			int n;
   1681 
   1682 			n = sc->sc_tbc;
   1683 			if (n > sc->sc_fifolen)
   1684 				n = sc->sc_fifolen;
   1685 			bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
   1686 			sc->sc_tbc -= n;
   1687 			sc->sc_tba += n;
   1688 		} else {
   1689 			/* Disable transmit completion interrupts if necessary. */
   1690 			if (ISSET(sc->sc_ier, IER_ETXRDY)) {
   1691 				CLR(sc->sc_ier, IER_ETXRDY);
   1692 				bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1693 			}
   1694 			if (sc->sc_tx_busy) {
   1695 				sc->sc_tx_busy = 0;
   1696 				sc->sc_tx_done = 1;
   1697 			}
   1698 		}
   1699 	}
   1700 
   1701 	/* Wake up the poller. */
   1702 #ifdef __GENERIC_SOFT_INTERRUPTS
   1703 	softintr_schedule(sc->sc_si);
   1704 #else
   1705 #ifndef alpha
   1706 	setsoftserial();
   1707 #else
   1708 	if (!com_softintr_scheduled) {
   1709 		com_softintr_scheduled = 1;
   1710 		timeout(comsoft, NULL, 1);
   1711 	}
   1712 #endif
   1713 #endif
   1714 
   1715 #if NRND > 0 && defined(RND_COM)
   1716 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1717 #endif
   1718 
   1719 	return (1);
   1720 }
   1721 
   1722 /*
   1723  * The following functions are polled getc and putc routines, shared
   1724  * by the console and kgdb glue.
   1725  */
   1726 
   1727 int
   1728 com_common_getc(iot, ioh)
   1729 	bus_space_tag_t iot;
   1730 	bus_space_handle_t ioh;
   1731 {
   1732 	int s = splserial();
   1733 	u_char stat, c;
   1734 
   1735 	while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
   1736 		;
   1737 	c = bus_space_read_1(iot, ioh, com_data);
   1738 	stat = bus_space_read_1(iot, ioh, com_iir);
   1739 	splx(s);
   1740 	return (c);
   1741 }
   1742 
   1743 void
   1744 com_common_putc(iot, ioh, c)
   1745 	bus_space_tag_t iot;
   1746 	bus_space_handle_t ioh;
   1747 	int c;
   1748 {
   1749 	int s = splserial();
   1750 	u_char stat;
   1751 	int timo;
   1752 
   1753 	/* wait for any pending transmission to finish */
   1754 	timo = 50000;
   1755 	while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY)
   1756 	    && --timo)
   1757 		;
   1758 	bus_space_write_1(iot, ioh, com_data, c);
   1759 	/* wait for this transmission to complete */
   1760 	timo = 1500000;
   1761 	while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY)
   1762 	    && --timo)
   1763 		;
   1764 	/* clear any interrupts generated by this transmission */
   1765 	stat = bus_space_read_1(iot, ioh, com_iir);
   1766 	splx(s);
   1767 }
   1768 
   1769 /*
   1770  * Initialize UART to known state.
   1771  */
   1772 int
   1773 cominit(iot, iobase, rate, frequency, cflag, iohp)
   1774 	bus_space_tag_t iot;
   1775 	int iobase;
   1776 	int rate, frequency;
   1777 	tcflag_t cflag;
   1778 	bus_space_handle_t *iohp;
   1779 {
   1780 	bus_space_handle_t ioh;
   1781 
   1782 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
   1783 		return (ENOMEM); /* ??? */
   1784 
   1785 	bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   1786 	bus_space_write_1(iot, ioh, com_efr, 0);
   1787 	bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
   1788 	rate = comspeed(rate, frequency);
   1789 	bus_space_write_1(iot, ioh, com_dlbl, rate);
   1790 	bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
   1791 	bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
   1792 	bus_space_write_1(iot, ioh, com_mcr, 0);
   1793 	bus_space_write_1(iot, ioh, com_fifo,
   1794 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
   1795 	bus_space_write_1(iot, ioh, com_ier, 0);
   1796 
   1797 	*iohp = ioh;
   1798 	return (0);
   1799 }
   1800 
   1801 /*
   1802  * Following are all routines needed for COM to act as console
   1803  */
   1804 
   1805 int
   1806 comcnattach(iot, iobase, rate, frequency, cflag)
   1807 	bus_space_tag_t iot;
   1808 	int iobase;
   1809 	int rate, frequency;
   1810 	tcflag_t cflag;
   1811 {
   1812 	int res;
   1813 	static struct consdev comcons = {
   1814 		NULL, NULL, comcngetc, comcnputc, comcnpollc, NODEV, CN_NORMAL
   1815 	};
   1816 
   1817 	res = cominit(iot, iobase, rate, frequency, cflag, &comconsioh);
   1818 	if (res)
   1819 		return (res);
   1820 
   1821 	cn_tab = &comcons;
   1822 
   1823 	comconstag = iot;
   1824 	comconsaddr = iobase;
   1825 	comconsrate = rate;
   1826 	comconscflag = cflag;
   1827 
   1828 	return (0);
   1829 }
   1830 
   1831 int
   1832 comcngetc(dev)
   1833 	dev_t dev;
   1834 {
   1835 
   1836 	return (com_common_getc(comconstag, comconsioh));
   1837 }
   1838 
   1839 /*
   1840  * Console kernel output character routine.
   1841  */
   1842 void
   1843 comcnputc(dev, c)
   1844 	dev_t dev;
   1845 	int c;
   1846 {
   1847 
   1848 	com_common_putc(comconstag, comconsioh, c);
   1849 }
   1850 
   1851 void
   1852 comcnpollc(dev, on)
   1853 	dev_t dev;
   1854 	int on;
   1855 {
   1856 
   1857 }
   1858 
   1859 #ifdef KGDB
   1860 int
   1861 com_kgdb_attach(iot, iobase, rate, frequency, cflag)
   1862 	bus_space_tag_t iot;
   1863 	int iobase;
   1864 	int rate, frequency;
   1865 	tcflag_t cflag;
   1866 {
   1867 	int res;
   1868 
   1869 	if (iot == comconstag && iobase == comconsaddr)
   1870 		return (EBUSY); /* cannot share with console */
   1871 
   1872 	res = cominit(iot, iobase, rate, frequency, cflag, &com_kgdb_ioh);
   1873 	if (res)
   1874 		return (res);
   1875 
   1876 	kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
   1877 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   1878 
   1879 	com_kgdb_iot = iot;
   1880 	com_kgdb_addr = iobase;
   1881 
   1882 	return (0);
   1883 }
   1884 
   1885 /* ARGSUSED */
   1886 int
   1887 com_kgdb_getc(arg)
   1888 	void *arg;
   1889 {
   1890 
   1891 	return (com_common_getc(com_kgdb_iot, com_kgdb_ioh));
   1892 }
   1893 
   1894 /* ARGSUSED */
   1895 void
   1896 com_kgdb_putc(arg, c)
   1897 	void *arg;
   1898 	int c;
   1899 {
   1900 
   1901 	return (com_common_putc(com_kgdb_iot, com_kgdb_ioh, c));
   1902 }
   1903 #endif /* KGDB */
   1904 
   1905 /* helper function to identify the com ports used by
   1906  console or KGDB (and not yet autoconf attached) */
   1907 int
   1908 com_is_console(iot, iobase, ioh)
   1909 	bus_space_tag_t iot;
   1910 	int iobase;
   1911 	bus_space_handle_t *ioh;
   1912 {
   1913 	bus_space_handle_t help;
   1914 
   1915 	if (!comconsattached &&
   1916 	    iot == comconstag && iobase == comconsaddr)
   1917 		help = comconsioh;
   1918 #ifdef KGDB
   1919 	else if (!com_kgdb_attached &&
   1920 	    iot == com_kgdb_iot && iobase == com_kgdb_addr)
   1921 		help = com_kgdb_ioh;
   1922 #endif
   1923 	else
   1924 		return (0);
   1925 
   1926 	if (ioh)
   1927 		*ioh = help;
   1928 	return (1);
   1929 }
   1930