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