com.c revision 1.12.2.5 1 /*-
2 * Copyright (c) 1993 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.12.2.5 1993/10/07 14:48:45 mycroft Exp $
36 */
37
38 /*
39 * COM driver, based originally on HP dca driver
40 * uses National Semiconductor NS16450/NS16550AF UART
41 */
42 #include "param.h"
43 #include "systm.h"
44 #include "ioctl.h"
45 #include "select.h"
46 #include "tty.h"
47 #include "proc.h"
48 #include "user.h"
49 #include "conf.h"
50 #include "file.h"
51 #include "uio.h"
52 #include "kernel.h"
53 #include "syslog.h"
54 #include "types.h"
55 #include "sys/device.h"
56
57 #include "machine/cpu.h"
58
59 #include "i386/isa/isavar.h"
60 #include "i386/isa/icu.h"
61 #include "i386/isa/comreg.h"
62 #include "i386/isa/ic/ns16550.h"
63
64 struct com_softc {
65 struct device sc_dev;
66 struct isadev sc_id;
67 struct intrhand sc_ih;
68
69 struct ringbuf {
70 int rb_count, rb_first, rb_last, rb_size;
71 char *rb_data;
72 } sc_q;
73 u_short sc_iobase;
74 u_char sc_flags;
75 #define COM_SOFTCAR 0x01
76 #define COM_FIFO 0x02
77 };
78 /* XXXX should be in com_softc, but not ready for that yet */
79 #include "com.h"
80 struct tty *com_tty[NCOM];
81
82 int comdefaultrate = TTYDEF_SPEED;
83
84 static int comprobe __P((struct device *, struct cfdata *, void *));
85 static void comforceintr __P((void *));
86 static void comattach __P((struct device *, struct device *, void *));
87 static int comintr __P((void *));
88
89 struct cfdriver comcd =
90 { NULL, "com", comprobe, comattach, sizeof (struct com_softc) };
91
92 int comparam __P((struct tty *, struct termios *));
93 void comstart __P((struct tty *));
94
95 int comconsole = -1;
96 int comconsinit;
97 int commajor;
98 extern struct tty *constty;
99
100 #define COMUNIT(x) (minor(x))
101
102 #define bis(c, b) do { const register u_short com_ad = (c); \
103 outb(com_ad, inb(com_ad) | (b)); } while(0)
104 #define bic(c, b) do { const register u_short com_ad = (c); \
105 outb(com_ad, inb(com_ad) &~ (b)); } while(0)
106
107 static int
108 comspeed(speed)
109 int speed;
110 {
111 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
112
113 int x, err;
114
115 if (speed == 0)
116 return 0;
117 if (speed < 0)
118 return -1;
119 x = divrnd((COM_FREQ / 16), speed);
120 if (x <= 0)
121 return -1;
122 err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
123 if (err < 0)
124 err = -err;
125 if (err > COM_TOLERANCE)
126 return -1;
127 return x;
128
129 #undef divrnd(n, q)
130 }
131
132 static int
133 _comprobe(iobase)
134 u_short iobase;
135 {
136
137 /* force access to id reg */
138 outb(iobase + com_cfcr, 0);
139 outb(iobase + com_iir, 0);
140 if (inb(iobase + com_iir) & 0x38)
141 return 0;
142 outb(iobase + com_ier, 0);
143 return 1;
144 }
145
146 static int
147 comprobe(parent, cf, aux)
148 struct device *parent;
149 struct cfdata *cf;
150 void *aux;
151 {
152 struct isa_attach_args *ia = aux;
153 u_short iobase = ia->ia_iobase;
154
155 if (iobase == IOBASEUNK)
156 return 0;
157
158 if (!_comprobe(iobase))
159 return 0;
160
161 if (ia->ia_irq == IRQUNK) {
162 ia->ia_irq = isa_discoverintr(comforceintr, aux);
163 if (ia->ia_irq == IRQNONE)
164 return 0;
165 }
166
167 /* disable interrupts */
168 outb(iobase + com_mcr, 0);
169 outb(iobase + com_ier, 0);
170
171 ia->ia_iosize = COM_NPORTS;
172 ia->ia_drq = DRQUNK;
173 ia->ia_msize = 0;
174 return 1;
175 }
176
177 static void
178 comforceintr(aux)
179 void *aux;
180 {
181 struct isa_attach_args *ia = aux;
182 u_short iobase = ia->ia_iobase;
183
184 #if 0
185 /*
186 * As usual, the PC compatible world isn't. We'd like to use the
187 * loopback feature to generate an interrupt, but alas, some lame
188 * clones don't support it.
189 */
190 outb(iobase + com_mcr, MCR_IENABLE | MCR_LOOPBACK);
191 outb(iobase + com_ier, IER_EMSC);
192 outb(iobase + com_mcr, MCR_IENABLE | MCR_LOOPBACK | MCR_DRS);
193 outb(iobase + com_mcr, MCR_IENABLE | MCR_LOOPBACK);
194 #else
195 /*
196 * So instead we try to force the transmit buffer empty (though
197 * it probably is already).
198 */
199 outb(iobase + com_cfcr, CFCR_8BITS); /* turn off DLAB */
200 outb(iobase + com_ier, 0);
201 outb(iobase + com_fifo, 0);
202 outb(iobase + com_lsr, LSR_TXRDY | LSR_TSRE);
203 outb(iobase + com_mcr, MCR_IENABLE);
204 outb(iobase + com_ier, IER_ETXRDY);
205 #endif
206 }
207
208 static void
209 comattach(parent, self, aux)
210 struct device *parent, *self;
211 void *aux;
212 {
213 struct com_softc *sc = (struct com_softc *)self;
214 struct isa_attach_args *ia = aux;
215 u_short iobase = ia->ia_iobase;
216 struct tty *tp;
217 u_char unit = sc->sc_dev.dv_unit;
218
219 if (iobase == comconsole)
220 delay(1000);
221
222 sc->sc_iobase = iobase;
223 sc->sc_flags = 0;
224
225 /* look for a NS 16550AF UART with FIFOs */
226 outb(iobase + com_fifo,
227 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
228 delay(100);
229 if ((inb(iobase + com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK) {
230 sc->sc_flags |= COM_FIFO;
231 printf(": ns16550\n");
232 } else
233 printf(": ns8250 or ns16450\n");
234 isa_establish(&sc->sc_id, &sc->sc_dev);
235
236 sc->sc_ih.ih_fun = comintr;
237 sc->sc_ih.ih_arg = sc;
238 intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
239
240 outb(iobase + com_ier, 0);
241 outb(iobase + com_mcr, MCR_IENABLE);
242
243 /*
244 * Need to reset baud rate, etc. of next print so reset comconsinit.
245 * Also make sure console is always "hardwired"
246 */
247 if (iobase == comconsole) {
248 constty = com_tty[unit] = ttymalloc();
249 comconsinit = 0;
250 sc->sc_flags |= COM_SOFTCAR;
251 }
252 }
253
254 int
255 comopen(dev, flag, mode, p)
256 dev_t dev;
257 int flag, mode;
258 struct proc *p;
259 {
260 int unit = COMUNIT(dev);
261 struct com_softc *sc;
262 u_short iobase;
263 struct tty *tp;
264 int s;
265 int error = 0;
266
267 if (unit >= comcd.cd_ndevs)
268 return ENXIO;
269 sc = comcd.cd_devs[unit];
270 if (!sc)
271 return ENXIO;
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 /* preserve previous speed, if any */
285 if (tp->t_ispeed == 0) {
286 tp->t_iflag = TTYDEF_IFLAG;
287 tp->t_oflag = TTYDEF_OFLAG;
288 tp->t_cflag = TTYDEF_CFLAG;
289 tp->t_lflag = TTYDEF_LFLAG;
290 tp->t_ispeed = tp->t_ospeed = comdefaultrate;
291 }
292 comparam(tp, &tp->t_termios);
293 ttsetwater(tp);
294 } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
295 return EBUSY;
296
297 s = spltty();
298 iobase = sc->sc_iobase;
299 outb(iobase + com_mcr, MCR_DTR | MCR_RTS | MCR_IENABLE);
300 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
301
302 if (sc->sc_flags & COM_SOFTCAR || inb(iobase + com_msr) & MSR_DCD)
303 tp->t_state |= TS_CARR_ON;
304 else
305 tp->t_state &= ~TS_CARR_ON;
306 while ((flag & O_NONBLOCK) == 0 && (tp->t_cflag & CLOCAL) == 0 &&
307 (tp->t_state & TS_CARR_ON) == 0) {
308 /* wait for carrier; allow signals */
309 tp->t_state |= TS_WOPEN;
310 if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
311 ttopen, 0)) {
312 splx(s);
313 return error;
314 }
315 }
316 splx(s);
317
318 return (*linesw[tp->t_line].l_open)(dev, tp);
319 }
320
321 int
322 comclose(dev, flag)
323 dev_t dev;
324 int flag;
325 {
326 int unit = COMUNIT(dev);
327 struct com_softc *sc = comcd.cd_devs[unit];
328 u_short iobase = sc->sc_iobase;
329 struct tty *tp = com_tty[unit];
330
331 (*linesw[tp->t_line].l_close)(tp, flag);
332 bic(iobase + com_cfcr, CFCR_SBREAK);
333 outb(iobase + com_ier, 0);
334 if (tp->t_cflag & HUPCL)
335 bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
336 ttyclose(tp);
337 tp->t_state &= ~(TS_ISOPEN | TS_WOPEN);
338 #ifdef notyet /* XXXX */
339 if (iobase != comconsole) {
340 ttyfree(tp);
341 com_tty[unit] = (struct tty *)NULL;
342 }
343 #endif
344 return 0;
345 }
346
347 int
348 comread(dev, uio, flag)
349 dev_t dev;
350 struct uio *uio;
351 int flag;
352 {
353 struct tty *tp = com_tty[COMUNIT(dev)];
354
355 return (*linesw[tp->t_line].l_read)(tp, uio, flag);
356 }
357
358 int
359 comwrite(dev, uio, flag)
360 dev_t dev;
361 struct uio *uio;
362 int flag;
363 {
364 struct tty *tp = com_tty[COMUNIT(dev)];
365
366 /* XXXX what is this for? */
367 if (constty == tp)
368 constty = NULL;
369 return (*linesw[tp->t_line].l_write)(tp, uio, flag);
370 }
371
372 static u_char
373 tiocm_xxx2mcr(data)
374 int data;
375 {
376 u_char m = 0;
377 if (data & TIOCM_DTR)
378 m |= MCR_DTR;
379 if (data & TIOCM_RTS)
380 m |= MCR_RTS;
381 return m;
382 }
383
384 int
385 comioctl(dev, cmd, data, flag)
386 dev_t dev;
387 int cmd;
388 caddr_t data;
389 int flag;
390 {
391 int unit = COMUNIT(dev);
392 struct com_softc *sc = comcd.cd_devs[unit];
393 u_short iobase = sc->sc_iobase;
394 struct tty *tp = com_tty[unit];
395 int error;
396
397 if (error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag))
398 return error;
399 if (error = ttioctl(tp, cmd, data, flag))
400 return error;
401
402 switch (cmd) {
403 case TIOCSBRK:
404 bis(iobase + com_cfcr, CFCR_SBREAK);
405 break;
406 case TIOCCBRK:
407 bic(iobase + com_cfcr, CFCR_SBREAK);
408 break;
409 case TIOCSDTR:
410 bis(iobase + com_mcr, MCR_DTR | MCR_RTS);
411 break;
412 case TIOCCDTR:
413 bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
414 break;
415 case TIOCMSET:
416 outb(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data) | MCR_IENABLE);
417 break;
418 case TIOCMBIS:
419 bis(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data));
420 break;
421 case TIOCMBIC:
422 bic(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data));
423 break;
424 case TIOCMGET:
425 {
426 u_char m = inb(iobase + com_mcr);
427 int bits = 0;
428
429 m = inb(iobase + com_mcr);
430 if (m & MCR_DTR)
431 bits |= TIOCM_DTR;
432 if (m & MCR_RTS)
433 bits |= TIOCM_RTS;
434 m = inb(iobase + com_msr);
435 if (m & MSR_CTS)
436 bits |= TIOCM_CTS;
437 if (m & MSR_DSR)
438 bits |= TIOCM_DSR;
439 if (m & (MSR_RI | MSR_TERI))
440 bits |= TIOCM_RI;
441 if (inb(iobase + com_ier))
442 bits |= TIOCM_LE;
443 *(int *)data = bits;
444 break;
445 }
446 break;
447 default:
448 return ENOTTY;
449 }
450
451 return 0;
452 }
453
454 int
455 comparam(tp, t)
456 struct tty *tp;
457 struct termios *t;
458 {
459 int unit = COMUNIT(tp->t_dev);
460 struct com_softc *sc = comcd.cd_devs[unit];
461 u_short iobase = sc->sc_iobase;
462 int ospeed = comspeed(t->c_ospeed);
463 u_char cflag = t->c_cflag;
464 u_char cfcr;
465
466 /* check requested parameters */
467 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
468 return EINVAL;
469
470 /* and copy to tty */
471 tp->t_ispeed = t->c_ispeed;
472 tp->t_ospeed = t->c_ospeed;
473 tp->t_cflag = cflag;
474
475 if (ospeed == 0)
476 bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
477 else
478 bis(iobase + com_mcr, MCR_DTR | MCR_RTS);
479
480 outb(iobase + com_cfcr, CFCR_DLAB);
481 outb(iobase + com_dlbl, ospeed & 0xff);
482 outb(iobase + com_dlbh, ospeed >> 8);
483
484 switch (cflag & CSIZE) {
485 case CS5:
486 cfcr = CFCR_5BITS;
487 break;
488 case CS6:
489 cfcr = CFCR_6BITS;
490 break;
491 case CS7:
492 cfcr = CFCR_7BITS;
493 break;
494 case CS8:
495 cfcr = CFCR_8BITS;
496 break;
497 }
498 if (cflag & PARENB) {
499 cfcr |= CFCR_PENAB;
500 if ((cflag & PARODD) == 0)
501 cfcr |= CFCR_PEVEN;
502 }
503 if (cflag & CSTOPB)
504 cfcr |= CFCR_STOPB;
505 outb(iobase + com_cfcr, cfcr);
506
507 /* when not using CRTS_IFLOW, RTS follows DTR */
508 if ((cflag & CRTS_IFLOW) == 0) {
509 u_char mcr = inb(iobase + com_mcr);
510
511 if (mcr & MCR_DTR)
512 if ((mcr & MCR_RTS) == 0)
513 outb(iobase + com_mcr, mcr | MCR_RTS);
514 else
515 ;
516 else
517 if (mcr & MCR_RTS)
518 outb(iobase + com_mcr, mcr &~ MCR_RTS);
519 else
520 ;
521 }
522
523 return 0;
524 }
525
526 void
527 comstart(tp)
528 struct tty *tp;
529 {
530 int unit = COMUNIT(tp->t_dev);
531 struct com_softc *sc = comcd.cd_devs[unit];
532 u_short iobase = sc->sc_iobase;
533 int s;
534
535 s = spltty();
536 if (tp->t_state & (TS_TTSTOP | TS_BUSY))
537 goto out;
538 if (tp->t_cflag & CCTS_OFLOW && (inb(iobase + com_mcr) & MSR_CTS) == 0)
539 goto out;
540 if (tp->t_outq.c_cc <= tp->t_lowat) {
541 if (tp->t_state & TS_ASLEEP) {
542 tp->t_state &= ~TS_ASLEEP;
543 wakeup((caddr_t)&tp->t_outq);
544 }
545 selwakeup(&tp->t_wsel);
546 }
547 if (tp->t_outq.c_cc == 0)
548 goto out;
549 tp->t_state |= TS_BUSY;
550 if ((inb(iobase + com_lsr) & LSR_TXRDY) == 0)
551 goto out;
552 if (sc->sc_flags & COM_FIFO) {
553 u_char buffer[16], *cp = buffer;
554 int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
555 do {
556 outb(iobase + com_data, *cp++);
557 } while (--n);
558 } else
559 outb(iobase + com_data, getc(&tp->t_outq));
560 out:
561 splx(s);
562 }
563
564 /*
565 * Stop output on a line.
566 */
567 void
568 comstop(tp, flag)
569 struct tty *tp;
570 {
571 int s;
572
573 s = spltty();
574 if (tp->t_state & TS_BUSY) {
575 if ((tp->t_state & TS_TTSTOP) == 0)
576 tp->t_state |= TS_FLUSH;
577 }
578 splx(s);
579 }
580
581 static void
582 comeint(sc, stat)
583 struct com_softc *sc;
584 int stat;
585 {
586 u_short iobase = sc->sc_iobase;
587 struct tty *tp = com_tty[sc->sc_dev.dv_unit];
588 int c;
589
590 c = inb(iobase + com_data);
591 if ((tp->t_state & TS_ISOPEN) == 0)
592 return;
593 if (stat & (LSR_BI | LSR_FE))
594 c |= TTY_FE;
595 else if (stat & LSR_PE)
596 c |= TTY_PE;
597 else if (stat & LSR_OE) {
598 c |= TTY_PE; /* XXX ought to have its own define */
599 log(LOG_WARNING, "com%d: silo overflow\n", sc->sc_dev.dv_unit);
600 }
601 /* XXXX put in FIFO and process later */
602 (*linesw[tp->t_line].l_rint)(c, tp);
603 }
604
605 static void
606 commint(sc)
607 struct com_softc *sc;
608 {
609 u_short iobase = sc->sc_iobase;
610 struct tty *tp = com_tty[sc->sc_dev.dv_unit];
611 u_char stat;
612
613 stat = inb(iobase + com_msr);
614 if (stat & MSR_DDCD && (sc->sc_flags & COM_SOFTCAR) == 0) {
615 if (stat & MSR_DCD)
616 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
617 else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
618 bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
619 } else if (stat & MSR_DCTS && tp->t_state & TS_ISOPEN &&
620 tp->t_cflag & CRTSCTS) {
621 /* the line is up and we want to do rts/cts flow control */
622 if (stat & MSR_CTS) {
623 tp->t_state &=~ TS_TTSTOP;
624 ttstart(tp);
625 } else
626 tp->t_state |= TS_TTSTOP;
627 }
628 }
629
630 static int
631 comintr(arg)
632 void *arg;
633 {
634 struct com_softc *sc = arg;
635 u_short iobase = sc->sc_iobase;
636 struct tty *tp;
637 u_char code;
638
639 code = inb(iobase + com_iir);
640 if (code & IIR_NOPEND)
641 return 0;
642
643 for (;;) {
644 switch (code & IIR_IMASK) {
645 case IIR_RXRDY:
646 case IIR_RXTOUT:
647 tp = com_tty[sc->sc_dev.dv_unit];
648 /* XXXX put in FIFO and process later */
649 #define RCVBYTE() \
650 code = inb(iobase + com_data); \
651 if (tp->t_state & TS_ISOPEN) \
652 (*linesw[tp->t_line].l_rint)(code, tp)
653 RCVBYTE();
654 if (sc->sc_flags & COM_FIFO)
655 while (code = inb(iobase + com_lsr) & LSR_RCV_MASK) {
656 if (code == LSR_RXRDY) {
657 RCVBYTE();
658 } else
659 comeint(sc, code);
660 }
661 break;
662 case IIR_TXRDY:
663 tp = com_tty[sc->sc_dev.dv_unit];
664 tp->t_state &=~ (TS_BUSY | TS_FLUSH);
665 if (tp->t_line)
666 (*linesw[tp->t_line].l_start)(tp);
667 else
668 comstart(tp);
669 break;
670 case IIR_RLS:
671 comeint(sc, inb(iobase + com_lsr));
672 break;
673 default:
674 log(LOG_WARNING, "com%d: weird iir=0x%x\n",
675 sc->sc_dev.dv_unit, code);
676 /* fall through */
677 case IIR_MLSC:
678 commint(sc);
679 break;
680 }
681
682 code = inb(iobase + com_iir);
683 if (code & IIR_NOPEND)
684 return 1;
685 }
686 }
687
688 /* XXXXXXXXXXXXXXXX ---- gremlins below here ---- XXXXXXXXXXXXXXXX */
689
690 #if 0
691 /*
692 * Following are all routines needed for COM to act as console
693 */
694 #include "i386/i386/cons.h"
695
696 comcnprobe(cp)
697 struct consdev *cp;
698 {
699 /* XXXX */
700 if (!_comprobe(CONADDR))
701 return CN_DEAD;
702
703 /* locate the major number */
704 for (commajor = 0; commajor < nchrdev; commajor++)
705 if (cdevsw[commajor].d_open == comopen)
706 break;
707
708 /* initialize required fields */
709 cp->cn_dev = makedev(commajor, unit);
710 #ifdef COMCONSOLE
711 cp->cn_pri = CN_REMOTE; /* Force a serial port console */
712 #else
713 cp->cn_pri = CN_NORMAL;
714 #endif
715 }
716
717 comcninit(cp)
718 struct consdev *cp;
719 {
720 int unit = COMUNIT(cp->cn_dev);
721
722 comconsole = CONADDR;
723 comconsinit = 0;
724 }
725
726 cominit(unit, rate)
727 int unit, rate;
728 {
729 int com;
730 int s;
731 short stat;
732
733 com = com_addr[unit];
734 s = splhigh();
735 outb(com+com_cfcr, CFCR_DLAB);
736 rate = COM_BAUDDIV(rate);
737 outb(com+com_data, rate & 0xFF);
738 outb(com+com_ier, rate >> 8);
739 outb(com+com_cfcr, CFCR_8BITS);
740 outb(com+com_ier, IER_ERXRDY | IER_ETXRDY);
741 outb(com+com_fifo, FIFO_ENABLE|FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_4);
742 stat = inb(com+com_iir);
743 splx(s);
744 }
745
746 comcngetc(dev)
747 {
748 com = com_addr[COMUNIT(dev)];
749 short stat;
750 int c, s;
751
752 s = splhigh();
753 while (((stat = inb(com+com_lsr)) & LSR_RXRDY) == 0)
754 ;
755 c = inb(com+com_data);
756 stat = inb(com+com_iir);
757 splx(s);
758 return c;
759 }
760
761 /*
762 * Console kernel output character routine.
763 */
764 comcnputc(dev, c)
765 dev_t dev;
766 int c;
767 {
768 com = com_addr[COMUNIT(dev)];
769 int timo;
770 short stat;
771 int s = splhigh();
772
773 if (comconsinit == 0) {
774 (void) cominit(COMUNIT(dev), comdefaultrate);
775 comconsinit = 1;
776 }
777 /* wait for any pending transmission to finish */
778 timo = 50000;
779 while (((stat = inb(com+com_lsr)) & LSR_TXRDY) == 0 && --timo)
780 ;
781 outb(com+com_data, c);
782 /* wait for this transmission to complete */
783 timo = 1500000;
784 while (((stat = inb(com+com_lsr)) & LSR_TXRDY) == 0 && --timo)
785 ;
786 /* clear any interrupts generated by this transmission */
787 stat = inb(com+com_iir);
788 splx(s);
789 }
790 #endif
791