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