Home | History | Annotate | Line # | Download | only in dev
ser.c revision 1.4
      1 /*	$NetBSD: ser.c,v 1.4 1998/03/25 09:46:10 leo Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Leo Weppelman.
      9  *
     10  * The driver structure is based on the i386 com-driver from Charles M. Hannum.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/select.h>
     44 #include <sys/tty.h>
     45 #include <sys/proc.h>
     46 #include <sys/user.h>
     47 #include <sys/conf.h>
     48 #include <sys/file.h>
     49 #include <sys/uio.h>
     50 #include <sys/kernel.h>
     51 #include <sys/syslog.h>
     52 #include <sys/types.h>
     53 #include <sys/device.h>
     54 
     55 #include <m68k/asm_single.h>
     56 
     57 #include <machine/iomap.h>
     58 #include <machine/mfp.h>
     59 #include <atari/atari/intr.h>
     60 #include <atari/dev/ym2149reg.h>
     61 #include <atari/dev/serreg.h>
     62 
     63 /* #define SER_DEBUG */
     64 
     65 #define	SERUNIT(x)	(minor(x) & 0x7ffff)
     66 #define	SERDIALOUT(x)	(minor(x) & 0x80000)
     67 
     68 /* XXX */
     69 #define	CONSBAUD	9600
     70 #define	CONSCFLAG	TTYDEF_CFLAG
     71 /* end XXX */
     72 
     73 /* Macros to clear/set/test flags. */
     74 #define SET(t, f)	(t) |= (f)
     75 #define CLR(t, f)	(t) &= ~(f)
     76 #define ISSET(t, f)	((t) & (f))
     77 
     78 #define	splserial()	spl6()
     79 
     80 /* Buffer size for character buffer */
     81 #define RXBUFSIZE 2048		/* More than enough..			*/
     82 #define RXBUFMASK (RXBUFSIZE-1)	/* Only iff previous is a power of 2	*/
     83 #define RXHIWAT   (RXBUFSIZE >> 2)
     84 
     85 struct ser_softc {
     86 	struct device	 sc_dev;
     87 	struct tty	*sc_tty;
     88 
     89 	int		 sc_overflows;
     90 	int		 sc_floods;
     91 	int		 sc_errors;
     92 
     93 	u_char		 sc_hwflags;
     94 	u_char		 sc_swflags;
     95 
     96 	int		 sc_ospeed;		/* delay + timer-d data	*/
     97 	u_char		 sc_imra;
     98 	u_char		 sc_imrb;
     99 	u_char		 sc_ucr;		/* Uart control		*/
    100 	u_char		 sc_msr;		/* Modem status		*/
    101 	u_char		 sc_tsr;		/* Tranceiver status	*/
    102 	u_char		 sc_rsr;		/* Receiver status	*/
    103 	u_char		 sc_mcr;		/* (Pseudo) Modem ctrl. */
    104 
    105 	u_char		 sc_msr_delta;
    106 	u_char		 sc_msr_mask;
    107 	u_char		 sc_mcr_active;
    108 	u_char		 sc_mcr_dtr, sc_mcr_rts, sc_msr_cts, sc_msr_dcd;
    109 
    110 	int		 sc_r_hiwat;
    111  	volatile u_int	 sc_rbget;
    112  	volatile u_int	 sc_rbput;
    113 	volatile u_int	 sc_rbavail;
    114  	u_char		 sc_rbuf[RXBUFSIZE];
    115 	u_char		 sc_lbuf[RXBUFSIZE];
    116 
    117 	volatile u_char	 sc_rx_blocked;
    118 	volatile u_char	 sc_rx_ready;
    119 	volatile u_char	 sc_tx_busy;
    120 	volatile u_char	 sc_tx_done;
    121 	volatile u_char	 sc_tx_stopped;
    122 	volatile u_char	 sc_st_check;
    123 
    124  	u_char		*sc_tba;
    125  	int		 sc_tbc;
    126 	int		 sc_heldtbc;
    127 
    128 	volatile u_char	 sc_heldchange;
    129 };
    130 
    131 /*
    132  * For sc_hwflags:
    133  */
    134 #define	SER_HW_CONSOLE	0x01
    135 
    136 cdev_decl(ser);
    137 
    138 void	ser_break __P((struct ser_softc *, int));
    139 void	ser_hwiflow __P((struct ser_softc *, int));
    140 void	ser_iflush __P((struct ser_softc *));
    141 void	ser_loadchannelregs __P((struct ser_softc *));
    142 void	ser_modem __P((struct ser_softc *, int));
    143 void	serdiag __P((void *));
    144 int	serhwiflow __P((struct tty *, int));
    145 void	serinit __P((int));
    146 void	serinitcons __P((int));
    147 int	baud;
    148 int	sermintr __P((void *));
    149 int	sertrintr __P((void *));
    150 int	serparam __P((struct tty *, struct termios *));
    151 void	serstart __P((struct tty *));
    152 
    153 struct consdev;
    154 void	sercnprobe	__P((struct consdev *));
    155 void	sercninit	__P((struct consdev *));
    156 int	sercngetc	__P((dev_t));
    157 void	sercnputc	__P((dev_t, int));
    158 void	sercnpollc	__P((dev_t, int));
    159 
    160 static void sermsrint __P((struct ser_softc *, struct tty*));
    161 static void serrxint __P((struct ser_softc *, struct tty*));
    162 static void ser_shutdown __P((struct ser_softc *));
    163 static int serspeed __P((long));
    164 static void sersoft __P((void *));
    165 static void sertxint __P((struct ser_softc *, struct tty*));
    166 
    167 static volatile int ser_softintr_scheduled = 0;
    168 static int	sermajor;
    169 
    170 /*
    171  * Autoconfig stuff
    172  */
    173 static void serattach __P((struct device *, struct device *, void *));
    174 static int  sermatch __P((struct device *, struct cfdata *, void *));
    175 
    176 struct cfattach ser_ca = {
    177 	sizeof(struct ser_softc), sermatch, serattach
    178 };
    179 
    180 extern struct cfdriver ser_cd;
    181 
    182 /*ARGSUSED*/
    183 static	int
    184 sermatch(pdp, cfp, auxp)
    185 struct	device	*pdp;
    186 struct	cfdata	*cfp;
    187 void		*auxp;
    188 {
    189 	if (!strcmp((char *)auxp, "ser") && cfp->cf_unit == 0)
    190 		return (1);
    191 	return (0);
    192 }
    193 
    194 /*ARGSUSED*/
    195 static void
    196 serattach(pdp, dp, auxp)
    197 struct	device *pdp, *dp;
    198 void	*auxp;
    199 {
    200 	struct ser_softc *sc = (void *)dp;
    201 
    202 	if (intr_establish(1, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
    203 		printf("serattach: Can't establish interrupt (1)\n");
    204 	if (intr_establish(2, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
    205 		printf("serattach: Can't establish interrupt (2)\n");
    206 	if (intr_establish(14, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
    207 		printf("serattach: Can't establish interrupt (14)\n");
    208 	if (intr_establish(9, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    209 		printf("serattach: Can't establish interrupt (9)\n");
    210 	if (intr_establish(10, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    211 		printf("serattach: Can't establish interrupt (10)\n");
    212 	if (intr_establish(11, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    213 		printf("serattach: Can't establish interrupt (11)\n");
    214 	if (intr_establish(12, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
    215 		printf("serattach: Can't establish interrupt (12)\n");
    216 
    217 	ym2149_rts(1);
    218 	ym2149_dtr(1);
    219 
    220 	/*
    221 	 * Enable but mask interrupts...
    222 	 * XXX: Look at edge-sensitivity for DCD/CTS interrupts.
    223 	 */
    224 	MFP->mf_ierb |= IB_SCTS|IB_SDCD;
    225 	MFP->mf_iera |= IA_RRDY|IA_RERR|IA_TRDY|IA_TERR;
    226 	MFP->mf_imrb &= ~(IB_SCTS|IB_SDCD);
    227 	MFP->mf_imra &= ~(IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
    228 
    229 #ifdef SERCONSOLE
    230 	/*
    231 	 * Activate serial console when DCD present...
    232 	 */
    233 	if (!(MFP->mf_gpip & MCR_DCD))
    234 		SET(sc->sc_hwflags, SER_HW_CONSOLE);
    235 #endif /* SERCONSOLE */
    236 
    237 	printf("\n");
    238 	if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
    239 		serinit(CONSBAUD);
    240 		printf("%s: console\n", sc->sc_dev.dv_xname);
    241 	}
    242 }
    243 
    244 #ifdef SER_DEBUG
    245 void serstatus __P((struct ser_softc *, char *));
    246 void
    247 serstatus(sc, str)
    248 	struct ser_softc *sc;
    249 	char *str;
    250 {
    251 	struct tty *tp = sc->sc_tty;
    252 
    253 	printf("%s: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    254 	    sc->sc_dev.dv_xname, str,
    255 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    256 	    ISSET(sc->sc_msr, MCR_DCD) ? "+" : "-",
    257 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    258 	    ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
    259 	    sc->sc_tx_stopped ? "+" : "-");
    260 
    261 	printf("%s: %s %scrtscts %scts %sts_ttstop  %srts %srx_blocked\n",
    262 	    sc->sc_dev.dv_xname, str,
    263 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    264 	    ISSET(sc->sc_msr, MCR_CTS) ? "+" : "-",
    265 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    266 	    ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
    267 	    sc->sc_rx_blocked ? "+" : "-");
    268 }
    269 #endif /* SER_DEBUG */
    270 
    271 int
    272 seropen(dev, flag, mode, p)
    273 	dev_t dev;
    274 	int flag, mode;
    275 	struct proc *p;
    276 {
    277 	int unit = SERUNIT(dev);
    278 	struct ser_softc *sc;
    279 	struct tty *tp;
    280 	int s, s2;
    281 	int error = 0;
    282 
    283 	if (unit >= ser_cd.cd_ndevs)
    284 		return (ENXIO);
    285 	sc = ser_cd.cd_devs[unit];
    286 	if (!sc)
    287 		return (ENXIO);
    288 
    289 	if (!sc->sc_tty) {
    290 		tp = sc->sc_tty = ttymalloc();
    291 		tty_attach(tp);
    292 	} else
    293 		tp = sc->sc_tty;
    294 
    295 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    296 	    ISSET(tp->t_state, TS_XCLUDE) &&
    297 	    p->p_ucred->cr_uid != 0)
    298 		return (EBUSY);
    299 
    300 	s = spltty();
    301 
    302 	/*
    303 	 * Do the following if this is a first open.
    304 	 */
    305 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    306 	    struct termios t;
    307 
    308 	    /* Turn on interrupts. */
    309 	    sc->sc_imra = IA_RRDY|IA_RERR|IA_TRDY|IA_TERR;
    310 	    sc->sc_imrb = IB_SCTS|IB_SDCD;
    311 	    single_inst_bset_b(MFP->mf_imra, sc->sc_imra);
    312 	    single_inst_bset_b(MFP->mf_imrb, sc->sc_imrb);
    313 
    314 	    /* Fetch the current modem control status, needed later. */
    315 	    sc->sc_msr = ~MFP->mf_gpip & (IO_SDCD|IO_SCTS|IO_SRI);
    316 
    317 	    /* Add some entry points needed by the tty layer. */
    318 	    tp->t_oproc = serstart;
    319 	    tp->t_param = serparam;
    320 	    tp->t_hwiflow = serhwiflow;
    321 	    tp->t_dev = dev;
    322 
    323 	    /*
    324 	     * Initialize the termios status to the defaults.  Add in the
    325 	     * sticky bits from TIOCSFLAGS.
    326 	     */
    327 	    t.c_ispeed = 0;
    328 	    if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
    329 		    t.c_ospeed = CONSBAUD;
    330 		    t.c_cflag  = CONSCFLAG;
    331 	    }
    332 	    else {
    333 		    t.c_ospeed = TTYDEF_SPEED;
    334 		    t.c_cflag = TTYDEF_CFLAG;
    335 	    }
    336 	    if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    337 		    SET(t.c_cflag, CLOCAL);
    338 	    if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    339 		    SET(t.c_cflag, CRTSCTS);
    340 	    if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    341 		    SET(t.c_cflag, MDMBUF);
    342 	    tp->t_iflag = TTYDEF_IFLAG;
    343 	    tp->t_oflag = TTYDEF_OFLAG;
    344 	    tp->t_lflag = TTYDEF_LFLAG;
    345 	    ttychars(tp);
    346 	    (void) serparam(tp, &t);
    347 	    ttsetwater(tp);
    348 
    349 	    s2 = splserial();
    350 
    351 	    /*
    352 	     * Turn on DTR.  We must always do this, even if carrier is not
    353 	     * present, because otherwise we'd have to use TIOCSDTR
    354 	     * immediately after setting CLOCAL.  We will drop DTR only on
    355 	     * the next high-low transition of DCD, or by explicit request.
    356 	     */
    357 	    ser_modem(sc, 1);
    358 
    359 	    /* Clear the input ring, and unblock. */
    360 	    sc->sc_rbput = sc->sc_rbget = 0;
    361 	    sc->sc_rbavail = RXBUFSIZE;
    362 	    ser_iflush(sc);
    363 	    sc->sc_rx_blocked = 0;
    364 	    ser_hwiflow(sc, 0);
    365 
    366 #ifdef SER_DEBUG
    367 	    serstatus(sc, "seropen  ");
    368 #endif
    369 
    370 	    splx(s2);
    371 	}
    372 
    373 	splx(s);
    374 
    375 	error = ttyopen(tp, SERDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    376 	if (error)
    377 		goto bad;
    378 
    379 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    380         if (error)
    381 		goto bad;
    382 
    383 	return (0);
    384 
    385 bad:
    386 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    387 		/*
    388 		 * We failed to open the device, and nobody else had it opened.
    389 		 * Clean up the state as appropriate.
    390 		 */
    391 		ser_shutdown(sc);
    392 	}
    393 
    394 	return (error);
    395 }
    396 
    397 int
    398 serclose(dev, flag, mode, p)
    399 	dev_t		dev;
    400 	int		flag, mode;
    401 	struct proc	*p;
    402 {
    403 	int unit = SERUNIT(dev);
    404 	struct ser_softc *sc = ser_cd.cd_devs[unit];
    405 	struct tty *tp = sc->sc_tty;
    406 
    407 	/* XXX This is for cons.c. */
    408 	if (!ISSET(tp->t_state, TS_ISOPEN))
    409 		return (0);
    410 
    411 	(*linesw[tp->t_line].l_close)(tp, flag);
    412 	ttyclose(tp);
    413 
    414 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    415 		/*
    416 		 * Although we got a last close, the device may still be in
    417 		 * use; e.g. if this was the dialout node, and there are still
    418 		 * processes waiting for carrier on the non-dialout node.
    419 		 */
    420 		ser_shutdown(sc);
    421 	}
    422 
    423 	return (0);
    424 }
    425 
    426 int
    427 serread(dev, uio, flag)
    428 	dev_t dev;
    429 	struct uio *uio;
    430 	int flag;
    431 {
    432 	struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(dev)];
    433 	struct tty *tp = sc->sc_tty;
    434 
    435 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    436 }
    437 
    438 int
    439 serwrite(dev, uio, flag)
    440 	dev_t dev;
    441 	struct uio *uio;
    442 	int flag;
    443 {
    444 	struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(dev)];
    445 	struct tty *tp = sc->sc_tty;
    446 
    447 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    448 }
    449 
    450 struct tty *
    451 sertty(dev)
    452 	dev_t dev;
    453 {
    454 	struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(dev)];
    455 	struct tty *tp = sc->sc_tty;
    456 
    457 	return (tp);
    458 }
    459 
    460 int
    461 serioctl(dev, cmd, data, flag, p)
    462 	dev_t dev;
    463 	u_long cmd;
    464 	caddr_t data;
    465 	int flag;
    466 	struct proc *p;
    467 {
    468 	int unit = SERUNIT(dev);
    469 	struct ser_softc *sc = ser_cd.cd_devs[unit];
    470 	struct tty *tp = sc->sc_tty;
    471 	int error;
    472 
    473 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    474 	if (error >= 0)
    475 		return (error);
    476 
    477 	error = ttioctl(tp, cmd, data, flag, p);
    478 	if (error >= 0)
    479 		return (error);
    480 
    481 	switch (cmd) {
    482 	case TIOCSBRK:
    483 		ser_break(sc, 1);
    484 		break;
    485 
    486 	case TIOCCBRK:
    487 		ser_break(sc, 0);
    488 		break;
    489 
    490 	case TIOCSDTR:
    491 		ser_modem(sc, 1);
    492 		break;
    493 
    494 	case TIOCCDTR:
    495 		ser_modem(sc, 0);
    496 		break;
    497 
    498 	case TIOCGFLAGS:
    499 		*(int *)data = sc->sc_swflags;
    500 		break;
    501 
    502 	case TIOCSFLAGS:
    503 		error = suser(p->p_ucred, &p->p_acflag);
    504 		if (error)
    505 			return (error);
    506 		sc->sc_swflags = *(int *)data;
    507 		break;
    508 
    509 	case TIOCMSET:
    510 	case TIOCMBIS:
    511 	case TIOCMBIC:
    512 	case TIOCMGET:
    513 	default:
    514 		return (ENOTTY);
    515 	}
    516 
    517 #ifdef SER_DEBUG
    518 		serstatus(sc, "serioctl ");
    519 #endif
    520 
    521 	return (0);
    522 }
    523 
    524 void
    525 ser_break(sc, onoff)
    526 	struct ser_softc *sc;
    527 	int onoff;
    528 {
    529 	int s;
    530 
    531 	s = splserial();
    532 	if (onoff)
    533 		SET(sc->sc_tsr, TSR_SBREAK);
    534 	else
    535 		CLR(sc->sc_tsr, TSR_SBREAK);
    536 
    537 	if (!sc->sc_heldchange) {
    538 		if (sc->sc_tx_busy) {
    539 			sc->sc_heldtbc = sc->sc_tbc;
    540 			sc->sc_tbc = 0;
    541 			sc->sc_heldchange = 1;
    542 		} else
    543 			ser_loadchannelregs(sc);
    544 	}
    545 	splx(s);
    546 }
    547 
    548 void
    549 ser_modem(sc, onoff)
    550 	struct ser_softc *sc;
    551 	int onoff;
    552 {
    553 	int s;
    554 
    555 	s = splserial();
    556 	if (onoff)
    557 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
    558 	else
    559 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
    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 			ser_loadchannelregs(sc);
    568 	}
    569 	splx(s);
    570 }
    571 
    572 int
    573 serparam(tp, t)
    574 	struct tty *tp;
    575 	struct termios *t;
    576 {
    577 	struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(tp->t_dev)];
    578 	int ospeed = serspeed(t->c_ospeed);
    579 	u_char ucr;
    580 	int s;
    581 
    582 	/* check requested parameters */
    583 	if (ospeed < 0)
    584 		return (EINVAL);
    585 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    586 		return (EINVAL);
    587 
    588 	sc->sc_rsr = RSR_ENAB;
    589 	sc->sc_tsr = TSR_ENAB;
    590 
    591 	ucr = UCR_CLKDIV;
    592 
    593 	switch (ISSET(t->c_cflag, CSIZE)) {
    594 	case CS5:
    595 		SET(ucr, UCR_5BITS);
    596 		break;
    597 	case CS6:
    598 		SET(ucr, UCR_6BITS);
    599 		break;
    600 	case CS7:
    601 		SET(ucr, UCR_7BITS);
    602 		break;
    603 	case CS8:
    604 		SET(ucr, UCR_8BITS);
    605 		break;
    606 	}
    607 	if (ISSET(t->c_cflag, PARENB)) {
    608 		SET(ucr, UCR_PENAB);
    609 		if (!ISSET(t->c_cflag, PARODD))
    610 			SET(ucr, UCR_PEVEN);
    611 	}
    612 	if (ISSET(t->c_cflag, CSTOPB))
    613 		SET(ucr, UCR_STOPB2);
    614 	else
    615 		SET(ucr, UCR_STOPB1);
    616 
    617 	s = splserial();
    618 
    619 	sc->sc_ucr = ucr;
    620 
    621 	/*
    622 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    623 	 * is always active.
    624 	 */
    625 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    626 	    ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
    627 		SET(t->c_cflag, CLOCAL);
    628 		CLR(t->c_cflag, HUPCL);
    629 	}
    630 
    631 	/*
    632 	 * If we're not in a mode that assumes a connection is present, then
    633 	 * ignore carrier changes.
    634 	 */
    635 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
    636 		sc->sc_msr_dcd = 0;
    637 	else
    638 		sc->sc_msr_dcd = MCR_DCD;
    639 	/*
    640 	 * Set the flow control pins depending on the current flow control
    641 	 * mode.
    642 	 */
    643 	if (ISSET(t->c_cflag, CRTSCTS)) {
    644 		sc->sc_mcr_dtr = MCR_DTR;
    645 		sc->sc_mcr_rts = MCR_RTS;
    646 		sc->sc_msr_cts = MCR_CTS;
    647 		sc->sc_r_hiwat = RXHIWAT;
    648 	} else if (ISSET(t->c_cflag, MDMBUF)) {
    649 		/*
    650 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
    651 		 * carrier detection.
    652 		 */
    653 		sc->sc_mcr_dtr = 0;
    654 		sc->sc_mcr_rts = MCR_DTR;
    655 		sc->sc_msr_cts = MCR_DCD;
    656 		sc->sc_r_hiwat = RXHIWAT;
    657 	} else {
    658 		/*
    659 		 * If no flow control, then always set RTS.  This will make
    660 		 * the other side happy if it mistakenly thinks we're doing
    661 		 * RTS/CTS flow control.
    662 		 */
    663 		sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
    664 		sc->sc_mcr_rts = 0;
    665 		sc->sc_msr_cts = 0;
    666 		sc->sc_r_hiwat = 0;
    667 		if (ISSET(sc->sc_mcr, MCR_DTR))
    668 			SET(sc->sc_mcr, MCR_RTS);
    669 		else
    670 			CLR(sc->sc_mcr, MCR_RTS);
    671 	}
    672 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
    673 
    674 #if 0
    675 	if (ospeed == 0)
    676 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
    677 	else
    678 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
    679 #endif
    680 
    681 	sc->sc_ospeed = ospeed;
    682 
    683 	/* and copy to tty */
    684 	tp->t_ispeed = 0;
    685 	tp->t_ospeed = t->c_ospeed;
    686 	tp->t_cflag = t->c_cflag;
    687 
    688 	if (!sc->sc_heldchange) {
    689 		if (sc->sc_tx_busy) {
    690 			sc->sc_heldtbc = sc->sc_tbc;
    691 			sc->sc_tbc = 0;
    692 			sc->sc_heldchange = 1;
    693 		} else
    694 			ser_loadchannelregs(sc);
    695 	}
    696 
    697 	splx(s);
    698 
    699 	/*
    700 	 * Update the tty layer's idea of the carrier bit, in case we changed
    701 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that if we
    702 	 * lose carrier while carrier detection is on.
    703 	 */
    704 	(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(sc->sc_msr, MCR_DCD));
    705 
    706 #ifdef SER_DEBUG
    707 	serstatus(sc, "serparam ");
    708 #endif
    709 
    710 	/* XXXXX FIX ME */
    711 	/* Block or unblock as needed. */
    712 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    713 		if (sc->sc_rx_blocked) {
    714 			sc->sc_rx_blocked = 0;
    715 			ser_hwiflow(sc, 0);
    716 		}
    717 		if (sc->sc_tx_stopped) {
    718 			sc->sc_tx_stopped = 0;
    719 			serstart(tp);
    720 		}
    721 	} else {
    722 #if 0
    723 		sermsrint(sc, tp);
    724 #endif
    725 	}
    726 
    727 	return (0);
    728 }
    729 
    730 void
    731 ser_iflush(sc)
    732 	struct ser_softc *sc;
    733 {
    734 	u_char	tmp;
    735 
    736 	/* flush any pending I/O */
    737 	while (ISSET(MFP->mf_rsr, RSR_CIP|RSR_BFULL))
    738 		tmp = MFP->mf_udr;
    739 }
    740 
    741 void
    742 ser_loadchannelregs(sc)
    743 	struct ser_softc *sc;
    744 {
    745 	/* XXXXX necessary? */
    746 	ser_iflush(sc);
    747 
    748 	/*
    749 	 * No interrupts please...
    750 	 */
    751 if((MFP->mf_imra & (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR)) != sc->sc_imra) {
    752 printf("loadchannelregs: mf_imra: %x sc_imra: %x\n", (u_int)MFP->mf_imra,
    753 						     (u_int)sc->sc_imra);
    754 }
    755 if((MFP->mf_imrb & (IB_SCTS|IB_SDCD)) != sc->sc_imrb) {
    756 printf("loadchannelregs: mf_imrb: %x sc_imrb: %x\n", (u_int)MFP->mf_imrb,
    757 						     (u_int)sc->sc_imrb);
    758 }
    759 	single_inst_bclr_b(MFP->mf_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
    760 	single_inst_bclr_b(MFP->mf_imrb, IB_SCTS|IB_SDCD);
    761 
    762 	MFP->mf_ucr = sc->sc_ucr;
    763 	MFP->mf_rsr = sc->sc_rsr;
    764 	MFP->mf_tsr = sc->sc_tsr;
    765 
    766 	single_inst_bclr_b(MFP->mf_tcdcr, 0x07);
    767 	MFP->mf_tddr  = sc->sc_ospeed;
    768 	single_inst_bset_b(MFP->mf_tcdcr, (sc->sc_ospeed >> 8) & 0x0f);
    769 
    770 	sc->sc_mcr_active = sc->sc_mcr;
    771 
    772 	if (machineid & ATARI_HADES) {
    773 		/* PCB fault, wires exchanged..... */
    774 		ym2149_rts(!(sc->sc_mcr_active & MCR_DTR));
    775 		ym2149_dtr(!(sc->sc_mcr_active & MCR_RTS));
    776 	}
    777 	else {
    778 		ym2149_rts(!(sc->sc_mcr_active & MCR_RTS));
    779 		ym2149_dtr(!(sc->sc_mcr_active & MCR_DTR));
    780 	}
    781 
    782 	single_inst_bset_b(MFP->mf_imra, sc->sc_imra);
    783 	single_inst_bset_b(MFP->mf_imrb, sc->sc_imrb);
    784 }
    785 
    786 int
    787 serhwiflow(tp, block)
    788 	struct tty *tp;
    789 	int block;
    790 {
    791 	struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(tp->t_dev)];
    792 	int s;
    793 
    794 	if (sc->sc_mcr_rts == 0)
    795 		return (0);
    796 
    797 	s = splserial();
    798 	if (block) {
    799 		/*
    800 		 * The tty layer is asking us to block input.
    801 		 * If we already did it, just return TRUE.
    802 		 */
    803 		if (sc->sc_rx_blocked)
    804 			goto out;
    805 		sc->sc_rx_blocked = 1;
    806 	} else {
    807 		/*
    808 		 * The tty layer is asking us to resume input.
    809 		 * The input ring is always empty by now.
    810 		 */
    811 		sc->sc_rx_blocked = 0;
    812 	}
    813 	ser_hwiflow(sc, block);
    814 out:
    815 	splx(s);
    816 	return (1);
    817 }
    818 
    819 /*
    820  * (un)block input via hw flowcontrol
    821  */
    822 void
    823 ser_hwiflow(sc, block)
    824 	struct ser_softc *sc;
    825 	int block;
    826 {
    827 	if (sc->sc_mcr_rts == 0)
    828 		return;
    829 
    830 	if (block) {
    831 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
    832 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
    833 	} else {
    834 		SET(sc->sc_mcr, sc->sc_mcr_rts);
    835 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
    836 	}
    837 	if (machineid & ATARI_HADES) {
    838 		/* PCB fault, wires exchanged..... */
    839 		ym2149_dtr(sc->sc_mcr_active & MCR_RTS);
    840 	}
    841 	else {
    842 		ym2149_rts(sc->sc_mcr_active & MCR_RTS);
    843 	}
    844 }
    845 
    846 void
    847 serstart(tp)
    848 	struct tty *tp;
    849 {
    850 	struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(tp->t_dev)];
    851 	int s;
    852 
    853 	s = spltty();
    854 	if (ISSET(tp->t_state, TS_BUSY))
    855 		goto out;
    856 	if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP))
    857 		goto stopped;
    858 
    859 	if (sc->sc_tx_stopped)
    860 		goto stopped;
    861 
    862 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    863 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    864 			CLR(tp->t_state, TS_ASLEEP);
    865 			wakeup(&tp->t_outq);
    866 		}
    867 		selwakeup(&tp->t_wsel);
    868 		if (tp->t_outq.c_cc == 0)
    869 			goto stopped;
    870 	}
    871 
    872 	/* Grab the first contiguous region of buffer space. */
    873 	{
    874 		u_char *tba;
    875 		int tbc;
    876 
    877 		tba = tp->t_outq.c_cf;
    878 		tbc = ndqb(&tp->t_outq, 0);
    879 
    880 		(void)splserial();
    881 
    882 		sc->sc_tba = tba;
    883 		sc->sc_tbc = tbc;
    884 	}
    885 
    886 	SET(tp->t_state, TS_BUSY);
    887 	sc->sc_tx_busy = 1;
    888 
    889 	/* Enable transmit completion interrupts if necessary. */
    890 	if (!ISSET(sc->sc_imra, IA_TRDY)) {
    891 		SET(sc->sc_imra, IA_TRDY|IA_TERR);
    892 		single_inst_bset_b(MFP->mf_imra, IA_TRDY|IA_TERR);
    893 	}
    894 
    895 	/* Output the first char */
    896 	MFP->mf_udr = *sc->sc_tba;
    897 	sc->sc_tbc --;
    898 	sc->sc_tba ++;
    899 
    900 	splx(s);
    901 	return;
    902 
    903 stopped:
    904 	/* Disable transmit completion interrupts if necessary. */
    905 	if (ISSET(sc->sc_imra, IA_TRDY)) {
    906 		CLR(sc->sc_imra, IA_TRDY|IA_TERR);
    907 		single_inst_bclr_b(MFP->mf_imra, IA_TRDY|IA_TERR);
    908 	}
    909 out:
    910 	splx(s);
    911 	return;
    912 }
    913 
    914 /*
    915  * Stop output on a line.
    916  */
    917 void
    918 serstop(tp, flag)
    919 	struct tty *tp;
    920 	int flag;
    921 {
    922 	struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(tp->t_dev)];
    923 	int s;
    924 
    925 	s = splserial();
    926 	if (ISSET(tp->t_state, TS_BUSY)) {
    927 		/* Stop transmitting at the next chunk. */
    928 		sc->sc_tbc = 0;
    929 		sc->sc_heldtbc = 0;
    930 		if (!ISSET(tp->t_state, TS_TTSTOP))
    931 			SET(tp->t_state, TS_FLUSH);
    932 	}
    933 	splx(s);
    934 }
    935 
    936 void
    937 serdiag(arg)
    938 	void *arg;
    939 {
    940 	struct ser_softc *sc = arg;
    941 	int overflows, floods;
    942 	int s;
    943 
    944 	s = splserial();
    945 	overflows = sc->sc_overflows;
    946 	sc->sc_overflows = 0;
    947 	floods = sc->sc_floods;
    948 	sc->sc_floods = 0;
    949 	sc->sc_errors = 0;
    950 	splx(s);
    951 
    952 	log(LOG_WARNING,
    953 	    "%s: %d silo overflow%s, %d ibuf flood%s\n",
    954 	    sc->sc_dev.dv_xname,
    955 	    overflows, overflows == 1 ? "" : "s",
    956 	    floods, floods == 1 ? "" : "s");
    957 }
    958 
    959 static
    960 void ser_shutdown(sc)
    961 	struct ser_softc *sc;
    962 {
    963 	int	s;
    964 	struct tty *tp = sc->sc_tty;
    965 
    966 
    967 	s = splserial();
    968 
    969 	/* If we were asserting flow control, then deassert it. */
    970 	sc->sc_rx_blocked = 1;
    971 	ser_hwiflow(sc, 1);
    972 
    973 	/* Clear any break condition set with TIOCSBRK. */
    974 	ser_break(sc, 0);
    975 
    976 	/*
    977 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    978 	 * notice even if we immediately open the port again.
    979 	 */
    980 	if (ISSET(tp->t_cflag, HUPCL)) {
    981 		ser_modem(sc, 0);
    982 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    983 	}
    984 
    985 	/* Turn off interrupts. */
    986 	CLR(sc->sc_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
    987 	CLR(sc->sc_imrb, IB_SCTS|IB_SDCD);
    988 	single_inst_bclr_b(MFP->mf_imrb, IB_SCTS|IB_SDCD);
    989 	single_inst_bclr_b(MFP->mf_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
    990 	splx(s);
    991 }
    992 
    993 static void
    994 serrxint(sc, tp)
    995 	struct ser_softc	*sc;
    996 	struct tty		*tp;
    997 {
    998 	u_int	get, cc, scc;
    999 	int	code;
   1000 	u_char	rsr;
   1001 	int	s;
   1002 	static int lsrmap[8] = {
   1003 		0,      TTY_PE,
   1004 		TTY_FE, TTY_PE|TTY_FE,
   1005 		TTY_FE, TTY_PE|TTY_FE,
   1006 		TTY_FE, TTY_PE|TTY_FE
   1007 	};
   1008 
   1009 	get = sc->sc_rbget;
   1010 	scc = cc = RXBUFSIZE - sc->sc_rbavail;
   1011 
   1012 	if (cc == RXBUFSIZE) {
   1013 		sc->sc_floods++;
   1014 		if (sc->sc_errors++ == 0)
   1015 			timeout(serdiag, sc, 60 * hz);
   1016 	}
   1017 
   1018 	while (cc--) {
   1019 		rsr = sc->sc_lbuf[get];
   1020 		if (ISSET(rsr, RSR_BREAK)) {
   1021 #ifdef DDB
   1022 			if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE))
   1023 				Debugger();
   1024 #endif
   1025 		}
   1026 		else if (ISSET(rsr, RSR_OERR)) {
   1027 			sc->sc_overflows++;
   1028 			if (sc->sc_errors++ == 0)
   1029 				timeout(serdiag, sc, 60 * hz);
   1030 		}
   1031 		code = sc->sc_rbuf[get] |
   1032 		    lsrmap[(rsr & (RSR_BREAK|RSR_FERR|RSR_PERR)) >> 3];
   1033 		(*linesw[tp->t_line].l_rint)(code, tp);
   1034 		get = (get + 1) & RXBUFMASK;
   1035 	}
   1036 
   1037 	sc->sc_rbget = get;
   1038 	s = splserial();
   1039 	sc->sc_rbavail += scc;
   1040 	/*
   1041 	 * Buffers should be ok again, release possible block, but only if the
   1042 	 * tty layer isn't blocking too.
   1043 	 */
   1044 	if (sc->sc_rx_blocked && !ISSET(tp->t_state, TS_TBLOCK)) {
   1045 		sc->sc_rx_blocked = 0;
   1046 		ser_hwiflow(sc, 0);
   1047 	}
   1048 	splx(s);
   1049 }
   1050 
   1051 static void
   1052 sertxint(sc, tp)
   1053 	struct ser_softc	*sc;
   1054 	struct tty		*tp;
   1055 {
   1056 
   1057 	CLR(tp->t_state, TS_BUSY);
   1058 	if (ISSET(tp->t_state, TS_FLUSH))
   1059 		CLR(tp->t_state, TS_FLUSH);
   1060 	else
   1061 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1062 	(*linesw[tp->t_line].l_start)(tp);
   1063 }
   1064 
   1065 static void
   1066 sermsrint(sc, tp)
   1067 	struct ser_softc	*sc;
   1068 	struct tty		*tp;
   1069 {
   1070 	u_char msr, delta;
   1071 	int s;
   1072 
   1073 	s = splserial();
   1074 	msr = sc->sc_msr;
   1075 	delta = sc->sc_msr_delta;
   1076 	sc->sc_msr_delta = 0;
   1077 	splx(s);
   1078 
   1079 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1080 		/*
   1081 		 * Inform the tty layer that carrier detect changed.
   1082 		 */
   1083 		(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MCR_DCD));
   1084 	}
   1085 
   1086 	if (ISSET(delta, sc->sc_msr_cts)) {
   1087 		/* Block or unblock output according to flow control. */
   1088 		if (ISSET(msr, sc->sc_msr_cts)) {
   1089 			sc->sc_tx_stopped = 0;
   1090 			(*linesw[tp->t_line].l_start)(tp);
   1091 		} else {
   1092 			sc->sc_tx_stopped = 1;
   1093 			serstop(tp, 0);
   1094 		}
   1095 	}
   1096 
   1097 #ifdef SER_DEBUG
   1098 	serstatus(sc, "sermsrint");
   1099 #endif
   1100 }
   1101 
   1102 void
   1103 sersoft(arg)
   1104 	void *arg;
   1105 {
   1106 	struct ser_softc *sc = arg;
   1107 	struct tty	*tp;
   1108 
   1109 	ser_softintr_scheduled = 0;
   1110 
   1111 	tp = sc->sc_tty;
   1112 	if (tp == NULL)
   1113 		return;
   1114 
   1115 	if (!ISSET(tp->t_state, TS_ISOPEN) && (tp->t_wopen == 0))
   1116 		return;
   1117 
   1118 	if (sc->sc_rx_ready) {
   1119 		sc->sc_rx_ready = 0;
   1120 		serrxint(sc, tp);
   1121 	}
   1122 
   1123 	if (sc->sc_st_check) {
   1124 		sc->sc_st_check = 0;
   1125 		sermsrint(sc, tp);
   1126 	}
   1127 
   1128 	if (sc->sc_tx_done) {
   1129 		sc->sc_tx_done = 0;
   1130 		sertxint(sc, tp);
   1131 	}
   1132 }
   1133 
   1134 int
   1135 sermintr(arg)
   1136 void	*arg;
   1137 {
   1138 	struct ser_softc *sc = arg;
   1139 	u_char	msr, delta;
   1140 
   1141 	msr = ~MFP->mf_gpip;
   1142 	delta = msr ^ sc->sc_msr;
   1143 	sc->sc_msr  = sc->sc_msr & ~(MCR_CTS|MCR_DCD|MCR_RI);
   1144 	sc->sc_msr |= msr & (MCR_CTS|MCR_DCD|MCR_RI);
   1145 
   1146 	if (ISSET(delta, sc->sc_msr_mask)) {
   1147 		sc->sc_msr_delta |= delta;
   1148 
   1149 		/*
   1150 		 * Stop output immediately if we lose the output
   1151 		 * flow control signal or carrier detect.
   1152 		 */
   1153 		if (ISSET(~msr, sc->sc_msr_mask)) {
   1154 			sc->sc_tbc = 0;
   1155 			sc->sc_heldtbc = 0;
   1156 #ifdef SER_DEBUG
   1157 			serstatus(sc, "sermintr  ");
   1158 #endif
   1159 		}
   1160 
   1161 		sc->sc_st_check = 1;
   1162 	}
   1163 	if (!ser_softintr_scheduled)
   1164 		add_sicallback((si_farg)sersoft, sc, 0);
   1165 	return 1;
   1166 }
   1167 
   1168 int
   1169 sertrintr(arg)
   1170 	void	*arg;
   1171 {
   1172 	struct ser_softc *sc = arg;
   1173 	u_int	put, cc;
   1174 	u_char	rsr, tsr;
   1175 
   1176 	put = sc->sc_rbput;
   1177 	cc = sc->sc_rbavail;
   1178 
   1179 	rsr = MFP->mf_rsr;
   1180 	if (ISSET(rsr, RSR_BFULL|RSR_BREAK)) {
   1181 		for (; ISSET(rsr, RSR_BFULL|RSR_BREAK) && cc > 0; cc--) {
   1182 			sc->sc_rbuf[put] = MFP->mf_udr;
   1183 			sc->sc_lbuf[put] = rsr;
   1184 			put = (put + 1) & RXBUFMASK;
   1185 			if ((rsr & RSR_BREAK) && (MFP->mf_rsr & RSR_BREAK))
   1186 				rsr = 0;
   1187 			else rsr = MFP->mf_rsr;
   1188 		}
   1189 		/*
   1190 		 * Current string of incoming characters ended because
   1191 		 * no more data was available. Schedule a receive event
   1192 		 * if any data was received. Drop any characters that
   1193 		 * we couldn't handle.
   1194 		 */
   1195 		sc->sc_rbput    = put;
   1196 		sc->sc_rbavail  = cc;
   1197 		sc->sc_rx_ready = 1;
   1198 		/*
   1199 		 * See if we are in danger of overflowing a buffer. If
   1200 		 * so, use hardware flow control to ease the pressure.
   1201 		 */
   1202 		if (sc->sc_rx_blocked == 0 &&
   1203 		    cc < sc->sc_r_hiwat) {
   1204 			sc->sc_rx_blocked = 1;
   1205 			ser_hwiflow(sc, 1);
   1206 		}
   1207 		/*
   1208 		 * If we're out of space, throw away any further input.
   1209 		 */
   1210 		if (!cc) {
   1211 			while (ISSET(rsr, RSR_BFULL|RSR_BREAK)) {
   1212 				rsr = MFP->mf_udr;
   1213 				rsr = MFP->mf_rsr;
   1214 			}
   1215 		}
   1216 	}
   1217 
   1218 	/*
   1219 	 * Done handling any receive interrupts. See if data can be
   1220 	 * transmitted as well. Schedule tx done event if no data left
   1221 	 * and tty was marked busy.
   1222 	 */
   1223 	tsr = MFP->mf_tsr;
   1224 	if (ISSET(tsr, TSR_BE)) {
   1225 		/*
   1226 		 * If we've delayed a parameter change, do it now, and restart
   1227 		 * output.
   1228 		 */
   1229 		if (sc->sc_heldchange) {
   1230 			ser_loadchannelregs(sc);
   1231 			sc->sc_heldchange = 0;
   1232 			sc->sc_tbc = sc->sc_heldtbc;
   1233 			sc->sc_heldtbc = 0;
   1234 		}
   1235 		/* Output the next character, if any. */
   1236 		if (sc->sc_tbc > 0) {
   1237 			MFP->mf_udr = *sc->sc_tba;
   1238 			sc->sc_tbc --;
   1239 			sc->sc_tba ++;
   1240 		} else if (sc->sc_tx_busy) {
   1241 			sc->sc_tx_busy = 0;
   1242 			sc->sc_tx_done = 1;
   1243 		}
   1244 	}
   1245 
   1246 	if (!ser_softintr_scheduled)
   1247 		add_sicallback((si_farg)sersoft, sc, 0);
   1248 	return 1;
   1249 }
   1250 
   1251 static int
   1252 serspeed(speed)
   1253 	long speed;
   1254 {
   1255 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
   1256 
   1257 	int div, x, err;
   1258 
   1259 	if (speed <= 0)
   1260 		return (-1);
   1261 
   1262 	for (div = 4; div <= 64; div *= 4) {
   1263 		x = divrnd((SER_FREQ / div), speed);
   1264 
   1265 		/*
   1266 		 * The value must fit in the timer-d dataregister. If
   1267 		 * not, try another delay-mode.
   1268 		 */
   1269 		if ((x/2) > 255)
   1270 			continue;
   1271 
   1272 		/*
   1273 		 * Baudrate to high for the interface or cannot be made
   1274 		 * within tolerance.
   1275 		 */
   1276 		if (x <= 0)
   1277 			return (-1);
   1278 
   1279 		err = divrnd((SER_FREQ / div) * 1000, speed * x) - 1000;
   1280 		if (err < 0)
   1281 			err = -err;
   1282 		if (err > SER_TOLERANCE)
   1283 			continue;
   1284 
   1285 		/*
   1286 		 * Translate 'div' to delay-code
   1287 		 */
   1288 		if (div == 4)
   1289 			div = 1;
   1290 		else if (div == 16)
   1291 			div = 3;
   1292 		else if (div == 64)
   1293 			div = 5;
   1294 
   1295 		return ((x/2) | (div << 8));
   1296 	}
   1297 	return (-1);
   1298 
   1299 #undef	divrnd(n, q)
   1300 }
   1301 
   1302 /*
   1303  * Following are all routines needed for SER to act as console
   1304  */
   1305 #include <dev/cons.h>
   1306 
   1307 void
   1308 sercnprobe(cp)
   1309 	struct consdev *cp;
   1310 {
   1311 	/*
   1312 	 * Activate serial console when DCD present...
   1313 	 */
   1314 	if (MFP->mf_gpip & MCR_DCD) {
   1315 		cp->cn_pri = CN_DEAD;
   1316 		return;
   1317 	}
   1318 	for (sermajor = 0; sermajor < nchrdev; sermajor++)
   1319 		if (cdevsw[sermajor].d_open == seropen)
   1320 			break;
   1321 
   1322 	/* initialize required fields */
   1323 	cp->cn_dev = makedev(sermajor, 0); /* XXX: LWP What unit? */
   1324 #ifdef SERCONSOLE
   1325 	cp->cn_pri = CN_REMOTE;	/* Force a serial port console */
   1326 #else
   1327 	cp->cn_pri = CN_NORMAL;
   1328 #endif
   1329 }
   1330 
   1331 void
   1332 sercninit(cp)
   1333 	struct consdev *cp;
   1334 {
   1335 	serinitcons(CONSBAUD);
   1336 }
   1337 
   1338 /*
   1339  * Initialize UART to known state.
   1340  */
   1341 void
   1342 serinit(baud)
   1343 int	baud;
   1344 {
   1345 	int ospeed = serspeed(baud);
   1346 
   1347 	MFP->mf_ucr = UCR_CLKDIV|UCR_8BITS|UCR_STOPB1;
   1348 	MFP->mf_rsr = RSR_ENAB;
   1349 	MFP->mf_tsr = TSR_ENAB;
   1350 
   1351 	single_inst_bclr_b(MFP->mf_tcdcr, 0x07);
   1352 	MFP->mf_tddr  = ospeed;
   1353 	single_inst_bset_b(MFP->mf_tcdcr, (ospeed >> 8) & 0x0f);
   1354 }
   1355 
   1356 /*
   1357  * Set UART for console use. Do normal init, then enable interrupts.
   1358  */
   1359 void
   1360 serinitcons(baud)
   1361 int	baud;
   1362 {
   1363 	serinit(baud);
   1364 
   1365 	/* Set rts/dtr */
   1366 	ym2149_rts(0);
   1367 	ym2149_dtr(0);
   1368 
   1369 	single_inst_bset_b(MFP->mf_imra, (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR));
   1370 }
   1371 
   1372 int
   1373 sercngetc(dev)
   1374 	dev_t dev;
   1375 {
   1376 	u_char	stat, c;
   1377 	int	s;
   1378 
   1379 	s = splserial();
   1380 	while (!ISSET(stat = MFP->mf_rsr, RSR_BFULL)) {
   1381 		if (!ISSET(stat, RSR_ENAB)) /* XXX */
   1382 			MFP->mf_rsr |= RSR_ENAB;
   1383 		if (stat & (RSR_FERR|RSR_PERR|RSR_OERR))
   1384 			c = MFP->mf_udr;
   1385 	}
   1386 	c = MFP->mf_udr;
   1387 	splx(s);
   1388 	return c;
   1389 }
   1390 
   1391 u_int s_imra;
   1392 u_int s_stat1, s_stat2, s_stat3;
   1393 void
   1394 sercnputc(dev, c)
   1395 	dev_t dev;
   1396 	int c;
   1397 {
   1398 	int	timo;
   1399 	u_char	stat, imra;
   1400 
   1401 	/* Mask serial interrupts */
   1402 	imra  = MFP->mf_imra & (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
   1403 	single_inst_bclr_b(MFP->mf_imra, imra);
   1404 s_imra = imra;
   1405 
   1406 	/* wait for any pending transmission to finish */
   1407 	timo = 50000;
   1408 s_stat1 = MFP->mf_tsr;
   1409 	while (!ISSET(stat = MFP->mf_tsr, TSR_BE) && --timo)
   1410 		;
   1411 	MFP->mf_udr = c;
   1412 	/* wait for this transmission to complete */
   1413 	timo = 1500000;
   1414 s_stat2 = MFP->mf_tsr;
   1415 	while (!ISSET(stat = MFP->mf_tsr, TSR_BE) && --timo)
   1416 		;
   1417 
   1418 s_stat3 = MFP->mf_tsr;
   1419 	/* Clear pending serial interrupts and re-enable */
   1420 	single_inst_bclr_b(MFP->mf_ipra, imra);
   1421 	single_inst_bset_b(MFP->mf_imra, imra);
   1422 }
   1423 
   1424 void
   1425 sercnpollc(dev, on)
   1426 	dev_t dev;
   1427 	int on;
   1428 {
   1429 
   1430 }
   1431