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>
51 #include <dev/uart/uart_ppstypes.h>
55 devclass_t uart_devclass;
56 const char uart_driver_name[] = "uart";
58 SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs =
59 SLIST_HEAD_INITIALIZER(uart_sysdevs);
61 static MALLOC_DEFINE(M_UART, "UART", "UART driver");
63 #ifndef UART_POLL_FREQ
64 #define UART_POLL_FREQ 50
66 static int uart_poll_freq = UART_POLL_FREQ;
67 SYSCTL_INT(_debug, OID_AUTO, uart_poll_freq, CTLFLAG_RDTUN, &uart_poll_freq,
68 0, "UART poll frequency");
70 static int uart_force_poll;
71 SYSCTL_INT(_debug, OID_AUTO, uart_force_poll, CTLFLAG_RDTUN, &uart_force_poll,
72 0, "Force UART polling");
75 uart_pps_mode_valid(int pps_mode)
79 switch(pps_mode & UART_PPS_SIGNAL_MASK) {
80 case UART_PPS_DISABLED:
88 opt = pps_mode & UART_PPS_OPTION_MASK;
89 if ((opt & ~(UART_PPS_INVERT_PULSE | UART_PPS_NARROW_PULSE)) != 0)
96 uart_pps_print_mode(struct uart_softc *sc)
99 device_printf(sc->sc_dev, "PPS capture mode: ");
100 switch(sc->sc_pps_mode & UART_PPS_SIGNAL_MASK) {
101 case UART_PPS_DISABLED:
114 if (sc->sc_pps_mode & UART_PPS_INVERT_PULSE)
116 if (sc->sc_pps_mode & UART_PPS_NARROW_PULSE)
117 printf("-NarrowPulse");
122 uart_pps_mode_sysctl(SYSCTL_HANDLER_ARGS)
124 struct uart_softc *sc;
128 tmp = sc->sc_pps_mode;
129 err = sysctl_handle_int(oidp, &tmp, 0, req);
130 if (err != 0 || req->newptr == NULL)
132 if (!uart_pps_mode_valid(tmp))
134 sc->sc_pps_mode = tmp;
139 uart_pps_process(struct uart_softc *sc, int ser_sig)
142 int is_assert, pps_sig;
144 /* Which signal is configured as PPS? Early out if none. */
145 switch(sc->sc_pps_mode & UART_PPS_SIGNAL_MASK) {
156 /* Early out if there is no change in the signal configured as PPS. */
157 if ((ser_sig & SER_DELTA(pps_sig)) == 0)
161 * In narrow-pulse mode we need to synthesize both capture and clear
162 * events from a single "delta occurred" indication from the uart
163 * hardware because the pulse width is too narrow to reliably detect
164 * both edges. However, when the pulse width is close to our interrupt
165 * processing latency we might intermittantly catch both edges. To
166 * guard against generating spurious events when that happens, we use a
167 * separate timer to ensure at least half a second elapses before we
168 * generate another event.
170 pps_capture(&sc->sc_pps);
171 if (sc->sc_pps_mode & UART_PPS_NARROW_PULSE) {
172 now = getsbinuptime();
173 if (now > sc->sc_pps_captime + 500 * SBT_1MS) {
174 sc->sc_pps_captime = now;
175 pps_event(&sc->sc_pps, PPS_CAPTUREASSERT);
176 pps_event(&sc->sc_pps, PPS_CAPTURECLEAR);
179 is_assert = ser_sig & pps_sig;
180 if (sc->sc_pps_mode & UART_PPS_INVERT_PULSE)
181 is_assert = !is_assert;
182 pps_event(&sc->sc_pps, is_assert ? PPS_CAPTUREASSERT :
188 uart_pps_init(struct uart_softc *sc)
190 struct sysctl_ctx_list *ctx;
191 struct sysctl_oid *tree;
193 ctx = device_get_sysctl_ctx(sc->sc_dev);
194 tree = device_get_sysctl_tree(sc->sc_dev);
197 * The historical default for pps capture mode is either DCD or CTS,
198 * depending on the UART_PPS_ON_CTS kernel option. Start with that,
199 * then try to fetch the tunable that overrides the mode for all uart
200 * devices, then try to fetch the sysctl-tunable that overrides the mode
201 * for one specific device.
203 #ifdef UART_PPS_ON_CTS
204 sc->sc_pps_mode = UART_PPS_CTS;
206 sc->sc_pps_mode = UART_PPS_DCD;
208 TUNABLE_INT_FETCH("hw.uart.pps_mode", &sc->sc_pps_mode);
209 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "pps_mode",
210 CTLTYPE_INT | CTLFLAG_RWTUN, sc, 0, uart_pps_mode_sysctl, "I",
211 "pulse mode: 0/1/2=disabled/CTS/DCD; "
212 "add 0x10 to invert, 0x20 for narrow pulse");
214 if (!uart_pps_mode_valid(sc->sc_pps_mode)) {
215 device_printf(sc->sc_dev,
216 "Invalid pps_mode 0x%02x configured; disabling PPS capture\n",
218 sc->sc_pps_mode = UART_PPS_DISABLED;
219 } else if (bootverbose) {
220 uart_pps_print_mode(sc);
223 sc->sc_pps.ppscap = PPS_CAPTUREBOTH;
224 sc->sc_pps.driver_mtx = uart_tty_getlock(sc);
225 sc->sc_pps.driver_abi = PPS_ABI_VERSION;
226 pps_init_abi(&sc->sc_pps);
230 uart_add_sysdev(struct uart_devinfo *di)
232 SLIST_INSERT_HEAD(&uart_sysdevs, di, next);
236 uart_getname(struct uart_class *uc)
238 return ((uc != NULL) ? uc->name : NULL);
242 uart_getops(struct uart_class *uc)
244 return ((uc != NULL) ? uc->uc_ops : NULL);
248 uart_getrange(struct uart_class *uc)
250 return ((uc != NULL) ? uc->uc_range : 0);
254 uart_getregshift(struct uart_class *uc)
256 return ((uc != NULL) ? uc->uc_rshift : 0);
260 uart_getregiowidth(struct uart_class *uc)
262 return ((uc != NULL) ? uc->uc_riowidth : 0);
266 * Schedule a soft interrupt. We do this on the 0 to !0 transition
267 * of the TTY pending interrupt status.
270 uart_sched_softih(struct uart_softc *sc, uint32_t ipend)
275 old = sc->sc_ttypend;
277 } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
279 if ((old & SER_INT_MASK) == 0)
280 swi_sched(sc->sc_softih, 0);
284 * A break condition has been detected. We treat the break condition as
285 * a special case that should not happen during normal operation. When
286 * the break condition is to be passed to higher levels in the form of
287 * a NUL character, we really want the break to be in the right place in
288 * the input stream. The overhead to achieve that is not in relation to
289 * the exceptional nature of the break condition, so we permit ourselves
293 uart_intr_break(void *arg)
295 struct uart_softc *sc = arg;
298 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
304 uart_sched_softih(sc, SER_INT_BREAK);
309 * Handle a receiver overrun situation. We lost at least 1 byte in the
310 * input stream and it's our job to contain the situation. We grab as
311 * much of the data we can, but otherwise flush the receiver FIFO to
312 * create some breathing room. The net effect is that we avoid the
313 * overrun condition to happen for the next X characters, where X is
314 * related to the FIFO size at the cost of losing data right away.
315 * So, instead of having multiple overrun interrupts in close proximity
316 * to each other and possibly pessimizing UART interrupt latency for
317 * other UARTs in a multiport configuration, we create a longer segment
318 * of missing characters by freeing up the FIFO.
319 * Each overrun condition is marked in the input buffer by a token. The
320 * token represents the loss of at least one, but possible more bytes in
324 uart_intr_overrun(void *arg)
326 struct uart_softc *sc = arg;
330 if (uart_rx_put(sc, UART_STAT_OVERRUN))
331 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
332 uart_sched_softih(sc, SER_INT_RXREADY);
334 UART_FLUSH(sc, UART_FLUSH_RECEIVER);
339 * Received data ready.
342 uart_intr_rxready(void *arg)
344 struct uart_softc *sc = arg;
350 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
351 while (rxp != sc->sc_rxput) {
352 kdb_alt_break(sc->sc_rxbuf[rxp++], &sc->sc_altbrk);
353 if (rxp == sc->sc_rxbufsz)
359 uart_sched_softih(sc, SER_INT_RXREADY);
361 sc->sc_rxput = sc->sc_rxget; /* Ignore received data. */
366 * Line or modem status change (OOB signalling).
367 * We pass the signals to the software interrupt handler for further
368 * processing. Note that we merge the delta bits, but set the state
369 * bits. This is to avoid losing state transitions due to having more
370 * than 1 hardware interrupt between software interrupts.
373 uart_intr_sigchg(void *arg)
375 struct uart_softc *sc = arg;
378 sig = UART_GETSIG(sc);
381 * Time pulse counting support, invoked whenever the PPS parameters are
382 * currently set to capture either edge of the signal.
384 if (sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) {
385 uart_pps_process(sc, sig);
389 * Keep track of signal changes, even when the device is not
390 * opened. This allows us to inform upper layers about a
391 * possible loss of DCD and thus the existence of a (possibly)
392 * different connection when we have DCD back, during the time
393 * that the device was closed.
396 old = sc->sc_ttypend;
397 new = old & ~SER_MASK_STATE;
398 new |= sig & SER_INT_SIGMASK;
399 } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
402 uart_sched_softih(sc, SER_INT_SIGCHG);
407 * The transmitter can accept more data.
410 uart_intr_txidle(void *arg)
412 struct uart_softc *sc = arg;
416 uart_sched_softih(sc, SER_INT_TXIDLE);
424 struct uart_softc *sc = arg;
425 int cnt, ipend, testintr;
428 return (FILTER_STRAY);
431 testintr = sc->sc_testintr;
432 while ((!testintr || cnt < 20) && (ipend = UART_IPEND(sc)) != 0) {
434 if (ipend & SER_INT_OVERRUN)
435 uart_intr_overrun(sc);
436 if (ipend & SER_INT_BREAK)
438 if (ipend & SER_INT_RXREADY)
439 uart_intr_rxready(sc);
440 if (ipend & SER_INT_SIGCHG)
441 uart_intr_sigchg(sc);
442 if (ipend & SER_INT_TXIDLE)
443 uart_intr_txidle(sc);
447 callout_reset(&sc->sc_timer, hz / uart_poll_freq,
448 (timeout_t *)uart_intr, sc);
451 return ((cnt == 0) ? FILTER_STRAY :
452 ((testintr && cnt == 20) ? FILTER_SCHEDULE_THREAD :
457 uart_bus_ihand(device_t dev, int ipend)
462 return (uart_intr_break);
463 case SER_INT_OVERRUN:
464 return (uart_intr_overrun);
465 case SER_INT_RXREADY:
466 return (uart_intr_rxready);
468 return (uart_intr_sigchg);
470 return (uart_intr_txidle);
476 uart_bus_ipend(device_t dev)
478 struct uart_softc *sc;
480 sc = device_get_softc(dev);
481 return (UART_IPEND(sc));
485 uart_bus_sysdev(device_t dev)
487 struct uart_softc *sc;
489 sc = device_get_softc(dev);
490 return ((sc->sc_sysdev != NULL) ? 1 : 0);
494 uart_bus_probe(device_t dev, int regshft, int regiowidth, int rclk, int rid, int chan)
496 struct uart_softc *sc;
497 struct uart_devinfo *sysdev;
500 sc = device_get_softc(dev);
503 * All uart_class references are weak. Check that the needed
504 * class has been compiled-in. Fail if not.
506 if (sc->sc_class == NULL)
510 * Initialize the instance. Note that the instance (=softc) does
511 * not necessarily match the hardware specific softc. We can't do
512 * anything about it now, because we may not attach to the device.
513 * Hardware drivers cannot use any of the class specific fields
516 kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class);
518 if (device_get_desc(dev) == NULL)
519 device_set_desc(dev, uart_getname(sc->sc_class));
522 * Allocate the register resource. We assume that all UARTs have
523 * a single register window in either I/O port space or memory
524 * mapped I/O space. Any UART that needs multiple windows will
525 * consequently not be supported by this driver as-is. We try I/O
526 * port space first because that's the common case.
529 sc->sc_rtype = SYS_RES_IOPORT;
530 sc->sc_rres = bus_alloc_resource_any(dev, sc->sc_rtype, &sc->sc_rrid,
532 if (sc->sc_rres == NULL) {
534 sc->sc_rtype = SYS_RES_MEMORY;
535 sc->sc_rres = bus_alloc_resource_any(dev, sc->sc_rtype,
536 &sc->sc_rrid, RF_ACTIVE);
537 if (sc->sc_rres == NULL)
542 * Fill in the bus access structure and compare this device with
543 * a possible console device and/or a debug port. We set the flags
544 * in the softc so that the hardware dependent probe can adjust
545 * accordingly. In general, you don't want to permanently disrupt
548 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
549 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
550 sc->sc_bas.chan = chan;
551 sc->sc_bas.regshft = regshft;
552 sc->sc_bas.regiowidth = regiowidth;
553 sc->sc_bas.rclk = (rclk == 0) ? sc->sc_class->uc_rclk : rclk;
555 SLIST_FOREACH(sysdev, &uart_sysdevs, next) {
556 if (chan == sysdev->bas.chan &&
557 uart_cpu_eqres(&sc->sc_bas, &sysdev->bas)) {
558 /* XXX check if ops matches class. */
559 sc->sc_sysdev = sysdev;
560 sysdev->bas.rclk = sc->sc_bas.rclk;
564 error = UART_PROBE(sc);
565 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
566 return ((error) ? error : BUS_PROBE_DEFAULT);
570 uart_bus_attach(device_t dev)
572 struct uart_softc *sc, *sc0;
577 * The sc_class field defines the type of UART we're going to work
578 * with and thus the size of the softc. Replace the generic softc
579 * with one that matches the UART now that we're certain we handle
582 sc0 = device_get_softc(dev);
583 if (sc0->sc_class->size > device_get_driver(dev)->size) {
584 sc = malloc(sc0->sc_class->size, M_UART, M_WAITOK|M_ZERO);
585 bcopy(sc0, sc, sizeof(*sc));
586 device_set_softc(dev, sc);
591 * Now that we know the softc for this device, connect the back
592 * pointer from the sysdev for this device, if any
594 if (sc->sc_sysdev != NULL)
595 sc->sc_sysdev->sc = sc;
598 * Protect ourselves against interrupts while we're not completely
599 * finished attaching and initializing. We don't expect interrupts
600 * until after UART_ATTACH(), though.
604 mtx_init(&sc->sc_hwmtx_s, "uart_hwmtx", NULL, MTX_SPIN);
605 if (sc->sc_hwmtx == NULL)
606 sc->sc_hwmtx = &sc->sc_hwmtx_s;
609 * Re-allocate. We expect that the softc contains the information
610 * collected by uart_bus_probe() intact.
612 sc->sc_rres = bus_alloc_resource_any(dev, sc->sc_rtype, &sc->sc_rrid,
614 if (sc->sc_rres == NULL) {
615 mtx_destroy(&sc->sc_hwmtx_s);
618 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
619 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
622 * Ensure there is room for at least three full FIFOs of data in the
623 * receive buffer (handles the case of low-level drivers with huge
624 * FIFOs), and also ensure that there is no less than the historical
625 * size of 384 bytes (handles the typical small-FIFO case).
627 sc->sc_rxbufsz = MAX(384, sc->sc_rxfifosz * 3);
628 sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf),
630 sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf),
633 error = UART_ATTACH(sc);
637 if (sc->sc_hwiflow || sc->sc_hwoflow) {
639 device_print_prettyname(dev);
640 if (sc->sc_hwiflow) {
641 printf("%sRTS iflow", sep);
644 if (sc->sc_hwoflow) {
645 printf("%sCTS oflow", sep);
651 if (sc->sc_sysdev != NULL) {
652 if (sc->sc_sysdev->baudrate == 0) {
653 if (UART_IOCTL(sc, UART_IOCTL_BAUD,
654 (intptr_t)&sc->sc_sysdev->baudrate) != 0)
655 sc->sc_sysdev->baudrate = -1;
657 switch (sc->sc_sysdev->type) {
658 case UART_DEV_CONSOLE:
659 device_printf(dev, "console");
661 case UART_DEV_DBGPORT:
662 device_printf(dev, "debug port");
664 case UART_DEV_KEYBOARD:
665 device_printf(dev, "keyboard");
668 device_printf(dev, "unknown system device");
671 printf(" (%d,%c,%d,%d)\n", sc->sc_sysdev->baudrate,
672 "noems"[sc->sc_sysdev->parity], sc->sc_sysdev->databits,
673 sc->sc_sysdev->stopbits);
678 filt = uart_intr(sc);
682 * Don't use interrupts if we couldn't clear any pending interrupt
683 * conditions. We may have broken H/W and polling is probably the
684 * safest thing to do.
686 if (filt != FILTER_SCHEDULE_THREAD && !uart_force_poll) {
687 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
688 &sc->sc_irid, RF_ACTIVE | RF_SHAREABLE);
690 if (sc->sc_ires != NULL) {
691 error = bus_setup_intr(dev, sc->sc_ires, INTR_TYPE_TTY,
692 uart_intr, NULL, sc, &sc->sc_icookie);
693 sc->sc_fastintr = (error == 0) ? 1 : 0;
695 if (!sc->sc_fastintr)
696 error = bus_setup_intr(dev, sc->sc_ires,
697 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
698 (driver_intr_t *)uart_intr, sc, &sc->sc_icookie);
701 device_printf(dev, "could not activate interrupt\n");
702 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
707 if (sc->sc_ires == NULL) {
708 /* No interrupt resource. Force polled mode. */
710 callout_init(&sc->sc_timer, 1);
711 callout_reset(&sc->sc_timer, hz / uart_poll_freq,
712 (timeout_t *)uart_intr, sc);
715 if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
717 device_print_prettyname(dev);
718 if (sc->sc_fastintr) {
719 printf("%sfast interrupt", sep);
723 printf("%spolled mode (%dHz)", sep, uart_poll_freq);
729 if (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL) {
730 if ((error = sc->sc_sysdev->attach(sc)) != 0)
733 if ((error = uart_tty_attach(sc)) != 0)
738 if (sc->sc_sysdev != NULL)
739 sc->sc_sysdev->hwmtx = sc->sc_hwmtx;
744 free(sc->sc_txbuf, M_UART);
745 free(sc->sc_rxbuf, M_UART);
747 if (sc->sc_ires != NULL) {
748 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
749 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
752 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
754 mtx_destroy(&sc->sc_hwmtx_s);
760 uart_bus_detach(device_t dev)
762 struct uart_softc *sc;
764 sc = device_get_softc(dev);
768 if (sc->sc_sysdev != NULL)
769 sc->sc_sysdev->hwmtx = NULL;
773 if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL)
774 (*sc->sc_sysdev->detach)(sc);
778 free(sc->sc_txbuf, M_UART);
779 free(sc->sc_rxbuf, M_UART);
781 if (sc->sc_ires != NULL) {
782 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
783 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
786 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
788 mtx_destroy(&sc->sc_hwmtx_s);
790 if (sc->sc_class->size > device_get_driver(dev)->size) {
791 device_set_softc(dev, NULL);
799 uart_bus_resume(device_t dev)
801 struct uart_softc *sc;
803 sc = device_get_softc(dev);
804 return (UART_ATTACH(sc));
808 uart_grab(struct uart_devinfo *di)
816 uart_ungrab(struct uart_devinfo *di)