ser.c revision 1.14.2.2 1 /* $NetBSD: ser.c,v 1.14.2.2 2002/09/06 08:33:25 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Leo Weppelman.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*-
39 * Copyright (c) 1993, 1994, 1995, 1996, 1997
40 * Charles M. Hannum. All rights reserved.
41 *
42 * Interrupt processing and hardware flow control partly based on code from
43 * Onno van der Linden and Gordon Ross.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by Charles M. Hannum.
56 * 4. The name of the author may not be used to endorse or promote products
57 * derived from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 /*
72 * Copyright (c) 1991 The Regents of the University of California.
73 * All rights reserved.
74 *
75 * Redistribution and use in source and binary forms, with or without
76 * modification, are permitted provided that the following conditions
77 * are met:
78 * 1. Redistributions of source code must retain the above copyright
79 * notice, this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright
81 * notice, this list of conditions and the following disclaimer in the
82 * documentation and/or other materials provided with the distribution.
83 * 3. All advertising materials mentioning features or use of this software
84 * must display the following acknowledgement:
85 * This product includes software developed by the University of
86 * California, Berkeley and its contributors.
87 * 4. Neither the name of the University nor the names of its contributors
88 * may be used to endorse or promote products derived from this software
89 * without specific prior written permission.
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
92 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
95 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
96 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
97 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
98 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
99 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
100 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
101 * SUCH DAMAGE.
102 *
103 * @(#)com.c 7.5 (Berkeley) 5/16/91
104 */
105
106 #include "opt_ddb.h"
107 #include "opt_mbtype.h"
108 #include "opt_serconsole.h"
109
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/ioctl.h>
113 #include <sys/select.h>
114 #include <sys/tty.h>
115 #include <sys/proc.h>
116 #include <sys/user.h>
117 #include <sys/conf.h>
118 #include <sys/file.h>
119 #include <sys/uio.h>
120 #include <sys/kernel.h>
121 #include <sys/syslog.h>
122 #include <sys/types.h>
123 #include <sys/device.h>
124
125 #include <m68k/asm_single.h>
126
127 #include <machine/iomap.h>
128 #include <machine/mfp.h>
129 #include <atari/atari/intr.h>
130 #include <atari/dev/serreg.h>
131
132 #if !defined(_MILANHW_)
133 #include <atari/dev/ym2149reg.h>
134 #else
135 /* MILAN has no ym2149 */
136 #define ym2149_dtr(set) { \
137 if (set) \
138 single_inst_bset_b(MFP->mf_gpip, 0x08); \
139 else single_inst_bclr_b(MFP->mf_gpip, 0x08); \
140 }
141
142 #define ym2149_rts(set) { \
143 if (set) \
144 single_inst_bset_b(MFP->mf_gpip, 0x01); \
145 else single_inst_bclr_b(MFP->mf_gpip, 0x01); \
146 }
147 #endif /* _MILANHW_ */
148
149 /* #define SER_DEBUG */
150
151 #define SERUNIT(x) (minor(x) & 0x7ffff)
152 #define SERDIALOUT(x) (minor(x) & 0x80000)
153
154 /* XXX */
155 #define CONSBAUD 9600
156 #define CONSCFLAG TTYDEF_CFLAG
157 /* end XXX */
158
159 /* Macros to clear/set/test flags. */
160 #define SET(t, f) (t) |= (f)
161 #define CLR(t, f) (t) &= ~(f)
162 #define ISSET(t, f) ((t) & (f))
163
164 #define splserial() spl6()
165
166 /* Buffer size for character buffer */
167 #define RXBUFSIZE 2048 /* More than enough.. */
168 #define RXBUFMASK (RXBUFSIZE-1) /* Only iff previous is a power of 2 */
169 #define RXHIWAT (RXBUFSIZE >> 2)
170
171 struct ser_softc {
172 struct device sc_dev;
173 struct tty *sc_tty;
174
175 struct callout sc_diag_ch;
176
177 int sc_overflows;
178 int sc_floods;
179 int sc_errors;
180
181 u_char sc_hwflags;
182 u_char sc_swflags;
183
184 int sc_ospeed; /* delay + timer-d data */
185 u_char sc_imra;
186 u_char sc_imrb;
187 u_char sc_ucr; /* Uart control */
188 u_char sc_msr; /* Modem status */
189 u_char sc_tsr; /* Tranceiver status */
190 u_char sc_rsr; /* Receiver status */
191 u_char sc_mcr; /* (Pseudo) Modem ctrl. */
192
193 u_char sc_msr_delta;
194 u_char sc_msr_mask;
195 u_char sc_mcr_active;
196 u_char sc_mcr_dtr, sc_mcr_rts, sc_msr_cts, sc_msr_dcd;
197
198 int sc_r_hiwat;
199 volatile u_int sc_rbget;
200 volatile u_int sc_rbput;
201 volatile u_int sc_rbavail;
202 u_char sc_rbuf[RXBUFSIZE];
203 u_char sc_lbuf[RXBUFSIZE];
204
205 volatile u_char sc_rx_blocked;
206 volatile u_char sc_rx_ready;
207 volatile u_char sc_tx_busy;
208 volatile u_char sc_tx_done;
209 volatile u_char sc_tx_stopped;
210 volatile u_char sc_st_check;
211
212 u_char *sc_tba;
213 int sc_tbc;
214 int sc_heldtbc;
215
216 volatile u_char sc_heldchange;
217 };
218
219 /*
220 * For sc_hwflags:
221 */
222 #define SER_HW_CONSOLE 0x01
223
224 cdev_decl(ser);
225
226 void ser_break __P((struct ser_softc *, int));
227 void ser_hwiflow __P((struct ser_softc *, int));
228 void ser_iflush __P((struct ser_softc *));
229 void ser_loadchannelregs __P((struct ser_softc *));
230 void ser_modem __P((struct ser_softc *, int));
231 void serdiag __P((void *));
232 int serhwiflow __P((struct tty *, int));
233 void serinit __P((int));
234 void serinitcons __P((int));
235 int baud;
236 int sermintr __P((void *));
237 int sertrintr __P((void *));
238 int serparam __P((struct tty *, struct termios *));
239 void serstart __P((struct tty *));
240
241 struct consdev;
242 void sercnprobe __P((struct consdev *));
243 void sercninit __P((struct consdev *));
244 int sercngetc __P((dev_t));
245 void sercnputc __P((dev_t, int));
246 void sercnpollc __P((dev_t, int));
247
248 static void sermsrint __P((struct ser_softc *, struct tty*));
249 static void serrxint __P((struct ser_softc *, struct tty*));
250 static void ser_shutdown __P((struct ser_softc *));
251 static int serspeed __P((long));
252 static void sersoft __P((void *));
253 static void sertxint __P((struct ser_softc *, struct tty*));
254
255 static volatile int ser_softintr_scheduled = 0;
256 static int sermajor;
257
258 /*
259 * Autoconfig stuff
260 */
261 static void serattach __P((struct device *, struct device *, void *));
262 static int sermatch __P((struct device *, struct cfdata *, void *));
263
264 struct cfattach ser_ca = {
265 sizeof(struct ser_softc), sermatch, serattach
266 };
267
268 extern struct cfdriver ser_cd;
269
270 /*ARGSUSED*/
271 static int
272 sermatch(pdp, cfp, auxp)
273 struct device *pdp;
274 struct cfdata *cfp;
275 void *auxp;
276 {
277 static int ser_matched = 0;
278
279 /* Match at most one ser unit */
280 if (strcmp((char *)auxp, "ser") || ser_matched)
281 return 0;
282
283 ser_matched = 1;
284 return 1;
285 }
286
287 /*ARGSUSED*/
288 static void
289 serattach(pdp, dp, auxp)
290 struct device *pdp, *dp;
291 void *auxp;
292 {
293 struct ser_softc *sc = (void *)dp;
294
295 if (intr_establish(1, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
296 printf("serattach: Can't establish interrupt (1)\n");
297 if (intr_establish(2, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
298 printf("serattach: Can't establish interrupt (2)\n");
299 if (intr_establish(14, USER_VEC, 0, (hw_ifun_t)sermintr, sc) == NULL)
300 printf("serattach: Can't establish interrupt (14)\n");
301 if (intr_establish(9, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
302 printf("serattach: Can't establish interrupt (9)\n");
303 if (intr_establish(10, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
304 printf("serattach: Can't establish interrupt (10)\n");
305 if (intr_establish(11, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
306 printf("serattach: Can't establish interrupt (11)\n");
307 if (intr_establish(12, USER_VEC, 0, (hw_ifun_t)sertrintr, sc) == NULL)
308 printf("serattach: Can't establish interrupt (12)\n");
309
310 ym2149_rts(1);
311 ym2149_dtr(1);
312
313 /*
314 * Enable but mask interrupts...
315 * XXX: Look at edge-sensitivity for DCD/CTS interrupts.
316 */
317 MFP->mf_ierb |= IB_SCTS|IB_SDCD;
318 MFP->mf_iera |= IA_RRDY|IA_RERR|IA_TRDY|IA_TERR;
319 MFP->mf_imrb &= ~(IB_SCTS|IB_SDCD);
320 MFP->mf_imra &= ~(IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
321
322 callout_init(&sc->sc_diag_ch);
323
324 #if SERCONSOLE > 0
325 /*
326 * Activate serial console when DCD present...
327 */
328 if (!(MFP->mf_gpip & MCR_DCD))
329 SET(sc->sc_hwflags, SER_HW_CONSOLE);
330 #endif /* SERCONSOLE > 0 */
331
332 printf("\n");
333 if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
334 serinit(CONSBAUD);
335 printf("%s: console\n", sc->sc_dev.dv_xname);
336 }
337 }
338
339 #ifdef SER_DEBUG
340 void serstatus __P((struct ser_softc *, char *));
341 void
342 serstatus(sc, str)
343 struct ser_softc *sc;
344 char *str;
345 {
346 struct tty *tp = sc->sc_tty;
347
348 printf("%s: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
349 sc->sc_dev.dv_xname, str,
350 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
351 ISSET(sc->sc_msr, MCR_DCD) ? "+" : "-",
352 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
353 ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
354 sc->sc_tx_stopped ? "+" : "-");
355
356 printf("%s: %s %scrtscts %scts %sts_ttstop %srts %srx_blocked\n",
357 sc->sc_dev.dv_xname, str,
358 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
359 ISSET(sc->sc_msr, MCR_CTS) ? "+" : "-",
360 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
361 ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
362 sc->sc_rx_blocked ? "+" : "-");
363 }
364 #endif /* SER_DEBUG */
365
366 int
367 seropen(dev, flag, mode, p)
368 dev_t dev;
369 int flag, mode;
370 struct proc *p;
371 {
372 int unit = SERUNIT(dev);
373 struct ser_softc *sc;
374 struct tty *tp;
375 int s, s2;
376 int error = 0;
377
378 if (unit >= ser_cd.cd_ndevs)
379 return (ENXIO);
380 sc = ser_cd.cd_devs[unit];
381 if (!sc)
382 return (ENXIO);
383
384 if (!sc->sc_tty) {
385 tp = sc->sc_tty = ttymalloc();
386 tty_attach(tp);
387 } else
388 tp = sc->sc_tty;
389
390 if (ISSET(tp->t_state, TS_ISOPEN) &&
391 ISSET(tp->t_state, TS_XCLUDE) &&
392 p->p_ucred->cr_uid != 0)
393 return (EBUSY);
394
395 s = spltty();
396
397 /*
398 * Do the following if this is a first open.
399 */
400 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
401 struct termios t;
402
403 /* Turn on interrupts. */
404 sc->sc_imra = IA_RRDY|IA_RERR|IA_TRDY|IA_TERR;
405 sc->sc_imrb = IB_SCTS|IB_SDCD;
406 single_inst_bset_b(MFP->mf_imra, sc->sc_imra);
407 single_inst_bset_b(MFP->mf_imrb, sc->sc_imrb);
408
409 /* Fetch the current modem control status, needed later. */
410 sc->sc_msr = ~MFP->mf_gpip & (IO_SDCD|IO_SCTS|IO_SRI);
411
412 /* Add some entry points needed by the tty layer. */
413 tp->t_oproc = serstart;
414 tp->t_param = serparam;
415 tp->t_hwiflow = serhwiflow;
416 tp->t_dev = dev;
417
418 /*
419 * Initialize the termios status to the defaults. Add in the
420 * sticky bits from TIOCSFLAGS.
421 */
422 t.c_ispeed = 0;
423 if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
424 t.c_ospeed = CONSBAUD;
425 t.c_cflag = CONSCFLAG;
426 }
427 else {
428 t.c_ospeed = TTYDEF_SPEED;
429 t.c_cflag = TTYDEF_CFLAG;
430 }
431 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
432 SET(t.c_cflag, CLOCAL);
433 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
434 SET(t.c_cflag, CRTSCTS);
435 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
436 SET(t.c_cflag, MDMBUF);
437 tp->t_iflag = TTYDEF_IFLAG;
438 tp->t_oflag = TTYDEF_OFLAG;
439 tp->t_lflag = TTYDEF_LFLAG;
440 ttychars(tp);
441 (void) serparam(tp, &t);
442 ttsetwater(tp);
443
444 s2 = splserial();
445
446 /*
447 * Turn on DTR. We must always do this, even if carrier is not
448 * present, because otherwise we'd have to use TIOCSDTR
449 * immediately after setting CLOCAL. We will drop DTR only on
450 * the next high-low transition of DCD, or by explicit request.
451 */
452 ser_modem(sc, 1);
453
454 /* Clear the input ring, and unblock. */
455 sc->sc_rbput = sc->sc_rbget = 0;
456 sc->sc_rbavail = RXBUFSIZE;
457 ser_iflush(sc);
458 sc->sc_rx_blocked = 0;
459 ser_hwiflow(sc, 0);
460
461 #ifdef SER_DEBUG
462 serstatus(sc, "seropen ");
463 #endif
464
465 splx(s2);
466 }
467
468 splx(s);
469
470 error = ttyopen(tp, SERDIALOUT(dev), ISSET(flag, O_NONBLOCK));
471 if (error)
472 goto bad;
473
474 error = (*tp->t_linesw->l_open)(dev, tp);
475 if (error)
476 goto bad;
477
478 return (0);
479
480 bad:
481 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
482 /*
483 * We failed to open the device, and nobody else had it opened.
484 * Clean up the state as appropriate.
485 */
486 ser_shutdown(sc);
487 }
488
489 return (error);
490 }
491
492 int
493 serclose(dev, flag, mode, p)
494 dev_t dev;
495 int flag, mode;
496 struct proc *p;
497 {
498 int unit = SERUNIT(dev);
499 struct ser_softc *sc = ser_cd.cd_devs[unit];
500 struct tty *tp = sc->sc_tty;
501
502 /* XXX This is for cons.c. */
503 if (!ISSET(tp->t_state, TS_ISOPEN))
504 return (0);
505
506 (*tp->t_linesw->l_close)(tp, flag);
507 ttyclose(tp);
508
509 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
510 /*
511 * Although we got a last close, the device may still be in
512 * use; e.g. if this was the dialout node, and there are still
513 * processes waiting for carrier on the non-dialout node.
514 */
515 ser_shutdown(sc);
516 }
517
518 return (0);
519 }
520
521 int
522 serread(dev, uio, flag)
523 dev_t dev;
524 struct uio *uio;
525 int flag;
526 {
527 struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(dev)];
528 struct tty *tp = sc->sc_tty;
529
530 return ((*tp->t_linesw->l_read)(tp, uio, flag));
531 }
532
533 int
534 serwrite(dev, uio, flag)
535 dev_t dev;
536 struct uio *uio;
537 int flag;
538 {
539 struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(dev)];
540 struct tty *tp = sc->sc_tty;
541
542 return ((*tp->t_linesw->l_write)(tp, uio, flag));
543 }
544
545 int
546 serpoll(dev, events, p)
547 dev_t dev;
548 int events;
549 struct proc *p;
550 {
551 struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(dev)];
552 struct tty *tp = sc->sc_tty;
553
554 return ((*tp->t_linesw->l_poll)(tp, events, p));
555 }
556
557 struct tty *
558 sertty(dev)
559 dev_t dev;
560 {
561 struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(dev)];
562 struct tty *tp = sc->sc_tty;
563
564 return (tp);
565 }
566
567 int
568 serioctl(dev, cmd, data, flag, p)
569 dev_t dev;
570 u_long cmd;
571 caddr_t data;
572 int flag;
573 struct proc *p;
574 {
575 int unit = SERUNIT(dev);
576 struct ser_softc *sc = ser_cd.cd_devs[unit];
577 struct tty *tp = sc->sc_tty;
578 int error;
579
580 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
581 if (error != EPASSTHROUGH)
582 return (error);
583
584 error = ttioctl(tp, cmd, data, flag, p);
585 if (error != EPASSTHROUGH)
586 return (error);
587
588 switch (cmd) {
589 case TIOCSBRK:
590 ser_break(sc, 1);
591 break;
592
593 case TIOCCBRK:
594 ser_break(sc, 0);
595 break;
596
597 case TIOCSDTR:
598 ser_modem(sc, 1);
599 break;
600
601 case TIOCCDTR:
602 ser_modem(sc, 0);
603 break;
604
605 case TIOCGFLAGS:
606 *(int *)data = sc->sc_swflags;
607 break;
608
609 case TIOCSFLAGS:
610 error = suser(p->p_ucred, &p->p_acflag);
611 if (error)
612 return (error);
613 sc->sc_swflags = *(int *)data;
614 break;
615
616 case TIOCMSET:
617 case TIOCMBIS:
618 case TIOCMBIC:
619 case TIOCMGET:
620 default:
621 return (EPASSTHROUGH);
622 }
623
624 #ifdef SER_DEBUG
625 serstatus(sc, "serioctl ");
626 #endif
627
628 return (0);
629 }
630
631 void
632 ser_break(sc, onoff)
633 struct ser_softc *sc;
634 int onoff;
635 {
636 int s;
637
638 s = splserial();
639 if (onoff)
640 SET(sc->sc_tsr, TSR_SBREAK);
641 else
642 CLR(sc->sc_tsr, TSR_SBREAK);
643
644 if (!sc->sc_heldchange) {
645 if (sc->sc_tx_busy) {
646 sc->sc_heldtbc = sc->sc_tbc;
647 sc->sc_tbc = 0;
648 sc->sc_heldchange = 1;
649 } else
650 ser_loadchannelregs(sc);
651 }
652 splx(s);
653 }
654
655 void
656 ser_modem(sc, onoff)
657 struct ser_softc *sc;
658 int onoff;
659 {
660 int s;
661
662 s = splserial();
663 if (onoff)
664 SET(sc->sc_mcr, sc->sc_mcr_dtr);
665 else
666 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
667
668 if (!sc->sc_heldchange) {
669 if (sc->sc_tx_busy) {
670 sc->sc_heldtbc = sc->sc_tbc;
671 sc->sc_tbc = 0;
672 sc->sc_heldchange = 1;
673 } else
674 ser_loadchannelregs(sc);
675 }
676 splx(s);
677 }
678
679 int
680 serparam(tp, t)
681 struct tty *tp;
682 struct termios *t;
683 {
684 struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(tp->t_dev)];
685 int ospeed = serspeed(t->c_ospeed);
686 u_char ucr;
687 int s;
688
689 /* check requested parameters */
690 if (ospeed < 0)
691 return (EINVAL);
692 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
693 return (EINVAL);
694
695 sc->sc_rsr = RSR_ENAB;
696 sc->sc_tsr = TSR_ENAB;
697
698 ucr = UCR_CLKDIV;
699
700 switch (ISSET(t->c_cflag, CSIZE)) {
701 case CS5:
702 SET(ucr, UCR_5BITS);
703 break;
704 case CS6:
705 SET(ucr, UCR_6BITS);
706 break;
707 case CS7:
708 SET(ucr, UCR_7BITS);
709 break;
710 case CS8:
711 SET(ucr, UCR_8BITS);
712 break;
713 }
714 if (ISSET(t->c_cflag, PARENB)) {
715 SET(ucr, UCR_PENAB);
716 if (!ISSET(t->c_cflag, PARODD))
717 SET(ucr, UCR_PEVEN);
718 }
719 if (ISSET(t->c_cflag, CSTOPB))
720 SET(ucr, UCR_STOPB2);
721 else
722 SET(ucr, UCR_STOPB1);
723
724 s = splserial();
725
726 sc->sc_ucr = ucr;
727
728 /*
729 * For the console, always force CLOCAL and !HUPCL, so that the port
730 * is always active.
731 */
732 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
733 ISSET(sc->sc_hwflags, SER_HW_CONSOLE)) {
734 SET(t->c_cflag, CLOCAL);
735 CLR(t->c_cflag, HUPCL);
736 }
737
738 /*
739 * If we're not in a mode that assumes a connection is present, then
740 * ignore carrier changes.
741 */
742 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
743 sc->sc_msr_dcd = 0;
744 else
745 sc->sc_msr_dcd = MCR_DCD;
746 /*
747 * Set the flow control pins depending on the current flow control
748 * mode.
749 */
750 if (ISSET(t->c_cflag, CRTSCTS)) {
751 sc->sc_mcr_dtr = MCR_DTR;
752 sc->sc_mcr_rts = MCR_RTS;
753 sc->sc_msr_cts = MCR_CTS;
754 sc->sc_r_hiwat = RXHIWAT;
755 } else if (ISSET(t->c_cflag, MDMBUF)) {
756 /*
757 * For DTR/DCD flow control, make sure we don't toggle DTR for
758 * carrier detection.
759 */
760 sc->sc_mcr_dtr = 0;
761 sc->sc_mcr_rts = MCR_DTR;
762 sc->sc_msr_cts = MCR_DCD;
763 sc->sc_r_hiwat = RXHIWAT;
764 } else {
765 /*
766 * If no flow control, then always set RTS. This will make
767 * the other side happy if it mistakenly thinks we're doing
768 * RTS/CTS flow control.
769 */
770 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
771 sc->sc_mcr_rts = 0;
772 sc->sc_msr_cts = 0;
773 sc->sc_r_hiwat = 0;
774 if (ISSET(sc->sc_mcr, MCR_DTR))
775 SET(sc->sc_mcr, MCR_RTS);
776 else
777 CLR(sc->sc_mcr, MCR_RTS);
778 }
779 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
780
781 #if 0
782 if (ospeed == 0)
783 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
784 else
785 SET(sc->sc_mcr, sc->sc_mcr_dtr);
786 #endif
787
788 sc->sc_ospeed = ospeed;
789
790 /* and copy to tty */
791 tp->t_ispeed = 0;
792 tp->t_ospeed = t->c_ospeed;
793 tp->t_cflag = t->c_cflag;
794
795 if (!sc->sc_heldchange) {
796 if (sc->sc_tx_busy) {
797 sc->sc_heldtbc = sc->sc_tbc;
798 sc->sc_tbc = 0;
799 sc->sc_heldchange = 1;
800 } else
801 ser_loadchannelregs(sc);
802 }
803
804 splx(s);
805
806 /*
807 * Update the tty layer's idea of the carrier bit, in case we changed
808 * CLOCAL or MDMBUF. We don't hang up here; we only do that if we
809 * lose carrier while carrier detection is on.
810 */
811 (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MCR_DCD));
812
813 #ifdef SER_DEBUG
814 serstatus(sc, "serparam ");
815 #endif
816
817 /* XXXXX FIX ME */
818 /* Block or unblock as needed. */
819 if (!ISSET(t->c_cflag, CHWFLOW)) {
820 if (sc->sc_rx_blocked) {
821 sc->sc_rx_blocked = 0;
822 ser_hwiflow(sc, 0);
823 }
824 if (sc->sc_tx_stopped) {
825 sc->sc_tx_stopped = 0;
826 serstart(tp);
827 }
828 } else {
829 #if 0
830 sermsrint(sc, tp);
831 #endif
832 }
833
834 return (0);
835 }
836
837 void
838 ser_iflush(sc)
839 struct ser_softc *sc;
840 {
841 u_char tmp;
842
843 /* flush any pending I/O */
844 while (ISSET(MFP->mf_rsr, RSR_CIP|RSR_BFULL))
845 tmp = MFP->mf_udr;
846 }
847
848 void
849 ser_loadchannelregs(sc)
850 struct ser_softc *sc;
851 {
852 /* XXXXX necessary? */
853 ser_iflush(sc);
854
855 /*
856 * No interrupts please...
857 */
858 if((MFP->mf_imra & (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR)) != sc->sc_imra) {
859 printf("loadchannelregs: mf_imra: %x sc_imra: %x\n", (u_int)MFP->mf_imra,
860 (u_int)sc->sc_imra);
861 }
862 if((MFP->mf_imrb & (IB_SCTS|IB_SDCD)) != sc->sc_imrb) {
863 printf("loadchannelregs: mf_imrb: %x sc_imrb: %x\n", (u_int)MFP->mf_imrb,
864 (u_int)sc->sc_imrb);
865 }
866 single_inst_bclr_b(MFP->mf_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
867 single_inst_bclr_b(MFP->mf_imrb, IB_SCTS|IB_SDCD);
868
869 MFP->mf_ucr = sc->sc_ucr;
870 MFP->mf_rsr = sc->sc_rsr;
871 MFP->mf_tsr = sc->sc_tsr;
872
873 single_inst_bclr_b(MFP->mf_tcdcr, 0x07);
874 MFP->mf_tddr = sc->sc_ospeed;
875 single_inst_bset_b(MFP->mf_tcdcr, (sc->sc_ospeed >> 8) & 0x0f);
876
877 sc->sc_mcr_active = sc->sc_mcr;
878
879 if (machineid & ATARI_HADES) {
880 /* PCB fault, wires exchanged..... */
881 ym2149_rts(!(sc->sc_mcr_active & MCR_DTR));
882 ym2149_dtr(!(sc->sc_mcr_active & MCR_RTS));
883 }
884 else {
885 ym2149_rts(!(sc->sc_mcr_active & MCR_RTS));
886 ym2149_dtr(!(sc->sc_mcr_active & MCR_DTR));
887 }
888
889 single_inst_bset_b(MFP->mf_imra, sc->sc_imra);
890 single_inst_bset_b(MFP->mf_imrb, sc->sc_imrb);
891 }
892
893 int
894 serhwiflow(tp, block)
895 struct tty *tp;
896 int block;
897 {
898 struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(tp->t_dev)];
899 int s;
900
901 if (sc->sc_mcr_rts == 0)
902 return (0);
903
904 s = splserial();
905 if (block) {
906 /*
907 * The tty layer is asking us to block input.
908 * If we already did it, just return TRUE.
909 */
910 if (sc->sc_rx_blocked)
911 goto out;
912 sc->sc_rx_blocked = 1;
913 } else {
914 /*
915 * The tty layer is asking us to resume input.
916 * The input ring is always empty by now.
917 */
918 sc->sc_rx_blocked = 0;
919 }
920 ser_hwiflow(sc, block);
921 out:
922 splx(s);
923 return (1);
924 }
925
926 /*
927 * (un)block input via hw flowcontrol
928 */
929 void
930 ser_hwiflow(sc, block)
931 struct ser_softc *sc;
932 int block;
933 {
934 if (sc->sc_mcr_rts == 0)
935 return;
936
937 if (block) {
938 CLR(sc->sc_mcr, sc->sc_mcr_rts);
939 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
940 } else {
941 SET(sc->sc_mcr, sc->sc_mcr_rts);
942 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
943 }
944 if (machineid & ATARI_HADES) {
945 /* PCB fault, wires exchanged..... */
946 ym2149_dtr(sc->sc_mcr_active & MCR_RTS);
947 }
948 else {
949 ym2149_rts(sc->sc_mcr_active & MCR_RTS);
950 }
951 }
952
953 void
954 serstart(tp)
955 struct tty *tp;
956 {
957 struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(tp->t_dev)];
958 int s;
959
960 s = spltty();
961 if (ISSET(tp->t_state, TS_BUSY))
962 goto out;
963 if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP))
964 goto stopped;
965
966 if (sc->sc_tx_stopped)
967 goto stopped;
968
969 if (tp->t_outq.c_cc <= tp->t_lowat) {
970 if (ISSET(tp->t_state, TS_ASLEEP)) {
971 CLR(tp->t_state, TS_ASLEEP);
972 wakeup(&tp->t_outq);
973 }
974 selwakeup(&tp->t_wsel);
975 if (tp->t_outq.c_cc == 0)
976 goto stopped;
977 }
978
979 /* Grab the first contiguous region of buffer space. */
980 {
981 u_char *tba;
982 int tbc;
983
984 tba = tp->t_outq.c_cf;
985 tbc = ndqb(&tp->t_outq, 0);
986
987 (void)splserial();
988
989 sc->sc_tba = tba;
990 sc->sc_tbc = tbc;
991 }
992
993 SET(tp->t_state, TS_BUSY);
994 sc->sc_tx_busy = 1;
995
996 /* Enable transmit completion interrupts if necessary. */
997 if (!ISSET(sc->sc_imra, IA_TRDY)) {
998 SET(sc->sc_imra, IA_TRDY|IA_TERR);
999 single_inst_bset_b(MFP->mf_imra, IA_TRDY|IA_TERR);
1000 }
1001
1002 /* Output the first char */
1003 MFP->mf_udr = *sc->sc_tba;
1004 sc->sc_tbc --;
1005 sc->sc_tba ++;
1006
1007 splx(s);
1008 return;
1009
1010 stopped:
1011 /* Disable transmit completion interrupts if necessary. */
1012 if (ISSET(sc->sc_imra, IA_TRDY)) {
1013 CLR(sc->sc_imra, IA_TRDY|IA_TERR);
1014 single_inst_bclr_b(MFP->mf_imra, IA_TRDY|IA_TERR);
1015 }
1016 out:
1017 splx(s);
1018 return;
1019 }
1020
1021 /*
1022 * Stop output on a line.
1023 */
1024 void
1025 serstop(tp, flag)
1026 struct tty *tp;
1027 int flag;
1028 {
1029 struct ser_softc *sc = ser_cd.cd_devs[SERUNIT(tp->t_dev)];
1030 int s;
1031
1032 s = splserial();
1033 if (ISSET(tp->t_state, TS_BUSY)) {
1034 /* Stop transmitting at the next chunk. */
1035 sc->sc_tbc = 0;
1036 sc->sc_heldtbc = 0;
1037 if (!ISSET(tp->t_state, TS_TTSTOP))
1038 SET(tp->t_state, TS_FLUSH);
1039 }
1040 splx(s);
1041 }
1042
1043 void
1044 serdiag(arg)
1045 void *arg;
1046 {
1047 struct ser_softc *sc = arg;
1048 int overflows, floods;
1049 int s;
1050
1051 s = splserial();
1052 overflows = sc->sc_overflows;
1053 sc->sc_overflows = 0;
1054 floods = sc->sc_floods;
1055 sc->sc_floods = 0;
1056 sc->sc_errors = 0;
1057 splx(s);
1058
1059 log(LOG_WARNING,
1060 "%s: %d silo overflow%s, %d ibuf flood%s\n",
1061 sc->sc_dev.dv_xname,
1062 overflows, overflows == 1 ? "" : "s",
1063 floods, floods == 1 ? "" : "s");
1064 }
1065
1066 static
1067 void ser_shutdown(sc)
1068 struct ser_softc *sc;
1069 {
1070 int s;
1071 struct tty *tp = sc->sc_tty;
1072
1073
1074 s = splserial();
1075
1076 /* If we were asserting flow control, then deassert it. */
1077 sc->sc_rx_blocked = 1;
1078 ser_hwiflow(sc, 1);
1079
1080 /* Clear any break condition set with TIOCSBRK. */
1081 ser_break(sc, 0);
1082
1083 /*
1084 * Hang up if necessary. Wait a bit, so the other side has time to
1085 * notice even if we immediately open the port again.
1086 */
1087 if (ISSET(tp->t_cflag, HUPCL)) {
1088 ser_modem(sc, 0);
1089 (void) tsleep(sc, TTIPRI, ttclos, hz);
1090 }
1091
1092 /* Turn off interrupts. */
1093 CLR(sc->sc_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
1094 CLR(sc->sc_imrb, IB_SCTS|IB_SDCD);
1095 single_inst_bclr_b(MFP->mf_imrb, IB_SCTS|IB_SDCD);
1096 single_inst_bclr_b(MFP->mf_imra, IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
1097 splx(s);
1098 }
1099
1100 static void
1101 serrxint(sc, tp)
1102 struct ser_softc *sc;
1103 struct tty *tp;
1104 {
1105 u_int get, cc, scc;
1106 int code;
1107 u_char rsr;
1108 int s;
1109 static int lsrmap[8] = {
1110 0, TTY_PE,
1111 TTY_FE, TTY_PE|TTY_FE,
1112 TTY_FE, TTY_PE|TTY_FE,
1113 TTY_FE, TTY_PE|TTY_FE
1114 };
1115
1116 get = sc->sc_rbget;
1117 scc = cc = RXBUFSIZE - sc->sc_rbavail;
1118
1119 if (cc == RXBUFSIZE) {
1120 sc->sc_floods++;
1121 if (sc->sc_errors++ == 0)
1122 callout_reset(&sc->sc_diag_ch, 60 * hz, serdiag, sc);
1123 }
1124
1125 while (cc--) {
1126 rsr = sc->sc_lbuf[get];
1127 if (ISSET(rsr, RSR_BREAK)) {
1128 #ifdef DDB
1129 if (ISSET(sc->sc_hwflags, SER_HW_CONSOLE))
1130 Debugger();
1131 #endif
1132 }
1133 else if (ISSET(rsr, RSR_OERR)) {
1134 sc->sc_overflows++;
1135 if (sc->sc_errors++ == 0)
1136 callout_reset(&sc->sc_diag_ch, 60 * hz,
1137 serdiag, sc);
1138 }
1139 code = sc->sc_rbuf[get] |
1140 lsrmap[(rsr & (RSR_BREAK|RSR_FERR|RSR_PERR)) >> 3];
1141 (*tp->t_linesw->l_rint)(code, tp);
1142 get = (get + 1) & RXBUFMASK;
1143 }
1144
1145 sc->sc_rbget = get;
1146 s = splserial();
1147 sc->sc_rbavail += scc;
1148 /*
1149 * Buffers should be ok again, release possible block, but only if the
1150 * tty layer isn't blocking too.
1151 */
1152 if (sc->sc_rx_blocked && !ISSET(tp->t_state, TS_TBLOCK)) {
1153 sc->sc_rx_blocked = 0;
1154 ser_hwiflow(sc, 0);
1155 }
1156 splx(s);
1157 }
1158
1159 static void
1160 sertxint(sc, tp)
1161 struct ser_softc *sc;
1162 struct tty *tp;
1163 {
1164
1165 CLR(tp->t_state, TS_BUSY);
1166 if (ISSET(tp->t_state, TS_FLUSH))
1167 CLR(tp->t_state, TS_FLUSH);
1168 else
1169 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1170 (*tp->t_linesw->l_start)(tp);
1171 }
1172
1173 static void
1174 sermsrint(sc, tp)
1175 struct ser_softc *sc;
1176 struct tty *tp;
1177 {
1178 u_char msr, delta;
1179 int s;
1180
1181 s = splserial();
1182 msr = sc->sc_msr;
1183 delta = sc->sc_msr_delta;
1184 sc->sc_msr_delta = 0;
1185 splx(s);
1186
1187 if (ISSET(delta, sc->sc_msr_dcd)) {
1188 /*
1189 * Inform the tty layer that carrier detect changed.
1190 */
1191 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MCR_DCD));
1192 }
1193
1194 if (ISSET(delta, sc->sc_msr_cts)) {
1195 /* Block or unblock output according to flow control. */
1196 if (ISSET(msr, sc->sc_msr_cts)) {
1197 sc->sc_tx_stopped = 0;
1198 (*tp->t_linesw->l_start)(tp);
1199 } else {
1200 sc->sc_tx_stopped = 1;
1201 serstop(tp, 0);
1202 }
1203 }
1204
1205 #ifdef SER_DEBUG
1206 serstatus(sc, "sermsrint");
1207 #endif
1208 }
1209
1210 void
1211 sersoft(arg)
1212 void *arg;
1213 {
1214 struct ser_softc *sc = arg;
1215 struct tty *tp;
1216
1217 ser_softintr_scheduled = 0;
1218
1219 tp = sc->sc_tty;
1220 if (tp == NULL)
1221 return;
1222
1223 if (!ISSET(tp->t_state, TS_ISOPEN) && (tp->t_wopen == 0))
1224 return;
1225
1226 if (sc->sc_rx_ready) {
1227 sc->sc_rx_ready = 0;
1228 serrxint(sc, tp);
1229 }
1230
1231 if (sc->sc_st_check) {
1232 sc->sc_st_check = 0;
1233 sermsrint(sc, tp);
1234 }
1235
1236 if (sc->sc_tx_done) {
1237 sc->sc_tx_done = 0;
1238 sertxint(sc, tp);
1239 }
1240 }
1241
1242 int
1243 sermintr(arg)
1244 void *arg;
1245 {
1246 struct ser_softc *sc = arg;
1247 u_char msr, delta;
1248
1249 msr = ~MFP->mf_gpip;
1250 delta = msr ^ sc->sc_msr;
1251 sc->sc_msr = sc->sc_msr & ~(MCR_CTS|MCR_DCD|MCR_RI);
1252 sc->sc_msr |= msr & (MCR_CTS|MCR_DCD|MCR_RI);
1253
1254 if (ISSET(delta, sc->sc_msr_mask)) {
1255 sc->sc_msr_delta |= delta;
1256
1257 /*
1258 * Stop output immediately if we lose the output
1259 * flow control signal or carrier detect.
1260 */
1261 if (ISSET(~msr, sc->sc_msr_mask)) {
1262 sc->sc_tbc = 0;
1263 sc->sc_heldtbc = 0;
1264 #ifdef SER_DEBUG
1265 serstatus(sc, "sermintr ");
1266 #endif
1267 }
1268
1269 sc->sc_st_check = 1;
1270 }
1271 if (!ser_softintr_scheduled)
1272 add_sicallback((si_farg)sersoft, sc, 0);
1273 return 1;
1274 }
1275
1276 int
1277 sertrintr(arg)
1278 void *arg;
1279 {
1280 struct ser_softc *sc = arg;
1281 u_int put, cc;
1282 u_char rsr, tsr;
1283
1284 put = sc->sc_rbput;
1285 cc = sc->sc_rbavail;
1286
1287 rsr = MFP->mf_rsr;
1288 if (ISSET(rsr, RSR_BFULL|RSR_BREAK)) {
1289 for (; ISSET(rsr, RSR_BFULL|RSR_BREAK) && cc > 0; cc--) {
1290 sc->sc_rbuf[put] = MFP->mf_udr;
1291 sc->sc_lbuf[put] = rsr;
1292 put = (put + 1) & RXBUFMASK;
1293 if ((rsr & RSR_BREAK) && (MFP->mf_rsr & RSR_BREAK))
1294 rsr = 0;
1295 else rsr = MFP->mf_rsr;
1296 }
1297 /*
1298 * Current string of incoming characters ended because
1299 * no more data was available. Schedule a receive event
1300 * if any data was received. Drop any characters that
1301 * we couldn't handle.
1302 */
1303 sc->sc_rbput = put;
1304 sc->sc_rbavail = cc;
1305 sc->sc_rx_ready = 1;
1306 /*
1307 * See if we are in danger of overflowing a buffer. If
1308 * so, use hardware flow control to ease the pressure.
1309 */
1310 if (sc->sc_rx_blocked == 0 &&
1311 cc < sc->sc_r_hiwat) {
1312 sc->sc_rx_blocked = 1;
1313 ser_hwiflow(sc, 1);
1314 }
1315 /*
1316 * If we're out of space, throw away any further input.
1317 */
1318 if (!cc) {
1319 while (ISSET(rsr, RSR_BFULL|RSR_BREAK)) {
1320 rsr = MFP->mf_udr;
1321 rsr = MFP->mf_rsr;
1322 }
1323 }
1324 }
1325
1326 /*
1327 * Done handling any receive interrupts. See if data can be
1328 * transmitted as well. Schedule tx done event if no data left
1329 * and tty was marked busy.
1330 */
1331 tsr = MFP->mf_tsr;
1332 if (ISSET(tsr, TSR_BE)) {
1333 /*
1334 * If we've delayed a parameter change, do it now, and restart
1335 * output.
1336 */
1337 if (sc->sc_heldchange) {
1338 ser_loadchannelregs(sc);
1339 sc->sc_heldchange = 0;
1340 sc->sc_tbc = sc->sc_heldtbc;
1341 sc->sc_heldtbc = 0;
1342 }
1343 /* Output the next character, if any. */
1344 if (sc->sc_tbc > 0) {
1345 MFP->mf_udr = *sc->sc_tba;
1346 sc->sc_tbc --;
1347 sc->sc_tba ++;
1348 } else if (sc->sc_tx_busy) {
1349 sc->sc_tx_busy = 0;
1350 sc->sc_tx_done = 1;
1351 }
1352 }
1353
1354 if (!ser_softintr_scheduled)
1355 add_sicallback((si_farg)sersoft, sc, 0);
1356 return 1;
1357 }
1358
1359 static int
1360 serspeed(speed)
1361 long speed;
1362 {
1363 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
1364
1365 int div, x, err;
1366
1367 if (speed <= 0)
1368 return (-1);
1369
1370 for (div = 4; div <= 64; div *= 4) {
1371 x = divrnd((SER_FREQ / div), speed);
1372
1373 /*
1374 * The value must fit in the timer-d dataregister. If
1375 * not, try another delay-mode.
1376 */
1377 if ((x/2) > 255)
1378 continue;
1379
1380 /*
1381 * Baudrate to high for the interface or cannot be made
1382 * within tolerance.
1383 */
1384 if (x <= 0)
1385 return (-1);
1386
1387 err = divrnd((SER_FREQ / div) * 1000, speed * x) - 1000;
1388 if (err < 0)
1389 err = -err;
1390 if (err > SER_TOLERANCE)
1391 continue;
1392
1393 /*
1394 * Translate 'div' to delay-code
1395 */
1396 if (div == 4)
1397 div = 1;
1398 else if (div == 16)
1399 div = 3;
1400 else if (div == 64)
1401 div = 5;
1402
1403 return ((x/2) | (div << 8));
1404 }
1405 return (-1);
1406
1407 #undef divrnd
1408 }
1409
1410 /*
1411 * Following are all routines needed for SER to act as console
1412 */
1413 #include <dev/cons.h>
1414
1415 void
1416 sercnprobe(cp)
1417 struct consdev *cp;
1418 {
1419 /*
1420 * Activate serial console when DCD present...
1421 */
1422 if (MFP->mf_gpip & MCR_DCD) {
1423 cp->cn_pri = CN_DEAD;
1424 return;
1425 }
1426 for (sermajor = 0; sermajor < nchrdev; sermajor++)
1427 if (cdevsw[sermajor].d_open == seropen)
1428 break;
1429
1430 /* initialize required fields */
1431 cp->cn_dev = makedev(sermajor, 0); /* XXX: LWP What unit? */
1432 #if SERCONSOLE > 0
1433 cp->cn_pri = CN_REMOTE; /* Force a serial port console */
1434 #else
1435 cp->cn_pri = CN_NORMAL;
1436 #endif /* SERCONSOLE > 0 */
1437 }
1438
1439 void
1440 sercninit(cp)
1441 struct consdev *cp;
1442 {
1443 serinitcons(CONSBAUD);
1444 }
1445
1446 /*
1447 * Initialize UART to known state.
1448 */
1449 void
1450 serinit(baud)
1451 int baud;
1452 {
1453 int ospeed = serspeed(baud);
1454
1455 MFP->mf_ucr = UCR_CLKDIV|UCR_8BITS|UCR_STOPB1;
1456 MFP->mf_rsr = RSR_ENAB;
1457 MFP->mf_tsr = TSR_ENAB;
1458
1459 single_inst_bclr_b(MFP->mf_tcdcr, 0x07);
1460 MFP->mf_tddr = ospeed;
1461 single_inst_bset_b(MFP->mf_tcdcr, (ospeed >> 8) & 0x0f);
1462 }
1463
1464 /*
1465 * Set UART for console use. Do normal init, then enable interrupts.
1466 */
1467 void
1468 serinitcons(baud)
1469 int baud;
1470 {
1471 serinit(baud);
1472
1473 /* Set rts/dtr */
1474 ym2149_rts(0);
1475 ym2149_dtr(0);
1476
1477 single_inst_bset_b(MFP->mf_imra, (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR));
1478 }
1479
1480 int
1481 sercngetc(dev)
1482 dev_t dev;
1483 {
1484 u_char stat, c;
1485 int s;
1486
1487 s = splserial();
1488 while (!ISSET(stat = MFP->mf_rsr, RSR_BFULL)) {
1489 if (!ISSET(stat, RSR_ENAB)) /* XXX */
1490 MFP->mf_rsr |= RSR_ENAB;
1491 if (stat & (RSR_FERR|RSR_PERR|RSR_OERR))
1492 c = MFP->mf_udr;
1493 }
1494 c = MFP->mf_udr;
1495 splx(s);
1496 return c;
1497 }
1498
1499 u_int s_imra;
1500 u_int s_stat1, s_stat2, s_stat3;
1501 void
1502 sercnputc(dev, c)
1503 dev_t dev;
1504 int c;
1505 {
1506 int timo;
1507 u_char stat, imra;
1508
1509 /* Mask serial interrupts */
1510 imra = MFP->mf_imra & (IA_RRDY|IA_RERR|IA_TRDY|IA_TERR);
1511 single_inst_bclr_b(MFP->mf_imra, imra);
1512 s_imra = imra;
1513
1514 /* wait for any pending transmission to finish */
1515 timo = 50000;
1516 s_stat1 = MFP->mf_tsr;
1517 while (!ISSET(stat = MFP->mf_tsr, TSR_BE) && --timo)
1518 ;
1519 MFP->mf_udr = c;
1520 /* wait for this transmission to complete */
1521 timo = 1500000;
1522 s_stat2 = MFP->mf_tsr;
1523 while (!ISSET(stat = MFP->mf_tsr, TSR_BE) && --timo)
1524 ;
1525
1526 s_stat3 = MFP->mf_tsr;
1527 /* Clear pending serial interrupts and re-enable */
1528 MFP->mf_ipra = (u_int8_t)~imra;
1529 single_inst_bset_b(MFP->mf_imra, imra);
1530 }
1531
1532 void
1533 sercnpollc(dev, on)
1534 dev_t dev;
1535 int on;
1536 {
1537
1538 }
1539