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