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