xref: /freebsd/sys/dev/uart/uart_dev_imx.c (revision be82b3a0)
1a2c472e7SAleksandr Rybalko /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_ddb.h"
33a2c472e7SAleksandr Rybalko 
34a2c472e7SAleksandr Rybalko #include <sys/param.h>
35a2c472e7SAleksandr Rybalko #include <sys/systm.h>
36a2c472e7SAleksandr Rybalko #include <sys/bus.h>
37a2c472e7SAleksandr Rybalko #include <sys/conf.h>
38a2c472e7SAleksandr Rybalko #include <sys/kdb.h>
39a2c472e7SAleksandr Rybalko #include <machine/bus.h>
40a2c472e7SAleksandr Rybalko 
41a2c472e7SAleksandr Rybalko #include <dev/uart/uart.h>
42a2c472e7SAleksandr Rybalko #include <dev/uart/uart_cpu.h>
433bb693afSIan Lepore #include <dev/uart/uart_cpu_fdt.h>
44a2c472e7SAleksandr Rybalko #include <dev/uart/uart_bus.h>
45a90f1975SIan Lepore #include <dev/uart/uart_dev_imx.h>
4694bc2117SOleksandr Tymoshenko 
474f64df4eSEmmanuel Vadot #if defined(__aarch64__)
4894bc2117SOleksandr Tymoshenko #define	IMX_ENABLE_CLOCKS
4994bc2117SOleksandr Tymoshenko #endif
5094bc2117SOleksandr Tymoshenko 
5194bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
52be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
5394bc2117SOleksandr Tymoshenko #endif
5494bc2117SOleksandr Tymoshenko 
55a2c472e7SAleksandr Rybalko #include "uart_if.h"
562d40ec16SIan Lepore 
572d40ec16SIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
582d40ec16SIan Lepore 
59a2c472e7SAleksandr Rybalko /*
600dc54d18SIan Lepore  * The hardare FIFOs are 32 bytes.  We want an interrupt when there are 24 bytes
610dc54d18SIan Lepore  * available to read or space for 24 more bytes to write.  While 8 bytes of
620dc54d18SIan Lepore  * slack before over/underrun might seem excessive, the hardware can run at
630dc54d18SIan Lepore  * 5mbps, which means 2uS per char, so at full speed 8 bytes provides only 16uS
640dc54d18SIan Lepore  * to get into the interrupt handler and service the fifo.
650dc54d18SIan Lepore  */
660dc54d18SIan Lepore #define	IMX_FIFOSZ		32
670dc54d18SIan Lepore #define	IMX_RXFIFO_LEVEL	24
680dc54d18SIan Lepore #define	IMX_TXFIFO_LEVEL	24
690dc54d18SIan Lepore 
700dc54d18SIan Lepore /*
71a2c472e7SAleksandr Rybalko  * Low-level UART interface.
72a2c472e7SAleksandr Rybalko  */
73a2c472e7SAleksandr Rybalko static int imx_uart_probe(struct uart_bas *bas);
74a2c472e7SAleksandr Rybalko static void imx_uart_init(struct uart_bas *bas, int, int, int, int);
75a2c472e7SAleksandr Rybalko static void imx_uart_term(struct uart_bas *bas);
76a2c472e7SAleksandr Rybalko static void imx_uart_putc(struct uart_bas *bas, int);
77a2c472e7SAleksandr Rybalko static int imx_uart_rxready(struct uart_bas *bas);
78a2c472e7SAleksandr Rybalko static int imx_uart_getc(struct uart_bas *bas, struct mtx *);
79a2c472e7SAleksandr Rybalko 
80a2c472e7SAleksandr Rybalko static struct uart_ops uart_imx_uart_ops = {
81a2c472e7SAleksandr Rybalko 	.probe = imx_uart_probe,
82a2c472e7SAleksandr Rybalko 	.init = imx_uart_init,
83a2c472e7SAleksandr Rybalko 	.term = imx_uart_term,
84a2c472e7SAleksandr Rybalko 	.putc = imx_uart_putc,
85a2c472e7SAleksandr Rybalko 	.rxready = imx_uart_rxready,
86a2c472e7SAleksandr Rybalko 	.getc = imx_uart_getc,
87a2c472e7SAleksandr Rybalko };
88a2c472e7SAleksandr Rybalko 
892d40ec16SIan Lepore #if 0 /* Handy when debugging. */
902d40ec16SIan Lepore static void
912d40ec16SIan Lepore dumpregs(struct uart_bas *bas, const char * msg)
922d40ec16SIan Lepore {
932d40ec16SIan Lepore 
942d40ec16SIan Lepore 	if (!bootverbose)
952d40ec16SIan Lepore 		return;
962d40ec16SIan Lepore 	printf("%s bsh 0x%08lx UCR1 0x%08x UCR2 0x%08x "
972d40ec16SIan Lepore 		"UCR3 0x%08x UCR4 0x%08x USR1 0x%08x USR2 0x%08x\n",
982d40ec16SIan Lepore 	    msg, bas->bsh,
992d40ec16SIan Lepore 	    GETREG(bas, REG(UCR1)), GETREG(bas, REG(UCR2)),
1002d40ec16SIan Lepore 	    GETREG(bas, REG(UCR3)), GETREG(bas, REG(UCR4)),
1012d40ec16SIan Lepore 	    GETREG(bas, REG(USR1)), GETREG(bas, REG(USR2)));
1022d40ec16SIan Lepore }
1032d40ec16SIan Lepore #endif
1042d40ec16SIan Lepore 
105a2c472e7SAleksandr Rybalko static int
imx_uart_probe(struct uart_bas * bas)106a2c472e7SAleksandr Rybalko imx_uart_probe(struct uart_bas *bas)
107a2c472e7SAleksandr Rybalko {
108a2c472e7SAleksandr Rybalko 
109a2c472e7SAleksandr Rybalko 	return (0);
110a2c472e7SAleksandr Rybalko }
111a2c472e7SAleksandr Rybalko 
112c3f0f284SIan Lepore static u_int
imx_uart_getbaud(struct uart_bas * bas)113c3f0f284SIan Lepore imx_uart_getbaud(struct uart_bas *bas)
114c3f0f284SIan Lepore {
115c3f0f284SIan Lepore 	uint32_t rate, ubir, ubmr;
116c3f0f284SIan Lepore 	u_int baud, blo, bhi, i;
117c3f0f284SIan Lepore 	static const u_int predivs[] = {6, 5, 4, 3, 2, 1, 7, 1};
118c3f0f284SIan Lepore 	static const u_int std_rates[] = {
119c3f0f284SIan Lepore 		9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600
120c3f0f284SIan Lepore 	};
121c3f0f284SIan Lepore 
122c3f0f284SIan Lepore 	/*
123c3f0f284SIan Lepore 	 * Get the baud rate the hardware is programmed for, then search the
124c3f0f284SIan Lepore 	 * table of standard baud rates for a number that's within 3% of the
125c3f0f284SIan Lepore 	 * actual rate the hardware is programmed for.  It's more comforting to
126c3f0f284SIan Lepore 	 * see that your console is running at 115200 than 114942.  Note that
127c3f0f284SIan Lepore 	 * here we cannot make a simplifying assumption that the predivider and
128c3f0f284SIan Lepore 	 * numerator are 1 (like we do when setting the baud rate), because we
129c3f0f284SIan Lepore 	 * don't know what u-boot might have set up.
130c3f0f284SIan Lepore 	 */
131c3f0f284SIan Lepore 	i = (GETREG(bas, REG(UFCR)) & IMXUART_UFCR_RFDIV_MASK) >>
132c3f0f284SIan Lepore 	    IMXUART_UFCR_RFDIV_SHIFT;
13394bc2117SOleksandr Tymoshenko 	rate = bas->rclk / predivs[i];
134c3f0f284SIan Lepore 	ubir = GETREG(bas, REG(UBIR)) + 1;
135c3f0f284SIan Lepore 	ubmr = GETREG(bas, REG(UBMR)) + 1;
136c3f0f284SIan Lepore 	baud = ((rate / 16 ) * ubir) / ubmr;
137c3f0f284SIan Lepore 
138c3f0f284SIan Lepore 	blo = (baud * 100) / 103;
139c3f0f284SIan Lepore 	bhi = (baud * 100) / 97;
140c3f0f284SIan Lepore 	for (i = 0; i < nitems(std_rates); i++) {
141c3f0f284SIan Lepore 		rate = std_rates[i];
142c3f0f284SIan Lepore 		if (rate >= blo && rate <= bhi) {
143c3f0f284SIan Lepore 			baud = rate;
144c3f0f284SIan Lepore 			break;
145c3f0f284SIan Lepore 		}
146c3f0f284SIan Lepore 	}
147c3f0f284SIan Lepore 
148c3f0f284SIan Lepore 	return (baud);
149c3f0f284SIan Lepore }
150c3f0f284SIan Lepore 
151a2c472e7SAleksandr Rybalko static void
imx_uart_init(struct uart_bas * bas,int baudrate,int databits,int stopbits,int parity)152a2c472e7SAleksandr Rybalko imx_uart_init(struct uart_bas *bas, int baudrate, int databits,
153a2c472e7SAleksandr Rybalko     int stopbits, int parity)
154a2c472e7SAleksandr Rybalko {
1552d40ec16SIan Lepore 	uint32_t baseclk, reg;
156a2c472e7SAleksandr Rybalko 
1572d40ec16SIan Lepore         /* Enable the device and the RX/TX channels. */
1582d40ec16SIan Lepore 	SET(bas, REG(UCR1), FLD(UCR1, UARTEN));
1592d40ec16SIan Lepore 	SET(bas, REG(UCR2), FLD(UCR2, RXEN) | FLD(UCR2, TXEN));
1602d40ec16SIan Lepore 
1612d40ec16SIan Lepore 	if (databits == 7)
1622d40ec16SIan Lepore 		DIS(bas, UCR2, WS);
1632d40ec16SIan Lepore 	else
1642d40ec16SIan Lepore 		ENA(bas, UCR2, WS);
1652d40ec16SIan Lepore 
1662d40ec16SIan Lepore 	if (stopbits == 2)
1672d40ec16SIan Lepore 		ENA(bas, UCR2, STPB);
1682d40ec16SIan Lepore 	else
1692d40ec16SIan Lepore 		DIS(bas, UCR2, STPB);
1702d40ec16SIan Lepore 
1712d40ec16SIan Lepore 	switch (parity) {
1722d40ec16SIan Lepore 	case UART_PARITY_ODD:
1732d40ec16SIan Lepore 		DIS(bas, UCR2, PROE);
1742d40ec16SIan Lepore 		ENA(bas, UCR2, PREN);
1752d40ec16SIan Lepore 		break;
1762d40ec16SIan Lepore 	case UART_PARITY_EVEN:
1772d40ec16SIan Lepore 		ENA(bas, UCR2, PROE);
1782d40ec16SIan Lepore 		ENA(bas, UCR2, PREN);
1792d40ec16SIan Lepore 		break;
1802d40ec16SIan Lepore 	case UART_PARITY_MARK:
1812d40ec16SIan Lepore 	case UART_PARITY_SPACE:
1822d40ec16SIan Lepore                 /* FALLTHROUGH: Hardware doesn't support mark/space. */
1832d40ec16SIan Lepore 	case UART_PARITY_NONE:
1842d40ec16SIan Lepore 	default:
1852d40ec16SIan Lepore 		DIS(bas, UCR2, PREN);
1862d40ec16SIan Lepore 		break;
1872d40ec16SIan Lepore 	}
1882d40ec16SIan Lepore 
1892d40ec16SIan Lepore 	/*
1902d40ec16SIan Lepore 	 * The hardware has an extremely flexible baud clock: it allows setting
1912d40ec16SIan Lepore 	 * both the numerator and denominator of the divider, as well as a
19249d0a4c3SIan Lepore 	 * separate pre-divider.  We simplify the problem of coming up with a
19349d0a4c3SIan Lepore 	 * workable pair of numbers by assuming a pre-divider and numerator of
19449d0a4c3SIan Lepore 	 * one because our base clock is so fast we can reach virtually any
19549d0a4c3SIan Lepore 	 * reasonable speed with a simple divisor.  The numerator value actually
19649d0a4c3SIan Lepore 	 * includes the 16x over-sampling (so a value of 16 means divide by 1);
19749d0a4c3SIan Lepore 	 * the register value is the numerator-1, so we have a hard-coded 15.
19849d0a4c3SIan Lepore 	 * Note that a quirk of the hardware requires that both UBIR and UBMR be
19949d0a4c3SIan Lepore 	 * set back to back in order for the change to take effect.
2002d40ec16SIan Lepore 	 */
20194bc2117SOleksandr Tymoshenko 	if ((baudrate > 0) && (bas->rclk != 0)) {
20294bc2117SOleksandr Tymoshenko 		baseclk = bas->rclk;
2032d40ec16SIan Lepore 		reg = GETREG(bas, REG(UFCR));
2042d40ec16SIan Lepore 		reg = (reg & ~IMXUART_UFCR_RFDIV_MASK) | IMXUART_UFCR_RFDIV_DIV1;
2052d40ec16SIan Lepore 		SETREG(bas, REG(UFCR), reg);
2062d40ec16SIan Lepore 		SETREG(bas, REG(UBIR), 15);
2072d40ec16SIan Lepore 		SETREG(bas, REG(UBMR), (baseclk / baudrate) - 1);
2082d40ec16SIan Lepore 	}
2090dc54d18SIan Lepore 
2100dc54d18SIan Lepore 	/*
2110dc54d18SIan Lepore 	 * Program the tx lowater and rx hiwater levels at which fifo-service
2120dc54d18SIan Lepore 	 * interrupts are signaled.  The tx value is interpetted as "when there
2130dc54d18SIan Lepore 	 * are only this many bytes remaining" (not "this many free").
2140dc54d18SIan Lepore 	 */
2150dc54d18SIan Lepore 	reg = GETREG(bas, REG(UFCR));
2160dc54d18SIan Lepore 	reg &= ~(IMXUART_UFCR_TXTL_MASK | IMXUART_UFCR_RXTL_MASK);
2170dc54d18SIan Lepore 	reg |= (IMX_FIFOSZ - IMX_TXFIFO_LEVEL) << IMXUART_UFCR_TXTL_SHIFT;
2180dc54d18SIan Lepore 	reg |= IMX_RXFIFO_LEVEL << IMXUART_UFCR_RXTL_SHIFT;
2190dc54d18SIan Lepore 	SETREG(bas, REG(UFCR), reg);
220a2c472e7SAleksandr Rybalko }
221a2c472e7SAleksandr Rybalko 
222a2c472e7SAleksandr Rybalko static void
imx_uart_term(struct uart_bas * bas)223a2c472e7SAleksandr Rybalko imx_uart_term(struct uart_bas *bas)
224a2c472e7SAleksandr Rybalko {
225a2c472e7SAleksandr Rybalko 
226a2c472e7SAleksandr Rybalko }
227a2c472e7SAleksandr Rybalko 
228a2c472e7SAleksandr Rybalko static void
imx_uart_putc(struct uart_bas * bas,int c)229a2c472e7SAleksandr Rybalko imx_uart_putc(struct uart_bas *bas, int c)
230a2c472e7SAleksandr Rybalko {
231a2c472e7SAleksandr Rybalko 
2320dc54d18SIan Lepore 	while (!(IS(bas, USR1, TRDY)))
233a2c472e7SAleksandr Rybalko 		;
234a2c472e7SAleksandr Rybalko 	SETREG(bas, REG(UTXD), c);
235a2c472e7SAleksandr Rybalko }
236a2c472e7SAleksandr Rybalko 
237a2c472e7SAleksandr Rybalko static int
imx_uart_rxready(struct uart_bas * bas)238a2c472e7SAleksandr Rybalko imx_uart_rxready(struct uart_bas *bas)
239a2c472e7SAleksandr Rybalko {
240a2c472e7SAleksandr Rybalko 
241a2c472e7SAleksandr Rybalko 	return ((IS(bas, USR2, RDR)) ? 1 : 0);
242a2c472e7SAleksandr Rybalko }
243a2c472e7SAleksandr Rybalko 
244a2c472e7SAleksandr Rybalko static int
imx_uart_getc(struct uart_bas * bas,struct mtx * hwmtx)245a2c472e7SAleksandr Rybalko imx_uart_getc(struct uart_bas *bas, struct mtx *hwmtx)
246a2c472e7SAleksandr Rybalko {
247a2c472e7SAleksandr Rybalko 	int c;
248a2c472e7SAleksandr Rybalko 
249a2c472e7SAleksandr Rybalko 	uart_lock(hwmtx);
250a2c472e7SAleksandr Rybalko 	while (!(IS(bas, USR2, RDR)))
251a2c472e7SAleksandr Rybalko 		;
252a2c472e7SAleksandr Rybalko 
253a2c472e7SAleksandr Rybalko 	c = GETREG(bas, REG(URXD));
254a2c472e7SAleksandr Rybalko 	uart_unlock(hwmtx);
255a2c472e7SAleksandr Rybalko #if defined(KDB)
256a2c472e7SAleksandr Rybalko 	if (c & FLD(URXD, BRK)) {
257a2c472e7SAleksandr Rybalko 		if (kdb_break())
258a2c472e7SAleksandr Rybalko 			return (0);
259a2c472e7SAleksandr Rybalko 	}
260a2c472e7SAleksandr Rybalko #endif
261a2c472e7SAleksandr Rybalko 	return (c & 0xff);
262a2c472e7SAleksandr Rybalko }
263a2c472e7SAleksandr Rybalko 
264a2c472e7SAleksandr Rybalko /*
265a2c472e7SAleksandr Rybalko  * High-level UART interface.
266a2c472e7SAleksandr Rybalko  */
267a2c472e7SAleksandr Rybalko struct imx_uart_softc {
268a2c472e7SAleksandr Rybalko 	struct uart_softc base;
269a2c472e7SAleksandr Rybalko };
270a2c472e7SAleksandr Rybalko 
271a2c472e7SAleksandr Rybalko static int imx_uart_bus_attach(struct uart_softc *);
272a2c472e7SAleksandr Rybalko static int imx_uart_bus_detach(struct uart_softc *);
273a2c472e7SAleksandr Rybalko static int imx_uart_bus_flush(struct uart_softc *, int);
274a2c472e7SAleksandr Rybalko static int imx_uart_bus_getsig(struct uart_softc *);
275a2c472e7SAleksandr Rybalko static int imx_uart_bus_ioctl(struct uart_softc *, int, intptr_t);
276a2c472e7SAleksandr Rybalko static int imx_uart_bus_ipend(struct uart_softc *);
277a2c472e7SAleksandr Rybalko static int imx_uart_bus_param(struct uart_softc *, int, int, int, int);
278a2c472e7SAleksandr Rybalko static int imx_uart_bus_probe(struct uart_softc *);
279a2c472e7SAleksandr Rybalko static int imx_uart_bus_receive(struct uart_softc *);
280a2c472e7SAleksandr Rybalko static int imx_uart_bus_setsig(struct uart_softc *, int);
281a2c472e7SAleksandr Rybalko static int imx_uart_bus_transmit(struct uart_softc *);
282d76a1ef4SWarner Losh static void imx_uart_bus_grab(struct uart_softc *);
283d76a1ef4SWarner Losh static void imx_uart_bus_ungrab(struct uart_softc *);
284a2c472e7SAleksandr Rybalko 
285a2c472e7SAleksandr Rybalko static kobj_method_t imx_uart_methods[] = {
286a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_attach,		imx_uart_bus_attach),
287a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_detach,		imx_uart_bus_detach),
288a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_flush,		imx_uart_bus_flush),
289a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_getsig,		imx_uart_bus_getsig),
290a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_ioctl,		imx_uart_bus_ioctl),
291a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_ipend,		imx_uart_bus_ipend),
292a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_param,		imx_uart_bus_param),
293a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_probe,		imx_uart_bus_probe),
294a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_receive,	imx_uart_bus_receive),
295a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_setsig,		imx_uart_bus_setsig),
296a2c472e7SAleksandr Rybalko 	KOBJMETHOD(uart_transmit,	imx_uart_bus_transmit),
297d76a1ef4SWarner Losh 	KOBJMETHOD(uart_grab,		imx_uart_bus_grab),
298d76a1ef4SWarner Losh 	KOBJMETHOD(uart_ungrab,		imx_uart_bus_ungrab),
299a2c472e7SAleksandr Rybalko 	{ 0, 0 }
300a2c472e7SAleksandr Rybalko };
301a2c472e7SAleksandr Rybalko 
3023bb693afSIan Lepore static struct uart_class uart_imx_class = {
303a2c472e7SAleksandr Rybalko 	"imx",
304a2c472e7SAleksandr Rybalko 	imx_uart_methods,
305a2c472e7SAleksandr Rybalko 	sizeof(struct imx_uart_softc),
306a2c472e7SAleksandr Rybalko 	.uc_ops = &uart_imx_uart_ops,
307a2c472e7SAleksandr Rybalko 	.uc_range = 0x100,
308405ada37SAndrew Turner 	.uc_rclk = 24000000, /* TODO: get value from CCM */
309405ada37SAndrew Turner 	.uc_rshift = 0
310a2c472e7SAleksandr Rybalko };
311a2c472e7SAleksandr Rybalko 
3123bb693afSIan Lepore static struct ofw_compat_data compat_data[] = {
3133bb693afSIan Lepore 	{"fsl,imx6q-uart",	(uintptr_t)&uart_imx_class},
3143bb693afSIan Lepore 	{"fsl,imx53-uart",	(uintptr_t)&uart_imx_class},
3153bb693afSIan Lepore 	{"fsl,imx51-uart",	(uintptr_t)&uart_imx_class},
3163bb693afSIan Lepore 	{"fsl,imx31-uart",	(uintptr_t)&uart_imx_class},
3173bb693afSIan Lepore 	{"fsl,imx27-uart",	(uintptr_t)&uart_imx_class},
3183bb693afSIan Lepore 	{"fsl,imx25-uart",	(uintptr_t)&uart_imx_class},
3193bb693afSIan Lepore 	{"fsl,imx21-uart",	(uintptr_t)&uart_imx_class},
3203bb693afSIan Lepore 	{NULL,			(uintptr_t)NULL},
3213bb693afSIan Lepore };
3223bb693afSIan Lepore UART_FDT_CLASS_AND_DEVICE(compat_data);
3233bb693afSIan Lepore 
324a2c472e7SAleksandr Rybalko #define	SIGCHG(c, i, s, d)				\
325a2c472e7SAleksandr Rybalko 	if (c) {					\
326a2c472e7SAleksandr Rybalko 		i |= (i & s) ? s : s | d;		\
327a2c472e7SAleksandr Rybalko 	} else {					\
328a2c472e7SAleksandr Rybalko 		i = (i & s) ? (i & ~s) | d : i;		\
329a2c472e7SAleksandr Rybalko 	}
330a2c472e7SAleksandr Rybalko 
33194bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
33294bc2117SOleksandr Tymoshenko static int
imx_uart_setup_clocks(struct uart_softc * sc)33394bc2117SOleksandr Tymoshenko imx_uart_setup_clocks(struct uart_softc *sc)
33494bc2117SOleksandr Tymoshenko {
33594bc2117SOleksandr Tymoshenko 	struct uart_bas *bas;
33694bc2117SOleksandr Tymoshenko 	clk_t ipgclk, perclk;
33794bc2117SOleksandr Tymoshenko 	uint64_t freq;
33894bc2117SOleksandr Tymoshenko 	int error;
33994bc2117SOleksandr Tymoshenko 
34094bc2117SOleksandr Tymoshenko 	bas = &sc->sc_bas;
34194bc2117SOleksandr Tymoshenko 
34294bc2117SOleksandr Tymoshenko 	if (clk_get_by_ofw_name(sc->sc_dev, 0, "ipg", &ipgclk) != 0)
34394bc2117SOleksandr Tymoshenko 		return (ENOENT);
34494bc2117SOleksandr Tymoshenko 
34594bc2117SOleksandr Tymoshenko 	if (clk_get_by_ofw_name(sc->sc_dev, 0, "per", &perclk) != 0) {
34694bc2117SOleksandr Tymoshenko 		return (ENOENT);
34794bc2117SOleksandr Tymoshenko 	}
34894bc2117SOleksandr Tymoshenko 
34994bc2117SOleksandr Tymoshenko 	error = clk_enable(ipgclk);
35094bc2117SOleksandr Tymoshenko 	if (error != 0) {
35194bc2117SOleksandr Tymoshenko 		device_printf(sc->sc_dev, "cannot enable ipg clock\n");
35294bc2117SOleksandr Tymoshenko 		return (error);
35394bc2117SOleksandr Tymoshenko 	}
35494bc2117SOleksandr Tymoshenko 
35594bc2117SOleksandr Tymoshenko 	error = clk_get_freq(perclk, &freq);
35694bc2117SOleksandr Tymoshenko 	if (error != 0) {
35794bc2117SOleksandr Tymoshenko 		device_printf(sc->sc_dev, "cannot get frequency\n");
35894bc2117SOleksandr Tymoshenko 		return (error);
35994bc2117SOleksandr Tymoshenko 	}
36094bc2117SOleksandr Tymoshenko 
36194bc2117SOleksandr Tymoshenko 	bas->rclk = (uint32_t)freq;
36294bc2117SOleksandr Tymoshenko 
36394bc2117SOleksandr Tymoshenko 	return (0);
36494bc2117SOleksandr Tymoshenko }
36594bc2117SOleksandr Tymoshenko #endif
36694bc2117SOleksandr Tymoshenko 
367a2c472e7SAleksandr Rybalko static int
imx_uart_bus_attach(struct uart_softc * sc)368a2c472e7SAleksandr Rybalko imx_uart_bus_attach(struct uart_softc *sc)
369a2c472e7SAleksandr Rybalko {
370a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
371a2c472e7SAleksandr Rybalko 	struct uart_devinfo *di;
372a2c472e7SAleksandr Rybalko 
373a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
37494bc2117SOleksandr Tymoshenko 
37594bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
37694bc2117SOleksandr Tymoshenko 	int error = imx_uart_setup_clocks(sc);
37794bc2117SOleksandr Tymoshenko 	if (error)
37894bc2117SOleksandr Tymoshenko 		return (error);
37994bc2117SOleksandr Tymoshenko #else
38094bc2117SOleksandr Tymoshenko 	bas->rclk = imx_ccm_uart_hz();
38194bc2117SOleksandr Tymoshenko #endif
38294bc2117SOleksandr Tymoshenko 
383a2c472e7SAleksandr Rybalko 	if (sc->sc_sysdev != NULL) {
384a2c472e7SAleksandr Rybalko 		di = sc->sc_sysdev;
385a2c472e7SAleksandr Rybalko 		imx_uart_init(bas, di->baudrate, di->databits, di->stopbits,
386a2c472e7SAleksandr Rybalko 		    di->parity);
387a2c472e7SAleksandr Rybalko 	} else {
388a2c472e7SAleksandr Rybalko 		imx_uart_init(bas, 115200, 8, 1, 0);
389a2c472e7SAleksandr Rybalko 	}
390a2c472e7SAleksandr Rybalko 
391a2c472e7SAleksandr Rybalko 	(void)imx_uart_bus_getsig(sc);
392a2c472e7SAleksandr Rybalko 
3930dc54d18SIan Lepore 	/* Clear all pending interrupts. */
3940dc54d18SIan Lepore 	SETREG(bas, REG(USR1), 0xffff);
3950dc54d18SIan Lepore 	SETREG(bas, REG(USR2), 0xffff);
3960dc54d18SIan Lepore 
3970dc54d18SIan Lepore 	DIS(bas, UCR4, DREN);
3980dc54d18SIan Lepore 	ENA(bas, UCR1, RRDYEN);
399a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, IDEN);
400a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, RXDSEN);
4010dc54d18SIan Lepore 	ENA(bas, UCR2, ATEN);
402a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, TXMPTYEN);
403a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, TRDYEN);
404a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, TCEN);
405a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, OREN);
406a2c472e7SAleksandr Rybalko 	ENA(bas, UCR4, BKEN);
407a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, WKEN);
408a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, ADEN);
409a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, ACIEN);
410a2c472e7SAleksandr Rybalko 	DIS(bas, UCR2, ESCI);
411a2c472e7SAleksandr Rybalko 	DIS(bas, UCR4, ENIRI);
412a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, AIRINTEN);
413a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, AWAKEN);
414a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, FRAERREN);
415a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, PARERREN);
416a2c472e7SAleksandr Rybalko 	DIS(bas, UCR1, RTSDEN);
417a2c472e7SAleksandr Rybalko 	DIS(bas, UCR2, RTSEN);
418a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, DTREN);
419a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, RI);
420a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, DCD);
421a2c472e7SAleksandr Rybalko 	DIS(bas, UCR3, DTRDEN);
4222d40ec16SIan Lepore 	ENA(bas, UCR2, IRTS);
4232d40ec16SIan Lepore 	ENA(bas, UCR3, RXDMUXSEL);
424a2c472e7SAleksandr Rybalko 
425a2c472e7SAleksandr Rybalko 	return (0);
426a2c472e7SAleksandr Rybalko }
427a2c472e7SAleksandr Rybalko 
428a2c472e7SAleksandr Rybalko static int
imx_uart_bus_detach(struct uart_softc * sc)429a2c472e7SAleksandr Rybalko imx_uart_bus_detach(struct uart_softc *sc)
430a2c472e7SAleksandr Rybalko {
431a2c472e7SAleksandr Rybalko 
432a2c472e7SAleksandr Rybalko 	SETREG(&sc->sc_bas, REG(UCR4), 0);
433a2c472e7SAleksandr Rybalko 
434a2c472e7SAleksandr Rybalko 	return (0);
435a2c472e7SAleksandr Rybalko }
436a2c472e7SAleksandr Rybalko 
437a2c472e7SAleksandr Rybalko static int
imx_uart_bus_flush(struct uart_softc * sc,int what)438a2c472e7SAleksandr Rybalko imx_uart_bus_flush(struct uart_softc *sc, int what)
439a2c472e7SAleksandr Rybalko {
440a2c472e7SAleksandr Rybalko 
441a2c472e7SAleksandr Rybalko 	/* TODO */
442a2c472e7SAleksandr Rybalko 	return (0);
443a2c472e7SAleksandr Rybalko }
444a2c472e7SAleksandr Rybalko 
445a2c472e7SAleksandr Rybalko static int
imx_uart_bus_getsig(struct uart_softc * sc)446a2c472e7SAleksandr Rybalko imx_uart_bus_getsig(struct uart_softc *sc)
447a2c472e7SAleksandr Rybalko {
448a2c472e7SAleksandr Rybalko 	uint32_t new, old, sig;
449a2c472e7SAleksandr Rybalko 	uint8_t bes;
450a2c472e7SAleksandr Rybalko 
451a2c472e7SAleksandr Rybalko 	do {
452a2c472e7SAleksandr Rybalko 		old = sc->sc_hwsig;
453a2c472e7SAleksandr Rybalko 		sig = old;
454a2c472e7SAleksandr Rybalko 		uart_lock(sc->sc_hwmtx);
455a2c472e7SAleksandr Rybalko 		bes = GETREG(&sc->sc_bas, REG(USR2));
456a2c472e7SAleksandr Rybalko 		uart_unlock(sc->sc_hwmtx);
457a2c472e7SAleksandr Rybalko 		/* XXX: chip can show delta */
458a2c472e7SAleksandr Rybalko 		SIGCHG(bes & FLD(USR2, DCDIN), sig, SER_DCD, SER_DDCD);
459a2c472e7SAleksandr Rybalko 		new = sig & ~SER_MASK_DELTA;
460a2c472e7SAleksandr Rybalko 	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
461a2c472e7SAleksandr Rybalko 
462a2c472e7SAleksandr Rybalko 	return (sig);
463a2c472e7SAleksandr Rybalko }
464a2c472e7SAleksandr Rybalko 
465a2c472e7SAleksandr Rybalko static int
imx_uart_bus_ioctl(struct uart_softc * sc,int request,intptr_t data)466a2c472e7SAleksandr Rybalko imx_uart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
467a2c472e7SAleksandr Rybalko {
468a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
469a2c472e7SAleksandr Rybalko 	int error;
470a2c472e7SAleksandr Rybalko 
471a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
472a2c472e7SAleksandr Rybalko 	error = 0;
473a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
474a2c472e7SAleksandr Rybalko 	switch (request) {
475a2c472e7SAleksandr Rybalko 	case UART_IOCTL_BREAK:
476a2c472e7SAleksandr Rybalko 		/* TODO */
477a2c472e7SAleksandr Rybalko 		break;
478a2c472e7SAleksandr Rybalko 	case UART_IOCTL_BAUD:
479c3f0f284SIan Lepore 		*(u_int*)data = imx_uart_getbaud(bas);
480a2c472e7SAleksandr Rybalko 		break;
481a2c472e7SAleksandr Rybalko 	default:
482a2c472e7SAleksandr Rybalko 		error = EINVAL;
483a2c472e7SAleksandr Rybalko 		break;
484a2c472e7SAleksandr Rybalko 	}
485a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
486a2c472e7SAleksandr Rybalko 
487a2c472e7SAleksandr Rybalko 	return (error);
488a2c472e7SAleksandr Rybalko }
489a2c472e7SAleksandr Rybalko 
490a2c472e7SAleksandr Rybalko static int
imx_uart_bus_ipend(struct uart_softc * sc)491a2c472e7SAleksandr Rybalko imx_uart_bus_ipend(struct uart_softc *sc)
492a2c472e7SAleksandr Rybalko {
493a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
494a2c472e7SAleksandr Rybalko 	int ipend;
495a2c472e7SAleksandr Rybalko 	uint32_t usr1, usr2;
4960dc54d18SIan Lepore 	uint32_t ucr1, ucr2, ucr4;
497a2c472e7SAleksandr Rybalko 
498a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
499a2c472e7SAleksandr Rybalko 	ipend = 0;
500a2c472e7SAleksandr Rybalko 
501a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
502a2c472e7SAleksandr Rybalko 
503a2c472e7SAleksandr Rybalko 	/* Read pending interrupts */
504a2c472e7SAleksandr Rybalko 	usr1 = GETREG(bas, REG(USR1));
505a2c472e7SAleksandr Rybalko 	usr2 = GETREG(bas, REG(USR2));
506a2c472e7SAleksandr Rybalko 	/* ACK interrupts */
507a2c472e7SAleksandr Rybalko 	SETREG(bas, REG(USR1), usr1);
508a2c472e7SAleksandr Rybalko 	SETREG(bas, REG(USR2), usr2);
509a2c472e7SAleksandr Rybalko 
510a2c472e7SAleksandr Rybalko 	ucr1 = GETREG(bas, REG(UCR1));
5110dc54d18SIan Lepore 	ucr2 = GETREG(bas, REG(UCR2));
512a2c472e7SAleksandr Rybalko 	ucr4 = GETREG(bas, REG(UCR4));
513a2c472e7SAleksandr Rybalko 
5140dc54d18SIan Lepore 	/* If we have reached tx low-water, we can tx some more now. */
5150dc54d18SIan Lepore 	if ((usr1 & FLD(USR1, TRDY)) && (ucr1 & FLD(UCR1, TRDYEN))) {
5160dc54d18SIan Lepore 		DIS(bas, UCR1, TRDYEN);
517a2c472e7SAleksandr Rybalko 		ipend |= SER_INT_TXIDLE;
518a2c472e7SAleksandr Rybalko 	}
5190dc54d18SIan Lepore 
5200dc54d18SIan Lepore 	/*
5210dc54d18SIan Lepore 	 * If we have reached the rx high-water, or if there are bytes in the rx
5220dc54d18SIan Lepore 	 * fifo and no new data has arrived for 8 character periods (aging
5230dc54d18SIan Lepore 	 * timer), we have input data to process.
5240dc54d18SIan Lepore 	 */
5250dc54d18SIan Lepore 	if (((usr1 & FLD(USR1, RRDY)) && (ucr1 & FLD(UCR1, RRDYEN))) ||
5260dc54d18SIan Lepore 	    ((usr1 & FLD(USR1, AGTIM)) && (ucr2 & FLD(UCR2, ATEN)))) {
5270dc54d18SIan Lepore 		DIS(bas, UCR1, RRDYEN);
5280dc54d18SIan Lepore 		DIS(bas, UCR2, ATEN);
529a2c472e7SAleksandr Rybalko 		ipend |= SER_INT_RXREADY;
530a2c472e7SAleksandr Rybalko 	}
5310dc54d18SIan Lepore 
5320dc54d18SIan Lepore 	/* A break can come in at any time, it never gets disabled. */
533a2c472e7SAleksandr Rybalko 	if ((usr2 & FLD(USR2, BRCD)) && (ucr4 & FLD(UCR4, BKEN)))
534a2c472e7SAleksandr Rybalko 		ipend |= SER_INT_BREAK;
535a2c472e7SAleksandr Rybalko 
536a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
537a2c472e7SAleksandr Rybalko 
538a2c472e7SAleksandr Rybalko 	return (ipend);
539a2c472e7SAleksandr Rybalko }
540a2c472e7SAleksandr Rybalko 
541a2c472e7SAleksandr Rybalko static int
imx_uart_bus_param(struct uart_softc * sc,int baudrate,int databits,int stopbits,int parity)542a2c472e7SAleksandr Rybalko imx_uart_bus_param(struct uart_softc *sc, int baudrate, int databits,
543a2c472e7SAleksandr Rybalko     int stopbits, int parity)
544a2c472e7SAleksandr Rybalko {
545a2c472e7SAleksandr Rybalko 
546a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
547a2c472e7SAleksandr Rybalko 	imx_uart_init(&sc->sc_bas, baudrate, databits, stopbits, parity);
548a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
549a2c472e7SAleksandr Rybalko 	return (0);
550a2c472e7SAleksandr Rybalko }
551a2c472e7SAleksandr Rybalko 
552a2c472e7SAleksandr Rybalko static int
imx_uart_bus_probe(struct uart_softc * sc)553a2c472e7SAleksandr Rybalko imx_uart_bus_probe(struct uart_softc *sc)
554a2c472e7SAleksandr Rybalko {
555a2c472e7SAleksandr Rybalko 	int error;
556a2c472e7SAleksandr Rybalko 
557a2c472e7SAleksandr Rybalko 	error = imx_uart_probe(&sc->sc_bas);
558a2c472e7SAleksandr Rybalko 	if (error)
559a2c472e7SAleksandr Rybalko 		return (error);
560a2c472e7SAleksandr Rybalko 
5610dc54d18SIan Lepore 	/*
5620dc54d18SIan Lepore 	 * On input we can read up to the full fifo size at once.  On output, we
5630dc54d18SIan Lepore 	 * want to write only as much as the programmed tx low water level,
5640dc54d18SIan Lepore 	 * because that's all we can be certain we have room for in the fifo
5650dc54d18SIan Lepore 	 * when we get a tx-ready interrupt.
5660dc54d18SIan Lepore 	 */
5670dc54d18SIan Lepore 	sc->sc_rxfifosz = IMX_FIFOSZ;
5680dc54d18SIan Lepore 	sc->sc_txfifosz = IMX_TXFIFO_LEVEL;
5694d7abca0SIan Lepore 
5703329109cSRui Paulo 	device_set_desc(sc->sc_dev, "Freescale i.MX UART");
571a2c472e7SAleksandr Rybalko 	return (0);
572a2c472e7SAleksandr Rybalko }
573a2c472e7SAleksandr Rybalko 
574a2c472e7SAleksandr Rybalko static int
imx_uart_bus_receive(struct uart_softc * sc)575a2c472e7SAleksandr Rybalko imx_uart_bus_receive(struct uart_softc *sc)
576a2c472e7SAleksandr Rybalko {
577a2c472e7SAleksandr Rybalko 	struct uart_bas *bas;
578a2c472e7SAleksandr Rybalko 	int xc, out;
579a2c472e7SAleksandr Rybalko 
580a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
581a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
582a2c472e7SAleksandr Rybalko 
5830dc54d18SIan Lepore 	/*
5840dc54d18SIan Lepore 	 * Empty the rx fifo.  We get the RRDY interrupt when IMX_RXFIFO_LEVEL
5850dc54d18SIan Lepore 	 * (the rx high-water level) is reached, but we set sc_rxfifosz to the
5860dc54d18SIan Lepore 	 * full hardware fifo size, so we can safely process however much is
5870dc54d18SIan Lepore 	 * there, not just the highwater size.
5880dc54d18SIan Lepore 	 */
589a2c472e7SAleksandr Rybalko 	while (IS(bas, USR2, RDR)) {
590a2c472e7SAleksandr Rybalko 		if (uart_rx_full(sc)) {
591a2c472e7SAleksandr Rybalko 			/* No space left in input buffer */
592a2c472e7SAleksandr Rybalko 			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
593a2c472e7SAleksandr Rybalko 			break;
594a2c472e7SAleksandr Rybalko 		}
595a2c472e7SAleksandr Rybalko 		xc = GETREG(bas, REG(URXD));
596a2c472e7SAleksandr Rybalko 		out = xc & 0x000000ff;
597a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, FRMERR))
598a2c472e7SAleksandr Rybalko 			out |= UART_STAT_FRAMERR;
599a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, PRERR))
600a2c472e7SAleksandr Rybalko 			out |= UART_STAT_PARERR;
601a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, OVRRUN))
602a2c472e7SAleksandr Rybalko 			out |= UART_STAT_OVERRUN;
603a2c472e7SAleksandr Rybalko 		if (xc & FLD(URXD, BRK))
604a2c472e7SAleksandr Rybalko 			out |= UART_STAT_BREAK;
605a2c472e7SAleksandr Rybalko 
606a2c472e7SAleksandr Rybalko 		uart_rx_put(sc, out);
607a2c472e7SAleksandr Rybalko 	}
6080dc54d18SIan Lepore 	ENA(bas, UCR1, RRDYEN);
6090dc54d18SIan Lepore 	ENA(bas, UCR2, ATEN);
610a2c472e7SAleksandr Rybalko 
611a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
612a2c472e7SAleksandr Rybalko 	return (0);
613a2c472e7SAleksandr Rybalko }
614a2c472e7SAleksandr Rybalko 
615a2c472e7SAleksandr Rybalko static int
imx_uart_bus_setsig(struct uart_softc * sc,int sig)616a2c472e7SAleksandr Rybalko imx_uart_bus_setsig(struct uart_softc *sc, int sig)
617a2c472e7SAleksandr Rybalko {
618a2c472e7SAleksandr Rybalko 
619a2c472e7SAleksandr Rybalko 	return (0);
620a2c472e7SAleksandr Rybalko }
621a2c472e7SAleksandr Rybalko 
622a2c472e7SAleksandr Rybalko static int
imx_uart_bus_transmit(struct uart_softc * sc)623a2c472e7SAleksandr Rybalko imx_uart_bus_transmit(struct uart_softc *sc)
624a2c472e7SAleksandr Rybalko {
625a2c472e7SAleksandr Rybalko 	struct uart_bas *bas = &sc->sc_bas;
626a2c472e7SAleksandr Rybalko 	int i;
627a2c472e7SAleksandr Rybalko 
628a2c472e7SAleksandr Rybalko 	bas = &sc->sc_bas;
629a2c472e7SAleksandr Rybalko 	uart_lock(sc->sc_hwmtx);
630a2c472e7SAleksandr Rybalko 
6310dc54d18SIan Lepore 	/*
6320dc54d18SIan Lepore 	 * Fill the tx fifo.  The uart core puts at most IMX_TXFIFO_LEVEL bytes
6330dc54d18SIan Lepore 	 * into the txbuf (because that's what sc_txfifosz is set to), and
6340dc54d18SIan Lepore 	 * because we got the TRDY (low-water reached) interrupt we know at
6350dc54d18SIan Lepore 	 * least that much space is available in the fifo.
6360dc54d18SIan Lepore 	 */
637a2c472e7SAleksandr Rybalko 	for (i = 0; i < sc->sc_txdatasz; i++) {
638a2c472e7SAleksandr Rybalko 		SETREG(bas, REG(UTXD), sc->sc_txbuf[i] & 0xff);
639a2c472e7SAleksandr Rybalko 	}
640a2c472e7SAleksandr Rybalko 	sc->sc_txbusy = 1;
6410dc54d18SIan Lepore 	ENA(bas, UCR1, TRDYEN);
642a2c472e7SAleksandr Rybalko 
643a2c472e7SAleksandr Rybalko 	uart_unlock(sc->sc_hwmtx);
644a2c472e7SAleksandr Rybalko 
645a2c472e7SAleksandr Rybalko 	return (0);
646a2c472e7SAleksandr Rybalko }
647d76a1ef4SWarner Losh 
648d76a1ef4SWarner Losh static void
imx_uart_bus_grab(struct uart_softc * sc)649d76a1ef4SWarner Losh imx_uart_bus_grab(struct uart_softc *sc)
650d76a1ef4SWarner Losh {
651d76a1ef4SWarner Losh 	struct uart_bas *bas = &sc->sc_bas;
652d76a1ef4SWarner Losh 
653d76a1ef4SWarner Losh 	bas = &sc->sc_bas;
654d76a1ef4SWarner Losh 	uart_lock(sc->sc_hwmtx);
6550dc54d18SIan Lepore 	DIS(bas, UCR1, RRDYEN);
6560dc54d18SIan Lepore 	DIS(bas, UCR2, ATEN);
657d76a1ef4SWarner Losh 	uart_unlock(sc->sc_hwmtx);
658d76a1ef4SWarner Losh }
659d76a1ef4SWarner Losh 
660d76a1ef4SWarner Losh static void
imx_uart_bus_ungrab(struct uart_softc * sc)661d76a1ef4SWarner Losh imx_uart_bus_ungrab(struct uart_softc *sc)
662d76a1ef4SWarner Losh {
663d76a1ef4SWarner Losh 	struct uart_bas *bas = &sc->sc_bas;
664d76a1ef4SWarner Losh 
665d76a1ef4SWarner Losh 	bas = &sc->sc_bas;
666d76a1ef4SWarner Losh 	uart_lock(sc->sc_hwmtx);
6670dc54d18SIan Lepore 	ENA(bas, UCR1, RRDYEN);
6680dc54d18SIan Lepore 	ENA(bas, UCR2, ATEN);
669d76a1ef4SWarner Losh 	uart_unlock(sc->sc_hwmtx);
670d76a1ef4SWarner Losh }
671