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