1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 22ac4ad2aSVineet Gupta /* 32ac4ad2aSVineet Gupta * ARC On-Chip(fpga) UART Driver 42ac4ad2aSVineet Gupta * 52ac4ad2aSVineet Gupta * Copyright (C) 2010-2012 Synopsys, Inc. (www.synopsys.com) 62ac4ad2aSVineet Gupta * 72ac4ad2aSVineet Gupta * vineetg: July 10th 2012 82ac4ad2aSVineet Gupta * -Decoupled the driver from arch/arc 92ac4ad2aSVineet Gupta * +Using platform_get_resource() for irq/membase (thx to bfin_uart.c) 102ac4ad2aSVineet Gupta * +Using early_platform_xxx() for early console (thx to mach-shmobile/xxx) 112ac4ad2aSVineet Gupta * 122ac4ad2aSVineet Gupta * Vineetg: Aug 21st 2010 132ac4ad2aSVineet Gupta * -Is uart_tx_stopped() not done in tty write path as it has already been 142ac4ad2aSVineet Gupta * taken care of, in serial core 152ac4ad2aSVineet Gupta * 162ac4ad2aSVineet Gupta * Vineetg: Aug 18th 2010 172ac4ad2aSVineet Gupta * -New Serial Core based ARC UART driver 182ac4ad2aSVineet Gupta * -Derived largely from blackfin driver albiet with some major tweaks 192ac4ad2aSVineet Gupta * 202ac4ad2aSVineet Gupta * TODO: 212ac4ad2aSVineet Gupta * -check if sysreq works 222ac4ad2aSVineet Gupta */ 232ac4ad2aSVineet Gupta 242ac4ad2aSVineet Gupta #include <linux/module.h> 252ac4ad2aSVineet Gupta #include <linux/serial.h> 262ac4ad2aSVineet Gupta #include <linux/console.h> 272ac4ad2aSVineet Gupta #include <linux/sysrq.h> 282ac4ad2aSVineet Gupta #include <linux/platform_device.h> 292ac4ad2aSVineet Gupta #include <linux/tty.h> 302ac4ad2aSVineet Gupta #include <linux/tty_flip.h> 312ac4ad2aSVineet Gupta #include <linux/serial_core.h> 322ac4ad2aSVineet Gupta #include <linux/io.h> 3391f1b62aSVineet Gupta #include <linux/of_irq.h> 3491f1b62aSVineet Gupta #include <linux/of_address.h> 352ac4ad2aSVineet Gupta 362ac4ad2aSVineet Gupta /************************************* 372ac4ad2aSVineet Gupta * ARC UART Hardware Specs 382ac4ad2aSVineet Gupta ************************************/ 392ac4ad2aSVineet Gupta #define ARC_UART_TX_FIFO_SIZE 1 402ac4ad2aSVineet Gupta 412ac4ad2aSVineet Gupta /* 422ac4ad2aSVineet Gupta * UART Register set (this is not a Standards Compliant IP) 432ac4ad2aSVineet Gupta * Also each reg is Word aligned, but only 8 bits wide 442ac4ad2aSVineet Gupta */ 452ac4ad2aSVineet Gupta #define R_ID0 0 462ac4ad2aSVineet Gupta #define R_ID1 4 472ac4ad2aSVineet Gupta #define R_ID2 8 482ac4ad2aSVineet Gupta #define R_ID3 12 492ac4ad2aSVineet Gupta #define R_DATA 16 502ac4ad2aSVineet Gupta #define R_STS 20 512ac4ad2aSVineet Gupta #define R_BAUDL 24 522ac4ad2aSVineet Gupta #define R_BAUDH 28 532ac4ad2aSVineet Gupta 542ac4ad2aSVineet Gupta /* Bits for UART Status Reg (R/W) */ 552ac4ad2aSVineet Gupta #define RXIENB 0x04 /* Receive Interrupt Enable */ 562ac4ad2aSVineet Gupta #define TXIENB 0x40 /* Transmit Interrupt Enable */ 572ac4ad2aSVineet Gupta 582ac4ad2aSVineet Gupta #define RXEMPTY 0x20 /* Receive FIFO Empty: No char receivede */ 592ac4ad2aSVineet Gupta #define TXEMPTY 0x80 /* Transmit FIFO Empty, thus char can be written into */ 602ac4ad2aSVineet Gupta 612ac4ad2aSVineet Gupta #define RXFULL 0x08 /* Receive FIFO full */ 622ac4ad2aSVineet Gupta #define RXFULL1 0x10 /* Receive FIFO has space for 1 char (tot space=4) */ 632ac4ad2aSVineet Gupta 642ac4ad2aSVineet Gupta #define RXFERR 0x01 /* Frame Error: Stop Bit not detected */ 652ac4ad2aSVineet Gupta #define RXOERR 0x02 /* OverFlow Err: Char recv but RXFULL still set */ 662ac4ad2aSVineet Gupta 672ac4ad2aSVineet Gupta /* Uart bit fiddling helpers: lowest level */ 6812d15e6fSVineet Gupta #define RBASE(port, reg) (port->membase + reg) 692ac4ad2aSVineet Gupta #define UART_REG_SET(u, r, v) writeb((v), RBASE(u, r)) 702ac4ad2aSVineet Gupta #define UART_REG_GET(u, r) readb(RBASE(u, r)) 712ac4ad2aSVineet Gupta 722ac4ad2aSVineet Gupta #define UART_REG_OR(u, r, v) UART_REG_SET(u, r, UART_REG_GET(u, r) | (v)) 732ac4ad2aSVineet Gupta #define UART_REG_CLR(u, r, v) UART_REG_SET(u, r, UART_REG_GET(u, r) & ~(v)) 742ac4ad2aSVineet Gupta 752ac4ad2aSVineet Gupta /* Uart bit fiddling helpers: API level */ 762ac4ad2aSVineet Gupta #define UART_SET_DATA(uart, val) UART_REG_SET(uart, R_DATA, val) 772ac4ad2aSVineet Gupta #define UART_GET_DATA(uart) UART_REG_GET(uart, R_DATA) 782ac4ad2aSVineet Gupta 792ac4ad2aSVineet Gupta #define UART_SET_BAUDH(uart, val) UART_REG_SET(uart, R_BAUDH, val) 802ac4ad2aSVineet Gupta #define UART_SET_BAUDL(uart, val) UART_REG_SET(uart, R_BAUDL, val) 812ac4ad2aSVineet Gupta 822ac4ad2aSVineet Gupta #define UART_CLR_STATUS(uart, val) UART_REG_CLR(uart, R_STS, val) 832ac4ad2aSVineet Gupta #define UART_GET_STATUS(uart) UART_REG_GET(uart, R_STS) 842ac4ad2aSVineet Gupta 852ac4ad2aSVineet Gupta #define UART_ALL_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, RXIENB|TXIENB) 862ac4ad2aSVineet Gupta #define UART_RX_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, RXIENB) 872ac4ad2aSVineet Gupta #define UART_TX_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, TXIENB) 882ac4ad2aSVineet Gupta 892ac4ad2aSVineet Gupta #define UART_ALL_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, RXIENB|TXIENB) 902ac4ad2aSVineet Gupta #define UART_RX_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, RXIENB) 912ac4ad2aSVineet Gupta #define UART_TX_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, TXIENB) 922ac4ad2aSVineet Gupta 932ac4ad2aSVineet Gupta #define ARC_SERIAL_DEV_NAME "ttyARC" 942ac4ad2aSVineet Gupta 952ac4ad2aSVineet Gupta struct arc_uart_port { 962ac4ad2aSVineet Gupta struct uart_port port; 972ac4ad2aSVineet Gupta unsigned long baud; 982ac4ad2aSVineet Gupta }; 992ac4ad2aSVineet Gupta 1002ac4ad2aSVineet Gupta #define to_arc_port(uport) container_of(uport, struct arc_uart_port, port) 1012ac4ad2aSVineet Gupta 1022ac4ad2aSVineet Gupta static struct arc_uart_port arc_uart_ports[CONFIG_SERIAL_ARC_NR_PORTS]; 1032ac4ad2aSVineet Gupta 1042ac4ad2aSVineet Gupta #ifdef CONFIG_SERIAL_ARC_CONSOLE 1052ac4ad2aSVineet Gupta static struct console arc_console; 1062ac4ad2aSVineet Gupta #endif 1072ac4ad2aSVineet Gupta 1082ac4ad2aSVineet Gupta #define DRIVER_NAME "arc-uart" 1092ac4ad2aSVineet Gupta 1102ac4ad2aSVineet Gupta static struct uart_driver arc_uart_driver = { 1112ac4ad2aSVineet Gupta .owner = THIS_MODULE, 1122ac4ad2aSVineet Gupta .driver_name = DRIVER_NAME, 1132ac4ad2aSVineet Gupta .dev_name = ARC_SERIAL_DEV_NAME, 1142ac4ad2aSVineet Gupta .major = 0, 1152ac4ad2aSVineet Gupta .minor = 0, 1162ac4ad2aSVineet Gupta .nr = CONFIG_SERIAL_ARC_NR_PORTS, 1172ac4ad2aSVineet Gupta #ifdef CONFIG_SERIAL_ARC_CONSOLE 1182ac4ad2aSVineet Gupta .cons = &arc_console, 1192ac4ad2aSVineet Gupta #endif 1202ac4ad2aSVineet Gupta }; 1212ac4ad2aSVineet Gupta 1222ac4ad2aSVineet Gupta static void arc_serial_stop_rx(struct uart_port *port) 1232ac4ad2aSVineet Gupta { 12412d15e6fSVineet Gupta UART_RX_IRQ_DISABLE(port); 1252ac4ad2aSVineet Gupta } 1262ac4ad2aSVineet Gupta 1272ac4ad2aSVineet Gupta static void arc_serial_stop_tx(struct uart_port *port) 1282ac4ad2aSVineet Gupta { 12912d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY)) 1302ac4ad2aSVineet Gupta cpu_relax(); 1312ac4ad2aSVineet Gupta 13212d15e6fSVineet Gupta UART_TX_IRQ_DISABLE(port); 1332ac4ad2aSVineet Gupta } 1342ac4ad2aSVineet Gupta 1352ac4ad2aSVineet Gupta /* 1362ac4ad2aSVineet Gupta * Return TIOCSER_TEMT when transmitter is not busy. 1372ac4ad2aSVineet Gupta */ 1382ac4ad2aSVineet Gupta static unsigned int arc_serial_tx_empty(struct uart_port *port) 1392ac4ad2aSVineet Gupta { 1402ac4ad2aSVineet Gupta unsigned int stat; 1412ac4ad2aSVineet Gupta 14212d15e6fSVineet Gupta stat = UART_GET_STATUS(port); 1432ac4ad2aSVineet Gupta if (stat & TXEMPTY) 1442ac4ad2aSVineet Gupta return TIOCSER_TEMT; 1452ac4ad2aSVineet Gupta 1462ac4ad2aSVineet Gupta return 0; 1472ac4ad2aSVineet Gupta } 1482ac4ad2aSVineet Gupta 1492ac4ad2aSVineet Gupta /* 1502ac4ad2aSVineet Gupta * Driver internal routine, used by both tty(serial core) as well as tx-isr 1512ac4ad2aSVineet Gupta * -Called under spinlock in either cases 1526e94dbc7SJiri Slaby * -also tty->flow.stopped has already been checked 1532ac4ad2aSVineet Gupta * = by uart_start( ) before calling us 1542ac4ad2aSVineet Gupta * = tx_ist checks that too before calling 1552ac4ad2aSVineet Gupta */ 15612d15e6fSVineet Gupta static void arc_serial_tx_chars(struct uart_port *port) 1572ac4ad2aSVineet Gupta { 15812d15e6fSVineet Gupta struct circ_buf *xmit = &port->state->xmit; 1592ac4ad2aSVineet Gupta int sent = 0; 1602ac4ad2aSVineet Gupta unsigned char ch; 1612ac4ad2aSVineet Gupta 16212d15e6fSVineet Gupta if (unlikely(port->x_char)) { 16312d15e6fSVineet Gupta UART_SET_DATA(port, port->x_char); 16412d15e6fSVineet Gupta port->icount.tx++; 16512d15e6fSVineet Gupta port->x_char = 0; 1662ac4ad2aSVineet Gupta sent = 1; 16799ecb001SPeter Hurley } else if (!uart_circ_empty(xmit)) { 1682ac4ad2aSVineet Gupta ch = xmit->buf[xmit->tail]; 1692ac4ad2aSVineet Gupta xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 17012d15e6fSVineet Gupta port->icount.tx++; 17112d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY)) 1722ac4ad2aSVineet Gupta cpu_relax(); 17312d15e6fSVineet Gupta UART_SET_DATA(port, ch); 1742ac4ad2aSVineet Gupta sent = 1; 1752ac4ad2aSVineet Gupta } 1762ac4ad2aSVineet Gupta 1772ac4ad2aSVineet Gupta /* 1782ac4ad2aSVineet Gupta * If num chars in xmit buffer are too few, ask tty layer for more. 1792ac4ad2aSVineet Gupta * By Hard ISR to schedule processing in software interrupt part 1802ac4ad2aSVineet Gupta */ 1812ac4ad2aSVineet Gupta if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 18212d15e6fSVineet Gupta uart_write_wakeup(port); 1832ac4ad2aSVineet Gupta 1842ac4ad2aSVineet Gupta if (sent) 18512d15e6fSVineet Gupta UART_TX_IRQ_ENABLE(port); 1862ac4ad2aSVineet Gupta } 1872ac4ad2aSVineet Gupta 1882ac4ad2aSVineet Gupta /* 1892ac4ad2aSVineet Gupta * port is locked and interrupts are disabled 1902ac4ad2aSVineet Gupta * uart_start( ) calls us under the port spinlock irqsave 1912ac4ad2aSVineet Gupta */ 1922ac4ad2aSVineet Gupta static void arc_serial_start_tx(struct uart_port *port) 1932ac4ad2aSVineet Gupta { 19412d15e6fSVineet Gupta arc_serial_tx_chars(port); 1952ac4ad2aSVineet Gupta } 1962ac4ad2aSVineet Gupta 19712d15e6fSVineet Gupta static void arc_serial_rx_chars(struct uart_port *port, unsigned int status) 1982ac4ad2aSVineet Gupta { 1995284eba7SVineet Gupta unsigned int ch, flg = 0; 2002ac4ad2aSVineet Gupta 2012ac4ad2aSVineet Gupta /* 2022ac4ad2aSVineet Gupta * UART has 4 deep RX-FIFO. Driver's recongnition of this fact 2032ac4ad2aSVineet Gupta * is very subtle. Here's how ... 2042ac4ad2aSVineet Gupta * Upon getting a RX-Intr, such that RX-EMPTY=0, meaning data available, 2052ac4ad2aSVineet Gupta * driver reads the DATA Reg and keeps doing that in a loop, until 2062ac4ad2aSVineet Gupta * RX-EMPTY=1. Multiple chars being avail, with a single Interrupt, 2072ac4ad2aSVineet Gupta * before RX-EMPTY=0, implies some sort of buffering going on in the 2082ac4ad2aSVineet Gupta * controller, which is indeed the Rx-FIFO. 2092ac4ad2aSVineet Gupta */ 2105284eba7SVineet Gupta do { 2115284eba7SVineet Gupta /* 2125284eba7SVineet Gupta * This could be an Rx Intr for err (no data), 2135284eba7SVineet Gupta * so check err and clear that Intr first 2145284eba7SVineet Gupta */ 2152ac4ad2aSVineet Gupta if (unlikely(status & (RXOERR | RXFERR))) { 2162ac4ad2aSVineet Gupta if (status & RXOERR) { 21712d15e6fSVineet Gupta port->icount.overrun++; 2182ac4ad2aSVineet Gupta flg = TTY_OVERRUN; 21912d15e6fSVineet Gupta UART_CLR_STATUS(port, RXOERR); 2202ac4ad2aSVineet Gupta } 2212ac4ad2aSVineet Gupta 2222ac4ad2aSVineet Gupta if (status & RXFERR) { 22312d15e6fSVineet Gupta port->icount.frame++; 2242ac4ad2aSVineet Gupta flg = TTY_FRAME; 22512d15e6fSVineet Gupta UART_CLR_STATUS(port, RXFERR); 2262ac4ad2aSVineet Gupta } 2272ac4ad2aSVineet Gupta } else 2282ac4ad2aSVineet Gupta flg = TTY_NORMAL; 2292ac4ad2aSVineet Gupta 2305284eba7SVineet Gupta if (status & RXEMPTY) 2315284eba7SVineet Gupta continue; 2325284eba7SVineet Gupta 23312d15e6fSVineet Gupta ch = UART_GET_DATA(port); 23412d15e6fSVineet Gupta port->icount.rx++; 2355284eba7SVineet Gupta 23612d15e6fSVineet Gupta if (!(uart_handle_sysrq_char(port, ch))) 23712d15e6fSVineet Gupta uart_insert_char(port, status, RXOERR, ch, flg); 2382ac4ad2aSVineet Gupta 23912d15e6fSVineet Gupta tty_flip_buffer_push(&port->state->port); 24012d15e6fSVineet Gupta } while (!((status = UART_GET_STATUS(port)) & RXEMPTY)); 2412ac4ad2aSVineet Gupta } 2422ac4ad2aSVineet Gupta 2432ac4ad2aSVineet Gupta /* 2442ac4ad2aSVineet Gupta * A note on the Interrupt handling state machine of this driver 2452ac4ad2aSVineet Gupta * 2462ac4ad2aSVineet Gupta * kernel printk writes funnel thru the console driver framework and in order 2472ac4ad2aSVineet Gupta * to keep things simple as well as efficient, it writes to UART in polled 2482ac4ad2aSVineet Gupta * mode, in one shot, and exits. 2492ac4ad2aSVineet Gupta * 2502ac4ad2aSVineet Gupta * OTOH, Userland output (via tty layer), uses interrupt based writes as there 2512ac4ad2aSVineet Gupta * can be undeterministic delay between char writes. 2522ac4ad2aSVineet Gupta * 2532ac4ad2aSVineet Gupta * Thus Rx-interrupts are always enabled, while tx-interrupts are by default 2542ac4ad2aSVineet Gupta * disabled. 2552ac4ad2aSVineet Gupta * 2562ac4ad2aSVineet Gupta * When tty has some data to send out, serial core calls driver's start_tx 2572ac4ad2aSVineet Gupta * which 2582ac4ad2aSVineet Gupta * -checks-if-tty-buffer-has-char-to-send 2592ac4ad2aSVineet Gupta * -writes-data-to-uart 2602ac4ad2aSVineet Gupta * -enable-tx-intr 2612ac4ad2aSVineet Gupta * 2622ac4ad2aSVineet Gupta * Once data bits are pushed out, controller raises the Tx-room-avail-Interrupt. 2632ac4ad2aSVineet Gupta * The first thing Tx ISR does is disable further Tx interrupts (as this could 2642ac4ad2aSVineet Gupta * be the last char to send, before settling down into the quiet polled mode). 2652ac4ad2aSVineet Gupta * It then calls the exact routine used by tty layer write to send out any 2662ac4ad2aSVineet Gupta * more char in tty buffer. In case of sending, it re-enables Tx-intr. In case 2672ac4ad2aSVineet Gupta * of no data, it remains disabled. 2682ac4ad2aSVineet Gupta * This is how the transmit state machine is dynamically switched on/off 2692ac4ad2aSVineet Gupta */ 2702ac4ad2aSVineet Gupta 2712ac4ad2aSVineet Gupta static irqreturn_t arc_serial_isr(int irq, void *dev_id) 2722ac4ad2aSVineet Gupta { 27312d15e6fSVineet Gupta struct uart_port *port = dev_id; 2742ac4ad2aSVineet Gupta unsigned int status; 2752ac4ad2aSVineet Gupta 27612d15e6fSVineet Gupta status = UART_GET_STATUS(port); 2772ac4ad2aSVineet Gupta 2782ac4ad2aSVineet Gupta /* 2792ac4ad2aSVineet Gupta * Single IRQ for both Rx (data available) Tx (room available) Interrupt 2802ac4ad2aSVineet Gupta * notifications from the UART Controller. 2812ac4ad2aSVineet Gupta * To demultiplex between the two, we check the relevant bits 2822ac4ad2aSVineet Gupta */ 2835284eba7SVineet Gupta if (status & RXIENB) { 2842ac4ad2aSVineet Gupta 2852ac4ad2aSVineet Gupta /* already in ISR, no need of xx_irqsave */ 28612d15e6fSVineet Gupta spin_lock(&port->lock); 28712d15e6fSVineet Gupta arc_serial_rx_chars(port, status); 28812d15e6fSVineet Gupta spin_unlock(&port->lock); 2892ac4ad2aSVineet Gupta } 2902ac4ad2aSVineet Gupta 2912ac4ad2aSVineet Gupta if ((status & TXIENB) && (status & TXEMPTY)) { 2922ac4ad2aSVineet Gupta 2932ac4ad2aSVineet Gupta /* Unconditionally disable further Tx-Interrupts. 2942ac4ad2aSVineet Gupta * will be enabled by tx_chars() if needed. 2952ac4ad2aSVineet Gupta */ 29612d15e6fSVineet Gupta UART_TX_IRQ_DISABLE(port); 2972ac4ad2aSVineet Gupta 29812d15e6fSVineet Gupta spin_lock(&port->lock); 2992ac4ad2aSVineet Gupta 30012d15e6fSVineet Gupta if (!uart_tx_stopped(port)) 30112d15e6fSVineet Gupta arc_serial_tx_chars(port); 3022ac4ad2aSVineet Gupta 30312d15e6fSVineet Gupta spin_unlock(&port->lock); 3042ac4ad2aSVineet Gupta } 3052ac4ad2aSVineet Gupta 3062ac4ad2aSVineet Gupta return IRQ_HANDLED; 3072ac4ad2aSVineet Gupta } 3082ac4ad2aSVineet Gupta 3092ac4ad2aSVineet Gupta static unsigned int arc_serial_get_mctrl(struct uart_port *port) 3102ac4ad2aSVineet Gupta { 3112ac4ad2aSVineet Gupta /* 3122ac4ad2aSVineet Gupta * Pretend we have a Modem status reg and following bits are 3132ac4ad2aSVineet Gupta * always set, to satify the serial core state machine 3142ac4ad2aSVineet Gupta * (DSR) Data Set Ready 3152ac4ad2aSVineet Gupta * (CTS) Clear To Send 3162ac4ad2aSVineet Gupta * (CAR) Carrier Detect 3172ac4ad2aSVineet Gupta */ 3182ac4ad2aSVineet Gupta return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR; 3192ac4ad2aSVineet Gupta } 3202ac4ad2aSVineet Gupta 3212ac4ad2aSVineet Gupta static void arc_serial_set_mctrl(struct uart_port *port, unsigned int mctrl) 3222ac4ad2aSVineet Gupta { 3232ac4ad2aSVineet Gupta /* MCR not present */ 3242ac4ad2aSVineet Gupta } 3252ac4ad2aSVineet Gupta 3262ac4ad2aSVineet Gupta static void arc_serial_break_ctl(struct uart_port *port, int break_state) 3272ac4ad2aSVineet Gupta { 3282ac4ad2aSVineet Gupta /* ARC UART doesn't support sending Break signal */ 3292ac4ad2aSVineet Gupta } 3302ac4ad2aSVineet Gupta 3312ac4ad2aSVineet Gupta static int arc_serial_startup(struct uart_port *port) 3322ac4ad2aSVineet Gupta { 3332ac4ad2aSVineet Gupta /* Before we hook up the ISR, Disable all UART Interrupts */ 33412d15e6fSVineet Gupta UART_ALL_IRQ_DISABLE(port); 3352ac4ad2aSVineet Gupta 33612d15e6fSVineet Gupta if (request_irq(port->irq, arc_serial_isr, 0, "arc uart rx-tx", port)) { 33712d15e6fSVineet Gupta dev_warn(port->dev, "Unable to attach ARC UART intr\n"); 3382ac4ad2aSVineet Gupta return -EBUSY; 3392ac4ad2aSVineet Gupta } 3402ac4ad2aSVineet Gupta 34112d15e6fSVineet Gupta UART_RX_IRQ_ENABLE(port); /* Only Rx IRQ enabled to begin with */ 3422ac4ad2aSVineet Gupta 3432ac4ad2aSVineet Gupta return 0; 3442ac4ad2aSVineet Gupta } 3452ac4ad2aSVineet Gupta 3462ac4ad2aSVineet Gupta /* This is not really needed */ 3472ac4ad2aSVineet Gupta static void arc_serial_shutdown(struct uart_port *port) 3482ac4ad2aSVineet Gupta { 34912d15e6fSVineet Gupta free_irq(port->irq, port); 3502ac4ad2aSVineet Gupta } 3512ac4ad2aSVineet Gupta 3522ac4ad2aSVineet Gupta static void 3532ac4ad2aSVineet Gupta arc_serial_set_termios(struct uart_port *port, struct ktermios *new, 3542ac4ad2aSVineet Gupta struct ktermios *old) 3552ac4ad2aSVineet Gupta { 3562ac4ad2aSVineet Gupta struct arc_uart_port *uart = to_arc_port(port); 3572ac4ad2aSVineet Gupta unsigned int baud, uartl, uarth, hw_val; 3582ac4ad2aSVineet Gupta unsigned long flags; 3592ac4ad2aSVineet Gupta 3602ac4ad2aSVineet Gupta /* 3612ac4ad2aSVineet Gupta * Use the generic handler so that any specially encoded baud rates 3622ac4ad2aSVineet Gupta * such as SPD_xx flags or "%B0" can be handled 3632ac4ad2aSVineet Gupta * Max Baud I suppose will not be more than current 115K * 4 3642ac4ad2aSVineet Gupta * Formula for ARC UART is: hw-val = ((CLK/(BAUD*4)) -1) 3652ac4ad2aSVineet Gupta * spread over two 8-bit registers 3662ac4ad2aSVineet Gupta */ 3672ac4ad2aSVineet Gupta baud = uart_get_baud_rate(port, new, old, 0, 460800); 3682ac4ad2aSVineet Gupta 3692ac4ad2aSVineet Gupta hw_val = port->uartclk / (uart->baud * 4) - 1; 3702ac4ad2aSVineet Gupta uartl = hw_val & 0xFF; 3712ac4ad2aSVineet Gupta uarth = (hw_val >> 8) & 0xFF; 3722ac4ad2aSVineet Gupta 3732ac4ad2aSVineet Gupta spin_lock_irqsave(&port->lock, flags); 3742ac4ad2aSVineet Gupta 37512d15e6fSVineet Gupta UART_ALL_IRQ_DISABLE(port); 3762ac4ad2aSVineet Gupta 37712d15e6fSVineet Gupta UART_SET_BAUDL(port, uartl); 37812d15e6fSVineet Gupta UART_SET_BAUDH(port, uarth); 3792ac4ad2aSVineet Gupta 38012d15e6fSVineet Gupta UART_RX_IRQ_ENABLE(port); 3812ac4ad2aSVineet Gupta 3822ac4ad2aSVineet Gupta /* 3832ac4ad2aSVineet Gupta * UART doesn't support Parity/Hardware Flow Control; 3842ac4ad2aSVineet Gupta * Only supports 8N1 character size 3852ac4ad2aSVineet Gupta */ 3862ac4ad2aSVineet Gupta new->c_cflag &= ~(CMSPAR|CRTSCTS|CSIZE); 3872ac4ad2aSVineet Gupta new->c_cflag |= CS8; 3882ac4ad2aSVineet Gupta 3892ac4ad2aSVineet Gupta if (old) 3902ac4ad2aSVineet Gupta tty_termios_copy_hw(new, old); 3912ac4ad2aSVineet Gupta 3922ac4ad2aSVineet Gupta /* Don't rewrite B0 */ 3932ac4ad2aSVineet Gupta if (tty_termios_baud_rate(new)) 3942ac4ad2aSVineet Gupta tty_termios_encode_baud_rate(new, baud, baud); 3952ac4ad2aSVineet Gupta 3962ac4ad2aSVineet Gupta uart_update_timeout(port, new->c_cflag, baud); 3972ac4ad2aSVineet Gupta 3982ac4ad2aSVineet Gupta spin_unlock_irqrestore(&port->lock, flags); 3992ac4ad2aSVineet Gupta } 4002ac4ad2aSVineet Gupta 4012ac4ad2aSVineet Gupta static const char *arc_serial_type(struct uart_port *port) 4022ac4ad2aSVineet Gupta { 40312d15e6fSVineet Gupta return port->type == PORT_ARC ? DRIVER_NAME : NULL; 4042ac4ad2aSVineet Gupta } 4052ac4ad2aSVineet Gupta 4062ac4ad2aSVineet Gupta static void arc_serial_release_port(struct uart_port *port) 4072ac4ad2aSVineet Gupta { 4082ac4ad2aSVineet Gupta } 4092ac4ad2aSVineet Gupta 4102ac4ad2aSVineet Gupta static int arc_serial_request_port(struct uart_port *port) 4112ac4ad2aSVineet Gupta { 4122ac4ad2aSVineet Gupta return 0; 4132ac4ad2aSVineet Gupta } 4142ac4ad2aSVineet Gupta 4152ac4ad2aSVineet Gupta /* 4162ac4ad2aSVineet Gupta * Verify the new serial_struct (for TIOCSSERIAL). 4172ac4ad2aSVineet Gupta */ 4182ac4ad2aSVineet Gupta static int 4192ac4ad2aSVineet Gupta arc_serial_verify_port(struct uart_port *port, struct serial_struct *ser) 4202ac4ad2aSVineet Gupta { 4212ac4ad2aSVineet Gupta if (port->type != PORT_UNKNOWN && ser->type != PORT_ARC) 4222ac4ad2aSVineet Gupta return -EINVAL; 4232ac4ad2aSVineet Gupta 4242ac4ad2aSVineet Gupta return 0; 4252ac4ad2aSVineet Gupta } 4262ac4ad2aSVineet Gupta 4272ac4ad2aSVineet Gupta /* 4282ac4ad2aSVineet Gupta * Configure/autoconfigure the port. 4292ac4ad2aSVineet Gupta */ 4302ac4ad2aSVineet Gupta static void arc_serial_config_port(struct uart_port *port, int flags) 4312ac4ad2aSVineet Gupta { 4322ac4ad2aSVineet Gupta if (flags & UART_CONFIG_TYPE) 43312d15e6fSVineet Gupta port->type = PORT_ARC; 4342ac4ad2aSVineet Gupta } 4352ac4ad2aSVineet Gupta 436f363ca2fSVineet Gupta #ifdef CONFIG_CONSOLE_POLL 4372ac4ad2aSVineet Gupta 438f363ca2fSVineet Gupta static void arc_serial_poll_putchar(struct uart_port *port, unsigned char chr) 4392ac4ad2aSVineet Gupta { 44012d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY)) 4412ac4ad2aSVineet Gupta cpu_relax(); 4422ac4ad2aSVineet Gupta 443f363ca2fSVineet Gupta UART_SET_DATA(port, chr); 4442ac4ad2aSVineet Gupta } 4452ac4ad2aSVineet Gupta 4462ac4ad2aSVineet Gupta static int arc_serial_poll_getchar(struct uart_port *port) 4472ac4ad2aSVineet Gupta { 4482ac4ad2aSVineet Gupta unsigned char chr; 4492ac4ad2aSVineet Gupta 45012d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & RXEMPTY)) 4512ac4ad2aSVineet Gupta cpu_relax(); 4522ac4ad2aSVineet Gupta 45312d15e6fSVineet Gupta chr = UART_GET_DATA(port); 4542ac4ad2aSVineet Gupta return chr; 4552ac4ad2aSVineet Gupta } 4562ac4ad2aSVineet Gupta #endif 4572ac4ad2aSVineet Gupta 4588232884eSJulia Lawall static const struct uart_ops arc_serial_pops = { 4592ac4ad2aSVineet Gupta .tx_empty = arc_serial_tx_empty, 4602ac4ad2aSVineet Gupta .set_mctrl = arc_serial_set_mctrl, 4612ac4ad2aSVineet Gupta .get_mctrl = arc_serial_get_mctrl, 4622ac4ad2aSVineet Gupta .stop_tx = arc_serial_stop_tx, 4632ac4ad2aSVineet Gupta .start_tx = arc_serial_start_tx, 4642ac4ad2aSVineet Gupta .stop_rx = arc_serial_stop_rx, 4652ac4ad2aSVineet Gupta .break_ctl = arc_serial_break_ctl, 4662ac4ad2aSVineet Gupta .startup = arc_serial_startup, 4672ac4ad2aSVineet Gupta .shutdown = arc_serial_shutdown, 4682ac4ad2aSVineet Gupta .set_termios = arc_serial_set_termios, 4692ac4ad2aSVineet Gupta .type = arc_serial_type, 4702ac4ad2aSVineet Gupta .release_port = arc_serial_release_port, 4712ac4ad2aSVineet Gupta .request_port = arc_serial_request_port, 4722ac4ad2aSVineet Gupta .config_port = arc_serial_config_port, 4732ac4ad2aSVineet Gupta .verify_port = arc_serial_verify_port, 4742ac4ad2aSVineet Gupta #ifdef CONFIG_CONSOLE_POLL 4752ac4ad2aSVineet Gupta .poll_put_char = arc_serial_poll_putchar, 4762ac4ad2aSVineet Gupta .poll_get_char = arc_serial_poll_getchar, 4772ac4ad2aSVineet Gupta #endif 4782ac4ad2aSVineet Gupta }; 4792ac4ad2aSVineet Gupta 4802ac4ad2aSVineet Gupta #ifdef CONFIG_SERIAL_ARC_CONSOLE 4812ac4ad2aSVineet Gupta 4829671f099SBill Pemberton static int arc_serial_console_setup(struct console *co, char *options) 4832ac4ad2aSVineet Gupta { 4842ac4ad2aSVineet Gupta struct uart_port *port; 4852ac4ad2aSVineet Gupta int baud = 115200; 4862ac4ad2aSVineet Gupta int bits = 8; 4872ac4ad2aSVineet Gupta int parity = 'n'; 4882ac4ad2aSVineet Gupta int flow = 'n'; 4892ac4ad2aSVineet Gupta 4902ac4ad2aSVineet Gupta if (co->index < 0 || co->index >= CONFIG_SERIAL_ARC_NR_PORTS) 4912ac4ad2aSVineet Gupta return -ENODEV; 4922ac4ad2aSVineet Gupta 4932ac4ad2aSVineet Gupta /* 4942ac4ad2aSVineet Gupta * The uart port backing the console (e.g. ttyARC1) might not have been 4952ac4ad2aSVineet Gupta * init yet. If so, defer the console setup to after the port. 4962ac4ad2aSVineet Gupta */ 4972ac4ad2aSVineet Gupta port = &arc_uart_ports[co->index].port; 4982ac4ad2aSVineet Gupta if (!port->membase) 4992ac4ad2aSVineet Gupta return -ENODEV; 5002ac4ad2aSVineet Gupta 5012ac4ad2aSVineet Gupta if (options) 5022ac4ad2aSVineet Gupta uart_parse_options(options, &baud, &parity, &bits, &flow); 5032ac4ad2aSVineet Gupta 5042ac4ad2aSVineet Gupta /* 5052ac4ad2aSVineet Gupta * Serial core will call port->ops->set_termios( ) 5062ac4ad2aSVineet Gupta * which will set the baud reg 5072ac4ad2aSVineet Gupta */ 5082ac4ad2aSVineet Gupta return uart_set_options(port, co, baud, parity, bits, flow); 5092ac4ad2aSVineet Gupta } 5102ac4ad2aSVineet Gupta 511*3f8bab17SJiri Slaby static void arc_serial_console_putchar(struct uart_port *port, unsigned char ch) 512f363ca2fSVineet Gupta { 513f363ca2fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY)) 514f363ca2fSVineet Gupta cpu_relax(); 515f363ca2fSVineet Gupta 516f363ca2fSVineet Gupta UART_SET_DATA(port, (unsigned char)ch); 517f363ca2fSVineet Gupta } 518f363ca2fSVineet Gupta 5192ac4ad2aSVineet Gupta /* 5202ac4ad2aSVineet Gupta * Interrupts are disabled on entering 5212ac4ad2aSVineet Gupta */ 5222ac4ad2aSVineet Gupta static void arc_serial_console_write(struct console *co, const char *s, 5232ac4ad2aSVineet Gupta unsigned int count) 5242ac4ad2aSVineet Gupta { 5252ac4ad2aSVineet Gupta struct uart_port *port = &arc_uart_ports[co->index].port; 5262ac4ad2aSVineet Gupta unsigned long flags; 5272ac4ad2aSVineet Gupta 5282ac4ad2aSVineet Gupta spin_lock_irqsave(&port->lock, flags); 529f363ca2fSVineet Gupta uart_console_write(port, s, count, arc_serial_console_putchar); 5302ac4ad2aSVineet Gupta spin_unlock_irqrestore(&port->lock, flags); 5312ac4ad2aSVineet Gupta } 5322ac4ad2aSVineet Gupta 5332ac4ad2aSVineet Gupta static struct console arc_console = { 5342ac4ad2aSVineet Gupta .name = ARC_SERIAL_DEV_NAME, 5352ac4ad2aSVineet Gupta .write = arc_serial_console_write, 5362ac4ad2aSVineet Gupta .device = uart_console_device, 5372ac4ad2aSVineet Gupta .setup = arc_serial_console_setup, 5382ac4ad2aSVineet Gupta .flags = CON_PRINTBUFFER, 5392ac4ad2aSVineet Gupta .index = -1, 5402ac4ad2aSVineet Gupta .data = &arc_uart_driver 5412ac4ad2aSVineet Gupta }; 5422ac4ad2aSVineet Gupta 543ac1e6965SJeffy Chen static void arc_early_serial_write(struct console *con, const char *s, 54427cfe4ecSVineet Gupta unsigned int n) 54527cfe4ecSVineet Gupta { 54627cfe4ecSVineet Gupta struct earlycon_device *dev = con->data; 54727cfe4ecSVineet Gupta 548f363ca2fSVineet Gupta uart_console_write(&dev->port, s, n, arc_serial_console_putchar); 54927cfe4ecSVineet Gupta } 55027cfe4ecSVineet Gupta 55127cfe4ecSVineet Gupta static int __init arc_early_console_setup(struct earlycon_device *dev, 55227cfe4ecSVineet Gupta const char *opt) 55327cfe4ecSVineet Gupta { 55427cfe4ecSVineet Gupta struct uart_port *port = &dev->port; 55527cfe4ecSVineet Gupta unsigned int l, h, hw_val; 55627cfe4ecSVineet Gupta 55727cfe4ecSVineet Gupta if (!dev->port.membase) 55827cfe4ecSVineet Gupta return -ENODEV; 55927cfe4ecSVineet Gupta 56027cfe4ecSVineet Gupta hw_val = port->uartclk / (dev->baud * 4) - 1; 56127cfe4ecSVineet Gupta l = hw_val & 0xFF; 56227cfe4ecSVineet Gupta h = (hw_val >> 8) & 0xFF; 56327cfe4ecSVineet Gupta 56427cfe4ecSVineet Gupta UART_SET_BAUDL(port, l); 56527cfe4ecSVineet Gupta UART_SET_BAUDH(port, h); 56627cfe4ecSVineet Gupta 56727cfe4ecSVineet Gupta dev->con->write = arc_early_serial_write; 56827cfe4ecSVineet Gupta return 0; 56927cfe4ecSVineet Gupta } 5708c6abf7aSVineet Gupta OF_EARLYCON_DECLARE(arc_uart, "snps,arc-uart", arc_early_console_setup); 57127cfe4ecSVineet Gupta 5722ac4ad2aSVineet Gupta #endif /* CONFIG_SERIAL_ARC_CONSOLE */ 5732ac4ad2aSVineet Gupta 5749671f099SBill Pemberton static int arc_serial_probe(struct platform_device *pdev) 5752ac4ad2aSVineet Gupta { 576ea28fd56SVineet Gupta struct device_node *np = pdev->dev.of_node; 5778dbe1d5eSVineet Gupta struct arc_uart_port *uart; 5788dbe1d5eSVineet Gupta struct uart_port *port; 5798dbe1d5eSVineet Gupta int dev_id; 5808dbe1d5eSVineet Gupta u32 val; 5812ac4ad2aSVineet Gupta 582ea28fd56SVineet Gupta /* no device tree device */ 583ea28fd56SVineet Gupta if (!np) 584ea28fd56SVineet Gupta return -ENODEV; 585ea28fd56SVineet Gupta 586ea28fd56SVineet Gupta dev_id = of_alias_get_id(np, "serial"); 58711c62d4fSVineet Gupta if (dev_id < 0) 58811c62d4fSVineet Gupta dev_id = 0; 589ea28fd56SVineet Gupta 590f9f57869SGeert Uytterhoeven if (dev_id >= ARRAY_SIZE(arc_uart_ports)) { 591f9f57869SGeert Uytterhoeven dev_err(&pdev->dev, "serial%d out of range\n", dev_id); 592f9f57869SGeert Uytterhoeven return -EINVAL; 593f9f57869SGeert Uytterhoeven } 594f9f57869SGeert Uytterhoeven 5958dbe1d5eSVineet Gupta uart = &arc_uart_ports[dev_id]; 5968dbe1d5eSVineet Gupta port = &uart->port; 5972ac4ad2aSVineet Gupta 5988dbe1d5eSVineet Gupta if (of_property_read_u32(np, "clock-frequency", &val)) { 5998dbe1d5eSVineet Gupta dev_err(&pdev->dev, "clock-frequency property NOTset\n"); 6008dbe1d5eSVineet Gupta return -EINVAL; 6018dbe1d5eSVineet Gupta } 6028dbe1d5eSVineet Gupta port->uartclk = val; 6038dbe1d5eSVineet Gupta 6048dbe1d5eSVineet Gupta if (of_property_read_u32(np, "current-speed", &val)) { 6058dbe1d5eSVineet Gupta dev_err(&pdev->dev, "current-speed property NOT set\n"); 6068dbe1d5eSVineet Gupta return -EINVAL; 6078dbe1d5eSVineet Gupta } 6088dbe1d5eSVineet Gupta uart->baud = val; 6098dbe1d5eSVineet Gupta 6108dbe1d5eSVineet Gupta port->membase = of_iomap(np, 0); 6118dbe1d5eSVineet Gupta if (!port->membase) 6128dbe1d5eSVineet Gupta /* No point of dev_err since UART itself is hosed here */ 6138dbe1d5eSVineet Gupta return -ENXIO; 6148dbe1d5eSVineet Gupta 6158dbe1d5eSVineet Gupta port->irq = irq_of_parse_and_map(np, 0); 6168dbe1d5eSVineet Gupta 6178dbe1d5eSVineet Gupta port->dev = &pdev->dev; 6188dbe1d5eSVineet Gupta port->iotype = UPIO_MEM; 6198dbe1d5eSVineet Gupta port->flags = UPF_BOOT_AUTOCONF; 6208dbe1d5eSVineet Gupta port->line = dev_id; 6218dbe1d5eSVineet Gupta port->ops = &arc_serial_pops; 622933505e9SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_ARC_CONSOLE); 6238dbe1d5eSVineet Gupta 6248dbe1d5eSVineet Gupta port->fifosize = ARC_UART_TX_FIFO_SIZE; 6258dbe1d5eSVineet Gupta 6268dbe1d5eSVineet Gupta /* 6278dbe1d5eSVineet Gupta * uart_insert_char( ) uses it in decideding whether to ignore a 6288dbe1d5eSVineet Gupta * char or not. Explicitly setting it here, removes the subtelty 6298dbe1d5eSVineet Gupta */ 6308dbe1d5eSVineet Gupta port->ignore_status_mask = 0; 6318dbe1d5eSVineet Gupta 6328dbe1d5eSVineet Gupta return uart_add_one_port(&arc_uart_driver, &arc_uart_ports[dev_id].port); 6332ac4ad2aSVineet Gupta } 6342ac4ad2aSVineet Gupta 635ae8d8a14SBill Pemberton static int arc_serial_remove(struct platform_device *pdev) 6362ac4ad2aSVineet Gupta { 6372ac4ad2aSVineet Gupta /* This will never be called */ 6382ac4ad2aSVineet Gupta return 0; 6392ac4ad2aSVineet Gupta } 6402ac4ad2aSVineet Gupta 641ea28fd56SVineet Gupta static const struct of_device_id arc_uart_dt_ids[] = { 642ea28fd56SVineet Gupta { .compatible = "snps,arc-uart" }, 643ea28fd56SVineet Gupta { /* Sentinel */ } 644ea28fd56SVineet Gupta }; 645ea28fd56SVineet Gupta MODULE_DEVICE_TABLE(of, arc_uart_dt_ids); 646ea28fd56SVineet Gupta 6472ac4ad2aSVineet Gupta static struct platform_driver arc_platform_driver = { 6482ac4ad2aSVineet Gupta .probe = arc_serial_probe, 6492d47b716SBill Pemberton .remove = arc_serial_remove, 6502ac4ad2aSVineet Gupta .driver = { 6512ac4ad2aSVineet Gupta .name = DRIVER_NAME, 652ea28fd56SVineet Gupta .of_match_table = arc_uart_dt_ids, 6532ac4ad2aSVineet Gupta }, 6542ac4ad2aSVineet Gupta }; 6552ac4ad2aSVineet Gupta 6562ac4ad2aSVineet Gupta static int __init arc_serial_init(void) 6572ac4ad2aSVineet Gupta { 6582ac4ad2aSVineet Gupta int ret; 6592ac4ad2aSVineet Gupta 6602ac4ad2aSVineet Gupta ret = uart_register_driver(&arc_uart_driver); 6612ac4ad2aSVineet Gupta if (ret) 6622ac4ad2aSVineet Gupta return ret; 6632ac4ad2aSVineet Gupta 6642ac4ad2aSVineet Gupta ret = platform_driver_register(&arc_platform_driver); 6652ac4ad2aSVineet Gupta if (ret) 6662ac4ad2aSVineet Gupta uart_unregister_driver(&arc_uart_driver); 6672ac4ad2aSVineet Gupta 6682ac4ad2aSVineet Gupta return ret; 6692ac4ad2aSVineet Gupta } 6702ac4ad2aSVineet Gupta 6712ac4ad2aSVineet Gupta static void __exit arc_serial_exit(void) 6722ac4ad2aSVineet Gupta { 6732ac4ad2aSVineet Gupta platform_driver_unregister(&arc_platform_driver); 6742ac4ad2aSVineet Gupta uart_unregister_driver(&arc_uart_driver); 6752ac4ad2aSVineet Gupta } 6762ac4ad2aSVineet Gupta 6772ac4ad2aSVineet Gupta module_init(arc_serial_init); 6782ac4ad2aSVineet Gupta module_exit(arc_serial_exit); 6792ac4ad2aSVineet Gupta 6802ac4ad2aSVineet Gupta MODULE_LICENSE("GPL"); 681d5a12ea7SAxel Lin MODULE_ALIAS("platform:" DRIVER_NAME); 6822ac4ad2aSVineet Gupta MODULE_AUTHOR("Vineet Gupta"); 6832ac4ad2aSVineet Gupta MODULE_DESCRIPTION("ARC(Synopsys) On-Chip(fpga) serial driver"); 684