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