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.
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
62 #include <sys/param.h>
63 #include <sys/systm.h>
66 #include <machine/bus.h>
67 #include <machine/pcpu.h>
69 #include <dev/uart/uart.h>
70 #include <dev/uart/uart_cpu.h>
71 #include <dev/uart/uart_bus.h>
73 #include <dev/ic/ns16550.h>
75 #include <mips/cavium/octeon_pcmap_regs.h>
77 #include <contrib/octeon-sdk/cvmx.h>
82 * Clear pending interrupts. THRE is cleared by reading IIR. Data
83 * that may have been received gets lost here.
86 oct16550_clrint (struct uart_bas *bas)
90 iir = uart_getreg(bas, REG_IIR);
91 while ((iir & IIR_NOPEND) == 0) {
94 (void)uart_getreg(bas, REG_LSR);
95 else if (iir == IIR_RXRDY || iir == IIR_RXTOUT)
96 (void)uart_getreg(bas, REG_DATA);
97 else if (iir == IIR_MLSC)
98 (void)uart_getreg(bas, REG_MSR);
99 else if (iir == IIR_BUSY)
100 (void) uart_getreg(bas, REG_USR);
102 iir = uart_getreg(bas, REG_IIR);
106 static int delay_changed = 1;
109 oct16550_delay (struct uart_bas *bas)
113 static int delay = 0;
115 if (!delay_changed) return delay;
117 lcr = uart_getreg(bas, REG_LCR);
118 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
120 divisor = uart_getreg(bas, REG_DLL) | (uart_getreg(bas, REG_DLH) << 8);
122 uart_setreg(bas, REG_LCR, lcr);
126 return 10; /* return an approx delay value */
128 /* 1/10th the time to transmit 1 character (estimate). */
130 return (16000000 * divisor / bas->rclk);
131 return (16000 * divisor / (bas->rclk / 1000));
136 oct16550_divisor (int rclk, int baudrate)
138 int actual_baud, divisor;
144 divisor = (rclk / (baudrate << 3) + 1) >> 1;
145 if (divisor == 0 || divisor >= 65536)
147 actual_baud = rclk / (divisor << 4);
149 /* 10 times error in percent: */
150 error = ((actual_baud - baudrate) * 2000 / baudrate + 1) >> 1;
152 /* 3.0% maximum error tolerance: */
153 if (error < -30 || error > 30)
160 oct16550_drain (struct uart_bas *bas, int what)
164 delay = oct16550_delay(bas);
166 if (what & UART_DRAIN_TRANSMITTER) {
168 * Pick an arbitrary high limit to avoid getting stuck in
169 * an infinite loop when the hardware is broken. Make the
170 * limit high enough to handle large FIFOs.
172 limit = 10*10*10*1024;
173 while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
176 /* printf("oct16550: transmitter appears stuck... "); */
181 if (what & UART_DRAIN_RECEIVER) {
183 * Pick an arbitrary high limit to avoid getting stuck in
184 * an infinite loop when the hardware is broken. Make the
185 * limit high enough to handle large FIFOs and integrated
186 * UARTs. The HP rx2600 for example has 3 UARTs on the
187 * management board that tend to get a lot of data send
188 * to it when the UART is first activated.
191 while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) && --limit) {
192 (void)uart_getreg(bas, REG_DATA);
197 /* printf("oct16550: receiver appears broken... "); */
206 * We can only flush UARTs with FIFOs. UARTs without FIFOs should be
207 * drained. WARNING: this function clobbers the FIFO setting!
210 oct16550_flush (struct uart_bas *bas, int what)
215 if (what & UART_FLUSH_TRANSMITTER)
217 if (what & UART_FLUSH_RECEIVER)
219 uart_setreg(bas, REG_FCR, fcr);
224 oct16550_param (struct uart_bas *bas, int baudrate, int databits, int stopbits,
233 else if (databits == 7)
235 else if (databits == 6)
245 divisor = oct16550_divisor(bas->rclk, baudrate);
248 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
250 uart_setreg(bas, REG_DLL, divisor & 0xff);
251 uart_setreg(bas, REG_DLH, (divisor >> 8) & 0xff);
256 /* Set LCR and clear DLAB. */
257 uart_setreg(bas, REG_LCR, lcr);
263 * Low-level UART interface.
265 static int oct16550_probe(struct uart_bas *bas);
266 static void oct16550_init(struct uart_bas *bas, int, int, int, int);
267 static void oct16550_term(struct uart_bas *bas);
268 static void oct16550_putc(struct uart_bas *bas, int);
269 static int oct16550_rxready(struct uart_bas *bas);
270 static int oct16550_getc(struct uart_bas *bas, struct mtx *);
272 struct uart_ops uart_oct16550_ops = {
273 .probe = oct16550_probe,
274 .init = oct16550_init,
275 .term = oct16550_term,
276 .putc = oct16550_putc,
277 .rxready = oct16550_rxready,
278 .getc = oct16550_getc,
282 oct16550_probe (struct uart_bas *bas)
286 /* Check known 0 bits that don't depend on DLAB. */
287 val = uart_getreg(bas, REG_IIR);
290 val = uart_getreg(bas, REG_MCR);
293 val = uart_getreg(bas, REG_USR);
300 oct16550_init (struct uart_bas *bas, int baudrate, int databits, int stopbits,
305 oct16550_param(bas, baudrate, databits, stopbits, parity);
307 /* Disable all interrupt sources. */
308 ier = uart_getreg(bas, REG_IER) & 0x0;
309 uart_setreg(bas, REG_IER, ier);
312 /* Disable the FIFO (if present). */
313 // uart_setreg(bas, REG_FCR, 0);
317 uart_setreg(bas, REG_MCR, MCR_RTS | MCR_DTR);
320 oct16550_clrint(bas);
324 oct16550_term (struct uart_bas *bas)
327 /* Clear RTS & DTR. */
328 uart_setreg(bas, REG_MCR, 0);
332 static inline void oct16550_wait_txhr_empty (struct uart_bas *bas, int limit, int delay)
334 while (((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0) &&
335 ((uart_getreg(bas, REG_USR) & USR_TXFIFO_NOTFULL) == 0))
340 oct16550_putc (struct uart_bas *bas, int c)
344 /* 1/10th the time to transmit 1 character (estimate). */
345 delay = oct16550_delay(bas);
346 oct16550_wait_txhr_empty(bas, 100, delay);
347 uart_setreg(bas, REG_DATA, c);
349 oct16550_wait_txhr_empty(bas, 100, delay);
353 oct16550_rxready (struct uart_bas *bas)
356 return ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) != 0 ? 1 : 0);
360 oct16550_getc (struct uart_bas *bas, struct mtx *hwmtx)
366 /* 1/10th the time to transmit 1 character (estimate). */
367 delay = oct16550_delay(bas);
369 while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) == 0) {
375 c = uart_getreg(bas, REG_DATA);
383 * High-level UART interface.
385 struct oct16550_softc {
386 struct uart_softc base;
392 static int oct16550_bus_attach(struct uart_softc *);
393 static int oct16550_bus_detach(struct uart_softc *);
394 static int oct16550_bus_flush(struct uart_softc *, int);
395 static int oct16550_bus_getsig(struct uart_softc *);
396 static int oct16550_bus_ioctl(struct uart_softc *, int, intptr_t);
397 static int oct16550_bus_ipend(struct uart_softc *);
398 static int oct16550_bus_param(struct uart_softc *, int, int, int, int);
399 static int oct16550_bus_probe(struct uart_softc *);
400 static int oct16550_bus_receive(struct uart_softc *);
401 static int oct16550_bus_setsig(struct uart_softc *, int);
402 static int oct16550_bus_transmit(struct uart_softc *);
403 static void oct16550_bus_grab(struct uart_softc *);
404 static void oct16550_bus_ungrab(struct uart_softc *);
406 static kobj_method_t oct16550_methods[] = {
407 KOBJMETHOD(uart_attach, oct16550_bus_attach),
408 KOBJMETHOD(uart_detach, oct16550_bus_detach),
409 KOBJMETHOD(uart_flush, oct16550_bus_flush),
410 KOBJMETHOD(uart_getsig, oct16550_bus_getsig),
411 KOBJMETHOD(uart_ioctl, oct16550_bus_ioctl),
412 KOBJMETHOD(uart_ipend, oct16550_bus_ipend),
413 KOBJMETHOD(uart_param, oct16550_bus_param),
414 KOBJMETHOD(uart_probe, oct16550_bus_probe),
415 KOBJMETHOD(uart_receive, oct16550_bus_receive),
416 KOBJMETHOD(uart_setsig, oct16550_bus_setsig),
417 KOBJMETHOD(uart_transmit, oct16550_bus_transmit),
418 KOBJMETHOD(uart_grab, oct16550_bus_grab),
419 KOBJMETHOD(uart_ungrab, oct16550_bus_ungrab),
423 struct uart_class uart_oct16550_class = {
426 sizeof(struct oct16550_softc),
427 .uc_ops = &uart_oct16550_ops,
433 #define SIGCHG(c, i, s, d) \
435 i |= (i & s) ? s : s | d; \
437 i = (i & s) ? (i & ~s) | d : i; \
441 oct16550_bus_attach (struct uart_softc *sc)
443 struct oct16550_softc *oct16550 = (struct oct16550_softc*)sc;
444 struct uart_bas *bas;
447 unit = device_get_unit(sc->sc_dev);
450 oct16550_drain(bas, UART_DRAIN_TRANSMITTER);
451 oct16550->mcr = uart_getreg(bas, REG_MCR);
452 oct16550->fcr = FCR_ENABLE | FCR_RX_HIGH;
453 uart_setreg(bas, REG_FCR, oct16550->fcr);
455 oct16550_bus_flush(sc, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
457 if (oct16550->mcr & MCR_DTR)
458 sc->sc_hwsig |= SER_DTR;
459 if (oct16550->mcr & MCR_RTS)
460 sc->sc_hwsig |= SER_RTS;
461 oct16550_bus_getsig(sc);
463 oct16550_clrint(bas);
464 oct16550->ier = uart_getreg(bas, REG_IER) & 0xf0;
465 oct16550->ier |= IER_EMSC | IER_ERLS | IER_ERXRDY;
466 uart_setreg(bas, REG_IER, oct16550->ier);
473 oct16550_bus_detach (struct uart_softc *sc)
475 struct uart_bas *bas;
479 ier = uart_getreg(bas, REG_IER) & 0xf0;
480 uart_setreg(bas, REG_IER, ier);
482 oct16550_clrint(bas);
487 oct16550_bus_flush (struct uart_softc *sc, int what)
489 struct oct16550_softc *oct16550 = (struct oct16550_softc*)sc;
490 struct uart_bas *bas;
494 uart_lock(sc->sc_hwmtx);
495 if (sc->sc_rxfifosz > 1) {
496 oct16550_flush(bas, what);
497 uart_setreg(bas, REG_FCR, oct16550->fcr);
501 error = oct16550_drain(bas, what);
502 uart_unlock(sc->sc_hwmtx);
507 oct16550_bus_getsig (struct uart_softc *sc)
509 uint32_t new, old, sig;
515 uart_lock(sc->sc_hwmtx);
516 msr = uart_getreg(&sc->sc_bas, REG_MSR);
517 uart_unlock(sc->sc_hwmtx);
518 SIGCHG(msr & MSR_DSR, sig, SER_DSR, SER_DDSR);
519 SIGCHG(msr & MSR_CTS, sig, SER_CTS, SER_DCTS);
520 SIGCHG(msr & MSR_DCD, sig, SER_DCD, SER_DDCD);
521 SIGCHG(msr & MSR_RI, sig, SER_RI, SER_DRI);
522 new = sig & ~SER_MASK_DELTA;
523 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
528 oct16550_bus_ioctl (struct uart_softc *sc, int request, intptr_t data)
530 struct uart_bas *bas;
531 int baudrate, divisor, error;
536 uart_lock(sc->sc_hwmtx);
538 case UART_IOCTL_BREAK:
539 lcr = uart_getreg(bas, REG_LCR);
544 uart_setreg(bas, REG_LCR, lcr);
547 case UART_IOCTL_IFLOW:
548 lcr = uart_getreg(bas, REG_LCR);
550 uart_setreg(bas, REG_LCR, 0xbf);
552 efr = uart_getreg(bas, REG_EFR);
557 uart_setreg(bas, REG_EFR, efr);
559 uart_setreg(bas, REG_LCR, lcr);
562 case UART_IOCTL_OFLOW:
563 lcr = uart_getreg(bas, REG_LCR);
565 uart_setreg(bas, REG_LCR, 0xbf);
567 efr = uart_getreg(bas, REG_EFR);
572 uart_setreg(bas, REG_EFR, efr);
574 uart_setreg(bas, REG_LCR, lcr);
577 case UART_IOCTL_BAUD:
578 lcr = uart_getreg(bas, REG_LCR);
579 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
581 divisor = uart_getreg(bas, REG_DLL) |
582 (uart_getreg(bas, REG_DLH) << 8);
584 uart_setreg(bas, REG_LCR, lcr);
586 baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
589 *(int*)data = baudrate;
597 uart_unlock(sc->sc_hwmtx);
603 oct16550_bus_ipend(struct uart_softc *sc)
605 struct uart_bas *bas;
610 uart_lock(sc->sc_hwmtx);
612 iir = uart_getreg(bas, REG_IIR) & IIR_IMASK;
613 if (iir != IIR_NOPEND) {
615 if (iir == IIR_RLS) {
616 lsr = uart_getreg(bas, REG_LSR);
618 ipend |= SER_INT_OVERRUN;
620 ipend |= SER_INT_BREAK;
622 ipend |= SER_INT_RXREADY;
624 } else if (iir == IIR_RXRDY) {
625 ipend |= SER_INT_RXREADY;
627 } else if (iir == IIR_RXTOUT) {
628 ipend |= SER_INT_RXREADY;
630 } else if (iir == IIR_TXRDY) {
631 ipend |= SER_INT_TXIDLE;
633 } else if (iir == IIR_MLSC) {
634 ipend |= SER_INT_SIGCHG;
636 } else if (iir == IIR_BUSY) {
637 (void) uart_getreg(bas, REG_USR);
640 uart_unlock(sc->sc_hwmtx);
646 oct16550_bus_param (struct uart_softc *sc, int baudrate, int databits,
647 int stopbits, int parity)
649 struct uart_bas *bas;
653 uart_lock(sc->sc_hwmtx);
654 error = oct16550_param(bas, baudrate, databits, stopbits, parity);
655 uart_unlock(sc->sc_hwmtx);
660 oct16550_bus_probe (struct uart_softc *sc)
662 struct uart_bas *bas;
666 bas->rclk = uart_oct16550_class.uc_rclk = cvmx_clock_get_rate(CVMX_CLOCK_SCLK);
668 error = oct16550_probe(bas);
673 uart_setreg(bas, REG_MCR, (MCR_DTR | MCR_RTS));
676 * Enable FIFOs. And check that the UART has them. If not, we're
677 * done. Since this is the first time we enable the FIFOs, we reset
680 oct16550_drain(bas, UART_DRAIN_TRANSMITTER);
681 #define ENABLE_OCTEON_FIFO 1
682 #ifdef ENABLE_OCTEON_FIFO
683 uart_setreg(bas, REG_FCR, FCR_ENABLE | FCR_XMT_RST | FCR_RCV_RST);
687 oct16550_flush(bas, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
689 if (device_get_unit(sc->sc_dev)) {
690 device_set_desc(sc->sc_dev, "Octeon-16550 channel 1");
692 device_set_desc(sc->sc_dev, "Octeon-16550 channel 0");
694 #ifdef ENABLE_OCTEON_FIFO
695 sc->sc_rxfifosz = 64;
696 sc->sc_txfifosz = 64;
705 * XXX there are some issues related to hardware flow control and
706 * it's likely that uart(4) is the cause. This basicly needs more
707 * investigation, but we avoid using for hardware flow control
710 /* 16650s or higher have automatic flow control. */
711 if (sc->sc_rxfifosz > 16) {
721 oct16550_bus_receive (struct uart_softc *sc)
723 struct uart_bas *bas;
728 uart_lock(sc->sc_hwmtx);
729 lsr = uart_getreg(bas, REG_LSR);
731 while (lsr & LSR_RXRDY) {
732 if (uart_rx_full(sc)) {
733 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
736 xc = uart_getreg(bas, REG_DATA);
738 xc |= UART_STAT_FRAMERR;
740 xc |= UART_STAT_PARERR;
742 lsr = uart_getreg(bas, REG_LSR);
744 /* Discard everything left in the Rx FIFO. */
746 * First do a dummy read/discard anyway, in case the UART was lying to us.
747 * This problem was seen on board, when IIR said RBR, but LSR said no RXRDY
748 * Results in a stuck ipend loop.
750 (void)uart_getreg(bas, REG_DATA);
751 while (lsr & LSR_RXRDY) {
752 (void)uart_getreg(bas, REG_DATA);
754 lsr = uart_getreg(bas, REG_LSR);
756 uart_unlock(sc->sc_hwmtx);
761 oct16550_bus_setsig (struct uart_softc *sc, int sig)
763 struct oct16550_softc *oct16550 = (struct oct16550_softc*)sc;
764 struct uart_bas *bas;
771 if (sig & SER_DDTR) {
772 SIGCHG(sig & SER_DTR, new, SER_DTR,
775 if (sig & SER_DRTS) {
776 SIGCHG(sig & SER_RTS, new, SER_RTS,
779 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
780 uart_lock(sc->sc_hwmtx);
781 oct16550->mcr &= ~(MCR_DTR|MCR_RTS);
783 oct16550->mcr |= MCR_DTR;
785 oct16550->mcr |= MCR_RTS;
786 uart_setreg(bas, REG_MCR, oct16550->mcr);
788 uart_unlock(sc->sc_hwmtx);
793 oct16550_bus_transmit (struct uart_softc *sc)
795 struct oct16550_softc *oct16550 = (struct oct16550_softc*)sc;
796 struct uart_bas *bas;
800 uart_lock(sc->sc_hwmtx);
801 #ifdef NO_UART_INTERRUPTS
802 for (i = 0; i < sc->sc_txdatasz; i++) {
803 oct16550_putc(bas, sc->sc_txbuf[i]);
807 oct16550_wait_txhr_empty(bas, 100, oct16550_delay(bas));
808 uart_setreg(bas, REG_IER, oct16550->ier | IER_ETXRDY);
811 for (i = 0; i < sc->sc_txdatasz; i++) {
812 uart_setreg(bas, REG_DATA, sc->sc_txbuf[i]);
817 uart_unlock(sc->sc_hwmtx);
822 oct16550_bus_grab(struct uart_softc *sc)
824 struct uart_bas *bas = &sc->sc_bas;
827 * turn off all interrupts to enter polling mode. Leave the
828 * saved mask alone. We'll restore whatever it was in ungrab.
829 * All pending interupt signals are reset when IER is set to 0.
831 uart_lock(sc->sc_hwmtx);
832 uart_setreg(bas, REG_IER, 0);
834 uart_unlock(sc->sc_hwmtx);
838 oct16550_bus_ungrab(struct uart_softc *sc)
840 struct oct16550_softc *oct16550 = (struct oct16550_softc*)sc;
841 struct uart_bas *bas = &sc->sc_bas;
844 * Restore previous interrupt mask
846 uart_lock(sc->sc_hwmtx);
847 uart_setreg(bas, REG_IER, oct16550->ier);
849 uart_unlock(sc->sc_hwmtx);