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 <machine/bus.h>
44 #include <machine/resource.h>
45 #include <machine/stdarg.h>
47 #include <dev/uart/uart.h>
48 #include <dev/uart/uart_bus.h>
49 #include <dev/uart/uart_cpu.h>
53 devclass_t uart_devclass;
54 const char uart_driver_name[] = "uart";
56 SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs =
57 SLIST_HEAD_INITIALIZER(uart_sysdevs);
59 static MALLOC_DEFINE(M_UART, "UART", "UART driver");
61 #ifndef UART_POLL_FREQ
62 #define UART_POLL_FREQ 50
64 static int uart_poll_freq = UART_POLL_FREQ;
65 TUNABLE_INT("debug.uart_poll_freq", &uart_poll_freq);
68 uart_add_sysdev(struct uart_devinfo *di)
70 SLIST_INSERT_HEAD(&uart_sysdevs, di, next);
74 uart_getname(struct uart_class *uc)
76 return ((uc != NULL) ? uc->name : NULL);
80 uart_getops(struct uart_class *uc)
82 return ((uc != NULL) ? uc->uc_ops : NULL);
86 uart_getrange(struct uart_class *uc)
88 return ((uc != NULL) ? uc->uc_range : 0);
92 * Schedule a soft interrupt. We do this on the 0 to !0 transition
93 * of the TTY pending interrupt status.
96 uart_sched_softih(struct uart_softc *sc, uint32_t ipend)
101 old = sc->sc_ttypend;
103 } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
105 if ((old & SER_INT_MASK) == 0)
106 swi_sched(sc->sc_softih, 0);
110 * A break condition has been detected. We treat the break condition as
111 * a special case that should not happen during normal operation. When
112 * the break condition is to be passed to higher levels in the form of
113 * a NUL character, we really want the break to be in the right place in
114 * the input stream. The overhead to achieve that is not in relation to
115 * the exceptional nature of the break condition, so we permit ourselves
119 uart_intr_break(void *arg)
121 struct uart_softc *sc = arg;
124 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
130 uart_sched_softih(sc, SER_INT_BREAK);
135 * Handle a receiver overrun situation. We lost at least 1 byte in the
136 * input stream and it's our job to contain the situation. We grab as
137 * much of the data we can, but otherwise flush the receiver FIFO to
138 * create some breathing room. The net effect is that we avoid the
139 * overrun condition to happen for the next X characters, where X is
140 * related to the FIFO size at the cost of losing data right away.
141 * So, instead of having multiple overrun interrupts in close proximity
142 * to each other and possibly pessimizing UART interrupt latency for
143 * other UARTs in a multiport configuration, we create a longer segment
144 * of missing characters by freeing up the FIFO.
145 * Each overrun condition is marked in the input buffer by a token. The
146 * token represents the loss of at least one, but possible more bytes in
150 uart_intr_overrun(void *arg)
152 struct uart_softc *sc = arg;
156 if (uart_rx_put(sc, UART_STAT_OVERRUN))
157 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
158 uart_sched_softih(sc, SER_INT_RXREADY);
160 UART_FLUSH(sc, UART_FLUSH_RECEIVER);
165 * Received data ready.
168 uart_intr_rxready(void *arg)
170 struct uart_softc *sc = arg;
176 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
177 while (rxp != sc->sc_rxput) {
178 kdb_alt_break(sc->sc_rxbuf[rxp++], &sc->sc_altbrk);
179 if (rxp == sc->sc_rxbufsz)
185 uart_sched_softih(sc, SER_INT_RXREADY);
187 sc->sc_rxput = sc->sc_rxget; /* Ignore received data. */
192 * Line or modem status change (OOB signalling).
193 * We pass the signals to the software interrupt handler for further
194 * processing. Note that we merge the delta bits, but set the state
195 * bits. This is to avoid losing state transitions due to having more
196 * than 1 hardware interrupt between software interrupts.
199 uart_intr_sigchg(void *arg)
201 struct uart_softc *sc = arg;
204 sig = UART_GETSIG(sc);
206 if (sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) {
207 if (sig & UART_SIG_DPPS) {
208 pps_capture(&sc->sc_pps);
209 pps_event(&sc->sc_pps, (sig & UART_SIG_PPS) ?
210 PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
215 * Keep track of signal changes, even when the device is not
216 * opened. This allows us to inform upper layers about a
217 * possible loss of DCD and thus the existence of a (possibly)
218 * different connection when we have DCD back, during the time
219 * that the device was closed.
222 old = sc->sc_ttypend;
223 new = old & ~SER_MASK_STATE;
224 new |= sig & SER_INT_SIGMASK;
225 } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
228 uart_sched_softih(sc, SER_INT_SIGCHG);
233 * The transmitter can accept more data.
236 uart_intr_txidle(void *arg)
238 struct uart_softc *sc = arg;
242 uart_sched_softih(sc, SER_INT_TXIDLE);
250 struct uart_softc *sc = arg;
251 int cnt, ipend, testintr;
254 return (FILTER_STRAY);
257 testintr = sc->sc_testintr;
258 while ((!testintr || cnt < 20) && (ipend = UART_IPEND(sc)) != 0) {
260 if (ipend & SER_INT_OVERRUN)
261 uart_intr_overrun(sc);
262 if (ipend & SER_INT_BREAK)
264 if (ipend & SER_INT_RXREADY)
265 uart_intr_rxready(sc);
266 if (ipend & SER_INT_SIGCHG)
267 uart_intr_sigchg(sc);
268 if (ipend & SER_INT_TXIDLE)
269 uart_intr_txidle(sc);
273 callout_reset(&sc->sc_timer, hz / uart_poll_freq,
274 (timeout_t *)uart_intr, sc);
277 return ((cnt == 0) ? FILTER_STRAY :
278 ((testintr && cnt == 20) ? FILTER_SCHEDULE_THREAD :
283 uart_bus_ihand(device_t dev, int ipend)
288 return (uart_intr_break);
289 case SER_INT_OVERRUN:
290 return (uart_intr_overrun);
291 case SER_INT_RXREADY:
292 return (uart_intr_rxready);
294 return (uart_intr_sigchg);
296 return (uart_intr_txidle);
302 uart_bus_ipend(device_t dev)
304 struct uart_softc *sc;
306 sc = device_get_softc(dev);
307 return (UART_IPEND(sc));
311 uart_bus_sysdev(device_t dev)
313 struct uart_softc *sc;
315 sc = device_get_softc(dev);
316 return ((sc->sc_sysdev != NULL) ? 1 : 0);
320 uart_bus_probe(device_t dev, int regshft, int rclk, int rid, int chan)
322 struct uart_softc *sc;
323 struct uart_devinfo *sysdev;
326 sc = device_get_softc(dev);
329 * All uart_class references are weak. Check that the needed
330 * class has been compiled-in. Fail if not.
332 if (sc->sc_class == NULL)
336 * Initialize the instance. Note that the instance (=softc) does
337 * not necessarily match the hardware specific softc. We can't do
338 * anything about it now, because we may not attach to the device.
339 * Hardware drivers cannot use any of the class specific fields
342 kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class);
344 if (device_get_desc(dev) == NULL)
345 device_set_desc(dev, uart_getname(sc->sc_class));
348 * Allocate the register resource. We assume that all UARTs have
349 * a single register window in either I/O port space or memory
350 * mapped I/O space. Any UART that needs multiple windows will
351 * consequently not be supported by this driver as-is. We try I/O
352 * port space first because that's the common case.
355 sc->sc_rtype = SYS_RES_IOPORT;
356 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid,
357 0, ~0, uart_getrange(sc->sc_class), RF_ACTIVE);
358 if (sc->sc_rres == NULL) {
360 sc->sc_rtype = SYS_RES_MEMORY;
361 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype,
362 &sc->sc_rrid, 0, ~0, uart_getrange(sc->sc_class),
364 if (sc->sc_rres == NULL)
369 * Fill in the bus access structure and compare this device with
370 * a possible console device and/or a debug port. We set the flags
371 * in the softc so that the hardware dependent probe can adjust
372 * accordingly. In general, you don't want to permanently disrupt
375 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
376 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
377 sc->sc_bas.chan = chan;
378 sc->sc_bas.regshft = regshft;
379 sc->sc_bas.rclk = (rclk == 0) ? sc->sc_class->uc_rclk : rclk;
381 SLIST_FOREACH(sysdev, &uart_sysdevs, next) {
382 if (chan == sysdev->bas.chan &&
383 uart_cpu_eqres(&sc->sc_bas, &sysdev->bas)) {
384 /* XXX check if ops matches class. */
385 sc->sc_sysdev = sysdev;
386 sysdev->bas.rclk = sc->sc_bas.rclk;
390 error = UART_PROBE(sc);
391 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
392 return ((error) ? error : BUS_PROBE_DEFAULT);
396 uart_bus_attach(device_t dev)
398 struct uart_softc *sc, *sc0;
403 * The sc_class field defines the type of UART we're going to work
404 * with and thus the size of the softc. Replace the generic softc
405 * with one that matches the UART now that we're certain we handle
408 sc0 = device_get_softc(dev);
409 if (sc0->sc_class->size > sizeof(*sc)) {
410 sc = malloc(sc0->sc_class->size, M_UART, M_WAITOK|M_ZERO);
411 bcopy(sc0, sc, sizeof(*sc));
412 device_set_softc(dev, sc);
417 * Now that we know the softc for this device, connect the back
418 * pointer from the sysdev for this device, if any
420 if (sc->sc_sysdev != NULL)
421 sc->sc_sysdev->sc = sc;
424 * Protect ourselves against interrupts while we're not completely
425 * finished attaching and initializing. We don't expect interrupts
426 * until after UART_ATTACH(), though.
430 mtx_init(&sc->sc_hwmtx_s, "uart_hwmtx", NULL, MTX_SPIN);
431 if (sc->sc_hwmtx == NULL)
432 sc->sc_hwmtx = &sc->sc_hwmtx_s;
435 * Re-allocate. We expect that the softc contains the information
436 * collected by uart_bus_probe() intact.
438 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid,
439 0, ~0, uart_getrange(sc->sc_class), RF_ACTIVE);
440 if (sc->sc_rres == NULL) {
441 mtx_destroy(&sc->sc_hwmtx_s);
444 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
445 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
448 * Ensure there is room for at least three full FIFOs of data in the
449 * receive buffer (handles the case of low-level drivers with huge
450 * FIFOs), and also ensure that there is no less than the historical
451 * size of 384 bytes (handles the typical small-FIFO case).
453 sc->sc_rxbufsz = MAX(384, sc->sc_rxfifosz * 3);
454 sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf),
456 sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf),
459 error = UART_ATTACH(sc);
463 if (sc->sc_hwiflow || sc->sc_hwoflow) {
465 device_print_prettyname(dev);
466 if (sc->sc_hwiflow) {
467 printf("%sRTS iflow", sep);
470 if (sc->sc_hwoflow) {
471 printf("%sCTS oflow", sep);
477 if (sc->sc_sysdev != NULL) {
478 if (sc->sc_sysdev->baudrate == 0) {
479 if (UART_IOCTL(sc, UART_IOCTL_BAUD,
480 (intptr_t)&sc->sc_sysdev->baudrate) != 0)
481 sc->sc_sysdev->baudrate = -1;
483 switch (sc->sc_sysdev->type) {
484 case UART_DEV_CONSOLE:
485 device_printf(dev, "console");
487 case UART_DEV_DBGPORT:
488 device_printf(dev, "debug port");
490 case UART_DEV_KEYBOARD:
491 device_printf(dev, "keyboard");
494 device_printf(dev, "unknown system device");
497 printf(" (%d,%c,%d,%d)\n", sc->sc_sysdev->baudrate,
498 "noems"[sc->sc_sysdev->parity], sc->sc_sysdev->databits,
499 sc->sc_sysdev->stopbits);
502 sc->sc_pps.ppscap = PPS_CAPTUREBOTH;
503 pps_init(&sc->sc_pps);
507 filt = uart_intr(sc);
511 * Don't use interrupts if we couldn't clear any pending interrupt
512 * conditions. We may have broken H/W and polling is probably the
513 * safest thing to do.
515 if (filt != FILTER_SCHEDULE_THREAD) {
517 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
518 &sc->sc_irid, RF_ACTIVE | RF_SHAREABLE);
520 if (sc->sc_ires != NULL) {
521 error = bus_setup_intr(dev, sc->sc_ires, INTR_TYPE_TTY,
522 uart_intr, NULL, sc, &sc->sc_icookie);
523 sc->sc_fastintr = (error == 0) ? 1 : 0;
525 if (!sc->sc_fastintr)
526 error = bus_setup_intr(dev, sc->sc_ires,
527 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
528 (driver_intr_t *)uart_intr, sc, &sc->sc_icookie);
531 device_printf(dev, "could not activate interrupt\n");
532 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
537 if (sc->sc_ires == NULL) {
538 /* No interrupt resource. Force polled mode. */
540 callout_init(&sc->sc_timer, 1);
543 if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
545 device_print_prettyname(dev);
546 if (sc->sc_fastintr) {
547 printf("%sfast interrupt", sep);
551 printf("%spolled mode (%dHz)", sep, uart_poll_freq);
557 error = (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL)
558 ? (*sc->sc_sysdev->attach)(sc) : uart_tty_attach(sc);
562 if (sc->sc_sysdev != NULL)
563 sc->sc_sysdev->hwmtx = sc->sc_hwmtx;
568 free(sc->sc_txbuf, M_UART);
569 free(sc->sc_rxbuf, M_UART);
571 if (sc->sc_ires != NULL) {
572 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
573 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
576 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
578 mtx_destroy(&sc->sc_hwmtx_s);
584 uart_bus_detach(device_t dev)
586 struct uart_softc *sc;
588 sc = device_get_softc(dev);
592 if (sc->sc_sysdev != NULL)
593 sc->sc_sysdev->hwmtx = NULL;
597 if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL)
598 (*sc->sc_sysdev->detach)(sc);
602 free(sc->sc_txbuf, M_UART);
603 free(sc->sc_rxbuf, M_UART);
605 if (sc->sc_ires != NULL) {
606 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
607 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
610 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
612 mtx_destroy(&sc->sc_hwmtx_s);
614 if (sc->sc_class->size > sizeof(*sc)) {
615 device_set_softc(dev, NULL);
618 device_set_softc(dev, NULL);
624 uart_bus_resume(device_t dev)
626 struct uart_softc *sc;
628 sc = device_get_softc(dev);
629 return (UART_ATTACH(sc));
633 uart_grab(struct uart_devinfo *di)
641 uart_ungrab(struct uart_devinfo *di)