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