1098ca2bdSWarner Losh /*- 227d5dc18SMarcel Moolenaar * Copyright (c) 2003 Marcel Moolenaar 327d5dc18SMarcel Moolenaar * All rights reserved. 427d5dc18SMarcel Moolenaar * 527d5dc18SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 627d5dc18SMarcel Moolenaar * modification, are permitted provided that the following conditions 727d5dc18SMarcel Moolenaar * are met: 827d5dc18SMarcel Moolenaar * 927d5dc18SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 1027d5dc18SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 1127d5dc18SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 1227d5dc18SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 1327d5dc18SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 1427d5dc18SMarcel Moolenaar * 1527d5dc18SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1627d5dc18SMarcel Moolenaar * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1727d5dc18SMarcel Moolenaar * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1827d5dc18SMarcel Moolenaar * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1927d5dc18SMarcel Moolenaar * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2027d5dc18SMarcel Moolenaar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2127d5dc18SMarcel Moolenaar * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2227d5dc18SMarcel Moolenaar * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2327d5dc18SMarcel Moolenaar * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2427d5dc18SMarcel Moolenaar * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2527d5dc18SMarcel Moolenaar */ 2627d5dc18SMarcel Moolenaar 2727d5dc18SMarcel Moolenaar #include <sys/cdefs.h> 2827d5dc18SMarcel Moolenaar __FBSDID("$FreeBSD$"); 2927d5dc18SMarcel Moolenaar 3027d5dc18SMarcel Moolenaar #include <sys/param.h> 3127d5dc18SMarcel Moolenaar #include <sys/systm.h> 3227d5dc18SMarcel Moolenaar #include <sys/bus.h> 3327d5dc18SMarcel Moolenaar #include <sys/conf.h> 3427d5dc18SMarcel Moolenaar #include <sys/cons.h> 3527d5dc18SMarcel Moolenaar #include <sys/fcntl.h> 3627d5dc18SMarcel Moolenaar #include <sys/interrupt.h> 37591bc192SMarcel Moolenaar #include <sys/kdb.h> 3827d5dc18SMarcel Moolenaar #include <sys/kernel.h> 3927d5dc18SMarcel Moolenaar #include <sys/malloc.h> 4027d5dc18SMarcel Moolenaar #include <sys/queue.h> 4127d5dc18SMarcel Moolenaar #include <sys/reboot.h> 4262145ff3SNeel Natu #include <sys/sysctl.h> 4327d5dc18SMarcel Moolenaar #include <machine/bus.h> 4427d5dc18SMarcel Moolenaar #include <sys/rman.h> 4527d5dc18SMarcel Moolenaar #include <machine/resource.h> 4627d5dc18SMarcel Moolenaar #include <machine/stdarg.h> 4727d5dc18SMarcel Moolenaar 4827d5dc18SMarcel Moolenaar #include <dev/uart/uart.h> 4927d5dc18SMarcel Moolenaar #include <dev/uart/uart_bus.h> 5027d5dc18SMarcel Moolenaar #include <dev/uart/uart_cpu.h> 5127d5dc18SMarcel Moolenaar 5227d5dc18SMarcel Moolenaar #include "uart_if.h" 5327d5dc18SMarcel Moolenaar 5427d5dc18SMarcel Moolenaar devclass_t uart_devclass; 5586fb5400SMarius Strobl const char uart_driver_name[] = "uart"; 5627d5dc18SMarcel Moolenaar 5727d5dc18SMarcel Moolenaar SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs = 5827d5dc18SMarcel Moolenaar SLIST_HEAD_INITIALIZER(uart_sysdevs); 5927d5dc18SMarcel Moolenaar 60d745c852SEd Schouten static MALLOC_DEFINE(M_UART, "UART", "UART driver"); 6127d5dc18SMarcel Moolenaar 62332cda07SPeter Grehan #ifndef UART_POLL_FREQ 63332cda07SPeter Grehan #define UART_POLL_FREQ 50 64332cda07SPeter Grehan #endif 65332cda07SPeter Grehan static int uart_poll_freq = UART_POLL_FREQ; 6662145ff3SNeel Natu SYSCTL_INT(_debug, OID_AUTO, uart_poll_freq, CTLFLAG_RDTUN, &uart_poll_freq, 6762145ff3SNeel Natu 0, "UART poll frequency"); 6862145ff3SNeel Natu 6962145ff3SNeel Natu static int uart_force_poll; 7062145ff3SNeel Natu SYSCTL_INT(_debug, OID_AUTO, uart_force_poll, CTLFLAG_RDTUN, &uart_force_poll, 7162145ff3SNeel Natu 0, "Force UART polling"); 72332cda07SPeter Grehan 73*196d3019SIan Lepore #define PPS_MODE_DISABLED 0 74*196d3019SIan Lepore #define PPS_MODE_CTS 1 75*196d3019SIan Lepore #define PPS_MODE_DCD 2 76*196d3019SIan Lepore 77*196d3019SIan Lepore static inline int 78*196d3019SIan Lepore uart_pps_signal(int pps_mode) 79*196d3019SIan Lepore { 80*196d3019SIan Lepore 81*196d3019SIan Lepore switch(pps_mode) 82*196d3019SIan Lepore { 83*196d3019SIan Lepore case PPS_MODE_CTS: 84*196d3019SIan Lepore return (SER_CTS); 85*196d3019SIan Lepore case PPS_MODE_DCD: 86*196d3019SIan Lepore return (SER_DCD); 87*196d3019SIan Lepore } 88*196d3019SIan Lepore return (0); 89*196d3019SIan Lepore } 90*196d3019SIan Lepore static inline int 91*196d3019SIan Lepore uart_pps_mode_valid(int pps_mode) 92*196d3019SIan Lepore { 93*196d3019SIan Lepore 94*196d3019SIan Lepore switch(pps_mode) 95*196d3019SIan Lepore { 96*196d3019SIan Lepore case PPS_MODE_DISABLED: 97*196d3019SIan Lepore case PPS_MODE_CTS: 98*196d3019SIan Lepore case PPS_MODE_DCD: 99*196d3019SIan Lepore return (true); 100*196d3019SIan Lepore } 101*196d3019SIan Lepore return (false); 102*196d3019SIan Lepore } 103*196d3019SIan Lepore 104*196d3019SIan Lepore static const char * 105*196d3019SIan Lepore uart_pps_mode_name(int pps_mode) 106*196d3019SIan Lepore { 107*196d3019SIan Lepore switch(pps_mode) 108*196d3019SIan Lepore { 109*196d3019SIan Lepore case PPS_MODE_DISABLED: 110*196d3019SIan Lepore return ("disabled"); 111*196d3019SIan Lepore case PPS_MODE_CTS: 112*196d3019SIan Lepore return ("CTS"); 113*196d3019SIan Lepore case PPS_MODE_DCD: 114*196d3019SIan Lepore return ("DCD"); 115*196d3019SIan Lepore } 116*196d3019SIan Lepore return ("invalid"); 117*196d3019SIan Lepore } 118*196d3019SIan Lepore 119*196d3019SIan Lepore static int 120*196d3019SIan Lepore uart_pps_mode_sysctl(SYSCTL_HANDLER_ARGS) 121*196d3019SIan Lepore { 122*196d3019SIan Lepore struct uart_softc *sc; 123*196d3019SIan Lepore int err, tmp; 124*196d3019SIan Lepore 125*196d3019SIan Lepore sc = arg1; 126*196d3019SIan Lepore tmp = sc->sc_pps_mode; 127*196d3019SIan Lepore err = sysctl_handle_int(oidp, &tmp, 0, req); 128*196d3019SIan Lepore if (err != 0 || req->newptr == NULL) 129*196d3019SIan Lepore return (err); 130*196d3019SIan Lepore if (!uart_pps_mode_valid(tmp)) 131*196d3019SIan Lepore return (EINVAL); 132*196d3019SIan Lepore sc->sc_pps_mode = tmp; 133*196d3019SIan Lepore return(0); 134*196d3019SIan Lepore } 135*196d3019SIan Lepore 136*196d3019SIan Lepore static void 137*196d3019SIan Lepore uart_pps_init(struct uart_softc *sc) 138*196d3019SIan Lepore { 139*196d3019SIan Lepore struct sysctl_ctx_list *ctx; 140*196d3019SIan Lepore struct sysctl_oid *tree; 141*196d3019SIan Lepore 142*196d3019SIan Lepore ctx = device_get_sysctl_ctx(sc->sc_dev); 143*196d3019SIan Lepore tree = device_get_sysctl_tree(sc->sc_dev); 144*196d3019SIan Lepore 145*196d3019SIan Lepore /* 146*196d3019SIan Lepore * The historical default for pps capture mode is either DCD or CTS, 147*196d3019SIan Lepore * depending on the UART_PPS_ON_CTS kernel option. Start with that, 148*196d3019SIan Lepore * then try to fetch the tunable that overrides the mode for all uart 149*196d3019SIan Lepore * devices, then try to fetch the sysctl-tunable that overrides the mode 150*196d3019SIan Lepore * for one specific device. 151*196d3019SIan Lepore */ 152*196d3019SIan Lepore #ifdef UART_PPS_ON_CTS 153*196d3019SIan Lepore sc->sc_pps_mode = PPS_MODE_CTS; 154*196d3019SIan Lepore #else 155*196d3019SIan Lepore sc->sc_pps_mode = PPS_MODE_DCD; 156*196d3019SIan Lepore #endif 157*196d3019SIan Lepore TUNABLE_INT_FETCH("hw.uart.pps_mode", &sc->sc_pps_mode); 158*196d3019SIan Lepore SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "pps_mode", 159*196d3019SIan Lepore CTLTYPE_INT | CTLFLAG_RWTUN, sc, 0, uart_pps_mode_sysctl, "I", 160*196d3019SIan Lepore "pulse capturing mode - 0/1/2 - disabled/CTS/DCD"); 161*196d3019SIan Lepore 162*196d3019SIan Lepore if (!uart_pps_mode_valid(sc->sc_pps_mode)) { 163*196d3019SIan Lepore device_printf(sc->sc_dev, 164*196d3019SIan Lepore "Invalid pps_mode %d configured; disabling PPS capture\n", 165*196d3019SIan Lepore sc->sc_pps_mode); 166*196d3019SIan Lepore sc->sc_pps_mode = PPS_MODE_DISABLED; 167*196d3019SIan Lepore } else if (bootverbose) { 168*196d3019SIan Lepore device_printf(sc->sc_dev, "PPS capture mode %d (%s)\n", 169*196d3019SIan Lepore sc->sc_pps_mode, uart_pps_mode_name(sc->sc_pps_mode)); 170*196d3019SIan Lepore } 171*196d3019SIan Lepore 172*196d3019SIan Lepore sc->sc_pps.ppscap = PPS_CAPTUREBOTH; 173*196d3019SIan Lepore sc->sc_pps.driver_mtx = uart_tty_getlock(sc); 174*196d3019SIan Lepore sc->sc_pps.driver_abi = PPS_ABI_VERSION; 175*196d3019SIan Lepore pps_init_abi(&sc->sc_pps); 176*196d3019SIan Lepore } 177*196d3019SIan Lepore 17827d5dc18SMarcel Moolenaar void 17927d5dc18SMarcel Moolenaar uart_add_sysdev(struct uart_devinfo *di) 18027d5dc18SMarcel Moolenaar { 18127d5dc18SMarcel Moolenaar SLIST_INSERT_HEAD(&uart_sysdevs, di, next); 18227d5dc18SMarcel Moolenaar } 18327d5dc18SMarcel Moolenaar 184f8100ce2SMarcel Moolenaar const char * 185f8100ce2SMarcel Moolenaar uart_getname(struct uart_class *uc) 186f8100ce2SMarcel Moolenaar { 187f8100ce2SMarcel Moolenaar return ((uc != NULL) ? uc->name : NULL); 188f8100ce2SMarcel Moolenaar } 189f8100ce2SMarcel Moolenaar 190f8100ce2SMarcel Moolenaar struct uart_ops * 191f8100ce2SMarcel Moolenaar uart_getops(struct uart_class *uc) 192f8100ce2SMarcel Moolenaar { 193f8100ce2SMarcel Moolenaar return ((uc != NULL) ? uc->uc_ops : NULL); 194f8100ce2SMarcel Moolenaar } 195f8100ce2SMarcel Moolenaar 196f8100ce2SMarcel Moolenaar int 197f8100ce2SMarcel Moolenaar uart_getrange(struct uart_class *uc) 198f8100ce2SMarcel Moolenaar { 199f8100ce2SMarcel Moolenaar return ((uc != NULL) ? uc->uc_range : 0); 200f8100ce2SMarcel Moolenaar } 201f8100ce2SMarcel Moolenaar 202405ada37SAndrew Turner u_int 203405ada37SAndrew Turner uart_getregshift(struct uart_class *uc) 204405ada37SAndrew Turner { 205405ada37SAndrew Turner return ((uc != NULL) ? uc->uc_rshift : 0); 206405ada37SAndrew Turner } 207405ada37SAndrew Turner 20827d5dc18SMarcel Moolenaar /* 2098af03381SMarcel Moolenaar * Schedule a soft interrupt. We do this on the 0 to !0 transition 2108af03381SMarcel Moolenaar * of the TTY pending interrupt status. 2118af03381SMarcel Moolenaar */ 2120acb3c4aSMarcel Moolenaar void 2138af03381SMarcel Moolenaar uart_sched_softih(struct uart_softc *sc, uint32_t ipend) 2148af03381SMarcel Moolenaar { 2158af03381SMarcel Moolenaar uint32_t new, old; 2168af03381SMarcel Moolenaar 2178af03381SMarcel Moolenaar do { 2188af03381SMarcel Moolenaar old = sc->sc_ttypend; 2198af03381SMarcel Moolenaar new = old | ipend; 2208af03381SMarcel Moolenaar } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new)); 2218af03381SMarcel Moolenaar 2228af03381SMarcel Moolenaar if ((old & SER_INT_MASK) == 0) 2238af03381SMarcel Moolenaar swi_sched(sc->sc_softih, 0); 2248af03381SMarcel Moolenaar } 2258af03381SMarcel Moolenaar 2268af03381SMarcel Moolenaar /* 22727d5dc18SMarcel Moolenaar * A break condition has been detected. We treat the break condition as 22827d5dc18SMarcel Moolenaar * a special case that should not happen during normal operation. When 22927d5dc18SMarcel Moolenaar * the break condition is to be passed to higher levels in the form of 23027d5dc18SMarcel Moolenaar * a NUL character, we really want the break to be in the right place in 23127d5dc18SMarcel Moolenaar * the input stream. The overhead to achieve that is not in relation to 23227d5dc18SMarcel Moolenaar * the exceptional nature of the break condition, so we permit ourselves 23327d5dc18SMarcel Moolenaar * to be sloppy. 23427d5dc18SMarcel Moolenaar */ 2358af03381SMarcel Moolenaar static __inline int 2368af03381SMarcel Moolenaar uart_intr_break(void *arg) 23727d5dc18SMarcel Moolenaar { 2388af03381SMarcel Moolenaar struct uart_softc *sc = arg; 23927d5dc18SMarcel Moolenaar 2404cf75455SRobert Watson #if defined(KDB) 24127d5dc18SMarcel Moolenaar if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 2424cf75455SRobert Watson if (kdb_break()) 2438af03381SMarcel Moolenaar return (0); 24427d5dc18SMarcel Moolenaar } 24527d5dc18SMarcel Moolenaar #endif 24627d5dc18SMarcel Moolenaar if (sc->sc_opened) 2478af03381SMarcel Moolenaar uart_sched_softih(sc, SER_INT_BREAK); 2488af03381SMarcel Moolenaar return (0); 24927d5dc18SMarcel Moolenaar } 25027d5dc18SMarcel Moolenaar 25127d5dc18SMarcel Moolenaar /* 25227d5dc18SMarcel Moolenaar * Handle a receiver overrun situation. We lost at least 1 byte in the 25327d5dc18SMarcel Moolenaar * input stream and it's our job to contain the situation. We grab as 25427d5dc18SMarcel Moolenaar * much of the data we can, but otherwise flush the receiver FIFO to 25527d5dc18SMarcel Moolenaar * create some breathing room. The net effect is that we avoid the 25627d5dc18SMarcel Moolenaar * overrun condition to happen for the next X characters, where X is 257a164074fSEitan Adler * related to the FIFO size at the cost of losing data right away. 25827d5dc18SMarcel Moolenaar * So, instead of having multiple overrun interrupts in close proximity 25927d5dc18SMarcel Moolenaar * to each other and possibly pessimizing UART interrupt latency for 26027d5dc18SMarcel Moolenaar * other UARTs in a multiport configuration, we create a longer segment 26127d5dc18SMarcel Moolenaar * of missing characters by freeing up the FIFO. 26227d5dc18SMarcel Moolenaar * Each overrun condition is marked in the input buffer by a token. The 26327d5dc18SMarcel Moolenaar * token represents the loss of at least one, but possible more bytes in 26427d5dc18SMarcel Moolenaar * the input stream. 26527d5dc18SMarcel Moolenaar */ 2668af03381SMarcel Moolenaar static __inline int 2678af03381SMarcel Moolenaar uart_intr_overrun(void *arg) 26827d5dc18SMarcel Moolenaar { 2698af03381SMarcel Moolenaar struct uart_softc *sc = arg; 27027d5dc18SMarcel Moolenaar 27127d5dc18SMarcel Moolenaar if (sc->sc_opened) { 27227d5dc18SMarcel Moolenaar UART_RECEIVE(sc); 27327d5dc18SMarcel Moolenaar if (uart_rx_put(sc, UART_STAT_OVERRUN)) 27427d5dc18SMarcel Moolenaar sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN; 2758af03381SMarcel Moolenaar uart_sched_softih(sc, SER_INT_RXREADY); 27627d5dc18SMarcel Moolenaar } 27727d5dc18SMarcel Moolenaar UART_FLUSH(sc, UART_FLUSH_RECEIVER); 2788af03381SMarcel Moolenaar return (0); 27927d5dc18SMarcel Moolenaar } 28027d5dc18SMarcel Moolenaar 28127d5dc18SMarcel Moolenaar /* 28227d5dc18SMarcel Moolenaar * Received data ready. 28327d5dc18SMarcel Moolenaar */ 2848af03381SMarcel Moolenaar static __inline int 2858af03381SMarcel Moolenaar uart_intr_rxready(void *arg) 28627d5dc18SMarcel Moolenaar { 2878af03381SMarcel Moolenaar struct uart_softc *sc = arg; 28827d5dc18SMarcel Moolenaar int rxp; 28927d5dc18SMarcel Moolenaar 29027d5dc18SMarcel Moolenaar rxp = sc->sc_rxput; 29127d5dc18SMarcel Moolenaar UART_RECEIVE(sc); 2924cf75455SRobert Watson #if defined(KDB) 29327d5dc18SMarcel Moolenaar if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 29427d5dc18SMarcel Moolenaar while (rxp != sc->sc_rxput) { 2954cf75455SRobert Watson kdb_alt_break(sc->sc_rxbuf[rxp++], &sc->sc_altbrk); 29627d5dc18SMarcel Moolenaar if (rxp == sc->sc_rxbufsz) 29727d5dc18SMarcel Moolenaar rxp = 0; 29827d5dc18SMarcel Moolenaar } 29927d5dc18SMarcel Moolenaar } 30027d5dc18SMarcel Moolenaar #endif 30127d5dc18SMarcel Moolenaar if (sc->sc_opened) 3028af03381SMarcel Moolenaar uart_sched_softih(sc, SER_INT_RXREADY); 30327d5dc18SMarcel Moolenaar else 30427d5dc18SMarcel Moolenaar sc->sc_rxput = sc->sc_rxget; /* Ignore received data. */ 3058af03381SMarcel Moolenaar return (1); 30627d5dc18SMarcel Moolenaar } 30727d5dc18SMarcel Moolenaar 30827d5dc18SMarcel Moolenaar /* 30927d5dc18SMarcel Moolenaar * Line or modem status change (OOB signalling). 31027d5dc18SMarcel Moolenaar * We pass the signals to the software interrupt handler for further 31127d5dc18SMarcel Moolenaar * processing. Note that we merge the delta bits, but set the state 312a164074fSEitan Adler * bits. This is to avoid losing state transitions due to having more 31327d5dc18SMarcel Moolenaar * than 1 hardware interrupt between software interrupts. 31427d5dc18SMarcel Moolenaar */ 3158af03381SMarcel Moolenaar static __inline int 3168af03381SMarcel Moolenaar uart_intr_sigchg(void *arg) 31727d5dc18SMarcel Moolenaar { 3188af03381SMarcel Moolenaar struct uart_softc *sc = arg; 319*196d3019SIan Lepore int new, old, pps_sig, sig; 32027d5dc18SMarcel Moolenaar 32127d5dc18SMarcel Moolenaar sig = UART_GETSIG(sc); 3228194412bSMarcel Moolenaar 3238194412bSMarcel Moolenaar if (sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) { 324*196d3019SIan Lepore pps_sig = uart_pps_signal(sc->sc_pps_mode); 325*196d3019SIan Lepore if (sig & SER_DELTA(pps_sig)) { 3268194412bSMarcel Moolenaar pps_capture(&sc->sc_pps); 327*196d3019SIan Lepore pps_event(&sc->sc_pps, (sig & pps_sig) ? 3288194412bSMarcel Moolenaar PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 3298194412bSMarcel Moolenaar } 3308194412bSMarcel Moolenaar } 3318194412bSMarcel Moolenaar 3328af03381SMarcel Moolenaar /* 3338af03381SMarcel Moolenaar * Keep track of signal changes, even when the device is not 3348af03381SMarcel Moolenaar * opened. This allows us to inform upper layers about a 3358af03381SMarcel Moolenaar * possible loss of DCD and thus the existence of a (possibly) 3368af03381SMarcel Moolenaar * different connection when we have DCD back, during the time 3378af03381SMarcel Moolenaar * that the device was closed. 3388af03381SMarcel Moolenaar */ 33927d5dc18SMarcel Moolenaar do { 34027d5dc18SMarcel Moolenaar old = sc->sc_ttypend; 341ea549414SMarcel Moolenaar new = old & ~SER_MASK_STATE; 3422d511805SMarcel Moolenaar new |= sig & SER_INT_SIGMASK; 34327d5dc18SMarcel Moolenaar } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new)); 3448af03381SMarcel Moolenaar 3458af03381SMarcel Moolenaar if (sc->sc_opened) 3468af03381SMarcel Moolenaar uart_sched_softih(sc, SER_INT_SIGCHG); 3478af03381SMarcel Moolenaar return (1); 34827d5dc18SMarcel Moolenaar } 34927d5dc18SMarcel Moolenaar 35027d5dc18SMarcel Moolenaar /* 35127d5dc18SMarcel Moolenaar * The transmitter can accept more data. 35227d5dc18SMarcel Moolenaar */ 3538af03381SMarcel Moolenaar static __inline int 3548af03381SMarcel Moolenaar uart_intr_txidle(void *arg) 35527d5dc18SMarcel Moolenaar { 3568af03381SMarcel Moolenaar struct uart_softc *sc = arg; 3578af03381SMarcel Moolenaar 35827d5dc18SMarcel Moolenaar if (sc->sc_txbusy) { 35927d5dc18SMarcel Moolenaar sc->sc_txbusy = 0; 3608af03381SMarcel Moolenaar uart_sched_softih(sc, SER_INT_TXIDLE); 36127d5dc18SMarcel Moolenaar } 3628af03381SMarcel Moolenaar return (0); 36327d5dc18SMarcel Moolenaar } 36427d5dc18SMarcel Moolenaar 365ef544f63SPaolo Pisati static int 36627d5dc18SMarcel Moolenaar uart_intr(void *arg) 36727d5dc18SMarcel Moolenaar { 36827d5dc18SMarcel Moolenaar struct uart_softc *sc = arg; 36986fb5400SMarius Strobl int cnt, ipend, testintr; 37027d5dc18SMarcel Moolenaar 371eead2d55SMarcel Moolenaar if (sc->sc_leaving) 372eead2d55SMarcel Moolenaar return (FILTER_STRAY); 373eead2d55SMarcel Moolenaar 374eead2d55SMarcel Moolenaar cnt = 0; 37586fb5400SMarius Strobl testintr = sc->sc_testintr; 37686fb5400SMarius Strobl while ((!testintr || cnt < 20) && (ipend = UART_IPEND(sc)) != 0) { 377eead2d55SMarcel Moolenaar cnt++; 3782d511805SMarcel Moolenaar if (ipend & SER_INT_OVERRUN) 37927d5dc18SMarcel Moolenaar uart_intr_overrun(sc); 3802d511805SMarcel Moolenaar if (ipend & SER_INT_BREAK) 38127d5dc18SMarcel Moolenaar uart_intr_break(sc); 3822d511805SMarcel Moolenaar if (ipend & SER_INT_RXREADY) 38327d5dc18SMarcel Moolenaar uart_intr_rxready(sc); 3842d511805SMarcel Moolenaar if (ipend & SER_INT_SIGCHG) 38527d5dc18SMarcel Moolenaar uart_intr_sigchg(sc); 3862d511805SMarcel Moolenaar if (ipend & SER_INT_TXIDLE) 38727d5dc18SMarcel Moolenaar uart_intr_txidle(sc); 3888af03381SMarcel Moolenaar } 389332cda07SPeter Grehan 390332cda07SPeter Grehan if (sc->sc_polled) { 391332cda07SPeter Grehan callout_reset(&sc->sc_timer, hz / uart_poll_freq, 392332cda07SPeter Grehan (timeout_t *)uart_intr, sc); 393332cda07SPeter Grehan } 394332cda07SPeter Grehan 395eead2d55SMarcel Moolenaar return ((cnt == 0) ? FILTER_STRAY : 39686fb5400SMarius Strobl ((testintr && cnt == 20) ? FILTER_SCHEDULE_THREAD : 39786fb5400SMarius Strobl FILTER_HANDLED)); 3988af03381SMarcel Moolenaar } 39927d5dc18SMarcel Moolenaar 4008af03381SMarcel Moolenaar serdev_intr_t * 4018af03381SMarcel Moolenaar uart_bus_ihand(device_t dev, int ipend) 4028af03381SMarcel Moolenaar { 4038af03381SMarcel Moolenaar 4048af03381SMarcel Moolenaar switch (ipend) { 4058af03381SMarcel Moolenaar case SER_INT_BREAK: 4068af03381SMarcel Moolenaar return (uart_intr_break); 4078af03381SMarcel Moolenaar case SER_INT_OVERRUN: 4088af03381SMarcel Moolenaar return (uart_intr_overrun); 4098af03381SMarcel Moolenaar case SER_INT_RXREADY: 4108af03381SMarcel Moolenaar return (uart_intr_rxready); 4118af03381SMarcel Moolenaar case SER_INT_SIGCHG: 4128af03381SMarcel Moolenaar return (uart_intr_sigchg); 4138af03381SMarcel Moolenaar case SER_INT_TXIDLE: 4148af03381SMarcel Moolenaar return (uart_intr_txidle); 4158af03381SMarcel Moolenaar } 4168af03381SMarcel Moolenaar return (NULL); 4178af03381SMarcel Moolenaar } 4188af03381SMarcel Moolenaar 4198af03381SMarcel Moolenaar int 420a31f91a0SMarcel Moolenaar uart_bus_ipend(device_t dev) 421a31f91a0SMarcel Moolenaar { 422a31f91a0SMarcel Moolenaar struct uart_softc *sc; 423a31f91a0SMarcel Moolenaar 424a31f91a0SMarcel Moolenaar sc = device_get_softc(dev); 425a31f91a0SMarcel Moolenaar return (UART_IPEND(sc)); 426a31f91a0SMarcel Moolenaar } 427a31f91a0SMarcel Moolenaar 428a31f91a0SMarcel Moolenaar int 4298af03381SMarcel Moolenaar uart_bus_sysdev(device_t dev) 4308af03381SMarcel Moolenaar { 4318af03381SMarcel Moolenaar struct uart_softc *sc; 4328af03381SMarcel Moolenaar 4338af03381SMarcel Moolenaar sc = device_get_softc(dev); 4348af03381SMarcel Moolenaar return ((sc->sc_sysdev != NULL) ? 1 : 0); 43527d5dc18SMarcel Moolenaar } 43627d5dc18SMarcel Moolenaar 43727d5dc18SMarcel Moolenaar int 438875f70dbSMarcel Moolenaar uart_bus_probe(device_t dev, int regshft, int rclk, int rid, int chan) 43927d5dc18SMarcel Moolenaar { 44027d5dc18SMarcel Moolenaar struct uart_softc *sc; 44127d5dc18SMarcel Moolenaar struct uart_devinfo *sysdev; 44227d5dc18SMarcel Moolenaar int error; 44327d5dc18SMarcel Moolenaar 444f8100ce2SMarcel Moolenaar sc = device_get_softc(dev); 445f8100ce2SMarcel Moolenaar 446f8100ce2SMarcel Moolenaar /* 447f8100ce2SMarcel Moolenaar * All uart_class references are weak. Check that the needed 448f8100ce2SMarcel Moolenaar * class has been compiled-in. Fail if not. 449f8100ce2SMarcel Moolenaar */ 450f8100ce2SMarcel Moolenaar if (sc->sc_class == NULL) 451f8100ce2SMarcel Moolenaar return (ENXIO); 452f8100ce2SMarcel Moolenaar 45327d5dc18SMarcel Moolenaar /* 45427d5dc18SMarcel Moolenaar * Initialize the instance. Note that the instance (=softc) does 45527d5dc18SMarcel Moolenaar * not necessarily match the hardware specific softc. We can't do 45627d5dc18SMarcel Moolenaar * anything about it now, because we may not attach to the device. 45727d5dc18SMarcel Moolenaar * Hardware drivers cannot use any of the class specific fields 45827d5dc18SMarcel Moolenaar * while probing. 45927d5dc18SMarcel Moolenaar */ 46027d5dc18SMarcel Moolenaar kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class); 46127d5dc18SMarcel Moolenaar sc->sc_dev = dev; 46227d5dc18SMarcel Moolenaar if (device_get_desc(dev) == NULL) 463f8100ce2SMarcel Moolenaar device_set_desc(dev, uart_getname(sc->sc_class)); 46427d5dc18SMarcel Moolenaar 46527d5dc18SMarcel Moolenaar /* 46627d5dc18SMarcel Moolenaar * Allocate the register resource. We assume that all UARTs have 46727d5dc18SMarcel Moolenaar * a single register window in either I/O port space or memory 46827d5dc18SMarcel Moolenaar * mapped I/O space. Any UART that needs multiple windows will 46927d5dc18SMarcel Moolenaar * consequently not be supported by this driver as-is. We try I/O 47027d5dc18SMarcel Moolenaar * port space first because that's the common case. 47127d5dc18SMarcel Moolenaar */ 47227d5dc18SMarcel Moolenaar sc->sc_rrid = rid; 47327d5dc18SMarcel Moolenaar sc->sc_rtype = SYS_RES_IOPORT; 47427d5dc18SMarcel Moolenaar sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid, 475f8100ce2SMarcel Moolenaar 0, ~0, uart_getrange(sc->sc_class), RF_ACTIVE); 47627d5dc18SMarcel Moolenaar if (sc->sc_rres == NULL) { 47727d5dc18SMarcel Moolenaar sc->sc_rrid = rid; 47827d5dc18SMarcel Moolenaar sc->sc_rtype = SYS_RES_MEMORY; 47927d5dc18SMarcel Moolenaar sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, 480f8100ce2SMarcel Moolenaar &sc->sc_rrid, 0, ~0, uart_getrange(sc->sc_class), 481f8100ce2SMarcel Moolenaar RF_ACTIVE); 48227d5dc18SMarcel Moolenaar if (sc->sc_rres == NULL) 48327d5dc18SMarcel Moolenaar return (ENXIO); 48427d5dc18SMarcel Moolenaar } 48527d5dc18SMarcel Moolenaar 48627d5dc18SMarcel Moolenaar /* 48727d5dc18SMarcel Moolenaar * Fill in the bus access structure and compare this device with 48827d5dc18SMarcel Moolenaar * a possible console device and/or a debug port. We set the flags 48927d5dc18SMarcel Moolenaar * in the softc so that the hardware dependent probe can adjust 49027d5dc18SMarcel Moolenaar * accordingly. In general, you don't want to permanently disrupt 49127d5dc18SMarcel Moolenaar * console I/O. 49227d5dc18SMarcel Moolenaar */ 49327d5dc18SMarcel Moolenaar sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 49427d5dc18SMarcel Moolenaar sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 495875f70dbSMarcel Moolenaar sc->sc_bas.chan = chan; 49627d5dc18SMarcel Moolenaar sc->sc_bas.regshft = regshft; 49727d5dc18SMarcel Moolenaar sc->sc_bas.rclk = (rclk == 0) ? sc->sc_class->uc_rclk : rclk; 49827d5dc18SMarcel Moolenaar 49927d5dc18SMarcel Moolenaar SLIST_FOREACH(sysdev, &uart_sysdevs, next) { 500875f70dbSMarcel Moolenaar if (chan == sysdev->bas.chan && 501875f70dbSMarcel Moolenaar uart_cpu_eqres(&sc->sc_bas, &sysdev->bas)) { 50227d5dc18SMarcel Moolenaar /* XXX check if ops matches class. */ 50327d5dc18SMarcel Moolenaar sc->sc_sysdev = sysdev; 5041c5e367bSMarcel Moolenaar sysdev->bas.rclk = sc->sc_bas.rclk; 50527d5dc18SMarcel Moolenaar } 50627d5dc18SMarcel Moolenaar } 50727d5dc18SMarcel Moolenaar 50827d5dc18SMarcel Moolenaar error = UART_PROBE(sc); 50927d5dc18SMarcel Moolenaar bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 510b923a710SMarcel Moolenaar return ((error) ? error : BUS_PROBE_DEFAULT); 51127d5dc18SMarcel Moolenaar } 51227d5dc18SMarcel Moolenaar 51327d5dc18SMarcel Moolenaar int 51427d5dc18SMarcel Moolenaar uart_bus_attach(device_t dev) 51527d5dc18SMarcel Moolenaar { 51627d5dc18SMarcel Moolenaar struct uart_softc *sc, *sc0; 51727d5dc18SMarcel Moolenaar const char *sep; 518eead2d55SMarcel Moolenaar int error, filt; 51927d5dc18SMarcel Moolenaar 52027d5dc18SMarcel Moolenaar /* 52127d5dc18SMarcel Moolenaar * The sc_class field defines the type of UART we're going to work 52227d5dc18SMarcel Moolenaar * with and thus the size of the softc. Replace the generic softc 52327d5dc18SMarcel Moolenaar * with one that matches the UART now that we're certain we handle 52427d5dc18SMarcel Moolenaar * the device. 52527d5dc18SMarcel Moolenaar */ 52627d5dc18SMarcel Moolenaar sc0 = device_get_softc(dev); 52727d5dc18SMarcel Moolenaar if (sc0->sc_class->size > sizeof(*sc)) { 52827d5dc18SMarcel Moolenaar sc = malloc(sc0->sc_class->size, M_UART, M_WAITOK|M_ZERO); 52927d5dc18SMarcel Moolenaar bcopy(sc0, sc, sizeof(*sc)); 53027d5dc18SMarcel Moolenaar device_set_softc(dev, sc); 53127d5dc18SMarcel Moolenaar } else 53227d5dc18SMarcel Moolenaar sc = sc0; 53327d5dc18SMarcel Moolenaar 53427d5dc18SMarcel Moolenaar /* 535d76a1ef4SWarner Losh * Now that we know the softc for this device, connect the back 536d76a1ef4SWarner Losh * pointer from the sysdev for this device, if any 537d76a1ef4SWarner Losh */ 538d76a1ef4SWarner Losh if (sc->sc_sysdev != NULL) 539d76a1ef4SWarner Losh sc->sc_sysdev->sc = sc; 540d76a1ef4SWarner Losh 541d76a1ef4SWarner Losh /* 54227d5dc18SMarcel Moolenaar * Protect ourselves against interrupts while we're not completely 54327d5dc18SMarcel Moolenaar * finished attaching and initializing. We don't expect interrupts 54486fb5400SMarius Strobl * until after UART_ATTACH(), though. 54527d5dc18SMarcel Moolenaar */ 54627d5dc18SMarcel Moolenaar sc->sc_leaving = 1; 54727d5dc18SMarcel Moolenaar 5488af03381SMarcel Moolenaar mtx_init(&sc->sc_hwmtx_s, "uart_hwmtx", NULL, MTX_SPIN); 5498af03381SMarcel Moolenaar if (sc->sc_hwmtx == NULL) 5508af03381SMarcel Moolenaar sc->sc_hwmtx = &sc->sc_hwmtx_s; 55106287620SMarcel Moolenaar 55227d5dc18SMarcel Moolenaar /* 55327d5dc18SMarcel Moolenaar * Re-allocate. We expect that the softc contains the information 55427d5dc18SMarcel Moolenaar * collected by uart_bus_probe() intact. 55527d5dc18SMarcel Moolenaar */ 55627d5dc18SMarcel Moolenaar sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid, 557f8100ce2SMarcel Moolenaar 0, ~0, uart_getrange(sc->sc_class), RF_ACTIVE); 5582682e7b6SMarius Strobl if (sc->sc_rres == NULL) { 5598af03381SMarcel Moolenaar mtx_destroy(&sc->sc_hwmtx_s); 56027d5dc18SMarcel Moolenaar return (ENXIO); 5612682e7b6SMarius Strobl } 562b8759afcSYoshihiro Takahashi sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 563b8759afcSYoshihiro Takahashi sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 564b8759afcSYoshihiro Takahashi 5653ad36a41SIan Lepore /* 5663ad36a41SIan Lepore * Ensure there is room for at least three full FIFOs of data in the 5673ad36a41SIan Lepore * receive buffer (handles the case of low-level drivers with huge 5683ad36a41SIan Lepore * FIFOs), and also ensure that there is no less than the historical 5693ad36a41SIan Lepore * size of 384 bytes (handles the typical small-FIFO case). 5703ad36a41SIan Lepore */ 5713ad36a41SIan Lepore sc->sc_rxbufsz = MAX(384, sc->sc_rxfifosz * 3); 57227d5dc18SMarcel Moolenaar sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf), 57327d5dc18SMarcel Moolenaar M_UART, M_WAITOK); 57427d5dc18SMarcel Moolenaar sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf), 57527d5dc18SMarcel Moolenaar M_UART, M_WAITOK); 57627d5dc18SMarcel Moolenaar 57727d5dc18SMarcel Moolenaar error = UART_ATTACH(sc); 57827d5dc18SMarcel Moolenaar if (error) 57927d5dc18SMarcel Moolenaar goto fail; 58027d5dc18SMarcel Moolenaar 58127d5dc18SMarcel Moolenaar if (sc->sc_hwiflow || sc->sc_hwoflow) { 58227d5dc18SMarcel Moolenaar sep = ""; 58327d5dc18SMarcel Moolenaar device_print_prettyname(dev); 58427d5dc18SMarcel Moolenaar if (sc->sc_hwiflow) { 58527d5dc18SMarcel Moolenaar printf("%sRTS iflow", sep); 58627d5dc18SMarcel Moolenaar sep = ", "; 58727d5dc18SMarcel Moolenaar } 58827d5dc18SMarcel Moolenaar if (sc->sc_hwoflow) { 58927d5dc18SMarcel Moolenaar printf("%sCTS oflow", sep); 59027d5dc18SMarcel Moolenaar sep = ", "; 59127d5dc18SMarcel Moolenaar } 59227d5dc18SMarcel Moolenaar printf("\n"); 59327d5dc18SMarcel Moolenaar } 59427d5dc18SMarcel Moolenaar 59527d5dc18SMarcel Moolenaar if (sc->sc_sysdev != NULL) { 5965dceed8aSWarner Losh if (sc->sc_sysdev->baudrate == 0) { 59754e2bcc7SMarcel Moolenaar if (UART_IOCTL(sc, UART_IOCTL_BAUD, 59854e2bcc7SMarcel Moolenaar (intptr_t)&sc->sc_sysdev->baudrate) != 0) 59954e2bcc7SMarcel Moolenaar sc->sc_sysdev->baudrate = -1; 60054e2bcc7SMarcel Moolenaar } 60127d5dc18SMarcel Moolenaar switch (sc->sc_sysdev->type) { 60227d5dc18SMarcel Moolenaar case UART_DEV_CONSOLE: 60327d5dc18SMarcel Moolenaar device_printf(dev, "console"); 60427d5dc18SMarcel Moolenaar break; 60527d5dc18SMarcel Moolenaar case UART_DEV_DBGPORT: 60627d5dc18SMarcel Moolenaar device_printf(dev, "debug port"); 60727d5dc18SMarcel Moolenaar break; 60827d5dc18SMarcel Moolenaar case UART_DEV_KEYBOARD: 60927d5dc18SMarcel Moolenaar device_printf(dev, "keyboard"); 61027d5dc18SMarcel Moolenaar break; 61127d5dc18SMarcel Moolenaar default: 61227d5dc18SMarcel Moolenaar device_printf(dev, "unknown system device"); 61327d5dc18SMarcel Moolenaar break; 61427d5dc18SMarcel Moolenaar } 61527d5dc18SMarcel Moolenaar printf(" (%d,%c,%d,%d)\n", sc->sc_sysdev->baudrate, 61627d5dc18SMarcel Moolenaar "noems"[sc->sc_sysdev->parity], sc->sc_sysdev->databits, 61727d5dc18SMarcel Moolenaar sc->sc_sysdev->stopbits); 61827d5dc18SMarcel Moolenaar } 61927d5dc18SMarcel Moolenaar 620eead2d55SMarcel Moolenaar sc->sc_leaving = 0; 62186fb5400SMarius Strobl sc->sc_testintr = 1; 622eead2d55SMarcel Moolenaar filt = uart_intr(sc); 62386fb5400SMarius Strobl sc->sc_testintr = 0; 624eead2d55SMarcel Moolenaar 625eead2d55SMarcel Moolenaar /* 626eead2d55SMarcel Moolenaar * Don't use interrupts if we couldn't clear any pending interrupt 627eead2d55SMarcel Moolenaar * conditions. We may have broken H/W and polling is probably the 628eead2d55SMarcel Moolenaar * safest thing to do. 629eead2d55SMarcel Moolenaar */ 63062145ff3SNeel Natu if (filt != FILTER_SCHEDULE_THREAD && !uart_force_poll) { 631eead2d55SMarcel Moolenaar sc->sc_irid = 0; 632eead2d55SMarcel Moolenaar sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, 633eead2d55SMarcel Moolenaar &sc->sc_irid, RF_ACTIVE | RF_SHAREABLE); 634eead2d55SMarcel Moolenaar } 635eead2d55SMarcel Moolenaar if (sc->sc_ires != NULL) { 636eead2d55SMarcel Moolenaar error = bus_setup_intr(dev, sc->sc_ires, INTR_TYPE_TTY, 637eead2d55SMarcel Moolenaar uart_intr, NULL, sc, &sc->sc_icookie); 638eead2d55SMarcel Moolenaar sc->sc_fastintr = (error == 0) ? 1 : 0; 639eead2d55SMarcel Moolenaar 640eead2d55SMarcel Moolenaar if (!sc->sc_fastintr) 641eead2d55SMarcel Moolenaar error = bus_setup_intr(dev, sc->sc_ires, 642eead2d55SMarcel Moolenaar INTR_TYPE_TTY | INTR_MPSAFE, NULL, 643eead2d55SMarcel Moolenaar (driver_intr_t *)uart_intr, sc, &sc->sc_icookie); 644eead2d55SMarcel Moolenaar 645eead2d55SMarcel Moolenaar if (error) { 646eead2d55SMarcel Moolenaar device_printf(dev, "could not activate interrupt\n"); 647eead2d55SMarcel Moolenaar bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 648eead2d55SMarcel Moolenaar sc->sc_ires); 649eead2d55SMarcel Moolenaar sc->sc_ires = NULL; 650eead2d55SMarcel Moolenaar } 651eead2d55SMarcel Moolenaar } 652eead2d55SMarcel Moolenaar if (sc->sc_ires == NULL) { 653eead2d55SMarcel Moolenaar /* No interrupt resource. Force polled mode. */ 654eead2d55SMarcel Moolenaar sc->sc_polled = 1; 655eead2d55SMarcel Moolenaar callout_init(&sc->sc_timer, 1); 65662145ff3SNeel Natu callout_reset(&sc->sc_timer, hz / uart_poll_freq, 65762145ff3SNeel Natu (timeout_t *)uart_intr, sc); 658eead2d55SMarcel Moolenaar } 659eead2d55SMarcel Moolenaar 660eead2d55SMarcel Moolenaar if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) { 661eead2d55SMarcel Moolenaar sep = ""; 662eead2d55SMarcel Moolenaar device_print_prettyname(dev); 663eead2d55SMarcel Moolenaar if (sc->sc_fastintr) { 664eead2d55SMarcel Moolenaar printf("%sfast interrupt", sep); 665eead2d55SMarcel Moolenaar sep = ", "; 666eead2d55SMarcel Moolenaar } 667eead2d55SMarcel Moolenaar if (sc->sc_polled) { 6681662b008SIan Lepore printf("%spolled mode (%dHz)", sep, uart_poll_freq); 669eead2d55SMarcel Moolenaar sep = ", "; 670eead2d55SMarcel Moolenaar } 671eead2d55SMarcel Moolenaar printf("\n"); 672eead2d55SMarcel Moolenaar } 673eead2d55SMarcel Moolenaar 674b59236ceSIan Lepore if (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL) { 675b59236ceSIan Lepore if ((error = sc->sc_sysdev->attach(sc)) != 0) 67627d5dc18SMarcel Moolenaar goto fail; 677b59236ceSIan Lepore } else { 678b59236ceSIan Lepore if ((error = uart_tty_attach(sc)) != 0) 679b59236ceSIan Lepore goto fail; 680*196d3019SIan Lepore uart_pps_init(sc); 681b59236ceSIan Lepore } 68227d5dc18SMarcel Moolenaar 6838af03381SMarcel Moolenaar if (sc->sc_sysdev != NULL) 6848af03381SMarcel Moolenaar sc->sc_sysdev->hwmtx = sc->sc_hwmtx; 6858af03381SMarcel Moolenaar 68627d5dc18SMarcel Moolenaar return (0); 68727d5dc18SMarcel Moolenaar 68827d5dc18SMarcel Moolenaar fail: 68927d5dc18SMarcel Moolenaar free(sc->sc_txbuf, M_UART); 69027d5dc18SMarcel Moolenaar free(sc->sc_rxbuf, M_UART); 69127d5dc18SMarcel Moolenaar 69227d5dc18SMarcel Moolenaar if (sc->sc_ires != NULL) { 69327d5dc18SMarcel Moolenaar bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 69427d5dc18SMarcel Moolenaar bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 69527d5dc18SMarcel Moolenaar sc->sc_ires); 69627d5dc18SMarcel Moolenaar } 69727d5dc18SMarcel Moolenaar bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 69827d5dc18SMarcel Moolenaar 6998af03381SMarcel Moolenaar mtx_destroy(&sc->sc_hwmtx_s); 7002682e7b6SMarius Strobl 70127d5dc18SMarcel Moolenaar return (error); 70227d5dc18SMarcel Moolenaar } 70327d5dc18SMarcel Moolenaar 70427d5dc18SMarcel Moolenaar int 70527d5dc18SMarcel Moolenaar uart_bus_detach(device_t dev) 70627d5dc18SMarcel Moolenaar { 70727d5dc18SMarcel Moolenaar struct uart_softc *sc; 70827d5dc18SMarcel Moolenaar 70927d5dc18SMarcel Moolenaar sc = device_get_softc(dev); 71027d5dc18SMarcel Moolenaar 71127d5dc18SMarcel Moolenaar sc->sc_leaving = 1; 71227d5dc18SMarcel Moolenaar 7138af03381SMarcel Moolenaar if (sc->sc_sysdev != NULL) 7148af03381SMarcel Moolenaar sc->sc_sysdev->hwmtx = NULL; 7158af03381SMarcel Moolenaar 71627d5dc18SMarcel Moolenaar UART_DETACH(sc); 71727d5dc18SMarcel Moolenaar 71827d5dc18SMarcel Moolenaar if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL) 71927d5dc18SMarcel Moolenaar (*sc->sc_sysdev->detach)(sc); 72027d5dc18SMarcel Moolenaar else 72127d5dc18SMarcel Moolenaar uart_tty_detach(sc); 72227d5dc18SMarcel Moolenaar 72327d5dc18SMarcel Moolenaar free(sc->sc_txbuf, M_UART); 72427d5dc18SMarcel Moolenaar free(sc->sc_rxbuf, M_UART); 72527d5dc18SMarcel Moolenaar 72627d5dc18SMarcel Moolenaar if (sc->sc_ires != NULL) { 72727d5dc18SMarcel Moolenaar bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 72827d5dc18SMarcel Moolenaar bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 72927d5dc18SMarcel Moolenaar sc->sc_ires); 73027d5dc18SMarcel Moolenaar } 73127d5dc18SMarcel Moolenaar bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 73227d5dc18SMarcel Moolenaar 7338af03381SMarcel Moolenaar mtx_destroy(&sc->sc_hwmtx_s); 7342682e7b6SMarius Strobl 73527d5dc18SMarcel Moolenaar if (sc->sc_class->size > sizeof(*sc)) { 73627d5dc18SMarcel Moolenaar device_set_softc(dev, NULL); 73727d5dc18SMarcel Moolenaar free(sc, M_UART); 73827d5dc18SMarcel Moolenaar } else 73927d5dc18SMarcel Moolenaar device_set_softc(dev, NULL); 74027d5dc18SMarcel Moolenaar 74127d5dc18SMarcel Moolenaar return (0); 74227d5dc18SMarcel Moolenaar } 7435b23b1b9SAndriy Gapon 7445b23b1b9SAndriy Gapon int 7455b23b1b9SAndriy Gapon uart_bus_resume(device_t dev) 7465b23b1b9SAndriy Gapon { 7475b23b1b9SAndriy Gapon struct uart_softc *sc; 7485b23b1b9SAndriy Gapon 7495b23b1b9SAndriy Gapon sc = device_get_softc(dev); 7505b23b1b9SAndriy Gapon return (UART_ATTACH(sc)); 7515b23b1b9SAndriy Gapon } 752d76a1ef4SWarner Losh 753d76a1ef4SWarner Losh void 754d76a1ef4SWarner Losh uart_grab(struct uart_devinfo *di) 755d76a1ef4SWarner Losh { 756d76a1ef4SWarner Losh 757d76a1ef4SWarner Losh if (di->sc) 758d76a1ef4SWarner Losh UART_GRAB(di->sc); 759d76a1ef4SWarner Losh } 760d76a1ef4SWarner Losh 761d76a1ef4SWarner Losh void 762d76a1ef4SWarner Losh uart_ungrab(struct uart_devinfo *di) 763d76a1ef4SWarner Losh { 764d76a1ef4SWarner Losh 765d76a1ef4SWarner Losh if (di->sc) 766d76a1ef4SWarner Losh UART_UNGRAB(di->sc); 767d76a1ef4SWarner Losh } 768