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> 45a2c472e7SAleksandr Rybalko #include <dev/uart/uart_bus.h> 46a90f1975SIan Lepore #include <dev/uart/uart_dev_imx.h> 47a2c472e7SAleksandr Rybalko #include "uart_if.h" 482d40ec16SIan Lepore 492d40ec16SIan Lepore #include <arm/freescale/imx/imx_ccmvar.h> 502d40ec16SIan Lepore 51a2c472e7SAleksandr Rybalko /* 52*0dc54d18SIan Lepore * The hardare FIFOs are 32 bytes. We want an interrupt when there are 24 bytes 53*0dc54d18SIan Lepore * available to read or space for 24 more bytes to write. While 8 bytes of 54*0dc54d18SIan Lepore * slack before over/underrun might seem excessive, the hardware can run at 55*0dc54d18SIan Lepore * 5mbps, which means 2uS per char, so at full speed 8 bytes provides only 16uS 56*0dc54d18SIan Lepore * to get into the interrupt handler and service the fifo. 57*0dc54d18SIan Lepore */ 58*0dc54d18SIan Lepore #define IMX_FIFOSZ 32 59*0dc54d18SIan Lepore #define IMX_RXFIFO_LEVEL 24 60*0dc54d18SIan Lepore #define IMX_TXFIFO_LEVEL 24 61*0dc54d18SIan Lepore 62*0dc54d18SIan Lepore /* 63a2c472e7SAleksandr Rybalko * Low-level UART interface. 64a2c472e7SAleksandr Rybalko */ 65a2c472e7SAleksandr Rybalko static int imx_uart_probe(struct uart_bas *bas); 66a2c472e7SAleksandr Rybalko static void imx_uart_init(struct uart_bas *bas, int, int, int, int); 67a2c472e7SAleksandr Rybalko static void imx_uart_term(struct uart_bas *bas); 68a2c472e7SAleksandr Rybalko static void imx_uart_putc(struct uart_bas *bas, int); 69a2c472e7SAleksandr Rybalko static int imx_uart_rxready(struct uart_bas *bas); 70a2c472e7SAleksandr Rybalko static int imx_uart_getc(struct uart_bas *bas, struct mtx *); 71a2c472e7SAleksandr Rybalko 72a2c472e7SAleksandr Rybalko static struct uart_ops uart_imx_uart_ops = { 73a2c472e7SAleksandr Rybalko .probe = imx_uart_probe, 74a2c472e7SAleksandr Rybalko .init = imx_uart_init, 75a2c472e7SAleksandr Rybalko .term = imx_uart_term, 76a2c472e7SAleksandr Rybalko .putc = imx_uart_putc, 77a2c472e7SAleksandr Rybalko .rxready = imx_uart_rxready, 78a2c472e7SAleksandr Rybalko .getc = imx_uart_getc, 79a2c472e7SAleksandr Rybalko }; 80a2c472e7SAleksandr Rybalko 812d40ec16SIan Lepore #if 0 /* Handy when debugging. */ 822d40ec16SIan Lepore static void 832d40ec16SIan Lepore dumpregs(struct uart_bas *bas, const char * msg) 842d40ec16SIan Lepore { 852d40ec16SIan Lepore 862d40ec16SIan Lepore if (!bootverbose) 872d40ec16SIan Lepore return; 882d40ec16SIan Lepore printf("%s bsh 0x%08lx UCR1 0x%08x UCR2 0x%08x " 892d40ec16SIan Lepore "UCR3 0x%08x UCR4 0x%08x USR1 0x%08x USR2 0x%08x\n", 902d40ec16SIan Lepore msg, bas->bsh, 912d40ec16SIan Lepore GETREG(bas, REG(UCR1)), GETREG(bas, REG(UCR2)), 922d40ec16SIan Lepore GETREG(bas, REG(UCR3)), GETREG(bas, REG(UCR4)), 932d40ec16SIan Lepore GETREG(bas, REG(USR1)), GETREG(bas, REG(USR2))); 942d40ec16SIan Lepore } 952d40ec16SIan Lepore #endif 962d40ec16SIan Lepore 97a2c472e7SAleksandr Rybalko static int 98a2c472e7SAleksandr Rybalko imx_uart_probe(struct uart_bas *bas) 99a2c472e7SAleksandr Rybalko { 100a2c472e7SAleksandr Rybalko 101a2c472e7SAleksandr Rybalko return (0); 102a2c472e7SAleksandr Rybalko } 103a2c472e7SAleksandr Rybalko 104c3f0f284SIan Lepore static u_int 105c3f0f284SIan Lepore imx_uart_getbaud(struct uart_bas *bas) 106c3f0f284SIan Lepore { 107c3f0f284SIan Lepore uint32_t rate, ubir, ubmr; 108c3f0f284SIan Lepore u_int baud, blo, bhi, i; 109c3f0f284SIan Lepore static const u_int predivs[] = {6, 5, 4, 3, 2, 1, 7, 1}; 110c3f0f284SIan Lepore static const u_int std_rates[] = { 111c3f0f284SIan Lepore 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600 112c3f0f284SIan Lepore }; 113c3f0f284SIan Lepore 114c3f0f284SIan Lepore /* 115c3f0f284SIan Lepore * Get the baud rate the hardware is programmed for, then search the 116c3f0f284SIan Lepore * table of standard baud rates for a number that's within 3% of the 117c3f0f284SIan Lepore * actual rate the hardware is programmed for. It's more comforting to 118c3f0f284SIan Lepore * see that your console is running at 115200 than 114942. Note that 119c3f0f284SIan Lepore * here we cannot make a simplifying assumption that the predivider and 120c3f0f284SIan Lepore * numerator are 1 (like we do when setting the baud rate), because we 121c3f0f284SIan Lepore * don't know what u-boot might have set up. 122c3f0f284SIan Lepore */ 123c3f0f284SIan Lepore i = (GETREG(bas, REG(UFCR)) & IMXUART_UFCR_RFDIV_MASK) >> 124c3f0f284SIan Lepore IMXUART_UFCR_RFDIV_SHIFT; 125c3f0f284SIan Lepore rate = imx_ccm_uart_hz() / predivs[i]; 126c3f0f284SIan Lepore ubir = GETREG(bas, REG(UBIR)) + 1; 127c3f0f284SIan Lepore ubmr = GETREG(bas, REG(UBMR)) + 1; 128c3f0f284SIan Lepore baud = ((rate / 16 ) * ubir) / ubmr; 129c3f0f284SIan Lepore 130c3f0f284SIan Lepore blo = (baud * 100) / 103; 131c3f0f284SIan Lepore bhi = (baud * 100) / 97; 132c3f0f284SIan Lepore for (i = 0; i < nitems(std_rates); i++) { 133c3f0f284SIan Lepore rate = std_rates[i]; 134c3f0f284SIan Lepore if (rate >= blo && rate <= bhi) { 135c3f0f284SIan Lepore baud = rate; 136c3f0f284SIan Lepore break; 137c3f0f284SIan Lepore } 138c3f0f284SIan Lepore } 139c3f0f284SIan Lepore 140c3f0f284SIan Lepore return (baud); 141c3f0f284SIan Lepore } 142c3f0f284SIan Lepore 143a2c472e7SAleksandr Rybalko static void 144a2c472e7SAleksandr Rybalko imx_uart_init(struct uart_bas *bas, int baudrate, int databits, 145a2c472e7SAleksandr Rybalko int stopbits, int parity) 146a2c472e7SAleksandr Rybalko { 1472d40ec16SIan Lepore uint32_t baseclk, reg; 148a2c472e7SAleksandr Rybalko 1492d40ec16SIan Lepore /* Enable the device and the RX/TX channels. */ 1502d40ec16SIan Lepore SET(bas, REG(UCR1), FLD(UCR1, UARTEN)); 1512d40ec16SIan Lepore SET(bas, REG(UCR2), FLD(UCR2, RXEN) | FLD(UCR2, TXEN)); 1522d40ec16SIan Lepore 1532d40ec16SIan Lepore if (databits == 7) 1542d40ec16SIan Lepore DIS(bas, UCR2, WS); 1552d40ec16SIan Lepore else 1562d40ec16SIan Lepore ENA(bas, UCR2, WS); 1572d40ec16SIan Lepore 1582d40ec16SIan Lepore if (stopbits == 2) 1592d40ec16SIan Lepore ENA(bas, UCR2, STPB); 1602d40ec16SIan Lepore else 1612d40ec16SIan Lepore DIS(bas, UCR2, STPB); 1622d40ec16SIan Lepore 1632d40ec16SIan Lepore switch (parity) { 1642d40ec16SIan Lepore case UART_PARITY_ODD: 1652d40ec16SIan Lepore DIS(bas, UCR2, PROE); 1662d40ec16SIan Lepore ENA(bas, UCR2, PREN); 1672d40ec16SIan Lepore break; 1682d40ec16SIan Lepore case UART_PARITY_EVEN: 1692d40ec16SIan Lepore ENA(bas, UCR2, PROE); 1702d40ec16SIan Lepore ENA(bas, UCR2, PREN); 1712d40ec16SIan Lepore break; 1722d40ec16SIan Lepore case UART_PARITY_MARK: 1732d40ec16SIan Lepore case UART_PARITY_SPACE: 1742d40ec16SIan Lepore /* FALLTHROUGH: Hardware doesn't support mark/space. */ 1752d40ec16SIan Lepore case UART_PARITY_NONE: 1762d40ec16SIan Lepore default: 1772d40ec16SIan Lepore DIS(bas, UCR2, PREN); 1782d40ec16SIan Lepore break; 1792d40ec16SIan Lepore } 1802d40ec16SIan Lepore 1812d40ec16SIan Lepore /* 1822d40ec16SIan Lepore * The hardware has an extremely flexible baud clock: it allows setting 1832d40ec16SIan Lepore * both the numerator and denominator of the divider, as well as a 18449d0a4c3SIan Lepore * separate pre-divider. We simplify the problem of coming up with a 18549d0a4c3SIan Lepore * workable pair of numbers by assuming a pre-divider and numerator of 18649d0a4c3SIan Lepore * one because our base clock is so fast we can reach virtually any 18749d0a4c3SIan Lepore * reasonable speed with a simple divisor. The numerator value actually 18849d0a4c3SIan Lepore * includes the 16x over-sampling (so a value of 16 means divide by 1); 18949d0a4c3SIan Lepore * the register value is the numerator-1, so we have a hard-coded 15. 19049d0a4c3SIan Lepore * Note that a quirk of the hardware requires that both UBIR and UBMR be 19149d0a4c3SIan Lepore * set back to back in order for the change to take effect. 1922d40ec16SIan Lepore */ 1932d40ec16SIan Lepore if (baudrate > 0) { 1942d40ec16SIan Lepore baseclk = imx_ccm_uart_hz(); 1952d40ec16SIan Lepore reg = GETREG(bas, REG(UFCR)); 1962d40ec16SIan Lepore reg = (reg & ~IMXUART_UFCR_RFDIV_MASK) | IMXUART_UFCR_RFDIV_DIV1; 1972d40ec16SIan Lepore SETREG(bas, REG(UFCR), reg); 1982d40ec16SIan Lepore SETREG(bas, REG(UBIR), 15); 1992d40ec16SIan Lepore SETREG(bas, REG(UBMR), (baseclk / baudrate) - 1); 2002d40ec16SIan Lepore } 201*0dc54d18SIan Lepore 202*0dc54d18SIan Lepore /* 203*0dc54d18SIan Lepore * Program the tx lowater and rx hiwater levels at which fifo-service 204*0dc54d18SIan Lepore * interrupts are signaled. The tx value is interpetted as "when there 205*0dc54d18SIan Lepore * are only this many bytes remaining" (not "this many free"). 206*0dc54d18SIan Lepore */ 207*0dc54d18SIan Lepore reg = GETREG(bas, REG(UFCR)); 208*0dc54d18SIan Lepore reg &= ~(IMXUART_UFCR_TXTL_MASK | IMXUART_UFCR_RXTL_MASK); 209*0dc54d18SIan Lepore reg |= (IMX_FIFOSZ - IMX_TXFIFO_LEVEL) << IMXUART_UFCR_TXTL_SHIFT; 210*0dc54d18SIan Lepore reg |= IMX_RXFIFO_LEVEL << IMXUART_UFCR_RXTL_SHIFT; 211*0dc54d18SIan Lepore SETREG(bas, REG(UFCR), reg); 212a2c472e7SAleksandr Rybalko } 213a2c472e7SAleksandr Rybalko 214a2c472e7SAleksandr Rybalko static void 215a2c472e7SAleksandr Rybalko imx_uart_term(struct uart_bas *bas) 216a2c472e7SAleksandr Rybalko { 217a2c472e7SAleksandr Rybalko 218a2c472e7SAleksandr Rybalko } 219a2c472e7SAleksandr Rybalko 220a2c472e7SAleksandr Rybalko static void 221a2c472e7SAleksandr Rybalko imx_uart_putc(struct uart_bas *bas, int c) 222a2c472e7SAleksandr Rybalko { 223a2c472e7SAleksandr Rybalko 224*0dc54d18SIan Lepore while (!(IS(bas, USR1, TRDY))) 225a2c472e7SAleksandr Rybalko ; 226a2c472e7SAleksandr Rybalko SETREG(bas, REG(UTXD), c); 227a2c472e7SAleksandr Rybalko } 228a2c472e7SAleksandr Rybalko 229a2c472e7SAleksandr Rybalko static int 230a2c472e7SAleksandr Rybalko imx_uart_rxready(struct uart_bas *bas) 231a2c472e7SAleksandr Rybalko { 232a2c472e7SAleksandr Rybalko 233a2c472e7SAleksandr Rybalko return ((IS(bas, USR2, RDR)) ? 1 : 0); 234a2c472e7SAleksandr Rybalko } 235a2c472e7SAleksandr Rybalko 236a2c472e7SAleksandr Rybalko static int 237a2c472e7SAleksandr Rybalko imx_uart_getc(struct uart_bas *bas, struct mtx *hwmtx) 238a2c472e7SAleksandr Rybalko { 239a2c472e7SAleksandr Rybalko int c; 240a2c472e7SAleksandr Rybalko 241a2c472e7SAleksandr Rybalko uart_lock(hwmtx); 242a2c472e7SAleksandr Rybalko while (!(IS(bas, USR2, RDR))) 243a2c472e7SAleksandr Rybalko ; 244a2c472e7SAleksandr Rybalko 245a2c472e7SAleksandr Rybalko c = GETREG(bas, REG(URXD)); 246a2c472e7SAleksandr Rybalko uart_unlock(hwmtx); 247a2c472e7SAleksandr Rybalko #if defined(KDB) 248a2c472e7SAleksandr Rybalko if (c & FLD(URXD, BRK)) { 249a2c472e7SAleksandr Rybalko if (kdb_break()) 250a2c472e7SAleksandr Rybalko return (0); 251a2c472e7SAleksandr Rybalko } 252a2c472e7SAleksandr Rybalko #endif 253a2c472e7SAleksandr Rybalko return (c & 0xff); 254a2c472e7SAleksandr Rybalko } 255a2c472e7SAleksandr Rybalko 256a2c472e7SAleksandr Rybalko /* 257a2c472e7SAleksandr Rybalko * High-level UART interface. 258a2c472e7SAleksandr Rybalko */ 259a2c472e7SAleksandr Rybalko struct imx_uart_softc { 260a2c472e7SAleksandr Rybalko struct uart_softc base; 261a2c472e7SAleksandr Rybalko }; 262a2c472e7SAleksandr Rybalko 263a2c472e7SAleksandr Rybalko static int imx_uart_bus_attach(struct uart_softc *); 264a2c472e7SAleksandr Rybalko static int imx_uart_bus_detach(struct uart_softc *); 265a2c472e7SAleksandr Rybalko static int imx_uart_bus_flush(struct uart_softc *, int); 266a2c472e7SAleksandr Rybalko static int imx_uart_bus_getsig(struct uart_softc *); 267a2c472e7SAleksandr Rybalko static int imx_uart_bus_ioctl(struct uart_softc *, int, intptr_t); 268a2c472e7SAleksandr Rybalko static int imx_uart_bus_ipend(struct uart_softc *); 269a2c472e7SAleksandr Rybalko static int imx_uart_bus_param(struct uart_softc *, int, int, int, int); 270a2c472e7SAleksandr Rybalko static int imx_uart_bus_probe(struct uart_softc *); 271a2c472e7SAleksandr Rybalko static int imx_uart_bus_receive(struct uart_softc *); 272a2c472e7SAleksandr Rybalko static int imx_uart_bus_setsig(struct uart_softc *, int); 273a2c472e7SAleksandr Rybalko static int imx_uart_bus_transmit(struct uart_softc *); 274d76a1ef4SWarner Losh static void imx_uart_bus_grab(struct uart_softc *); 275d76a1ef4SWarner Losh static void imx_uart_bus_ungrab(struct uart_softc *); 276a2c472e7SAleksandr Rybalko 277a2c472e7SAleksandr Rybalko static kobj_method_t imx_uart_methods[] = { 278a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_attach, imx_uart_bus_attach), 279a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_detach, imx_uart_bus_detach), 280a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_flush, imx_uart_bus_flush), 281a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_getsig, imx_uart_bus_getsig), 282a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_ioctl, imx_uart_bus_ioctl), 283a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_ipend, imx_uart_bus_ipend), 284a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_param, imx_uart_bus_param), 285a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_probe, imx_uart_bus_probe), 286a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_receive, imx_uart_bus_receive), 287a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_setsig, imx_uart_bus_setsig), 288a2c472e7SAleksandr Rybalko KOBJMETHOD(uart_transmit, imx_uart_bus_transmit), 289d76a1ef4SWarner Losh KOBJMETHOD(uart_grab, imx_uart_bus_grab), 290d76a1ef4SWarner Losh KOBJMETHOD(uart_ungrab, imx_uart_bus_ungrab), 291a2c472e7SAleksandr Rybalko { 0, 0 } 292a2c472e7SAleksandr Rybalko }; 293a2c472e7SAleksandr Rybalko 294a2c472e7SAleksandr Rybalko struct uart_class uart_imx_class = { 295a2c472e7SAleksandr Rybalko "imx", 296a2c472e7SAleksandr Rybalko imx_uart_methods, 297a2c472e7SAleksandr Rybalko sizeof(struct imx_uart_softc), 298a2c472e7SAleksandr Rybalko .uc_ops = &uart_imx_uart_ops, 299a2c472e7SAleksandr Rybalko .uc_range = 0x100, 300a2c472e7SAleksandr Rybalko .uc_rclk = 24000000 /* TODO: get value from CCM */ 301a2c472e7SAleksandr Rybalko }; 302a2c472e7SAleksandr Rybalko 303a2c472e7SAleksandr Rybalko #define SIGCHG(c, i, s, d) \ 304a2c472e7SAleksandr Rybalko if (c) { \ 305a2c472e7SAleksandr Rybalko i |= (i & s) ? s : s | d; \ 306a2c472e7SAleksandr Rybalko } else { \ 307a2c472e7SAleksandr Rybalko i = (i & s) ? (i & ~s) | d : i; \ 308a2c472e7SAleksandr Rybalko } 309a2c472e7SAleksandr Rybalko 310a2c472e7SAleksandr Rybalko static int 311a2c472e7SAleksandr Rybalko imx_uart_bus_attach(struct uart_softc *sc) 312a2c472e7SAleksandr Rybalko { 313a2c472e7SAleksandr Rybalko struct uart_bas *bas; 314a2c472e7SAleksandr Rybalko struct uart_devinfo *di; 315a2c472e7SAleksandr Rybalko 316a2c472e7SAleksandr Rybalko bas = &sc->sc_bas; 317a2c472e7SAleksandr Rybalko if (sc->sc_sysdev != NULL) { 318a2c472e7SAleksandr Rybalko di = sc->sc_sysdev; 319a2c472e7SAleksandr Rybalko imx_uart_init(bas, di->baudrate, di->databits, di->stopbits, 320a2c472e7SAleksandr Rybalko di->parity); 321a2c472e7SAleksandr Rybalko } else { 322a2c472e7SAleksandr Rybalko imx_uart_init(bas, 115200, 8, 1, 0); 323a2c472e7SAleksandr Rybalko } 324a2c472e7SAleksandr Rybalko 325a2c472e7SAleksandr Rybalko (void)imx_uart_bus_getsig(sc); 326a2c472e7SAleksandr Rybalko 327*0dc54d18SIan Lepore /* Clear all pending interrupts. */ 328*0dc54d18SIan Lepore SETREG(bas, REG(USR1), 0xffff); 329*0dc54d18SIan Lepore SETREG(bas, REG(USR2), 0xffff); 330*0dc54d18SIan Lepore 331*0dc54d18SIan Lepore DIS(bas, UCR4, DREN); 332*0dc54d18SIan Lepore ENA(bas, UCR1, RRDYEN); 333a2c472e7SAleksandr Rybalko DIS(bas, UCR1, IDEN); 334a2c472e7SAleksandr Rybalko DIS(bas, UCR3, RXDSEN); 335*0dc54d18SIan Lepore ENA(bas, UCR2, ATEN); 336a2c472e7SAleksandr Rybalko DIS(bas, UCR1, TXMPTYEN); 337a2c472e7SAleksandr Rybalko DIS(bas, UCR1, TRDYEN); 338a2c472e7SAleksandr Rybalko DIS(bas, UCR4, TCEN); 339a2c472e7SAleksandr Rybalko DIS(bas, UCR4, OREN); 340a2c472e7SAleksandr Rybalko ENA(bas, UCR4, BKEN); 341a2c472e7SAleksandr Rybalko DIS(bas, UCR4, WKEN); 342a2c472e7SAleksandr Rybalko DIS(bas, UCR1, ADEN); 343a2c472e7SAleksandr Rybalko DIS(bas, UCR3, ACIEN); 344a2c472e7SAleksandr Rybalko DIS(bas, UCR2, ESCI); 345a2c472e7SAleksandr Rybalko DIS(bas, UCR4, ENIRI); 346a2c472e7SAleksandr Rybalko DIS(bas, UCR3, AIRINTEN); 347a2c472e7SAleksandr Rybalko DIS(bas, UCR3, AWAKEN); 348a2c472e7SAleksandr Rybalko DIS(bas, UCR3, FRAERREN); 349a2c472e7SAleksandr Rybalko DIS(bas, UCR3, PARERREN); 350a2c472e7SAleksandr Rybalko DIS(bas, UCR1, RTSDEN); 351a2c472e7SAleksandr Rybalko DIS(bas, UCR2, RTSEN); 352a2c472e7SAleksandr Rybalko DIS(bas, UCR3, DTREN); 353a2c472e7SAleksandr Rybalko DIS(bas, UCR3, RI); 354a2c472e7SAleksandr Rybalko DIS(bas, UCR3, DCD); 355a2c472e7SAleksandr Rybalko DIS(bas, UCR3, DTRDEN); 3562d40ec16SIan Lepore ENA(bas, UCR2, IRTS); 3572d40ec16SIan Lepore ENA(bas, UCR3, RXDMUXSEL); 358a2c472e7SAleksandr Rybalko 359a2c472e7SAleksandr Rybalko return (0); 360a2c472e7SAleksandr Rybalko } 361a2c472e7SAleksandr Rybalko 362a2c472e7SAleksandr Rybalko static int 363a2c472e7SAleksandr Rybalko imx_uart_bus_detach(struct uart_softc *sc) 364a2c472e7SAleksandr Rybalko { 365a2c472e7SAleksandr Rybalko 366a2c472e7SAleksandr Rybalko SETREG(&sc->sc_bas, REG(UCR4), 0); 367a2c472e7SAleksandr Rybalko 368a2c472e7SAleksandr Rybalko return (0); 369a2c472e7SAleksandr Rybalko } 370a2c472e7SAleksandr Rybalko 371a2c472e7SAleksandr Rybalko static int 372a2c472e7SAleksandr Rybalko imx_uart_bus_flush(struct uart_softc *sc, int what) 373a2c472e7SAleksandr Rybalko { 374a2c472e7SAleksandr Rybalko 375a2c472e7SAleksandr Rybalko /* TODO */ 376a2c472e7SAleksandr Rybalko return (0); 377a2c472e7SAleksandr Rybalko } 378a2c472e7SAleksandr Rybalko 379a2c472e7SAleksandr Rybalko static int 380a2c472e7SAleksandr Rybalko imx_uart_bus_getsig(struct uart_softc *sc) 381a2c472e7SAleksandr Rybalko { 382a2c472e7SAleksandr Rybalko uint32_t new, old, sig; 383a2c472e7SAleksandr Rybalko uint8_t bes; 384a2c472e7SAleksandr Rybalko 385a2c472e7SAleksandr Rybalko do { 386a2c472e7SAleksandr Rybalko old = sc->sc_hwsig; 387a2c472e7SAleksandr Rybalko sig = old; 388a2c472e7SAleksandr Rybalko uart_lock(sc->sc_hwmtx); 389a2c472e7SAleksandr Rybalko bes = GETREG(&sc->sc_bas, REG(USR2)); 390a2c472e7SAleksandr Rybalko uart_unlock(sc->sc_hwmtx); 391a2c472e7SAleksandr Rybalko /* XXX: chip can show delta */ 392a2c472e7SAleksandr Rybalko SIGCHG(bes & FLD(USR2, DCDIN), sig, SER_DCD, SER_DDCD); 393a2c472e7SAleksandr Rybalko new = sig & ~SER_MASK_DELTA; 394a2c472e7SAleksandr Rybalko } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new)); 395a2c472e7SAleksandr Rybalko 396a2c472e7SAleksandr Rybalko return (sig); 397a2c472e7SAleksandr Rybalko } 398a2c472e7SAleksandr Rybalko 399a2c472e7SAleksandr Rybalko static int 400a2c472e7SAleksandr Rybalko imx_uart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data) 401a2c472e7SAleksandr Rybalko { 402a2c472e7SAleksandr Rybalko struct uart_bas *bas; 403a2c472e7SAleksandr Rybalko int error; 404a2c472e7SAleksandr Rybalko 405a2c472e7SAleksandr Rybalko bas = &sc->sc_bas; 406a2c472e7SAleksandr Rybalko error = 0; 407a2c472e7SAleksandr Rybalko uart_lock(sc->sc_hwmtx); 408a2c472e7SAleksandr Rybalko switch (request) { 409a2c472e7SAleksandr Rybalko case UART_IOCTL_BREAK: 410a2c472e7SAleksandr Rybalko /* TODO */ 411a2c472e7SAleksandr Rybalko break; 412a2c472e7SAleksandr Rybalko case UART_IOCTL_BAUD: 413c3f0f284SIan Lepore *(u_int*)data = imx_uart_getbaud(bas); 414a2c472e7SAleksandr Rybalko break; 415a2c472e7SAleksandr Rybalko default: 416a2c472e7SAleksandr Rybalko error = EINVAL; 417a2c472e7SAleksandr Rybalko break; 418a2c472e7SAleksandr Rybalko } 419a2c472e7SAleksandr Rybalko uart_unlock(sc->sc_hwmtx); 420a2c472e7SAleksandr Rybalko 421a2c472e7SAleksandr Rybalko return (error); 422a2c472e7SAleksandr Rybalko } 423a2c472e7SAleksandr Rybalko 424a2c472e7SAleksandr Rybalko static int 425a2c472e7SAleksandr Rybalko imx_uart_bus_ipend(struct uart_softc *sc) 426a2c472e7SAleksandr Rybalko { 427a2c472e7SAleksandr Rybalko struct uart_bas *bas; 428a2c472e7SAleksandr Rybalko int ipend; 429a2c472e7SAleksandr Rybalko uint32_t usr1, usr2; 430*0dc54d18SIan Lepore uint32_t ucr1, ucr2, ucr4; 431a2c472e7SAleksandr Rybalko 432a2c472e7SAleksandr Rybalko bas = &sc->sc_bas; 433a2c472e7SAleksandr Rybalko ipend = 0; 434a2c472e7SAleksandr Rybalko 435a2c472e7SAleksandr Rybalko uart_lock(sc->sc_hwmtx); 436a2c472e7SAleksandr Rybalko 437a2c472e7SAleksandr Rybalko /* Read pending interrupts */ 438a2c472e7SAleksandr Rybalko usr1 = GETREG(bas, REG(USR1)); 439a2c472e7SAleksandr Rybalko usr2 = GETREG(bas, REG(USR2)); 440a2c472e7SAleksandr Rybalko /* ACK interrupts */ 441a2c472e7SAleksandr Rybalko SETREG(bas, REG(USR1), usr1); 442a2c472e7SAleksandr Rybalko SETREG(bas, REG(USR2), usr2); 443a2c472e7SAleksandr Rybalko 444a2c472e7SAleksandr Rybalko ucr1 = GETREG(bas, REG(UCR1)); 445*0dc54d18SIan Lepore ucr2 = GETREG(bas, REG(UCR2)); 446a2c472e7SAleksandr Rybalko ucr4 = GETREG(bas, REG(UCR4)); 447a2c472e7SAleksandr Rybalko 448*0dc54d18SIan Lepore /* If we have reached tx low-water, we can tx some more now. */ 449*0dc54d18SIan Lepore if ((usr1 & FLD(USR1, TRDY)) && (ucr1 & FLD(UCR1, TRDYEN))) { 450*0dc54d18SIan Lepore DIS(bas, UCR1, TRDYEN); 451a2c472e7SAleksandr Rybalko ipend |= SER_INT_TXIDLE; 452a2c472e7SAleksandr Rybalko } 453*0dc54d18SIan Lepore 454*0dc54d18SIan Lepore /* 455*0dc54d18SIan Lepore * If we have reached the rx high-water, or if there are bytes in the rx 456*0dc54d18SIan Lepore * fifo and no new data has arrived for 8 character periods (aging 457*0dc54d18SIan Lepore * timer), we have input data to process. 458*0dc54d18SIan Lepore */ 459*0dc54d18SIan Lepore if (((usr1 & FLD(USR1, RRDY)) && (ucr1 & FLD(UCR1, RRDYEN))) || 460*0dc54d18SIan Lepore ((usr1 & FLD(USR1, AGTIM)) && (ucr2 & FLD(UCR2, ATEN)))) { 461*0dc54d18SIan Lepore DIS(bas, UCR1, RRDYEN); 462*0dc54d18SIan Lepore DIS(bas, UCR2, ATEN); 463a2c472e7SAleksandr Rybalko ipend |= SER_INT_RXREADY; 464a2c472e7SAleksandr Rybalko } 465*0dc54d18SIan Lepore 466*0dc54d18SIan Lepore /* A break can come in at any time, it never gets disabled. */ 467a2c472e7SAleksandr Rybalko if ((usr2 & FLD(USR2, BRCD)) && (ucr4 & FLD(UCR4, BKEN))) 468a2c472e7SAleksandr Rybalko ipend |= SER_INT_BREAK; 469a2c472e7SAleksandr Rybalko 470a2c472e7SAleksandr Rybalko uart_unlock(sc->sc_hwmtx); 471a2c472e7SAleksandr Rybalko 472a2c472e7SAleksandr Rybalko return (ipend); 473a2c472e7SAleksandr Rybalko } 474a2c472e7SAleksandr Rybalko 475a2c472e7SAleksandr Rybalko static int 476a2c472e7SAleksandr Rybalko imx_uart_bus_param(struct uart_softc *sc, int baudrate, int databits, 477a2c472e7SAleksandr Rybalko int stopbits, int parity) 478a2c472e7SAleksandr Rybalko { 479a2c472e7SAleksandr Rybalko 480a2c472e7SAleksandr Rybalko uart_lock(sc->sc_hwmtx); 481a2c472e7SAleksandr Rybalko imx_uart_init(&sc->sc_bas, baudrate, databits, stopbits, parity); 482a2c472e7SAleksandr Rybalko uart_unlock(sc->sc_hwmtx); 483a2c472e7SAleksandr Rybalko return (0); 484a2c472e7SAleksandr Rybalko } 485a2c472e7SAleksandr Rybalko 486a2c472e7SAleksandr Rybalko static int 487a2c472e7SAleksandr Rybalko imx_uart_bus_probe(struct uart_softc *sc) 488a2c472e7SAleksandr Rybalko { 489a2c472e7SAleksandr Rybalko int error; 490a2c472e7SAleksandr Rybalko 491a2c472e7SAleksandr Rybalko error = imx_uart_probe(&sc->sc_bas); 492a2c472e7SAleksandr Rybalko if (error) 493a2c472e7SAleksandr Rybalko return (error); 494a2c472e7SAleksandr Rybalko 495*0dc54d18SIan Lepore /* 496*0dc54d18SIan Lepore * On input we can read up to the full fifo size at once. On output, we 497*0dc54d18SIan Lepore * want to write only as much as the programmed tx low water level, 498*0dc54d18SIan Lepore * because that's all we can be certain we have room for in the fifo 499*0dc54d18SIan Lepore * when we get a tx-ready interrupt. 500*0dc54d18SIan Lepore */ 501*0dc54d18SIan Lepore sc->sc_rxfifosz = IMX_FIFOSZ; 502*0dc54d18SIan Lepore sc->sc_txfifosz = IMX_TXFIFO_LEVEL; 5034d7abca0SIan Lepore 5043329109cSRui Paulo device_set_desc(sc->sc_dev, "Freescale i.MX UART"); 505a2c472e7SAleksandr Rybalko return (0); 506a2c472e7SAleksandr Rybalko } 507a2c472e7SAleksandr Rybalko 508a2c472e7SAleksandr Rybalko static int 509a2c472e7SAleksandr Rybalko imx_uart_bus_receive(struct uart_softc *sc) 510a2c472e7SAleksandr Rybalko { 511a2c472e7SAleksandr Rybalko struct uart_bas *bas; 512a2c472e7SAleksandr Rybalko int xc, out; 513a2c472e7SAleksandr Rybalko 514a2c472e7SAleksandr Rybalko bas = &sc->sc_bas; 515a2c472e7SAleksandr Rybalko uart_lock(sc->sc_hwmtx); 516a2c472e7SAleksandr Rybalko 517*0dc54d18SIan Lepore /* 518*0dc54d18SIan Lepore * Empty the rx fifo. We get the RRDY interrupt when IMX_RXFIFO_LEVEL 519*0dc54d18SIan Lepore * (the rx high-water level) is reached, but we set sc_rxfifosz to the 520*0dc54d18SIan Lepore * full hardware fifo size, so we can safely process however much is 521*0dc54d18SIan Lepore * there, not just the highwater size. 522*0dc54d18SIan Lepore */ 523a2c472e7SAleksandr Rybalko while (IS(bas, USR2, RDR)) { 524a2c472e7SAleksandr Rybalko if (uart_rx_full(sc)) { 525a2c472e7SAleksandr Rybalko /* No space left in input buffer */ 526a2c472e7SAleksandr Rybalko sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN; 527a2c472e7SAleksandr Rybalko break; 528a2c472e7SAleksandr Rybalko } 529a2c472e7SAleksandr Rybalko xc = GETREG(bas, REG(URXD)); 530a2c472e7SAleksandr Rybalko out = xc & 0x000000ff; 531a2c472e7SAleksandr Rybalko if (xc & FLD(URXD, FRMERR)) 532a2c472e7SAleksandr Rybalko out |= UART_STAT_FRAMERR; 533a2c472e7SAleksandr Rybalko if (xc & FLD(URXD, PRERR)) 534a2c472e7SAleksandr Rybalko out |= UART_STAT_PARERR; 535a2c472e7SAleksandr Rybalko if (xc & FLD(URXD, OVRRUN)) 536a2c472e7SAleksandr Rybalko out |= UART_STAT_OVERRUN; 537a2c472e7SAleksandr Rybalko if (xc & FLD(URXD, BRK)) 538a2c472e7SAleksandr Rybalko out |= UART_STAT_BREAK; 539a2c472e7SAleksandr Rybalko 540a2c472e7SAleksandr Rybalko uart_rx_put(sc, out); 541a2c472e7SAleksandr Rybalko } 542*0dc54d18SIan Lepore ENA(bas, UCR1, RRDYEN); 543*0dc54d18SIan Lepore ENA(bas, UCR2, ATEN); 544a2c472e7SAleksandr Rybalko 545a2c472e7SAleksandr Rybalko uart_unlock(sc->sc_hwmtx); 546a2c472e7SAleksandr Rybalko return (0); 547a2c472e7SAleksandr Rybalko } 548a2c472e7SAleksandr Rybalko 549a2c472e7SAleksandr Rybalko static int 550a2c472e7SAleksandr Rybalko imx_uart_bus_setsig(struct uart_softc *sc, int sig) 551a2c472e7SAleksandr Rybalko { 552a2c472e7SAleksandr Rybalko 553a2c472e7SAleksandr Rybalko return (0); 554a2c472e7SAleksandr Rybalko } 555a2c472e7SAleksandr Rybalko 556a2c472e7SAleksandr Rybalko static int 557a2c472e7SAleksandr Rybalko imx_uart_bus_transmit(struct uart_softc *sc) 558a2c472e7SAleksandr Rybalko { 559a2c472e7SAleksandr Rybalko struct uart_bas *bas = &sc->sc_bas; 560a2c472e7SAleksandr Rybalko int i; 561a2c472e7SAleksandr Rybalko 562a2c472e7SAleksandr Rybalko bas = &sc->sc_bas; 563a2c472e7SAleksandr Rybalko uart_lock(sc->sc_hwmtx); 564a2c472e7SAleksandr Rybalko 565*0dc54d18SIan Lepore /* 566*0dc54d18SIan Lepore * Fill the tx fifo. The uart core puts at most IMX_TXFIFO_LEVEL bytes 567*0dc54d18SIan Lepore * into the txbuf (because that's what sc_txfifosz is set to), and 568*0dc54d18SIan Lepore * because we got the TRDY (low-water reached) interrupt we know at 569*0dc54d18SIan Lepore * least that much space is available in the fifo. 570*0dc54d18SIan Lepore */ 571a2c472e7SAleksandr Rybalko for (i = 0; i < sc->sc_txdatasz; i++) { 572a2c472e7SAleksandr Rybalko SETREG(bas, REG(UTXD), sc->sc_txbuf[i] & 0xff); 573a2c472e7SAleksandr Rybalko } 574a2c472e7SAleksandr Rybalko sc->sc_txbusy = 1; 575*0dc54d18SIan Lepore ENA(bas, UCR1, TRDYEN); 576a2c472e7SAleksandr Rybalko 577a2c472e7SAleksandr Rybalko uart_unlock(sc->sc_hwmtx); 578a2c472e7SAleksandr Rybalko 579a2c472e7SAleksandr Rybalko return (0); 580a2c472e7SAleksandr Rybalko } 581d76a1ef4SWarner Losh 582d76a1ef4SWarner Losh static void 583d76a1ef4SWarner Losh imx_uart_bus_grab(struct uart_softc *sc) 584d76a1ef4SWarner Losh { 585d76a1ef4SWarner Losh struct uart_bas *bas = &sc->sc_bas; 586d76a1ef4SWarner Losh 587d76a1ef4SWarner Losh bas = &sc->sc_bas; 588d76a1ef4SWarner Losh uart_lock(sc->sc_hwmtx); 589*0dc54d18SIan Lepore DIS(bas, UCR1, RRDYEN); 590*0dc54d18SIan Lepore DIS(bas, UCR2, ATEN); 591d76a1ef4SWarner Losh uart_unlock(sc->sc_hwmtx); 592d76a1ef4SWarner Losh } 593d76a1ef4SWarner Losh 594d76a1ef4SWarner Losh static void 595d76a1ef4SWarner Losh imx_uart_bus_ungrab(struct uart_softc *sc) 596d76a1ef4SWarner Losh { 597d76a1ef4SWarner Losh struct uart_bas *bas = &sc->sc_bas; 598d76a1ef4SWarner Losh 599d76a1ef4SWarner Losh bas = &sc->sc_bas; 600d76a1ef4SWarner Losh uart_lock(sc->sc_hwmtx); 601*0dc54d18SIan Lepore ENA(bas, UCR1, RRDYEN); 602*0dc54d18SIan Lepore ENA(bas, UCR2, ATEN); 603d76a1ef4SWarner Losh uart_unlock(sc->sc_hwmtx); 604d76a1ef4SWarner Losh } 605