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