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