com.c revision 1.73 1 /* $NetBSD: com.c,v 1.73 1996/02/26 22:48:32 scottr Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Charles M. Hannum. All rights reserved.
6 * Copyright (c) 1991 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)com.c 7.5 (Berkeley) 5/16/91
38 */
39
40 /*
41 * COM driver, based on HP dca driver
42 * uses National Semiconductor NS16450/NS16550AF UART
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 <dev/isa/isavar.h>
63 #include <dev/isa/comreg.h>
64 #include <dev/ic/ns16550reg.h>
65 #ifdef COM_HAYESP
66 #include <dev/ic/hayespreg.h>
67 #endif
68 #define com_lcr com_cfcr
69
70 #define COM_IBUFSIZE (2 * 512)
71 #define COM_IHIGHWATER ((3 * COM_IBUFSIZE) / 4)
72
73 struct com_softc {
74 struct device sc_dev;
75 void *sc_ih;
76 struct tty *sc_tty;
77
78 int sc_overflows;
79 int sc_floods;
80 int sc_errors;
81
82 int sc_halt;
83
84 int sc_iobase;
85 #ifdef COM_HAYESP
86 int sc_hayespbase;
87 #endif
88 u_char sc_hwflags;
89 #define COM_HW_NOIEN 0x01
90 #define COM_HW_FIFO 0x02
91 #define COM_HW_HAYESP 0x04
92 #define COM_HW_CONSOLE 0x40
93 u_char sc_swflags;
94 #define COM_SW_SOFTCAR 0x01
95 #define COM_SW_CLOCAL 0x02
96 #define COM_SW_CRTSCTS 0x04
97 #define COM_SW_MDMBUF 0x08
98 u_char sc_msr, sc_mcr, sc_lcr, sc_ier;
99 u_char sc_dtr;
100
101 u_char *sc_ibuf, *sc_ibufp, *sc_ibufhigh, *sc_ibufend;
102 u_char sc_ibufs[2][COM_IBUFSIZE];
103 };
104
105 int comprobe __P((struct device *, void *, void *));
106 void comattach __P((struct device *, struct device *, void *));
107 int comopen __P((dev_t, int, int, struct proc *));
108 int comclose __P((dev_t, int, int, struct proc *));
109 void comdiag __P((void *));
110 int comintr __P((void *));
111 void compoll __P((void *));
112 int comparam __P((struct tty *, struct termios *));
113 void comstart __P((struct tty *));
114
115 struct cfdriver comcd = {
116 NULL, "com", comprobe, comattach, DV_TTY, sizeof(struct com_softc)
117 };
118
119 #ifdef COMCONSOLE
120 int comdefaultrate = CONSPEED;
121 int comconsole = COMCONSOLE;
122 #else
123 int comdefaultrate = TTYDEF_SPEED;
124 int comconsole = -1;
125 #endif
126 int comconsinit;
127 int commajor;
128 int comsopen = 0;
129 int comevents = 0;
130
131 #ifdef KGDB
132 #include <machine/remote-sl.h>
133 extern int kgdb_dev;
134 extern int kgdb_rate;
135 extern int kgdb_debug_init;
136 #endif
137
138 #define COMUNIT(x) (minor(x))
139
140 /* Macros to clear/set/test flags. */
141 #define SET(t, f) (t) |= (f)
142 #define CLR(t, f) (t) &= ~(f)
143 #define ISSET(t, f) ((t) & (f))
144
145 int
146 comspeed(speed)
147 long speed;
148 {
149 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
150
151 int x, err;
152
153 if (speed == 0)
154 return 0;
155 if (speed < 0)
156 return -1;
157 x = divrnd((COM_FREQ / 16), speed);
158 if (x <= 0)
159 return -1;
160 err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
161 if (err < 0)
162 err = -err;
163 if (err > COM_TOLERANCE)
164 return -1;
165 return x;
166
167 #undef divrnd(n, q)
168 }
169
170 int
171 comprobe1(iobase)
172 int iobase;
173 {
174
175 /* force access to id reg */
176 outb(iobase + com_lcr, 0);
177 outb(iobase + com_iir, 0);
178 if (inb(iobase + com_iir) & 0x38)
179 return 0;
180
181 return 1;
182 }
183
184 #ifdef COM_HAYESP
185 int
186 comprobeHAYESP(iobase, sc)
187 int iobase;
188 struct com_softc *sc;
189 {
190 char val, dips;
191 int combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
192
193 /*
194 * Hayes ESP cards have two iobases. One is for compatibility with
195 * 16550 serial chips, and at the same ISA PC base addresses. The
196 * other is for ESP-specific enhanced features, and lies at a
197 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
198 */
199
200 /* Test for ESP signature */
201 if ((inb(iobase) & 0xf3) == 0)
202 return 0;
203
204 /*
205 * ESP is present at ESP enhanced base address; unknown com port
206 */
207
208 /* Get the dip-switch configurations */
209 outb(iobase + HAYESP_CMD1, HAYESP_GETDIPS);
210 dips = inb(iobase + HAYESP_STATUS1);
211
212 /* Determine which com port this ESP card services: bits 0,1 of */
213 /* dips is the port # (0-3); combaselist[val] is the com_iobase */
214 if (sc->sc_iobase != combaselist[dips & 0x03])
215 return 0;
216
217 printf(": ESP");
218
219 /* Check ESP Self Test bits. */
220 /* Check for ESP version 2.0: bits 4,5,6 == 010 */
221 outb(iobase + HAYESP_CMD1, HAYESP_GETTEST);
222 val = inb(iobase + HAYESP_STATUS1); /* Clear reg 1 */
223 val = inb(iobase + HAYESP_STATUS2);
224 if ((val & 0x70) < 0x20) {
225 printf("-old (%o)", val & 0x70);
226 /* we do not support the necessary features */
227 return 0;
228 }
229
230 /* Check for ability to emulate 16550: bit 8 == 1 */
231 if ((dips & 0x80) == 0) {
232 printf(" slave");
233 /* XXX Does slave really mean no 16550 support?? */
234 return 0;
235 }
236
237 /*
238 * If we made it this far, we are a full-featured ESP v2.0 (or
239 * better), at the correct com port address.
240 */
241
242 SET(sc->sc_hwflags, COM_HW_HAYESP);
243 printf(", 1024 byte fifo\n");
244 return 1;
245 }
246 #endif
247
248 int
249 comprobe(parent, match, aux)
250 struct device *parent;
251 void *match, *aux;
252 {
253 struct isa_attach_args *ia = aux;
254 int iobase = ia->ia_iobase;
255
256 if (!comprobe1(iobase))
257 return 0;
258
259 ia->ia_iosize = COM_NPORTS;
260 ia->ia_msize = 0;
261 return 1;
262 }
263
264 void
265 comattach(parent, self, aux)
266 struct device *parent, *self;
267 void *aux;
268 {
269 struct com_softc *sc = (void *)self;
270 struct isa_attach_args *ia = aux;
271 struct cfdata *cf = sc->sc_dev.dv_cfdata;
272 int iobase = ia->ia_iobase;
273 struct tty *tp;
274 #ifdef COM_HAYESP
275 int hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
276 int *hayespp;
277 #endif
278
279 sc->sc_iobase = iobase;
280 sc->sc_hwflags = ISSET(cf->cf_flags, COM_HW_NOIEN);
281 sc->sc_swflags = 0;
282
283 if (sc->sc_dev.dv_unit == comconsole)
284 delay(1000);
285
286 #ifdef COM_HAYESP
287 /* Look for a Hayes ESP board. */
288 for (hayespp = hayesp_ports; *hayespp != 0; hayespp++)
289 if (comprobeHAYESP(*hayespp, sc)) {
290 sc->sc_hayespbase = *hayespp;
291 break;
292 }
293 /* No ESP; look for other things. */
294 if (*hayespp == 0) {
295 #endif
296
297 /* look for a NS 16550AF UART with FIFOs */
298 outb(iobase + com_fifo,
299 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
300 delay(100);
301 if (ISSET(inb(iobase + com_iir), IIR_FIFO_MASK) == IIR_FIFO_MASK)
302 if (ISSET(inb(iobase + com_fifo), FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
303 SET(sc->sc_hwflags, COM_HW_FIFO);
304 printf(": ns16550a, working fifo\n");
305 } else
306 printf(": ns16550, broken fifo\n");
307 else
308 printf(": ns8250 or ns16450, no fifo\n");
309 outb(iobase + com_fifo, 0);
310 #ifdef COM_HAYESP
311 }
312 #endif
313
314 /* disable interrupts */
315 outb(iobase + com_ier, 0);
316 outb(iobase + com_mcr, 0);
317
318 if (ia->ia_irq != IRQUNK)
319 sc->sc_ih = isa_intr_establish(ia->ia_irq, IST_EDGE, IPL_TTY,
320 comintr, sc);
321
322 #ifdef KGDB
323 if (kgdb_dev == makedev(commajor, unit)) {
324 if (comconsole == unit)
325 kgdb_dev = -1; /* can't debug over console port */
326 else {
327 (void) cominit(unit, kgdb_rate);
328 if (kgdb_debug_init) {
329 /*
330 * Print prefix of device name,
331 * let kgdb_connect print the rest.
332 */
333 printf("%s: ", sc->sc_dev.dv_xname);
334 kgdb_connect(1);
335 } else
336 printf("%s: kgdb enabled\n",
337 sc->sc_dev.dv_xname);
338 }
339 }
340 #endif
341
342 if (sc->sc_dev.dv_unit == comconsole) {
343 /*
344 * Need to reset baud rate, etc. of next print so reset
345 * comconsinit. Also make sure console is always "hardwired".
346 */
347 comconsinit = 0;
348 SET(sc->sc_hwflags, COM_HW_CONSOLE);
349 SET(sc->sc_swflags, COM_SW_SOFTCAR);
350 }
351 }
352
353 int
354 comopen(dev, flag, mode, p)
355 dev_t dev;
356 int flag, mode;
357 struct proc *p;
358 {
359 int unit = COMUNIT(dev);
360 struct com_softc *sc;
361 int iobase;
362 struct tty *tp;
363 int s;
364 int error = 0;
365
366 if (unit >= comcd.cd_ndevs)
367 return ENXIO;
368 sc = comcd.cd_devs[unit];
369 if (!sc)
370 return ENXIO;
371
372 if (!sc->sc_tty)
373 tp = sc->sc_tty = ttymalloc();
374 else
375 tp = sc->sc_tty;
376
377 tp->t_oproc = comstart;
378 tp->t_param = comparam;
379 tp->t_dev = dev;
380 if (!ISSET(tp->t_state, TS_ISOPEN)) {
381 SET(tp->t_state, TS_WOPEN);
382 ttychars(tp);
383 tp->t_iflag = TTYDEF_IFLAG;
384 tp->t_oflag = TTYDEF_OFLAG;
385 tp->t_cflag = TTYDEF_CFLAG;
386 if (ISSET(sc->sc_swflags, COM_SW_CLOCAL))
387 SET(tp->t_cflag, CLOCAL);
388 if (ISSET(sc->sc_swflags, COM_SW_CRTSCTS))
389 SET(tp->t_cflag, CRTSCTS);
390 if (ISSET(sc->sc_swflags, COM_SW_MDMBUF))
391 SET(tp->t_cflag, MDMBUF);
392 tp->t_lflag = TTYDEF_LFLAG;
393 tp->t_ispeed = tp->t_ospeed = comdefaultrate;
394
395 s = spltty();
396
397 comparam(tp, &tp->t_termios);
398 ttsetwater(tp);
399
400 if (comsopen++ == 0)
401 timeout(compoll, NULL, 1);
402
403 sc->sc_ibufp = sc->sc_ibuf = sc->sc_ibufs[0];
404 sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
405 sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
406
407 iobase = sc->sc_iobase;
408 #ifdef COM_HAYESP
409 /* Setup the ESP board */
410 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
411 int hayespbase = sc->sc_hayespbase;
412
413 outb(iobase + com_fifo,
414 FIFO_DMA_MODE|FIFO_ENABLE|
415 FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_8);
416
417 /* Set 16550 compatibility mode */
418 outb(hayespbase + HAYESP_CMD1, HAYESP_SETMODE);
419 outb(hayespbase + HAYESP_CMD2,
420 HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
421 HAYESP_MODE_SCALE);
422
423 /* Set RTS/CTS flow control */
424 outb(hayespbase + HAYESP_CMD1, HAYESP_SETFLOWTYPE);
425 outb(hayespbase + HAYESP_CMD2, HAYESP_FLOW_RTS);
426 outb(hayespbase + HAYESP_CMD2, HAYESP_FLOW_CTS);
427
428 /* Set flow control levels */
429 outb(hayespbase + HAYESP_CMD1, HAYESP_SETRXFLOW);
430 outb(hayespbase + HAYESP_CMD2,
431 HAYESP_HIBYTE(HAYESP_RXHIWMARK));
432 outb(hayespbase + HAYESP_CMD2,
433 HAYESP_LOBYTE(HAYESP_RXHIWMARK));
434 outb(hayespbase + HAYESP_CMD2,
435 HAYESP_HIBYTE(HAYESP_RXLOWMARK));
436 outb(hayespbase + HAYESP_CMD2,
437 HAYESP_LOBYTE(HAYESP_RXLOWMARK));
438 } else
439 #endif
440 if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
441 /* Set the FIFO threshold based on the receive speed. */
442 outb(iobase + com_fifo,
443 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
444 (tp->t_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
445 /* flush any pending I/O */
446 while (ISSET(inb(iobase + com_lsr), LSR_RXRDY))
447 (void) inb(iobase + com_data);
448 /* you turn me on, baby */
449 sc->sc_mcr = MCR_DTR | MCR_RTS;
450 if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
451 SET(sc->sc_mcr, MCR_IENABLE);
452 outb(iobase + com_mcr, sc->sc_mcr);
453 sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
454 outb(iobase + com_ier, sc->sc_ier);
455
456 sc->sc_msr = inb(iobase + com_msr);
457 if (ISSET(sc->sc_swflags, COM_SW_SOFTCAR) ||
458 ISSET(sc->sc_msr, MSR_DCD) || ISSET(tp->t_cflag, MDMBUF))
459 SET(tp->t_state, TS_CARR_ON);
460 else
461 CLR(tp->t_state, TS_CARR_ON);
462 } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
463 return EBUSY;
464 else
465 s = spltty();
466
467 /* wait for carrier if necessary */
468 if (!ISSET(flag, O_NONBLOCK))
469 while (!ISSET(tp->t_cflag, CLOCAL) &&
470 !ISSET(tp->t_state, TS_CARR_ON)) {
471 SET(tp->t_state, TS_WOPEN);
472 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
473 ttopen, 0);
474 if (error) {
475 /* XXX should turn off chip if we're the
476 only waiter */
477 splx(s);
478 return error;
479 }
480 }
481 splx(s);
482
483 return (*linesw[tp->t_line].l_open)(dev, tp);
484 }
485
486 int
487 comclose(dev, flag, mode, p)
488 dev_t dev;
489 int flag, mode;
490 struct proc *p;
491 {
492 int unit = COMUNIT(dev);
493 struct com_softc *sc = comcd.cd_devs[unit];
494 struct tty *tp = sc->sc_tty;
495 int iobase = sc->sc_iobase;
496 int s;
497
498 /* XXX This is for cons.c. */
499 if (!ISSET(tp->t_state, TS_ISOPEN))
500 return 0;
501
502 (*linesw[tp->t_line].l_close)(tp, flag);
503 s = spltty();
504 CLR(sc->sc_lcr, LCR_SBREAK);
505 outb(iobase + com_lcr, sc->sc_lcr);
506 outb(iobase + com_ier, 0);
507 if (ISSET(tp->t_cflag, HUPCL) &&
508 !ISSET(sc->sc_swflags, COM_SW_SOFTCAR)) {
509 /* XXX perhaps only clear DTR */
510 outb(iobase + com_mcr, 0);
511 }
512 CLR(tp->t_state, TS_BUSY | TS_FLUSH);
513 if (--comsopen == 0)
514 untimeout(compoll, NULL);
515 splx(s);
516 ttyclose(tp);
517 #ifdef notyet /* XXXX */
518 if (unit != comconsole) {
519 ttyfree(tp);
520 sc->sc_tty = 0;
521 }
522 #endif
523 return 0;
524 }
525
526 int
527 comread(dev, uio, flag)
528 dev_t dev;
529 struct uio *uio;
530 int flag;
531 {
532 struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
533 struct tty *tp = sc->sc_tty;
534
535 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
536 }
537
538 int
539 comwrite(dev, uio, flag)
540 dev_t dev;
541 struct uio *uio;
542 int flag;
543 {
544 struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
545 struct tty *tp = sc->sc_tty;
546
547 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
548 }
549
550 struct tty *
551 comtty(dev)
552 dev_t dev;
553 {
554 struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
555 struct tty *tp = sc->sc_tty;
556
557 return (tp);
558 }
559
560 static u_char
561 tiocm_xxx2mcr(data)
562 int data;
563 {
564 u_char m = 0;
565
566 if (ISSET(data, TIOCM_DTR))
567 SET(m, MCR_DTR);
568 if (ISSET(data, TIOCM_RTS))
569 SET(m, MCR_RTS);
570 return m;
571 }
572
573 int
574 comioctl(dev, cmd, data, flag, p)
575 dev_t dev;
576 u_long cmd;
577 caddr_t data;
578 int flag;
579 struct proc *p;
580 {
581 int unit = COMUNIT(dev);
582 struct com_softc *sc = comcd.cd_devs[unit];
583 struct tty *tp = sc->sc_tty;
584 int iobase = sc->sc_iobase;
585 int error;
586
587 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
588 if (error >= 0)
589 return error;
590 error = ttioctl(tp, cmd, data, flag, p);
591 if (error >= 0)
592 return error;
593
594 switch (cmd) {
595 case TIOCSBRK:
596 SET(sc->sc_lcr, LCR_SBREAK);
597 outb(iobase + com_lcr, sc->sc_lcr);
598 break;
599 case TIOCCBRK:
600 CLR(sc->sc_lcr, LCR_SBREAK);
601 outb(iobase + com_lcr, sc->sc_lcr);
602 break;
603 case TIOCSDTR:
604 SET(sc->sc_mcr, sc->sc_dtr);
605 outb(iobase + com_mcr, sc->sc_mcr);
606 break;
607 case TIOCCDTR:
608 CLR(sc->sc_mcr, sc->sc_dtr);
609 outb(iobase + com_mcr, sc->sc_mcr);
610 break;
611 case TIOCMSET:
612 CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
613 case TIOCMBIS:
614 SET(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
615 outb(iobase + com_mcr, sc->sc_mcr);
616 break;
617 case TIOCMBIC:
618 CLR(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
619 outb(iobase + com_mcr, sc->sc_mcr);
620 break;
621 case TIOCMGET: {
622 u_char m;
623 int bits = 0;
624
625 m = sc->sc_mcr;
626 if (ISSET(m, MCR_DTR))
627 SET(bits, TIOCM_DTR);
628 if (ISSET(m, MCR_RTS))
629 SET(bits, TIOCM_RTS);
630 m = sc->sc_msr;
631 if (ISSET(m, MSR_DCD))
632 SET(bits, TIOCM_CD);
633 if (ISSET(m, MSR_CTS))
634 SET(bits, TIOCM_CTS);
635 if (ISSET(m, MSR_DSR))
636 SET(bits, TIOCM_DSR);
637 if (ISSET(m, MSR_RI | MSR_TERI))
638 SET(bits, TIOCM_RI);
639 if (inb(iobase + com_ier))
640 SET(bits, TIOCM_LE);
641 *(int *)data = bits;
642 break;
643 }
644 case TIOCGFLAGS: {
645 int driverbits, userbits = 0;
646
647 driverbits = sc->sc_swflags;
648 if (ISSET(driverbits, COM_SW_SOFTCAR))
649 SET(userbits, TIOCFLAG_SOFTCAR);
650 if (ISSET(driverbits, COM_SW_CLOCAL))
651 SET(userbits, TIOCFLAG_CLOCAL);
652 if (ISSET(driverbits, COM_SW_CRTSCTS))
653 SET(userbits, TIOCFLAG_CRTSCTS);
654 if (ISSET(driverbits, COM_SW_MDMBUF))
655 SET(userbits, TIOCFLAG_MDMBUF);
656
657 *(int *)data = userbits;
658 break;
659 }
660 case TIOCSFLAGS: {
661 int userbits, driverbits = 0;
662
663 error = suser(p->p_ucred, &p->p_acflag);
664 if (error != 0)
665 return(EPERM);
666
667 userbits = *(int *)data;
668 if (ISSET(userbits, TIOCFLAG_SOFTCAR) ||
669 ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
670 SET(driverbits, COM_SW_SOFTCAR);
671 if (ISSET(userbits, TIOCFLAG_CLOCAL))
672 SET(driverbits, COM_SW_CLOCAL);
673 if (ISSET(userbits, TIOCFLAG_CRTSCTS))
674 SET(driverbits, COM_SW_CRTSCTS);
675 if (ISSET(userbits, TIOCFLAG_MDMBUF))
676 SET(driverbits, COM_SW_MDMBUF);
677
678 sc->sc_swflags = driverbits;
679 break;
680 }
681 default:
682 return ENOTTY;
683 }
684
685 return 0;
686 }
687
688 int
689 comparam(tp, t)
690 struct tty *tp;
691 struct termios *t;
692 {
693 struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
694 int iobase = sc->sc_iobase;
695 int ospeed = comspeed(t->c_ospeed);
696 u_char lcr;
697 tcflag_t oldcflag;
698 int s;
699
700 /* check requested parameters */
701 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
702 return EINVAL;
703
704 lcr = ISSET(sc->sc_lcr, LCR_SBREAK);
705
706 switch (ISSET(t->c_cflag, CSIZE)) {
707 case CS5:
708 SET(lcr, LCR_5BITS);
709 break;
710 case CS6:
711 SET(lcr, LCR_6BITS);
712 break;
713 case CS7:
714 SET(lcr, LCR_7BITS);
715 break;
716 case CS8:
717 SET(lcr, LCR_8BITS);
718 break;
719 }
720 if (ISSET(t->c_cflag, PARENB)) {
721 SET(lcr, LCR_PENAB);
722 if (!ISSET(t->c_cflag, PARODD))
723 SET(lcr, LCR_PEVEN);
724 }
725 if (ISSET(t->c_cflag, CSTOPB))
726 SET(lcr, LCR_STOPB);
727
728 sc->sc_lcr = lcr;
729
730 s = spltty();
731
732 if (ospeed == 0) {
733 CLR(sc->sc_mcr, MCR_DTR);
734 outb(iobase + com_mcr, sc->sc_mcr);
735 }
736
737 /*
738 * Set the FIFO threshold based on the receive speed, if we are
739 * changing it.
740 */
741 #if 1
742 if (tp->t_ispeed != t->c_ispeed) {
743 #else
744 if (1) {
745 #endif
746 if (ospeed != 0) {
747 /*
748 * Make sure the transmit FIFO is empty before
749 * proceeding. If we don't do this, some revisions
750 * of the UART will hang. Interestingly enough,
751 * even if we do this will the last character is
752 * still being pushed out, they don't hang. This
753 * seems good enough.
754 */
755 while (ISSET(tp->t_state, TS_BUSY)) {
756 int error;
757
758 ++sc->sc_halt;
759 error = ttysleep(tp, &tp->t_outq,
760 TTOPRI | PCATCH, "comprm", 0);
761 --sc->sc_halt;
762 if (error) {
763 splx(s);
764 comstart(tp);
765 return (error);
766 }
767 }
768
769 outb(iobase + com_lcr, lcr | LCR_DLAB);
770 outb(iobase + com_dlbl, ospeed);
771 outb(iobase + com_dlbh, ospeed >> 8);
772 outb(iobase + com_lcr, lcr);
773 SET(sc->sc_mcr, MCR_DTR);
774 outb(iobase + com_mcr, sc->sc_mcr);
775 } else
776 outb(iobase + com_lcr, lcr);
777
778 if (!ISSET(sc->sc_hwflags, COM_HW_HAYESP) &&
779 ISSET(sc->sc_hwflags, COM_HW_FIFO))
780 outb(iobase + com_fifo,
781 FIFO_ENABLE |
782 (t->c_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
783 } else
784 outb(iobase + com_lcr, lcr);
785
786 /* When not using CRTSCTS, RTS follows DTR. */
787 if (!ISSET(t->c_cflag, CRTSCTS)) {
788 if (ISSET(sc->sc_mcr, MCR_DTR)) {
789 if (!ISSET(sc->sc_mcr, MCR_RTS)) {
790 SET(sc->sc_mcr, MCR_RTS);
791 outb(iobase + com_mcr, sc->sc_mcr);
792 }
793 } else {
794 if (ISSET(sc->sc_mcr, MCR_RTS)) {
795 CLR(sc->sc_mcr, MCR_RTS);
796 outb(iobase + com_mcr, sc->sc_mcr);
797 }
798 }
799 sc->sc_dtr = MCR_DTR | MCR_RTS;
800 } else
801 sc->sc_dtr = MCR_DTR;
802
803 /* and copy to tty */
804 tp->t_ispeed = t->c_ispeed;
805 tp->t_ospeed = t->c_ospeed;
806 oldcflag = tp->t_cflag;
807 tp->t_cflag = t->c_cflag;
808
809 /*
810 * If DCD is off and MDMBUF is changed, ask the tty layer if we should
811 * stop the device.
812 */
813 if (!ISSET(sc->sc_msr, MSR_DCD) &&
814 !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
815 ISSET(oldcflag, MDMBUF) != ISSET(tp->t_cflag, MDMBUF) &&
816 (*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
817 CLR(sc->sc_mcr, sc->sc_dtr);
818 outb(iobase + com_mcr, sc->sc_mcr);
819 }
820
821 /* Just to be sure... */
822 splx(s);
823 comstart(tp);
824 return 0;
825 }
826
827 void
828 comstart(tp)
829 struct tty *tp;
830 {
831 struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
832 int iobase = sc->sc_iobase;
833 int s;
834
835 s = spltty();
836 if (ISSET(tp->t_state, TS_BUSY))
837 goto out;
838 if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP) ||
839 sc->sc_halt > 0)
840 goto stopped;
841 if (ISSET(tp->t_cflag, CRTSCTS) && !ISSET(sc->sc_msr, MSR_CTS))
842 goto stopped;
843 if (tp->t_outq.c_cc <= tp->t_lowat) {
844 if (ISSET(tp->t_state, TS_ASLEEP)) {
845 CLR(tp->t_state, TS_ASLEEP);
846 wakeup(&tp->t_outq);
847 }
848 if (tp->t_outq.c_cc == 0)
849 goto stopped;
850 selwakeup(&tp->t_wsel);
851 }
852 SET(tp->t_state, TS_BUSY);
853
854 if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
855 SET(sc->sc_ier, IER_ETXRDY);
856 outb(iobase + com_ier, sc->sc_ier);
857 }
858 #ifdef COM_HAYESP
859 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
860 u_char buffer[1024], *cp = buffer;
861 int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
862 do
863 outb(iobase + com_data, *cp++);
864 while (--n);
865 }
866 else
867 #endif
868 if (ISSET(sc->sc_hwflags, COM_HW_FIFO)) {
869 u_char buffer[16], *cp = buffer;
870 int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
871 do {
872 outb(iobase + com_data, *cp++);
873 } while (--n);
874 } else
875 outb(iobase + com_data, getc(&tp->t_outq));
876 out:
877 splx(s);
878 return;
879 stopped:
880 if (ISSET(sc->sc_ier, IER_ETXRDY)) {
881 CLR(sc->sc_ier, IER_ETXRDY);
882 outb(iobase + com_ier, sc->sc_ier);
883 }
884 splx(s);
885 }
886
887 /*
888 * Stop output on a line.
889 */
890 void
891 comstop(tp, flag)
892 struct tty *tp;
893 {
894 int s;
895
896 s = spltty();
897 if (ISSET(tp->t_state, TS_BUSY))
898 if (!ISSET(tp->t_state, TS_TTSTOP))
899 SET(tp->t_state, TS_FLUSH);
900 splx(s);
901 }
902
903 void
904 comdiag(arg)
905 void *arg;
906 {
907 struct com_softc *sc = arg;
908 int overflows, floods;
909 int s;
910
911 s = spltty();
912 sc->sc_errors = 0;
913 overflows = sc->sc_overflows;
914 sc->sc_overflows = 0;
915 floods = sc->sc_floods;
916 sc->sc_floods = 0;
917 splx(s);
918
919 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf overflow%s\n",
920 sc->sc_dev.dv_xname,
921 overflows, overflows == 1 ? "" : "s",
922 floods, floods == 1 ? "" : "s");
923 }
924
925 void
926 compoll(arg)
927 void *arg;
928 {
929 int unit;
930 struct com_softc *sc;
931 struct tty *tp;
932 register u_char *ibufp;
933 u_char *ibufend;
934 register int c;
935 int s;
936 static int lsrmap[8] = {
937 0, TTY_PE,
938 TTY_FE, TTY_PE|TTY_FE,
939 TTY_FE, TTY_PE|TTY_FE,
940 TTY_FE, TTY_PE|TTY_FE
941 };
942
943 s = spltty();
944 if (comevents == 0) {
945 splx(s);
946 goto out;
947 }
948 comevents = 0;
949 splx(s);
950
951 for (unit = 0; unit < comcd.cd_ndevs; unit++) {
952 sc = comcd.cd_devs[unit];
953 if (sc == 0 || sc->sc_ibufp == sc->sc_ibuf)
954 continue;
955
956 tp = sc->sc_tty;
957
958 s = spltty();
959
960 ibufp = sc->sc_ibuf;
961 ibufend = sc->sc_ibufp;
962
963 if (ibufp == ibufend) {
964 splx(s);
965 continue;
966 }
967
968 sc->sc_ibufp = sc->sc_ibuf = (ibufp == sc->sc_ibufs[0]) ?
969 sc->sc_ibufs[1] : sc->sc_ibufs[0];
970 sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
971 sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
972
973 if (tp == 0 || !ISSET(tp->t_state, TS_ISOPEN)) {
974 splx(s);
975 continue;
976 }
977
978 if (ISSET(tp->t_cflag, CRTSCTS) &&
979 !ISSET(sc->sc_mcr, MCR_RTS)) {
980 /* XXX */
981 SET(sc->sc_mcr, MCR_RTS);
982 outb(sc->sc_iobase + com_mcr, sc->sc_mcr);
983 }
984
985 splx(s);
986
987 while (ibufp < ibufend) {
988 c = *ibufp++;
989 if (*ibufp & LSR_OE) {
990 sc->sc_overflows++;
991 if (sc->sc_errors++ == 0)
992 timeout(comdiag, sc, 60 * hz);
993 }
994 /* This is ugly, but fast. */
995 c |= lsrmap[(*ibufp++ & (LSR_BI|LSR_FE|LSR_PE)) >> 2];
996 (*linesw[tp->t_line].l_rint)(c, tp);
997 }
998 }
999
1000 out:
1001 timeout(compoll, NULL, 1);
1002 }
1003
1004 int
1005 comintr(arg)
1006 void *arg;
1007 {
1008 struct com_softc *sc = arg;
1009 int iobase = sc->sc_iobase;
1010 struct tty *tp;
1011 u_char lsr, data, msr, delta;
1012 #ifdef COM_DEBUG
1013 int n;
1014 struct {
1015 u_char iir, lsr, msr;
1016 } iter[32];
1017 #endif
1018
1019 #ifdef COM_DEBUG
1020 n = 0;
1021 if (ISSET(iter[n].iir = inb(iobase + com_iir), IIR_NOPEND))
1022 return (0);
1023 #else
1024 if (ISSET(inb(iobase + com_iir), IIR_NOPEND))
1025 return (0);
1026 #endif
1027
1028 tp = sc->sc_tty;
1029
1030 for (;;) {
1031 #ifdef COM_DEBUG
1032 iter[n].lsr =
1033 #endif
1034 lsr = inb(iobase + com_lsr);
1035
1036 if (ISSET(lsr, LSR_RXRDY)) {
1037 register u_char *p = sc->sc_ibufp;
1038
1039 comevents = 1;
1040 do {
1041 data = inb(iobase + com_data);
1042 if (ISSET(lsr, LSR_BI)) {
1043 #ifdef notdef
1044 printf("break %02x %02x %02x %02x\n",
1045 sc->sc_msr, sc->sc_mcr, sc->sc_lcr,
1046 sc->sc_dtr);
1047 #endif
1048 #ifdef DDB
1049 if (sc->sc_dev.dv_unit == comconsole) {
1050 Debugger();
1051 goto next;
1052 }
1053 #endif
1054 }
1055 if (p >= sc->sc_ibufend) {
1056 sc->sc_floods++;
1057 if (sc->sc_errors++ == 0)
1058 timeout(comdiag, sc, 60 * hz);
1059 } else {
1060 *p++ = data;
1061 *p++ = lsr;
1062 if (p == sc->sc_ibufhigh &&
1063 ISSET(tp->t_cflag, CRTSCTS)) {
1064 /* XXX */
1065 CLR(sc->sc_mcr, MCR_RTS);
1066 outb(iobase + com_mcr,
1067 sc->sc_mcr);
1068 }
1069 }
1070 next:
1071 #ifdef COM_DEBUG
1072 if (++n >= 32)
1073 goto ohfudge;
1074 iter[n].lsr =
1075 #endif
1076 lsr = inb(iobase + com_lsr);
1077 } while (ISSET(lsr, LSR_RXRDY));
1078
1079 sc->sc_ibufp = p;
1080 }
1081 #ifdef COM_DEBUG
1082 else if (ISSET(lsr, LSR_BI|LSR_FE|LSR_PE|LSR_OE))
1083 printf("weird lsr %02x\n", lsr);
1084 #endif
1085
1086 #ifdef COM_DEBUG
1087 iter[n].msr =
1088 #endif
1089 msr = inb(iobase + com_msr);
1090
1091 if (msr != sc->sc_msr) {
1092 delta = msr ^ sc->sc_msr;
1093 sc->sc_msr = msr;
1094 if (ISSET(delta, MSR_DCD) &&
1095 !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
1096 (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD)) == 0) {
1097 CLR(sc->sc_mcr, sc->sc_dtr);
1098 outb(iobase + com_mcr, sc->sc_mcr);
1099 }
1100 if (ISSET(delta & msr, MSR_CTS) &&
1101 ISSET(tp->t_cflag, CRTSCTS)) {
1102 /* the line is up and we want to do rts/cts flow control */
1103 (*linesw[tp->t_line].l_start)(tp);
1104 }
1105 }
1106
1107 if (ISSET(lsr, LSR_TXRDY) && ISSET(tp->t_state, TS_BUSY)) {
1108 CLR(tp->t_state, TS_BUSY | TS_FLUSH);
1109 if (sc->sc_halt > 0)
1110 wakeup(&tp->t_outq);
1111 (*linesw[tp->t_line].l_start)(tp);
1112 }
1113
1114 #ifdef COM_DEBUG
1115 if (++n >= 32)
1116 goto ohfudge;
1117 if (ISSET(iter[n].iir = inb(iobase + com_iir), IIR_NOPEND))
1118 return (1);
1119 #else
1120 if (ISSET(inb(iobase + com_iir), IIR_NOPEND))
1121 return (1);
1122 #endif
1123 }
1124 #ifdef COM_DEBUG
1125 ohfudge:
1126 printf("comintr: too many iterations");
1127 for (n = 0; n < 32; n++) {
1128 if ((n % 4) == 0)
1129 printf("\ncomintr: iter[%02d]", n);
1130 printf(" %02x %02x %02x", iter[n].iir, iter[n].lsr, iter[n].msr);
1131 }
1132 printf("\n");
1133 printf("comintr: msr %02x mcr %02x lcr %02x ier %02x\n",
1134 sc->sc_msr, sc->sc_mcr, sc->sc_lcr, sc->sc_ier);
1135 printf("comintr: state %08x cc %d\n", sc->sc_tty->t_state,
1136 sc->sc_tty->t_outq.c_cc);
1137 #endif
1138 }
1139
1140 /*
1141 * Following are all routines needed for COM to act as console
1142 */
1143 #include <dev/cons.h>
1144
1145 void
1146 comcnprobe(cp)
1147 struct consdev *cp;
1148 {
1149
1150 if (!comprobe1(CONADDR)) {
1151 cp->cn_pri = CN_DEAD;
1152 return;
1153 }
1154
1155 /* locate the major number */
1156 for (commajor = 0; commajor < nchrdev; commajor++)
1157 if (cdevsw[commajor].d_open == comopen)
1158 break;
1159
1160 /* initialize required fields */
1161 cp->cn_dev = makedev(commajor, CONUNIT);
1162 #ifdef COMCONSOLE
1163 cp->cn_pri = CN_REMOTE; /* Force a serial port console */
1164 #else
1165 cp->cn_pri = CN_NORMAL;
1166 #endif
1167 }
1168
1169 void
1170 comcninit(cp)
1171 struct consdev *cp;
1172 {
1173
1174 cominit(CONUNIT, comdefaultrate);
1175 comconsole = CONUNIT;
1176 comconsinit = 0;
1177 }
1178
1179 cominit(unit, rate)
1180 int unit, rate;
1181 {
1182 int s = splhigh();
1183 int iobase = CONADDR;
1184 u_char stat;
1185
1186 outb(iobase + com_lcr, LCR_DLAB);
1187 rate = comspeed(comdefaultrate);
1188 outb(iobase + com_dlbl, rate);
1189 outb(iobase + com_dlbh, rate >> 8);
1190 outb(iobase + com_lcr, LCR_8BITS);
1191 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
1192 outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
1193 stat = inb(iobase + com_iir);
1194 splx(s);
1195 }
1196
1197 comcngetc(dev)
1198 dev_t dev;
1199 {
1200 int s = splhigh();
1201 int iobase = CONADDR;
1202 u_char stat, c;
1203
1204 while (!ISSET(stat = inb(iobase + com_lsr), LSR_RXRDY))
1205 ;
1206 c = inb(iobase + com_data);
1207 stat = inb(iobase + com_iir);
1208 splx(s);
1209 return c;
1210 }
1211
1212 /*
1213 * Console kernel output character routine.
1214 */
1215 void
1216 comcnputc(dev, c)
1217 dev_t dev;
1218 int c;
1219 {
1220 int s = splhigh();
1221 int iobase = CONADDR;
1222 u_char stat;
1223 register int timo;
1224
1225 #ifdef KGDB
1226 if (dev != kgdb_dev)
1227 #endif
1228 if (comconsinit == 0) {
1229 (void) cominit(COMUNIT(dev), comdefaultrate);
1230 comconsinit = 1;
1231 }
1232 /* wait for any pending transmission to finish */
1233 timo = 50000;
1234 while (!ISSET(stat = inb(iobase + com_lsr), LSR_TXRDY) && --timo)
1235 ;
1236 outb(iobase + com_data, c);
1237 /* wait for this transmission to complete */
1238 timo = 1500000;
1239 while (!ISSET(stat = inb(iobase + com_lsr), LSR_TXRDY) && --timo)
1240 ;
1241 /* clear any interrupts generated by this transmission */
1242 stat = inb(iobase + com_iir);
1243 splx(s);
1244 }
1245
1246 void
1247 comcnpollc(dev, on)
1248 dev_t dev;
1249 int on;
1250 {
1251
1252 }
1253