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