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