xref: /netbsd-src/sys/arch/amiga/dev/mfc.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: mfc.c,v 1.24 2001/05/30 15:24:27 lukem 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 __P((void *auxp, const char *));
185 void	mfcattach __P((struct device *, struct device *, void *));
186 int	mfcmatch __P((struct device *, struct cfdata *, void *));
187 
188 #if NMFCS > 0
189 int	mfcsmatch __P((struct device *, struct cfdata *, void *));
190 void	mfcsattach __P((struct device *, struct device *, void *));
191 int	mfcsparam __P(( struct tty *, struct termios *));
192 int	mfcshwiflow __P((struct tty *, int));
193 void	mfcsstart __P((struct tty *));
194 int	mfcsmctl __P((dev_t, int, int));
195 void	mfcsxintr __P((int));
196 void	mfcseint __P((int, int));
197 void	mfcsmint __P((register int));
198 #endif
199 
200 #if NMFCP > 0
201 void mfcpattach __P((struct device *, struct device *, void *));
202 int mfcpmatch __P((struct device *, struct cfdata *, void *));
203 #endif
204 int mfcintr __P((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(pdp, cfp, auxp)
316 	struct device *pdp;
317 	struct cfdata *cfp;
318 	void *auxp;
319 {
320 	struct zbus_args *zap;
321 
322 	zap = auxp;
323 	if (zap->manid == 2092 &&
324 	    (zap->prodid == 16 || zap->prodid == 17 || zap->prodid == 18))
325 
326 		return(1);
327 	return(0);
328 }
329 
330 void
331 mfcattach(pdp, dp, auxp)
332 	struct device *pdp, *dp;
333 	void *auxp;
334 {
335 	struct mfc_softc *scc;
336 	struct zbus_args *zap;
337 	struct mfc_args ma;
338 	int unit;
339 	struct mfc_regs *rp;
340 
341 	zap = auxp;
342 
343 	printf ("\n");
344 
345 	scc = (struct mfc_softc *)dp;
346 	unit = scc->sc_dev.dv_unit;
347 	scc->sc_regs = rp = zap->va;
348 	if (zap->prodid == 18)
349 		scc->mfc_iii = 3;
350 	scc->clk_frq = scc->mfc_iii ? 230400 : 115200;
351 
352 	rp->du_opcr = 0x00;		/* configure output port? */
353 	rp->du_btrst = 0x0f;		/* clear modem lines */
354 	rp->du_ivr = 0;			/* IVR */
355 	rp->du_imr = 0;			/* IMR */
356 	rp->du_acr = 0xe0;		/* baud rate generate set 2 */
357 	rp->du_ctur = 0;
358 	rp->du_ctlr = 4;
359 	rp->du_csra = 0xcc;		/* clock select = 38400 */
360 	rp->du_cra = 0x10;		/* reset mode register ptr */
361 	rp->du_cra = 0x20;
362 	rp->du_cra = 0x30;
363 	rp->du_cra = 0x40;
364 	rp->du_mr1a = 0x93;		/* MRA1 */
365 	rp->du_mr2a = 0x17;		/* MRA2 */
366 	rp->du_csrb = 0xcc;		/* clock select = 38400 */
367 	rp->du_crb = 0x10;		/* reset mode register ptr */
368 	rp->du_crb = 0x20;
369 	rp->du_crb = 0x30;
370 	rp->du_crb = 0x40;
371 	rp->du_mr1b = 0x93;		/* MRB1 */
372 	rp->du_mr2b = 0x17;		/* MRB2 */
373 	rp->du_cra = 0x05;		/* enable A Rx & Tx */
374 	rp->du_crb = 0x05;		/* enable B Rx & Tx */
375 
376 	scc->sc_isr.isr_intr = mfcintr;
377 	scc->sc_isr.isr_arg = scc;
378 	scc->sc_isr.isr_ipl = 6;
379 	add_isr(&scc->sc_isr);
380 
381 	/* configure ports */
382 	bcopy(zap, &ma.zargs, sizeof(struct zbus_args));
383 	ma.subdev = "mfcs";
384 	ma.unit = unit * 2;
385 	config_found(dp, &ma, mfcprint);
386 	ma.unit = unit * 2 + 1;
387 	config_found(dp, &ma, mfcprint);
388 	ma.subdev = "mfcp";
389 	ma.unit = unit;
390 	config_found(dp, &ma, mfcprint);
391 }
392 
393 /*
394  *
395  */
396 int
397 mfcsmatch(pdp, cfp, auxp)
398 	struct device *pdp;
399 	struct cfdata *cfp;
400 	void *auxp;
401 {
402 	struct mfc_args *ma;
403 
404 	ma = auxp;
405 	if (strcmp(ma->subdev, "mfcs") == 0)
406 		return (1);
407 	return (0);
408 }
409 
410 void
411 mfcsattach(pdp, dp, auxp)
412 	struct device *pdp, *dp;
413 	void *auxp;
414 {
415 	int unit;
416 	struct mfcs_softc *sc;
417 	struct mfc_softc *scc;
418 	struct mfc_args *ma;
419 	struct mfc_regs *rp;
420 
421 	sc = (struct mfcs_softc *) dp;
422 	scc = (struct mfc_softc *) pdp;
423 	ma = auxp;
424 
425 	if (dp) {
426 		printf (": input fifo %d output fifo %d\n", SERIBUF_SIZE,
427 		    SEROBUF_SIZE);
428 		alloc_sicallback();
429 	}
430 
431 	unit = ma->unit;
432 	mfcs_active |= 1 << unit;
433 	sc->rptr = sc->wptr = sc->inbuf;
434 	sc->sc_mfc = scc;
435 	sc->sc_regs = rp = scc->sc_regs;
436 	sc->sc_duart = (struct duart_regs *) ((unit & 1) ? &rp->du_mr1b :
437 	    &rp->du_mr1a);
438 	/*
439 	 * should have only one vbl routine to handle all ports?
440 	 */
441 	sc->vbl_node.function = (void (*) (void *)) mfcsmint;
442 	sc->vbl_node.data = (void *) unit;
443 	add_vbl_function(&sc->vbl_node, 1, (void *) unit);
444 }
445 
446 /*
447  * print diag if pnp is NULL else just extra
448  */
449 int
450 mfcprint(auxp, pnp)
451 	void *auxp;
452 	const char *pnp;
453 {
454 	if (pnp == NULL)
455 		return(UNCONF);
456 	return(QUIET);
457 }
458 
459 int
460 mfcsopen(dev, flag, mode, p)
461 	dev_t dev;
462 	int flag, mode;
463 	struct proc *p;
464 {
465 	struct tty *tp;
466 	struct mfcs_softc *sc;
467 	int unit, error, s;
468 
469 	error = 0;
470 	unit = dev & 0x1f;
471 
472 	if (unit >= mfcs_cd.cd_ndevs || (mfcs_active & (1 << unit)) == 0)
473 		return (ENXIO);
474 	sc = mfcs_cd.cd_devs[unit];
475 
476 	s = spltty();
477 
478 	if (sc->sc_tty)
479 		tp = sc->sc_tty;
480 	else {
481 		tp = sc->sc_tty = ttymalloc();
482 		tty_attach(tp);
483 	}
484 
485 	tp->t_oproc = (void (*) (struct tty *)) mfcsstart;
486 	tp->t_param = mfcsparam;
487 	tp->t_dev = dev;
488 	tp->t_hwiflow = mfcshwiflow;
489 
490 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
491 		ttychars(tp);
492 		if (tp->t_ispeed == 0) {
493 			/*
494 			 * only when cleared do we reset to defaults.
495 			 */
496 			tp->t_iflag = TTYDEF_IFLAG;
497 			tp->t_oflag = TTYDEF_OFLAG;
498 			tp->t_cflag = TTYDEF_CFLAG;
499 			tp->t_lflag = TTYDEF_LFLAG;
500 			tp->t_ispeed = tp->t_ospeed = mfcsdefaultrate;
501 		}
502 		/*
503 		 * do these all the time
504 		 */
505 		if (sc->swflags & TIOCFLAG_CLOCAL)
506 			tp->t_cflag |= CLOCAL;
507 		if (sc->swflags & TIOCFLAG_CRTSCTS)
508 			tp->t_cflag |= CRTSCTS;
509 		if (sc->swflags & TIOCFLAG_MDMBUF)
510 			tp->t_cflag |= MDMBUF;
511 		mfcsparam(tp, &tp->t_termios);
512 		ttsetwater(tp);
513 
514 		(void)mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET);
515 		if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
516 		    (mfcsmctl(dev, 0, DMGET) & TIOCM_CD))
517 			tp->t_state |= TS_CARR_ON;
518 		else
519 			tp->t_state &= ~TS_CARR_ON;
520 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
521 		splx(s);
522 		return(EBUSY);
523 	}
524 
525 	/*
526 	 * if NONBLOCK requested, ignore carrier
527 	 */
528 	if (flag & O_NONBLOCK)
529 		goto done;
530 
531 	/*
532 	 * block waiting for carrier
533 	 */
534 	while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) {
535 		tp->t_wopen++;
536 		error = ttysleep(tp, (caddr_t)&tp->t_rawq,
537 		    TTIPRI | PCATCH, ttopen, 0);
538 		tp->t_wopen--;
539 		if (error) {
540 			splx(s);
541 			return(error);
542 		}
543 	}
544 done:
545 	/* This is a way to handle lost XON characters */
546 	if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) {
547 		tp->t_state &= ~TS_TTSTOP;
548 	        ttstart (tp);
549 	}
550 
551 	splx(s);
552 	/*
553 	 * Reset the tty pointer, as there could have been a dialout
554 	 * use of the tty with a dialin open waiting.
555 	 */
556 	tp->t_dev = dev;
557 	return tp->t_linesw->l_open(dev, tp);
558 }
559 
560 /*ARGSUSED*/
561 int
562 mfcsclose(dev, flag, mode, p)
563 	dev_t dev;
564 	int flag, mode;
565 	struct proc *p;
566 {
567 	struct tty *tp;
568 	int unit;
569 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
570 	struct mfc_softc *scc= sc->sc_mfc;
571 
572 	unit = dev & 31;
573 
574 	tp = sc->sc_tty;
575 	tp->t_linesw->l_close(tp, flag);
576 	sc->sc_duart->ch_cr = 0x70;			/* stop break */
577 
578 	scc->imask &= ~(0x7 << ((unit & 1) * 4));
579 	scc->sc_regs->du_imr = scc->imask;
580 	if (sc->flags & CT_USED) {
581 		--scc->ct_usecnt;
582 		sc->flags &= ~CT_USED;
583 	}
584 
585 	/*
586 	 * If the device is closed, it's close, no matter whether we deal with
587 	 * modem control signals nor not.
588 	 */
589 #if 0
590 	if (tp->t_cflag & HUPCL || tp->t_wopen != 0 ||
591 	    (tp->t_state & TS_ISOPEN) == 0)
592 #endif
593 		(void) mfcsmctl(dev, 0, DMSET);
594 	ttyclose(tp);
595 #if not_yet
596 	if (tp != &mfcs_cons) {
597 		remove_vbl_function(&sc->vbl_node);
598 		ttyfree(tp);
599 		sc->sc_tty = (struct tty *) NULL;
600 	}
601 #endif
602 	return (0);
603 }
604 
605 int
606 mfcsread(dev, uio, flag)
607 	dev_t dev;
608 	struct uio *uio;
609 	int flag;
610 {
611 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
612 	struct tty *tp = sc->sc_tty;
613 	if (tp == NULL)
614 		return(ENXIO);
615 	return tp->t_linesw->l_read(tp, uio, flag);
616 }
617 
618 int
619 mfcswrite(dev, uio, flag)
620 	dev_t dev;
621 	struct uio *uio;
622 	int flag;
623 {
624 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
625 	struct tty *tp = sc->sc_tty;
626 
627 	if (tp == NULL)
628 		return(ENXIO);
629 	return tp->t_linesw->l_write(tp, uio, flag);
630 }
631 
632 int
633 mfcspoll(dev, events, p)
634 	dev_t dev;
635 	int events;
636 	struct proc *p;
637 {
638 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
639 	struct tty *tp = sc->sc_tty;
640 
641 	if (tp == NULL)
642 		return(ENXIO);
643 	return ((*tp->t_linesw->l_poll)(tp, events, p));
644 }
645 
646 struct tty *
647 mfcstty(dev)
648 	dev_t dev;
649 {
650 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
651 
652 	return (sc->sc_tty);
653 }
654 
655 int
656 mfcsioctl(dev, cmd, data, flag, p)
657 	dev_t	dev;
658 	u_long	cmd;
659 	caddr_t data;
660 	int	flag;
661 	struct proc *p;
662 {
663 	register struct tty *tp;
664 	register int error;
665 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
666 
667 	tp = sc->sc_tty;
668 	if (!tp)
669 		return ENXIO;
670 
671 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, p);
672 	if (error >= 0)
673 		return(error);
674 
675 	error = ttioctl(tp, cmd, data, flag, p);
676 	if (error >= 0)
677 		return(error);
678 
679 	switch (cmd) {
680 	case TIOCSBRK:
681 		sc->sc_duart->ch_cr = 0x60;		/* start break */
682 		break;
683 
684 	case TIOCCBRK:
685 		sc->sc_duart->ch_cr = 0x70;		/* stop break */
686 		break;
687 
688 	case TIOCSDTR:
689 		(void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
690 		break;
691 
692 	case TIOCCDTR:
693 		(void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
694 		break;
695 
696 	case TIOCMSET:
697 		(void) mfcsmctl(dev, *(int *) data, DMSET);
698 		break;
699 
700 	case TIOCMBIS:
701 		(void) mfcsmctl(dev, *(int *) data, DMBIS);
702 		break;
703 
704 	case TIOCMBIC:
705 		(void) mfcsmctl(dev, *(int *) data, DMBIC);
706 		break;
707 
708 	case TIOCMGET:
709 		*(int *)data = mfcsmctl(dev, 0, DMGET);
710 		break;
711 	case TIOCGFLAGS:
712 		*(int *)data = SWFLAGS(dev);
713 		break;
714 	case TIOCSFLAGS:
715 		error = suser(p->p_ucred, &p->p_acflag);
716 		if (error != 0)
717 			return(EPERM);
718 
719 		sc->swflags = *(int *)data;
720                 sc->swflags &= /* only allow valid flags */
721                   (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
722 		/* XXXX need to change duart parameters? */
723 		break;
724 	default:
725 		return(ENOTTY);
726 	}
727 
728 	return(0);
729 }
730 
731 int
732 mfcsparam(tp, t)
733 	struct tty *tp;
734 	struct termios *t;
735 {
736 	int cflag, unit, ospeed;
737 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
738 	struct mfc_softc *scc= sc->sc_mfc;
739 
740 	cflag = t->c_cflag;
741 	unit = tp->t_dev & 31;
742 	if (sc->flags & CT_USED) {
743 		--scc->ct_usecnt;
744 		sc->flags &= ~CT_USED;
745 	}
746 	ospeed = ttspeedtab(t->c_ospeed, scc->mfc_iii ? mfcs3speedtab2 :
747 	    mfcs2speedtab2);
748 
749 	/*
750 	 * If Baud Rate Generator can't generate requested speed,
751 	 * try to use the counter/timer.
752 	 */
753 	if (ospeed < 0 && (scc->clk_frq % t->c_ospeed) == 0) {
754 		ospeed = scc->clk_frq / t->c_ospeed;	/* divisor */
755 		if (scc->ct_usecnt > 0 && scc->ct_val != ospeed)
756 			ospeed = -1;
757 		else {
758 			scc->sc_regs->du_ctur = ospeed >> 8;
759 			scc->sc_regs->du_ctlr = ospeed;
760 			scc->ct_val = ospeed;
761 			++scc->ct_usecnt;
762 			sc->flags |= CT_USED;
763 			ospeed = 0xdd;
764 		}
765 	}
766 	/* XXXX 68681 duart could handle split speeds */
767 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
768 		return(EINVAL);
769 
770 	/* XXXX handle parity, character size, stop bits, flow control */
771 
772 	/*
773 	 * copy to tty
774 	 */
775 	tp->t_ispeed = t->c_ispeed;
776 	tp->t_ospeed = t->c_ospeed;
777 	tp->t_cflag = cflag;
778 
779 	/*
780 	 * enable interrupts
781 	 */
782 	scc->imask |= (0x2 << ((unit & 1) * 4)) | 0x80;
783 	scc->sc_regs->du_imr = scc->imask;
784 #if defined(DEBUG) && 0
785 	printf("mfcsparam: speed %d => %x ct %d imask %x cflag %x\n",
786 	    t->c_ospeed, ospeed, scc->ct_val, scc->imask, cflag);
787 #endif
788 	if (ospeed == 0)
789 		(void)mfcsmctl(tp->t_dev, 0, DMSET);	/* hang up line */
790 	else {
791 		/*
792 		 * (re)enable DTR
793 		 * and set baud rate. (8 bit mode)
794 		 */
795 		(void)mfcsmctl(tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);
796 		sc->sc_duart->ch_csr = ospeed;
797 	}
798 	return(0);
799 }
800 
801 int
802 mfcshwiflow(tp, flag)
803         struct tty *tp;
804         int flag;
805 {
806 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
807 	int unit = tp->t_dev & 1;
808 
809         if (flag)
810 		sc->sc_regs->du_btrst = 1 << unit;
811 	else
812 		sc->sc_regs->du_btst = 1 << unit;
813         return 1;
814 }
815 
816 void
817 mfcsstart(tp)
818 	struct tty *tp;
819 {
820 	int cc, s, unit;
821 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
822 	struct mfc_softc *scc= sc->sc_mfc;
823 
824 	if ((tp->t_state & TS_ISOPEN) == 0)
825 		return;
826 
827 	unit = tp->t_dev & 1;
828 
829 	s = splser();
830 	if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))
831 		goto out;
832 
833 	cc = tp->t_outq.c_cc;
834 	if (cc <= tp->t_lowat) {
835 		if (tp->t_state & TS_ASLEEP) {
836 			tp->t_state &= ~TS_ASLEEP;
837 			wakeup((caddr_t) & tp->t_outq);
838 		}
839 		selwakeup(&tp->t_wsel);
840 	}
841 	if (cc == 0 || (tp->t_state & TS_BUSY))
842 		goto out;
843 
844 	/*
845 	 * We only do bulk transfers if using CTSRTS flow control, not for
846 	 * (probably sloooow) ixon/ixoff devices.
847 	 */
848 	if ((tp->t_cflag & CRTSCTS) == 0)
849 		cc = 1;
850 
851 	/*
852 	 * Limit the amount of output we do in one burst
853 	 * to prevent hogging the CPU.
854 	 */
855 	if (cc > SEROBUF_SIZE)
856 		cc = SEROBUF_SIZE;
857 	cc = q_to_b(&tp->t_outq, sc->outbuf, cc);
858 	if (cc > 0) {
859 		tp->t_state |= TS_BUSY;
860 
861 		sc->ptr = sc->outbuf;
862 		sc->end = sc->outbuf + cc;
863 
864 		/*
865 		 * Get first character out, then have TBE-interrupts blow out
866 		 * further characters, until buffer is empty, and TS_BUSY gets
867 		 * cleared.
868 		 */
869 		sc->sc_duart->ch_tb = *sc->ptr++;
870 		scc->imask |= 1 << (unit * 4);
871 		sc->sc_regs->du_imr = scc->imask;
872 	}
873 out:
874 	splx(s);
875 }
876 
877 /*
878  * Stop output on a line.
879  */
880 /*ARGSUSED*/
881 void
882 mfcsstop(tp, flag)
883 	struct tty *tp;
884 	int flag;
885 {
886 	int s;
887 
888 	s = splser();
889 	if (tp->t_state & TS_BUSY) {
890 		if ((tp->t_state & TS_TTSTOP) == 0)
891 			tp->t_state |= TS_FLUSH;
892 	}
893 	splx(s);
894 }
895 
896 int
897 mfcsmctl(dev, bits, how)
898 	dev_t dev;
899 	int bits, how;
900 {
901 	int unit, s;
902 	u_char ub = 0;
903 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
904 
905 	unit = dev & 1;
906 
907 	/*
908 	 * convert TIOCM* mask into CIA mask
909 	 * which is active low
910 	 */
911 	if (how != DMGET) {
912 		/*
913 		 * need to save current state of DTR & RTS ?
914 		 */
915 		if (bits & TIOCM_DTR)
916 			ub |= 0x04 << unit;
917 		if (bits & TIOCM_RTS)
918 			ub |= 0x01 << unit;
919 	}
920 	s = splser();
921 	switch (how) {
922 	case DMSET:
923 		sc->sc_regs->du_btst = ub;
924 		sc->sc_regs->du_btrst = ub ^ (0x05 << unit);
925 		break;
926 
927 	case DMBIC:
928 		sc->sc_regs->du_btrst = ub;
929 		ub = ~sc->sc_regs->du_ip;
930 		break;
931 
932 	case DMBIS:
933 		sc->sc_regs->du_btst = ub;
934 		ub = ~sc->sc_regs->du_ip;
935 		break;
936 
937 	case DMGET:
938 		ub = ~sc->sc_regs->du_ip;
939 		break;
940 	}
941 	(void)splx(s);
942 
943 	/* XXXX should keep DTR & RTS states in softc? */
944 	bits = TIOCM_DTR | TIOCM_RTS;
945 	if (ub & (1 << unit))
946 		bits |= TIOCM_CTS;
947 	if (ub & (4 << unit))
948 		bits |= TIOCM_DSR;
949 	if (ub & (0x10 << unit))
950 		bits |= TIOCM_CD;
951 	/* XXXX RI is not supported on all boards */
952 	if (sc->sc_regs->pad26 & (1 << unit))
953 		bits |= TIOCM_RI;
954 
955 	return(bits);
956 }
957 
958 /*
959  * Level 6 interrupt processing for the MultiFaceCard 68681 DUART
960  */
961 
962 int
963 mfcintr(arg)
964 	void *arg;
965 {
966 	struct mfc_softc *scc = arg;
967 	struct mfcs_softc *sc;
968 	struct mfc_regs *regs;
969 	struct tty *tp;
970 	int istat, unit;
971 	u_short c;
972 
973 	regs = scc->sc_regs;
974 	istat = regs->du_isr & scc->imask;
975 	if (istat == 0)
976 		return (0);
977 	unit = scc->sc_dev.dv_unit * 2;
978 	if (istat & 0x02) {		/* channel A receive interrupt */
979 		sc = mfcs_cd.cd_devs[unit];
980 		while (1) {
981 			c = regs->du_sra << 8;
982 			if ((c & 0x0100) == 0)
983 				break;
984 			c |= regs->du_rba;
985 			if (sc->incnt == SERIBUF_SIZE)
986 				++sc->ovfl;
987 			else {
988 				*sc->wptr++ = c;
989 				if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
990 					sc->wptr = sc->inbuf;
991 				++sc->incnt;
992 				if (sc->incnt > SERIBUF_SIZE - 16)
993 					regs->du_btrst = 1;
994 			}
995 			if (c & 0x1000)
996 				regs->du_cra = 0x40;
997 		}
998 	}
999 	if (istat & 0x20) {		/* channel B receive interrupt */
1000 		sc = mfcs_cd.cd_devs[unit + 1];
1001 		while (1) {
1002 			c = regs->du_srb << 8;
1003 			if ((c & 0x0100) == 0)
1004 				break;
1005 			c |= regs->du_rbb;
1006 			if (sc->incnt == SERIBUF_SIZE)
1007 				++sc->ovfl;
1008 			else {
1009 				*sc->wptr++ = c;
1010 				if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
1011 					sc->wptr = sc->inbuf;
1012 				++sc->incnt;
1013 				if (sc->incnt > SERIBUF_SIZE - 16)
1014 					regs->du_btrst = 2;
1015 			}
1016 			if (c & 0x1000)
1017 				regs->du_crb = 0x40;
1018 		}
1019 	}
1020 	if (istat & 0x01) {		/* channel A transmit interrupt */
1021 		sc = mfcs_cd.cd_devs[unit];
1022 		tp = sc->sc_tty;
1023 		if (sc->ptr == sc->end) {
1024 			tp->t_state &= ~(TS_BUSY | TS_FLUSH);
1025 			scc->imask &= ~0x01;
1026 			regs->du_imr = scc->imask;
1027 			add_sicallback (tp->t_linesw ?
1028 			    (sifunc_t)tp->t_linesw->l_start
1029 			    : (sifunc_t)mfcsstart, tp, NULL);
1030 
1031 		}
1032 		else
1033 			regs->du_tba = *sc->ptr++;
1034 	}
1035 	if (istat & 0x10) {		/* channel B transmit interrupt */
1036 		sc = mfcs_cd.cd_devs[unit + 1];
1037 		tp = sc->sc_tty;
1038 		if (sc->ptr == sc->end) {
1039 			tp->t_state &= ~(TS_BUSY | TS_FLUSH);
1040 			scc->imask &= ~0x10;
1041 			regs->du_imr = scc->imask;
1042 			add_sicallback (tp->t_linesw ?
1043 			    (sifunc_t)tp->t_linesw->l_start
1044 			    : (sifunc_t)mfcsstart, tp, NULL);
1045 		}
1046 		else
1047 			regs->du_tbb = *sc->ptr++;
1048 	}
1049 	if (istat & 0x80) {		/* input port change interrupt */
1050 		c = regs->du_ipcr;
1051 		printf ("%s: ipcr %02x", scc->sc_dev.dv_xname, c);
1052 	}
1053 	return(1);
1054 }
1055 
1056 void
1057 mfcsxintr(unit)
1058 	int unit;
1059 {
1060 	int s1, s2, ovfl;
1061 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
1062 	struct tty *tp = sc->sc_tty;
1063 
1064 	/*
1065 	 * Make sure we're not interrupted by another
1066 	 * vbl, but allow level6 ints
1067 	 */
1068 	s1 = spltty();
1069 
1070 	/*
1071 	 * pass along any acumulated information
1072 	 * while input is not blocked
1073 	 */
1074 	while (sc->incnt && (tp->t_state & TS_TBLOCK) == 0) {
1075 		/*
1076 		 * no collision with ser_fastint()
1077 		 */
1078 		mfcseint(unit, *sc->rptr++);
1079 
1080 		ovfl = 0;
1081 		/* lock against mfcs_fastint() */
1082 		s2 = splser();
1083 		--sc->incnt;
1084 		if (sc->rptr == sc->inbuf + SERIBUF_SIZE)
1085 			sc->rptr = sc->inbuf;
1086 		if (sc->ovfl != 0) {
1087 			ovfl = sc->ovfl;
1088 			sc->ovfl = 0;
1089 		}
1090 		splx(s2);
1091 		if (ovfl != 0)
1092 			log(LOG_WARNING, "%s: %d buffer overflow!\n",
1093 			    sc->sc_dev.dv_xname, ovfl);
1094 	}
1095 	if (sc->incnt == 0 && (tp->t_state & TS_TBLOCK) == 0) {
1096 		sc->sc_regs->du_btst = 1 << unit;	/* XXXX */
1097 	}
1098 	splx(s1);
1099 }
1100 
1101 void
1102 mfcseint(unit, stat)
1103 	int unit, stat;
1104 {
1105 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
1106 	struct tty *tp;
1107 	u_char ch;
1108 	int c;
1109 
1110 	tp = sc->sc_tty;
1111 	ch = stat & 0xff;
1112 	c = ch;
1113 
1114 	if ((tp->t_state & TS_ISOPEN) == 0) {
1115 #ifdef KGDB
1116 		/* we don't care about parity errors */
1117 		if (kgdb_dev == makedev(sermajor, unit) && c == FRAME_END)
1118 			kgdb_connect(0);	/* trap into kgdb */
1119 #endif
1120 		return;
1121 	}
1122 
1123 	/*
1124 	 * Check for break and (if enabled) parity error.
1125 	 */
1126 	if (stat & 0xc000)
1127 		c |= TTY_FE;
1128 	else if (stat & 0x2000)
1129 			c |= TTY_PE;
1130 
1131 	if (stat & 0x1000)
1132 		log(LOG_WARNING, "%s: fifo overflow\n",
1133 		    ((struct mfcs_softc *)mfcs_cd.cd_devs[unit])->sc_dev.dv_xname);
1134 
1135 	tp->t_linesw->l_rint(c, tp);
1136 }
1137 
1138 /*
1139  * This interrupt is periodically invoked in the vertical blank
1140  * interrupt.  It's used to keep track of the modem control lines
1141  * and (new with the fast_int code) to move accumulated data
1142  * up into the tty layer.
1143  */
1144 void
1145 mfcsmint(unit)
1146 	int unit;
1147 {
1148 	struct tty *tp;
1149 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
1150 	u_char stat, last, istat;
1151 
1152 	tp = sc->sc_tty;
1153 	if (!tp)
1154 		return;
1155 
1156 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
1157 		sc->rptr = sc->wptr = sc->inbuf;
1158 		sc->incnt = 0;
1159 		return;
1160 	}
1161 	/*
1162 	 * empty buffer
1163 	 */
1164 	mfcsxintr(unit);
1165 
1166 	stat = ~sc->sc_regs->du_ip;
1167 	last = sc->sc_mfc->last_ip;
1168 	sc->sc_mfc->last_ip = stat;
1169 
1170 	/*
1171 	 * check whether any interesting signal changed state
1172 	 */
1173 	istat = stat ^ last;
1174 
1175 	if ((istat & (0x10 << (unit & 1))) && 		/* CD changed */
1176 	    (SWFLAGS(tp->t_dev) & TIOCFLAG_SOFTCAR) == 0) {
1177 		if (stat & (0x10 << (unit & 1)))
1178 			tp->t_linesw->l_modem(tp, 1);
1179 		else if (tp->t_linesw->l_modem(tp, 0) == 0) {
1180 			sc->sc_regs->du_btrst = 0x0a << (unit & 1);
1181 		}
1182 	}
1183 }
1184