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