]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/uart/uart_core.c
MFC r286469, r286591, r286595, r286596, r286613:
[FreeBSD/stable/10.git] / sys / dev / uart / uart_core.c
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 const 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 TUNABLE_INT("debug.uart_poll_freq", &uart_poll_freq);
67
68 #define PPS_MODE_DISABLED       0
69 #define PPS_MODE_CTS            1
70 #define PPS_MODE_DCD            2
71
72 static inline int
73 uart_pps_signal(int pps_mode)
74 {
75
76         switch(pps_mode) {
77         case PPS_MODE_CTS:
78                 return (SER_CTS);
79         case PPS_MODE_DCD:
80                 return (SER_DCD);
81         }
82         return (0);
83 }
84 static inline int
85 uart_pps_mode_valid(int pps_mode)
86 {
87
88         switch(pps_mode) {
89         case PPS_MODE_DISABLED:
90         case PPS_MODE_CTS:
91         case PPS_MODE_DCD:
92                 return (true);
93         }
94         return (false);
95 }
96
97 static const char *
98 uart_pps_mode_name(int pps_mode)
99 {
100         switch(pps_mode) {
101         case PPS_MODE_DISABLED:
102                 return ("disabled");
103         case PPS_MODE_CTS:
104                 return ("CTS");
105         case PPS_MODE_DCD:
106                 return ("DCD");
107         }
108         return ("invalid");
109 }
110
111 static int
112 uart_pps_mode_sysctl(SYSCTL_HANDLER_ARGS)
113 {
114         struct uart_softc *sc;
115         int err, tmp;
116
117         sc = arg1;
118         tmp = sc->sc_pps_mode;
119         err = sysctl_handle_int(oidp, &tmp, 0, req);
120         if (err != 0 || req->newptr == NULL)
121                 return (err);
122         if (!uart_pps_mode_valid(tmp))
123                 return (EINVAL);
124         sc->sc_pps_mode = tmp;
125         return(0);
126 }
127
128 static void
129 uart_pps_init(struct uart_softc *sc)
130 {
131         struct sysctl_ctx_list *ctx;
132         struct sysctl_oid *tree;
133
134         ctx = device_get_sysctl_ctx(sc->sc_dev);
135         tree = device_get_sysctl_tree(sc->sc_dev);
136
137         /*
138          * The historical default for pps capture mode is either DCD or CTS,
139          * depending on the UART_PPS_ON_CTS kernel option.  Start with that,
140          * then try to fetch the tunable that overrides the mode for all uart
141          * devices, then try to fetch the sysctl-tunable that overrides the mode
142          * for one specific device.
143          */
144 #ifdef UART_PPS_ON_CTS
145         sc->sc_pps_mode = PPS_MODE_CTS;
146 #else
147         sc->sc_pps_mode = PPS_MODE_DCD;
148 #endif
149         TUNABLE_INT_FETCH("hw.uart.pps_mode", &sc->sc_pps_mode);
150         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "pps_mode",
151             CTLTYPE_INT | CTLFLAG_RWTUN, sc, 0, uart_pps_mode_sysctl, "I",
152             "pulse capturing mode - 0/1/2 - disabled/CTS/DCD");
153
154         if (!uart_pps_mode_valid(sc->sc_pps_mode)) {
155                 device_printf(sc->sc_dev, 
156                     "Invalid pps_mode %d configured; disabling PPS capture\n",
157                     sc->sc_pps_mode);
158                 sc->sc_pps_mode = PPS_MODE_DISABLED;
159         } else if (bootverbose) {
160                 device_printf(sc->sc_dev, "PPS capture mode %d (%s)\n",
161                     sc->sc_pps_mode, uart_pps_mode_name(sc->sc_pps_mode));
162         }
163
164         sc->sc_pps.ppscap = PPS_CAPTUREBOTH;
165         sc->sc_pps.driver_mtx = uart_tty_getlock(sc);
166         sc->sc_pps.driver_abi = PPS_ABI_VERSION;
167         pps_init_abi(&sc->sc_pps);
168 }
169
170 void
171 uart_add_sysdev(struct uart_devinfo *di)
172 {
173         SLIST_INSERT_HEAD(&uart_sysdevs, di, next);
174 }
175
176 const char *
177 uart_getname(struct uart_class *uc)
178 {
179         return ((uc != NULL) ? uc->name : NULL);
180 }
181
182 struct uart_ops *
183 uart_getops(struct uart_class *uc)
184 {
185         return ((uc != NULL) ? uc->uc_ops : NULL);
186 }
187
188 int
189 uart_getrange(struct uart_class *uc)
190 {
191         return ((uc != NULL) ? uc->uc_range : 0);
192 }
193
194 /*
195  * Schedule a soft interrupt. We do this on the 0 to !0 transition
196  * of the TTY pending interrupt status.
197  */
198 void
199 uart_sched_softih(struct uart_softc *sc, uint32_t ipend)
200 {
201         uint32_t new, old;
202
203         do {
204                 old = sc->sc_ttypend;
205                 new = old | ipend;
206         } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
207
208         if ((old & SER_INT_MASK) == 0)
209                 swi_sched(sc->sc_softih, 0);
210 }
211
212 /*
213  * A break condition has been detected. We treat the break condition as
214  * a special case that should not happen during normal operation. When
215  * the break condition is to be passed to higher levels in the form of
216  * a NUL character, we really want the break to be in the right place in
217  * the input stream. The overhead to achieve that is not in relation to
218  * the exceptional nature of the break condition, so we permit ourselves
219  * to be sloppy.
220  */
221 static __inline int
222 uart_intr_break(void *arg)
223 {
224         struct uart_softc *sc = arg;
225
226 #if defined(KDB)
227         if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
228                 if (kdb_break())
229                         return (0);
230         }
231 #endif
232         if (sc->sc_opened)
233                 uart_sched_softih(sc, SER_INT_BREAK);
234         return (0);
235 }
236
237 /*
238  * Handle a receiver overrun situation. We lost at least 1 byte in the
239  * input stream and it's our job to contain the situation. We grab as
240  * much of the data we can, but otherwise flush the receiver FIFO to
241  * create some breathing room. The net effect is that we avoid the
242  * overrun condition to happen for the next X characters, where X is
243  * related to the FIFO size at the cost of losing data right away.
244  * So, instead of having multiple overrun interrupts in close proximity
245  * to each other and possibly pessimizing UART interrupt latency for
246  * other UARTs in a multiport configuration, we create a longer segment
247  * of missing characters by freeing up the FIFO.
248  * Each overrun condition is marked in the input buffer by a token. The
249  * token represents the loss of at least one, but possible more bytes in
250  * the input stream.
251  */
252 static __inline int
253 uart_intr_overrun(void *arg)
254 {
255         struct uart_softc *sc = arg;
256
257         if (sc->sc_opened) {
258                 UART_RECEIVE(sc);
259                 if (uart_rx_put(sc, UART_STAT_OVERRUN))
260                         sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
261                 uart_sched_softih(sc, SER_INT_RXREADY);
262         }
263         UART_FLUSH(sc, UART_FLUSH_RECEIVER);
264         return (0);
265 }
266
267 /*
268  * Received data ready.
269  */
270 static __inline int
271 uart_intr_rxready(void *arg)
272 {
273         struct uart_softc *sc = arg;
274         int rxp;
275
276         rxp = sc->sc_rxput;
277         UART_RECEIVE(sc);
278 #if defined(KDB)
279         if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
280                 while (rxp != sc->sc_rxput) {
281                         kdb_alt_break(sc->sc_rxbuf[rxp++], &sc->sc_altbrk);
282                         if (rxp == sc->sc_rxbufsz)
283                                 rxp = 0;
284                 }
285         }
286 #endif
287         if (sc->sc_opened)
288                 uart_sched_softih(sc, SER_INT_RXREADY);
289         else
290                 sc->sc_rxput = sc->sc_rxget;    /* Ignore received data. */
291         return (1);
292 }
293
294 /*
295  * Line or modem status change (OOB signalling).
296  * We pass the signals to the software interrupt handler for further
297  * processing. Note that we merge the delta bits, but set the state
298  * bits. This is to avoid losing state transitions due to having more
299  * than 1 hardware interrupt between software interrupts.
300  */
301 static __inline int
302 uart_intr_sigchg(void *arg)
303 {
304         struct uart_softc *sc = arg;
305         int new, old, pps_sig, sig;
306
307         sig = UART_GETSIG(sc);
308
309         /*
310          * Time pulse counting support. Note that both CTS and DCD are
311          * active-low signals. The status bit is high to indicate that
312          * the signal on the line is low, which corresponds to a PPS
313          * clear event.
314          */
315         if (sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) {
316                 pps_sig = uart_pps_signal(sc->sc_pps_mode);
317                 if (sig & SER_DELTA(pps_sig)) {
318                         pps_capture(&sc->sc_pps);
319                         pps_event(&sc->sc_pps, (sig & pps_sig) ?
320                             PPS_CAPTURECLEAR : PPS_CAPTUREASSERT);
321                 }
322         }
323
324         /*
325          * Keep track of signal changes, even when the device is not
326          * opened. This allows us to inform upper layers about a
327          * possible loss of DCD and thus the existence of a (possibly)
328          * different connection when we have DCD back, during the time
329          * that the device was closed.
330          */
331         do {
332                 old = sc->sc_ttypend;
333                 new = old & ~SER_MASK_STATE;
334                 new |= sig & SER_INT_SIGMASK;
335         } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
336
337         if (sc->sc_opened)
338                 uart_sched_softih(sc, SER_INT_SIGCHG);
339         return (1);
340 }
341
342 /*
343  * The transmitter can accept more data.
344  */
345 static __inline int
346 uart_intr_txidle(void *arg)
347 {
348         struct uart_softc *sc = arg;
349
350         if (sc->sc_txbusy) {
351                 sc->sc_txbusy = 0;
352                 uart_sched_softih(sc, SER_INT_TXIDLE);
353         }
354         return (0);
355 }
356
357 static int
358 uart_intr(void *arg)
359 {
360         struct uart_softc *sc = arg;
361         int cnt, ipend, testintr;
362
363         if (sc->sc_leaving)
364                 return (FILTER_STRAY);
365
366         cnt = 0;
367         testintr = sc->sc_testintr;
368         while ((!testintr || cnt < 20) && (ipend = UART_IPEND(sc)) != 0) {
369                 cnt++;
370                 if (ipend & SER_INT_OVERRUN)
371                         uart_intr_overrun(sc);
372                 if (ipend & SER_INT_BREAK)
373                         uart_intr_break(sc);
374                 if (ipend & SER_INT_RXREADY)
375                         uart_intr_rxready(sc);
376                 if (ipend & SER_INT_SIGCHG)
377                         uart_intr_sigchg(sc);
378                 if (ipend & SER_INT_TXIDLE)
379                         uart_intr_txidle(sc);
380         }
381
382         if (sc->sc_polled) {
383                 callout_reset(&sc->sc_timer, hz / uart_poll_freq,
384                     (timeout_t *)uart_intr, sc);
385         }
386
387         return ((cnt == 0) ? FILTER_STRAY :
388             ((testintr && cnt == 20) ? FILTER_SCHEDULE_THREAD :
389             FILTER_HANDLED));
390 }
391
392 serdev_intr_t *
393 uart_bus_ihand(device_t dev, int ipend)
394 {
395
396         switch (ipend) {
397         case SER_INT_BREAK:
398                 return (uart_intr_break);
399         case SER_INT_OVERRUN:
400                 return (uart_intr_overrun);
401         case SER_INT_RXREADY:
402                 return (uart_intr_rxready);
403         case SER_INT_SIGCHG:
404                 return (uart_intr_sigchg);
405         case SER_INT_TXIDLE:
406                 return (uart_intr_txidle);
407         }
408         return (NULL);
409 }
410
411 int
412 uart_bus_ipend(device_t dev)
413 {
414         struct uart_softc *sc;
415
416         sc = device_get_softc(dev);
417         return (UART_IPEND(sc));
418 }
419
420 int
421 uart_bus_sysdev(device_t dev)
422 {
423         struct uart_softc *sc;
424
425         sc = device_get_softc(dev);
426         return ((sc->sc_sysdev != NULL) ? 1 : 0);
427 }
428
429 int
430 uart_bus_probe(device_t dev, int regshft, int rclk, int rid, int chan)
431 {
432         struct uart_softc *sc;
433         struct uart_devinfo *sysdev;
434         int error;
435
436         sc = device_get_softc(dev);
437
438         /*
439          * All uart_class references are weak. Check that the needed
440          * class has been compiled-in. Fail if not.
441          */
442         if (sc->sc_class == NULL)
443                 return (ENXIO);
444
445         /*
446          * Initialize the instance. Note that the instance (=softc) does
447          * not necessarily match the hardware specific softc. We can't do
448          * anything about it now, because we may not attach to the device.
449          * Hardware drivers cannot use any of the class specific fields
450          * while probing.
451          */
452         kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class);
453         sc->sc_dev = dev;
454         if (device_get_desc(dev) == NULL)
455                 device_set_desc(dev, uart_getname(sc->sc_class));
456
457         /*
458          * Allocate the register resource. We assume that all UARTs have
459          * a single register window in either I/O port space or memory
460          * mapped I/O space. Any UART that needs multiple windows will
461          * consequently not be supported by this driver as-is. We try I/O
462          * port space first because that's the common case.
463          */
464         sc->sc_rrid = rid;
465         sc->sc_rtype = SYS_RES_IOPORT;
466         sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid,
467             0, ~0, uart_getrange(sc->sc_class), RF_ACTIVE);
468         if (sc->sc_rres == NULL) {
469                 sc->sc_rrid = rid;
470                 sc->sc_rtype = SYS_RES_MEMORY;
471                 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype,
472                     &sc->sc_rrid, 0, ~0, uart_getrange(sc->sc_class),
473                     RF_ACTIVE);
474                 if (sc->sc_rres == NULL)
475                         return (ENXIO);
476         }
477
478         /*
479          * Fill in the bus access structure and compare this device with
480          * a possible console device and/or a debug port. We set the flags
481          * in the softc so that the hardware dependent probe can adjust
482          * accordingly. In general, you don't want to permanently disrupt
483          * console I/O.
484          */
485         sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
486         sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
487         sc->sc_bas.chan = chan;
488         sc->sc_bas.regshft = regshft;
489         sc->sc_bas.rclk = (rclk == 0) ? sc->sc_class->uc_rclk : rclk;
490
491         SLIST_FOREACH(sysdev, &uart_sysdevs, next) {
492                 if (chan == sysdev->bas.chan &&
493                     uart_cpu_eqres(&sc->sc_bas, &sysdev->bas)) {
494                         /* XXX check if ops matches class. */
495                         sc->sc_sysdev = sysdev;
496                         sysdev->bas.rclk = sc->sc_bas.rclk;
497                 }
498         }
499
500         error = UART_PROBE(sc);
501         bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
502         return ((error) ? error : BUS_PROBE_DEFAULT);
503 }
504
505 int
506 uart_bus_attach(device_t dev)
507 {
508         struct uart_softc *sc, *sc0;
509         const char *sep;
510         int error, filt;
511
512         /*
513          * The sc_class field defines the type of UART we're going to work
514          * with and thus the size of the softc. Replace the generic softc
515          * with one that matches the UART now that we're certain we handle
516          * the device.
517          */
518         sc0 = device_get_softc(dev);
519         if (sc0->sc_class->size > sizeof(*sc)) {
520                 sc = malloc(sc0->sc_class->size, M_UART, M_WAITOK|M_ZERO);
521                 bcopy(sc0, sc, sizeof(*sc));
522                 device_set_softc(dev, sc);
523         } else
524                 sc = sc0;
525
526         /*
527          * Now that we know the softc for this device, connect the back
528          * pointer from the sysdev for this device, if any
529          */
530         if (sc->sc_sysdev != NULL)
531                 sc->sc_sysdev->sc = sc;
532
533         /*
534          * Protect ourselves against interrupts while we're not completely
535          * finished attaching and initializing. We don't expect interrupts
536          * until after UART_ATTACH(), though.
537          */
538         sc->sc_leaving = 1;
539
540         mtx_init(&sc->sc_hwmtx_s, "uart_hwmtx", NULL, MTX_SPIN);
541         if (sc->sc_hwmtx == NULL)
542                 sc->sc_hwmtx = &sc->sc_hwmtx_s;
543
544         /*
545          * Re-allocate. We expect that the softc contains the information
546          * collected by uart_bus_probe() intact.
547          */
548         sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid,
549             0, ~0, uart_getrange(sc->sc_class), RF_ACTIVE);
550         if (sc->sc_rres == NULL) {
551                 mtx_destroy(&sc->sc_hwmtx_s);
552                 return (ENXIO);
553         }
554         sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
555         sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
556
557         /*
558          * Ensure there is room for at least three full FIFOs of data in the
559          * receive buffer (handles the case of low-level drivers with huge
560          * FIFOs), and also ensure that there is no less than the historical
561          * size of 384 bytes (handles the typical small-FIFO case).
562          */
563         sc->sc_rxbufsz = MAX(384, sc->sc_rxfifosz * 3);
564         sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf),
565             M_UART, M_WAITOK);
566         sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf),
567             M_UART, M_WAITOK);
568
569         error = UART_ATTACH(sc);
570         if (error)
571                 goto fail;
572
573         if (sc->sc_hwiflow || sc->sc_hwoflow) {
574                 sep = "";
575                 device_print_prettyname(dev);
576                 if (sc->sc_hwiflow) {
577                         printf("%sRTS iflow", sep);
578                         sep = ", ";
579                 }
580                 if (sc->sc_hwoflow) {
581                         printf("%sCTS oflow", sep);
582                         sep = ", ";
583                 }
584                 printf("\n");
585         }
586
587         if (sc->sc_sysdev != NULL) {
588                 if (sc->sc_sysdev->baudrate == 0) {
589                         if (UART_IOCTL(sc, UART_IOCTL_BAUD,
590                             (intptr_t)&sc->sc_sysdev->baudrate) != 0)
591                                 sc->sc_sysdev->baudrate = -1;
592                 }
593                 switch (sc->sc_sysdev->type) {
594                 case UART_DEV_CONSOLE:
595                         device_printf(dev, "console");
596                         break;
597                 case UART_DEV_DBGPORT:
598                         device_printf(dev, "debug port");
599                         break;
600                 case UART_DEV_KEYBOARD:
601                         device_printf(dev, "keyboard");
602                         break;
603                 default:
604                         device_printf(dev, "unknown system device");
605                         break;
606                 }
607                 printf(" (%d,%c,%d,%d)\n", sc->sc_sysdev->baudrate,
608                     "noems"[sc->sc_sysdev->parity], sc->sc_sysdev->databits,
609                     sc->sc_sysdev->stopbits);
610         }
611
612         sc->sc_leaving = 0;
613         sc->sc_testintr = 1;
614         filt = uart_intr(sc);
615         sc->sc_testintr = 0;
616
617         /*
618          * Don't use interrupts if we couldn't clear any pending interrupt
619          * conditions. We may have broken H/W and polling is probably the
620          * safest thing to do.
621          */
622         if (filt != FILTER_SCHEDULE_THREAD) {
623                 sc->sc_irid = 0;
624                 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
625                     &sc->sc_irid, RF_ACTIVE | RF_SHAREABLE);
626         }
627         if (sc->sc_ires != NULL) {
628                 error = bus_setup_intr(dev, sc->sc_ires, INTR_TYPE_TTY,
629                     uart_intr, NULL, sc, &sc->sc_icookie);
630                 sc->sc_fastintr = (error == 0) ? 1 : 0;
631
632                 if (!sc->sc_fastintr)
633                         error = bus_setup_intr(dev, sc->sc_ires,
634                             INTR_TYPE_TTY | INTR_MPSAFE, NULL,
635                             (driver_intr_t *)uart_intr, sc, &sc->sc_icookie);
636
637                 if (error) {
638                         device_printf(dev, "could not activate interrupt\n");
639                         bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
640                             sc->sc_ires);
641                         sc->sc_ires = NULL;
642                 }
643         }
644         if (sc->sc_ires == NULL) {
645                 /* No interrupt resource. Force polled mode. */
646                 sc->sc_polled = 1;
647                 callout_init(&sc->sc_timer, 1);
648         }
649
650         if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
651                 sep = "";
652                 device_print_prettyname(dev);
653                 if (sc->sc_fastintr) {
654                         printf("%sfast interrupt", sep);
655                         sep = ", ";
656                 }
657                 if (sc->sc_polled) {
658                         printf("%spolled mode (%dHz)", sep, uart_poll_freq);
659                         sep = ", ";
660                 }
661                 printf("\n");
662         }
663
664         if (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL) {
665                 if ((error = sc->sc_sysdev->attach(sc)) != 0)
666                         goto fail;
667         } else {
668                 if ((error = uart_tty_attach(sc)) != 0)
669                         goto fail;
670                 uart_pps_init(sc);
671         }
672
673         if (sc->sc_sysdev != NULL)
674                 sc->sc_sysdev->hwmtx = sc->sc_hwmtx;
675
676         return (0);
677
678  fail:
679         free(sc->sc_txbuf, M_UART);
680         free(sc->sc_rxbuf, M_UART);
681
682         if (sc->sc_ires != NULL) {
683                 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
684                 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
685                     sc->sc_ires);
686         }
687         bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
688
689         mtx_destroy(&sc->sc_hwmtx_s);
690
691         return (error);
692 }
693
694 int
695 uart_bus_detach(device_t dev)
696 {
697         struct uart_softc *sc;
698
699         sc = device_get_softc(dev);
700
701         sc->sc_leaving = 1;
702
703         if (sc->sc_sysdev != NULL)
704                 sc->sc_sysdev->hwmtx = NULL;
705
706         UART_DETACH(sc);
707
708         if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL)
709                 (*sc->sc_sysdev->detach)(sc);
710         else
711                 uart_tty_detach(sc);
712
713         free(sc->sc_txbuf, M_UART);
714         free(sc->sc_rxbuf, M_UART);
715
716         if (sc->sc_ires != NULL) {
717                 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
718                 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
719                     sc->sc_ires);
720         }
721         bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
722
723         mtx_destroy(&sc->sc_hwmtx_s);
724
725         if (sc->sc_class->size > sizeof(*sc)) {
726                 device_set_softc(dev, NULL);
727                 free(sc, M_UART);
728         } else
729                 device_set_softc(dev, NULL);
730
731         return (0);
732 }
733
734 int
735 uart_bus_resume(device_t dev)
736 {
737         struct uart_softc *sc;
738
739         sc = device_get_softc(dev);
740         return (UART_ATTACH(sc));
741 }
742
743 void
744 uart_grab(struct uart_devinfo *di)
745 {
746
747         if (di->sc)
748                 UART_GRAB(di->sc);
749 }
750
751 void
752 uart_ungrab(struct uart_devinfo *di)
753 {
754
755         if (di->sc)
756                 UART_UNGRAB(di->sc);
757 }