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