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 #ifndef KLD_MODULE 31 #include "opt_comconsole.h" 32 #endif 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/bus.h> 37 #include <sys/conf.h> 38 #include <sys/cons.h> 39 #include <sys/fcntl.h> 40 #include <sys/interrupt.h> 41 #include <sys/kdb.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/queue.h> 45 #include <sys/reboot.h> 46 #include <machine/bus.h> 47 #include <sys/rman.h> 48 #include <sys/termios.h> 49 #include <sys/tty.h> 50 #include <machine/resource.h> 51 #include <machine/stdarg.h> 52 53 #include <dev/uart/uart.h> 54 #include <dev/uart/uart_bus.h> 55 #include <dev/uart/uart_cpu.h> 56 57 #include "uart_if.h" 58 59 devclass_t uart_devclass; 60 char uart_driver_name[] = "uart"; 61 62 SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs = 63 SLIST_HEAD_INITIALIZER(uart_sysdevs); 64 65 MALLOC_DEFINE(M_UART, "UART", "UART driver"); 66 67 void 68 uart_add_sysdev(struct uart_devinfo *di) 69 { 70 SLIST_INSERT_HEAD(&uart_sysdevs, di, next); 71 } 72 73 /* 74 * Schedule a soft interrupt. We do this on the 0 to !0 transition 75 * of the TTY pending interrupt status. 76 */ 77 static void 78 uart_sched_softih(struct uart_softc *sc, uint32_t ipend) 79 { 80 uint32_t new, old; 81 82 do { 83 old = sc->sc_ttypend; 84 new = old | ipend; 85 } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new)); 86 87 if ((old & SER_INT_MASK) == 0) 88 swi_sched(sc->sc_softih, 0); 89 } 90 91 /* 92 * A break condition has been detected. We treat the break condition as 93 * a special case that should not happen during normal operation. When 94 * the break condition is to be passed to higher levels in the form of 95 * a NUL character, we really want the break to be in the right place in 96 * the input stream. The overhead to achieve that is not in relation to 97 * the exceptional nature of the break condition, so we permit ourselves 98 * to be sloppy. 99 */ 100 static __inline int 101 uart_intr_break(void *arg) 102 { 103 struct uart_softc *sc = arg; 104 105 #if defined(KDB) && defined(BREAK_TO_DEBUGGER) 106 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 107 kdb_enter("Line break on console"); 108 return (0); 109 } 110 #endif 111 if (sc->sc_opened) 112 uart_sched_softih(sc, SER_INT_BREAK); 113 return (0); 114 } 115 116 /* 117 * Handle a receiver overrun situation. We lost at least 1 byte in the 118 * input stream and it's our job to contain the situation. We grab as 119 * much of the data we can, but otherwise flush the receiver FIFO to 120 * create some breathing room. The net effect is that we avoid the 121 * overrun condition to happen for the next X characters, where X is 122 * related to the FIFO size at the cost of loosing data right away. 123 * So, instead of having multiple overrun interrupts in close proximity 124 * to each other and possibly pessimizing UART interrupt latency for 125 * other UARTs in a multiport configuration, we create a longer segment 126 * of missing characters by freeing up the FIFO. 127 * Each overrun condition is marked in the input buffer by a token. The 128 * token represents the loss of at least one, but possible more bytes in 129 * the input stream. 130 */ 131 static __inline int 132 uart_intr_overrun(void *arg) 133 { 134 struct uart_softc *sc = arg; 135 136 if (sc->sc_opened) { 137 UART_RECEIVE(sc); 138 if (uart_rx_put(sc, UART_STAT_OVERRUN)) 139 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN; 140 uart_sched_softih(sc, SER_INT_RXREADY); 141 } 142 UART_FLUSH(sc, UART_FLUSH_RECEIVER); 143 return (0); 144 } 145 146 /* 147 * Received data ready. 148 */ 149 static __inline int 150 uart_intr_rxready(void *arg) 151 { 152 struct uart_softc *sc = arg; 153 int rxp; 154 155 rxp = sc->sc_rxput; 156 UART_RECEIVE(sc); 157 #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER) 158 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 159 while (rxp != sc->sc_rxput) { 160 if (kdb_alt_break(sc->sc_rxbuf[rxp++], &sc->sc_altbrk)) 161 kdb_enter("Break sequence on console"); 162 if (rxp == sc->sc_rxbufsz) 163 rxp = 0; 164 } 165 } 166 #endif 167 if (sc->sc_opened) 168 uart_sched_softih(sc, SER_INT_RXREADY); 169 else 170 sc->sc_rxput = sc->sc_rxget; /* Ignore received data. */ 171 return (1); 172 } 173 174 /* 175 * Line or modem status change (OOB signalling). 176 * We pass the signals to the software interrupt handler for further 177 * processing. Note that we merge the delta bits, but set the state 178 * bits. This is to avoid loosing state transitions due to having more 179 * than 1 hardware interrupt between software interrupts. 180 */ 181 static __inline int 182 uart_intr_sigchg(void *arg) 183 { 184 struct uart_softc *sc = arg; 185 int new, old, sig; 186 187 sig = UART_GETSIG(sc); 188 189 if (sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) { 190 if (sig & UART_SIG_DPPS) { 191 pps_capture(&sc->sc_pps); 192 pps_event(&sc->sc_pps, (sig & UART_SIG_PPS) ? 193 PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 194 } 195 } 196 197 /* 198 * Keep track of signal changes, even when the device is not 199 * opened. This allows us to inform upper layers about a 200 * possible loss of DCD and thus the existence of a (possibly) 201 * different connection when we have DCD back, during the time 202 * that the device was closed. 203 */ 204 do { 205 old = sc->sc_ttypend; 206 new = old & ~SER_MASK_STATE; 207 new |= sig & SER_INT_SIGMASK; 208 } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new)); 209 210 if (sc->sc_opened) 211 uart_sched_softih(sc, SER_INT_SIGCHG); 212 return (1); 213 } 214 215 /* 216 * The transmitter can accept more data. 217 */ 218 static __inline int 219 uart_intr_txidle(void *arg) 220 { 221 struct uart_softc *sc = arg; 222 223 if (sc->sc_txbusy) { 224 sc->sc_txbusy = 0; 225 uart_sched_softih(sc, SER_INT_TXIDLE); 226 } 227 return (0); 228 } 229 230 static void 231 uart_intr(void *arg) 232 { 233 struct uart_softc *sc = arg; 234 int ipend; 235 236 while (!sc->sc_leaving && (ipend = UART_IPEND(sc)) != 0) { 237 if (ipend & SER_INT_OVERRUN) 238 uart_intr_overrun(sc); 239 if (ipend & SER_INT_BREAK) 240 uart_intr_break(sc); 241 if (ipend & SER_INT_RXREADY) 242 uart_intr_rxready(sc); 243 if (ipend & SER_INT_SIGCHG) 244 uart_intr_sigchg(sc); 245 if (ipend & SER_INT_TXIDLE) 246 uart_intr_txidle(sc); 247 } 248 } 249 250 serdev_intr_t * 251 uart_bus_ihand(device_t dev, int ipend) 252 { 253 254 switch (ipend) { 255 case SER_INT_BREAK: 256 return (uart_intr_break); 257 case SER_INT_OVERRUN: 258 return (uart_intr_overrun); 259 case SER_INT_RXREADY: 260 return (uart_intr_rxready); 261 case SER_INT_SIGCHG: 262 return (uart_intr_sigchg); 263 case SER_INT_TXIDLE: 264 return (uart_intr_txidle); 265 } 266 return (NULL); 267 } 268 269 int 270 uart_bus_ipend(device_t dev) 271 { 272 struct uart_softc *sc; 273 274 sc = device_get_softc(dev); 275 return (UART_IPEND(sc)); 276 } 277 278 int 279 uart_bus_sysdev(device_t dev) 280 { 281 struct uart_softc *sc; 282 283 sc = device_get_softc(dev); 284 return ((sc->sc_sysdev != NULL) ? 1 : 0); 285 } 286 287 int 288 uart_bus_probe(device_t dev, int regshft, int rclk, int rid, int chan) 289 { 290 struct uart_softc *sc; 291 struct uart_devinfo *sysdev; 292 int error; 293 294 /* 295 * Initialize the instance. Note that the instance (=softc) does 296 * not necessarily match the hardware specific softc. We can't do 297 * anything about it now, because we may not attach to the device. 298 * Hardware drivers cannot use any of the class specific fields 299 * while probing. 300 */ 301 sc = device_get_softc(dev); 302 kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class); 303 sc->sc_dev = dev; 304 if (device_get_desc(dev) == NULL) 305 device_set_desc(dev, sc->sc_class->name); 306 307 /* 308 * Allocate the register resource. We assume that all UARTs have 309 * a single register window in either I/O port space or memory 310 * mapped I/O space. Any UART that needs multiple windows will 311 * consequently not be supported by this driver as-is. We try I/O 312 * port space first because that's the common case. 313 */ 314 sc->sc_rrid = rid; 315 sc->sc_rtype = SYS_RES_IOPORT; 316 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid, 317 0, ~0, sc->sc_class->uc_range, RF_ACTIVE); 318 if (sc->sc_rres == NULL) { 319 sc->sc_rrid = rid; 320 sc->sc_rtype = SYS_RES_MEMORY; 321 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, 322 &sc->sc_rrid, 0, ~0, sc->sc_class->uc_range, RF_ACTIVE); 323 if (sc->sc_rres == NULL) 324 return (ENXIO); 325 } 326 327 /* 328 * Fill in the bus access structure and compare this device with 329 * a possible console device and/or a debug port. We set the flags 330 * in the softc so that the hardware dependent probe can adjust 331 * accordingly. In general, you don't want to permanently disrupt 332 * console I/O. 333 */ 334 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 335 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 336 sc->sc_bas.chan = chan; 337 sc->sc_bas.regshft = regshft; 338 sc->sc_bas.rclk = (rclk == 0) ? sc->sc_class->uc_rclk : rclk; 339 340 SLIST_FOREACH(sysdev, &uart_sysdevs, next) { 341 if (chan == sysdev->bas.chan && 342 uart_cpu_eqres(&sc->sc_bas, &sysdev->bas)) { 343 /* XXX check if ops matches class. */ 344 sc->sc_sysdev = sysdev; 345 break; 346 } 347 } 348 349 error = UART_PROBE(sc); 350 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 351 return ((error) ? error : BUS_PROBE_DEFAULT); 352 } 353 354 int 355 uart_bus_attach(device_t dev) 356 { 357 struct uart_softc *sc, *sc0; 358 const char *sep; 359 int error; 360 361 /* 362 * The sc_class field defines the type of UART we're going to work 363 * with and thus the size of the softc. Replace the generic softc 364 * with one that matches the UART now that we're certain we handle 365 * the device. 366 */ 367 sc0 = device_get_softc(dev); 368 if (sc0->sc_class->size > sizeof(*sc)) { 369 sc = malloc(sc0->sc_class->size, M_UART, M_WAITOK|M_ZERO); 370 bcopy(sc0, sc, sizeof(*sc)); 371 device_set_softc(dev, sc); 372 } else 373 sc = sc0; 374 375 /* 376 * Protect ourselves against interrupts while we're not completely 377 * finished attaching and initializing. We don't expect interrupts 378 * until after UART_ATTACH() though. 379 */ 380 sc->sc_leaving = 1; 381 382 mtx_init(&sc->sc_hwmtx_s, "uart_hwmtx", NULL, MTX_SPIN); 383 if (sc->sc_hwmtx == NULL) 384 sc->sc_hwmtx = &sc->sc_hwmtx_s; 385 386 /* 387 * Re-allocate. We expect that the softc contains the information 388 * collected by uart_bus_probe() intact. 389 */ 390 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid, 391 0, ~0, sc->sc_class->uc_range, RF_ACTIVE); 392 if (sc->sc_rres == NULL) { 393 mtx_destroy(&sc->sc_hwmtx_s); 394 return (ENXIO); 395 } 396 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 397 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 398 399 sc->sc_irid = 0; 400 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid, 401 RF_ACTIVE | RF_SHAREABLE); 402 if (sc->sc_ires != NULL) { 403 error = bus_setup_intr(dev, 404 sc->sc_ires, INTR_TYPE_TTY | INTR_FAST, uart_intr, 405 sc, &sc->sc_icookie); 406 if (error) 407 error = bus_setup_intr(dev, 408 sc->sc_ires, INTR_TYPE_TTY | INTR_MPSAFE, 409 uart_intr, sc, &sc->sc_icookie); 410 else 411 sc->sc_fastintr = 1; 412 413 if (error) { 414 device_printf(dev, "could not activate interrupt\n"); 415 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 416 sc->sc_ires); 417 sc->sc_ires = NULL; 418 } 419 } 420 if (sc->sc_ires == NULL) { 421 /* XXX no interrupt resource. Force polled mode. */ 422 sc->sc_polled = 1; 423 } 424 425 sc->sc_rxbufsz = IBUFSIZ; 426 sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf), 427 M_UART, M_WAITOK); 428 sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf), 429 M_UART, M_WAITOK); 430 431 error = UART_ATTACH(sc); 432 if (error) 433 goto fail; 434 435 if (sc->sc_hwiflow || sc->sc_hwoflow) { 436 sep = ""; 437 device_print_prettyname(dev); 438 if (sc->sc_hwiflow) { 439 printf("%sRTS iflow", sep); 440 sep = ", "; 441 } 442 if (sc->sc_hwoflow) { 443 printf("%sCTS oflow", sep); 444 sep = ", "; 445 } 446 printf("\n"); 447 } 448 449 if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) { 450 sep = ""; 451 device_print_prettyname(dev); 452 if (sc->sc_fastintr) { 453 printf("%sfast interrupt", sep); 454 sep = ", "; 455 } 456 if (sc->sc_polled) { 457 printf("%spolled mode", sep); 458 sep = ", "; 459 } 460 printf("\n"); 461 } 462 463 if (sc->sc_sysdev != NULL) { 464 if (sc->sc_sysdev->baudrate == 0) { 465 if (UART_IOCTL(sc, UART_IOCTL_BAUD, 466 (intptr_t)&sc->sc_sysdev->baudrate) != 0) 467 sc->sc_sysdev->baudrate = -1; 468 } 469 switch (sc->sc_sysdev->type) { 470 case UART_DEV_CONSOLE: 471 device_printf(dev, "console"); 472 break; 473 case UART_DEV_DBGPORT: 474 device_printf(dev, "debug port"); 475 break; 476 case UART_DEV_KEYBOARD: 477 device_printf(dev, "keyboard"); 478 break; 479 default: 480 device_printf(dev, "unknown system device"); 481 break; 482 } 483 printf(" (%d,%c,%d,%d)\n", sc->sc_sysdev->baudrate, 484 "noems"[sc->sc_sysdev->parity], sc->sc_sysdev->databits, 485 sc->sc_sysdev->stopbits); 486 } 487 488 sc->sc_pps.ppscap = PPS_CAPTUREBOTH; 489 pps_init(&sc->sc_pps); 490 491 error = (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL) 492 ? (*sc->sc_sysdev->attach)(sc) : uart_tty_attach(sc); 493 if (error) 494 goto fail; 495 496 if (sc->sc_sysdev != NULL) 497 sc->sc_sysdev->hwmtx = sc->sc_hwmtx; 498 499 sc->sc_leaving = 0; 500 uart_intr(sc); 501 return (0); 502 503 fail: 504 free(sc->sc_txbuf, M_UART); 505 free(sc->sc_rxbuf, M_UART); 506 507 if (sc->sc_ires != NULL) { 508 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 509 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 510 sc->sc_ires); 511 } 512 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 513 514 mtx_destroy(&sc->sc_hwmtx_s); 515 516 return (error); 517 } 518 519 int 520 uart_bus_detach(device_t dev) 521 { 522 struct uart_softc *sc; 523 524 sc = device_get_softc(dev); 525 526 sc->sc_leaving = 1; 527 528 if (sc->sc_sysdev != NULL) 529 sc->sc_sysdev->hwmtx = NULL; 530 531 UART_DETACH(sc); 532 533 if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL) 534 (*sc->sc_sysdev->detach)(sc); 535 else 536 uart_tty_detach(sc); 537 538 free(sc->sc_txbuf, M_UART); 539 free(sc->sc_rxbuf, M_UART); 540 541 if (sc->sc_ires != NULL) { 542 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 543 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 544 sc->sc_ires); 545 } 546 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 547 548 mtx_destroy(&sc->sc_hwmtx_s); 549 550 if (sc->sc_class->size > sizeof(*sc)) { 551 device_set_softc(dev, NULL); 552 free(sc, M_UART); 553 } else 554 device_set_softc(dev, NULL); 555 556 return (0); 557 } 558