1ab4382d2SGreg Kroah-Hartman /* 2ab4382d2SGreg Kroah-Hartman * This file is subject to the terms and conditions of the GNU General Public 3ab4382d2SGreg Kroah-Hartman * License. See the file "COPYING" in the main directory of this archive 4ab4382d2SGreg Kroah-Hartman * for more details. 5ab4382d2SGreg Kroah-Hartman * 6ab4382d2SGreg Kroah-Hartman * Derived from many drivers using generic_serial interface. 7ab4382d2SGreg Kroah-Hartman * 8ab4382d2SGreg Kroah-Hartman * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr> 9ab4382d2SGreg Kroah-Hartman * 10ab4382d2SGreg Kroah-Hartman * Serial driver for BCM63xx integrated UART. 11ab4382d2SGreg Kroah-Hartman * 12ab4382d2SGreg Kroah-Hartman * Hardware flow control was _not_ tested since I only have RX/TX on 13ab4382d2SGreg Kroah-Hartman * my board. 14ab4382d2SGreg Kroah-Hartman */ 15ab4382d2SGreg Kroah-Hartman 16ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_BCM63XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 17ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ 18ab4382d2SGreg Kroah-Hartman #endif 19ab4382d2SGreg Kroah-Hartman 20ab4382d2SGreg Kroah-Hartman #include <linux/kernel.h> 21ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h> 22ab4382d2SGreg Kroah-Hartman #include <linux/init.h> 23ab4382d2SGreg Kroah-Hartman #include <linux/delay.h> 24ab4382d2SGreg Kroah-Hartman #include <linux/module.h> 25ab4382d2SGreg Kroah-Hartman #include <linux/console.h> 26ab4382d2SGreg Kroah-Hartman #include <linux/clk.h> 27ab4382d2SGreg Kroah-Hartman #include <linux/tty.h> 28ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h> 29ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h> 30ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> 31ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 32d29e0d04SFlorian Fainelli #include <linux/serial_bcm63xx.h> 33f04430ceSFlorian Fainelli #include <linux/io.h> 349277285fSFlorian Fainelli #include <linux/of.h> 35ab4382d2SGreg Kroah-Hartman 36ab4382d2SGreg Kroah-Hartman #define BCM63XX_NR_UARTS 2 37ab4382d2SGreg Kroah-Hartman 38ab4382d2SGreg Kroah-Hartman static struct uart_port ports[BCM63XX_NR_UARTS]; 39ab4382d2SGreg Kroah-Hartman 40ab4382d2SGreg Kroah-Hartman /* 41ab4382d2SGreg Kroah-Hartman * rx interrupt mask / stat 42ab4382d2SGreg Kroah-Hartman * 43ab4382d2SGreg Kroah-Hartman * mask: 44ab4382d2SGreg Kroah-Hartman * - rx fifo full 45ab4382d2SGreg Kroah-Hartman * - rx fifo above threshold 46ab4382d2SGreg Kroah-Hartman * - rx fifo not empty for too long 47ab4382d2SGreg Kroah-Hartman */ 48ab4382d2SGreg Kroah-Hartman #define UART_RX_INT_MASK (UART_IR_MASK(UART_IR_RXOVER) | \ 49ab4382d2SGreg Kroah-Hartman UART_IR_MASK(UART_IR_RXTHRESH) | \ 50ab4382d2SGreg Kroah-Hartman UART_IR_MASK(UART_IR_RXTIMEOUT)) 51ab4382d2SGreg Kroah-Hartman 52ab4382d2SGreg Kroah-Hartman #define UART_RX_INT_STAT (UART_IR_STAT(UART_IR_RXOVER) | \ 53ab4382d2SGreg Kroah-Hartman UART_IR_STAT(UART_IR_RXTHRESH) | \ 54ab4382d2SGreg Kroah-Hartman UART_IR_STAT(UART_IR_RXTIMEOUT)) 55ab4382d2SGreg Kroah-Hartman 56ab4382d2SGreg Kroah-Hartman /* 57ab4382d2SGreg Kroah-Hartman * tx interrupt mask / stat 58ab4382d2SGreg Kroah-Hartman * 59ab4382d2SGreg Kroah-Hartman * mask: 60ab4382d2SGreg Kroah-Hartman * - tx fifo empty 61ab4382d2SGreg Kroah-Hartman * - tx fifo below threshold 62ab4382d2SGreg Kroah-Hartman */ 63ab4382d2SGreg Kroah-Hartman #define UART_TX_INT_MASK (UART_IR_MASK(UART_IR_TXEMPTY) | \ 64ab4382d2SGreg Kroah-Hartman UART_IR_MASK(UART_IR_TXTRESH)) 65ab4382d2SGreg Kroah-Hartman 66ab4382d2SGreg Kroah-Hartman #define UART_TX_INT_STAT (UART_IR_STAT(UART_IR_TXEMPTY) | \ 67ab4382d2SGreg Kroah-Hartman UART_IR_STAT(UART_IR_TXTRESH)) 68ab4382d2SGreg Kroah-Hartman 69ab4382d2SGreg Kroah-Hartman /* 70ab4382d2SGreg Kroah-Hartman * external input interrupt 71ab4382d2SGreg Kroah-Hartman * 72ab4382d2SGreg Kroah-Hartman * mask: any edge on CTS, DCD 73ab4382d2SGreg Kroah-Hartman */ 74ab4382d2SGreg Kroah-Hartman #define UART_EXTINP_INT_MASK (UART_EXTINP_IRMASK(UART_EXTINP_IR_CTS) | \ 75ab4382d2SGreg Kroah-Hartman UART_EXTINP_IRMASK(UART_EXTINP_IR_DCD)) 76ab4382d2SGreg Kroah-Hartman 77ab4382d2SGreg Kroah-Hartman /* 78ab4382d2SGreg Kroah-Hartman * handy uart register accessor 79ab4382d2SGreg Kroah-Hartman */ 80ab4382d2SGreg Kroah-Hartman static inline unsigned int bcm_uart_readl(struct uart_port *port, 81ab4382d2SGreg Kroah-Hartman unsigned int offset) 82ab4382d2SGreg Kroah-Hartman { 8399cf9790SFlorian Fainelli return __raw_readl(port->membase + offset); 84ab4382d2SGreg Kroah-Hartman } 85ab4382d2SGreg Kroah-Hartman 86ab4382d2SGreg Kroah-Hartman static inline void bcm_uart_writel(struct uart_port *port, 87ab4382d2SGreg Kroah-Hartman unsigned int value, unsigned int offset) 88ab4382d2SGreg Kroah-Hartman { 8999cf9790SFlorian Fainelli __raw_writel(value, port->membase + offset); 90ab4382d2SGreg Kroah-Hartman } 91ab4382d2SGreg Kroah-Hartman 92ab4382d2SGreg Kroah-Hartman /* 93ab4382d2SGreg Kroah-Hartman * serial core request to check if uart tx fifo is empty 94ab4382d2SGreg Kroah-Hartman */ 95ab4382d2SGreg Kroah-Hartman static unsigned int bcm_uart_tx_empty(struct uart_port *port) 96ab4382d2SGreg Kroah-Hartman { 97ab4382d2SGreg Kroah-Hartman unsigned int val; 98ab4382d2SGreg Kroah-Hartman 99ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG); 100ab4382d2SGreg Kroah-Hartman return (val & UART_IR_STAT(UART_IR_TXEMPTY)) ? 1 : 0; 101ab4382d2SGreg Kroah-Hartman } 102ab4382d2SGreg Kroah-Hartman 103ab4382d2SGreg Kroah-Hartman /* 104ab4382d2SGreg Kroah-Hartman * serial core request to set RTS and DTR pin state and loopback mode 105ab4382d2SGreg Kroah-Hartman */ 106ab4382d2SGreg Kroah-Hartman static void bcm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) 107ab4382d2SGreg Kroah-Hartman { 108ab4382d2SGreg Kroah-Hartman unsigned int val; 109ab4382d2SGreg Kroah-Hartman 110ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_MCTL_REG); 111ab4382d2SGreg Kroah-Hartman val &= ~(UART_MCTL_DTR_MASK | UART_MCTL_RTS_MASK); 112ab4382d2SGreg Kroah-Hartman /* invert of written value is reflected on the pin */ 113ab4382d2SGreg Kroah-Hartman if (!(mctrl & TIOCM_DTR)) 114ab4382d2SGreg Kroah-Hartman val |= UART_MCTL_DTR_MASK; 115ab4382d2SGreg Kroah-Hartman if (!(mctrl & TIOCM_RTS)) 116ab4382d2SGreg Kroah-Hartman val |= UART_MCTL_RTS_MASK; 117ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_MCTL_REG); 118ab4382d2SGreg Kroah-Hartman 119ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG); 120ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_LOOP) 121ab4382d2SGreg Kroah-Hartman val |= UART_CTL_LOOPBACK_MASK; 122ab4382d2SGreg Kroah-Hartman else 123ab4382d2SGreg Kroah-Hartman val &= ~UART_CTL_LOOPBACK_MASK; 124ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG); 125ab4382d2SGreg Kroah-Hartman } 126ab4382d2SGreg Kroah-Hartman 127ab4382d2SGreg Kroah-Hartman /* 128ab4382d2SGreg Kroah-Hartman * serial core request to return RI, CTS, DCD and DSR pin state 129ab4382d2SGreg Kroah-Hartman */ 130ab4382d2SGreg Kroah-Hartman static unsigned int bcm_uart_get_mctrl(struct uart_port *port) 131ab4382d2SGreg Kroah-Hartman { 132ab4382d2SGreg Kroah-Hartman unsigned int val, mctrl; 133ab4382d2SGreg Kroah-Hartman 134ab4382d2SGreg Kroah-Hartman mctrl = 0; 135ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_EXTINP_REG); 136ab4382d2SGreg Kroah-Hartman if (val & UART_EXTINP_RI_MASK) 137ab4382d2SGreg Kroah-Hartman mctrl |= TIOCM_RI; 138ab4382d2SGreg Kroah-Hartman if (val & UART_EXTINP_CTS_MASK) 139ab4382d2SGreg Kroah-Hartman mctrl |= TIOCM_CTS; 140ab4382d2SGreg Kroah-Hartman if (val & UART_EXTINP_DCD_MASK) 141ab4382d2SGreg Kroah-Hartman mctrl |= TIOCM_CD; 142ab4382d2SGreg Kroah-Hartman if (val & UART_EXTINP_DSR_MASK) 143ab4382d2SGreg Kroah-Hartman mctrl |= TIOCM_DSR; 144ab4382d2SGreg Kroah-Hartman return mctrl; 145ab4382d2SGreg Kroah-Hartman } 146ab4382d2SGreg Kroah-Hartman 147ab4382d2SGreg Kroah-Hartman /* 148ab4382d2SGreg Kroah-Hartman * serial core request to disable tx ASAP (used for flow control) 149ab4382d2SGreg Kroah-Hartman */ 150ab4382d2SGreg Kroah-Hartman static void bcm_uart_stop_tx(struct uart_port *port) 151ab4382d2SGreg Kroah-Hartman { 152ab4382d2SGreg Kroah-Hartman unsigned int val; 153ab4382d2SGreg Kroah-Hartman 154ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG); 155ab4382d2SGreg Kroah-Hartman val &= ~(UART_CTL_TXEN_MASK); 156ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG); 157ab4382d2SGreg Kroah-Hartman 158ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG); 159ab4382d2SGreg Kroah-Hartman val &= ~UART_TX_INT_MASK; 160ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_IR_REG); 161ab4382d2SGreg Kroah-Hartman } 162ab4382d2SGreg Kroah-Hartman 163ab4382d2SGreg Kroah-Hartman /* 164ab4382d2SGreg Kroah-Hartman * serial core request to (re)enable tx 165ab4382d2SGreg Kroah-Hartman */ 166ab4382d2SGreg Kroah-Hartman static void bcm_uart_start_tx(struct uart_port *port) 167ab4382d2SGreg Kroah-Hartman { 168ab4382d2SGreg Kroah-Hartman unsigned int val; 169ab4382d2SGreg Kroah-Hartman 170ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG); 171ab4382d2SGreg Kroah-Hartman val |= UART_TX_INT_MASK; 172ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_IR_REG); 173ab4382d2SGreg Kroah-Hartman 174ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG); 175ab4382d2SGreg Kroah-Hartman val |= UART_CTL_TXEN_MASK; 176ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG); 177ab4382d2SGreg Kroah-Hartman } 178ab4382d2SGreg Kroah-Hartman 179ab4382d2SGreg Kroah-Hartman /* 180ab4382d2SGreg Kroah-Hartman * serial core request to stop rx, called before port shutdown 181ab4382d2SGreg Kroah-Hartman */ 182ab4382d2SGreg Kroah-Hartman static void bcm_uart_stop_rx(struct uart_port *port) 183ab4382d2SGreg Kroah-Hartman { 184ab4382d2SGreg Kroah-Hartman unsigned int val; 185ab4382d2SGreg Kroah-Hartman 186ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG); 187ab4382d2SGreg Kroah-Hartman val &= ~UART_RX_INT_MASK; 188ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_IR_REG); 189ab4382d2SGreg Kroah-Hartman } 190ab4382d2SGreg Kroah-Hartman 191ab4382d2SGreg Kroah-Hartman /* 192ab4382d2SGreg Kroah-Hartman * serial core request to enable modem status interrupt reporting 193ab4382d2SGreg Kroah-Hartman */ 194ab4382d2SGreg Kroah-Hartman static void bcm_uart_enable_ms(struct uart_port *port) 195ab4382d2SGreg Kroah-Hartman { 196ab4382d2SGreg Kroah-Hartman unsigned int val; 197ab4382d2SGreg Kroah-Hartman 198ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG); 199ab4382d2SGreg Kroah-Hartman val |= UART_IR_MASK(UART_IR_EXTIP); 200ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_IR_REG); 201ab4382d2SGreg Kroah-Hartman } 202ab4382d2SGreg Kroah-Hartman 203ab4382d2SGreg Kroah-Hartman /* 204ab4382d2SGreg Kroah-Hartman * serial core request to start/stop emitting break char 205ab4382d2SGreg Kroah-Hartman */ 206ab4382d2SGreg Kroah-Hartman static void bcm_uart_break_ctl(struct uart_port *port, int ctl) 207ab4382d2SGreg Kroah-Hartman { 208ab4382d2SGreg Kroah-Hartman unsigned long flags; 209ab4382d2SGreg Kroah-Hartman unsigned int val; 210ab4382d2SGreg Kroah-Hartman 211ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 212ab4382d2SGreg Kroah-Hartman 213ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG); 214ab4382d2SGreg Kroah-Hartman if (ctl) 215ab4382d2SGreg Kroah-Hartman val |= UART_CTL_XMITBRK_MASK; 216ab4382d2SGreg Kroah-Hartman else 217ab4382d2SGreg Kroah-Hartman val &= ~UART_CTL_XMITBRK_MASK; 218ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG); 219ab4382d2SGreg Kroah-Hartman 220ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 221ab4382d2SGreg Kroah-Hartman } 222ab4382d2SGreg Kroah-Hartman 223ab4382d2SGreg Kroah-Hartman /* 224ab4382d2SGreg Kroah-Hartman * return port type in string format 225ab4382d2SGreg Kroah-Hartman */ 226ab4382d2SGreg Kroah-Hartman static const char *bcm_uart_type(struct uart_port *port) 227ab4382d2SGreg Kroah-Hartman { 228ab4382d2SGreg Kroah-Hartman return (port->type == PORT_BCM63XX) ? "bcm63xx_uart" : NULL; 229ab4382d2SGreg Kroah-Hartman } 230ab4382d2SGreg Kroah-Hartman 231ab4382d2SGreg Kroah-Hartman /* 232ab4382d2SGreg Kroah-Hartman * read all chars in rx fifo and send them to core 233ab4382d2SGreg Kroah-Hartman */ 234ab4382d2SGreg Kroah-Hartman static void bcm_uart_do_rx(struct uart_port *port) 235ab4382d2SGreg Kroah-Hartman { 236064256feSJonas Gorski struct tty_port *tty_port = &port->state->port; 237ab4382d2SGreg Kroah-Hartman unsigned int max_count; 238ab4382d2SGreg Kroah-Hartman 239ab4382d2SGreg Kroah-Hartman /* limit number of char read in interrupt, should not be 240ab4382d2SGreg Kroah-Hartman * higher than fifo size anyway since we're much faster than 241ab4382d2SGreg Kroah-Hartman * serial port */ 242ab4382d2SGreg Kroah-Hartman max_count = 32; 243ab4382d2SGreg Kroah-Hartman do { 244ab4382d2SGreg Kroah-Hartman unsigned int iestat, c, cstat; 245ab4382d2SGreg Kroah-Hartman char flag; 246ab4382d2SGreg Kroah-Hartman 247ab4382d2SGreg Kroah-Hartman /* get overrun/fifo empty information from ier 248ab4382d2SGreg Kroah-Hartman * register */ 249ab4382d2SGreg Kroah-Hartman iestat = bcm_uart_readl(port, UART_IR_REG); 2503bc46b31SMaxime Bizon 2513bc46b31SMaxime Bizon if (unlikely(iestat & UART_IR_STAT(UART_IR_RXOVER))) { 2523bc46b31SMaxime Bizon unsigned int val; 2533bc46b31SMaxime Bizon 2543bc46b31SMaxime Bizon /* fifo reset is required to clear 2553bc46b31SMaxime Bizon * interrupt */ 2563bc46b31SMaxime Bizon val = bcm_uart_readl(port, UART_CTL_REG); 2573bc46b31SMaxime Bizon val |= UART_CTL_RSTRXFIFO_MASK; 2583bc46b31SMaxime Bizon bcm_uart_writel(port, val, UART_CTL_REG); 2593bc46b31SMaxime Bizon 2603bc46b31SMaxime Bizon port->icount.overrun++; 261064256feSJonas Gorski tty_insert_flip_char(tty_port, 0, TTY_OVERRUN); 2623bc46b31SMaxime Bizon } 2633bc46b31SMaxime Bizon 264ab4382d2SGreg Kroah-Hartman if (!(iestat & UART_IR_STAT(UART_IR_RXNOTEMPTY))) 265ab4382d2SGreg Kroah-Hartman break; 266ab4382d2SGreg Kroah-Hartman 267ab4382d2SGreg Kroah-Hartman cstat = c = bcm_uart_readl(port, UART_FIFO_REG); 268ab4382d2SGreg Kroah-Hartman port->icount.rx++; 269ab4382d2SGreg Kroah-Hartman flag = TTY_NORMAL; 270ab4382d2SGreg Kroah-Hartman c &= 0xff; 271ab4382d2SGreg Kroah-Hartman 272ab4382d2SGreg Kroah-Hartman if (unlikely((cstat & UART_FIFO_ANYERR_MASK))) { 273ab4382d2SGreg Kroah-Hartman /* do stats first */ 274ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_BRKDET_MASK) { 275ab4382d2SGreg Kroah-Hartman port->icount.brk++; 276ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 277ab4382d2SGreg Kroah-Hartman continue; 278ab4382d2SGreg Kroah-Hartman } 279ab4382d2SGreg Kroah-Hartman 280ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_PARERR_MASK) 281ab4382d2SGreg Kroah-Hartman port->icount.parity++; 282ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_FRAMEERR_MASK) 283ab4382d2SGreg Kroah-Hartman port->icount.frame++; 284ab4382d2SGreg Kroah-Hartman 285ab4382d2SGreg Kroah-Hartman /* update flag wrt read_status_mask */ 286ab4382d2SGreg Kroah-Hartman cstat &= port->read_status_mask; 287ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_BRKDET_MASK) 288ab4382d2SGreg Kroah-Hartman flag = TTY_BREAK; 289ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_FRAMEERR_MASK) 290ab4382d2SGreg Kroah-Hartman flag = TTY_FRAME; 291ab4382d2SGreg Kroah-Hartman if (cstat & UART_FIFO_PARERR_MASK) 292ab4382d2SGreg Kroah-Hartman flag = TTY_PARITY; 293ab4382d2SGreg Kroah-Hartman } 294ab4382d2SGreg Kroah-Hartman 295ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c)) 296ab4382d2SGreg Kroah-Hartman continue; 297ab4382d2SGreg Kroah-Hartman 298ab4382d2SGreg Kroah-Hartman 299ab4382d2SGreg Kroah-Hartman if ((cstat & port->ignore_status_mask) == 0) 300064256feSJonas Gorski tty_insert_flip_char(tty_port, c, flag); 301ab4382d2SGreg Kroah-Hartman 302ab4382d2SGreg Kroah-Hartman } while (--max_count); 303ab4382d2SGreg Kroah-Hartman 304b4d49924SViresh Kumar spin_unlock(&port->lock); 305064256feSJonas Gorski tty_flip_buffer_push(tty_port); 306b4d49924SViresh Kumar spin_lock(&port->lock); 307ab4382d2SGreg Kroah-Hartman } 308ab4382d2SGreg Kroah-Hartman 309ab4382d2SGreg Kroah-Hartman /* 310ab4382d2SGreg Kroah-Hartman * fill tx fifo with chars to send, stop when fifo is about to be full 311ab4382d2SGreg Kroah-Hartman * or when all chars have been sent. 312ab4382d2SGreg Kroah-Hartman */ 313ab4382d2SGreg Kroah-Hartman static void bcm_uart_do_tx(struct uart_port *port) 314ab4382d2SGreg Kroah-Hartman { 315ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit; 316ab4382d2SGreg Kroah-Hartman unsigned int val, max_count; 317ab4382d2SGreg Kroah-Hartman 318ab4382d2SGreg Kroah-Hartman if (port->x_char) { 319ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, port->x_char, UART_FIFO_REG); 320ab4382d2SGreg Kroah-Hartman port->icount.tx++; 321ab4382d2SGreg Kroah-Hartman port->x_char = 0; 322ab4382d2SGreg Kroah-Hartman return; 323ab4382d2SGreg Kroah-Hartman } 324ab4382d2SGreg Kroah-Hartman 325ab4382d2SGreg Kroah-Hartman if (uart_tx_stopped(port)) { 326ab4382d2SGreg Kroah-Hartman bcm_uart_stop_tx(port); 327ab4382d2SGreg Kroah-Hartman return; 328ab4382d2SGreg Kroah-Hartman } 329ab4382d2SGreg Kroah-Hartman 330ab4382d2SGreg Kroah-Hartman xmit = &port->state->xmit; 331ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit)) 332ab4382d2SGreg Kroah-Hartman goto txq_empty; 333ab4382d2SGreg Kroah-Hartman 334ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_MCTL_REG); 335ab4382d2SGreg Kroah-Hartman val = (val & UART_MCTL_TXFIFOFILL_MASK) >> UART_MCTL_TXFIFOFILL_SHIFT; 336ab4382d2SGreg Kroah-Hartman max_count = port->fifosize - val; 337ab4382d2SGreg Kroah-Hartman 338ab4382d2SGreg Kroah-Hartman while (max_count--) { 339ab4382d2SGreg Kroah-Hartman unsigned int c; 340ab4382d2SGreg Kroah-Hartman 341ab4382d2SGreg Kroah-Hartman c = xmit->buf[xmit->tail]; 342ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, c, UART_FIFO_REG); 343ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 344ab4382d2SGreg Kroah-Hartman port->icount.tx++; 345ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit)) 346ab4382d2SGreg Kroah-Hartman break; 347ab4382d2SGreg Kroah-Hartman } 348ab4382d2SGreg Kroah-Hartman 349ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 350ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 351ab4382d2SGreg Kroah-Hartman 352ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit)) 353ab4382d2SGreg Kroah-Hartman goto txq_empty; 354ab4382d2SGreg Kroah-Hartman return; 355ab4382d2SGreg Kroah-Hartman 356ab4382d2SGreg Kroah-Hartman txq_empty: 357ab4382d2SGreg Kroah-Hartman /* nothing to send, disable transmit interrupt */ 358ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG); 359ab4382d2SGreg Kroah-Hartman val &= ~UART_TX_INT_MASK; 360ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_IR_REG); 361ab4382d2SGreg Kroah-Hartman return; 362ab4382d2SGreg Kroah-Hartman } 363ab4382d2SGreg Kroah-Hartman 364ab4382d2SGreg Kroah-Hartman /* 365ab4382d2SGreg Kroah-Hartman * process uart interrupt 366ab4382d2SGreg Kroah-Hartman */ 367ab4382d2SGreg Kroah-Hartman static irqreturn_t bcm_uart_interrupt(int irq, void *dev_id) 368ab4382d2SGreg Kroah-Hartman { 369ab4382d2SGreg Kroah-Hartman struct uart_port *port; 370ab4382d2SGreg Kroah-Hartman unsigned int irqstat; 371ab4382d2SGreg Kroah-Hartman 372ab4382d2SGreg Kroah-Hartman port = dev_id; 373ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 374ab4382d2SGreg Kroah-Hartman 375ab4382d2SGreg Kroah-Hartman irqstat = bcm_uart_readl(port, UART_IR_REG); 376ab4382d2SGreg Kroah-Hartman if (irqstat & UART_RX_INT_STAT) 377ab4382d2SGreg Kroah-Hartman bcm_uart_do_rx(port); 378ab4382d2SGreg Kroah-Hartman 379ab4382d2SGreg Kroah-Hartman if (irqstat & UART_TX_INT_STAT) 380ab4382d2SGreg Kroah-Hartman bcm_uart_do_tx(port); 381ab4382d2SGreg Kroah-Hartman 382ab4382d2SGreg Kroah-Hartman if (irqstat & UART_IR_MASK(UART_IR_EXTIP)) { 383ab4382d2SGreg Kroah-Hartman unsigned int estat; 384ab4382d2SGreg Kroah-Hartman 385ab4382d2SGreg Kroah-Hartman estat = bcm_uart_readl(port, UART_EXTINP_REG); 386ab4382d2SGreg Kroah-Hartman if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_CTS)) 387ab4382d2SGreg Kroah-Hartman uart_handle_cts_change(port, 388ab4382d2SGreg Kroah-Hartman estat & UART_EXTINP_CTS_MASK); 389ab4382d2SGreg Kroah-Hartman if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_DCD)) 390ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 391ab4382d2SGreg Kroah-Hartman estat & UART_EXTINP_DCD_MASK); 392ab4382d2SGreg Kroah-Hartman } 393ab4382d2SGreg Kroah-Hartman 394ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 395ab4382d2SGreg Kroah-Hartman return IRQ_HANDLED; 396ab4382d2SGreg Kroah-Hartman } 397ab4382d2SGreg Kroah-Hartman 398ab4382d2SGreg Kroah-Hartman /* 399ab4382d2SGreg Kroah-Hartman * enable rx & tx operation on uart 400ab4382d2SGreg Kroah-Hartman */ 401ab4382d2SGreg Kroah-Hartman static void bcm_uart_enable(struct uart_port *port) 402ab4382d2SGreg Kroah-Hartman { 403ab4382d2SGreg Kroah-Hartman unsigned int val; 404ab4382d2SGreg Kroah-Hartman 405ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG); 406ab4382d2SGreg Kroah-Hartman val |= (UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK | UART_CTL_RXEN_MASK); 407ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG); 408ab4382d2SGreg Kroah-Hartman } 409ab4382d2SGreg Kroah-Hartman 410ab4382d2SGreg Kroah-Hartman /* 411ab4382d2SGreg Kroah-Hartman * disable rx & tx operation on uart 412ab4382d2SGreg Kroah-Hartman */ 413ab4382d2SGreg Kroah-Hartman static void bcm_uart_disable(struct uart_port *port) 414ab4382d2SGreg Kroah-Hartman { 415ab4382d2SGreg Kroah-Hartman unsigned int val; 416ab4382d2SGreg Kroah-Hartman 417ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG); 418ab4382d2SGreg Kroah-Hartman val &= ~(UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK | 419ab4382d2SGreg Kroah-Hartman UART_CTL_RXEN_MASK); 420ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG); 421ab4382d2SGreg Kroah-Hartman } 422ab4382d2SGreg Kroah-Hartman 423ab4382d2SGreg Kroah-Hartman /* 424ab4382d2SGreg Kroah-Hartman * clear all unread data in rx fifo and unsent data in tx fifo 425ab4382d2SGreg Kroah-Hartman */ 426ab4382d2SGreg Kroah-Hartman static void bcm_uart_flush(struct uart_port *port) 427ab4382d2SGreg Kroah-Hartman { 428ab4382d2SGreg Kroah-Hartman unsigned int val; 429ab4382d2SGreg Kroah-Hartman 430ab4382d2SGreg Kroah-Hartman /* empty rx and tx fifo */ 431ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG); 432ab4382d2SGreg Kroah-Hartman val |= UART_CTL_RSTRXFIFO_MASK | UART_CTL_RSTTXFIFO_MASK; 433ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG); 434ab4382d2SGreg Kroah-Hartman 435ab4382d2SGreg Kroah-Hartman /* read any pending char to make sure all irq status are 436ab4382d2SGreg Kroah-Hartman * cleared */ 437ab4382d2SGreg Kroah-Hartman (void)bcm_uart_readl(port, UART_FIFO_REG); 438ab4382d2SGreg Kroah-Hartman } 439ab4382d2SGreg Kroah-Hartman 440ab4382d2SGreg Kroah-Hartman /* 441ab4382d2SGreg Kroah-Hartman * serial core request to initialize uart and start rx operation 442ab4382d2SGreg Kroah-Hartman */ 443ab4382d2SGreg Kroah-Hartman static int bcm_uart_startup(struct uart_port *port) 444ab4382d2SGreg Kroah-Hartman { 445ab4382d2SGreg Kroah-Hartman unsigned int val; 446ab4382d2SGreg Kroah-Hartman int ret; 447ab4382d2SGreg Kroah-Hartman 448ab4382d2SGreg Kroah-Hartman /* mask all irq and flush port */ 449ab4382d2SGreg Kroah-Hartman bcm_uart_disable(port); 450ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, 0, UART_IR_REG); 451ab4382d2SGreg Kroah-Hartman bcm_uart_flush(port); 452ab4382d2SGreg Kroah-Hartman 453ab4382d2SGreg Kroah-Hartman /* clear any pending external input interrupt */ 454ab4382d2SGreg Kroah-Hartman (void)bcm_uart_readl(port, UART_EXTINP_REG); 455ab4382d2SGreg Kroah-Hartman 456ab4382d2SGreg Kroah-Hartman /* set rx/tx fifo thresh to fifo half size */ 457ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_MCTL_REG); 458ab4382d2SGreg Kroah-Hartman val &= ~(UART_MCTL_RXFIFOTHRESH_MASK | UART_MCTL_TXFIFOTHRESH_MASK); 459ab4382d2SGreg Kroah-Hartman val |= (port->fifosize / 2) << UART_MCTL_RXFIFOTHRESH_SHIFT; 460ab4382d2SGreg Kroah-Hartman val |= (port->fifosize / 2) << UART_MCTL_TXFIFOTHRESH_SHIFT; 461ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_MCTL_REG); 462ab4382d2SGreg Kroah-Hartman 463ab4382d2SGreg Kroah-Hartman /* set rx fifo timeout to 1 char time */ 464ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_CTL_REG); 465ab4382d2SGreg Kroah-Hartman val &= ~UART_CTL_RXTMOUTCNT_MASK; 466ab4382d2SGreg Kroah-Hartman val |= 1 << UART_CTL_RXTMOUTCNT_SHIFT; 467ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_CTL_REG); 468ab4382d2SGreg Kroah-Hartman 469ab4382d2SGreg Kroah-Hartman /* report any edge on dcd and cts */ 470ab4382d2SGreg Kroah-Hartman val = UART_EXTINP_INT_MASK; 471ab4382d2SGreg Kroah-Hartman val |= UART_EXTINP_DCD_NOSENSE_MASK; 472ab4382d2SGreg Kroah-Hartman val |= UART_EXTINP_CTS_NOSENSE_MASK; 473ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, val, UART_EXTINP_REG); 474ab4382d2SGreg Kroah-Hartman 475ab4382d2SGreg Kroah-Hartman /* register irq and enable rx interrupts */ 476ab4382d2SGreg Kroah-Hartman ret = request_irq(port->irq, bcm_uart_interrupt, 0, 477ab4382d2SGreg Kroah-Hartman bcm_uart_type(port), port); 478ab4382d2SGreg Kroah-Hartman if (ret) 479ab4382d2SGreg Kroah-Hartman return ret; 480ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, UART_RX_INT_MASK, UART_IR_REG); 481ab4382d2SGreg Kroah-Hartman bcm_uart_enable(port); 482ab4382d2SGreg Kroah-Hartman return 0; 483ab4382d2SGreg Kroah-Hartman } 484ab4382d2SGreg Kroah-Hartman 485ab4382d2SGreg Kroah-Hartman /* 486ab4382d2SGreg Kroah-Hartman * serial core request to flush & disable uart 487ab4382d2SGreg Kroah-Hartman */ 488ab4382d2SGreg Kroah-Hartman static void bcm_uart_shutdown(struct uart_port *port) 489ab4382d2SGreg Kroah-Hartman { 490ab4382d2SGreg Kroah-Hartman unsigned long flags; 491ab4382d2SGreg Kroah-Hartman 492ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 493ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, 0, UART_IR_REG); 494ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 495ab4382d2SGreg Kroah-Hartman 496ab4382d2SGreg Kroah-Hartman bcm_uart_disable(port); 497ab4382d2SGreg Kroah-Hartman bcm_uart_flush(port); 498ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 499ab4382d2SGreg Kroah-Hartman } 500ab4382d2SGreg Kroah-Hartman 501ab4382d2SGreg Kroah-Hartman /* 502ab4382d2SGreg Kroah-Hartman * serial core request to change current uart setting 503ab4382d2SGreg Kroah-Hartman */ 504ab4382d2SGreg Kroah-Hartman static void bcm_uart_set_termios(struct uart_port *port, 505ab4382d2SGreg Kroah-Hartman struct ktermios *new, 506ab4382d2SGreg Kroah-Hartman struct ktermios *old) 507ab4382d2SGreg Kroah-Hartman { 508ab4382d2SGreg Kroah-Hartman unsigned int ctl, baud, quot, ier; 509ab4382d2SGreg Kroah-Hartman unsigned long flags; 510ab4382d2SGreg Kroah-Hartman 511ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 512ab4382d2SGreg Kroah-Hartman 513ab4382d2SGreg Kroah-Hartman /* disable uart while changing speed */ 514ab4382d2SGreg Kroah-Hartman bcm_uart_disable(port); 515ab4382d2SGreg Kroah-Hartman bcm_uart_flush(port); 516ab4382d2SGreg Kroah-Hartman 517ab4382d2SGreg Kroah-Hartman /* update Control register */ 518ab4382d2SGreg Kroah-Hartman ctl = bcm_uart_readl(port, UART_CTL_REG); 519ab4382d2SGreg Kroah-Hartman ctl &= ~UART_CTL_BITSPERSYM_MASK; 520ab4382d2SGreg Kroah-Hartman 521ab4382d2SGreg Kroah-Hartman switch (new->c_cflag & CSIZE) { 522ab4382d2SGreg Kroah-Hartman case CS5: 523ab4382d2SGreg Kroah-Hartman ctl |= (0 << UART_CTL_BITSPERSYM_SHIFT); 524ab4382d2SGreg Kroah-Hartman break; 525ab4382d2SGreg Kroah-Hartman case CS6: 526ab4382d2SGreg Kroah-Hartman ctl |= (1 << UART_CTL_BITSPERSYM_SHIFT); 527ab4382d2SGreg Kroah-Hartman break; 528ab4382d2SGreg Kroah-Hartman case CS7: 529ab4382d2SGreg Kroah-Hartman ctl |= (2 << UART_CTL_BITSPERSYM_SHIFT); 530ab4382d2SGreg Kroah-Hartman break; 531ab4382d2SGreg Kroah-Hartman default: 532ab4382d2SGreg Kroah-Hartman ctl |= (3 << UART_CTL_BITSPERSYM_SHIFT); 533ab4382d2SGreg Kroah-Hartman break; 534ab4382d2SGreg Kroah-Hartman } 535ab4382d2SGreg Kroah-Hartman 536ab4382d2SGreg Kroah-Hartman ctl &= ~UART_CTL_STOPBITS_MASK; 537ab4382d2SGreg Kroah-Hartman if (new->c_cflag & CSTOPB) 538ab4382d2SGreg Kroah-Hartman ctl |= UART_CTL_STOPBITS_2; 539ab4382d2SGreg Kroah-Hartman else 540ab4382d2SGreg Kroah-Hartman ctl |= UART_CTL_STOPBITS_1; 541ab4382d2SGreg Kroah-Hartman 542ab4382d2SGreg Kroah-Hartman ctl &= ~(UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK); 543ab4382d2SGreg Kroah-Hartman if (new->c_cflag & PARENB) 544ab4382d2SGreg Kroah-Hartman ctl |= (UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK); 545ab4382d2SGreg Kroah-Hartman ctl &= ~(UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK); 546ab4382d2SGreg Kroah-Hartman if (new->c_cflag & PARODD) 547ab4382d2SGreg Kroah-Hartman ctl |= (UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK); 548ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, ctl, UART_CTL_REG); 549ab4382d2SGreg Kroah-Hartman 550ab4382d2SGreg Kroah-Hartman /* update Baudword register */ 551ab4382d2SGreg Kroah-Hartman baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16); 552ab4382d2SGreg Kroah-Hartman quot = uart_get_divisor(port, baud) - 1; 553ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, quot, UART_BAUD_REG); 554ab4382d2SGreg Kroah-Hartman 555ab4382d2SGreg Kroah-Hartman /* update Interrupt register */ 556ab4382d2SGreg Kroah-Hartman ier = bcm_uart_readl(port, UART_IR_REG); 557ab4382d2SGreg Kroah-Hartman 558ab4382d2SGreg Kroah-Hartman ier &= ~UART_IR_MASK(UART_IR_EXTIP); 559ab4382d2SGreg Kroah-Hartman if (UART_ENABLE_MS(port, new->c_cflag)) 560ab4382d2SGreg Kroah-Hartman ier |= UART_IR_MASK(UART_IR_EXTIP); 561ab4382d2SGreg Kroah-Hartman 562ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, ier, UART_IR_REG); 563ab4382d2SGreg Kroah-Hartman 564ab4382d2SGreg Kroah-Hartman /* update read/ignore mask */ 565ab4382d2SGreg Kroah-Hartman port->read_status_mask = UART_FIFO_VALID_MASK; 566ab4382d2SGreg Kroah-Hartman if (new->c_iflag & INPCK) { 567ab4382d2SGreg Kroah-Hartman port->read_status_mask |= UART_FIFO_FRAMEERR_MASK; 568ab4382d2SGreg Kroah-Hartman port->read_status_mask |= UART_FIFO_PARERR_MASK; 569ab4382d2SGreg Kroah-Hartman } 570*ef8b9ddcSPeter Hurley if (new->c_iflag & (IGNBRK | BRKINT)) 571ab4382d2SGreg Kroah-Hartman port->read_status_mask |= UART_FIFO_BRKDET_MASK; 572ab4382d2SGreg Kroah-Hartman 573ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 574ab4382d2SGreg Kroah-Hartman if (new->c_iflag & IGNPAR) 575ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= UART_FIFO_PARERR_MASK; 576ab4382d2SGreg Kroah-Hartman if (new->c_iflag & IGNBRK) 577ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= UART_FIFO_BRKDET_MASK; 578ab4382d2SGreg Kroah-Hartman if (!(new->c_cflag & CREAD)) 579ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= UART_FIFO_VALID_MASK; 580ab4382d2SGreg Kroah-Hartman 581ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, new->c_cflag, baud); 582ab4382d2SGreg Kroah-Hartman bcm_uart_enable(port); 583ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 584ab4382d2SGreg Kroah-Hartman } 585ab4382d2SGreg Kroah-Hartman 586ab4382d2SGreg Kroah-Hartman /* 587ab4382d2SGreg Kroah-Hartman * serial core request to claim uart iomem 588ab4382d2SGreg Kroah-Hartman */ 589ab4382d2SGreg Kroah-Hartman static int bcm_uart_request_port(struct uart_port *port) 590ab4382d2SGreg Kroah-Hartman { 591ab4382d2SGreg Kroah-Hartman unsigned int size; 592ab4382d2SGreg Kroah-Hartman 5935811712fSFlorian Fainelli size = UART_REG_SIZE; 594ab4382d2SGreg Kroah-Hartman if (!request_mem_region(port->mapbase, size, "bcm63xx")) { 595ab4382d2SGreg Kroah-Hartman dev_err(port->dev, "Memory region busy\n"); 596ab4382d2SGreg Kroah-Hartman return -EBUSY; 597ab4382d2SGreg Kroah-Hartman } 598ab4382d2SGreg Kroah-Hartman 599ab4382d2SGreg Kroah-Hartman port->membase = ioremap(port->mapbase, size); 600ab4382d2SGreg Kroah-Hartman if (!port->membase) { 601ab4382d2SGreg Kroah-Hartman dev_err(port->dev, "Unable to map registers\n"); 602ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 603ab4382d2SGreg Kroah-Hartman return -EBUSY; 604ab4382d2SGreg Kroah-Hartman } 605ab4382d2SGreg Kroah-Hartman return 0; 606ab4382d2SGreg Kroah-Hartman } 607ab4382d2SGreg Kroah-Hartman 608ab4382d2SGreg Kroah-Hartman /* 609ab4382d2SGreg Kroah-Hartman * serial core request to release uart iomem 610ab4382d2SGreg Kroah-Hartman */ 611ab4382d2SGreg Kroah-Hartman static void bcm_uart_release_port(struct uart_port *port) 612ab4382d2SGreg Kroah-Hartman { 6135811712fSFlorian Fainelli release_mem_region(port->mapbase, UART_REG_SIZE); 614ab4382d2SGreg Kroah-Hartman iounmap(port->membase); 615ab4382d2SGreg Kroah-Hartman } 616ab4382d2SGreg Kroah-Hartman 617ab4382d2SGreg Kroah-Hartman /* 618ab4382d2SGreg Kroah-Hartman * serial core request to do any port required autoconfiguration 619ab4382d2SGreg Kroah-Hartman */ 620ab4382d2SGreg Kroah-Hartman static void bcm_uart_config_port(struct uart_port *port, int flags) 621ab4382d2SGreg Kroah-Hartman { 622ab4382d2SGreg Kroah-Hartman if (flags & UART_CONFIG_TYPE) { 623ab4382d2SGreg Kroah-Hartman if (bcm_uart_request_port(port)) 624ab4382d2SGreg Kroah-Hartman return; 625ab4382d2SGreg Kroah-Hartman port->type = PORT_BCM63XX; 626ab4382d2SGreg Kroah-Hartman } 627ab4382d2SGreg Kroah-Hartman } 628ab4382d2SGreg Kroah-Hartman 629ab4382d2SGreg Kroah-Hartman /* 630ab4382d2SGreg Kroah-Hartman * serial core request to check that port information in serinfo are 631ab4382d2SGreg Kroah-Hartman * suitable 632ab4382d2SGreg Kroah-Hartman */ 633ab4382d2SGreg Kroah-Hartman static int bcm_uart_verify_port(struct uart_port *port, 634ab4382d2SGreg Kroah-Hartman struct serial_struct *serinfo) 635ab4382d2SGreg Kroah-Hartman { 636ab4382d2SGreg Kroah-Hartman if (port->type != PORT_BCM63XX) 637ab4382d2SGreg Kroah-Hartman return -EINVAL; 638ab4382d2SGreg Kroah-Hartman if (port->irq != serinfo->irq) 639ab4382d2SGreg Kroah-Hartman return -EINVAL; 640ab4382d2SGreg Kroah-Hartman if (port->iotype != serinfo->io_type) 641ab4382d2SGreg Kroah-Hartman return -EINVAL; 642ab4382d2SGreg Kroah-Hartman if (port->mapbase != (unsigned long)serinfo->iomem_base) 643ab4382d2SGreg Kroah-Hartman return -EINVAL; 644ab4382d2SGreg Kroah-Hartman return 0; 645ab4382d2SGreg Kroah-Hartman } 646ab4382d2SGreg Kroah-Hartman 647ab4382d2SGreg Kroah-Hartman /* serial core callbacks */ 648ab4382d2SGreg Kroah-Hartman static struct uart_ops bcm_uart_ops = { 649ab4382d2SGreg Kroah-Hartman .tx_empty = bcm_uart_tx_empty, 650ab4382d2SGreg Kroah-Hartman .get_mctrl = bcm_uart_get_mctrl, 651ab4382d2SGreg Kroah-Hartman .set_mctrl = bcm_uart_set_mctrl, 652ab4382d2SGreg Kroah-Hartman .start_tx = bcm_uart_start_tx, 653ab4382d2SGreg Kroah-Hartman .stop_tx = bcm_uart_stop_tx, 654ab4382d2SGreg Kroah-Hartman .stop_rx = bcm_uart_stop_rx, 655ab4382d2SGreg Kroah-Hartman .enable_ms = bcm_uart_enable_ms, 656ab4382d2SGreg Kroah-Hartman .break_ctl = bcm_uart_break_ctl, 657ab4382d2SGreg Kroah-Hartman .startup = bcm_uart_startup, 658ab4382d2SGreg Kroah-Hartman .shutdown = bcm_uart_shutdown, 659ab4382d2SGreg Kroah-Hartman .set_termios = bcm_uart_set_termios, 660ab4382d2SGreg Kroah-Hartman .type = bcm_uart_type, 661ab4382d2SGreg Kroah-Hartman .release_port = bcm_uart_release_port, 662ab4382d2SGreg Kroah-Hartman .request_port = bcm_uart_request_port, 663ab4382d2SGreg Kroah-Hartman .config_port = bcm_uart_config_port, 664ab4382d2SGreg Kroah-Hartman .verify_port = bcm_uart_verify_port, 665ab4382d2SGreg Kroah-Hartman }; 666ab4382d2SGreg Kroah-Hartman 667ab4382d2SGreg Kroah-Hartman 668ab4382d2SGreg Kroah-Hartman 669ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_BCM63XX_CONSOLE 670ab4382d2SGreg Kroah-Hartman static inline void wait_for_xmitr(struct uart_port *port) 671ab4382d2SGreg Kroah-Hartman { 672ab4382d2SGreg Kroah-Hartman unsigned int tmout; 673ab4382d2SGreg Kroah-Hartman 674ab4382d2SGreg Kroah-Hartman /* Wait up to 10ms for the character(s) to be sent. */ 675ab4382d2SGreg Kroah-Hartman tmout = 10000; 676ab4382d2SGreg Kroah-Hartman while (--tmout) { 677ab4382d2SGreg Kroah-Hartman unsigned int val; 678ab4382d2SGreg Kroah-Hartman 679ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_IR_REG); 680ab4382d2SGreg Kroah-Hartman if (val & UART_IR_STAT(UART_IR_TXEMPTY)) 681ab4382d2SGreg Kroah-Hartman break; 682ab4382d2SGreg Kroah-Hartman udelay(1); 683ab4382d2SGreg Kroah-Hartman } 684ab4382d2SGreg Kroah-Hartman 685ab4382d2SGreg Kroah-Hartman /* Wait up to 1s for flow control if necessary */ 686ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_CONS_FLOW) { 687ab4382d2SGreg Kroah-Hartman tmout = 1000000; 688ab4382d2SGreg Kroah-Hartman while (--tmout) { 689ab4382d2SGreg Kroah-Hartman unsigned int val; 690ab4382d2SGreg Kroah-Hartman 691ab4382d2SGreg Kroah-Hartman val = bcm_uart_readl(port, UART_EXTINP_REG); 692ab4382d2SGreg Kroah-Hartman if (val & UART_EXTINP_CTS_MASK) 693ab4382d2SGreg Kroah-Hartman break; 694ab4382d2SGreg Kroah-Hartman udelay(1); 695ab4382d2SGreg Kroah-Hartman } 696ab4382d2SGreg Kroah-Hartman } 697ab4382d2SGreg Kroah-Hartman } 698ab4382d2SGreg Kroah-Hartman 699ab4382d2SGreg Kroah-Hartman /* 700ab4382d2SGreg Kroah-Hartman * output given char 701ab4382d2SGreg Kroah-Hartman */ 702ab4382d2SGreg Kroah-Hartman static void bcm_console_putchar(struct uart_port *port, int ch) 703ab4382d2SGreg Kroah-Hartman { 704ab4382d2SGreg Kroah-Hartman wait_for_xmitr(port); 705ab4382d2SGreg Kroah-Hartman bcm_uart_writel(port, ch, UART_FIFO_REG); 706ab4382d2SGreg Kroah-Hartman } 707ab4382d2SGreg Kroah-Hartman 708ab4382d2SGreg Kroah-Hartman /* 709ab4382d2SGreg Kroah-Hartman * console core request to output given string 710ab4382d2SGreg Kroah-Hartman */ 711ab4382d2SGreg Kroah-Hartman static void bcm_console_write(struct console *co, const char *s, 712ab4382d2SGreg Kroah-Hartman unsigned int count) 713ab4382d2SGreg Kroah-Hartman { 714ab4382d2SGreg Kroah-Hartman struct uart_port *port; 715ab4382d2SGreg Kroah-Hartman unsigned long flags; 716ab4382d2SGreg Kroah-Hartman int locked; 717ab4382d2SGreg Kroah-Hartman 718ab4382d2SGreg Kroah-Hartman port = &ports[co->index]; 719ab4382d2SGreg Kroah-Hartman 720ab4382d2SGreg Kroah-Hartman local_irq_save(flags); 721ab4382d2SGreg Kroah-Hartman if (port->sysrq) { 722ab4382d2SGreg Kroah-Hartman /* bcm_uart_interrupt() already took the lock */ 723ab4382d2SGreg Kroah-Hartman locked = 0; 724ab4382d2SGreg Kroah-Hartman } else if (oops_in_progress) { 725ab4382d2SGreg Kroah-Hartman locked = spin_trylock(&port->lock); 726ab4382d2SGreg Kroah-Hartman } else { 727ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 728ab4382d2SGreg Kroah-Hartman locked = 1; 729ab4382d2SGreg Kroah-Hartman } 730ab4382d2SGreg Kroah-Hartman 731ab4382d2SGreg Kroah-Hartman /* call helper to deal with \r\n */ 732ab4382d2SGreg Kroah-Hartman uart_console_write(port, s, count, bcm_console_putchar); 733ab4382d2SGreg Kroah-Hartman 734ab4382d2SGreg Kroah-Hartman /* and wait for char to be transmitted */ 735ab4382d2SGreg Kroah-Hartman wait_for_xmitr(port); 736ab4382d2SGreg Kroah-Hartman 737ab4382d2SGreg Kroah-Hartman if (locked) 738ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 739ab4382d2SGreg Kroah-Hartman local_irq_restore(flags); 740ab4382d2SGreg Kroah-Hartman } 741ab4382d2SGreg Kroah-Hartman 742ab4382d2SGreg Kroah-Hartman /* 743ab4382d2SGreg Kroah-Hartman * console core request to setup given console, find matching uart 744ab4382d2SGreg Kroah-Hartman * port and setup it. 745ab4382d2SGreg Kroah-Hartman */ 746ab4382d2SGreg Kroah-Hartman static int bcm_console_setup(struct console *co, char *options) 747ab4382d2SGreg Kroah-Hartman { 748ab4382d2SGreg Kroah-Hartman struct uart_port *port; 749ab4382d2SGreg Kroah-Hartman int baud = 9600; 750ab4382d2SGreg Kroah-Hartman int bits = 8; 751ab4382d2SGreg Kroah-Hartman int parity = 'n'; 752ab4382d2SGreg Kroah-Hartman int flow = 'n'; 753ab4382d2SGreg Kroah-Hartman 754ab4382d2SGreg Kroah-Hartman if (co->index < 0 || co->index >= BCM63XX_NR_UARTS) 755ab4382d2SGreg Kroah-Hartman return -EINVAL; 756ab4382d2SGreg Kroah-Hartman port = &ports[co->index]; 757ab4382d2SGreg Kroah-Hartman if (!port->membase) 758ab4382d2SGreg Kroah-Hartman return -ENODEV; 759ab4382d2SGreg Kroah-Hartman if (options) 760ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 761ab4382d2SGreg Kroah-Hartman 762ab4382d2SGreg Kroah-Hartman return uart_set_options(port, co, baud, parity, bits, flow); 763ab4382d2SGreg Kroah-Hartman } 764ab4382d2SGreg Kroah-Hartman 765ab4382d2SGreg Kroah-Hartman static struct uart_driver bcm_uart_driver; 766ab4382d2SGreg Kroah-Hartman 767ab4382d2SGreg Kroah-Hartman static struct console bcm63xx_console = { 768ab4382d2SGreg Kroah-Hartman .name = "ttyS", 769ab4382d2SGreg Kroah-Hartman .write = bcm_console_write, 770ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 771ab4382d2SGreg Kroah-Hartman .setup = bcm_console_setup, 772ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 773ab4382d2SGreg Kroah-Hartman .index = -1, 774ab4382d2SGreg Kroah-Hartman .data = &bcm_uart_driver, 775ab4382d2SGreg Kroah-Hartman }; 776ab4382d2SGreg Kroah-Hartman 777ab4382d2SGreg Kroah-Hartman static int __init bcm63xx_console_init(void) 778ab4382d2SGreg Kroah-Hartman { 779ab4382d2SGreg Kroah-Hartman register_console(&bcm63xx_console); 780ab4382d2SGreg Kroah-Hartman return 0; 781ab4382d2SGreg Kroah-Hartman } 782ab4382d2SGreg Kroah-Hartman 783ab4382d2SGreg Kroah-Hartman console_initcall(bcm63xx_console_init); 784ab4382d2SGreg Kroah-Hartman 785ab4382d2SGreg Kroah-Hartman #define BCM63XX_CONSOLE (&bcm63xx_console) 786ab4382d2SGreg Kroah-Hartman #else 787ab4382d2SGreg Kroah-Hartman #define BCM63XX_CONSOLE NULL 788ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_BCM63XX_CONSOLE */ 789ab4382d2SGreg Kroah-Hartman 790ab4382d2SGreg Kroah-Hartman static struct uart_driver bcm_uart_driver = { 791ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 792ab4382d2SGreg Kroah-Hartman .driver_name = "bcm63xx_uart", 793ab4382d2SGreg Kroah-Hartman .dev_name = "ttyS", 794ab4382d2SGreg Kroah-Hartman .major = TTY_MAJOR, 795ab4382d2SGreg Kroah-Hartman .minor = 64, 796ab4382d2SGreg Kroah-Hartman .nr = BCM63XX_NR_UARTS, 797ab4382d2SGreg Kroah-Hartman .cons = BCM63XX_CONSOLE, 798ab4382d2SGreg Kroah-Hartman }; 799ab4382d2SGreg Kroah-Hartman 800ab4382d2SGreg Kroah-Hartman /* 801ab4382d2SGreg Kroah-Hartman * platform driver probe/remove callback 802ab4382d2SGreg Kroah-Hartman */ 8039671f099SBill Pemberton static int bcm_uart_probe(struct platform_device *pdev) 804ab4382d2SGreg Kroah-Hartman { 805ab4382d2SGreg Kroah-Hartman struct resource *res_mem, *res_irq; 806ab4382d2SGreg Kroah-Hartman struct uart_port *port; 807ab4382d2SGreg Kroah-Hartman struct clk *clk; 808ab4382d2SGreg Kroah-Hartman int ret; 809ab4382d2SGreg Kroah-Hartman 8109277285fSFlorian Fainelli if (pdev->dev.of_node) 8119277285fSFlorian Fainelli pdev->id = of_alias_get_id(pdev->dev.of_node, "uart"); 8129277285fSFlorian Fainelli 813ab4382d2SGreg Kroah-Hartman if (pdev->id < 0 || pdev->id >= BCM63XX_NR_UARTS) 814ab4382d2SGreg Kroah-Hartman return -EINVAL; 815ab4382d2SGreg Kroah-Hartman 816ab4382d2SGreg Kroah-Hartman if (ports[pdev->id].membase) 817ab4382d2SGreg Kroah-Hartman return -EBUSY; 818ab4382d2SGreg Kroah-Hartman 819ab4382d2SGreg Kroah-Hartman res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 820ab4382d2SGreg Kroah-Hartman if (!res_mem) 821ab4382d2SGreg Kroah-Hartman return -ENODEV; 822ab4382d2SGreg Kroah-Hartman 823ab4382d2SGreg Kroah-Hartman res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 824ab4382d2SGreg Kroah-Hartman if (!res_irq) 825ab4382d2SGreg Kroah-Hartman return -ENODEV; 826ab4382d2SGreg Kroah-Hartman 827ab4382d2SGreg Kroah-Hartman clk = clk_get(&pdev->dev, "periph"); 828ab4382d2SGreg Kroah-Hartman if (IS_ERR(clk)) 829ab4382d2SGreg Kroah-Hartman return -ENODEV; 830ab4382d2SGreg Kroah-Hartman 831ab4382d2SGreg Kroah-Hartman port = &ports[pdev->id]; 832ab4382d2SGreg Kroah-Hartman memset(port, 0, sizeof(*port)); 833ab4382d2SGreg Kroah-Hartman port->iotype = UPIO_MEM; 834ab4382d2SGreg Kroah-Hartman port->mapbase = res_mem->start; 835ab4382d2SGreg Kroah-Hartman port->irq = res_irq->start; 836ab4382d2SGreg Kroah-Hartman port->ops = &bcm_uart_ops; 837ab4382d2SGreg Kroah-Hartman port->flags = UPF_BOOT_AUTOCONF; 838ab4382d2SGreg Kroah-Hartman port->dev = &pdev->dev; 839ab4382d2SGreg Kroah-Hartman port->fifosize = 16; 840ab4382d2SGreg Kroah-Hartman port->uartclk = clk_get_rate(clk) / 2; 841ab4382d2SGreg Kroah-Hartman port->line = pdev->id; 842ab4382d2SGreg Kroah-Hartman clk_put(clk); 843ab4382d2SGreg Kroah-Hartman 844ab4382d2SGreg Kroah-Hartman ret = uart_add_one_port(&bcm_uart_driver, port); 845ab4382d2SGreg Kroah-Hartman if (ret) { 846ab4382d2SGreg Kroah-Hartman ports[pdev->id].membase = 0; 847ab4382d2SGreg Kroah-Hartman return ret; 848ab4382d2SGreg Kroah-Hartman } 849ab4382d2SGreg Kroah-Hartman platform_set_drvdata(pdev, port); 850ab4382d2SGreg Kroah-Hartman return 0; 851ab4382d2SGreg Kroah-Hartman } 852ab4382d2SGreg Kroah-Hartman 853ae8d8a14SBill Pemberton static int bcm_uart_remove(struct platform_device *pdev) 854ab4382d2SGreg Kroah-Hartman { 855ab4382d2SGreg Kroah-Hartman struct uart_port *port; 856ab4382d2SGreg Kroah-Hartman 857ab4382d2SGreg Kroah-Hartman port = platform_get_drvdata(pdev); 858ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&bcm_uart_driver, port); 859ab4382d2SGreg Kroah-Hartman /* mark port as free */ 860ab4382d2SGreg Kroah-Hartman ports[pdev->id].membase = 0; 861ab4382d2SGreg Kroah-Hartman return 0; 862ab4382d2SGreg Kroah-Hartman } 863ab4382d2SGreg Kroah-Hartman 8649277285fSFlorian Fainelli static const struct of_device_id bcm63xx_of_match[] = { 8659277285fSFlorian Fainelli { .compatible = "brcm,bcm6345-uart" }, 8669277285fSFlorian Fainelli { /* sentinel */ } 8679277285fSFlorian Fainelli }; 8689277285fSFlorian Fainelli MODULE_DEVICE_TABLE(of, bcm63xx_of_match); 8699277285fSFlorian Fainelli 870ab4382d2SGreg Kroah-Hartman /* 871ab4382d2SGreg Kroah-Hartman * platform driver stuff 872ab4382d2SGreg Kroah-Hartman */ 873ab4382d2SGreg Kroah-Hartman static struct platform_driver bcm_uart_platform_driver = { 874ab4382d2SGreg Kroah-Hartman .probe = bcm_uart_probe, 8752d47b716SBill Pemberton .remove = bcm_uart_remove, 876ab4382d2SGreg Kroah-Hartman .driver = { 877ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 878ab4382d2SGreg Kroah-Hartman .name = "bcm63xx_uart", 8799277285fSFlorian Fainelli .of_match_table = bcm63xx_of_match, 880ab4382d2SGreg Kroah-Hartman }, 881ab4382d2SGreg Kroah-Hartman }; 882ab4382d2SGreg Kroah-Hartman 883ab4382d2SGreg Kroah-Hartman static int __init bcm_uart_init(void) 884ab4382d2SGreg Kroah-Hartman { 885ab4382d2SGreg Kroah-Hartman int ret; 886ab4382d2SGreg Kroah-Hartman 887ab4382d2SGreg Kroah-Hartman ret = uart_register_driver(&bcm_uart_driver); 888ab4382d2SGreg Kroah-Hartman if (ret) 889ab4382d2SGreg Kroah-Hartman return ret; 890ab4382d2SGreg Kroah-Hartman 891ab4382d2SGreg Kroah-Hartman ret = platform_driver_register(&bcm_uart_platform_driver); 892ab4382d2SGreg Kroah-Hartman if (ret) 893ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&bcm_uart_driver); 894ab4382d2SGreg Kroah-Hartman 895ab4382d2SGreg Kroah-Hartman return ret; 896ab4382d2SGreg Kroah-Hartman } 897ab4382d2SGreg Kroah-Hartman 898ab4382d2SGreg Kroah-Hartman static void __exit bcm_uart_exit(void) 899ab4382d2SGreg Kroah-Hartman { 900ab4382d2SGreg Kroah-Hartman platform_driver_unregister(&bcm_uart_platform_driver); 901ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&bcm_uart_driver); 902ab4382d2SGreg Kroah-Hartman } 903ab4382d2SGreg Kroah-Hartman 904ab4382d2SGreg Kroah-Hartman module_init(bcm_uart_init); 905ab4382d2SGreg Kroah-Hartman module_exit(bcm_uart_exit); 906ab4382d2SGreg Kroah-Hartman 907ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>"); 908ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Broadcom 63<xx integrated uart driver"); 909ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 910