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 #include "opt_ddb.h" 33 #endif 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/bus.h> 38 #include <sys/conf.h> 39 #include <sys/cons.h> 40 #include <sys/fcntl.h> 41 #include <sys/interrupt.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 <ddb/ddb.h> 54 55 #include <dev/uart/uart.h> 56 #include <dev/uart/uart_bus.h> 57 #include <dev/uart/uart_cpu.h> 58 59 #include "uart_if.h" 60 61 devclass_t uart_devclass; 62 char uart_driver_name[] = "uart"; 63 64 SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs = 65 SLIST_HEAD_INITIALIZER(uart_sysdevs); 66 67 MALLOC_DEFINE(M_UART, "UART", "UART driver"); 68 69 void 70 uart_add_sysdev(struct uart_devinfo *di) 71 { 72 SLIST_INSERT_HEAD(&uart_sysdevs, di, next); 73 } 74 75 /* 76 * A break condition has been detected. We treat the break condition as 77 * a special case that should not happen during normal operation. When 78 * the break condition is to be passed to higher levels in the form of 79 * a NUL character, we really want the break to be in the right place in 80 * the input stream. The overhead to achieve that is not in relation to 81 * the exceptional nature of the break condition, so we permit ourselves 82 * to be sloppy. 83 */ 84 static void 85 uart_intr_break(struct uart_softc *sc) 86 { 87 88 #if defined(DDB) && defined(BREAK_TO_DEBUGGER) 89 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 90 breakpoint(); 91 return; 92 } 93 #endif 94 if (sc->sc_opened) 95 atomic_set_32(&sc->sc_ttypend, UART_IPEND_BREAK); 96 } 97 98 /* 99 * Handle a receiver overrun situation. We lost at least 1 byte in the 100 * input stream and it's our job to contain the situation. We grab as 101 * much of the data we can, but otherwise flush the receiver FIFO to 102 * create some breathing room. The net effect is that we avoid the 103 * overrun condition to happen for the next X characters, where X is 104 * related to the FIFO size at the cost of loosing data right away. 105 * So, instead of having multiple overrun interrupts in close proximity 106 * to each other and possibly pessimizing UART interrupt latency for 107 * other UARTs in a multiport configuration, we create a longer segment 108 * of missing characters by freeing up the FIFO. 109 * Each overrun condition is marked in the input buffer by a token. The 110 * token represents the loss of at least one, but possible more bytes in 111 * the input stream. 112 */ 113 static void 114 uart_intr_overrun(struct uart_softc *sc) 115 { 116 117 if (sc->sc_opened) { 118 UART_RECEIVE(sc); 119 if (uart_rx_put(sc, UART_STAT_OVERRUN)) 120 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN; 121 atomic_set_32(&sc->sc_ttypend, UART_IPEND_RXREADY); 122 } 123 UART_FLUSH(sc, UART_FLUSH_RECEIVER); 124 } 125 126 /* 127 * Received data ready. 128 */ 129 static void 130 uart_intr_rxready(struct uart_softc *sc) 131 { 132 int rxp; 133 134 rxp = sc->sc_rxput; 135 UART_RECEIVE(sc); 136 #if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER) 137 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 138 while (rxp != sc->sc_rxput) { 139 if (db_alt_break(sc->sc_rxbuf[rxp++], &sc->sc_altbrk)) 140 breakpoint(); 141 if (rxp == sc->sc_rxbufsz) 142 rxp = 0; 143 } 144 } 145 #endif 146 if (sc->sc_opened) 147 atomic_set_32(&sc->sc_ttypend, UART_IPEND_RXREADY); 148 else 149 sc->sc_rxput = sc->sc_rxget; /* Ignore received data. */ 150 } 151 152 /* 153 * Line or modem status change (OOB signalling). 154 * We pass the signals to the software interrupt handler for further 155 * processing. Note that we merge the delta bits, but set the state 156 * bits. This is to avoid loosing state transitions due to having more 157 * than 1 hardware interrupt between software interrupts. 158 */ 159 static void 160 uart_intr_sigchg(struct uart_softc *sc) 161 { 162 int new, old, sig; 163 164 sig = UART_GETSIG(sc); 165 do { 166 old = sc->sc_ttypend; 167 new = old & ~UART_SIGMASK_STATE; 168 new |= sig & UART_IPEND_SIGMASK; 169 new |= UART_IPEND_SIGCHG; 170 } while (!atomic_cmpset_32(&sc->sc_ttypend, old, new)); 171 } 172 173 /* 174 * The transmitter can accept more data. 175 */ 176 static void 177 uart_intr_txidle(struct uart_softc *sc) 178 { 179 if (sc->sc_txbusy) { 180 sc->sc_txbusy = 0; 181 atomic_set_32(&sc->sc_ttypend, UART_IPEND_TXIDLE); 182 } 183 } 184 185 static void 186 uart_intr(void *arg) 187 { 188 struct uart_softc *sc = arg; 189 int ipend; 190 191 if (sc->sc_leaving) 192 return; 193 194 ipend = UART_IPEND(sc); 195 if (ipend & UART_IPEND_OVERRUN) 196 uart_intr_overrun(sc); 197 if (ipend & UART_IPEND_BREAK) 198 uart_intr_break(sc); 199 if (ipend & UART_IPEND_RXREADY) 200 uart_intr_rxready(sc); 201 if (ipend & UART_IPEND_SIGCHG) 202 uart_intr_sigchg(sc); 203 if (ipend & UART_IPEND_TXIDLE) 204 uart_intr_txidle(sc); 205 206 if (sc->sc_opened && sc->sc_ttypend != 0) 207 swi_sched(sc->sc_softih, 0); 208 } 209 210 int 211 uart_bus_probe(device_t dev, int regshft, int rclk, int rid) 212 { 213 struct uart_softc *sc; 214 struct uart_devinfo *sysdev; 215 int error; 216 217 /* 218 * Initialize the instance. Note that the instance (=softc) does 219 * not necessarily match the hardware specific softc. We can't do 220 * anything about it now, because we may not attach to the device. 221 * Hardware drivers cannot use any of the class specific fields 222 * while probing. 223 */ 224 sc = device_get_softc(dev); 225 kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class); 226 sc->sc_dev = dev; 227 if (device_get_desc(dev) == NULL) 228 device_set_desc(dev, sc->sc_class->name); 229 230 /* 231 * Allocate the register resource. We assume that all UARTs have 232 * a single register window in either I/O port space or memory 233 * mapped I/O space. Any UART that needs multiple windows will 234 * consequently not be supported by this driver as-is. We try I/O 235 * port space first because that's the common case. 236 */ 237 sc->sc_rrid = rid; 238 sc->sc_rtype = SYS_RES_IOPORT; 239 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid, 240 0, ~0, sc->sc_class->uc_range, RF_ACTIVE); 241 if (sc->sc_rres == NULL) { 242 sc->sc_rrid = rid; 243 sc->sc_rtype = SYS_RES_MEMORY; 244 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, 245 &sc->sc_rrid, 0, ~0, sc->sc_class->uc_range, RF_ACTIVE); 246 if (sc->sc_rres == NULL) 247 return (ENXIO); 248 } 249 250 /* 251 * Fill in the bus access structure and compare this device with 252 * a possible console device and/or a debug port. We set the flags 253 * in the softc so that the hardware dependent probe can adjust 254 * accordingly. In general, you don't want to permanently disrupt 255 * console I/O. 256 */ 257 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 258 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 259 sc->sc_bas.regshft = regshft; 260 sc->sc_bas.rclk = (rclk == 0) ? sc->sc_class->uc_rclk : rclk; 261 262 SLIST_FOREACH(sysdev, &uart_sysdevs, next) { 263 if (uart_cpu_eqres(&sc->sc_bas, &sysdev->bas)) { 264 /* XXX check if ops matches class. */ 265 sc->sc_sysdev = sysdev; 266 break; 267 } 268 } 269 270 error = UART_PROBE(sc); 271 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 272 return (error); 273 } 274 275 int 276 uart_bus_attach(device_t dev) 277 { 278 struct uart_softc *sc, *sc0; 279 const char *sep; 280 int error; 281 282 /* 283 * The sc_class field defines the type of UART we're going to work 284 * with and thus the size of the softc. Replace the generic softc 285 * with one that matches the UART now that we're certain we handle 286 * the device. 287 */ 288 sc0 = device_get_softc(dev); 289 if (sc0->sc_class->size > sizeof(*sc)) { 290 sc = malloc(sc0->sc_class->size, M_UART, M_WAITOK|M_ZERO); 291 bcopy(sc0, sc, sizeof(*sc)); 292 device_set_softc(dev, sc); 293 } else 294 sc = sc0; 295 296 /* 297 * Protect ourselves against interrupts while we're not completely 298 * finished attaching and initializing. We don't expect interrupts 299 * until after UART_ATTACH() though. 300 */ 301 sc->sc_leaving = 1; 302 303 /* 304 * Re-allocate. We expect that the softc contains the information 305 * collected by uart_bus_probe() intact. 306 */ 307 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid, 308 0, ~0, sc->sc_class->uc_range, RF_ACTIVE); 309 if (sc->sc_rres == NULL) 310 return (ENXIO); 311 312 sc->sc_irid = 0; 313 sc->sc_ires = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irid, 314 0, ~0, 1, RF_ACTIVE); 315 if (sc->sc_ires != NULL) { 316 error = BUS_SETUP_INTR(device_get_parent(dev), dev, 317 sc->sc_ires, INTR_TYPE_TTY | INTR_FAST, uart_intr, 318 sc, &sc->sc_icookie); 319 if (error) 320 error = BUS_SETUP_INTR(device_get_parent(dev), dev, 321 sc->sc_ires, INTR_TYPE_TTY, uart_intr, sc, 322 &sc->sc_icookie); 323 else 324 sc->sc_fastintr = 1; 325 326 if (error) { 327 device_printf(dev, "could not activate interrupt\n"); 328 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 329 sc->sc_ires); 330 sc->sc_ires = NULL; 331 } 332 } 333 if (sc->sc_ires == NULL) { 334 /* XXX no interrupt resource. Force polled mode. */ 335 sc->sc_polled = 1; 336 } 337 338 sc->sc_rxbufsz = IBUFSIZ; 339 sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf), 340 M_UART, M_WAITOK); 341 sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf), 342 M_UART, M_WAITOK); 343 344 error = UART_ATTACH(sc); 345 if (error) 346 goto fail; 347 348 if (sc->sc_hwiflow || sc->sc_hwoflow) { 349 sep = ""; 350 device_print_prettyname(dev); 351 if (sc->sc_hwiflow) { 352 printf("%sRTS iflow", sep); 353 sep = ", "; 354 } 355 if (sc->sc_hwoflow) { 356 printf("%sCTS oflow", sep); 357 sep = ", "; 358 } 359 printf("\n"); 360 } 361 362 if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) { 363 sep = ""; 364 device_print_prettyname(dev); 365 if (sc->sc_fastintr) { 366 printf("%sfast interrupt", sep); 367 sep = ", "; 368 } 369 if (sc->sc_polled) { 370 printf("%spolled mode", sep); 371 sep = ", "; 372 } 373 printf("\n"); 374 } 375 376 if (sc->sc_sysdev != NULL) { 377 switch (sc->sc_sysdev->type) { 378 case UART_DEV_CONSOLE: 379 device_printf(dev, "console"); 380 break; 381 case UART_DEV_DBGPORT: 382 device_printf(dev, "debug port"); 383 break; 384 case UART_DEV_KEYBOARD: 385 device_printf(dev, "keyboard"); 386 break; 387 default: 388 device_printf(dev, "unknown system device"); 389 break; 390 } 391 printf(" (%d,%c,%d,%d)\n", sc->sc_sysdev->baudrate, 392 "noems"[sc->sc_sysdev->parity], sc->sc_sysdev->databits, 393 sc->sc_sysdev->stopbits); 394 } 395 396 error = (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL) 397 ? (*sc->sc_sysdev->attach)(sc) : uart_tty_attach(sc); 398 if (error) 399 goto fail; 400 401 sc->sc_leaving = 0; 402 uart_intr(sc); 403 return (0); 404 405 fail: 406 free(sc->sc_txbuf, M_UART); 407 free(sc->sc_rxbuf, M_UART); 408 409 if (sc->sc_ires != NULL) { 410 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 411 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 412 sc->sc_ires); 413 } 414 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 415 416 return (error); 417 } 418 419 int 420 uart_bus_detach(device_t dev) 421 { 422 struct uart_softc *sc; 423 424 sc = device_get_softc(dev); 425 426 sc->sc_leaving = 1; 427 428 UART_DETACH(sc); 429 430 if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL) 431 (*sc->sc_sysdev->detach)(sc); 432 else 433 uart_tty_detach(sc); 434 435 free(sc->sc_txbuf, M_UART); 436 free(sc->sc_rxbuf, M_UART); 437 438 if (sc->sc_ires != NULL) { 439 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 440 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 441 sc->sc_ires); 442 } 443 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 444 445 if (sc->sc_class->size > sizeof(*sc)) { 446 device_set_softc(dev, NULL); 447 free(sc, M_UART); 448 } else 449 device_set_softc(dev, NULL); 450 451 return (0); 452 } 453