xref: /freebsd/sys/dev/uart/uart_core.c (revision 16858c207b93f8524df6b3e2755f9e0575e8bf77)
1 /*-
2  * Copyright (c) 2003 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/cons.h>
35 #include <sys/fcntl.h>
36 #include <sys/interrupt.h>
37 #include <sys/kdb.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <sys/reboot.h>
42 #include <sys/sysctl.h>
43 #include <machine/bus.h>
44 #include <sys/rman.h>
45 #include <machine/resource.h>
46 #include <machine/stdarg.h>
47 
48 #include <dev/uart/uart.h>
49 #include <dev/uart/uart_bus.h>
50 #include <dev/uart/uart_cpu.h>
51 
52 #include "uart_if.h"
53 
54 devclass_t uart_devclass;
55 char uart_driver_name[] = "uart";
56 
57 SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs =
58     SLIST_HEAD_INITIALIZER(uart_sysdevs);
59 
60 static MALLOC_DEFINE(M_UART, "UART", "UART driver");
61 
62 #ifndef	UART_POLL_FREQ
63 #define	UART_POLL_FREQ		50
64 #endif
65 static int uart_poll_freq = UART_POLL_FREQ;
66 SYSCTL_INT(_debug, OID_AUTO, uart_poll_freq, CTLFLAG_RDTUN, &uart_poll_freq,
67     0, "UART poll frequency");
68 
69 static int uart_force_poll;
70 SYSCTL_INT(_debug, OID_AUTO, uart_force_poll, CTLFLAG_RDTUN, &uart_force_poll,
71     0, "Force UART polling");
72 
73 void
74 uart_add_sysdev(struct uart_devinfo *di)
75 {
76 	SLIST_INSERT_HEAD(&uart_sysdevs, di, next);
77 }
78 
79 const char *
80 uart_getname(struct uart_class *uc)
81 {
82 	return ((uc != NULL) ? uc->name : NULL);
83 }
84 
85 struct uart_ops *
86 uart_getops(struct uart_class *uc)
87 {
88 	return ((uc != NULL) ? uc->uc_ops : NULL);
89 }
90 
91 int
92 uart_getrange(struct uart_class *uc)
93 {
94 	return ((uc != NULL) ? uc->uc_range : 0);
95 }
96 
97 u_int
98 uart_getregshift(struct uart_class *uc)
99 {
100 	return ((uc != NULL) ? uc->uc_rshift : 0);
101 }
102 
103 /*
104  * Schedule a soft interrupt. We do this on the 0 to !0 transition
105  * of the TTY pending interrupt status.
106  */
107 void
108 uart_sched_softih(struct uart_softc *sc, uint32_t ipend)
109 {
110 	uint32_t new, old;
111 
112 	do {
113 		old = sc->sc_ttypend;
114 		new = old | ipend;
115 	} while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
116 
117 	if ((old & SER_INT_MASK) == 0)
118 		swi_sched(sc->sc_softih, 0);
119 }
120 
121 /*
122  * A break condition has been detected. We treat the break condition as
123  * a special case that should not happen during normal operation. When
124  * the break condition is to be passed to higher levels in the form of
125  * a NUL character, we really want the break to be in the right place in
126  * the input stream. The overhead to achieve that is not in relation to
127  * the exceptional nature of the break condition, so we permit ourselves
128  * to be sloppy.
129  */
130 static __inline int
131 uart_intr_break(void *arg)
132 {
133 	struct uart_softc *sc = arg;
134 
135 #if defined(KDB)
136 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
137 		if (kdb_break())
138 			return (0);
139 	}
140 #endif
141 	if (sc->sc_opened)
142 		uart_sched_softih(sc, SER_INT_BREAK);
143 	return (0);
144 }
145 
146 /*
147  * Handle a receiver overrun situation. We lost at least 1 byte in the
148  * input stream and it's our job to contain the situation. We grab as
149  * much of the data we can, but otherwise flush the receiver FIFO to
150  * create some breathing room. The net effect is that we avoid the
151  * overrun condition to happen for the next X characters, where X is
152  * related to the FIFO size at the cost of losing data right away.
153  * So, instead of having multiple overrun interrupts in close proximity
154  * to each other and possibly pessimizing UART interrupt latency for
155  * other UARTs in a multiport configuration, we create a longer segment
156  * of missing characters by freeing up the FIFO.
157  * Each overrun condition is marked in the input buffer by a token. The
158  * token represents the loss of at least one, but possible more bytes in
159  * the input stream.
160  */
161 static __inline int
162 uart_intr_overrun(void *arg)
163 {
164 	struct uart_softc *sc = arg;
165 
166 	if (sc->sc_opened) {
167 		UART_RECEIVE(sc);
168 		if (uart_rx_put(sc, UART_STAT_OVERRUN))
169 			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
170 		uart_sched_softih(sc, SER_INT_RXREADY);
171 	}
172 	UART_FLUSH(sc, UART_FLUSH_RECEIVER);
173 	return (0);
174 }
175 
176 /*
177  * Received data ready.
178  */
179 static __inline int
180 uart_intr_rxready(void *arg)
181 {
182 	struct uart_softc *sc = arg;
183 	int rxp;
184 
185 	rxp = sc->sc_rxput;
186 	UART_RECEIVE(sc);
187 #if defined(KDB)
188 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
189 		while (rxp != sc->sc_rxput) {
190 			kdb_alt_break(sc->sc_rxbuf[rxp++], &sc->sc_altbrk);
191 			if (rxp == sc->sc_rxbufsz)
192 				rxp = 0;
193 		}
194 	}
195 #endif
196 	if (sc->sc_opened)
197 		uart_sched_softih(sc, SER_INT_RXREADY);
198 	else
199 		sc->sc_rxput = sc->sc_rxget;	/* Ignore received data. */
200 	return (1);
201 }
202 
203 /*
204  * Line or modem status change (OOB signalling).
205  * We pass the signals to the software interrupt handler for further
206  * processing. Note that we merge the delta bits, but set the state
207  * bits. This is to avoid losing state transitions due to having more
208  * than 1 hardware interrupt between software interrupts.
209  */
210 static __inline int
211 uart_intr_sigchg(void *arg)
212 {
213 	struct uart_softc *sc = arg;
214 	int new, old, sig;
215 
216 	sig = UART_GETSIG(sc);
217 
218 	if (sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) {
219 		if (sig & UART_SIG_DPPS) {
220 			pps_capture(&sc->sc_pps);
221 			pps_event(&sc->sc_pps, (sig & UART_SIG_PPS) ?
222 			    PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
223 		}
224 	}
225 
226 	/*
227 	 * Keep track of signal changes, even when the device is not
228 	 * opened. This allows us to inform upper layers about a
229 	 * possible loss of DCD and thus the existence of a (possibly)
230 	 * different connection when we have DCD back, during the time
231 	 * that the device was closed.
232 	 */
233 	do {
234 		old = sc->sc_ttypend;
235 		new = old & ~SER_MASK_STATE;
236 		new |= sig & SER_INT_SIGMASK;
237 	} while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
238 
239 	if (sc->sc_opened)
240 		uart_sched_softih(sc, SER_INT_SIGCHG);
241 	return (1);
242 }
243 
244 /*
245  * The transmitter can accept more data.
246  */
247 static __inline int
248 uart_intr_txidle(void *arg)
249 {
250 	struct uart_softc *sc = arg;
251 
252 	if (sc->sc_txbusy) {
253 		sc->sc_txbusy = 0;
254 		uart_sched_softih(sc, SER_INT_TXIDLE);
255 	}
256 	return (0);
257 }
258 
259 static int
260 uart_intr(void *arg)
261 {
262 	struct uart_softc *sc = arg;
263 	int cnt, ipend;
264 
265 	if (sc->sc_leaving)
266 		return (FILTER_STRAY);
267 
268 	cnt = 0;
269 	while (cnt < 20 && (ipend = UART_IPEND(sc)) != 0) {
270 		cnt++;
271 		if (ipend & SER_INT_OVERRUN)
272 			uart_intr_overrun(sc);
273 		if (ipend & SER_INT_BREAK)
274 			uart_intr_break(sc);
275 		if (ipend & SER_INT_RXREADY)
276 			uart_intr_rxready(sc);
277 		if (ipend & SER_INT_SIGCHG)
278 			uart_intr_sigchg(sc);
279 		if (ipend & SER_INT_TXIDLE)
280 			uart_intr_txidle(sc);
281 	}
282 
283 	if (sc->sc_polled) {
284 		callout_reset(&sc->sc_timer, hz / uart_poll_freq,
285 		    (timeout_t *)uart_intr, sc);
286 	}
287 
288 	return ((cnt == 0) ? FILTER_STRAY :
289 	    ((cnt == 20) ? FILTER_SCHEDULE_THREAD : FILTER_HANDLED));
290 }
291 
292 serdev_intr_t *
293 uart_bus_ihand(device_t dev, int ipend)
294 {
295 
296 	switch (ipend) {
297 	case SER_INT_BREAK:
298 		return (uart_intr_break);
299 	case SER_INT_OVERRUN:
300 		return (uart_intr_overrun);
301 	case SER_INT_RXREADY:
302 		return (uart_intr_rxready);
303 	case SER_INT_SIGCHG:
304 		return (uart_intr_sigchg);
305 	case SER_INT_TXIDLE:
306 		return (uart_intr_txidle);
307 	}
308 	return (NULL);
309 }
310 
311 int
312 uart_bus_ipend(device_t dev)
313 {
314 	struct uart_softc *sc;
315 
316 	sc = device_get_softc(dev);
317 	return (UART_IPEND(sc));
318 }
319 
320 int
321 uart_bus_sysdev(device_t dev)
322 {
323 	struct uart_softc *sc;
324 
325 	sc = device_get_softc(dev);
326 	return ((sc->sc_sysdev != NULL) ? 1 : 0);
327 }
328 
329 int
330 uart_bus_probe(device_t dev, int regshft, int rclk, int rid, int chan)
331 {
332 	struct uart_softc *sc;
333 	struct uart_devinfo *sysdev;
334 	int error;
335 
336 	sc = device_get_softc(dev);
337 
338 	/*
339 	 * All uart_class references are weak. Check that the needed
340 	 * class has been compiled-in. Fail if not.
341 	 */
342 	if (sc->sc_class == NULL)
343 		return (ENXIO);
344 
345 	/*
346 	 * Initialize the instance. Note that the instance (=softc) does
347 	 * not necessarily match the hardware specific softc. We can't do
348 	 * anything about it now, because we may not attach to the device.
349 	 * Hardware drivers cannot use any of the class specific fields
350 	 * while probing.
351 	 */
352 	kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class);
353 	sc->sc_dev = dev;
354 	if (device_get_desc(dev) == NULL)
355 		device_set_desc(dev, uart_getname(sc->sc_class));
356 
357 	/*
358 	 * Allocate the register resource. We assume that all UARTs have
359 	 * a single register window in either I/O port space or memory
360 	 * mapped I/O space. Any UART that needs multiple windows will
361 	 * consequently not be supported by this driver as-is. We try I/O
362 	 * port space first because that's the common case.
363 	 */
364 	sc->sc_rrid = rid;
365 	sc->sc_rtype = SYS_RES_IOPORT;
366 	sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid,
367 	    0, ~0, uart_getrange(sc->sc_class), RF_ACTIVE);
368 	if (sc->sc_rres == NULL) {
369 		sc->sc_rrid = rid;
370 		sc->sc_rtype = SYS_RES_MEMORY;
371 		sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype,
372 		    &sc->sc_rrid, 0, ~0, uart_getrange(sc->sc_class),
373 		    RF_ACTIVE);
374 		if (sc->sc_rres == NULL)
375 			return (ENXIO);
376 	}
377 
378 	/*
379 	 * Fill in the bus access structure and compare this device with
380 	 * a possible console device and/or a debug port. We set the flags
381 	 * in the softc so that the hardware dependent probe can adjust
382 	 * accordingly. In general, you don't want to permanently disrupt
383 	 * console I/O.
384 	 */
385 	sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
386 	sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
387 	sc->sc_bas.chan = chan;
388 	sc->sc_bas.regshft = regshft;
389 	sc->sc_bas.rclk = (rclk == 0) ? sc->sc_class->uc_rclk : rclk;
390 
391 	SLIST_FOREACH(sysdev, &uart_sysdevs, next) {
392 		if (chan == sysdev->bas.chan &&
393 		    uart_cpu_eqres(&sc->sc_bas, &sysdev->bas)) {
394 			/* XXX check if ops matches class. */
395 			sc->sc_sysdev = sysdev;
396 			sysdev->bas.rclk = sc->sc_bas.rclk;
397 		}
398 	}
399 
400 	error = UART_PROBE(sc);
401 	bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
402 	return ((error) ? error : BUS_PROBE_DEFAULT);
403 }
404 
405 int
406 uart_bus_attach(device_t dev)
407 {
408 	struct uart_softc *sc, *sc0;
409 	const char *sep;
410 	int error, filt;
411 
412 	/*
413 	 * The sc_class field defines the type of UART we're going to work
414 	 * with and thus the size of the softc. Replace the generic softc
415 	 * with one that matches the UART now that we're certain we handle
416 	 * the device.
417 	 */
418 	sc0 = device_get_softc(dev);
419 	if (sc0->sc_class->size > sizeof(*sc)) {
420 		sc = malloc(sc0->sc_class->size, M_UART, M_WAITOK|M_ZERO);
421 		bcopy(sc0, sc, sizeof(*sc));
422 		device_set_softc(dev, sc);
423 	} else
424 		sc = sc0;
425 
426 	/*
427 	 * Now that we know the softc for this device, connect the back
428 	 * pointer from the sysdev for this device, if any
429 	 */
430 	if (sc->sc_sysdev != NULL)
431 		sc->sc_sysdev->sc = sc;
432 
433 	/*
434 	 * Protect ourselves against interrupts while we're not completely
435 	 * finished attaching and initializing. We don't expect interrupts
436 	 * until after UART_ATTACH() though.
437 	 */
438 	sc->sc_leaving = 1;
439 
440 	mtx_init(&sc->sc_hwmtx_s, "uart_hwmtx", NULL, MTX_SPIN);
441 	if (sc->sc_hwmtx == NULL)
442 		sc->sc_hwmtx = &sc->sc_hwmtx_s;
443 
444 	/*
445 	 * Re-allocate. We expect that the softc contains the information
446 	 * collected by uart_bus_probe() intact.
447 	 */
448 	sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid,
449 	    0, ~0, uart_getrange(sc->sc_class), RF_ACTIVE);
450 	if (sc->sc_rres == NULL) {
451 		mtx_destroy(&sc->sc_hwmtx_s);
452 		return (ENXIO);
453 	}
454 	sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
455 	sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
456 
457 	/*
458 	 * Ensure there is room for at least three full FIFOs of data in the
459 	 * receive buffer (handles the case of low-level drivers with huge
460 	 * FIFOs), and also ensure that there is no less than the historical
461 	 * size of 384 bytes (handles the typical small-FIFO case).
462 	 */
463 	sc->sc_rxbufsz = MAX(384, sc->sc_rxfifosz * 3);
464 	sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf),
465 	    M_UART, M_WAITOK);
466 	sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf),
467 	    M_UART, M_WAITOK);
468 
469 	error = UART_ATTACH(sc);
470 	if (error)
471 		goto fail;
472 
473 	if (sc->sc_hwiflow || sc->sc_hwoflow) {
474 		sep = "";
475 		device_print_prettyname(dev);
476 		if (sc->sc_hwiflow) {
477 			printf("%sRTS iflow", sep);
478 			sep = ", ";
479 		}
480 		if (sc->sc_hwoflow) {
481 			printf("%sCTS oflow", sep);
482 			sep = ", ";
483 		}
484 		printf("\n");
485 	}
486 
487 	if (sc->sc_sysdev != NULL) {
488 		if (sc->sc_sysdev->baudrate == 0) {
489 			if (UART_IOCTL(sc, UART_IOCTL_BAUD,
490 			    (intptr_t)&sc->sc_sysdev->baudrate) != 0)
491 				sc->sc_sysdev->baudrate = -1;
492 		}
493 		switch (sc->sc_sysdev->type) {
494 		case UART_DEV_CONSOLE:
495 			device_printf(dev, "console");
496 			break;
497 		case UART_DEV_DBGPORT:
498 			device_printf(dev, "debug port");
499 			break;
500 		case UART_DEV_KEYBOARD:
501 			device_printf(dev, "keyboard");
502 			break;
503 		default:
504 			device_printf(dev, "unknown system device");
505 			break;
506 		}
507 		printf(" (%d,%c,%d,%d)\n", sc->sc_sysdev->baudrate,
508 		    "noems"[sc->sc_sysdev->parity], sc->sc_sysdev->databits,
509 		    sc->sc_sysdev->stopbits);
510 	}
511 
512 	sc->sc_pps.ppscap = PPS_CAPTUREBOTH;
513 	pps_init(&sc->sc_pps);
514 
515 	sc->sc_leaving = 0;
516 	filt = uart_intr(sc);
517 
518 	/*
519 	 * Don't use interrupts if we couldn't clear any pending interrupt
520 	 * conditions. We may have broken H/W and polling is probably the
521 	 * safest thing to do.
522 	 */
523 	if (filt != FILTER_SCHEDULE_THREAD && !uart_force_poll) {
524 		sc->sc_irid = 0;
525 		sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
526 		    &sc->sc_irid, RF_ACTIVE | RF_SHAREABLE);
527 	}
528 	if (sc->sc_ires != NULL) {
529 		error = bus_setup_intr(dev, sc->sc_ires, INTR_TYPE_TTY,
530 		    uart_intr, NULL, sc, &sc->sc_icookie);
531 		sc->sc_fastintr = (error == 0) ? 1 : 0;
532 
533 		if (!sc->sc_fastintr)
534 			error = bus_setup_intr(dev, sc->sc_ires,
535 			    INTR_TYPE_TTY | INTR_MPSAFE, NULL,
536 			    (driver_intr_t *)uart_intr, sc, &sc->sc_icookie);
537 
538 		if (error) {
539 			device_printf(dev, "could not activate interrupt\n");
540 			bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
541 			    sc->sc_ires);
542 			sc->sc_ires = NULL;
543 		}
544 	}
545 	if (sc->sc_ires == NULL) {
546 		/* No interrupt resource. Force polled mode. */
547 		sc->sc_polled = 1;
548 		callout_init(&sc->sc_timer, 1);
549 		callout_reset(&sc->sc_timer, hz / uart_poll_freq,
550 		    (timeout_t *)uart_intr, sc);
551 	}
552 
553 	if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
554 		sep = "";
555 		device_print_prettyname(dev);
556 		if (sc->sc_fastintr) {
557 			printf("%sfast interrupt", sep);
558 			sep = ", ";
559 		}
560 		if (sc->sc_polled) {
561 			printf("%spolled mode (%dHz)", sep, uart_poll_freq);
562 			sep = ", ";
563 		}
564 		printf("\n");
565 	}
566 
567 	error = (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL)
568 	    ? (*sc->sc_sysdev->attach)(sc) : uart_tty_attach(sc);
569 	if (error)
570 		goto fail;
571 
572 	if (sc->sc_sysdev != NULL)
573 		sc->sc_sysdev->hwmtx = sc->sc_hwmtx;
574 
575 	return (0);
576 
577  fail:
578 	free(sc->sc_txbuf, M_UART);
579 	free(sc->sc_rxbuf, M_UART);
580 
581 	if (sc->sc_ires != NULL) {
582 		bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
583 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
584 		    sc->sc_ires);
585 	}
586 	bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
587 
588 	mtx_destroy(&sc->sc_hwmtx_s);
589 
590 	return (error);
591 }
592 
593 int
594 uart_bus_detach(device_t dev)
595 {
596 	struct uart_softc *sc;
597 
598 	sc = device_get_softc(dev);
599 
600 	sc->sc_leaving = 1;
601 
602 	if (sc->sc_sysdev != NULL)
603 		sc->sc_sysdev->hwmtx = NULL;
604 
605 	UART_DETACH(sc);
606 
607 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL)
608 		(*sc->sc_sysdev->detach)(sc);
609 	else
610 		uart_tty_detach(sc);
611 
612 	free(sc->sc_txbuf, M_UART);
613 	free(sc->sc_rxbuf, M_UART);
614 
615 	if (sc->sc_ires != NULL) {
616 		bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
617 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
618 		    sc->sc_ires);
619 	}
620 	bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
621 
622 	mtx_destroy(&sc->sc_hwmtx_s);
623 
624 	if (sc->sc_class->size > sizeof(*sc)) {
625 		device_set_softc(dev, NULL);
626 		free(sc, M_UART);
627 	} else
628 		device_set_softc(dev, NULL);
629 
630 	return (0);
631 }
632 
633 int
634 uart_bus_resume(device_t dev)
635 {
636 	struct uart_softc *sc;
637 
638 	sc = device_get_softc(dev);
639 	return (UART_ATTACH(sc));
640 }
641 
642 void
643 uart_grab(struct uart_devinfo *di)
644 {
645 
646 	if (di->sc)
647 		UART_GRAB(di->sc);
648 }
649 
650 void
651 uart_ungrab(struct uart_devinfo *di)
652 {
653 
654 	if (di->sc)
655 		UART_UNGRAB(di->sc);
656 }
657