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