2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Oleksandr Rybalko under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
41 #include <machine/bus.h>
43 #include <dev/uart/uart.h>
44 #include <dev/uart/uart_cpu.h>
45 #include <dev/uart/uart_cpu_fdt.h>
46 #include <dev/uart/uart_bus.h>
47 #include <dev/uart/uart_dev_imx.h>
49 #if defined(__aarch64__)
50 #define IMX_ENABLE_CLOCKS
53 #ifdef IMX_ENABLE_CLOCKS
54 #include <dev/extres/clk/clk.h>
59 #include <arm/freescale/imx/imx_ccmvar.h>
62 * The hardare FIFOs are 32 bytes. We want an interrupt when there are 24 bytes
63 * available to read or space for 24 more bytes to write. While 8 bytes of
64 * slack before over/underrun might seem excessive, the hardware can run at
65 * 5mbps, which means 2uS per char, so at full speed 8 bytes provides only 16uS
66 * to get into the interrupt handler and service the fifo.
69 #define IMX_RXFIFO_LEVEL 24
70 #define IMX_TXFIFO_LEVEL 24
73 * Low-level UART interface.
75 static int imx_uart_probe(struct uart_bas *bas);
76 static void imx_uart_init(struct uart_bas *bas, int, int, int, int);
77 static void imx_uart_term(struct uart_bas *bas);
78 static void imx_uart_putc(struct uart_bas *bas, int);
79 static int imx_uart_rxready(struct uart_bas *bas);
80 static int imx_uart_getc(struct uart_bas *bas, struct mtx *);
82 static struct uart_ops uart_imx_uart_ops = {
83 .probe = imx_uart_probe,
84 .init = imx_uart_init,
85 .term = imx_uart_term,
86 .putc = imx_uart_putc,
87 .rxready = imx_uart_rxready,
88 .getc = imx_uart_getc,
91 #if 0 /* Handy when debugging. */
93 dumpregs(struct uart_bas *bas, const char * msg)
98 printf("%s bsh 0x%08lx UCR1 0x%08x UCR2 0x%08x "
99 "UCR3 0x%08x UCR4 0x%08x USR1 0x%08x USR2 0x%08x\n",
101 GETREG(bas, REG(UCR1)), GETREG(bas, REG(UCR2)),
102 GETREG(bas, REG(UCR3)), GETREG(bas, REG(UCR4)),
103 GETREG(bas, REG(USR1)), GETREG(bas, REG(USR2)));
108 imx_uart_probe(struct uart_bas *bas)
115 imx_uart_getbaud(struct uart_bas *bas)
117 uint32_t rate, ubir, ubmr;
118 u_int baud, blo, bhi, i;
119 static const u_int predivs[] = {6, 5, 4, 3, 2, 1, 7, 1};
120 static const u_int std_rates[] = {
121 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600
125 * Get the baud rate the hardware is programmed for, then search the
126 * table of standard baud rates for a number that's within 3% of the
127 * actual rate the hardware is programmed for. It's more comforting to
128 * see that your console is running at 115200 than 114942. Note that
129 * here we cannot make a simplifying assumption that the predivider and
130 * numerator are 1 (like we do when setting the baud rate), because we
131 * don't know what u-boot might have set up.
133 i = (GETREG(bas, REG(UFCR)) & IMXUART_UFCR_RFDIV_MASK) >>
134 IMXUART_UFCR_RFDIV_SHIFT;
135 rate = bas->rclk / predivs[i];
136 ubir = GETREG(bas, REG(UBIR)) + 1;
137 ubmr = GETREG(bas, REG(UBMR)) + 1;
138 baud = ((rate / 16 ) * ubir) / ubmr;
140 blo = (baud * 100) / 103;
141 bhi = (baud * 100) / 97;
142 for (i = 0; i < nitems(std_rates); i++) {
144 if (rate >= blo && rate <= bhi) {
154 imx_uart_init(struct uart_bas *bas, int baudrate, int databits,
155 int stopbits, int parity)
157 uint32_t baseclk, reg;
159 /* Enable the device and the RX/TX channels. */
160 SET(bas, REG(UCR1), FLD(UCR1, UARTEN));
161 SET(bas, REG(UCR2), FLD(UCR2, RXEN) | FLD(UCR2, TXEN));
169 ENA(bas, UCR2, STPB);
171 DIS(bas, UCR2, STPB);
174 case UART_PARITY_ODD:
175 DIS(bas, UCR2, PROE);
176 ENA(bas, UCR2, PREN);
178 case UART_PARITY_EVEN:
179 ENA(bas, UCR2, PROE);
180 ENA(bas, UCR2, PREN);
182 case UART_PARITY_MARK:
183 case UART_PARITY_SPACE:
184 /* FALLTHROUGH: Hardware doesn't support mark/space. */
185 case UART_PARITY_NONE:
187 DIS(bas, UCR2, PREN);
192 * The hardware has an extremely flexible baud clock: it allows setting
193 * both the numerator and denominator of the divider, as well as a
194 * separate pre-divider. We simplify the problem of coming up with a
195 * workable pair of numbers by assuming a pre-divider and numerator of
196 * one because our base clock is so fast we can reach virtually any
197 * reasonable speed with a simple divisor. The numerator value actually
198 * includes the 16x over-sampling (so a value of 16 means divide by 1);
199 * the register value is the numerator-1, so we have a hard-coded 15.
200 * Note that a quirk of the hardware requires that both UBIR and UBMR be
201 * set back to back in order for the change to take effect.
203 if ((baudrate > 0) && (bas->rclk != 0)) {
205 reg = GETREG(bas, REG(UFCR));
206 reg = (reg & ~IMXUART_UFCR_RFDIV_MASK) | IMXUART_UFCR_RFDIV_DIV1;
207 SETREG(bas, REG(UFCR), reg);
208 SETREG(bas, REG(UBIR), 15);
209 SETREG(bas, REG(UBMR), (baseclk / baudrate) - 1);
213 * Program the tx lowater and rx hiwater levels at which fifo-service
214 * interrupts are signaled. The tx value is interpetted as "when there
215 * are only this many bytes remaining" (not "this many free").
217 reg = GETREG(bas, REG(UFCR));
218 reg &= ~(IMXUART_UFCR_TXTL_MASK | IMXUART_UFCR_RXTL_MASK);
219 reg |= (IMX_FIFOSZ - IMX_TXFIFO_LEVEL) << IMXUART_UFCR_TXTL_SHIFT;
220 reg |= IMX_RXFIFO_LEVEL << IMXUART_UFCR_RXTL_SHIFT;
221 SETREG(bas, REG(UFCR), reg);
225 imx_uart_term(struct uart_bas *bas)
231 imx_uart_putc(struct uart_bas *bas, int c)
234 while (!(IS(bas, USR1, TRDY)))
236 SETREG(bas, REG(UTXD), c);
240 imx_uart_rxready(struct uart_bas *bas)
243 return ((IS(bas, USR2, RDR)) ? 1 : 0);
247 imx_uart_getc(struct uart_bas *bas, struct mtx *hwmtx)
252 while (!(IS(bas, USR2, RDR)))
255 c = GETREG(bas, REG(URXD));
258 if (c & FLD(URXD, BRK)) {
267 * High-level UART interface.
269 struct imx_uart_softc {
270 struct uart_softc base;
273 static int imx_uart_bus_attach(struct uart_softc *);
274 static int imx_uart_bus_detach(struct uart_softc *);
275 static int imx_uart_bus_flush(struct uart_softc *, int);
276 static int imx_uart_bus_getsig(struct uart_softc *);
277 static int imx_uart_bus_ioctl(struct uart_softc *, int, intptr_t);
278 static int imx_uart_bus_ipend(struct uart_softc *);
279 static int imx_uart_bus_param(struct uart_softc *, int, int, int, int);
280 static int imx_uart_bus_probe(struct uart_softc *);
281 static int imx_uart_bus_receive(struct uart_softc *);
282 static int imx_uart_bus_setsig(struct uart_softc *, int);
283 static int imx_uart_bus_transmit(struct uart_softc *);
284 static void imx_uart_bus_grab(struct uart_softc *);
285 static void imx_uart_bus_ungrab(struct uart_softc *);
287 static kobj_method_t imx_uart_methods[] = {
288 KOBJMETHOD(uart_attach, imx_uart_bus_attach),
289 KOBJMETHOD(uart_detach, imx_uart_bus_detach),
290 KOBJMETHOD(uart_flush, imx_uart_bus_flush),
291 KOBJMETHOD(uart_getsig, imx_uart_bus_getsig),
292 KOBJMETHOD(uart_ioctl, imx_uart_bus_ioctl),
293 KOBJMETHOD(uart_ipend, imx_uart_bus_ipend),
294 KOBJMETHOD(uart_param, imx_uart_bus_param),
295 KOBJMETHOD(uart_probe, imx_uart_bus_probe),
296 KOBJMETHOD(uart_receive, imx_uart_bus_receive),
297 KOBJMETHOD(uart_setsig, imx_uart_bus_setsig),
298 KOBJMETHOD(uart_transmit, imx_uart_bus_transmit),
299 KOBJMETHOD(uart_grab, imx_uart_bus_grab),
300 KOBJMETHOD(uart_ungrab, imx_uart_bus_ungrab),
304 static struct uart_class uart_imx_class = {
307 sizeof(struct imx_uart_softc),
308 .uc_ops = &uart_imx_uart_ops,
310 .uc_rclk = 24000000, /* TODO: get value from CCM */
314 static struct ofw_compat_data compat_data[] = {
315 {"fsl,imx6q-uart", (uintptr_t)&uart_imx_class},
316 {"fsl,imx53-uart", (uintptr_t)&uart_imx_class},
317 {"fsl,imx51-uart", (uintptr_t)&uart_imx_class},
318 {"fsl,imx31-uart", (uintptr_t)&uart_imx_class},
319 {"fsl,imx27-uart", (uintptr_t)&uart_imx_class},
320 {"fsl,imx25-uart", (uintptr_t)&uart_imx_class},
321 {"fsl,imx21-uart", (uintptr_t)&uart_imx_class},
322 {NULL, (uintptr_t)NULL},
324 UART_FDT_CLASS_AND_DEVICE(compat_data);
326 #define SIGCHG(c, i, s, d) \
328 i |= (i & s) ? s : s | d; \
330 i = (i & s) ? (i & ~s) | d : i; \
333 #ifdef IMX_ENABLE_CLOCKS
335 imx_uart_setup_clocks(struct uart_softc *sc)
337 struct uart_bas *bas;
338 clk_t ipgclk, perclk;
344 if (clk_get_by_ofw_name(sc->sc_dev, 0, "ipg", &ipgclk) != 0)
347 if (clk_get_by_ofw_name(sc->sc_dev, 0, "per", &perclk) != 0) {
351 error = clk_enable(ipgclk);
353 device_printf(sc->sc_dev, "cannot enable ipg clock\n");
357 error = clk_get_freq(perclk, &freq);
359 device_printf(sc->sc_dev, "cannot get frequency\n");
363 bas->rclk = (uint32_t)freq;
370 imx_uart_bus_attach(struct uart_softc *sc)
372 struct uart_bas *bas;
373 struct uart_devinfo *di;
377 #ifdef IMX_ENABLE_CLOCKS
378 int error = imx_uart_setup_clocks(sc);
382 bas->rclk = imx_ccm_uart_hz();
385 if (sc->sc_sysdev != NULL) {
387 imx_uart_init(bas, di->baudrate, di->databits, di->stopbits,
390 imx_uart_init(bas, 115200, 8, 1, 0);
393 (void)imx_uart_bus_getsig(sc);
395 /* Clear all pending interrupts. */
396 SETREG(bas, REG(USR1), 0xffff);
397 SETREG(bas, REG(USR2), 0xffff);
399 DIS(bas, UCR4, DREN);
400 ENA(bas, UCR1, RRDYEN);
401 DIS(bas, UCR1, IDEN);
402 DIS(bas, UCR3, RXDSEN);
403 ENA(bas, UCR2, ATEN);
404 DIS(bas, UCR1, TXMPTYEN);
405 DIS(bas, UCR1, TRDYEN);
406 DIS(bas, UCR4, TCEN);
407 DIS(bas, UCR4, OREN);
408 ENA(bas, UCR4, BKEN);
409 DIS(bas, UCR4, WKEN);
410 DIS(bas, UCR1, ADEN);
411 DIS(bas, UCR3, ACIEN);
412 DIS(bas, UCR2, ESCI);
413 DIS(bas, UCR4, ENIRI);
414 DIS(bas, UCR3, AIRINTEN);
415 DIS(bas, UCR3, AWAKEN);
416 DIS(bas, UCR3, FRAERREN);
417 DIS(bas, UCR3, PARERREN);
418 DIS(bas, UCR1, RTSDEN);
419 DIS(bas, UCR2, RTSEN);
420 DIS(bas, UCR3, DTREN);
423 DIS(bas, UCR3, DTRDEN);
424 ENA(bas, UCR2, IRTS);
425 ENA(bas, UCR3, RXDMUXSEL);
431 imx_uart_bus_detach(struct uart_softc *sc)
434 SETREG(&sc->sc_bas, REG(UCR4), 0);
440 imx_uart_bus_flush(struct uart_softc *sc, int what)
448 imx_uart_bus_getsig(struct uart_softc *sc)
450 uint32_t new, old, sig;
456 uart_lock(sc->sc_hwmtx);
457 bes = GETREG(&sc->sc_bas, REG(USR2));
458 uart_unlock(sc->sc_hwmtx);
459 /* XXX: chip can show delta */
460 SIGCHG(bes & FLD(USR2, DCDIN), sig, SER_DCD, SER_DDCD);
461 new = sig & ~SER_MASK_DELTA;
462 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
468 imx_uart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
470 struct uart_bas *bas;
475 uart_lock(sc->sc_hwmtx);
477 case UART_IOCTL_BREAK:
480 case UART_IOCTL_BAUD:
481 *(u_int*)data = imx_uart_getbaud(bas);
487 uart_unlock(sc->sc_hwmtx);
493 imx_uart_bus_ipend(struct uart_softc *sc)
495 struct uart_bas *bas;
498 uint32_t ucr1, ucr2, ucr4;
503 uart_lock(sc->sc_hwmtx);
505 /* Read pending interrupts */
506 usr1 = GETREG(bas, REG(USR1));
507 usr2 = GETREG(bas, REG(USR2));
509 SETREG(bas, REG(USR1), usr1);
510 SETREG(bas, REG(USR2), usr2);
512 ucr1 = GETREG(bas, REG(UCR1));
513 ucr2 = GETREG(bas, REG(UCR2));
514 ucr4 = GETREG(bas, REG(UCR4));
516 /* If we have reached tx low-water, we can tx some more now. */
517 if ((usr1 & FLD(USR1, TRDY)) && (ucr1 & FLD(UCR1, TRDYEN))) {
518 DIS(bas, UCR1, TRDYEN);
519 ipend |= SER_INT_TXIDLE;
523 * If we have reached the rx high-water, or if there are bytes in the rx
524 * fifo and no new data has arrived for 8 character periods (aging
525 * timer), we have input data to process.
527 if (((usr1 & FLD(USR1, RRDY)) && (ucr1 & FLD(UCR1, RRDYEN))) ||
528 ((usr1 & FLD(USR1, AGTIM)) && (ucr2 & FLD(UCR2, ATEN)))) {
529 DIS(bas, UCR1, RRDYEN);
530 DIS(bas, UCR2, ATEN);
531 ipend |= SER_INT_RXREADY;
534 /* A break can come in at any time, it never gets disabled. */
535 if ((usr2 & FLD(USR2, BRCD)) && (ucr4 & FLD(UCR4, BKEN)))
536 ipend |= SER_INT_BREAK;
538 uart_unlock(sc->sc_hwmtx);
544 imx_uart_bus_param(struct uart_softc *sc, int baudrate, int databits,
545 int stopbits, int parity)
548 uart_lock(sc->sc_hwmtx);
549 imx_uart_init(&sc->sc_bas, baudrate, databits, stopbits, parity);
550 uart_unlock(sc->sc_hwmtx);
555 imx_uart_bus_probe(struct uart_softc *sc)
559 error = imx_uart_probe(&sc->sc_bas);
564 * On input we can read up to the full fifo size at once. On output, we
565 * want to write only as much as the programmed tx low water level,
566 * because that's all we can be certain we have room for in the fifo
567 * when we get a tx-ready interrupt.
569 sc->sc_rxfifosz = IMX_FIFOSZ;
570 sc->sc_txfifosz = IMX_TXFIFO_LEVEL;
572 device_set_desc(sc->sc_dev, "Freescale i.MX UART");
577 imx_uart_bus_receive(struct uart_softc *sc)
579 struct uart_bas *bas;
583 uart_lock(sc->sc_hwmtx);
586 * Empty the rx fifo. We get the RRDY interrupt when IMX_RXFIFO_LEVEL
587 * (the rx high-water level) is reached, but we set sc_rxfifosz to the
588 * full hardware fifo size, so we can safely process however much is
589 * there, not just the highwater size.
591 while (IS(bas, USR2, RDR)) {
592 if (uart_rx_full(sc)) {
593 /* No space left in input buffer */
594 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
597 xc = GETREG(bas, REG(URXD));
598 out = xc & 0x000000ff;
599 if (xc & FLD(URXD, FRMERR))
600 out |= UART_STAT_FRAMERR;
601 if (xc & FLD(URXD, PRERR))
602 out |= UART_STAT_PARERR;
603 if (xc & FLD(URXD, OVRRUN))
604 out |= UART_STAT_OVERRUN;
605 if (xc & FLD(URXD, BRK))
606 out |= UART_STAT_BREAK;
608 uart_rx_put(sc, out);
610 ENA(bas, UCR1, RRDYEN);
611 ENA(bas, UCR2, ATEN);
613 uart_unlock(sc->sc_hwmtx);
618 imx_uart_bus_setsig(struct uart_softc *sc, int sig)
625 imx_uart_bus_transmit(struct uart_softc *sc)
627 struct uart_bas *bas = &sc->sc_bas;
631 uart_lock(sc->sc_hwmtx);
634 * Fill the tx fifo. The uart core puts at most IMX_TXFIFO_LEVEL bytes
635 * into the txbuf (because that's what sc_txfifosz is set to), and
636 * because we got the TRDY (low-water reached) interrupt we know at
637 * least that much space is available in the fifo.
639 for (i = 0; i < sc->sc_txdatasz; i++) {
640 SETREG(bas, REG(UTXD), sc->sc_txbuf[i] & 0xff);
643 ENA(bas, UCR1, TRDYEN);
645 uart_unlock(sc->sc_hwmtx);
651 imx_uart_bus_grab(struct uart_softc *sc)
653 struct uart_bas *bas = &sc->sc_bas;
656 uart_lock(sc->sc_hwmtx);
657 DIS(bas, UCR1, RRDYEN);
658 DIS(bas, UCR2, ATEN);
659 uart_unlock(sc->sc_hwmtx);
663 imx_uart_bus_ungrab(struct uart_softc *sc)
665 struct uart_bas *bas = &sc->sc_bas;
668 uart_lock(sc->sc_hwmtx);
669 ENA(bas, UCR1, RRDYEN);
670 ENA(bas, UCR2, ATEN);
671 uart_unlock(sc->sc_hwmtx);