Home | History | Annotate | Line # | Download | only in dev
mfc.c revision 1.25
      1 /*	$NetBSD: mfc.c,v 1.25 2002/01/26 13:40:58 aymeric Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1994 Michael L. Hitch
      5  * Copyright (c) 1982, 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include "opt_kgdb.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/device.h>
     43 #include <sys/tty.h>
     44 #include <sys/proc.h>
     45 #include <sys/file.h>
     46 #include <sys/malloc.h>
     47 #include <sys/uio.h>
     48 #include <sys/kernel.h>
     49 #include <sys/syslog.h>
     50 #include <sys/queue.h>
     51 #include <machine/cpu.h>
     52 #include <amiga/amiga/device.h>
     53 #include <amiga/amiga/isr.h>
     54 #include <amiga/amiga/custom.h>
     55 #include <amiga/amiga/cia.h>
     56 #include <amiga/amiga/cc.h>
     57 #include <amiga/dev/zbusvar.h>
     58 
     59 #include <dev/cons.h>
     60 
     61 #include <sys/conf.h>
     62 #include <machine/conf.h>
     63 
     64 #include "mfcs.h"
     65 
     66 #ifndef SEROBUF_SIZE
     67 #define SEROBUF_SIZE	128
     68 #endif
     69 #ifndef SERIBUF_SIZE
     70 #define SERIBUF_SIZE	1024
     71 #endif
     72 
     73 #define splser()	spl6()
     74 
     75 /*
     76  * 68581 DUART registers
     77  */
     78 struct mfc_regs {
     79 	volatile u_char du_mr1a;
     80 #define	du_mr2a		du_mr1a
     81 	u_char pad0;
     82 	volatile u_char du_csra;
     83 #define	du_sra		du_csra
     84 	u_char pad2;
     85 	volatile u_char du_cra;
     86 	u_char pad4;
     87 	volatile u_char du_tba;
     88 #define	du_rba		du_tba
     89 	u_char pad6;
     90 	volatile u_char du_acr;
     91 #define	du_ipcr		du_acr
     92 	u_char pad8;
     93 	volatile u_char du_imr;
     94 #define	du_isr		du_imr
     95 	u_char pad10;
     96 	volatile u_char du_ctur;
     97 #define	du_cmsb		du_ctur
     98 	u_char pad12;
     99 	volatile u_char du_ctlr;
    100 #define	du_clsb		du_ctlr
    101 	u_char pad14;
    102 	volatile u_char du_mr1b;
    103 #define	du_mr2b		du_mr1b
    104 	u_char pad16;
    105 	volatile u_char du_csrb;
    106 #define	du_srb		du_csrb
    107 	u_char pad18;
    108 	volatile u_char du_crb;
    109 	u_char pad20;
    110 	volatile u_char du_tbb;
    111 #define	du_rbb		du_tbb
    112 	u_char pad22;
    113 	volatile u_char du_ivr;
    114 	u_char pad24;
    115 	volatile u_char du_opcr;
    116 #define	du_ip		du_opcr
    117 	u_char pad26;
    118 	volatile u_char du_btst;
    119 #define	du_strc		du_btst
    120 	u_char pad28;
    121 	volatile u_char du_btrst;
    122 #define	du_stpc		du_btrst
    123 	u_char pad30;
    124 };
    125 
    126 /*
    127  * 68681 DUART serial port registers
    128  */
    129 struct duart_regs {
    130 	volatile u_char ch_mr1;
    131 #define	ch_mr2		ch_mr1
    132 	u_char pad0;
    133 	volatile u_char	ch_csr;
    134 #define	ch_sr		ch_csr
    135 	u_char pad1;
    136 	volatile u_char	ch_cr;
    137 	u_char pad2;
    138 	volatile u_char	ch_tb;
    139 #define	ch_rb		ch_tb
    140 	u_char pad3;
    141 };
    142 
    143 struct mfc_softc {
    144 	struct	device sc_dev;
    145 	struct	isr sc_isr;
    146 	struct	mfc_regs *sc_regs;
    147 	u_long	clk_frq;
    148 	u_short	ct_val;
    149 	u_char	ct_usecnt;
    150 	u_char	imask;
    151 	u_char	mfc_iii;
    152 	u_char	last_ip;
    153 };
    154 
    155 #if NMFCS > 0
    156 struct mfcs_softc {
    157 	struct	device sc_dev;
    158 	struct	tty *sc_tty;
    159 	struct	duart_regs *sc_duart;
    160 	struct	mfc_regs *sc_regs;
    161 	struct	mfc_softc *sc_mfc;
    162 	int	swflags;
    163 	long	flags;			/* XXX */
    164 #define CT_USED	1			/* CT in use */
    165 	u_short	*rptr, *wptr, incnt, ovfl;
    166 	u_short	inbuf[SERIBUF_SIZE];
    167 	char	*ptr, *end;
    168 	char	outbuf[SEROBUF_SIZE];
    169 	struct vbl_node vbl_node;
    170 };
    171 #endif
    172 
    173 #if NMFCP > 0
    174 struct mfcp_softc {
    175 };
    176 #endif
    177 
    178 struct mfc_args {
    179 	struct zbus_args zargs;
    180 	char	*subdev;
    181 	char	unit;
    182 };
    183 
    184 int	mfcprint(void *auxp, const char *);
    185 void	mfcattach(struct device *, struct device *, void *);
    186 int	mfcmatch(struct device *, struct cfdata *, void *);
    187 
    188 #if NMFCS > 0
    189 int	mfcsmatch(struct device *, struct cfdata *, void *);
    190 void	mfcsattach(struct device *, struct device *, void *);
    191 int	mfcsparam( struct tty *, struct termios *);
    192 int	mfcshwiflow(struct tty *, int);
    193 void	mfcsstart(struct tty *);
    194 int	mfcsmctl(dev_t, int, int);
    195 void	mfcsxintr(int);
    196 void	mfcseint(int, int);
    197 void	mfcsmint(register int);
    198 #endif
    199 
    200 #if NMFCP > 0
    201 void mfcpattach(struct device *, struct device *, void *);
    202 int mfcpmatch(struct device *, struct cfdata *, void *);
    203 #endif
    204 int mfcintr(void *);
    205 
    206 struct cfattach mfc_ca = {
    207 	sizeof(struct mfc_softc), mfcmatch, mfcattach
    208 };
    209 
    210 #if NMFCS > 0
    211 struct cfattach mfcs_ca = {
    212 	sizeof(struct mfcs_softc), mfcsmatch, mfcsattach
    213 };
    214 
    215 extern struct cfdriver mfcs_cd;
    216 #endif
    217 
    218 #if NMFCP > 0
    219 struct cfattach mfcp_ca = {
    220 	sizeof(struct mfcp_softc, mfcpmatch, mfcpattach
    221 };
    222 #endif
    223 
    224 
    225 int	mfcs_active;
    226 int	mfcsdefaultrate = 38400 /*TTYDEF_SPEED*/;
    227 #define SWFLAGS(dev) (sc->swflags | (((dev) & 0x80) == 0 ? TIOCFLAG_SOFTCAR : 0))
    228 
    229 #ifdef notyet
    230 /*
    231  * MultiFaceCard III, II+ (not supported yet), and
    232  * SerialMaster 500+ (not supported yet)
    233  * baud rate tables for BRG set 1 [not used yet]
    234  */
    235 
    236 struct speedtab mfcs3speedtab1[] = {
    237 	{ 0,		0	},
    238 	{ 100,		0x00	},
    239 	{ 220,		0x11	},
    240 	{ 600,		0x44	},
    241 	{ 1200,		0x55	},
    242 	{ 2400,		0x66	},
    243 	{ 4800,		0x88	},
    244 	{ 9600,		0x99	},
    245 	{ 19200,	0xbb	},
    246 	{ 115200,	0xcc	},
    247 	{ -1,		-1	}
    248 };
    249 
    250 /*
    251  * MultiFaceCard II, I, and SerialMaster 500
    252  * baud rate tables for BRG set 1 [not used yet]
    253  */
    254 
    255 struct speedtab mfcs2speedtab1[] = {
    256 	{ 0,		0	},
    257 	{ 50,		0x00	},
    258 	{ 110,		0x11	},
    259 	{ 300,		0x44	},
    260 	{ 600,		0x55	},
    261 	{ 1200,		0x66	},
    262 	{ 2400,		0x88	},
    263  	{ 4800,		0x99	},
    264 	{ 9600,		0xbb	},
    265 	{ 38400,	0xcc	},
    266 	{ -1,		-1	}
    267 };
    268 #endif
    269 
    270 /*
    271  * MultiFaceCard III, II+ (not supported yet), and
    272  * SerialMaster 500+ (not supported yet)
    273  * baud rate tables for BRG set 2
    274  */
    275 
    276 struct speedtab mfcs3speedtab2[] = {
    277 	{ 0,		0	},
    278 	{ 150,		0x00	},
    279 	{ 200,		0x11	},
    280 	{ 300,		0x33	},
    281 	{ 600,		0x44	},
    282 	{ 1200,		0x55	},
    283 	{ 2400,		0x66	},
    284 	{ 4800,		0x88	},
    285 	{ 9600,		0x99	},
    286 	{ 19200,	0xbb	},
    287 	{ 38400,	0xcc	},
    288 	{ -1,		-1	}
    289 };
    290 
    291 /*
    292  * MultiFaceCard II, I, and SerialMaster 500
    293  * baud rate tables for BRG set 2
    294  */
    295 
    296 struct speedtab mfcs2speedtab2[] = {
    297 	{ 0,		0	},
    298 	{ 75,		0x00	},
    299 	{ 100,		0x11	},
    300 	{ 150,		0x33	},
    301 	{ 300,		0x44	},
    302 	{ 600,		0x55	},
    303 	{ 1200,		0x66	},
    304 	{ 2400,		0x88	},
    305  	{ 4800,		0x99	},
    306 	{ 9600,		0xbb	},
    307 	{ 19200,	0xcc	},
    308 	{ -1,		-1	}
    309 };
    310 
    311 /*
    312  * if we are an bsc/Alf Data MultFaceCard (I, II, and III)
    313  */
    314 int
    315 mfcmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    316 {
    317 	struct zbus_args *zap;
    318 
    319 	zap = auxp;
    320 	if (zap->manid == 2092 &&
    321 	    (zap->prodid == 16 || zap->prodid == 17 || zap->prodid == 18))
    322 
    323 		return(1);
    324 	return(0);
    325 }
    326 
    327 void
    328 mfcattach(struct device *pdp, struct device *dp, void *auxp)
    329 {
    330 	struct mfc_softc *scc;
    331 	struct zbus_args *zap;
    332 	struct mfc_args ma;
    333 	int unit;
    334 	struct mfc_regs *rp;
    335 
    336 	zap = auxp;
    337 
    338 	printf ("\n");
    339 
    340 	scc = (struct mfc_softc *)dp;
    341 	unit = scc->sc_dev.dv_unit;
    342 	scc->sc_regs = rp = zap->va;
    343 	if (zap->prodid == 18)
    344 		scc->mfc_iii = 3;
    345 	scc->clk_frq = scc->mfc_iii ? 230400 : 115200;
    346 
    347 	rp->du_opcr = 0x00;		/* configure output port? */
    348 	rp->du_btrst = 0x0f;		/* clear modem lines */
    349 	rp->du_ivr = 0;			/* IVR */
    350 	rp->du_imr = 0;			/* IMR */
    351 	rp->du_acr = 0xe0;		/* baud rate generate set 2 */
    352 	rp->du_ctur = 0;
    353 	rp->du_ctlr = 4;
    354 	rp->du_csra = 0xcc;		/* clock select = 38400 */
    355 	rp->du_cra = 0x10;		/* reset mode register ptr */
    356 	rp->du_cra = 0x20;
    357 	rp->du_cra = 0x30;
    358 	rp->du_cra = 0x40;
    359 	rp->du_mr1a = 0x93;		/* MRA1 */
    360 	rp->du_mr2a = 0x17;		/* MRA2 */
    361 	rp->du_csrb = 0xcc;		/* clock select = 38400 */
    362 	rp->du_crb = 0x10;		/* reset mode register ptr */
    363 	rp->du_crb = 0x20;
    364 	rp->du_crb = 0x30;
    365 	rp->du_crb = 0x40;
    366 	rp->du_mr1b = 0x93;		/* MRB1 */
    367 	rp->du_mr2b = 0x17;		/* MRB2 */
    368 	rp->du_cra = 0x05;		/* enable A Rx & Tx */
    369 	rp->du_crb = 0x05;		/* enable B Rx & Tx */
    370 
    371 	scc->sc_isr.isr_intr = mfcintr;
    372 	scc->sc_isr.isr_arg = scc;
    373 	scc->sc_isr.isr_ipl = 6;
    374 	add_isr(&scc->sc_isr);
    375 
    376 	/* configure ports */
    377 	bcopy(zap, &ma.zargs, sizeof(struct zbus_args));
    378 	ma.subdev = "mfcs";
    379 	ma.unit = unit * 2;
    380 	config_found(dp, &ma, mfcprint);
    381 	ma.unit = unit * 2 + 1;
    382 	config_found(dp, &ma, mfcprint);
    383 	ma.subdev = "mfcp";
    384 	ma.unit = unit;
    385 	config_found(dp, &ma, mfcprint);
    386 }
    387 
    388 /*
    389  *
    390  */
    391 int
    392 mfcsmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    393 {
    394 	struct mfc_args *ma;
    395 
    396 	ma = auxp;
    397 	if (strcmp(ma->subdev, "mfcs") == 0)
    398 		return (1);
    399 	return (0);
    400 }
    401 
    402 void
    403 mfcsattach(struct device *pdp, struct device *dp, void *auxp)
    404 {
    405 	int unit;
    406 	struct mfcs_softc *sc;
    407 	struct mfc_softc *scc;
    408 	struct mfc_args *ma;
    409 	struct mfc_regs *rp;
    410 
    411 	sc = (struct mfcs_softc *) dp;
    412 	scc = (struct mfc_softc *) pdp;
    413 	ma = auxp;
    414 
    415 	if (dp) {
    416 		printf (": input fifo %d output fifo %d\n", SERIBUF_SIZE,
    417 		    SEROBUF_SIZE);
    418 		alloc_sicallback();
    419 	}
    420 
    421 	unit = ma->unit;
    422 	mfcs_active |= 1 << unit;
    423 	sc->rptr = sc->wptr = sc->inbuf;
    424 	sc->sc_mfc = scc;
    425 	sc->sc_regs = rp = scc->sc_regs;
    426 	sc->sc_duart = (struct duart_regs *) ((unit & 1) ? &rp->du_mr1b :
    427 	    &rp->du_mr1a);
    428 	/*
    429 	 * should have only one vbl routine to handle all ports?
    430 	 */
    431 	sc->vbl_node.function = (void (*) (void *)) mfcsmint;
    432 	sc->vbl_node.data = (void *) unit;
    433 	add_vbl_function(&sc->vbl_node, 1, (void *) unit);
    434 }
    435 
    436 /*
    437  * print diag if pnp is NULL else just extra
    438  */
    439 int
    440 mfcprint(void *auxp, const char *pnp)
    441 {
    442 	if (pnp == NULL)
    443 		return(UNCONF);
    444 	return(QUIET);
    445 }
    446 
    447 int
    448 mfcsopen(dev_t dev, int flag, int mode, struct proc *p)
    449 {
    450 	struct tty *tp;
    451 	struct mfcs_softc *sc;
    452 	int unit, error, s;
    453 
    454 	error = 0;
    455 	unit = dev & 0x1f;
    456 
    457 	if (unit >= mfcs_cd.cd_ndevs || (mfcs_active & (1 << unit)) == 0)
    458 		return (ENXIO);
    459 	sc = mfcs_cd.cd_devs[unit];
    460 
    461 	s = spltty();
    462 
    463 	if (sc->sc_tty)
    464 		tp = sc->sc_tty;
    465 	else {
    466 		tp = sc->sc_tty = ttymalloc();
    467 		tty_attach(tp);
    468 	}
    469 
    470 	tp->t_oproc = (void (*) (struct tty *)) mfcsstart;
    471 	tp->t_param = mfcsparam;
    472 	tp->t_dev = dev;
    473 	tp->t_hwiflow = mfcshwiflow;
    474 
    475 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
    476 		ttychars(tp);
    477 		if (tp->t_ispeed == 0) {
    478 			/*
    479 			 * only when cleared do we reset to defaults.
    480 			 */
    481 			tp->t_iflag = TTYDEF_IFLAG;
    482 			tp->t_oflag = TTYDEF_OFLAG;
    483 			tp->t_cflag = TTYDEF_CFLAG;
    484 			tp->t_lflag = TTYDEF_LFLAG;
    485 			tp->t_ispeed = tp->t_ospeed = mfcsdefaultrate;
    486 		}
    487 		/*
    488 		 * do these all the time
    489 		 */
    490 		if (sc->swflags & TIOCFLAG_CLOCAL)
    491 			tp->t_cflag |= CLOCAL;
    492 		if (sc->swflags & TIOCFLAG_CRTSCTS)
    493 			tp->t_cflag |= CRTSCTS;
    494 		if (sc->swflags & TIOCFLAG_MDMBUF)
    495 			tp->t_cflag |= MDMBUF;
    496 		mfcsparam(tp, &tp->t_termios);
    497 		ttsetwater(tp);
    498 
    499 		(void)mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET);
    500 		if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
    501 		    (mfcsmctl(dev, 0, DMGET) & TIOCM_CD))
    502 			tp->t_state |= TS_CARR_ON;
    503 		else
    504 			tp->t_state &= ~TS_CARR_ON;
    505 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    506 		splx(s);
    507 		return(EBUSY);
    508 	}
    509 
    510 	/*
    511 	 * if NONBLOCK requested, ignore carrier
    512 	 */
    513 	if (flag & O_NONBLOCK)
    514 		goto done;
    515 
    516 	/*
    517 	 * block waiting for carrier
    518 	 */
    519 	while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) {
    520 		tp->t_wopen++;
    521 		error = ttysleep(tp, (caddr_t)&tp->t_rawq,
    522 		    TTIPRI | PCATCH, ttopen, 0);
    523 		tp->t_wopen--;
    524 		if (error) {
    525 			splx(s);
    526 			return(error);
    527 		}
    528 	}
    529 done:
    530 	/* This is a way to handle lost XON characters */
    531 	if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) {
    532 		tp->t_state &= ~TS_TTSTOP;
    533 	        ttstart (tp);
    534 	}
    535 
    536 	splx(s);
    537 	/*
    538 	 * Reset the tty pointer, as there could have been a dialout
    539 	 * use of the tty with a dialin open waiting.
    540 	 */
    541 	tp->t_dev = dev;
    542 	return tp->t_linesw->l_open(dev, tp);
    543 }
    544 
    545 /*ARGSUSED*/
    546 int
    547 mfcsclose(dev_t dev, int flag, int mode, struct proc *p)
    548 {
    549 	struct tty *tp;
    550 	int unit;
    551 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
    552 	struct mfc_softc *scc= sc->sc_mfc;
    553 
    554 	unit = dev & 31;
    555 
    556 	tp = sc->sc_tty;
    557 	tp->t_linesw->l_close(tp, flag);
    558 	sc->sc_duart->ch_cr = 0x70;			/* stop break */
    559 
    560 	scc->imask &= ~(0x7 << ((unit & 1) * 4));
    561 	scc->sc_regs->du_imr = scc->imask;
    562 	if (sc->flags & CT_USED) {
    563 		--scc->ct_usecnt;
    564 		sc->flags &= ~CT_USED;
    565 	}
    566 
    567 	/*
    568 	 * If the device is closed, it's close, no matter whether we deal with
    569 	 * modem control signals nor not.
    570 	 */
    571 #if 0
    572 	if (tp->t_cflag & HUPCL || tp->t_wopen != 0 ||
    573 	    (tp->t_state & TS_ISOPEN) == 0)
    574 #endif
    575 		(void) mfcsmctl(dev, 0, DMSET);
    576 	ttyclose(tp);
    577 #if not_yet
    578 	if (tp != &mfcs_cons) {
    579 		remove_vbl_function(&sc->vbl_node);
    580 		ttyfree(tp);
    581 		sc->sc_tty = (struct tty *) NULL;
    582 	}
    583 #endif
    584 	return (0);
    585 }
    586 
    587 int
    588 mfcsread(dev_t dev, struct uio *uio, int flag)
    589 {
    590 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
    591 	struct tty *tp = sc->sc_tty;
    592 	if (tp == NULL)
    593 		return(ENXIO);
    594 	return tp->t_linesw->l_read(tp, uio, flag);
    595 }
    596 
    597 int
    598 mfcswrite(dev_t dev, struct uio *uio, int flag)
    599 {
    600 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
    601 	struct tty *tp = sc->sc_tty;
    602 
    603 	if (tp == NULL)
    604 		return(ENXIO);
    605 	return tp->t_linesw->l_write(tp, uio, flag);
    606 }
    607 
    608 int
    609 mfcspoll(dev_t dev, int events, struct proc *p)
    610 {
    611 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
    612 	struct tty *tp = sc->sc_tty;
    613 
    614 	if (tp == NULL)
    615 		return(ENXIO);
    616 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    617 }
    618 
    619 struct tty *
    620 mfcstty(dev_t dev)
    621 {
    622 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
    623 
    624 	return (sc->sc_tty);
    625 }
    626 
    627 int
    628 mfcsioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    629 {
    630 	register struct tty *tp;
    631 	register int error;
    632 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
    633 
    634 	tp = sc->sc_tty;
    635 	if (!tp)
    636 		return ENXIO;
    637 
    638 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, p);
    639 	if (error >= 0)
    640 		return(error);
    641 
    642 	error = ttioctl(tp, cmd, data, flag, p);
    643 	if (error >= 0)
    644 		return(error);
    645 
    646 	switch (cmd) {
    647 	case TIOCSBRK:
    648 		sc->sc_duart->ch_cr = 0x60;		/* start break */
    649 		break;
    650 
    651 	case TIOCCBRK:
    652 		sc->sc_duart->ch_cr = 0x70;		/* stop break */
    653 		break;
    654 
    655 	case TIOCSDTR:
    656 		(void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
    657 		break;
    658 
    659 	case TIOCCDTR:
    660 		(void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
    661 		break;
    662 
    663 	case TIOCMSET:
    664 		(void) mfcsmctl(dev, *(int *) data, DMSET);
    665 		break;
    666 
    667 	case TIOCMBIS:
    668 		(void) mfcsmctl(dev, *(int *) data, DMBIS);
    669 		break;
    670 
    671 	case TIOCMBIC:
    672 		(void) mfcsmctl(dev, *(int *) data, DMBIC);
    673 		break;
    674 
    675 	case TIOCMGET:
    676 		*(int *)data = mfcsmctl(dev, 0, DMGET);
    677 		break;
    678 	case TIOCGFLAGS:
    679 		*(int *)data = SWFLAGS(dev);
    680 		break;
    681 	case TIOCSFLAGS:
    682 		error = suser(p->p_ucred, &p->p_acflag);
    683 		if (error != 0)
    684 			return(EPERM);
    685 
    686 		sc->swflags = *(int *)data;
    687                 sc->swflags &= /* only allow valid flags */
    688                   (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
    689 		/* XXXX need to change duart parameters? */
    690 		break;
    691 	default:
    692 		return(ENOTTY);
    693 	}
    694 
    695 	return(0);
    696 }
    697 
    698 int
    699 mfcsparam(struct tty *tp, struct termios *t)
    700 {
    701 	int cflag, unit, ospeed;
    702 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
    703 	struct mfc_softc *scc= sc->sc_mfc;
    704 
    705 	cflag = t->c_cflag;
    706 	unit = tp->t_dev & 31;
    707 	if (sc->flags & CT_USED) {
    708 		--scc->ct_usecnt;
    709 		sc->flags &= ~CT_USED;
    710 	}
    711 	ospeed = ttspeedtab(t->c_ospeed, scc->mfc_iii ? mfcs3speedtab2 :
    712 	    mfcs2speedtab2);
    713 
    714 	/*
    715 	 * If Baud Rate Generator can't generate requested speed,
    716 	 * try to use the counter/timer.
    717 	 */
    718 	if (ospeed < 0 && (scc->clk_frq % t->c_ospeed) == 0) {
    719 		ospeed = scc->clk_frq / t->c_ospeed;	/* divisor */
    720 		if (scc->ct_usecnt > 0 && scc->ct_val != ospeed)
    721 			ospeed = -1;
    722 		else {
    723 			scc->sc_regs->du_ctur = ospeed >> 8;
    724 			scc->sc_regs->du_ctlr = ospeed;
    725 			scc->ct_val = ospeed;
    726 			++scc->ct_usecnt;
    727 			sc->flags |= CT_USED;
    728 			ospeed = 0xdd;
    729 		}
    730 	}
    731 	/* XXXX 68681 duart could handle split speeds */
    732 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    733 		return(EINVAL);
    734 
    735 	/* XXXX handle parity, character size, stop bits, flow control */
    736 
    737 	/*
    738 	 * copy to tty
    739 	 */
    740 	tp->t_ispeed = t->c_ispeed;
    741 	tp->t_ospeed = t->c_ospeed;
    742 	tp->t_cflag = cflag;
    743 
    744 	/*
    745 	 * enable interrupts
    746 	 */
    747 	scc->imask |= (0x2 << ((unit & 1) * 4)) | 0x80;
    748 	scc->sc_regs->du_imr = scc->imask;
    749 #if defined(DEBUG) && 0
    750 	printf("mfcsparam: speed %d => %x ct %d imask %x cflag %x\n",
    751 	    t->c_ospeed, ospeed, scc->ct_val, scc->imask, cflag);
    752 #endif
    753 	if (ospeed == 0)
    754 		(void)mfcsmctl(tp->t_dev, 0, DMSET);	/* hang up line */
    755 	else {
    756 		/*
    757 		 * (re)enable DTR
    758 		 * and set baud rate. (8 bit mode)
    759 		 */
    760 		(void)mfcsmctl(tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);
    761 		sc->sc_duart->ch_csr = ospeed;
    762 	}
    763 	return(0);
    764 }
    765 
    766 int
    767 mfcshwiflow(struct tty *tp, int flag)
    768 {
    769 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
    770 	int unit = tp->t_dev & 1;
    771 
    772         if (flag)
    773 		sc->sc_regs->du_btrst = 1 << unit;
    774 	else
    775 		sc->sc_regs->du_btst = 1 << unit;
    776         return 1;
    777 }
    778 
    779 void
    780 mfcsstart(struct tty *tp)
    781 {
    782 	int cc, s, unit;
    783 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
    784 	struct mfc_softc *scc= sc->sc_mfc;
    785 
    786 	if ((tp->t_state & TS_ISOPEN) == 0)
    787 		return;
    788 
    789 	unit = tp->t_dev & 1;
    790 
    791 	s = splser();
    792 	if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))
    793 		goto out;
    794 
    795 	cc = tp->t_outq.c_cc;
    796 	if (cc <= tp->t_lowat) {
    797 		if (tp->t_state & TS_ASLEEP) {
    798 			tp->t_state &= ~TS_ASLEEP;
    799 			wakeup((caddr_t) & tp->t_outq);
    800 		}
    801 		selwakeup(&tp->t_wsel);
    802 	}
    803 	if (cc == 0 || (tp->t_state & TS_BUSY))
    804 		goto out;
    805 
    806 	/*
    807 	 * We only do bulk transfers if using CTSRTS flow control, not for
    808 	 * (probably sloooow) ixon/ixoff devices.
    809 	 */
    810 	if ((tp->t_cflag & CRTSCTS) == 0)
    811 		cc = 1;
    812 
    813 	/*
    814 	 * Limit the amount of output we do in one burst
    815 	 * to prevent hogging the CPU.
    816 	 */
    817 	if (cc > SEROBUF_SIZE)
    818 		cc = SEROBUF_SIZE;
    819 	cc = q_to_b(&tp->t_outq, sc->outbuf, cc);
    820 	if (cc > 0) {
    821 		tp->t_state |= TS_BUSY;
    822 
    823 		sc->ptr = sc->outbuf;
    824 		sc->end = sc->outbuf + cc;
    825 
    826 		/*
    827 		 * Get first character out, then have TBE-interrupts blow out
    828 		 * further characters, until buffer is empty, and TS_BUSY gets
    829 		 * cleared.
    830 		 */
    831 		sc->sc_duart->ch_tb = *sc->ptr++;
    832 		scc->imask |= 1 << (unit * 4);
    833 		sc->sc_regs->du_imr = scc->imask;
    834 	}
    835 out:
    836 	splx(s);
    837 }
    838 
    839 /*
    840  * Stop output on a line.
    841  */
    842 /*ARGSUSED*/
    843 void
    844 mfcsstop(struct tty *tp, int flag)
    845 {
    846 	int s;
    847 
    848 	s = splser();
    849 	if (tp->t_state & TS_BUSY) {
    850 		if ((tp->t_state & TS_TTSTOP) == 0)
    851 			tp->t_state |= TS_FLUSH;
    852 	}
    853 	splx(s);
    854 }
    855 
    856 int
    857 mfcsmctl(dev_t dev, int bits, int how)
    858 {
    859 	int unit, s;
    860 	u_char ub = 0;
    861 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
    862 
    863 	unit = dev & 1;
    864 
    865 	/*
    866 	 * convert TIOCM* mask into CIA mask
    867 	 * which is active low
    868 	 */
    869 	if (how != DMGET) {
    870 		/*
    871 		 * need to save current state of DTR & RTS ?
    872 		 */
    873 		if (bits & TIOCM_DTR)
    874 			ub |= 0x04 << unit;
    875 		if (bits & TIOCM_RTS)
    876 			ub |= 0x01 << unit;
    877 	}
    878 	s = splser();
    879 	switch (how) {
    880 	case DMSET:
    881 		sc->sc_regs->du_btst = ub;
    882 		sc->sc_regs->du_btrst = ub ^ (0x05 << unit);
    883 		break;
    884 
    885 	case DMBIC:
    886 		sc->sc_regs->du_btrst = ub;
    887 		ub = ~sc->sc_regs->du_ip;
    888 		break;
    889 
    890 	case DMBIS:
    891 		sc->sc_regs->du_btst = ub;
    892 		ub = ~sc->sc_regs->du_ip;
    893 		break;
    894 
    895 	case DMGET:
    896 		ub = ~sc->sc_regs->du_ip;
    897 		break;
    898 	}
    899 	(void)splx(s);
    900 
    901 	/* XXXX should keep DTR & RTS states in softc? */
    902 	bits = TIOCM_DTR | TIOCM_RTS;
    903 	if (ub & (1 << unit))
    904 		bits |= TIOCM_CTS;
    905 	if (ub & (4 << unit))
    906 		bits |= TIOCM_DSR;
    907 	if (ub & (0x10 << unit))
    908 		bits |= TIOCM_CD;
    909 	/* XXXX RI is not supported on all boards */
    910 	if (sc->sc_regs->pad26 & (1 << unit))
    911 		bits |= TIOCM_RI;
    912 
    913 	return(bits);
    914 }
    915 
    916 /*
    917  * Level 6 interrupt processing for the MultiFaceCard 68681 DUART
    918  */
    919 
    920 int
    921 mfcintr(void *arg)
    922 {
    923 	struct mfc_softc *scc = arg;
    924 	struct mfcs_softc *sc;
    925 	struct mfc_regs *regs;
    926 	struct tty *tp;
    927 	int istat, unit;
    928 	u_short c;
    929 
    930 	regs = scc->sc_regs;
    931 	istat = regs->du_isr & scc->imask;
    932 	if (istat == 0)
    933 		return (0);
    934 	unit = scc->sc_dev.dv_unit * 2;
    935 	if (istat & 0x02) {		/* channel A receive interrupt */
    936 		sc = mfcs_cd.cd_devs[unit];
    937 		while (1) {
    938 			c = regs->du_sra << 8;
    939 			if ((c & 0x0100) == 0)
    940 				break;
    941 			c |= regs->du_rba;
    942 			if (sc->incnt == SERIBUF_SIZE)
    943 				++sc->ovfl;
    944 			else {
    945 				*sc->wptr++ = c;
    946 				if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
    947 					sc->wptr = sc->inbuf;
    948 				++sc->incnt;
    949 				if (sc->incnt > SERIBUF_SIZE - 16)
    950 					regs->du_btrst = 1;
    951 			}
    952 			if (c & 0x1000)
    953 				regs->du_cra = 0x40;
    954 		}
    955 	}
    956 	if (istat & 0x20) {		/* channel B receive interrupt */
    957 		sc = mfcs_cd.cd_devs[unit + 1];
    958 		while (1) {
    959 			c = regs->du_srb << 8;
    960 			if ((c & 0x0100) == 0)
    961 				break;
    962 			c |= regs->du_rbb;
    963 			if (sc->incnt == SERIBUF_SIZE)
    964 				++sc->ovfl;
    965 			else {
    966 				*sc->wptr++ = c;
    967 				if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
    968 					sc->wptr = sc->inbuf;
    969 				++sc->incnt;
    970 				if (sc->incnt > SERIBUF_SIZE - 16)
    971 					regs->du_btrst = 2;
    972 			}
    973 			if (c & 0x1000)
    974 				regs->du_crb = 0x40;
    975 		}
    976 	}
    977 	if (istat & 0x01) {		/* channel A transmit interrupt */
    978 		sc = mfcs_cd.cd_devs[unit];
    979 		tp = sc->sc_tty;
    980 		if (sc->ptr == sc->end) {
    981 			tp->t_state &= ~(TS_BUSY | TS_FLUSH);
    982 			scc->imask &= ~0x01;
    983 			regs->du_imr = scc->imask;
    984 			add_sicallback (tp->t_linesw ?
    985 			    (sifunc_t)tp->t_linesw->l_start
    986 			    : (sifunc_t)mfcsstart, tp, NULL);
    987 
    988 		}
    989 		else
    990 			regs->du_tba = *sc->ptr++;
    991 	}
    992 	if (istat & 0x10) {		/* channel B transmit interrupt */
    993 		sc = mfcs_cd.cd_devs[unit + 1];
    994 		tp = sc->sc_tty;
    995 		if (sc->ptr == sc->end) {
    996 			tp->t_state &= ~(TS_BUSY | TS_FLUSH);
    997 			scc->imask &= ~0x10;
    998 			regs->du_imr = scc->imask;
    999 			add_sicallback (tp->t_linesw ?
   1000 			    (sifunc_t)tp->t_linesw->l_start
   1001 			    : (sifunc_t)mfcsstart, tp, NULL);
   1002 		}
   1003 		else
   1004 			regs->du_tbb = *sc->ptr++;
   1005 	}
   1006 	if (istat & 0x80) {		/* input port change interrupt */
   1007 		c = regs->du_ipcr;
   1008 		printf ("%s: ipcr %02x", scc->sc_dev.dv_xname, c);
   1009 	}
   1010 	return(1);
   1011 }
   1012 
   1013 void
   1014 mfcsxintr(int unit)
   1015 {
   1016 	int s1, s2, ovfl;
   1017 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
   1018 	struct tty *tp = sc->sc_tty;
   1019 
   1020 	/*
   1021 	 * Make sure we're not interrupted by another
   1022 	 * vbl, but allow level6 ints
   1023 	 */
   1024 	s1 = spltty();
   1025 
   1026 	/*
   1027 	 * pass along any acumulated information
   1028 	 * while input is not blocked
   1029 	 */
   1030 	while (sc->incnt && (tp->t_state & TS_TBLOCK) == 0) {
   1031 		/*
   1032 		 * no collision with ser_fastint()
   1033 		 */
   1034 		mfcseint(unit, *sc->rptr++);
   1035 
   1036 		ovfl = 0;
   1037 		/* lock against mfcs_fastint() */
   1038 		s2 = splser();
   1039 		--sc->incnt;
   1040 		if (sc->rptr == sc->inbuf + SERIBUF_SIZE)
   1041 			sc->rptr = sc->inbuf;
   1042 		if (sc->ovfl != 0) {
   1043 			ovfl = sc->ovfl;
   1044 			sc->ovfl = 0;
   1045 		}
   1046 		splx(s2);
   1047 		if (ovfl != 0)
   1048 			log(LOG_WARNING, "%s: %d buffer overflow!\n",
   1049 			    sc->sc_dev.dv_xname, ovfl);
   1050 	}
   1051 	if (sc->incnt == 0 && (tp->t_state & TS_TBLOCK) == 0) {
   1052 		sc->sc_regs->du_btst = 1 << unit;	/* XXXX */
   1053 	}
   1054 	splx(s1);
   1055 }
   1056 
   1057 void
   1058 mfcseint(int unit, int stat)
   1059 {
   1060 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
   1061 	struct tty *tp;
   1062 	u_char ch;
   1063 	int c;
   1064 
   1065 	tp = sc->sc_tty;
   1066 	ch = stat & 0xff;
   1067 	c = ch;
   1068 
   1069 	if ((tp->t_state & TS_ISOPEN) == 0) {
   1070 #ifdef KGDB
   1071 		/* we don't care about parity errors */
   1072 		if (kgdb_dev == makedev(sermajor, unit) && c == FRAME_END)
   1073 			kgdb_connect(0);	/* trap into kgdb */
   1074 #endif
   1075 		return;
   1076 	}
   1077 
   1078 	/*
   1079 	 * Check for break and (if enabled) parity error.
   1080 	 */
   1081 	if (stat & 0xc000)
   1082 		c |= TTY_FE;
   1083 	else if (stat & 0x2000)
   1084 			c |= TTY_PE;
   1085 
   1086 	if (stat & 0x1000)
   1087 		log(LOG_WARNING, "%s: fifo overflow\n",
   1088 		    ((struct mfcs_softc *)mfcs_cd.cd_devs[unit])->sc_dev.dv_xname);
   1089 
   1090 	tp->t_linesw->l_rint(c, tp);
   1091 }
   1092 
   1093 /*
   1094  * This interrupt is periodically invoked in the vertical blank
   1095  * interrupt.  It's used to keep track of the modem control lines
   1096  * and (new with the fast_int code) to move accumulated data
   1097  * up into the tty layer.
   1098  */
   1099 void
   1100 mfcsmint(int unit)
   1101 {
   1102 	struct tty *tp;
   1103 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
   1104 	u_char stat, last, istat;
   1105 
   1106 	tp = sc->sc_tty;
   1107 	if (!tp)
   1108 		return;
   1109 
   1110 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
   1111 		sc->rptr = sc->wptr = sc->inbuf;
   1112 		sc->incnt = 0;
   1113 		return;
   1114 	}
   1115 	/*
   1116 	 * empty buffer
   1117 	 */
   1118 	mfcsxintr(unit);
   1119 
   1120 	stat = ~sc->sc_regs->du_ip;
   1121 	last = sc->sc_mfc->last_ip;
   1122 	sc->sc_mfc->last_ip = stat;
   1123 
   1124 	/*
   1125 	 * check whether any interesting signal changed state
   1126 	 */
   1127 	istat = stat ^ last;
   1128 
   1129 	if ((istat & (0x10 << (unit & 1))) && 		/* CD changed */
   1130 	    (SWFLAGS(tp->t_dev) & TIOCFLAG_SOFTCAR) == 0) {
   1131 		if (stat & (0x10 << (unit & 1)))
   1132 			tp->t_linesw->l_modem(tp, 1);
   1133 		else if (tp->t_linesw->l_modem(tp, 0) == 0) {
   1134 			sc->sc_regs->du_btrst = 0x0a << (unit & 1);
   1135 		}
   1136 	}
   1137 }
   1138