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