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