Home | History | Annotate | Line # | Download | only in xscale
ixp425_com.c revision 1.3
      1 /*	$NetBSD: ixp425_com.c,v 1.3 2003/05/31 06:24:18 ichiro Exp $ */
      2 /*
      3  * Copyright (c) 2003
      4  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Ichiro FUKUHARA.
     18  * 4. The name of the company nor the name of the author may be used to
     19  *    endorse or promote products derived from this software without specific
     20  *    prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     26  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1991 The Regents of the University of California.
     37  * All rights reserved.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *      This product includes software developed by the University of
     50  *      California, Berkeley and its contributors.
     51  * 4. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *      @(#)com.c       7.5 (Berkeley) 5/16/91
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: ixp425_com.c,v 1.3 2003/05/31 06:24:18 ichiro Exp $");
     72 
     73 #include "opt_ddb.h"
     74 #include "opt_kgdb.h"
     75 
     76 #include "rnd.h"
     77 #if NRND > 0 && defined(RND_COM)
     78 #include <sys/rnd.h>
     79 #endif
     80 
     81 #include <sys/param.h>
     82 #include <sys/systm.h>
     83 #include <sys/types.h>
     84 #include <sys/conf.h>
     85 #include <sys/file.h>
     86 #include <sys/device.h>
     87 #include <sys/kernel.h>
     88 #include <sys/malloc.h>
     89 #include <sys/tty.h>
     90 #include <sys/uio.h>
     91 #include <sys/vnode.h>
     92 
     93 #include <machine/intr.h>
     94 #include <machine/bus.h>
     95 
     96 #include <arm/xscale/ixp425reg.h>
     97 #include <arm/xscale/ixp425_comvar.h>
     98 
     99 #include <dev/cons.h>
    100 
    101 #include "ixpcom.h"
    102 
    103 static int	ixp4xx_comparam(struct tty *, struct termios *);
    104 static void	ixp4xx_comstart(struct tty *);
    105 static int	ixp4xx_comhwiflow(struct tty *, int);
    106 
    107 static u_int	cflag2lcr(tcflag_t);
    108 static void	ixp4xx_com_modem(struct ixp4xx_com_softc *, int);
    109 static void	ixp4xx_com_iflush(struct ixp4xx_com_softc *);
    110 static void	tiocm_to_ixp4xx_com(struct ixp4xx_com_softc *, u_long, int);
    111 static int	ixp4xx_com_to_tiocm(struct ixp4xx_com_softc *);
    112 
    113 static void	ixp4xx_com_set_cr(struct ixp4xx_com_softc *);
    114 
    115 static void	ixp4xxcomsoft(void *);
    116 inline static void	ixp4xx_com_txsoft(struct ixp4xx_com_softc *, struct tty *);
    117 inline static void	ixp4xx_com_rxsoft(struct ixp4xx_com_softc *, struct tty *);
    118 
    119 int		ixp4xx_cominit(bus_space_tag_t, bus_space_handle_t, int, int, tcflag_t);
    120 
    121 int             ixp4xx_comcngetc(dev_t);
    122 void            ixp4xx_comcnputc(dev_t, int);
    123 void            ixp4xx_comcnpollc(dev_t, int);
    124 void            ixp4xx_comcnprobe(struct consdev *);
    125 void            ixp4xx_comcninit(struct consdev *);
    126 
    127 static struct ixp4xx_com_cons_softc {
    128 	bus_space_tag_t		sc_iot;
    129 	bus_space_handle_t	sc_ioh;
    130 	bus_addr_t		sc_baseaddr;
    131 	int			sc_ospeed;
    132 	tcflag_t		sc_cflag;
    133 	int			sc_attached;
    134 } ixp4xx_comcn_sc;
    135 
    136 static struct cnm_state ixp4xx_com_cnm_state;
    137 
    138 struct ixp4xx_com_softc* ixp4xx_com_sc = NULL;
    139 
    140 extern struct cfdriver ixpcom_cd;
    141 
    142 dev_type_open(ixp4xx_comopen);
    143 dev_type_close(ixp4xx_comclose);
    144 dev_type_read(ixp4xx_comread);
    145 dev_type_write(ixp4xx_comwrite);
    146 dev_type_ioctl(ixp4xx_comioctl);
    147 dev_type_stop(ixp4xx_comstop);
    148 dev_type_tty(ixp4xx_comtty);
    149 dev_type_poll(ixp4xx_compoll);
    150 
    151 const struct cdevsw ixpcom_cdevsw = {
    152 	ixp4xx_comopen, ixp4xx_comclose, ixp4xx_comread, ixp4xx_comwrite,
    153 	ixp4xx_comioctl, ixp4xx_comstop, ixp4xx_comtty, ixp4xx_compoll,
    154 	nommap, ttykqfilter, D_TTY
    155 };
    156 
    157 #define COMUNIT_MASK    0x7ffff
    158 #define COMDIALOUT_MASK 0x80000
    159 
    160 #define COMUNIT(x)      (minor(x) & COMUNIT_MASK)
    161 #define COMDIALOUT(x)   (minor(x) & COMDIALOUT_MASK)
    162 
    163 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \
    164 			ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
    165 
    166 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
    167 
    168 #define COM_LOCK(sc);
    169 #define COM_UNLOCK(sc);
    170 
    171 #ifndef COM_TOLERANCE
    172 #define COM_TOLERANCE 30	/* XXX: baud rate tolerance, in 0.1% units */
    173 #endif
    174 
    175 #define SET(t, f)       (t) |= (f)
    176 #define CLR(t, f)       (t) &= ~(f)
    177 #define ISSET(t, f)     ((t) & (f))
    178 
    179 #ifdef COM_DEBUG
    180 int com_debug = 0;
    181 #endif
    182 
    183 int
    184 ixp4xx_comspeed(long speed, long frequency)
    185 {
    186 #define divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    187 
    188 	int x, err;
    189 
    190 	if (speed <= 0)
    191 		return (-1);
    192 	x = divrnd(frequency / 16, speed);
    193 	if (x <= 0)
    194 		return (-1);
    195 	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
    196 	if (err < 0)
    197 		err = -err;
    198 	if (err > COM_TOLERANCE)
    199 		return (-1);
    200 	return (x);
    201 #undef  divrnd
    202 }
    203 
    204 #if XXX
    205 static void
    206 ixp4xx_enable_debugport(struct ixp4xx_com_softc *sc)
    207 {
    208 	int s;
    209 
    210 	/* Turn on line break interrupt, set carrier. */
    211 	s = splserial();
    212 	COM_LOCK(sc);
    213 	SET(sc->sc_ier, IER_RAVIE | IER_UUE);
    214 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_IER, sc->sc_ier);
    215 	SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
    216 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_MCR, sc->sc_mcr);
    217 	COM_UNLOCK(sc);
    218 	splx(s);
    219 }
    220 #endif
    221 
    222 void
    223 ixp4xx_com_attach_subr(struct ixp4xx_com_softc *sc)
    224 {
    225 	bus_space_tag_t iot = sc->sc_iot;
    226 	bus_space_handle_t ioh = sc->sc_ioh;
    227 
    228 	struct tty *tp;
    229 
    230 	ixp4xx_com_sc = sc;
    231 #if 0
    232 	callout_init(&sc->sc_diag_callout);
    233 #endif
    234 	/* Disable interrupts before configuring the device. */
    235 	SET(sc->sc_ier, IER_UUE);
    236 	bus_space_write_4(iot, ioh, IXP425_UART_IER, sc->sc_ier);
    237 
    238 	if (iot == ixp4xx_comcn_sc.sc_iot
    239 		&& sc->sc_baseaddr == ixp4xx_comcn_sc.sc_baseaddr) {
    240 		ixp4xx_comcn_sc.sc_attached = 1;
    241 
    242 		/* Make sure the console is always "hardwired". */
    243 		delay(20000);		/* wait for output to finish */
    244 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    245 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    246 	}
    247 	bus_space_write_4(iot, ioh, IXP425_UART_FCR,
    248 			  FCR_TRIGGER_8 | FCR_RESETTF | FCR_RESETRF | FCR_ENABLE);
    249 #ifdef KGDB
    250 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
    251                 ixp4xx_com_kgdb_attached = 1;
    252                 printf("%s: kgdb\n", sc->sc_dev.dv_xname);
    253                 ixp4xx_enable_debugport(sc);
    254                 return;
    255         }
    256 #endif
    257 
    258 	tp = ttymalloc();
    259 	tp->t_oproc = ixp4xx_comstart;
    260 	tp->t_param = ixp4xx_comparam;
    261 	tp->t_hwiflow = ixp4xx_comhwiflow;
    262 
    263 	sc->sc_tty = tp;
    264 	sc->sc_rbuf = malloc(IXPCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    265 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    266 	sc->sc_rbavail = IXPCOM_RING_SIZE;
    267 	if (sc->sc_rbuf == NULL) {
    268 		printf("%s: unable to allocate ring buffer\n",
    269 			sc->sc_dev.dv_xname);
    270 		return;
    271 	}
    272 	sc->sc_ebuf = sc->sc_rbuf + (IXPCOM_RING_SIZE << 1);
    273 
    274 	tty_attach(tp);
    275 
    276 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    277 		int maj;
    278 
    279 		/* locate the major number */
    280 		maj = cdevsw_lookup_major(&ixpcom_cdevsw);
    281 		tp->t_dev = cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
    282 		printf("%s: console (major=%d)\n", sc->sc_dev.dv_xname, maj);
    283 	}
    284 
    285 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, ixp4xxcomsoft, sc);
    286 
    287 #if NRND > 0 && defined(RND_COM)
    288 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    289 			  RND_TYPE_TTY, 0);
    290 #endif
    291 
    292 	/* if there are no enable/disable functions, assume the device
    293 	   is always enabled */
    294 	if (!sc->enable)
    295 		sc->enabled = 1;
    296 
    297         SET(sc->sc_hwflags, COM_HW_DEV_OK);
    298 }
    299 
    300 static int
    301 ixp4xx_comparam(struct tty *tp, struct termios *t)
    302 {
    303 	struct ixp4xx_com_softc *sc =
    304 		device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
    305         int ospeed;
    306         u_char lcr;
    307         int s;
    308 
    309 	ospeed = ixp4xx_comspeed(t->c_ospeed, sc->sc_frequency);
    310 
    311 	/* Check requested parameters. */
    312 	if (ospeed < 0)
    313 		return EINVAL;
    314 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    315 		return EINVAL;
    316 
    317 	/*
    318 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    319 	 * is always active.
    320 	 */
    321 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    322 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    323 		SET(t->c_cflag, CLOCAL);
    324 		CLR(t->c_cflag, HUPCL);
    325 	}
    326 
    327 	/*
    328 	 * If there were no changes, don't do anything.  This avoids dropping
    329 	 * input and improves performance when all we did was frob things like
    330 	 * VMIN and VTIME.
    331 	 */
    332 	if (tp->t_ospeed == t->c_ospeed &&
    333 	    tp->t_cflag == t->c_cflag)
    334 		return (0);
    335 
    336 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
    337 
    338 	s = splserial();
    339 	COM_LOCK(sc);
    340 
    341 	sc->sc_lcr = lcr;
    342 
    343         /*
    344          * If we're not in a mode that assumes a connection is present, then
    345          * ignore carrier changes.
    346          */
    347         if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
    348                 sc->sc_msr_dcd = 0;
    349         else
    350                 sc->sc_msr_dcd = MSR_DCD;
    351         /*
    352          * Set the flow control pins depending on the current flow control
    353          * mode.
    354          */
    355         if (ISSET(t->c_cflag, CRTSCTS)) {
    356                 sc->sc_mcr_dtr = MCR_DTR;
    357                 sc->sc_mcr_rts = MCR_RTS;
    358                 sc->sc_msr_cts = MSR_CTS;
    359         } else if (ISSET(t->c_cflag, MDMBUF)) {
    360                 /*
    361                  * For DTR/DCD flow control, make sure we don't toggle DTR for
    362                  * carrier detection.
    363                  */
    364                 sc->sc_mcr_dtr = 0;
    365                 sc->sc_mcr_rts = MCR_DTR;
    366                 sc->sc_msr_cts = MSR_DCD;
    367         } else {
    368                 /*
    369                  * If no flow control, then always set RTS.  This will make
    370                  * the other side happy if it mistakenly thinks we're doing
    371                  * RTS/CTS flow control.
    372                  */
    373                 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
    374                 sc->sc_mcr_rts = 0;
    375                 sc->sc_msr_cts = 0;
    376                 if (ISSET(sc->sc_mcr, MCR_DTR))
    377                         SET(sc->sc_mcr, MCR_RTS);
    378                 else
    379                         CLR(sc->sc_mcr, MCR_RTS);
    380         }
    381         sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
    382 
    383 	sc->sc_dlbl = ospeed;
    384 	sc->sc_dlbh = ospeed >> 8;
    385 
    386         /*
    387          * Set the FIFO threshold based on the receive speed.
    388          *
    389          *  * If it's a low speed, it's probably a mouse or some other
    390          *    interactive device, so set the threshold low.
    391          *  * If it's a high speed, trim the trigger level down to prevent
    392          *    overflows.
    393          *  * Otherwise set it a bit higher.
    394          */
    395 #if NOTYET
    396 	if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
    397 		sc->sc_fcr = FCR_ENABLE |
    398 			(t->c_ospeed <= 1200 ? FCR_TRIGGER_1 :
    399 			 t->c_ospeed <= 38400 ? FCR_TRIGGER_16 : FCR_TRIGGER_8);
    400 	else
    401 		sc->sc_fcr = 0;
    402 #endif
    403 
    404 	/* And copy to tty. */
    405 	tp->t_ispeed = 0;
    406 	tp->t_ospeed = t->c_ospeed;
    407 	tp->t_cflag = t->c_cflag;
    408 
    409 	if (!sc->sc_heldchange) {
    410 		if (sc->sc_tx_busy) {
    411 			sc->sc_heldtbc = sc->sc_tbc;
    412 			sc->sc_tbc = 0;
    413 			sc->sc_heldchange = 1;
    414 		} else
    415 			ixp4xx_com_set_cr(sc);
    416 	}
    417 
    418 	COM_UNLOCK(sc);
    419 	splx(s);
    420 
    421 	/*
    422 	 * Update the tty layer's idea of the carrier bit.
    423 	 * We tell tty the carrier is always on.
    424 	 */
    425 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    426 
    427 #ifdef COM_DEBUG
    428 	if (com_debug)
    429 		comstatus(sc, "comparam ");
    430 #endif
    431 
    432 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    433 		if (sc->sc_tx_stopped) {
    434 			sc->sc_tx_stopped = 0;
    435 			ixp4xx_comstart(tp);
    436 		}
    437 	}
    438 
    439 	return (0);
    440 }
    441 
    442 void
    443 ixp4xx_com_set_cr(struct ixp4xx_com_softc *sc)
    444 {
    445 	bus_space_tag_t iot = sc->sc_iot;
    446 	bus_space_handle_t ioh = sc->sc_ioh;
    447 
    448 	ixp4xx_com_iflush(sc);
    449 
    450 	bus_space_write_4(iot, ioh, IXP425_UART_LCR, sc->sc_lcr | LCR_DLAB);
    451 	bus_space_write_4(iot, ioh, IXP425_UART_DLL, sc->sc_dlbl);
    452 	bus_space_write_4(iot, ioh, IXP425_UART_DLH, sc->sc_dlbh);
    453 	bus_space_write_4(iot, ioh, IXP425_UART_LCR, sc->sc_lcr);
    454 	bus_space_write_4(iot, ioh, IXP425_UART_MCR, sc->sc_mcr_active = sc->sc_mcr);
    455 	bus_space_write_4(iot, ioh, IXP425_UART_FCR, sc->sc_fcr);
    456 	bus_space_write_4(iot, ioh, IXP425_UART_IER, sc->sc_ier | IER_UUE);
    457 }
    458 
    459 static int
    460 ixp4xx_comhwiflow(struct tty *tp, int block)
    461 {
    462         return (0);
    463 }
    464 
    465 static void
    466 ixp4xx_com_filltx(struct ixp4xx_com_softc *sc)
    467 {
    468 	bus_space_tag_t iot = sc->sc_iot;
    469 	bus_space_handle_t ioh = sc->sc_ioh;
    470 	int n;
    471 
    472 	n = 0;
    473 	while (bus_space_read_4(iot, ioh, IXP425_UART_LSR) & LSR_TDRQ) {
    474 		if (n >= sc->sc_tbc)
    475 			break;
    476 		bus_space_write_4(iot, ioh, IXP425_UART_DATA,
    477 				  0xff & *(sc->sc_tba + n));
    478 		n++;
    479 	}
    480 	sc->sc_tbc -= n;
    481 }
    482 
    483 static void
    484 ixp4xx_comstart(struct tty *tp)
    485 {
    486 	struct ixp4xx_com_softc *sc
    487 		= device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
    488 	int s;
    489 
    490 	if (COM_ISALIVE(sc) == 0)
    491 		return;
    492 
    493 	s = spltty();
    494 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    495 		goto out;
    496 	if (sc->sc_tx_stopped)
    497 		goto out;
    498 
    499 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    500 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    501 			CLR(tp->t_state, TS_ASLEEP);
    502 			wakeup(&tp->t_outq);
    503 		}
    504 		selwakeup(&tp->t_wsel);
    505 		if (tp->t_outq.c_cc == 0)
    506 			goto out;
    507 	}
    508 
    509 	/* Grab the first contiguous region of buffer space. */
    510 	{
    511 		u_char *tba;
    512 		int tbc;
    513 
    514 		tba = tp->t_outq.c_cf;
    515 		tbc = ndqb(&tp->t_outq, 0);
    516 
    517 		(void)splserial();
    518 		COM_LOCK(sc);
    519 
    520 		sc->sc_tba = tba;
    521 		sc->sc_tbc = tbc;
    522 	}
    523 
    524 	SET(tp->t_state, TS_BUSY);
    525 	sc->sc_tx_busy = 1;
    526 
    527 	/* Enable transmit completion interrupts if necessary. */
    528 	if (!ISSET(sc->sc_ier, IER_TIE)) {
    529 		SET(sc->sc_ier, IER_TIE);
    530 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_IER,
    531 			sc->sc_ier | IER_UUE);
    532 	}
    533 
    534 	/* Output the first chunk of the contiguous buffer. */
    535 	ixp4xx_com_filltx(sc);
    536 
    537 	COM_UNLOCK(sc);
    538 out:
    539 	splx(s);
    540 	return;
    541 }
    542 
    543 static void
    544 ixp4xx_com_break(struct ixp4xx_com_softc *sc, int onoff)
    545 {
    546 	if (onoff)
    547 		SET(sc->sc_lcr, LCR_SBREAK);
    548 	else
    549 		CLR(sc->sc_lcr, LCR_SBREAK);
    550 
    551 	if (!sc->sc_heldchange) {
    552 		if (sc->sc_tx_busy) {
    553 			sc->sc_heldtbc = sc->sc_tbc;
    554 			sc->sc_tbc = 0;
    555 			sc->sc_heldchange = 1;
    556 		} else
    557 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_LCR,
    558 					sc->sc_lcr);
    559 	}
    560 }
    561 
    562 static void
    563 ixp4xx_com_shutdown(struct ixp4xx_com_softc *sc)
    564 {
    565 	struct tty *tp = sc->sc_tty;
    566 	int s;
    567 
    568 	s = splserial();
    569 	COM_LOCK(sc);
    570 
    571 	/* Clear any break condition set with TIOCSBRK. */
    572 	ixp4xx_com_break(sc, 0);
    573 
    574 	/*
    575 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    576 	 * notice even if we immediately open the port again.
    577 	 * Avoid tsleeping above splhigh().
    578 	 */
    579 	if (ISSET(tp->t_cflag, HUPCL)) {
    580 		ixp4xx_com_modem(sc, 0);
    581 		COM_UNLOCK(sc);
    582 		splx(s);
    583 		/* XXX tsleep will only timeout */
    584 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    585 		s = splserial();
    586 		COM_LOCK(sc);
    587 	}
    588 
    589 	/* Turn off interrupts. */
    590 	sc->sc_ier &= ~(IER_RAVIE | IER_TIE);
    591 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_IER,
    592 		sc->sc_ier | IER_UUE);
    593 
    594 	if (sc->disable) {
    595 #ifdef DIAGNOSTIC
    596 		if (!sc->enabled)
    597 			panic("ixpcom_shutdown: not enabled?");
    598 #endif
    599 		(*sc->disable)(sc);
    600 		sc->enabled = 0;
    601 	}
    602 	COM_UNLOCK(sc);
    603 	splx(s);
    604 }
    605 
    606 int
    607 ixp4xx_comopen(dev, flag, mode, p)
    608         dev_t dev;
    609         int flag, mode;
    610         struct proc *p;
    611 {
    612 	struct ixp4xx_com_softc *sc;
    613 	struct tty *tp;
    614 	int s, s2;
    615 	int error;
    616 
    617 	sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
    618 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    619 		sc->sc_rbuf == NULL)
    620 		return (ENXIO);
    621 
    622 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    623 		return (ENXIO);
    624 
    625 #ifdef KGDB
    626 	/*
    627 	 * If this is the kgdb port, no other use is permitted.
    628 	 */
    629 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    630 		return (EBUSY);
    631 #endif
    632 
    633 	tp = sc->sc_tty;
    634 
    635 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    636 	    ISSET(tp->t_state, TS_XCLUDE) &&
    637 		p->p_ucred->cr_uid != 0)
    638 		return (EBUSY);
    639 
    640 	s = spltty();
    641 
    642 	/*
    643 	 * Do the following iff this is a first open.
    644 	 */
    645 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    646 		struct termios t;
    647 
    648 		tp->t_dev = dev;
    649 
    650 		s2 = splserial();
    651 		COM_LOCK(sc);
    652 
    653 		if (sc->enable) {
    654 			if ((*sc->enable)(sc)) {
    655 				COM_UNLOCK(sc);
    656 				splx(s2);
    657 				splx(s);
    658 				printf("%s: device enable failed\n",
    659 					sc->sc_dev.dv_xname);
    660 				return (EIO);
    661 			}
    662 			sc->enabled = 1;
    663 		}
    664 		/* Turn on interrupts. */
    665 		SET(sc->sc_ier, IER_RAVIE | IER_RLSE | IER_RIE);
    666 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_IER,
    667 			sc->sc_ier);
    668 #if 0
    669 		/* Fetch the current modem control status, needed later. */
    670 		sc->sc_msr = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    671 					      IXP425_UART_MSR);
    672 #endif
    673 		COM_UNLOCK(sc);
    674 		splx(s2);
    675 
    676 		/*
    677                  * Initialize the termios status to the defaults.  Add in the
    678                  * sticky bits from TIOCSFLAGS.
    679                  */
    680                 t.c_ispeed = 0;
    681                 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    682                         t.c_ospeed = ixp4xx_comcn_sc.sc_ospeed;
    683                         t.c_cflag = ixp4xx_comcn_sc.sc_cflag;
    684                 } else {
    685                         t.c_ospeed = TTYDEF_SPEED;
    686                         t.c_cflag = TTYDEF_CFLAG;
    687                 }
    688                 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    689                         SET(t.c_cflag, CLOCAL);
    690                 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    691                         SET(t.c_cflag, CRTSCTS);
    692                 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    693                         SET(t.c_cflag, MDMBUF);
    694                 /* Make sure ixpcomparam() will do something. */
    695                 tp->t_ospeed = 0;
    696                 (void) ixp4xx_comparam(tp, &t);
    697                 tp->t_iflag = TTYDEF_IFLAG;
    698                 tp->t_oflag = TTYDEF_OFLAG;
    699                 tp->t_lflag = TTYDEF_LFLAG;
    700                 ttychars(tp);
    701                 ttsetwater(tp);
    702 
    703                 s2 = splserial();
    704                 COM_LOCK(sc);
    705 
    706 		/*
    707 		 * Turn on DTR.  We must always do this, even if carrier is not
    708 		 * present, because otherwise we'd have to use TIOCSDTR
    709 		 * immediately after setting CLOCAL, which applications do not
    710 		 * expect.  We always assert DTR while the device is open
    711 		 * unless explicitly requested to deassert it.
    712 		 */
    713 		ixp4xx_com_modem(sc, 1);
    714                 /* Clear the input ring, and unblock. */
    715 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    716 		sc->sc_rbavail = IXPCOM_RING_SIZE;
    717 		ixp4xx_com_iflush(sc);
    718 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    719 
    720 #ifdef COM_DEBUG
    721 		if (com_debug)
    722 			comstatus(sc, "ixp4xx_comopen  ");
    723 #endif
    724 
    725 		COM_UNLOCK(sc);
    726 		splx(s2);
    727 	}
    728 	splx(s);
    729 
    730 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    731 	if (error)
    732 		goto bad;
    733 
    734 	error = (*tp->t_linesw->l_open)(dev, tp);
    735 	if (error)
    736 		goto bad;
    737 
    738 	return (0);
    739 
    740 bad:
    741 	panic("bad");
    742 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    743 		/*
    744 		 * We failed to open the device, and nobody else had it opened.
    745 		 * Clean up the state as appropriate.
    746 		 */
    747 		ixp4xx_com_shutdown(sc);
    748 	}
    749 	return (error);
    750 }
    751 
    752 int
    753 ixp4xx_comclose(dev, flag, mode, p)
    754         dev_t dev;
    755         int flag, mode;
    756         struct proc *p;
    757 {
    758 	struct ixp4xx_com_softc *sc =
    759 		device_lookup(&ixpcom_cd, COMUNIT(dev));
    760 	struct tty *tp = sc->sc_tty;
    761 
    762 	/* XXX This is for cons.c. */
    763 	if (!ISSET(tp->t_state, TS_ISOPEN))
    764 		return (0);
    765 
    766 	(*tp->t_linesw->l_close)(tp, flag);
    767 	ttyclose(tp);
    768 
    769 	if (COM_ISALIVE(sc) == 0)
    770 		return (0);
    771 
    772 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    773 		/*
    774 		 * Although we got a last close, the device may still be in
    775 		 * use; e.g. if this was the dialout node, and there are still
    776 		 * processes waiting for carrier on the non-dialout node.
    777 		 */
    778 		ixp4xx_com_shutdown(sc);
    779 	}
    780 
    781 	return (0);
    782 }
    783 
    784 int
    785 ixp4xx_comread(dev, uio, flag)
    786         dev_t dev;
    787         struct uio *uio;
    788         int flag;
    789 {
    790 	struct ixp4xx_com_softc *sc =
    791 		device_lookup(&ixpcom_cd, COMUNIT(dev));
    792 	struct tty *tp = sc->sc_tty;
    793 
    794 	if (COM_ISALIVE(sc) == 0)
    795 		return (EIO);
    796 
    797 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    798 }
    799 
    800 int
    801 ixp4xx_comwrite(dev, uio, flag)
    802         dev_t dev;
    803         struct uio *uio;
    804         int flag;
    805 {
    806 	struct ixp4xx_com_softc *sc =
    807 		device_lookup(&ixpcom_cd, COMUNIT(dev));
    808 	struct tty *tp = sc->sc_tty;
    809 
    810 	if (COM_ISALIVE(sc) == 0)
    811 		return (EIO);
    812 
    813 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    814 }
    815 
    816 int
    817 ixp4xx_compoll(dev, events, p)
    818         dev_t dev;
    819         int events;
    820         struct proc *p;
    821 {
    822 	struct ixp4xx_com_softc *sc =
    823 		device_lookup(&ixpcom_cd, COMUNIT(dev));
    824 	struct tty *tp = sc->sc_tty;
    825 
    826 	if (COM_ISALIVE(sc) == 0)
    827 		return (EIO);
    828 
    829 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    830 }
    831 
    832 struct tty *
    833 ixp4xx_comtty(dev_t dev)
    834 {
    835 	struct ixp4xx_com_softc *sc =
    836 		device_lookup(&ixpcom_cd, COMUNIT(dev));
    837 	struct tty *tp = sc->sc_tty;
    838 
    839 	return (tp);
    840 }
    841 
    842 int
    843 ixp4xx_comioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    844 {
    845 	struct ixp4xx_com_softc *sc =
    846 		device_lookup(&ixpcom_cd, COMUNIT(dev));
    847 	struct tty *tp = sc->sc_tty;
    848 	int error;
    849 	int s;
    850 
    851 	if (COM_ISALIVE(sc) == 0)
    852 		return (EIO);
    853 
    854 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    855 	if (error != EPASSTHROUGH)
    856 		return (error);
    857 
    858 	error = ttioctl(tp, cmd, data, flag, p);
    859 	if (error != EPASSTHROUGH)
    860 		return (error);
    861 
    862 	error = 0;
    863 
    864 	s = splserial();
    865 	COM_LOCK(sc);
    866 
    867 	switch (cmd) {
    868 	case TIOCSBRK:
    869 		ixp4xx_com_break(sc, 1);
    870 		break;
    871 
    872 	case TIOCCBRK:
    873 		ixp4xx_com_break(sc, 0);
    874 		break;
    875 
    876 	case TIOCSDTR:
    877 		ixp4xx_com_modem(sc, 1);
    878 		break;
    879 
    880 	case TIOCCDTR:
    881 		ixp4xx_com_modem(sc, 0);
    882 		break;
    883 
    884 	case TIOCGFLAGS:
    885 		*(int *)data = sc->sc_swflags;
    886 		break;
    887 
    888 	case TIOCSFLAGS:
    889 		error = suser(p->p_ucred, &p->p_acflag);
    890 		if (error)
    891 			break;
    892 		sc->sc_swflags = *(int *)data;
    893 		break;
    894 
    895 	case TIOCMSET:
    896 	case TIOCMBIS:
    897 	case TIOCMBIC:
    898 		tiocm_to_ixp4xx_com(sc, cmd, *(int *)data);
    899 		break;
    900 
    901 	case TIOCMGET:
    902 		*(int *)data = ixp4xx_com_to_tiocm(sc);
    903 		break;
    904 
    905 	default:
    906 		error = EPASSTHROUGH;
    907 		break;
    908 	}
    909 
    910 	COM_UNLOCK(sc);
    911 	splx(s);
    912 
    913 #ifdef COM_DEBUG
    914 	if (com_debug)
    915 		comstatus(sc, "comioctl ");
    916 #endif
    917 
    918 	return (error);
    919 }
    920 
    921 void
    922 ixp4xx_comstop(struct tty *tp, int flag)
    923 {
    924 	struct ixp4xx_com_softc *sc =
    925 		device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
    926 	int s;
    927 
    928 	s = splserial();
    929 	COM_LOCK(sc);
    930 	if (ISSET(tp->t_state, TS_BUSY)) {
    931 	/* Stop transmitting at the next chunk. */
    932 		sc->sc_tbc = 0;
    933 		sc->sc_heldtbc = 0;
    934 		if (!ISSET(tp->t_state, TS_TTSTOP))
    935 		SET(tp->t_state, TS_FLUSH);
    936 	}
    937 	COM_UNLOCK(sc);
    938 	splx(s);
    939 }
    940 
    941 static void
    942 ixp4xx_com_modem(struct ixp4xx_com_softc *sc, int onoff)
    943 {
    944 
    945 	if (sc->sc_mcr_dtr == 0)
    946 		return;
    947 
    948 	if (onoff)
    949 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
    950 	else
    951 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
    952 
    953 	if (!sc->sc_heldchange) {
    954 		if (sc->sc_tx_busy) {
    955 			sc->sc_heldtbc = sc->sc_tbc;
    956 			sc->sc_tbc = 0;
    957 			sc->sc_heldchange = 1;
    958 		} else
    959 			ixp4xx_com_set_cr(sc);
    960 	}
    961 }
    962 
    963 static void
    964 tiocm_to_ixp4xx_com(struct ixp4xx_com_softc *sc, u_long how, int ttybits)
    965 {
    966 	u_char combits;
    967 
    968 	combits = 0;
    969 	if (ISSET(ttybits, TIOCM_DTR))
    970 		SET(combits, MCR_DTR);
    971 	if (ISSET(ttybits, TIOCM_RTS))
    972 		SET(combits, MCR_RTS);
    973 
    974 	switch (how) {
    975 	case TIOCMBIC:
    976 		CLR(sc->sc_mcr, combits);
    977 		break;
    978 
    979 	case TIOCMBIS:
    980 		SET(sc->sc_mcr, combits);
    981 		break;
    982 
    983 	case TIOCMSET:
    984 		CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
    985 		SET(sc->sc_mcr, combits);
    986 		break;
    987 	}
    988 
    989 	if (!sc->sc_heldchange) {
    990 		if (sc->sc_tx_busy) {
    991 			sc->sc_heldtbc = sc->sc_tbc;
    992 			sc->sc_tbc = 0;
    993 			sc->sc_heldchange = 1;
    994 		} else
    995 			ixp4xx_com_set_cr(sc);
    996 	}
    997 }
    998 
    999 static int
   1000 ixp4xx_com_to_tiocm(struct ixp4xx_com_softc *sc)
   1001 {
   1002 	u_char combits;
   1003 	int ttybits = 0;
   1004 
   1005 	combits = sc->sc_mcr;
   1006 	if (ISSET(combits, MCR_DTR))
   1007 		SET(ttybits, TIOCM_DTR);
   1008 	if (ISSET(combits, MCR_RTS))
   1009 		SET(ttybits, TIOCM_RTS);
   1010 
   1011 	combits = sc->sc_msr;
   1012 	if (ISSET(combits, MSR_DCD))
   1013 		SET(ttybits, TIOCM_CD);
   1014 	if (ISSET(combits, MSR_CTS))
   1015 		SET(ttybits, TIOCM_CTS);
   1016 	if (ISSET(combits, MSR_DSR))
   1017 		SET(ttybits, TIOCM_DSR);
   1018 	if (ISSET(combits, MSR_RI | MSR_TERI))
   1019 		SET(ttybits, TIOCM_RI);
   1020 
   1021 	if ((sc->sc_ier & IER_UUE) != 0)
   1022 		SET(ttybits, TIOCM_LE);
   1023 
   1024 	return (ttybits);
   1025 }
   1026 
   1027 static u_int
   1028 cflag2lcr(tcflag_t cflag)
   1029 {
   1030 	u_char lcr = 0;
   1031 
   1032 	switch (ISSET(cflag, CSIZE)) {
   1033 	case CS5:
   1034 		SET(lcr, LCR_5BITS);
   1035 		break;
   1036 	case CS6:
   1037 		SET(lcr, LCR_6BITS);
   1038 		break;
   1039 	case CS7:
   1040 		SET(lcr, LCR_7BITS);
   1041 		break;
   1042 	case CS8:
   1043 		SET(lcr, LCR_8BITS);
   1044 		break;
   1045 	}
   1046 	if (ISSET(cflag, PARENB)) {
   1047 		SET(lcr, LCR_PENE);
   1048 		if (!ISSET(cflag, PARODD))
   1049 			SET(lcr, LCR_PODD);
   1050 	}
   1051 	if (ISSET(cflag, CSTOPB))
   1052 		SET(lcr, LCR_1STOP);
   1053 
   1054 	return (lcr);
   1055 }
   1056 
   1057 static void
   1058 ixp4xx_com_iflush(struct ixp4xx_com_softc *sc)
   1059 {
   1060 	bus_space_tag_t iot = sc->sc_iot;
   1061 	bus_space_handle_t ioh = sc->sc_ioh;
   1062 #ifdef DIAGNOSTIC
   1063 	int reg;
   1064 #endif
   1065 	int timo;
   1066 
   1067 #ifdef DIAGNOSTIC
   1068 	reg = 0xffff;
   1069 #endif
   1070 	timo = 50000;
   1071 	/* flush any pending I/O */
   1072 	while (ISSET(bus_space_read_4(iot, ioh, IXP425_UART_LSR), LSR_DR)
   1073 		&& --timo)
   1074 #ifdef DIAGNOSTIC
   1075 		reg =
   1076 #else
   1077 			(void)
   1078 #endif
   1079 			bus_space_read_4(iot, ioh, IXP425_UART_DATA);
   1080 #ifdef DIAGNOSTIC
   1081 	if (!timo)
   1082 		printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
   1083 			reg);
   1084 #endif
   1085 }
   1086 
   1087 static void
   1088 ixp4xxcomsoft(void* arg)
   1089 {
   1090 	struct ixp4xx_com_softc *sc = arg;
   1091 
   1092 	if (COM_ISALIVE(sc) == 0)
   1093 		return;
   1094 
   1095 	if (sc->sc_rx_ready) {
   1096 		sc->sc_rx_ready = 0;
   1097 		ixp4xx_com_rxsoft(sc, sc->sc_tty);
   1098 	}
   1099 	if (sc->sc_tx_done) {
   1100 		sc->sc_tx_done = 0;
   1101 		ixp4xx_com_txsoft(sc, sc->sc_tty);
   1102 	}
   1103 }
   1104 
   1105 inline static void
   1106 ixp4xx_com_txsoft(struct ixp4xx_com_softc *sc, struct tty *tp)
   1107 {
   1108 	CLR(tp->t_state, TS_BUSY);
   1109 	if (ISSET(tp->t_state, TS_FLUSH))
   1110 		CLR(tp->t_state, TS_FLUSH);
   1111 	else
   1112 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1113 	(*tp->t_linesw->l_start)(tp);
   1114 }
   1115 
   1116 inline static void
   1117 ixp4xx_com_rxsoft(struct ixp4xx_com_softc *sc, struct tty *tp)
   1118 {
   1119 	int (*rint) __P((int c, struct tty *tp)) = tp->t_linesw->l_rint;
   1120 	u_char *get, *end;
   1121 	u_int cc, scc;
   1122 	u_char lsr;
   1123 	int code;
   1124 	int s;
   1125 
   1126 	end = sc->sc_ebuf;
   1127 	get = sc->sc_rbget;
   1128 	scc = cc = IXPCOM_RING_SIZE - sc->sc_rbavail;
   1129 	while (cc) {
   1130 		code = get[0];
   1131 		lsr = get[1];
   1132 		if (ISSET(lsr, LSR_OE | LSR_FE | LSR_PE)) {
   1133 			if (ISSET(lsr, LSR_FE))
   1134 				SET(code, TTY_FE);
   1135 			if (ISSET(lsr, LSR_PE))
   1136 				SET(code, TTY_PE);
   1137 		}
   1138 		if ((*rint)(code, tp) == -1) {
   1139 			/*
   1140 			 * The line discipline's buffer is out of space.
   1141 			 */
   1142 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1143 				/*
   1144 				 * We're either not using flow control, or the
   1145 				 * line discipline didn't tell us to block for
   1146 				 * some reason.  Either way, we have no way to
   1147 				 * know when there's more space available, so
   1148 				 * just drop the rest of the data.
   1149 				 */
   1150 				get += cc << 1;
   1151 				if (get >= end)
   1152 					get -= IXPCOM_RING_SIZE << 1;
   1153 				cc = 0;
   1154 			} else {
   1155 				/*
   1156 				 * Don't schedule any more receive processing
   1157 				 * until the line discipline tells us there's
   1158 				 * space available (through comhwiflow()).
   1159 				 * Leave the rest of the data in the input
   1160 				 * buffer.
   1161 				 */
   1162 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1163 			}
   1164 			break;
   1165 		}
   1166 		get += 2;
   1167 		if (get >= end)
   1168 			get = sc->sc_rbuf;
   1169 		cc--;
   1170 	}
   1171 
   1172 	if (cc != scc) {
   1173 		sc->sc_rbget = get;
   1174 		s = splserial();
   1175 		COM_LOCK(sc);
   1176 
   1177 		cc = sc->sc_rbavail += scc - cc;
   1178 		/* Buffers should be ok again, release possible block. */
   1179 		if (cc >= 1) {
   1180 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1181 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1182 				SET(sc->sc_ier,IER_RAVIE);
   1183 				ixp4xx_com_set_cr(sc);
   1184 			}
   1185 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1186 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1187 			}
   1188 		}
   1189 		COM_UNLOCK(sc);
   1190 		splx(s);
   1191 	}
   1192 }
   1193 
   1194 int
   1195 ixp4xxcomintr(void* arg)
   1196 {
   1197 	struct ixp4xx_com_softc *sc = arg;
   1198 	bus_space_tag_t iot = sc->sc_iot;
   1199 	bus_space_handle_t ioh = sc->sc_ioh;
   1200 	u_char *put, *end;
   1201 	u_int cc, res;
   1202 	u_int32_t c;
   1203 	if (COM_ISALIVE(sc) == 0)
   1204 		return (0);
   1205 
   1206 	COM_LOCK(sc);
   1207 	res = bus_space_read_4(iot, ioh, IXP425_UART_IER) & IER_UUE;
   1208 
   1209 	if (!res) {
   1210 		COM_UNLOCK(sc);
   1211 		return (0);
   1212 	}
   1213 
   1214 	res = bus_space_read_4(iot, ioh, IXP425_UART_LSR);
   1215 	if (!ISSET(res, LSR_DR | LSR_TDRQ))
   1216 		return (0);
   1217 
   1218 	end = sc->sc_ebuf;
   1219 	put = sc->sc_rbput;
   1220 	cc = sc->sc_rbavail;
   1221 
   1222 	if (ISSET(res, LSR_DR)) {
   1223 		if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1224 			while (cc > 0) {
   1225 				if (!ISSET(res, LSR_DR))
   1226 					break;
   1227 				c = bus_space_read_4(iot, ioh, IXP425_UART_DATA);
   1228 				if (ISSET(res, LSR_FE)) {
   1229 					cn_check_magic(sc->sc_tty->t_dev,
   1230 						       CNC_BREAK,
   1231 						       ixp4xx_com_cnm_state);
   1232 				}
   1233 				put[0] = c & 0xff;
   1234 				put[1] = (c >> 8) & 0xff;
   1235 				cn_check_magic(sc->sc_tty->t_dev,
   1236 					       put[0], ixp4xx_com_cnm_state);
   1237 				put += 2;
   1238 				if (put >= end)
   1239 					put = sc->sc_rbuf;
   1240 				cc--;
   1241 				res = bus_space_read_4(iot, ioh, IXP425_UART_LSR);
   1242 			}
   1243 
   1244 			/*
   1245 			 * Current string of incoming characters ended because
   1246 			 * no more data was available or we ran out of space.
   1247 			 * Schedule a receive event if any data was received.
   1248 			 * If we're out of space, turn off receive interrupts.
   1249 			 */
   1250 			sc->sc_rbput = put;
   1251 			sc->sc_rbavail = cc;
   1252 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1253 				sc->sc_rx_ready = 1;
   1254 
   1255 			/*
   1256 			 * See if we are in danger of overflowing a buffer. If
   1257 			 * so, use hardware flow control to ease the pressure.
   1258 			 */
   1259 			/* later XXXX */
   1260 
   1261 			/*
   1262                          * If we're out of space, disable receive interrupts
   1263                          * until the queue has drained a bit.
   1264                          */
   1265 			if (!cc) {
   1266 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1267 				CLR(sc->sc_ier, IER_RAVIE);
   1268 				ixp4xx_com_set_cr(sc);
   1269 			}
   1270 		} else {
   1271 #ifdef DIAGNOSTIC
   1272 			panic("ixpcomintr: we shouldn't reach here");
   1273 #endif
   1274 			CLR(sc->sc_ier, IER_RAVIE);
   1275 			ixp4xx_com_set_cr(sc);
   1276 		}
   1277 	}
   1278 
   1279 	/*
   1280 	 * Done handling any receive interrupts. See if data can be
   1281 	 * transmitted as well. Schedule tx done event if no data left
   1282 	 * and tty was marked busy.
   1283 	 */
   1284 
   1285 	res = bus_space_read_4(iot, ioh, IXP425_UART_LSR);
   1286 	if (ISSET(res, LSR_TDRQ)) {
   1287 		/*
   1288 		 * If we've delayed a parameter change, do it now, and restart
   1289 		 * output.
   1290 		 */
   1291 		if (sc->sc_heldchange) {
   1292 			ixp4xx_com_set_cr(sc);
   1293 			sc->sc_heldchange = 0;
   1294 			sc->sc_tbc = sc->sc_heldtbc;
   1295 			sc->sc_heldtbc = 0;
   1296 		}
   1297 
   1298 		/* Output the next chunk of the contiguous buffer, if any. */
   1299 		if (sc->sc_tbc > 0) {
   1300 			ixp4xx_com_filltx(sc);
   1301 		} else {
   1302 			/* Disable transmit completion interrupts if necessary. */
   1303 			if (ISSET(sc->sc_ier, IER_TIE)) {
   1304 				CLR(sc->sc_ier, IER_TIE);
   1305 				ixp4xx_com_filltx(sc);
   1306 			}
   1307 			if (sc->sc_tx_busy) {
   1308 				sc->sc_tx_busy = 0;
   1309 				sc->sc_tx_done = 1;
   1310 			}
   1311 		}
   1312 	}
   1313 	COM_UNLOCK(sc);
   1314 
   1315 	/* Wake up the poller. */
   1316 	softintr_schedule(sc->sc_si);
   1317 
   1318 #if NRND > 0 && defined(RND_COM)
   1319 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1320 #endif
   1321 	return (1);
   1322 }
   1323 
   1324 /*
   1325  * Initialize UART for use as console
   1326  */
   1327 int
   1328 ixp4xx_cominit(bus_space_tag_t iot, bus_space_handle_t ioh, int rate,
   1329 	int frequency, tcflag_t cflag)
   1330 {
   1331 	bus_space_write_4(iot, ioh, IXP425_UART_IER, IER_UUE);
   1332 
   1333 	bus_space_write_4(iot, ioh, IXP425_UART_LCR, LCR_DLAB);
   1334 	rate = ixp4xx_comspeed(rate, frequency);
   1335 	bus_space_write_4(iot, ioh, IXP425_UART_DLL, rate);
   1336 	bus_space_write_4(iot, ioh, IXP425_UART_DLH, rate >> 8);
   1337 	bus_space_write_4(iot, ioh, IXP425_UART_LCR, cflag2lcr(cflag));
   1338 	bus_space_write_4(iot, ioh, IXP425_UART_MCR, MCR_DTR | MCR_RTS);
   1339 	bus_space_write_4(iot, ioh, IXP425_UART_FCR,
   1340 		FCR_ENABLE | FCR_RESETRF | FCR_RESETTF | FCR_TRIGGER_1);
   1341 
   1342 	return (0);
   1343 }
   1344 
   1345 
   1346 /*
   1347  * There are routines needed to act as console
   1348  */
   1349 
   1350 struct consdev ixp4xx_comcons = {
   1351         NULL, NULL, ixp4xx_comcngetc, ixp4xx_comcnputc, ixp4xx_comcnpollc,
   1352 	NULL, NULL, NULL, NODEV, CN_NORMAL
   1353 };
   1354 
   1355 int
   1356 ixp4xx_comcnattach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh,
   1357 	int rate, int frequency, tcflag_t cflag)
   1358 {
   1359 	int res;
   1360 
   1361 	res = ixp4xx_cominit(iot, ioh, rate, frequency, cflag);
   1362 
   1363 	if (res)
   1364 		return (res);
   1365 
   1366 	cn_tab = &ixp4xx_comcons;
   1367 	cn_init_magic(&ixp4xx_com_cnm_state);
   1368 
   1369 	/* default magic is a couple of BREAK. */
   1370         cn_set_magic("\047\001\047\001");
   1371 
   1372 	ixp4xx_comcn_sc.sc_iot = iot;
   1373 	ixp4xx_comcn_sc.sc_ioh = ioh;
   1374 	ixp4xx_comcn_sc.sc_baseaddr = iobase;
   1375 	ixp4xx_comcn_sc.sc_ospeed = rate;
   1376 	ixp4xx_comcn_sc.sc_cflag = cflag;
   1377 
   1378 	return (0);
   1379 }
   1380 
   1381 void
   1382 ixp4xx_comcnputc(dev, c)
   1383         dev_t dev;
   1384         int c;
   1385 {
   1386         bus_space_tag_t         iot = ixp4xx_comcn_sc.sc_iot;
   1387 	bus_space_handle_t      ioh = ixp4xx_comcn_sc.sc_ioh;
   1388 	int s;
   1389 
   1390 	s = splserial();
   1391 
   1392 	while(!(bus_space_read_4(iot, ioh, IXP425_UART_LSR) & LSR_TDRQ))
   1393 		;
   1394 
   1395         bus_space_write_4(iot, ioh, IXP425_UART_DATA, c);
   1396 
   1397 	splx(s);
   1398 }
   1399 
   1400 int
   1401 ixp4xx_comcngetc(dev)
   1402         dev_t dev;
   1403 {
   1404 	int c,s;
   1405         bus_space_tag_t         iot = ixp4xx_comcn_sc.sc_iot;
   1406         bus_space_handle_t      ioh = ixp4xx_comcn_sc.sc_ioh;
   1407 
   1408 	s = splserial();
   1409 
   1410         while(!(bus_space_read_4(iot, ioh, IXP425_UART_LSR) & LSR_DR))
   1411                 ;
   1412 	c = bus_space_read_4(iot, ioh, IXP425_UART_DATA);
   1413 	c &= 0xff;
   1414 	splx(s);
   1415 
   1416 	return (c);
   1417 }
   1418 
   1419 
   1420 void
   1421 ixp4xx_comcnprobe(cp)
   1422         struct consdev *cp;
   1423 {
   1424         cp->cn_pri = CN_REMOTE;
   1425 }
   1426 
   1427 void
   1428 ixp4xx_comcnpollc(dev, on)
   1429         dev_t dev;
   1430         int on;
   1431 {
   1432 }
   1433