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