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