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