12ac4ad2aSVineet Gupta /* 22ac4ad2aSVineet Gupta * ARC On-Chip(fpga) UART Driver 32ac4ad2aSVineet Gupta * 42ac4ad2aSVineet Gupta * Copyright (C) 2010-2012 Synopsys, Inc. (www.synopsys.com) 52ac4ad2aSVineet Gupta * 62ac4ad2aSVineet Gupta * This program is free software; you can redistribute it and/or modify 72ac4ad2aSVineet Gupta * it under the terms of the GNU General Public License version 2 as 82ac4ad2aSVineet Gupta * published by the Free Software Foundation. 92ac4ad2aSVineet Gupta * 102ac4ad2aSVineet Gupta * vineetg: July 10th 2012 112ac4ad2aSVineet Gupta * -Decoupled the driver from arch/arc 122ac4ad2aSVineet Gupta * +Using platform_get_resource() for irq/membase (thx to bfin_uart.c) 132ac4ad2aSVineet Gupta * +Using early_platform_xxx() for early console (thx to mach-shmobile/xxx) 142ac4ad2aSVineet Gupta * 152ac4ad2aSVineet Gupta * Vineetg: Aug 21st 2010 162ac4ad2aSVineet Gupta * -Is uart_tx_stopped() not done in tty write path as it has already been 172ac4ad2aSVineet Gupta * taken care of, in serial core 182ac4ad2aSVineet Gupta * 192ac4ad2aSVineet Gupta * Vineetg: Aug 18th 2010 202ac4ad2aSVineet Gupta * -New Serial Core based ARC UART driver 212ac4ad2aSVineet Gupta * -Derived largely from blackfin driver albiet with some major tweaks 222ac4ad2aSVineet Gupta * 232ac4ad2aSVineet Gupta * TODO: 242ac4ad2aSVineet Gupta * -check if sysreq works 252ac4ad2aSVineet Gupta */ 262ac4ad2aSVineet Gupta 272ac4ad2aSVineet Gupta #if defined(CONFIG_SERIAL_ARC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 282ac4ad2aSVineet Gupta #define SUPPORT_SYSRQ 292ac4ad2aSVineet Gupta #endif 302ac4ad2aSVineet Gupta 312ac4ad2aSVineet Gupta #include <linux/module.h> 322ac4ad2aSVineet Gupta #include <linux/serial.h> 332ac4ad2aSVineet Gupta #include <linux/console.h> 342ac4ad2aSVineet Gupta #include <linux/sysrq.h> 352ac4ad2aSVineet Gupta #include <linux/platform_device.h> 362ac4ad2aSVineet Gupta #include <linux/tty.h> 372ac4ad2aSVineet Gupta #include <linux/tty_flip.h> 382ac4ad2aSVineet Gupta #include <linux/serial_core.h> 392ac4ad2aSVineet Gupta #include <linux/io.h> 4091f1b62aSVineet Gupta #include <linux/of_irq.h> 4191f1b62aSVineet Gupta #include <linux/of_address.h> 422ac4ad2aSVineet Gupta 432ac4ad2aSVineet Gupta /************************************* 442ac4ad2aSVineet Gupta * ARC UART Hardware Specs 452ac4ad2aSVineet Gupta ************************************/ 462ac4ad2aSVineet Gupta #define ARC_UART_TX_FIFO_SIZE 1 472ac4ad2aSVineet Gupta 482ac4ad2aSVineet Gupta /* 492ac4ad2aSVineet Gupta * UART Register set (this is not a Standards Compliant IP) 502ac4ad2aSVineet Gupta * Also each reg is Word aligned, but only 8 bits wide 512ac4ad2aSVineet Gupta */ 522ac4ad2aSVineet Gupta #define R_ID0 0 532ac4ad2aSVineet Gupta #define R_ID1 4 542ac4ad2aSVineet Gupta #define R_ID2 8 552ac4ad2aSVineet Gupta #define R_ID3 12 562ac4ad2aSVineet Gupta #define R_DATA 16 572ac4ad2aSVineet Gupta #define R_STS 20 582ac4ad2aSVineet Gupta #define R_BAUDL 24 592ac4ad2aSVineet Gupta #define R_BAUDH 28 602ac4ad2aSVineet Gupta 612ac4ad2aSVineet Gupta /* Bits for UART Status Reg (R/W) */ 622ac4ad2aSVineet Gupta #define RXIENB 0x04 /* Receive Interrupt Enable */ 632ac4ad2aSVineet Gupta #define TXIENB 0x40 /* Transmit Interrupt Enable */ 642ac4ad2aSVineet Gupta 652ac4ad2aSVineet Gupta #define RXEMPTY 0x20 /* Receive FIFO Empty: No char receivede */ 662ac4ad2aSVineet Gupta #define TXEMPTY 0x80 /* Transmit FIFO Empty, thus char can be written into */ 672ac4ad2aSVineet Gupta 682ac4ad2aSVineet Gupta #define RXFULL 0x08 /* Receive FIFO full */ 692ac4ad2aSVineet Gupta #define RXFULL1 0x10 /* Receive FIFO has space for 1 char (tot space=4) */ 702ac4ad2aSVineet Gupta 712ac4ad2aSVineet Gupta #define RXFERR 0x01 /* Frame Error: Stop Bit not detected */ 722ac4ad2aSVineet Gupta #define RXOERR 0x02 /* OverFlow Err: Char recv but RXFULL still set */ 732ac4ad2aSVineet Gupta 742ac4ad2aSVineet Gupta /* Uart bit fiddling helpers: lowest level */ 7512d15e6fSVineet Gupta #define RBASE(port, reg) (port->membase + reg) 762ac4ad2aSVineet Gupta #define UART_REG_SET(u, r, v) writeb((v), RBASE(u, r)) 772ac4ad2aSVineet Gupta #define UART_REG_GET(u, r) readb(RBASE(u, r)) 782ac4ad2aSVineet Gupta 792ac4ad2aSVineet Gupta #define UART_REG_OR(u, r, v) UART_REG_SET(u, r, UART_REG_GET(u, r) | (v)) 802ac4ad2aSVineet Gupta #define UART_REG_CLR(u, r, v) UART_REG_SET(u, r, UART_REG_GET(u, r) & ~(v)) 812ac4ad2aSVineet Gupta 822ac4ad2aSVineet Gupta /* Uart bit fiddling helpers: API level */ 832ac4ad2aSVineet Gupta #define UART_SET_DATA(uart, val) UART_REG_SET(uart, R_DATA, val) 842ac4ad2aSVineet Gupta #define UART_GET_DATA(uart) UART_REG_GET(uart, R_DATA) 852ac4ad2aSVineet Gupta 862ac4ad2aSVineet Gupta #define UART_SET_BAUDH(uart, val) UART_REG_SET(uart, R_BAUDH, val) 872ac4ad2aSVineet Gupta #define UART_SET_BAUDL(uart, val) UART_REG_SET(uart, R_BAUDL, val) 882ac4ad2aSVineet Gupta 892ac4ad2aSVineet Gupta #define UART_CLR_STATUS(uart, val) UART_REG_CLR(uart, R_STS, val) 902ac4ad2aSVineet Gupta #define UART_GET_STATUS(uart) UART_REG_GET(uart, R_STS) 912ac4ad2aSVineet Gupta 922ac4ad2aSVineet Gupta #define UART_ALL_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, RXIENB|TXIENB) 932ac4ad2aSVineet Gupta #define UART_RX_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, RXIENB) 942ac4ad2aSVineet Gupta #define UART_TX_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, TXIENB) 952ac4ad2aSVineet Gupta 962ac4ad2aSVineet Gupta #define UART_ALL_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, RXIENB|TXIENB) 972ac4ad2aSVineet Gupta #define UART_RX_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, RXIENB) 982ac4ad2aSVineet Gupta #define UART_TX_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, TXIENB) 992ac4ad2aSVineet Gupta 1002ac4ad2aSVineet Gupta #define ARC_SERIAL_DEV_NAME "ttyARC" 1012ac4ad2aSVineet Gupta 1022ac4ad2aSVineet Gupta struct arc_uart_port { 1032ac4ad2aSVineet Gupta struct uart_port port; 1042ac4ad2aSVineet Gupta unsigned long baud; 1052ac4ad2aSVineet Gupta }; 1062ac4ad2aSVineet Gupta 1072ac4ad2aSVineet Gupta #define to_arc_port(uport) container_of(uport, struct arc_uart_port, port) 1082ac4ad2aSVineet Gupta 1092ac4ad2aSVineet Gupta static struct arc_uart_port arc_uart_ports[CONFIG_SERIAL_ARC_NR_PORTS]; 1102ac4ad2aSVineet Gupta 1112ac4ad2aSVineet Gupta #ifdef CONFIG_SERIAL_ARC_CONSOLE 1122ac4ad2aSVineet Gupta static struct console arc_console; 1132ac4ad2aSVineet Gupta #endif 1142ac4ad2aSVineet Gupta 1152ac4ad2aSVineet Gupta #define DRIVER_NAME "arc-uart" 1162ac4ad2aSVineet Gupta 1172ac4ad2aSVineet Gupta static struct uart_driver arc_uart_driver = { 1182ac4ad2aSVineet Gupta .owner = THIS_MODULE, 1192ac4ad2aSVineet Gupta .driver_name = DRIVER_NAME, 1202ac4ad2aSVineet Gupta .dev_name = ARC_SERIAL_DEV_NAME, 1212ac4ad2aSVineet Gupta .major = 0, 1222ac4ad2aSVineet Gupta .minor = 0, 1232ac4ad2aSVineet Gupta .nr = CONFIG_SERIAL_ARC_NR_PORTS, 1242ac4ad2aSVineet Gupta #ifdef CONFIG_SERIAL_ARC_CONSOLE 1252ac4ad2aSVineet Gupta .cons = &arc_console, 1262ac4ad2aSVineet Gupta #endif 1272ac4ad2aSVineet Gupta }; 1282ac4ad2aSVineet Gupta 1292ac4ad2aSVineet Gupta static void arc_serial_stop_rx(struct uart_port *port) 1302ac4ad2aSVineet Gupta { 13112d15e6fSVineet Gupta UART_RX_IRQ_DISABLE(port); 1322ac4ad2aSVineet Gupta } 1332ac4ad2aSVineet Gupta 1342ac4ad2aSVineet Gupta static void arc_serial_stop_tx(struct uart_port *port) 1352ac4ad2aSVineet Gupta { 13612d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY)) 1372ac4ad2aSVineet Gupta cpu_relax(); 1382ac4ad2aSVineet Gupta 13912d15e6fSVineet Gupta UART_TX_IRQ_DISABLE(port); 1402ac4ad2aSVineet Gupta } 1412ac4ad2aSVineet Gupta 1422ac4ad2aSVineet Gupta /* 1432ac4ad2aSVineet Gupta * Return TIOCSER_TEMT when transmitter is not busy. 1442ac4ad2aSVineet Gupta */ 1452ac4ad2aSVineet Gupta static unsigned int arc_serial_tx_empty(struct uart_port *port) 1462ac4ad2aSVineet Gupta { 1472ac4ad2aSVineet Gupta unsigned int stat; 1482ac4ad2aSVineet Gupta 14912d15e6fSVineet Gupta stat = UART_GET_STATUS(port); 1502ac4ad2aSVineet Gupta if (stat & TXEMPTY) 1512ac4ad2aSVineet Gupta return TIOCSER_TEMT; 1522ac4ad2aSVineet Gupta 1532ac4ad2aSVineet Gupta return 0; 1542ac4ad2aSVineet Gupta } 1552ac4ad2aSVineet Gupta 1562ac4ad2aSVineet Gupta /* 1572ac4ad2aSVineet Gupta * Driver internal routine, used by both tty(serial core) as well as tx-isr 1582ac4ad2aSVineet Gupta * -Called under spinlock in either cases 159ee797069SJiri Slaby * -also tty->stopped has already been checked 1602ac4ad2aSVineet Gupta * = by uart_start( ) before calling us 1612ac4ad2aSVineet Gupta * = tx_ist checks that too before calling 1622ac4ad2aSVineet Gupta */ 16312d15e6fSVineet Gupta static void arc_serial_tx_chars(struct uart_port *port) 1642ac4ad2aSVineet Gupta { 16512d15e6fSVineet Gupta struct circ_buf *xmit = &port->state->xmit; 1662ac4ad2aSVineet Gupta int sent = 0; 1672ac4ad2aSVineet Gupta unsigned char ch; 1682ac4ad2aSVineet Gupta 16912d15e6fSVineet Gupta if (unlikely(port->x_char)) { 17012d15e6fSVineet Gupta UART_SET_DATA(port, port->x_char); 17112d15e6fSVineet Gupta port->icount.tx++; 17212d15e6fSVineet Gupta port->x_char = 0; 1732ac4ad2aSVineet Gupta sent = 1; 17499ecb001SPeter Hurley } else if (!uart_circ_empty(xmit)) { 1752ac4ad2aSVineet Gupta ch = xmit->buf[xmit->tail]; 1762ac4ad2aSVineet Gupta xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 17712d15e6fSVineet Gupta port->icount.tx++; 17812d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY)) 1792ac4ad2aSVineet Gupta cpu_relax(); 18012d15e6fSVineet Gupta UART_SET_DATA(port, ch); 1812ac4ad2aSVineet Gupta sent = 1; 1822ac4ad2aSVineet Gupta } 1832ac4ad2aSVineet Gupta 1842ac4ad2aSVineet Gupta /* 1852ac4ad2aSVineet Gupta * If num chars in xmit buffer are too few, ask tty layer for more. 1862ac4ad2aSVineet Gupta * By Hard ISR to schedule processing in software interrupt part 1872ac4ad2aSVineet Gupta */ 1882ac4ad2aSVineet Gupta if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 18912d15e6fSVineet Gupta uart_write_wakeup(port); 1902ac4ad2aSVineet Gupta 1912ac4ad2aSVineet Gupta if (sent) 19212d15e6fSVineet Gupta UART_TX_IRQ_ENABLE(port); 1932ac4ad2aSVineet Gupta } 1942ac4ad2aSVineet Gupta 1952ac4ad2aSVineet Gupta /* 1962ac4ad2aSVineet Gupta * port is locked and interrupts are disabled 1972ac4ad2aSVineet Gupta * uart_start( ) calls us under the port spinlock irqsave 1982ac4ad2aSVineet Gupta */ 1992ac4ad2aSVineet Gupta static void arc_serial_start_tx(struct uart_port *port) 2002ac4ad2aSVineet Gupta { 20112d15e6fSVineet Gupta arc_serial_tx_chars(port); 2022ac4ad2aSVineet Gupta } 2032ac4ad2aSVineet Gupta 20412d15e6fSVineet Gupta static void arc_serial_rx_chars(struct uart_port *port, unsigned int status) 2052ac4ad2aSVineet Gupta { 2065284eba7SVineet Gupta unsigned int ch, flg = 0; 2072ac4ad2aSVineet Gupta 2082ac4ad2aSVineet Gupta /* 2092ac4ad2aSVineet Gupta * UART has 4 deep RX-FIFO. Driver's recongnition of this fact 2102ac4ad2aSVineet Gupta * is very subtle. Here's how ... 2112ac4ad2aSVineet Gupta * Upon getting a RX-Intr, such that RX-EMPTY=0, meaning data available, 2122ac4ad2aSVineet Gupta * driver reads the DATA Reg and keeps doing that in a loop, until 2132ac4ad2aSVineet Gupta * RX-EMPTY=1. Multiple chars being avail, with a single Interrupt, 2142ac4ad2aSVineet Gupta * before RX-EMPTY=0, implies some sort of buffering going on in the 2152ac4ad2aSVineet Gupta * controller, which is indeed the Rx-FIFO. 2162ac4ad2aSVineet Gupta */ 2175284eba7SVineet Gupta do { 2185284eba7SVineet Gupta /* 2195284eba7SVineet Gupta * This could be an Rx Intr for err (no data), 2205284eba7SVineet Gupta * so check err and clear that Intr first 2215284eba7SVineet Gupta */ 2222ac4ad2aSVineet Gupta if (unlikely(status & (RXOERR | RXFERR))) { 2232ac4ad2aSVineet Gupta if (status & RXOERR) { 22412d15e6fSVineet Gupta port->icount.overrun++; 2252ac4ad2aSVineet Gupta flg = TTY_OVERRUN; 22612d15e6fSVineet Gupta UART_CLR_STATUS(port, RXOERR); 2272ac4ad2aSVineet Gupta } 2282ac4ad2aSVineet Gupta 2292ac4ad2aSVineet Gupta if (status & RXFERR) { 23012d15e6fSVineet Gupta port->icount.frame++; 2312ac4ad2aSVineet Gupta flg = TTY_FRAME; 23212d15e6fSVineet Gupta UART_CLR_STATUS(port, RXFERR); 2332ac4ad2aSVineet Gupta } 2342ac4ad2aSVineet Gupta } else 2352ac4ad2aSVineet Gupta flg = TTY_NORMAL; 2362ac4ad2aSVineet Gupta 2375284eba7SVineet Gupta if (status & RXEMPTY) 2385284eba7SVineet Gupta continue; 2395284eba7SVineet Gupta 24012d15e6fSVineet Gupta ch = UART_GET_DATA(port); 24112d15e6fSVineet Gupta port->icount.rx++; 2425284eba7SVineet Gupta 24312d15e6fSVineet Gupta if (!(uart_handle_sysrq_char(port, ch))) 24412d15e6fSVineet Gupta uart_insert_char(port, status, RXOERR, ch, flg); 2452ac4ad2aSVineet Gupta 24612d15e6fSVineet Gupta spin_unlock(&port->lock); 24712d15e6fSVineet Gupta tty_flip_buffer_push(&port->state->port); 24812d15e6fSVineet Gupta spin_lock(&port->lock); 24912d15e6fSVineet Gupta } while (!((status = UART_GET_STATUS(port)) & RXEMPTY)); 2502ac4ad2aSVineet Gupta } 2512ac4ad2aSVineet Gupta 2522ac4ad2aSVineet Gupta /* 2532ac4ad2aSVineet Gupta * A note on the Interrupt handling state machine of this driver 2542ac4ad2aSVineet Gupta * 2552ac4ad2aSVineet Gupta * kernel printk writes funnel thru the console driver framework and in order 2562ac4ad2aSVineet Gupta * to keep things simple as well as efficient, it writes to UART in polled 2572ac4ad2aSVineet Gupta * mode, in one shot, and exits. 2582ac4ad2aSVineet Gupta * 2592ac4ad2aSVineet Gupta * OTOH, Userland output (via tty layer), uses interrupt based writes as there 2602ac4ad2aSVineet Gupta * can be undeterministic delay between char writes. 2612ac4ad2aSVineet Gupta * 2622ac4ad2aSVineet Gupta * Thus Rx-interrupts are always enabled, while tx-interrupts are by default 2632ac4ad2aSVineet Gupta * disabled. 2642ac4ad2aSVineet Gupta * 2652ac4ad2aSVineet Gupta * When tty has some data to send out, serial core calls driver's start_tx 2662ac4ad2aSVineet Gupta * which 2672ac4ad2aSVineet Gupta * -checks-if-tty-buffer-has-char-to-send 2682ac4ad2aSVineet Gupta * -writes-data-to-uart 2692ac4ad2aSVineet Gupta * -enable-tx-intr 2702ac4ad2aSVineet Gupta * 2712ac4ad2aSVineet Gupta * Once data bits are pushed out, controller raises the Tx-room-avail-Interrupt. 2722ac4ad2aSVineet Gupta * The first thing Tx ISR does is disable further Tx interrupts (as this could 2732ac4ad2aSVineet Gupta * be the last char to send, before settling down into the quiet polled mode). 2742ac4ad2aSVineet Gupta * It then calls the exact routine used by tty layer write to send out any 2752ac4ad2aSVineet Gupta * more char in tty buffer. In case of sending, it re-enables Tx-intr. In case 2762ac4ad2aSVineet Gupta * of no data, it remains disabled. 2772ac4ad2aSVineet Gupta * This is how the transmit state machine is dynamically switched on/off 2782ac4ad2aSVineet Gupta */ 2792ac4ad2aSVineet Gupta 2802ac4ad2aSVineet Gupta static irqreturn_t arc_serial_isr(int irq, void *dev_id) 2812ac4ad2aSVineet Gupta { 28212d15e6fSVineet Gupta struct uart_port *port = dev_id; 2832ac4ad2aSVineet Gupta unsigned int status; 2842ac4ad2aSVineet Gupta 28512d15e6fSVineet Gupta status = UART_GET_STATUS(port); 2862ac4ad2aSVineet Gupta 2872ac4ad2aSVineet Gupta /* 2882ac4ad2aSVineet Gupta * Single IRQ for both Rx (data available) Tx (room available) Interrupt 2892ac4ad2aSVineet Gupta * notifications from the UART Controller. 2902ac4ad2aSVineet Gupta * To demultiplex between the two, we check the relevant bits 2912ac4ad2aSVineet Gupta */ 2925284eba7SVineet Gupta if (status & RXIENB) { 2932ac4ad2aSVineet Gupta 2942ac4ad2aSVineet Gupta /* already in ISR, no need of xx_irqsave */ 29512d15e6fSVineet Gupta spin_lock(&port->lock); 29612d15e6fSVineet Gupta arc_serial_rx_chars(port, status); 29712d15e6fSVineet Gupta spin_unlock(&port->lock); 2982ac4ad2aSVineet Gupta } 2992ac4ad2aSVineet Gupta 3002ac4ad2aSVineet Gupta if ((status & TXIENB) && (status & TXEMPTY)) { 3012ac4ad2aSVineet Gupta 3022ac4ad2aSVineet Gupta /* Unconditionally disable further Tx-Interrupts. 3032ac4ad2aSVineet Gupta * will be enabled by tx_chars() if needed. 3042ac4ad2aSVineet Gupta */ 30512d15e6fSVineet Gupta UART_TX_IRQ_DISABLE(port); 3062ac4ad2aSVineet Gupta 30712d15e6fSVineet Gupta spin_lock(&port->lock); 3082ac4ad2aSVineet Gupta 30912d15e6fSVineet Gupta if (!uart_tx_stopped(port)) 31012d15e6fSVineet Gupta arc_serial_tx_chars(port); 3112ac4ad2aSVineet Gupta 31212d15e6fSVineet Gupta spin_unlock(&port->lock); 3132ac4ad2aSVineet Gupta } 3142ac4ad2aSVineet Gupta 3152ac4ad2aSVineet Gupta return IRQ_HANDLED; 3162ac4ad2aSVineet Gupta } 3172ac4ad2aSVineet Gupta 3182ac4ad2aSVineet Gupta static unsigned int arc_serial_get_mctrl(struct uart_port *port) 3192ac4ad2aSVineet Gupta { 3202ac4ad2aSVineet Gupta /* 3212ac4ad2aSVineet Gupta * Pretend we have a Modem status reg and following bits are 3222ac4ad2aSVineet Gupta * always set, to satify the serial core state machine 3232ac4ad2aSVineet Gupta * (DSR) Data Set Ready 3242ac4ad2aSVineet Gupta * (CTS) Clear To Send 3252ac4ad2aSVineet Gupta * (CAR) Carrier Detect 3262ac4ad2aSVineet Gupta */ 3272ac4ad2aSVineet Gupta return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR; 3282ac4ad2aSVineet Gupta } 3292ac4ad2aSVineet Gupta 3302ac4ad2aSVineet Gupta static void arc_serial_set_mctrl(struct uart_port *port, unsigned int mctrl) 3312ac4ad2aSVineet Gupta { 3322ac4ad2aSVineet Gupta /* MCR not present */ 3332ac4ad2aSVineet Gupta } 3342ac4ad2aSVineet Gupta 3352ac4ad2aSVineet Gupta static void arc_serial_break_ctl(struct uart_port *port, int break_state) 3362ac4ad2aSVineet Gupta { 3372ac4ad2aSVineet Gupta /* ARC UART doesn't support sending Break signal */ 3382ac4ad2aSVineet Gupta } 3392ac4ad2aSVineet Gupta 3402ac4ad2aSVineet Gupta static int arc_serial_startup(struct uart_port *port) 3412ac4ad2aSVineet Gupta { 3422ac4ad2aSVineet Gupta /* Before we hook up the ISR, Disable all UART Interrupts */ 34312d15e6fSVineet Gupta UART_ALL_IRQ_DISABLE(port); 3442ac4ad2aSVineet Gupta 34512d15e6fSVineet Gupta if (request_irq(port->irq, arc_serial_isr, 0, "arc uart rx-tx", port)) { 34612d15e6fSVineet Gupta dev_warn(port->dev, "Unable to attach ARC UART intr\n"); 3472ac4ad2aSVineet Gupta return -EBUSY; 3482ac4ad2aSVineet Gupta } 3492ac4ad2aSVineet Gupta 35012d15e6fSVineet Gupta UART_RX_IRQ_ENABLE(port); /* Only Rx IRQ enabled to begin with */ 3512ac4ad2aSVineet Gupta 3522ac4ad2aSVineet Gupta return 0; 3532ac4ad2aSVineet Gupta } 3542ac4ad2aSVineet Gupta 3552ac4ad2aSVineet Gupta /* This is not really needed */ 3562ac4ad2aSVineet Gupta static void arc_serial_shutdown(struct uart_port *port) 3572ac4ad2aSVineet Gupta { 35812d15e6fSVineet Gupta free_irq(port->irq, port); 3592ac4ad2aSVineet Gupta } 3602ac4ad2aSVineet Gupta 3612ac4ad2aSVineet Gupta static void 3622ac4ad2aSVineet Gupta arc_serial_set_termios(struct uart_port *port, struct ktermios *new, 3632ac4ad2aSVineet Gupta struct ktermios *old) 3642ac4ad2aSVineet Gupta { 3652ac4ad2aSVineet Gupta struct arc_uart_port *uart = to_arc_port(port); 3662ac4ad2aSVineet Gupta unsigned int baud, uartl, uarth, hw_val; 3672ac4ad2aSVineet Gupta unsigned long flags; 3682ac4ad2aSVineet Gupta 3692ac4ad2aSVineet Gupta /* 3702ac4ad2aSVineet Gupta * Use the generic handler so that any specially encoded baud rates 3712ac4ad2aSVineet Gupta * such as SPD_xx flags or "%B0" can be handled 3722ac4ad2aSVineet Gupta * Max Baud I suppose will not be more than current 115K * 4 3732ac4ad2aSVineet Gupta * Formula for ARC UART is: hw-val = ((CLK/(BAUD*4)) -1) 3742ac4ad2aSVineet Gupta * spread over two 8-bit registers 3752ac4ad2aSVineet Gupta */ 3762ac4ad2aSVineet Gupta baud = uart_get_baud_rate(port, new, old, 0, 460800); 3772ac4ad2aSVineet Gupta 3782ac4ad2aSVineet Gupta hw_val = port->uartclk / (uart->baud * 4) - 1; 3792ac4ad2aSVineet Gupta uartl = hw_val & 0xFF; 3802ac4ad2aSVineet Gupta uarth = (hw_val >> 8) & 0xFF; 3812ac4ad2aSVineet Gupta 3822ac4ad2aSVineet Gupta spin_lock_irqsave(&port->lock, flags); 3832ac4ad2aSVineet Gupta 38412d15e6fSVineet Gupta UART_ALL_IRQ_DISABLE(port); 3852ac4ad2aSVineet Gupta 38612d15e6fSVineet Gupta UART_SET_BAUDL(port, uartl); 38712d15e6fSVineet Gupta UART_SET_BAUDH(port, uarth); 3882ac4ad2aSVineet Gupta 38912d15e6fSVineet Gupta UART_RX_IRQ_ENABLE(port); 3902ac4ad2aSVineet Gupta 3912ac4ad2aSVineet Gupta /* 3922ac4ad2aSVineet Gupta * UART doesn't support Parity/Hardware Flow Control; 3932ac4ad2aSVineet Gupta * Only supports 8N1 character size 3942ac4ad2aSVineet Gupta */ 3952ac4ad2aSVineet Gupta new->c_cflag &= ~(CMSPAR|CRTSCTS|CSIZE); 3962ac4ad2aSVineet Gupta new->c_cflag |= CS8; 3972ac4ad2aSVineet Gupta 3982ac4ad2aSVineet Gupta if (old) 3992ac4ad2aSVineet Gupta tty_termios_copy_hw(new, old); 4002ac4ad2aSVineet Gupta 4012ac4ad2aSVineet Gupta /* Don't rewrite B0 */ 4022ac4ad2aSVineet Gupta if (tty_termios_baud_rate(new)) 4032ac4ad2aSVineet Gupta tty_termios_encode_baud_rate(new, baud, baud); 4042ac4ad2aSVineet Gupta 4052ac4ad2aSVineet Gupta uart_update_timeout(port, new->c_cflag, baud); 4062ac4ad2aSVineet Gupta 4072ac4ad2aSVineet Gupta spin_unlock_irqrestore(&port->lock, flags); 4082ac4ad2aSVineet Gupta } 4092ac4ad2aSVineet Gupta 4102ac4ad2aSVineet Gupta static const char *arc_serial_type(struct uart_port *port) 4112ac4ad2aSVineet Gupta { 41212d15e6fSVineet Gupta return port->type == PORT_ARC ? DRIVER_NAME : NULL; 4132ac4ad2aSVineet Gupta } 4142ac4ad2aSVineet Gupta 4152ac4ad2aSVineet Gupta static void arc_serial_release_port(struct uart_port *port) 4162ac4ad2aSVineet Gupta { 4172ac4ad2aSVineet Gupta } 4182ac4ad2aSVineet Gupta 4192ac4ad2aSVineet Gupta static int arc_serial_request_port(struct uart_port *port) 4202ac4ad2aSVineet Gupta { 4212ac4ad2aSVineet Gupta return 0; 4222ac4ad2aSVineet Gupta } 4232ac4ad2aSVineet Gupta 4242ac4ad2aSVineet Gupta /* 4252ac4ad2aSVineet Gupta * Verify the new serial_struct (for TIOCSSERIAL). 4262ac4ad2aSVineet Gupta */ 4272ac4ad2aSVineet Gupta static int 4282ac4ad2aSVineet Gupta arc_serial_verify_port(struct uart_port *port, struct serial_struct *ser) 4292ac4ad2aSVineet Gupta { 4302ac4ad2aSVineet Gupta if (port->type != PORT_UNKNOWN && ser->type != PORT_ARC) 4312ac4ad2aSVineet Gupta return -EINVAL; 4322ac4ad2aSVineet Gupta 4332ac4ad2aSVineet Gupta return 0; 4342ac4ad2aSVineet Gupta } 4352ac4ad2aSVineet Gupta 4362ac4ad2aSVineet Gupta /* 4372ac4ad2aSVineet Gupta * Configure/autoconfigure the port. 4382ac4ad2aSVineet Gupta */ 4392ac4ad2aSVineet Gupta static void arc_serial_config_port(struct uart_port *port, int flags) 4402ac4ad2aSVineet Gupta { 4412ac4ad2aSVineet Gupta if (flags & UART_CONFIG_TYPE) 44212d15e6fSVineet Gupta port->type = PORT_ARC; 4432ac4ad2aSVineet Gupta } 4442ac4ad2aSVineet Gupta 445f363ca2fSVineet Gupta #ifdef CONFIG_CONSOLE_POLL 4462ac4ad2aSVineet Gupta 447f363ca2fSVineet Gupta static void arc_serial_poll_putchar(struct uart_port *port, unsigned char chr) 4482ac4ad2aSVineet Gupta { 44912d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY)) 4502ac4ad2aSVineet Gupta cpu_relax(); 4512ac4ad2aSVineet Gupta 452f363ca2fSVineet Gupta UART_SET_DATA(port, chr); 4532ac4ad2aSVineet Gupta } 4542ac4ad2aSVineet Gupta 4552ac4ad2aSVineet Gupta static int arc_serial_poll_getchar(struct uart_port *port) 4562ac4ad2aSVineet Gupta { 4572ac4ad2aSVineet Gupta unsigned char chr; 4582ac4ad2aSVineet Gupta 45912d15e6fSVineet Gupta while (!(UART_GET_STATUS(port) & RXEMPTY)) 4602ac4ad2aSVineet Gupta cpu_relax(); 4612ac4ad2aSVineet Gupta 46212d15e6fSVineet Gupta chr = UART_GET_DATA(port); 4632ac4ad2aSVineet Gupta return chr; 4642ac4ad2aSVineet Gupta } 4652ac4ad2aSVineet Gupta #endif 4662ac4ad2aSVineet Gupta 4678232884eSJulia Lawall static const struct uart_ops arc_serial_pops = { 4682ac4ad2aSVineet Gupta .tx_empty = arc_serial_tx_empty, 4692ac4ad2aSVineet Gupta .set_mctrl = arc_serial_set_mctrl, 4702ac4ad2aSVineet Gupta .get_mctrl = arc_serial_get_mctrl, 4712ac4ad2aSVineet Gupta .stop_tx = arc_serial_stop_tx, 4722ac4ad2aSVineet Gupta .start_tx = arc_serial_start_tx, 4732ac4ad2aSVineet Gupta .stop_rx = arc_serial_stop_rx, 4742ac4ad2aSVineet Gupta .break_ctl = arc_serial_break_ctl, 4752ac4ad2aSVineet Gupta .startup = arc_serial_startup, 4762ac4ad2aSVineet Gupta .shutdown = arc_serial_shutdown, 4772ac4ad2aSVineet Gupta .set_termios = arc_serial_set_termios, 4782ac4ad2aSVineet Gupta .type = arc_serial_type, 4792ac4ad2aSVineet Gupta .release_port = arc_serial_release_port, 4802ac4ad2aSVineet Gupta .request_port = arc_serial_request_port, 4812ac4ad2aSVineet Gupta .config_port = arc_serial_config_port, 4822ac4ad2aSVineet Gupta .verify_port = arc_serial_verify_port, 4832ac4ad2aSVineet Gupta #ifdef CONFIG_CONSOLE_POLL 4842ac4ad2aSVineet Gupta .poll_put_char = arc_serial_poll_putchar, 4852ac4ad2aSVineet Gupta .poll_get_char = arc_serial_poll_getchar, 4862ac4ad2aSVineet Gupta #endif 4872ac4ad2aSVineet Gupta }; 4882ac4ad2aSVineet Gupta 4892ac4ad2aSVineet Gupta #ifdef CONFIG_SERIAL_ARC_CONSOLE 4902ac4ad2aSVineet Gupta 4919671f099SBill Pemberton static int arc_serial_console_setup(struct console *co, char *options) 4922ac4ad2aSVineet Gupta { 4932ac4ad2aSVineet Gupta struct uart_port *port; 4942ac4ad2aSVineet Gupta int baud = 115200; 4952ac4ad2aSVineet Gupta int bits = 8; 4962ac4ad2aSVineet Gupta int parity = 'n'; 4972ac4ad2aSVineet Gupta int flow = 'n'; 4982ac4ad2aSVineet Gupta 4992ac4ad2aSVineet Gupta if (co->index < 0 || co->index >= CONFIG_SERIAL_ARC_NR_PORTS) 5002ac4ad2aSVineet Gupta return -ENODEV; 5012ac4ad2aSVineet Gupta 5022ac4ad2aSVineet Gupta /* 5032ac4ad2aSVineet Gupta * The uart port backing the console (e.g. ttyARC1) might not have been 5042ac4ad2aSVineet Gupta * init yet. If so, defer the console setup to after the port. 5052ac4ad2aSVineet Gupta */ 5062ac4ad2aSVineet Gupta port = &arc_uart_ports[co->index].port; 5072ac4ad2aSVineet Gupta if (!port->membase) 5082ac4ad2aSVineet Gupta return -ENODEV; 5092ac4ad2aSVineet Gupta 5102ac4ad2aSVineet Gupta if (options) 5112ac4ad2aSVineet Gupta uart_parse_options(options, &baud, &parity, &bits, &flow); 5122ac4ad2aSVineet Gupta 5132ac4ad2aSVineet Gupta /* 5142ac4ad2aSVineet Gupta * Serial core will call port->ops->set_termios( ) 5152ac4ad2aSVineet Gupta * which will set the baud reg 5162ac4ad2aSVineet Gupta */ 5172ac4ad2aSVineet Gupta return uart_set_options(port, co, baud, parity, bits, flow); 5182ac4ad2aSVineet Gupta } 5192ac4ad2aSVineet Gupta 520f363ca2fSVineet Gupta static void arc_serial_console_putchar(struct uart_port *port, int ch) 521f363ca2fSVineet Gupta { 522f363ca2fSVineet Gupta while (!(UART_GET_STATUS(port) & TXEMPTY)) 523f363ca2fSVineet Gupta cpu_relax(); 524f363ca2fSVineet Gupta 525f363ca2fSVineet Gupta UART_SET_DATA(port, (unsigned char)ch); 526f363ca2fSVineet Gupta } 527f363ca2fSVineet Gupta 5282ac4ad2aSVineet Gupta /* 5292ac4ad2aSVineet Gupta * Interrupts are disabled on entering 5302ac4ad2aSVineet Gupta */ 5312ac4ad2aSVineet Gupta static void arc_serial_console_write(struct console *co, const char *s, 5322ac4ad2aSVineet Gupta unsigned int count) 5332ac4ad2aSVineet Gupta { 5342ac4ad2aSVineet Gupta struct uart_port *port = &arc_uart_ports[co->index].port; 5352ac4ad2aSVineet Gupta unsigned long flags; 5362ac4ad2aSVineet Gupta 5372ac4ad2aSVineet Gupta spin_lock_irqsave(&port->lock, flags); 538f363ca2fSVineet Gupta uart_console_write(port, s, count, arc_serial_console_putchar); 5392ac4ad2aSVineet Gupta spin_unlock_irqrestore(&port->lock, flags); 5402ac4ad2aSVineet Gupta } 5412ac4ad2aSVineet Gupta 5422ac4ad2aSVineet Gupta static struct console arc_console = { 5432ac4ad2aSVineet Gupta .name = ARC_SERIAL_DEV_NAME, 5442ac4ad2aSVineet Gupta .write = arc_serial_console_write, 5452ac4ad2aSVineet Gupta .device = uart_console_device, 5462ac4ad2aSVineet Gupta .setup = arc_serial_console_setup, 5472ac4ad2aSVineet Gupta .flags = CON_PRINTBUFFER, 5482ac4ad2aSVineet Gupta .index = -1, 5492ac4ad2aSVineet Gupta .data = &arc_uart_driver 5502ac4ad2aSVineet Gupta }; 5512ac4ad2aSVineet Gupta 552*ac1e6965SJeffy Chen static void arc_early_serial_write(struct console *con, const char *s, 55327cfe4ecSVineet Gupta unsigned int n) 55427cfe4ecSVineet Gupta { 55527cfe4ecSVineet Gupta struct earlycon_device *dev = con->data; 55627cfe4ecSVineet Gupta 557f363ca2fSVineet Gupta uart_console_write(&dev->port, s, n, arc_serial_console_putchar); 55827cfe4ecSVineet Gupta } 55927cfe4ecSVineet Gupta 56027cfe4ecSVineet Gupta static int __init arc_early_console_setup(struct earlycon_device *dev, 56127cfe4ecSVineet Gupta const char *opt) 56227cfe4ecSVineet Gupta { 56327cfe4ecSVineet Gupta struct uart_port *port = &dev->port; 56427cfe4ecSVineet Gupta unsigned int l, h, hw_val; 56527cfe4ecSVineet Gupta 56627cfe4ecSVineet Gupta if (!dev->port.membase) 56727cfe4ecSVineet Gupta return -ENODEV; 56827cfe4ecSVineet Gupta 56927cfe4ecSVineet Gupta hw_val = port->uartclk / (dev->baud * 4) - 1; 57027cfe4ecSVineet Gupta l = hw_val & 0xFF; 57127cfe4ecSVineet Gupta h = (hw_val >> 8) & 0xFF; 57227cfe4ecSVineet Gupta 57327cfe4ecSVineet Gupta UART_SET_BAUDL(port, l); 57427cfe4ecSVineet Gupta UART_SET_BAUDH(port, h); 57527cfe4ecSVineet Gupta 57627cfe4ecSVineet Gupta dev->con->write = arc_early_serial_write; 57727cfe4ecSVineet Gupta return 0; 57827cfe4ecSVineet Gupta } 5798c6abf7aSVineet Gupta OF_EARLYCON_DECLARE(arc_uart, "snps,arc-uart", arc_early_console_setup); 58027cfe4ecSVineet Gupta 5812ac4ad2aSVineet Gupta #endif /* CONFIG_SERIAL_ARC_CONSOLE */ 5822ac4ad2aSVineet Gupta 5839671f099SBill Pemberton static int arc_serial_probe(struct platform_device *pdev) 5842ac4ad2aSVineet Gupta { 585ea28fd56SVineet Gupta struct device_node *np = pdev->dev.of_node; 5868dbe1d5eSVineet Gupta struct arc_uart_port *uart; 5878dbe1d5eSVineet Gupta struct uart_port *port; 5888dbe1d5eSVineet Gupta int dev_id; 5898dbe1d5eSVineet Gupta u32 val; 5902ac4ad2aSVineet Gupta 591ea28fd56SVineet Gupta /* no device tree device */ 592ea28fd56SVineet Gupta if (!np) 593ea28fd56SVineet Gupta return -ENODEV; 594ea28fd56SVineet Gupta 595ea28fd56SVineet Gupta dev_id = of_alias_get_id(np, "serial"); 59611c62d4fSVineet Gupta if (dev_id < 0) 59711c62d4fSVineet Gupta dev_id = 0; 598ea28fd56SVineet Gupta 5998dbe1d5eSVineet Gupta uart = &arc_uart_ports[dev_id]; 6008dbe1d5eSVineet Gupta port = &uart->port; 6012ac4ad2aSVineet Gupta 6028dbe1d5eSVineet Gupta if (of_property_read_u32(np, "clock-frequency", &val)) { 6038dbe1d5eSVineet Gupta dev_err(&pdev->dev, "clock-frequency property NOTset\n"); 6048dbe1d5eSVineet Gupta return -EINVAL; 6058dbe1d5eSVineet Gupta } 6068dbe1d5eSVineet Gupta port->uartclk = val; 6078dbe1d5eSVineet Gupta 6088dbe1d5eSVineet Gupta if (of_property_read_u32(np, "current-speed", &val)) { 6098dbe1d5eSVineet Gupta dev_err(&pdev->dev, "current-speed property NOT set\n"); 6108dbe1d5eSVineet Gupta return -EINVAL; 6118dbe1d5eSVineet Gupta } 6128dbe1d5eSVineet Gupta uart->baud = val; 6138dbe1d5eSVineet Gupta 6148dbe1d5eSVineet Gupta port->membase = of_iomap(np, 0); 6158dbe1d5eSVineet Gupta if (!port->membase) 6168dbe1d5eSVineet Gupta /* No point of dev_err since UART itself is hosed here */ 6178dbe1d5eSVineet Gupta return -ENXIO; 6188dbe1d5eSVineet Gupta 6198dbe1d5eSVineet Gupta port->irq = irq_of_parse_and_map(np, 0); 6208dbe1d5eSVineet Gupta 6218dbe1d5eSVineet Gupta port->dev = &pdev->dev; 6228dbe1d5eSVineet Gupta port->iotype = UPIO_MEM; 6238dbe1d5eSVineet Gupta port->flags = UPF_BOOT_AUTOCONF; 6248dbe1d5eSVineet Gupta port->line = dev_id; 6258dbe1d5eSVineet Gupta port->ops = &arc_serial_pops; 6268dbe1d5eSVineet Gupta 6278dbe1d5eSVineet Gupta port->fifosize = ARC_UART_TX_FIFO_SIZE; 6288dbe1d5eSVineet Gupta 6298dbe1d5eSVineet Gupta /* 6308dbe1d5eSVineet Gupta * uart_insert_char( ) uses it in decideding whether to ignore a 6318dbe1d5eSVineet Gupta * char or not. Explicitly setting it here, removes the subtelty 6328dbe1d5eSVineet Gupta */ 6338dbe1d5eSVineet Gupta port->ignore_status_mask = 0; 6348dbe1d5eSVineet Gupta 6358dbe1d5eSVineet Gupta return uart_add_one_port(&arc_uart_driver, &arc_uart_ports[dev_id].port); 6362ac4ad2aSVineet Gupta } 6372ac4ad2aSVineet Gupta 638ae8d8a14SBill Pemberton static int arc_serial_remove(struct platform_device *pdev) 6392ac4ad2aSVineet Gupta { 6402ac4ad2aSVineet Gupta /* This will never be called */ 6412ac4ad2aSVineet Gupta return 0; 6422ac4ad2aSVineet Gupta } 6432ac4ad2aSVineet Gupta 644ea28fd56SVineet Gupta static const struct of_device_id arc_uart_dt_ids[] = { 645ea28fd56SVineet Gupta { .compatible = "snps,arc-uart" }, 646ea28fd56SVineet Gupta { /* Sentinel */ } 647ea28fd56SVineet Gupta }; 648ea28fd56SVineet Gupta MODULE_DEVICE_TABLE(of, arc_uart_dt_ids); 649ea28fd56SVineet Gupta 6502ac4ad2aSVineet Gupta static struct platform_driver arc_platform_driver = { 6512ac4ad2aSVineet Gupta .probe = arc_serial_probe, 6522d47b716SBill Pemberton .remove = arc_serial_remove, 6532ac4ad2aSVineet Gupta .driver = { 6542ac4ad2aSVineet Gupta .name = DRIVER_NAME, 655ea28fd56SVineet Gupta .of_match_table = arc_uart_dt_ids, 6562ac4ad2aSVineet Gupta }, 6572ac4ad2aSVineet Gupta }; 6582ac4ad2aSVineet Gupta 6592ac4ad2aSVineet Gupta static int __init arc_serial_init(void) 6602ac4ad2aSVineet Gupta { 6612ac4ad2aSVineet Gupta int ret; 6622ac4ad2aSVineet Gupta 6632ac4ad2aSVineet Gupta ret = uart_register_driver(&arc_uart_driver); 6642ac4ad2aSVineet Gupta if (ret) 6652ac4ad2aSVineet Gupta return ret; 6662ac4ad2aSVineet Gupta 6672ac4ad2aSVineet Gupta ret = platform_driver_register(&arc_platform_driver); 6682ac4ad2aSVineet Gupta if (ret) 6692ac4ad2aSVineet Gupta uart_unregister_driver(&arc_uart_driver); 6702ac4ad2aSVineet Gupta 6712ac4ad2aSVineet Gupta return ret; 6722ac4ad2aSVineet Gupta } 6732ac4ad2aSVineet Gupta 6742ac4ad2aSVineet Gupta static void __exit arc_serial_exit(void) 6752ac4ad2aSVineet Gupta { 6762ac4ad2aSVineet Gupta platform_driver_unregister(&arc_platform_driver); 6772ac4ad2aSVineet Gupta uart_unregister_driver(&arc_uart_driver); 6782ac4ad2aSVineet Gupta } 6792ac4ad2aSVineet Gupta 6802ac4ad2aSVineet Gupta module_init(arc_serial_init); 6812ac4ad2aSVineet Gupta module_exit(arc_serial_exit); 6822ac4ad2aSVineet Gupta 6832ac4ad2aSVineet Gupta MODULE_LICENSE("GPL"); 684d5a12ea7SAxel Lin MODULE_ALIAS("platform:" DRIVER_NAME); 6852ac4ad2aSVineet Gupta MODULE_AUTHOR("Vineet Gupta"); 6862ac4ad2aSVineet Gupta MODULE_DESCRIPTION("ARC(Synopsys) On-Chip(fpga) serial driver"); 687