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