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