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