com.c revision 1.136 1 /* $NetBSD: com.c,v 1.136 1998/02/19 09:23:38 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995, 1996, 1997
5 * Charles M. Hannum. All rights reserved.
6 *
7 * Interrupt processing and hardware flow control partly based on code from
8 * Onno van der Linden and Gordon Ross.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Charles M. Hannum.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * Copyright (c) 1991 The Regents of the University of California.
38 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)com.c 7.5 (Berkeley) 5/16/91
69 */
70
71 /*
72 * COM driver, uses National Semiconductor NS16450/NS16550AF UART
73 * Supports automatic hardware flow control on StarTech ST16C650A UART
74 */
75
76 #include "rnd.h"
77 #if NRND > 0 && defined(RND_COM)
78 #include <sys/rnd.h>
79 #endif
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/ioctl.h>
84 #include <sys/select.h>
85 #include <sys/tty.h>
86 #include <sys/proc.h>
87 #include <sys/user.h>
88 #include <sys/conf.h>
89 #include <sys/file.h>
90 #include <sys/uio.h>
91 #include <sys/kernel.h>
92 #include <sys/syslog.h>
93 #include <sys/types.h>
94 #include <sys/device.h>
95 #include <sys/malloc.h>
96
97 #include <machine/intr.h>
98 #include <machine/bus.h>
99
100 #include <dev/ic/comreg.h>
101 #include <dev/ic/comvar.h>
102 #include <dev/ic/ns16550reg.h>
103 #include <dev/ic/st16650reg.h>
104 #ifdef COM_HAYESP
105 #include <dev/ic/hayespreg.h>
106 #endif
107 #define com_lcr com_cfcr
108 #include <dev/cons.h>
109
110 #include "com.h"
111
112 #ifdef COM_HAYESP
113 int comprobeHAYESP __P((bus_space_handle_t hayespioh, struct com_softc *sc));
114 #endif
115
116 #if defined(DDB) || defined(KGDB)
117 static void com_enable_debugport __P((struct com_softc *));
118 #endif
119 void com_attach_subr __P((struct com_softc *sc));
120 void com_config __P((struct com_softc *));
121 int comspeed __P((long, long));
122 static u_char cflag2lcr __P((tcflag_t));
123 int comparam __P((struct tty *, struct termios *));
124 void comstart __P((struct tty *));
125 void comstop __P((struct tty *, int));
126 int comhwiflow __P((struct tty *, int));
127
128 void com_loadchannelregs __P((struct com_softc *));
129 void com_hwiflow __P((struct com_softc *));
130 void com_break __P((struct com_softc *, int));
131 void com_modem __P((struct com_softc *, int));
132 void com_iflush __P((struct com_softc *));
133
134 int com_common_getc __P((bus_space_tag_t, bus_space_handle_t));
135 void com_common_putc __P((bus_space_tag_t, bus_space_handle_t, int));
136
137 /* XXX: These belong elsewhere */
138 cdev_decl(com);
139 bdev_decl(com);
140
141 int comcngetc __P((dev_t));
142 void comcnputc __P((dev_t, int));
143 void comcnpollc __P((dev_t, int));
144
145 #define integrate static inline
146 #ifdef __GENERIC_SOFT_INTERRUPTS
147 void comsoft __P((void *));
148 #else
149 #ifndef __NO_SOFT_SERIAL_INTERRUPT
150 void comsoft __P((void));
151 #else
152 void comsoft __P((void *));
153 #endif
154 #endif
155 integrate void com_rxsoft __P((struct com_softc *, struct tty *));
156 integrate void com_txsoft __P((struct com_softc *, struct tty *));
157 integrate void com_stsoft __P((struct com_softc *, struct tty *));
158 integrate void com_schedrx __P((struct com_softc *));
159 void comdiag __P((void *));
160
161 extern struct cfdriver com_cd;
162
163 /*
164 * Make this an option variable one can patch.
165 * But be warned: this must be a power of 2!
166 */
167 u_int com_rbuf_size = COM_RING_SIZE;
168
169 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
170 u_int com_rbuf_hiwat = (COM_RING_SIZE * 1) / 4;
171 u_int com_rbuf_lowat = (COM_RING_SIZE * 3) / 4;
172
173 static int comconsaddr;
174 static bus_space_tag_t comconstag;
175 static bus_space_handle_t comconsioh;
176 static int comconsattached;
177 static int comconsrate;
178 static tcflag_t comconscflag;
179
180 static u_char tiocm_xxx2mcr __P((int));
181
182 #ifndef __GENERIC_SOFT_INTERRUPTS
183 #ifdef __NO_SOFT_SERIAL_INTERRUPT
184 volatile int com_softintr_scheduled;
185 #endif
186 #endif
187
188 #ifdef KGDB
189 #include <sys/kgdb.h>
190
191 static int com_kgdb_addr;
192 static bus_space_tag_t com_kgdb_iot;
193 static bus_space_handle_t com_kgdb_ioh;
194 static int com_kgdb_attached;
195
196 int com_kgdb_getc __P((void *));
197 void com_kgdb_putc __P((void *, int));
198 #endif /* KGDB */
199
200 #define COMUNIT(x) (minor(x))
201
202 int
203 comspeed(speed, frequency)
204 long speed, frequency;
205 {
206 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
207
208 int x, err;
209
210 #if 0
211 if (speed == 0)
212 return (0);
213 #endif
214 if (speed <= 0)
215 return (-1);
216 x = divrnd(frequency / 16, speed);
217 if (x <= 0)
218 return (-1);
219 err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
220 if (err < 0)
221 err = -err;
222 if (err > COM_TOLERANCE)
223 return (-1);
224 return (x);
225
226 #undef divrnd(n, q)
227 }
228
229 #ifdef COM_DEBUG
230 int com_debug = 0;
231
232 void comstatus __P((struct com_softc *, char *));
233 void
234 comstatus(sc, str)
235 struct com_softc *sc;
236 char *str;
237 {
238 struct tty *tp = sc->sc_tty;
239
240 printf("%s: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
241 sc->sc_dev.dv_xname, str,
242 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
243 ISSET(sc->sc_msr, MSR_DCD) ? "+" : "-",
244 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
245 ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
246 sc->sc_tx_stopped ? "+" : "-");
247
248 printf("%s: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n",
249 sc->sc_dev.dv_xname, str,
250 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
251 ISSET(sc->sc_msr, MSR_CTS) ? "+" : "-",
252 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
253 ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
254 sc->sc_rx_flags);
255 }
256 #endif
257
258 int
259 comprobe1(iot, ioh, iobase)
260 bus_space_tag_t iot;
261 bus_space_handle_t ioh;
262 int iobase;
263 {
264
265 /* force access to id reg */
266 bus_space_write_1(iot, ioh, com_lcr, 0);
267 bus_space_write_1(iot, ioh, com_iir, 0);
268 if (bus_space_read_1(iot, ioh, com_iir) & 0x38)
269 return (0);
270
271 return (1);
272 }
273
274 #ifdef COM_HAYESP
275 int
276 comprobeHAYESP(hayespioh, sc)
277 bus_space_handle_t hayespioh;
278 struct com_softc *sc;
279 {
280 char val, dips;
281 int combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
282 bus_space_tag_t iot = sc->sc_iot;
283
284 /*
285 * Hayes ESP cards have two iobases. One is for compatibility with
286 * 16550 serial chips, and at the same ISA PC base addresses. The
287 * other is for ESP-specific enhanced features, and lies at a
288 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
289 */
290
291 /* Test for ESP signature */
292 if ((bus_space_read_1(iot, hayespioh, 0) & 0xf3) == 0)
293 return (0);
294
295 /*
296 * ESP is present at ESP enhanced base address; unknown com port
297 */
298
299 /* Get the dip-switch configurations */
300 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
301 dips = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1);
302
303 /* Determine which com port this ESP card services: bits 0,1 of */
304 /* dips is the port # (0-3); combaselist[val] is the com_iobase */
305 if (sc->sc_iobase != combaselist[dips & 0x03])
306 return (0);
307
308 printf(": ESP");
309
310 /* Check ESP Self Test bits. */
311 /* Check for ESP version 2.0: bits 4,5,6 == 010 */
312 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
313 val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1); /* Clear reg1 */
314 val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS2);
315 if ((val & 0x70) < 0x20) {
316 printf("-old (%o)", val & 0x70);
317 /* we do not support the necessary features */
318 return (0);
319 }
320
321 /* Check for ability to emulate 16550: bit 8 == 1 */
322 if ((dips & 0x80) == 0) {
323 printf(" slave");
324 /* XXX Does slave really mean no 16550 support?? */
325 return (0);
326 }
327
328 /*
329 * If we made it this far, we are a full-featured ESP v2.0 (or
330 * better), at the correct com port address.
331 */
332
333 SET(sc->sc_hwflags, COM_HW_HAYESP);
334 printf(", 1024 byte fifo\n");
335 return (1);
336 }
337 #endif
338
339 #if defined(DDB) || defined(KGDB)
340 static void
341 com_enable_debugport(sc)
342 struct com_softc *sc;
343 {
344 int s;
345
346 /* Turn on line break interrupt, set carrier. */
347 s = splserial();
348 sc->sc_ier = IER_ERXRDY;
349 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
350 SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
351 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
352 splx(s);
353 }
354 #endif
355
356 void
357 com_attach_subr(sc)
358 struct com_softc *sc;
359 {
360 int iobase = sc->sc_iobase;
361 bus_space_tag_t iot = sc->sc_iot;
362 bus_space_handle_t ioh = sc->sc_ioh;
363 struct tty *tp;
364 #ifdef COM16650
365 u_int8_t lcr;
366 #endif
367 #ifdef COM_HAYESP
368 int hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
369 int *hayespp;
370 #endif
371
372 /* Disable interrupts before configuring the device. */
373 sc->sc_ier = 0;
374 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
375
376 if (iot == comconstag && iobase == comconsaddr) {
377 comconsattached = 1;
378
379 /* Make sure the console is always "hardwired". */
380 delay(1000); /* wait for output to finish */
381 SET(sc->sc_hwflags, COM_HW_CONSOLE);
382 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
383 }
384
385 #ifdef COM_HAYESP
386 /* Look for a Hayes ESP board. */
387 for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
388 bus_space_handle_t hayespioh;
389
390 #define HAYESP_NPORTS 8 /* XXX XXX XXX ??? ??? ??? */
391 if (bus_space_map(iot, *hayespp, HAYESP_NPORTS, 0, &hayespioh))
392 continue;
393 if (comprobeHAYESP(hayespioh, sc)) {
394 sc->sc_hayespioh = hayespioh;
395 sc->sc_fifolen = 1024;
396
397 break;
398 }
399 bus_space_unmap(iot, hayespioh, HAYESP_NPORTS);
400 }
401 /* No ESP; look for other things. */
402 if (!ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
403 #endif
404 sc->sc_fifolen = 1;
405 /* look for a NS 16550AF UART with FIFOs */
406 bus_space_write_1(iot, ioh, com_fifo,
407 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
408 delay(100);
409 if (ISSET(bus_space_read_1(iot, ioh, com_iir), IIR_FIFO_MASK)
410 == IIR_FIFO_MASK)
411 if (ISSET(bus_space_read_1(iot, ioh, com_fifo), FIFO_TRIGGER_14)
412 == FIFO_TRIGGER_14) {
413 SET(sc->sc_hwflags, COM_HW_FIFO);
414
415 #ifdef COM16650
416 /*
417 * IIR changes into the EFR if LCR is set to LCR_EERS
418 * on 16650s. We also know IIR != 0 at this point.
419 * Write 0 into the EFR, and read it. If the result
420 * is 0, we have a 16650.
421 *
422 * Older 16650s were broken; the test to detect them
423 * is taken from the Linux driver. Apparently
424 * setting DLAB enable gives access to the EFR on
425 * these chips.
426 */
427 lcr = bus_space_read_1(iot, ioh, com_lcr);
428 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
429 bus_space_write_1(iot, ioh, com_efr, 0);
430 if (bus_space_read_1(iot, ioh, com_efr) == 0) {
431 bus_space_write_1(iot, ioh, com_lcr,
432 lcr | LCR_DLAB);
433 if (bus_space_read_1(iot, ioh, com_efr) == 0) {
434 CLR(sc->sc_hwflags, COM_HW_FIFO);
435 sc->sc_fifolen = 0;
436 } else {
437 SET(sc->sc_hwflags, COM_HW_FLOW);
438 sc->sc_fifolen = 32;
439 }
440 } else
441 #endif
442 sc->sc_fifolen = 16;
443
444 #ifdef COM16650
445 bus_space_write_1(iot, ioh, com_lcr, lcr);
446 if (sc->sc_fifolen == 0)
447 printf(": st16650, broken fifo\n");
448 else if (sc->sc_fifolen == 32)
449 printf(": st16650a, working fifo\n");
450 else
451 #endif
452 printf(": ns16550a, working fifo\n");
453 } else
454 printf(": ns16550, broken fifo\n");
455 else
456 printf(": ns8250 or ns16450, no fifo\n");
457 bus_space_write_1(iot, ioh, com_fifo, 0);
458 #ifdef COM_HAYESP
459 }
460 #endif
461
462 tp = ttymalloc();
463 tp->t_oproc = comstart;
464 tp->t_param = comparam;
465 tp->t_hwiflow = comhwiflow;
466 tty_attach(tp);
467
468 sc->sc_tty = tp;
469 sc->sc_rbuf = malloc(com_rbuf_size << 1, M_DEVBUF, M_WAITOK);
470 sc->sc_ebuf = sc->sc_rbuf + (com_rbuf_size << 1);
471
472 if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
473 SET(sc->sc_mcr, MCR_IENABLE);
474
475 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
476 int maj;
477
478 /* locate the major number */
479 for (maj = 0; maj < nchrdev; maj++)
480 if (cdevsw[maj].d_open == comopen)
481 break;
482
483 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
484
485 printf("%s: console\n", sc->sc_dev.dv_xname);
486 }
487
488 #ifdef KGDB
489 /*
490 * Allow kgdb to "take over" this port. If this is
491 * the kgdb device, it has exclusive use.
492 */
493 if (iot == com_kgdb_iot && iobase == com_kgdb_addr) {
494 com_kgdb_attached = 1;
495
496 SET(sc->sc_hwflags, COM_HW_KGDB);
497 printf("%s: kgdb\n", sc->sc_dev.dv_xname);
498 }
499 #endif
500
501 #ifdef __GENERIC_SOFT_INTERRUPTS
502 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, comsoft, sc);
503 #endif
504
505 #if NRND > 0 && defined(RND_COM)
506 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
507 RND_TYPE_TTY);
508 #endif
509
510 /* if there are no enable/disable functions, assume the device
511 is always enabled */
512 if (!sc->enable)
513 sc->enabled = 1;
514
515 com_config(sc);
516
517 SET(sc->sc_hwflags, COM_HW_DEV_OK);
518 }
519
520 void
521 com_config(sc)
522 struct com_softc *sc;
523 {
524 bus_space_tag_t iot = sc->sc_iot;
525 bus_space_handle_t ioh = sc->sc_ioh;
526
527 /* Disable interrupts before configuring the device. */
528 sc->sc_ier = 0;
529 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
530
531 #ifdef COM_HAYESP
532 /* Look for a Hayes ESP board. */
533 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
534 sc->sc_fifolen = 1024;
535
536 /* Set 16550 compatibility mode */
537 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
538 HAYESP_SETMODE);
539 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
540 HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
541 HAYESP_MODE_SCALE);
542
543 /* Set RTS/CTS flow control */
544 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
545 HAYESP_SETFLOWTYPE);
546 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
547 HAYESP_FLOW_RTS);
548 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
549 HAYESP_FLOW_CTS);
550
551 /* Set flow control levels */
552 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
553 HAYESP_SETRXFLOW);
554 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
555 HAYESP_HIBYTE(HAYESP_RXHIWMARK));
556 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
557 HAYESP_LOBYTE(HAYESP_RXHIWMARK));
558 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
559 HAYESP_HIBYTE(HAYESP_RXLOWMARK));
560 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
561 HAYESP_LOBYTE(HAYESP_RXLOWMARK));
562 }
563 #endif
564
565 #ifdef DDB
566 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
567 com_enable_debugport(sc);
568 #endif
569
570 #ifdef KGDB
571 /*
572 * Allow kgdb to "take over" this port. If this is
573 * the kgdb device, it has exclusive use.
574 */
575 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
576 com_enable_debugport(sc);
577 #endif
578 }
579
580 int
581 comopen(dev, flag, mode, p)
582 dev_t dev;
583 int flag, mode;
584 struct proc *p;
585 {
586 int unit = COMUNIT(dev);
587 struct com_softc *sc;
588 struct tty *tp;
589 int s, s2;
590 int error = 0;
591
592 if (unit >= com_cd.cd_ndevs)
593 return (ENXIO);
594 sc = com_cd.cd_devs[unit];
595 if (!sc || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
596 return (ENXIO);
597
598 #ifdef KGDB
599 /*
600 * If this is the kgdb port, no other use is permitted.
601 */
602 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
603 return (EBUSY);
604 #endif
605
606 tp = sc->sc_tty;
607
608 if (ISSET(tp->t_state, TS_ISOPEN) &&
609 ISSET(tp->t_state, TS_XCLUDE) &&
610 p->p_ucred->cr_uid != 0)
611 return (EBUSY);
612
613 s = spltty();
614
615 /* We need to set this early for the benefit of comsoft(). */
616 SET(tp->t_state, TS_WOPEN);
617
618 /*
619 * Do the following iff this is a first open.
620 */
621 if (!ISSET(tp->t_state, TS_ISOPEN)) {
622 struct termios t;
623
624 tp->t_dev = dev;
625
626 s2 = splserial();
627
628 if (sc->enable) {
629 if ((*sc->enable)(sc)) {
630 splx(s);
631 printf("%s: device enable failed\n",
632 sc->sc_dev.dv_xname);
633 return (EIO);
634 }
635 sc->enabled = 1;
636 com_config(sc);
637 }
638
639 /* Turn on interrupts. */
640 sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
641 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
642
643 /* Fetch the current modem control status, needed later. */
644 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
645
646 splx(s2);
647
648 /*
649 * Initialize the termios status to the defaults. Add in the
650 * sticky bits from TIOCSFLAGS.
651 */
652 t.c_ispeed = 0;
653 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
654 t.c_ospeed = comconsrate;
655 t.c_cflag = comconscflag;
656 } else {
657 t.c_ospeed = TTYDEF_SPEED;
658 t.c_cflag = TTYDEF_CFLAG;
659 }
660 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
661 SET(t.c_cflag, CLOCAL);
662 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
663 SET(t.c_cflag, CRTSCTS);
664 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
665 SET(t.c_cflag, MDMBUF);
666 /* Make sure comparam() will do something. */
667 tp->t_ospeed = 0;
668 (void) comparam(tp, &t);
669 tp->t_iflag = TTYDEF_IFLAG;
670 tp->t_oflag = TTYDEF_OFLAG;
671 tp->t_lflag = TTYDEF_LFLAG;
672 ttychars(tp);
673 ttsetwater(tp);
674
675 s2 = splserial();
676
677 /*
678 * Turn on DTR. We must always do this, even if carrier is not
679 * present, because otherwise we'd have to use TIOCSDTR
680 * immediately after setting CLOCAL, which applications do not
681 * expect. We always assert DTR while the device is open
682 * unless explicitly requested to deassert it.
683 */
684 com_modem(sc, 1);
685
686 /* Clear the input ring, and unblock. */
687 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
688 sc->sc_rbavail = com_rbuf_size;
689 com_iflush(sc);
690 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
691 com_hwiflow(sc);
692
693 #ifdef COM_DEBUG
694 if (com_debug)
695 comstatus(sc, "comopen ");
696 #endif
697
698 splx(s2);
699 }
700 error = 0;
701
702 /* If we're doing a blocking open... */
703 if (!ISSET(flag, O_NONBLOCK))
704 /* ...then wait for carrier. */
705 while (!ISSET(tp->t_state, TS_CARR_ON) &&
706 !ISSET(tp->t_cflag, CLOCAL | MDMBUF)) {
707 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
708 ttopen, 0);
709 if (error) {
710 /*
711 * If the open was interrupted and nobody
712 * else has the device open, then hang up.
713 */
714 if (!ISSET(tp->t_state, TS_ISOPEN)) {
715 s2 = splserial();
716
717 /* Hang up. */
718 com_modem(sc, 0);
719
720 CLR(tp->t_state, TS_WOPEN);
721 ttwakeup(tp);
722
723 splx(s2);
724 }
725 break;
726 }
727 SET(tp->t_state, TS_WOPEN);
728 }
729
730 splx(s);
731 if (error == 0)
732 error = (*linesw[tp->t_line].l_open)(dev, tp);
733 return (error);
734 }
735
736 int
737 comclose(dev, flag, mode, p)
738 dev_t dev;
739 int flag, mode;
740 struct proc *p;
741 {
742 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
743 struct tty *tp = sc->sc_tty;
744 int s;
745
746 /* XXX This is for cons.c. */
747 if (!ISSET(tp->t_state, TS_ISOPEN))
748 return (0);
749
750 (*linesw[tp->t_line].l_close)(tp, flag);
751 ttyclose(tp);
752
753 s = splserial();
754
755 /* If we were asserting flow control, then deassert it. */
756 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
757 com_hwiflow(sc);
758
759 /* Clear any break condition set with TIOCSBRK. */
760 com_break(sc, 0);
761
762 /*
763 * Hang up if necessary. Wait a bit, so the other side has time to
764 * notice even if we immediately open the port again.
765 */
766 if (ISSET(tp->t_cflag, HUPCL)) {
767 com_modem(sc, 0);
768 (void) tsleep(sc, TTIPRI, ttclos, hz);
769 }
770
771 /* Turn off interrupts. */
772 #ifdef DDB
773 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
774 sc->sc_ier = IER_ERXRDY; /* interrupt on break */
775 else
776 #endif
777 sc->sc_ier = 0;
778 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
779
780 if (sc->disable) {
781 if (sc->enabled) {
782 (*sc->disable)(sc);
783 sc->enabled = 0;
784 }
785 }
786
787 splx(s);
788
789 return (0);
790 }
791
792 int
793 comread(dev, uio, flag)
794 dev_t dev;
795 struct uio *uio;
796 int flag;
797 {
798 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
799 struct tty *tp = sc->sc_tty;
800
801 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
802 }
803
804 int
805 comwrite(dev, uio, flag)
806 dev_t dev;
807 struct uio *uio;
808 int flag;
809 {
810 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
811 struct tty *tp = sc->sc_tty;
812
813 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
814 }
815
816 struct tty *
817 comtty(dev)
818 dev_t dev;
819 {
820 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
821 struct tty *tp = sc->sc_tty;
822
823 return (tp);
824 }
825
826 static u_char
827 tiocm_xxx2mcr(data)
828 int data;
829 {
830 u_char m = 0;
831
832 if (ISSET(data, TIOCM_DTR))
833 SET(m, MCR_DTR);
834 if (ISSET(data, TIOCM_RTS))
835 SET(m, MCR_RTS);
836 return m;
837 }
838
839 int
840 comioctl(dev, cmd, data, flag, p)
841 dev_t dev;
842 u_long cmd;
843 caddr_t data;
844 int flag;
845 struct proc *p;
846 {
847 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
848 struct tty *tp = sc->sc_tty;
849 int error;
850 int s;
851
852 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
853 if (error >= 0)
854 return (error);
855
856 error = ttioctl(tp, cmd, data, flag, p);
857 if (error >= 0)
858 return (error);
859
860 s = splserial();
861
862 switch (cmd) {
863 case TIOCSBRK:
864 com_break(sc, 1);
865 break;
866
867 case TIOCCBRK:
868 com_break(sc, 0);
869 break;
870
871 case TIOCSDTR:
872 com_modem(sc, 1);
873 break;
874
875 case TIOCCDTR:
876 com_modem(sc, 0);
877 break;
878
879 case TIOCGFLAGS:
880 *(int *)data = sc->sc_swflags;
881 break;
882
883 case TIOCSFLAGS:
884 error = suser(p->p_ucred, &p->p_acflag);
885 if (error)
886 break;
887 sc->sc_swflags = *(int *)data;
888 break;
889
890 case TIOCMSET:
891 CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
892 /*FALLTHROUGH*/
893
894 case TIOCMBIS:
895 SET(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
896 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
897 break;
898
899 case TIOCMBIC:
900 CLR(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
901 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
902 break;
903
904 case TIOCMGET: {
905 u_char m;
906 int bits = 0;
907
908 m = sc->sc_mcr;
909 if (ISSET(m, MCR_DTR))
910 SET(bits, TIOCM_DTR);
911 if (ISSET(m, MCR_RTS))
912 SET(bits, TIOCM_RTS);
913 m = sc->sc_msr;
914 if (ISSET(m, MSR_DCD))
915 SET(bits, TIOCM_CD);
916 if (ISSET(m, MSR_CTS))
917 SET(bits, TIOCM_CTS);
918 if (ISSET(m, MSR_DSR))
919 SET(bits, TIOCM_DSR);
920 if (ISSET(m, MSR_RI | MSR_TERI))
921 SET(bits, TIOCM_RI);
922 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_ier))
923 SET(bits, TIOCM_LE);
924 *(int *)data = bits;
925 break;
926 }
927 default:
928 error = ENOTTY;
929 break;
930 }
931
932 splx(s);
933
934 #ifdef COM_DEBUG
935 if (com_debug)
936 comstatus(sc, "comioctl ");
937 #endif
938
939 return (error);
940 }
941
942 integrate void
943 com_schedrx(sc)
944 struct com_softc *sc;
945 {
946
947 sc->sc_rx_ready = 1;
948
949 /* Wake up the poller. */
950 #ifdef __GENERIC_SOFT_INTERRUPTS
951 softintr_schedule(sc->sc_si);
952 #else
953 #ifndef __NO_SOFT_SERIAL_INTERRUPT
954 setsoftserial();
955 #else
956 if (!com_softintr_scheduled) {
957 com_softintr_scheduled = 1;
958 timeout(comsoft, NULL, 1);
959 }
960 #endif
961 #endif
962 }
963
964 void
965 com_break(sc, onoff)
966 struct com_softc *sc;
967 int onoff;
968 {
969
970 if (onoff)
971 SET(sc->sc_lcr, LCR_SBREAK);
972 else
973 CLR(sc->sc_lcr, LCR_SBREAK);
974
975 if (!sc->sc_heldchange) {
976 if (sc->sc_tx_busy) {
977 sc->sc_heldtbc = sc->sc_tbc;
978 sc->sc_tbc = 0;
979 sc->sc_heldchange = 1;
980 } else
981 com_loadchannelregs(sc);
982 }
983 }
984
985 void
986 com_modem(sc, onoff)
987 struct com_softc *sc;
988 int onoff;
989 {
990
991 if (onoff)
992 SET(sc->sc_mcr, sc->sc_mcr_dtr);
993 else
994 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
995
996 if (!sc->sc_heldchange) {
997 if (sc->sc_tx_busy) {
998 sc->sc_heldtbc = sc->sc_tbc;
999 sc->sc_tbc = 0;
1000 sc->sc_heldchange = 1;
1001 } else
1002 com_loadchannelregs(sc);
1003 }
1004 }
1005
1006 static u_char
1007 cflag2lcr(cflag)
1008 tcflag_t cflag;
1009 {
1010 u_char lcr = 0;
1011
1012 switch (ISSET(cflag, CSIZE)) {
1013 case CS5:
1014 SET(lcr, LCR_5BITS);
1015 break;
1016 case CS6:
1017 SET(lcr, LCR_6BITS);
1018 break;
1019 case CS7:
1020 SET(lcr, LCR_7BITS);
1021 break;
1022 case CS8:
1023 SET(lcr, LCR_8BITS);
1024 break;
1025 }
1026 if (ISSET(cflag, PARENB)) {
1027 SET(lcr, LCR_PENAB);
1028 if (!ISSET(cflag, PARODD))
1029 SET(lcr, LCR_PEVEN);
1030 }
1031 if (ISSET(cflag, CSTOPB))
1032 SET(lcr, LCR_STOPB);
1033
1034 return (lcr);
1035 }
1036
1037 int
1038 comparam(tp, t)
1039 struct tty *tp;
1040 struct termios *t;
1041 {
1042 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1043 int ospeed = comspeed(t->c_ospeed, sc->sc_frequency);
1044 u_char lcr;
1045 int s;
1046
1047 /* Check requested parameters. */
1048 if (ospeed < 0)
1049 return (EINVAL);
1050 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1051 return (EINVAL);
1052
1053 /*
1054 * For the console, always force CLOCAL and !HUPCL, so that the port
1055 * is always active.
1056 */
1057 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
1058 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1059 SET(t->c_cflag, CLOCAL);
1060 CLR(t->c_cflag, HUPCL);
1061 }
1062
1063 /*
1064 * If there were no changes, don't do anything. This avoids dropping
1065 * input and improves performance when all we did was frob things like
1066 * VMIN and VTIME.
1067 */
1068 if (tp->t_ospeed == t->c_ospeed &&
1069 tp->t_cflag == t->c_cflag)
1070 return (0);
1071
1072 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
1073
1074 s = splserial();
1075
1076 sc->sc_lcr = lcr;
1077
1078 /*
1079 * If we're not in a mode that assumes a connection is present, then
1080 * ignore carrier changes.
1081 */
1082 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1083 sc->sc_msr_dcd = 0;
1084 else
1085 sc->sc_msr_dcd = MSR_DCD;
1086 /*
1087 * Set the flow control pins depending on the current flow control
1088 * mode.
1089 */
1090 if (ISSET(t->c_cflag, CRTSCTS)) {
1091 sc->sc_mcr_dtr = MCR_DTR;
1092 sc->sc_mcr_rts = MCR_RTS;
1093 sc->sc_msr_cts = MSR_CTS;
1094 sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
1095 } else if (ISSET(t->c_cflag, MDMBUF)) {
1096 /*
1097 * For DTR/DCD flow control, make sure we don't toggle DTR for
1098 * carrier detection.
1099 */
1100 sc->sc_mcr_dtr = 0;
1101 sc->sc_mcr_rts = MCR_DTR;
1102 sc->sc_msr_cts = MSR_DCD;
1103 sc->sc_efr = 0;
1104 } else {
1105 /*
1106 * If no flow control, then always set RTS. This will make
1107 * the other side happy if it mistakenly thinks we're doing
1108 * RTS/CTS flow control.
1109 */
1110 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
1111 sc->sc_mcr_rts = 0;
1112 sc->sc_msr_cts = 0;
1113 sc->sc_efr = 0;
1114 if (ISSET(sc->sc_mcr, MCR_DTR))
1115 SET(sc->sc_mcr, MCR_RTS);
1116 else
1117 CLR(sc->sc_mcr, MCR_RTS);
1118 }
1119 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
1120
1121 #if 0
1122 if (ospeed == 0)
1123 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1124 else
1125 SET(sc->sc_mcr, sc->sc_mcr_dtr);
1126 #endif
1127
1128 sc->sc_dlbl = ospeed;
1129 sc->sc_dlbh = ospeed >> 8;
1130
1131 /*
1132 * Set the FIFO threshold based on the receive speed.
1133 *
1134 * * If it's a low speed, it's probably a mouse or some other
1135 * interactive device, so set the threshold low.
1136 * * If it's a high speed, trim the trigger level down to prevent
1137 * overflows.
1138 * * Otherwise set it a bit higher.
1139 */
1140 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP))
1141 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
1142 else if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
1143 sc->sc_fifo = FIFO_ENABLE |
1144 (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
1145 t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
1146 else
1147 sc->sc_fifo = 0;
1148
1149 /* And copy to tty. */
1150 tp->t_ispeed = 0;
1151 tp->t_ospeed = t->c_ospeed;
1152 tp->t_cflag = t->c_cflag;
1153
1154 if (!sc->sc_heldchange) {
1155 if (sc->sc_tx_busy) {
1156 sc->sc_heldtbc = sc->sc_tbc;
1157 sc->sc_tbc = 0;
1158 sc->sc_heldchange = 1;
1159 } else
1160 com_loadchannelregs(sc);
1161 }
1162
1163 if (!ISSET(t->c_cflag, CHWFLOW)) {
1164 /* Disable the high water mark. */
1165 sc->sc_r_hiwat = 0;
1166 sc->sc_r_lowat = 0;
1167 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1168 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1169 com_schedrx(sc);
1170 }
1171 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1172 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1173 com_hwiflow(sc);
1174 }
1175 } else {
1176 sc->sc_r_hiwat = com_rbuf_hiwat;
1177 sc->sc_r_lowat = com_rbuf_lowat;
1178 }
1179
1180 splx(s);
1181
1182 skip:
1183 /*
1184 * Update the tty layer's idea of the carrier bit, in case we changed
1185 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1186 * explicit request.
1187 */
1188 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
1189
1190 #ifdef COM_DEBUG
1191 if (com_debug)
1192 comstatus(sc, "comparam ");
1193 #endif
1194
1195 if (!ISSET(t->c_cflag, CHWFLOW)) {
1196 if (sc->sc_tx_stopped) {
1197 sc->sc_tx_stopped = 0;
1198 comstart(tp);
1199 }
1200 }
1201
1202 return (0);
1203 }
1204
1205 void
1206 com_iflush(sc)
1207 struct com_softc *sc;
1208 {
1209 bus_space_tag_t iot = sc->sc_iot;
1210 bus_space_handle_t ioh = sc->sc_ioh;
1211 #ifdef DIAGNOSTIC
1212 int reg;
1213 #endif
1214 int timo;
1215
1216 #ifdef DIAGNOSTIC
1217 reg = 0xffff;
1218 #endif
1219 timo = 50000;
1220 /* flush any pending I/O */
1221 while (ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY)
1222 && --timo)
1223 #ifdef DIAGNOSTIC
1224 reg =
1225 #else
1226 (void)
1227 #endif
1228 bus_space_read_1(iot, ioh, com_data);
1229 #ifdef DIAGNOSTIC
1230 if (!timo)
1231 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
1232 reg);
1233 #endif
1234 }
1235
1236 void
1237 com_loadchannelregs(sc)
1238 struct com_softc *sc;
1239 {
1240 bus_space_tag_t iot = sc->sc_iot;
1241 bus_space_handle_t ioh = sc->sc_ioh;
1242
1243 /* XXXXX necessary? */
1244 com_iflush(sc);
1245
1246 bus_space_write_1(iot, ioh, com_ier, 0);
1247
1248 if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
1249 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
1250 bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
1251 }
1252 bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr | LCR_DLAB);
1253 bus_space_write_1(iot, ioh, com_dlbl, sc->sc_dlbl);
1254 bus_space_write_1(iot, ioh, com_dlbh, sc->sc_dlbh);
1255 bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
1256 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active = sc->sc_mcr);
1257 bus_space_write_1(iot, ioh, com_fifo, sc->sc_fifo);
1258
1259 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1260 }
1261
1262 int
1263 comhwiflow(tp, block)
1264 struct tty *tp;
1265 int block;
1266 {
1267 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1268 int s;
1269
1270 if (sc->sc_mcr_rts == 0)
1271 return (0);
1272
1273 s = splserial();
1274 if (block) {
1275 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1276 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1277 com_hwiflow(sc);
1278 }
1279 } else {
1280 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1281 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1282 com_schedrx(sc);
1283 }
1284 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1285 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1286 com_hwiflow(sc);
1287 }
1288 }
1289 splx(s);
1290 return (1);
1291 }
1292
1293 /*
1294 * (un)block input via hw flowcontrol
1295 */
1296 void
1297 com_hwiflow(sc)
1298 struct com_softc *sc;
1299 {
1300 bus_space_tag_t iot = sc->sc_iot;
1301 bus_space_handle_t ioh = sc->sc_ioh;
1302
1303 if (sc->sc_mcr_rts == 0)
1304 return;
1305
1306 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1307 CLR(sc->sc_mcr, sc->sc_mcr_rts);
1308 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1309 } else {
1310 SET(sc->sc_mcr, sc->sc_mcr_rts);
1311 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1312 }
1313 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
1314 }
1315
1316
1317 void
1318 comstart(tp)
1319 struct tty *tp;
1320 {
1321 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1322 bus_space_tag_t iot = sc->sc_iot;
1323 bus_space_handle_t ioh = sc->sc_ioh;
1324 int s;
1325
1326 s = spltty();
1327 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1328 goto out;
1329 if (sc->sc_tx_stopped)
1330 goto out;
1331
1332 if (tp->t_outq.c_cc <= tp->t_lowat) {
1333 if (ISSET(tp->t_state, TS_ASLEEP)) {
1334 CLR(tp->t_state, TS_ASLEEP);
1335 wakeup(&tp->t_outq);
1336 }
1337 selwakeup(&tp->t_wsel);
1338 if (tp->t_outq.c_cc == 0)
1339 goto out;
1340 }
1341
1342 /* Grab the first contiguous region of buffer space. */
1343 {
1344 u_char *tba;
1345 int tbc;
1346
1347 tba = tp->t_outq.c_cf;
1348 tbc = ndqb(&tp->t_outq, 0);
1349
1350 (void)splserial();
1351
1352 sc->sc_tba = tba;
1353 sc->sc_tbc = tbc;
1354 }
1355
1356 SET(tp->t_state, TS_BUSY);
1357 sc->sc_tx_busy = 1;
1358
1359 /* Enable transmit completion interrupts if necessary. */
1360 if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
1361 SET(sc->sc_ier, IER_ETXRDY);
1362 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1363 }
1364
1365 /* Output the first chunk of the contiguous buffer. */
1366 {
1367 int n;
1368
1369 n = sc->sc_tbc;
1370 if (n > sc->sc_fifolen)
1371 n = sc->sc_fifolen;
1372 bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
1373 sc->sc_tbc -= n;
1374 sc->sc_tba += n;
1375 }
1376 out:
1377 splx(s);
1378 return;
1379 }
1380
1381 /*
1382 * Stop output on a line.
1383 */
1384 void
1385 comstop(tp, flag)
1386 struct tty *tp;
1387 int flag;
1388 {
1389 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1390 int s;
1391
1392 s = splserial();
1393 if (ISSET(tp->t_state, TS_BUSY)) {
1394 /* Stop transmitting at the next chunk. */
1395 sc->sc_tbc = 0;
1396 sc->sc_heldtbc = 0;
1397 if (!ISSET(tp->t_state, TS_TTSTOP))
1398 SET(tp->t_state, TS_FLUSH);
1399 }
1400 splx(s);
1401 }
1402
1403 void
1404 comdiag(arg)
1405 void *arg;
1406 {
1407 struct com_softc *sc = arg;
1408 int overflows, floods;
1409 int s;
1410
1411 s = splserial();
1412 overflows = sc->sc_overflows;
1413 sc->sc_overflows = 0;
1414 floods = sc->sc_floods;
1415 sc->sc_floods = 0;
1416 sc->sc_errors = 0;
1417 splx(s);
1418
1419 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1420 sc->sc_dev.dv_xname,
1421 overflows, overflows == 1 ? "" : "s",
1422 floods, floods == 1 ? "" : "s");
1423 }
1424
1425 integrate void
1426 com_rxsoft(sc, tp)
1427 struct com_softc *sc;
1428 struct tty *tp;
1429 {
1430 int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
1431 u_char *get, *end;
1432 u_int cc, scc;
1433 u_char lsr;
1434 int code;
1435 int s;
1436
1437 end = sc->sc_ebuf;
1438 get = sc->sc_rbget;
1439 scc = cc = com_rbuf_size - sc->sc_rbavail;
1440
1441 if (cc == com_rbuf_size) {
1442 sc->sc_floods++;
1443 if (sc->sc_errors++ == 0)
1444 timeout(comdiag, sc, 60 * hz);
1445 }
1446
1447 while (cc) {
1448 code = get[0];
1449 lsr = get[1];
1450 if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
1451 if (ISSET(lsr, LSR_OE)) {
1452 sc->sc_overflows++;
1453 if (sc->sc_errors++ == 0)
1454 timeout(comdiag, sc, 60 * hz);
1455 }
1456 if (ISSET(lsr, LSR_BI | LSR_FE))
1457 SET(code, TTY_FE);
1458 if (ISSET(lsr, LSR_PE))
1459 SET(code, TTY_PE);
1460 }
1461 if ((*rint)(code, tp) == -1) {
1462 /*
1463 * The line discipline's buffer is out of space.
1464 */
1465 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1466 /*
1467 * We're either not using flow control, or the
1468 * line discipline didn't tell us to block for
1469 * some reason. Either way, we have no way to
1470 * know when there's more space available, so
1471 * just drop the rest of the data.
1472 */
1473 get += cc << 1;
1474 if (get >= end)
1475 get -= com_rbuf_size << 1;
1476 cc = 0;
1477 } else {
1478 /*
1479 * Don't schedule any more receive processing
1480 * until the line discipline tells us there's
1481 * space available (through comhwiflow()).
1482 * Leave the rest of the data in the input
1483 * buffer.
1484 */
1485 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1486 }
1487 break;
1488 }
1489 get += 2;
1490 if (get >= end)
1491 get = sc->sc_rbuf;
1492 cc--;
1493 }
1494
1495 if (cc != scc) {
1496 sc->sc_rbget = get;
1497 s = splserial();
1498 cc = sc->sc_rbavail += scc - cc;
1499 /* Buffers should be ok again, release possible block. */
1500 if (cc >= sc->sc_r_lowat) {
1501 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1502 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1503 SET(sc->sc_ier, IER_ERXRDY);
1504 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
1505 }
1506 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1507 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1508 com_hwiflow(sc);
1509 }
1510 }
1511 splx(s);
1512 }
1513 }
1514
1515 integrate void
1516 com_txsoft(sc, tp)
1517 struct com_softc *sc;
1518 struct tty *tp;
1519 {
1520
1521 CLR(tp->t_state, TS_BUSY);
1522 if (ISSET(tp->t_state, TS_FLUSH))
1523 CLR(tp->t_state, TS_FLUSH);
1524 else
1525 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1526 (*linesw[tp->t_line].l_start)(tp);
1527 }
1528
1529 integrate void
1530 com_stsoft(sc, tp)
1531 struct com_softc *sc;
1532 struct tty *tp;
1533 {
1534 u_char msr, delta;
1535 int s;
1536
1537 s = splserial();
1538 msr = sc->sc_msr;
1539 delta = sc->sc_msr_delta;
1540 sc->sc_msr_delta = 0;
1541 splx(s);
1542
1543 if (ISSET(delta, sc->sc_msr_dcd)) {
1544 /*
1545 * Inform the tty layer that carrier detect changed.
1546 */
1547 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD));
1548 }
1549
1550 if (ISSET(delta, sc->sc_msr_cts)) {
1551 /* Block or unblock output according to flow control. */
1552 if (ISSET(msr, sc->sc_msr_cts)) {
1553 sc->sc_tx_stopped = 0;
1554 (*linesw[tp->t_line].l_start)(tp);
1555 } else {
1556 sc->sc_tx_stopped = 1;
1557 }
1558 }
1559
1560 #ifdef COM_DEBUG
1561 if (com_debug)
1562 comstatus(sc, "com_stsoft");
1563 #endif
1564 }
1565
1566 #ifdef __GENERIC_SOFT_INTERRUPTS
1567 void
1568 comsoft(arg)
1569 void *arg;
1570 {
1571 struct com_softc *sc = arg;
1572 struct tty *tp;
1573
1574 if (!sc->enabled)
1575 return;
1576
1577 {
1578 #else
1579 void
1580 #ifndef __NO_SOFT_SERIAL_INTERRUPT
1581 comsoft()
1582 #else
1583 comsoft(arg)
1584 void *arg;
1585 #endif
1586 {
1587 struct com_softc *sc;
1588 struct tty *tp;
1589 int unit;
1590 #ifdef __NO_SOFT_SERIAL_INTERRUPT
1591 int s;
1592
1593 s = splsoftserial();
1594 com_softintr_scheduled = 0;
1595 #endif
1596
1597 for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
1598 sc = com_cd.cd_devs[unit];
1599 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
1600 continue;
1601
1602 if (!sc->enabled)
1603 continue;
1604
1605 tp = sc->sc_tty;
1606 if (tp == NULL || !ISSET(tp->t_state, TS_ISOPEN | TS_WOPEN))
1607 continue;
1608 #endif
1609 tp = sc->sc_tty;
1610
1611 if (sc->sc_rx_ready) {
1612 sc->sc_rx_ready = 0;
1613 com_rxsoft(sc, tp);
1614 }
1615
1616 if (sc->sc_st_check) {
1617 sc->sc_st_check = 0;
1618 com_stsoft(sc, tp);
1619 }
1620
1621 if (sc->sc_tx_done) {
1622 sc->sc_tx_done = 0;
1623 com_txsoft(sc, tp);
1624 }
1625 }
1626
1627 #ifndef __GENERIC_SOFT_INTERRUPTS
1628 #ifdef __NO_SOFT_SERIAL_INTERRUPT
1629 splx(s);
1630 #endif
1631 #endif
1632 }
1633
1634 int
1635 comintr(arg)
1636 void *arg;
1637 {
1638 struct com_softc *sc = arg;
1639 bus_space_tag_t iot = sc->sc_iot;
1640 bus_space_handle_t ioh = sc->sc_ioh;
1641 u_char *put, *end;
1642 u_int cc;
1643 u_char lsr, iir;
1644
1645 if (!sc->enabled)
1646 return (0);
1647
1648 iir = bus_space_read_1(iot, ioh, com_iir);
1649 if (ISSET(iir, IIR_NOPEND))
1650 return (0);
1651
1652 end = sc->sc_ebuf;
1653 put = sc->sc_rbput;
1654 cc = sc->sc_rbavail;
1655
1656 do {
1657 u_char msr, delta;
1658
1659 lsr = bus_space_read_1(iot, ioh, com_lsr);
1660 #if defined(DDB) || defined(KGDB)
1661 if (ISSET(lsr, LSR_BI)) {
1662 #ifdef DDB
1663 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1664 Debugger();
1665 continue;
1666 }
1667 #endif
1668 #ifdef KGDB
1669 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
1670 kgdb_connect(1);
1671 continue;
1672 }
1673 #endif
1674 }
1675 #endif /* DDB || KGDB */
1676
1677 if (ISSET(lsr, LSR_RCV_MASK) &&
1678 !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1679 while (cc > 0) {
1680 put[0] = bus_space_read_1(iot, ioh, com_data);
1681 put[1] = lsr;
1682 put += 2;
1683 if (put >= end)
1684 put = sc->sc_rbuf;
1685 cc--;
1686
1687 lsr = bus_space_read_1(iot, ioh, com_lsr);
1688 if (!ISSET(lsr, LSR_RCV_MASK))
1689 break;
1690 }
1691
1692 /*
1693 * Current string of incoming characters ended because
1694 * no more data was available or we ran out of space.
1695 * Schedule a receive event if any data was received.
1696 * If we're out of space, turn off receive interrupts.
1697 */
1698 sc->sc_rbput = put;
1699 sc->sc_rbavail = cc;
1700 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1701 sc->sc_rx_ready = 1;
1702
1703 /*
1704 * See if we are in danger of overflowing a buffer. If
1705 * so, use hardware flow control to ease the pressure.
1706 */
1707 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1708 cc < sc->sc_r_hiwat) {
1709 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1710 com_hwiflow(sc);
1711 }
1712
1713 /*
1714 * If we're out of space, disable receive interrupts
1715 * until the queue has drained a bit.
1716 */
1717 if (!cc) {
1718 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1719 CLR(sc->sc_ier, IER_ERXRDY);
1720 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1721 }
1722 } else {
1723 if ((iir & IIR_IMASK) == IIR_RXRDY) {
1724 bus_space_write_1(iot, ioh, com_ier, 0);
1725 delay(10);
1726 bus_space_write_1(iot, ioh, com_ier,sc->sc_ier);
1727 iir = IIR_NOPEND;
1728 continue;
1729 }
1730 }
1731
1732 msr = bus_space_read_1(iot, ioh, com_msr);
1733 delta = msr ^ sc->sc_msr;
1734 sc->sc_msr = msr;
1735 if (ISSET(delta, sc->sc_msr_mask)) {
1736 SET(sc->sc_msr_delta, delta);
1737
1738 /*
1739 * Stop output immediately if we lose the output
1740 * flow control signal or carrier detect.
1741 */
1742 if (ISSET(~msr, sc->sc_msr_mask)) {
1743 sc->sc_tbc = 0;
1744 sc->sc_heldtbc = 0;
1745 #ifdef COM_DEBUG
1746 if (com_debug)
1747 comstatus(sc, "comintr ");
1748 #endif
1749 }
1750
1751 sc->sc_st_check = 1;
1752 }
1753 } while (!ISSET((iir = bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND));
1754
1755 /*
1756 * Done handling any receive interrupts. See if data can be
1757 * transmitted as well. Schedule tx done event if no data left
1758 * and tty was marked busy.
1759 */
1760 if (ISSET(lsr, LSR_TXRDY)) {
1761 /*
1762 * If we've delayed a parameter change, do it now, and restart
1763 * output.
1764 */
1765 if (sc->sc_heldchange) {
1766 com_loadchannelregs(sc);
1767 sc->sc_heldchange = 0;
1768 sc->sc_tbc = sc->sc_heldtbc;
1769 sc->sc_heldtbc = 0;
1770 }
1771
1772 /* Output the next chunk of the contiguous buffer, if any. */
1773 if (sc->sc_tbc > 0) {
1774 int n;
1775
1776 n = sc->sc_tbc;
1777 if (n > sc->sc_fifolen)
1778 n = sc->sc_fifolen;
1779 bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
1780 sc->sc_tbc -= n;
1781 sc->sc_tba += n;
1782 } else {
1783 /* Disable transmit completion interrupts if necessary. */
1784 if (ISSET(sc->sc_ier, IER_ETXRDY)) {
1785 CLR(sc->sc_ier, IER_ETXRDY);
1786 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1787 }
1788 if (sc->sc_tx_busy) {
1789 sc->sc_tx_busy = 0;
1790 sc->sc_tx_done = 1;
1791 }
1792 }
1793 }
1794
1795 /* Wake up the poller. */
1796 #ifdef __GENERIC_SOFT_INTERRUPTS
1797 softintr_schedule(sc->sc_si);
1798 #else
1799 #ifndef __NO_SOFT_SERIAL_INTERRUPT
1800 setsoftserial();
1801 #else
1802 if (!com_softintr_scheduled) {
1803 com_softintr_scheduled = 1;
1804 timeout(comsoft, NULL, 1);
1805 }
1806 #endif
1807 #endif
1808
1809 #if NRND > 0 && defined(RND_COM)
1810 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1811 #endif
1812
1813 return (1);
1814 }
1815
1816 /*
1817 * The following functions are polled getc and putc routines, shared
1818 * by the console and kgdb glue.
1819 */
1820
1821 int
1822 com_common_getc(iot, ioh)
1823 bus_space_tag_t iot;
1824 bus_space_handle_t ioh;
1825 {
1826 int s = splserial();
1827 u_char stat, c;
1828
1829 /* block until a character becomes available */
1830 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
1831 ;
1832
1833 c = bus_space_read_1(iot, ioh, com_data);
1834 stat = bus_space_read_1(iot, ioh, com_iir);
1835 splx(s);
1836 return (c);
1837 }
1838
1839 void
1840 com_common_putc(iot, ioh, c)
1841 bus_space_tag_t iot;
1842 bus_space_handle_t ioh;
1843 int c;
1844 {
1845 int s = splserial();
1846 u_char stat;
1847 int timo;
1848
1849 /* wait for any pending transmission to finish */
1850 timo = 50000;
1851 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY)
1852 && --timo)
1853 ;
1854
1855 bus_space_write_1(iot, ioh, com_data, c);
1856 /* wait for this transmission to complete */
1857 timo = 1500000;
1858 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY)
1859 && --timo)
1860 ;
1861
1862 /* clear any interrupts generated by this transmission */
1863 stat = bus_space_read_1(iot, ioh, com_iir);
1864 splx(s);
1865 }
1866
1867 /*
1868 * Initialize UART to known state.
1869 */
1870 int
1871 cominit(iot, iobase, rate, frequency, cflag, iohp)
1872 bus_space_tag_t iot;
1873 int iobase;
1874 int rate, frequency;
1875 tcflag_t cflag;
1876 bus_space_handle_t *iohp;
1877 {
1878 bus_space_handle_t ioh;
1879
1880 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
1881 return (ENOMEM); /* ??? */
1882
1883 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
1884 bus_space_write_1(iot, ioh, com_efr, 0);
1885 bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
1886 rate = comspeed(rate, frequency);
1887 bus_space_write_1(iot, ioh, com_dlbl, rate);
1888 bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
1889 bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
1890 bus_space_write_1(iot, ioh, com_mcr, 0);
1891 bus_space_write_1(iot, ioh, com_fifo,
1892 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
1893 bus_space_write_1(iot, ioh, com_ier, 0);
1894
1895 *iohp = ioh;
1896 return (0);
1897 }
1898
1899 /*
1900 * Following are all routines needed for COM to act as console
1901 */
1902
1903 int
1904 comcnattach(iot, iobase, rate, frequency, cflag)
1905 bus_space_tag_t iot;
1906 int iobase;
1907 int rate, frequency;
1908 tcflag_t cflag;
1909 {
1910 int res;
1911 static struct consdev comcons = {
1912 NULL, NULL, comcngetc, comcnputc, comcnpollc, NODEV, CN_NORMAL
1913 };
1914
1915 res = cominit(iot, iobase, rate, frequency, cflag, &comconsioh);
1916 if (res)
1917 return (res);
1918
1919 cn_tab = &comcons;
1920
1921 comconstag = iot;
1922 comconsaddr = iobase;
1923 comconsrate = rate;
1924 comconscflag = cflag;
1925
1926 return (0);
1927 }
1928
1929 int
1930 comcngetc(dev)
1931 dev_t dev;
1932 {
1933
1934 return (com_common_getc(comconstag, comconsioh));
1935 }
1936
1937 /*
1938 * Console kernel output character routine.
1939 */
1940 void
1941 comcnputc(dev, c)
1942 dev_t dev;
1943 int c;
1944 {
1945
1946 com_common_putc(comconstag, comconsioh, c);
1947 }
1948
1949 void
1950 comcnpollc(dev, on)
1951 dev_t dev;
1952 int on;
1953 {
1954
1955 }
1956
1957 #ifdef KGDB
1958 int
1959 com_kgdb_attach(iot, iobase, rate, frequency, cflag)
1960 bus_space_tag_t iot;
1961 int iobase;
1962 int rate, frequency;
1963 tcflag_t cflag;
1964 {
1965 int res;
1966
1967 if (iot == comconstag && iobase == comconsaddr)
1968 return (EBUSY); /* cannot share with console */
1969
1970 res = cominit(iot, iobase, rate, frequency, cflag, &com_kgdb_ioh);
1971 if (res)
1972 return (res);
1973
1974 kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
1975 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1976
1977 com_kgdb_iot = iot;
1978 com_kgdb_addr = iobase;
1979
1980 return (0);
1981 }
1982
1983 /* ARGSUSED */
1984 int
1985 com_kgdb_getc(arg)
1986 void *arg;
1987 {
1988
1989 return (com_common_getc(com_kgdb_iot, com_kgdb_ioh));
1990 }
1991
1992 /* ARGSUSED */
1993 void
1994 com_kgdb_putc(arg, c)
1995 void *arg;
1996 int c;
1997 {
1998
1999 return (com_common_putc(com_kgdb_iot, com_kgdb_ioh, c));
2000 }
2001 #endif /* KGDB */
2002
2003 /* helper function to identify the com ports used by
2004 console or KGDB (and not yet autoconf attached) */
2005 int
2006 com_is_console(iot, iobase, ioh)
2007 bus_space_tag_t iot;
2008 int iobase;
2009 bus_space_handle_t *ioh;
2010 {
2011 bus_space_handle_t help;
2012
2013 if (!comconsattached &&
2014 iot == comconstag && iobase == comconsaddr)
2015 help = comconsioh;
2016 #ifdef KGDB
2017 else if (!com_kgdb_attached &&
2018 iot == com_kgdb_iot && iobase == com_kgdb_addr)
2019 help = com_kgdb_ioh;
2020 #endif
2021 else
2022 return (0);
2023
2024 if (ioh)
2025 *ioh = help;
2026 return (1);
2027 }
2028