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