2 * Copyright (c) 2003 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/interrupt.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>
45 #include <machine/resource.h>
46 #include <machine/stdarg.h>
48 #include <dev/uart/uart.h>
49 #include <dev/uart/uart_bus.h>
50 #include <dev/uart/uart_cpu.h>
54 devclass_t uart_devclass;
55 const char uart_driver_name[] = "uart";
57 SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs =
58 SLIST_HEAD_INITIALIZER(uart_sysdevs);
60 static MALLOC_DEFINE(M_UART, "UART", "UART driver");
62 #ifndef UART_POLL_FREQ
63 #define UART_POLL_FREQ 50
65 static int uart_poll_freq = UART_POLL_FREQ;
66 TUNABLE_INT("debug.uart_poll_freq", &uart_poll_freq);
68 #define PPS_MODE_DISABLED 0
69 #define PPS_MODE_CTS 1
70 #define PPS_MODE_DCD 2
73 uart_pps_signal(int pps_mode)
85 uart_pps_mode_valid(int pps_mode)
89 case PPS_MODE_DISABLED:
98 uart_pps_mode_name(int pps_mode)
101 case PPS_MODE_DISABLED:
112 uart_pps_mode_sysctl(SYSCTL_HANDLER_ARGS)
114 struct uart_softc *sc;
118 tmp = sc->sc_pps_mode;
119 err = sysctl_handle_int(oidp, &tmp, 0, req);
120 if (err != 0 || req->newptr == NULL)
122 if (!uart_pps_mode_valid(tmp))
124 sc->sc_pps_mode = tmp;
129 uart_pps_init(struct uart_softc *sc)
131 struct sysctl_ctx_list *ctx;
132 struct sysctl_oid *tree;
134 ctx = device_get_sysctl_ctx(sc->sc_dev);
135 tree = device_get_sysctl_tree(sc->sc_dev);
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.
144 #ifdef UART_PPS_ON_CTS
145 sc->sc_pps_mode = PPS_MODE_CTS;
147 sc->sc_pps_mode = PPS_MODE_DCD;
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");
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",
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));
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);
171 uart_add_sysdev(struct uart_devinfo *di)
173 SLIST_INSERT_HEAD(&uart_sysdevs, di, next);
177 uart_getname(struct uart_class *uc)
179 return ((uc != NULL) ? uc->name : NULL);
183 uart_getops(struct uart_class *uc)
185 return ((uc != NULL) ? uc->uc_ops : NULL);
189 uart_getrange(struct uart_class *uc)
191 return ((uc != NULL) ? uc->uc_range : 0);
195 * Schedule a soft interrupt. We do this on the 0 to !0 transition
196 * of the TTY pending interrupt status.
199 uart_sched_softih(struct uart_softc *sc, uint32_t ipend)
204 old = sc->sc_ttypend;
206 } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
208 if ((old & SER_INT_MASK) == 0)
209 swi_sched(sc->sc_softih, 0);
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
222 uart_intr_break(void *arg)
224 struct uart_softc *sc = arg;
227 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
233 uart_sched_softih(sc, SER_INT_BREAK);
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
253 uart_intr_overrun(void *arg)
255 struct uart_softc *sc = arg;
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);
263 UART_FLUSH(sc, UART_FLUSH_RECEIVER);
268 * Received data ready.
271 uart_intr_rxready(void *arg)
273 struct uart_softc *sc = arg;
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)
288 uart_sched_softih(sc, SER_INT_RXREADY);
290 sc->sc_rxput = sc->sc_rxget; /* Ignore received data. */
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.
302 uart_intr_sigchg(void *arg)
304 struct uart_softc *sc = arg;
305 int new, old, pps_sig, sig;
307 sig = UART_GETSIG(sc);
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
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);
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.
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));
338 uart_sched_softih(sc, SER_INT_SIGCHG);
343 * The transmitter can accept more data.
346 uart_intr_txidle(void *arg)
348 struct uart_softc *sc = arg;
352 uart_sched_softih(sc, SER_INT_TXIDLE);
360 struct uart_softc *sc = arg;
361 int cnt, ipend, testintr;
364 return (FILTER_STRAY);
367 testintr = sc->sc_testintr;
368 while ((!testintr || cnt < 20) && (ipend = UART_IPEND(sc)) != 0) {
370 if (ipend & SER_INT_OVERRUN)
371 uart_intr_overrun(sc);
372 if (ipend & SER_INT_BREAK)
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);
383 callout_reset(&sc->sc_timer, hz / uart_poll_freq,
384 (timeout_t *)uart_intr, sc);
387 return ((cnt == 0) ? FILTER_STRAY :
388 ((testintr && cnt == 20) ? FILTER_SCHEDULE_THREAD :
393 uart_bus_ihand(device_t dev, int ipend)
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);
404 return (uart_intr_sigchg);
406 return (uart_intr_txidle);
412 uart_bus_ipend(device_t dev)
414 struct uart_softc *sc;
416 sc = device_get_softc(dev);
417 return (UART_IPEND(sc));
421 uart_bus_sysdev(device_t dev)
423 struct uart_softc *sc;
425 sc = device_get_softc(dev);
426 return ((sc->sc_sysdev != NULL) ? 1 : 0);
430 uart_bus_probe(device_t dev, int regshft, int rclk, int rid, int chan)
432 struct uart_softc *sc;
433 struct uart_devinfo *sysdev;
436 sc = device_get_softc(dev);
439 * All uart_class references are weak. Check that the needed
440 * class has been compiled-in. Fail if not.
442 if (sc->sc_class == NULL)
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
452 kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class);
454 if (device_get_desc(dev) == NULL)
455 device_set_desc(dev, uart_getname(sc->sc_class));
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.
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) {
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),
474 if (sc->sc_rres == NULL)
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
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;
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;
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);
506 uart_bus_attach(device_t dev)
508 struct uart_softc *sc, *sc0;
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
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);
527 * Now that we know the softc for this device, connect the back
528 * pointer from the sysdev for this device, if any
530 if (sc->sc_sysdev != NULL)
531 sc->sc_sysdev->sc = sc;
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.
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;
545 * Re-allocate. We expect that the softc contains the information
546 * collected by uart_bus_probe() intact.
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);
554 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
555 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
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).
563 sc->sc_rxbufsz = MAX(384, sc->sc_rxfifosz * 3);
564 sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf),
566 sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf),
569 error = UART_ATTACH(sc);
573 if (sc->sc_hwiflow || sc->sc_hwoflow) {
575 device_print_prettyname(dev);
576 if (sc->sc_hwiflow) {
577 printf("%sRTS iflow", sep);
580 if (sc->sc_hwoflow) {
581 printf("%sCTS oflow", sep);
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;
593 switch (sc->sc_sysdev->type) {
594 case UART_DEV_CONSOLE:
595 device_printf(dev, "console");
597 case UART_DEV_DBGPORT:
598 device_printf(dev, "debug port");
600 case UART_DEV_KEYBOARD:
601 device_printf(dev, "keyboard");
604 device_printf(dev, "unknown system device");
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);
614 filt = uart_intr(sc);
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.
622 if (filt != FILTER_SCHEDULE_THREAD) {
624 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
625 &sc->sc_irid, RF_ACTIVE | RF_SHAREABLE);
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;
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);
638 device_printf(dev, "could not activate interrupt\n");
639 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
644 if (sc->sc_ires == NULL) {
645 /* No interrupt resource. Force polled mode. */
647 callout_init(&sc->sc_timer, 1);
650 if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
652 device_print_prettyname(dev);
653 if (sc->sc_fastintr) {
654 printf("%sfast interrupt", sep);
658 printf("%spolled mode (%dHz)", sep, uart_poll_freq);
664 if (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL) {
665 if ((error = sc->sc_sysdev->attach(sc)) != 0)
668 if ((error = uart_tty_attach(sc)) != 0)
673 if (sc->sc_sysdev != NULL)
674 sc->sc_sysdev->hwmtx = sc->sc_hwmtx;
679 free(sc->sc_txbuf, M_UART);
680 free(sc->sc_rxbuf, M_UART);
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,
687 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
689 mtx_destroy(&sc->sc_hwmtx_s);
695 uart_bus_detach(device_t dev)
697 struct uart_softc *sc;
699 sc = device_get_softc(dev);
703 if (sc->sc_sysdev != NULL)
704 sc->sc_sysdev->hwmtx = NULL;
708 if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL)
709 (*sc->sc_sysdev->detach)(sc);
713 free(sc->sc_txbuf, M_UART);
714 free(sc->sc_rxbuf, M_UART);
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,
721 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
723 mtx_destroy(&sc->sc_hwmtx_s);
725 if (sc->sc_class->size > sizeof(*sc)) {
726 device_set_softc(dev, NULL);
729 device_set_softc(dev, NULL);
735 uart_bus_resume(device_t dev)
737 struct uart_softc *sc;
739 sc = device_get_softc(dev);
740 return (UART_ATTACH(sc));
744 uart_grab(struct uart_devinfo *di)
752 uart_ungrab(struct uart_devinfo *di)