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