com.c revision 1.33 1 /*-
2 * Copyright (c) 1993, 1994 Charles Hannum.
3 * Copyright (c) 1991 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: @(#)com.c 7.5 (Berkeley) 5/16/91
35 * $Id: com.c,v 1.33 1994/08/07 11:27:51 mycroft Exp $
36 */
37
38 /*
39 * COM driver, based on HP dca driver
40 * uses National Semiconductor NS16450/NS16550AF UART
41 */
42 #include "com.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/ioctl.h>
47 #include <sys/select.h>
48 #include <sys/tty.h>
49 #include <sys/proc.h>
50 #include <sys/user.h>
51 #include <sys/conf.h>
52 #include <sys/file.h>
53 #include <sys/uio.h>
54 #include <sys/kernel.h>
55 #include <sys/syslog.h>
56 #include <sys/types.h>
57 #include <sys/device.h>
58
59 #include <machine/cpu.h>
60 #include <machine/pio.h>
61
62 #include <i386/isa/isavar.h>
63 #include <i386/isa/comreg.h>
64 #include <i386/isa/ic/ns16550.h>
65
66 struct com_softc {
67 struct device sc_dev;
68 struct intrhand sc_ih;
69
70 int sc_overflows;
71 u_short sc_iobase;
72 u_char sc_hwflags;
73 #define COM_HW_NOIEN 0x01
74 #define COM_HW_FIFO 0x02
75 #define COM_HW_CONSOLE 0x40
76 u_char sc_swflags;
77 #define COM_SW_SOFTCAR 0x01
78 #define COM_SW_CLOCAL 0x02
79 #define COM_SW_CRTSCTS 0x04
80 #define COM_SW_MDMBUF 0x08
81 u_char sc_msr, sc_mcr;
82 };
83 /* XXXX should be in com_softc, but not ready for that yet */
84 struct tty *com_tty[NCOM];
85
86 int comprobe();
87 void comattach();
88 int comopen __P((dev_t, int, int, struct proc *));
89 int comclose __P((dev_t, int, int, struct proc *));
90 void comdiag __P((void *));
91 int comintr __P((struct com_softc *));
92 int comparam __P((struct tty *, struct termios *));
93 void comstart __P((struct tty *));
94
95 struct cfdriver comcd = {
96 NULL, "com", comprobe, comattach, DV_TTY, sizeof(struct com_softc)
97 };
98
99 int comdefaultrate = TTYDEF_SPEED;
100 #ifdef COMCONSOLE
101 int comconsole = COMCONSOLE;
102 #else
103 int comconsole = -1;
104 #endif
105 int comconsinit;
106 int commajor;
107
108 #ifdef KGDB
109 #include <machine/remote-sl.h>
110 extern int kgdb_dev;
111 extern int kgdb_rate;
112 extern int kgdb_debug_init;
113 #endif
114
115 #define COMUNIT(x) (minor(x))
116
117 #define bis(c, b) do { const register u_short com_ad = (c); \
118 outb(com_ad, inb(com_ad) | (b)); } while(0)
119 #define bic(c, b) do { const register u_short com_ad = (c); \
120 outb(com_ad, inb(com_ad) & ~(b)); } while(0)
121
122 int
123 comspeed(speed)
124 long speed;
125 {
126 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
127
128 int x, err;
129
130 if (speed == 0)
131 return 0;
132 if (speed < 0)
133 return -1;
134 x = divrnd((COM_FREQ / 16), speed);
135 if (x <= 0)
136 return -1;
137 err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
138 if (err < 0)
139 err = -err;
140 if (err > COM_TOLERANCE)
141 return -1;
142 return x;
143
144 #undef divrnd(n, q)
145 }
146
147 int
148 comprobe1(iobase)
149 u_short iobase;
150 {
151
152 /* force access to id reg */
153 outb(iobase + com_cfcr, 0);
154 outb(iobase + com_iir, 0);
155 if (inb(iobase + com_iir) & 0x38)
156 return 0;
157
158 return 1;
159 }
160
161 int
162 comprobe(parent, self, aux)
163 struct device *parent, *self;
164 void *aux;
165 {
166 struct com_softc *sc = (void *)self;
167 struct isa_attach_args *ia = aux;
168 u_short iobase = ia->ia_iobase;
169
170 if (!comprobe1(iobase))
171 return 0;
172
173 ia->ia_iosize = COM_NPORTS;
174 ia->ia_msize = 0;
175 return 1;
176 }
177
178 void
179 comattach(parent, self, aux)
180 struct device *parent, *self;
181 void *aux;
182 {
183 struct com_softc *sc = (void *)self;
184 struct isa_attach_args *ia = aux;
185 struct cfdata *cf = sc->sc_dev.dv_cfdata;
186 u_short iobase = ia->ia_iobase;
187 struct tty *tp;
188
189 sc->sc_iobase = iobase;
190 sc->sc_hwflags = cf->cf_flags & COM_HW_NOIEN;
191 sc->sc_swflags = 0;
192
193 if (sc->sc_dev.dv_unit == comconsole)
194 delay(1000);
195
196 /* look for a NS 16550AF UART with FIFOs */
197 outb(iobase + com_fifo,
198 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
199 delay(100);
200 if ((inb(iobase + com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK)
201 if ((inb(iobase + com_fifo) & FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
202 sc->sc_hwflags |= COM_HW_FIFO;
203 printf(": ns16550a, working fifo\n");
204 } else
205 printf(": ns82550 or ns16550, broken fifo\n");
206 else
207 printf(": ns82450 or ns16450, no fifo\n");
208 outb(iobase + com_fifo, 0);
209
210 /* disable interrupts */
211 outb(iobase + com_ier, 0);
212 outb(iobase + com_mcr, 0);
213
214 if (!parent) { /* XXX no master */
215 sc->sc_ih.ih_fun = comintr;
216 sc->sc_ih.ih_arg = sc;
217 sc->sc_ih.ih_level = IPL_TTY;
218 intr_establish(ia->ia_irq, &sc->sc_ih);
219 }
220
221 #ifdef KGDB
222 if (kgdb_dev == makedev(commajor, unit)) {
223 if (comconsole == unit)
224 kgdb_dev = -1; /* can't debug over console port */
225 else {
226 (void) cominit(unit, kgdb_rate);
227 if (kgdb_debug_init) {
228 /*
229 * Print prefix of device name,
230 * let kgdb_connect print the rest.
231 */
232 printf("%s: ", sc->sc_dev.dv_xname);
233 kgdb_connect(1);
234 } else
235 printf("%s: kgdb enabled\n",
236 sc->sc_dev.dv_xname);
237 }
238 }
239 #endif
240
241 if (sc->sc_dev.dv_unit == comconsole) {
242 /*
243 * Need to reset baud rate, etc. of next print so reset
244 * comconsinit. Also make sure console is always "hardwired".
245 */
246 comconsinit = 0;
247 sc->sc_hwflags |= COM_HW_CONSOLE;
248 sc->sc_swflags |= COM_SW_SOFTCAR;
249 }
250 }
251
252 int
253 comopen(dev, flag, mode, p)
254 dev_t dev;
255 int flag, mode;
256 struct proc *p;
257 {
258 int unit = COMUNIT(dev);
259 struct com_softc *sc;
260 u_short iobase;
261 struct tty *tp;
262 int s;
263 int error = 0;
264
265 if (unit >= comcd.cd_ndevs)
266 return ENXIO;
267 sc = comcd.cd_devs[unit];
268 if (!sc)
269 return ENXIO;
270
271 s = spltty();
272
273 if (!com_tty[unit])
274 tp = com_tty[unit] = ttymalloc();
275 else
276 tp = com_tty[unit];
277
278 tp->t_oproc = comstart;
279 tp->t_param = comparam;
280 tp->t_dev = dev;
281 if ((tp->t_state & TS_ISOPEN) == 0) {
282 tp->t_state |= TS_WOPEN;
283 ttychars(tp);
284 tp->t_iflag = TTYDEF_IFLAG;
285 tp->t_oflag = TTYDEF_OFLAG;
286 tp->t_cflag = TTYDEF_CFLAG;
287 if (sc->sc_swflags & COM_SW_CLOCAL)
288 tp->t_cflag |= CLOCAL;
289 if (sc->sc_swflags & COM_SW_CRTSCTS)
290 tp->t_cflag |= CRTSCTS;
291 if (sc->sc_swflags & COM_SW_MDMBUF)
292 tp->t_cflag |= MDMBUF;
293 tp->t_lflag = TTYDEF_LFLAG;
294 tp->t_ispeed = tp->t_ospeed = comdefaultrate;
295 comparam(tp, &tp->t_termios);
296 ttsetwater(tp);
297
298 iobase = sc->sc_iobase;
299 /* flush any pending I/O */
300 if (sc->sc_hwflags & COM_HW_FIFO)
301 outb(iobase + com_fifo,
302 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
303 FIFO_TRIGGER_8);
304 (void) inb(iobase + com_lsr);
305 (void) inb(iobase + com_data);
306 /* you turn me on, baby */
307 sc->sc_mcr = MCR_DTR | MCR_RTS;
308 if (!(sc->sc_hwflags & COM_HW_NOIEN))
309 sc->sc_mcr |= MCR_IENABLE;
310 outb(iobase + com_mcr, sc->sc_mcr);
311 outb(iobase + com_ier,
312 IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
313
314 sc->sc_msr = inb(iobase + com_msr);
315 if (sc->sc_swflags & COM_SW_SOFTCAR || sc->sc_msr & MSR_DCD ||
316 tp->t_lflag&MDMBUF)
317 tp->t_state |= TS_CARR_ON;
318 else
319 tp->t_state &= ~TS_CARR_ON;
320 } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) {
321 splx(s);
322 return EBUSY;
323 }
324
325 /* wait for carrier if necessary */
326 if ((flag & O_NONBLOCK) == 0)
327 while ((tp->t_cflag & CLOCAL) == 0 &&
328 (tp->t_state & TS_CARR_ON) == 0) {
329 tp->t_state |= TS_WOPEN;
330 error = ttysleep(tp, (caddr_t)&tp->t_rawq,
331 TTIPRI | PCATCH, ttopen, 0);
332 if (error) {
333 /* XXX should turn off chip if we're the
334 only waiter */
335 splx(s);
336 return error;
337 }
338 }
339 splx(s);
340
341 return (*linesw[tp->t_line].l_open)(dev, tp);
342 }
343
344 int
345 comclose(dev, flag, mode, p)
346 dev_t dev;
347 int flag, mode;
348 struct proc *p;
349 {
350 int unit = COMUNIT(dev);
351 struct com_softc *sc = comcd.cd_devs[unit];
352 u_short iobase = sc->sc_iobase;
353 struct tty *tp = com_tty[unit];
354
355 (*linesw[tp->t_line].l_close)(tp, flag);
356 #ifdef KGDB
357 /* do not disable interrupts if debugging */
358 if (kgdb_dev != makedev(commajor, unit))
359 #endif
360 {
361 bic(iobase + com_cfcr, CFCR_SBREAK);
362 outb(iobase + com_ier, 0);
363 if (tp->t_cflag & HUPCL &&
364 (sc->sc_swflags & COM_SW_SOFTCAR) == 0)
365 /* XXX perhaps only clear DTR */
366 outb(iobase + com_mcr, 0);
367 }
368 ttyclose(tp);
369 #ifdef notyet /* XXXX */
370 if (unit != comconsole) {
371 ttyfree(tp);
372 com_tty[unit] = (struct tty *)NULL;
373 }
374 #endif
375 return 0;
376 }
377
378 int
379 comread(dev, uio, flag)
380 dev_t dev;
381 struct uio *uio;
382 int flag;
383 {
384 struct tty *tp = com_tty[COMUNIT(dev)];
385
386 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
387 }
388
389 int
390 comwrite(dev, uio, flag)
391 dev_t dev;
392 struct uio *uio;
393 int flag;
394 {
395 struct tty *tp = com_tty[COMUNIT(dev)];
396
397 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
398 }
399
400 static u_char
401 tiocm_xxx2mcr(data)
402 int data;
403 {
404 u_char m = 0;
405
406 if (data & TIOCM_DTR)
407 m |= MCR_DTR;
408 if (data & TIOCM_RTS)
409 m |= MCR_RTS;
410 return m;
411 }
412
413 int
414 comioctl(dev, cmd, data, flag, p)
415 dev_t dev;
416 int cmd;
417 caddr_t data;
418 int flag;
419 struct proc *p;
420 {
421 int unit = COMUNIT(dev);
422 struct com_softc *sc = comcd.cd_devs[unit];
423 u_short iobase = sc->sc_iobase;
424 struct tty *tp = com_tty[unit];
425 int error;
426
427 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
428 if (error >= 0)
429 return error;
430 error = ttioctl(tp, cmd, data, flag, p);
431 if (error >= 0)
432 return error;
433
434 switch (cmd) {
435 case TIOCSBRK:
436 bis(iobase + com_cfcr, CFCR_SBREAK);
437 break;
438 case TIOCCBRK:
439 bic(iobase + com_cfcr, CFCR_SBREAK);
440 break;
441 case TIOCSDTR:
442 outb(iobase + com_mcr, sc->sc_mcr |= (MCR_DTR | MCR_RTS));
443 break;
444 case TIOCCDTR:
445 outb(iobase + com_mcr, sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
446 break;
447 case TIOCMSET:
448 sc->sc_mcr &= ~(MCR_DTR | MCR_RTS);
449 case TIOCMBIS:
450 outb(iobase + com_mcr,
451 sc->sc_mcr |= tiocm_xxx2mcr(*(int *)data));
452 break;
453 case TIOCMBIC:
454 outb(iobase + com_mcr,
455 sc->sc_mcr &= ~tiocm_xxx2mcr(*(int *)data));
456 break;
457 case TIOCMGET: {
458 u_char m;
459 int bits = 0;
460
461 m = sc->sc_mcr;
462 if (m & MCR_DTR)
463 bits |= TIOCM_DTR;
464 if (m & MCR_RTS)
465 bits |= TIOCM_RTS;
466 m = sc->sc_msr;
467 if (m & MSR_DCD)
468 bits |= TIOCM_CD;
469 if (m & MSR_CTS)
470 bits |= TIOCM_CTS;
471 if (m & MSR_DSR)
472 bits |= TIOCM_DSR;
473 if (m & (MSR_RI | MSR_TERI))
474 bits |= TIOCM_RI;
475 if (inb(iobase + com_ier))
476 bits |= TIOCM_LE;
477 *(int *)data = bits;
478 break;
479 }
480 case TIOCGFLAGS: {
481 int bits = 0;
482
483 if (sc->sc_swflags & COM_SW_SOFTCAR)
484 bits |= TIOCFLAG_SOFTCAR;
485 if (sc->sc_swflags & COM_SW_CLOCAL)
486 bits |= TIOCFLAG_CLOCAL;
487 if (sc->sc_swflags & COM_SW_CRTSCTS)
488 bits |= TIOCFLAG_CRTSCTS;
489 if (sc->sc_swflags & COM_SW_MDMBUF)
490 bits |= TIOCFLAG_MDMBUF;
491
492 *(int *)data = bits;
493 break;
494 }
495 case TIOCSFLAGS: {
496 int userbits, driverbits = 0;
497
498 error = suser(p->p_ucred, &p->p_acflag);
499 if (error != 0)
500 return(EPERM);
501
502 userbits = *(int *)data;
503 if ((userbits & TIOCFLAG_SOFTCAR) ||
504 (sc->sc_hwflags & COM_HW_CONSOLE))
505 driverbits |= COM_SW_SOFTCAR;
506 if (userbits & TIOCFLAG_CLOCAL)
507 driverbits |= COM_SW_CLOCAL;
508 if (userbits & TIOCFLAG_CRTSCTS)
509 driverbits |= COM_SW_CRTSCTS;
510 if (userbits & TIOCFLAG_MDMBUF)
511 driverbits |= COM_SW_MDMBUF;
512
513 sc->sc_swflags = driverbits;
514 break;
515 }
516 default:
517 return ENOTTY;
518 }
519
520 return 0;
521 }
522
523 int
524 comparam(tp, t)
525 struct tty *tp;
526 struct termios *t;
527 {
528 struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
529 u_short iobase = sc->sc_iobase;
530 int ospeed = comspeed(t->c_ospeed);
531 u_char cfcr;
532 int s;
533
534 /* check requested parameters */
535 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
536 return EINVAL;
537
538 switch (t->c_cflag & CSIZE) {
539 case CS5:
540 cfcr = CFCR_5BITS;
541 break;
542 case CS6:
543 cfcr = CFCR_6BITS;
544 break;
545 case CS7:
546 cfcr = CFCR_7BITS;
547 break;
548 case CS8:
549 cfcr = CFCR_8BITS;
550 break;
551 }
552 if (t->c_cflag & PARENB) {
553 cfcr |= CFCR_PENAB;
554 if ((t->c_cflag & PARODD) == 0)
555 cfcr |= CFCR_PEVEN;
556 }
557 if (t->c_cflag & CSTOPB)
558 cfcr |= CFCR_STOPB;
559
560 s = spltty();
561
562 if (ospeed == 0)
563 outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_DTR);
564 else
565 outb(iobase + com_mcr, sc->sc_mcr |= MCR_DTR);
566
567 outb(iobase + com_cfcr, cfcr | CFCR_DLAB);
568 outb(iobase + com_dlbl, ospeed);
569 outb(iobase + com_dlbh, ospeed>>8);
570 outb(iobase + com_cfcr, cfcr);
571
572 /* When not using CRTSCTS, RTS follows DTR. */
573 if ((t->c_cflag & CRTSCTS) == 0) {
574 if (sc->sc_mcr & MCR_DTR) {
575 if ((sc->sc_mcr & MCR_RTS) == 0)
576 outb(iobase + com_mcr, sc->sc_mcr |= MCR_RTS);
577 } else {
578 if (sc->sc_mcr & MCR_RTS)
579 outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_RTS);
580 }
581 }
582
583 /*
584 * If CTS is off and CRTSCTS is changed, we must toggle TS_TTSTOP.
585 * XXX should be done at tty layer.
586 */
587 if ((sc->sc_msr & MSR_CTS) == 0 &&
588 (tp->t_cflag & CRTSCTS) != (t->c_cflag & CRTSCTS)) {
589 if ((t->c_cflag & CRTSCTS) == 0) {
590 tp->t_state &= ~TS_TTSTOP;
591 (*linesw[tp->t_line].l_start)(tp);
592 } else
593 tp->t_state |= TS_TTSTOP;
594 }
595
596 /*
597 * If DCD is off and MDMBUF is changed, we must toggle TS_TTSTOP.
598 * XXX should be done at tty layer.
599 */
600 if ((sc->sc_swflags & COM_SW_SOFTCAR) == 0 &&
601 (sc->sc_msr & MSR_DCD) == 0 &&
602 (tp->t_cflag & MDMBUF) != (t->c_cflag & MDMBUF)) {
603 if ((t->c_cflag & MDMBUF) == 0) {
604 tp->t_state &= ~TS_TTSTOP;
605 (*linesw[tp->t_line].l_start)(tp);
606 } else
607 tp->t_state |= TS_TTSTOP;
608 }
609
610 /* and copy to tty */
611 tp->t_ispeed = t->c_ispeed;
612 tp->t_ospeed = t->c_ospeed;
613 tp->t_cflag = t->c_cflag;
614
615 splx(s);
616 return 0;
617 }
618
619 void
620 comstart(tp)
621 struct tty *tp;
622 {
623 struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
624 u_short iobase = sc->sc_iobase;
625 int s;
626
627 s = spltty();
628 if (tp->t_state & (TS_TTSTOP | TS_BUSY))
629 goto out;
630 #if 0 /* XXXX I think this is handled adequately by commint() and comparam(). */
631 if (tp->t_cflag & CRTSCTS && (sc->sc_mcr & MSR_CTS) == 0)
632 goto out;
633 #endif
634 if (tp->t_outq.c_cc <= tp->t_lowat) {
635 if (tp->t_state & TS_ASLEEP) {
636 tp->t_state &= ~TS_ASLEEP;
637 wakeup((caddr_t)&tp->t_outq);
638 }
639 selwakeup(&tp->t_wsel);
640 }
641 if (tp->t_outq.c_cc == 0)
642 goto out;
643 tp->t_state |= TS_BUSY;
644 if ((inb(iobase + com_lsr) & LSR_TXRDY) == 0)
645 goto out;
646 if (sc->sc_hwflags & COM_HW_FIFO) {
647 u_char buffer[16], *cp = buffer;
648 int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
649 do {
650 outb(iobase + com_data, *cp++);
651 } while (--n);
652 } else
653 outb(iobase + com_data, getc(&tp->t_outq));
654 out:
655 splx(s);
656 }
657
658 /*
659 * Stop output on a line.
660 */
661 void
662 comstop(tp, flag)
663 struct tty *tp;
664 {
665 int s;
666
667 s = spltty();
668 if (tp->t_state & TS_BUSY)
669 if ((tp->t_state & TS_TTSTOP) == 0)
670 tp->t_state |= TS_FLUSH;
671 splx(s);
672 }
673
674 static inline void
675 comeint(sc, stat)
676 struct com_softc *sc;
677 int stat;
678 {
679 u_short iobase = sc->sc_iobase;
680 int unit = sc->sc_dev.dv_unit;
681 struct tty *tp = com_tty[unit];
682 int c;
683
684 c = inb(iobase + com_data);
685 if ((tp->t_state & TS_ISOPEN) == 0) {
686 #ifdef KGDB
687 /* we don't care about parity errors */
688 if (((stat & (LSR_BI | LSR_FE | LSR_PE)) == LSR_PE) &&
689 kgdb_dev == makedev(commajor, unit) && c == FRAME_END)
690 kgdb_connect(0); /* trap into kgdb */
691 #endif
692 return;
693 }
694 if (stat & (LSR_BI | LSR_FE))
695 c |= TTY_FE;
696 else if (stat & LSR_PE)
697 c |= TTY_PE;
698 if (stat & LSR_OE) {
699 if (sc->sc_overflows++ == 0)
700 timeout(comdiag, sc, 60 * hz);
701 }
702 /* XXXX put in FIFO and process later */
703 (*linesw[tp->t_line].l_rint)(c, tp);
704 }
705
706 static inline void
707 commint(sc)
708 struct com_softc *sc;
709 {
710 u_short iobase = sc->sc_iobase;
711 struct tty *tp = com_tty[sc->sc_dev.dv_unit];
712 u_char msr, delta;
713
714 msr = inb(iobase + com_msr);
715 delta = msr ^ sc->sc_msr;
716 sc->sc_msr = msr;
717
718 if (delta & MSR_DCD && (sc->sc_swflags & COM_SW_SOFTCAR) == 0) {
719 if (msr & MSR_DCD)
720 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
721 else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
722 outb(iobase + com_mcr,
723 sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
724 }
725 if (delta & MSR_CTS && tp->t_cflag & CRTSCTS) {
726 /* the line is up and we want to do rts/cts flow control */
727 if (msr & MSR_CTS) {
728 tp->t_state &= ~TS_TTSTOP;
729 (*linesw[tp->t_line].l_start)(tp);
730 } else
731 tp->t_state |= TS_TTSTOP;
732 }
733 }
734
735 void
736 comdiag(arg)
737 void *arg;
738 {
739 struct com_softc *sc = arg;
740 int overflows;
741 int s;
742
743 s = spltty();
744 overflows = sc->sc_overflows;
745 sc->sc_overflows = 0;
746 splx(s);
747
748 if (overflows)
749 log(LOG_WARNING, "%s: %d silo overflow%s\n",
750 sc->sc_dev.dv_xname, overflows, overflows == 1 ? "" : "s");
751 }
752
753 int
754 comintr(sc)
755 struct com_softc *sc;
756 {
757 u_short iobase = sc->sc_iobase;
758 struct tty *tp;
759 u_char code;
760
761 code = inb(iobase + com_iir) & IIR_IMASK;
762 if (code & IIR_NOPEND)
763 return 0;
764
765 for (;;) {
766 if (code & IIR_RXRDY) {
767 tp = com_tty[sc->sc_dev.dv_unit];
768 /* XXXX put in FIFO and process later */
769 while (code = (inb(iobase + com_lsr) & LSR_RCV_MASK)) {
770 if (code == LSR_RXRDY) {
771 code = inb(iobase + com_data);
772 if (tp->t_state & TS_ISOPEN)
773 (*linesw[tp->t_line].l_rint)(code, tp);
774 #ifdef KGDB
775 else {
776 if (kgdb_dev == makedev(commajor, unit) &&
777 code == FRAME_END)
778 kgdb_connect(0);
779 }
780 #endif
781 } else
782 comeint(sc, code);
783 }
784 } else if (code == IIR_TXRDY) {
785 tp = com_tty[sc->sc_dev.dv_unit];
786 tp->t_state &= ~TS_BUSY;
787 if (tp->t_state & TS_FLUSH)
788 tp->t_state &= ~TS_FLUSH;
789 else
790 if (tp->t_line)
791 (*linesw[tp->t_line].l_start)(tp);
792 else
793 comstart(tp);
794 } else if (code == IIR_MLSC) {
795 commint(sc);
796 } else {
797 log(LOG_WARNING, "%s: weird interrupt: iir=0x%02x\n",
798 sc->sc_dev.dv_xname, code);
799 }
800 code = inb(iobase + com_iir) & IIR_IMASK;
801 if (code & IIR_NOPEND)
802 return 1;
803 }
804 }
805
806 /*
807 * Following are all routines needed for COM to act as console
808 */
809 #include <dev/cons.h>
810
811 comcnprobe(cp)
812 struct consdev *cp;
813 {
814
815 if (!comprobe1(CONADDR)) {
816 cp->cn_pri = CN_DEAD;
817 return;
818 }
819
820 /* locate the major number */
821 for (commajor = 0; commajor < nchrdev; commajor++)
822 if (cdevsw[commajor].d_open == comopen)
823 break;
824
825 /* initialize required fields */
826 cp->cn_dev = makedev(commajor, CONUNIT);
827 #ifdef COMCONSOLE
828 cp->cn_pri = CN_REMOTE; /* Force a serial port console */
829 #else
830 cp->cn_pri = CN_NORMAL;
831 #endif
832 }
833
834 comcninit(cp)
835 struct consdev *cp;
836 {
837
838 cominit(CONUNIT, comdefaultrate);
839 comconsole = CONUNIT;
840 comconsinit = 0;
841 }
842
843 cominit(unit, rate)
844 int unit, rate;
845 {
846 int s = splhigh();
847 u_short iobase = CONADDR;
848 u_char stat;
849
850 outb(iobase + com_cfcr, CFCR_DLAB);
851 rate = comspeed(comdefaultrate);
852 outb(iobase + com_dlbl, rate);
853 outb(iobase + com_dlbh, rate >> 8);
854 outb(iobase + com_cfcr, CFCR_8BITS);
855 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
856 outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
857 stat = inb(iobase + com_iir);
858 splx(s);
859 }
860
861 comcngetc(dev)
862 dev_t dev;
863 {
864 int s = splhigh();
865 u_short iobase = CONADDR;
866 u_char stat, c;
867
868 while (((stat = inb(iobase + com_lsr)) & LSR_RXRDY) == 0)
869 ;
870 c = inb(iobase + com_data);
871 stat = inb(iobase + com_iir);
872 splx(s);
873 return c;
874 }
875
876 /*
877 * Console kernel output character routine.
878 */
879 comcnputc(dev, c)
880 dev_t dev;
881 int c;
882 {
883 int s = splhigh();
884 u_short iobase = CONADDR;
885 u_char stat;
886 register int timo;
887
888 #ifdef KGDB
889 if (dev != kgdb_dev)
890 #endif
891 if (comconsinit == 0) {
892 (void) cominit(COMUNIT(dev), comdefaultrate);
893 comconsinit = 1;
894 }
895 /* wait for any pending transmission to finish */
896 timo = 50000;
897 while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
898 ;
899 outb(iobase + com_data, c);
900 /* wait for this transmission to complete */
901 timo = 1500000;
902 while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
903 ;
904 /* clear any interrupts generated by this transmission */
905 stat = inb(iobase + com_iir);
906 splx(s);
907 }
908