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