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