Home | History | Annotate | Line # | Download | only in dev
plcom.c revision 1.30
      1 /*	$NetBSD: plcom.c,v 1.30 2009/11/21 20:32:28 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 ARM Ltd
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the company may not be used to endorse or promote
     16  *    products derived from this software without specific prior written
     17  *    permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
     32  * All rights reserved.
     33  *
     34  * This code is derived from software contributed to The NetBSD Foundation
     35  * by Charles M. Hannum.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  *
     46  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     47  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     48  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     49  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     50  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     52  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     56  * POSSIBILITY OF SUCH DAMAGE.
     57  */
     58 
     59 /*
     60  * Copyright (c) 1991 The Regents of the University of California.
     61  * All rights reserved.
     62  *
     63  * Redistribution and use in source and binary forms, with or without
     64  * modification, are permitted provided that the following conditions
     65  * are met:
     66  * 1. Redistributions of source code must retain the above copyright
     67  *    notice, this list of conditions and the following disclaimer.
     68  * 2. Redistributions in binary form must reproduce the above copyright
     69  *    notice, this list of conditions and the following disclaimer in the
     70  *    documentation and/or other materials provided with the distribution.
     71  * 3. Neither the name of the University nor the names of its contributors
     72  *    may be used to endorse or promote products derived from this software
     73  *    without specific prior written permission.
     74  *
     75  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     76  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     77  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     78  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     79  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     80  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     81  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     82  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     83  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     84  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     85  * SUCH DAMAGE.
     86  *
     87  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     88  */
     89 
     90 /*
     91  * COM driver for the Prime Cell PL010 UART, which is similar to the 16C550,
     92  * but has a completely different programmer's model.
     93  * Derived from the NS16550AF com driver.
     94  */
     95 
     96 #include <sys/cdefs.h>
     97 __KERNEL_RCSID(0, "$NetBSD: plcom.c,v 1.30 2009/11/21 20:32:28 rmind Exp $");
     98 
     99 #include "opt_plcom.h"
    100 #include "opt_ddb.h"
    101 #include "opt_kgdb.h"
    102 #include "opt_lockdebug.h"
    103 #include "opt_multiprocessor.h"
    104 
    105 #include "rnd.h"
    106 #if NRND > 0 && defined(RND_COM)
    107 #include <sys/rnd.h>
    108 #endif
    109 
    110 /*
    111  * Override cnmagic(9) macro before including <sys/systm.h>.
    112  * We need to know if cn_check_magic triggered debugger, so set a flag.
    113  * Callers of cn_check_magic must declare int cn_trapped = 0;
    114  * XXX: this is *ugly*!
    115  */
    116 #define cn_trap()				\
    117 	do {					\
    118 		console_debugger();		\
    119 		cn_trapped = 1;			\
    120 	} while (/* CONSTCOND */ 0)
    121 
    122 #include <sys/param.h>
    123 #include <sys/systm.h>
    124 #include <sys/ioctl.h>
    125 #include <sys/select.h>
    126 #include <sys/tty.h>
    127 #include <sys/proc.h>
    128 #include <sys/conf.h>
    129 #include <sys/file.h>
    130 #include <sys/uio.h>
    131 #include <sys/kernel.h>
    132 #include <sys/syslog.h>
    133 #include <sys/types.h>
    134 #include <sys/device.h>
    135 #include <sys/malloc.h>
    136 #include <sys/timepps.h>
    137 #include <sys/vnode.h>
    138 #include <sys/kauth.h>
    139 #include <sys/intr.h>
    140 #include <sys/bus.h>
    141 
    142 #include <evbarm/dev/plcomreg.h>
    143 #include <evbarm/dev/plcomvar.h>
    144 
    145 #include <dev/cons.h>
    146 
    147 static void plcom_enable_debugport (struct plcom_softc *);
    148 
    149 void	plcom_config	(struct plcom_softc *);
    150 void	plcom_shutdown	(struct plcom_softc *);
    151 int	plcomspeed	(long, long);
    152 static	u_char	cflag2lcr (tcflag_t);
    153 int	plcomparam	(struct tty *, struct termios *);
    154 void	plcomstart	(struct tty *);
    155 int	plcomhwiflow	(struct tty *, int);
    156 
    157 void	plcom_loadchannelregs (struct plcom_softc *);
    158 void	plcom_hwiflow	(struct plcom_softc *);
    159 void	plcom_break	(struct plcom_softc *, int);
    160 void	plcom_modem	(struct plcom_softc *, int);
    161 void	tiocm_to_plcom	(struct plcom_softc *, u_long, int);
    162 int	plcom_to_tiocm	(struct plcom_softc *);
    163 void	plcom_iflush	(struct plcom_softc *);
    164 
    165 int	plcom_common_getc (dev_t, bus_space_tag_t, bus_space_handle_t);
    166 void	plcom_common_putc (dev_t, bus_space_tag_t, bus_space_handle_t, int);
    167 
    168 int	plcominit	(bus_space_tag_t, bus_addr_t, int, int, tcflag_t,
    169 			    bus_space_handle_t *);
    170 
    171 dev_type_open(plcomopen);
    172 dev_type_close(plcomclose);
    173 dev_type_read(plcomread);
    174 dev_type_write(plcomwrite);
    175 dev_type_ioctl(plcomioctl);
    176 dev_type_stop(plcomstop);
    177 dev_type_tty(plcomtty);
    178 dev_type_poll(plcompoll);
    179 
    180 int	plcomcngetc	(dev_t);
    181 void	plcomcnputc	(dev_t, int);
    182 void	plcomcnpollc	(dev_t, int);
    183 
    184 #define	integrate	static inline
    185 void 	plcomsoft	(void *);
    186 integrate void plcom_rxsoft	(struct plcom_softc *, struct tty *);
    187 integrate void plcom_txsoft	(struct plcom_softc *, struct tty *);
    188 integrate void plcom_stsoft	(struct plcom_softc *, struct tty *);
    189 integrate void plcom_schedrx	(struct plcom_softc *);
    190 void	plcomdiag		(void *);
    191 
    192 extern struct cfdriver plcom_cd;
    193 
    194 const struct cdevsw plcom_cdevsw = {
    195 	plcomopen, plcomclose, plcomread, plcomwrite, plcomioctl,
    196 	plcomstop, plcomtty, plcompoll, nommap, ttykqfilter, D_TTY
    197 };
    198 
    199 /*
    200  * Make this an option variable one can patch.
    201  * But be warned:  this must be a power of 2!
    202  */
    203 u_int plcom_rbuf_size = PLCOM_RING_SIZE;
    204 
    205 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    206 u_int plcom_rbuf_hiwat = (PLCOM_RING_SIZE * 1) / 4;
    207 u_int plcom_rbuf_lowat = (PLCOM_RING_SIZE * 3) / 4;
    208 
    209 static int	plcomconsunit = -1;
    210 static bus_space_tag_t plcomconstag;
    211 static bus_space_handle_t plcomconsioh;
    212 static int	plcomconsattached;
    213 static int plcomconsrate;
    214 static tcflag_t plcomconscflag;
    215 static struct cnm_state plcom_cnm_state;
    216 
    217 static int ppscap =
    218 	PPS_TSFMT_TSPEC |
    219 	PPS_CAPTUREASSERT |
    220 	PPS_CAPTURECLEAR |
    221 #ifdef  PPS_SYNC
    222 	PPS_HARDPPSONASSERT | PPS_HARDPPSONCLEAR |
    223 #endif	/* PPS_SYNC */
    224 	PPS_OFFSETASSERT | PPS_OFFSETCLEAR;
    225 
    226 #ifdef KGDB
    227 #include <sys/kgdb.h>
    228 
    229 static int plcom_kgdb_unit;
    230 static bus_space_tag_t plcom_kgdb_iot;
    231 static bus_space_handle_t plcom_kgdb_ioh;
    232 static int plcom_kgdb_attached;
    233 
    234 int	plcom_kgdb_getc (void *);
    235 void	plcom_kgdb_putc (void *, int);
    236 #endif /* KGDB */
    237 
    238 #define	PLCOMUNIT_MASK	0x7ffff
    239 #define	PLCOMDIALOUT_MASK	0x80000
    240 
    241 #define	PLCOMUNIT(x)	(minor(x) & PLCOMUNIT_MASK)
    242 #define	PLCOMDIALOUT(x)	(minor(x) & PLCOMDIALOUT_MASK)
    243 
    244 #define	PLCOM_ISALIVE(sc)	((sc)->enabled != 0 && \
    245 				 device_is_active(&(sc)->sc_dev))
    246 
    247 #define	BR	BUS_SPACE_BARRIER_READ
    248 #define	BW	BUS_SPACE_BARRIER_WRITE
    249 #define PLCOM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, PLCOM_UART_SIZE, (f))
    250 
    251 #define PLCOM_LOCK(sc) simple_lock(&(sc)->sc_lock)
    252 #define PLCOM_UNLOCK(sc) simple_unlock(&(sc)->sc_lock)
    253 
    254 int
    255 plcomspeed(long speed, long frequency)
    256 {
    257 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    258 
    259 	int x, err;
    260 
    261 #if 0
    262 	if (speed == 0)
    263 		return 0;
    264 #endif
    265 	if (speed <= 0)
    266 		return -1;
    267 	x = divrnd(frequency / 16, speed);
    268 	if (x <= 0)
    269 		return -1;
    270 	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
    271 	if (err < 0)
    272 		err = -err;
    273 	if (err > PLCOM_TOLERANCE)
    274 		return -1;
    275 	return x;
    276 
    277 #undef	divrnd
    278 }
    279 
    280 #ifdef PLCOM_DEBUG
    281 int	plcom_debug = 0;
    282 
    283 void plcomstatus (struct plcom_softc *, char *);
    284 void
    285 plcomstatus(struct plcom_softc *sc, char *str)
    286 {
    287 	struct tty *tp = sc->sc_tty;
    288 
    289 	printf("%s: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    290 	    sc->sc_dev.dv_xname, str,
    291 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    292 	    ISSET(sc->sc_msr, MSR_DCD) ? "+" : "-",
    293 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    294 	    ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
    295 	    sc->sc_tx_stopped ? "+" : "-");
    296 
    297 	printf("%s: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
    298 	    sc->sc_dev.dv_xname, str,
    299 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    300 	    ISSET(sc->sc_msr, MSR_CTS) ? "+" : "-",
    301 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    302 	    ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
    303 	    sc->sc_rx_flags);
    304 }
    305 #endif
    306 
    307 int
    308 plcomprobe1(bus_space_tag_t iot, bus_space_handle_t ioh)
    309 {
    310 	int data;
    311 
    312 	/* Disable the UART.  */
    313 	bus_space_write_1(iot, ioh, plcom_cr, 0);
    314 	/* Make sure the FIFO is off.  */
    315 	bus_space_write_1(iot, ioh, plcom_lcr, LCR_8BITS);
    316 	/* Disable interrupts.  */
    317 	bus_space_write_1(iot, ioh, plcom_iir, 0);
    318 
    319 	/* Make sure we swallow anything in the receiving register.  */
    320 	data = bus_space_read_1(iot, ioh, plcom_dr);
    321 
    322 	if (bus_space_read_1(iot, ioh, plcom_lcr) != LCR_8BITS)
    323 		return 0;
    324 
    325 	data = bus_space_read_1(iot, ioh, plcom_fr) & (FR_RXFF | FR_RXFE);
    326 
    327 	if (data != FR_RXFE)
    328 		return 0;
    329 
    330 	return 1;
    331 }
    332 
    333 static void
    334 plcom_enable_debugport(struct plcom_softc *sc)
    335 {
    336 	int s;
    337 
    338 	/* Turn on line break interrupt, set carrier. */
    339 	s = splserial();
    340 	PLCOM_LOCK(sc);
    341 	sc->sc_cr = CR_RIE | CR_RTIE | CR_UARTEN;
    342 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
    343 	SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
    344 	/* XXX device_unit() abuse */
    345 	sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
    346 	    sc->sc_mcr);
    347 	PLCOM_UNLOCK(sc);
    348 	splx(s);
    349 }
    350 
    351 void
    352 plcom_attach_subr(struct plcom_softc *sc)
    353 {
    354 	int unit = sc->sc_iounit;
    355 	bus_space_tag_t iot = sc->sc_iot;
    356 	bus_space_handle_t ioh = sc->sc_ioh;
    357 	struct tty *tp;
    358 
    359 	callout_init(&sc->sc_diag_callout, 0);
    360 	simple_lock_init(&sc->sc_lock);
    361 
    362 	/* Disable interrupts before configuring the device. */
    363 	sc->sc_cr = 0;
    364 
    365 	if (plcomconstag && unit == plcomconsunit) {
    366 		plcomconsattached = 1;
    367 
    368 		plcomconstag = iot;
    369 		plcomconsioh = ioh;
    370 
    371 		/* Make sure the console is always "hardwired". */
    372 		delay(1000);			/* wait for output to finish */
    373 		SET(sc->sc_hwflags, PLCOM_HW_CONSOLE);
    374 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    375 		/* Must re-enable the console immediately, or we will
    376 		   hang when trying to print.  */
    377 		sc->sc_cr = CR_UARTEN;
    378 	}
    379 
    380 	bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
    381 
    382 	/* The PL010 has a 16-byte fifo, but the tx interrupt triggers when
    383 	   there is space for 8 more bytes.  */
    384 	sc->sc_fifolen = 8;
    385 	printf("\n");
    386 
    387 	if (ISSET(sc->sc_hwflags, PLCOM_HW_TXFIFO_DISABLE)) {
    388 		sc->sc_fifolen = 1;
    389 		printf("%s: txfifo disabled\n", sc->sc_dev.dv_xname);
    390 	}
    391 
    392 	if (sc->sc_fifolen > 1)
    393 		SET(sc->sc_hwflags, PLCOM_HW_FIFO);
    394 
    395 	tp = ttymalloc();
    396 	tp->t_oproc = plcomstart;
    397 	tp->t_param = plcomparam;
    398 	tp->t_hwiflow = plcomhwiflow;
    399 
    400 	sc->sc_tty = tp;
    401 	sc->sc_rbuf = malloc(plcom_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    402 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    403 	sc->sc_rbavail = plcom_rbuf_size;
    404 	if (sc->sc_rbuf == NULL) {
    405 		printf("%s: unable to allocate ring buffer\n",
    406 		    sc->sc_dev.dv_xname);
    407 		return;
    408 	}
    409 	sc->sc_ebuf = sc->sc_rbuf + (plcom_rbuf_size << 1);
    410 
    411 	tty_attach(tp);
    412 
    413 	if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
    414 		int maj;
    415 
    416 		/* locate the major number */
    417 		maj = cdevsw_lookup_major(&plcom_cdevsw);
    418 
    419 		cn_tab->cn_dev = makedev(maj, device_unit(&sc->sc_dev));
    420 
    421 		printf("%s: console\n", sc->sc_dev.dv_xname);
    422 	}
    423 
    424 #ifdef KGDB
    425 	/*
    426 	 * Allow kgdb to "take over" this port.  If this is
    427 	 * the kgdb device, it has exclusive use.
    428 	 */
    429 	if (iot == plcom_kgdb_iot && unit == plcom_kgdb_unit) {
    430 		plcom_kgdb_attached = 1;
    431 
    432 		SET(sc->sc_hwflags, PLCOM_HW_KGDB);
    433 		printf("%s: kgdb\n", sc->sc_dev.dv_xname);
    434 	}
    435 #endif
    436 
    437 	sc->sc_si = softint_establish(SOFTINT_SERIAL, plcomsoft, sc);
    438 
    439 #if NRND > 0 && defined(RND_COM)
    440 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    441 			  RND_TYPE_TTY, 0);
    442 #endif
    443 
    444 	/* if there are no enable/disable functions, assume the device
    445 	   is always enabled */
    446 	if (!sc->enable)
    447 		sc->enabled = 1;
    448 
    449 	plcom_config(sc);
    450 
    451 	SET(sc->sc_hwflags, PLCOM_HW_DEV_OK);
    452 }
    453 
    454 void
    455 plcom_config(struct plcom_softc *sc)
    456 {
    457 	bus_space_tag_t iot = sc->sc_iot;
    458 	bus_space_handle_t ioh = sc->sc_ioh;
    459 
    460 	/* Disable interrupts before configuring the device. */
    461 	sc->sc_cr = 0;
    462 	bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
    463 
    464 	if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE|PLCOM_HW_KGDB))
    465 		plcom_enable_debugport(sc);
    466 }
    467 
    468 int
    469 plcom_detach(struct device *self, int flags)
    470 {
    471 	struct plcom_softc *sc = (struct plcom_softc *)self;
    472 	int maj, mn;
    473 
    474 	/* locate the major number */
    475 	maj = cdevsw_lookup_major(&plcom_cdevsw);
    476 
    477 	/* Nuke the vnodes for any open instances. */
    478 	mn = device_unit(self);
    479 	vdevgone(maj, mn, mn, VCHR);
    480 
    481 	mn |= PLCOMDIALOUT_MASK;
    482 	vdevgone(maj, mn, mn, VCHR);
    483 
    484 	/* Free the receive buffer. */
    485 	free(sc->sc_rbuf, M_DEVBUF);
    486 
    487 	/* Detach and free the tty. */
    488 	tty_detach(sc->sc_tty);
    489 	ttyfree(sc->sc_tty);
    490 
    491 	/* Unhook the soft interrupt handler. */
    492 	softint_disestablish(sc->sc_si);
    493 
    494 #if NRND > 0 && defined(RND_COM)
    495 	/* Unhook the entropy source. */
    496 	rnd_detach_source(&sc->rnd_source);
    497 #endif
    498 
    499 	return 0;
    500 }
    501 
    502 int
    503 plcom_activate(struct device *self, enum devact act)
    504 {
    505 	struct plcom_softc *sc = (struct plcom_softc *)self;
    506 	int s, rv = 0;
    507 
    508 	s = splserial();
    509 	PLCOM_LOCK(sc);
    510 	switch (act) {
    511 	case DVACT_ACTIVATE:
    512 		rv = EOPNOTSUPP;
    513 		break;
    514 
    515 	case DVACT_DEACTIVATE:
    516 		if (sc->sc_hwflags & (PLCOM_HW_CONSOLE|PLCOM_HW_KGDB)) {
    517 			rv = EBUSY;
    518 			break;
    519 		}
    520 
    521 		if (sc->disable != NULL && sc->enabled != 0) {
    522 			(*sc->disable)(sc);
    523 			sc->enabled = 0;
    524 		}
    525 		break;
    526 	}
    527 
    528 	PLCOM_UNLOCK(sc);
    529 	splx(s);
    530 	return rv;
    531 }
    532 
    533 void
    534 plcom_shutdown(struct plcom_softc *sc)
    535 {
    536 	struct tty *tp = sc->sc_tty;
    537 	int s;
    538 
    539 	s = splserial();
    540 	PLCOM_LOCK(sc);
    541 
    542 	/* If we were asserting flow control, then deassert it. */
    543 	SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
    544 	plcom_hwiflow(sc);
    545 
    546 	/* Clear any break condition set with TIOCSBRK. */
    547 	plcom_break(sc, 0);
    548 
    549 	/* Turn off PPS capture on last close. */
    550 	mutex_spin_enter(&timecounter_lock);
    551 	sc->sc_ppsmask = 0;
    552 	sc->ppsparam.mode = 0;
    553 	mutex_spin_exit(&timecounter_lock);
    554 
    555 	/*
    556 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    557 	 * notice even if we immediately open the port again.
    558 	 * Avoid tsleeping above splhigh().
    559 	 */
    560 	if (ISSET(tp->t_cflag, HUPCL)) {
    561 		plcom_modem(sc, 0);
    562 		PLCOM_UNLOCK(sc);
    563 		splx(s);
    564 		/* XXX tsleep will only timeout */
    565 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    566 		s = splserial();
    567 		PLCOM_LOCK(sc);
    568 	}
    569 
    570 	/* Turn off interrupts. */
    571 	if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE))
    572 		/* interrupt on break */
    573 		sc->sc_cr = CR_RIE | CR_RTIE | CR_UARTEN;
    574 	else
    575 		sc->sc_cr = 0;
    576 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
    577 
    578 	if (sc->disable) {
    579 #ifdef DIAGNOSTIC
    580 		if (!sc->enabled)
    581 			panic("plcom_shutdown: not enabled?");
    582 #endif
    583 		(*sc->disable)(sc);
    584 		sc->enabled = 0;
    585 	}
    586 	PLCOM_UNLOCK(sc);
    587 	splx(s);
    588 }
    589 
    590 int
    591 plcomopen(dev_t dev, int flag, int mode, struct lwp *l)
    592 {
    593 	struct plcom_softc *sc;
    594 	struct tty *tp;
    595 	int s, s2;
    596 	int error;
    597 
    598 	sc = device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    599 	if (sc == NULL || !ISSET(sc->sc_hwflags, PLCOM_HW_DEV_OK) ||
    600 		sc->sc_rbuf == NULL)
    601 		return ENXIO;
    602 
    603 	if (!device_is_active(&sc->sc_dev))
    604 		return ENXIO;
    605 
    606 #ifdef KGDB
    607 	/*
    608 	 * If this is the kgdb port, no other use is permitted.
    609 	 */
    610 	if (ISSET(sc->sc_hwflags, PLCOM_HW_KGDB))
    611 		return EBUSY;
    612 #endif
    613 
    614 	tp = sc->sc_tty;
    615 
    616 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    617 		return (EBUSY);
    618 
    619 	s = spltty();
    620 
    621 	/*
    622 	 * Do the following iff this is a first open.
    623 	 */
    624 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    625 		struct termios t;
    626 
    627 		tp->t_dev = dev;
    628 
    629 		s2 = splserial();
    630 		PLCOM_LOCK(sc);
    631 
    632 		if (sc->enable) {
    633 			if ((*sc->enable)(sc)) {
    634 				PLCOM_UNLOCK(sc);
    635 				splx(s2);
    636 				splx(s);
    637 				printf("%s: device enable failed\n",
    638 				       sc->sc_dev.dv_xname);
    639 				return EIO;
    640 			}
    641 			sc->enabled = 1;
    642 			plcom_config(sc);
    643 		}
    644 
    645 		/* Turn on interrupts. */
    646 		/* IER_ERXRDY | IER_ERLS | IER_EMSC;  */
    647 		sc->sc_cr = CR_RIE | CR_RTIE | CR_MSIE | CR_UARTEN;
    648 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
    649 
    650 		/* Fetch the current modem control status, needed later. */
    651 		sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, plcom_fr);
    652 
    653 		/* Clear PPS capture state on first open. */
    654 
    655 		mutex_spin_enter(&timecounter_lock);
    656 		sc->sc_ppsmask = 0;
    657 		sc->ppsparam.mode = 0;
    658 		mutex_spin_exit(&timecounter_lock);
    659 
    660 		PLCOM_UNLOCK(sc);
    661 		splx(s2);
    662 
    663 		/*
    664 		 * Initialize the termios status to the defaults.  Add in the
    665 		 * sticky bits from TIOCSFLAGS.
    666 		 */
    667 		t.c_ispeed = 0;
    668 		if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
    669 			t.c_ospeed = plcomconsrate;
    670 			t.c_cflag = plcomconscflag;
    671 		} else {
    672 			t.c_ospeed = TTYDEF_SPEED;
    673 			t.c_cflag = TTYDEF_CFLAG;
    674 		}
    675 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    676 			SET(t.c_cflag, CLOCAL);
    677 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    678 			SET(t.c_cflag, CRTSCTS);
    679 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    680 			SET(t.c_cflag, MDMBUF);
    681 		/* Make sure plcomparam() will do something. */
    682 		tp->t_ospeed = 0;
    683 		(void) plcomparam(tp, &t);
    684 		tp->t_iflag = TTYDEF_IFLAG;
    685 		tp->t_oflag = TTYDEF_OFLAG;
    686 		tp->t_lflag = TTYDEF_LFLAG;
    687 		ttychars(tp);
    688 		ttsetwater(tp);
    689 
    690 		s2 = splserial();
    691 		PLCOM_LOCK(sc);
    692 
    693 		/*
    694 		 * Turn on DTR.  We must always do this, even if carrier is not
    695 		 * present, because otherwise we'd have to use TIOCSDTR
    696 		 * immediately after setting CLOCAL, which applications do not
    697 		 * expect.  We always assert DTR while the device is open
    698 		 * unless explicitly requested to deassert it.
    699 		 */
    700 		plcom_modem(sc, 1);
    701 
    702 		/* Clear the input ring, and unblock. */
    703 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    704 		sc->sc_rbavail = plcom_rbuf_size;
    705 		plcom_iflush(sc);
    706 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    707 		plcom_hwiflow(sc);
    708 
    709 #ifdef PLCOM_DEBUG
    710 		if (plcom_debug)
    711 			plcomstatus(sc, "plcomopen  ");
    712 #endif
    713 
    714 		PLCOM_UNLOCK(sc);
    715 		splx(s2);
    716 	}
    717 
    718 	splx(s);
    719 
    720 	error = ttyopen(tp, PLCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    721 	if (error)
    722 		goto bad;
    723 
    724 	error = (*tp->t_linesw->l_open)(dev, tp);
    725 	if (error)
    726 		goto bad;
    727 
    728 	return 0;
    729 
    730 bad:
    731 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    732 		/*
    733 		 * We failed to open the device, and nobody else had it opened.
    734 		 * Clean up the state as appropriate.
    735 		 */
    736 		plcom_shutdown(sc);
    737 	}
    738 
    739 	return error;
    740 }
    741 
    742 int
    743 plcomclose(dev_t dev, int flag, int mode, struct lwp *l)
    744 {
    745 	struct plcom_softc *sc =
    746 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    747 	struct tty *tp = sc->sc_tty;
    748 
    749 	/* XXX This is for cons.c. */
    750 	if (!ISSET(tp->t_state, TS_ISOPEN))
    751 		return 0;
    752 
    753 	(*tp->t_linesw->l_close)(tp, flag);
    754 	ttyclose(tp);
    755 
    756 	if (PLCOM_ISALIVE(sc) == 0)
    757 		return 0;
    758 
    759 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    760 		/*
    761 		 * Although we got a last close, the device may still be in
    762 		 * use; e.g. if this was the dialout node, and there are still
    763 		 * processes waiting for carrier on the non-dialout node.
    764 		 */
    765 		plcom_shutdown(sc);
    766 	}
    767 
    768 	return 0;
    769 }
    770 
    771 int
    772 plcomread(dev_t dev, struct uio *uio, int flag)
    773 {
    774 	struct plcom_softc *sc =
    775 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    776 	struct tty *tp = sc->sc_tty;
    777 
    778 	if (PLCOM_ISALIVE(sc) == 0)
    779 		return EIO;
    780 
    781 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    782 }
    783 
    784 int
    785 plcomwrite(dev_t dev, struct uio *uio, int flag)
    786 {
    787 	struct plcom_softc *sc =
    788 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    789 	struct tty *tp = sc->sc_tty;
    790 
    791 	if (PLCOM_ISALIVE(sc) == 0)
    792 		return EIO;
    793 
    794 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    795 }
    796 
    797 int
    798 plcompoll(dev_t dev, int events, struct lwp *l)
    799 {
    800 	struct plcom_softc *sc =
    801 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    802 	struct tty *tp = sc->sc_tty;
    803 
    804 	if (PLCOM_ISALIVE(sc) == 0)
    805 		return EIO;
    806 
    807 	return (*tp->t_linesw->l_poll)(tp, events, l);
    808 }
    809 
    810 struct tty *
    811 plcomtty(dev_t dev)
    812 {
    813 	struct plcom_softc *sc =
    814 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    815 	struct tty *tp = sc->sc_tty;
    816 
    817 	return tp;
    818 }
    819 
    820 int
    821 plcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    822 {
    823 	struct plcom_softc *sc =
    824 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    825 	struct tty *tp = sc->sc_tty;
    826 	int error;
    827 	int s;
    828 
    829 	if (PLCOM_ISALIVE(sc) == 0)
    830 		return EIO;
    831 
    832 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    833 	if (error != EPASSTHROUGH)
    834 		return error;
    835 
    836 	error = ttioctl(tp, cmd, data, flag, l);
    837 	if (error != EPASSTHROUGH)
    838 		return error;
    839 
    840 	error = 0;
    841 
    842 	s = splserial();
    843 	PLCOM_LOCK(sc);
    844 
    845 	switch (cmd) {
    846 	case TIOCSBRK:
    847 		plcom_break(sc, 1);
    848 		break;
    849 
    850 	case TIOCCBRK:
    851 		plcom_break(sc, 0);
    852 		break;
    853 
    854 	case TIOCSDTR:
    855 		plcom_modem(sc, 1);
    856 		break;
    857 
    858 	case TIOCCDTR:
    859 		plcom_modem(sc, 0);
    860 		break;
    861 
    862 	case TIOCGFLAGS:
    863 		*(int *)data = sc->sc_swflags;
    864 		break;
    865 
    866 	case TIOCSFLAGS:
    867 		error = kauth_authorize_device_tty(l->l_cred,
    868 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    869 		if (error)
    870 			break;
    871 		sc->sc_swflags = *(int *)data;
    872 		break;
    873 
    874 	case TIOCMSET:
    875 	case TIOCMBIS:
    876 	case TIOCMBIC:
    877 		tiocm_to_plcom(sc, cmd, *(int *)data);
    878 		break;
    879 
    880 	case TIOCMGET:
    881 		*(int *)data = plcom_to_tiocm(sc);
    882 		break;
    883 
    884 	case PPS_IOC_CREATE:
    885 		break;
    886 
    887 	case PPS_IOC_DESTROY:
    888 		break;
    889 
    890 	case PPS_IOC_GETPARAMS: {
    891 		pps_params_t *pp;
    892 		pp = (pps_params_t *)data;
    893 		mutex_spin_enter(&timecounter_lock);
    894 		*pp = sc->ppsparam;
    895 		mutex_spin_exit(&timecounter_lock);
    896 		break;
    897 	}
    898 
    899 	case PPS_IOC_SETPARAMS: {
    900 	  	pps_params_t *pp;
    901 		int mode;
    902 		pp = (pps_params_t *)data;
    903 		mutex_spin_enter(&timecounter_lock);
    904 		if (pp->mode & ~ppscap) {
    905 			error = EINVAL;
    906 			mutex_spin_exit(&timecounter_lock);
    907 			break;
    908 		}
    909 		sc->ppsparam = *pp;
    910 	 	/*
    911 		 * Compute msr masks from user-specified timestamp state.
    912 		 */
    913 		mode = sc->ppsparam.mode;
    914 #ifdef	PPS_SYNC
    915 		if (mode & PPS_HARDPPSONASSERT) {
    916 			mode |= PPS_CAPTUREASSERT;
    917 			/* XXX revoke any previous HARDPPS source */
    918 		}
    919 		if (mode & PPS_HARDPPSONCLEAR) {
    920 			mode |= PPS_CAPTURECLEAR;
    921 			/* XXX revoke any previous HARDPPS source */
    922 		}
    923 #endif	/* PPS_SYNC */
    924 		switch (mode & PPS_CAPTUREBOTH) {
    925 		case 0:
    926 			sc->sc_ppsmask = 0;
    927 			break;
    928 
    929 		case PPS_CAPTUREASSERT:
    930 			sc->sc_ppsmask = MSR_DCD;
    931 			sc->sc_ppsassert = MSR_DCD;
    932 			sc->sc_ppsclear = -1;
    933 			break;
    934 
    935 		case PPS_CAPTURECLEAR:
    936 			sc->sc_ppsmask = MSR_DCD;
    937 			sc->sc_ppsassert = -1;
    938 			sc->sc_ppsclear = 0;
    939 			break;
    940 
    941 		case PPS_CAPTUREBOTH:
    942 			sc->sc_ppsmask = MSR_DCD;
    943 			sc->sc_ppsassert = MSR_DCD;
    944 			sc->sc_ppsclear = 0;
    945 			break;
    946 
    947 		default:
    948 			error = EINVAL;
    949 			break;
    950 		}
    951 		mutex_spin_exit(&timecounter_lock);
    952 		break;
    953 	}
    954 
    955 	case PPS_IOC_GETCAP:
    956 		*(int*)data = ppscap;
    957 		break;
    958 
    959 	case PPS_IOC_FETCH: {
    960 		pps_info_t *pi;
    961 		pi = (pps_info_t *)data;
    962 		mutex_spin_enter(&timecounter_lock);
    963 		*pi = sc->ppsinfo;
    964 		mutex_spin_exit(&timecounter_lock);
    965 		break;
    966 	}
    967 
    968 	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
    969 		/*
    970 		 * Some GPS clocks models use the falling rather than
    971 		 * rising edge as the on-the-second signal.
    972 		 * The old API has no way to specify PPS polarity.
    973 		 */
    974 		mutex_spin_enter(&timecounter_lock);
    975 		sc->sc_ppsmask = MSR_DCD;
    976 #ifndef PPS_TRAILING_EDGE
    977 		sc->sc_ppsassert = MSR_DCD;
    978 		sc->sc_ppsclear = -1;
    979 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
    980 		    &sc->ppsinfo.assert_timestamp);
    981 #else
    982 		sc->sc_ppsassert = -1
    983 		sc->sc_ppsclear = 0;
    984 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
    985 		    &sc->ppsinfo.clear_timestamp);
    986 #endif
    987 		mutex_spin_exit(&timecounter_lock);
    988 		break;
    989 
    990 	default:
    991 		error = EPASSTHROUGH;
    992 		break;
    993 	}
    994 
    995 	PLCOM_UNLOCK(sc);
    996 	splx(s);
    997 
    998 #ifdef PLCOM_DEBUG
    999 	if (plcom_debug)
   1000 		plcomstatus(sc, "plcomioctl ");
   1001 #endif
   1002 
   1003 	return error;
   1004 }
   1005 
   1006 integrate void
   1007 plcom_schedrx(struct plcom_softc *sc)
   1008 {
   1009 
   1010 	sc->sc_rx_ready = 1;
   1011 
   1012 	/* Wake up the poller. */
   1013 	softint_schedule(sc->sc_si);
   1014 }
   1015 
   1016 void
   1017 plcom_break(struct plcom_softc *sc, int onoff)
   1018 {
   1019 
   1020 	if (onoff)
   1021 		SET(sc->sc_lcr, LCR_BRK);
   1022 	else
   1023 		CLR(sc->sc_lcr, LCR_BRK);
   1024 
   1025 	if (!sc->sc_heldchange) {
   1026 		if (sc->sc_tx_busy) {
   1027 			sc->sc_heldtbc = sc->sc_tbc;
   1028 			sc->sc_tbc = 0;
   1029 			sc->sc_heldchange = 1;
   1030 		} else
   1031 			plcom_loadchannelregs(sc);
   1032 	}
   1033 }
   1034 
   1035 void
   1036 plcom_modem(struct plcom_softc *sc, int onoff)
   1037 {
   1038 
   1039 	if (sc->sc_mcr_dtr == 0)
   1040 		return;
   1041 
   1042 	if (onoff)
   1043 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1044 	else
   1045 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1046 
   1047 	if (!sc->sc_heldchange) {
   1048 		if (sc->sc_tx_busy) {
   1049 			sc->sc_heldtbc = sc->sc_tbc;
   1050 			sc->sc_tbc = 0;
   1051 			sc->sc_heldchange = 1;
   1052 		} else
   1053 			plcom_loadchannelregs(sc);
   1054 	}
   1055 }
   1056 
   1057 void
   1058 tiocm_to_plcom(struct plcom_softc *sc, u_long how, int ttybits)
   1059 {
   1060 	u_char plcombits;
   1061 
   1062 	plcombits = 0;
   1063 	if (ISSET(ttybits, TIOCM_DTR))
   1064 		SET(plcombits, MCR_DTR);
   1065 	if (ISSET(ttybits, TIOCM_RTS))
   1066 		SET(plcombits, MCR_RTS);
   1067 
   1068 	switch (how) {
   1069 	case TIOCMBIC:
   1070 		CLR(sc->sc_mcr, plcombits);
   1071 		break;
   1072 
   1073 	case TIOCMBIS:
   1074 		SET(sc->sc_mcr, plcombits);
   1075 		break;
   1076 
   1077 	case TIOCMSET:
   1078 		CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
   1079 		SET(sc->sc_mcr, plcombits);
   1080 		break;
   1081 	}
   1082 
   1083 	if (!sc->sc_heldchange) {
   1084 		if (sc->sc_tx_busy) {
   1085 			sc->sc_heldtbc = sc->sc_tbc;
   1086 			sc->sc_tbc = 0;
   1087 			sc->sc_heldchange = 1;
   1088 		} else
   1089 			plcom_loadchannelregs(sc);
   1090 	}
   1091 }
   1092 
   1093 int
   1094 plcom_to_tiocm(struct plcom_softc *sc)
   1095 {
   1096 	u_char plcombits;
   1097 	int ttybits = 0;
   1098 
   1099 	plcombits = sc->sc_mcr;
   1100 	if (ISSET(plcombits, MCR_DTR))
   1101 		SET(ttybits, TIOCM_DTR);
   1102 	if (ISSET(plcombits, MCR_RTS))
   1103 		SET(ttybits, TIOCM_RTS);
   1104 
   1105 	plcombits = sc->sc_msr;
   1106 	if (ISSET(plcombits, MSR_DCD))
   1107 		SET(ttybits, TIOCM_CD);
   1108 	if (ISSET(plcombits, MSR_CTS))
   1109 		SET(ttybits, TIOCM_CTS);
   1110 	if (ISSET(plcombits, MSR_DSR))
   1111 		SET(ttybits, TIOCM_DSR);
   1112 
   1113 	if (sc->sc_cr != 0)
   1114 		SET(ttybits, TIOCM_LE);
   1115 
   1116 	return ttybits;
   1117 }
   1118 
   1119 static u_char
   1120 cflag2lcr(tcflag_t cflag)
   1121 {
   1122 	u_char lcr = 0;
   1123 
   1124 	switch (ISSET(cflag, CSIZE)) {
   1125 	case CS5:
   1126 		SET(lcr, LCR_5BITS);
   1127 		break;
   1128 	case CS6:
   1129 		SET(lcr, LCR_6BITS);
   1130 		break;
   1131 	case CS7:
   1132 		SET(lcr, LCR_7BITS);
   1133 		break;
   1134 	case CS8:
   1135 		SET(lcr, LCR_8BITS);
   1136 		break;
   1137 	}
   1138 	if (ISSET(cflag, PARENB)) {
   1139 		SET(lcr, LCR_PEN);
   1140 		if (!ISSET(cflag, PARODD))
   1141 			SET(lcr, LCR_EPS);
   1142 	}
   1143 	if (ISSET(cflag, CSTOPB))
   1144 		SET(lcr, LCR_STP2);
   1145 
   1146 	return lcr;
   1147 }
   1148 
   1149 int
   1150 plcomparam(struct tty *tp, struct termios *t)
   1151 {
   1152 	struct plcom_softc *sc =
   1153 		device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
   1154 	int ospeed;
   1155 	u_char lcr;
   1156 	int s;
   1157 
   1158 	if (PLCOM_ISALIVE(sc) == 0)
   1159 		return EIO;
   1160 
   1161 	ospeed = plcomspeed(t->c_ospeed, sc->sc_frequency);
   1162 
   1163 	/* Check requested parameters. */
   1164 	if (ospeed < 0)
   1165 		return EINVAL;
   1166 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1167 		return EINVAL;
   1168 
   1169 	/*
   1170 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1171 	 * is always active.
   1172 	 */
   1173 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
   1174 	    ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
   1175 		SET(t->c_cflag, CLOCAL);
   1176 		CLR(t->c_cflag, HUPCL);
   1177 	}
   1178 
   1179 	/*
   1180 	 * If there were no changes, don't do anything.  This avoids dropping
   1181 	 * input and improves performance when all we did was frob things like
   1182 	 * VMIN and VTIME.
   1183 	 */
   1184 	if (tp->t_ospeed == t->c_ospeed &&
   1185 	    tp->t_cflag == t->c_cflag)
   1186 		return 0;
   1187 
   1188 	lcr = ISSET(sc->sc_lcr, LCR_BRK) | cflag2lcr(t->c_cflag);
   1189 
   1190 	s = splserial();
   1191 	PLCOM_LOCK(sc);
   1192 
   1193 	sc->sc_lcr = lcr;
   1194 
   1195 	/*
   1196 	 * PL010 has a fixed-length FIFO trigger point.
   1197 	 */
   1198 	if (ISSET(sc->sc_hwflags, PLCOM_HW_FIFO))
   1199 		sc->sc_fifo = 1;
   1200 	else
   1201 		sc->sc_fifo = 0;
   1202 
   1203 	if (sc->sc_fifo)
   1204 		SET(sc->sc_lcr, LCR_FEN);
   1205 
   1206 	/*
   1207 	 * If we're not in a mode that assumes a connection is present, then
   1208 	 * ignore carrier changes.
   1209 	 */
   1210 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1211 		sc->sc_msr_dcd = 0;
   1212 	else
   1213 		sc->sc_msr_dcd = MSR_DCD;
   1214 	/*
   1215 	 * Set the flow control pins depending on the current flow control
   1216 	 * mode.
   1217 	 */
   1218 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1219 		sc->sc_mcr_dtr = MCR_DTR;
   1220 		sc->sc_mcr_rts = MCR_RTS;
   1221 		sc->sc_msr_cts = MSR_CTS;
   1222 	} else if (ISSET(t->c_cflag, MDMBUF)) {
   1223 		/*
   1224 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1225 		 * carrier detection.
   1226 		 */
   1227 		sc->sc_mcr_dtr = 0;
   1228 		sc->sc_mcr_rts = MCR_DTR;
   1229 		sc->sc_msr_cts = MSR_DCD;
   1230 	} else {
   1231 		/*
   1232 		 * If no flow control, then always set RTS.  This will make
   1233 		 * the other side happy if it mistakenly thinks we're doing
   1234 		 * RTS/CTS flow control.
   1235 		 */
   1236 		sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
   1237 		sc->sc_mcr_rts = 0;
   1238 		sc->sc_msr_cts = 0;
   1239 		if (ISSET(sc->sc_mcr, MCR_DTR))
   1240 			SET(sc->sc_mcr, MCR_RTS);
   1241 		else
   1242 			CLR(sc->sc_mcr, MCR_RTS);
   1243 	}
   1244 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
   1245 
   1246 #if 0
   1247 	if (ospeed == 0)
   1248 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1249 	else
   1250 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1251 #endif
   1252 
   1253 	sc->sc_dlbl = ospeed;
   1254 	sc->sc_dlbh = ospeed >> 8;
   1255 
   1256 	/* And copy to tty. */
   1257 	tp->t_ispeed = 0;
   1258 	tp->t_ospeed = t->c_ospeed;
   1259 	tp->t_cflag = t->c_cflag;
   1260 
   1261 	if (!sc->sc_heldchange) {
   1262 		if (sc->sc_tx_busy) {
   1263 			sc->sc_heldtbc = sc->sc_tbc;
   1264 			sc->sc_tbc = 0;
   1265 			sc->sc_heldchange = 1;
   1266 		} else
   1267 			plcom_loadchannelregs(sc);
   1268 	}
   1269 
   1270 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1271 		/* Disable the high water mark. */
   1272 		sc->sc_r_hiwat = 0;
   1273 		sc->sc_r_lowat = 0;
   1274 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1275 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1276 			plcom_schedrx(sc);
   1277 		}
   1278 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1279 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1280 			plcom_hwiflow(sc);
   1281 		}
   1282 	} else {
   1283 		sc->sc_r_hiwat = plcom_rbuf_hiwat;
   1284 		sc->sc_r_lowat = plcom_rbuf_lowat;
   1285 	}
   1286 
   1287 	PLCOM_UNLOCK(sc);
   1288 	splx(s);
   1289 
   1290 	/*
   1291 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1292 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1293 	 * explicit request.
   1294 	 */
   1295 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
   1296 
   1297 #ifdef PLCOM_DEBUG
   1298 	if (plcom_debug)
   1299 		plcomstatus(sc, "plcomparam ");
   1300 #endif
   1301 
   1302 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1303 		if (sc->sc_tx_stopped) {
   1304 			sc->sc_tx_stopped = 0;
   1305 			plcomstart(tp);
   1306 		}
   1307 	}
   1308 
   1309 	return 0;
   1310 }
   1311 
   1312 void
   1313 plcom_iflush(struct plcom_softc *sc)
   1314 {
   1315 	bus_space_tag_t iot = sc->sc_iot;
   1316 	bus_space_handle_t ioh = sc->sc_ioh;
   1317 #ifdef DIAGNOSTIC
   1318 	int reg;
   1319 #endif
   1320 	int timo;
   1321 
   1322 #ifdef DIAGNOSTIC
   1323 	reg = 0xffff;
   1324 #endif
   1325 	timo = 50000;
   1326 	/* flush any pending I/O */
   1327 	while (! ISSET(bus_space_read_1(iot, ioh, plcom_fr), FR_RXFE)
   1328 	    && --timo)
   1329 #ifdef DIAGNOSTIC
   1330 		reg =
   1331 #else
   1332 		    (void)
   1333 #endif
   1334 		    bus_space_read_1(iot, ioh, plcom_dr);
   1335 #ifdef DIAGNOSTIC
   1336 	if (!timo)
   1337 		printf("%s: plcom_iflush timeout %02x\n", sc->sc_dev.dv_xname,
   1338 		       reg);
   1339 #endif
   1340 }
   1341 
   1342 void
   1343 plcom_loadchannelregs(struct plcom_softc *sc)
   1344 {
   1345 	bus_space_tag_t iot = sc->sc_iot;
   1346 	bus_space_handle_t ioh = sc->sc_ioh;
   1347 
   1348 	/* XXXXX necessary? */
   1349 	plcom_iflush(sc);
   1350 
   1351 	bus_space_write_1(iot, ioh, plcom_cr, 0);
   1352 
   1353 	bus_space_write_1(iot, ioh, plcom_dlbl, sc->sc_dlbl);
   1354 	bus_space_write_1(iot, ioh, plcom_dlbh, sc->sc_dlbh);
   1355 	bus_space_write_1(iot, ioh, plcom_lcr, sc->sc_lcr);
   1356 	/* XXX device_unit() abuse */
   1357 	sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
   1358 	    sc->sc_mcr_active = sc->sc_mcr);
   1359 
   1360 	bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
   1361 }
   1362 
   1363 int
   1364 plcomhwiflow(struct tty *tp, int block)
   1365 {
   1366 	struct plcom_softc *sc =
   1367 		device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
   1368 	int s;
   1369 
   1370 	if (PLCOM_ISALIVE(sc) == 0)
   1371 		return 0;
   1372 
   1373 	if (sc->sc_mcr_rts == 0)
   1374 		return 0;
   1375 
   1376 	s = splserial();
   1377 	PLCOM_LOCK(sc);
   1378 
   1379 	if (block) {
   1380 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1381 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1382 			plcom_hwiflow(sc);
   1383 		}
   1384 	} else {
   1385 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1386 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1387 			plcom_schedrx(sc);
   1388 		}
   1389 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1390 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1391 			plcom_hwiflow(sc);
   1392 		}
   1393 	}
   1394 
   1395 	PLCOM_UNLOCK(sc);
   1396 	splx(s);
   1397 	return 1;
   1398 }
   1399 
   1400 /*
   1401  * (un)block input via hw flowcontrol
   1402  */
   1403 void
   1404 plcom_hwiflow(struct plcom_softc *sc)
   1405 {
   1406 	if (sc->sc_mcr_rts == 0)
   1407 		return;
   1408 
   1409 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
   1410 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
   1411 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
   1412 	} else {
   1413 		SET(sc->sc_mcr, sc->sc_mcr_rts);
   1414 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
   1415 	}
   1416 	/* XXX device_unit() abuse */
   1417 	sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
   1418 	    sc->sc_mcr_active);
   1419 }
   1420 
   1421 
   1422 void
   1423 plcomstart(struct tty *tp)
   1424 {
   1425 	struct plcom_softc *sc =
   1426 		device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
   1427 	bus_space_tag_t iot = sc->sc_iot;
   1428 	bus_space_handle_t ioh = sc->sc_ioh;
   1429 	int s;
   1430 
   1431 	if (PLCOM_ISALIVE(sc) == 0)
   1432 		return;
   1433 
   1434 	s = spltty();
   1435 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1436 		goto out;
   1437 	if (sc->sc_tx_stopped)
   1438 		goto out;
   1439 
   1440 	if (!ttypull(tp))
   1441 		goto out;
   1442 
   1443 	/* Grab the first contiguous region of buffer space. */
   1444 	{
   1445 		u_char *tba;
   1446 		int tbc;
   1447 
   1448 		tba = tp->t_outq.c_cf;
   1449 		tbc = ndqb(&tp->t_outq, 0);
   1450 
   1451 		(void)splserial();
   1452 		PLCOM_LOCK(sc);
   1453 
   1454 		sc->sc_tba = tba;
   1455 		sc->sc_tbc = tbc;
   1456 	}
   1457 
   1458 	SET(tp->t_state, TS_BUSY);
   1459 	sc->sc_tx_busy = 1;
   1460 
   1461 	/* Enable transmit completion interrupts if necessary. */
   1462 	if (!ISSET(sc->sc_cr, CR_TIE)) {
   1463 		SET(sc->sc_cr, CR_TIE);
   1464 		bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
   1465 	}
   1466 
   1467 	/* Output the first chunk of the contiguous buffer. */
   1468 	{
   1469 		int n;
   1470 
   1471 		n = sc->sc_tbc;
   1472 		if (n > sc->sc_fifolen)
   1473 			n = sc->sc_fifolen;
   1474 		bus_space_write_multi_1(iot, ioh, plcom_dr, sc->sc_tba, n);
   1475 		sc->sc_tbc -= n;
   1476 		sc->sc_tba += n;
   1477 	}
   1478 	PLCOM_UNLOCK(sc);
   1479 out:
   1480 	splx(s);
   1481 	return;
   1482 }
   1483 
   1484 /*
   1485  * Stop output on a line.
   1486  */
   1487 void
   1488 plcomstop(struct tty *tp, int flag)
   1489 {
   1490 	struct plcom_softc *sc =
   1491 		device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
   1492 	int s;
   1493 
   1494 	s = splserial();
   1495 	PLCOM_LOCK(sc);
   1496 	if (ISSET(tp->t_state, TS_BUSY)) {
   1497 		/* Stop transmitting at the next chunk. */
   1498 		sc->sc_tbc = 0;
   1499 		sc->sc_heldtbc = 0;
   1500 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1501 			SET(tp->t_state, TS_FLUSH);
   1502 	}
   1503 	PLCOM_UNLOCK(sc);
   1504 	splx(s);
   1505 }
   1506 
   1507 void
   1508 plcomdiag(void *arg)
   1509 {
   1510 	struct plcom_softc *sc = arg;
   1511 	int overflows, floods;
   1512 	int s;
   1513 
   1514 	s = splserial();
   1515 	PLCOM_LOCK(sc);
   1516 	overflows = sc->sc_overflows;
   1517 	sc->sc_overflows = 0;
   1518 	floods = sc->sc_floods;
   1519 	sc->sc_floods = 0;
   1520 	sc->sc_errors = 0;
   1521 	PLCOM_UNLOCK(sc);
   1522 	splx(s);
   1523 
   1524 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1525 	    sc->sc_dev.dv_xname,
   1526 	    overflows, overflows == 1 ? "" : "s",
   1527 	    floods, floods == 1 ? "" : "s");
   1528 }
   1529 
   1530 integrate void
   1531 plcom_rxsoft(struct plcom_softc *sc, struct tty *tp)
   1532 {
   1533 	int (*rint) (int, struct tty *) = tp->t_linesw->l_rint;
   1534 	u_char *get, *end;
   1535 	u_int cc, scc;
   1536 	u_char rsr;
   1537 	int code;
   1538 	int s;
   1539 
   1540 	end = sc->sc_ebuf;
   1541 	get = sc->sc_rbget;
   1542 	scc = cc = plcom_rbuf_size - sc->sc_rbavail;
   1543 
   1544 	if (cc == plcom_rbuf_size) {
   1545 		sc->sc_floods++;
   1546 		if (sc->sc_errors++ == 0)
   1547 			callout_reset(&sc->sc_diag_callout, 60 * hz,
   1548 			    plcomdiag, sc);
   1549 	}
   1550 
   1551 	while (cc) {
   1552 		code = get[0];
   1553 		rsr = get[1];
   1554 		if (ISSET(rsr, RSR_OE | RSR_BE | RSR_FE | RSR_PE)) {
   1555 			if (ISSET(rsr, RSR_OE)) {
   1556 				sc->sc_overflows++;
   1557 				if (sc->sc_errors++ == 0)
   1558 					callout_reset(&sc->sc_diag_callout,
   1559 					    60 * hz, plcomdiag, sc);
   1560 			}
   1561 			if (ISSET(rsr, RSR_BE | RSR_FE))
   1562 				SET(code, TTY_FE);
   1563 			if (ISSET(rsr, RSR_PE))
   1564 				SET(code, TTY_PE);
   1565 		}
   1566 		if ((*rint)(code, tp) == -1) {
   1567 			/*
   1568 			 * The line discipline's buffer is out of space.
   1569 			 */
   1570 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1571 				/*
   1572 				 * We're either not using flow control, or the
   1573 				 * line discipline didn't tell us to block for
   1574 				 * some reason.  Either way, we have no way to
   1575 				 * know when there's more space available, so
   1576 				 * just drop the rest of the data.
   1577 				 */
   1578 				get += cc << 1;
   1579 				if (get >= end)
   1580 					get -= plcom_rbuf_size << 1;
   1581 				cc = 0;
   1582 			} else {
   1583 				/*
   1584 				 * Don't schedule any more receive processing
   1585 				 * until the line discipline tells us there's
   1586 				 * space available (through plcomhwiflow()).
   1587 				 * Leave the rest of the data in the input
   1588 				 * buffer.
   1589 				 */
   1590 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1591 			}
   1592 			break;
   1593 		}
   1594 		get += 2;
   1595 		if (get >= end)
   1596 			get = sc->sc_rbuf;
   1597 		cc--;
   1598 	}
   1599 
   1600 	if (cc != scc) {
   1601 		sc->sc_rbget = get;
   1602 		s = splserial();
   1603 		PLCOM_LOCK(sc);
   1604 
   1605 		cc = sc->sc_rbavail += scc - cc;
   1606 		/* Buffers should be ok again, release possible block. */
   1607 		if (cc >= sc->sc_r_lowat) {
   1608 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1609 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1610 				SET(sc->sc_cr, CR_RIE | CR_RTIE);
   1611 				bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
   1612 			}
   1613 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1614 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1615 				plcom_hwiflow(sc);
   1616 			}
   1617 		}
   1618 		PLCOM_UNLOCK(sc);
   1619 		splx(s);
   1620 	}
   1621 }
   1622 
   1623 integrate void
   1624 plcom_txsoft(struct plcom_softc *sc, struct tty *tp)
   1625 {
   1626 
   1627 	CLR(tp->t_state, TS_BUSY);
   1628 	if (ISSET(tp->t_state, TS_FLUSH))
   1629 		CLR(tp->t_state, TS_FLUSH);
   1630 	else
   1631 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1632 	(*tp->t_linesw->l_start)(tp);
   1633 }
   1634 
   1635 integrate void
   1636 plcom_stsoft(struct plcom_softc *sc, struct tty *tp)
   1637 {
   1638 	u_char msr, delta;
   1639 	int s;
   1640 
   1641 	s = splserial();
   1642 	PLCOM_LOCK(sc);
   1643 	msr = sc->sc_msr;
   1644 	delta = sc->sc_msr_delta;
   1645 	sc->sc_msr_delta = 0;
   1646 	PLCOM_UNLOCK(sc);
   1647 	splx(s);
   1648 
   1649 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1650 		/*
   1651 		 * Inform the tty layer that carrier detect changed.
   1652 		 */
   1653 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
   1654 	}
   1655 
   1656 	if (ISSET(delta, sc->sc_msr_cts)) {
   1657 		/* Block or unblock output according to flow control. */
   1658 		if (ISSET(msr, sc->sc_msr_cts)) {
   1659 			sc->sc_tx_stopped = 0;
   1660 			(*tp->t_linesw->l_start)(tp);
   1661 		} else {
   1662 			sc->sc_tx_stopped = 1;
   1663 		}
   1664 	}
   1665 
   1666 #ifdef PLCOM_DEBUG
   1667 	if (plcom_debug)
   1668 		plcomstatus(sc, "plcom_stsoft");
   1669 #endif
   1670 }
   1671 
   1672 void
   1673 plcomsoft(void *arg)
   1674 {
   1675 	struct plcom_softc *sc = arg;
   1676 	struct tty *tp;
   1677 
   1678 	if (PLCOM_ISALIVE(sc) == 0)
   1679 		return;
   1680 
   1681 	tp = sc->sc_tty;
   1682 
   1683 	if (sc->sc_rx_ready) {
   1684 		sc->sc_rx_ready = 0;
   1685 		plcom_rxsoft(sc, tp);
   1686 	}
   1687 
   1688 	if (sc->sc_st_check) {
   1689 		sc->sc_st_check = 0;
   1690 		plcom_stsoft(sc, tp);
   1691 	}
   1692 
   1693 	if (sc->sc_tx_done) {
   1694 		sc->sc_tx_done = 0;
   1695 		plcom_txsoft(sc, tp);
   1696 	}
   1697 }
   1698 
   1699 int
   1700 plcomintr(void *arg)
   1701 {
   1702 	struct plcom_softc *sc = arg;
   1703 	bus_space_tag_t iot = sc->sc_iot;
   1704 	bus_space_handle_t ioh = sc->sc_ioh;
   1705 	u_char *put, *end;
   1706 	u_int cc;
   1707 	u_char rsr, iir;
   1708 
   1709 	if (PLCOM_ISALIVE(sc) == 0)
   1710 		return 0;
   1711 
   1712 	PLCOM_LOCK(sc);
   1713 	iir = bus_space_read_1(iot, ioh, plcom_iir);
   1714 	if (! ISSET(iir, IIR_IMASK)) {
   1715 		PLCOM_UNLOCK(sc);
   1716 		return 0;
   1717 	}
   1718 
   1719 	end = sc->sc_ebuf;
   1720 	put = sc->sc_rbput;
   1721 	cc = sc->sc_rbavail;
   1722 
   1723 	do {
   1724 		u_char	msr, delta, fr;
   1725 
   1726 		fr = bus_space_read_1(iot, ioh, plcom_fr);
   1727 
   1728 		if (!ISSET(fr, FR_RXFE) &&
   1729 		    !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1730 			while (cc > 0) {
   1731 				int cn_trapped = 0;
   1732 				put[0] = bus_space_read_1(iot, ioh,
   1733 				    plcom_dr);
   1734 				rsr = bus_space_read_1(iot, ioh, plcom_rsr);
   1735 				/* Clear any error status.  */
   1736 				if (ISSET(rsr,
   1737 				    (RSR_BE | RSR_OE | RSR_PE | RSR_FE)))
   1738 					bus_space_write_1(iot, ioh, plcom_ecr,
   1739 					    0);
   1740 				if (ISSET(rsr, RSR_BE)) {
   1741 					cn_trapped = 0;
   1742 					cn_check_magic(sc->sc_tty->t_dev,
   1743 					    CNC_BREAK, plcom_cnm_state);
   1744 					if (cn_trapped)
   1745 						continue;
   1746 #if defined(KGDB)
   1747 					if (ISSET(sc->sc_hwflags,
   1748 					    PLCOM_HW_KGDB)) {
   1749 						kgdb_connect(1);
   1750 						continue;
   1751 					}
   1752 #endif
   1753 				}
   1754 
   1755 				put[1] = rsr;
   1756 				cn_trapped = 0;
   1757 				cn_check_magic(sc->sc_tty->t_dev,
   1758 					       put[0], plcom_cnm_state);
   1759 				if (cn_trapped) {
   1760 					fr = bus_space_read_1(iot, ioh,
   1761 					    plcom_fr);
   1762 					if (ISSET(fr, FR_RXFE))
   1763 						break;
   1764 
   1765 					continue;
   1766 				}
   1767 				put += 2;
   1768 				if (put >= end)
   1769 					put = sc->sc_rbuf;
   1770 				cc--;
   1771 
   1772 				fr = bus_space_read_1(iot, ioh, plcom_fr);
   1773 				if (ISSET(fr, FR_RXFE))
   1774 					break;
   1775 			}
   1776 
   1777 			/*
   1778 			 * Current string of incoming characters ended because
   1779 			 * no more data was available or we ran out of space.
   1780 			 * Schedule a receive event if any data was received.
   1781 			 * If we're out of space, turn off receive interrupts.
   1782 			 */
   1783 			sc->sc_rbput = put;
   1784 			sc->sc_rbavail = cc;
   1785 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1786 				sc->sc_rx_ready = 1;
   1787 
   1788 			/*
   1789 			 * See if we are in danger of overflowing a buffer. If
   1790 			 * so, use hardware flow control to ease the pressure.
   1791 			 */
   1792 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   1793 			    cc < sc->sc_r_hiwat) {
   1794 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1795 				plcom_hwiflow(sc);
   1796 			}
   1797 
   1798 			/*
   1799 			 * If we're out of space, disable receive interrupts
   1800 			 * until the queue has drained a bit.
   1801 			 */
   1802 			if (!cc) {
   1803 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1804 				CLR(sc->sc_cr, CR_RIE | CR_RTIE);
   1805 				bus_space_write_1(iot, ioh, plcom_cr,
   1806 				    sc->sc_cr);
   1807 			}
   1808 		} else {
   1809 			if (ISSET(iir, IIR_RIS)) {
   1810 				bus_space_write_1(iot, ioh, plcom_cr, 0);
   1811 				delay(10);
   1812 				bus_space_write_1(iot, ioh, plcom_cr,
   1813 				    sc->sc_cr);
   1814 				continue;
   1815 			}
   1816 		}
   1817 
   1818 		msr = bus_space_read_1(iot, ioh, plcom_fr);
   1819 		delta = msr ^ sc->sc_msr;
   1820 		sc->sc_msr = msr;
   1821 		/* Clear any pending modem status interrupt.  */
   1822 		if (iir & IIR_MIS)
   1823 			bus_space_write_1(iot, ioh, plcom_icr, 0);
   1824 		/*
   1825 		 * Pulse-per-second (PSS) signals on edge of DCD?
   1826 		 * Process these even if line discipline is ignoring DCD.
   1827 		 */
   1828 		if (delta & sc->sc_ppsmask) {
   1829 			struct timeval tv;
   1830 			mutex_spin_enter(&timecounter_lock);
   1831 		    	if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
   1832 				/* XXX nanotime() */
   1833 				microtime(&tv);
   1834 				TIMEVAL_TO_TIMESPEC(&tv,
   1835 				    &sc->ppsinfo.assert_timestamp);
   1836 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
   1837 					timespecadd(&sc->ppsinfo.assert_timestamp,
   1838 					    &sc->ppsparam.assert_offset,
   1839 						    &sc->ppsinfo.assert_timestamp);
   1840 				}
   1841 
   1842 #ifdef PPS_SYNC
   1843 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
   1844 					hardpps(&tv, tv.tv_usec);
   1845 #endif
   1846 				sc->ppsinfo.assert_sequence++;
   1847 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   1848 
   1849 			} else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
   1850 				/* XXX nanotime() */
   1851 				microtime(&tv);
   1852 				TIMEVAL_TO_TIMESPEC(&tv,
   1853 				    &sc->ppsinfo.clear_timestamp);
   1854 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
   1855 					timespecadd(&sc->ppsinfo.clear_timestamp,
   1856 					    &sc->ppsparam.clear_offset,
   1857 					    &sc->ppsinfo.clear_timestamp);
   1858 				}
   1859 
   1860 #ifdef PPS_SYNC
   1861 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
   1862 					hardpps(&tv, tv.tv_usec);
   1863 #endif
   1864 				sc->ppsinfo.clear_sequence++;
   1865 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   1866 			}
   1867 			mutex_spin_exit(&timecounter_lock);
   1868 		}
   1869 
   1870 		/*
   1871 		 * Process normal status changes
   1872 		 */
   1873 		if (ISSET(delta, sc->sc_msr_mask)) {
   1874 			SET(sc->sc_msr_delta, delta);
   1875 
   1876 			/*
   1877 			 * Stop output immediately if we lose the output
   1878 			 * flow control signal or carrier detect.
   1879 			 */
   1880 			if (ISSET(~msr, sc->sc_msr_mask)) {
   1881 				sc->sc_tbc = 0;
   1882 				sc->sc_heldtbc = 0;
   1883 #ifdef PLCOM_DEBUG
   1884 				if (plcom_debug)
   1885 					plcomstatus(sc, "plcomintr  ");
   1886 #endif
   1887 			}
   1888 
   1889 			sc->sc_st_check = 1;
   1890 		}
   1891 
   1892 		/*
   1893 		 * Done handling any receive interrupts. See if data
   1894 		 * can be * transmitted as well. Schedule tx done
   1895 		 * event if no data left * and tty was marked busy.
   1896 		 */
   1897 		if (ISSET(iir, IIR_TIS)) {
   1898 			/*
   1899 			 * If we've delayed a parameter change, do it
   1900 			 * now, and restart * output.
   1901 			 */
   1902 			if (sc->sc_heldchange) {
   1903 				plcom_loadchannelregs(sc);
   1904 				sc->sc_heldchange = 0;
   1905 				sc->sc_tbc = sc->sc_heldtbc;
   1906 				sc->sc_heldtbc = 0;
   1907 			}
   1908 
   1909 			/*
   1910 			 * Output the next chunk of the contiguous
   1911 			 * buffer, if any.
   1912 			 */
   1913 			if (sc->sc_tbc > 0) {
   1914 				int n;
   1915 
   1916 				n = sc->sc_tbc;
   1917 				if (n > sc->sc_fifolen)
   1918 					n = sc->sc_fifolen;
   1919 				bus_space_write_multi_1(iot, ioh, plcom_dr,
   1920 				    sc->sc_tba, n);
   1921 				sc->sc_tbc -= n;
   1922 				sc->sc_tba += n;
   1923 			} else {
   1924 				/*
   1925 				 * Disable transmit plcompletion
   1926 				 * interrupts if necessary.
   1927 				 */
   1928 				if (ISSET(sc->sc_cr, CR_TIE)) {
   1929 					CLR(sc->sc_cr, CR_TIE);
   1930 					bus_space_write_1(iot, ioh, plcom_cr,
   1931 					    sc->sc_cr);
   1932 				}
   1933 				if (sc->sc_tx_busy) {
   1934 					sc->sc_tx_busy = 0;
   1935 					sc->sc_tx_done = 1;
   1936 				}
   1937 			}
   1938 		}
   1939 	} while (ISSET((iir = bus_space_read_1(iot, ioh, plcom_iir)),
   1940 	    IIR_IMASK));
   1941 
   1942 	PLCOM_UNLOCK(sc);
   1943 
   1944 	/* Wake up the poller. */
   1945 	softint_schedule(sc->sc_si);
   1946 
   1947 #if NRND > 0 && defined(RND_COM)
   1948 	rnd_add_uint32(&sc->rnd_source, iir | rsr);
   1949 #endif
   1950 
   1951 	return 1;
   1952 }
   1953 
   1954 /*
   1955  * The following functions are polled getc and putc routines, shared
   1956  * by the console and kgdb glue.
   1957  *
   1958  * The read-ahead code is so that you can detect pending in-band
   1959  * cn_magic in polled mode while doing output rather than having to
   1960  * wait until the kernel decides it needs input.
   1961  */
   1962 
   1963 #define MAX_READAHEAD	20
   1964 static int plcom_readahead[MAX_READAHEAD];
   1965 static int plcom_readaheadcount = 0;
   1966 
   1967 int
   1968 plcom_common_getc(dev_t dev, bus_space_tag_t iot, bus_space_handle_t ioh)
   1969 {
   1970 	int s = splserial();
   1971 	u_char stat, c;
   1972 
   1973 	/* got a character from reading things earlier */
   1974 	if (plcom_readaheadcount > 0) {
   1975 		int i;
   1976 
   1977 		c = plcom_readahead[0];
   1978 		for (i = 1; i < plcom_readaheadcount; i++) {
   1979 			plcom_readahead[i-1] = plcom_readahead[i];
   1980 		}
   1981 		plcom_readaheadcount--;
   1982 		splx(s);
   1983 		return c;
   1984 	}
   1985 
   1986 	/* block until a character becomes available */
   1987 	while (ISSET(stat = bus_space_read_1(iot, ioh, plcom_fr), FR_RXFE))
   1988 		;
   1989 
   1990 	c = bus_space_read_1(iot, ioh, plcom_dr);
   1991 	stat = bus_space_read_1(iot, ioh, plcom_iir);
   1992 	{
   1993 		int cn_trapped = 0; /* unused */
   1994 #ifdef DDB
   1995 		extern int db_active;
   1996 		if (!db_active)
   1997 #endif
   1998 			cn_check_magic(dev, c, plcom_cnm_state);
   1999 	}
   2000 	splx(s);
   2001 	return c;
   2002 }
   2003 
   2004 void
   2005 plcom_common_putc(dev_t dev, bus_space_tag_t iot, bus_space_handle_t ioh,
   2006     int c)
   2007 {
   2008 	int s = splserial();
   2009 	int timo;
   2010 
   2011 	int cin, stat;
   2012 	if (plcom_readaheadcount < MAX_READAHEAD
   2013 	     && !ISSET(stat = bus_space_read_1(iot, ioh, plcom_fr), FR_RXFE)) {
   2014 		int cn_trapped = 0;
   2015 		cin = bus_space_read_1(iot, ioh, plcom_dr);
   2016 		stat = bus_space_read_1(iot, ioh, plcom_iir);
   2017 		cn_check_magic(dev, cin, plcom_cnm_state);
   2018 		plcom_readahead[plcom_readaheadcount++] = cin;
   2019 	}
   2020 
   2021 	/* wait for any pending transmission to finish */
   2022 	timo = 150000;
   2023 	while (!ISSET(bus_space_read_1(iot, ioh, plcom_fr), FR_TXFE) && --timo)
   2024 		continue;
   2025 
   2026 	bus_space_write_1(iot, ioh, plcom_dr, c);
   2027 	PLCOM_BARRIER(iot, ioh, BR | BW);
   2028 
   2029 	/* wait for this transmission to complete */
   2030 	timo = 1500000;
   2031 	while (!ISSET(bus_space_read_1(iot, ioh, plcom_fr), FR_TXFE) && --timo)
   2032 		continue;
   2033 
   2034 	splx(s);
   2035 }
   2036 
   2037 /*
   2038  * Initialize UART for use as console or KGDB line.
   2039  */
   2040 int
   2041 plcominit(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
   2042     tcflag_t cflag, bus_space_handle_t *iohp)
   2043 {
   2044 	bus_space_handle_t ioh;
   2045 
   2046 	if (bus_space_map(iot, iobase, PLCOM_UART_SIZE, 0, &ioh))
   2047 		return ENOMEM; /* ??? */
   2048 
   2049 	rate = plcomspeed(rate, frequency);
   2050 	bus_space_write_1(iot, ioh, plcom_cr, 0);
   2051 	bus_space_write_1(iot, ioh, plcom_dlbl, rate);
   2052 	bus_space_write_1(iot, ioh, plcom_dlbh, rate >> 8);
   2053 	bus_space_write_1(iot, ioh, plcom_lcr, cflag2lcr(cflag) | LCR_FEN);
   2054 	bus_space_write_1(iot, ioh, plcom_cr, CR_UARTEN);
   2055 
   2056 #if 0
   2057 	/* Ought to do something like this, but we have no sc to
   2058 	   dereference. */
   2059 	/* XXX device_unit() abuse */
   2060 	sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
   2061 	    MCR_DTR | MCR_RTS);
   2062 #endif
   2063 
   2064 	*iohp = ioh;
   2065 	return 0;
   2066 }
   2067 
   2068 /*
   2069  * Following are all routines needed for PLCOM to act as console
   2070  */
   2071 struct consdev plcomcons = {
   2072 	NULL, NULL, plcomcngetc, plcomcnputc, plcomcnpollc, NULL,
   2073 	NULL, NULL, NODEV, CN_NORMAL
   2074 };
   2075 
   2076 
   2077 int
   2078 plcomcnattach(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
   2079     tcflag_t cflag, int unit)
   2080 {
   2081 	int res;
   2082 
   2083 	res = plcominit(iot, iobase, rate, frequency, cflag, &plcomconsioh);
   2084 	if (res)
   2085 		return res;
   2086 
   2087 	cn_tab = &plcomcons;
   2088 	cn_init_magic(&plcom_cnm_state);
   2089 	cn_set_magic("\047\001"); /* default magic is BREAK */
   2090 
   2091 	plcomconstag = iot;
   2092 	plcomconsunit = unit;
   2093 	plcomconsrate = rate;
   2094 	plcomconscflag = cflag;
   2095 
   2096 	return 0;
   2097 }
   2098 
   2099 void
   2100 plcomcndetach(void)
   2101 {
   2102 	bus_space_unmap(plcomconstag, plcomconsioh, PLCOM_UART_SIZE);
   2103 	plcomconstag = NULL;
   2104 
   2105 	cn_tab = NULL;
   2106 }
   2107 
   2108 int
   2109 plcomcngetc(dev_t dev)
   2110 {
   2111 	return plcom_common_getc(dev, plcomconstag, plcomconsioh);
   2112 }
   2113 
   2114 /*
   2115  * Console kernel output character routine.
   2116  */
   2117 void
   2118 plcomcnputc(dev_t dev, int c)
   2119 {
   2120 	plcom_common_putc(dev, plcomconstag, plcomconsioh, c);
   2121 }
   2122 
   2123 void
   2124 plcomcnpollc(dev_t dev, int on)
   2125 {
   2126 
   2127 }
   2128 
   2129 #ifdef KGDB
   2130 int
   2131 plcom_kgdb_attach(bus_space_tag_t iot, bus_addr_t iobase, int rate,
   2132    int frequency, tcflag_t cflag, int unit)
   2133 {
   2134 	int res;
   2135 
   2136 	if (iot == plcomconstag && iobase == plcomconsunit)
   2137 		return EBUSY; /* cannot share with console */
   2138 
   2139 	res = plcominit(iot, iobase, rate, frequency, cflag, &plcom_kgdb_ioh);
   2140 	if (res)
   2141 		return res;
   2142 
   2143 	kgdb_attach(plcom_kgdb_getc, plcom_kgdb_putc, NULL);
   2144 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   2145 
   2146 	plcom_kgdb_iot = iot;
   2147 	plcom_kgdb_unit = unit;
   2148 
   2149 	return 0;
   2150 }
   2151 
   2152 /* ARGSUSED */
   2153 int
   2154 plcom_kgdb_getc(void *arg)
   2155 {
   2156 	return plcom_common_getc(NODEV, plcom_kgdb_iot, plcom_kgdb_ioh);
   2157 }
   2158 
   2159 /* ARGSUSED */
   2160 void
   2161 plcom_kgdb_putc(void *arg, int c)
   2162 {
   2163 	plcom_common_putc(NODEV, plcom_kgdb_iot, plcom_kgdb_ioh, c);
   2164 }
   2165 #endif /* KGDB */
   2166 
   2167 /* helper function to identify the plcom ports used by
   2168  console or KGDB (and not yet autoconf attached) */
   2169 int
   2170 plcom_is_console(bus_space_tag_t iot, int unit,
   2171     bus_space_handle_t *ioh)
   2172 {
   2173 	bus_space_handle_t help;
   2174 
   2175 	if (!plcomconsattached &&
   2176 	    iot == plcomconstag && unit == plcomconsunit)
   2177 		help = plcomconsioh;
   2178 #ifdef KGDB
   2179 	else if (!plcom_kgdb_attached &&
   2180 	    iot == plcom_kgdb_iot && unit == plcom_kgdb_unit)
   2181 		help = plcom_kgdb_ioh;
   2182 #endif
   2183 	else
   2184 		return 0;
   2185 
   2186 	if (ioh)
   2187 		*ioh = help;
   2188 	return 1;
   2189 }
   2190