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
arc_serial_stop_rx(struct uart_port * port)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
arc_serial_stop_tx(struct uart_port * port)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 */
arc_serial_tx_empty(struct uart_port * port)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 */
arc_serial_tx_chars(struct uart_port * port)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];
1698a8dee2cSIlpo Järvinen uart_xmit_advance(port, 1);
17012d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY))
1712ac4ad2aSVineet Gupta cpu_relax();
17212d15e6fSVineet Gupta UART_SET_DATA(port, ch);
1732ac4ad2aSVineet Gupta sent = 1;
1742ac4ad2aSVineet Gupta }
1752ac4ad2aSVineet Gupta
1762ac4ad2aSVineet Gupta /*
1772ac4ad2aSVineet Gupta * If num chars in xmit buffer are too few, ask tty layer for more.
1782ac4ad2aSVineet Gupta * By Hard ISR to schedule processing in software interrupt part
1792ac4ad2aSVineet Gupta */
1802ac4ad2aSVineet Gupta if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
18112d15e6fSVineet Gupta uart_write_wakeup(port);
1822ac4ad2aSVineet Gupta
1832ac4ad2aSVineet Gupta if (sent)
18412d15e6fSVineet Gupta UART_TX_IRQ_ENABLE(port);
1852ac4ad2aSVineet Gupta }
1862ac4ad2aSVineet Gupta
1872ac4ad2aSVineet Gupta /*
1882ac4ad2aSVineet Gupta * port is locked and interrupts are disabled
1892ac4ad2aSVineet Gupta * uart_start( ) calls us under the port spinlock irqsave
1902ac4ad2aSVineet Gupta */
arc_serial_start_tx(struct uart_port * port)1912ac4ad2aSVineet Gupta static void arc_serial_start_tx(struct uart_port *port)
1922ac4ad2aSVineet Gupta {
19312d15e6fSVineet Gupta arc_serial_tx_chars(port);
1942ac4ad2aSVineet Gupta }
1952ac4ad2aSVineet Gupta
arc_serial_rx_chars(struct uart_port * port,unsigned int status)19612d15e6fSVineet Gupta static void arc_serial_rx_chars(struct uart_port *port, unsigned int status)
1972ac4ad2aSVineet Gupta {
1982ac4ad2aSVineet Gupta /*
1992ac4ad2aSVineet Gupta * UART has 4 deep RX-FIFO. Driver's recongnition of this fact
2002ac4ad2aSVineet Gupta * is very subtle. Here's how ...
2012ac4ad2aSVineet Gupta * Upon getting a RX-Intr, such that RX-EMPTY=0, meaning data available,
2022ac4ad2aSVineet Gupta * driver reads the DATA Reg and keeps doing that in a loop, until
2032ac4ad2aSVineet Gupta * RX-EMPTY=1. Multiple chars being avail, with a single Interrupt,
2042ac4ad2aSVineet Gupta * before RX-EMPTY=0, implies some sort of buffering going on in the
2052ac4ad2aSVineet Gupta * controller, which is indeed the Rx-FIFO.
2062ac4ad2aSVineet Gupta */
2075284eba7SVineet Gupta do {
208*fd2b55f8SJiri Slaby u8 ch, flg = TTY_NORMAL;
20929ec63efSJiri Slaby
2105284eba7SVineet Gupta /*
2115284eba7SVineet Gupta * This could be an Rx Intr for err (no data),
2125284eba7SVineet Gupta * so check err and clear that Intr first
2135284eba7SVineet Gupta */
2142ac4ad2aSVineet Gupta if (status & RXOERR) {
21512d15e6fSVineet Gupta port->icount.overrun++;
2162ac4ad2aSVineet Gupta flg = TTY_OVERRUN;
21712d15e6fSVineet Gupta UART_CLR_STATUS(port, RXOERR);
2182ac4ad2aSVineet Gupta }
2192ac4ad2aSVineet Gupta
2202ac4ad2aSVineet Gupta if (status & RXFERR) {
22112d15e6fSVineet Gupta port->icount.frame++;
2222ac4ad2aSVineet Gupta flg = TTY_FRAME;
22312d15e6fSVineet Gupta UART_CLR_STATUS(port, RXFERR);
2242ac4ad2aSVineet Gupta }
2252ac4ad2aSVineet Gupta
2265284eba7SVineet Gupta if (status & RXEMPTY)
2275284eba7SVineet Gupta continue;
2285284eba7SVineet Gupta
22912d15e6fSVineet Gupta ch = UART_GET_DATA(port);
23012d15e6fSVineet Gupta port->icount.rx++;
2315284eba7SVineet Gupta
23212d15e6fSVineet Gupta if (!(uart_handle_sysrq_char(port, ch)))
23312d15e6fSVineet Gupta uart_insert_char(port, status, RXOERR, ch, flg);
2342ac4ad2aSVineet Gupta
23512d15e6fSVineet Gupta tty_flip_buffer_push(&port->state->port);
23612d15e6fSVineet Gupta } while (!((status = UART_GET_STATUS(port)) & RXEMPTY));
2372ac4ad2aSVineet Gupta }
2382ac4ad2aSVineet Gupta
2392ac4ad2aSVineet Gupta /*
2402ac4ad2aSVineet Gupta * A note on the Interrupt handling state machine of this driver
2412ac4ad2aSVineet Gupta *
2422ac4ad2aSVineet Gupta * kernel printk writes funnel thru the console driver framework and in order
2432ac4ad2aSVineet Gupta * to keep things simple as well as efficient, it writes to UART in polled
2442ac4ad2aSVineet Gupta * mode, in one shot, and exits.
2452ac4ad2aSVineet Gupta *
2462ac4ad2aSVineet Gupta * OTOH, Userland output (via tty layer), uses interrupt based writes as there
2472ac4ad2aSVineet Gupta * can be undeterministic delay between char writes.
2482ac4ad2aSVineet Gupta *
2492ac4ad2aSVineet Gupta * Thus Rx-interrupts are always enabled, while tx-interrupts are by default
2502ac4ad2aSVineet Gupta * disabled.
2512ac4ad2aSVineet Gupta *
2522ac4ad2aSVineet Gupta * When tty has some data to send out, serial core calls driver's start_tx
2532ac4ad2aSVineet Gupta * which
2542ac4ad2aSVineet Gupta * -checks-if-tty-buffer-has-char-to-send
2552ac4ad2aSVineet Gupta * -writes-data-to-uart
2562ac4ad2aSVineet Gupta * -enable-tx-intr
2572ac4ad2aSVineet Gupta *
2582ac4ad2aSVineet Gupta * Once data bits are pushed out, controller raises the Tx-room-avail-Interrupt.
2592ac4ad2aSVineet Gupta * The first thing Tx ISR does is disable further Tx interrupts (as this could
2602ac4ad2aSVineet Gupta * be the last char to send, before settling down into the quiet polled mode).
2612ac4ad2aSVineet Gupta * It then calls the exact routine used by tty layer write to send out any
2622ac4ad2aSVineet Gupta * more char in tty buffer. In case of sending, it re-enables Tx-intr. In case
2632ac4ad2aSVineet Gupta * of no data, it remains disabled.
2642ac4ad2aSVineet Gupta * This is how the transmit state machine is dynamically switched on/off
2652ac4ad2aSVineet Gupta */
2662ac4ad2aSVineet Gupta
arc_serial_isr(int irq,void * dev_id)2672ac4ad2aSVineet Gupta static irqreturn_t arc_serial_isr(int irq, void *dev_id)
2682ac4ad2aSVineet Gupta {
26912d15e6fSVineet Gupta struct uart_port *port = dev_id;
2702ac4ad2aSVineet Gupta unsigned int status;
2712ac4ad2aSVineet Gupta
27212d15e6fSVineet Gupta status = UART_GET_STATUS(port);
2732ac4ad2aSVineet Gupta
2742ac4ad2aSVineet Gupta /*
2752ac4ad2aSVineet Gupta * Single IRQ for both Rx (data available) Tx (room available) Interrupt
2762ac4ad2aSVineet Gupta * notifications from the UART Controller.
2772ac4ad2aSVineet Gupta * To demultiplex between the two, we check the relevant bits
2782ac4ad2aSVineet Gupta */
2795284eba7SVineet Gupta if (status & RXIENB) {
2802ac4ad2aSVineet Gupta
2812ac4ad2aSVineet Gupta /* already in ISR, no need of xx_irqsave */
28212d15e6fSVineet Gupta spin_lock(&port->lock);
28312d15e6fSVineet Gupta arc_serial_rx_chars(port, status);
28412d15e6fSVineet Gupta spin_unlock(&port->lock);
2852ac4ad2aSVineet Gupta }
2862ac4ad2aSVineet Gupta
2872ac4ad2aSVineet Gupta if ((status & TXIENB) && (status & TXEMPTY)) {
2882ac4ad2aSVineet Gupta
2892ac4ad2aSVineet Gupta /* Unconditionally disable further Tx-Interrupts.
2902ac4ad2aSVineet Gupta * will be enabled by tx_chars() if needed.
2912ac4ad2aSVineet Gupta */
29212d15e6fSVineet Gupta UART_TX_IRQ_DISABLE(port);
2932ac4ad2aSVineet Gupta
29412d15e6fSVineet Gupta spin_lock(&port->lock);
2952ac4ad2aSVineet Gupta
29612d15e6fSVineet Gupta if (!uart_tx_stopped(port))
29712d15e6fSVineet Gupta arc_serial_tx_chars(port);
2982ac4ad2aSVineet Gupta
29912d15e6fSVineet Gupta spin_unlock(&port->lock);
3002ac4ad2aSVineet Gupta }
3012ac4ad2aSVineet Gupta
3022ac4ad2aSVineet Gupta return IRQ_HANDLED;
3032ac4ad2aSVineet Gupta }
3042ac4ad2aSVineet Gupta
arc_serial_get_mctrl(struct uart_port * port)3052ac4ad2aSVineet Gupta static unsigned int arc_serial_get_mctrl(struct uart_port *port)
3062ac4ad2aSVineet Gupta {
3072ac4ad2aSVineet Gupta /*
3082ac4ad2aSVineet Gupta * Pretend we have a Modem status reg and following bits are
3092ac4ad2aSVineet Gupta * always set, to satify the serial core state machine
3102ac4ad2aSVineet Gupta * (DSR) Data Set Ready
3112ac4ad2aSVineet Gupta * (CTS) Clear To Send
3122ac4ad2aSVineet Gupta * (CAR) Carrier Detect
3132ac4ad2aSVineet Gupta */
3142ac4ad2aSVineet Gupta return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
3152ac4ad2aSVineet Gupta }
3162ac4ad2aSVineet Gupta
arc_serial_set_mctrl(struct uart_port * port,unsigned int mctrl)3172ac4ad2aSVineet Gupta static void arc_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
3182ac4ad2aSVineet Gupta {
3192ac4ad2aSVineet Gupta /* MCR not present */
3202ac4ad2aSVineet Gupta }
3212ac4ad2aSVineet Gupta
arc_serial_break_ctl(struct uart_port * port,int break_state)3222ac4ad2aSVineet Gupta static void arc_serial_break_ctl(struct uart_port *port, int break_state)
3232ac4ad2aSVineet Gupta {
3242ac4ad2aSVineet Gupta /* ARC UART doesn't support sending Break signal */
3252ac4ad2aSVineet Gupta }
3262ac4ad2aSVineet Gupta
arc_serial_startup(struct uart_port * port)3272ac4ad2aSVineet Gupta static int arc_serial_startup(struct uart_port *port)
3282ac4ad2aSVineet Gupta {
3292ac4ad2aSVineet Gupta /* Before we hook up the ISR, Disable all UART Interrupts */
33012d15e6fSVineet Gupta UART_ALL_IRQ_DISABLE(port);
3312ac4ad2aSVineet Gupta
33212d15e6fSVineet Gupta if (request_irq(port->irq, arc_serial_isr, 0, "arc uart rx-tx", port)) {
33312d15e6fSVineet Gupta dev_warn(port->dev, "Unable to attach ARC UART intr\n");
3342ac4ad2aSVineet Gupta return -EBUSY;
3352ac4ad2aSVineet Gupta }
3362ac4ad2aSVineet Gupta
33712d15e6fSVineet Gupta UART_RX_IRQ_ENABLE(port); /* Only Rx IRQ enabled to begin with */
3382ac4ad2aSVineet Gupta
3392ac4ad2aSVineet Gupta return 0;
3402ac4ad2aSVineet Gupta }
3412ac4ad2aSVineet Gupta
3422ac4ad2aSVineet Gupta /* This is not really needed */
arc_serial_shutdown(struct uart_port * port)3432ac4ad2aSVineet Gupta static void arc_serial_shutdown(struct uart_port *port)
3442ac4ad2aSVineet Gupta {
34512d15e6fSVineet Gupta free_irq(port->irq, port);
3462ac4ad2aSVineet Gupta }
3472ac4ad2aSVineet Gupta
3482ac4ad2aSVineet Gupta static void
arc_serial_set_termios(struct uart_port * port,struct ktermios * new,const struct ktermios * old)3492ac4ad2aSVineet Gupta arc_serial_set_termios(struct uart_port *port, struct ktermios *new,
350bec5b814SIlpo Järvinen const struct ktermios *old)
3512ac4ad2aSVineet Gupta {
3522ac4ad2aSVineet Gupta struct arc_uart_port *uart = to_arc_port(port);
3532ac4ad2aSVineet Gupta unsigned int baud, uartl, uarth, hw_val;
3542ac4ad2aSVineet Gupta unsigned long flags;
3552ac4ad2aSVineet Gupta
3562ac4ad2aSVineet Gupta /*
3572ac4ad2aSVineet Gupta * Use the generic handler so that any specially encoded baud rates
3582ac4ad2aSVineet Gupta * such as SPD_xx flags or "%B0" can be handled
3592ac4ad2aSVineet Gupta * Max Baud I suppose will not be more than current 115K * 4
3602ac4ad2aSVineet Gupta * Formula for ARC UART is: hw-val = ((CLK/(BAUD*4)) -1)
3612ac4ad2aSVineet Gupta * spread over two 8-bit registers
3622ac4ad2aSVineet Gupta */
3632ac4ad2aSVineet Gupta baud = uart_get_baud_rate(port, new, old, 0, 460800);
3642ac4ad2aSVineet Gupta
3652ac4ad2aSVineet Gupta hw_val = port->uartclk / (uart->baud * 4) - 1;
3662ac4ad2aSVineet Gupta uartl = hw_val & 0xFF;
3672ac4ad2aSVineet Gupta uarth = (hw_val >> 8) & 0xFF;
3682ac4ad2aSVineet Gupta
3692ac4ad2aSVineet Gupta spin_lock_irqsave(&port->lock, flags);
3702ac4ad2aSVineet Gupta
37112d15e6fSVineet Gupta UART_ALL_IRQ_DISABLE(port);
3722ac4ad2aSVineet Gupta
37312d15e6fSVineet Gupta UART_SET_BAUDL(port, uartl);
37412d15e6fSVineet Gupta UART_SET_BAUDH(port, uarth);
3752ac4ad2aSVineet Gupta
37612d15e6fSVineet Gupta UART_RX_IRQ_ENABLE(port);
3772ac4ad2aSVineet Gupta
3782ac4ad2aSVineet Gupta /*
3792ac4ad2aSVineet Gupta * UART doesn't support Parity/Hardware Flow Control;
3802ac4ad2aSVineet Gupta * Only supports 8N1 character size
3812ac4ad2aSVineet Gupta */
3822ac4ad2aSVineet Gupta new->c_cflag &= ~(CMSPAR|CRTSCTS|CSIZE);
3832ac4ad2aSVineet Gupta new->c_cflag |= CS8;
3842ac4ad2aSVineet Gupta
3852ac4ad2aSVineet Gupta if (old)
3862ac4ad2aSVineet Gupta tty_termios_copy_hw(new, old);
3872ac4ad2aSVineet Gupta
3882ac4ad2aSVineet Gupta /* Don't rewrite B0 */
3892ac4ad2aSVineet Gupta if (tty_termios_baud_rate(new))
3902ac4ad2aSVineet Gupta tty_termios_encode_baud_rate(new, baud, baud);
3912ac4ad2aSVineet Gupta
3922ac4ad2aSVineet Gupta uart_update_timeout(port, new->c_cflag, baud);
3932ac4ad2aSVineet Gupta
3942ac4ad2aSVineet Gupta spin_unlock_irqrestore(&port->lock, flags);
3952ac4ad2aSVineet Gupta }
3962ac4ad2aSVineet Gupta
arc_serial_type(struct uart_port * port)3972ac4ad2aSVineet Gupta static const char *arc_serial_type(struct uart_port *port)
3982ac4ad2aSVineet Gupta {
39912d15e6fSVineet Gupta return port->type == PORT_ARC ? DRIVER_NAME : NULL;
4002ac4ad2aSVineet Gupta }
4012ac4ad2aSVineet Gupta
arc_serial_release_port(struct uart_port * port)4022ac4ad2aSVineet Gupta static void arc_serial_release_port(struct uart_port *port)
4032ac4ad2aSVineet Gupta {
4042ac4ad2aSVineet Gupta }
4052ac4ad2aSVineet Gupta
arc_serial_request_port(struct uart_port * port)4062ac4ad2aSVineet Gupta static int arc_serial_request_port(struct uart_port *port)
4072ac4ad2aSVineet Gupta {
4082ac4ad2aSVineet Gupta return 0;
4092ac4ad2aSVineet Gupta }
4102ac4ad2aSVineet Gupta
4112ac4ad2aSVineet Gupta /*
4122ac4ad2aSVineet Gupta * Verify the new serial_struct (for TIOCSSERIAL).
4132ac4ad2aSVineet Gupta */
4142ac4ad2aSVineet Gupta static int
arc_serial_verify_port(struct uart_port * port,struct serial_struct * ser)4152ac4ad2aSVineet Gupta arc_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
4162ac4ad2aSVineet Gupta {
4172ac4ad2aSVineet Gupta if (port->type != PORT_UNKNOWN && ser->type != PORT_ARC)
4182ac4ad2aSVineet Gupta return -EINVAL;
4192ac4ad2aSVineet Gupta
4202ac4ad2aSVineet Gupta return 0;
4212ac4ad2aSVineet Gupta }
4222ac4ad2aSVineet Gupta
4232ac4ad2aSVineet Gupta /*
4242ac4ad2aSVineet Gupta * Configure/autoconfigure the port.
4252ac4ad2aSVineet Gupta */
arc_serial_config_port(struct uart_port * port,int flags)4262ac4ad2aSVineet Gupta static void arc_serial_config_port(struct uart_port *port, int flags)
4272ac4ad2aSVineet Gupta {
4282ac4ad2aSVineet Gupta if (flags & UART_CONFIG_TYPE)
42912d15e6fSVineet Gupta port->type = PORT_ARC;
4302ac4ad2aSVineet Gupta }
4312ac4ad2aSVineet Gupta
432f363ca2fSVineet Gupta #ifdef CONFIG_CONSOLE_POLL
4332ac4ad2aSVineet Gupta
arc_serial_poll_putchar(struct uart_port * port,unsigned char chr)434f363ca2fSVineet Gupta static void arc_serial_poll_putchar(struct uart_port *port, unsigned char chr)
4352ac4ad2aSVineet Gupta {
43612d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY))
4372ac4ad2aSVineet Gupta cpu_relax();
4382ac4ad2aSVineet Gupta
439f363ca2fSVineet Gupta UART_SET_DATA(port, chr);
4402ac4ad2aSVineet Gupta }
4412ac4ad2aSVineet Gupta
arc_serial_poll_getchar(struct uart_port * port)4422ac4ad2aSVineet Gupta static int arc_serial_poll_getchar(struct uart_port *port)
4432ac4ad2aSVineet Gupta {
4442ac4ad2aSVineet Gupta unsigned char chr;
4452ac4ad2aSVineet Gupta
44612d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & RXEMPTY))
4472ac4ad2aSVineet Gupta cpu_relax();
4482ac4ad2aSVineet Gupta
44912d15e6fSVineet Gupta chr = UART_GET_DATA(port);
4502ac4ad2aSVineet Gupta return chr;
4512ac4ad2aSVineet Gupta }
4522ac4ad2aSVineet Gupta #endif
4532ac4ad2aSVineet Gupta
4548232884eSJulia Lawall static const struct uart_ops arc_serial_pops = {
4552ac4ad2aSVineet Gupta .tx_empty = arc_serial_tx_empty,
4562ac4ad2aSVineet Gupta .set_mctrl = arc_serial_set_mctrl,
4572ac4ad2aSVineet Gupta .get_mctrl = arc_serial_get_mctrl,
4582ac4ad2aSVineet Gupta .stop_tx = arc_serial_stop_tx,
4592ac4ad2aSVineet Gupta .start_tx = arc_serial_start_tx,
4602ac4ad2aSVineet Gupta .stop_rx = arc_serial_stop_rx,
4612ac4ad2aSVineet Gupta .break_ctl = arc_serial_break_ctl,
4622ac4ad2aSVineet Gupta .startup = arc_serial_startup,
4632ac4ad2aSVineet Gupta .shutdown = arc_serial_shutdown,
4642ac4ad2aSVineet Gupta .set_termios = arc_serial_set_termios,
4652ac4ad2aSVineet Gupta .type = arc_serial_type,
4662ac4ad2aSVineet Gupta .release_port = arc_serial_release_port,
4672ac4ad2aSVineet Gupta .request_port = arc_serial_request_port,
4682ac4ad2aSVineet Gupta .config_port = arc_serial_config_port,
4692ac4ad2aSVineet Gupta .verify_port = arc_serial_verify_port,
4702ac4ad2aSVineet Gupta #ifdef CONFIG_CONSOLE_POLL
4712ac4ad2aSVineet Gupta .poll_put_char = arc_serial_poll_putchar,
4722ac4ad2aSVineet Gupta .poll_get_char = arc_serial_poll_getchar,
4732ac4ad2aSVineet Gupta #endif
4742ac4ad2aSVineet Gupta };
4752ac4ad2aSVineet Gupta
4762ac4ad2aSVineet Gupta #ifdef CONFIG_SERIAL_ARC_CONSOLE
4772ac4ad2aSVineet Gupta
arc_serial_console_setup(struct console * co,char * options)4789671f099SBill Pemberton static int arc_serial_console_setup(struct console *co, char *options)
4792ac4ad2aSVineet Gupta {
4802ac4ad2aSVineet Gupta struct uart_port *port;
4812ac4ad2aSVineet Gupta int baud = 115200;
4822ac4ad2aSVineet Gupta int bits = 8;
4832ac4ad2aSVineet Gupta int parity = 'n';
4842ac4ad2aSVineet Gupta int flow = 'n';
4852ac4ad2aSVineet Gupta
4862ac4ad2aSVineet Gupta if (co->index < 0 || co->index >= CONFIG_SERIAL_ARC_NR_PORTS)
4872ac4ad2aSVineet Gupta return -ENODEV;
4882ac4ad2aSVineet Gupta
4892ac4ad2aSVineet Gupta /*
4902ac4ad2aSVineet Gupta * The uart port backing the console (e.g. ttyARC1) might not have been
4912ac4ad2aSVineet Gupta * init yet. If so, defer the console setup to after the port.
4922ac4ad2aSVineet Gupta */
4932ac4ad2aSVineet Gupta port = &arc_uart_ports[co->index].port;
4942ac4ad2aSVineet Gupta if (!port->membase)
4952ac4ad2aSVineet Gupta return -ENODEV;
4962ac4ad2aSVineet Gupta
4972ac4ad2aSVineet Gupta if (options)
4982ac4ad2aSVineet Gupta uart_parse_options(options, &baud, &parity, &bits, &flow);
4992ac4ad2aSVineet Gupta
5002ac4ad2aSVineet Gupta /*
5012ac4ad2aSVineet Gupta * Serial core will call port->ops->set_termios( )
5022ac4ad2aSVineet Gupta * which will set the baud reg
5032ac4ad2aSVineet Gupta */
5042ac4ad2aSVineet Gupta return uart_set_options(port, co, baud, parity, bits, flow);
5052ac4ad2aSVineet Gupta }
5062ac4ad2aSVineet Gupta
arc_serial_console_putchar(struct uart_port * port,unsigned char ch)5073f8bab17SJiri Slaby static void arc_serial_console_putchar(struct uart_port *port, unsigned char ch)
508f363ca2fSVineet Gupta {
509f363ca2fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY))
510f363ca2fSVineet Gupta cpu_relax();
511f363ca2fSVineet Gupta
512f363ca2fSVineet Gupta UART_SET_DATA(port, (unsigned char)ch);
513f363ca2fSVineet Gupta }
514f363ca2fSVineet Gupta
5152ac4ad2aSVineet Gupta /*
5162ac4ad2aSVineet Gupta * Interrupts are disabled on entering
5172ac4ad2aSVineet Gupta */
arc_serial_console_write(struct console * co,const char * s,unsigned int count)5182ac4ad2aSVineet Gupta static void arc_serial_console_write(struct console *co, const char *s,
5192ac4ad2aSVineet Gupta unsigned int count)
5202ac4ad2aSVineet Gupta {
5212ac4ad2aSVineet Gupta struct uart_port *port = &arc_uart_ports[co->index].port;
5222ac4ad2aSVineet Gupta unsigned long flags;
5232ac4ad2aSVineet Gupta
5242ac4ad2aSVineet Gupta spin_lock_irqsave(&port->lock, flags);
525f363ca2fSVineet Gupta uart_console_write(port, s, count, arc_serial_console_putchar);
5262ac4ad2aSVineet Gupta spin_unlock_irqrestore(&port->lock, flags);
5272ac4ad2aSVineet Gupta }
5282ac4ad2aSVineet Gupta
5292ac4ad2aSVineet Gupta static struct console arc_console = {
5302ac4ad2aSVineet Gupta .name = ARC_SERIAL_DEV_NAME,
5312ac4ad2aSVineet Gupta .write = arc_serial_console_write,
5322ac4ad2aSVineet Gupta .device = uart_console_device,
5332ac4ad2aSVineet Gupta .setup = arc_serial_console_setup,
5342ac4ad2aSVineet Gupta .flags = CON_PRINTBUFFER,
5352ac4ad2aSVineet Gupta .index = -1,
5362ac4ad2aSVineet Gupta .data = &arc_uart_driver
5372ac4ad2aSVineet Gupta };
5382ac4ad2aSVineet Gupta
arc_early_serial_write(struct console * con,const char * s,unsigned int n)539ac1e6965SJeffy Chen static void arc_early_serial_write(struct console *con, const char *s,
54027cfe4ecSVineet Gupta unsigned int n)
54127cfe4ecSVineet Gupta {
54227cfe4ecSVineet Gupta struct earlycon_device *dev = con->data;
54327cfe4ecSVineet Gupta
544f363ca2fSVineet Gupta uart_console_write(&dev->port, s, n, arc_serial_console_putchar);
54527cfe4ecSVineet Gupta }
54627cfe4ecSVineet Gupta
arc_early_console_setup(struct earlycon_device * dev,const char * opt)54727cfe4ecSVineet Gupta static int __init arc_early_console_setup(struct earlycon_device *dev,
54827cfe4ecSVineet Gupta const char *opt)
54927cfe4ecSVineet Gupta {
55027cfe4ecSVineet Gupta struct uart_port *port = &dev->port;
55127cfe4ecSVineet Gupta unsigned int l, h, hw_val;
55227cfe4ecSVineet Gupta
55327cfe4ecSVineet Gupta if (!dev->port.membase)
55427cfe4ecSVineet Gupta return -ENODEV;
55527cfe4ecSVineet Gupta
55627cfe4ecSVineet Gupta hw_val = port->uartclk / (dev->baud * 4) - 1;
55727cfe4ecSVineet Gupta l = hw_val & 0xFF;
55827cfe4ecSVineet Gupta h = (hw_val >> 8) & 0xFF;
55927cfe4ecSVineet Gupta
56027cfe4ecSVineet Gupta UART_SET_BAUDL(port, l);
56127cfe4ecSVineet Gupta UART_SET_BAUDH(port, h);
56227cfe4ecSVineet Gupta
56327cfe4ecSVineet Gupta dev->con->write = arc_early_serial_write;
56427cfe4ecSVineet Gupta return 0;
56527cfe4ecSVineet Gupta }
5668c6abf7aSVineet Gupta OF_EARLYCON_DECLARE(arc_uart, "snps,arc-uart", arc_early_console_setup);
56727cfe4ecSVineet Gupta
5682ac4ad2aSVineet Gupta #endif /* CONFIG_SERIAL_ARC_CONSOLE */
5692ac4ad2aSVineet Gupta
arc_serial_probe(struct platform_device * pdev)5709671f099SBill Pemberton static int arc_serial_probe(struct platform_device *pdev)
5712ac4ad2aSVineet Gupta {
572ea28fd56SVineet Gupta struct device_node *np = pdev->dev.of_node;
5738dbe1d5eSVineet Gupta struct arc_uart_port *uart;
5748dbe1d5eSVineet Gupta struct uart_port *port;
5758dbe1d5eSVineet Gupta int dev_id;
5768dbe1d5eSVineet Gupta u32 val;
5772ac4ad2aSVineet Gupta
578ea28fd56SVineet Gupta /* no device tree device */
579ea28fd56SVineet Gupta if (!np)
580ea28fd56SVineet Gupta return -ENODEV;
581ea28fd56SVineet Gupta
582ea28fd56SVineet Gupta dev_id = of_alias_get_id(np, "serial");
58311c62d4fSVineet Gupta if (dev_id < 0)
58411c62d4fSVineet Gupta dev_id = 0;
585ea28fd56SVineet Gupta
586f9f57869SGeert Uytterhoeven if (dev_id >= ARRAY_SIZE(arc_uart_ports)) {
587f9f57869SGeert Uytterhoeven dev_err(&pdev->dev, "serial%d out of range\n", dev_id);
588f9f57869SGeert Uytterhoeven return -EINVAL;
589f9f57869SGeert Uytterhoeven }
590f9f57869SGeert Uytterhoeven
5918dbe1d5eSVineet Gupta uart = &arc_uart_ports[dev_id];
5928dbe1d5eSVineet Gupta port = &uart->port;
5932ac4ad2aSVineet Gupta
5948dbe1d5eSVineet Gupta if (of_property_read_u32(np, "clock-frequency", &val)) {
5958dbe1d5eSVineet Gupta dev_err(&pdev->dev, "clock-frequency property NOTset\n");
5968dbe1d5eSVineet Gupta return -EINVAL;
5978dbe1d5eSVineet Gupta }
5988dbe1d5eSVineet Gupta port->uartclk = val;
5998dbe1d5eSVineet Gupta
6008dbe1d5eSVineet Gupta if (of_property_read_u32(np, "current-speed", &val)) {
6018dbe1d5eSVineet Gupta dev_err(&pdev->dev, "current-speed property NOT set\n");
6028dbe1d5eSVineet Gupta return -EINVAL;
6038dbe1d5eSVineet Gupta }
6048dbe1d5eSVineet Gupta uart->baud = val;
6058dbe1d5eSVineet Gupta
6068ab5fc55SKe Zhang port->membase = devm_platform_ioremap_resource(pdev, 0);
6078ab5fc55SKe Zhang if (IS_ERR(port->membase)) {
6088dbe1d5eSVineet Gupta /* No point of dev_err since UART itself is hosed here */
6098ab5fc55SKe Zhang return PTR_ERR(port->membase);
6108ab5fc55SKe Zhang }
6118dbe1d5eSVineet Gupta
6128dbe1d5eSVineet Gupta port->irq = irq_of_parse_and_map(np, 0);
6138dbe1d5eSVineet Gupta
6148dbe1d5eSVineet Gupta port->dev = &pdev->dev;
6158dbe1d5eSVineet Gupta port->iotype = UPIO_MEM;
6168dbe1d5eSVineet Gupta port->flags = UPF_BOOT_AUTOCONF;
6178dbe1d5eSVineet Gupta port->line = dev_id;
6188dbe1d5eSVineet Gupta port->ops = &arc_serial_pops;
619933505e9SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_ARC_CONSOLE);
6208dbe1d5eSVineet Gupta
6218dbe1d5eSVineet Gupta port->fifosize = ARC_UART_TX_FIFO_SIZE;
6228dbe1d5eSVineet Gupta
6238dbe1d5eSVineet Gupta /*
6248dbe1d5eSVineet Gupta * uart_insert_char( ) uses it in decideding whether to ignore a
6258dbe1d5eSVineet Gupta * char or not. Explicitly setting it here, removes the subtelty
6268dbe1d5eSVineet Gupta */
6278dbe1d5eSVineet Gupta port->ignore_status_mask = 0;
6288dbe1d5eSVineet Gupta
6298dbe1d5eSVineet Gupta return uart_add_one_port(&arc_uart_driver, &arc_uart_ports[dev_id].port);
6302ac4ad2aSVineet Gupta }
6312ac4ad2aSVineet Gupta
632ea28fd56SVineet Gupta static const struct of_device_id arc_uart_dt_ids[] = {
633ea28fd56SVineet Gupta { .compatible = "snps,arc-uart" },
634ea28fd56SVineet Gupta { /* Sentinel */ }
635ea28fd56SVineet Gupta };
636ea28fd56SVineet Gupta MODULE_DEVICE_TABLE(of, arc_uart_dt_ids);
637ea28fd56SVineet Gupta
6382ac4ad2aSVineet Gupta static struct platform_driver arc_platform_driver = {
6392ac4ad2aSVineet Gupta .probe = arc_serial_probe,
6402ac4ad2aSVineet Gupta .driver = {
6412ac4ad2aSVineet Gupta .name = DRIVER_NAME,
642ea28fd56SVineet Gupta .of_match_table = arc_uart_dt_ids,
6432ac4ad2aSVineet Gupta },
6442ac4ad2aSVineet Gupta };
6452ac4ad2aSVineet Gupta
arc_serial_init(void)6462ac4ad2aSVineet Gupta static int __init arc_serial_init(void)
6472ac4ad2aSVineet Gupta {
6482ac4ad2aSVineet Gupta int ret;
6492ac4ad2aSVineet Gupta
6502ac4ad2aSVineet Gupta ret = uart_register_driver(&arc_uart_driver);
6512ac4ad2aSVineet Gupta if (ret)
6522ac4ad2aSVineet Gupta return ret;
6532ac4ad2aSVineet Gupta
6542ac4ad2aSVineet Gupta ret = platform_driver_register(&arc_platform_driver);
6552ac4ad2aSVineet Gupta if (ret)
6562ac4ad2aSVineet Gupta uart_unregister_driver(&arc_uart_driver);
6572ac4ad2aSVineet Gupta
6582ac4ad2aSVineet Gupta return ret;
6592ac4ad2aSVineet Gupta }
6602ac4ad2aSVineet Gupta
arc_serial_exit(void)6612ac4ad2aSVineet Gupta static void __exit arc_serial_exit(void)
6622ac4ad2aSVineet Gupta {
6632ac4ad2aSVineet Gupta platform_driver_unregister(&arc_platform_driver);
6642ac4ad2aSVineet Gupta uart_unregister_driver(&arc_uart_driver);
6652ac4ad2aSVineet Gupta }
6662ac4ad2aSVineet Gupta
6672ac4ad2aSVineet Gupta module_init(arc_serial_init);
6682ac4ad2aSVineet Gupta module_exit(arc_serial_exit);
6692ac4ad2aSVineet Gupta
6702ac4ad2aSVineet Gupta MODULE_LICENSE("GPL");
671d5a12ea7SAxel Lin MODULE_ALIAS("platform:" DRIVER_NAME);
6722ac4ad2aSVineet Gupta MODULE_AUTHOR("Vineet Gupta");
6732ac4ad2aSVineet Gupta MODULE_DESCRIPTION("ARC(Synopsys) On-Chip(fpga) serial driver");
674