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