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 73196d3019SIan Lepore #define PPS_MODE_DISABLED 0 74196d3019SIan Lepore #define PPS_MODE_CTS 1 75196d3019SIan Lepore #define PPS_MODE_DCD 2 76196d3019SIan Lepore 77196d3019SIan Lepore static inline int 78196d3019SIan Lepore uart_pps_signal(int pps_mode) 79196d3019SIan Lepore { 80196d3019SIan Lepore 81dc4aaf6dSIan Lepore switch(pps_mode) { 82196d3019SIan Lepore case PPS_MODE_CTS: 83196d3019SIan Lepore return (SER_CTS); 84196d3019SIan Lepore case PPS_MODE_DCD: 85196d3019SIan Lepore return (SER_DCD); 86196d3019SIan Lepore } 87196d3019SIan Lepore return (0); 88196d3019SIan Lepore } 89196d3019SIan Lepore static inline int 90196d3019SIan Lepore uart_pps_mode_valid(int pps_mode) 91196d3019SIan Lepore { 92196d3019SIan Lepore 93dc4aaf6dSIan Lepore switch(pps_mode) { 94196d3019SIan Lepore case PPS_MODE_DISABLED: 95196d3019SIan Lepore case PPS_MODE_CTS: 96196d3019SIan Lepore case PPS_MODE_DCD: 97196d3019SIan Lepore return (true); 98196d3019SIan Lepore } 99196d3019SIan Lepore return (false); 100196d3019SIan Lepore } 101196d3019SIan Lepore 102196d3019SIan Lepore static const char * 103196d3019SIan Lepore uart_pps_mode_name(int pps_mode) 104196d3019SIan Lepore { 105dc4aaf6dSIan Lepore switch(pps_mode) { 106196d3019SIan Lepore case PPS_MODE_DISABLED: 107196d3019SIan Lepore return ("disabled"); 108196d3019SIan Lepore case PPS_MODE_CTS: 109196d3019SIan Lepore return ("CTS"); 110196d3019SIan Lepore case PPS_MODE_DCD: 111196d3019SIan Lepore return ("DCD"); 112196d3019SIan Lepore } 113196d3019SIan Lepore return ("invalid"); 114196d3019SIan Lepore } 115196d3019SIan Lepore 116196d3019SIan Lepore static int 117196d3019SIan Lepore uart_pps_mode_sysctl(SYSCTL_HANDLER_ARGS) 118196d3019SIan Lepore { 119196d3019SIan Lepore struct uart_softc *sc; 120196d3019SIan Lepore int err, tmp; 121196d3019SIan Lepore 122196d3019SIan Lepore sc = arg1; 123196d3019SIan Lepore tmp = sc->sc_pps_mode; 124196d3019SIan Lepore err = sysctl_handle_int(oidp, &tmp, 0, req); 125196d3019SIan Lepore if (err != 0 || req->newptr == NULL) 126196d3019SIan Lepore return (err); 127196d3019SIan Lepore if (!uart_pps_mode_valid(tmp)) 128196d3019SIan Lepore return (EINVAL); 129196d3019SIan Lepore sc->sc_pps_mode = tmp; 130196d3019SIan Lepore return(0); 131196d3019SIan Lepore } 132196d3019SIan Lepore 133196d3019SIan Lepore static void 134196d3019SIan Lepore uart_pps_init(struct uart_softc *sc) 135196d3019SIan Lepore { 136196d3019SIan Lepore struct sysctl_ctx_list *ctx; 137196d3019SIan Lepore struct sysctl_oid *tree; 138196d3019SIan Lepore 139196d3019SIan Lepore ctx = device_get_sysctl_ctx(sc->sc_dev); 140196d3019SIan Lepore tree = device_get_sysctl_tree(sc->sc_dev); 141196d3019SIan Lepore 142196d3019SIan Lepore /* 143196d3019SIan Lepore * The historical default for pps capture mode is either DCD or CTS, 144196d3019SIan Lepore * depending on the UART_PPS_ON_CTS kernel option. Start with that, 145196d3019SIan Lepore * then try to fetch the tunable that overrides the mode for all uart 146196d3019SIan Lepore * devices, then try to fetch the sysctl-tunable that overrides the mode 147196d3019SIan Lepore * for one specific device. 148196d3019SIan Lepore */ 149196d3019SIan Lepore #ifdef UART_PPS_ON_CTS 150196d3019SIan Lepore sc->sc_pps_mode = PPS_MODE_CTS; 151196d3019SIan Lepore #else 152196d3019SIan Lepore sc->sc_pps_mode = PPS_MODE_DCD; 153196d3019SIan Lepore #endif 154196d3019SIan Lepore TUNABLE_INT_FETCH("hw.uart.pps_mode", &sc->sc_pps_mode); 155196d3019SIan Lepore SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "pps_mode", 156196d3019SIan Lepore CTLTYPE_INT | CTLFLAG_RWTUN, sc, 0, uart_pps_mode_sysctl, "I", 157196d3019SIan Lepore "pulse capturing mode - 0/1/2 - disabled/CTS/DCD"); 158196d3019SIan Lepore 159196d3019SIan Lepore if (!uart_pps_mode_valid(sc->sc_pps_mode)) { 160196d3019SIan Lepore device_printf(sc->sc_dev, 161196d3019SIan Lepore "Invalid pps_mode %d configured; disabling PPS capture\n", 162196d3019SIan Lepore sc->sc_pps_mode); 163196d3019SIan Lepore sc->sc_pps_mode = PPS_MODE_DISABLED; 164196d3019SIan Lepore } else if (bootverbose) { 165196d3019SIan Lepore device_printf(sc->sc_dev, "PPS capture mode %d (%s)\n", 166196d3019SIan Lepore sc->sc_pps_mode, uart_pps_mode_name(sc->sc_pps_mode)); 167196d3019SIan Lepore } 168196d3019SIan Lepore 169196d3019SIan Lepore sc->sc_pps.ppscap = PPS_CAPTUREBOTH; 170196d3019SIan Lepore sc->sc_pps.driver_mtx = uart_tty_getlock(sc); 171196d3019SIan Lepore sc->sc_pps.driver_abi = PPS_ABI_VERSION; 172196d3019SIan Lepore pps_init_abi(&sc->sc_pps); 173196d3019SIan Lepore } 174196d3019SIan Lepore 17527d5dc18SMarcel Moolenaar void 17627d5dc18SMarcel Moolenaar uart_add_sysdev(struct uart_devinfo *di) 17727d5dc18SMarcel Moolenaar { 17827d5dc18SMarcel Moolenaar SLIST_INSERT_HEAD(&uart_sysdevs, di, next); 17927d5dc18SMarcel Moolenaar } 18027d5dc18SMarcel Moolenaar 181f8100ce2SMarcel Moolenaar const char * 182f8100ce2SMarcel Moolenaar uart_getname(struct uart_class *uc) 183f8100ce2SMarcel Moolenaar { 184f8100ce2SMarcel Moolenaar return ((uc != NULL) ? uc->name : NULL); 185f8100ce2SMarcel Moolenaar } 186f8100ce2SMarcel Moolenaar 187f8100ce2SMarcel Moolenaar struct uart_ops * 188f8100ce2SMarcel Moolenaar uart_getops(struct uart_class *uc) 189f8100ce2SMarcel Moolenaar { 190f8100ce2SMarcel Moolenaar return ((uc != NULL) ? uc->uc_ops : NULL); 191f8100ce2SMarcel Moolenaar } 192f8100ce2SMarcel Moolenaar 193f8100ce2SMarcel Moolenaar int 194f8100ce2SMarcel Moolenaar uart_getrange(struct uart_class *uc) 195f8100ce2SMarcel Moolenaar { 196f8100ce2SMarcel Moolenaar return ((uc != NULL) ? uc->uc_range : 0); 197f8100ce2SMarcel Moolenaar } 198f8100ce2SMarcel Moolenaar 199405ada37SAndrew Turner u_int 200405ada37SAndrew Turner uart_getregshift(struct uart_class *uc) 201405ada37SAndrew Turner { 202405ada37SAndrew Turner return ((uc != NULL) ? uc->uc_rshift : 0); 203405ada37SAndrew Turner } 204405ada37SAndrew Turner 20527d5dc18SMarcel Moolenaar /* 2068af03381SMarcel Moolenaar * Schedule a soft interrupt. We do this on the 0 to !0 transition 2078af03381SMarcel Moolenaar * of the TTY pending interrupt status. 2088af03381SMarcel Moolenaar */ 2090acb3c4aSMarcel Moolenaar void 2108af03381SMarcel Moolenaar uart_sched_softih(struct uart_softc *sc, uint32_t ipend) 2118af03381SMarcel Moolenaar { 2128af03381SMarcel Moolenaar uint32_t new, old; 2138af03381SMarcel Moolenaar 2148af03381SMarcel Moolenaar do { 2158af03381SMarcel Moolenaar old = sc->sc_ttypend; 2168af03381SMarcel Moolenaar new = old | ipend; 2178af03381SMarcel Moolenaar } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new)); 2188af03381SMarcel Moolenaar 2198af03381SMarcel Moolenaar if ((old & SER_INT_MASK) == 0) 2208af03381SMarcel Moolenaar swi_sched(sc->sc_softih, 0); 2218af03381SMarcel Moolenaar } 2228af03381SMarcel Moolenaar 2238af03381SMarcel Moolenaar /* 22427d5dc18SMarcel Moolenaar * A break condition has been detected. We treat the break condition as 22527d5dc18SMarcel Moolenaar * a special case that should not happen during normal operation. When 22627d5dc18SMarcel Moolenaar * the break condition is to be passed to higher levels in the form of 22727d5dc18SMarcel Moolenaar * a NUL character, we really want the break to be in the right place in 22827d5dc18SMarcel Moolenaar * the input stream. The overhead to achieve that is not in relation to 22927d5dc18SMarcel Moolenaar * the exceptional nature of the break condition, so we permit ourselves 23027d5dc18SMarcel Moolenaar * to be sloppy. 23127d5dc18SMarcel Moolenaar */ 2328af03381SMarcel Moolenaar static __inline int 2338af03381SMarcel Moolenaar uart_intr_break(void *arg) 23427d5dc18SMarcel Moolenaar { 2358af03381SMarcel Moolenaar struct uart_softc *sc = arg; 23627d5dc18SMarcel Moolenaar 2374cf75455SRobert Watson #if defined(KDB) 23827d5dc18SMarcel Moolenaar if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 2394cf75455SRobert Watson if (kdb_break()) 2408af03381SMarcel Moolenaar return (0); 24127d5dc18SMarcel Moolenaar } 24227d5dc18SMarcel Moolenaar #endif 24327d5dc18SMarcel Moolenaar if (sc->sc_opened) 2448af03381SMarcel Moolenaar uart_sched_softih(sc, SER_INT_BREAK); 2458af03381SMarcel Moolenaar return (0); 24627d5dc18SMarcel Moolenaar } 24727d5dc18SMarcel Moolenaar 24827d5dc18SMarcel Moolenaar /* 24927d5dc18SMarcel Moolenaar * Handle a receiver overrun situation. We lost at least 1 byte in the 25027d5dc18SMarcel Moolenaar * input stream and it's our job to contain the situation. We grab as 25127d5dc18SMarcel Moolenaar * much of the data we can, but otherwise flush the receiver FIFO to 25227d5dc18SMarcel Moolenaar * create some breathing room. The net effect is that we avoid the 25327d5dc18SMarcel Moolenaar * overrun condition to happen for the next X characters, where X is 254a164074fSEitan Adler * related to the FIFO size at the cost of losing data right away. 25527d5dc18SMarcel Moolenaar * So, instead of having multiple overrun interrupts in close proximity 25627d5dc18SMarcel Moolenaar * to each other and possibly pessimizing UART interrupt latency for 25727d5dc18SMarcel Moolenaar * other UARTs in a multiport configuration, we create a longer segment 25827d5dc18SMarcel Moolenaar * of missing characters by freeing up the FIFO. 25927d5dc18SMarcel Moolenaar * Each overrun condition is marked in the input buffer by a token. The 26027d5dc18SMarcel Moolenaar * token represents the loss of at least one, but possible more bytes in 26127d5dc18SMarcel Moolenaar * the input stream. 26227d5dc18SMarcel Moolenaar */ 2638af03381SMarcel Moolenaar static __inline int 2648af03381SMarcel Moolenaar uart_intr_overrun(void *arg) 26527d5dc18SMarcel Moolenaar { 2668af03381SMarcel Moolenaar struct uart_softc *sc = arg; 26727d5dc18SMarcel Moolenaar 26827d5dc18SMarcel Moolenaar if (sc->sc_opened) { 26927d5dc18SMarcel Moolenaar UART_RECEIVE(sc); 27027d5dc18SMarcel Moolenaar if (uart_rx_put(sc, UART_STAT_OVERRUN)) 27127d5dc18SMarcel Moolenaar sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN; 2728af03381SMarcel Moolenaar uart_sched_softih(sc, SER_INT_RXREADY); 27327d5dc18SMarcel Moolenaar } 27427d5dc18SMarcel Moolenaar UART_FLUSH(sc, UART_FLUSH_RECEIVER); 2758af03381SMarcel Moolenaar return (0); 27627d5dc18SMarcel Moolenaar } 27727d5dc18SMarcel Moolenaar 27827d5dc18SMarcel Moolenaar /* 27927d5dc18SMarcel Moolenaar * Received data ready. 28027d5dc18SMarcel Moolenaar */ 2818af03381SMarcel Moolenaar static __inline int 2828af03381SMarcel Moolenaar uart_intr_rxready(void *arg) 28327d5dc18SMarcel Moolenaar { 2848af03381SMarcel Moolenaar struct uart_softc *sc = arg; 28527d5dc18SMarcel Moolenaar int rxp; 28627d5dc18SMarcel Moolenaar 28727d5dc18SMarcel Moolenaar rxp = sc->sc_rxput; 28827d5dc18SMarcel Moolenaar UART_RECEIVE(sc); 2894cf75455SRobert Watson #if defined(KDB) 29027d5dc18SMarcel Moolenaar if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 29127d5dc18SMarcel Moolenaar while (rxp != sc->sc_rxput) { 2924cf75455SRobert Watson kdb_alt_break(sc->sc_rxbuf[rxp++], &sc->sc_altbrk); 29327d5dc18SMarcel Moolenaar if (rxp == sc->sc_rxbufsz) 29427d5dc18SMarcel Moolenaar rxp = 0; 29527d5dc18SMarcel Moolenaar } 29627d5dc18SMarcel Moolenaar } 29727d5dc18SMarcel Moolenaar #endif 29827d5dc18SMarcel Moolenaar if (sc->sc_opened) 2998af03381SMarcel Moolenaar uart_sched_softih(sc, SER_INT_RXREADY); 30027d5dc18SMarcel Moolenaar else 30127d5dc18SMarcel Moolenaar sc->sc_rxput = sc->sc_rxget; /* Ignore received data. */ 3028af03381SMarcel Moolenaar return (1); 30327d5dc18SMarcel Moolenaar } 30427d5dc18SMarcel Moolenaar 30527d5dc18SMarcel Moolenaar /* 30627d5dc18SMarcel Moolenaar * Line or modem status change (OOB signalling). 30727d5dc18SMarcel Moolenaar * We pass the signals to the software interrupt handler for further 30827d5dc18SMarcel Moolenaar * processing. Note that we merge the delta bits, but set the state 309a164074fSEitan Adler * bits. This is to avoid losing state transitions due to having more 31027d5dc18SMarcel Moolenaar * than 1 hardware interrupt between software interrupts. 31127d5dc18SMarcel Moolenaar */ 3128af03381SMarcel Moolenaar static __inline int 3138af03381SMarcel Moolenaar uart_intr_sigchg(void *arg) 31427d5dc18SMarcel Moolenaar { 3158af03381SMarcel Moolenaar struct uart_softc *sc = arg; 316196d3019SIan Lepore int new, old, pps_sig, sig; 31727d5dc18SMarcel Moolenaar 31827d5dc18SMarcel Moolenaar sig = UART_GETSIG(sc); 3198194412bSMarcel Moolenaar 320fac6a198SIan Lepore /* 321fac6a198SIan Lepore * Time pulse counting support. Note that both CTS and DCD are 322fac6a198SIan Lepore * active-low signals. The status bit is high to indicate that 323fac6a198SIan Lepore * the signal on the line is low, which corresponds to a PPS 324fac6a198SIan Lepore * clear event. 325fac6a198SIan Lepore */ 3268194412bSMarcel Moolenaar if (sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) { 327196d3019SIan Lepore pps_sig = uart_pps_signal(sc->sc_pps_mode); 328196d3019SIan Lepore if (sig & SER_DELTA(pps_sig)) { 3298194412bSMarcel Moolenaar pps_capture(&sc->sc_pps); 330196d3019SIan Lepore pps_event(&sc->sc_pps, (sig & pps_sig) ? 331fac6a198SIan Lepore PPS_CAPTURECLEAR : PPS_CAPTUREASSERT); 3328194412bSMarcel Moolenaar } 3338194412bSMarcel Moolenaar } 3348194412bSMarcel Moolenaar 3358af03381SMarcel Moolenaar /* 3368af03381SMarcel Moolenaar * Keep track of signal changes, even when the device is not 3378af03381SMarcel Moolenaar * opened. This allows us to inform upper layers about a 3388af03381SMarcel Moolenaar * possible loss of DCD and thus the existence of a (possibly) 3398af03381SMarcel Moolenaar * different connection when we have DCD back, during the time 3408af03381SMarcel Moolenaar * that the device was closed. 3418af03381SMarcel Moolenaar */ 34227d5dc18SMarcel Moolenaar do { 34327d5dc18SMarcel Moolenaar old = sc->sc_ttypend; 344ea549414SMarcel Moolenaar new = old & ~SER_MASK_STATE; 3452d511805SMarcel Moolenaar new |= sig & SER_INT_SIGMASK; 34627d5dc18SMarcel Moolenaar } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new)); 3478af03381SMarcel Moolenaar 3488af03381SMarcel Moolenaar if (sc->sc_opened) 3498af03381SMarcel Moolenaar uart_sched_softih(sc, SER_INT_SIGCHG); 3508af03381SMarcel Moolenaar return (1); 35127d5dc18SMarcel Moolenaar } 35227d5dc18SMarcel Moolenaar 35327d5dc18SMarcel Moolenaar /* 35427d5dc18SMarcel Moolenaar * The transmitter can accept more data. 35527d5dc18SMarcel Moolenaar */ 3568af03381SMarcel Moolenaar static __inline int 3578af03381SMarcel Moolenaar uart_intr_txidle(void *arg) 35827d5dc18SMarcel Moolenaar { 3598af03381SMarcel Moolenaar struct uart_softc *sc = arg; 3608af03381SMarcel Moolenaar 36127d5dc18SMarcel Moolenaar if (sc->sc_txbusy) { 36227d5dc18SMarcel Moolenaar sc->sc_txbusy = 0; 3638af03381SMarcel Moolenaar uart_sched_softih(sc, SER_INT_TXIDLE); 36427d5dc18SMarcel Moolenaar } 3658af03381SMarcel Moolenaar return (0); 36627d5dc18SMarcel Moolenaar } 36727d5dc18SMarcel Moolenaar 368ef544f63SPaolo Pisati static int 36927d5dc18SMarcel Moolenaar uart_intr(void *arg) 37027d5dc18SMarcel Moolenaar { 37127d5dc18SMarcel Moolenaar struct uart_softc *sc = arg; 37286fb5400SMarius Strobl int cnt, ipend, testintr; 37327d5dc18SMarcel Moolenaar 374eead2d55SMarcel Moolenaar if (sc->sc_leaving) 375eead2d55SMarcel Moolenaar return (FILTER_STRAY); 376eead2d55SMarcel Moolenaar 377eead2d55SMarcel Moolenaar cnt = 0; 37886fb5400SMarius Strobl testintr = sc->sc_testintr; 37986fb5400SMarius Strobl while ((!testintr || cnt < 20) && (ipend = UART_IPEND(sc)) != 0) { 380eead2d55SMarcel Moolenaar cnt++; 3812d511805SMarcel Moolenaar if (ipend & SER_INT_OVERRUN) 38227d5dc18SMarcel Moolenaar uart_intr_overrun(sc); 3832d511805SMarcel Moolenaar if (ipend & SER_INT_BREAK) 38427d5dc18SMarcel Moolenaar uart_intr_break(sc); 3852d511805SMarcel Moolenaar if (ipend & SER_INT_RXREADY) 38627d5dc18SMarcel Moolenaar uart_intr_rxready(sc); 3872d511805SMarcel Moolenaar if (ipend & SER_INT_SIGCHG) 38827d5dc18SMarcel Moolenaar uart_intr_sigchg(sc); 3892d511805SMarcel Moolenaar if (ipend & SER_INT_TXIDLE) 39027d5dc18SMarcel Moolenaar uart_intr_txidle(sc); 3918af03381SMarcel Moolenaar } 392332cda07SPeter Grehan 393332cda07SPeter Grehan if (sc->sc_polled) { 394332cda07SPeter Grehan callout_reset(&sc->sc_timer, hz / uart_poll_freq, 395332cda07SPeter Grehan (timeout_t *)uart_intr, sc); 396332cda07SPeter Grehan } 397332cda07SPeter Grehan 398eead2d55SMarcel Moolenaar return ((cnt == 0) ? FILTER_STRAY : 39986fb5400SMarius Strobl ((testintr && cnt == 20) ? FILTER_SCHEDULE_THREAD : 40086fb5400SMarius Strobl FILTER_HANDLED)); 4018af03381SMarcel Moolenaar } 40227d5dc18SMarcel Moolenaar 4038af03381SMarcel Moolenaar serdev_intr_t * 4048af03381SMarcel Moolenaar uart_bus_ihand(device_t dev, int ipend) 4058af03381SMarcel Moolenaar { 4068af03381SMarcel Moolenaar 4078af03381SMarcel Moolenaar switch (ipend) { 4088af03381SMarcel Moolenaar case SER_INT_BREAK: 4098af03381SMarcel Moolenaar return (uart_intr_break); 4108af03381SMarcel Moolenaar case SER_INT_OVERRUN: 4118af03381SMarcel Moolenaar return (uart_intr_overrun); 4128af03381SMarcel Moolenaar case SER_INT_RXREADY: 4138af03381SMarcel Moolenaar return (uart_intr_rxready); 4148af03381SMarcel Moolenaar case SER_INT_SIGCHG: 4158af03381SMarcel Moolenaar return (uart_intr_sigchg); 4168af03381SMarcel Moolenaar case SER_INT_TXIDLE: 4178af03381SMarcel Moolenaar return (uart_intr_txidle); 4188af03381SMarcel Moolenaar } 4198af03381SMarcel Moolenaar return (NULL); 4208af03381SMarcel Moolenaar } 4218af03381SMarcel Moolenaar 4228af03381SMarcel Moolenaar int 423a31f91a0SMarcel Moolenaar uart_bus_ipend(device_t dev) 424a31f91a0SMarcel Moolenaar { 425a31f91a0SMarcel Moolenaar struct uart_softc *sc; 426a31f91a0SMarcel Moolenaar 427a31f91a0SMarcel Moolenaar sc = device_get_softc(dev); 428a31f91a0SMarcel Moolenaar return (UART_IPEND(sc)); 429a31f91a0SMarcel Moolenaar } 430a31f91a0SMarcel Moolenaar 431a31f91a0SMarcel Moolenaar int 4328af03381SMarcel Moolenaar uart_bus_sysdev(device_t dev) 4338af03381SMarcel Moolenaar { 4348af03381SMarcel Moolenaar struct uart_softc *sc; 4358af03381SMarcel Moolenaar 4368af03381SMarcel Moolenaar sc = device_get_softc(dev); 4378af03381SMarcel Moolenaar return ((sc->sc_sysdev != NULL) ? 1 : 0); 43827d5dc18SMarcel Moolenaar } 43927d5dc18SMarcel Moolenaar 44027d5dc18SMarcel Moolenaar int 441875f70dbSMarcel Moolenaar uart_bus_probe(device_t dev, int regshft, int rclk, int rid, int chan) 44227d5dc18SMarcel Moolenaar { 44327d5dc18SMarcel Moolenaar struct uart_softc *sc; 44427d5dc18SMarcel Moolenaar struct uart_devinfo *sysdev; 44527d5dc18SMarcel Moolenaar int error; 44627d5dc18SMarcel Moolenaar 447f8100ce2SMarcel Moolenaar sc = device_get_softc(dev); 448f8100ce2SMarcel Moolenaar 449f8100ce2SMarcel Moolenaar /* 450f8100ce2SMarcel Moolenaar * All uart_class references are weak. Check that the needed 451f8100ce2SMarcel Moolenaar * class has been compiled-in. Fail if not. 452f8100ce2SMarcel Moolenaar */ 453f8100ce2SMarcel Moolenaar if (sc->sc_class == NULL) 454f8100ce2SMarcel Moolenaar return (ENXIO); 455f8100ce2SMarcel Moolenaar 45627d5dc18SMarcel Moolenaar /* 45727d5dc18SMarcel Moolenaar * Initialize the instance. Note that the instance (=softc) does 45827d5dc18SMarcel Moolenaar * not necessarily match the hardware specific softc. We can't do 45927d5dc18SMarcel Moolenaar * anything about it now, because we may not attach to the device. 46027d5dc18SMarcel Moolenaar * Hardware drivers cannot use any of the class specific fields 46127d5dc18SMarcel Moolenaar * while probing. 46227d5dc18SMarcel Moolenaar */ 46327d5dc18SMarcel Moolenaar kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class); 46427d5dc18SMarcel Moolenaar sc->sc_dev = dev; 46527d5dc18SMarcel Moolenaar if (device_get_desc(dev) == NULL) 466f8100ce2SMarcel Moolenaar device_set_desc(dev, uart_getname(sc->sc_class)); 46727d5dc18SMarcel Moolenaar 46827d5dc18SMarcel Moolenaar /* 46927d5dc18SMarcel Moolenaar * Allocate the register resource. We assume that all UARTs have 47027d5dc18SMarcel Moolenaar * a single register window in either I/O port space or memory 47127d5dc18SMarcel Moolenaar * mapped I/O space. Any UART that needs multiple windows will 47227d5dc18SMarcel Moolenaar * consequently not be supported by this driver as-is. We try I/O 47327d5dc18SMarcel Moolenaar * port space first because that's the common case. 47427d5dc18SMarcel Moolenaar */ 47527d5dc18SMarcel Moolenaar sc->sc_rrid = rid; 47627d5dc18SMarcel Moolenaar sc->sc_rtype = SYS_RES_IOPORT; 477*9def69ecSMarcel Moolenaar sc->sc_rres = bus_alloc_resource_any(dev, sc->sc_rtype, &sc->sc_rrid, 478*9def69ecSMarcel Moolenaar RF_ACTIVE); 47927d5dc18SMarcel Moolenaar if (sc->sc_rres == NULL) { 48027d5dc18SMarcel Moolenaar sc->sc_rrid = rid; 48127d5dc18SMarcel Moolenaar sc->sc_rtype = SYS_RES_MEMORY; 482*9def69ecSMarcel Moolenaar sc->sc_rres = bus_alloc_resource_any(dev, sc->sc_rtype, 483*9def69ecSMarcel Moolenaar &sc->sc_rrid, RF_ACTIVE); 48427d5dc18SMarcel Moolenaar if (sc->sc_rres == NULL) 48527d5dc18SMarcel Moolenaar return (ENXIO); 48627d5dc18SMarcel Moolenaar } 48727d5dc18SMarcel Moolenaar 48827d5dc18SMarcel Moolenaar /* 48927d5dc18SMarcel Moolenaar * Fill in the bus access structure and compare this device with 49027d5dc18SMarcel Moolenaar * a possible console device and/or a debug port. We set the flags 49127d5dc18SMarcel Moolenaar * in the softc so that the hardware dependent probe can adjust 49227d5dc18SMarcel Moolenaar * accordingly. In general, you don't want to permanently disrupt 49327d5dc18SMarcel Moolenaar * console I/O. 49427d5dc18SMarcel Moolenaar */ 49527d5dc18SMarcel Moolenaar sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 49627d5dc18SMarcel Moolenaar sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 497875f70dbSMarcel Moolenaar sc->sc_bas.chan = chan; 49827d5dc18SMarcel Moolenaar sc->sc_bas.regshft = regshft; 49927d5dc18SMarcel Moolenaar sc->sc_bas.rclk = (rclk == 0) ? sc->sc_class->uc_rclk : rclk; 50027d5dc18SMarcel Moolenaar 50127d5dc18SMarcel Moolenaar SLIST_FOREACH(sysdev, &uart_sysdevs, next) { 502875f70dbSMarcel Moolenaar if (chan == sysdev->bas.chan && 503875f70dbSMarcel Moolenaar uart_cpu_eqres(&sc->sc_bas, &sysdev->bas)) { 50427d5dc18SMarcel Moolenaar /* XXX check if ops matches class. */ 50527d5dc18SMarcel Moolenaar sc->sc_sysdev = sysdev; 5061c5e367bSMarcel Moolenaar sysdev->bas.rclk = sc->sc_bas.rclk; 50727d5dc18SMarcel Moolenaar } 50827d5dc18SMarcel Moolenaar } 50927d5dc18SMarcel Moolenaar 51027d5dc18SMarcel Moolenaar error = UART_PROBE(sc); 51127d5dc18SMarcel Moolenaar bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 512b923a710SMarcel Moolenaar return ((error) ? error : BUS_PROBE_DEFAULT); 51327d5dc18SMarcel Moolenaar } 51427d5dc18SMarcel Moolenaar 51527d5dc18SMarcel Moolenaar int 51627d5dc18SMarcel Moolenaar uart_bus_attach(device_t dev) 51727d5dc18SMarcel Moolenaar { 51827d5dc18SMarcel Moolenaar struct uart_softc *sc, *sc0; 51927d5dc18SMarcel Moolenaar const char *sep; 520eead2d55SMarcel Moolenaar int error, filt; 52127d5dc18SMarcel Moolenaar 52227d5dc18SMarcel Moolenaar /* 52327d5dc18SMarcel Moolenaar * The sc_class field defines the type of UART we're going to work 52427d5dc18SMarcel Moolenaar * with and thus the size of the softc. Replace the generic softc 52527d5dc18SMarcel Moolenaar * with one that matches the UART now that we're certain we handle 52627d5dc18SMarcel Moolenaar * the device. 52727d5dc18SMarcel Moolenaar */ 52827d5dc18SMarcel Moolenaar sc0 = device_get_softc(dev); 52927d5dc18SMarcel Moolenaar if (sc0->sc_class->size > sizeof(*sc)) { 53027d5dc18SMarcel Moolenaar sc = malloc(sc0->sc_class->size, M_UART, M_WAITOK|M_ZERO); 53127d5dc18SMarcel Moolenaar bcopy(sc0, sc, sizeof(*sc)); 53227d5dc18SMarcel Moolenaar device_set_softc(dev, sc); 53327d5dc18SMarcel Moolenaar } else 53427d5dc18SMarcel Moolenaar sc = sc0; 53527d5dc18SMarcel Moolenaar 53627d5dc18SMarcel Moolenaar /* 537d76a1ef4SWarner Losh * Now that we know the softc for this device, connect the back 538d76a1ef4SWarner Losh * pointer from the sysdev for this device, if any 539d76a1ef4SWarner Losh */ 540d76a1ef4SWarner Losh if (sc->sc_sysdev != NULL) 541d76a1ef4SWarner Losh sc->sc_sysdev->sc = sc; 542d76a1ef4SWarner Losh 543d76a1ef4SWarner Losh /* 54427d5dc18SMarcel Moolenaar * Protect ourselves against interrupts while we're not completely 54527d5dc18SMarcel Moolenaar * finished attaching and initializing. We don't expect interrupts 54686fb5400SMarius Strobl * until after UART_ATTACH(), though. 54727d5dc18SMarcel Moolenaar */ 54827d5dc18SMarcel Moolenaar sc->sc_leaving = 1; 54927d5dc18SMarcel Moolenaar 5508af03381SMarcel Moolenaar mtx_init(&sc->sc_hwmtx_s, "uart_hwmtx", NULL, MTX_SPIN); 5518af03381SMarcel Moolenaar if (sc->sc_hwmtx == NULL) 5528af03381SMarcel Moolenaar sc->sc_hwmtx = &sc->sc_hwmtx_s; 55306287620SMarcel Moolenaar 55427d5dc18SMarcel Moolenaar /* 55527d5dc18SMarcel Moolenaar * Re-allocate. We expect that the softc contains the information 55627d5dc18SMarcel Moolenaar * collected by uart_bus_probe() intact. 55727d5dc18SMarcel Moolenaar */ 558*9def69ecSMarcel Moolenaar sc->sc_rres = bus_alloc_resource_any(dev, sc->sc_rtype, &sc->sc_rrid, 559*9def69ecSMarcel Moolenaar RF_ACTIVE); 5602682e7b6SMarius Strobl if (sc->sc_rres == NULL) { 5618af03381SMarcel Moolenaar mtx_destroy(&sc->sc_hwmtx_s); 56227d5dc18SMarcel Moolenaar return (ENXIO); 5632682e7b6SMarius Strobl } 564b8759afcSYoshihiro Takahashi sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 565b8759afcSYoshihiro Takahashi sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 566b8759afcSYoshihiro Takahashi 5673ad36a41SIan Lepore /* 5683ad36a41SIan Lepore * Ensure there is room for at least three full FIFOs of data in the 5693ad36a41SIan Lepore * receive buffer (handles the case of low-level drivers with huge 5703ad36a41SIan Lepore * FIFOs), and also ensure that there is no less than the historical 5713ad36a41SIan Lepore * size of 384 bytes (handles the typical small-FIFO case). 5723ad36a41SIan Lepore */ 5733ad36a41SIan Lepore sc->sc_rxbufsz = MAX(384, sc->sc_rxfifosz * 3); 57427d5dc18SMarcel Moolenaar sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf), 57527d5dc18SMarcel Moolenaar M_UART, M_WAITOK); 57627d5dc18SMarcel Moolenaar sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf), 57727d5dc18SMarcel Moolenaar M_UART, M_WAITOK); 57827d5dc18SMarcel Moolenaar 57927d5dc18SMarcel Moolenaar error = UART_ATTACH(sc); 58027d5dc18SMarcel Moolenaar if (error) 58127d5dc18SMarcel Moolenaar goto fail; 58227d5dc18SMarcel Moolenaar 58327d5dc18SMarcel Moolenaar if (sc->sc_hwiflow || sc->sc_hwoflow) { 58427d5dc18SMarcel Moolenaar sep = ""; 58527d5dc18SMarcel Moolenaar device_print_prettyname(dev); 58627d5dc18SMarcel Moolenaar if (sc->sc_hwiflow) { 58727d5dc18SMarcel Moolenaar printf("%sRTS iflow", sep); 58827d5dc18SMarcel Moolenaar sep = ", "; 58927d5dc18SMarcel Moolenaar } 59027d5dc18SMarcel Moolenaar if (sc->sc_hwoflow) { 59127d5dc18SMarcel Moolenaar printf("%sCTS oflow", sep); 59227d5dc18SMarcel Moolenaar sep = ", "; 59327d5dc18SMarcel Moolenaar } 59427d5dc18SMarcel Moolenaar printf("\n"); 59527d5dc18SMarcel Moolenaar } 59627d5dc18SMarcel Moolenaar 59727d5dc18SMarcel Moolenaar if (sc->sc_sysdev != NULL) { 5985dceed8aSWarner Losh if (sc->sc_sysdev->baudrate == 0) { 59954e2bcc7SMarcel Moolenaar if (UART_IOCTL(sc, UART_IOCTL_BAUD, 60054e2bcc7SMarcel Moolenaar (intptr_t)&sc->sc_sysdev->baudrate) != 0) 60154e2bcc7SMarcel Moolenaar sc->sc_sysdev->baudrate = -1; 60254e2bcc7SMarcel Moolenaar } 60327d5dc18SMarcel Moolenaar switch (sc->sc_sysdev->type) { 60427d5dc18SMarcel Moolenaar case UART_DEV_CONSOLE: 60527d5dc18SMarcel Moolenaar device_printf(dev, "console"); 60627d5dc18SMarcel Moolenaar break; 60727d5dc18SMarcel Moolenaar case UART_DEV_DBGPORT: 60827d5dc18SMarcel Moolenaar device_printf(dev, "debug port"); 60927d5dc18SMarcel Moolenaar break; 61027d5dc18SMarcel Moolenaar case UART_DEV_KEYBOARD: 61127d5dc18SMarcel Moolenaar device_printf(dev, "keyboard"); 61227d5dc18SMarcel Moolenaar break; 61327d5dc18SMarcel Moolenaar default: 61427d5dc18SMarcel Moolenaar device_printf(dev, "unknown system device"); 61527d5dc18SMarcel Moolenaar break; 61627d5dc18SMarcel Moolenaar } 61727d5dc18SMarcel Moolenaar printf(" (%d,%c,%d,%d)\n", sc->sc_sysdev->baudrate, 61827d5dc18SMarcel Moolenaar "noems"[sc->sc_sysdev->parity], sc->sc_sysdev->databits, 61927d5dc18SMarcel Moolenaar sc->sc_sysdev->stopbits); 62027d5dc18SMarcel Moolenaar } 62127d5dc18SMarcel Moolenaar 622eead2d55SMarcel Moolenaar sc->sc_leaving = 0; 62386fb5400SMarius Strobl sc->sc_testintr = 1; 624eead2d55SMarcel Moolenaar filt = uart_intr(sc); 62586fb5400SMarius Strobl sc->sc_testintr = 0; 626eead2d55SMarcel Moolenaar 627eead2d55SMarcel Moolenaar /* 628eead2d55SMarcel Moolenaar * Don't use interrupts if we couldn't clear any pending interrupt 629eead2d55SMarcel Moolenaar * conditions. We may have broken H/W and polling is probably the 630eead2d55SMarcel Moolenaar * safest thing to do. 631eead2d55SMarcel Moolenaar */ 63262145ff3SNeel Natu if (filt != FILTER_SCHEDULE_THREAD && !uart_force_poll) { 633eead2d55SMarcel Moolenaar sc->sc_irid = 0; 634eead2d55SMarcel Moolenaar sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, 635eead2d55SMarcel Moolenaar &sc->sc_irid, RF_ACTIVE | RF_SHAREABLE); 636eead2d55SMarcel Moolenaar } 637eead2d55SMarcel Moolenaar if (sc->sc_ires != NULL) { 638eead2d55SMarcel Moolenaar error = bus_setup_intr(dev, sc->sc_ires, INTR_TYPE_TTY, 639eead2d55SMarcel Moolenaar uart_intr, NULL, sc, &sc->sc_icookie); 640eead2d55SMarcel Moolenaar sc->sc_fastintr = (error == 0) ? 1 : 0; 641eead2d55SMarcel Moolenaar 642eead2d55SMarcel Moolenaar if (!sc->sc_fastintr) 643eead2d55SMarcel Moolenaar error = bus_setup_intr(dev, sc->sc_ires, 644eead2d55SMarcel Moolenaar INTR_TYPE_TTY | INTR_MPSAFE, NULL, 645eead2d55SMarcel Moolenaar (driver_intr_t *)uart_intr, sc, &sc->sc_icookie); 646eead2d55SMarcel Moolenaar 647eead2d55SMarcel Moolenaar if (error) { 648eead2d55SMarcel Moolenaar device_printf(dev, "could not activate interrupt\n"); 649eead2d55SMarcel Moolenaar bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 650eead2d55SMarcel Moolenaar sc->sc_ires); 651eead2d55SMarcel Moolenaar sc->sc_ires = NULL; 652eead2d55SMarcel Moolenaar } 653eead2d55SMarcel Moolenaar } 654eead2d55SMarcel Moolenaar if (sc->sc_ires == NULL) { 655eead2d55SMarcel Moolenaar /* No interrupt resource. Force polled mode. */ 656eead2d55SMarcel Moolenaar sc->sc_polled = 1; 657eead2d55SMarcel Moolenaar callout_init(&sc->sc_timer, 1); 65862145ff3SNeel Natu callout_reset(&sc->sc_timer, hz / uart_poll_freq, 65962145ff3SNeel Natu (timeout_t *)uart_intr, sc); 660eead2d55SMarcel Moolenaar } 661eead2d55SMarcel Moolenaar 662eead2d55SMarcel Moolenaar if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) { 663eead2d55SMarcel Moolenaar sep = ""; 664eead2d55SMarcel Moolenaar device_print_prettyname(dev); 665eead2d55SMarcel Moolenaar if (sc->sc_fastintr) { 666eead2d55SMarcel Moolenaar printf("%sfast interrupt", sep); 667eead2d55SMarcel Moolenaar sep = ", "; 668eead2d55SMarcel Moolenaar } 669eead2d55SMarcel Moolenaar if (sc->sc_polled) { 6701662b008SIan Lepore printf("%spolled mode (%dHz)", sep, uart_poll_freq); 671eead2d55SMarcel Moolenaar sep = ", "; 672eead2d55SMarcel Moolenaar } 673eead2d55SMarcel Moolenaar printf("\n"); 674eead2d55SMarcel Moolenaar } 675eead2d55SMarcel Moolenaar 676b59236ceSIan Lepore if (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL) { 677b59236ceSIan Lepore if ((error = sc->sc_sysdev->attach(sc)) != 0) 67827d5dc18SMarcel Moolenaar goto fail; 679b59236ceSIan Lepore } else { 680b59236ceSIan Lepore if ((error = uart_tty_attach(sc)) != 0) 681b59236ceSIan Lepore goto fail; 682196d3019SIan Lepore uart_pps_init(sc); 683b59236ceSIan Lepore } 68427d5dc18SMarcel Moolenaar 6858af03381SMarcel Moolenaar if (sc->sc_sysdev != NULL) 6868af03381SMarcel Moolenaar sc->sc_sysdev->hwmtx = sc->sc_hwmtx; 6878af03381SMarcel Moolenaar 68827d5dc18SMarcel Moolenaar return (0); 68927d5dc18SMarcel Moolenaar 69027d5dc18SMarcel Moolenaar fail: 69127d5dc18SMarcel Moolenaar free(sc->sc_txbuf, M_UART); 69227d5dc18SMarcel Moolenaar free(sc->sc_rxbuf, M_UART); 69327d5dc18SMarcel Moolenaar 69427d5dc18SMarcel Moolenaar if (sc->sc_ires != NULL) { 69527d5dc18SMarcel Moolenaar bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 69627d5dc18SMarcel Moolenaar bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 69727d5dc18SMarcel Moolenaar sc->sc_ires); 69827d5dc18SMarcel Moolenaar } 69927d5dc18SMarcel Moolenaar bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 70027d5dc18SMarcel Moolenaar 7018af03381SMarcel Moolenaar mtx_destroy(&sc->sc_hwmtx_s); 7022682e7b6SMarius Strobl 70327d5dc18SMarcel Moolenaar return (error); 70427d5dc18SMarcel Moolenaar } 70527d5dc18SMarcel Moolenaar 70627d5dc18SMarcel Moolenaar int 70727d5dc18SMarcel Moolenaar uart_bus_detach(device_t dev) 70827d5dc18SMarcel Moolenaar { 70927d5dc18SMarcel Moolenaar struct uart_softc *sc; 71027d5dc18SMarcel Moolenaar 71127d5dc18SMarcel Moolenaar sc = device_get_softc(dev); 71227d5dc18SMarcel Moolenaar 71327d5dc18SMarcel Moolenaar sc->sc_leaving = 1; 71427d5dc18SMarcel Moolenaar 7158af03381SMarcel Moolenaar if (sc->sc_sysdev != NULL) 7168af03381SMarcel Moolenaar sc->sc_sysdev->hwmtx = NULL; 7178af03381SMarcel Moolenaar 71827d5dc18SMarcel Moolenaar UART_DETACH(sc); 71927d5dc18SMarcel Moolenaar 72027d5dc18SMarcel Moolenaar if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL) 72127d5dc18SMarcel Moolenaar (*sc->sc_sysdev->detach)(sc); 72227d5dc18SMarcel Moolenaar else 72327d5dc18SMarcel Moolenaar uart_tty_detach(sc); 72427d5dc18SMarcel Moolenaar 72527d5dc18SMarcel Moolenaar free(sc->sc_txbuf, M_UART); 72627d5dc18SMarcel Moolenaar free(sc->sc_rxbuf, M_UART); 72727d5dc18SMarcel Moolenaar 72827d5dc18SMarcel Moolenaar if (sc->sc_ires != NULL) { 72927d5dc18SMarcel Moolenaar bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 73027d5dc18SMarcel Moolenaar bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 73127d5dc18SMarcel Moolenaar sc->sc_ires); 73227d5dc18SMarcel Moolenaar } 73327d5dc18SMarcel Moolenaar bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 73427d5dc18SMarcel Moolenaar 7358af03381SMarcel Moolenaar mtx_destroy(&sc->sc_hwmtx_s); 7362682e7b6SMarius Strobl 73727d5dc18SMarcel Moolenaar if (sc->sc_class->size > sizeof(*sc)) { 73827d5dc18SMarcel Moolenaar device_set_softc(dev, NULL); 73927d5dc18SMarcel Moolenaar free(sc, M_UART); 74027d5dc18SMarcel Moolenaar } else 74127d5dc18SMarcel Moolenaar device_set_softc(dev, NULL); 74227d5dc18SMarcel Moolenaar 74327d5dc18SMarcel Moolenaar return (0); 74427d5dc18SMarcel Moolenaar } 7455b23b1b9SAndriy Gapon 7465b23b1b9SAndriy Gapon int 7475b23b1b9SAndriy Gapon uart_bus_resume(device_t dev) 7485b23b1b9SAndriy Gapon { 7495b23b1b9SAndriy Gapon struct uart_softc *sc; 7505b23b1b9SAndriy Gapon 7515b23b1b9SAndriy Gapon sc = device_get_softc(dev); 7525b23b1b9SAndriy Gapon return (UART_ATTACH(sc)); 7535b23b1b9SAndriy Gapon } 754d76a1ef4SWarner Losh 755d76a1ef4SWarner Losh void 756d76a1ef4SWarner Losh uart_grab(struct uart_devinfo *di) 757d76a1ef4SWarner Losh { 758d76a1ef4SWarner Losh 759d76a1ef4SWarner Losh if (di->sc) 760d76a1ef4SWarner Losh UART_GRAB(di->sc); 761d76a1ef4SWarner Losh } 762d76a1ef4SWarner Losh 763d76a1ef4SWarner Losh void 764d76a1ef4SWarner Losh uart_ungrab(struct uart_devinfo *di) 765d76a1ef4SWarner Losh { 766d76a1ef4SWarner Losh 767d76a1ef4SWarner Losh if (di->sc) 768d76a1ef4SWarner Losh UART_UNGRAB(di->sc); 769d76a1ef4SWarner Losh } 770