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