Home | History | Annotate | Line # | Download | only in ep93xx
epcom.c revision 1.29
      1 /*	$NetBSD: epcom.c,v 1.29 2014/11/15 19:20:01 christos Exp $ */
      2 /*
      3  * Copyright (c) 1998, 1999, 2001, 2002, 2004 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Jesse Off
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Ichiro FUKUHARA and Naoto Shimazaki.
     11  *
     12  * This code is derived from software contributed to The NetBSD Foundation
     13  * by IWAMOTO Toshihiro.
     14  *
     15  * This code is derived from software contributed to The NetBSD Foundation
     16  * by Charles M. Hannum.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1991 The Regents of the University of California.
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  *
     68  *      @(#)com.c       7.5 (Berkeley) 5/16/91
     69  */
     70 
     71 /*
     72  * TODO: hardware flow control
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: epcom.c,v 1.29 2014/11/15 19:20:01 christos Exp $");
     77 
     78 #include "opt_ddb.h"
     79 #include "opt_kgdb.h"
     80 #include "epcom.h"
     81 
     82 #include "rnd.h"
     83 #ifdef RND_COM
     84 #include <sys/rnd.h>
     85 #endif
     86 
     87 /*
     88  * Override cnmagic(9) macro before including <sys/systm.h>.
     89  * We need to know if cn_check_magic triggered debugger, so set a flag.
     90  * Callers of cn_check_magic must declare int cn_trapped = 0;
     91  * XXX: this is *ugly*!
     92  */
     93 #define cn_trap()				\
     94 	do {					\
     95 		console_debugger();		\
     96 		cn_trapped = 1;			\
     97 	} while (/* CONSTCOND */ 0)
     98 
     99 
    100 #include <sys/param.h>
    101 #include <sys/systm.h>
    102 #include <sys/types.h>
    103 #include <sys/conf.h>
    104 #include <sys/file.h>
    105 #include <sys/device.h>
    106 #include <sys/kernel.h>
    107 #include <sys/malloc.h>
    108 #include <sys/tty.h>
    109 #include <sys/uio.h>
    110 #include <sys/vnode.h>
    111 #include <sys/kauth.h>
    112 
    113 #include <machine/intr.h>
    114 #include <sys/bus.h>
    115 
    116 #include <arm/ep93xx/epcomreg.h>
    117 #include <arm/ep93xx/epcomvar.h>
    118 #include <arm/ep93xx/ep93xxreg.h>
    119 #include <arm/ep93xx/ep93xxvar.h>
    120 
    121 #include <dev/cons.h>
    122 
    123 static int	epcomparam(struct tty *, struct termios *);
    124 static void	epcomstart(struct tty *);
    125 static int	epcomhwiflow(struct tty *, int);
    126 
    127 static u_int	cflag2lcrhi(tcflag_t);
    128 static void	epcom_iflush(struct epcom_softc *);
    129 static void	epcom_set(struct epcom_softc *);
    130 
    131 int             epcomcngetc(dev_t);
    132 void            epcomcnputc(dev_t, int);
    133 void            epcomcnpollc(dev_t, int);
    134 
    135 static void	epcomsoft(void* arg);
    136 inline static void	epcom_txsoft(struct epcom_softc *, struct tty *);
    137 inline static void	epcom_rxsoft(struct epcom_softc *, struct tty *);
    138 
    139 void            epcomcnprobe(struct consdev *);
    140 void            epcomcninit(struct consdev *);
    141 
    142 static struct epcom_cons_softc {
    143 	bus_space_tag_t		sc_iot;
    144 	bus_space_handle_t	sc_ioh;
    145 	bus_addr_t		sc_hwbase;
    146 	int			sc_ospeed;
    147 	tcflag_t		sc_cflag;
    148 	int			sc_attached;
    149 } epcomcn_sc;
    150 
    151 static struct cnm_state epcom_cnm_state;
    152 
    153 extern struct cfdriver epcom_cd;
    154 
    155 dev_type_open(epcomopen);
    156 dev_type_close(epcomclose);
    157 dev_type_read(epcomread);
    158 dev_type_write(epcomwrite);
    159 dev_type_ioctl(epcomioctl);
    160 dev_type_stop(epcomstop);
    161 dev_type_tty(epcomtty);
    162 dev_type_poll(epcompoll);
    163 
    164 const struct cdevsw epcom_cdevsw = {
    165 	.d_open = epcomopen,
    166 	.d_close = epcomclose,
    167 	.d_read = epcomread,
    168 	.d_write = epcomwrite,
    169 	.d_ioctl = epcomioctl,
    170 	.d_stop = epcomstop,
    171 	.d_tty = epcomtty,
    172 	.d_poll = epcompoll,
    173 	.d_mmap = nommap,
    174 	.d_kqfilter = ttykqfilter,
    175 	.d_discard = nodiscard,
    176 	.d_flag = D_TTY
    177 };
    178 
    179 struct consdev epcomcons = {
    180 	NULL, NULL, epcomcngetc, epcomcnputc, epcomcnpollc, NULL,
    181 	NULL, NULL, NODEV, CN_NORMAL
    182 };
    183 
    184 #ifndef DEFAULT_COMSPEED
    185 #define DEFAULT_COMSPEED 115200
    186 #endif
    187 
    188 #define COMUNIT(x)	TTUNIT(x)
    189 #define COMDIALOUT(x)	TTDIALOUT(x)
    190 
    191 #define COM_ISALIVE(sc)	((sc)->enabled != 0 && \
    192 			device_is_active((sc)->sc_dev))
    193 
    194 void
    195 epcom_attach_subr(struct epcom_softc *sc)
    196 {
    197 	struct tty *tp;
    198 
    199 	if (sc->sc_iot == epcomcn_sc.sc_iot
    200 	    && sc->sc_hwbase == epcomcn_sc.sc_hwbase) {
    201 		epcomcn_sc.sc_attached = 1;
    202 		sc->sc_lcrlo = EPCOMSPEED2BRD(epcomcn_sc.sc_ospeed) & 0xff;
    203 		sc->sc_lcrmid = EPCOMSPEED2BRD(epcomcn_sc.sc_ospeed) >> 8;
    204 
    205 		/* Make sure the console is always "hardwired". */
    206 		delay(10000);	/* wait for output to finish */
    207 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    208 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    209 	}
    210 
    211 	tp = tty_alloc();
    212 	tp->t_oproc = epcomstart;
    213 	tp->t_param = epcomparam;
    214 	tp->t_hwiflow = epcomhwiflow;
    215 
    216 	sc->sc_tty = tp;
    217 	sc->sc_rbuf = malloc(EPCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    218 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    219 	sc->sc_rbavail = EPCOM_RING_SIZE;
    220 	if (sc->sc_rbuf == NULL) {
    221 		printf("%s: unable to allocate ring buffer\n",
    222 		    device_xname(sc->sc_dev));
    223 		return;
    224 	}
    225 	sc->sc_ebuf = sc->sc_rbuf + (EPCOM_RING_SIZE << 1);
    226 	sc->sc_tbc = 0;
    227 
    228 	sc->sc_lcrlo = EPCOMSPEED2BRD(DEFAULT_COMSPEED) & 0xff;
    229 	sc->sc_lcrmid = EPCOMSPEED2BRD(DEFAULT_COMSPEED) >> 8;
    230 	sc->sc_lcrhi = cflag2lcrhi(CS8); /* 8N1 */
    231 
    232 	tty_attach(tp);
    233 
    234 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    235 		int maj;
    236 
    237 		/* locate the major number */
    238 		maj = cdevsw_lookup_major(&epcom_cdevsw);
    239 
    240 		cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev));
    241 
    242 		aprint_normal("%s: console\n", device_xname(sc->sc_dev));
    243 	}
    244 
    245 	sc->sc_si = softint_establish(SOFTINT_SERIAL, epcomsoft, sc);
    246 
    247 #ifdef RND_COM
    248 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    249 			  RND_TYPE_TTY, RND_FLAG_DEFAULT);
    250 #endif
    251 
    252 	/* if there are no enable/disable functions, assume the device
    253 	   is always enabled */
    254 	if (!sc->enable)
    255 		sc->enabled = 1;
    256 
    257 	/* XXX configure register */
    258 	/* xxx_config(sc) */
    259 
    260 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    261 }
    262 
    263 static int
    264 epcomparam(struct tty *tp, struct termios *t)
    265 {
    266 	struct epcom_softc *sc
    267 		= device_lookup_private(&epcom_cd, COMUNIT(tp->t_dev));
    268 	int s;
    269 
    270 	if (COM_ISALIVE(sc) == 0)
    271 		return (EIO);
    272 
    273 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    274 		return (EINVAL);
    275 
    276 	/*
    277 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    278 	 * is always active.
    279 	 */
    280 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    281 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    282 		SET(t->c_cflag, CLOCAL);
    283 		CLR(t->c_cflag, HUPCL);
    284 	}
    285 
    286 	/*
    287 	 * If there were no changes, don't do anything.  This avoids dropping
    288 	 * input and improves performance when all we did was frob things like
    289 	 * VMIN and VTIME.
    290 	 */
    291 	if (tp->t_ospeed == t->c_ospeed &&
    292 	    tp->t_cflag == t->c_cflag)
    293 		return (0);
    294 
    295 	s = splserial();
    296 
    297 	sc->sc_lcrhi = cflag2lcrhi(t->c_cflag);
    298 	sc->sc_lcrlo = EPCOMSPEED2BRD(t->c_ospeed) & 0xff;
    299 	sc->sc_lcrmid = EPCOMSPEED2BRD(t->c_ospeed) >> 8;
    300 
    301 	/* And copy to tty. */
    302 	tp->t_ispeed = 0;
    303 	tp->t_ospeed = t->c_ospeed;
    304 	tp->t_cflag = t->c_cflag;
    305 	epcom_set(sc);
    306 
    307 	splx(s);
    308 
    309 	/*
    310 	 * Update the tty layer's idea of the carrier bit.
    311 	 * We tell tty the carrier is always on.
    312 	 */
    313 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    314 
    315 #ifdef COM_DEBUG
    316 	if (com_debug)
    317 		comstatus(sc, "comparam ");
    318 #endif
    319 
    320 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    321 		if (sc->sc_tx_stopped) {
    322 			sc->sc_tx_stopped = 0;
    323 			epcomstart(tp);
    324 		}
    325 	}
    326 
    327 	return (0);
    328 }
    329 
    330 static int
    331 epcomhwiflow(struct tty *tp, int block)
    332 {
    333 	return (0);
    334 }
    335 
    336 static void
    337 epcom_filltx(struct epcom_softc *sc)
    338 {
    339 	bus_space_tag_t iot = sc->sc_iot;
    340 	bus_space_handle_t ioh = sc->sc_ioh;
    341 	int n;
    342 
    343 	n = 0;
    344         while ((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_TXFF) == 0) {
    345 		if (n >= sc->sc_tbc)
    346 			break;
    347 		bus_space_write_4(iot, ioh, EPCOM_Data,
    348 				  0xff & *(sc->sc_tba + n));
    349 		n++;
    350         }
    351         sc->sc_tbc -= n;
    352         sc->sc_tba += n;
    353 }
    354 
    355 static void
    356 epcomstart(struct tty *tp)
    357 {
    358 	struct epcom_softc *sc
    359 		= device_lookup_private(&epcom_cd, COMUNIT(tp->t_dev));
    360 	int s;
    361 
    362 	if (COM_ISALIVE(sc) == 0)
    363 		return;
    364 
    365 	s = spltty();
    366 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    367 		goto out;
    368 	if (sc->sc_tx_stopped)
    369 		goto out;
    370 	if (!ttypull(tp))
    371 		goto out;
    372 
    373 	/* Grab the first contiguous region of buffer space. */
    374 	{
    375 		u_char *tba;
    376 		int tbc;
    377 
    378 		tba = tp->t_outq.c_cf;
    379 		tbc = ndqb(&tp->t_outq, 0);
    380 
    381 		(void)splserial();
    382 
    383 		sc->sc_tba = tba;
    384 		sc->sc_tbc = tbc;
    385 	}
    386 
    387 	SET(tp->t_state, TS_BUSY);
    388 	sc->sc_tx_busy = 1;
    389 
    390 	/* Output the first chunk of the contiguous buffer. */
    391 	epcom_filltx(sc);
    392 
    393 	if (!ISSET(sc->sc_ctrl, Ctrl_TIE)) {
    394 		SET(sc->sc_ctrl, Ctrl_TIE);
    395 		epcom_set(sc);
    396 	}
    397 
    398 out:
    399 	splx(s);
    400 	return;
    401 }
    402 
    403 static void
    404 epcom_break(struct epcom_softc *sc, int onoff)
    405 {
    406 	if (onoff)
    407 		SET(sc->sc_lcrhi, LinCtrlHigh_BRK);
    408 	else
    409 		CLR(sc->sc_lcrhi, LinCtrlHigh_BRK);
    410 	epcom_set(sc);
    411 }
    412 
    413 static void
    414 epcom_shutdown(struct epcom_softc *sc)
    415 {
    416 	int s;
    417 
    418 	s = splserial();
    419 
    420 	/* Turn off interrupts. */
    421 	CLR(sc->sc_ctrl, (Ctrl_TIE|Ctrl_RTIE|Ctrl_RIE));
    422 
    423 	/* Clear any break condition set with TIOCSBRK. */
    424 	epcom_break(sc, 0);
    425 	epcom_set(sc);
    426 
    427 	if (sc->disable) {
    428 #ifdef DIAGNOSTIC
    429 		if (!sc->enabled)
    430 			panic("epcom_shutdown: not enabled?");
    431 #endif
    432 		(*sc->disable)(sc);
    433 		sc->enabled = 0;
    434 	}
    435 	splx(s);
    436 }
    437 
    438 int
    439 epcomopen(dev_t dev, int flag, int mode, struct lwp *l)
    440 {
    441 	struct epcom_softc *sc;
    442 	struct tty *tp;
    443 	int s, s2;
    444 	int error;
    445 
    446 	sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
    447 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    448 		sc->sc_rbuf == NULL)
    449 		return (ENXIO);
    450 
    451 	if (!device_is_active(sc->sc_dev))
    452 		return (ENXIO);
    453 
    454 #ifdef KGDB
    455 	/*
    456 	 * If this is the kgdb port, no other use is permitted.
    457 	 */
    458 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    459 		return (EBUSY);
    460 #endif
    461 
    462 	tp = sc->sc_tty;
    463 
    464 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    465 		return (EBUSY);
    466 
    467 	s = spltty();
    468 
    469 	/*
    470 	 * Do the following iff this is a first open.
    471 	 */
    472 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    473 		struct termios t;
    474 
    475 		tp->t_dev = dev;
    476 
    477 		s2 = splserial();
    478 
    479 		if (sc->enable) {
    480 			if ((*sc->enable)(sc)) {
    481 				splx(s2);
    482 				splx(s);
    483 				printf("%s: device enable failed\n",
    484 				       device_xname(sc->sc_dev));
    485 				return (EIO);
    486 			}
    487 			sc->enabled = 1;
    488 #if 0
    489 /* XXXXXXXXXXXXXXX */
    490 			com_config(sc);
    491 #endif
    492 		}
    493 
    494 		/* Turn on interrupts. */
    495 		SET(sc->sc_ctrl, (Ctrl_UARTE|Ctrl_RIE|Ctrl_RTIE));
    496 		epcom_set(sc);
    497 
    498 #if 0
    499 		/* Fetch the current modem control status, needed later. */
    500 		sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
    501 
    502 		/* Clear PPS capture state on first open. */
    503 		sc->sc_ppsmask = 0;
    504 		sc->ppsparam.mode = 0;
    505 #endif
    506 
    507 		splx(s2);
    508 
    509 		/*
    510 		 * Initialize the termios status to the defaults.  Add in the
    511 		 * sticky bits from TIOCSFLAGS.
    512 		 */
    513 		t.c_ispeed = 0;
    514 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    515 			t.c_ospeed = epcomcn_sc.sc_ospeed;
    516 			t.c_cflag = epcomcn_sc.sc_cflag;
    517 		} else {
    518 			t.c_ospeed = TTYDEF_SPEED;
    519 			t.c_cflag = TTYDEF_CFLAG;
    520 		}
    521 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    522 			SET(t.c_cflag, CLOCAL);
    523 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    524 			SET(t.c_cflag, CRTSCTS);
    525 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    526 			SET(t.c_cflag, MDMBUF);
    527 		/* Make sure epcomparam() will do something. */
    528 		tp->t_ospeed = 0;
    529 		(void) epcomparam(tp, &t);
    530 		tp->t_iflag = TTYDEF_IFLAG;
    531 		tp->t_oflag = TTYDEF_OFLAG;
    532 		tp->t_lflag = TTYDEF_LFLAG;
    533 		ttychars(tp);
    534 		ttsetwater(tp);
    535 
    536 		s2 = splserial();
    537 
    538 		/* Clear the input ring, and unblock. */
    539 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    540 		sc->sc_rbavail = EPCOM_RING_SIZE;
    541 		epcom_iflush(sc);
    542 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    543 
    544 #ifdef COM_DEBUG
    545 		if (epcom_debug)
    546 			comstatus(sc, "epcomopen  ");
    547 #endif
    548 
    549 		splx(s2);
    550 	}
    551 
    552 	splx(s);
    553 
    554 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    555 	if (error)
    556 		goto bad;
    557 
    558 	error = (*tp->t_linesw->l_open)(dev, tp);
    559 	if (error)
    560 		goto bad;
    561 
    562 	return (0);
    563 
    564 bad:
    565 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    566 		/*
    567 		 * We failed to open the device, and nobody else had it opened.
    568 		 * Clean up the state as appropriate.
    569 		 */
    570 		epcom_shutdown(sc);
    571 	}
    572 
    573 	return (error);
    574 }
    575 
    576 int
    577 epcomclose(dev_t dev, int flag, int mode, struct lwp *l)
    578 {
    579 	struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
    580 	struct tty *tp = sc->sc_tty;
    581 
    582 	/* XXX This is for cons.c. */
    583 	if (!ISSET(tp->t_state, TS_ISOPEN))
    584 		return (0);
    585 
    586 	(*tp->t_linesw->l_close)(tp, flag);
    587 	ttyclose(tp);
    588 
    589 	if (COM_ISALIVE(sc) == 0)
    590 		return (0);
    591 
    592 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    593 		/*
    594 		 * Although we got a last close, the device may still be in
    595 		 * use; e.g. if this was the dialout node, and there are still
    596 		 * processes waiting for carrier on the non-dialout node.
    597 		 */
    598 		epcom_shutdown(sc);
    599 	}
    600 
    601 	return (0);
    602 }
    603 
    604 int
    605 epcomread(dev_t dev, struct uio *uio, int flag)
    606 {
    607 	struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
    608 	struct tty *tp = sc->sc_tty;
    609 
    610 	if (COM_ISALIVE(sc) == 0)
    611 		return (EIO);
    612 
    613 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    614 }
    615 
    616 int
    617 epcomwrite(dev_t dev, struct uio *uio, int flag)
    618 {
    619 	struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
    620 	struct tty *tp = sc->sc_tty;
    621 
    622 	if (COM_ISALIVE(sc) == 0)
    623 		return (EIO);
    624 
    625 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    626 }
    627 
    628 int
    629 epcompoll(dev_t dev, int events, struct lwp *l)
    630 {
    631 	struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
    632 	struct tty *tp = sc->sc_tty;
    633 
    634 	if (COM_ISALIVE(sc) == 0)
    635 		return (EIO);
    636 
    637 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    638 }
    639 
    640 struct tty *
    641 epcomtty(dev_t dev)
    642 {
    643 	struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
    644 	struct tty *tp = sc->sc_tty;
    645 
    646 	return (tp);
    647 }
    648 
    649 int
    650 epcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    651 {
    652 	struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
    653 	struct tty *tp = sc->sc_tty;
    654 	int error;
    655 	int s;
    656 
    657 	if (COM_ISALIVE(sc) == 0)
    658 		return (EIO);
    659 
    660 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    661 	if (error != EPASSTHROUGH)
    662 		return (error);
    663 
    664 	error = ttioctl(tp, cmd, data, flag, l);
    665 	if (error != EPASSTHROUGH)
    666 		return (error);
    667 
    668 	error = 0;
    669 
    670 	s = splserial();
    671 
    672 	switch (cmd) {
    673 	case TIOCSBRK:
    674 		epcom_break(sc, 1);
    675 		break;
    676 
    677 	case TIOCCBRK:
    678 		epcom_break(sc, 0);
    679 		break;
    680 
    681 	case TIOCGFLAGS:
    682 		*(int *)data = sc->sc_swflags;
    683 		break;
    684 
    685 	case TIOCSFLAGS:
    686 		error = kauth_authorize_device_tty(l->l_cred,
    687 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    688 		if (error)
    689 			break;
    690 		sc->sc_swflags = *(int *)data;
    691 		break;
    692 
    693 	default:
    694 		error = EPASSTHROUGH;
    695 		break;
    696 	}
    697 
    698 	splx(s);
    699 
    700 	return (error);
    701 }
    702 
    703 /*
    704  * Stop output on a line.
    705  */
    706 void
    707 epcomstop(struct tty *tp, int flag)
    708 {
    709 	struct epcom_softc *sc
    710 		= device_lookup_private(&epcom_cd, COMUNIT(tp->t_dev));
    711 	int s;
    712 
    713 	s = splserial();
    714 	if (ISSET(tp->t_state, TS_BUSY)) {
    715 		/* Stop transmitting at the next chunk. */
    716 		sc->sc_tbc = 0;
    717 		if (!ISSET(tp->t_state, TS_TTSTOP))
    718 			SET(tp->t_state, TS_FLUSH);
    719 	}
    720 	splx(s);
    721 }
    722 
    723 static u_int
    724 cflag2lcrhi(tcflag_t cflag)
    725 {
    726 	u_int lcrhi;
    727 
    728 	switch (cflag & CSIZE) {
    729 	case CS7:
    730 		lcrhi = 0x40;
    731 		break;
    732 	case CS6:
    733 		lcrhi = 0x20;
    734 		break;
    735 	case CS8:
    736 	default:
    737 		lcrhi = 0x60;
    738 		break;
    739 	}
    740 	lcrhi |= (cflag & PARENB) ? LinCtrlHigh_PEN : 0;
    741 	lcrhi |= (cflag & PARODD) ? 0 : LinCtrlHigh_EPS;
    742 	lcrhi |= (cflag & CSTOPB) ? LinCtrlHigh_STP2 : 0;
    743 	lcrhi |= LinCtrlHigh_FEN;  /* FIFO always enabled */
    744 
    745 	return (lcrhi);
    746 }
    747 
    748 static void
    749 epcom_iflush(struct epcom_softc *sc)
    750 {
    751 	bus_space_tag_t iot = sc->sc_iot;
    752 	bus_space_handle_t ioh = sc->sc_ioh;
    753 #ifdef DIAGNOSTIC
    754 	int reg;
    755 #endif
    756 	int timo;
    757 
    758 #ifdef DIAGNOSTIC
    759 	reg = 0xffff;
    760 #endif
    761 	timo = 50000;
    762 	/* flush any pending I/O */
    763 	while ((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_RXFE) == 0
    764 	       && --timo)
    765 #ifdef DIAGNOSTIC
    766 		reg =
    767 #else
    768 			(void)
    769 #endif
    770 			bus_space_read_4(iot, ioh, EPCOM_Data);
    771 #ifdef DIAGNOSTIC
    772 	if (!timo)
    773 		printf("%s: com_iflush timeout %02x\n", device_xname(sc->sc_dev),
    774 		       reg);
    775 #endif
    776 }
    777 
    778 static void
    779 epcom_set(struct epcom_softc *sc)
    780 {
    781 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_LinCtrlLow,
    782 			  sc->sc_lcrlo);
    783 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_LinCtrlMid,
    784 			  sc->sc_lcrmid);
    785 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_LinCtrlHigh,
    786 			  sc->sc_lcrhi);
    787 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_Ctrl,
    788 			  sc->sc_ctrl);
    789 }
    790 
    791 int
    792 epcomcnattach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh,
    793     int ospeed, tcflag_t cflag)
    794 {
    795 	u_int lcrlo, lcrmid, lcrhi, ctrl, pwrcnt;
    796 	bus_space_handle_t syscon_ioh;
    797 
    798 	cn_tab = &epcomcons;
    799 	cn_init_magic(&epcom_cnm_state);
    800 	cn_set_magic("\047\001");
    801 
    802 	epcomcn_sc.sc_iot = iot;
    803 	epcomcn_sc.sc_ioh = ioh;
    804 	epcomcn_sc.sc_hwbase = iobase;
    805 	epcomcn_sc.sc_ospeed = ospeed;
    806 	epcomcn_sc.sc_cflag = cflag;
    807 
    808 	lcrhi = cflag2lcrhi(cflag);
    809 	lcrlo = EPCOMSPEED2BRD(ospeed) & 0xff;
    810 	lcrmid = EPCOMSPEED2BRD(ospeed) >> 8;
    811 	ctrl = Ctrl_UARTE;
    812 
    813 	bus_space_map(iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
    814 		EP93XX_APB_SYSCON_SIZE, 0, &syscon_ioh);
    815 	pwrcnt = bus_space_read_4(iot, syscon_ioh, EP93XX_SYSCON_PwrCnt);
    816 	pwrcnt &= ~(PwrCnt_UARTBAUD);
    817 	bus_space_write_4(iot, syscon_ioh, EP93XX_SYSCON_PwrCnt, pwrcnt);
    818 	bus_space_unmap(iot, syscon_ioh, EP93XX_APB_SYSCON_SIZE);
    819 
    820 	bus_space_write_4(iot, ioh, EPCOM_LinCtrlLow, lcrlo);
    821 	bus_space_write_4(iot, ioh, EPCOM_LinCtrlMid, lcrmid);
    822 	bus_space_write_4(iot, ioh, EPCOM_LinCtrlHigh, lcrhi);
    823 	bus_space_write_4(iot, ioh, EPCOM_Ctrl, ctrl);
    824 
    825 	return (0);
    826 }
    827 
    828 void
    829 epcomcnprobe(struct consdev *cp)
    830 {
    831 	cp->cn_pri = CN_REMOTE;
    832 }
    833 
    834 void
    835 epcomcnpollc(dev_t dev, int on)
    836 {
    837 }
    838 
    839 void
    840 epcomcnputc(dev_t dev, int c)
    841 {
    842 	int			s;
    843 	bus_space_tag_t		iot = epcomcn_sc.sc_iot;
    844 	bus_space_handle_t	ioh = epcomcn_sc.sc_ioh;
    845 
    846 	s = splserial();
    847 
    848 	while((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_TXFF) != 0)
    849 		;
    850 
    851 	bus_space_write_4(iot, ioh, EPCOM_Data, c);
    852 
    853 #ifdef DEBUG
    854 	if (c == '\r') {
    855 		while((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_TXFE) == 0)
    856 			;
    857 	}
    858 #endif
    859 
    860 	splx(s);
    861 }
    862 
    863 int
    864 epcomcngetc(dev_t dev)
    865 {
    866 	int			c, sts;
    867 	int			s;
    868 	bus_space_tag_t		iot = epcomcn_sc.sc_iot;
    869 	bus_space_handle_t	ioh = epcomcn_sc.sc_ioh;
    870 
    871         s = splserial();
    872 
    873 	while((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_RXFE) != 0)
    874 		;
    875 
    876 	c = bus_space_read_4(iot, ioh, EPCOM_Data);
    877 	sts = bus_space_read_4(iot, ioh, EPCOM_RXSts);
    878 	if (ISSET(sts, RXSts_BE)) c = CNC_BREAK;
    879 #ifdef DDB
    880 	extern int db_active;
    881 	if (!db_active)
    882 #endif
    883 	{
    884 		int cn_trapped __unused = 0;
    885 
    886 		cn_check_magic(dev, c, epcom_cnm_state);
    887 	}
    888 	c &= 0xff;
    889 	splx(s);
    890 
    891 	return (c);
    892 }
    893 
    894 inline static void
    895 epcom_txsoft(struct epcom_softc *sc, struct tty *tp)
    896 {
    897 	CLR(tp->t_state, TS_BUSY);
    898 	if (ISSET(tp->t_state, TS_FLUSH))
    899 		CLR(tp->t_state, TS_FLUSH);
    900         else
    901 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
    902 	(*tp->t_linesw->l_start)(tp);
    903 }
    904 
    905 inline static void
    906 epcom_rxsoft(struct epcom_softc *sc, struct tty *tp)
    907 {
    908 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
    909 	u_char *get, *end;
    910 	u_int cc, scc;
    911 	u_char sts;
    912 	int code;
    913 	int s;
    914 
    915 	end = sc->sc_ebuf;
    916 	get = sc->sc_rbget;
    917 	scc = cc = EPCOM_RING_SIZE - sc->sc_rbavail;
    918 #if 0
    919 	if (cc == EPCOM_RING_SIZE) {
    920 		sc->sc_floods++;
    921 		if (sc->sc_errors++ == 0)
    922 			callout_reset(&sc->sc_diag_callout, 60 * hz,
    923 			    comdiag, sc);
    924 	}
    925 #endif
    926 	while (cc) {
    927 		code = get[0];
    928 		sts = get[1];
    929 		if (ISSET(sts, RXSts_OE | RXSts_FE | RXSts_PE | RXSts_BE)) {
    930 #if 0
    931 			if (ISSET(lsr, DR_ROR)) {
    932 				sc->sc_overflows++;
    933 				if (sc->sc_errors++ == 0)
    934 					callout_reset(&sc->sc_diag_callout,
    935 					    60 * hz, comdiag, sc);
    936 			}
    937 #endif
    938 			if (ISSET(sts, (RXSts_FE|RXSts_BE)))
    939 				SET(code, TTY_FE);
    940 			if (ISSET(sts, RXSts_PE))
    941 				SET(code, TTY_PE);
    942 		}
    943 		if ((*rint)(code, tp) == -1) {
    944 			/*
    945 			 * The line discipline's buffer is out of space.
    946 			 */
    947 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
    948 				/*
    949 				 * We're either not using flow control, or the
    950 				 * line discipline didn't tell us to block for
    951 				 * some reason.  Either way, we have no way to
    952 				 * know when there's more space available, so
    953 				 * just drop the rest of the data.
    954 				 */
    955 				get += cc << 1;
    956 				if (get >= end)
    957 					get -= EPCOM_RING_SIZE << 1;
    958 				cc = 0;
    959 			} else {
    960 				/*
    961 				 * Don't schedule any more receive processing
    962 				 * until the line discipline tells us there's
    963 				 * space available (through comhwiflow()).
    964 				 * Leave the rest of the data in the input
    965 				 * buffer.
    966 				 */
    967 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
    968 			}
    969 			break;
    970 		}
    971 		get += 2;
    972 		if (get >= end)
    973 			get = sc->sc_rbuf;
    974 		cc--;
    975 	}
    976 
    977 	if (cc != scc) {
    978 		sc->sc_rbget = get;
    979 		s = splserial();
    980 
    981 		cc = sc->sc_rbavail += scc - cc;
    982 		/* Buffers should be ok again, release possible block. */
    983 		if (cc >= 1) {
    984 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
    985 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
    986 				SET(sc->sc_ctrl, (Ctrl_RIE|Ctrl_RTIE));
    987 				epcom_set(sc);
    988 			}
    989 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
    990 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
    991 #if 0
    992 				com_hwiflow(sc);
    993 #endif
    994 			}
    995 		}
    996 		splx(s);
    997 	}
    998 }
    999 
   1000 static void
   1001 epcomsoft(void* arg)
   1002 {
   1003 	struct epcom_softc *sc = arg;
   1004 
   1005 	if (COM_ISALIVE(sc) == 0)
   1006 		return;
   1007 
   1008 	if (sc->sc_rx_ready) {
   1009 		sc->sc_rx_ready = 0;
   1010 		epcom_rxsoft(sc, sc->sc_tty);
   1011 	}
   1012 	if (sc->sc_tx_done) {
   1013 		sc->sc_tx_done = 0;
   1014 		epcom_txsoft(sc, sc->sc_tty);
   1015 	}
   1016 }
   1017 
   1018 int
   1019 epcomintr(void* arg)
   1020 {
   1021 	struct epcom_softc *sc = arg;
   1022 	bus_space_tag_t iot = sc->sc_iot;
   1023 	bus_space_handle_t ioh = sc->sc_ioh;
   1024 	u_char *put, *end;
   1025 	u_int cc;
   1026 	u_int flagr;
   1027 	uint32_t c, csts;
   1028 
   1029 	(void) bus_space_read_4(iot, ioh, EPCOM_IntIDIntClr);
   1030 
   1031 	if (COM_ISALIVE(sc) == 0)
   1032 		panic("intr on disabled epcom");
   1033 
   1034 	flagr = bus_space_read_4(iot, ioh, EPCOM_Flag);
   1035 
   1036 	end = sc->sc_ebuf;
   1037 	put = sc->sc_rbput;
   1038 	cc = sc->sc_rbavail;
   1039 
   1040 	if (!(ISSET(flagr, Flag_RXFE))) {
   1041 		if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1042 			while (cc > 0) {
   1043 				if (ISSET(flagr, Flag_RXFE))
   1044 					break;
   1045 				c = bus_space_read_4(iot, ioh, EPCOM_Data);
   1046 				csts = bus_space_read_4(iot, ioh, EPCOM_RXSts);
   1047 				if (ISSET(csts, RXSts_BE)) {
   1048 					int cn_trapped = 0;
   1049 
   1050 					cn_check_magic(sc->sc_tty->t_dev,
   1051 					  CNC_BREAK, epcom_cnm_state);
   1052 					if (cn_trapped)
   1053 						goto next;
   1054 #if defined(KGDB) && !defined(DDB)
   1055 					if (ISSET(sc->sc_hwflags, COM_HW_KGDB)){
   1056 						kgdb_connect(1);
   1057 						goto next;
   1058 					}
   1059 #endif
   1060 				} else {
   1061 					int cn_trapped = 0;
   1062 
   1063 					cn_check_magic(sc->sc_tty->t_dev,
   1064 					  (c & 0xff), epcom_cnm_state);
   1065 					if (cn_trapped)
   1066 						goto next;
   1067 				}
   1068 
   1069 
   1070 				put[0] = c & 0xff;
   1071 				put[1] = csts & 0xf;
   1072 				put += 2;
   1073 				if (put >= end)
   1074 					put = sc->sc_rbuf;
   1075 				cc--;
   1076 			next:
   1077 				flagr = bus_space_read_4(iot, ioh, EPCOM_Flag);
   1078 			}
   1079 
   1080 			/*
   1081 			 * Current string of incoming characters ended because
   1082 			 * no more data was available or we ran out of space.
   1083 			 * Schedule a receive event if any data was received.
   1084 			 * If we're out of space, turn off receive interrupts.
   1085 			 */
   1086 			sc->sc_rbput = put;
   1087 			sc->sc_rbavail = cc;
   1088 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1089 				sc->sc_rx_ready = 1;
   1090 
   1091 			/*
   1092 			 * See if we are in danger of overflowing a buffer. If
   1093 			 * so, use hardware flow control to ease the pressure.
   1094 			 */
   1095 
   1096 			/* but epcom cannot. X-( */
   1097 
   1098 			/*
   1099 			 * If we're out of space, disable receive interrupts
   1100 			 * until the queue has drained a bit.
   1101 			 */
   1102 			if (!cc) {
   1103 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1104 				CLR(sc->sc_ctrl, (Ctrl_RIE|Ctrl_RTIE));
   1105 				epcom_set(sc);
   1106 			}
   1107 		} else {
   1108 #ifdef DIAGNOSTIC
   1109 			panic("epcomintr: we shouldn't reach here");
   1110 #endif
   1111 			CLR(sc->sc_ctrl, (Ctrl_RIE|Ctrl_RTIE));
   1112 			epcom_set(sc);
   1113 		}
   1114 	}
   1115 
   1116 	/*
   1117 	 * Done handling any receive interrupts. See if data can be
   1118 	 * transmitted as well. Schedule tx done event if no data left
   1119 	 * and tty was marked busy.
   1120 	 */
   1121 
   1122 	if (!ISSET(flagr, Flag_TXFF) && sc->sc_tbc > 0) {
   1123 		/* Output the next chunk of the contiguous buffer, if any. */
   1124 		epcom_filltx(sc);
   1125 	} else {
   1126 		/* Disable transmit completion interrupts if necessary. */
   1127 		if (ISSET(sc->sc_ctrl, Ctrl_TIE)) {
   1128 			CLR(sc->sc_ctrl, Ctrl_TIE);
   1129 			epcom_set(sc);
   1130 		}
   1131 		if (sc->sc_tx_busy) {
   1132 			sc->sc_tx_busy = 0;
   1133 			sc->sc_tx_done = 1;
   1134 		}
   1135 	}
   1136 
   1137 	/* Wake up the poller. */
   1138 	softint_schedule(sc->sc_si);
   1139 
   1140 #if 0 /* XXX: broken */
   1141 #ifdef RND_COM
   1142 	rnd_add_uint32(&sc->rnd_source, intr ^ flagr);
   1143 #endif
   1144 #endif
   1145 	return (1);
   1146 }
   1147