com.c revision 1.23 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.23 1994/03/12 07:43:03 cgd 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/isa_device.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_MULTI 0x01
72 #define COM_HW_CONFIGBITS COM_HW_MULTI
73 #define COM_HW_FIFO 0x02
74 #define COM_HW_CONSOLE 0x40
75 u_char sc_swflags;
76 #define COM_SW_SOFTCAR 0x01
77 #define COM_SW_CLOCAL 0x02
78 #define COM_SW_CRTSCTS 0x04
79 u_char sc_msr, sc_mcr;
80 } com_softc[NCOM];
81 /* XXXX should be in com_softc, but not ready for that yet */
82 struct tty *com_tty[NCOM];
83
84 int comprobe __P((struct isa_device *));
85 int comattach __P((struct isa_device *));
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 isa_driver comdriver = {
93 comprobe, comattach, "com"
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(isa_dev)
160 struct isa_device *isa_dev;
161 {
162 struct com_softc *sc = &com_softc[isa_dev->id_unit];
163 u_short iobase = isa_dev->id_iobase;
164
165 /* XXX HACK */
166 sprintf(sc->sc_dev.dv_xname, "%s%d", comdriver.name, isa_dev->id_unit);
167 sc->sc_dev.dv_unit = isa_dev->id_unit;
168
169 if (!comprobe1(iobase))
170 return 0;
171
172 return COM_NPORTS;
173 }
174
175 int
176 comattach(isa_dev)
177 struct isa_device *isa_dev;
178 {
179 int unit = isa_dev->id_unit;
180 struct com_softc *sc = &com_softc[unit];
181 u_short iobase = isa_dev->id_iobase;
182 struct tty *tp;
183
184 if (unit == comconsole)
185 delay(1000);
186
187 sc->sc_iobase = iobase;
188 sc->sc_hwflags = 0;
189 sc->sc_swflags = 0;
190
191 printf("%s: ", sc->sc_dev.dv_xname);
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 /*
232 * Need to reset baud rate, etc. of next print so reset comconsinit.
233 * Also make sure console is always "hardwired".
234 */
235 if (unit == comconsole) {
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 > NCOM)
256 return ENXIO;
257 sc = &com_softc[unit];
258 if (!sc->sc_iobase)
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 tp->t_lflag = TTYDEF_LFLAG;
282 tp->t_ispeed = tp->t_ospeed = comdefaultrate;
283 comparam(tp, &tp->t_termios);
284 ttsetwater(tp);
285
286 iobase = sc->sc_iobase;
287 /* flush any pending I/O */
288 if (sc->sc_hwflags & COM_HW_FIFO)
289 outb(iobase + com_fifo,
290 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
291 FIFO_TRIGGER_8);
292 (void) inb(iobase + com_lsr);
293 (void) inb(iobase + com_data);
294 /* you turn me on, baby */
295 outb(iobase + com_mcr,
296 sc->sc_mcr = MCR_DTR | MCR_RTS | MCR_IENABLE);
297 outb(iobase + com_ier,
298 IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
299
300 sc->sc_msr = inb(iobase + com_msr);
301 if (sc->sc_swflags & COM_SW_SOFTCAR || sc->sc_msr & MSR_DCD)
302 tp->t_state |= TS_CARR_ON;
303 else
304 tp->t_state &= ~TS_CARR_ON;
305 } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) {
306 splx(s);
307 return EBUSY;
308 }
309
310 /* wait for carrier if necessary */
311 if ((flag & O_NONBLOCK) == 0)
312 while ((tp->t_cflag & CLOCAL) == 0 &&
313 (tp->t_state & TS_CARR_ON) == 0) {
314 tp->t_state |= TS_WOPEN;
315 error = ttysleep(tp, (caddr_t)&tp->t_rawq,
316 TTIPRI | PCATCH, ttopen, 0);
317 if (error) {
318 /* XXX should turn off chip if we're the
319 only waiter */
320 splx(s);
321 return error;
322 }
323 }
324 splx(s);
325
326 return (*linesw[tp->t_line].l_open)(dev, tp);
327 }
328
329 int
330 comclose(dev, flag, mode, p)
331 dev_t dev;
332 int flag, mode;
333 struct proc *p;
334 {
335 int unit = COMUNIT(dev);
336 struct com_softc *sc = &com_softc[unit];
337 u_short iobase = sc->sc_iobase;
338 struct tty *tp = com_tty[unit];
339
340 (*linesw[tp->t_line].l_close)(tp, flag);
341 #ifdef KGDB
342 /* do not disable interrupts if debugging */
343 if (kgdb_dev != makedev(commajor, unit))
344 #endif
345 {
346 bic(iobase + com_cfcr, CFCR_SBREAK);
347 outb(iobase + com_ier, 0);
348 if (tp->t_cflag & HUPCL &&
349 (sc->sc_swflags & COM_SW_SOFTCAR) == 0)
350 /* XXX perhaps only clear DTR */
351 outb(iobase + com_mcr, 0);
352 }
353 ttyclose(tp);
354 #ifdef notyet /* XXXX */
355 if (unit != comconsole) {
356 ttyfree(tp);
357 com_tty[unit] = (struct tty *)NULL;
358 }
359 #endif
360 return 0;
361 }
362
363 int
364 comread(dev, uio, flag)
365 dev_t dev;
366 struct uio *uio;
367 int flag;
368 {
369 struct tty *tp = com_tty[COMUNIT(dev)];
370
371 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
372 }
373
374 int
375 comwrite(dev, uio, flag)
376 dev_t dev;
377 struct uio *uio;
378 int flag;
379 {
380 struct tty *tp = com_tty[COMUNIT(dev)];
381
382 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
383 }
384
385 static u_char
386 tiocm_xxx2mcr(data)
387 int data;
388 {
389 u_char m = 0;
390
391 if (data & TIOCM_DTR)
392 m |= MCR_DTR;
393 if (data & TIOCM_RTS)
394 m |= MCR_RTS;
395 return m;
396 }
397
398 int
399 comioctl(dev, cmd, data, flag, p)
400 dev_t dev;
401 int cmd;
402 caddr_t data;
403 int flag;
404 struct proc *p;
405 {
406 int unit = COMUNIT(dev);
407 struct com_softc *sc = &com_softc[unit];
408 u_short iobase = sc->sc_iobase;
409 struct tty *tp = com_tty[unit];
410 int error;
411
412 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
413 if (error >= 0)
414 return error;
415 error = ttioctl(tp, cmd, data, flag, p);
416 if (error >= 0)
417 return error;
418
419 switch (cmd) {
420 case TIOCSBRK:
421 bis(iobase + com_cfcr, CFCR_SBREAK);
422 break;
423 case TIOCCBRK:
424 bic(iobase + com_cfcr, CFCR_SBREAK);
425 break;
426 case TIOCSDTR:
427 outb(iobase + com_mcr, sc->sc_mcr |= (MCR_DTR | MCR_RTS));
428 break;
429 case TIOCCDTR:
430 outb(iobase + com_mcr, sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
431 break;
432 case TIOCMSET:
433 sc->sc_mcr &= ~(MCR_DTR | MCR_RTS);
434 case TIOCMBIS:
435 outb(iobase + com_mcr,
436 sc->sc_mcr |= tiocm_xxx2mcr(*(int *)data));
437 break;
438 case TIOCMBIC:
439 outb(iobase + com_mcr,
440 sc->sc_mcr &= ~tiocm_xxx2mcr(*(int *)data));
441 break;
442 case TIOCMGET: {
443 u_char m;
444 int bits = 0;
445
446 m = sc->sc_mcr;
447 if (m & MCR_DTR)
448 bits |= TIOCM_DTR;
449 if (m & MCR_RTS)
450 bits |= TIOCM_RTS;
451 m = sc->sc_msr;
452 if (m & MSR_DCD)
453 bits |= TIOCM_CD;
454 if (m & MSR_CTS)
455 bits |= TIOCM_CTS;
456 if (m & MSR_DSR)
457 bits |= TIOCM_DSR;
458 if (m & (MSR_RI | MSR_TERI))
459 bits |= TIOCM_RI;
460 if (inb(iobase + com_ier))
461 bits |= TIOCM_LE;
462 *(int *)data = bits;
463 break;
464 }
465 case TIOCGFLAGS: {
466 int bits = 0;
467
468 if (sc->sc_swflags & COM_SW_SOFTCAR)
469 bits |= TIOCFLAG_SOFTCAR;
470 if (sc->sc_swflags & COM_SW_CLOCAL)
471 bits |= TIOCFLAG_CLOCAL;
472 if (sc->sc_swflags & COM_SW_CRTSCTS)
473 bits |= TIOCFLAG_CRTSCTS;
474
475 *(int *)data = bits;
476 break;
477 }
478 case TIOCSFLAGS: {
479 int userbits, driverbits = 0;
480
481 error = suser(p->p_ucred, &p->p_acflag);
482 if (error != 0)
483 return(EPERM);
484
485 userbits = *(int *)data;
486 if ((userbits & TIOCFLAG_SOFTCAR) ||
487 (sc->sc_hwflags & COM_HW_CONSOLE))
488 driverbits |= COM_SW_SOFTCAR;
489 if (userbits & TIOCFLAG_CLOCAL)
490 driverbits |= COM_SW_CLOCAL;
491 if (userbits & TIOCFLAG_CRTSCTS)
492 driverbits |= COM_SW_CRTSCTS;
493
494 sc->sc_swflags = driverbits;
495 break;
496 }
497 default:
498 return ENOTTY;
499 }
500
501 return 0;
502 }
503
504 int
505 comparam(tp, t)
506 struct tty *tp;
507 struct termios *t;
508 {
509 struct com_softc *sc = &com_softc[COMUNIT(tp->t_dev)];
510 u_short iobase = sc->sc_iobase;
511 int ospeed = comspeed(t->c_ospeed);
512 u_char cfcr;
513 int s;
514
515 /* check requested parameters */
516 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
517 return EINVAL;
518
519 switch (t->c_cflag & CSIZE) {
520 case CS5:
521 cfcr = CFCR_5BITS;
522 break;
523 case CS6:
524 cfcr = CFCR_6BITS;
525 break;
526 case CS7:
527 cfcr = CFCR_7BITS;
528 break;
529 case CS8:
530 cfcr = CFCR_8BITS;
531 break;
532 }
533 if (t->c_cflag & PARENB) {
534 cfcr |= CFCR_PENAB;
535 if ((t->c_cflag & PARODD) == 0)
536 cfcr |= CFCR_PEVEN;
537 }
538 if (t->c_cflag & CSTOPB)
539 cfcr |= CFCR_STOPB;
540
541 s = spltty();
542
543 /* and copy to tty */
544 tp->t_ispeed = t->c_ispeed;
545 tp->t_ospeed = t->c_ospeed;
546 tp->t_cflag = t->c_cflag;
547
548 if (ospeed == 0)
549 outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_DTR);
550 else
551 outb(iobase + com_mcr, sc->sc_mcr |= MCR_DTR);
552
553 outb(iobase + com_cfcr, cfcr | CFCR_DLAB);
554 outb(iobase + com_dlbl, ospeed);
555 outb(iobase + com_dlbh, ospeed>>8);
556 outb(iobase + com_cfcr, cfcr);
557
558 /* When not using CRTSCTS, RTS follows DTR. */
559 if ((t->c_cflag & CRTSCTS) == 0) {
560 if (sc->sc_mcr & MCR_DTR) {
561 if ((sc->sc_mcr & MCR_RTS) == 0)
562 outb(iobase + com_mcr, sc->sc_mcr |= MCR_RTS);
563 } else {
564 if (sc->sc_mcr & MCR_RTS)
565 outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_RTS);
566 }
567 }
568
569 /* If CTS is off and CRTSCTS is changed, we must toggle TS_TTSTOP. */
570 if ((sc->sc_msr & MSR_CTS) == 0 &&
571 (tp->t_cflag & CRTSCTS) != (t->c_cflag & CRTSCTS)) {
572 if ((t->c_cflag & CRTSCTS) == 0) {
573 tp->t_state &= ~TS_TTSTOP;
574 ttstart(tp);
575 } else
576 tp->t_state |= TS_TTSTOP;
577 }
578
579 splx(s);
580 return 0;
581 }
582
583 void
584 comstart(tp)
585 struct tty *tp;
586 {
587 struct com_softc *sc = &com_softc[COMUNIT(tp->t_dev)];
588 u_short iobase = sc->sc_iobase;
589 int s;
590
591 s = spltty();
592 if (tp->t_state & (TS_TTSTOP | TS_BUSY))
593 goto out;
594 #if 0 /* XXXX I think this is handled adequately by commint() and comparam(). */
595 if (tp->t_cflag & CRTSCTS && (sc->sc_mcr & MSR_CTS) == 0)
596 goto out;
597 #endif
598 if (tp->t_outq.c_cc <= tp->t_lowat) {
599 if (tp->t_state & TS_ASLEEP) {
600 tp->t_state &= ~TS_ASLEEP;
601 wakeup((caddr_t)&tp->t_outq);
602 }
603 selwakeup(&tp->t_wsel);
604 }
605 if (tp->t_outq.c_cc == 0)
606 goto out;
607 tp->t_state |= TS_BUSY;
608 if ((inb(iobase + com_lsr) & LSR_TXRDY) == 0)
609 goto out;
610 if (sc->sc_hwflags & COM_HW_FIFO) {
611 u_char buffer[16], *cp = buffer;
612 int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
613 do {
614 outb(iobase + com_data, *cp++);
615 } while (--n);
616 } else
617 outb(iobase + com_data, getc(&tp->t_outq));
618 out:
619 splx(s);
620 }
621
622 /*
623 * Stop output on a line.
624 */
625 void
626 comstop(tp, flag)
627 struct tty *tp;
628 {
629 int s;
630
631 s = spltty();
632 if (tp->t_state & TS_BUSY)
633 if ((tp->t_state & TS_TTSTOP) == 0)
634 tp->t_state |= TS_FLUSH;
635 splx(s);
636 }
637
638 static inline void
639 comeint(sc, stat)
640 struct com_softc *sc;
641 int stat;
642 {
643 u_short iobase = sc->sc_iobase;
644 int unit = sc->sc_dev.dv_unit;
645 struct tty *tp = com_tty[unit];
646 int c;
647
648 c = inb(iobase + com_data);
649 if ((tp->t_state & TS_ISOPEN) == 0) {
650 #ifdef KGDB
651 /* we don't care about parity errors */
652 if (((stat & (LSR_BI | LSR_FE | LSR_PE)) == LSR_PE) &&
653 kgdb_dev == makedev(commajor, unit) && c == FRAME_END)
654 kgdb_connect(0); /* trap into kgdb */
655 #endif
656 return;
657 }
658 if (stat & (LSR_BI | LSR_FE))
659 c |= TTY_FE;
660 else if (stat & LSR_PE)
661 c |= TTY_PE;
662 if (stat & LSR_OE)
663 log(LOG_WARNING, "%s: silo overflow\n", sc->sc_dev.dv_xname);
664 /* XXXX put in FIFO and process later */
665 (*linesw[tp->t_line].l_rint)(c, tp);
666 }
667
668 static inline void
669 commint(sc)
670 struct com_softc *sc;
671 {
672 u_short iobase = sc->sc_iobase;
673 struct tty *tp = com_tty[sc->sc_dev.dv_unit];
674 u_char msr, delta;
675
676 msr = inb(iobase + com_msr);
677 delta = msr ^ sc->sc_msr;
678 sc->sc_msr = msr;
679
680 if (delta & MSR_DCD && (sc->sc_swflags & COM_SW_SOFTCAR) == 0) {
681 if (msr & MSR_DCD)
682 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
683 else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
684 outb(iobase + com_mcr,
685 sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
686 }
687 if (delta & MSR_CTS && tp->t_cflag & CRTSCTS) {
688 /* the line is up and we want to do rts/cts flow control */
689 if (msr & MSR_CTS) {
690 tp->t_state &= ~TS_TTSTOP;
691 ttstart(tp);
692 } else
693 tp->t_state |= TS_TTSTOP;
694 }
695 }
696
697 int
698 comintr(unit)
699 int unit;
700 {
701 struct com_softc *sc = &com_softc[unit];
702 u_short iobase = sc->sc_iobase;
703 struct tty *tp;
704 u_char code;
705
706 code = inb(iobase + com_iir) & IIR_IMASK;
707 if (code & IIR_NOPEND)
708 return 0;
709
710 for (;;) {
711 if (code & IIR_RXRDY) {
712 tp = com_tty[sc->sc_dev.dv_unit];
713 /* XXXX put in FIFO and process later */
714 while (code = (inb(iobase + com_lsr) & LSR_RCV_MASK)) {
715 if (code == LSR_RXRDY) {
716 code = inb(iobase + com_data);
717 if (tp->t_state & TS_ISOPEN)
718 (*linesw[tp->t_line].l_rint)(code, tp);
719 #ifdef KGDB
720 else {
721 if (kgdb_dev == makedev(commajor, unit) &&
722 code == FRAME_END)
723 kgdb_connect(0);
724 }
725 #endif
726 } else
727 comeint(sc, code);
728 }
729 } else if (code == IIR_TXRDY) {
730 tp = com_tty[sc->sc_dev.dv_unit];
731 tp->t_state &= ~TS_BUSY;
732 if (tp->t_state & TS_FLUSH)
733 tp->t_state &= ~TS_FLUSH;
734 else
735 if (tp->t_line)
736 (*linesw[tp->t_line].l_start)(tp);
737 else
738 comstart(tp);
739 } else if (code == IIR_MLSC) {
740 commint(sc);
741 } else {
742 log(LOG_WARNING, "%s: weird interrupt: iir=0x%02x\n",
743 sc->sc_dev.dv_xname, code);
744 }
745 code = inb(iobase + com_iir) & IIR_IMASK;
746 if (code & IIR_NOPEND)
747 return 1;
748 }
749 }
750
751 /*
752 * Following are all routines needed for COM to act as console
753 */
754 #include <dev/cons.h>
755
756 comcnprobe(cp)
757 struct consdev *cp;
758 {
759 int unit = CONUNIT;
760
761 if (!comprobe1(CONADDR)) {
762 cp->cn_pri = CN_DEAD;
763 return;
764 }
765
766 /* locate the major number */
767 for (commajor = 0; commajor < nchrdev; commajor++)
768 if (cdevsw[commajor].d_open == comopen)
769 break;
770
771 com_softc[unit].sc_iobase = CONADDR;
772
773 /* initialize required fields */
774 cp->cn_dev = makedev(commajor, unit);
775 #ifdef COMCONSOLE
776 cp->cn_pri = CN_REMOTE; /* Force a serial port console */
777 #else
778 cp->cn_pri = CN_NORMAL;
779 #endif
780 }
781
782 comcninit(cp)
783 struct consdev *cp;
784 {
785 int unit = CONUNIT;
786
787 cominit(unit, comdefaultrate);
788 comconsole = unit;
789 comconsinit = 0;
790 }
791
792 cominit(unit, rate)
793 int unit, rate;
794 {
795 int s = splhigh();
796 u_short iobase = com_softc[unit].sc_iobase;
797 u_char stat;
798
799 outb(iobase + com_cfcr, CFCR_DLAB);
800 rate = comspeed(comdefaultrate);
801 outb(iobase + com_dlbl, rate);
802 outb(iobase + com_dlbh, rate >> 8);
803 outb(iobase + com_cfcr, CFCR_8BITS);
804 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
805 outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
806 stat = inb(iobase + com_iir);
807 splx(s);
808 }
809
810 comcngetc(dev)
811 dev_t dev;
812 {
813 int s = splhigh();
814 u_short iobase = com_softc[COMUNIT(dev)].sc_iobase;
815 u_char stat, c;
816
817 while (((stat = inb(iobase + com_lsr)) & LSR_RXRDY) == 0)
818 ;
819 c = inb(iobase + com_data);
820 stat = inb(iobase + com_iir);
821 splx(s);
822 return c;
823 }
824
825 /*
826 * Console kernel output character routine.
827 */
828 comcnputc(dev, c)
829 dev_t dev;
830 int c;
831 {
832 int s = splhigh();
833 u_short iobase = com_softc[COMUNIT(dev)].sc_iobase;
834 u_char stat;
835 register int timo;
836
837 #ifdef KGDB
838 if (dev != kgdb_dev)
839 #endif
840 if (comconsinit == 0) {
841 (void) cominit(COMUNIT(dev), comdefaultrate);
842 comconsinit = 1;
843 }
844 /* wait for any pending transmission to finish */
845 timo = 50000;
846 while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
847 ;
848 outb(iobase + com_data, c);
849 /* wait for this transmission to complete */
850 timo = 1500000;
851 while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
852 ;
853 /* clear any interrupts generated by this transmission */
854 stat = inb(iobase + com_iir);
855 splx(s);
856 }
857