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