2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause
4 * Copyright (c) 2003 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Derived from uart_dev_ns8250.c
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/systm.h>
65 #include <machine/bus.h>
66 #include <machine/pcpu.h>
68 #include <dev/uart/uart.h>
69 #include <dev/uart/uart_cpu.h>
70 #include <dev/uart/uart_bus.h>
72 #include <dev/ic/ns16550.h>
74 #include <mips/cavium/octeon_pcmap_regs.h>
76 #include <contrib/octeon-sdk/cvmx.h>
81 * Clear pending interrupts. THRE is cleared by reading IIR. Data
82 * that may have been received gets lost here.
85 oct16550_clrint (struct uart_bas *bas)
89 iir = uart_getreg(bas, REG_IIR);
90 while ((iir & IIR_NOPEND) == 0) {
93 (void)uart_getreg(bas, REG_LSR);
94 else if (iir == IIR_RXRDY || iir == IIR_RXTOUT)
95 (void)uart_getreg(bas, REG_DATA);
96 else if (iir == IIR_MLSC)
97 (void)uart_getreg(bas, REG_MSR);
98 else if (iir == IIR_BUSY)
99 (void) uart_getreg(bas, REG_USR);
101 iir = uart_getreg(bas, REG_IIR);
105 static int delay_changed = 1;
108 oct16550_delay (struct uart_bas *bas)
112 static int delay = 0;
114 if (!delay_changed) return delay;
116 lcr = uart_getreg(bas, REG_LCR);
117 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
119 divisor = uart_getreg(bas, REG_DLL) | (uart_getreg(bas, REG_DLH) << 8);
121 uart_setreg(bas, REG_LCR, lcr);
125 return 10; /* return an approx delay value */
127 /* 1/10th the time to transmit 1 character (estimate). */
129 return (16000000 * divisor / bas->rclk);
130 return (16000 * divisor / (bas->rclk / 1000));
135 oct16550_divisor (int rclk, int baudrate)
137 int actual_baud, divisor;
143 divisor = (rclk / (baudrate << 3) + 1) >> 1;
144 if (divisor == 0 || divisor >= 65536)
146 actual_baud = rclk / (divisor << 4);
148 /* 10 times error in percent: */
149 error = ((actual_baud - baudrate) * 2000 / baudrate + 1) >> 1;
151 /* 3.0% maximum error tolerance: */
152 if (error < -30 || error > 30)
159 oct16550_drain (struct uart_bas *bas, int what)
163 delay = oct16550_delay(bas);
165 if (what & UART_DRAIN_TRANSMITTER) {
167 * Pick an arbitrary high limit to avoid getting stuck in
168 * an infinite loop when the hardware is broken. Make the
169 * limit high enough to handle large FIFOs.
171 limit = 10*10*10*1024;
172 while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
175 /* printf("oct16550: transmitter appears stuck... "); */
180 if (what & UART_DRAIN_RECEIVER) {
182 * Pick an arbitrary high limit to avoid getting stuck in
183 * an infinite loop when the hardware is broken. Make the
184 * limit high enough to handle large FIFOs and integrated
185 * UARTs. The HP rx2600 for example has 3 UARTs on the
186 * management board that tend to get a lot of data send
187 * to it when the UART is first activated.
190 while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) && --limit) {
191 (void)uart_getreg(bas, REG_DATA);
196 /* printf("oct16550: receiver appears broken... "); */
205 * We can only flush UARTs with FIFOs. UARTs without FIFOs should be
206 * drained. WARNING: this function clobbers the FIFO setting!
209 oct16550_flush (struct uart_bas *bas, int what)
214 if (what & UART_FLUSH_TRANSMITTER)
216 if (what & UART_FLUSH_RECEIVER)
218 uart_setreg(bas, REG_FCR, fcr);
223 oct16550_param (struct uart_bas *bas, int baudrate, int databits, int stopbits,
232 else if (databits == 7)
234 else if (databits == 6)
244 divisor = oct16550_divisor(bas->rclk, baudrate);
247 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
249 uart_setreg(bas, REG_DLL, divisor & 0xff);
250 uart_setreg(bas, REG_DLH, (divisor >> 8) & 0xff);
255 /* Set LCR and clear DLAB. */
256 uart_setreg(bas, REG_LCR, lcr);
262 * Low-level UART interface.
264 static int oct16550_probe(struct uart_bas *bas);
265 static void oct16550_init(struct uart_bas *bas, int, int, int, int);
266 static void oct16550_term(struct uart_bas *bas);
267 static void oct16550_putc(struct uart_bas *bas, int);
268 static int oct16550_rxready(struct uart_bas *bas);
269 static int oct16550_getc(struct uart_bas *bas, struct mtx *);
271 struct uart_ops uart_oct16550_ops = {
272 .probe = oct16550_probe,
273 .init = oct16550_init,
274 .term = oct16550_term,
275 .putc = oct16550_putc,
276 .rxready = oct16550_rxready,
277 .getc = oct16550_getc,
281 oct16550_probe (struct uart_bas *bas)
285 /* Check known 0 bits that don't depend on DLAB. */
286 val = uart_getreg(bas, REG_IIR);
289 val = uart_getreg(bas, REG_MCR);
292 val = uart_getreg(bas, REG_USR);
299 oct16550_init (struct uart_bas *bas, int baudrate, int databits, int stopbits,
304 oct16550_param(bas, baudrate, databits, stopbits, parity);
306 /* Disable all interrupt sources. */
307 ier = uart_getreg(bas, REG_IER) & 0x0;
308 uart_setreg(bas, REG_IER, ier);
311 /* Disable the FIFO (if present). */
312 // uart_setreg(bas, REG_FCR, 0);
316 uart_setreg(bas, REG_MCR, MCR_RTS | MCR_DTR);
319 oct16550_clrint(bas);
323 oct16550_term (struct uart_bas *bas)
326 /* Clear RTS & DTR. */
327 uart_setreg(bas, REG_MCR, 0);
331 static inline void oct16550_wait_txhr_empty (struct uart_bas *bas, int limit, int delay)
333 while (((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0) &&
334 ((uart_getreg(bas, REG_USR) & USR_TXFIFO_NOTFULL) == 0))
339 oct16550_putc (struct uart_bas *bas, int c)
343 /* 1/10th the time to transmit 1 character (estimate). */
344 delay = oct16550_delay(bas);
345 oct16550_wait_txhr_empty(bas, 100, delay);
346 uart_setreg(bas, REG_DATA, c);
348 oct16550_wait_txhr_empty(bas, 100, delay);
352 oct16550_rxready (struct uart_bas *bas)
355 return ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) != 0 ? 1 : 0);
359 oct16550_getc (struct uart_bas *bas, struct mtx *hwmtx)
365 /* 1/10th the time to transmit 1 character (estimate). */
366 delay = oct16550_delay(bas);
368 while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) == 0) {
374 c = uart_getreg(bas, REG_DATA);
382 * High-level UART interface.
384 struct oct16550_softc {
385 struct uart_softc base;
391 static int oct16550_bus_attach(struct uart_softc *);
392 static int oct16550_bus_detach(struct uart_softc *);
393 static int oct16550_bus_flush(struct uart_softc *, int);
394 static int oct16550_bus_getsig(struct uart_softc *);
395 static int oct16550_bus_ioctl(struct uart_softc *, int, intptr_t);
396 static int oct16550_bus_ipend(struct uart_softc *);
397 static int oct16550_bus_param(struct uart_softc *, int, int, int, int);
398 static int oct16550_bus_probe(struct uart_softc *);
399 static int oct16550_bus_receive(struct uart_softc *);
400 static int oct16550_bus_setsig(struct uart_softc *, int);
401 static int oct16550_bus_transmit(struct uart_softc *);
402 static void oct16550_bus_grab(struct uart_softc *);
403 static void oct16550_bus_ungrab(struct uart_softc *);
405 static kobj_method_t oct16550_methods[] = {
406 KOBJMETHOD(uart_attach, oct16550_bus_attach),
407 KOBJMETHOD(uart_detach, oct16550_bus_detach),
408 KOBJMETHOD(uart_flush, oct16550_bus_flush),
409 KOBJMETHOD(uart_getsig, oct16550_bus_getsig),
410 KOBJMETHOD(uart_ioctl, oct16550_bus_ioctl),
411 KOBJMETHOD(uart_ipend, oct16550_bus_ipend),
412 KOBJMETHOD(uart_param, oct16550_bus_param),
413 KOBJMETHOD(uart_probe, oct16550_bus_probe),
414 KOBJMETHOD(uart_receive, oct16550_bus_receive),
415 KOBJMETHOD(uart_setsig, oct16550_bus_setsig),
416 KOBJMETHOD(uart_transmit, oct16550_bus_transmit),
417 KOBJMETHOD(uart_grab, oct16550_bus_grab),
418 KOBJMETHOD(uart_ungrab, oct16550_bus_ungrab),
422 struct uart_class uart_oct16550_class = {
425 sizeof(struct oct16550_softc),
426 .uc_ops = &uart_oct16550_ops,
432 #define SIGCHG(c, i, s, d) \
434 i |= (i & s) ? s : s | d; \
436 i = (i & s) ? (i & ~s) | d : i; \
440 oct16550_bus_attach (struct uart_softc *sc)
442 struct oct16550_softc *oct16550 = (struct oct16550_softc*)sc;
443 struct uart_bas *bas;
446 unit = device_get_unit(sc->sc_dev);
449 oct16550_drain(bas, UART_DRAIN_TRANSMITTER);
450 oct16550->mcr = uart_getreg(bas, REG_MCR);
451 oct16550->fcr = FCR_ENABLE | FCR_RX_HIGH;
452 uart_setreg(bas, REG_FCR, oct16550->fcr);
454 oct16550_bus_flush(sc, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
456 if (oct16550->mcr & MCR_DTR)
457 sc->sc_hwsig |= SER_DTR;
458 if (oct16550->mcr & MCR_RTS)
459 sc->sc_hwsig |= SER_RTS;
460 oct16550_bus_getsig(sc);
462 oct16550_clrint(bas);
463 oct16550->ier = uart_getreg(bas, REG_IER) & 0xf0;
464 oct16550->ier |= IER_EMSC | IER_ERLS | IER_ERXRDY;
465 uart_setreg(bas, REG_IER, oct16550->ier);
472 oct16550_bus_detach (struct uart_softc *sc)
474 struct uart_bas *bas;
478 ier = uart_getreg(bas, REG_IER) & 0xf0;
479 uart_setreg(bas, REG_IER, ier);
481 oct16550_clrint(bas);
486 oct16550_bus_flush (struct uart_softc *sc, int what)
488 struct oct16550_softc *oct16550 = (struct oct16550_softc*)sc;
489 struct uart_bas *bas;
493 uart_lock(sc->sc_hwmtx);
494 if (sc->sc_rxfifosz > 1) {
495 oct16550_flush(bas, what);
496 uart_setreg(bas, REG_FCR, oct16550->fcr);
500 error = oct16550_drain(bas, what);
501 uart_unlock(sc->sc_hwmtx);
506 oct16550_bus_getsig (struct uart_softc *sc)
508 uint32_t new, old, sig;
514 uart_lock(sc->sc_hwmtx);
515 msr = uart_getreg(&sc->sc_bas, REG_MSR);
516 uart_unlock(sc->sc_hwmtx);
517 SIGCHG(msr & MSR_DSR, sig, SER_DSR, SER_DDSR);
518 SIGCHG(msr & MSR_CTS, sig, SER_CTS, SER_DCTS);
519 SIGCHG(msr & MSR_DCD, sig, SER_DCD, SER_DDCD);
520 SIGCHG(msr & MSR_RI, sig, SER_RI, SER_DRI);
521 new = sig & ~SER_MASK_DELTA;
522 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
527 oct16550_bus_ioctl (struct uart_softc *sc, int request, intptr_t data)
529 struct uart_bas *bas;
530 int baudrate, divisor, error;
535 uart_lock(sc->sc_hwmtx);
537 case UART_IOCTL_BREAK:
538 lcr = uart_getreg(bas, REG_LCR);
543 uart_setreg(bas, REG_LCR, lcr);
546 case UART_IOCTL_IFLOW:
547 lcr = uart_getreg(bas, REG_LCR);
549 uart_setreg(bas, REG_LCR, 0xbf);
551 efr = uart_getreg(bas, REG_EFR);
556 uart_setreg(bas, REG_EFR, efr);
558 uart_setreg(bas, REG_LCR, lcr);
561 case UART_IOCTL_OFLOW:
562 lcr = uart_getreg(bas, REG_LCR);
564 uart_setreg(bas, REG_LCR, 0xbf);
566 efr = uart_getreg(bas, REG_EFR);
571 uart_setreg(bas, REG_EFR, efr);
573 uart_setreg(bas, REG_LCR, lcr);
576 case UART_IOCTL_BAUD:
577 lcr = uart_getreg(bas, REG_LCR);
578 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
580 divisor = uart_getreg(bas, REG_DLL) |
581 (uart_getreg(bas, REG_DLH) << 8);
583 uart_setreg(bas, REG_LCR, lcr);
585 baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
588 *(int*)data = baudrate;
596 uart_unlock(sc->sc_hwmtx);
601 oct16550_bus_ipend(struct uart_softc *sc)
603 struct uart_bas *bas;
608 uart_lock(sc->sc_hwmtx);
610 iir = uart_getreg(bas, REG_IIR) & IIR_IMASK;
611 if (iir != IIR_NOPEND) {
612 if (iir == IIR_RLS) {
613 lsr = uart_getreg(bas, REG_LSR);
615 ipend |= SER_INT_OVERRUN;
617 ipend |= SER_INT_BREAK;
619 ipend |= SER_INT_RXREADY;
621 } else if (iir == IIR_RXRDY) {
622 ipend |= SER_INT_RXREADY;
624 } else if (iir == IIR_RXTOUT) {
625 ipend |= SER_INT_RXREADY;
627 } else if (iir == IIR_TXRDY) {
628 ipend |= SER_INT_TXIDLE;
630 } else if (iir == IIR_MLSC) {
631 ipend |= SER_INT_SIGCHG;
633 } else if (iir == IIR_BUSY) {
634 (void) uart_getreg(bas, REG_USR);
637 uart_unlock(sc->sc_hwmtx);
643 oct16550_bus_param (struct uart_softc *sc, int baudrate, int databits,
644 int stopbits, int parity)
646 struct uart_bas *bas;
650 uart_lock(sc->sc_hwmtx);
651 error = oct16550_param(bas, baudrate, databits, stopbits, parity);
652 uart_unlock(sc->sc_hwmtx);
657 oct16550_bus_probe (struct uart_softc *sc)
659 struct uart_bas *bas;
663 bas->rclk = uart_oct16550_class.uc_rclk = cvmx_clock_get_rate(CVMX_CLOCK_SCLK);
665 error = oct16550_probe(bas);
670 uart_setreg(bas, REG_MCR, (MCR_DTR | MCR_RTS));
673 * Enable FIFOs. And check that the UART has them. If not, we're
674 * done. Since this is the first time we enable the FIFOs, we reset
677 oct16550_drain(bas, UART_DRAIN_TRANSMITTER);
678 #define ENABLE_OCTEON_FIFO 1
679 #ifdef ENABLE_OCTEON_FIFO
680 uart_setreg(bas, REG_FCR, FCR_ENABLE | FCR_XMT_RST | FCR_RCV_RST);
684 oct16550_flush(bas, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
686 if (device_get_unit(sc->sc_dev)) {
687 device_set_desc(sc->sc_dev, "Octeon-16550 channel 1");
689 device_set_desc(sc->sc_dev, "Octeon-16550 channel 0");
691 #ifdef ENABLE_OCTEON_FIFO
692 sc->sc_rxfifosz = 64;
693 sc->sc_txfifosz = 64;
701 * XXX there are some issues related to hardware flow control and
702 * it's likely that uart(4) is the cause. This basicly needs more
703 * investigation, but we avoid using for hardware flow control
706 /* 16650s or higher have automatic flow control. */
707 if (sc->sc_rxfifosz > 16) {
717 oct16550_bus_receive (struct uart_softc *sc)
719 struct uart_bas *bas;
724 uart_lock(sc->sc_hwmtx);
725 lsr = uart_getreg(bas, REG_LSR);
727 while (lsr & LSR_RXRDY) {
728 if (uart_rx_full(sc)) {
729 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
732 xc = uart_getreg(bas, REG_DATA);
734 xc |= UART_STAT_FRAMERR;
736 xc |= UART_STAT_PARERR;
738 lsr = uart_getreg(bas, REG_LSR);
740 /* Discard everything left in the Rx FIFO. */
742 * First do a dummy read/discard anyway, in case the UART was lying to us.
743 * This problem was seen on board, when IIR said RBR, but LSR said no RXRDY
744 * Results in a stuck ipend loop.
746 (void)uart_getreg(bas, REG_DATA);
747 while (lsr & LSR_RXRDY) {
748 (void)uart_getreg(bas, REG_DATA);
750 lsr = uart_getreg(bas, REG_LSR);
752 uart_unlock(sc->sc_hwmtx);
757 oct16550_bus_setsig (struct uart_softc *sc, int sig)
759 struct oct16550_softc *oct16550 = (struct oct16550_softc*)sc;
760 struct uart_bas *bas;
767 if (sig & SER_DDTR) {
768 SIGCHG(sig & SER_DTR, new, SER_DTR,
771 if (sig & SER_DRTS) {
772 SIGCHG(sig & SER_RTS, new, SER_RTS,
775 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
776 uart_lock(sc->sc_hwmtx);
777 oct16550->mcr &= ~(MCR_DTR|MCR_RTS);
779 oct16550->mcr |= MCR_DTR;
781 oct16550->mcr |= MCR_RTS;
782 uart_setreg(bas, REG_MCR, oct16550->mcr);
784 uart_unlock(sc->sc_hwmtx);
789 oct16550_bus_transmit (struct uart_softc *sc)
791 struct oct16550_softc *oct16550 = (struct oct16550_softc*)sc;
792 struct uart_bas *bas;
796 uart_lock(sc->sc_hwmtx);
797 #ifdef NO_UART_INTERRUPTS
798 for (i = 0; i < sc->sc_txdatasz; i++) {
799 oct16550_putc(bas, sc->sc_txbuf[i]);
803 oct16550_wait_txhr_empty(bas, 100, oct16550_delay(bas));
804 uart_setreg(bas, REG_IER, oct16550->ier | IER_ETXRDY);
807 for (i = 0; i < sc->sc_txdatasz; i++) {
808 uart_setreg(bas, REG_DATA, sc->sc_txbuf[i]);
813 uart_unlock(sc->sc_hwmtx);
818 oct16550_bus_grab(struct uart_softc *sc)
820 struct uart_bas *bas = &sc->sc_bas;
823 * turn off all interrupts to enter polling mode. Leave the
824 * saved mask alone. We'll restore whatever it was in ungrab.
825 * All pending interupt signals are reset when IER is set to 0.
827 uart_lock(sc->sc_hwmtx);
828 uart_setreg(bas, REG_IER, 0);
830 uart_unlock(sc->sc_hwmtx);
834 oct16550_bus_ungrab(struct uart_softc *sc)
836 struct oct16550_softc *oct16550 = (struct oct16550_softc*)sc;
837 struct uart_bas *bas = &sc->sc_bas;
840 * Restore previous interrupt mask
842 uart_lock(sc->sc_hwmtx);
843 uart_setreg(bas, REG_IER, oct16550->ier);
845 uart_unlock(sc->sc_hwmtx);