com.c revision 1.153 1 /* $NetBSD: com.c,v 1.153 1999/02/03 23:57:27 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 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 tiocm_to_com __P((struct com_softc *, int, int));
141 int com_to_tiocm __P((struct com_softc *));
142 void com_iflush __P((struct com_softc *));
143
144 int com_common_getc __P((bus_space_tag_t, bus_space_handle_t));
145 void com_common_putc __P((bus_space_tag_t, bus_space_handle_t, int));
146
147 /* XXX: These belong elsewhere */
148 cdev_decl(com);
149 bdev_decl(com);
150
151 int comcngetc __P((dev_t));
152 void comcnputc __P((dev_t, int));
153 void comcnpollc __P((dev_t, int));
154
155 #define integrate static inline
156 #ifdef __GENERIC_SOFT_INTERRUPTS
157 void comsoft __P((void *));
158 #else
159 #ifndef __NO_SOFT_SERIAL_INTERRUPT
160 void comsoft __P((void));
161 #else
162 void comsoft __P((void *));
163 #endif
164 #endif
165 integrate void com_rxsoft __P((struct com_softc *, struct tty *));
166 integrate void com_txsoft __P((struct com_softc *, struct tty *));
167 integrate void com_stsoft __P((struct com_softc *, struct tty *));
168 integrate void com_schedrx __P((struct com_softc *));
169 void comdiag __P((void *));
170
171 extern struct cfdriver com_cd;
172
173 /*
174 * Make this an option variable one can patch.
175 * But be warned: this must be a power of 2!
176 */
177 u_int com_rbuf_size = COM_RING_SIZE;
178
179 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
180 u_int com_rbuf_hiwat = (COM_RING_SIZE * 1) / 4;
181 u_int com_rbuf_lowat = (COM_RING_SIZE * 3) / 4;
182
183 static int comconsaddr;
184 static bus_space_tag_t comconstag;
185 static bus_space_handle_t comconsioh;
186 static int comconsattached;
187 static int comconsrate;
188 static tcflag_t comconscflag;
189
190 static int ppscap =
191 PPS_TSFMT_TSPEC |
192 PPS_CAPTUREASSERT |
193 PPS_CAPTURECLEAR |
194 #ifdef PPS_SYNC
195 PPS_HARDPPSONASSERT | PPS_HARDPPSONCLEAR |
196 #endif /* PPS_SYNC */
197 PPS_OFFSETASSERT | PPS_OFFSETCLEAR;
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 int
952 comioctl(dev, cmd, data, flag, p)
953 dev_t dev;
954 u_long cmd;
955 caddr_t data;
956 int flag;
957 struct proc *p;
958 {
959 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
960 struct tty *tp = sc->sc_tty;
961 int error;
962 int s;
963
964 if (COM_ISALIVE(sc) == 0)
965 return (EIO);
966
967 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
968 if (error >= 0)
969 return (error);
970
971 error = ttioctl(tp, cmd, data, flag, p);
972 if (error >= 0)
973 return (error);
974
975 error = 0;
976
977 s = splserial();
978
979 switch (cmd) {
980 case TIOCSBRK:
981 com_break(sc, 1);
982 break;
983
984 case TIOCCBRK:
985 com_break(sc, 0);
986 break;
987
988 case TIOCSDTR:
989 com_modem(sc, 1);
990 break;
991
992 case TIOCCDTR:
993 com_modem(sc, 0);
994 break;
995
996 case TIOCGFLAGS:
997 *(int *)data = sc->sc_swflags;
998 break;
999
1000 case TIOCSFLAGS:
1001 error = suser(p->p_ucred, &p->p_acflag);
1002 if (error)
1003 break;
1004 sc->sc_swflags = *(int *)data;
1005 break;
1006
1007 case TIOCMSET:
1008 case TIOCMBIS:
1009 case TIOCMBIC:
1010 tiocm_to_com(sc, cmd, *(int *)data);
1011 break;
1012
1013 case TIOCMGET:
1014 *(int *)data = com_to_tiocm(sc);
1015 break;
1016
1017 case PPS_CREATE:
1018 break;
1019
1020 case PPS_DESTROY:
1021 break;
1022
1023 case PPS_GETPARAMS: {
1024 pps_params_t *pp;
1025 pp = (pps_params_t *)data;
1026 *pp = sc->ppsparam;
1027 break;
1028 }
1029
1030 case PPS_SETPARAMS: {
1031 pps_params_t *pp;
1032 int mode;
1033 pp = (pps_params_t *)data;
1034 if (pp->mode & ~ppscap) {
1035 error = EINVAL;
1036 break;
1037 }
1038 sc->ppsparam = *pp;
1039 /*
1040 * Compute msr masks from user-specified timestamp state.
1041 */
1042 mode = sc->ppsparam.mode;
1043 #ifdef PPS_SYNC
1044 if (mode & PPS_HARDPPSONASSERT) {
1045 mode |= PPS_CAPTUREASSERT;
1046 /* XXX revoke any previous HARDPPS source */
1047 }
1048 if (mode & PPS_HARDPPSONCLEAR) {
1049 mode |= PPS_CAPTURECLEAR;
1050 /* XXX revoke any previous HARDPPS source */
1051 }
1052 #endif /* PPS_SYNC */
1053 switch (mode & PPS_CAPTUREBOTH) {
1054 case 0:
1055 sc->sc_ppsmask = 0;
1056 break;
1057
1058 case PPS_CAPTUREASSERT:
1059 sc->sc_ppsmask = MSR_DCD;
1060 sc->sc_ppsassert = MSR_DCD;
1061 sc->sc_ppsclear = -1;
1062 break;
1063
1064 case PPS_CAPTURECLEAR:
1065 sc->sc_ppsmask = MSR_DCD;
1066 sc->sc_ppsassert = -1;
1067 sc->sc_ppsclear = 0;
1068 break;
1069
1070 case PPS_CAPTUREBOTH:
1071 sc->sc_ppsmask = MSR_DCD;
1072 sc->sc_ppsassert = MSR_DCD;
1073 sc->sc_ppsclear = 0;
1074 break;
1075
1076 default:
1077 error = EINVAL;
1078 break;
1079 }
1080 break;
1081 }
1082
1083 case PPS_GETCAP:
1084 *(int*)data = ppscap;
1085 break;
1086
1087 case PPS_FETCH: {
1088 pps_info_t *pi;
1089 pi = (pps_info_t *)data;
1090 *pi = sc->ppsinfo;
1091 break;
1092 }
1093
1094 case PPS_WAIT:
1095 /* XXX */
1096 error = EOPNOTSUPP;
1097 break;
1098
1099 case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */
1100 /*
1101 * Some GPS clocks models use the falling rather than
1102 * rising edge as the on-the-second signal.
1103 * The old API has no way to specify PPS polarity.
1104 */
1105 sc->sc_ppsmask = MSR_DCD;
1106 #ifndef PPS_TRAILING_EDGE
1107 sc->sc_ppsassert = MSR_DCD;
1108 sc->sc_ppsclear = -1;
1109 TIMESPEC_TO_TIMEVAL((struct timeval *)data,
1110 &sc->ppsinfo.assert_timestamp);
1111 #else
1112 sc->sc_ppsassert = -1
1113 sc->sc_ppsclear = 0;
1114 TIMESPEC_TO_TIMEVAL((struct timeval *)data,
1115 &sc->ppsinfo.clear_timestamp);
1116 #endif
1117 break;
1118
1119 default:
1120 error = ENOTTY;
1121 break;
1122 }
1123
1124 splx(s);
1125
1126 #ifdef COM_DEBUG
1127 if (com_debug)
1128 comstatus(sc, "comioctl ");
1129 #endif
1130
1131 return (error);
1132 }
1133
1134 integrate void
1135 com_schedrx(sc)
1136 struct com_softc *sc;
1137 {
1138
1139 sc->sc_rx_ready = 1;
1140
1141 /* Wake up the poller. */
1142 #ifdef __GENERIC_SOFT_INTERRUPTS
1143 softintr_schedule(sc->sc_si);
1144 #else
1145 #ifndef __NO_SOFT_SERIAL_INTERRUPT
1146 setsoftserial();
1147 #else
1148 if (!com_softintr_scheduled) {
1149 com_softintr_scheduled = 1;
1150 timeout(comsoft, NULL, 1);
1151 }
1152 #endif
1153 #endif
1154 }
1155
1156 void
1157 com_break(sc, onoff)
1158 struct com_softc *sc;
1159 int onoff;
1160 {
1161
1162 if (onoff)
1163 SET(sc->sc_lcr, LCR_SBREAK);
1164 else
1165 CLR(sc->sc_lcr, LCR_SBREAK);
1166
1167 if (!sc->sc_heldchange) {
1168 if (sc->sc_tx_busy) {
1169 sc->sc_heldtbc = sc->sc_tbc;
1170 sc->sc_tbc = 0;
1171 sc->sc_heldchange = 1;
1172 } else
1173 com_loadchannelregs(sc);
1174 }
1175 }
1176
1177 void
1178 com_modem(sc, onoff)
1179 struct com_softc *sc;
1180 int onoff;
1181 {
1182
1183 if (sc->sc_mcr_dtr == 0)
1184 return;
1185
1186 if (onoff)
1187 SET(sc->sc_mcr, sc->sc_mcr_dtr);
1188 else
1189 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1190
1191 if (!sc->sc_heldchange) {
1192 if (sc->sc_tx_busy) {
1193 sc->sc_heldtbc = sc->sc_tbc;
1194 sc->sc_tbc = 0;
1195 sc->sc_heldchange = 1;
1196 } else
1197 com_loadchannelregs(sc);
1198 }
1199 }
1200
1201 void
1202 tiocm_to_com(sc, how, ttybits)
1203 struct com_softc *sc;
1204 int how, ttybits;
1205 {
1206 u_char combits;
1207
1208 combits = 0;
1209 if (ISSET(ttybits, TIOCM_DTR))
1210 SET(combits, MCR_DTR);
1211 if (ISSET(ttybits, TIOCM_RTS))
1212 SET(combits, MCR_RTS);
1213
1214 switch (how) {
1215 case TIOCMBIC:
1216 CLR(sc->sc_mcr, combits);
1217 break;
1218
1219 case TIOCMBIS:
1220 SET(sc->sc_mcr, combits);
1221 break;
1222
1223 case TIOCMSET:
1224 CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
1225 SET(sc->sc_mcr, combits);
1226 break;
1227 }
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 int
1240 com_to_tiocm(sc)
1241 struct com_softc *sc;
1242 {
1243 u_char combits;
1244 int ttybits = 0;
1245
1246 combits = sc->sc_mcr;
1247 if (ISSET(combits, MCR_DTR))
1248 SET(ttybits, TIOCM_DTR);
1249 if (ISSET(combits, MCR_RTS))
1250 SET(ttybits, TIOCM_RTS);
1251
1252 combits = sc->sc_msr;
1253 if (ISSET(combits, MSR_DCD))
1254 SET(ttybits, TIOCM_CD);
1255 if (ISSET(combits, MSR_CTS))
1256 SET(ttybits, TIOCM_CTS);
1257 if (ISSET(combits, MSR_DSR))
1258 SET(ttybits, TIOCM_DSR);
1259 if (ISSET(combits, MSR_RI | MSR_TERI))
1260 SET(ttybits, TIOCM_RI);
1261
1262 if (sc->sc_ier != 0)
1263 SET(ttybits, TIOCM_LE);
1264
1265 return (ttybits);
1266 }
1267
1268 static u_char
1269 cflag2lcr(cflag)
1270 tcflag_t cflag;
1271 {
1272 u_char lcr = 0;
1273
1274 switch (ISSET(cflag, CSIZE)) {
1275 case CS5:
1276 SET(lcr, LCR_5BITS);
1277 break;
1278 case CS6:
1279 SET(lcr, LCR_6BITS);
1280 break;
1281 case CS7:
1282 SET(lcr, LCR_7BITS);
1283 break;
1284 case CS8:
1285 SET(lcr, LCR_8BITS);
1286 break;
1287 }
1288 if (ISSET(cflag, PARENB)) {
1289 SET(lcr, LCR_PENAB);
1290 if (!ISSET(cflag, PARODD))
1291 SET(lcr, LCR_PEVEN);
1292 }
1293 if (ISSET(cflag, CSTOPB))
1294 SET(lcr, LCR_STOPB);
1295
1296 return (lcr);
1297 }
1298
1299 int
1300 comparam(tp, t)
1301 struct tty *tp;
1302 struct termios *t;
1303 {
1304 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1305 int ospeed = comspeed(t->c_ospeed, sc->sc_frequency);
1306 u_char lcr;
1307 int s;
1308
1309 if (COM_ISALIVE(sc) == 0)
1310 return (EIO);
1311
1312 /* Check requested parameters. */
1313 if (ospeed < 0)
1314 return (EINVAL);
1315 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1316 return (EINVAL);
1317
1318 /*
1319 * For the console, always force CLOCAL and !HUPCL, so that the port
1320 * is always active.
1321 */
1322 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
1323 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1324 SET(t->c_cflag, CLOCAL);
1325 CLR(t->c_cflag, HUPCL);
1326 }
1327
1328 /*
1329 * If there were no changes, don't do anything. This avoids dropping
1330 * input and improves performance when all we did was frob things like
1331 * VMIN and VTIME.
1332 */
1333 if (tp->t_ospeed == t->c_ospeed &&
1334 tp->t_cflag == t->c_cflag)
1335 return (0);
1336
1337 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
1338
1339 s = splserial();
1340
1341 sc->sc_lcr = lcr;
1342
1343 /*
1344 * If we're not in a mode that assumes a connection is present, then
1345 * ignore carrier changes.
1346 */
1347 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1348 sc->sc_msr_dcd = 0;
1349 else
1350 sc->sc_msr_dcd = MSR_DCD;
1351 /*
1352 * Set the flow control pins depending on the current flow control
1353 * mode.
1354 */
1355 if (ISSET(t->c_cflag, CRTSCTS)) {
1356 sc->sc_mcr_dtr = MCR_DTR;
1357 sc->sc_mcr_rts = MCR_RTS;
1358 sc->sc_msr_cts = MSR_CTS;
1359 sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
1360 } else if (ISSET(t->c_cflag, MDMBUF)) {
1361 /*
1362 * For DTR/DCD flow control, make sure we don't toggle DTR for
1363 * carrier detection.
1364 */
1365 sc->sc_mcr_dtr = 0;
1366 sc->sc_mcr_rts = MCR_DTR;
1367 sc->sc_msr_cts = MSR_DCD;
1368 sc->sc_efr = 0;
1369 } else {
1370 /*
1371 * If no flow control, then always set RTS. This will make
1372 * the other side happy if it mistakenly thinks we're doing
1373 * RTS/CTS flow control.
1374 */
1375 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
1376 sc->sc_mcr_rts = 0;
1377 sc->sc_msr_cts = 0;
1378 sc->sc_efr = 0;
1379 if (ISSET(sc->sc_mcr, MCR_DTR))
1380 SET(sc->sc_mcr, MCR_RTS);
1381 else
1382 CLR(sc->sc_mcr, MCR_RTS);
1383 }
1384 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
1385
1386 #if 0
1387 if (ospeed == 0)
1388 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1389 else
1390 SET(sc->sc_mcr, sc->sc_mcr_dtr);
1391 #endif
1392
1393 sc->sc_dlbl = ospeed;
1394 sc->sc_dlbh = ospeed >> 8;
1395
1396 /*
1397 * Set the FIFO threshold based on the receive speed.
1398 *
1399 * * If it's a low speed, it's probably a mouse or some other
1400 * interactive device, so set the threshold low.
1401 * * If it's a high speed, trim the trigger level down to prevent
1402 * overflows.
1403 * * Otherwise set it a bit higher.
1404 */
1405 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP))
1406 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
1407 else if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
1408 sc->sc_fifo = FIFO_ENABLE |
1409 (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
1410 t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
1411 else
1412 sc->sc_fifo = 0;
1413
1414 /* And copy to tty. */
1415 tp->t_ispeed = 0;
1416 tp->t_ospeed = t->c_ospeed;
1417 tp->t_cflag = t->c_cflag;
1418
1419 if (!sc->sc_heldchange) {
1420 if (sc->sc_tx_busy) {
1421 sc->sc_heldtbc = sc->sc_tbc;
1422 sc->sc_tbc = 0;
1423 sc->sc_heldchange = 1;
1424 } else
1425 com_loadchannelregs(sc);
1426 }
1427
1428 if (!ISSET(t->c_cflag, CHWFLOW)) {
1429 /* Disable the high water mark. */
1430 sc->sc_r_hiwat = 0;
1431 sc->sc_r_lowat = 0;
1432 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1433 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1434 com_schedrx(sc);
1435 }
1436 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1437 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1438 com_hwiflow(sc);
1439 }
1440 } else {
1441 sc->sc_r_hiwat = com_rbuf_hiwat;
1442 sc->sc_r_lowat = com_rbuf_lowat;
1443 }
1444
1445 splx(s);
1446
1447 /*
1448 * Update the tty layer's idea of the carrier bit, in case we changed
1449 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1450 * explicit request.
1451 */
1452 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
1453
1454 #ifdef COM_DEBUG
1455 if (com_debug)
1456 comstatus(sc, "comparam ");
1457 #endif
1458
1459 if (!ISSET(t->c_cflag, CHWFLOW)) {
1460 if (sc->sc_tx_stopped) {
1461 sc->sc_tx_stopped = 0;
1462 comstart(tp);
1463 }
1464 }
1465
1466 return (0);
1467 }
1468
1469 void
1470 com_iflush(sc)
1471 struct com_softc *sc;
1472 {
1473 bus_space_tag_t iot = sc->sc_iot;
1474 bus_space_handle_t ioh = sc->sc_ioh;
1475 #ifdef DIAGNOSTIC
1476 int reg;
1477 #endif
1478 int timo;
1479
1480 #ifdef DIAGNOSTIC
1481 reg = 0xffff;
1482 #endif
1483 timo = 50000;
1484 /* flush any pending I/O */
1485 while (ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY)
1486 && --timo)
1487 #ifdef DIAGNOSTIC
1488 reg =
1489 #else
1490 (void)
1491 #endif
1492 bus_space_read_1(iot, ioh, com_data);
1493 #ifdef DIAGNOSTIC
1494 if (!timo)
1495 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
1496 reg);
1497 #endif
1498 }
1499
1500 void
1501 com_loadchannelregs(sc)
1502 struct com_softc *sc;
1503 {
1504 bus_space_tag_t iot = sc->sc_iot;
1505 bus_space_handle_t ioh = sc->sc_ioh;
1506
1507 /* XXXXX necessary? */
1508 com_iflush(sc);
1509
1510 bus_space_write_1(iot, ioh, com_ier, 0);
1511
1512 if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
1513 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
1514 bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
1515 }
1516 bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr | LCR_DLAB);
1517 bus_space_write_1(iot, ioh, com_dlbl, sc->sc_dlbl);
1518 bus_space_write_1(iot, ioh, com_dlbh, sc->sc_dlbh);
1519 bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
1520 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active = sc->sc_mcr);
1521 bus_space_write_1(iot, ioh, com_fifo, sc->sc_fifo);
1522
1523 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1524 }
1525
1526 int
1527 comhwiflow(tp, block)
1528 struct tty *tp;
1529 int block;
1530 {
1531 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1532 int s;
1533
1534 if (COM_ISALIVE(sc) == 0)
1535 return (0);
1536
1537 if (sc->sc_mcr_rts == 0)
1538 return (0);
1539
1540 s = splserial();
1541 if (block) {
1542 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1543 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1544 com_hwiflow(sc);
1545 }
1546 } else {
1547 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1548 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1549 com_schedrx(sc);
1550 }
1551 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1552 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1553 com_hwiflow(sc);
1554 }
1555 }
1556 splx(s);
1557 return (1);
1558 }
1559
1560 /*
1561 * (un)block input via hw flowcontrol
1562 */
1563 void
1564 com_hwiflow(sc)
1565 struct com_softc *sc;
1566 {
1567 bus_space_tag_t iot = sc->sc_iot;
1568 bus_space_handle_t ioh = sc->sc_ioh;
1569
1570 if (sc->sc_mcr_rts == 0)
1571 return;
1572
1573 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1574 CLR(sc->sc_mcr, sc->sc_mcr_rts);
1575 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1576 } else {
1577 SET(sc->sc_mcr, sc->sc_mcr_rts);
1578 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1579 }
1580 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
1581 }
1582
1583
1584 void
1585 comstart(tp)
1586 struct tty *tp;
1587 {
1588 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1589 bus_space_tag_t iot = sc->sc_iot;
1590 bus_space_handle_t ioh = sc->sc_ioh;
1591 int s;
1592
1593 if (COM_ISALIVE(sc) == 0)
1594 return;
1595
1596 s = spltty();
1597 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1598 goto out;
1599 if (sc->sc_tx_stopped)
1600 goto out;
1601
1602 if (tp->t_outq.c_cc <= tp->t_lowat) {
1603 if (ISSET(tp->t_state, TS_ASLEEP)) {
1604 CLR(tp->t_state, TS_ASLEEP);
1605 wakeup(&tp->t_outq);
1606 }
1607 selwakeup(&tp->t_wsel);
1608 if (tp->t_outq.c_cc == 0)
1609 goto out;
1610 }
1611
1612 /* Grab the first contiguous region of buffer space. */
1613 {
1614 u_char *tba;
1615 int tbc;
1616
1617 tba = tp->t_outq.c_cf;
1618 tbc = ndqb(&tp->t_outq, 0);
1619
1620 (void)splserial();
1621
1622 sc->sc_tba = tba;
1623 sc->sc_tbc = tbc;
1624 }
1625
1626 SET(tp->t_state, TS_BUSY);
1627 sc->sc_tx_busy = 1;
1628
1629 /* Enable transmit completion interrupts if necessary. */
1630 if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
1631 SET(sc->sc_ier, IER_ETXRDY);
1632 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1633 }
1634
1635 /* Output the first chunk of the contiguous buffer. */
1636 {
1637 int n;
1638
1639 n = sc->sc_tbc;
1640 if (n > sc->sc_fifolen)
1641 n = sc->sc_fifolen;
1642 bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
1643 sc->sc_tbc -= n;
1644 sc->sc_tba += n;
1645 }
1646 out:
1647 splx(s);
1648 return;
1649 }
1650
1651 /*
1652 * Stop output on a line.
1653 */
1654 void
1655 comstop(tp, flag)
1656 struct tty *tp;
1657 int flag;
1658 {
1659 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1660 int s;
1661
1662 s = splserial();
1663 if (ISSET(tp->t_state, TS_BUSY)) {
1664 /* Stop transmitting at the next chunk. */
1665 sc->sc_tbc = 0;
1666 sc->sc_heldtbc = 0;
1667 if (!ISSET(tp->t_state, TS_TTSTOP))
1668 SET(tp->t_state, TS_FLUSH);
1669 }
1670 splx(s);
1671 }
1672
1673 void
1674 comdiag(arg)
1675 void *arg;
1676 {
1677 struct com_softc *sc = arg;
1678 int overflows, floods;
1679 int s;
1680
1681 s = splserial();
1682 overflows = sc->sc_overflows;
1683 sc->sc_overflows = 0;
1684 floods = sc->sc_floods;
1685 sc->sc_floods = 0;
1686 sc->sc_errors = 0;
1687 splx(s);
1688
1689 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1690 sc->sc_dev.dv_xname,
1691 overflows, overflows == 1 ? "" : "s",
1692 floods, floods == 1 ? "" : "s");
1693 }
1694
1695 integrate void
1696 com_rxsoft(sc, tp)
1697 struct com_softc *sc;
1698 struct tty *tp;
1699 {
1700 int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
1701 u_char *get, *end;
1702 u_int cc, scc;
1703 u_char lsr;
1704 int code;
1705 int s;
1706
1707 end = sc->sc_ebuf;
1708 get = sc->sc_rbget;
1709 scc = cc = com_rbuf_size - sc->sc_rbavail;
1710
1711 if (cc == com_rbuf_size) {
1712 sc->sc_floods++;
1713 if (sc->sc_errors++ == 0)
1714 timeout(comdiag, sc, 60 * hz);
1715 }
1716
1717 while (cc) {
1718 code = get[0];
1719 lsr = get[1];
1720 if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
1721 if (ISSET(lsr, LSR_OE)) {
1722 sc->sc_overflows++;
1723 if (sc->sc_errors++ == 0)
1724 timeout(comdiag, sc, 60 * hz);
1725 }
1726 if (ISSET(lsr, LSR_BI | LSR_FE))
1727 SET(code, TTY_FE);
1728 if (ISSET(lsr, LSR_PE))
1729 SET(code, TTY_PE);
1730 }
1731 if ((*rint)(code, tp) == -1) {
1732 /*
1733 * The line discipline's buffer is out of space.
1734 */
1735 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1736 /*
1737 * We're either not using flow control, or the
1738 * line discipline didn't tell us to block for
1739 * some reason. Either way, we have no way to
1740 * know when there's more space available, so
1741 * just drop the rest of the data.
1742 */
1743 get += cc << 1;
1744 if (get >= end)
1745 get -= com_rbuf_size << 1;
1746 cc = 0;
1747 } else {
1748 /*
1749 * Don't schedule any more receive processing
1750 * until the line discipline tells us there's
1751 * space available (through comhwiflow()).
1752 * Leave the rest of the data in the input
1753 * buffer.
1754 */
1755 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1756 }
1757 break;
1758 }
1759 get += 2;
1760 if (get >= end)
1761 get = sc->sc_rbuf;
1762 cc--;
1763 }
1764
1765 if (cc != scc) {
1766 sc->sc_rbget = get;
1767 s = splserial();
1768 cc = sc->sc_rbavail += scc - cc;
1769 /* Buffers should be ok again, release possible block. */
1770 if (cc >= sc->sc_r_lowat) {
1771 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1772 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1773 SET(sc->sc_ier, IER_ERXRDY);
1774 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
1775 }
1776 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1777 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1778 com_hwiflow(sc);
1779 }
1780 }
1781 splx(s);
1782 }
1783 }
1784
1785 integrate void
1786 com_txsoft(sc, tp)
1787 struct com_softc *sc;
1788 struct tty *tp;
1789 {
1790
1791 CLR(tp->t_state, TS_BUSY);
1792 if (ISSET(tp->t_state, TS_FLUSH))
1793 CLR(tp->t_state, TS_FLUSH);
1794 else
1795 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1796 (*linesw[tp->t_line].l_start)(tp);
1797 }
1798
1799 integrate void
1800 com_stsoft(sc, tp)
1801 struct com_softc *sc;
1802 struct tty *tp;
1803 {
1804 u_char msr, delta;
1805 int s;
1806
1807 s = splserial();
1808 msr = sc->sc_msr;
1809 delta = sc->sc_msr_delta;
1810 sc->sc_msr_delta = 0;
1811 splx(s);
1812
1813 if (ISSET(delta, sc->sc_msr_dcd)) {
1814 /*
1815 * Inform the tty layer that carrier detect changed.
1816 */
1817 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD));
1818 }
1819
1820 if (ISSET(delta, sc->sc_msr_cts)) {
1821 /* Block or unblock output according to flow control. */
1822 if (ISSET(msr, sc->sc_msr_cts)) {
1823 sc->sc_tx_stopped = 0;
1824 (*linesw[tp->t_line].l_start)(tp);
1825 } else {
1826 sc->sc_tx_stopped = 1;
1827 }
1828 }
1829
1830 #ifdef COM_DEBUG
1831 if (com_debug)
1832 comstatus(sc, "com_stsoft");
1833 #endif
1834 }
1835
1836 #ifdef __GENERIC_SOFT_INTERRUPTS
1837 void
1838 comsoft(arg)
1839 void *arg;
1840 {
1841 struct com_softc *sc = arg;
1842 struct tty *tp;
1843
1844 if (COM_ISALIVE(sc) == 0)
1845 return;
1846
1847 {
1848 #else
1849 void
1850 #ifndef __NO_SOFT_SERIAL_INTERRUPT
1851 comsoft()
1852 #else
1853 comsoft(arg)
1854 void *arg;
1855 #endif
1856 {
1857 struct com_softc *sc;
1858 struct tty *tp;
1859 int unit;
1860 #ifdef __NO_SOFT_SERIAL_INTERRUPT
1861 int s;
1862
1863 s = splsoftserial();
1864 com_softintr_scheduled = 0;
1865 #endif
1866
1867 for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
1868 sc = com_cd.cd_devs[unit];
1869 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
1870 continue;
1871
1872 if (COM_ISALIVE(sc) == 0)
1873 continue;
1874
1875 tp = sc->sc_tty;
1876 if (tp == NULL)
1877 continue;
1878 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
1879 continue;
1880 #endif
1881 tp = sc->sc_tty;
1882
1883 if (sc->sc_rx_ready) {
1884 sc->sc_rx_ready = 0;
1885 com_rxsoft(sc, tp);
1886 }
1887
1888 if (sc->sc_st_check) {
1889 sc->sc_st_check = 0;
1890 com_stsoft(sc, tp);
1891 }
1892
1893 if (sc->sc_tx_done) {
1894 sc->sc_tx_done = 0;
1895 com_txsoft(sc, tp);
1896 }
1897 }
1898
1899 #ifndef __GENERIC_SOFT_INTERRUPTS
1900 #ifdef __NO_SOFT_SERIAL_INTERRUPT
1901 splx(s);
1902 #endif
1903 #endif
1904 }
1905
1906 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
1907 /* there has got to be a better way to do comsoft() */
1908 }}
1909 #endif
1910
1911 int
1912 comintr(arg)
1913 void *arg;
1914 {
1915 struct com_softc *sc = arg;
1916 bus_space_tag_t iot = sc->sc_iot;
1917 bus_space_handle_t ioh = sc->sc_ioh;
1918 u_char *put, *end;
1919 u_int cc;
1920 u_char lsr, iir;
1921
1922 if (COM_ISALIVE(sc) == 0)
1923 return (0);
1924
1925 iir = bus_space_read_1(iot, ioh, com_iir);
1926 if (ISSET(iir, IIR_NOPEND))
1927 return (0);
1928
1929 end = sc->sc_ebuf;
1930 put = sc->sc_rbput;
1931 cc = sc->sc_rbavail;
1932
1933 do {
1934 u_char msr, delta;
1935
1936 lsr = bus_space_read_1(iot, ioh, com_lsr);
1937 #if defined(DDB) || defined(KGDB)
1938 if (ISSET(lsr, LSR_BI)) {
1939 #ifdef DDB
1940 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1941 console_debugger();
1942 continue;
1943 }
1944 #endif
1945 #ifdef KGDB
1946 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
1947 kgdb_connect(1);
1948 continue;
1949 }
1950 #endif
1951 }
1952 #endif /* DDB || KGDB */
1953
1954 if (ISSET(lsr, LSR_RCV_MASK) &&
1955 !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1956 while (cc > 0) {
1957 put[0] = bus_space_read_1(iot, ioh, com_data);
1958 put[1] = lsr;
1959 put += 2;
1960 if (put >= end)
1961 put = sc->sc_rbuf;
1962 cc--;
1963
1964 lsr = bus_space_read_1(iot, ioh, com_lsr);
1965 if (!ISSET(lsr, LSR_RCV_MASK))
1966 break;
1967 }
1968
1969 /*
1970 * Current string of incoming characters ended because
1971 * no more data was available or we ran out of space.
1972 * Schedule a receive event if any data was received.
1973 * If we're out of space, turn off receive interrupts.
1974 */
1975 sc->sc_rbput = put;
1976 sc->sc_rbavail = cc;
1977 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1978 sc->sc_rx_ready = 1;
1979
1980 /*
1981 * See if we are in danger of overflowing a buffer. If
1982 * so, use hardware flow control to ease the pressure.
1983 */
1984 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1985 cc < sc->sc_r_hiwat) {
1986 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1987 com_hwiflow(sc);
1988 }
1989
1990 /*
1991 * If we're out of space, disable receive interrupts
1992 * until the queue has drained a bit.
1993 */
1994 if (!cc) {
1995 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1996 CLR(sc->sc_ier, IER_ERXRDY);
1997 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1998 }
1999 } else {
2000 if ((iir & IIR_IMASK) == IIR_RXRDY) {
2001 bus_space_write_1(iot, ioh, com_ier, 0);
2002 delay(10);
2003 bus_space_write_1(iot, ioh, com_ier,sc->sc_ier);
2004 iir = IIR_NOPEND;
2005 continue;
2006 }
2007 }
2008
2009 msr = bus_space_read_1(iot, ioh, com_msr);
2010 delta = msr ^ sc->sc_msr;
2011 sc->sc_msr = msr;
2012 if (ISSET(delta, sc->sc_msr_mask)) {
2013 SET(sc->sc_msr_delta, delta);
2014
2015 /*
2016 * Pulse-per-second clock signal on edge of DCD?
2017 */
2018 if (ISSET(delta, sc->sc_ppsmask)) {
2019 struct timeval tv;
2020 if (ISSET(msr, sc->sc_ppsmask) ==
2021 sc->sc_ppsassert) {
2022 /* XXX nanotime() */
2023 microtime(&tv);
2024 TIMEVAL_TO_TIMESPEC(&tv,
2025 &sc->ppsinfo.assert_timestamp);
2026 if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
2027 timespecadd(&sc->ppsinfo.assert_timestamp,
2028 &sc->ppsparam.assert_offset,
2029 &sc->ppsinfo.assert_timestamp);
2030 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp);
2031 }
2032
2033 #ifdef PPS_SYNC
2034 if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
2035 hardpps(&tv, tv.tv_usec);
2036 #endif
2037 sc->ppsinfo.assert_sequence++;
2038 sc->ppsinfo.current_mode =
2039 sc->ppsparam.mode;
2040
2041 } else if (ISSET(msr, sc->sc_ppsmask) ==
2042 sc->sc_ppsclear) {
2043 /* XXX nanotime() */
2044 microtime(&tv);
2045 TIMEVAL_TO_TIMESPEC(&tv,
2046 &sc->ppsinfo.clear_timestamp);
2047 if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
2048 timespecadd(&sc->ppsinfo.clear_timestamp,
2049 &sc->ppsparam.clear_offset,
2050 &sc->ppsinfo.clear_timestamp);
2051 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp);
2052 }
2053
2054 #ifdef PPS_SYNC
2055 if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
2056 hardpps(&tv, tv.tv_usec);
2057 #endif
2058 sc->ppsinfo.clear_sequence++;
2059 sc->ppsinfo.current_mode =
2060 sc->ppsparam.mode;
2061 }
2062 }
2063
2064 /*
2065 * Stop output immediately if we lose the output
2066 * flow control signal or carrier detect.
2067 */
2068 if (ISSET(~msr, sc->sc_msr_mask)) {
2069 sc->sc_tbc = 0;
2070 sc->sc_heldtbc = 0;
2071 #ifdef COM_DEBUG
2072 if (com_debug)
2073 comstatus(sc, "comintr ");
2074 #endif
2075 }
2076
2077 sc->sc_st_check = 1;
2078 }
2079 } while (!ISSET((iir = bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND));
2080
2081 /*
2082 * Done handling any receive interrupts. See if data can be
2083 * transmitted as well. Schedule tx done event if no data left
2084 * and tty was marked busy.
2085 */
2086 if (ISSET(lsr, LSR_TXRDY)) {
2087 /*
2088 * If we've delayed a parameter change, do it now, and restart
2089 * output.
2090 */
2091 if (sc->sc_heldchange) {
2092 com_loadchannelregs(sc);
2093 sc->sc_heldchange = 0;
2094 sc->sc_tbc = sc->sc_heldtbc;
2095 sc->sc_heldtbc = 0;
2096 }
2097
2098 /* Output the next chunk of the contiguous buffer, if any. */
2099 if (sc->sc_tbc > 0) {
2100 int n;
2101
2102 n = sc->sc_tbc;
2103 if (n > sc->sc_fifolen)
2104 n = sc->sc_fifolen;
2105 bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
2106 sc->sc_tbc -= n;
2107 sc->sc_tba += n;
2108 } else {
2109 /* Disable transmit completion interrupts if necessary. */
2110 if (ISSET(sc->sc_ier, IER_ETXRDY)) {
2111 CLR(sc->sc_ier, IER_ETXRDY);
2112 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
2113 }
2114 if (sc->sc_tx_busy) {
2115 sc->sc_tx_busy = 0;
2116 sc->sc_tx_done = 1;
2117 }
2118 }
2119 }
2120
2121 /* Wake up the poller. */
2122 #ifdef __GENERIC_SOFT_INTERRUPTS
2123 softintr_schedule(sc->sc_si);
2124 #else
2125 #ifndef __NO_SOFT_SERIAL_INTERRUPT
2126 setsoftserial();
2127 #else
2128 if (!com_softintr_scheduled) {
2129 com_softintr_scheduled = 1;
2130 timeout(comsoft, NULL, 1);
2131 }
2132 #endif
2133 #endif
2134
2135 #if NRND > 0 && defined(RND_COM)
2136 rnd_add_uint32(&sc->rnd_source, iir | lsr);
2137 #endif
2138
2139 return (1);
2140 }
2141
2142 /*
2143 * The following functions are polled getc and putc routines, shared
2144 * by the console and kgdb glue.
2145 */
2146
2147 int
2148 com_common_getc(iot, ioh)
2149 bus_space_tag_t iot;
2150 bus_space_handle_t ioh;
2151 {
2152 int s = splserial();
2153 u_char stat, c;
2154
2155 /* block until a character becomes available */
2156 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
2157 ;
2158
2159 c = bus_space_read_1(iot, ioh, com_data);
2160 stat = bus_space_read_1(iot, ioh, com_iir);
2161 splx(s);
2162 return (c);
2163 }
2164
2165 void
2166 com_common_putc(iot, ioh, c)
2167 bus_space_tag_t iot;
2168 bus_space_handle_t ioh;
2169 int c;
2170 {
2171 int s = splserial();
2172 u_char stat;
2173 int timo;
2174
2175 /* wait for any pending transmission to finish */
2176 timo = 50000;
2177 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY)
2178 && --timo)
2179 ;
2180
2181 bus_space_write_1(iot, ioh, com_data, c);
2182 /* wait for this transmission to complete */
2183 timo = 1500000;
2184 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY)
2185 && --timo)
2186 ;
2187
2188 /* clear any interrupts generated by this transmission */
2189 stat = bus_space_read_1(iot, ioh, com_iir);
2190 splx(s);
2191 }
2192
2193 /*
2194 * Initialize UART to known state.
2195 */
2196 int
2197 cominit(iot, iobase, rate, frequency, cflag, iohp)
2198 bus_space_tag_t iot;
2199 int iobase;
2200 int rate, frequency;
2201 tcflag_t cflag;
2202 bus_space_handle_t *iohp;
2203 {
2204 bus_space_handle_t ioh;
2205
2206 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
2207 return (ENOMEM); /* ??? */
2208
2209 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
2210 bus_space_write_1(iot, ioh, com_efr, 0);
2211 bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
2212 rate = comspeed(rate, frequency);
2213 bus_space_write_1(iot, ioh, com_dlbl, rate);
2214 bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
2215 bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
2216 bus_space_write_1(iot, ioh, com_mcr, 0);
2217 bus_space_write_1(iot, ioh, com_fifo,
2218 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
2219 bus_space_write_1(iot, ioh, com_ier, 0);
2220
2221 *iohp = ioh;
2222 return (0);
2223 }
2224
2225 /*
2226 * Following are all routines needed for COM to act as console
2227 */
2228
2229 int
2230 comcnattach(iot, iobase, rate, frequency, cflag)
2231 bus_space_tag_t iot;
2232 int iobase;
2233 int rate, frequency;
2234 tcflag_t cflag;
2235 {
2236 int res;
2237 static struct consdev comcons = {
2238 NULL, NULL, comcngetc, comcnputc, comcnpollc, NODEV, CN_NORMAL
2239 };
2240
2241 res = cominit(iot, iobase, rate, frequency, cflag, &comconsioh);
2242 if (res)
2243 return (res);
2244
2245 cn_tab = &comcons;
2246
2247 comconstag = iot;
2248 comconsaddr = iobase;
2249 comconsrate = rate;
2250 comconscflag = cflag;
2251
2252 return (0);
2253 }
2254
2255 int
2256 comcngetc(dev)
2257 dev_t dev;
2258 {
2259
2260 return (com_common_getc(comconstag, comconsioh));
2261 }
2262
2263 /*
2264 * Console kernel output character routine.
2265 */
2266 void
2267 comcnputc(dev, c)
2268 dev_t dev;
2269 int c;
2270 {
2271
2272 com_common_putc(comconstag, comconsioh, c);
2273 }
2274
2275 void
2276 comcnpollc(dev, on)
2277 dev_t dev;
2278 int on;
2279 {
2280
2281 }
2282
2283 #ifdef KGDB
2284 int
2285 com_kgdb_attach(iot, iobase, rate, frequency, cflag)
2286 bus_space_tag_t iot;
2287 int iobase;
2288 int rate, frequency;
2289 tcflag_t cflag;
2290 {
2291 int res;
2292
2293 if (iot == comconstag && iobase == comconsaddr)
2294 return (EBUSY); /* cannot share with console */
2295
2296 res = cominit(iot, iobase, rate, frequency, cflag, &com_kgdb_ioh);
2297 if (res)
2298 return (res);
2299
2300 kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
2301 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
2302
2303 com_kgdb_iot = iot;
2304 com_kgdb_addr = iobase;
2305
2306 return (0);
2307 }
2308
2309 /* ARGSUSED */
2310 int
2311 com_kgdb_getc(arg)
2312 void *arg;
2313 {
2314
2315 return (com_common_getc(com_kgdb_iot, com_kgdb_ioh));
2316 }
2317
2318 /* ARGSUSED */
2319 void
2320 com_kgdb_putc(arg, c)
2321 void *arg;
2322 int c;
2323 {
2324
2325 return (com_common_putc(com_kgdb_iot, com_kgdb_ioh, c));
2326 }
2327 #endif /* KGDB */
2328
2329 /* helper function to identify the com ports used by
2330 console or KGDB (and not yet autoconf attached) */
2331 int
2332 com_is_console(iot, iobase, ioh)
2333 bus_space_tag_t iot;
2334 int iobase;
2335 bus_space_handle_t *ioh;
2336 {
2337 bus_space_handle_t help;
2338
2339 if (!comconsattached &&
2340 iot == comconstag && iobase == comconsaddr)
2341 help = comconsioh;
2342 #ifdef KGDB
2343 else if (!com_kgdb_attached &&
2344 iot == com_kgdb_iot && iobase == com_kgdb_addr)
2345 help = com_kgdb_ioh;
2346 #endif
2347 else
2348 return (0);
2349
2350 if (ioh)
2351 *ioh = help;
2352 return (1);
2353 }
2354