xref: /freebsd/sys/dev/uart/uart_dev_imx.c (revision 4f64df4e32a7c14c5862b5f866bae2b090d25edb)
1a2c472e7SAleksandr Rybalko /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4a2c472e7SAleksandr Rybalko  * Copyright (c) 2012 The FreeBSD Foundation
5a2c472e7SAleksandr Rybalko  *
6a2c472e7SAleksandr Rybalko  * This software was developed by Oleksandr Rybalko under sponsorship
7a2c472e7SAleksandr Rybalko  * from the FreeBSD Foundation.
8a2c472e7SAleksandr Rybalko  *
9a2c472e7SAleksandr Rybalko  * Redistribution and use in source and binary forms, with or without
10a2c472e7SAleksandr Rybalko  * modification, are permitted provided that the following conditions
11a2c472e7SAleksandr Rybalko  * are met:
12a2c472e7SAleksandr Rybalko  * 1.	Redistributions of source code must retain the above copyright
13a2c472e7SAleksandr Rybalko  *	notice, this list of conditions and the following disclaimer.
14a2c472e7SAleksandr Rybalko  * 2.	Redistributions in binary form must reproduce the above copyright
15a2c472e7SAleksandr Rybalko  *	notice, this list of conditions and the following disclaimer in the
16a2c472e7SAleksandr Rybalko  *	documentation and/or other materials provided with the distribution.
17a2c472e7SAleksandr Rybalko  *
18a2c472e7SAleksandr Rybalko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19a2c472e7SAleksandr Rybalko  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a2c472e7SAleksandr Rybalko  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a2c472e7SAleksandr Rybalko  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a2c472e7SAleksandr Rybalko  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a2c472e7SAleksandr Rybalko  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a2c472e7SAleksandr Rybalko  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a2c472e7SAleksandr Rybalko  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a2c472e7SAleksandr Rybalko  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a2c472e7SAleksandr Rybalko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a2c472e7SAleksandr Rybalko  * SUCH DAMAGE.
29a2c472e7SAleksandr Rybalko  */
30a2c472e7SAleksandr Rybalko 
31a2c472e7SAleksandr Rybalko #include <sys/cdefs.h>
32a2c472e7SAleksandr Rybalko __FBSDID("$FreeBSD$");
33a2c472e7SAleksandr Rybalko 
34a2c472e7SAleksandr Rybalko #include "opt_ddb.h"
35a2c472e7SAleksandr Rybalko 
36a2c472e7SAleksandr Rybalko #include <sys/param.h>
37a2c472e7SAleksandr Rybalko #include <sys/systm.h>
38a2c472e7SAleksandr Rybalko #include <sys/bus.h>
39a2c472e7SAleksandr Rybalko #include <sys/conf.h>
40a2c472e7SAleksandr Rybalko #include <sys/kdb.h>
41a2c472e7SAleksandr Rybalko #include <machine/bus.h>
42a2c472e7SAleksandr Rybalko 
43a2c472e7SAleksandr Rybalko #include <dev/uart/uart.h>
44a2c472e7SAleksandr Rybalko #include <dev/uart/uart_cpu.h>
453bb693afSIan Lepore #include <dev/uart/uart_cpu_fdt.h>
46a2c472e7SAleksandr Rybalko #include <dev/uart/uart_bus.h>
47a90f1975SIan Lepore #include <dev/uart/uart_dev_imx.h>
4894bc2117SOleksandr Tymoshenko 
49*4f64df4eSEmmanuel Vadot #if defined(__aarch64__)
5094bc2117SOleksandr Tymoshenko #define	IMX_ENABLE_CLOCKS
5194bc2117SOleksandr Tymoshenko #endif
5294bc2117SOleksandr Tymoshenko 
5394bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
5494bc2117SOleksandr Tymoshenko #include <dev/extres/clk/clk.h>
5594bc2117SOleksandr Tymoshenko #endif
5694bc2117SOleksandr Tymoshenko 
57a2c472e7SAleksandr Rybalko #include "uart_if.h"
582d40ec16SIan Lepore 
592d40ec16SIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
602d40ec16SIan Lepore 
61a2c472e7SAleksandr Rybalko /*
620dc54d18SIan Lepore  * The hardare FIFOs are 32 bytes.  We want an interrupt when there are 24 bytes
630dc54d18SIan Lepore  * available to read or space for 24 more bytes to write.  While 8 bytes of
640dc54d18SIan Lepore  * slack before over/underrun might seem excessive, the hardware can run at
650dc54d18SIan Lepore  * 5mbps, which means 2uS per char, so at full speed 8 bytes provides only 16uS
660dc54d18SIan Lepore  * to get into the interrupt handler and service the fifo.
670dc54d18SIan Lepore  */
680dc54d18SIan Lepore #define	IMX_FIFOSZ		32
690dc54d18SIan Lepore #define	IMX_RXFIFO_LEVEL	24
700dc54d18SIan Lepore #define	IMX_TXFIFO_LEVEL	24
710dc54d18SIan Lepore 
720dc54d18SIan Lepore /*
73a2c472e7SAleksandr Rybalko  * Low-level UART interface.
74a2c472e7SAleksandr Rybalko  */
75a2c472e7SAleksandr Rybalko static int imx_uart_probe(struct uart_bas *bas);
76a2c472e7SAleksandr Rybalko static void imx_uart_init(struct uart_bas *bas, int, int, int, int);
77a2c472e7SAleksandr Rybalko static void imx_uart_term(struct uart_bas *bas);
78a2c472e7SAleksandr Rybalko static void imx_uart_putc(struct uart_bas *bas, int);
79a2c472e7SAleksandr Rybalko static int imx_uart_rxready(struct uart_bas *bas);
80a2c472e7SAleksandr Rybalko static int imx_uart_getc(struct uart_bas *bas, struct mtx *);
81a2c472e7SAleksandr Rybalko 
82a2c472e7SAleksandr Rybalko static struct uart_ops uart_imx_uart_ops = {
83a2c472e7SAleksandr Rybalko 	.probe = imx_uart_probe,
84a2c472e7SAleksandr Rybalko 	.init = imx_uart_init,
85a2c472e7SAleksandr Rybalko 	.term = imx_uart_term,
86a2c472e7SAleksandr Rybalko 	.putc = imx_uart_putc,
87a2c472e7SAleksandr Rybalko 	.rxready = imx_uart_rxready,
88a2c472e7SAleksandr Rybalko 	.getc = imx_uart_getc,
89a2c472e7SAleksandr Rybalko };
90a2c472e7SAleksandr Rybalko 
912d40ec16SIan Lepore #if 0 /* Handy when debugging. */
922d40ec16SIan Lepore static void
932d40ec16SIan Lepore dumpregs(struct uart_bas *bas, const char * msg)
942d40ec16SIan Lepore {
952d40ec16SIan Lepore 
962d40ec16SIan Lepore 	if (!bootverbose)
972d40ec16SIan Lepore 		return;
982d40ec16SIan Lepore 	printf("%s bsh 0x%08lx UCR1 0x%08x UCR2 0x%08x "
992d40ec16SIan Lepore 		"UCR3 0x%08x UCR4 0x%08x USR1 0x%08x USR2 0x%08x\n",
1002d40ec16SIan Lepore 	    msg, bas->bsh,
1012d40ec16SIan Lepore 	    GETREG(bas, REG(UCR1)), GETREG(bas, REG(UCR2)),
1022d40ec16SIan Lepore 	    GETREG(bas, REG(UCR3)), GETREG(bas, REG(UCR4)),
1032d40ec16SIan Lepore 	    GETREG(bas, REG(USR1)), GETREG(bas, REG(USR2)));
1042d40ec16SIan Lepore }
1052d40ec16SIan Lepore #endif
1062d40ec16SIan Lepore 
107a2c472e7SAleksandr Rybalko static int
108a2c472e7SAleksandr Rybalko imx_uart_probe(struct uart_bas *bas)
109a2c472e7SAleksandr Rybalko {
110a2c472e7SAleksandr Rybalko 
111a2c472e7SAleksandr Rybalko 	return (0);
112a2c472e7SAleksandr Rybalko }
113a2c472e7SAleksandr Rybalko 
114c3f0f284SIan Lepore static u_int
115c3f0f284SIan Lepore imx_uart_getbaud(struct uart_bas *bas)
116c3f0f284SIan Lepore {
117c3f0f284SIan Lepore 	uint32_t rate, ubir, ubmr;
118c3f0f284SIan Lepore 	u_int baud, blo, bhi, i;
119c3f0f284SIan Lepore 	static const u_int predivs[] = {6, 5, 4, 3, 2, 1, 7, 1};
120c3f0f284SIan Lepore 	static const u_int std_rates[] = {
121c3f0f284SIan Lepore 		9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600
122c3f0f284SIan Lepore 	};
123c3f0f284SIan Lepore 
124c3f0f284SIan Lepore 	/*
125c3f0f284SIan Lepore 	 * Get the baud rate the hardware is programmed for, then search the
126c3f0f284SIan Lepore 	 * table of standard baud rates for a number that's within 3% of the
127c3f0f284SIan Lepore 	 * actual rate the hardware is programmed for.  It's more comforting to
128c3f0f284SIan Lepore 	 * see that your console is running at 115200 than 114942.  Note that
129c3f0f284SIan Lepore 	 * here we cannot make a simplifying assumption that the predivider and
130c3f0f284SIan Lepore 	 * numerator are 1 (like we do when setting the baud rate), because we
131c3f0f284SIan Lepore 	 * don't know what u-boot might have set up.
132c3f0f284SIan Lepore 	 */
133c3f0f284SIan Lepore 	i = (GETREG(bas, REG(UFCR)) & IMXUART_UFCR_RFDIV_MASK) >>
134c3f0f284SIan Lepore 	    IMXUART_UFCR_RFDIV_SHIFT;
13594bc2117SOleksandr Tymoshenko 	rate = bas->rclk / predivs[i];
136c3f0f284SIan Lepore 	ubir = GETREG(bas, REG(UBIR)) + 1;
137c3f0f284SIan Lepore 	ubmr = GETREG(bas, REG(UBMR)) + 1;
138c3f0f284SIan Lepore 	baud = ((rate / 16 ) * ubir) / ubmr;
139c3f0f284SIan Lepore 
140c3f0f284SIan Lepore 	blo = (baud * 100) / 103;
141c3f0f284SIan Lepore 	bhi = (baud * 100) / 97;
142c3f0f284SIan Lepore 	for (i = 0; i < nitems(std_rates); i++) {
143c3f0f284SIan Lepore 		rate = std_rates[i];
144c3f0f284SIan Lepore 		if (rate >= blo && rate <= bhi) {
145c3f0f284SIan Lepore 			baud = rate;
146c3f0f284SIan Lepore 			break;
147c3f0f284SIan Lepore 		}
148c3f0f284SIan Lepore 	}
149c3f0f284SIan Lepore 
150c3f0f284SIan Lepore 	return (baud);
151c3f0f284SIan Lepore }
152c3f0f284SIan Lepore 
153a2c472e7SAleksandr Rybalko static void
154a2c472e7SAleksandr Rybalko imx_uart_init(struct uart_bas *bas, int baudrate, int databits,
155a2c472e7SAleksandr Rybalko     int stopbits, int parity)
156a2c472e7SAleksandr Rybalko {
1572d40ec16SIan Lepore 	uint32_t baseclk, reg;
158a2c472e7SAleksandr Rybalko 
1592d40ec16SIan Lepore         /* Enable the device and the RX/TX channels. */
1602d40ec16SIan Lepore 	SET(bas, REG(UCR1), FLD(UCR1, UARTEN));
1612d40ec16SIan Lepore 	SET(bas, REG(UCR2), FLD(UCR2, RXEN) | FLD(UCR2, TXEN));
1622d40ec16SIan Lepore 
1632d40ec16SIan Lepore 	if (databits == 7)
1642d40ec16SIan Lepore 		DIS(bas, UCR2, WS);
1652d40ec16SIan Lepore 	else
1662d40ec16SIan Lepore 		ENA(bas, UCR2, WS);
1672d40ec16SIan Lepore 
1682d40ec16SIan Lepore 	if (stopbits == 2)
1692d40ec16SIan Lepore 		ENA(bas, UCR2, STPB);
1702d40ec16SIan Lepore 	else
1712d40ec16SIan Lepore 		DIS(bas, UCR2, STPB);
1722d40ec16SIan Lepore 
1732d40ec16SIan Lepore 	switch (parity) {
1742d40ec16SIan Lepore 	case UART_PARITY_ODD:
1752d40ec16SIan Lepore 		DIS(bas, UCR2, PROE);
1762d40ec16SIan Lepore 		ENA(bas, UCR2, PREN);
1772d40ec16SIan Lepore 		break;
1782d40ec16SIan Lepore 	case UART_PARITY_EVEN:
1792d40ec16SIan Lepore 		ENA(bas, UCR2, PROE);
1802d40ec16SIan Lepore 		ENA(bas, UCR2, PREN);
1812d40ec16SIan Lepore 		break;
1822d40ec16SIan Lepore 	case UART_PARITY_MARK:
1832d40ec16SIan Lepore 	case UART_PARITY_SPACE:
1842d40ec16SIan Lepore                 /* FALLTHROUGH: Hardware doesn't support mark/space. */
1852d40ec16SIan Lepore 	case UART_PARITY_NONE:
1862d40ec16SIan Lepore 	default:
1872d40ec16SIan Lepore 		DIS(bas, UCR2, PREN);
1882d40ec16SIan Lepore 		break;
1892d40ec16SIan Lepore 	}
1902d40ec16SIan Lepore 
1912d40ec16SIan Lepore 	/*
1922d40ec16SIan Lepore 	 * The hardware has an extremely flexible baud clock: it allows setting
1932d40ec16SIan Lepore 	 * both the numerator and denominator of the divider, as well as a
19449d0a4c3SIan Lepore 	 * separate pre-divider.  We simplify the problem of coming up with a
19549d0a4c3SIan Lepore 	 * workable pair of numbers by assuming a pre-divider and numerator of
19649d0a4c3SIan Lepore 	 * one because our base clock is so fast we can reach virtually any
19749d0a4c3SIan Lepore 	 * reasonable speed with a simple divisor.  The numerator value actually
19849d0a4c3SIan Lepore 	 * includes the 16x over-sampling (so a value of 16 means divide by 1);
19949d0a4c3SIan Lepore 	 * the register value is the numerator-1, so we have a hard-coded 15.
20049d0a4c3SIan Lepore 	 * Note that a quirk of the hardware requires that both UBIR and UBMR be
20149d0a4c3SIan Lepore 	 * set back to back in order for the change to take effect.
2022d40ec16SIan Lepore 	 */
20394bc2117SOleksandr Tymoshenko 	if ((baudrate > 0) && (bas->rclk != 0)) {
20494bc2117SOleksandr Tymoshenko 		baseclk = bas->rclk;
2052d40ec16SIan Lepore 		reg = GETREG(bas, REG(UFCR));
2062d40ec16SIan Lepore 		reg = (reg & ~IMXUART_UFCR_RFDIV_MASK) | IMXUART_UFCR_RFDIV_DIV1;
2072d40ec16SIan Lepore 		SETREG(bas, REG(UFCR), reg);
2082d40ec16SIan Lepore 		SETREG(bas, REG(UBIR), 15);
2092d40ec16SIan Lepore 		SETREG(bas, REG(UBMR), (baseclk / baudrate) - 1);
2102d40ec16SIan Lepore 	}
2110dc54d18SIan Lepore 
2120dc54d18SIan Lepore 	/*
2130dc54d18SIan Lepore 	 * Program the tx lowater and rx hiwater levels at which fifo-service
2140dc54d18SIan Lepore 	 * interrupts are signaled.  The tx value is interpetted as "when there
2150dc54d18SIan Lepore 	 * are only this many bytes remaining" (not "this many free").
2160dc54d18SIan Lepore 	 */
2170dc54d18SIan Lepore 	reg = GETREG(bas, REG(UFCR));
2180dc54d18SIan Lepore 	reg &= ~(IMXUART_UFCR_TXTL_MASK | IMXUART_UFCR_RXTL_MASK);
2190dc54d18SIan Lepore 	reg |= (IMX_FIFOSZ - IMX_TXFIFO_LEVEL) << IMXUART_UFCR_TXTL_SHIFT;
2200dc54d18SIan Lepore 	reg |= IMX_RXFIFO_LEVEL << IMXUART_UFCR_RXTL_SHIFT;
2210dc54d18SIan Lepore 	SETREG(bas, REG(UFCR), reg);
222a2c472e7SAleksandr Rybalko }
223a2c472e7SAleksandr Rybalko 
224a2c472e7SAleksandr Rybalko static void
225a2c472e7SAleksandr Rybalko imx_uart_term(struct uart_bas *bas)
226a2c472e7SAleksandr Rybalko {
227a2c472e7SAleksandr Rybalko 
228a2c472e7SAleksandr Rybalko }
229a2c472e7SAleksandr Rybalko 
230a2c472e7SAleksandr Rybalko static void
231a2c472e7SAleksandr Rybalko imx_uart_putc(struct uart_bas *bas, int c)
232a2c472e7SAleksandr Rybalko {
233a2c472e7SAleksandr Rybalko 
2340dc54d18SIan Lepore 	while (!(IS(bas, USR1, TRDY)))
235a2c472e7SAleksandr Rybalko 		;
236a2c472e7SAleksandr Rybalko 	SETREG(bas, REG(UTXD), c);
237a2c472e7SAleksandr Rybalko }
238a2c472e7SAleksandr Rybalko 
239a2c472e7SAleksandr Rybalko static int
240a2c472e7SAleksandr Rybalko imx_uart_rxready(struct uart_bas *bas)
241a2c472e7SAleksandr Rybalko {
242a2c472e7SAleksandr Rybalko 
243a2c472e7SAleksandr Rybalko 	return ((IS(bas, USR2, RDR)) ? 1 : 0);
244a2c472e7SAleksandr Rybalko }
245a2c472e7SAleksandr Rybalko 
246a2c472e7SAleksandr Rybalko static int
247a2c472e7SAleksandr Rybalko imx_uart_getc(struct uart_bas *bas, struct mtx *hwmtx)
248a2c472e7SAleksandr Rybalko {
249a2c472e7SAleksandr Rybalko 	int c;
250a2c472e7SAleksandr Rybalko 
251a2c472e7SAleksandr Rybalko 	uart_lock(hwmtx);
252a2c472e7SAleksandr Rybalko 	while (!(IS(bas, USR2, RDR)))
253a2c472e7SAleksandr Rybalko 		;
254a2c472e7SAleksandr Rybalko 
255a2c472e7SAleksandr Rybalko 	c = GETREG(bas, REG(URXD));
256a2c472e7SAleksandr Rybalko 	uart_unlock(hwmtx);
257a2c472e7SAleksandr Rybalko #if defined(KDB)
258a2c472e7SAleksandr Rybalko 	if (c & FLD(URXD, BRK)) {
259a2c472e7SAleksandr Rybalko 		if (kdb_break())
260a2c472e7SAleksandr Rybalko 			return (0);
261a2c472e7SAleksandr Rybalko 	}
262a2c472e7SAleksandr Rybalko #endif
263a2c472e7SAleksandr Rybalko 	return (c & 0xff);
264a2c472e7SAleksandr Rybalko }
265a2c472e7SAleksandr Rybalko 
266a2c472e7SAleksandr Rybalko /*
267a2c472e7SAleksandr Rybalko  * High-level UART interface.
268a2c472e7SAleksandr Rybalko  */
269a2c472e7SAleksandr Rybalko struct imx_uart_softc {
270a2c472e7SAleksandr Rybalko 	struct uart_softc base;
271a2c472e7SAleksandr Rybalko };
272a2c472e7SAleksandr Rybalko 
273a2c472e7SAleksandr Rybalko static int imx_uart_bus_attach(struct uart_softc *);
274a2c472e7SAleksandr Rybalko static int imx_uart_bus_detach(struct uart_softc *);
275a2c472e7SAleksandr Rybalko static int imx_uart_bus_flush(struct uart_softc *, int);
276a2c472e7SAleksandr Rybalko static int imx_uart_bus_getsig(struct uart_softc *);
277a2c472e7SAleksandr Rybalko static int imx_uart_bus_ioctl(struct uart_softc *, int, intptr_t);
278a2c472e7SAleksandr Rybalko static int imx_uart_bus_ipend(struct uart_softc *);
279a2c472e7SAleksandr Rybalko static int imx_uart_bus_param(struct uart_softc *, int, int, int, int);
280a2c472e7SAleksandr Rybalko static int imx_uart_bus_probe(struct uart_softc *);
281a2c472e7SAleksandr Rybalko static int imx_uart_bus_receive(struct uart_softc *);
282a2c472e7SAleksandr Rybalko static int imx_uart_bus_setsig(struct uart_softc *, int);
283a2c472e7SAleksandr Rybalko static int imx_uart_bus_transmit(struct uart_softc *);
284d76a1ef4SWarner Losh static void imx_uart_bus_grab(struct uart_softc *);
285d76a1ef4SWarner Losh static void imx_uart_bus_ungrab(struct uart_softc *);
286a2c472e7SAleksandr Rybalko 
287a2c472e7SAleksandr Rybalko static kobj_method_t imx_uart_methods[] = {
288a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_attach,		imx_uart_bus_attach),
289a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_detach,		imx_uart_bus_detach),
290a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_flush,		imx_uart_bus_flush),
291a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_getsig,		imx_uart_bus_getsig),
292a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_ioctl,		imx_uart_bus_ioctl),
293a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_ipend,		imx_uart_bus_ipend),
294a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_param,		imx_uart_bus_param),
295a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_probe,		imx_uart_bus_probe),
296a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_receive,	imx_uart_bus_receive),
297a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_setsig,		imx_uart_bus_setsig),
298a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_transmit,	imx_uart_bus_transmit),
299d76a1ef4SWarner Losh 	KOBJMETHOD(uart_grab,		imx_uart_bus_grab),
300d76a1ef4SWarner Losh 	KOBJMETHOD(uart_ungrab,		imx_uart_bus_ungrab),
301a2c472e7SAleksandr Rybalko 	{ 0, 0 }
302a2c472e7SAleksandr Rybalko };
303a2c472e7SAleksandr Rybalko 
3043bb693afSIan Lepore static struct uart_class uart_imx_class = {
305a2c472e7SAleksandr Rybalko 	"imx",
306a2c472e7SAleksandr Rybalko 	imx_uart_methods,
307a2c472e7SAleksandr Rybalko 	sizeof(struct imx_uart_softc),
308a2c472e7SAleksandr Rybalko 	.uc_ops = &uart_imx_uart_ops,
309a2c472e7SAleksandr Rybalko 	.uc_range = 0x100,
310405ada37SAndrew Turner 	.uc_rclk = 24000000, /* TODO: get value from CCM */
311405ada37SAndrew Turner 	.uc_rshift = 0
312a2c472e7SAleksandr Rybalko };
313a2c472e7SAleksandr Rybalko 
3143bb693afSIan Lepore static struct ofw_compat_data compat_data[] = {
3153bb693afSIan Lepore 	{"fsl,imx6q-uart",	(uintptr_t)&uart_imx_class},
3163bb693afSIan Lepore 	{"fsl,imx53-uart",	(uintptr_t)&uart_imx_class},
3173bb693afSIan Lepore 	{"fsl,imx51-uart",	(uintptr_t)&uart_imx_class},
3183bb693afSIan Lepore 	{"fsl,imx31-uart",	(uintptr_t)&uart_imx_class},
3193bb693afSIan Lepore 	{"fsl,imx27-uart",	(uintptr_t)&uart_imx_class},
3203bb693afSIan Lepore 	{"fsl,imx25-uart",	(uintptr_t)&uart_imx_class},
3213bb693afSIan Lepore 	{"fsl,imx21-uart",	(uintptr_t)&uart_imx_class},
3223bb693afSIan Lepore 	{NULL,			(uintptr_t)NULL},
3233bb693afSIan Lepore };
3243bb693afSIan Lepore UART_FDT_CLASS_AND_DEVICE(compat_data);
3253bb693afSIan Lepore 
326a2c472e7SAleksandr Rybalko #define	SIGCHG(c, i, s, d)				\
327a2c472e7SAleksandr Rybalko 	if (c) {					\
328a2c472e7SAleksandr Rybalko 		i |= (i & s) ? s : s | d;		\
329a2c472e7SAleksandr Rybalko 	} else {					\
330a2c472e7SAleksandr Rybalko 		i = (i & s) ? (i & ~s) | d : i;		\
331a2c472e7SAleksandr Rybalko 	}
332a2c472e7SAleksandr Rybalko 
33394bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
33494bc2117SOleksandr Tymoshenko static int
33594bc2117SOleksandr Tymoshenko imx_uart_setup_clocks(struct uart_softc *sc)
33694bc2117SOleksandr Tymoshenko {
33794bc2117SOleksandr Tymoshenko 	struct uart_bas *bas;
33894bc2117SOleksandr Tymoshenko 	clk_t ipgclk, perclk;
33994bc2117SOleksandr Tymoshenko 	uint64_t freq;
34094bc2117SOleksandr Tymoshenko 	int error;
34194bc2117SOleksandr Tymoshenko 
34294bc2117SOleksandr Tymoshenko 	bas = &sc->sc_bas;
34394bc2117SOleksandr Tymoshenko 
34494bc2117SOleksandr Tymoshenko 	if (clk_get_by_ofw_name(sc->sc_dev, 0, "ipg", &ipgclk) != 0)
34594bc2117SOleksandr Tymoshenko 		return (ENOENT);
34694bc2117SOleksandr Tymoshenko 
34794bc2117SOleksandr Tymoshenko 	if (clk_get_by_ofw_name(sc->sc_dev, 0, "per", &perclk) != 0) {
34894bc2117SOleksandr Tymoshenko 		return (ENOENT);
34994bc2117SOleksandr Tymoshenko 	}
35094bc2117SOleksandr Tymoshenko 
35194bc2117SOleksandr Tymoshenko 	error = clk_enable(ipgclk);
35294bc2117SOleksandr Tymoshenko 	if (error != 0) {
35394bc2117SOleksandr Tymoshenko 		device_printf(sc->sc_dev, "cannot enable ipg clock\n");
35494bc2117SOleksandr Tymoshenko 		return (error);
35594bc2117SOleksandr Tymoshenko 	}
35694bc2117SOleksandr Tymoshenko 
35794bc2117SOleksandr Tymoshenko 	error = clk_get_freq(perclk, &freq);
35894bc2117SOleksandr Tymoshenko 	if (error != 0) {
35994bc2117SOleksandr Tymoshenko 		device_printf(sc->sc_dev, "cannot get frequency\n");
36094bc2117SOleksandr Tymoshenko 		return (error);
36194bc2117SOleksandr Tymoshenko 	}
36294bc2117SOleksandr Tymoshenko 
36394bc2117SOleksandr Tymoshenko 	bas->rclk = (uint32_t)freq;
36494bc2117SOleksandr Tymoshenko 
36594bc2117SOleksandr Tymoshenko 	return (0);
36694bc2117SOleksandr Tymoshenko }
36794bc2117SOleksandr Tymoshenko #endif
36894bc2117SOleksandr Tymoshenko 
369a2c472e7SAleksandr Rybalko static int
370a2c472e7SAleksandr Rybalko imx_uart_bus_attach(struct uart_softc *sc)
371a2c472e7SAleksandr Rybalko {
372a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
373a2c472e7SAleksandr Rybalko 	struct uart_devinfo *di;
374a2c472e7SAleksandr Rybalko 
375a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
37694bc2117SOleksandr Tymoshenko 
37794bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
37894bc2117SOleksandr Tymoshenko 	int error = imx_uart_setup_clocks(sc);
37994bc2117SOleksandr Tymoshenko 	if (error)
38094bc2117SOleksandr Tymoshenko 		return (error);
38194bc2117SOleksandr Tymoshenko #else
38294bc2117SOleksandr Tymoshenko 	bas->rclk = imx_ccm_uart_hz();
38394bc2117SOleksandr Tymoshenko #endif
38494bc2117SOleksandr Tymoshenko 
385a2c472e7SAleksandr Rybalko 	if (sc->sc_sysdev != NULL) {
386a2c472e7SAleksandr Rybalko 		di = sc->sc_sysdev;
387a2c472e7SAleksandr Rybalko 		imx_uart_init(bas, di->baudrate, di->databits, di->stopbits,
388a2c472e7SAleksandr Rybalko 		    di->parity);
389a2c472e7SAleksandr Rybalko 	} else {
390a2c472e7SAleksandr Rybalko 		imx_uart_init(bas, 115200, 8, 1, 0);
391a2c472e7SAleksandr Rybalko 	}
392a2c472e7SAleksandr Rybalko 
393a2c472e7SAleksandr Rybalko 	(void)imx_uart_bus_getsig(sc);
394a2c472e7SAleksandr Rybalko 
3950dc54d18SIan Lepore 	/* Clear all pending interrupts. */
3960dc54d18SIan Lepore 	SETREG(bas, REG(USR1), 0xffff);
3970dc54d18SIan Lepore 	SETREG(bas, REG(USR2), 0xffff);
3980dc54d18SIan Lepore 
3990dc54d18SIan Lepore 	DIS(bas, UCR4, DREN);
4000dc54d18SIan Lepore 	ENA(bas, UCR1, RRDYEN);
401a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, IDEN);
402a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, RXDSEN);
4030dc54d18SIan Lepore 	ENA(bas, UCR2, ATEN);
404a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, TXMPTYEN);
405a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, TRDYEN);
406a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, TCEN);
407a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, OREN);
408a2c472e7SAleksandr Rybalko 	ENA(bas, UCR4, BKEN);
409a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, WKEN);
410a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, ADEN);
411a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, ACIEN);
412a2c472e7SAleksandr Rybalko 	DIS(bas, UCR2, ESCI);
413a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, ENIRI);
414a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, AIRINTEN);
415a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, AWAKEN);
416a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, FRAERREN);
417a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, PARERREN);
418a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, RTSDEN);
419a2c472e7SAleksandr Rybalko 	DIS(bas, UCR2, RTSEN);
420a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, DTREN);
421a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, RI);
422a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, DCD);
423a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, DTRDEN);
4242d40ec16SIan Lepore 	ENA(bas, UCR2, IRTS);
4252d40ec16SIan Lepore 	ENA(bas, UCR3, RXDMUXSEL);
426a2c472e7SAleksandr Rybalko 
427a2c472e7SAleksandr Rybalko 	return (0);
428a2c472e7SAleksandr Rybalko }
429a2c472e7SAleksandr Rybalko 
430a2c472e7SAleksandr Rybalko static int
431a2c472e7SAleksandr Rybalko imx_uart_bus_detach(struct uart_softc *sc)
432a2c472e7SAleksandr Rybalko {
433a2c472e7SAleksandr Rybalko 
434a2c472e7SAleksandr Rybalko 	SETREG(&sc->sc_bas, REG(UCR4), 0);
435a2c472e7SAleksandr Rybalko 
436a2c472e7SAleksandr Rybalko 	return (0);
437a2c472e7SAleksandr Rybalko }
438a2c472e7SAleksandr Rybalko 
439a2c472e7SAleksandr Rybalko static int
440a2c472e7SAleksandr Rybalko imx_uart_bus_flush(struct uart_softc *sc, int what)
441a2c472e7SAleksandr Rybalko {
442a2c472e7SAleksandr Rybalko 
443a2c472e7SAleksandr Rybalko 	/* TODO */
444a2c472e7SAleksandr Rybalko 	return (0);
445a2c472e7SAleksandr Rybalko }
446a2c472e7SAleksandr Rybalko 
447a2c472e7SAleksandr Rybalko static int
448a2c472e7SAleksandr Rybalko imx_uart_bus_getsig(struct uart_softc *sc)
449a2c472e7SAleksandr Rybalko {
450a2c472e7SAleksandr Rybalko 	uint32_t new, old, sig;
451a2c472e7SAleksandr Rybalko 	uint8_t bes;
452a2c472e7SAleksandr Rybalko 
453a2c472e7SAleksandr Rybalko 	do {
454a2c472e7SAleksandr Rybalko 		old = sc->sc_hwsig;
455a2c472e7SAleksandr Rybalko 		sig = old;
456a2c472e7SAleksandr Rybalko 		uart_lock(sc->sc_hwmtx);
457a2c472e7SAleksandr Rybalko 		bes = GETREG(&sc->sc_bas, REG(USR2));
458a2c472e7SAleksandr Rybalko 		uart_unlock(sc->sc_hwmtx);
459a2c472e7SAleksandr Rybalko 		/* XXX: chip can show delta */
460a2c472e7SAleksandr Rybalko 		SIGCHG(bes & FLD(USR2, DCDIN), sig, SER_DCD, SER_DDCD);
461a2c472e7SAleksandr Rybalko 		new = sig & ~SER_MASK_DELTA;
462a2c472e7SAleksandr Rybalko 	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
463a2c472e7SAleksandr Rybalko 
464a2c472e7SAleksandr Rybalko 	return (sig);
465a2c472e7SAleksandr Rybalko }
466a2c472e7SAleksandr Rybalko 
467a2c472e7SAleksandr Rybalko static int
468a2c472e7SAleksandr Rybalko imx_uart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
469a2c472e7SAleksandr Rybalko {
470a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
471a2c472e7SAleksandr Rybalko 	int error;
472a2c472e7SAleksandr Rybalko 
473a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
474a2c472e7SAleksandr Rybalko 	error = 0;
475a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
476a2c472e7SAleksandr Rybalko 	switch (request) {
477a2c472e7SAleksandr Rybalko 	case UART_IOCTL_BREAK:
478a2c472e7SAleksandr Rybalko 		/* TODO */
479a2c472e7SAleksandr Rybalko 		break;
480a2c472e7SAleksandr Rybalko 	case UART_IOCTL_BAUD:
481c3f0f284SIan Lepore 		*(u_int*)data = imx_uart_getbaud(bas);
482a2c472e7SAleksandr Rybalko 		break;
483a2c472e7SAleksandr Rybalko 	default:
484a2c472e7SAleksandr Rybalko 		error = EINVAL;
485a2c472e7SAleksandr Rybalko 		break;
486a2c472e7SAleksandr Rybalko 	}
487a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
488a2c472e7SAleksandr Rybalko 
489a2c472e7SAleksandr Rybalko 	return (error);
490a2c472e7SAleksandr Rybalko }
491a2c472e7SAleksandr Rybalko 
492a2c472e7SAleksandr Rybalko static int
493a2c472e7SAleksandr Rybalko imx_uart_bus_ipend(struct uart_softc *sc)
494a2c472e7SAleksandr Rybalko {
495a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
496a2c472e7SAleksandr Rybalko 	int ipend;
497a2c472e7SAleksandr Rybalko 	uint32_t usr1, usr2;
4980dc54d18SIan Lepore 	uint32_t ucr1, ucr2, ucr4;
499a2c472e7SAleksandr Rybalko 
500a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
501a2c472e7SAleksandr Rybalko 	ipend = 0;
502a2c472e7SAleksandr Rybalko 
503a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
504a2c472e7SAleksandr Rybalko 
505a2c472e7SAleksandr Rybalko 	/* Read pending interrupts */
506a2c472e7SAleksandr Rybalko 	usr1 = GETREG(bas, REG(USR1));
507a2c472e7SAleksandr Rybalko 	usr2 = GETREG(bas, REG(USR2));
508a2c472e7SAleksandr Rybalko 	/* ACK interrupts */
509a2c472e7SAleksandr Rybalko 	SETREG(bas, REG(USR1), usr1);
510a2c472e7SAleksandr Rybalko 	SETREG(bas, REG(USR2), usr2);
511a2c472e7SAleksandr Rybalko 
512a2c472e7SAleksandr Rybalko 	ucr1 = GETREG(bas, REG(UCR1));
5130dc54d18SIan Lepore 	ucr2 = GETREG(bas, REG(UCR2));
514a2c472e7SAleksandr Rybalko 	ucr4 = GETREG(bas, REG(UCR4));
515a2c472e7SAleksandr Rybalko 
5160dc54d18SIan Lepore 	/* If we have reached tx low-water, we can tx some more now. */
5170dc54d18SIan Lepore 	if ((usr1 & FLD(USR1, TRDY)) && (ucr1 & FLD(UCR1, TRDYEN))) {
5180dc54d18SIan Lepore 		DIS(bas, UCR1, TRDYEN);
519a2c472e7SAleksandr Rybalko 		ipend |= SER_INT_TXIDLE;
520a2c472e7SAleksandr Rybalko 	}
5210dc54d18SIan Lepore 
5220dc54d18SIan Lepore 	/*
5230dc54d18SIan Lepore 	 * If we have reached the rx high-water, or if there are bytes in the rx
5240dc54d18SIan Lepore 	 * fifo and no new data has arrived for 8 character periods (aging
5250dc54d18SIan Lepore 	 * timer), we have input data to process.
5260dc54d18SIan Lepore 	 */
5270dc54d18SIan Lepore 	if (((usr1 & FLD(USR1, RRDY)) && (ucr1 & FLD(UCR1, RRDYEN))) ||
5280dc54d18SIan Lepore 	    ((usr1 & FLD(USR1, AGTIM)) && (ucr2 & FLD(UCR2, ATEN)))) {
5290dc54d18SIan Lepore 		DIS(bas, UCR1, RRDYEN);
5300dc54d18SIan Lepore 		DIS(bas, UCR2, ATEN);
531a2c472e7SAleksandr Rybalko 		ipend |= SER_INT_RXREADY;
532a2c472e7SAleksandr Rybalko 	}
5330dc54d18SIan Lepore 
5340dc54d18SIan Lepore 	/* A break can come in at any time, it never gets disabled. */
535a2c472e7SAleksandr Rybalko 	if ((usr2 & FLD(USR2, BRCD)) && (ucr4 & FLD(UCR4, BKEN)))
536a2c472e7SAleksandr Rybalko 		ipend |= SER_INT_BREAK;
537a2c472e7SAleksandr Rybalko 
538a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
539a2c472e7SAleksandr Rybalko 
540a2c472e7SAleksandr Rybalko 	return (ipend);
541a2c472e7SAleksandr Rybalko }
542a2c472e7SAleksandr Rybalko 
543a2c472e7SAleksandr Rybalko static int
544a2c472e7SAleksandr Rybalko imx_uart_bus_param(struct uart_softc *sc, int baudrate, int databits,
545a2c472e7SAleksandr Rybalko     int stopbits, int parity)
546a2c472e7SAleksandr Rybalko {
547a2c472e7SAleksandr Rybalko 
548a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
549a2c472e7SAleksandr Rybalko 	imx_uart_init(&sc->sc_bas, baudrate, databits, stopbits, parity);
550a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
551a2c472e7SAleksandr Rybalko 	return (0);
552a2c472e7SAleksandr Rybalko }
553a2c472e7SAleksandr Rybalko 
554a2c472e7SAleksandr Rybalko static int
555a2c472e7SAleksandr Rybalko imx_uart_bus_probe(struct uart_softc *sc)
556a2c472e7SAleksandr Rybalko {
557a2c472e7SAleksandr Rybalko 	int error;
558a2c472e7SAleksandr Rybalko 
559a2c472e7SAleksandr Rybalko 	error = imx_uart_probe(&sc->sc_bas);
560a2c472e7SAleksandr Rybalko 	if (error)
561a2c472e7SAleksandr Rybalko 		return (error);
562a2c472e7SAleksandr Rybalko 
5630dc54d18SIan Lepore 	/*
5640dc54d18SIan Lepore 	 * On input we can read up to the full fifo size at once.  On output, we
5650dc54d18SIan Lepore 	 * want to write only as much as the programmed tx low water level,
5660dc54d18SIan Lepore 	 * because that's all we can be certain we have room for in the fifo
5670dc54d18SIan Lepore 	 * when we get a tx-ready interrupt.
5680dc54d18SIan Lepore 	 */
5690dc54d18SIan Lepore 	sc->sc_rxfifosz = IMX_FIFOSZ;
5700dc54d18SIan Lepore 	sc->sc_txfifosz = IMX_TXFIFO_LEVEL;
5714d7abca0SIan Lepore 
5723329109cSRui Paulo 	device_set_desc(sc->sc_dev, "Freescale i.MX UART");
573a2c472e7SAleksandr Rybalko 	return (0);
574a2c472e7SAleksandr Rybalko }
575a2c472e7SAleksandr Rybalko 
576a2c472e7SAleksandr Rybalko static int
577a2c472e7SAleksandr Rybalko imx_uart_bus_receive(struct uart_softc *sc)
578a2c472e7SAleksandr Rybalko {
579a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
580a2c472e7SAleksandr Rybalko 	int xc, out;
581a2c472e7SAleksandr Rybalko 
582a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
583a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
584a2c472e7SAleksandr Rybalko 
5850dc54d18SIan Lepore 	/*
5860dc54d18SIan Lepore 	 * Empty the rx fifo.  We get the RRDY interrupt when IMX_RXFIFO_LEVEL
5870dc54d18SIan Lepore 	 * (the rx high-water level) is reached, but we set sc_rxfifosz to the
5880dc54d18SIan Lepore 	 * full hardware fifo size, so we can safely process however much is
5890dc54d18SIan Lepore 	 * there, not just the highwater size.
5900dc54d18SIan Lepore 	 */
591a2c472e7SAleksandr Rybalko 	while (IS(bas, USR2, RDR)) {
592a2c472e7SAleksandr Rybalko 		if (uart_rx_full(sc)) {
593a2c472e7SAleksandr Rybalko 			/* No space left in input buffer */
594a2c472e7SAleksandr Rybalko 			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
595a2c472e7SAleksandr Rybalko 			break;
596a2c472e7SAleksandr Rybalko 		}
597a2c472e7SAleksandr Rybalko 		xc = GETREG(bas, REG(URXD));
598a2c472e7SAleksandr Rybalko 		out = xc & 0x000000ff;
599a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, FRMERR))
600a2c472e7SAleksandr Rybalko 			out |= UART_STAT_FRAMERR;
601a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, PRERR))
602a2c472e7SAleksandr Rybalko 			out |= UART_STAT_PARERR;
603a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, OVRRUN))
604a2c472e7SAleksandr Rybalko 			out |= UART_STAT_OVERRUN;
605a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, BRK))
606a2c472e7SAleksandr Rybalko 			out |= UART_STAT_BREAK;
607a2c472e7SAleksandr Rybalko 
608a2c472e7SAleksandr Rybalko 		uart_rx_put(sc, out);
609a2c472e7SAleksandr Rybalko 	}
6100dc54d18SIan Lepore 	ENA(bas, UCR1, RRDYEN);
6110dc54d18SIan Lepore 	ENA(bas, UCR2, ATEN);
612a2c472e7SAleksandr Rybalko 
613a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
614a2c472e7SAleksandr Rybalko 	return (0);
615a2c472e7SAleksandr Rybalko }
616a2c472e7SAleksandr Rybalko 
617a2c472e7SAleksandr Rybalko static int
618a2c472e7SAleksandr Rybalko imx_uart_bus_setsig(struct uart_softc *sc, int sig)
619a2c472e7SAleksandr Rybalko {
620a2c472e7SAleksandr Rybalko 
621a2c472e7SAleksandr Rybalko 	return (0);
622a2c472e7SAleksandr Rybalko }
623a2c472e7SAleksandr Rybalko 
624a2c472e7SAleksandr Rybalko static int
625a2c472e7SAleksandr Rybalko imx_uart_bus_transmit(struct uart_softc *sc)
626a2c472e7SAleksandr Rybalko {
627a2c472e7SAleksandr Rybalko 	struct uart_bas *bas = &sc->sc_bas;
628a2c472e7SAleksandr Rybalko 	int i;
629a2c472e7SAleksandr Rybalko 
630a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
631a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
632a2c472e7SAleksandr Rybalko 
6330dc54d18SIan Lepore 	/*
6340dc54d18SIan Lepore 	 * Fill the tx fifo.  The uart core puts at most IMX_TXFIFO_LEVEL bytes
6350dc54d18SIan Lepore 	 * into the txbuf (because that's what sc_txfifosz is set to), and
6360dc54d18SIan Lepore 	 * because we got the TRDY (low-water reached) interrupt we know at
6370dc54d18SIan Lepore 	 * least that much space is available in the fifo.
6380dc54d18SIan Lepore 	 */
639a2c472e7SAleksandr Rybalko 	for (i = 0; i < sc->sc_txdatasz; i++) {
640a2c472e7SAleksandr Rybalko 		SETREG(bas, REG(UTXD), sc->sc_txbuf[i] & 0xff);
641a2c472e7SAleksandr Rybalko 	}
642a2c472e7SAleksandr Rybalko 	sc->sc_txbusy = 1;
6430dc54d18SIan Lepore 	ENA(bas, UCR1, TRDYEN);
644a2c472e7SAleksandr Rybalko 
645a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
646a2c472e7SAleksandr Rybalko 
647a2c472e7SAleksandr Rybalko 	return (0);
648a2c472e7SAleksandr Rybalko }
649d76a1ef4SWarner Losh 
650d76a1ef4SWarner Losh static void
651d76a1ef4SWarner Losh imx_uart_bus_grab(struct uart_softc *sc)
652d76a1ef4SWarner Losh {
653d76a1ef4SWarner Losh 	struct uart_bas *bas = &sc->sc_bas;
654d76a1ef4SWarner Losh 
655d76a1ef4SWarner Losh 	bas = &sc->sc_bas;
656d76a1ef4SWarner Losh 	uart_lock(sc->sc_hwmtx);
6570dc54d18SIan Lepore 	DIS(bas, UCR1, RRDYEN);
6580dc54d18SIan Lepore 	DIS(bas, UCR2, ATEN);
659d76a1ef4SWarner Losh 	uart_unlock(sc->sc_hwmtx);
660d76a1ef4SWarner Losh }
661d76a1ef4SWarner Losh 
662d76a1ef4SWarner Losh static void
663d76a1ef4SWarner Losh imx_uart_bus_ungrab(struct uart_softc *sc)
664d76a1ef4SWarner Losh {
665d76a1ef4SWarner Losh 	struct uart_bas *bas = &sc->sc_bas;
666d76a1ef4SWarner Losh 
667d76a1ef4SWarner Losh 	bas = &sc->sc_bas;
668d76a1ef4SWarner Losh 	uart_lock(sc->sc_hwmtx);
6690dc54d18SIan Lepore 	ENA(bas, UCR1, RRDYEN);
6700dc54d18SIan Lepore 	ENA(bas, UCR2, ATEN);
671d76a1ef4SWarner Losh 	uart_unlock(sc->sc_hwmtx);
672d76a1ef4SWarner Losh }
673