xref: /freebsd/sys/dev/uart/uart_core.c (revision 76bfa33f259891a8b9108e20141bd18e2c8d312f)
1098ca2bdSWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
427d5dc18SMarcel Moolenaar  * Copyright (c) 2003 Marcel Moolenaar
527d5dc18SMarcel Moolenaar  * All rights reserved.
627d5dc18SMarcel Moolenaar  *
727d5dc18SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
827d5dc18SMarcel Moolenaar  * modification, are permitted provided that the following conditions
927d5dc18SMarcel Moolenaar  * are met:
1027d5dc18SMarcel Moolenaar  *
1127d5dc18SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
1227d5dc18SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
1327d5dc18SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
1427d5dc18SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
1527d5dc18SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
1627d5dc18SMarcel Moolenaar  *
1727d5dc18SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1827d5dc18SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1927d5dc18SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2027d5dc18SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2127d5dc18SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2227d5dc18SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2327d5dc18SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2427d5dc18SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2527d5dc18SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2627d5dc18SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727d5dc18SMarcel Moolenaar  */
2827d5dc18SMarcel Moolenaar 
2927d5dc18SMarcel Moolenaar #include <sys/param.h>
3027d5dc18SMarcel Moolenaar #include <sys/systm.h>
3127d5dc18SMarcel Moolenaar #include <sys/bus.h>
3227d5dc18SMarcel Moolenaar #include <sys/conf.h>
3327d5dc18SMarcel Moolenaar #include <sys/cons.h>
3427d5dc18SMarcel Moolenaar #include <sys/fcntl.h>
3527d5dc18SMarcel Moolenaar #include <sys/interrupt.h>
36591bc192SMarcel Moolenaar #include <sys/kdb.h>
3727d5dc18SMarcel Moolenaar #include <sys/kernel.h>
3827d5dc18SMarcel Moolenaar #include <sys/malloc.h>
3927d5dc18SMarcel Moolenaar #include <sys/queue.h>
4027d5dc18SMarcel Moolenaar #include <sys/reboot.h>
4162145ff3SNeel Natu #include <sys/sysctl.h>
4227d5dc18SMarcel Moolenaar #include <machine/bus.h>
4327d5dc18SMarcel Moolenaar #include <sys/rman.h>
4427d5dc18SMarcel Moolenaar #include <machine/resource.h>
4527d5dc18SMarcel Moolenaar #include <machine/stdarg.h>
4627d5dc18SMarcel Moolenaar 
4727d5dc18SMarcel Moolenaar #include <dev/uart/uart.h>
4827d5dc18SMarcel Moolenaar #include <dev/uart/uart_bus.h>
4927d5dc18SMarcel Moolenaar #include <dev/uart/uart_cpu.h>
50fdfbb3f5SIan Lepore #include <dev/uart/uart_ppstypes.h>
5127d5dc18SMarcel Moolenaar 
5227d5dc18SMarcel Moolenaar #include "uart_if.h"
5327d5dc18SMarcel Moolenaar 
5486fb5400SMarius Strobl const char uart_driver_name[] = "uart";
5527d5dc18SMarcel Moolenaar 
5627d5dc18SMarcel Moolenaar SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs =
5727d5dc18SMarcel Moolenaar     SLIST_HEAD_INITIALIZER(uart_sysdevs);
5827d5dc18SMarcel Moolenaar 
59d745c852SEd Schouten static MALLOC_DEFINE(M_UART, "UART", "UART driver");
6027d5dc18SMarcel Moolenaar 
61332cda07SPeter Grehan #ifndef	UART_POLL_FREQ
62332cda07SPeter Grehan #define	UART_POLL_FREQ		50
63332cda07SPeter Grehan #endif
64332cda07SPeter Grehan static int uart_poll_freq = UART_POLL_FREQ;
6562145ff3SNeel Natu SYSCTL_INT(_debug, OID_AUTO, uart_poll_freq, CTLFLAG_RDTUN, &uart_poll_freq,
6662145ff3SNeel Natu     0, "UART poll frequency");
6762145ff3SNeel Natu 
6862145ff3SNeel Natu static int uart_force_poll;
6962145ff3SNeel Natu SYSCTL_INT(_debug, OID_AUTO, uart_force_poll, CTLFLAG_RDTUN, &uart_force_poll,
7062145ff3SNeel Natu     0, "Force UART polling");
71332cda07SPeter Grehan 
72196d3019SIan Lepore static inline int
uart_pps_mode_valid(int pps_mode)73196d3019SIan Lepore uart_pps_mode_valid(int pps_mode)
74196d3019SIan Lepore {
75fdfbb3f5SIan Lepore 	int opt;
76196d3019SIan Lepore 
77fdfbb3f5SIan Lepore 	switch(pps_mode & UART_PPS_SIGNAL_MASK) {
78fdfbb3f5SIan Lepore 	case UART_PPS_DISABLED:
79fdfbb3f5SIan Lepore 	case UART_PPS_CTS:
80fdfbb3f5SIan Lepore 	case UART_PPS_DCD:
81fdfbb3f5SIan Lepore 		break;
82fdfbb3f5SIan Lepore 	default:
83196d3019SIan Lepore 		return (false);
84196d3019SIan Lepore 	}
85196d3019SIan Lepore 
86fdfbb3f5SIan Lepore 	opt = pps_mode & UART_PPS_OPTION_MASK;
87fdfbb3f5SIan Lepore 	if ((opt & ~(UART_PPS_INVERT_PULSE | UART_PPS_NARROW_PULSE)) != 0)
88fdfbb3f5SIan Lepore 		return (false);
89fdfbb3f5SIan Lepore 
90fdfbb3f5SIan Lepore 	return (true);
91196d3019SIan Lepore }
92fdfbb3f5SIan Lepore 
93fdfbb3f5SIan Lepore static void
uart_pps_print_mode(struct uart_softc * sc)94fdfbb3f5SIan Lepore uart_pps_print_mode(struct uart_softc *sc)
95fdfbb3f5SIan Lepore {
96fdfbb3f5SIan Lepore 
97fdfbb3f5SIan Lepore 	device_printf(sc->sc_dev, "PPS capture mode: ");
987bd8311dSJustin Hibbits 	switch(sc->sc_pps_mode & UART_PPS_SIGNAL_MASK) {
99fdfbb3f5SIan Lepore 	case UART_PPS_DISABLED:
100fdfbb3f5SIan Lepore 		printf("disabled");
1017bd8311dSJustin Hibbits 		break;
102fdfbb3f5SIan Lepore 	case UART_PPS_CTS:
103fdfbb3f5SIan Lepore 		printf("CTS");
1047bd8311dSJustin Hibbits 		break;
105fdfbb3f5SIan Lepore 	case UART_PPS_DCD:
106fdfbb3f5SIan Lepore 		printf("DCD");
1077bd8311dSJustin Hibbits 		break;
108fdfbb3f5SIan Lepore 	default:
109fdfbb3f5SIan Lepore 		printf("invalid");
1107bd8311dSJustin Hibbits 		break;
111fdfbb3f5SIan Lepore 	}
112fdfbb3f5SIan Lepore 	if (sc->sc_pps_mode & UART_PPS_INVERT_PULSE)
113fdfbb3f5SIan Lepore 		printf("-Inverted");
114fdfbb3f5SIan Lepore 	if (sc->sc_pps_mode & UART_PPS_NARROW_PULSE)
115fdfbb3f5SIan Lepore 		printf("-NarrowPulse");
116fdfbb3f5SIan Lepore 	printf("\n");
117196d3019SIan Lepore }
118196d3019SIan Lepore 
119196d3019SIan Lepore static int
uart_pps_mode_sysctl(SYSCTL_HANDLER_ARGS)120196d3019SIan Lepore uart_pps_mode_sysctl(SYSCTL_HANDLER_ARGS)
121196d3019SIan Lepore {
122196d3019SIan Lepore 	struct uart_softc *sc;
123196d3019SIan Lepore 	int err, tmp;
124196d3019SIan Lepore 
125196d3019SIan Lepore 	sc = arg1;
126196d3019SIan Lepore 	tmp = sc->sc_pps_mode;
127196d3019SIan Lepore 	err = sysctl_handle_int(oidp, &tmp, 0, req);
128196d3019SIan Lepore 	if (err != 0 || req->newptr == NULL)
129196d3019SIan Lepore 		return (err);
130196d3019SIan Lepore 	if (!uart_pps_mode_valid(tmp))
131196d3019SIan Lepore 		return (EINVAL);
132196d3019SIan Lepore 	sc->sc_pps_mode = tmp;
133196d3019SIan Lepore 	return(0);
134196d3019SIan Lepore }
135196d3019SIan Lepore 
136196d3019SIan Lepore static void
uart_pps_process(struct uart_softc * sc,int ser_sig)137fdfbb3f5SIan Lepore uart_pps_process(struct uart_softc *sc, int ser_sig)
138fdfbb3f5SIan Lepore {
139fdfbb3f5SIan Lepore 	sbintime_t now;
140fdfbb3f5SIan Lepore 	int is_assert, pps_sig;
141fdfbb3f5SIan Lepore 
142fdfbb3f5SIan Lepore 	/* Which signal is configured as PPS?  Early out if none. */
143fdfbb3f5SIan Lepore 	switch(sc->sc_pps_mode & UART_PPS_SIGNAL_MASK) {
144fdfbb3f5SIan Lepore 	case UART_PPS_CTS:
145fdfbb3f5SIan Lepore 		pps_sig = SER_CTS;
146fdfbb3f5SIan Lepore 		break;
147fdfbb3f5SIan Lepore 	case UART_PPS_DCD:
148fdfbb3f5SIan Lepore 		pps_sig = SER_DCD;
149fdfbb3f5SIan Lepore 		break;
150fdfbb3f5SIan Lepore 	default:
151fdfbb3f5SIan Lepore 		return;
152fdfbb3f5SIan Lepore 	}
153fdfbb3f5SIan Lepore 
154fdfbb3f5SIan Lepore 	/* Early out if there is no change in the signal configured as PPS. */
155fdfbb3f5SIan Lepore 	if ((ser_sig & SER_DELTA(pps_sig)) == 0)
156fdfbb3f5SIan Lepore 		return;
157fdfbb3f5SIan Lepore 
158fdfbb3f5SIan Lepore 	/*
159fdfbb3f5SIan Lepore 	 * In narrow-pulse mode we need to synthesize both capture and clear
160fdfbb3f5SIan Lepore 	 * events from a single "delta occurred" indication from the uart
161fdfbb3f5SIan Lepore 	 * hardware because the pulse width is too narrow to reliably detect
162fdfbb3f5SIan Lepore 	 * both edges.  However, when the pulse width is close to our interrupt
163fdfbb3f5SIan Lepore 	 * processing latency we might intermittantly catch both edges.  To
164fdfbb3f5SIan Lepore 	 * guard against generating spurious events when that happens, we use a
165fdfbb3f5SIan Lepore 	 * separate timer to ensure at least half a second elapses before we
166fdfbb3f5SIan Lepore 	 * generate another event.
167fdfbb3f5SIan Lepore 	 */
168fdfbb3f5SIan Lepore 	pps_capture(&sc->sc_pps);
169fdfbb3f5SIan Lepore 	if (sc->sc_pps_mode & UART_PPS_NARROW_PULSE) {
170fdfbb3f5SIan Lepore 		now = getsbinuptime();
171fdfbb3f5SIan Lepore 		if (now > sc->sc_pps_captime + 500 * SBT_1MS) {
172fdfbb3f5SIan Lepore 			sc->sc_pps_captime = now;
173fdfbb3f5SIan Lepore 			pps_event(&sc->sc_pps, PPS_CAPTUREASSERT);
174fdfbb3f5SIan Lepore 			pps_event(&sc->sc_pps, PPS_CAPTURECLEAR);
175fdfbb3f5SIan Lepore 		}
176fdfbb3f5SIan Lepore 	} else  {
177fdfbb3f5SIan Lepore 		is_assert = ser_sig & pps_sig;
178fdfbb3f5SIan Lepore 		if (sc->sc_pps_mode & UART_PPS_INVERT_PULSE)
179fdfbb3f5SIan Lepore 			is_assert = !is_assert;
180fdfbb3f5SIan Lepore 		pps_event(&sc->sc_pps, is_assert ? PPS_CAPTUREASSERT :
181fdfbb3f5SIan Lepore 		    PPS_CAPTURECLEAR);
182fdfbb3f5SIan Lepore 	}
183fdfbb3f5SIan Lepore }
184fdfbb3f5SIan Lepore 
185fdfbb3f5SIan Lepore static void
uart_pps_init(struct uart_softc * sc)186196d3019SIan Lepore uart_pps_init(struct uart_softc *sc)
187196d3019SIan Lepore {
188196d3019SIan Lepore 	struct sysctl_ctx_list *ctx;
189196d3019SIan Lepore 	struct sysctl_oid *tree;
190196d3019SIan Lepore 
191196d3019SIan Lepore 	ctx = device_get_sysctl_ctx(sc->sc_dev);
192196d3019SIan Lepore 	tree = device_get_sysctl_tree(sc->sc_dev);
193196d3019SIan Lepore 
194196d3019SIan Lepore 	/*
195196d3019SIan Lepore 	 * The historical default for pps capture mode is either DCD or CTS,
196196d3019SIan Lepore 	 * depending on the UART_PPS_ON_CTS kernel option.  Start with that,
197196d3019SIan Lepore 	 * then try to fetch the tunable that overrides the mode for all uart
198196d3019SIan Lepore 	 * devices, then try to fetch the sysctl-tunable that overrides the mode
199196d3019SIan Lepore 	 * for one specific device.
200196d3019SIan Lepore 	 */
201196d3019SIan Lepore #ifdef UART_PPS_ON_CTS
202fdfbb3f5SIan Lepore 	sc->sc_pps_mode = UART_PPS_CTS;
203196d3019SIan Lepore #else
204fdfbb3f5SIan Lepore 	sc->sc_pps_mode = UART_PPS_DCD;
205196d3019SIan Lepore #endif
206196d3019SIan Lepore 	TUNABLE_INT_FETCH("hw.uart.pps_mode", &sc->sc_pps_mode);
207196d3019SIan Lepore 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "pps_mode",
208c214c2c0SAlexander Motin 	    CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, sc, 0,
2097029da5cSPawel Biernacki 	    uart_pps_mode_sysctl, "I", "pulse mode: 0/1/2=disabled/CTS/DCD; "
210fdfbb3f5SIan Lepore 	    "add 0x10 to invert, 0x20 for narrow pulse");
211196d3019SIan Lepore 
212196d3019SIan Lepore 	if (!uart_pps_mode_valid(sc->sc_pps_mode)) {
213196d3019SIan Lepore 		device_printf(sc->sc_dev,
214fdfbb3f5SIan Lepore 		    "Invalid pps_mode 0x%02x configured; disabling PPS capture\n",
215196d3019SIan Lepore 		    sc->sc_pps_mode);
216fdfbb3f5SIan Lepore 		sc->sc_pps_mode = UART_PPS_DISABLED;
217196d3019SIan Lepore 	} else if (bootverbose) {
218fdfbb3f5SIan Lepore 		uart_pps_print_mode(sc);
219196d3019SIan Lepore 	}
220196d3019SIan Lepore 
221196d3019SIan Lepore 	sc->sc_pps.ppscap = PPS_CAPTUREBOTH;
222196d3019SIan Lepore 	sc->sc_pps.driver_mtx = uart_tty_getlock(sc);
223196d3019SIan Lepore 	sc->sc_pps.driver_abi = PPS_ABI_VERSION;
224196d3019SIan Lepore 	pps_init_abi(&sc->sc_pps);
225196d3019SIan Lepore }
226196d3019SIan Lepore 
22727d5dc18SMarcel Moolenaar void
uart_add_sysdev(struct uart_devinfo * di)22827d5dc18SMarcel Moolenaar uart_add_sysdev(struct uart_devinfo *di)
22927d5dc18SMarcel Moolenaar {
23027d5dc18SMarcel Moolenaar 	SLIST_INSERT_HEAD(&uart_sysdevs, di, next);
23127d5dc18SMarcel Moolenaar }
23227d5dc18SMarcel Moolenaar 
233f8100ce2SMarcel Moolenaar const char *
uart_getname(struct uart_class * uc)234f8100ce2SMarcel Moolenaar uart_getname(struct uart_class *uc)
235f8100ce2SMarcel Moolenaar {
236f8100ce2SMarcel Moolenaar 	return ((uc != NULL) ? uc->name : NULL);
237f8100ce2SMarcel Moolenaar }
238f8100ce2SMarcel Moolenaar 
239f8100ce2SMarcel Moolenaar struct uart_ops *
uart_getops(struct uart_class * uc)240f8100ce2SMarcel Moolenaar uart_getops(struct uart_class *uc)
241f8100ce2SMarcel Moolenaar {
242f8100ce2SMarcel Moolenaar 	return ((uc != NULL) ? uc->uc_ops : NULL);
243f8100ce2SMarcel Moolenaar }
244f8100ce2SMarcel Moolenaar 
245f8100ce2SMarcel Moolenaar int
uart_getrange(struct uart_class * uc)246f8100ce2SMarcel Moolenaar uart_getrange(struct uart_class *uc)
247f8100ce2SMarcel Moolenaar {
248f8100ce2SMarcel Moolenaar 	return ((uc != NULL) ? uc->uc_range : 0);
249f8100ce2SMarcel Moolenaar }
250f8100ce2SMarcel Moolenaar 
251405ada37SAndrew Turner u_int
uart_getregshift(struct uart_class * uc)252405ada37SAndrew Turner uart_getregshift(struct uart_class *uc)
253405ada37SAndrew Turner {
254405ada37SAndrew Turner 	return ((uc != NULL) ? uc->uc_rshift : 0);
255405ada37SAndrew Turner }
256405ada37SAndrew Turner 
257c214a270SRuslan Bukin u_int
uart_getregiowidth(struct uart_class * uc)258c214a270SRuslan Bukin uart_getregiowidth(struct uart_class *uc)
259c214a270SRuslan Bukin {
260c214a270SRuslan Bukin 	return ((uc != NULL) ? uc->uc_riowidth : 0);
261c214a270SRuslan Bukin }
262c214a270SRuslan Bukin 
26327d5dc18SMarcel Moolenaar /*
2648af03381SMarcel Moolenaar  * Schedule a soft interrupt. We do this on the 0 to !0 transition
2658af03381SMarcel Moolenaar  * of the TTY pending interrupt status.
2668af03381SMarcel Moolenaar  */
2670acb3c4aSMarcel Moolenaar void
uart_sched_softih(struct uart_softc * sc,uint32_t ipend)2688af03381SMarcel Moolenaar uart_sched_softih(struct uart_softc *sc, uint32_t ipend)
2698af03381SMarcel Moolenaar {
2708af03381SMarcel Moolenaar 	uint32_t new, old;
2718af03381SMarcel Moolenaar 
2728af03381SMarcel Moolenaar 	do {
2738af03381SMarcel Moolenaar 		old = sc->sc_ttypend;
2748af03381SMarcel Moolenaar 		new = old | ipend;
2758af03381SMarcel Moolenaar 	} while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
2768af03381SMarcel Moolenaar 
2778af03381SMarcel Moolenaar 	if ((old & SER_INT_MASK) == 0)
2788af03381SMarcel Moolenaar 		swi_sched(sc->sc_softih, 0);
2798af03381SMarcel Moolenaar }
2808af03381SMarcel Moolenaar 
2818af03381SMarcel Moolenaar /*
28227d5dc18SMarcel Moolenaar  * A break condition has been detected. We treat the break condition as
28327d5dc18SMarcel Moolenaar  * a special case that should not happen during normal operation. When
28427d5dc18SMarcel Moolenaar  * the break condition is to be passed to higher levels in the form of
28527d5dc18SMarcel Moolenaar  * a NUL character, we really want the break to be in the right place in
28627d5dc18SMarcel Moolenaar  * the input stream. The overhead to achieve that is not in relation to
28727d5dc18SMarcel Moolenaar  * the exceptional nature of the break condition, so we permit ourselves
28827d5dc18SMarcel Moolenaar  * to be sloppy.
28927d5dc18SMarcel Moolenaar  */
2908af03381SMarcel Moolenaar static __inline int
uart_intr_break(void * arg)2918af03381SMarcel Moolenaar uart_intr_break(void *arg)
29227d5dc18SMarcel Moolenaar {
2938af03381SMarcel Moolenaar 	struct uart_softc *sc = arg;
29427d5dc18SMarcel Moolenaar 
2954cf75455SRobert Watson #if defined(KDB)
29627d5dc18SMarcel Moolenaar 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
2974cf75455SRobert Watson 		if (kdb_break())
2988af03381SMarcel Moolenaar 			return (0);
29927d5dc18SMarcel Moolenaar 	}
30027d5dc18SMarcel Moolenaar #endif
30127d5dc18SMarcel Moolenaar 	if (sc->sc_opened)
3028af03381SMarcel Moolenaar 		uart_sched_softih(sc, SER_INT_BREAK);
3038af03381SMarcel Moolenaar 	return (0);
30427d5dc18SMarcel Moolenaar }
30527d5dc18SMarcel Moolenaar 
30627d5dc18SMarcel Moolenaar /*
30727d5dc18SMarcel Moolenaar  * Handle a receiver overrun situation. We lost at least 1 byte in the
30827d5dc18SMarcel Moolenaar  * input stream and it's our job to contain the situation. We grab as
30927d5dc18SMarcel Moolenaar  * much of the data we can, but otherwise flush the receiver FIFO to
31027d5dc18SMarcel Moolenaar  * create some breathing room. The net effect is that we avoid the
31127d5dc18SMarcel Moolenaar  * overrun condition to happen for the next X characters, where X is
312a164074fSEitan Adler  * related to the FIFO size at the cost of losing data right away.
31327d5dc18SMarcel Moolenaar  * So, instead of having multiple overrun interrupts in close proximity
31427d5dc18SMarcel Moolenaar  * to each other and possibly pessimizing UART interrupt latency for
31527d5dc18SMarcel Moolenaar  * other UARTs in a multiport configuration, we create a longer segment
31627d5dc18SMarcel Moolenaar  * of missing characters by freeing up the FIFO.
31727d5dc18SMarcel Moolenaar  * Each overrun condition is marked in the input buffer by a token. The
31827d5dc18SMarcel Moolenaar  * token represents the loss of at least one, but possible more bytes in
31927d5dc18SMarcel Moolenaar  * the input stream.
32027d5dc18SMarcel Moolenaar  */
3218af03381SMarcel Moolenaar static __inline int
uart_intr_overrun(void * arg)3228af03381SMarcel Moolenaar uart_intr_overrun(void *arg)
32327d5dc18SMarcel Moolenaar {
3248af03381SMarcel Moolenaar 	struct uart_softc *sc = arg;
32527d5dc18SMarcel Moolenaar 
32627d5dc18SMarcel Moolenaar 	if (sc->sc_opened) {
32727d5dc18SMarcel Moolenaar 		UART_RECEIVE(sc);
32827d5dc18SMarcel Moolenaar 		if (uart_rx_put(sc, UART_STAT_OVERRUN))
32927d5dc18SMarcel Moolenaar 			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
3308af03381SMarcel Moolenaar 		uart_sched_softih(sc, SER_INT_RXREADY);
33127d5dc18SMarcel Moolenaar 	}
33264c4dfcdSJohn Baldwin 	sc->sc_rxoverruns++;
33327d5dc18SMarcel Moolenaar 	UART_FLUSH(sc, UART_FLUSH_RECEIVER);
3348af03381SMarcel Moolenaar 	return (0);
33527d5dc18SMarcel Moolenaar }
33627d5dc18SMarcel Moolenaar 
33727d5dc18SMarcel Moolenaar /*
33827d5dc18SMarcel Moolenaar  * Received data ready.
33927d5dc18SMarcel Moolenaar  */
3408af03381SMarcel Moolenaar static __inline int
uart_intr_rxready(void * arg)3418af03381SMarcel Moolenaar uart_intr_rxready(void *arg)
34227d5dc18SMarcel Moolenaar {
3438af03381SMarcel Moolenaar 	struct uart_softc *sc = arg;
344f2edc915SMateusz Guzik #if defined(KDB)
34527d5dc18SMarcel Moolenaar 	int rxp;
34627d5dc18SMarcel Moolenaar 
34727d5dc18SMarcel Moolenaar 	rxp = sc->sc_rxput;
348f2edc915SMateusz Guzik #endif
34927d5dc18SMarcel Moolenaar 	UART_RECEIVE(sc);
3504cf75455SRobert Watson #if defined(KDB)
35127d5dc18SMarcel Moolenaar 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
35227d5dc18SMarcel Moolenaar 		while (rxp != sc->sc_rxput) {
3534cf75455SRobert Watson 			kdb_alt_break(sc->sc_rxbuf[rxp++], &sc->sc_altbrk);
35427d5dc18SMarcel Moolenaar 			if (rxp == sc->sc_rxbufsz)
35527d5dc18SMarcel Moolenaar 				rxp = 0;
35627d5dc18SMarcel Moolenaar 		}
35727d5dc18SMarcel Moolenaar 	}
35827d5dc18SMarcel Moolenaar #endif
35927d5dc18SMarcel Moolenaar 	if (sc->sc_opened)
3608af03381SMarcel Moolenaar 		uart_sched_softih(sc, SER_INT_RXREADY);
36127d5dc18SMarcel Moolenaar 	else
36227d5dc18SMarcel Moolenaar 		sc->sc_rxput = sc->sc_rxget;	/* Ignore received data. */
3638af03381SMarcel Moolenaar 	return (1);
36427d5dc18SMarcel Moolenaar }
36527d5dc18SMarcel Moolenaar 
36627d5dc18SMarcel Moolenaar /*
36727d5dc18SMarcel Moolenaar  * Line or modem status change (OOB signalling).
36827d5dc18SMarcel Moolenaar  * We pass the signals to the software interrupt handler for further
36927d5dc18SMarcel Moolenaar  * processing. Note that we merge the delta bits, but set the state
370a164074fSEitan Adler  * bits. This is to avoid losing state transitions due to having more
37127d5dc18SMarcel Moolenaar  * than 1 hardware interrupt between software interrupts.
37227d5dc18SMarcel Moolenaar  */
3738af03381SMarcel Moolenaar static __inline int
uart_intr_sigchg(void * arg)3748af03381SMarcel Moolenaar uart_intr_sigchg(void *arg)
37527d5dc18SMarcel Moolenaar {
3768af03381SMarcel Moolenaar 	struct uart_softc *sc = arg;
377fdfbb3f5SIan Lepore 	int new, old, sig;
37827d5dc18SMarcel Moolenaar 
37927d5dc18SMarcel Moolenaar 	sig = UART_GETSIG(sc);
3808194412bSMarcel Moolenaar 
381fac6a198SIan Lepore 	/*
382fdfbb3f5SIan Lepore 	 * Time pulse counting support, invoked whenever the PPS parameters are
383fdfbb3f5SIan Lepore 	 * currently set to capture either edge of the signal.
384fac6a198SIan Lepore 	 */
3858194412bSMarcel Moolenaar 	if (sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) {
386fdfbb3f5SIan Lepore 		uart_pps_process(sc, sig);
3878194412bSMarcel Moolenaar 	}
3888194412bSMarcel Moolenaar 
3898af03381SMarcel Moolenaar 	/*
3908af03381SMarcel Moolenaar 	 * Keep track of signal changes, even when the device is not
3918af03381SMarcel Moolenaar 	 * opened. This allows us to inform upper layers about a
3928af03381SMarcel Moolenaar 	 * possible loss of DCD and thus the existence of a (possibly)
3938af03381SMarcel Moolenaar 	 * different connection when we have DCD back, during the time
3948af03381SMarcel Moolenaar 	 * that the device was closed.
3958af03381SMarcel Moolenaar 	 */
39627d5dc18SMarcel Moolenaar 	do {
39727d5dc18SMarcel Moolenaar 		old = sc->sc_ttypend;
398ea549414SMarcel Moolenaar 		new = old & ~SER_MASK_STATE;
3992d511805SMarcel Moolenaar 		new |= sig & SER_INT_SIGMASK;
40027d5dc18SMarcel Moolenaar 	} while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
4018af03381SMarcel Moolenaar 
4028af03381SMarcel Moolenaar 	if (sc->sc_opened)
4038af03381SMarcel Moolenaar 		uart_sched_softih(sc, SER_INT_SIGCHG);
4048af03381SMarcel Moolenaar 	return (1);
40527d5dc18SMarcel Moolenaar }
40627d5dc18SMarcel Moolenaar 
40727d5dc18SMarcel Moolenaar /*
40827d5dc18SMarcel Moolenaar  * The transmitter can accept more data.
40927d5dc18SMarcel Moolenaar  */
4108af03381SMarcel Moolenaar static __inline int
uart_intr_txidle(void * arg)4118af03381SMarcel Moolenaar uart_intr_txidle(void *arg)
41227d5dc18SMarcel Moolenaar {
4138af03381SMarcel Moolenaar 	struct uart_softc *sc = arg;
4148af03381SMarcel Moolenaar 
41527d5dc18SMarcel Moolenaar 	if (sc->sc_txbusy) {
41627d5dc18SMarcel Moolenaar 		sc->sc_txbusy = 0;
4178af03381SMarcel Moolenaar 		uart_sched_softih(sc, SER_INT_TXIDLE);
41827d5dc18SMarcel Moolenaar 	}
4198af03381SMarcel Moolenaar 	return (0);
42027d5dc18SMarcel Moolenaar }
42127d5dc18SMarcel Moolenaar 
422ef544f63SPaolo Pisati static int
uart_intr(void * arg)42327d5dc18SMarcel Moolenaar uart_intr(void *arg)
42427d5dc18SMarcel Moolenaar {
42527d5dc18SMarcel Moolenaar 	struct uart_softc *sc = arg;
42686fb5400SMarius Strobl 	int cnt, ipend, testintr;
42727d5dc18SMarcel Moolenaar 
428eead2d55SMarcel Moolenaar 	if (sc->sc_leaving)
429eead2d55SMarcel Moolenaar 		return (FILTER_STRAY);
430eead2d55SMarcel Moolenaar 
431eead2d55SMarcel Moolenaar 	cnt = 0;
43286fb5400SMarius Strobl 	testintr = sc->sc_testintr;
43386fb5400SMarius Strobl 	while ((!testintr || cnt < 20) && (ipend = UART_IPEND(sc)) != 0) {
434eead2d55SMarcel Moolenaar 		cnt++;
4352d511805SMarcel Moolenaar 		if (ipend & SER_INT_OVERRUN)
43627d5dc18SMarcel Moolenaar 			uart_intr_overrun(sc);
4372d511805SMarcel Moolenaar 		if (ipend & SER_INT_BREAK)
43827d5dc18SMarcel Moolenaar 			uart_intr_break(sc);
4392d511805SMarcel Moolenaar 		if (ipend & SER_INT_RXREADY)
44027d5dc18SMarcel Moolenaar 			uart_intr_rxready(sc);
4412d511805SMarcel Moolenaar 		if (ipend & SER_INT_SIGCHG)
44227d5dc18SMarcel Moolenaar 			uart_intr_sigchg(sc);
4432d511805SMarcel Moolenaar 		if (ipend & SER_INT_TXIDLE)
44427d5dc18SMarcel Moolenaar 			uart_intr_txidle(sc);
4458af03381SMarcel Moolenaar 	}
446332cda07SPeter Grehan 
447332cda07SPeter Grehan 	if (sc->sc_polled) {
448332cda07SPeter Grehan 		callout_reset(&sc->sc_timer, hz / uart_poll_freq,
4495773ac11SJohn Baldwin 		    (callout_func_t *)uart_intr, sc);
450332cda07SPeter Grehan 	}
451332cda07SPeter Grehan 
452eead2d55SMarcel Moolenaar 	return ((cnt == 0) ? FILTER_STRAY :
45386fb5400SMarius Strobl 	    ((testintr && cnt == 20) ? FILTER_SCHEDULE_THREAD :
45486fb5400SMarius Strobl 	    FILTER_HANDLED));
4558af03381SMarcel Moolenaar }
45627d5dc18SMarcel Moolenaar 
4578af03381SMarcel Moolenaar serdev_intr_t *
uart_bus_ihand(device_t dev,int ipend)4588af03381SMarcel Moolenaar uart_bus_ihand(device_t dev, int ipend)
4598af03381SMarcel Moolenaar {
4608af03381SMarcel Moolenaar 
4618af03381SMarcel Moolenaar 	switch (ipend) {
4628af03381SMarcel Moolenaar 	case SER_INT_BREAK:
4638af03381SMarcel Moolenaar 		return (uart_intr_break);
4648af03381SMarcel Moolenaar 	case SER_INT_OVERRUN:
4658af03381SMarcel Moolenaar 		return (uart_intr_overrun);
4668af03381SMarcel Moolenaar 	case SER_INT_RXREADY:
4678af03381SMarcel Moolenaar 		return (uart_intr_rxready);
4688af03381SMarcel Moolenaar 	case SER_INT_SIGCHG:
4698af03381SMarcel Moolenaar 		return (uart_intr_sigchg);
4708af03381SMarcel Moolenaar 	case SER_INT_TXIDLE:
4718af03381SMarcel Moolenaar 		return (uart_intr_txidle);
4728af03381SMarcel Moolenaar 	}
4738af03381SMarcel Moolenaar 	return (NULL);
4748af03381SMarcel Moolenaar }
4758af03381SMarcel Moolenaar 
4768af03381SMarcel Moolenaar int
uart_bus_ipend(device_t dev)477a31f91a0SMarcel Moolenaar uart_bus_ipend(device_t dev)
478a31f91a0SMarcel Moolenaar {
479a31f91a0SMarcel Moolenaar 	struct uart_softc *sc;
480a31f91a0SMarcel Moolenaar 
481a31f91a0SMarcel Moolenaar 	sc = device_get_softc(dev);
482a31f91a0SMarcel Moolenaar 	return (UART_IPEND(sc));
483a31f91a0SMarcel Moolenaar }
484a31f91a0SMarcel Moolenaar 
485a31f91a0SMarcel Moolenaar int
uart_bus_sysdev(device_t dev)4868af03381SMarcel Moolenaar uart_bus_sysdev(device_t dev)
4878af03381SMarcel Moolenaar {
4888af03381SMarcel Moolenaar 	struct uart_softc *sc;
4898af03381SMarcel Moolenaar 
4908af03381SMarcel Moolenaar 	sc = device_get_softc(dev);
4918af03381SMarcel Moolenaar 	return ((sc->sc_sysdev != NULL) ? 1 : 0);
49227d5dc18SMarcel Moolenaar }
49327d5dc18SMarcel Moolenaar 
49427d5dc18SMarcel Moolenaar int
uart_bus_probe(device_t dev,int regshft,int regiowidth,int rclk,int rid,int chan,int quirks)495381388b9SMatt Macy uart_bus_probe(device_t dev, int regshft, int regiowidth, int rclk, int rid, int chan, int quirks)
49627d5dc18SMarcel Moolenaar {
49727d5dc18SMarcel Moolenaar 	struct uart_softc *sc;
49827d5dc18SMarcel Moolenaar 	struct uart_devinfo *sysdev;
49927d5dc18SMarcel Moolenaar 	int error;
50027d5dc18SMarcel Moolenaar 
501f8100ce2SMarcel Moolenaar 	sc = device_get_softc(dev);
502f8100ce2SMarcel Moolenaar 
503f8100ce2SMarcel Moolenaar 	/*
504f8100ce2SMarcel Moolenaar 	 * All uart_class references are weak. Check that the needed
505f8100ce2SMarcel Moolenaar 	 * class has been compiled-in. Fail if not.
506f8100ce2SMarcel Moolenaar 	 */
507f8100ce2SMarcel Moolenaar 	if (sc->sc_class == NULL)
508f8100ce2SMarcel Moolenaar 		return (ENXIO);
509f8100ce2SMarcel Moolenaar 
51027d5dc18SMarcel Moolenaar 	/*
51127d5dc18SMarcel Moolenaar 	 * Initialize the instance. Note that the instance (=softc) does
51227d5dc18SMarcel Moolenaar 	 * not necessarily match the hardware specific softc. We can't do
51327d5dc18SMarcel Moolenaar 	 * anything about it now, because we may not attach to the device.
51427d5dc18SMarcel Moolenaar 	 * Hardware drivers cannot use any of the class specific fields
51527d5dc18SMarcel Moolenaar 	 * while probing.
51627d5dc18SMarcel Moolenaar 	 */
51727d5dc18SMarcel Moolenaar 	kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class);
51827d5dc18SMarcel Moolenaar 	sc->sc_dev = dev;
51927d5dc18SMarcel Moolenaar 	if (device_get_desc(dev) == NULL)
520f8100ce2SMarcel Moolenaar 		device_set_desc(dev, uart_getname(sc->sc_class));
52127d5dc18SMarcel Moolenaar 
52227d5dc18SMarcel Moolenaar 	/*
52327d5dc18SMarcel Moolenaar 	 * Allocate the register resource. We assume that all UARTs have
52427d5dc18SMarcel Moolenaar 	 * a single register window in either I/O port space or memory
52527d5dc18SMarcel Moolenaar 	 * mapped I/O space. Any UART that needs multiple windows will
52627d5dc18SMarcel Moolenaar 	 * consequently not be supported by this driver as-is. We try I/O
52727d5dc18SMarcel Moolenaar 	 * port space first because that's the common case.
52827d5dc18SMarcel Moolenaar 	 */
52927d5dc18SMarcel Moolenaar 	sc->sc_rrid = rid;
53027d5dc18SMarcel Moolenaar 	sc->sc_rtype = SYS_RES_IOPORT;
5319def69ecSMarcel Moolenaar 	sc->sc_rres = bus_alloc_resource_any(dev, sc->sc_rtype, &sc->sc_rrid,
5329def69ecSMarcel Moolenaar 	    RF_ACTIVE);
53327d5dc18SMarcel Moolenaar 	if (sc->sc_rres == NULL) {
53427d5dc18SMarcel Moolenaar 		sc->sc_rrid = rid;
53527d5dc18SMarcel Moolenaar 		sc->sc_rtype = SYS_RES_MEMORY;
5369def69ecSMarcel Moolenaar 		sc->sc_rres = bus_alloc_resource_any(dev, sc->sc_rtype,
5379def69ecSMarcel Moolenaar 		    &sc->sc_rrid, RF_ACTIVE);
53827d5dc18SMarcel Moolenaar 		if (sc->sc_rres == NULL)
53927d5dc18SMarcel Moolenaar 			return (ENXIO);
54027d5dc18SMarcel Moolenaar 	}
54127d5dc18SMarcel Moolenaar 
54227d5dc18SMarcel Moolenaar 	/*
54327d5dc18SMarcel Moolenaar 	 * Fill in the bus access structure and compare this device with
54427d5dc18SMarcel Moolenaar 	 * a possible console device and/or a debug port. We set the flags
54527d5dc18SMarcel Moolenaar 	 * in the softc so that the hardware dependent probe can adjust
54627d5dc18SMarcel Moolenaar 	 * accordingly. In general, you don't want to permanently disrupt
54727d5dc18SMarcel Moolenaar 	 * console I/O.
54827d5dc18SMarcel Moolenaar 	 */
54927d5dc18SMarcel Moolenaar 	sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
55027d5dc18SMarcel Moolenaar 	sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
551875f70dbSMarcel Moolenaar 	sc->sc_bas.chan = chan;
55227d5dc18SMarcel Moolenaar 	sc->sc_bas.regshft = regshft;
553c214a270SRuslan Bukin 	sc->sc_bas.regiowidth = regiowidth;
55427d5dc18SMarcel Moolenaar 	sc->sc_bas.rclk = (rclk == 0) ? sc->sc_class->uc_rclk : rclk;
555381388b9SMatt Macy 	sc->sc_bas.busy_detect = !!(quirks & UART_F_BUSY_DETECT);
55627d5dc18SMarcel Moolenaar 
55727d5dc18SMarcel Moolenaar 	SLIST_FOREACH(sysdev, &uart_sysdevs, next) {
558875f70dbSMarcel Moolenaar 		if (chan == sysdev->bas.chan &&
559875f70dbSMarcel Moolenaar 		    uart_cpu_eqres(&sc->sc_bas, &sysdev->bas)) {
56027d5dc18SMarcel Moolenaar 			/* XXX check if ops matches class. */
56127d5dc18SMarcel Moolenaar 			sc->sc_sysdev = sysdev;
562fa93443aSWarner Losh 			if (sysdev->bas.rclk != 0) {
563fa93443aSWarner Losh 				/* Let the boot sequence control */
564fa93443aSWarner Losh 				sc->sc_bas.rclk = sysdev->bas.rclk;
565fa93443aSWarner Losh 			} else {
566fa93443aSWarner Losh 				/* Boot didn't set it, use use class */
5671c5e367bSMarcel Moolenaar 				sysdev->bas.rclk = sc->sc_bas.rclk;
56827d5dc18SMarcel Moolenaar 			}
56927d5dc18SMarcel Moolenaar                 }
570fa93443aSWarner Losh 	}
57127d5dc18SMarcel Moolenaar 
57227d5dc18SMarcel Moolenaar 	error = UART_PROBE(sc);
57327d5dc18SMarcel Moolenaar 	bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
574*76bfa33fSWarner Losh 	return ((error) ? error : 0);
57527d5dc18SMarcel Moolenaar }
57627d5dc18SMarcel Moolenaar 
57727d5dc18SMarcel Moolenaar int
uart_bus_attach(device_t dev)57827d5dc18SMarcel Moolenaar uart_bus_attach(device_t dev)
57927d5dc18SMarcel Moolenaar {
58027d5dc18SMarcel Moolenaar 	struct uart_softc *sc, *sc0;
58127d5dc18SMarcel Moolenaar 	const char *sep;
582eead2d55SMarcel Moolenaar 	int error, filt;
58327d5dc18SMarcel Moolenaar 
58427d5dc18SMarcel Moolenaar 	/*
58527d5dc18SMarcel Moolenaar 	 * The sc_class field defines the type of UART we're going to work
58627d5dc18SMarcel Moolenaar 	 * with and thus the size of the softc. Replace the generic softc
58727d5dc18SMarcel Moolenaar 	 * with one that matches the UART now that we're certain we handle
58827d5dc18SMarcel Moolenaar 	 * the device.
58927d5dc18SMarcel Moolenaar 	 */
59027d5dc18SMarcel Moolenaar 	sc0 = device_get_softc(dev);
5915515b0cbSRuslan Bukin 	if (sc0->sc_class->size > device_get_driver(dev)->size) {
59227d5dc18SMarcel Moolenaar 		sc = malloc(sc0->sc_class->size, M_UART, M_WAITOK|M_ZERO);
59327d5dc18SMarcel Moolenaar 		bcopy(sc0, sc, sizeof(*sc));
59427d5dc18SMarcel Moolenaar 		device_set_softc(dev, sc);
59527d5dc18SMarcel Moolenaar 	} else
59627d5dc18SMarcel Moolenaar 		sc = sc0;
59727d5dc18SMarcel Moolenaar 
59827d5dc18SMarcel Moolenaar 	/*
599d76a1ef4SWarner Losh 	 * Now that we know the softc for this device, connect the back
600d76a1ef4SWarner Losh 	 * pointer from the sysdev for this device, if any
601d76a1ef4SWarner Losh 	 */
602d76a1ef4SWarner Losh 	if (sc->sc_sysdev != NULL)
603d76a1ef4SWarner Losh 		sc->sc_sysdev->sc = sc;
604d76a1ef4SWarner Losh 
605d76a1ef4SWarner Losh 	/*
60627d5dc18SMarcel Moolenaar 	 * Protect ourselves against interrupts while we're not completely
60727d5dc18SMarcel Moolenaar 	 * finished attaching and initializing. We don't expect interrupts
60886fb5400SMarius Strobl 	 * until after UART_ATTACH(), though.
60927d5dc18SMarcel Moolenaar 	 */
61027d5dc18SMarcel Moolenaar 	sc->sc_leaving = 1;
61127d5dc18SMarcel Moolenaar 
6128af03381SMarcel Moolenaar 	mtx_init(&sc->sc_hwmtx_s, "uart_hwmtx", NULL, MTX_SPIN);
6138af03381SMarcel Moolenaar 	if (sc->sc_hwmtx == NULL)
6148af03381SMarcel Moolenaar 		sc->sc_hwmtx = &sc->sc_hwmtx_s;
61506287620SMarcel Moolenaar 
61627d5dc18SMarcel Moolenaar 	/*
61727d5dc18SMarcel Moolenaar 	 * Re-allocate. We expect that the softc contains the information
61827d5dc18SMarcel Moolenaar 	 * collected by uart_bus_probe() intact.
61927d5dc18SMarcel Moolenaar 	 */
6209def69ecSMarcel Moolenaar 	sc->sc_rres = bus_alloc_resource_any(dev, sc->sc_rtype, &sc->sc_rrid,
6219def69ecSMarcel Moolenaar 	    RF_ACTIVE);
6222682e7b6SMarius Strobl 	if (sc->sc_rres == NULL) {
6238af03381SMarcel Moolenaar 		mtx_destroy(&sc->sc_hwmtx_s);
62427d5dc18SMarcel Moolenaar 		return (ENXIO);
6252682e7b6SMarius Strobl 	}
626b8759afcSYoshihiro Takahashi 	sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
627b8759afcSYoshihiro Takahashi 	sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
628b8759afcSYoshihiro Takahashi 
6293ad36a41SIan Lepore 	/*
6303ad36a41SIan Lepore 	 * Ensure there is room for at least three full FIFOs of data in the
6313ad36a41SIan Lepore 	 * receive buffer (handles the case of low-level drivers with huge
6323ad36a41SIan Lepore 	 * FIFOs), and also ensure that there is no less than the historical
6333ad36a41SIan Lepore 	 * size of 384 bytes (handles the typical small-FIFO case).
6343ad36a41SIan Lepore 	 */
6353ad36a41SIan Lepore 	sc->sc_rxbufsz = MAX(384, sc->sc_rxfifosz * 3);
63627d5dc18SMarcel Moolenaar 	sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf),
63727d5dc18SMarcel Moolenaar 	    M_UART, M_WAITOK);
63827d5dc18SMarcel Moolenaar 	sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf),
63927d5dc18SMarcel Moolenaar 	    M_UART, M_WAITOK);
64027d5dc18SMarcel Moolenaar 
64127d5dc18SMarcel Moolenaar 	error = UART_ATTACH(sc);
64227d5dc18SMarcel Moolenaar 	if (error)
64327d5dc18SMarcel Moolenaar 		goto fail;
64427d5dc18SMarcel Moolenaar 
64527d5dc18SMarcel Moolenaar 	if (sc->sc_hwiflow || sc->sc_hwoflow) {
64627d5dc18SMarcel Moolenaar 		sep = "";
64727d5dc18SMarcel Moolenaar 		device_print_prettyname(dev);
64827d5dc18SMarcel Moolenaar 		if (sc->sc_hwiflow) {
64927d5dc18SMarcel Moolenaar 			printf("%sRTS iflow", sep);
65027d5dc18SMarcel Moolenaar 			sep = ", ";
65127d5dc18SMarcel Moolenaar 		}
65227d5dc18SMarcel Moolenaar 		if (sc->sc_hwoflow) {
65327d5dc18SMarcel Moolenaar 			printf("%sCTS oflow", sep);
65427d5dc18SMarcel Moolenaar 			sep = ", ";
65527d5dc18SMarcel Moolenaar 		}
65627d5dc18SMarcel Moolenaar 		printf("\n");
65727d5dc18SMarcel Moolenaar 	}
65827d5dc18SMarcel Moolenaar 
65927d5dc18SMarcel Moolenaar 	if (sc->sc_sysdev != NULL) {
6605dceed8aSWarner Losh 		if (sc->sc_sysdev->baudrate == 0) {
66154e2bcc7SMarcel Moolenaar 			if (UART_IOCTL(sc, UART_IOCTL_BAUD,
66254e2bcc7SMarcel Moolenaar 			    (intptr_t)&sc->sc_sysdev->baudrate) != 0)
66354e2bcc7SMarcel Moolenaar 				sc->sc_sysdev->baudrate = -1;
66454e2bcc7SMarcel Moolenaar 		}
66527d5dc18SMarcel Moolenaar 		switch (sc->sc_sysdev->type) {
66627d5dc18SMarcel Moolenaar 		case UART_DEV_CONSOLE:
66727d5dc18SMarcel Moolenaar 			device_printf(dev, "console");
66827d5dc18SMarcel Moolenaar 			break;
66927d5dc18SMarcel Moolenaar 		case UART_DEV_DBGPORT:
67027d5dc18SMarcel Moolenaar 			device_printf(dev, "debug port");
67127d5dc18SMarcel Moolenaar 			break;
67227d5dc18SMarcel Moolenaar 		case UART_DEV_KEYBOARD:
67327d5dc18SMarcel Moolenaar 			device_printf(dev, "keyboard");
67427d5dc18SMarcel Moolenaar 			break;
67527d5dc18SMarcel Moolenaar 		default:
67627d5dc18SMarcel Moolenaar 			device_printf(dev, "unknown system device");
67727d5dc18SMarcel Moolenaar 			break;
67827d5dc18SMarcel Moolenaar 		}
67927d5dc18SMarcel Moolenaar 		printf(" (%d,%c,%d,%d)\n", sc->sc_sysdev->baudrate,
68027d5dc18SMarcel Moolenaar 		    "noems"[sc->sc_sysdev->parity], sc->sc_sysdev->databits,
68127d5dc18SMarcel Moolenaar 		    sc->sc_sysdev->stopbits);
68227d5dc18SMarcel Moolenaar 	}
68327d5dc18SMarcel Moolenaar 
684eead2d55SMarcel Moolenaar 	sc->sc_leaving = 0;
68586fb5400SMarius Strobl 	sc->sc_testintr = 1;
686eead2d55SMarcel Moolenaar 	filt = uart_intr(sc);
68786fb5400SMarius Strobl 	sc->sc_testintr = 0;
688eead2d55SMarcel Moolenaar 
689eead2d55SMarcel Moolenaar 	/*
690eead2d55SMarcel Moolenaar 	 * Don't use interrupts if we couldn't clear any pending interrupt
691eead2d55SMarcel Moolenaar 	 * conditions. We may have broken H/W and polling is probably the
692eead2d55SMarcel Moolenaar 	 * safest thing to do.
693eead2d55SMarcel Moolenaar 	 */
69462145ff3SNeel Natu 	if (filt != FILTER_SCHEDULE_THREAD && !uart_force_poll) {
695eead2d55SMarcel Moolenaar 		sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
696eead2d55SMarcel Moolenaar 		    &sc->sc_irid, RF_ACTIVE | RF_SHAREABLE);
697eead2d55SMarcel Moolenaar 	}
698eead2d55SMarcel Moolenaar 	if (sc->sc_ires != NULL) {
699eead2d55SMarcel Moolenaar 		error = bus_setup_intr(dev, sc->sc_ires, INTR_TYPE_TTY,
700eead2d55SMarcel Moolenaar 		    uart_intr, NULL, sc, &sc->sc_icookie);
701eead2d55SMarcel Moolenaar 		sc->sc_fastintr = (error == 0) ? 1 : 0;
702eead2d55SMarcel Moolenaar 
703eead2d55SMarcel Moolenaar 		if (!sc->sc_fastintr)
704eead2d55SMarcel Moolenaar 			error = bus_setup_intr(dev, sc->sc_ires,
705eead2d55SMarcel Moolenaar 			    INTR_TYPE_TTY | INTR_MPSAFE, NULL,
706eead2d55SMarcel Moolenaar 			    (driver_intr_t *)uart_intr, sc, &sc->sc_icookie);
707eead2d55SMarcel Moolenaar 
708eead2d55SMarcel Moolenaar 		if (error) {
709eead2d55SMarcel Moolenaar 			device_printf(dev, "could not activate interrupt\n");
710eead2d55SMarcel Moolenaar 			bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
711eead2d55SMarcel Moolenaar 			    sc->sc_ires);
712eead2d55SMarcel Moolenaar 			sc->sc_ires = NULL;
713eead2d55SMarcel Moolenaar 		}
714eead2d55SMarcel Moolenaar 	}
715eead2d55SMarcel Moolenaar 	if (sc->sc_ires == NULL) {
716eead2d55SMarcel Moolenaar 		/* No interrupt resource. Force polled mode. */
717eead2d55SMarcel Moolenaar 		sc->sc_polled = 1;
718eead2d55SMarcel Moolenaar 		callout_init(&sc->sc_timer, 1);
71962145ff3SNeel Natu 		callout_reset(&sc->sc_timer, hz / uart_poll_freq,
7205773ac11SJohn Baldwin 		    (callout_func_t *)uart_intr, sc);
721eead2d55SMarcel Moolenaar 	}
722eead2d55SMarcel Moolenaar 
723eead2d55SMarcel Moolenaar 	if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
724eead2d55SMarcel Moolenaar 		sep = "";
725eead2d55SMarcel Moolenaar 		device_print_prettyname(dev);
726eead2d55SMarcel Moolenaar 		if (sc->sc_fastintr) {
727eead2d55SMarcel Moolenaar 			printf("%sfast interrupt", sep);
728eead2d55SMarcel Moolenaar 			sep = ", ";
729eead2d55SMarcel Moolenaar 		}
730eead2d55SMarcel Moolenaar 		if (sc->sc_polled) {
7311662b008SIan Lepore 			printf("%spolled mode (%dHz)", sep, uart_poll_freq);
732eead2d55SMarcel Moolenaar 			sep = ", ";
733eead2d55SMarcel Moolenaar 		}
734eead2d55SMarcel Moolenaar 		printf("\n");
735eead2d55SMarcel Moolenaar 	}
736eead2d55SMarcel Moolenaar 
737b59236ceSIan Lepore 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL) {
738b59236ceSIan Lepore 		if ((error = sc->sc_sysdev->attach(sc)) != 0)
73927d5dc18SMarcel Moolenaar 			goto fail;
740b59236ceSIan Lepore 	} else {
741b59236ceSIan Lepore 		if ((error = uart_tty_attach(sc)) != 0)
742b59236ceSIan Lepore 			goto fail;
743196d3019SIan Lepore 		uart_pps_init(sc);
744b59236ceSIan Lepore 	}
74527d5dc18SMarcel Moolenaar 
7468af03381SMarcel Moolenaar 	if (sc->sc_sysdev != NULL)
7478af03381SMarcel Moolenaar 		sc->sc_sysdev->hwmtx = sc->sc_hwmtx;
7488af03381SMarcel Moolenaar 
74964c4dfcdSJohn Baldwin 	if (sc->sc_rxfifosz > 1)
75064c4dfcdSJohn Baldwin 		SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
75164c4dfcdSJohn Baldwin 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
75264c4dfcdSJohn Baldwin 		    "rx_overruns", CTLFLAG_RD, &sc->sc_rxoverruns, 0,
75364c4dfcdSJohn Baldwin 		    "Receive overruns");
75464c4dfcdSJohn Baldwin 
755cc7854e1SWarner Losh 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
756cc7854e1SWarner Losh 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
757cc7854e1SWarner Losh 	    "rclk", CTLFLAG_RD, &sc->sc_bas.rclk, 0,
758cc7854e1SWarner Losh 	    "Baud clock for device");
759cc7854e1SWarner Losh 
76027d5dc18SMarcel Moolenaar 	return (0);
76127d5dc18SMarcel Moolenaar 
76227d5dc18SMarcel Moolenaar  fail:
76327d5dc18SMarcel Moolenaar 	free(sc->sc_txbuf, M_UART);
76427d5dc18SMarcel Moolenaar 	free(sc->sc_rxbuf, M_UART);
76527d5dc18SMarcel Moolenaar 
76627d5dc18SMarcel Moolenaar 	if (sc->sc_ires != NULL) {
76727d5dc18SMarcel Moolenaar 		bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
76827d5dc18SMarcel Moolenaar 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
76927d5dc18SMarcel Moolenaar 		    sc->sc_ires);
77027d5dc18SMarcel Moolenaar 	}
77127d5dc18SMarcel Moolenaar 	bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
77227d5dc18SMarcel Moolenaar 
7738af03381SMarcel Moolenaar 	mtx_destroy(&sc->sc_hwmtx_s);
7742682e7b6SMarius Strobl 
77527d5dc18SMarcel Moolenaar 	return (error);
77627d5dc18SMarcel Moolenaar }
77727d5dc18SMarcel Moolenaar 
77827d5dc18SMarcel Moolenaar int
uart_bus_detach(device_t dev)77927d5dc18SMarcel Moolenaar uart_bus_detach(device_t dev)
78027d5dc18SMarcel Moolenaar {
78127d5dc18SMarcel Moolenaar 	struct uart_softc *sc;
78227d5dc18SMarcel Moolenaar 
78327d5dc18SMarcel Moolenaar 	sc = device_get_softc(dev);
78427d5dc18SMarcel Moolenaar 
78527d5dc18SMarcel Moolenaar 	sc->sc_leaving = 1;
78627d5dc18SMarcel Moolenaar 
7878af03381SMarcel Moolenaar 	if (sc->sc_sysdev != NULL)
7888af03381SMarcel Moolenaar 		sc->sc_sysdev->hwmtx = NULL;
7898af03381SMarcel Moolenaar 
79027d5dc18SMarcel Moolenaar 	UART_DETACH(sc);
79127d5dc18SMarcel Moolenaar 
79227d5dc18SMarcel Moolenaar 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL)
79327d5dc18SMarcel Moolenaar 		(*sc->sc_sysdev->detach)(sc);
79427d5dc18SMarcel Moolenaar 	else
79527d5dc18SMarcel Moolenaar 		uart_tty_detach(sc);
79627d5dc18SMarcel Moolenaar 
79727d5dc18SMarcel Moolenaar 	free(sc->sc_txbuf, M_UART);
79827d5dc18SMarcel Moolenaar 	free(sc->sc_rxbuf, M_UART);
79927d5dc18SMarcel Moolenaar 
80027d5dc18SMarcel Moolenaar 	if (sc->sc_ires != NULL) {
80127d5dc18SMarcel Moolenaar 		bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
80227d5dc18SMarcel Moolenaar 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
80327d5dc18SMarcel Moolenaar 		    sc->sc_ires);
80427d5dc18SMarcel Moolenaar 	}
80527d5dc18SMarcel Moolenaar 	bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
80627d5dc18SMarcel Moolenaar 
8078af03381SMarcel Moolenaar 	mtx_destroy(&sc->sc_hwmtx_s);
8082682e7b6SMarius Strobl 
8095515b0cbSRuslan Bukin 	if (sc->sc_class->size > device_get_driver(dev)->size) {
81027d5dc18SMarcel Moolenaar 		device_set_softc(dev, NULL);
81127d5dc18SMarcel Moolenaar 		free(sc, M_UART);
8125515b0cbSRuslan Bukin 	}
81327d5dc18SMarcel Moolenaar 
81427d5dc18SMarcel Moolenaar 	return (0);
81527d5dc18SMarcel Moolenaar }
8165b23b1b9SAndriy Gapon 
8175b23b1b9SAndriy Gapon int
uart_bus_resume(device_t dev)8185b23b1b9SAndriy Gapon uart_bus_resume(device_t dev)
8195b23b1b9SAndriy Gapon {
8205b23b1b9SAndriy Gapon 	struct uart_softc *sc;
8215b23b1b9SAndriy Gapon 
8225b23b1b9SAndriy Gapon 	sc = device_get_softc(dev);
8235b23b1b9SAndriy Gapon 	return (UART_ATTACH(sc));
8245b23b1b9SAndriy Gapon }
825d76a1ef4SWarner Losh 
826d76a1ef4SWarner Losh void
uart_grab(struct uart_devinfo * di)827d76a1ef4SWarner Losh uart_grab(struct uart_devinfo *di)
828d76a1ef4SWarner Losh {
829d76a1ef4SWarner Losh 
830d76a1ef4SWarner Losh 	if (di->sc)
831d76a1ef4SWarner Losh 		UART_GRAB(di->sc);
832d76a1ef4SWarner Losh }
833d76a1ef4SWarner Losh 
834d76a1ef4SWarner Losh void
uart_ungrab(struct uart_devinfo * di)835d76a1ef4SWarner Losh uart_ungrab(struct uart_devinfo *di)
836d76a1ef4SWarner Losh {
837d76a1ef4SWarner Losh 
838d76a1ef4SWarner Losh 	if (di->sc)
839d76a1ef4SWarner Losh 		UART_UNGRAB(di->sc);
840d76a1ef4SWarner Losh }
841