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