xref: /freebsd/sys/dev/uart/uart_dev_imx.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
1a2c472e7SAleksandr Rybalko /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*718cf2ccSPedro F. Giffuni  *
4a2c472e7SAleksandr Rybalko  * Copyright (c) 2012 The FreeBSD Foundation
5a2c472e7SAleksandr Rybalko  * All rights reserved.
6a2c472e7SAleksandr Rybalko  *
7a2c472e7SAleksandr Rybalko  * This software was developed by Oleksandr Rybalko under sponsorship
8a2c472e7SAleksandr Rybalko  * from the FreeBSD Foundation.
9a2c472e7SAleksandr Rybalko  *
10a2c472e7SAleksandr Rybalko  * Redistribution and use in source and binary forms, with or without
11a2c472e7SAleksandr Rybalko  * modification, are permitted provided that the following conditions
12a2c472e7SAleksandr Rybalko  * are met:
13a2c472e7SAleksandr Rybalko  * 1.	Redistributions of source code must retain the above copyright
14a2c472e7SAleksandr Rybalko  *	notice, this list of conditions and the following disclaimer.
15a2c472e7SAleksandr Rybalko  * 2.	Redistributions in binary form must reproduce the above copyright
16a2c472e7SAleksandr Rybalko  *	notice, this list of conditions and the following disclaimer in the
17a2c472e7SAleksandr Rybalko  *	documentation and/or other materials provided with the distribution.
18a2c472e7SAleksandr Rybalko  *
19a2c472e7SAleksandr Rybalko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20a2c472e7SAleksandr Rybalko  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a2c472e7SAleksandr Rybalko  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a2c472e7SAleksandr Rybalko  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23a2c472e7SAleksandr Rybalko  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a2c472e7SAleksandr Rybalko  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a2c472e7SAleksandr Rybalko  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a2c472e7SAleksandr Rybalko  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a2c472e7SAleksandr Rybalko  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a2c472e7SAleksandr Rybalko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a2c472e7SAleksandr Rybalko  * SUCH DAMAGE.
30a2c472e7SAleksandr Rybalko  */
31a2c472e7SAleksandr Rybalko 
32a2c472e7SAleksandr Rybalko #include <sys/cdefs.h>
33a2c472e7SAleksandr Rybalko __FBSDID("$FreeBSD$");
34a2c472e7SAleksandr Rybalko 
35a2c472e7SAleksandr Rybalko #include "opt_ddb.h"
36a2c472e7SAleksandr Rybalko 
37a2c472e7SAleksandr Rybalko #include <sys/param.h>
38a2c472e7SAleksandr Rybalko #include <sys/systm.h>
39a2c472e7SAleksandr Rybalko #include <sys/bus.h>
40a2c472e7SAleksandr Rybalko #include <sys/conf.h>
41a2c472e7SAleksandr Rybalko #include <sys/kdb.h>
42a2c472e7SAleksandr Rybalko #include <machine/bus.h>
43a2c472e7SAleksandr Rybalko #include <machine/fdt.h>
44a2c472e7SAleksandr Rybalko 
45a2c472e7SAleksandr Rybalko #include <dev/uart/uart.h>
46a2c472e7SAleksandr Rybalko #include <dev/uart/uart_cpu.h>
473bb693afSIan Lepore #include <dev/uart/uart_cpu_fdt.h>
48a2c472e7SAleksandr Rybalko #include <dev/uart/uart_bus.h>
49a90f1975SIan Lepore #include <dev/uart/uart_dev_imx.h>
50a2c472e7SAleksandr Rybalko #include "uart_if.h"
512d40ec16SIan Lepore 
522d40ec16SIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
532d40ec16SIan Lepore 
54a2c472e7SAleksandr Rybalko /*
550dc54d18SIan Lepore  * The hardare FIFOs are 32 bytes.  We want an interrupt when there are 24 bytes
560dc54d18SIan Lepore  * available to read or space for 24 more bytes to write.  While 8 bytes of
570dc54d18SIan Lepore  * slack before over/underrun might seem excessive, the hardware can run at
580dc54d18SIan Lepore  * 5mbps, which means 2uS per char, so at full speed 8 bytes provides only 16uS
590dc54d18SIan Lepore  * to get into the interrupt handler and service the fifo.
600dc54d18SIan Lepore  */
610dc54d18SIan Lepore #define	IMX_FIFOSZ		32
620dc54d18SIan Lepore #define	IMX_RXFIFO_LEVEL	24
630dc54d18SIan Lepore #define	IMX_TXFIFO_LEVEL	24
640dc54d18SIan Lepore 
650dc54d18SIan Lepore /*
66a2c472e7SAleksandr Rybalko  * Low-level UART interface.
67a2c472e7SAleksandr Rybalko  */
68a2c472e7SAleksandr Rybalko static int imx_uart_probe(struct uart_bas *bas);
69a2c472e7SAleksandr Rybalko static void imx_uart_init(struct uart_bas *bas, int, int, int, int);
70a2c472e7SAleksandr Rybalko static void imx_uart_term(struct uart_bas *bas);
71a2c472e7SAleksandr Rybalko static void imx_uart_putc(struct uart_bas *bas, int);
72a2c472e7SAleksandr Rybalko static int imx_uart_rxready(struct uart_bas *bas);
73a2c472e7SAleksandr Rybalko static int imx_uart_getc(struct uart_bas *bas, struct mtx *);
74a2c472e7SAleksandr Rybalko 
75a2c472e7SAleksandr Rybalko static struct uart_ops uart_imx_uart_ops = {
76a2c472e7SAleksandr Rybalko 	.probe = imx_uart_probe,
77a2c472e7SAleksandr Rybalko 	.init = imx_uart_init,
78a2c472e7SAleksandr Rybalko 	.term = imx_uart_term,
79a2c472e7SAleksandr Rybalko 	.putc = imx_uart_putc,
80a2c472e7SAleksandr Rybalko 	.rxready = imx_uart_rxready,
81a2c472e7SAleksandr Rybalko 	.getc = imx_uart_getc,
82a2c472e7SAleksandr Rybalko };
83a2c472e7SAleksandr Rybalko 
842d40ec16SIan Lepore #if 0 /* Handy when debugging. */
852d40ec16SIan Lepore static void
862d40ec16SIan Lepore dumpregs(struct uart_bas *bas, const char * msg)
872d40ec16SIan Lepore {
882d40ec16SIan Lepore 
892d40ec16SIan Lepore 	if (!bootverbose)
902d40ec16SIan Lepore 		return;
912d40ec16SIan Lepore 	printf("%s bsh 0x%08lx UCR1 0x%08x UCR2 0x%08x "
922d40ec16SIan Lepore 		"UCR3 0x%08x UCR4 0x%08x USR1 0x%08x USR2 0x%08x\n",
932d40ec16SIan Lepore 	    msg, bas->bsh,
942d40ec16SIan Lepore 	    GETREG(bas, REG(UCR1)), GETREG(bas, REG(UCR2)),
952d40ec16SIan Lepore 	    GETREG(bas, REG(UCR3)), GETREG(bas, REG(UCR4)),
962d40ec16SIan Lepore 	    GETREG(bas, REG(USR1)), GETREG(bas, REG(USR2)));
972d40ec16SIan Lepore }
982d40ec16SIan Lepore #endif
992d40ec16SIan Lepore 
100a2c472e7SAleksandr Rybalko static int
101a2c472e7SAleksandr Rybalko imx_uart_probe(struct uart_bas *bas)
102a2c472e7SAleksandr Rybalko {
103a2c472e7SAleksandr Rybalko 
104a2c472e7SAleksandr Rybalko 	return (0);
105a2c472e7SAleksandr Rybalko }
106a2c472e7SAleksandr Rybalko 
107c3f0f284SIan Lepore static u_int
108c3f0f284SIan Lepore imx_uart_getbaud(struct uart_bas *bas)
109c3f0f284SIan Lepore {
110c3f0f284SIan Lepore 	uint32_t rate, ubir, ubmr;
111c3f0f284SIan Lepore 	u_int baud, blo, bhi, i;
112c3f0f284SIan Lepore 	static const u_int predivs[] = {6, 5, 4, 3, 2, 1, 7, 1};
113c3f0f284SIan Lepore 	static const u_int std_rates[] = {
114c3f0f284SIan Lepore 		9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600
115c3f0f284SIan Lepore 	};
116c3f0f284SIan Lepore 
117c3f0f284SIan Lepore 	/*
118c3f0f284SIan Lepore 	 * Get the baud rate the hardware is programmed for, then search the
119c3f0f284SIan Lepore 	 * table of standard baud rates for a number that's within 3% of the
120c3f0f284SIan Lepore 	 * actual rate the hardware is programmed for.  It's more comforting to
121c3f0f284SIan Lepore 	 * see that your console is running at 115200 than 114942.  Note that
122c3f0f284SIan Lepore 	 * here we cannot make a simplifying assumption that the predivider and
123c3f0f284SIan Lepore 	 * numerator are 1 (like we do when setting the baud rate), because we
124c3f0f284SIan Lepore 	 * don't know what u-boot might have set up.
125c3f0f284SIan Lepore 	 */
126c3f0f284SIan Lepore 	i = (GETREG(bas, REG(UFCR)) & IMXUART_UFCR_RFDIV_MASK) >>
127c3f0f284SIan Lepore 	    IMXUART_UFCR_RFDIV_SHIFT;
128c3f0f284SIan Lepore 	rate = imx_ccm_uart_hz() / predivs[i];
129c3f0f284SIan Lepore 	ubir = GETREG(bas, REG(UBIR)) + 1;
130c3f0f284SIan Lepore 	ubmr = GETREG(bas, REG(UBMR)) + 1;
131c3f0f284SIan Lepore 	baud = ((rate / 16 ) * ubir) / ubmr;
132c3f0f284SIan Lepore 
133c3f0f284SIan Lepore 	blo = (baud * 100) / 103;
134c3f0f284SIan Lepore 	bhi = (baud * 100) / 97;
135c3f0f284SIan Lepore 	for (i = 0; i < nitems(std_rates); i++) {
136c3f0f284SIan Lepore 		rate = std_rates[i];
137c3f0f284SIan Lepore 		if (rate >= blo && rate <= bhi) {
138c3f0f284SIan Lepore 			baud = rate;
139c3f0f284SIan Lepore 			break;
140c3f0f284SIan Lepore 		}
141c3f0f284SIan Lepore 	}
142c3f0f284SIan Lepore 
143c3f0f284SIan Lepore 	return (baud);
144c3f0f284SIan Lepore }
145c3f0f284SIan Lepore 
146a2c472e7SAleksandr Rybalko static void
147a2c472e7SAleksandr Rybalko imx_uart_init(struct uart_bas *bas, int baudrate, int databits,
148a2c472e7SAleksandr Rybalko     int stopbits, int parity)
149a2c472e7SAleksandr Rybalko {
1502d40ec16SIan Lepore 	uint32_t baseclk, reg;
151a2c472e7SAleksandr Rybalko 
1522d40ec16SIan Lepore         /* Enable the device and the RX/TX channels. */
1532d40ec16SIan Lepore 	SET(bas, REG(UCR1), FLD(UCR1, UARTEN));
1542d40ec16SIan Lepore 	SET(bas, REG(UCR2), FLD(UCR2, RXEN) | FLD(UCR2, TXEN));
1552d40ec16SIan Lepore 
1562d40ec16SIan Lepore 	if (databits == 7)
1572d40ec16SIan Lepore 		DIS(bas, UCR2, WS);
1582d40ec16SIan Lepore 	else
1592d40ec16SIan Lepore 		ENA(bas, UCR2, WS);
1602d40ec16SIan Lepore 
1612d40ec16SIan Lepore 	if (stopbits == 2)
1622d40ec16SIan Lepore 		ENA(bas, UCR2, STPB);
1632d40ec16SIan Lepore 	else
1642d40ec16SIan Lepore 		DIS(bas, UCR2, STPB);
1652d40ec16SIan Lepore 
1662d40ec16SIan Lepore 	switch (parity) {
1672d40ec16SIan Lepore 	case UART_PARITY_ODD:
1682d40ec16SIan Lepore 		DIS(bas, UCR2, PROE);
1692d40ec16SIan Lepore 		ENA(bas, UCR2, PREN);
1702d40ec16SIan Lepore 		break;
1712d40ec16SIan Lepore 	case UART_PARITY_EVEN:
1722d40ec16SIan Lepore 		ENA(bas, UCR2, PROE);
1732d40ec16SIan Lepore 		ENA(bas, UCR2, PREN);
1742d40ec16SIan Lepore 		break;
1752d40ec16SIan Lepore 	case UART_PARITY_MARK:
1762d40ec16SIan Lepore 	case UART_PARITY_SPACE:
1772d40ec16SIan Lepore                 /* FALLTHROUGH: Hardware doesn't support mark/space. */
1782d40ec16SIan Lepore 	case UART_PARITY_NONE:
1792d40ec16SIan Lepore 	default:
1802d40ec16SIan Lepore 		DIS(bas, UCR2, PREN);
1812d40ec16SIan Lepore 		break;
1822d40ec16SIan Lepore 	}
1832d40ec16SIan Lepore 
1842d40ec16SIan Lepore 	/*
1852d40ec16SIan Lepore 	 * The hardware has an extremely flexible baud clock: it allows setting
1862d40ec16SIan Lepore 	 * both the numerator and denominator of the divider, as well as a
18749d0a4c3SIan Lepore 	 * separate pre-divider.  We simplify the problem of coming up with a
18849d0a4c3SIan Lepore 	 * workable pair of numbers by assuming a pre-divider and numerator of
18949d0a4c3SIan Lepore 	 * one because our base clock is so fast we can reach virtually any
19049d0a4c3SIan Lepore 	 * reasonable speed with a simple divisor.  The numerator value actually
19149d0a4c3SIan Lepore 	 * includes the 16x over-sampling (so a value of 16 means divide by 1);
19249d0a4c3SIan Lepore 	 * the register value is the numerator-1, so we have a hard-coded 15.
19349d0a4c3SIan Lepore 	 * Note that a quirk of the hardware requires that both UBIR and UBMR be
19449d0a4c3SIan Lepore 	 * set back to back in order for the change to take effect.
1952d40ec16SIan Lepore 	 */
1962d40ec16SIan Lepore 	if (baudrate > 0) {
1972d40ec16SIan Lepore 		baseclk = imx_ccm_uart_hz();
1982d40ec16SIan Lepore 		reg = GETREG(bas, REG(UFCR));
1992d40ec16SIan Lepore 		reg = (reg & ~IMXUART_UFCR_RFDIV_MASK) | IMXUART_UFCR_RFDIV_DIV1;
2002d40ec16SIan Lepore 		SETREG(bas, REG(UFCR), reg);
2012d40ec16SIan Lepore 		SETREG(bas, REG(UBIR), 15);
2022d40ec16SIan Lepore 		SETREG(bas, REG(UBMR), (baseclk / baudrate) - 1);
2032d40ec16SIan Lepore 	}
2040dc54d18SIan Lepore 
2050dc54d18SIan Lepore 	/*
2060dc54d18SIan Lepore 	 * Program the tx lowater and rx hiwater levels at which fifo-service
2070dc54d18SIan Lepore 	 * interrupts are signaled.  The tx value is interpetted as "when there
2080dc54d18SIan Lepore 	 * are only this many bytes remaining" (not "this many free").
2090dc54d18SIan Lepore 	 */
2100dc54d18SIan Lepore 	reg = GETREG(bas, REG(UFCR));
2110dc54d18SIan Lepore 	reg &= ~(IMXUART_UFCR_TXTL_MASK | IMXUART_UFCR_RXTL_MASK);
2120dc54d18SIan Lepore 	reg |= (IMX_FIFOSZ - IMX_TXFIFO_LEVEL) << IMXUART_UFCR_TXTL_SHIFT;
2130dc54d18SIan Lepore 	reg |= IMX_RXFIFO_LEVEL << IMXUART_UFCR_RXTL_SHIFT;
2140dc54d18SIan Lepore 	SETREG(bas, REG(UFCR), reg);
215a2c472e7SAleksandr Rybalko }
216a2c472e7SAleksandr Rybalko 
217a2c472e7SAleksandr Rybalko static void
218a2c472e7SAleksandr Rybalko imx_uart_term(struct uart_bas *bas)
219a2c472e7SAleksandr Rybalko {
220a2c472e7SAleksandr Rybalko 
221a2c472e7SAleksandr Rybalko }
222a2c472e7SAleksandr Rybalko 
223a2c472e7SAleksandr Rybalko static void
224a2c472e7SAleksandr Rybalko imx_uart_putc(struct uart_bas *bas, int c)
225a2c472e7SAleksandr Rybalko {
226a2c472e7SAleksandr Rybalko 
2270dc54d18SIan Lepore 	while (!(IS(bas, USR1, TRDY)))
228a2c472e7SAleksandr Rybalko 		;
229a2c472e7SAleksandr Rybalko 	SETREG(bas, REG(UTXD), c);
230a2c472e7SAleksandr Rybalko }
231a2c472e7SAleksandr Rybalko 
232a2c472e7SAleksandr Rybalko static int
233a2c472e7SAleksandr Rybalko imx_uart_rxready(struct uart_bas *bas)
234a2c472e7SAleksandr Rybalko {
235a2c472e7SAleksandr Rybalko 
236a2c472e7SAleksandr Rybalko 	return ((IS(bas, USR2, RDR)) ? 1 : 0);
237a2c472e7SAleksandr Rybalko }
238a2c472e7SAleksandr Rybalko 
239a2c472e7SAleksandr Rybalko static int
240a2c472e7SAleksandr Rybalko imx_uart_getc(struct uart_bas *bas, struct mtx *hwmtx)
241a2c472e7SAleksandr Rybalko {
242a2c472e7SAleksandr Rybalko 	int c;
243a2c472e7SAleksandr Rybalko 
244a2c472e7SAleksandr Rybalko 	uart_lock(hwmtx);
245a2c472e7SAleksandr Rybalko 	while (!(IS(bas, USR2, RDR)))
246a2c472e7SAleksandr Rybalko 		;
247a2c472e7SAleksandr Rybalko 
248a2c472e7SAleksandr Rybalko 	c = GETREG(bas, REG(URXD));
249a2c472e7SAleksandr Rybalko 	uart_unlock(hwmtx);
250a2c472e7SAleksandr Rybalko #if defined(KDB)
251a2c472e7SAleksandr Rybalko 	if (c & FLD(URXD, BRK)) {
252a2c472e7SAleksandr Rybalko 		if (kdb_break())
253a2c472e7SAleksandr Rybalko 			return (0);
254a2c472e7SAleksandr Rybalko 	}
255a2c472e7SAleksandr Rybalko #endif
256a2c472e7SAleksandr Rybalko 	return (c & 0xff);
257a2c472e7SAleksandr Rybalko }
258a2c472e7SAleksandr Rybalko 
259a2c472e7SAleksandr Rybalko /*
260a2c472e7SAleksandr Rybalko  * High-level UART interface.
261a2c472e7SAleksandr Rybalko  */
262a2c472e7SAleksandr Rybalko struct imx_uart_softc {
263a2c472e7SAleksandr Rybalko 	struct uart_softc base;
264a2c472e7SAleksandr Rybalko };
265a2c472e7SAleksandr Rybalko 
266a2c472e7SAleksandr Rybalko static int imx_uart_bus_attach(struct uart_softc *);
267a2c472e7SAleksandr Rybalko static int imx_uart_bus_detach(struct uart_softc *);
268a2c472e7SAleksandr Rybalko static int imx_uart_bus_flush(struct uart_softc *, int);
269a2c472e7SAleksandr Rybalko static int imx_uart_bus_getsig(struct uart_softc *);
270a2c472e7SAleksandr Rybalko static int imx_uart_bus_ioctl(struct uart_softc *, int, intptr_t);
271a2c472e7SAleksandr Rybalko static int imx_uart_bus_ipend(struct uart_softc *);
272a2c472e7SAleksandr Rybalko static int imx_uart_bus_param(struct uart_softc *, int, int, int, int);
273a2c472e7SAleksandr Rybalko static int imx_uart_bus_probe(struct uart_softc *);
274a2c472e7SAleksandr Rybalko static int imx_uart_bus_receive(struct uart_softc *);
275a2c472e7SAleksandr Rybalko static int imx_uart_bus_setsig(struct uart_softc *, int);
276a2c472e7SAleksandr Rybalko static int imx_uart_bus_transmit(struct uart_softc *);
277d76a1ef4SWarner Losh static void imx_uart_bus_grab(struct uart_softc *);
278d76a1ef4SWarner Losh static void imx_uart_bus_ungrab(struct uart_softc *);
279a2c472e7SAleksandr Rybalko 
280a2c472e7SAleksandr Rybalko static kobj_method_t imx_uart_methods[] = {
281a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_attach,		imx_uart_bus_attach),
282a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_detach,		imx_uart_bus_detach),
283a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_flush,		imx_uart_bus_flush),
284a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_getsig,		imx_uart_bus_getsig),
285a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_ioctl,		imx_uart_bus_ioctl),
286a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_ipend,		imx_uart_bus_ipend),
287a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_param,		imx_uart_bus_param),
288a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_probe,		imx_uart_bus_probe),
289a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_receive,	imx_uart_bus_receive),
290a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_setsig,		imx_uart_bus_setsig),
291a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_transmit,	imx_uart_bus_transmit),
292d76a1ef4SWarner Losh 	KOBJMETHOD(uart_grab,		imx_uart_bus_grab),
293d76a1ef4SWarner Losh 	KOBJMETHOD(uart_ungrab,		imx_uart_bus_ungrab),
294a2c472e7SAleksandr Rybalko 	{ 0, 0 }
295a2c472e7SAleksandr Rybalko };
296a2c472e7SAleksandr Rybalko 
2973bb693afSIan Lepore static struct uart_class uart_imx_class = {
298a2c472e7SAleksandr Rybalko 	"imx",
299a2c472e7SAleksandr Rybalko 	imx_uart_methods,
300a2c472e7SAleksandr Rybalko 	sizeof(struct imx_uart_softc),
301a2c472e7SAleksandr Rybalko 	.uc_ops = &uart_imx_uart_ops,
302a2c472e7SAleksandr Rybalko 	.uc_range = 0x100,
303405ada37SAndrew Turner 	.uc_rclk = 24000000, /* TODO: get value from CCM */
304405ada37SAndrew Turner 	.uc_rshift = 0
305a2c472e7SAleksandr Rybalko };
306a2c472e7SAleksandr Rybalko 
3073bb693afSIan Lepore static struct ofw_compat_data compat_data[] = {
3083bb693afSIan Lepore 	{"fsl,imx6q-uart",	(uintptr_t)&uart_imx_class},
3093bb693afSIan Lepore 	{"fsl,imx53-uart",	(uintptr_t)&uart_imx_class},
3103bb693afSIan Lepore 	{"fsl,imx51-uart",	(uintptr_t)&uart_imx_class},
3113bb693afSIan Lepore 	{"fsl,imx31-uart",	(uintptr_t)&uart_imx_class},
3123bb693afSIan Lepore 	{"fsl,imx27-uart",	(uintptr_t)&uart_imx_class},
3133bb693afSIan Lepore 	{"fsl,imx25-uart",	(uintptr_t)&uart_imx_class},
3143bb693afSIan Lepore 	{"fsl,imx21-uart",	(uintptr_t)&uart_imx_class},
3153bb693afSIan Lepore 	{NULL,			(uintptr_t)NULL},
3163bb693afSIan Lepore };
3173bb693afSIan Lepore UART_FDT_CLASS_AND_DEVICE(compat_data);
3183bb693afSIan Lepore 
319a2c472e7SAleksandr Rybalko #define	SIGCHG(c, i, s, d)				\
320a2c472e7SAleksandr Rybalko 	if (c) {					\
321a2c472e7SAleksandr Rybalko 		i |= (i & s) ? s : s | d;		\
322a2c472e7SAleksandr Rybalko 	} else {					\
323a2c472e7SAleksandr Rybalko 		i = (i & s) ? (i & ~s) | d : i;		\
324a2c472e7SAleksandr Rybalko 	}
325a2c472e7SAleksandr Rybalko 
326a2c472e7SAleksandr Rybalko static int
327a2c472e7SAleksandr Rybalko imx_uart_bus_attach(struct uart_softc *sc)
328a2c472e7SAleksandr Rybalko {
329a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
330a2c472e7SAleksandr Rybalko 	struct uart_devinfo *di;
331a2c472e7SAleksandr Rybalko 
332a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
333a2c472e7SAleksandr Rybalko 	if (sc->sc_sysdev != NULL) {
334a2c472e7SAleksandr Rybalko 		di = sc->sc_sysdev;
335a2c472e7SAleksandr Rybalko 		imx_uart_init(bas, di->baudrate, di->databits, di->stopbits,
336a2c472e7SAleksandr Rybalko 		    di->parity);
337a2c472e7SAleksandr Rybalko 	} else {
338a2c472e7SAleksandr Rybalko 		imx_uart_init(bas, 115200, 8, 1, 0);
339a2c472e7SAleksandr Rybalko 	}
340a2c472e7SAleksandr Rybalko 
341a2c472e7SAleksandr Rybalko 	(void)imx_uart_bus_getsig(sc);
342a2c472e7SAleksandr Rybalko 
3430dc54d18SIan Lepore 	/* Clear all pending interrupts. */
3440dc54d18SIan Lepore 	SETREG(bas, REG(USR1), 0xffff);
3450dc54d18SIan Lepore 	SETREG(bas, REG(USR2), 0xffff);
3460dc54d18SIan Lepore 
3470dc54d18SIan Lepore 	DIS(bas, UCR4, DREN);
3480dc54d18SIan Lepore 	ENA(bas, UCR1, RRDYEN);
349a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, IDEN);
350a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, RXDSEN);
3510dc54d18SIan Lepore 	ENA(bas, UCR2, ATEN);
352a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, TXMPTYEN);
353a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, TRDYEN);
354a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, TCEN);
355a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, OREN);
356a2c472e7SAleksandr Rybalko 	ENA(bas, UCR4, BKEN);
357a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, WKEN);
358a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, ADEN);
359a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, ACIEN);
360a2c472e7SAleksandr Rybalko 	DIS(bas, UCR2, ESCI);
361a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, ENIRI);
362a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, AIRINTEN);
363a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, AWAKEN);
364a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, FRAERREN);
365a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, PARERREN);
366a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, RTSDEN);
367a2c472e7SAleksandr Rybalko 	DIS(bas, UCR2, RTSEN);
368a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, DTREN);
369a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, RI);
370a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, DCD);
371a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, DTRDEN);
3722d40ec16SIan Lepore 	ENA(bas, UCR2, IRTS);
3732d40ec16SIan Lepore 	ENA(bas, UCR3, RXDMUXSEL);
374a2c472e7SAleksandr Rybalko 
375a2c472e7SAleksandr Rybalko 	return (0);
376a2c472e7SAleksandr Rybalko }
377a2c472e7SAleksandr Rybalko 
378a2c472e7SAleksandr Rybalko static int
379a2c472e7SAleksandr Rybalko imx_uart_bus_detach(struct uart_softc *sc)
380a2c472e7SAleksandr Rybalko {
381a2c472e7SAleksandr Rybalko 
382a2c472e7SAleksandr Rybalko 	SETREG(&sc->sc_bas, REG(UCR4), 0);
383a2c472e7SAleksandr Rybalko 
384a2c472e7SAleksandr Rybalko 	return (0);
385a2c472e7SAleksandr Rybalko }
386a2c472e7SAleksandr Rybalko 
387a2c472e7SAleksandr Rybalko static int
388a2c472e7SAleksandr Rybalko imx_uart_bus_flush(struct uart_softc *sc, int what)
389a2c472e7SAleksandr Rybalko {
390a2c472e7SAleksandr Rybalko 
391a2c472e7SAleksandr Rybalko 	/* TODO */
392a2c472e7SAleksandr Rybalko 	return (0);
393a2c472e7SAleksandr Rybalko }
394a2c472e7SAleksandr Rybalko 
395a2c472e7SAleksandr Rybalko static int
396a2c472e7SAleksandr Rybalko imx_uart_bus_getsig(struct uart_softc *sc)
397a2c472e7SAleksandr Rybalko {
398a2c472e7SAleksandr Rybalko 	uint32_t new, old, sig;
399a2c472e7SAleksandr Rybalko 	uint8_t bes;
400a2c472e7SAleksandr Rybalko 
401a2c472e7SAleksandr Rybalko 	do {
402a2c472e7SAleksandr Rybalko 		old = sc->sc_hwsig;
403a2c472e7SAleksandr Rybalko 		sig = old;
404a2c472e7SAleksandr Rybalko 		uart_lock(sc->sc_hwmtx);
405a2c472e7SAleksandr Rybalko 		bes = GETREG(&sc->sc_bas, REG(USR2));
406a2c472e7SAleksandr Rybalko 		uart_unlock(sc->sc_hwmtx);
407a2c472e7SAleksandr Rybalko 		/* XXX: chip can show delta */
408a2c472e7SAleksandr Rybalko 		SIGCHG(bes & FLD(USR2, DCDIN), sig, SER_DCD, SER_DDCD);
409a2c472e7SAleksandr Rybalko 		new = sig & ~SER_MASK_DELTA;
410a2c472e7SAleksandr Rybalko 	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
411a2c472e7SAleksandr Rybalko 
412a2c472e7SAleksandr Rybalko 	return (sig);
413a2c472e7SAleksandr Rybalko }
414a2c472e7SAleksandr Rybalko 
415a2c472e7SAleksandr Rybalko static int
416a2c472e7SAleksandr Rybalko imx_uart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
417a2c472e7SAleksandr Rybalko {
418a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
419a2c472e7SAleksandr Rybalko 	int error;
420a2c472e7SAleksandr Rybalko 
421a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
422a2c472e7SAleksandr Rybalko 	error = 0;
423a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
424a2c472e7SAleksandr Rybalko 	switch (request) {
425a2c472e7SAleksandr Rybalko 	case UART_IOCTL_BREAK:
426a2c472e7SAleksandr Rybalko 		/* TODO */
427a2c472e7SAleksandr Rybalko 		break;
428a2c472e7SAleksandr Rybalko 	case UART_IOCTL_BAUD:
429c3f0f284SIan Lepore 		*(u_int*)data = imx_uart_getbaud(bas);
430a2c472e7SAleksandr Rybalko 		break;
431a2c472e7SAleksandr Rybalko 	default:
432a2c472e7SAleksandr Rybalko 		error = EINVAL;
433a2c472e7SAleksandr Rybalko 		break;
434a2c472e7SAleksandr Rybalko 	}
435a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
436a2c472e7SAleksandr Rybalko 
437a2c472e7SAleksandr Rybalko 	return (error);
438a2c472e7SAleksandr Rybalko }
439a2c472e7SAleksandr Rybalko 
440a2c472e7SAleksandr Rybalko static int
441a2c472e7SAleksandr Rybalko imx_uart_bus_ipend(struct uart_softc *sc)
442a2c472e7SAleksandr Rybalko {
443a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
444a2c472e7SAleksandr Rybalko 	int ipend;
445a2c472e7SAleksandr Rybalko 	uint32_t usr1, usr2;
4460dc54d18SIan Lepore 	uint32_t ucr1, ucr2, ucr4;
447a2c472e7SAleksandr Rybalko 
448a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
449a2c472e7SAleksandr Rybalko 	ipend = 0;
450a2c472e7SAleksandr Rybalko 
451a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
452a2c472e7SAleksandr Rybalko 
453a2c472e7SAleksandr Rybalko 	/* Read pending interrupts */
454a2c472e7SAleksandr Rybalko 	usr1 = GETREG(bas, REG(USR1));
455a2c472e7SAleksandr Rybalko 	usr2 = GETREG(bas, REG(USR2));
456a2c472e7SAleksandr Rybalko 	/* ACK interrupts */
457a2c472e7SAleksandr Rybalko 	SETREG(bas, REG(USR1), usr1);
458a2c472e7SAleksandr Rybalko 	SETREG(bas, REG(USR2), usr2);
459a2c472e7SAleksandr Rybalko 
460a2c472e7SAleksandr Rybalko 	ucr1 = GETREG(bas, REG(UCR1));
4610dc54d18SIan Lepore 	ucr2 = GETREG(bas, REG(UCR2));
462a2c472e7SAleksandr Rybalko 	ucr4 = GETREG(bas, REG(UCR4));
463a2c472e7SAleksandr Rybalko 
4640dc54d18SIan Lepore 	/* If we have reached tx low-water, we can tx some more now. */
4650dc54d18SIan Lepore 	if ((usr1 & FLD(USR1, TRDY)) && (ucr1 & FLD(UCR1, TRDYEN))) {
4660dc54d18SIan Lepore 		DIS(bas, UCR1, TRDYEN);
467a2c472e7SAleksandr Rybalko 		ipend |= SER_INT_TXIDLE;
468a2c472e7SAleksandr Rybalko 	}
4690dc54d18SIan Lepore 
4700dc54d18SIan Lepore 	/*
4710dc54d18SIan Lepore 	 * If we have reached the rx high-water, or if there are bytes in the rx
4720dc54d18SIan Lepore 	 * fifo and no new data has arrived for 8 character periods (aging
4730dc54d18SIan Lepore 	 * timer), we have input data to process.
4740dc54d18SIan Lepore 	 */
4750dc54d18SIan Lepore 	if (((usr1 & FLD(USR1, RRDY)) && (ucr1 & FLD(UCR1, RRDYEN))) ||
4760dc54d18SIan Lepore 	    ((usr1 & FLD(USR1, AGTIM)) && (ucr2 & FLD(UCR2, ATEN)))) {
4770dc54d18SIan Lepore 		DIS(bas, UCR1, RRDYEN);
4780dc54d18SIan Lepore 		DIS(bas, UCR2, ATEN);
479a2c472e7SAleksandr Rybalko 		ipend |= SER_INT_RXREADY;
480a2c472e7SAleksandr Rybalko 	}
4810dc54d18SIan Lepore 
4820dc54d18SIan Lepore 	/* A break can come in at any time, it never gets disabled. */
483a2c472e7SAleksandr Rybalko 	if ((usr2 & FLD(USR2, BRCD)) && (ucr4 & FLD(UCR4, BKEN)))
484a2c472e7SAleksandr Rybalko 		ipend |= SER_INT_BREAK;
485a2c472e7SAleksandr Rybalko 
486a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
487a2c472e7SAleksandr Rybalko 
488a2c472e7SAleksandr Rybalko 	return (ipend);
489a2c472e7SAleksandr Rybalko }
490a2c472e7SAleksandr Rybalko 
491a2c472e7SAleksandr Rybalko static int
492a2c472e7SAleksandr Rybalko imx_uart_bus_param(struct uart_softc *sc, int baudrate, int databits,
493a2c472e7SAleksandr Rybalko     int stopbits, int parity)
494a2c472e7SAleksandr Rybalko {
495a2c472e7SAleksandr Rybalko 
496a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
497a2c472e7SAleksandr Rybalko 	imx_uart_init(&sc->sc_bas, baudrate, databits, stopbits, parity);
498a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
499a2c472e7SAleksandr Rybalko 	return (0);
500a2c472e7SAleksandr Rybalko }
501a2c472e7SAleksandr Rybalko 
502a2c472e7SAleksandr Rybalko static int
503a2c472e7SAleksandr Rybalko imx_uart_bus_probe(struct uart_softc *sc)
504a2c472e7SAleksandr Rybalko {
505a2c472e7SAleksandr Rybalko 	int error;
506a2c472e7SAleksandr Rybalko 
507a2c472e7SAleksandr Rybalko 	error = imx_uart_probe(&sc->sc_bas);
508a2c472e7SAleksandr Rybalko 	if (error)
509a2c472e7SAleksandr Rybalko 		return (error);
510a2c472e7SAleksandr Rybalko 
5110dc54d18SIan Lepore 	/*
5120dc54d18SIan Lepore 	 * On input we can read up to the full fifo size at once.  On output, we
5130dc54d18SIan Lepore 	 * want to write only as much as the programmed tx low water level,
5140dc54d18SIan Lepore 	 * because that's all we can be certain we have room for in the fifo
5150dc54d18SIan Lepore 	 * when we get a tx-ready interrupt.
5160dc54d18SIan Lepore 	 */
5170dc54d18SIan Lepore 	sc->sc_rxfifosz = IMX_FIFOSZ;
5180dc54d18SIan Lepore 	sc->sc_txfifosz = IMX_TXFIFO_LEVEL;
5194d7abca0SIan Lepore 
5203329109cSRui Paulo 	device_set_desc(sc->sc_dev, "Freescale i.MX UART");
521a2c472e7SAleksandr Rybalko 	return (0);
522a2c472e7SAleksandr Rybalko }
523a2c472e7SAleksandr Rybalko 
524a2c472e7SAleksandr Rybalko static int
525a2c472e7SAleksandr Rybalko imx_uart_bus_receive(struct uart_softc *sc)
526a2c472e7SAleksandr Rybalko {
527a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
528a2c472e7SAleksandr Rybalko 	int xc, out;
529a2c472e7SAleksandr Rybalko 
530a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
531a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
532a2c472e7SAleksandr Rybalko 
5330dc54d18SIan Lepore 	/*
5340dc54d18SIan Lepore 	 * Empty the rx fifo.  We get the RRDY interrupt when IMX_RXFIFO_LEVEL
5350dc54d18SIan Lepore 	 * (the rx high-water level) is reached, but we set sc_rxfifosz to the
5360dc54d18SIan Lepore 	 * full hardware fifo size, so we can safely process however much is
5370dc54d18SIan Lepore 	 * there, not just the highwater size.
5380dc54d18SIan Lepore 	 */
539a2c472e7SAleksandr Rybalko 	while (IS(bas, USR2, RDR)) {
540a2c472e7SAleksandr Rybalko 		if (uart_rx_full(sc)) {
541a2c472e7SAleksandr Rybalko 			/* No space left in input buffer */
542a2c472e7SAleksandr Rybalko 			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
543a2c472e7SAleksandr Rybalko 			break;
544a2c472e7SAleksandr Rybalko 		}
545a2c472e7SAleksandr Rybalko 		xc = GETREG(bas, REG(URXD));
546a2c472e7SAleksandr Rybalko 		out = xc & 0x000000ff;
547a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, FRMERR))
548a2c472e7SAleksandr Rybalko 			out |= UART_STAT_FRAMERR;
549a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, PRERR))
550a2c472e7SAleksandr Rybalko 			out |= UART_STAT_PARERR;
551a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, OVRRUN))
552a2c472e7SAleksandr Rybalko 			out |= UART_STAT_OVERRUN;
553a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, BRK))
554a2c472e7SAleksandr Rybalko 			out |= UART_STAT_BREAK;
555a2c472e7SAleksandr Rybalko 
556a2c472e7SAleksandr Rybalko 		uart_rx_put(sc, out);
557a2c472e7SAleksandr Rybalko 	}
5580dc54d18SIan Lepore 	ENA(bas, UCR1, RRDYEN);
5590dc54d18SIan Lepore 	ENA(bas, UCR2, ATEN);
560a2c472e7SAleksandr Rybalko 
561a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
562a2c472e7SAleksandr Rybalko 	return (0);
563a2c472e7SAleksandr Rybalko }
564a2c472e7SAleksandr Rybalko 
565a2c472e7SAleksandr Rybalko static int
566a2c472e7SAleksandr Rybalko imx_uart_bus_setsig(struct uart_softc *sc, int sig)
567a2c472e7SAleksandr Rybalko {
568a2c472e7SAleksandr Rybalko 
569a2c472e7SAleksandr Rybalko 	return (0);
570a2c472e7SAleksandr Rybalko }
571a2c472e7SAleksandr Rybalko 
572a2c472e7SAleksandr Rybalko static int
573a2c472e7SAleksandr Rybalko imx_uart_bus_transmit(struct uart_softc *sc)
574a2c472e7SAleksandr Rybalko {
575a2c472e7SAleksandr Rybalko 	struct uart_bas *bas = &sc->sc_bas;
576a2c472e7SAleksandr Rybalko 	int i;
577a2c472e7SAleksandr Rybalko 
578a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
579a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
580a2c472e7SAleksandr Rybalko 
5810dc54d18SIan Lepore 	/*
5820dc54d18SIan Lepore 	 * Fill the tx fifo.  The uart core puts at most IMX_TXFIFO_LEVEL bytes
5830dc54d18SIan Lepore 	 * into the txbuf (because that's what sc_txfifosz is set to), and
5840dc54d18SIan Lepore 	 * because we got the TRDY (low-water reached) interrupt we know at
5850dc54d18SIan Lepore 	 * least that much space is available in the fifo.
5860dc54d18SIan Lepore 	 */
587a2c472e7SAleksandr Rybalko 	for (i = 0; i < sc->sc_txdatasz; i++) {
588a2c472e7SAleksandr Rybalko 		SETREG(bas, REG(UTXD), sc->sc_txbuf[i] & 0xff);
589a2c472e7SAleksandr Rybalko 	}
590a2c472e7SAleksandr Rybalko 	sc->sc_txbusy = 1;
5910dc54d18SIan Lepore 	ENA(bas, UCR1, TRDYEN);
592a2c472e7SAleksandr Rybalko 
593a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
594a2c472e7SAleksandr Rybalko 
595a2c472e7SAleksandr Rybalko 	return (0);
596a2c472e7SAleksandr Rybalko }
597d76a1ef4SWarner Losh 
598d76a1ef4SWarner Losh static void
599d76a1ef4SWarner Losh imx_uart_bus_grab(struct uart_softc *sc)
600d76a1ef4SWarner Losh {
601d76a1ef4SWarner Losh 	struct uart_bas *bas = &sc->sc_bas;
602d76a1ef4SWarner Losh 
603d76a1ef4SWarner Losh 	bas = &sc->sc_bas;
604d76a1ef4SWarner Losh 	uart_lock(sc->sc_hwmtx);
6050dc54d18SIan Lepore 	DIS(bas, UCR1, RRDYEN);
6060dc54d18SIan Lepore 	DIS(bas, UCR2, ATEN);
607d76a1ef4SWarner Losh 	uart_unlock(sc->sc_hwmtx);
608d76a1ef4SWarner Losh }
609d76a1ef4SWarner Losh 
610d76a1ef4SWarner Losh static void
611d76a1ef4SWarner Losh imx_uart_bus_ungrab(struct uart_softc *sc)
612d76a1ef4SWarner Losh {
613d76a1ef4SWarner Losh 	struct uart_bas *bas = &sc->sc_bas;
614d76a1ef4SWarner Losh 
615d76a1ef4SWarner Losh 	bas = &sc->sc_bas;
616d76a1ef4SWarner Losh 	uart_lock(sc->sc_hwmtx);
6170dc54d18SIan Lepore 	ENA(bas, UCR1, RRDYEN);
6180dc54d18SIan Lepore 	ENA(bas, UCR2, ATEN);
619d76a1ef4SWarner Losh 	uart_unlock(sc->sc_hwmtx);
620d76a1ef4SWarner Losh }
621