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 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/kernel.h> 35 #include <sys/sysctl.h> 36 #include <machine/bus.h> 37 38 #include <dev/uart/uart.h> 39 #include <dev/uart/uart_cpu.h> 40 #include <dev/uart/uart_bus.h> 41 42 #include <dev/ic/ns16550.h> 43 44 #include "uart_if.h" 45 46 #define DEFAULT_RCLK 1843200 47 48 /* 49 * Clear pending interrupts. THRE is cleared by reading IIR. Data 50 * that may have been received gets lost here. 51 */ 52 static void 53 ns8250_clrint(struct uart_bas *bas) 54 { 55 uint8_t iir, lsr; 56 57 iir = uart_getreg(bas, REG_IIR); 58 while ((iir & IIR_NOPEND) == 0) { 59 iir &= IIR_IMASK; 60 if (iir == IIR_RLS) { 61 lsr = uart_getreg(bas, REG_LSR); 62 if (lsr & (LSR_BI|LSR_FE|LSR_PE)) 63 (void)uart_getreg(bas, REG_DATA); 64 } else if (iir == IIR_RXRDY || iir == IIR_RXTOUT) 65 (void)uart_getreg(bas, REG_DATA); 66 else if (iir == IIR_MLSC) 67 (void)uart_getreg(bas, REG_MSR); 68 uart_barrier(bas); 69 iir = uart_getreg(bas, REG_IIR); 70 } 71 } 72 73 static int 74 ns8250_delay(struct uart_bas *bas) 75 { 76 int divisor; 77 u_char lcr; 78 79 lcr = uart_getreg(bas, REG_LCR); 80 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB); 81 uart_barrier(bas); 82 divisor = uart_getreg(bas, REG_DLL) | (uart_getreg(bas, REG_DLH) << 8); 83 uart_barrier(bas); 84 uart_setreg(bas, REG_LCR, lcr); 85 uart_barrier(bas); 86 87 /* 1/10th the time to transmit 1 character (estimate). */ 88 if (divisor <= 134) 89 return (16000000 * divisor / bas->rclk); 90 return (16000 * divisor / (bas->rclk / 1000)); 91 } 92 93 static int 94 ns8250_divisor(int rclk, int baudrate) 95 { 96 int actual_baud, divisor; 97 int error; 98 99 if (baudrate == 0) 100 return (0); 101 102 divisor = (rclk / (baudrate << 3) + 1) >> 1; 103 if (divisor == 0 || divisor >= 65536) 104 return (0); 105 actual_baud = rclk / (divisor << 4); 106 107 /* 10 times error in percent: */ 108 error = ((actual_baud - baudrate) * 2000 / baudrate + 1) >> 1; 109 110 /* 3.0% maximum error tolerance: */ 111 if (error < -30 || error > 30) 112 return (0); 113 114 return (divisor); 115 } 116 117 static int 118 ns8250_drain(struct uart_bas *bas, int what) 119 { 120 int delay, limit; 121 122 delay = ns8250_delay(bas); 123 124 if (what & UART_DRAIN_TRANSMITTER) { 125 /* 126 * Pick an arbitrary high limit to avoid getting stuck in 127 * an infinite loop when the hardware is broken. Make the 128 * limit high enough to handle large FIFOs. 129 */ 130 limit = 10*1024; 131 while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit) 132 DELAY(delay); 133 if (limit == 0) { 134 /* printf("ns8250: transmitter appears stuck... "); */ 135 return (EIO); 136 } 137 } 138 139 if (what & UART_DRAIN_RECEIVER) { 140 /* 141 * Pick an arbitrary high limit to avoid getting stuck in 142 * an infinite loop when the hardware is broken. Make the 143 * limit high enough to handle large FIFOs and integrated 144 * UARTs. The HP rx2600 for example has 3 UARTs on the 145 * management board that tend to get a lot of data send 146 * to it when the UART is first activated. 147 */ 148 limit=10*4096; 149 while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) && --limit) { 150 (void)uart_getreg(bas, REG_DATA); 151 uart_barrier(bas); 152 DELAY(delay << 2); 153 } 154 if (limit == 0) { 155 /* printf("ns8250: receiver appears broken... "); */ 156 return (EIO); 157 } 158 } 159 160 return (0); 161 } 162 163 /* 164 * We can only flush UARTs with FIFOs. UARTs without FIFOs should be 165 * drained. WARNING: this function clobbers the FIFO setting! 166 */ 167 static void 168 ns8250_flush(struct uart_bas *bas, int what) 169 { 170 uint8_t fcr; 171 172 fcr = FCR_ENABLE; 173 if (what & UART_FLUSH_TRANSMITTER) 174 fcr |= FCR_XMT_RST; 175 if (what & UART_FLUSH_RECEIVER) 176 fcr |= FCR_RCV_RST; 177 uart_setreg(bas, REG_FCR, fcr); 178 uart_barrier(bas); 179 } 180 181 static int 182 ns8250_param(struct uart_bas *bas, int baudrate, int databits, int stopbits, 183 int parity) 184 { 185 int divisor; 186 uint8_t lcr; 187 188 lcr = 0; 189 if (databits >= 8) 190 lcr |= LCR_8BITS; 191 else if (databits == 7) 192 lcr |= LCR_7BITS; 193 else if (databits == 6) 194 lcr |= LCR_6BITS; 195 else 196 lcr |= LCR_5BITS; 197 if (stopbits > 1) 198 lcr |= LCR_STOPB; 199 lcr |= parity << 3; 200 201 /* Set baudrate. */ 202 if (baudrate > 0) { 203 divisor = ns8250_divisor(bas->rclk, baudrate); 204 if (divisor == 0) 205 return (EINVAL); 206 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB); 207 uart_barrier(bas); 208 uart_setreg(bas, REG_DLL, divisor & 0xff); 209 uart_setreg(bas, REG_DLH, (divisor >> 8) & 0xff); 210 uart_barrier(bas); 211 } 212 213 /* Set LCR and clear DLAB. */ 214 uart_setreg(bas, REG_LCR, lcr); 215 uart_barrier(bas); 216 return (0); 217 } 218 219 /* 220 * Low-level UART interface. 221 */ 222 static int ns8250_probe(struct uart_bas *bas); 223 static void ns8250_init(struct uart_bas *bas, int, int, int, int); 224 static void ns8250_term(struct uart_bas *bas); 225 static void ns8250_putc(struct uart_bas *bas, int); 226 static int ns8250_rxready(struct uart_bas *bas); 227 static int ns8250_getc(struct uart_bas *bas, struct mtx *); 228 229 static struct uart_ops uart_ns8250_ops = { 230 .probe = ns8250_probe, 231 .init = ns8250_init, 232 .term = ns8250_term, 233 .putc = ns8250_putc, 234 .rxready = ns8250_rxready, 235 .getc = ns8250_getc, 236 }; 237 238 static int 239 ns8250_probe(struct uart_bas *bas) 240 { 241 u_char val; 242 243 /* Check known 0 bits that don't depend on DLAB. */ 244 val = uart_getreg(bas, REG_IIR); 245 if (val & 0x30) 246 return (ENXIO); 247 /* 248 * Bit 6 of the MCR (= 0x40) appears to be 1 for the Sun1699 249 * chip, but otherwise doesn't seem to have a function. In 250 * other words, uart(4) works regardless. Ignore that bit so 251 * the probe succeeds. 252 */ 253 val = uart_getreg(bas, REG_MCR); 254 if (val & 0xa0) 255 return (ENXIO); 256 257 return (0); 258 } 259 260 static void 261 ns8250_init(struct uart_bas *bas, int baudrate, int databits, int stopbits, 262 int parity) 263 { 264 u_char ier; 265 266 if (bas->rclk == 0) 267 bas->rclk = DEFAULT_RCLK; 268 ns8250_param(bas, baudrate, databits, stopbits, parity); 269 270 /* Disable all interrupt sources. */ 271 /* 272 * We use 0xe0 instead of 0xf0 as the mask because the XScale PXA 273 * UARTs split the receive time-out interrupt bit out separately as 274 * 0x10. This gets handled by ier_mask and ier_rxbits below. 275 */ 276 ier = uart_getreg(bas, REG_IER) & 0xe0; 277 uart_setreg(bas, REG_IER, ier); 278 uart_barrier(bas); 279 280 /* Disable the FIFO (if present). */ 281 uart_setreg(bas, REG_FCR, 0); 282 uart_barrier(bas); 283 284 /* Set RTS & DTR. */ 285 uart_setreg(bas, REG_MCR, MCR_IE | MCR_RTS | MCR_DTR); 286 uart_barrier(bas); 287 288 ns8250_clrint(bas); 289 } 290 291 static void 292 ns8250_term(struct uart_bas *bas) 293 { 294 295 /* Clear RTS & DTR. */ 296 uart_setreg(bas, REG_MCR, MCR_IE); 297 uart_barrier(bas); 298 } 299 300 static void 301 ns8250_putc(struct uart_bas *bas, int c) 302 { 303 int limit; 304 305 limit = 250000; 306 while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0 && --limit) 307 DELAY(4); 308 uart_setreg(bas, REG_DATA, c); 309 uart_barrier(bas); 310 limit = 250000; 311 while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit) 312 DELAY(4); 313 } 314 315 static int 316 ns8250_rxready(struct uart_bas *bas) 317 { 318 319 return ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) != 0 ? 1 : 0); 320 } 321 322 static int 323 ns8250_getc(struct uart_bas *bas, struct mtx *hwmtx) 324 { 325 int c; 326 327 uart_lock(hwmtx); 328 329 while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) == 0) { 330 uart_unlock(hwmtx); 331 DELAY(4); 332 uart_lock(hwmtx); 333 } 334 335 c = uart_getreg(bas, REG_DATA); 336 337 uart_unlock(hwmtx); 338 339 return (c); 340 } 341 342 /* 343 * High-level UART interface. 344 */ 345 struct ns8250_softc { 346 struct uart_softc base; 347 uint8_t fcr; 348 uint8_t ier; 349 uint8_t mcr; 350 351 uint8_t ier_mask; 352 uint8_t ier_rxbits; 353 }; 354 355 static int ns8250_bus_attach(struct uart_softc *); 356 static int ns8250_bus_detach(struct uart_softc *); 357 static int ns8250_bus_flush(struct uart_softc *, int); 358 static int ns8250_bus_getsig(struct uart_softc *); 359 static int ns8250_bus_ioctl(struct uart_softc *, int, intptr_t); 360 static int ns8250_bus_ipend(struct uart_softc *); 361 static int ns8250_bus_param(struct uart_softc *, int, int, int, int); 362 static int ns8250_bus_probe(struct uart_softc *); 363 static int ns8250_bus_receive(struct uart_softc *); 364 static int ns8250_bus_setsig(struct uart_softc *, int); 365 static int ns8250_bus_transmit(struct uart_softc *); 366 367 static kobj_method_t ns8250_methods[] = { 368 KOBJMETHOD(uart_attach, ns8250_bus_attach), 369 KOBJMETHOD(uart_detach, ns8250_bus_detach), 370 KOBJMETHOD(uart_flush, ns8250_bus_flush), 371 KOBJMETHOD(uart_getsig, ns8250_bus_getsig), 372 KOBJMETHOD(uart_ioctl, ns8250_bus_ioctl), 373 KOBJMETHOD(uart_ipend, ns8250_bus_ipend), 374 KOBJMETHOD(uart_param, ns8250_bus_param), 375 KOBJMETHOD(uart_probe, ns8250_bus_probe), 376 KOBJMETHOD(uart_receive, ns8250_bus_receive), 377 KOBJMETHOD(uart_setsig, ns8250_bus_setsig), 378 KOBJMETHOD(uart_transmit, ns8250_bus_transmit), 379 { 0, 0 } 380 }; 381 382 struct uart_class uart_ns8250_class = { 383 "ns8250", 384 ns8250_methods, 385 sizeof(struct ns8250_softc), 386 .uc_ops = &uart_ns8250_ops, 387 .uc_range = 8, 388 .uc_rclk = DEFAULT_RCLK 389 }; 390 391 #define SIGCHG(c, i, s, d) \ 392 if (c) { \ 393 i |= (i & s) ? s : s | d; \ 394 } else { \ 395 i = (i & s) ? (i & ~s) | d : i; \ 396 } 397 398 static int 399 ns8250_bus_attach(struct uart_softc *sc) 400 { 401 struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc; 402 struct uart_bas *bas; 403 unsigned int ivar; 404 405 bas = &sc->sc_bas; 406 407 ns8250->mcr = uart_getreg(bas, REG_MCR); 408 ns8250->fcr = FCR_ENABLE; 409 if (!resource_int_value("uart", device_get_unit(sc->sc_dev), "flags", 410 &ivar)) { 411 if (UART_FLAGS_FCR_RX_LOW(ivar)) 412 ns8250->fcr |= FCR_RX_LOW; 413 else if (UART_FLAGS_FCR_RX_MEDL(ivar)) 414 ns8250->fcr |= FCR_RX_MEDL; 415 else if (UART_FLAGS_FCR_RX_HIGH(ivar)) 416 ns8250->fcr |= FCR_RX_HIGH; 417 else 418 ns8250->fcr |= FCR_RX_MEDH; 419 } else 420 ns8250->fcr |= FCR_RX_MEDH; 421 422 /* Get IER mask */ 423 ivar = 0xf0; 424 resource_int_value("uart", device_get_unit(sc->sc_dev), "ier_mask", 425 &ivar); 426 ns8250->ier_mask = (uint8_t)(ivar & 0xff); 427 428 /* Get IER RX interrupt bits */ 429 ivar = IER_EMSC | IER_ERLS | IER_ERXRDY; 430 resource_int_value("uart", device_get_unit(sc->sc_dev), "ier_rxbits", 431 &ivar); 432 ns8250->ier_rxbits = (uint8_t)(ivar & 0xff); 433 434 uart_setreg(bas, REG_FCR, ns8250->fcr); 435 uart_barrier(bas); 436 ns8250_bus_flush(sc, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER); 437 438 if (ns8250->mcr & MCR_DTR) 439 sc->sc_hwsig |= SER_DTR; 440 if (ns8250->mcr & MCR_RTS) 441 sc->sc_hwsig |= SER_RTS; 442 ns8250_bus_getsig(sc); 443 444 ns8250_clrint(bas); 445 ns8250->ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask; 446 ns8250->ier |= ns8250->ier_rxbits; 447 uart_setreg(bas, REG_IER, ns8250->ier); 448 uart_barrier(bas); 449 450 return (0); 451 } 452 453 static int 454 ns8250_bus_detach(struct uart_softc *sc) 455 { 456 struct ns8250_softc *ns8250; 457 struct uart_bas *bas; 458 u_char ier; 459 460 ns8250 = (struct ns8250_softc *)sc; 461 bas = &sc->sc_bas; 462 ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask; 463 uart_setreg(bas, REG_IER, ier); 464 uart_barrier(bas); 465 ns8250_clrint(bas); 466 return (0); 467 } 468 469 static int 470 ns8250_bus_flush(struct uart_softc *sc, int what) 471 { 472 struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc; 473 struct uart_bas *bas; 474 int error; 475 476 bas = &sc->sc_bas; 477 uart_lock(sc->sc_hwmtx); 478 if (sc->sc_rxfifosz > 1) { 479 ns8250_flush(bas, what); 480 uart_setreg(bas, REG_FCR, ns8250->fcr); 481 uart_barrier(bas); 482 error = 0; 483 } else 484 error = ns8250_drain(bas, what); 485 uart_unlock(sc->sc_hwmtx); 486 return (error); 487 } 488 489 static int 490 ns8250_bus_getsig(struct uart_softc *sc) 491 { 492 uint32_t new, old, sig; 493 uint8_t msr; 494 495 do { 496 old = sc->sc_hwsig; 497 sig = old; 498 uart_lock(sc->sc_hwmtx); 499 msr = uart_getreg(&sc->sc_bas, REG_MSR); 500 uart_unlock(sc->sc_hwmtx); 501 SIGCHG(msr & MSR_DSR, sig, SER_DSR, SER_DDSR); 502 SIGCHG(msr & MSR_CTS, sig, SER_CTS, SER_DCTS); 503 SIGCHG(msr & MSR_DCD, sig, SER_DCD, SER_DDCD); 504 SIGCHG(msr & MSR_RI, sig, SER_RI, SER_DRI); 505 new = sig & ~SER_MASK_DELTA; 506 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new)); 507 return (sig); 508 } 509 510 static int 511 ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data) 512 { 513 struct uart_bas *bas; 514 int baudrate, divisor, error; 515 uint8_t efr, lcr; 516 517 bas = &sc->sc_bas; 518 error = 0; 519 uart_lock(sc->sc_hwmtx); 520 switch (request) { 521 case UART_IOCTL_BREAK: 522 lcr = uart_getreg(bas, REG_LCR); 523 if (data) 524 lcr |= LCR_SBREAK; 525 else 526 lcr &= ~LCR_SBREAK; 527 uart_setreg(bas, REG_LCR, lcr); 528 uart_barrier(bas); 529 break; 530 case UART_IOCTL_IFLOW: 531 lcr = uart_getreg(bas, REG_LCR); 532 uart_barrier(bas); 533 uart_setreg(bas, REG_LCR, 0xbf); 534 uart_barrier(bas); 535 efr = uart_getreg(bas, REG_EFR); 536 if (data) 537 efr |= EFR_RTS; 538 else 539 efr &= ~EFR_RTS; 540 uart_setreg(bas, REG_EFR, efr); 541 uart_barrier(bas); 542 uart_setreg(bas, REG_LCR, lcr); 543 uart_barrier(bas); 544 break; 545 case UART_IOCTL_OFLOW: 546 lcr = uart_getreg(bas, REG_LCR); 547 uart_barrier(bas); 548 uart_setreg(bas, REG_LCR, 0xbf); 549 uart_barrier(bas); 550 efr = uart_getreg(bas, REG_EFR); 551 if (data) 552 efr |= EFR_CTS; 553 else 554 efr &= ~EFR_CTS; 555 uart_setreg(bas, REG_EFR, efr); 556 uart_barrier(bas); 557 uart_setreg(bas, REG_LCR, lcr); 558 uart_barrier(bas); 559 break; 560 case UART_IOCTL_BAUD: 561 lcr = uart_getreg(bas, REG_LCR); 562 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB); 563 uart_barrier(bas); 564 divisor = uart_getreg(bas, REG_DLL) | 565 (uart_getreg(bas, REG_DLH) << 8); 566 uart_barrier(bas); 567 uart_setreg(bas, REG_LCR, lcr); 568 uart_barrier(bas); 569 baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0; 570 if (baudrate > 0) 571 *(int*)data = baudrate; 572 else 573 error = ENXIO; 574 break; 575 default: 576 error = EINVAL; 577 break; 578 } 579 uart_unlock(sc->sc_hwmtx); 580 return (error); 581 } 582 583 static int 584 ns8250_bus_ipend(struct uart_softc *sc) 585 { 586 struct uart_bas *bas; 587 struct ns8250_softc *ns8250; 588 int ipend; 589 uint8_t iir, lsr; 590 591 ns8250 = (struct ns8250_softc *)sc; 592 bas = &sc->sc_bas; 593 uart_lock(sc->sc_hwmtx); 594 iir = uart_getreg(bas, REG_IIR); 595 if (iir & IIR_NOPEND) { 596 uart_unlock(sc->sc_hwmtx); 597 return (0); 598 } 599 ipend = 0; 600 if (iir & IIR_RXRDY) { 601 lsr = uart_getreg(bas, REG_LSR); 602 if (lsr & LSR_OE) 603 ipend |= SER_INT_OVERRUN; 604 if (lsr & LSR_BI) 605 ipend |= SER_INT_BREAK; 606 if (lsr & LSR_RXRDY) 607 ipend |= SER_INT_RXREADY; 608 } else { 609 if (iir & IIR_TXRDY) { 610 ipend |= SER_INT_TXIDLE; 611 uart_setreg(bas, REG_IER, ns8250->ier); 612 } else 613 ipend |= SER_INT_SIGCHG; 614 } 615 if (ipend == 0) 616 ns8250_clrint(bas); 617 uart_unlock(sc->sc_hwmtx); 618 return (ipend); 619 } 620 621 static int 622 ns8250_bus_param(struct uart_softc *sc, int baudrate, int databits, 623 int stopbits, int parity) 624 { 625 struct uart_bas *bas; 626 int error; 627 628 bas = &sc->sc_bas; 629 uart_lock(sc->sc_hwmtx); 630 error = ns8250_param(bas, baudrate, databits, stopbits, parity); 631 uart_unlock(sc->sc_hwmtx); 632 return (error); 633 } 634 635 static int 636 ns8250_bus_probe(struct uart_softc *sc) 637 { 638 struct ns8250_softc *ns8250; 639 struct uart_bas *bas; 640 int count, delay, error, limit; 641 uint8_t lsr, mcr, ier; 642 643 ns8250 = (struct ns8250_softc *)sc; 644 bas = &sc->sc_bas; 645 646 error = ns8250_probe(bas); 647 if (error) 648 return (error); 649 650 mcr = MCR_IE; 651 if (sc->sc_sysdev == NULL) { 652 /* By using ns8250_init() we also set DTR and RTS. */ 653 ns8250_init(bas, 115200, 8, 1, UART_PARITY_NONE); 654 } else 655 mcr |= MCR_DTR | MCR_RTS; 656 657 error = ns8250_drain(bas, UART_DRAIN_TRANSMITTER); 658 if (error) 659 return (error); 660 661 /* 662 * Set loopback mode. This avoids having garbage on the wire and 663 * also allows us send and receive data. We set DTR and RTS to 664 * avoid the possibility that automatic flow-control prevents 665 * any data from being sent. 666 */ 667 uart_setreg(bas, REG_MCR, MCR_LOOPBACK | MCR_IE | MCR_DTR | MCR_RTS); 668 uart_barrier(bas); 669 670 /* 671 * Enable FIFOs. And check that the UART has them. If not, we're 672 * done. Since this is the first time we enable the FIFOs, we reset 673 * them. 674 */ 675 uart_setreg(bas, REG_FCR, FCR_ENABLE); 676 uart_barrier(bas); 677 if (!(uart_getreg(bas, REG_IIR) & IIR_FIFO_MASK)) { 678 /* 679 * NS16450 or INS8250. We don't bother to differentiate 680 * between them. They're too old to be interesting. 681 */ 682 uart_setreg(bas, REG_MCR, mcr); 683 uart_barrier(bas); 684 sc->sc_rxfifosz = sc->sc_txfifosz = 1; 685 device_set_desc(sc->sc_dev, "8250 or 16450 or compatible"); 686 return (0); 687 } 688 689 uart_setreg(bas, REG_FCR, FCR_ENABLE | FCR_XMT_RST | FCR_RCV_RST); 690 uart_barrier(bas); 691 692 count = 0; 693 delay = ns8250_delay(bas); 694 695 /* We have FIFOs. Drain the transmitter and receiver. */ 696 error = ns8250_drain(bas, UART_DRAIN_RECEIVER|UART_DRAIN_TRANSMITTER); 697 if (error) { 698 uart_setreg(bas, REG_MCR, mcr); 699 uart_setreg(bas, REG_FCR, 0); 700 uart_barrier(bas); 701 goto describe; 702 } 703 704 /* 705 * We should have a sufficiently clean "pipe" to determine the 706 * size of the FIFOs. We send as much characters as is reasonable 707 * and wait for the overflow bit in the LSR register to be 708 * asserted, counting the characters as we send them. Based on 709 * that count we know the FIFO size. 710 */ 711 do { 712 uart_setreg(bas, REG_DATA, 0); 713 uart_barrier(bas); 714 count++; 715 716 limit = 30; 717 lsr = 0; 718 /* 719 * LSR bits are cleared upon read, so we must accumulate 720 * them to be able to test LSR_OE below. 721 */ 722 while (((lsr |= uart_getreg(bas, REG_LSR)) & LSR_TEMT) == 0 && 723 --limit) 724 DELAY(delay); 725 if (limit == 0) { 726 ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask; 727 uart_setreg(bas, REG_IER, ier); 728 uart_setreg(bas, REG_MCR, mcr); 729 uart_setreg(bas, REG_FCR, 0); 730 uart_barrier(bas); 731 count = 0; 732 goto describe; 733 } 734 } while ((lsr & LSR_OE) == 0 && count < 130); 735 count--; 736 737 uart_setreg(bas, REG_MCR, mcr); 738 739 /* Reset FIFOs. */ 740 ns8250_flush(bas, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER); 741 742 describe: 743 if (count >= 14 && count <= 16) { 744 sc->sc_rxfifosz = 16; 745 device_set_desc(sc->sc_dev, "16550 or compatible"); 746 } else if (count >= 28 && count <= 32) { 747 sc->sc_rxfifosz = 32; 748 device_set_desc(sc->sc_dev, "16650 or compatible"); 749 } else if (count >= 56 && count <= 64) { 750 sc->sc_rxfifosz = 64; 751 device_set_desc(sc->sc_dev, "16750 or compatible"); 752 } else if (count >= 112 && count <= 128) { 753 sc->sc_rxfifosz = 128; 754 device_set_desc(sc->sc_dev, "16950 or compatible"); 755 } else { 756 sc->sc_rxfifosz = 16; 757 device_set_desc(sc->sc_dev, 758 "Non-standard ns8250 class UART with FIFOs"); 759 } 760 761 /* 762 * Force the Tx FIFO size to 16 bytes for now. We don't program the 763 * Tx trigger. Also, we assume that all data has been sent when the 764 * interrupt happens. 765 */ 766 sc->sc_txfifosz = 16; 767 768 #if 0 769 /* 770 * XXX there are some issues related to hardware flow control and 771 * it's likely that uart(4) is the cause. This basicly needs more 772 * investigation, but we avoid using for hardware flow control 773 * until then. 774 */ 775 /* 16650s or higher have automatic flow control. */ 776 if (sc->sc_rxfifosz > 16) { 777 sc->sc_hwiflow = 1; 778 sc->sc_hwoflow = 1; 779 } 780 #endif 781 782 return (0); 783 } 784 785 static int 786 ns8250_bus_receive(struct uart_softc *sc) 787 { 788 struct uart_bas *bas; 789 int xc; 790 uint8_t lsr; 791 792 bas = &sc->sc_bas; 793 uart_lock(sc->sc_hwmtx); 794 lsr = uart_getreg(bas, REG_LSR); 795 while (lsr & LSR_RXRDY) { 796 if (uart_rx_full(sc)) { 797 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN; 798 break; 799 } 800 xc = uart_getreg(bas, REG_DATA); 801 if (lsr & LSR_FE) 802 xc |= UART_STAT_FRAMERR; 803 if (lsr & LSR_PE) 804 xc |= UART_STAT_PARERR; 805 uart_rx_put(sc, xc); 806 lsr = uart_getreg(bas, REG_LSR); 807 } 808 /* Discard everything left in the Rx FIFO. */ 809 while (lsr & LSR_RXRDY) { 810 (void)uart_getreg(bas, REG_DATA); 811 uart_barrier(bas); 812 lsr = uart_getreg(bas, REG_LSR); 813 } 814 uart_unlock(sc->sc_hwmtx); 815 return (0); 816 } 817 818 static int 819 ns8250_bus_setsig(struct uart_softc *sc, int sig) 820 { 821 struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc; 822 struct uart_bas *bas; 823 uint32_t new, old; 824 825 bas = &sc->sc_bas; 826 do { 827 old = sc->sc_hwsig; 828 new = old; 829 if (sig & SER_DDTR) { 830 SIGCHG(sig & SER_DTR, new, SER_DTR, 831 SER_DDTR); 832 } 833 if (sig & SER_DRTS) { 834 SIGCHG(sig & SER_RTS, new, SER_RTS, 835 SER_DRTS); 836 } 837 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new)); 838 uart_lock(sc->sc_hwmtx); 839 ns8250->mcr &= ~(MCR_DTR|MCR_RTS); 840 if (new & SER_DTR) 841 ns8250->mcr |= MCR_DTR; 842 if (new & SER_RTS) 843 ns8250->mcr |= MCR_RTS; 844 uart_setreg(bas, REG_MCR, ns8250->mcr); 845 uart_barrier(bas); 846 uart_unlock(sc->sc_hwmtx); 847 return (0); 848 } 849 850 static int broken_txfifo = 0; 851 SYSCTL_INT(_hw, OID_AUTO, broken_txfifo, CTLFLAG_RW | CTLFLAG_TUN, 852 &broken_txfifo, 0, "UART FIFO has QEMU emulation bug"); 853 TUNABLE_INT("hw.broken_txfifo", &broken_txfifo); 854 855 static int 856 ns8250_bus_transmit(struct uart_softc *sc) 857 { 858 struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc; 859 struct uart_bas *bas; 860 int i; 861 862 bas = &sc->sc_bas; 863 uart_lock(sc->sc_hwmtx); 864 while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0) 865 ; 866 uart_setreg(bas, REG_IER, ns8250->ier | IER_ETXRDY); 867 uart_barrier(bas); 868 for (i = 0; i < sc->sc_txdatasz; i++) { 869 uart_setreg(bas, REG_DATA, sc->sc_txbuf[i]); 870 uart_barrier(bas); 871 } 872 if (broken_txfifo) 873 ns8250_drain(bas, UART_DRAIN_TRANSMITTER); 874 else 875 sc->sc_txbusy = 1; 876 uart_unlock(sc->sc_hwmtx); 877 if (broken_txfifo) 878 uart_sched_softih(sc, SER_INT_TXIDLE); 879 return (0); 880 } 881