1ab4382d2SGreg Kroah-Hartman /* 2ab4382d2SGreg Kroah-Hartman * Driver for Atmel AT91 / AT32 Serial ports 3ab4382d2SGreg Kroah-Hartman * Copyright (C) 2003 Rick Bronson 4ab4382d2SGreg Kroah-Hartman * 5ab4382d2SGreg Kroah-Hartman * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd. 6ab4382d2SGreg Kroah-Hartman * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. 7ab4382d2SGreg Kroah-Hartman * 8ab4382d2SGreg Kroah-Hartman * DMA support added by Chip Coldwell. 9ab4382d2SGreg Kroah-Hartman * 10ab4382d2SGreg Kroah-Hartman * This program is free software; you can redistribute it and/or modify 11ab4382d2SGreg Kroah-Hartman * it under the terms of the GNU General Public License as published by 12ab4382d2SGreg Kroah-Hartman * the Free Software Foundation; either version 2 of the License, or 13ab4382d2SGreg Kroah-Hartman * (at your option) any later version. 14ab4382d2SGreg Kroah-Hartman * 15ab4382d2SGreg Kroah-Hartman * This program is distributed in the hope that it will be useful, 16ab4382d2SGreg Kroah-Hartman * but WITHOUT ANY WARRANTY; without even the implied warranty of 17ab4382d2SGreg Kroah-Hartman * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18ab4382d2SGreg Kroah-Hartman * GNU General Public License for more details. 19ab4382d2SGreg Kroah-Hartman * 20ab4382d2SGreg Kroah-Hartman * You should have received a copy of the GNU General Public License 21ab4382d2SGreg Kroah-Hartman * along with this program; if not, write to the Free Software 22ab4382d2SGreg Kroah-Hartman * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23ab4382d2SGreg Kroah-Hartman * 24ab4382d2SGreg Kroah-Hartman */ 25ab4382d2SGreg Kroah-Hartman #include <linux/module.h> 26ab4382d2SGreg Kroah-Hartman #include <linux/tty.h> 27ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h> 28ab4382d2SGreg Kroah-Hartman #include <linux/slab.h> 29ab4382d2SGreg Kroah-Hartman #include <linux/init.h> 30ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> 31ab4382d2SGreg Kroah-Hartman #include <linux/clk.h> 32ab4382d2SGreg Kroah-Hartman #include <linux/console.h> 33ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h> 34ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h> 35ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h> 36ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h> 37ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h> 38ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h> 39ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h> 40ab4382d2SGreg Kroah-Hartman 41ab4382d2SGreg Kroah-Hartman #include <asm/io.h> 42ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h> 43ab4382d2SGreg Kroah-Hartman 44ab4382d2SGreg Kroah-Hartman #include <asm/mach/serial_at91.h> 45ab4382d2SGreg Kroah-Hartman #include <mach/board.h> 46ab4382d2SGreg Kroah-Hartman 47ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARM 48ab4382d2SGreg Kroah-Hartman #include <mach/cpu.h> 49ab4382d2SGreg Kroah-Hartman #include <mach/gpio.h> 50ab4382d2SGreg Kroah-Hartman #endif 51ab4382d2SGreg Kroah-Hartman 52ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE 512 53ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */ 54ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */ 55ab4382d2SGreg Kroah-Hartman 56ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 57ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ 58ab4382d2SGreg Kroah-Hartman #endif 59ab4382d2SGreg Kroah-Hartman 60ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 61ab4382d2SGreg Kroah-Hartman 62ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port); 63ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port); 64ab4382d2SGreg Kroah-Hartman 65ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT 66ab4382d2SGreg Kroah-Hartman 67ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we 68ab4382d2SGreg Kroah-Hartman * should coexist with the 8250 driver, such as if we have an external 16C550 69ab4382d2SGreg Kroah-Hartman * UART. */ 70ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR 204 71ab4382d2SGreg Kroah-Hartman #define MINOR_START 154 72ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyAT" 73ab4382d2SGreg Kroah-Hartman 74ab4382d2SGreg Kroah-Hartman #else 75ab4382d2SGreg Kroah-Hartman 76ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port 77ab4382d2SGreg Kroah-Hartman * name, but it is legally reserved for the 8250 driver. */ 78ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR TTY_MAJOR 79ab4382d2SGreg Kroah-Hartman #define MINOR_START 64 80ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyS" 81ab4382d2SGreg Kroah-Hartman 82ab4382d2SGreg Kroah-Hartman #endif 83ab4382d2SGreg Kroah-Hartman 84ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT 256 85ab4382d2SGreg Kroah-Hartman 86ab4382d2SGreg Kroah-Hartman /* UART registers. CR is write-only, hence no GET macro */ 87ab4382d2SGreg Kroah-Hartman #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR) 88ab4382d2SGreg Kroah-Hartman #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR) 89ab4382d2SGreg Kroah-Hartman #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR) 90ab4382d2SGreg Kroah-Hartman #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER) 91ab4382d2SGreg Kroah-Hartman #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR) 92ab4382d2SGreg Kroah-Hartman #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR) 93ab4382d2SGreg Kroah-Hartman #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR) 94ab4382d2SGreg Kroah-Hartman #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR) 95ab4382d2SGreg Kroah-Hartman #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR) 96ab4382d2SGreg Kroah-Hartman #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR) 97ab4382d2SGreg Kroah-Hartman #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR) 98ab4382d2SGreg Kroah-Hartman #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR) 99ab4382d2SGreg Kroah-Hartman #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR) 100ab4382d2SGreg Kroah-Hartman 101ab4382d2SGreg Kroah-Hartman /* PDC registers */ 102ab4382d2SGreg Kroah-Hartman #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR) 103ab4382d2SGreg Kroah-Hartman #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR) 104ab4382d2SGreg Kroah-Hartman 105ab4382d2SGreg Kroah-Hartman #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR) 106ab4382d2SGreg Kroah-Hartman #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR) 107ab4382d2SGreg Kroah-Hartman #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR) 108ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR) 109ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR) 110ab4382d2SGreg Kroah-Hartman 111ab4382d2SGreg Kroah-Hartman #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR) 112ab4382d2SGreg Kroah-Hartman #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR) 113ab4382d2SGreg Kroah-Hartman #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR) 114ab4382d2SGreg Kroah-Hartman 115ab4382d2SGreg Kroah-Hartman static int (*atmel_open_hook)(struct uart_port *); 116ab4382d2SGreg Kroah-Hartman static void (*atmel_close_hook)(struct uart_port *); 117ab4382d2SGreg Kroah-Hartman 118ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer { 119ab4382d2SGreg Kroah-Hartman unsigned char *buf; 120ab4382d2SGreg Kroah-Hartman dma_addr_t dma_addr; 121ab4382d2SGreg Kroah-Hartman unsigned int dma_size; 122ab4382d2SGreg Kroah-Hartman unsigned int ofs; 123ab4382d2SGreg Kroah-Hartman }; 124ab4382d2SGreg Kroah-Hartman 125ab4382d2SGreg Kroah-Hartman struct atmel_uart_char { 126ab4382d2SGreg Kroah-Hartman u16 status; 127ab4382d2SGreg Kroah-Hartman u16 ch; 128ab4382d2SGreg Kroah-Hartman }; 129ab4382d2SGreg Kroah-Hartman 130ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024 131ab4382d2SGreg Kroah-Hartman 132ab4382d2SGreg Kroah-Hartman /* 133ab4382d2SGreg Kroah-Hartman * We wrap our port structure around the generic uart_port. 134ab4382d2SGreg Kroah-Hartman */ 135ab4382d2SGreg Kroah-Hartman struct atmel_uart_port { 136ab4382d2SGreg Kroah-Hartman struct uart_port uart; /* uart */ 137ab4382d2SGreg Kroah-Hartman struct clk *clk; /* uart clock */ 138ab4382d2SGreg Kroah-Hartman int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */ 139ab4382d2SGreg Kroah-Hartman u32 backup_imr; /* IMR saved during suspend */ 140ab4382d2SGreg Kroah-Hartman int break_active; /* break being received */ 141ab4382d2SGreg Kroah-Hartman 142ab4382d2SGreg Kroah-Hartman short use_dma_rx; /* enable PDC receiver */ 143ab4382d2SGreg Kroah-Hartman short pdc_rx_idx; /* current PDC RX buffer */ 144ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ 145ab4382d2SGreg Kroah-Hartman 146ab4382d2SGreg Kroah-Hartman short use_dma_tx; /* enable PDC transmitter */ 147ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ 148ab4382d2SGreg Kroah-Hartman 149ab4382d2SGreg Kroah-Hartman struct tasklet_struct tasklet; 150ab4382d2SGreg Kroah-Hartman unsigned int irq_status; 151ab4382d2SGreg Kroah-Hartman unsigned int irq_status_prev; 152ab4382d2SGreg Kroah-Hartman 153ab4382d2SGreg Kroah-Hartman struct circ_buf rx_ring; 154ab4382d2SGreg Kroah-Hartman 155ab4382d2SGreg Kroah-Hartman struct serial_rs485 rs485; /* rs485 settings */ 156ab4382d2SGreg Kroah-Hartman unsigned int tx_done_mask; 157ab4382d2SGreg Kroah-Hartman }; 158ab4382d2SGreg Kroah-Hartman 159ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; 160ab4382d2SGreg Kroah-Hartman 161ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ 162ab4382d2SGreg Kroah-Hartman static struct console atmel_console; 163ab4382d2SGreg Kroah-Hartman #endif 164ab4382d2SGreg Kroah-Hartman 165ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port * 166ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart) 167ab4382d2SGreg Kroah-Hartman { 168ab4382d2SGreg Kroah-Hartman return container_of(uart, struct atmel_uart_port, uart); 169ab4382d2SGreg Kroah-Hartman } 170ab4382d2SGreg Kroah-Hartman 171ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC 172ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_rx(struct uart_port *port) 173ab4382d2SGreg Kroah-Hartman { 174ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 175ab4382d2SGreg Kroah-Hartman 176ab4382d2SGreg Kroah-Hartman return atmel_port->use_dma_rx; 177ab4382d2SGreg Kroah-Hartman } 178ab4382d2SGreg Kroah-Hartman 179ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_tx(struct uart_port *port) 180ab4382d2SGreg Kroah-Hartman { 181ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 182ab4382d2SGreg Kroah-Hartman 183ab4382d2SGreg Kroah-Hartman return atmel_port->use_dma_tx; 184ab4382d2SGreg Kroah-Hartman } 185ab4382d2SGreg Kroah-Hartman #else 186ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_rx(struct uart_port *port) 187ab4382d2SGreg Kroah-Hartman { 188ab4382d2SGreg Kroah-Hartman return false; 189ab4382d2SGreg Kroah-Hartman } 190ab4382d2SGreg Kroah-Hartman 191ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_tx(struct uart_port *port) 192ab4382d2SGreg Kroah-Hartman { 193ab4382d2SGreg Kroah-Hartman return false; 194ab4382d2SGreg Kroah-Hartman } 195ab4382d2SGreg Kroah-Hartman #endif 196ab4382d2SGreg Kroah-Hartman 197ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */ 198ab4382d2SGreg Kroah-Hartman void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf) 199ab4382d2SGreg Kroah-Hartman { 200ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 201ab4382d2SGreg Kroah-Hartman unsigned int mode; 202ab4382d2SGreg Kroah-Hartman 203ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 204ab4382d2SGreg Kroah-Hartman 205ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 206ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, atmel_port->tx_done_mask); 207ab4382d2SGreg Kroah-Hartman 208ab4382d2SGreg Kroah-Hartman mode = UART_GET_MR(port); 209ab4382d2SGreg Kroah-Hartman 210ab4382d2SGreg Kroah-Hartman /* Resetting serial mode to RS232 (0x0) */ 211ab4382d2SGreg Kroah-Hartman mode &= ~ATMEL_US_USMODE; 212ab4382d2SGreg Kroah-Hartman 213ab4382d2SGreg Kroah-Hartman atmel_port->rs485 = *rs485conf; 214ab4382d2SGreg Kroah-Hartman 215ab4382d2SGreg Kroah-Hartman if (rs485conf->flags & SER_RS485_ENABLED) { 216ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS485\n"); 217ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 218ab4382d2SGreg Kroah-Hartman if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND) 219ab4382d2SGreg Kroah-Hartman UART_PUT_TTGR(port, rs485conf->delay_rts_after_send); 220ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 221ab4382d2SGreg Kroah-Hartman } else { 222ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS232\n"); 223ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_tx(port)) 224ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | 225ab4382d2SGreg Kroah-Hartman ATMEL_US_TXBUFE; 226ab4382d2SGreg Kroah-Hartman else 227ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 228ab4382d2SGreg Kroah-Hartman } 229ab4382d2SGreg Kroah-Hartman UART_PUT_MR(port, mode); 230ab4382d2SGreg Kroah-Hartman 231ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 232ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 233ab4382d2SGreg Kroah-Hartman 234ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 235ab4382d2SGreg Kroah-Hartman 236ab4382d2SGreg Kroah-Hartman } 237ab4382d2SGreg Kroah-Hartman 238ab4382d2SGreg Kroah-Hartman /* 239ab4382d2SGreg Kroah-Hartman * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. 240ab4382d2SGreg Kroah-Hartman */ 241ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port) 242ab4382d2SGreg Kroah-Hartman { 243ab4382d2SGreg Kroah-Hartman return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0; 244ab4382d2SGreg Kroah-Hartman } 245ab4382d2SGreg Kroah-Hartman 246ab4382d2SGreg Kroah-Hartman /* 247ab4382d2SGreg Kroah-Hartman * Set state of the modem control output lines 248ab4382d2SGreg Kroah-Hartman */ 249ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) 250ab4382d2SGreg Kroah-Hartman { 251ab4382d2SGreg Kroah-Hartman unsigned int control = 0; 252ab4382d2SGreg Kroah-Hartman unsigned int mode; 253ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 254ab4382d2SGreg Kroah-Hartman 255ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91RM9200 256ab4382d2SGreg Kroah-Hartman if (cpu_is_at91rm9200()) { 257ab4382d2SGreg Kroah-Hartman /* 258ab4382d2SGreg Kroah-Hartman * AT91RM9200 Errata #39: RTS0 is not internally connected 259ab4382d2SGreg Kroah-Hartman * to PA21. We need to drive the pin manually. 260ab4382d2SGreg Kroah-Hartman */ 261ab4382d2SGreg Kroah-Hartman if (port->mapbase == AT91RM9200_BASE_US0) { 262ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_RTS) 263ab4382d2SGreg Kroah-Hartman at91_set_gpio_value(AT91_PIN_PA21, 0); 264ab4382d2SGreg Kroah-Hartman else 265ab4382d2SGreg Kroah-Hartman at91_set_gpio_value(AT91_PIN_PA21, 1); 266ab4382d2SGreg Kroah-Hartman } 267ab4382d2SGreg Kroah-Hartman } 268ab4382d2SGreg Kroah-Hartman #endif 269ab4382d2SGreg Kroah-Hartman 270ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_RTS) 271ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_RTSEN; 272ab4382d2SGreg Kroah-Hartman else 273ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_RTSDIS; 274ab4382d2SGreg Kroah-Hartman 275ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_DTR) 276ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTREN; 277ab4382d2SGreg Kroah-Hartman else 278ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTRDIS; 279ab4382d2SGreg Kroah-Hartman 280ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, control); 281ab4382d2SGreg Kroah-Hartman 282ab4382d2SGreg Kroah-Hartman /* Local loopback mode? */ 283ab4382d2SGreg Kroah-Hartman mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE; 284ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_LOOP) 285ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_LOC_LOOP; 286ab4382d2SGreg Kroah-Hartman else 287ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_NORMAL; 288ab4382d2SGreg Kroah-Hartman 289ab4382d2SGreg Kroah-Hartman /* Resetting serial mode to RS232 (0x0) */ 290ab4382d2SGreg Kroah-Hartman mode &= ~ATMEL_US_USMODE; 291ab4382d2SGreg Kroah-Hartman 292ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_ENABLED) { 293ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS485\n"); 294ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND) 295ab4382d2SGreg Kroah-Hartman UART_PUT_TTGR(port, 296ab4382d2SGreg Kroah-Hartman atmel_port->rs485.delay_rts_after_send); 297ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 298ab4382d2SGreg Kroah-Hartman } else { 299ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS232\n"); 300ab4382d2SGreg Kroah-Hartman } 301ab4382d2SGreg Kroah-Hartman UART_PUT_MR(port, mode); 302ab4382d2SGreg Kroah-Hartman } 303ab4382d2SGreg Kroah-Hartman 304ab4382d2SGreg Kroah-Hartman /* 305ab4382d2SGreg Kroah-Hartman * Get state of the modem control input lines 306ab4382d2SGreg Kroah-Hartman */ 307ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port) 308ab4382d2SGreg Kroah-Hartman { 309ab4382d2SGreg Kroah-Hartman unsigned int status, ret = 0; 310ab4382d2SGreg Kroah-Hartman 311ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 312ab4382d2SGreg Kroah-Hartman 313ab4382d2SGreg Kroah-Hartman /* 314ab4382d2SGreg Kroah-Hartman * The control signals are active low. 315ab4382d2SGreg Kroah-Hartman */ 316ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DCD)) 317ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CD; 318ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_CTS)) 319ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CTS; 320ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DSR)) 321ab4382d2SGreg Kroah-Hartman ret |= TIOCM_DSR; 322ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_RI)) 323ab4382d2SGreg Kroah-Hartman ret |= TIOCM_RI; 324ab4382d2SGreg Kroah-Hartman 325ab4382d2SGreg Kroah-Hartman return ret; 326ab4382d2SGreg Kroah-Hartman } 327ab4382d2SGreg Kroah-Hartman 328ab4382d2SGreg Kroah-Hartman /* 329ab4382d2SGreg Kroah-Hartman * Stop transmitting. 330ab4382d2SGreg Kroah-Hartman */ 331ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port) 332ab4382d2SGreg Kroah-Hartman { 333ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 334ab4382d2SGreg Kroah-Hartman 335ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_tx(port)) { 336ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 337ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 338ab4382d2SGreg Kroah-Hartman } 339ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 340ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, atmel_port->tx_done_mask); 341ab4382d2SGreg Kroah-Hartman 342ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_ENABLED) 343ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 344ab4382d2SGreg Kroah-Hartman } 345ab4382d2SGreg Kroah-Hartman 346ab4382d2SGreg Kroah-Hartman /* 347ab4382d2SGreg Kroah-Hartman * Start transmitting. 348ab4382d2SGreg Kroah-Hartman */ 349ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port) 350ab4382d2SGreg Kroah-Hartman { 351ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 352ab4382d2SGreg Kroah-Hartman 353ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_tx(port)) { 354ab4382d2SGreg Kroah-Hartman if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN) 355ab4382d2SGreg Kroah-Hartman /* The transmitter is already running. Yes, we 356ab4382d2SGreg Kroah-Hartman really need this.*/ 357ab4382d2SGreg Kroah-Hartman return; 358ab4382d2SGreg Kroah-Hartman 359ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_ENABLED) 360ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 361ab4382d2SGreg Kroah-Hartman 362ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 363ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); 364ab4382d2SGreg Kroah-Hartman } 365ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 366ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 367ab4382d2SGreg Kroah-Hartman } 368ab4382d2SGreg Kroah-Hartman 369ab4382d2SGreg Kroah-Hartman /* 370ab4382d2SGreg Kroah-Hartman * start receiving - port is in process of being opened. 371ab4382d2SGreg Kroah-Hartman */ 372ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port) 373ab4382d2SGreg Kroah-Hartman { 374ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */ 375ab4382d2SGreg Kroah-Hartman 376ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_rx(port)) { 377ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 378ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 379ab4382d2SGreg Kroah-Hartman port->read_status_mask); 380ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); 381ab4382d2SGreg Kroah-Hartman } else { 382ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_RXRDY); 383ab4382d2SGreg Kroah-Hartman } 384ab4382d2SGreg Kroah-Hartman } 385ab4382d2SGreg Kroah-Hartman 386ab4382d2SGreg Kroah-Hartman /* 387ab4382d2SGreg Kroah-Hartman * Stop receiving - port is in process of being closed. 388ab4382d2SGreg Kroah-Hartman */ 389ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port) 390ab4382d2SGreg Kroah-Hartman { 391ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_rx(port)) { 392ab4382d2SGreg Kroah-Hartman /* disable PDC receive */ 393ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS); 394ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 395ab4382d2SGreg Kroah-Hartman port->read_status_mask); 396ab4382d2SGreg Kroah-Hartman } else { 397ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXRDY); 398ab4382d2SGreg Kroah-Hartman } 399ab4382d2SGreg Kroah-Hartman } 400ab4382d2SGreg Kroah-Hartman 401ab4382d2SGreg Kroah-Hartman /* 402ab4382d2SGreg Kroah-Hartman * Enable modem status interrupts 403ab4382d2SGreg Kroah-Hartman */ 404ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port) 405ab4382d2SGreg Kroah-Hartman { 406ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC 407ab4382d2SGreg Kroah-Hartman | ATMEL_US_DCDIC | ATMEL_US_CTSIC); 408ab4382d2SGreg Kroah-Hartman } 409ab4382d2SGreg Kroah-Hartman 410ab4382d2SGreg Kroah-Hartman /* 411ab4382d2SGreg Kroah-Hartman * Control the transmission of a break signal 412ab4382d2SGreg Kroah-Hartman */ 413ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state) 414ab4382d2SGreg Kroah-Hartman { 415ab4382d2SGreg Kroah-Hartman if (break_state != 0) 416ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */ 417ab4382d2SGreg Kroah-Hartman else 418ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */ 419ab4382d2SGreg Kroah-Hartman } 420ab4382d2SGreg Kroah-Hartman 421ab4382d2SGreg Kroah-Hartman /* 422ab4382d2SGreg Kroah-Hartman * Stores the incoming character in the ring buffer 423ab4382d2SGreg Kroah-Hartman */ 424ab4382d2SGreg Kroah-Hartman static void 425ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status, 426ab4382d2SGreg Kroah-Hartman unsigned int ch) 427ab4382d2SGreg Kroah-Hartman { 428ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 429ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 430ab4382d2SGreg Kroah-Hartman struct atmel_uart_char *c; 431ab4382d2SGreg Kroah-Hartman 432ab4382d2SGreg Kroah-Hartman if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) 433ab4382d2SGreg Kroah-Hartman /* Buffer overflow, ignore char */ 434ab4382d2SGreg Kroah-Hartman return; 435ab4382d2SGreg Kroah-Hartman 436ab4382d2SGreg Kroah-Hartman c = &((struct atmel_uart_char *)ring->buf)[ring->head]; 437ab4382d2SGreg Kroah-Hartman c->status = status; 438ab4382d2SGreg Kroah-Hartman c->ch = ch; 439ab4382d2SGreg Kroah-Hartman 440ab4382d2SGreg Kroah-Hartman /* Make sure the character is stored before we update head. */ 441ab4382d2SGreg Kroah-Hartman smp_wmb(); 442ab4382d2SGreg Kroah-Hartman 443ab4382d2SGreg Kroah-Hartman ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 444ab4382d2SGreg Kroah-Hartman } 445ab4382d2SGreg Kroah-Hartman 446ab4382d2SGreg Kroah-Hartman /* 447ab4382d2SGreg Kroah-Hartman * Deal with parity, framing and overrun errors. 448ab4382d2SGreg Kroah-Hartman */ 449ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status) 450ab4382d2SGreg Kroah-Hartman { 451ab4382d2SGreg Kroah-Hartman /* clear error */ 452ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); 453ab4382d2SGreg Kroah-Hartman 454ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 455ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 456ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 457ab4382d2SGreg Kroah-Hartman port->icount.brk++; 458ab4382d2SGreg Kroah-Hartman } 459ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 460ab4382d2SGreg Kroah-Hartman port->icount.parity++; 461ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 462ab4382d2SGreg Kroah-Hartman port->icount.frame++; 463ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 464ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 465ab4382d2SGreg Kroah-Hartman } 466ab4382d2SGreg Kroah-Hartman 467ab4382d2SGreg Kroah-Hartman /* 468ab4382d2SGreg Kroah-Hartman * Characters received (called from interrupt handler) 469ab4382d2SGreg Kroah-Hartman */ 470ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port) 471ab4382d2SGreg Kroah-Hartman { 472ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 473ab4382d2SGreg Kroah-Hartman unsigned int status, ch; 474ab4382d2SGreg Kroah-Hartman 475ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 476ab4382d2SGreg Kroah-Hartman while (status & ATMEL_US_RXRDY) { 477ab4382d2SGreg Kroah-Hartman ch = UART_GET_CHAR(port); 478ab4382d2SGreg Kroah-Hartman 479ab4382d2SGreg Kroah-Hartman /* 480ab4382d2SGreg Kroah-Hartman * note that the error handling code is 481ab4382d2SGreg Kroah-Hartman * out of the main execution path 482ab4382d2SGreg Kroah-Hartman */ 483ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 484ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK) 485ab4382d2SGreg Kroah-Hartman || atmel_port->break_active)) { 486ab4382d2SGreg Kroah-Hartman 487ab4382d2SGreg Kroah-Hartman /* clear error */ 488ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); 489ab4382d2SGreg Kroah-Hartman 490ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK 491ab4382d2SGreg Kroah-Hartman && !atmel_port->break_active) { 492ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 1; 493ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_RXBRK); 494ab4382d2SGreg Kroah-Hartman } else { 495ab4382d2SGreg Kroah-Hartman /* 496ab4382d2SGreg Kroah-Hartman * This is either the end-of-break 497ab4382d2SGreg Kroah-Hartman * condition or we've received at 498ab4382d2SGreg Kroah-Hartman * least one character without RXBRK 499ab4382d2SGreg Kroah-Hartman * being set. In both cases, the next 500ab4382d2SGreg Kroah-Hartman * RXBRK will indicate start-of-break. 501ab4382d2SGreg Kroah-Hartman */ 502ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXBRK); 503ab4382d2SGreg Kroah-Hartman status &= ~ATMEL_US_RXBRK; 504ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 505ab4382d2SGreg Kroah-Hartman } 506ab4382d2SGreg Kroah-Hartman } 507ab4382d2SGreg Kroah-Hartman 508ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(port, status, ch); 509ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 510ab4382d2SGreg Kroah-Hartman } 511ab4382d2SGreg Kroah-Hartman 512ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 513ab4382d2SGreg Kroah-Hartman } 514ab4382d2SGreg Kroah-Hartman 515ab4382d2SGreg Kroah-Hartman /* 516ab4382d2SGreg Kroah-Hartman * Transmit characters (called from tasklet with TXRDY interrupt 517ab4382d2SGreg Kroah-Hartman * disabled) 518ab4382d2SGreg Kroah-Hartman */ 519ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port) 520ab4382d2SGreg Kroah-Hartman { 521ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 522ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 523ab4382d2SGreg Kroah-Hartman 524ab4382d2SGreg Kroah-Hartman if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) { 525ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, port->x_char); 526ab4382d2SGreg Kroah-Hartman port->icount.tx++; 527ab4382d2SGreg Kroah-Hartman port->x_char = 0; 528ab4382d2SGreg Kroah-Hartman } 529ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 530ab4382d2SGreg Kroah-Hartman return; 531ab4382d2SGreg Kroah-Hartman 532ab4382d2SGreg Kroah-Hartman while (UART_GET_CSR(port) & atmel_port->tx_done_mask) { 533ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, xmit->buf[xmit->tail]); 534ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 535ab4382d2SGreg Kroah-Hartman port->icount.tx++; 536ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit)) 537ab4382d2SGreg Kroah-Hartman break; 538ab4382d2SGreg Kroah-Hartman } 539ab4382d2SGreg Kroah-Hartman 540ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 541ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 542ab4382d2SGreg Kroah-Hartman 543ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit)) 544ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 545ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 546ab4382d2SGreg Kroah-Hartman } 547ab4382d2SGreg Kroah-Hartman 548ab4382d2SGreg Kroah-Hartman /* 549ab4382d2SGreg Kroah-Hartman * receive interrupt handler. 550ab4382d2SGreg Kroah-Hartman */ 551ab4382d2SGreg Kroah-Hartman static void 552ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending) 553ab4382d2SGreg Kroah-Hartman { 554ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 555ab4382d2SGreg Kroah-Hartman 556ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_rx(port)) { 557ab4382d2SGreg Kroah-Hartman /* 558ab4382d2SGreg Kroah-Hartman * PDC receive. Just schedule the tasklet and let it 559ab4382d2SGreg Kroah-Hartman * figure out the details. 560ab4382d2SGreg Kroah-Hartman * 561ab4382d2SGreg Kroah-Hartman * TODO: We're not handling error flags correctly at 562ab4382d2SGreg Kroah-Hartman * the moment. 563ab4382d2SGreg Kroah-Hartman */ 564ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) { 565ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, (ATMEL_US_ENDRX 566ab4382d2SGreg Kroah-Hartman | ATMEL_US_TIMEOUT)); 567ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 568ab4382d2SGreg Kroah-Hartman } 569ab4382d2SGreg Kroah-Hartman 570ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE | 571ab4382d2SGreg Kroah-Hartman ATMEL_US_FRAME | ATMEL_US_PARE)) 572ab4382d2SGreg Kroah-Hartman atmel_pdc_rxerr(port, pending); 573ab4382d2SGreg Kroah-Hartman } 574ab4382d2SGreg Kroah-Hartman 575ab4382d2SGreg Kroah-Hartman /* Interrupt receive */ 576ab4382d2SGreg Kroah-Hartman if (pending & ATMEL_US_RXRDY) 577ab4382d2SGreg Kroah-Hartman atmel_rx_chars(port); 578ab4382d2SGreg Kroah-Hartman else if (pending & ATMEL_US_RXBRK) { 579ab4382d2SGreg Kroah-Hartman /* 580ab4382d2SGreg Kroah-Hartman * End of break detected. If it came along with a 581ab4382d2SGreg Kroah-Hartman * character, atmel_rx_chars will handle it. 582ab4382d2SGreg Kroah-Hartman */ 583ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); 584ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXBRK); 585ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 586ab4382d2SGreg Kroah-Hartman } 587ab4382d2SGreg Kroah-Hartman } 588ab4382d2SGreg Kroah-Hartman 589ab4382d2SGreg Kroah-Hartman /* 590ab4382d2SGreg Kroah-Hartman * transmit interrupt handler. (Transmit is IRQF_NODELAY safe) 591ab4382d2SGreg Kroah-Hartman */ 592ab4382d2SGreg Kroah-Hartman static void 593ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending) 594ab4382d2SGreg Kroah-Hartman { 595ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 596ab4382d2SGreg Kroah-Hartman 597ab4382d2SGreg Kroah-Hartman if (pending & atmel_port->tx_done_mask) { 598ab4382d2SGreg Kroah-Hartman /* Either PDC or interrupt transmission */ 599ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, atmel_port->tx_done_mask); 600ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 601ab4382d2SGreg Kroah-Hartman } 602ab4382d2SGreg Kroah-Hartman } 603ab4382d2SGreg Kroah-Hartman 604ab4382d2SGreg Kroah-Hartman /* 605ab4382d2SGreg Kroah-Hartman * status flags interrupt handler. 606ab4382d2SGreg Kroah-Hartman */ 607ab4382d2SGreg Kroah-Hartman static void 608ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending, 609ab4382d2SGreg Kroah-Hartman unsigned int status) 610ab4382d2SGreg Kroah-Hartman { 611ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 612ab4382d2SGreg Kroah-Hartman 613ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC 614ab4382d2SGreg Kroah-Hartman | ATMEL_US_CTSIC)) { 615ab4382d2SGreg Kroah-Hartman atmel_port->irq_status = status; 616ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 617ab4382d2SGreg Kroah-Hartman } 618ab4382d2SGreg Kroah-Hartman } 619ab4382d2SGreg Kroah-Hartman 620ab4382d2SGreg Kroah-Hartman /* 621ab4382d2SGreg Kroah-Hartman * Interrupt handler 622ab4382d2SGreg Kroah-Hartman */ 623ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id) 624ab4382d2SGreg Kroah-Hartman { 625ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 626ab4382d2SGreg Kroah-Hartman unsigned int status, pending, pass_counter = 0; 627ab4382d2SGreg Kroah-Hartman 628ab4382d2SGreg Kroah-Hartman do { 629ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 630ab4382d2SGreg Kroah-Hartman pending = status & UART_GET_IMR(port); 631ab4382d2SGreg Kroah-Hartman if (!pending) 632ab4382d2SGreg Kroah-Hartman break; 633ab4382d2SGreg Kroah-Hartman 634ab4382d2SGreg Kroah-Hartman atmel_handle_receive(port, pending); 635ab4382d2SGreg Kroah-Hartman atmel_handle_status(port, pending, status); 636ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(port, pending); 637ab4382d2SGreg Kroah-Hartman } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT); 638ab4382d2SGreg Kroah-Hartman 639ab4382d2SGreg Kroah-Hartman return pass_counter ? IRQ_HANDLED : IRQ_NONE; 640ab4382d2SGreg Kroah-Hartman } 641ab4382d2SGreg Kroah-Hartman 642ab4382d2SGreg Kroah-Hartman /* 643ab4382d2SGreg Kroah-Hartman * Called from tasklet with ENDTX and TXBUFE interrupts disabled. 644ab4382d2SGreg Kroah-Hartman */ 645ab4382d2SGreg Kroah-Hartman static void atmel_tx_dma(struct uart_port *port) 646ab4382d2SGreg Kroah-Hartman { 647ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 648ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 649ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 650ab4382d2SGreg Kroah-Hartman int count; 651ab4382d2SGreg Kroah-Hartman 652ab4382d2SGreg Kroah-Hartman /* nothing left to transmit? */ 653ab4382d2SGreg Kroah-Hartman if (UART_GET_TCR(port)) 654ab4382d2SGreg Kroah-Hartman return; 655ab4382d2SGreg Kroah-Hartman 656ab4382d2SGreg Kroah-Hartman xmit->tail += pdc->ofs; 657ab4382d2SGreg Kroah-Hartman xmit->tail &= UART_XMIT_SIZE - 1; 658ab4382d2SGreg Kroah-Hartman 659ab4382d2SGreg Kroah-Hartman port->icount.tx += pdc->ofs; 660ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 661ab4382d2SGreg Kroah-Hartman 662ab4382d2SGreg Kroah-Hartman /* more to transmit - setup next transfer */ 663ab4382d2SGreg Kroah-Hartman 664ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 665ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 666ab4382d2SGreg Kroah-Hartman 667ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 668ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, 669ab4382d2SGreg Kroah-Hartman pdc->dma_addr, 670ab4382d2SGreg Kroah-Hartman pdc->dma_size, 671ab4382d2SGreg Kroah-Hartman DMA_TO_DEVICE); 672ab4382d2SGreg Kroah-Hartman 673ab4382d2SGreg Kroah-Hartman count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 674ab4382d2SGreg Kroah-Hartman pdc->ofs = count; 675ab4382d2SGreg Kroah-Hartman 676ab4382d2SGreg Kroah-Hartman UART_PUT_TPR(port, pdc->dma_addr + xmit->tail); 677ab4382d2SGreg Kroah-Hartman UART_PUT_TCR(port, count); 678ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 679ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); 680ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 681ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 682ab4382d2SGreg Kroah-Hartman } else { 683ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_ENABLED) { 684ab4382d2SGreg Kroah-Hartman /* DMA done, stop TX, start RX for RS485 */ 685ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 686ab4382d2SGreg Kroah-Hartman } 687ab4382d2SGreg Kroah-Hartman } 688ab4382d2SGreg Kroah-Hartman 689ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 690ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 691ab4382d2SGreg Kroah-Hartman } 692ab4382d2SGreg Kroah-Hartman 693ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port) 694ab4382d2SGreg Kroah-Hartman { 695ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 696ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 697ab4382d2SGreg Kroah-Hartman unsigned int flg; 698ab4382d2SGreg Kroah-Hartman unsigned int status; 699ab4382d2SGreg Kroah-Hartman 700ab4382d2SGreg Kroah-Hartman while (ring->head != ring->tail) { 701ab4382d2SGreg Kroah-Hartman struct atmel_uart_char c; 702ab4382d2SGreg Kroah-Hartman 703ab4382d2SGreg Kroah-Hartman /* Make sure c is loaded after head. */ 704ab4382d2SGreg Kroah-Hartman smp_rmb(); 705ab4382d2SGreg Kroah-Hartman 706ab4382d2SGreg Kroah-Hartman c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; 707ab4382d2SGreg Kroah-Hartman 708ab4382d2SGreg Kroah-Hartman ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 709ab4382d2SGreg Kroah-Hartman 710ab4382d2SGreg Kroah-Hartman port->icount.rx++; 711ab4382d2SGreg Kroah-Hartman status = c.status; 712ab4382d2SGreg Kroah-Hartman flg = TTY_NORMAL; 713ab4382d2SGreg Kroah-Hartman 714ab4382d2SGreg Kroah-Hartman /* 715ab4382d2SGreg Kroah-Hartman * note that the error handling code is 716ab4382d2SGreg Kroah-Hartman * out of the main execution path 717ab4382d2SGreg Kroah-Hartman */ 718ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 719ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK))) { 720ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 721ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 722ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 723ab4382d2SGreg Kroah-Hartman 724ab4382d2SGreg Kroah-Hartman port->icount.brk++; 725ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 726ab4382d2SGreg Kroah-Hartman continue; 727ab4382d2SGreg Kroah-Hartman } 728ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 729ab4382d2SGreg Kroah-Hartman port->icount.parity++; 730ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 731ab4382d2SGreg Kroah-Hartman port->icount.frame++; 732ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 733ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 734ab4382d2SGreg Kroah-Hartman 735ab4382d2SGreg Kroah-Hartman status &= port->read_status_mask; 736ab4382d2SGreg Kroah-Hartman 737ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) 738ab4382d2SGreg Kroah-Hartman flg = TTY_BREAK; 739ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_PARE) 740ab4382d2SGreg Kroah-Hartman flg = TTY_PARITY; 741ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_FRAME) 742ab4382d2SGreg Kroah-Hartman flg = TTY_FRAME; 743ab4382d2SGreg Kroah-Hartman } 744ab4382d2SGreg Kroah-Hartman 745ab4382d2SGreg Kroah-Hartman 746ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c.ch)) 747ab4382d2SGreg Kroah-Hartman continue; 748ab4382d2SGreg Kroah-Hartman 749ab4382d2SGreg Kroah-Hartman uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg); 750ab4382d2SGreg Kroah-Hartman } 751ab4382d2SGreg Kroah-Hartman 752ab4382d2SGreg Kroah-Hartman /* 753ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 754ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 755ab4382d2SGreg Kroah-Hartman */ 756ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 757ab4382d2SGreg Kroah-Hartman tty_flip_buffer_push(port->state->port.tty); 758ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 759ab4382d2SGreg Kroah-Hartman } 760ab4382d2SGreg Kroah-Hartman 761ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_dma(struct uart_port *port) 762ab4382d2SGreg Kroah-Hartman { 763ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 764ab4382d2SGreg Kroah-Hartman struct tty_struct *tty = port->state->port.tty; 765ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc; 766ab4382d2SGreg Kroah-Hartman int rx_idx = atmel_port->pdc_rx_idx; 767ab4382d2SGreg Kroah-Hartman unsigned int head; 768ab4382d2SGreg Kroah-Hartman unsigned int tail; 769ab4382d2SGreg Kroah-Hartman unsigned int count; 770ab4382d2SGreg Kroah-Hartman 771ab4382d2SGreg Kroah-Hartman do { 772ab4382d2SGreg Kroah-Hartman /* Reset the UART timeout early so that we don't miss one */ 773ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STTTO); 774ab4382d2SGreg Kroah-Hartman 775ab4382d2SGreg Kroah-Hartman pdc = &atmel_port->pdc_rx[rx_idx]; 776ab4382d2SGreg Kroah-Hartman head = UART_GET_RPR(port) - pdc->dma_addr; 777ab4382d2SGreg Kroah-Hartman tail = pdc->ofs; 778ab4382d2SGreg Kroah-Hartman 779ab4382d2SGreg Kroah-Hartman /* If the PDC has switched buffers, RPR won't contain 780ab4382d2SGreg Kroah-Hartman * any address within the current buffer. Since head 781ab4382d2SGreg Kroah-Hartman * is unsigned, we just need a one-way comparison to 782ab4382d2SGreg Kroah-Hartman * find out. 783ab4382d2SGreg Kroah-Hartman * 784ab4382d2SGreg Kroah-Hartman * In this case, we just need to consume the entire 785ab4382d2SGreg Kroah-Hartman * buffer and resubmit it for DMA. This will clear the 786ab4382d2SGreg Kroah-Hartman * ENDRX bit as well, so that we can safely re-enable 787ab4382d2SGreg Kroah-Hartman * all interrupts below. 788ab4382d2SGreg Kroah-Hartman */ 789ab4382d2SGreg Kroah-Hartman head = min(head, pdc->dma_size); 790ab4382d2SGreg Kroah-Hartman 791ab4382d2SGreg Kroah-Hartman if (likely(head != tail)) { 792ab4382d2SGreg Kroah-Hartman dma_sync_single_for_cpu(port->dev, pdc->dma_addr, 793ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 794ab4382d2SGreg Kroah-Hartman 795ab4382d2SGreg Kroah-Hartman /* 796ab4382d2SGreg Kroah-Hartman * head will only wrap around when we recycle 797ab4382d2SGreg Kroah-Hartman * the DMA buffer, and when that happens, we 798ab4382d2SGreg Kroah-Hartman * explicitly set tail to 0. So head will 799ab4382d2SGreg Kroah-Hartman * always be greater than tail. 800ab4382d2SGreg Kroah-Hartman */ 801ab4382d2SGreg Kroah-Hartman count = head - tail; 802ab4382d2SGreg Kroah-Hartman 803ab4382d2SGreg Kroah-Hartman tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count); 804ab4382d2SGreg Kroah-Hartman 805ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, pdc->dma_addr, 806ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 807ab4382d2SGreg Kroah-Hartman 808ab4382d2SGreg Kroah-Hartman port->icount.rx += count; 809ab4382d2SGreg Kroah-Hartman pdc->ofs = head; 810ab4382d2SGreg Kroah-Hartman } 811ab4382d2SGreg Kroah-Hartman 812ab4382d2SGreg Kroah-Hartman /* 813ab4382d2SGreg Kroah-Hartman * If the current buffer is full, we need to check if 814ab4382d2SGreg Kroah-Hartman * the next one contains any additional data. 815ab4382d2SGreg Kroah-Hartman */ 816ab4382d2SGreg Kroah-Hartman if (head >= pdc->dma_size) { 817ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 818ab4382d2SGreg Kroah-Hartman UART_PUT_RNPR(port, pdc->dma_addr); 819ab4382d2SGreg Kroah-Hartman UART_PUT_RNCR(port, pdc->dma_size); 820ab4382d2SGreg Kroah-Hartman 821ab4382d2SGreg Kroah-Hartman rx_idx = !rx_idx; 822ab4382d2SGreg Kroah-Hartman atmel_port->pdc_rx_idx = rx_idx; 823ab4382d2SGreg Kroah-Hartman } 824ab4382d2SGreg Kroah-Hartman } while (head >= pdc->dma_size); 825ab4382d2SGreg Kroah-Hartman 826ab4382d2SGreg Kroah-Hartman /* 827ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 828ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 829ab4382d2SGreg Kroah-Hartman */ 830ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 831ab4382d2SGreg Kroah-Hartman tty_flip_buffer_push(tty); 832ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 833ab4382d2SGreg Kroah-Hartman 834ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 835ab4382d2SGreg Kroah-Hartman } 836ab4382d2SGreg Kroah-Hartman 837ab4382d2SGreg Kroah-Hartman /* 838ab4382d2SGreg Kroah-Hartman * tasklet handling tty stuff outside the interrupt handler. 839ab4382d2SGreg Kroah-Hartman */ 840ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data) 841ab4382d2SGreg Kroah-Hartman { 842ab4382d2SGreg Kroah-Hartman struct uart_port *port = (struct uart_port *)data; 843ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 844ab4382d2SGreg Kroah-Hartman unsigned int status; 845ab4382d2SGreg Kroah-Hartman unsigned int status_change; 846ab4382d2SGreg Kroah-Hartman 847ab4382d2SGreg Kroah-Hartman /* The interrupt handler does not take the lock */ 848ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 849ab4382d2SGreg Kroah-Hartman 850ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_tx(port)) 851ab4382d2SGreg Kroah-Hartman atmel_tx_dma(port); 852ab4382d2SGreg Kroah-Hartman else 853ab4382d2SGreg Kroah-Hartman atmel_tx_chars(port); 854ab4382d2SGreg Kroah-Hartman 855ab4382d2SGreg Kroah-Hartman status = atmel_port->irq_status; 856ab4382d2SGreg Kroah-Hartman status_change = status ^ atmel_port->irq_status_prev; 857ab4382d2SGreg Kroah-Hartman 858ab4382d2SGreg Kroah-Hartman if (status_change & (ATMEL_US_RI | ATMEL_US_DSR 859ab4382d2SGreg Kroah-Hartman | ATMEL_US_DCD | ATMEL_US_CTS)) { 860ab4382d2SGreg Kroah-Hartman /* TODO: All reads to CSR will clear these interrupts! */ 861ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_RI) 862ab4382d2SGreg Kroah-Hartman port->icount.rng++; 863ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_DSR) 864ab4382d2SGreg Kroah-Hartman port->icount.dsr++; 865ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_DCD) 866ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); 867ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_CTS) 868ab4382d2SGreg Kroah-Hartman uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); 869ab4382d2SGreg Kroah-Hartman 870ab4382d2SGreg Kroah-Hartman wake_up_interruptible(&port->state->port.delta_msr_wait); 871ab4382d2SGreg Kroah-Hartman 872ab4382d2SGreg Kroah-Hartman atmel_port->irq_status_prev = status; 873ab4382d2SGreg Kroah-Hartman } 874ab4382d2SGreg Kroah-Hartman 875ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_rx(port)) 876ab4382d2SGreg Kroah-Hartman atmel_rx_from_dma(port); 877ab4382d2SGreg Kroah-Hartman else 878ab4382d2SGreg Kroah-Hartman atmel_rx_from_ring(port); 879ab4382d2SGreg Kroah-Hartman 880ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 881ab4382d2SGreg Kroah-Hartman } 882ab4382d2SGreg Kroah-Hartman 883ab4382d2SGreg Kroah-Hartman /* 884ab4382d2SGreg Kroah-Hartman * Perform initialization and enable port for reception 885ab4382d2SGreg Kroah-Hartman */ 886ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port) 887ab4382d2SGreg Kroah-Hartman { 888ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 889ab4382d2SGreg Kroah-Hartman struct tty_struct *tty = port->state->port.tty; 890ab4382d2SGreg Kroah-Hartman int retval; 891ab4382d2SGreg Kroah-Hartman 892ab4382d2SGreg Kroah-Hartman /* 893ab4382d2SGreg Kroah-Hartman * Ensure that no interrupts are enabled otherwise when 894ab4382d2SGreg Kroah-Hartman * request_irq() is called we could get stuck trying to 895ab4382d2SGreg Kroah-Hartman * handle an unexpected interrupt 896ab4382d2SGreg Kroah-Hartman */ 897ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 898ab4382d2SGreg Kroah-Hartman 899ab4382d2SGreg Kroah-Hartman /* 900ab4382d2SGreg Kroah-Hartman * Allocate the IRQ 901ab4382d2SGreg Kroah-Hartman */ 902ab4382d2SGreg Kroah-Hartman retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, 903ab4382d2SGreg Kroah-Hartman tty ? tty->name : "atmel_serial", port); 904ab4382d2SGreg Kroah-Hartman if (retval) { 905ab4382d2SGreg Kroah-Hartman printk("atmel_serial: atmel_startup - Can't get irq\n"); 906ab4382d2SGreg Kroah-Hartman return retval; 907ab4382d2SGreg Kroah-Hartman } 908ab4382d2SGreg Kroah-Hartman 909ab4382d2SGreg Kroah-Hartman /* 910ab4382d2SGreg Kroah-Hartman * Initialize DMA (if necessary) 911ab4382d2SGreg Kroah-Hartman */ 912ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_rx(port)) { 913ab4382d2SGreg Kroah-Hartman int i; 914ab4382d2SGreg Kroah-Hartman 915ab4382d2SGreg Kroah-Hartman for (i = 0; i < 2; i++) { 916ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 917ab4382d2SGreg Kroah-Hartman 918ab4382d2SGreg Kroah-Hartman pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); 919ab4382d2SGreg Kroah-Hartman if (pdc->buf == NULL) { 920ab4382d2SGreg Kroah-Hartman if (i != 0) { 921ab4382d2SGreg Kroah-Hartman dma_unmap_single(port->dev, 922ab4382d2SGreg Kroah-Hartman atmel_port->pdc_rx[0].dma_addr, 923ab4382d2SGreg Kroah-Hartman PDC_BUFFER_SIZE, 924ab4382d2SGreg Kroah-Hartman DMA_FROM_DEVICE); 925ab4382d2SGreg Kroah-Hartman kfree(atmel_port->pdc_rx[0].buf); 926ab4382d2SGreg Kroah-Hartman } 927ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 928ab4382d2SGreg Kroah-Hartman return -ENOMEM; 929ab4382d2SGreg Kroah-Hartman } 930ab4382d2SGreg Kroah-Hartman pdc->dma_addr = dma_map_single(port->dev, 931ab4382d2SGreg Kroah-Hartman pdc->buf, 932ab4382d2SGreg Kroah-Hartman PDC_BUFFER_SIZE, 933ab4382d2SGreg Kroah-Hartman DMA_FROM_DEVICE); 934ab4382d2SGreg Kroah-Hartman pdc->dma_size = PDC_BUFFER_SIZE; 935ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 936ab4382d2SGreg Kroah-Hartman } 937ab4382d2SGreg Kroah-Hartman 938ab4382d2SGreg Kroah-Hartman atmel_port->pdc_rx_idx = 0; 939ab4382d2SGreg Kroah-Hartman 940ab4382d2SGreg Kroah-Hartman UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr); 941ab4382d2SGreg Kroah-Hartman UART_PUT_RCR(port, PDC_BUFFER_SIZE); 942ab4382d2SGreg Kroah-Hartman 943ab4382d2SGreg Kroah-Hartman UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr); 944ab4382d2SGreg Kroah-Hartman UART_PUT_RNCR(port, PDC_BUFFER_SIZE); 945ab4382d2SGreg Kroah-Hartman } 946ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_tx(port)) { 947ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 948ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 949ab4382d2SGreg Kroah-Hartman 950ab4382d2SGreg Kroah-Hartman pdc->buf = xmit->buf; 951ab4382d2SGreg Kroah-Hartman pdc->dma_addr = dma_map_single(port->dev, 952ab4382d2SGreg Kroah-Hartman pdc->buf, 953ab4382d2SGreg Kroah-Hartman UART_XMIT_SIZE, 954ab4382d2SGreg Kroah-Hartman DMA_TO_DEVICE); 955ab4382d2SGreg Kroah-Hartman pdc->dma_size = UART_XMIT_SIZE; 956ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 957ab4382d2SGreg Kroah-Hartman } 958ab4382d2SGreg Kroah-Hartman 959ab4382d2SGreg Kroah-Hartman /* 960ab4382d2SGreg Kroah-Hartman * If there is a specific "open" function (to register 961ab4382d2SGreg Kroah-Hartman * control line interrupts) 962ab4382d2SGreg Kroah-Hartman */ 963ab4382d2SGreg Kroah-Hartman if (atmel_open_hook) { 964ab4382d2SGreg Kroah-Hartman retval = atmel_open_hook(port); 965ab4382d2SGreg Kroah-Hartman if (retval) { 966ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 967ab4382d2SGreg Kroah-Hartman return retval; 968ab4382d2SGreg Kroah-Hartman } 969ab4382d2SGreg Kroah-Hartman } 970ab4382d2SGreg Kroah-Hartman 971ab4382d2SGreg Kroah-Hartman /* Save current CSR for comparison in atmel_tasklet_func() */ 972ab4382d2SGreg Kroah-Hartman atmel_port->irq_status_prev = UART_GET_CSR(port); 973ab4382d2SGreg Kroah-Hartman atmel_port->irq_status = atmel_port->irq_status_prev; 974ab4382d2SGreg Kroah-Hartman 975ab4382d2SGreg Kroah-Hartman /* 976ab4382d2SGreg Kroah-Hartman * Finally, enable the serial port 977ab4382d2SGreg Kroah-Hartman */ 978ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 979ab4382d2SGreg Kroah-Hartman /* enable xmit & rcvr */ 980ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 981ab4382d2SGreg Kroah-Hartman 982ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_rx(port)) { 983ab4382d2SGreg Kroah-Hartman /* set UART timeout */ 984ab4382d2SGreg Kroah-Hartman UART_PUT_RTOR(port, PDC_RX_TIMEOUT); 985ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STTTO); 986ab4382d2SGreg Kroah-Hartman 987ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 988ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 989ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); 990ab4382d2SGreg Kroah-Hartman } else { 991ab4382d2SGreg Kroah-Hartman /* enable receive only */ 992ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_RXRDY); 993ab4382d2SGreg Kroah-Hartman } 994ab4382d2SGreg Kroah-Hartman 995ab4382d2SGreg Kroah-Hartman return 0; 996ab4382d2SGreg Kroah-Hartman } 997ab4382d2SGreg Kroah-Hartman 998ab4382d2SGreg Kroah-Hartman /* 999ab4382d2SGreg Kroah-Hartman * Disable the port 1000ab4382d2SGreg Kroah-Hartman */ 1001ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port) 1002ab4382d2SGreg Kroah-Hartman { 1003ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1004ab4382d2SGreg Kroah-Hartman /* 1005ab4382d2SGreg Kroah-Hartman * Ensure everything is stopped. 1006ab4382d2SGreg Kroah-Hartman */ 1007ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 1008ab4382d2SGreg Kroah-Hartman atmel_stop_tx(port); 1009ab4382d2SGreg Kroah-Hartman 1010ab4382d2SGreg Kroah-Hartman /* 1011ab4382d2SGreg Kroah-Hartman * Shut-down the DMA. 1012ab4382d2SGreg Kroah-Hartman */ 1013ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_rx(port)) { 1014ab4382d2SGreg Kroah-Hartman int i; 1015ab4382d2SGreg Kroah-Hartman 1016ab4382d2SGreg Kroah-Hartman for (i = 0; i < 2; i++) { 1017ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1018ab4382d2SGreg Kroah-Hartman 1019ab4382d2SGreg Kroah-Hartman dma_unmap_single(port->dev, 1020ab4382d2SGreg Kroah-Hartman pdc->dma_addr, 1021ab4382d2SGreg Kroah-Hartman pdc->dma_size, 1022ab4382d2SGreg Kroah-Hartman DMA_FROM_DEVICE); 1023ab4382d2SGreg Kroah-Hartman kfree(pdc->buf); 1024ab4382d2SGreg Kroah-Hartman } 1025ab4382d2SGreg Kroah-Hartman } 1026ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_tx(port)) { 1027ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1028ab4382d2SGreg Kroah-Hartman 1029ab4382d2SGreg Kroah-Hartman dma_unmap_single(port->dev, 1030ab4382d2SGreg Kroah-Hartman pdc->dma_addr, 1031ab4382d2SGreg Kroah-Hartman pdc->dma_size, 1032ab4382d2SGreg Kroah-Hartman DMA_TO_DEVICE); 1033ab4382d2SGreg Kroah-Hartman } 1034ab4382d2SGreg Kroah-Hartman 1035ab4382d2SGreg Kroah-Hartman /* 1036ab4382d2SGreg Kroah-Hartman * Disable all interrupts, port and break condition. 1037ab4382d2SGreg Kroah-Hartman */ 1038ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); 1039ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 1040ab4382d2SGreg Kroah-Hartman 1041ab4382d2SGreg Kroah-Hartman /* 1042ab4382d2SGreg Kroah-Hartman * Free the interrupt 1043ab4382d2SGreg Kroah-Hartman */ 1044ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 1045ab4382d2SGreg Kroah-Hartman 1046ab4382d2SGreg Kroah-Hartman /* 1047ab4382d2SGreg Kroah-Hartman * If there is a specific "close" function (to unregister 1048ab4382d2SGreg Kroah-Hartman * control line interrupts) 1049ab4382d2SGreg Kroah-Hartman */ 1050ab4382d2SGreg Kroah-Hartman if (atmel_close_hook) 1051ab4382d2SGreg Kroah-Hartman atmel_close_hook(port); 1052ab4382d2SGreg Kroah-Hartman } 1053ab4382d2SGreg Kroah-Hartman 1054ab4382d2SGreg Kroah-Hartman /* 1055ab4382d2SGreg Kroah-Hartman * Flush any TX data submitted for DMA. Called when the TX circular 1056ab4382d2SGreg Kroah-Hartman * buffer is reset. 1057ab4382d2SGreg Kroah-Hartman */ 1058ab4382d2SGreg Kroah-Hartman static void atmel_flush_buffer(struct uart_port *port) 1059ab4382d2SGreg Kroah-Hartman { 1060ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1061ab4382d2SGreg Kroah-Hartman 1062ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_tx(port)) { 1063ab4382d2SGreg Kroah-Hartman UART_PUT_TCR(port, 0); 1064ab4382d2SGreg Kroah-Hartman atmel_port->pdc_tx.ofs = 0; 1065ab4382d2SGreg Kroah-Hartman } 1066ab4382d2SGreg Kroah-Hartman } 1067ab4382d2SGreg Kroah-Hartman 1068ab4382d2SGreg Kroah-Hartman /* 1069ab4382d2SGreg Kroah-Hartman * Power / Clock management. 1070ab4382d2SGreg Kroah-Hartman */ 1071ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state, 1072ab4382d2SGreg Kroah-Hartman unsigned int oldstate) 1073ab4382d2SGreg Kroah-Hartman { 1074ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1075ab4382d2SGreg Kroah-Hartman 1076ab4382d2SGreg Kroah-Hartman switch (state) { 1077ab4382d2SGreg Kroah-Hartman case 0: 1078ab4382d2SGreg Kroah-Hartman /* 1079ab4382d2SGreg Kroah-Hartman * Enable the peripheral clock for this serial port. 1080ab4382d2SGreg Kroah-Hartman * This is called on uart_open() or a resume event. 1081ab4382d2SGreg Kroah-Hartman */ 1082ab4382d2SGreg Kroah-Hartman clk_enable(atmel_port->clk); 1083ab4382d2SGreg Kroah-Hartman 1084ab4382d2SGreg Kroah-Hartman /* re-enable interrupts if we disabled some on suspend */ 1085ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->backup_imr); 1086ab4382d2SGreg Kroah-Hartman break; 1087ab4382d2SGreg Kroah-Hartman case 3: 1088ab4382d2SGreg Kroah-Hartman /* Back up the interrupt mask and disable all interrupts */ 1089ab4382d2SGreg Kroah-Hartman atmel_port->backup_imr = UART_GET_IMR(port); 1090ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 1091ab4382d2SGreg Kroah-Hartman 1092ab4382d2SGreg Kroah-Hartman /* 1093ab4382d2SGreg Kroah-Hartman * Disable the peripheral clock for this serial port. 1094ab4382d2SGreg Kroah-Hartman * This is called on uart_close() or a suspend event. 1095ab4382d2SGreg Kroah-Hartman */ 1096ab4382d2SGreg Kroah-Hartman clk_disable(atmel_port->clk); 1097ab4382d2SGreg Kroah-Hartman break; 1098ab4382d2SGreg Kroah-Hartman default: 1099ab4382d2SGreg Kroah-Hartman printk(KERN_ERR "atmel_serial: unknown pm %d\n", state); 1100ab4382d2SGreg Kroah-Hartman } 1101ab4382d2SGreg Kroah-Hartman } 1102ab4382d2SGreg Kroah-Hartman 1103ab4382d2SGreg Kroah-Hartman /* 1104ab4382d2SGreg Kroah-Hartman * Change the port parameters 1105ab4382d2SGreg Kroah-Hartman */ 1106ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, 1107ab4382d2SGreg Kroah-Hartman struct ktermios *old) 1108ab4382d2SGreg Kroah-Hartman { 1109ab4382d2SGreg Kroah-Hartman unsigned long flags; 1110ab4382d2SGreg Kroah-Hartman unsigned int mode, imr, quot, baud; 1111ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1112ab4382d2SGreg Kroah-Hartman 1113ab4382d2SGreg Kroah-Hartman /* Get current mode register */ 1114ab4382d2SGreg Kroah-Hartman mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL 1115ab4382d2SGreg Kroah-Hartman | ATMEL_US_NBSTOP | ATMEL_US_PAR 1116ab4382d2SGreg Kroah-Hartman | ATMEL_US_USMODE); 1117ab4382d2SGreg Kroah-Hartman 1118ab4382d2SGreg Kroah-Hartman baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); 1119ab4382d2SGreg Kroah-Hartman quot = uart_get_divisor(port, baud); 1120ab4382d2SGreg Kroah-Hartman 1121ab4382d2SGreg Kroah-Hartman if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */ 1122ab4382d2SGreg Kroah-Hartman quot /= 8; 1123ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USCLKS_MCK_DIV8; 1124ab4382d2SGreg Kroah-Hartman } 1125ab4382d2SGreg Kroah-Hartman 1126ab4382d2SGreg Kroah-Hartman /* byte size */ 1127ab4382d2SGreg Kroah-Hartman switch (termios->c_cflag & CSIZE) { 1128ab4382d2SGreg Kroah-Hartman case CS5: 1129ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_5; 1130ab4382d2SGreg Kroah-Hartman break; 1131ab4382d2SGreg Kroah-Hartman case CS6: 1132ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_6; 1133ab4382d2SGreg Kroah-Hartman break; 1134ab4382d2SGreg Kroah-Hartman case CS7: 1135ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_7; 1136ab4382d2SGreg Kroah-Hartman break; 1137ab4382d2SGreg Kroah-Hartman default: 1138ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_8; 1139ab4382d2SGreg Kroah-Hartman break; 1140ab4382d2SGreg Kroah-Hartman } 1141ab4382d2SGreg Kroah-Hartman 1142ab4382d2SGreg Kroah-Hartman /* stop bits */ 1143ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CSTOPB) 1144ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_NBSTOP_2; 1145ab4382d2SGreg Kroah-Hartman 1146ab4382d2SGreg Kroah-Hartman /* parity */ 1147ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARENB) { 1148ab4382d2SGreg Kroah-Hartman /* Mark or Space parity */ 1149ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CMSPAR) { 1150ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARODD) 1151ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_MARK; 1152ab4382d2SGreg Kroah-Hartman else 1153ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_SPACE; 1154ab4382d2SGreg Kroah-Hartman } else if (termios->c_cflag & PARODD) 1155ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_ODD; 1156ab4382d2SGreg Kroah-Hartman else 1157ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_EVEN; 1158ab4382d2SGreg Kroah-Hartman } else 1159ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_NONE; 1160ab4382d2SGreg Kroah-Hartman 1161ab4382d2SGreg Kroah-Hartman /* hardware handshake (RTS/CTS) */ 1162ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CRTSCTS) 1163ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_HWHS; 1164ab4382d2SGreg Kroah-Hartman else 1165ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_NORMAL; 1166ab4382d2SGreg Kroah-Hartman 1167ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 1168ab4382d2SGreg Kroah-Hartman 1169ab4382d2SGreg Kroah-Hartman port->read_status_mask = ATMEL_US_OVRE; 1170ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & INPCK) 1171ab4382d2SGreg Kroah-Hartman port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 1172ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & (BRKINT | PARMRK)) 1173ab4382d2SGreg Kroah-Hartman port->read_status_mask |= ATMEL_US_RXBRK; 1174ab4382d2SGreg Kroah-Hartman 1175ab4382d2SGreg Kroah-Hartman if (atmel_use_dma_rx(port)) 1176ab4382d2SGreg Kroah-Hartman /* need to enable error interrupts */ 1177ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, port->read_status_mask); 1178ab4382d2SGreg Kroah-Hartman 1179ab4382d2SGreg Kroah-Hartman /* 1180ab4382d2SGreg Kroah-Hartman * Characters to ignore 1181ab4382d2SGreg Kroah-Hartman */ 1182ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 1183ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 1184ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 1185ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNBRK) { 1186ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_RXBRK; 1187ab4382d2SGreg Kroah-Hartman /* 1188ab4382d2SGreg Kroah-Hartman * If we're ignoring parity and break indicators, 1189ab4382d2SGreg Kroah-Hartman * ignore overruns too (for real raw support). 1190ab4382d2SGreg Kroah-Hartman */ 1191ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 1192ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_OVRE; 1193ab4382d2SGreg Kroah-Hartman } 1194ab4382d2SGreg Kroah-Hartman /* TODO: Ignore all characters if CREAD is set.*/ 1195ab4382d2SGreg Kroah-Hartman 1196ab4382d2SGreg Kroah-Hartman /* update the per-port timeout */ 1197ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, termios->c_cflag, baud); 1198ab4382d2SGreg Kroah-Hartman 1199ab4382d2SGreg Kroah-Hartman /* 1200ab4382d2SGreg Kroah-Hartman * save/disable interrupts. The tty layer will ensure that the 1201ab4382d2SGreg Kroah-Hartman * transmitter is empty if requested by the caller, so there's 1202ab4382d2SGreg Kroah-Hartman * no need to wait for it here. 1203ab4382d2SGreg Kroah-Hartman */ 1204ab4382d2SGreg Kroah-Hartman imr = UART_GET_IMR(port); 1205ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 1206ab4382d2SGreg Kroah-Hartman 1207ab4382d2SGreg Kroah-Hartman /* disable receiver and transmitter */ 1208ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS); 1209ab4382d2SGreg Kroah-Hartman 1210ab4382d2SGreg Kroah-Hartman /* Resetting serial mode to RS232 (0x0) */ 1211ab4382d2SGreg Kroah-Hartman mode &= ~ATMEL_US_USMODE; 1212ab4382d2SGreg Kroah-Hartman 1213ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_ENABLED) { 1214ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS485\n"); 1215ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND) 1216ab4382d2SGreg Kroah-Hartman UART_PUT_TTGR(port, 1217ab4382d2SGreg Kroah-Hartman atmel_port->rs485.delay_rts_after_send); 1218ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 1219ab4382d2SGreg Kroah-Hartman } else { 1220ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS232\n"); 1221ab4382d2SGreg Kroah-Hartman } 1222ab4382d2SGreg Kroah-Hartman 1223ab4382d2SGreg Kroah-Hartman /* set the parity, stop bits and data size */ 1224ab4382d2SGreg Kroah-Hartman UART_PUT_MR(port, mode); 1225ab4382d2SGreg Kroah-Hartman 1226ab4382d2SGreg Kroah-Hartman /* set the baud rate */ 1227ab4382d2SGreg Kroah-Hartman UART_PUT_BRGR(port, quot); 1228ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 1229ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 1230ab4382d2SGreg Kroah-Hartman 1231ab4382d2SGreg Kroah-Hartman /* restore interrupts */ 1232ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, imr); 1233ab4382d2SGreg Kroah-Hartman 1234ab4382d2SGreg Kroah-Hartman /* CTS flow-control and modem-status interrupts */ 1235ab4382d2SGreg Kroah-Hartman if (UART_ENABLE_MS(port, termios->c_cflag)) 1236ab4382d2SGreg Kroah-Hartman port->ops->enable_ms(port); 1237ab4382d2SGreg Kroah-Hartman 1238ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 1239ab4382d2SGreg Kroah-Hartman } 1240ab4382d2SGreg Kroah-Hartman 124142bd7a4fSViktar Palstsiuk static void atmel_set_ldisc(struct uart_port *port, int new) 124242bd7a4fSViktar Palstsiuk { 124342bd7a4fSViktar Palstsiuk int line = port->line; 124442bd7a4fSViktar Palstsiuk 124542bd7a4fSViktar Palstsiuk if (line >= port->state->port.tty->driver->num) 124642bd7a4fSViktar Palstsiuk return; 124742bd7a4fSViktar Palstsiuk 124842bd7a4fSViktar Palstsiuk if (port->state->port.tty->ldisc->ops->num == N_PPS) { 124942bd7a4fSViktar Palstsiuk port->flags |= UPF_HARDPPS_CD; 125042bd7a4fSViktar Palstsiuk atmel_enable_ms(port); 125142bd7a4fSViktar Palstsiuk } else { 125242bd7a4fSViktar Palstsiuk port->flags &= ~UPF_HARDPPS_CD; 125342bd7a4fSViktar Palstsiuk } 125442bd7a4fSViktar Palstsiuk } 125542bd7a4fSViktar Palstsiuk 1256ab4382d2SGreg Kroah-Hartman /* 1257ab4382d2SGreg Kroah-Hartman * Return string describing the specified port 1258ab4382d2SGreg Kroah-Hartman */ 1259ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port) 1260ab4382d2SGreg Kroah-Hartman { 1261ab4382d2SGreg Kroah-Hartman return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; 1262ab4382d2SGreg Kroah-Hartman } 1263ab4382d2SGreg Kroah-Hartman 1264ab4382d2SGreg Kroah-Hartman /* 1265ab4382d2SGreg Kroah-Hartman * Release the memory region(s) being used by 'port'. 1266ab4382d2SGreg Kroah-Hartman */ 1267ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port) 1268ab4382d2SGreg Kroah-Hartman { 1269ab4382d2SGreg Kroah-Hartman struct platform_device *pdev = to_platform_device(port->dev); 1270ab4382d2SGreg Kroah-Hartman int size = pdev->resource[0].end - pdev->resource[0].start + 1; 1271ab4382d2SGreg Kroah-Hartman 1272ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 1273ab4382d2SGreg Kroah-Hartman 1274ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 1275ab4382d2SGreg Kroah-Hartman iounmap(port->membase); 1276ab4382d2SGreg Kroah-Hartman port->membase = NULL; 1277ab4382d2SGreg Kroah-Hartman } 1278ab4382d2SGreg Kroah-Hartman } 1279ab4382d2SGreg Kroah-Hartman 1280ab4382d2SGreg Kroah-Hartman /* 1281ab4382d2SGreg Kroah-Hartman * Request the memory region(s) being used by 'port'. 1282ab4382d2SGreg Kroah-Hartman */ 1283ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port) 1284ab4382d2SGreg Kroah-Hartman { 1285ab4382d2SGreg Kroah-Hartman struct platform_device *pdev = to_platform_device(port->dev); 1286ab4382d2SGreg Kroah-Hartman int size = pdev->resource[0].end - pdev->resource[0].start + 1; 1287ab4382d2SGreg Kroah-Hartman 1288ab4382d2SGreg Kroah-Hartman if (!request_mem_region(port->mapbase, size, "atmel_serial")) 1289ab4382d2SGreg Kroah-Hartman return -EBUSY; 1290ab4382d2SGreg Kroah-Hartman 1291ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 1292ab4382d2SGreg Kroah-Hartman port->membase = ioremap(port->mapbase, size); 1293ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 1294ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 1295ab4382d2SGreg Kroah-Hartman return -ENOMEM; 1296ab4382d2SGreg Kroah-Hartman } 1297ab4382d2SGreg Kroah-Hartman } 1298ab4382d2SGreg Kroah-Hartman 1299ab4382d2SGreg Kroah-Hartman return 0; 1300ab4382d2SGreg Kroah-Hartman } 1301ab4382d2SGreg Kroah-Hartman 1302ab4382d2SGreg Kroah-Hartman /* 1303ab4382d2SGreg Kroah-Hartman * Configure/autoconfigure the port. 1304ab4382d2SGreg Kroah-Hartman */ 1305ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags) 1306ab4382d2SGreg Kroah-Hartman { 1307ab4382d2SGreg Kroah-Hartman if (flags & UART_CONFIG_TYPE) { 1308ab4382d2SGreg Kroah-Hartman port->type = PORT_ATMEL; 1309ab4382d2SGreg Kroah-Hartman atmel_request_port(port); 1310ab4382d2SGreg Kroah-Hartman } 1311ab4382d2SGreg Kroah-Hartman } 1312ab4382d2SGreg Kroah-Hartman 1313ab4382d2SGreg Kroah-Hartman /* 1314ab4382d2SGreg Kroah-Hartman * Verify the new serial_struct (for TIOCSSERIAL). 1315ab4382d2SGreg Kroah-Hartman */ 1316ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser) 1317ab4382d2SGreg Kroah-Hartman { 1318ab4382d2SGreg Kroah-Hartman int ret = 0; 1319ab4382d2SGreg Kroah-Hartman if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) 1320ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 1321ab4382d2SGreg Kroah-Hartman if (port->irq != ser->irq) 1322ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 1323ab4382d2SGreg Kroah-Hartman if (ser->io_type != SERIAL_IO_MEM) 1324ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 1325ab4382d2SGreg Kroah-Hartman if (port->uartclk / 16 != ser->baud_base) 1326ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 1327ab4382d2SGreg Kroah-Hartman if ((void *)port->mapbase != ser->iomem_base) 1328ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 1329ab4382d2SGreg Kroah-Hartman if (port->iobase != ser->port) 1330ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 1331ab4382d2SGreg Kroah-Hartman if (ser->hub6 != 0) 1332ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 1333ab4382d2SGreg Kroah-Hartman return ret; 1334ab4382d2SGreg Kroah-Hartman } 1335ab4382d2SGreg Kroah-Hartman 1336ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 1337ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port) 1338ab4382d2SGreg Kroah-Hartman { 1339ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY)) 1340ab4382d2SGreg Kroah-Hartman cpu_relax(); 1341ab4382d2SGreg Kroah-Hartman 1342ab4382d2SGreg Kroah-Hartman return UART_GET_CHAR(port); 1343ab4382d2SGreg Kroah-Hartman } 1344ab4382d2SGreg Kroah-Hartman 1345ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch) 1346ab4382d2SGreg Kroah-Hartman { 1347ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY)) 1348ab4382d2SGreg Kroah-Hartman cpu_relax(); 1349ab4382d2SGreg Kroah-Hartman 1350ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, ch); 1351ab4382d2SGreg Kroah-Hartman } 1352ab4382d2SGreg Kroah-Hartman #endif 1353ab4382d2SGreg Kroah-Hartman 1354ab4382d2SGreg Kroah-Hartman static int 1355ab4382d2SGreg Kroah-Hartman atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg) 1356ab4382d2SGreg Kroah-Hartman { 1357ab4382d2SGreg Kroah-Hartman struct serial_rs485 rs485conf; 1358ab4382d2SGreg Kroah-Hartman 1359ab4382d2SGreg Kroah-Hartman switch (cmd) { 1360ab4382d2SGreg Kroah-Hartman case TIOCSRS485: 1361ab4382d2SGreg Kroah-Hartman if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg, 1362ab4382d2SGreg Kroah-Hartman sizeof(rs485conf))) 1363ab4382d2SGreg Kroah-Hartman return -EFAULT; 1364ab4382d2SGreg Kroah-Hartman 1365ab4382d2SGreg Kroah-Hartman atmel_config_rs485(port, &rs485conf); 1366ab4382d2SGreg Kroah-Hartman break; 1367ab4382d2SGreg Kroah-Hartman 1368ab4382d2SGreg Kroah-Hartman case TIOCGRS485: 1369ab4382d2SGreg Kroah-Hartman if (copy_to_user((struct serial_rs485 *) arg, 1370ab4382d2SGreg Kroah-Hartman &(to_atmel_uart_port(port)->rs485), 1371ab4382d2SGreg Kroah-Hartman sizeof(rs485conf))) 1372ab4382d2SGreg Kroah-Hartman return -EFAULT; 1373ab4382d2SGreg Kroah-Hartman break; 1374ab4382d2SGreg Kroah-Hartman 1375ab4382d2SGreg Kroah-Hartman default: 1376ab4382d2SGreg Kroah-Hartman return -ENOIOCTLCMD; 1377ab4382d2SGreg Kroah-Hartman } 1378ab4382d2SGreg Kroah-Hartman return 0; 1379ab4382d2SGreg Kroah-Hartman } 1380ab4382d2SGreg Kroah-Hartman 1381ab4382d2SGreg Kroah-Hartman 1382ab4382d2SGreg Kroah-Hartman 1383ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = { 1384ab4382d2SGreg Kroah-Hartman .tx_empty = atmel_tx_empty, 1385ab4382d2SGreg Kroah-Hartman .set_mctrl = atmel_set_mctrl, 1386ab4382d2SGreg Kroah-Hartman .get_mctrl = atmel_get_mctrl, 1387ab4382d2SGreg Kroah-Hartman .stop_tx = atmel_stop_tx, 1388ab4382d2SGreg Kroah-Hartman .start_tx = atmel_start_tx, 1389ab4382d2SGreg Kroah-Hartman .stop_rx = atmel_stop_rx, 1390ab4382d2SGreg Kroah-Hartman .enable_ms = atmel_enable_ms, 1391ab4382d2SGreg Kroah-Hartman .break_ctl = atmel_break_ctl, 1392ab4382d2SGreg Kroah-Hartman .startup = atmel_startup, 1393ab4382d2SGreg Kroah-Hartman .shutdown = atmel_shutdown, 1394ab4382d2SGreg Kroah-Hartman .flush_buffer = atmel_flush_buffer, 1395ab4382d2SGreg Kroah-Hartman .set_termios = atmel_set_termios, 139642bd7a4fSViktar Palstsiuk .set_ldisc = atmel_set_ldisc, 1397ab4382d2SGreg Kroah-Hartman .type = atmel_type, 1398ab4382d2SGreg Kroah-Hartman .release_port = atmel_release_port, 1399ab4382d2SGreg Kroah-Hartman .request_port = atmel_request_port, 1400ab4382d2SGreg Kroah-Hartman .config_port = atmel_config_port, 1401ab4382d2SGreg Kroah-Hartman .verify_port = atmel_verify_port, 1402ab4382d2SGreg Kroah-Hartman .pm = atmel_serial_pm, 1403ab4382d2SGreg Kroah-Hartman .ioctl = atmel_ioctl, 1404ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 1405ab4382d2SGreg Kroah-Hartman .poll_get_char = atmel_poll_get_char, 1406ab4382d2SGreg Kroah-Hartman .poll_put_char = atmel_poll_put_char, 1407ab4382d2SGreg Kroah-Hartman #endif 1408ab4382d2SGreg Kroah-Hartman }; 1409ab4382d2SGreg Kroah-Hartman 1410ab4382d2SGreg Kroah-Hartman /* 1411ab4382d2SGreg Kroah-Hartman * Configure the port from the platform device resource info. 1412ab4382d2SGreg Kroah-Hartman */ 1413ab4382d2SGreg Kroah-Hartman static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port, 1414ab4382d2SGreg Kroah-Hartman struct platform_device *pdev) 1415ab4382d2SGreg Kroah-Hartman { 1416ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_port->uart; 1417ab4382d2SGreg Kroah-Hartman struct atmel_uart_data *data = pdev->dev.platform_data; 1418ab4382d2SGreg Kroah-Hartman 1419ab4382d2SGreg Kroah-Hartman port->iotype = UPIO_MEM; 1420ab4382d2SGreg Kroah-Hartman port->flags = UPF_BOOT_AUTOCONF; 1421ab4382d2SGreg Kroah-Hartman port->ops = &atmel_pops; 1422ab4382d2SGreg Kroah-Hartman port->fifosize = 1; 14232b348e2fSJean-Christophe PLAGNIOL-VILLARD port->line = data->num; 1424ab4382d2SGreg Kroah-Hartman port->dev = &pdev->dev; 1425ab4382d2SGreg Kroah-Hartman port->mapbase = pdev->resource[0].start; 1426ab4382d2SGreg Kroah-Hartman port->irq = pdev->resource[1].start; 1427ab4382d2SGreg Kroah-Hartman 1428ab4382d2SGreg Kroah-Hartman tasklet_init(&atmel_port->tasklet, atmel_tasklet_func, 1429ab4382d2SGreg Kroah-Hartman (unsigned long)port); 1430ab4382d2SGreg Kroah-Hartman 1431ab4382d2SGreg Kroah-Hartman memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); 1432ab4382d2SGreg Kroah-Hartman 1433ab4382d2SGreg Kroah-Hartman if (data->regs) 1434ab4382d2SGreg Kroah-Hartman /* Already mapped by setup code */ 1435ab4382d2SGreg Kroah-Hartman port->membase = data->regs; 1436ab4382d2SGreg Kroah-Hartman else { 1437ab4382d2SGreg Kroah-Hartman port->flags |= UPF_IOREMAP; 1438ab4382d2SGreg Kroah-Hartman port->membase = NULL; 1439ab4382d2SGreg Kroah-Hartman } 1440ab4382d2SGreg Kroah-Hartman 1441ab4382d2SGreg Kroah-Hartman /* for console, the clock could already be configured */ 1442ab4382d2SGreg Kroah-Hartman if (!atmel_port->clk) { 1443ab4382d2SGreg Kroah-Hartman atmel_port->clk = clk_get(&pdev->dev, "usart"); 1444ab4382d2SGreg Kroah-Hartman clk_enable(atmel_port->clk); 1445ab4382d2SGreg Kroah-Hartman port->uartclk = clk_get_rate(atmel_port->clk); 1446ab4382d2SGreg Kroah-Hartman clk_disable(atmel_port->clk); 1447ab4382d2SGreg Kroah-Hartman /* only enable clock when USART is in use */ 1448ab4382d2SGreg Kroah-Hartman } 1449ab4382d2SGreg Kroah-Hartman 1450ab4382d2SGreg Kroah-Hartman atmel_port->use_dma_rx = data->use_dma_rx; 1451ab4382d2SGreg Kroah-Hartman atmel_port->use_dma_tx = data->use_dma_tx; 1452ab4382d2SGreg Kroah-Hartman atmel_port->rs485 = data->rs485; 1453ab4382d2SGreg Kroah-Hartman /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ 1454ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_ENABLED) 1455ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 1456ab4382d2SGreg Kroah-Hartman else if (atmel_use_dma_tx(port)) { 1457ab4382d2SGreg Kroah-Hartman port->fifosize = PDC_BUFFER_SIZE; 1458ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; 1459ab4382d2SGreg Kroah-Hartman } else { 1460ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 1461ab4382d2SGreg Kroah-Hartman } 1462ab4382d2SGreg Kroah-Hartman } 1463ab4382d2SGreg Kroah-Hartman 1464ab4382d2SGreg Kroah-Hartman /* 1465ab4382d2SGreg Kroah-Hartman * Register board-specific modem-control line handlers. 1466ab4382d2SGreg Kroah-Hartman */ 1467ab4382d2SGreg Kroah-Hartman void __init atmel_register_uart_fns(struct atmel_port_fns *fns) 1468ab4382d2SGreg Kroah-Hartman { 1469ab4382d2SGreg Kroah-Hartman if (fns->enable_ms) 1470ab4382d2SGreg Kroah-Hartman atmel_pops.enable_ms = fns->enable_ms; 1471ab4382d2SGreg Kroah-Hartman if (fns->get_mctrl) 1472ab4382d2SGreg Kroah-Hartman atmel_pops.get_mctrl = fns->get_mctrl; 1473ab4382d2SGreg Kroah-Hartman if (fns->set_mctrl) 1474ab4382d2SGreg Kroah-Hartman atmel_pops.set_mctrl = fns->set_mctrl; 1475ab4382d2SGreg Kroah-Hartman atmel_open_hook = fns->open; 1476ab4382d2SGreg Kroah-Hartman atmel_close_hook = fns->close; 1477ab4382d2SGreg Kroah-Hartman atmel_pops.pm = fns->pm; 1478ab4382d2SGreg Kroah-Hartman atmel_pops.set_wake = fns->set_wake; 1479ab4382d2SGreg Kroah-Hartman } 1480ab4382d2SGreg Kroah-Hartman 1481ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 1482ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch) 1483ab4382d2SGreg Kroah-Hartman { 1484ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY)) 1485ab4382d2SGreg Kroah-Hartman cpu_relax(); 1486ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, ch); 1487ab4382d2SGreg Kroah-Hartman } 1488ab4382d2SGreg Kroah-Hartman 1489ab4382d2SGreg Kroah-Hartman /* 1490ab4382d2SGreg Kroah-Hartman * Interrupts are disabled on entering 1491ab4382d2SGreg Kroah-Hartman */ 1492ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count) 1493ab4382d2SGreg Kroah-Hartman { 1494ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 1495ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1496ab4382d2SGreg Kroah-Hartman unsigned int status, imr; 1497ab4382d2SGreg Kroah-Hartman unsigned int pdc_tx; 1498ab4382d2SGreg Kroah-Hartman 1499ab4382d2SGreg Kroah-Hartman /* 1500ab4382d2SGreg Kroah-Hartman * First, save IMR and then disable interrupts 1501ab4382d2SGreg Kroah-Hartman */ 1502ab4382d2SGreg Kroah-Hartman imr = UART_GET_IMR(port); 1503ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask); 1504ab4382d2SGreg Kroah-Hartman 1505ab4382d2SGreg Kroah-Hartman /* Store PDC transmit status and disable it */ 1506ab4382d2SGreg Kroah-Hartman pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN; 1507ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 1508ab4382d2SGreg Kroah-Hartman 1509ab4382d2SGreg Kroah-Hartman uart_console_write(port, s, count, atmel_console_putchar); 1510ab4382d2SGreg Kroah-Hartman 1511ab4382d2SGreg Kroah-Hartman /* 1512ab4382d2SGreg Kroah-Hartman * Finally, wait for transmitter to become empty 1513ab4382d2SGreg Kroah-Hartman * and restore IMR 1514ab4382d2SGreg Kroah-Hartman */ 1515ab4382d2SGreg Kroah-Hartman do { 1516ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 1517ab4382d2SGreg Kroah-Hartman } while (!(status & ATMEL_US_TXRDY)); 1518ab4382d2SGreg Kroah-Hartman 1519ab4382d2SGreg Kroah-Hartman /* Restore PDC transmit status */ 1520ab4382d2SGreg Kroah-Hartman if (pdc_tx) 1521ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); 1522ab4382d2SGreg Kroah-Hartman 1523ab4382d2SGreg Kroah-Hartman /* set interrupts back the way they were */ 1524ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, imr); 1525ab4382d2SGreg Kroah-Hartman } 1526ab4382d2SGreg Kroah-Hartman 1527ab4382d2SGreg Kroah-Hartman /* 1528ab4382d2SGreg Kroah-Hartman * If the port was already initialised (eg, by a boot loader), 1529ab4382d2SGreg Kroah-Hartman * try to determine the current setup. 1530ab4382d2SGreg Kroah-Hartman */ 1531ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud, 1532ab4382d2SGreg Kroah-Hartman int *parity, int *bits) 1533ab4382d2SGreg Kroah-Hartman { 1534ab4382d2SGreg Kroah-Hartman unsigned int mr, quot; 1535ab4382d2SGreg Kroah-Hartman 1536ab4382d2SGreg Kroah-Hartman /* 1537ab4382d2SGreg Kroah-Hartman * If the baud rate generator isn't running, the port wasn't 1538ab4382d2SGreg Kroah-Hartman * initialized by the boot loader. 1539ab4382d2SGreg Kroah-Hartman */ 1540ab4382d2SGreg Kroah-Hartman quot = UART_GET_BRGR(port) & ATMEL_US_CD; 1541ab4382d2SGreg Kroah-Hartman if (!quot) 1542ab4382d2SGreg Kroah-Hartman return; 1543ab4382d2SGreg Kroah-Hartman 1544ab4382d2SGreg Kroah-Hartman mr = UART_GET_MR(port) & ATMEL_US_CHRL; 1545ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_CHRL_8) 1546ab4382d2SGreg Kroah-Hartman *bits = 8; 1547ab4382d2SGreg Kroah-Hartman else 1548ab4382d2SGreg Kroah-Hartman *bits = 7; 1549ab4382d2SGreg Kroah-Hartman 1550ab4382d2SGreg Kroah-Hartman mr = UART_GET_MR(port) & ATMEL_US_PAR; 1551ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_PAR_EVEN) 1552ab4382d2SGreg Kroah-Hartman *parity = 'e'; 1553ab4382d2SGreg Kroah-Hartman else if (mr == ATMEL_US_PAR_ODD) 1554ab4382d2SGreg Kroah-Hartman *parity = 'o'; 1555ab4382d2SGreg Kroah-Hartman 1556ab4382d2SGreg Kroah-Hartman /* 1557ab4382d2SGreg Kroah-Hartman * The serial core only rounds down when matching this to a 1558ab4382d2SGreg Kroah-Hartman * supported baud rate. Make sure we don't end up slightly 1559ab4382d2SGreg Kroah-Hartman * lower than one of those, as it would make us fall through 1560ab4382d2SGreg Kroah-Hartman * to a much lower baud rate than we really want. 1561ab4382d2SGreg Kroah-Hartman */ 1562ab4382d2SGreg Kroah-Hartman *baud = port->uartclk / (16 * (quot - 1)); 1563ab4382d2SGreg Kroah-Hartman } 1564ab4382d2SGreg Kroah-Hartman 1565ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options) 1566ab4382d2SGreg Kroah-Hartman { 1567ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 1568ab4382d2SGreg Kroah-Hartman int baud = 115200; 1569ab4382d2SGreg Kroah-Hartman int bits = 8; 1570ab4382d2SGreg Kroah-Hartman int parity = 'n'; 1571ab4382d2SGreg Kroah-Hartman int flow = 'n'; 1572ab4382d2SGreg Kroah-Hartman 1573ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 1574ab4382d2SGreg Kroah-Hartman /* Port not initialized yet - delay setup */ 1575ab4382d2SGreg Kroah-Hartman return -ENODEV; 1576ab4382d2SGreg Kroah-Hartman } 1577ab4382d2SGreg Kroah-Hartman 1578ab4382d2SGreg Kroah-Hartman clk_enable(atmel_ports[co->index].clk); 1579ab4382d2SGreg Kroah-Hartman 1580ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 1581ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 1582ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 1583ab4382d2SGreg Kroah-Hartman 1584ab4382d2SGreg Kroah-Hartman if (options) 1585ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 1586ab4382d2SGreg Kroah-Hartman else 1587ab4382d2SGreg Kroah-Hartman atmel_console_get_options(port, &baud, &parity, &bits); 1588ab4382d2SGreg Kroah-Hartman 1589ab4382d2SGreg Kroah-Hartman return uart_set_options(port, co, baud, parity, bits, flow); 1590ab4382d2SGreg Kroah-Hartman } 1591ab4382d2SGreg Kroah-Hartman 1592ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart; 1593ab4382d2SGreg Kroah-Hartman 1594ab4382d2SGreg Kroah-Hartman static struct console atmel_console = { 1595ab4382d2SGreg Kroah-Hartman .name = ATMEL_DEVICENAME, 1596ab4382d2SGreg Kroah-Hartman .write = atmel_console_write, 1597ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 1598ab4382d2SGreg Kroah-Hartman .setup = atmel_console_setup, 1599ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 1600ab4382d2SGreg Kroah-Hartman .index = -1, 1601ab4382d2SGreg Kroah-Hartman .data = &atmel_uart, 1602ab4382d2SGreg Kroah-Hartman }; 1603ab4382d2SGreg Kroah-Hartman 1604ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE (&atmel_console) 1605ab4382d2SGreg Kroah-Hartman 1606ab4382d2SGreg Kroah-Hartman /* 1607ab4382d2SGreg Kroah-Hartman * Early console initialization (before VM subsystem initialized). 1608ab4382d2SGreg Kroah-Hartman */ 1609ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void) 1610ab4382d2SGreg Kroah-Hartman { 1611ab4382d2SGreg Kroah-Hartman if (atmel_default_console_device) { 1612*0d0a3cc1SVoss, Nikolaus struct atmel_uart_data *pdata = 1613*0d0a3cc1SVoss, Nikolaus atmel_default_console_device->dev.platform_data; 1614*0d0a3cc1SVoss, Nikolaus 1615*0d0a3cc1SVoss, Nikolaus add_preferred_console(ATMEL_DEVICENAME, pdata->num, NULL); 1616*0d0a3cc1SVoss, Nikolaus atmel_init_port(&atmel_ports[pdata->num], 1617ab4382d2SGreg Kroah-Hartman atmel_default_console_device); 1618ab4382d2SGreg Kroah-Hartman register_console(&atmel_console); 1619ab4382d2SGreg Kroah-Hartman } 1620ab4382d2SGreg Kroah-Hartman 1621ab4382d2SGreg Kroah-Hartman return 0; 1622ab4382d2SGreg Kroah-Hartman } 1623ab4382d2SGreg Kroah-Hartman 1624ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init); 1625ab4382d2SGreg Kroah-Hartman 1626ab4382d2SGreg Kroah-Hartman /* 1627ab4382d2SGreg Kroah-Hartman * Late console initialization. 1628ab4382d2SGreg Kroah-Hartman */ 1629ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void) 1630ab4382d2SGreg Kroah-Hartman { 1631ab4382d2SGreg Kroah-Hartman if (atmel_default_console_device 1632ab4382d2SGreg Kroah-Hartman && !(atmel_console.flags & CON_ENABLED)) 1633ab4382d2SGreg Kroah-Hartman register_console(&atmel_console); 1634ab4382d2SGreg Kroah-Hartman 1635ab4382d2SGreg Kroah-Hartman return 0; 1636ab4382d2SGreg Kroah-Hartman } 1637ab4382d2SGreg Kroah-Hartman 1638ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init); 1639ab4382d2SGreg Kroah-Hartman 1640ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 1641ab4382d2SGreg Kroah-Hartman { 1642ab4382d2SGreg Kroah-Hartman return port->cons && port->cons->index == port->line; 1643ab4382d2SGreg Kroah-Hartman } 1644ab4382d2SGreg Kroah-Hartman 1645ab4382d2SGreg Kroah-Hartman #else 1646ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE NULL 1647ab4382d2SGreg Kroah-Hartman 1648ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 1649ab4382d2SGreg Kroah-Hartman { 1650ab4382d2SGreg Kroah-Hartman return false; 1651ab4382d2SGreg Kroah-Hartman } 1652ab4382d2SGreg Kroah-Hartman #endif 1653ab4382d2SGreg Kroah-Hartman 1654ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = { 1655ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 1656ab4382d2SGreg Kroah-Hartman .driver_name = "atmel_serial", 1657ab4382d2SGreg Kroah-Hartman .dev_name = ATMEL_DEVICENAME, 1658ab4382d2SGreg Kroah-Hartman .major = SERIAL_ATMEL_MAJOR, 1659ab4382d2SGreg Kroah-Hartman .minor = MINOR_START, 1660ab4382d2SGreg Kroah-Hartman .nr = ATMEL_MAX_UART, 1661ab4382d2SGreg Kroah-Hartman .cons = ATMEL_CONSOLE_DEVICE, 1662ab4382d2SGreg Kroah-Hartman }; 1663ab4382d2SGreg Kroah-Hartman 1664ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM 1665ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void) 1666ab4382d2SGreg Kroah-Hartman { 1667ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91 1668ab4382d2SGreg Kroah-Hartman return at91_suspend_entering_slow_clock(); 1669ab4382d2SGreg Kroah-Hartman #else 1670ab4382d2SGreg Kroah-Hartman return false; 1671ab4382d2SGreg Kroah-Hartman #endif 1672ab4382d2SGreg Kroah-Hartman } 1673ab4382d2SGreg Kroah-Hartman 1674ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev, 1675ab4382d2SGreg Kroah-Hartman pm_message_t state) 1676ab4382d2SGreg Kroah-Hartman { 1677ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 1678ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1679ab4382d2SGreg Kroah-Hartman 1680ab4382d2SGreg Kroah-Hartman if (atmel_is_console_port(port) && console_suspend_enabled) { 1681ab4382d2SGreg Kroah-Hartman /* Drain the TX shifter */ 1682ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) 1683ab4382d2SGreg Kroah-Hartman cpu_relax(); 1684ab4382d2SGreg Kroah-Hartman } 1685ab4382d2SGreg Kroah-Hartman 1686ab4382d2SGreg Kroah-Hartman /* we can not wake up if we're running on slow clock */ 1687ab4382d2SGreg Kroah-Hartman atmel_port->may_wakeup = device_may_wakeup(&pdev->dev); 1688ab4382d2SGreg Kroah-Hartman if (atmel_serial_clk_will_stop()) 1689ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, 0); 1690ab4382d2SGreg Kroah-Hartman 1691ab4382d2SGreg Kroah-Hartman uart_suspend_port(&atmel_uart, port); 1692ab4382d2SGreg Kroah-Hartman 1693ab4382d2SGreg Kroah-Hartman return 0; 1694ab4382d2SGreg Kroah-Hartman } 1695ab4382d2SGreg Kroah-Hartman 1696ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev) 1697ab4382d2SGreg Kroah-Hartman { 1698ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 1699ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1700ab4382d2SGreg Kroah-Hartman 1701ab4382d2SGreg Kroah-Hartman uart_resume_port(&atmel_uart, port); 1702ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup); 1703ab4382d2SGreg Kroah-Hartman 1704ab4382d2SGreg Kroah-Hartman return 0; 1705ab4382d2SGreg Kroah-Hartman } 1706ab4382d2SGreg Kroah-Hartman #else 1707ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL 1708ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL 1709ab4382d2SGreg Kroah-Hartman #endif 1710ab4382d2SGreg Kroah-Hartman 1711ab4382d2SGreg Kroah-Hartman static int __devinit atmel_serial_probe(struct platform_device *pdev) 1712ab4382d2SGreg Kroah-Hartman { 1713ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *port; 1714deba1a0dSJean-Christophe PLAGNIOL-VILLARD struct atmel_uart_data *pdata = pdev->dev.platform_data; 1715ab4382d2SGreg Kroah-Hartman void *data; 1716ab4382d2SGreg Kroah-Hartman int ret; 1717ab4382d2SGreg Kroah-Hartman 1718ab4382d2SGreg Kroah-Hartman BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); 1719ab4382d2SGreg Kroah-Hartman 1720deba1a0dSJean-Christophe PLAGNIOL-VILLARD port = &atmel_ports[pdata->num]; 1721ab4382d2SGreg Kroah-Hartman port->backup_imr = 0; 1722ab4382d2SGreg Kroah-Hartman 1723ab4382d2SGreg Kroah-Hartman atmel_init_port(port, pdev); 1724ab4382d2SGreg Kroah-Hartman 1725ab4382d2SGreg Kroah-Hartman if (!atmel_use_dma_rx(&port->uart)) { 1726ab4382d2SGreg Kroah-Hartman ret = -ENOMEM; 1727ab4382d2SGreg Kroah-Hartman data = kmalloc(sizeof(struct atmel_uart_char) 1728ab4382d2SGreg Kroah-Hartman * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); 1729ab4382d2SGreg Kroah-Hartman if (!data) 1730ab4382d2SGreg Kroah-Hartman goto err_alloc_ring; 1731ab4382d2SGreg Kroah-Hartman port->rx_ring.buf = data; 1732ab4382d2SGreg Kroah-Hartman } 1733ab4382d2SGreg Kroah-Hartman 1734ab4382d2SGreg Kroah-Hartman ret = uart_add_one_port(&atmel_uart, &port->uart); 1735ab4382d2SGreg Kroah-Hartman if (ret) 1736ab4382d2SGreg Kroah-Hartman goto err_add_port; 1737ab4382d2SGreg Kroah-Hartman 1738ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 1739ab4382d2SGreg Kroah-Hartman if (atmel_is_console_port(&port->uart) 1740ab4382d2SGreg Kroah-Hartman && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) { 1741ab4382d2SGreg Kroah-Hartman /* 1742ab4382d2SGreg Kroah-Hartman * The serial core enabled the clock for us, so undo 1743ab4382d2SGreg Kroah-Hartman * the clk_enable() in atmel_console_setup() 1744ab4382d2SGreg Kroah-Hartman */ 1745ab4382d2SGreg Kroah-Hartman clk_disable(port->clk); 1746ab4382d2SGreg Kroah-Hartman } 1747ab4382d2SGreg Kroah-Hartman #endif 1748ab4382d2SGreg Kroah-Hartman 1749ab4382d2SGreg Kroah-Hartman device_init_wakeup(&pdev->dev, 1); 1750ab4382d2SGreg Kroah-Hartman platform_set_drvdata(pdev, port); 1751ab4382d2SGreg Kroah-Hartman 1752fc887b15SLinus Torvalds if (port->rs485.flags & SER_RS485_ENABLED) { 1753fc887b15SLinus Torvalds UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL); 1754fc887b15SLinus Torvalds UART_PUT_CR(&port->uart, ATMEL_US_RTSEN); 1755fc887b15SLinus Torvalds } 1756fc887b15SLinus Torvalds 1757ab4382d2SGreg Kroah-Hartman return 0; 1758ab4382d2SGreg Kroah-Hartman 1759ab4382d2SGreg Kroah-Hartman err_add_port: 1760ab4382d2SGreg Kroah-Hartman kfree(port->rx_ring.buf); 1761ab4382d2SGreg Kroah-Hartman port->rx_ring.buf = NULL; 1762ab4382d2SGreg Kroah-Hartman err_alloc_ring: 1763ab4382d2SGreg Kroah-Hartman if (!atmel_is_console_port(&port->uart)) { 1764ab4382d2SGreg Kroah-Hartman clk_put(port->clk); 1765ab4382d2SGreg Kroah-Hartman port->clk = NULL; 1766ab4382d2SGreg Kroah-Hartman } 1767ab4382d2SGreg Kroah-Hartman 1768ab4382d2SGreg Kroah-Hartman return ret; 1769ab4382d2SGreg Kroah-Hartman } 1770ab4382d2SGreg Kroah-Hartman 1771ab4382d2SGreg Kroah-Hartman static int __devexit atmel_serial_remove(struct platform_device *pdev) 1772ab4382d2SGreg Kroah-Hartman { 1773ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 1774ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1775ab4382d2SGreg Kroah-Hartman int ret = 0; 1776ab4382d2SGreg Kroah-Hartman 1777ab4382d2SGreg Kroah-Hartman device_init_wakeup(&pdev->dev, 0); 1778ab4382d2SGreg Kroah-Hartman platform_set_drvdata(pdev, NULL); 1779ab4382d2SGreg Kroah-Hartman 1780ab4382d2SGreg Kroah-Hartman ret = uart_remove_one_port(&atmel_uart, port); 1781ab4382d2SGreg Kroah-Hartman 1782ab4382d2SGreg Kroah-Hartman tasklet_kill(&atmel_port->tasklet); 1783ab4382d2SGreg Kroah-Hartman kfree(atmel_port->rx_ring.buf); 1784ab4382d2SGreg Kroah-Hartman 1785ab4382d2SGreg Kroah-Hartman /* "port" is allocated statically, so we shouldn't free it */ 1786ab4382d2SGreg Kroah-Hartman 1787ab4382d2SGreg Kroah-Hartman clk_put(atmel_port->clk); 1788ab4382d2SGreg Kroah-Hartman 1789ab4382d2SGreg Kroah-Hartman return ret; 1790ab4382d2SGreg Kroah-Hartman } 1791ab4382d2SGreg Kroah-Hartman 1792ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = { 1793ab4382d2SGreg Kroah-Hartman .probe = atmel_serial_probe, 1794ab4382d2SGreg Kroah-Hartman .remove = __devexit_p(atmel_serial_remove), 1795ab4382d2SGreg Kroah-Hartman .suspend = atmel_serial_suspend, 1796ab4382d2SGreg Kroah-Hartman .resume = atmel_serial_resume, 1797ab4382d2SGreg Kroah-Hartman .driver = { 1798ab4382d2SGreg Kroah-Hartman .name = "atmel_usart", 1799ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 1800ab4382d2SGreg Kroah-Hartman }, 1801ab4382d2SGreg Kroah-Hartman }; 1802ab4382d2SGreg Kroah-Hartman 1803ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void) 1804ab4382d2SGreg Kroah-Hartman { 1805ab4382d2SGreg Kroah-Hartman int ret; 1806ab4382d2SGreg Kroah-Hartman 1807ab4382d2SGreg Kroah-Hartman ret = uart_register_driver(&atmel_uart); 1808ab4382d2SGreg Kroah-Hartman if (ret) 1809ab4382d2SGreg Kroah-Hartman return ret; 1810ab4382d2SGreg Kroah-Hartman 1811ab4382d2SGreg Kroah-Hartman ret = platform_driver_register(&atmel_serial_driver); 1812ab4382d2SGreg Kroah-Hartman if (ret) 1813ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&atmel_uart); 1814ab4382d2SGreg Kroah-Hartman 1815ab4382d2SGreg Kroah-Hartman return ret; 1816ab4382d2SGreg Kroah-Hartman } 1817ab4382d2SGreg Kroah-Hartman 1818ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void) 1819ab4382d2SGreg Kroah-Hartman { 1820ab4382d2SGreg Kroah-Hartman platform_driver_unregister(&atmel_serial_driver); 1821ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&atmel_uart); 1822ab4382d2SGreg Kroah-Hartman } 1823ab4382d2SGreg Kroah-Hartman 1824ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init); 1825ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit); 1826ab4382d2SGreg Kroah-Hartman 1827ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson"); 1828ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver"); 1829ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 1830ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart"); 1831