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