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