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> 365fbe46b6SNicolas Ferre #include <linux/of.h> 375fbe46b6SNicolas Ferre #include <linux/of_device.h> 38354e57f3SLinus Walleij #include <linux/of_gpio.h> 39ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h> 40ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h> 41ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h> 42ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h> 43bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h> 442e68c22fSElen Song #include <linux/timer.h> 45354e57f3SLinus Walleij #include <linux/gpio.h> 46e0b0baadSRichard Genoud #include <linux/gpio/consumer.h> 47e0b0baadSRichard Genoud #include <linux/err.h> 48*ab5e4e41SRichard Genoud #include <linux/irq.h> 49ab4382d2SGreg Kroah-Hartman 50ab4382d2SGreg Kroah-Hartman #include <asm/io.h> 51ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h> 52ab4382d2SGreg Kroah-Hartman 53ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE 512 54ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */ 55ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */ 56ab4382d2SGreg Kroah-Hartman 57ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 58ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ 59ab4382d2SGreg Kroah-Hartman #endif 60ab4382d2SGreg Kroah-Hartman 61ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 62ab4382d2SGreg Kroah-Hartman 63e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h" 64e0b0baadSRichard Genoud 65ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port); 66ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port); 67ab4382d2SGreg Kroah-Hartman 68ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT 69ab4382d2SGreg Kroah-Hartman 70ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we 71ab4382d2SGreg Kroah-Hartman * should coexist with the 8250 driver, such as if we have an external 16C550 72ab4382d2SGreg Kroah-Hartman * UART. */ 73ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR 204 74ab4382d2SGreg Kroah-Hartman #define MINOR_START 154 75ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyAT" 76ab4382d2SGreg Kroah-Hartman 77ab4382d2SGreg Kroah-Hartman #else 78ab4382d2SGreg Kroah-Hartman 79ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port 80ab4382d2SGreg Kroah-Hartman * name, but it is legally reserved for the 8250 driver. */ 81ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR TTY_MAJOR 82ab4382d2SGreg Kroah-Hartman #define MINOR_START 64 83ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyS" 84ab4382d2SGreg Kroah-Hartman 85ab4382d2SGreg Kroah-Hartman #endif 86ab4382d2SGreg Kroah-Hartman 87ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT 256 88ab4382d2SGreg Kroah-Hartman 89ab4382d2SGreg Kroah-Hartman /* UART registers. CR is write-only, hence no GET macro */ 90ab4382d2SGreg Kroah-Hartman #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR) 91ab4382d2SGreg Kroah-Hartman #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR) 92ab4382d2SGreg Kroah-Hartman #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR) 93ab4382d2SGreg Kroah-Hartman #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER) 94ab4382d2SGreg Kroah-Hartman #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR) 95ab4382d2SGreg Kroah-Hartman #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR) 96ab4382d2SGreg Kroah-Hartman #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR) 97ab4382d2SGreg Kroah-Hartman #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR) 98ab4382d2SGreg Kroah-Hartman #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR) 99ab4382d2SGreg Kroah-Hartman #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR) 100ab4382d2SGreg Kroah-Hartman #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR) 101ab4382d2SGreg Kroah-Hartman #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR) 102ab4382d2SGreg Kroah-Hartman #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR) 103055560b0SElen Song #define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME) 104731d9caeSNicolas Ferre #define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION) 105ab4382d2SGreg Kroah-Hartman 106ab4382d2SGreg Kroah-Hartman /* PDC registers */ 107ab4382d2SGreg Kroah-Hartman #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR) 108ab4382d2SGreg Kroah-Hartman #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR) 109ab4382d2SGreg Kroah-Hartman 110ab4382d2SGreg Kroah-Hartman #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR) 111ab4382d2SGreg Kroah-Hartman #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR) 112ab4382d2SGreg Kroah-Hartman #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR) 113ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR) 114ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR) 115ab4382d2SGreg Kroah-Hartman 116ab4382d2SGreg Kroah-Hartman #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR) 117ab4382d2SGreg Kroah-Hartman #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR) 118ab4382d2SGreg Kroah-Hartman #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR) 119ab4382d2SGreg Kroah-Hartman 120ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer { 121ab4382d2SGreg Kroah-Hartman unsigned char *buf; 122ab4382d2SGreg Kroah-Hartman dma_addr_t dma_addr; 123ab4382d2SGreg Kroah-Hartman unsigned int dma_size; 124ab4382d2SGreg Kroah-Hartman unsigned int ofs; 125ab4382d2SGreg Kroah-Hartman }; 126ab4382d2SGreg Kroah-Hartman 127ab4382d2SGreg Kroah-Hartman struct atmel_uart_char { 128ab4382d2SGreg Kroah-Hartman u16 status; 129ab4382d2SGreg Kroah-Hartman u16 ch; 130ab4382d2SGreg Kroah-Hartman }; 131ab4382d2SGreg Kroah-Hartman 132ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024 133ab4382d2SGreg Kroah-Hartman 134ab4382d2SGreg Kroah-Hartman /* 135ab4382d2SGreg Kroah-Hartman * We wrap our port structure around the generic uart_port. 136ab4382d2SGreg Kroah-Hartman */ 137ab4382d2SGreg Kroah-Hartman struct atmel_uart_port { 138ab4382d2SGreg Kroah-Hartman struct uart_port uart; /* uart */ 139ab4382d2SGreg Kroah-Hartman struct clk *clk; /* uart clock */ 140ab4382d2SGreg Kroah-Hartman int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */ 141ab4382d2SGreg Kroah-Hartman u32 backup_imr; /* IMR saved during suspend */ 142ab4382d2SGreg Kroah-Hartman int break_active; /* break being received */ 143ab4382d2SGreg Kroah-Hartman 14434df42f5SElen Song bool use_dma_rx; /* enable DMA receiver */ 14564e22ebeSElen Song bool use_pdc_rx; /* enable PDC receiver */ 146ab4382d2SGreg Kroah-Hartman short pdc_rx_idx; /* current PDC RX buffer */ 147ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ 148ab4382d2SGreg Kroah-Hartman 14908f738beSElen Song bool use_dma_tx; /* enable DMA transmitter */ 15064e22ebeSElen Song bool use_pdc_tx; /* enable PDC transmitter */ 151ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ 152ab4382d2SGreg Kroah-Hartman 15308f738beSElen Song spinlock_t lock_tx; /* port lock */ 15434df42f5SElen Song spinlock_t lock_rx; /* port lock */ 15508f738beSElen Song struct dma_chan *chan_tx; 15634df42f5SElen Song struct dma_chan *chan_rx; 15708f738beSElen Song struct dma_async_tx_descriptor *desc_tx; 15834df42f5SElen Song struct dma_async_tx_descriptor *desc_rx; 15908f738beSElen Song dma_cookie_t cookie_tx; 16034df42f5SElen Song dma_cookie_t cookie_rx; 16108f738beSElen Song struct scatterlist sg_tx; 16234df42f5SElen Song struct scatterlist sg_rx; 163ab4382d2SGreg Kroah-Hartman struct tasklet_struct tasklet; 164ab4382d2SGreg Kroah-Hartman unsigned int irq_status; 165ab4382d2SGreg Kroah-Hartman unsigned int irq_status_prev; 166ab4382d2SGreg Kroah-Hartman 167ab4382d2SGreg Kroah-Hartman struct circ_buf rx_ring; 168ab4382d2SGreg Kroah-Hartman 169ab4382d2SGreg Kroah-Hartman struct serial_rs485 rs485; /* rs485 settings */ 170e0b0baadSRichard Genoud struct mctrl_gpios *gpios; 171*ab5e4e41SRichard Genoud int gpio_irq[UART_GPIO_MAX]; 172ab4382d2SGreg Kroah-Hartman unsigned int tx_done_mask; 173*ab5e4e41SRichard Genoud bool ms_irq_enabled; 174055560b0SElen Song bool is_usart; /* usart or uart */ 1752e68c22fSElen Song struct timer_list uart_timer; /* uart timer */ 176a930e528SElen Song int (*prepare_rx)(struct uart_port *port); 177a930e528SElen Song int (*prepare_tx)(struct uart_port *port); 178a930e528SElen Song void (*schedule_rx)(struct uart_port *port); 179a930e528SElen Song void (*schedule_tx)(struct uart_port *port); 180a930e528SElen Song void (*release_rx)(struct uart_port *port); 181a930e528SElen Song void (*release_tx)(struct uart_port *port); 182ab4382d2SGreg Kroah-Hartman }; 183ab4382d2SGreg Kroah-Hartman 184ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; 185503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART); 186ab4382d2SGreg Kroah-Hartman 187ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ 188ab4382d2SGreg Kroah-Hartman static struct console atmel_console; 189ab4382d2SGreg Kroah-Hartman #endif 190ab4382d2SGreg Kroah-Hartman 1915fbe46b6SNicolas Ferre #if defined(CONFIG_OF) 1925fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = { 1935fbe46b6SNicolas Ferre { .compatible = "atmel,at91rm9200-usart" }, 1945fbe46b6SNicolas Ferre { .compatible = "atmel,at91sam9260-usart" }, 1955fbe46b6SNicolas Ferre { /* sentinel */ } 1965fbe46b6SNicolas Ferre }; 1975fbe46b6SNicolas Ferre 1985fbe46b6SNicolas Ferre MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids); 1995fbe46b6SNicolas Ferre #endif 2005fbe46b6SNicolas Ferre 201ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port * 202ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart) 203ab4382d2SGreg Kroah-Hartman { 204ab4382d2SGreg Kroah-Hartman return container_of(uart, struct atmel_uart_port, uart); 205ab4382d2SGreg Kroah-Hartman } 206ab4382d2SGreg Kroah-Hartman 207ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC 20864e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port) 209ab4382d2SGreg Kroah-Hartman { 210ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 211ab4382d2SGreg Kroah-Hartman 21264e22ebeSElen Song return atmel_port->use_pdc_rx; 213ab4382d2SGreg Kroah-Hartman } 214ab4382d2SGreg Kroah-Hartman 21564e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port) 216ab4382d2SGreg Kroah-Hartman { 217ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 218ab4382d2SGreg Kroah-Hartman 21964e22ebeSElen Song return atmel_port->use_pdc_tx; 220ab4382d2SGreg Kroah-Hartman } 221ab4382d2SGreg Kroah-Hartman #else 22264e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port) 223ab4382d2SGreg Kroah-Hartman { 224ab4382d2SGreg Kroah-Hartman return false; 225ab4382d2SGreg Kroah-Hartman } 226ab4382d2SGreg Kroah-Hartman 22764e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port) 228ab4382d2SGreg Kroah-Hartman { 229ab4382d2SGreg Kroah-Hartman return false; 230ab4382d2SGreg Kroah-Hartman } 231ab4382d2SGreg Kroah-Hartman #endif 232ab4382d2SGreg Kroah-Hartman 23308f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port) 23408f738beSElen Song { 23508f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 23608f738beSElen Song 23708f738beSElen Song return atmel_port->use_dma_tx; 23808f738beSElen Song } 23908f738beSElen Song 24034df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port) 24134df42f5SElen Song { 24234df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 24334df42f5SElen Song 24434df42f5SElen Song return atmel_port->use_dma_rx; 24534df42f5SElen Song } 24634df42f5SElen Song 247e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port) 248e0b0baadSRichard Genoud { 249e0b0baadSRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 250e0b0baadSRichard Genoud unsigned int status, ret = 0; 251e0b0baadSRichard Genoud 252e0b0baadSRichard Genoud status = UART_GET_CSR(port); 253e0b0baadSRichard Genoud 254e0b0baadSRichard Genoud mctrl_gpio_get(atmel_port->gpios, &ret); 255e0b0baadSRichard Genoud 256e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 257e0b0baadSRichard Genoud UART_GPIO_CTS))) { 258e0b0baadSRichard Genoud if (ret & TIOCM_CTS) 259e0b0baadSRichard Genoud status &= ~ATMEL_US_CTS; 260e0b0baadSRichard Genoud else 261e0b0baadSRichard Genoud status |= ATMEL_US_CTS; 262e0b0baadSRichard Genoud } 263e0b0baadSRichard Genoud 264e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 265e0b0baadSRichard Genoud UART_GPIO_DSR))) { 266e0b0baadSRichard Genoud if (ret & TIOCM_DSR) 267e0b0baadSRichard Genoud status &= ~ATMEL_US_DSR; 268e0b0baadSRichard Genoud else 269e0b0baadSRichard Genoud status |= ATMEL_US_DSR; 270e0b0baadSRichard Genoud } 271e0b0baadSRichard Genoud 272e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 273e0b0baadSRichard Genoud UART_GPIO_RI))) { 274e0b0baadSRichard Genoud if (ret & TIOCM_RI) 275e0b0baadSRichard Genoud status &= ~ATMEL_US_RI; 276e0b0baadSRichard Genoud else 277e0b0baadSRichard Genoud status |= ATMEL_US_RI; 278e0b0baadSRichard Genoud } 279e0b0baadSRichard Genoud 280e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 281e0b0baadSRichard Genoud UART_GPIO_DCD))) { 282e0b0baadSRichard Genoud if (ret & TIOCM_CD) 283e0b0baadSRichard Genoud status &= ~ATMEL_US_DCD; 284e0b0baadSRichard Genoud else 285e0b0baadSRichard Genoud status |= ATMEL_US_DCD; 286e0b0baadSRichard Genoud } 287e0b0baadSRichard Genoud 288e0b0baadSRichard Genoud return status; 289e0b0baadSRichard Genoud } 290e0b0baadSRichard Genoud 291ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */ 292ab4382d2SGreg Kroah-Hartman void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf) 293ab4382d2SGreg Kroah-Hartman { 294ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 295ab4382d2SGreg Kroah-Hartman unsigned int mode; 296dbf1115dSClaudio Scordino unsigned long flags; 297ab4382d2SGreg Kroah-Hartman 298dbf1115dSClaudio Scordino spin_lock_irqsave(&port->lock, flags); 299ab4382d2SGreg Kroah-Hartman 300ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 301ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, atmel_port->tx_done_mask); 302ab4382d2SGreg Kroah-Hartman 303ab4382d2SGreg Kroah-Hartman mode = UART_GET_MR(port); 304ab4382d2SGreg Kroah-Hartman 305ab4382d2SGreg Kroah-Hartman /* Resetting serial mode to RS232 (0x0) */ 306ab4382d2SGreg Kroah-Hartman mode &= ~ATMEL_US_USMODE; 307ab4382d2SGreg Kroah-Hartman 308ab4382d2SGreg Kroah-Hartman atmel_port->rs485 = *rs485conf; 309ab4382d2SGreg Kroah-Hartman 310ab4382d2SGreg Kroah-Hartman if (rs485conf->flags & SER_RS485_ENABLED) { 311ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS485\n"); 312ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 31393f3350cSClaudio Scordino if ((rs485conf->delay_rts_after_send) > 0) 314ab4382d2SGreg Kroah-Hartman UART_PUT_TTGR(port, rs485conf->delay_rts_after_send); 315ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 316ab4382d2SGreg Kroah-Hartman } else { 317ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS232\n"); 31864e22ebeSElen Song if (atmel_use_pdc_tx(port)) 319ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | 320ab4382d2SGreg Kroah-Hartman ATMEL_US_TXBUFE; 321ab4382d2SGreg Kroah-Hartman else 322ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 323ab4382d2SGreg Kroah-Hartman } 324ab4382d2SGreg Kroah-Hartman UART_PUT_MR(port, mode); 325ab4382d2SGreg Kroah-Hartman 326ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 327ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 328ab4382d2SGreg Kroah-Hartman 329dbf1115dSClaudio Scordino spin_unlock_irqrestore(&port->lock, flags); 330ab4382d2SGreg Kroah-Hartman 331ab4382d2SGreg Kroah-Hartman } 332ab4382d2SGreg Kroah-Hartman 333ab4382d2SGreg Kroah-Hartman /* 334ab4382d2SGreg Kroah-Hartman * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. 335ab4382d2SGreg Kroah-Hartman */ 336ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port) 337ab4382d2SGreg Kroah-Hartman { 338ab4382d2SGreg Kroah-Hartman return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0; 339ab4382d2SGreg Kroah-Hartman } 340ab4382d2SGreg Kroah-Hartman 341ab4382d2SGreg Kroah-Hartman /* 342ab4382d2SGreg Kroah-Hartman * Set state of the modem control output lines 343ab4382d2SGreg Kroah-Hartman */ 344ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) 345ab4382d2SGreg Kroah-Hartman { 346ab4382d2SGreg Kroah-Hartman unsigned int control = 0; 347ab4382d2SGreg Kroah-Hartman unsigned int mode; 348ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 349ab4382d2SGreg Kroah-Hartman 350ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_RTS) 351ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_RTSEN; 352ab4382d2SGreg Kroah-Hartman else 353ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_RTSDIS; 354ab4382d2SGreg Kroah-Hartman 355ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_DTR) 356ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTREN; 357ab4382d2SGreg Kroah-Hartman else 358ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTRDIS; 359ab4382d2SGreg Kroah-Hartman 360ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, control); 361ab4382d2SGreg Kroah-Hartman 362e0b0baadSRichard Genoud mctrl_gpio_set(atmel_port->gpios, mctrl); 363e0b0baadSRichard Genoud 364ab4382d2SGreg Kroah-Hartman /* Local loopback mode? */ 365ab4382d2SGreg Kroah-Hartman mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE; 366ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_LOOP) 367ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_LOC_LOOP; 368ab4382d2SGreg Kroah-Hartman else 369ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_NORMAL; 370ab4382d2SGreg Kroah-Hartman 371ab4382d2SGreg Kroah-Hartman /* Resetting serial mode to RS232 (0x0) */ 372ab4382d2SGreg Kroah-Hartman mode &= ~ATMEL_US_USMODE; 373ab4382d2SGreg Kroah-Hartman 374ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_ENABLED) { 375ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS485\n"); 37693f3350cSClaudio Scordino if ((atmel_port->rs485.delay_rts_after_send) > 0) 377ab4382d2SGreg Kroah-Hartman UART_PUT_TTGR(port, 378ab4382d2SGreg Kroah-Hartman atmel_port->rs485.delay_rts_after_send); 379ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 380ab4382d2SGreg Kroah-Hartman } else { 381ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS232\n"); 382ab4382d2SGreg Kroah-Hartman } 383ab4382d2SGreg Kroah-Hartman UART_PUT_MR(port, mode); 384ab4382d2SGreg Kroah-Hartman } 385ab4382d2SGreg Kroah-Hartman 386ab4382d2SGreg Kroah-Hartman /* 387ab4382d2SGreg Kroah-Hartman * Get state of the modem control input lines 388ab4382d2SGreg Kroah-Hartman */ 389ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port) 390ab4382d2SGreg Kroah-Hartman { 391e0b0baadSRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 392e0b0baadSRichard Genoud unsigned int ret = 0, status; 393ab4382d2SGreg Kroah-Hartman 394ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 395ab4382d2SGreg Kroah-Hartman 396ab4382d2SGreg Kroah-Hartman /* 397ab4382d2SGreg Kroah-Hartman * The control signals are active low. 398ab4382d2SGreg Kroah-Hartman */ 399ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DCD)) 400ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CD; 401ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_CTS)) 402ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CTS; 403ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DSR)) 404ab4382d2SGreg Kroah-Hartman ret |= TIOCM_DSR; 405ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_RI)) 406ab4382d2SGreg Kroah-Hartman ret |= TIOCM_RI; 407ab4382d2SGreg Kroah-Hartman 408e0b0baadSRichard Genoud return mctrl_gpio_get(atmel_port->gpios, &ret); 409ab4382d2SGreg Kroah-Hartman } 410ab4382d2SGreg Kroah-Hartman 411ab4382d2SGreg Kroah-Hartman /* 412ab4382d2SGreg Kroah-Hartman * Stop transmitting. 413ab4382d2SGreg Kroah-Hartman */ 414ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port) 415ab4382d2SGreg Kroah-Hartman { 416ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 417ab4382d2SGreg Kroah-Hartman 41864e22ebeSElen Song if (atmel_use_pdc_tx(port)) { 419ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 420ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 421ab4382d2SGreg Kroah-Hartman } 422ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 423ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, atmel_port->tx_done_mask); 424ab4382d2SGreg Kroah-Hartman 42583cac9f3SBernhard Roth if ((atmel_port->rs485.flags & SER_RS485_ENABLED) && 42683cac9f3SBernhard Roth !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) 427ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 428ab4382d2SGreg Kroah-Hartman } 429ab4382d2SGreg Kroah-Hartman 430ab4382d2SGreg Kroah-Hartman /* 431ab4382d2SGreg Kroah-Hartman * Start transmitting. 432ab4382d2SGreg Kroah-Hartman */ 433ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port) 434ab4382d2SGreg Kroah-Hartman { 435ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 436ab4382d2SGreg Kroah-Hartman 43764e22ebeSElen Song if (atmel_use_pdc_tx(port)) { 438ab4382d2SGreg Kroah-Hartman if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN) 439ab4382d2SGreg Kroah-Hartman /* The transmitter is already running. Yes, we 440ab4382d2SGreg Kroah-Hartman really need this.*/ 441ab4382d2SGreg Kroah-Hartman return; 442ab4382d2SGreg Kroah-Hartman 44383cac9f3SBernhard Roth if ((atmel_port->rs485.flags & SER_RS485_ENABLED) && 44483cac9f3SBernhard Roth !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) 445ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 446ab4382d2SGreg Kroah-Hartman 447ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 448ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); 449ab4382d2SGreg Kroah-Hartman } 450ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 451ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 452ab4382d2SGreg Kroah-Hartman } 453ab4382d2SGreg Kroah-Hartman 454ab4382d2SGreg Kroah-Hartman /* 455ab4382d2SGreg Kroah-Hartman * start receiving - port is in process of being opened. 456ab4382d2SGreg Kroah-Hartman */ 457ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port) 458ab4382d2SGreg Kroah-Hartman { 459ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */ 460ab4382d2SGreg Kroah-Hartman 46157c36868SSiftar, Gabe UART_PUT_CR(port, ATMEL_US_RXEN); 46257c36868SSiftar, Gabe 46364e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 464ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 465ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 466ab4382d2SGreg Kroah-Hartman port->read_status_mask); 467ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); 468ab4382d2SGreg Kroah-Hartman } else { 469ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_RXRDY); 470ab4382d2SGreg Kroah-Hartman } 471ab4382d2SGreg Kroah-Hartman } 472ab4382d2SGreg Kroah-Hartman 473ab4382d2SGreg Kroah-Hartman /* 474ab4382d2SGreg Kroah-Hartman * Stop receiving - port is in process of being closed. 475ab4382d2SGreg Kroah-Hartman */ 476ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port) 477ab4382d2SGreg Kroah-Hartman { 47857c36868SSiftar, Gabe UART_PUT_CR(port, ATMEL_US_RXDIS); 47957c36868SSiftar, Gabe 48064e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 481ab4382d2SGreg Kroah-Hartman /* disable PDC receive */ 482ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS); 483ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 484ab4382d2SGreg Kroah-Hartman port->read_status_mask); 485ab4382d2SGreg Kroah-Hartman } else { 486ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXRDY); 487ab4382d2SGreg Kroah-Hartman } 488ab4382d2SGreg Kroah-Hartman } 489ab4382d2SGreg Kroah-Hartman 490ab4382d2SGreg Kroah-Hartman /* 491ab4382d2SGreg Kroah-Hartman * Enable modem status interrupts 492ab4382d2SGreg Kroah-Hartman */ 493ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port) 494ab4382d2SGreg Kroah-Hartman { 495*ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 496*ab5e4e41SRichard Genoud uint32_t ier = 0; 497*ab5e4e41SRichard Genoud 498*ab5e4e41SRichard Genoud /* 499*ab5e4e41SRichard Genoud * Interrupt should not be enabled twice 500*ab5e4e41SRichard Genoud */ 501*ab5e4e41SRichard Genoud if (atmel_port->ms_irq_enabled) 502*ab5e4e41SRichard Genoud return; 503*ab5e4e41SRichard Genoud 504*ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = true; 505*ab5e4e41SRichard Genoud 506*ab5e4e41SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0) 507*ab5e4e41SRichard Genoud enable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]); 508*ab5e4e41SRichard Genoud else 509*ab5e4e41SRichard Genoud ier |= ATMEL_US_CTSIC; 510*ab5e4e41SRichard Genoud 511*ab5e4e41SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0) 512*ab5e4e41SRichard Genoud enable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]); 513*ab5e4e41SRichard Genoud else 514*ab5e4e41SRichard Genoud ier |= ATMEL_US_DSRIC; 515*ab5e4e41SRichard Genoud 516*ab5e4e41SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0) 517*ab5e4e41SRichard Genoud enable_irq(atmel_port->gpio_irq[UART_GPIO_RI]); 518*ab5e4e41SRichard Genoud else 519*ab5e4e41SRichard Genoud ier |= ATMEL_US_RIIC; 520*ab5e4e41SRichard Genoud 521*ab5e4e41SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0) 522*ab5e4e41SRichard Genoud enable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]); 523*ab5e4e41SRichard Genoud else 524*ab5e4e41SRichard Genoud ier |= ATMEL_US_DCDIC; 525*ab5e4e41SRichard Genoud 526*ab5e4e41SRichard Genoud UART_PUT_IER(port, ier); 527ab4382d2SGreg Kroah-Hartman } 528ab4382d2SGreg Kroah-Hartman 529ab4382d2SGreg Kroah-Hartman /* 530ab4382d2SGreg Kroah-Hartman * Control the transmission of a break signal 531ab4382d2SGreg Kroah-Hartman */ 532ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state) 533ab4382d2SGreg Kroah-Hartman { 534ab4382d2SGreg Kroah-Hartman if (break_state != 0) 535ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */ 536ab4382d2SGreg Kroah-Hartman else 537ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */ 538ab4382d2SGreg Kroah-Hartman } 539ab4382d2SGreg Kroah-Hartman 540ab4382d2SGreg Kroah-Hartman /* 541ab4382d2SGreg Kroah-Hartman * Stores the incoming character in the ring buffer 542ab4382d2SGreg Kroah-Hartman */ 543ab4382d2SGreg Kroah-Hartman static void 544ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status, 545ab4382d2SGreg Kroah-Hartman unsigned int ch) 546ab4382d2SGreg Kroah-Hartman { 547ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 548ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 549ab4382d2SGreg Kroah-Hartman struct atmel_uart_char *c; 550ab4382d2SGreg Kroah-Hartman 551ab4382d2SGreg Kroah-Hartman if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) 552ab4382d2SGreg Kroah-Hartman /* Buffer overflow, ignore char */ 553ab4382d2SGreg Kroah-Hartman return; 554ab4382d2SGreg Kroah-Hartman 555ab4382d2SGreg Kroah-Hartman c = &((struct atmel_uart_char *)ring->buf)[ring->head]; 556ab4382d2SGreg Kroah-Hartman c->status = status; 557ab4382d2SGreg Kroah-Hartman c->ch = ch; 558ab4382d2SGreg Kroah-Hartman 559ab4382d2SGreg Kroah-Hartman /* Make sure the character is stored before we update head. */ 560ab4382d2SGreg Kroah-Hartman smp_wmb(); 561ab4382d2SGreg Kroah-Hartman 562ab4382d2SGreg Kroah-Hartman ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 563ab4382d2SGreg Kroah-Hartman } 564ab4382d2SGreg Kroah-Hartman 565ab4382d2SGreg Kroah-Hartman /* 566ab4382d2SGreg Kroah-Hartman * Deal with parity, framing and overrun errors. 567ab4382d2SGreg Kroah-Hartman */ 568ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status) 569ab4382d2SGreg Kroah-Hartman { 570ab4382d2SGreg Kroah-Hartman /* clear error */ 571ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); 572ab4382d2SGreg Kroah-Hartman 573ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 574ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 575ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 576ab4382d2SGreg Kroah-Hartman port->icount.brk++; 577ab4382d2SGreg Kroah-Hartman } 578ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 579ab4382d2SGreg Kroah-Hartman port->icount.parity++; 580ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 581ab4382d2SGreg Kroah-Hartman port->icount.frame++; 582ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 583ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 584ab4382d2SGreg Kroah-Hartman } 585ab4382d2SGreg Kroah-Hartman 586ab4382d2SGreg Kroah-Hartman /* 587ab4382d2SGreg Kroah-Hartman * Characters received (called from interrupt handler) 588ab4382d2SGreg Kroah-Hartman */ 589ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port) 590ab4382d2SGreg Kroah-Hartman { 591ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 592ab4382d2SGreg Kroah-Hartman unsigned int status, ch; 593ab4382d2SGreg Kroah-Hartman 594ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 595ab4382d2SGreg Kroah-Hartman while (status & ATMEL_US_RXRDY) { 596ab4382d2SGreg Kroah-Hartman ch = UART_GET_CHAR(port); 597ab4382d2SGreg Kroah-Hartman 598ab4382d2SGreg Kroah-Hartman /* 599ab4382d2SGreg Kroah-Hartman * note that the error handling code is 600ab4382d2SGreg Kroah-Hartman * out of the main execution path 601ab4382d2SGreg Kroah-Hartman */ 602ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 603ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK) 604ab4382d2SGreg Kroah-Hartman || atmel_port->break_active)) { 605ab4382d2SGreg Kroah-Hartman 606ab4382d2SGreg Kroah-Hartman /* clear error */ 607ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); 608ab4382d2SGreg Kroah-Hartman 609ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK 610ab4382d2SGreg Kroah-Hartman && !atmel_port->break_active) { 611ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 1; 612ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_RXBRK); 613ab4382d2SGreg Kroah-Hartman } else { 614ab4382d2SGreg Kroah-Hartman /* 615ab4382d2SGreg Kroah-Hartman * This is either the end-of-break 616ab4382d2SGreg Kroah-Hartman * condition or we've received at 617ab4382d2SGreg Kroah-Hartman * least one character without RXBRK 618ab4382d2SGreg Kroah-Hartman * being set. In both cases, the next 619ab4382d2SGreg Kroah-Hartman * RXBRK will indicate start-of-break. 620ab4382d2SGreg Kroah-Hartman */ 621ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXBRK); 622ab4382d2SGreg Kroah-Hartman status &= ~ATMEL_US_RXBRK; 623ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 624ab4382d2SGreg Kroah-Hartman } 625ab4382d2SGreg Kroah-Hartman } 626ab4382d2SGreg Kroah-Hartman 627ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(port, status, ch); 628ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 629ab4382d2SGreg Kroah-Hartman } 630ab4382d2SGreg Kroah-Hartman 631ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 632ab4382d2SGreg Kroah-Hartman } 633ab4382d2SGreg Kroah-Hartman 634ab4382d2SGreg Kroah-Hartman /* 635ab4382d2SGreg Kroah-Hartman * Transmit characters (called from tasklet with TXRDY interrupt 636ab4382d2SGreg Kroah-Hartman * disabled) 637ab4382d2SGreg Kroah-Hartman */ 638ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port) 639ab4382d2SGreg Kroah-Hartman { 640ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 641ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 642ab4382d2SGreg Kroah-Hartman 643ab4382d2SGreg Kroah-Hartman if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) { 644ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, port->x_char); 645ab4382d2SGreg Kroah-Hartman port->icount.tx++; 646ab4382d2SGreg Kroah-Hartman port->x_char = 0; 647ab4382d2SGreg Kroah-Hartman } 648ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 649ab4382d2SGreg Kroah-Hartman return; 650ab4382d2SGreg Kroah-Hartman 651ab4382d2SGreg Kroah-Hartman while (UART_GET_CSR(port) & atmel_port->tx_done_mask) { 652ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, xmit->buf[xmit->tail]); 653ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 654ab4382d2SGreg Kroah-Hartman port->icount.tx++; 655ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit)) 656ab4382d2SGreg Kroah-Hartman break; 657ab4382d2SGreg Kroah-Hartman } 658ab4382d2SGreg Kroah-Hartman 659ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 660ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 661ab4382d2SGreg Kroah-Hartman 662ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit)) 663ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 664ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 665ab4382d2SGreg Kroah-Hartman } 666ab4382d2SGreg Kroah-Hartman 66708f738beSElen Song static void atmel_complete_tx_dma(void *arg) 66808f738beSElen Song { 66908f738beSElen Song struct atmel_uart_port *atmel_port = arg; 67008f738beSElen Song struct uart_port *port = &atmel_port->uart; 67108f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 67208f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 67308f738beSElen Song unsigned long flags; 67408f738beSElen Song 67508f738beSElen Song spin_lock_irqsave(&port->lock, flags); 67608f738beSElen Song 67708f738beSElen Song if (chan) 67808f738beSElen Song dmaengine_terminate_all(chan); 67908f738beSElen Song xmit->tail += sg_dma_len(&atmel_port->sg_tx); 68008f738beSElen Song xmit->tail &= UART_XMIT_SIZE - 1; 68108f738beSElen Song 68208f738beSElen Song port->icount.tx += sg_dma_len(&atmel_port->sg_tx); 68308f738beSElen Song 68408f738beSElen Song spin_lock_irq(&atmel_port->lock_tx); 68508f738beSElen Song async_tx_ack(atmel_port->desc_tx); 68608f738beSElen Song atmel_port->cookie_tx = -EINVAL; 68708f738beSElen Song atmel_port->desc_tx = NULL; 68808f738beSElen Song spin_unlock_irq(&atmel_port->lock_tx); 68908f738beSElen Song 69008f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 69108f738beSElen Song uart_write_wakeup(port); 69208f738beSElen Song 69308f738beSElen Song /* Do we really need this? */ 69408f738beSElen Song if (!uart_circ_empty(xmit)) 69508f738beSElen Song tasklet_schedule(&atmel_port->tasklet); 69608f738beSElen Song 69708f738beSElen Song spin_unlock_irqrestore(&port->lock, flags); 69808f738beSElen Song } 69908f738beSElen Song 70008f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port) 70108f738beSElen Song { 70208f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 70308f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 70408f738beSElen Song 70508f738beSElen Song if (chan) { 70608f738beSElen Song dmaengine_terminate_all(chan); 70708f738beSElen Song dma_release_channel(chan); 70808f738beSElen Song dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1, 70908f738beSElen Song DMA_MEM_TO_DEV); 71008f738beSElen Song } 71108f738beSElen Song 71208f738beSElen Song atmel_port->desc_tx = NULL; 71308f738beSElen Song atmel_port->chan_tx = NULL; 71408f738beSElen Song atmel_port->cookie_tx = -EINVAL; 71508f738beSElen Song } 71608f738beSElen Song 71708f738beSElen Song /* 71808f738beSElen Song * Called from tasklet with TXRDY interrupt is disabled. 71908f738beSElen Song */ 72008f738beSElen Song static void atmel_tx_dma(struct uart_port *port) 72108f738beSElen Song { 72208f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 72308f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 72408f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 72508f738beSElen Song struct dma_async_tx_descriptor *desc; 72608f738beSElen Song struct scatterlist *sg = &atmel_port->sg_tx; 72708f738beSElen Song 72808f738beSElen Song /* Make sure we have an idle channel */ 72908f738beSElen Song if (atmel_port->desc_tx != NULL) 73008f738beSElen Song return; 73108f738beSElen Song 73208f738beSElen Song if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 73308f738beSElen Song /* 73408f738beSElen Song * DMA is idle now. 73508f738beSElen Song * Port xmit buffer is already mapped, 73608f738beSElen Song * and it is one page... Just adjust 73708f738beSElen Song * offsets and lengths. Since it is a circular buffer, 73808f738beSElen Song * we have to transmit till the end, and then the rest. 73908f738beSElen Song * Take the port lock to get a 74008f738beSElen Song * consistent xmit buffer state. 74108f738beSElen Song */ 74208f738beSElen Song sg->offset = xmit->tail & (UART_XMIT_SIZE - 1); 74308f738beSElen Song sg_dma_address(sg) = (sg_dma_address(sg) & 74408f738beSElen Song ~(UART_XMIT_SIZE - 1)) 74508f738beSElen Song + sg->offset; 74608f738beSElen Song sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head, 74708f738beSElen Song xmit->tail, 74808f738beSElen Song UART_XMIT_SIZE); 74908f738beSElen Song BUG_ON(!sg_dma_len(sg)); 75008f738beSElen Song 75108f738beSElen Song desc = dmaengine_prep_slave_sg(chan, 75208f738beSElen Song sg, 75308f738beSElen Song 1, 75408f738beSElen Song DMA_MEM_TO_DEV, 75508f738beSElen Song DMA_PREP_INTERRUPT | 75608f738beSElen Song DMA_CTRL_ACK); 75708f738beSElen Song if (!desc) { 75808f738beSElen Song dev_err(port->dev, "Failed to send via dma!\n"); 75908f738beSElen Song return; 76008f738beSElen Song } 76108f738beSElen Song 76208f738beSElen Song dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV); 76308f738beSElen Song 76408f738beSElen Song atmel_port->desc_tx = desc; 76508f738beSElen Song desc->callback = atmel_complete_tx_dma; 76608f738beSElen Song desc->callback_param = atmel_port; 76708f738beSElen Song atmel_port->cookie_tx = dmaengine_submit(desc); 76808f738beSElen Song 76908f738beSElen Song } else { 77008f738beSElen Song if (atmel_port->rs485.flags & SER_RS485_ENABLED) { 77108f738beSElen Song /* DMA done, stop TX, start RX for RS485 */ 77208f738beSElen Song atmel_start_rx(port); 77308f738beSElen Song } 77408f738beSElen Song } 77508f738beSElen Song 77608f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 77708f738beSElen Song uart_write_wakeup(port); 77808f738beSElen Song } 77908f738beSElen Song 78008f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port) 78108f738beSElen Song { 78208f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 78308f738beSElen Song dma_cap_mask_t mask; 78408f738beSElen Song struct dma_slave_config config; 78508f738beSElen Song int ret, nent; 78608f738beSElen Song 78708f738beSElen Song dma_cap_zero(mask); 78808f738beSElen Song dma_cap_set(DMA_SLAVE, mask); 78908f738beSElen Song 79008f738beSElen Song atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx"); 79108f738beSElen Song if (atmel_port->chan_tx == NULL) 79208f738beSElen Song goto chan_err; 79308f738beSElen Song dev_info(port->dev, "using %s for tx DMA transfers\n", 79408f738beSElen Song dma_chan_name(atmel_port->chan_tx)); 79508f738beSElen Song 79608f738beSElen Song spin_lock_init(&atmel_port->lock_tx); 79708f738beSElen Song sg_init_table(&atmel_port->sg_tx, 1); 79808f738beSElen Song /* UART circular tx buffer is an aligned page. */ 79908f738beSElen Song BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK); 80008f738beSElen Song sg_set_page(&atmel_port->sg_tx, 80108f738beSElen Song virt_to_page(port->state->xmit.buf), 80208f738beSElen Song UART_XMIT_SIZE, 80308f738beSElen Song (int)port->state->xmit.buf & ~PAGE_MASK); 80408f738beSElen Song nent = dma_map_sg(port->dev, 80508f738beSElen Song &atmel_port->sg_tx, 80608f738beSElen Song 1, 80708f738beSElen Song DMA_MEM_TO_DEV); 80808f738beSElen Song 80908f738beSElen Song if (!nent) { 81008f738beSElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 81108f738beSElen Song goto chan_err; 81208f738beSElen Song } else { 81308f738beSElen Song dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__, 81408f738beSElen Song sg_dma_len(&atmel_port->sg_tx), 81508f738beSElen Song port->state->xmit.buf, 81608f738beSElen Song sg_dma_address(&atmel_port->sg_tx)); 81708f738beSElen Song } 81808f738beSElen Song 81908f738beSElen Song /* Configure the slave DMA */ 82008f738beSElen Song memset(&config, 0, sizeof(config)); 82108f738beSElen Song config.direction = DMA_MEM_TO_DEV; 82208f738beSElen Song config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 82308f738beSElen Song config.dst_addr = port->mapbase + ATMEL_US_THR; 82408f738beSElen Song 82508f738beSElen Song ret = dmaengine_device_control(atmel_port->chan_tx, 82608f738beSElen Song DMA_SLAVE_CONFIG, 82708f738beSElen Song (unsigned long)&config); 82808f738beSElen Song if (ret) { 82908f738beSElen Song dev_err(port->dev, "DMA tx slave configuration failed\n"); 83008f738beSElen Song goto chan_err; 83108f738beSElen Song } 83208f738beSElen Song 83308f738beSElen Song return 0; 83408f738beSElen Song 83508f738beSElen Song chan_err: 83608f738beSElen Song dev_err(port->dev, "TX channel not available, switch to pio\n"); 83708f738beSElen Song atmel_port->use_dma_tx = 0; 83808f738beSElen Song if (atmel_port->chan_tx) 83908f738beSElen Song atmel_release_tx_dma(port); 84008f738beSElen Song return -EINVAL; 84108f738beSElen Song } 84208f738beSElen Song 84334df42f5SElen Song static void atmel_flip_buffer_rx_dma(struct uart_port *port, 84434df42f5SElen Song char *buf, size_t count) 84534df42f5SElen Song { 84634df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 84734df42f5SElen Song struct tty_port *tport = &port->state->port; 84834df42f5SElen Song 84934df42f5SElen Song dma_sync_sg_for_cpu(port->dev, 85034df42f5SElen Song &atmel_port->sg_rx, 85134df42f5SElen Song 1, 85234df42f5SElen Song DMA_DEV_TO_MEM); 85334df42f5SElen Song 85434df42f5SElen Song tty_insert_flip_string(tport, buf, count); 85534df42f5SElen Song 85634df42f5SElen Song dma_sync_sg_for_device(port->dev, 85734df42f5SElen Song &atmel_port->sg_rx, 85834df42f5SElen Song 1, 85934df42f5SElen Song DMA_DEV_TO_MEM); 86034df42f5SElen Song /* 86134df42f5SElen Song * Drop the lock here since it might end up calling 86234df42f5SElen Song * uart_start(), which takes the lock. 86334df42f5SElen Song */ 86434df42f5SElen Song spin_unlock(&port->lock); 86534df42f5SElen Song tty_flip_buffer_push(tport); 86634df42f5SElen Song spin_lock(&port->lock); 86734df42f5SElen Song } 86834df42f5SElen Song 86934df42f5SElen Song static void atmel_complete_rx_dma(void *arg) 87034df42f5SElen Song { 87134df42f5SElen Song struct uart_port *port = arg; 87234df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 87334df42f5SElen Song 87434df42f5SElen Song tasklet_schedule(&atmel_port->tasklet); 87534df42f5SElen Song } 87634df42f5SElen Song 87734df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port) 87834df42f5SElen Song { 87934df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 88034df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 88134df42f5SElen Song 88234df42f5SElen Song if (chan) { 88334df42f5SElen Song dmaengine_terminate_all(chan); 88434df42f5SElen Song dma_release_channel(chan); 88534df42f5SElen Song dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1, 88634df42f5SElen Song DMA_DEV_TO_MEM); 88734df42f5SElen Song } 88834df42f5SElen Song 88934df42f5SElen Song atmel_port->desc_rx = NULL; 89034df42f5SElen Song atmel_port->chan_rx = NULL; 89134df42f5SElen Song atmel_port->cookie_rx = -EINVAL; 89234df42f5SElen Song } 89334df42f5SElen Song 89434df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port) 89534df42f5SElen Song { 89634df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 89734df42f5SElen Song struct circ_buf *ring = &atmel_port->rx_ring; 89834df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 89934df42f5SElen Song struct dma_tx_state state; 90034df42f5SElen Song enum dma_status dmastat; 90134df42f5SElen Song size_t pending, count; 90234df42f5SElen Song 90334df42f5SElen Song 90434df42f5SElen Song /* Reset the UART timeout early so that we don't miss one */ 90534df42f5SElen Song UART_PUT_CR(port, ATMEL_US_STTTO); 90634df42f5SElen Song dmastat = dmaengine_tx_status(chan, 90734df42f5SElen Song atmel_port->cookie_rx, 90834df42f5SElen Song &state); 90934df42f5SElen Song /* Restart a new tasklet if DMA status is error */ 91034df42f5SElen Song if (dmastat == DMA_ERROR) { 91134df42f5SElen Song dev_dbg(port->dev, "Get residue error, restart tasklet\n"); 91234df42f5SElen Song UART_PUT_IER(port, ATMEL_US_TIMEOUT); 91334df42f5SElen Song tasklet_schedule(&atmel_port->tasklet); 91434df42f5SElen Song return; 91534df42f5SElen Song } 91634df42f5SElen Song /* current transfer size should no larger than dma buffer */ 91734df42f5SElen Song pending = sg_dma_len(&atmel_port->sg_rx) - state.residue; 91834df42f5SElen Song BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx)); 91934df42f5SElen Song 92034df42f5SElen Song /* 92134df42f5SElen Song * This will take the chars we have so far, 92234df42f5SElen Song * ring->head will record the transfer size, only new bytes come 92334df42f5SElen Song * will insert into the framework. 92434df42f5SElen Song */ 92534df42f5SElen Song if (pending > ring->head) { 92634df42f5SElen Song count = pending - ring->head; 92734df42f5SElen Song 92834df42f5SElen Song atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count); 92934df42f5SElen Song 93034df42f5SElen Song ring->head += count; 93134df42f5SElen Song if (ring->head == sg_dma_len(&atmel_port->sg_rx)) 93234df42f5SElen Song ring->head = 0; 93334df42f5SElen Song 93434df42f5SElen Song port->icount.rx += count; 93534df42f5SElen Song } 93634df42f5SElen Song 93734df42f5SElen Song UART_PUT_IER(port, ATMEL_US_TIMEOUT); 93834df42f5SElen Song } 93934df42f5SElen Song 94034df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port) 94134df42f5SElen Song { 94234df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 94334df42f5SElen Song struct dma_async_tx_descriptor *desc; 94434df42f5SElen Song dma_cap_mask_t mask; 94534df42f5SElen Song struct dma_slave_config config; 94634df42f5SElen Song struct circ_buf *ring; 94734df42f5SElen Song int ret, nent; 94834df42f5SElen Song 94934df42f5SElen Song ring = &atmel_port->rx_ring; 95034df42f5SElen Song 95134df42f5SElen Song dma_cap_zero(mask); 95234df42f5SElen Song dma_cap_set(DMA_CYCLIC, mask); 95334df42f5SElen Song 95434df42f5SElen Song atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx"); 95534df42f5SElen Song if (atmel_port->chan_rx == NULL) 95634df42f5SElen Song goto chan_err; 95734df42f5SElen Song dev_info(port->dev, "using %s for rx DMA transfers\n", 95834df42f5SElen Song dma_chan_name(atmel_port->chan_rx)); 95934df42f5SElen Song 96034df42f5SElen Song spin_lock_init(&atmel_port->lock_rx); 96134df42f5SElen Song sg_init_table(&atmel_port->sg_rx, 1); 96234df42f5SElen Song /* UART circular rx buffer is an aligned page. */ 96334df42f5SElen Song BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK); 96434df42f5SElen Song sg_set_page(&atmel_port->sg_rx, 96534df42f5SElen Song virt_to_page(ring->buf), 96634df42f5SElen Song ATMEL_SERIAL_RINGSIZE, 96734df42f5SElen Song (int)ring->buf & ~PAGE_MASK); 96834df42f5SElen Song nent = dma_map_sg(port->dev, 96934df42f5SElen Song &atmel_port->sg_rx, 97034df42f5SElen Song 1, 97134df42f5SElen Song DMA_DEV_TO_MEM); 97234df42f5SElen Song 97334df42f5SElen Song if (!nent) { 97434df42f5SElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 97534df42f5SElen Song goto chan_err; 97634df42f5SElen Song } else { 97734df42f5SElen Song dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__, 97834df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 97934df42f5SElen Song ring->buf, 98034df42f5SElen Song sg_dma_address(&atmel_port->sg_rx)); 98134df42f5SElen Song } 98234df42f5SElen Song 98334df42f5SElen Song /* Configure the slave DMA */ 98434df42f5SElen Song memset(&config, 0, sizeof(config)); 98534df42f5SElen Song config.direction = DMA_DEV_TO_MEM; 98634df42f5SElen Song config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 98734df42f5SElen Song config.src_addr = port->mapbase + ATMEL_US_RHR; 98834df42f5SElen Song 98934df42f5SElen Song ret = dmaengine_device_control(atmel_port->chan_rx, 99034df42f5SElen Song DMA_SLAVE_CONFIG, 99134df42f5SElen Song (unsigned long)&config); 99234df42f5SElen Song if (ret) { 99334df42f5SElen Song dev_err(port->dev, "DMA rx slave configuration failed\n"); 99434df42f5SElen Song goto chan_err; 99534df42f5SElen Song } 99634df42f5SElen Song /* 99734df42f5SElen Song * Prepare a cyclic dma transfer, assign 2 descriptors, 99834df42f5SElen Song * each one is half ring buffer size 99934df42f5SElen Song */ 100034df42f5SElen Song desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx, 100134df42f5SElen Song sg_dma_address(&atmel_port->sg_rx), 100234df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 100334df42f5SElen Song sg_dma_len(&atmel_port->sg_rx)/2, 100434df42f5SElen Song DMA_DEV_TO_MEM, 100534df42f5SElen Song DMA_PREP_INTERRUPT); 100634df42f5SElen Song desc->callback = atmel_complete_rx_dma; 100734df42f5SElen Song desc->callback_param = port; 100834df42f5SElen Song atmel_port->desc_rx = desc; 100934df42f5SElen Song atmel_port->cookie_rx = dmaengine_submit(desc); 101034df42f5SElen Song 101134df42f5SElen Song return 0; 101234df42f5SElen Song 101334df42f5SElen Song chan_err: 101434df42f5SElen Song dev_err(port->dev, "RX channel not available, switch to pio\n"); 101534df42f5SElen Song atmel_port->use_dma_rx = 0; 101634df42f5SElen Song if (atmel_port->chan_rx) 101734df42f5SElen Song atmel_release_rx_dma(port); 101834df42f5SElen Song return -EINVAL; 101934df42f5SElen Song } 102034df42f5SElen Song 10212e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data) 10222e68c22fSElen Song { 10232e68c22fSElen Song struct uart_port *port = (void *)data; 10242e68c22fSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 10252e68c22fSElen Song 10262e68c22fSElen Song tasklet_schedule(&atmel_port->tasklet); 10272e68c22fSElen Song mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port)); 10282e68c22fSElen Song } 10292e68c22fSElen Song 1030ab4382d2SGreg Kroah-Hartman /* 1031ab4382d2SGreg Kroah-Hartman * receive interrupt handler. 1032ab4382d2SGreg Kroah-Hartman */ 1033ab4382d2SGreg Kroah-Hartman static void 1034ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending) 1035ab4382d2SGreg Kroah-Hartman { 1036ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1037ab4382d2SGreg Kroah-Hartman 103864e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 1039ab4382d2SGreg Kroah-Hartman /* 1040ab4382d2SGreg Kroah-Hartman * PDC receive. Just schedule the tasklet and let it 1041ab4382d2SGreg Kroah-Hartman * figure out the details. 1042ab4382d2SGreg Kroah-Hartman * 1043ab4382d2SGreg Kroah-Hartman * TODO: We're not handling error flags correctly at 1044ab4382d2SGreg Kroah-Hartman * the moment. 1045ab4382d2SGreg Kroah-Hartman */ 1046ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) { 1047ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, (ATMEL_US_ENDRX 1048ab4382d2SGreg Kroah-Hartman | ATMEL_US_TIMEOUT)); 1049ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 1050ab4382d2SGreg Kroah-Hartman } 1051ab4382d2SGreg Kroah-Hartman 1052ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE | 1053ab4382d2SGreg Kroah-Hartman ATMEL_US_FRAME | ATMEL_US_PARE)) 1054ab4382d2SGreg Kroah-Hartman atmel_pdc_rxerr(port, pending); 1055ab4382d2SGreg Kroah-Hartman } 1056ab4382d2SGreg Kroah-Hartman 105734df42f5SElen Song if (atmel_use_dma_rx(port)) { 105834df42f5SElen Song if (pending & ATMEL_US_TIMEOUT) { 105934df42f5SElen Song UART_PUT_IDR(port, ATMEL_US_TIMEOUT); 106034df42f5SElen Song tasklet_schedule(&atmel_port->tasklet); 106134df42f5SElen Song } 106234df42f5SElen Song } 106334df42f5SElen Song 1064ab4382d2SGreg Kroah-Hartman /* Interrupt receive */ 1065ab4382d2SGreg Kroah-Hartman if (pending & ATMEL_US_RXRDY) 1066ab4382d2SGreg Kroah-Hartman atmel_rx_chars(port); 1067ab4382d2SGreg Kroah-Hartman else if (pending & ATMEL_US_RXBRK) { 1068ab4382d2SGreg Kroah-Hartman /* 1069ab4382d2SGreg Kroah-Hartman * End of break detected. If it came along with a 1070ab4382d2SGreg Kroah-Hartman * character, atmel_rx_chars will handle it. 1071ab4382d2SGreg Kroah-Hartman */ 1072ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); 1073ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXBRK); 1074ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 1075ab4382d2SGreg Kroah-Hartman } 1076ab4382d2SGreg Kroah-Hartman } 1077ab4382d2SGreg Kroah-Hartman 1078ab4382d2SGreg Kroah-Hartman /* 1079ab4382d2SGreg Kroah-Hartman * transmit interrupt handler. (Transmit is IRQF_NODELAY safe) 1080ab4382d2SGreg Kroah-Hartman */ 1081ab4382d2SGreg Kroah-Hartman static void 1082ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending) 1083ab4382d2SGreg Kroah-Hartman { 1084ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1085ab4382d2SGreg Kroah-Hartman 1086ab4382d2SGreg Kroah-Hartman if (pending & atmel_port->tx_done_mask) { 1087ab4382d2SGreg Kroah-Hartman /* Either PDC or interrupt transmission */ 1088ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, atmel_port->tx_done_mask); 1089ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 1090ab4382d2SGreg Kroah-Hartman } 1091ab4382d2SGreg Kroah-Hartman } 1092ab4382d2SGreg Kroah-Hartman 1093ab4382d2SGreg Kroah-Hartman /* 1094ab4382d2SGreg Kroah-Hartman * status flags interrupt handler. 1095ab4382d2SGreg Kroah-Hartman */ 1096ab4382d2SGreg Kroah-Hartman static void 1097ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending, 1098ab4382d2SGreg Kroah-Hartman unsigned int status) 1099ab4382d2SGreg Kroah-Hartman { 1100ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1101ab4382d2SGreg Kroah-Hartman 1102ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC 1103ab4382d2SGreg Kroah-Hartman | ATMEL_US_CTSIC)) { 1104ab4382d2SGreg Kroah-Hartman atmel_port->irq_status = status; 1105ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 1106ab4382d2SGreg Kroah-Hartman } 1107ab4382d2SGreg Kroah-Hartman } 1108ab4382d2SGreg Kroah-Hartman 1109ab4382d2SGreg Kroah-Hartman /* 1110ab4382d2SGreg Kroah-Hartman * Interrupt handler 1111ab4382d2SGreg Kroah-Hartman */ 1112ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id) 1113ab4382d2SGreg Kroah-Hartman { 1114ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 1115*ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1116ab4382d2SGreg Kroah-Hartman unsigned int status, pending, pass_counter = 0; 1117*ab5e4e41SRichard Genoud bool gpio_handled = false; 1118ab4382d2SGreg Kroah-Hartman 1119ab4382d2SGreg Kroah-Hartman do { 1120e0b0baadSRichard Genoud status = atmel_get_lines_status(port); 1121ab4382d2SGreg Kroah-Hartman pending = status & UART_GET_IMR(port); 1122*ab5e4e41SRichard Genoud if (!gpio_handled) { 1123*ab5e4e41SRichard Genoud /* 1124*ab5e4e41SRichard Genoud * Dealing with GPIO interrupt 1125*ab5e4e41SRichard Genoud */ 1126*ab5e4e41SRichard Genoud if (irq == atmel_port->gpio_irq[UART_GPIO_CTS]) 1127*ab5e4e41SRichard Genoud pending |= ATMEL_US_CTSIC; 1128*ab5e4e41SRichard Genoud 1129*ab5e4e41SRichard Genoud if (irq == atmel_port->gpio_irq[UART_GPIO_DSR]) 1130*ab5e4e41SRichard Genoud pending |= ATMEL_US_DSRIC; 1131*ab5e4e41SRichard Genoud 1132*ab5e4e41SRichard Genoud if (irq == atmel_port->gpio_irq[UART_GPIO_RI]) 1133*ab5e4e41SRichard Genoud pending |= ATMEL_US_RIIC; 1134*ab5e4e41SRichard Genoud 1135*ab5e4e41SRichard Genoud if (irq == atmel_port->gpio_irq[UART_GPIO_DCD]) 1136*ab5e4e41SRichard Genoud pending |= ATMEL_US_DCDIC; 1137*ab5e4e41SRichard Genoud 1138*ab5e4e41SRichard Genoud gpio_handled = true; 1139*ab5e4e41SRichard Genoud } 1140ab4382d2SGreg Kroah-Hartman if (!pending) 1141ab4382d2SGreg Kroah-Hartman break; 1142ab4382d2SGreg Kroah-Hartman 1143ab4382d2SGreg Kroah-Hartman atmel_handle_receive(port, pending); 1144ab4382d2SGreg Kroah-Hartman atmel_handle_status(port, pending, status); 1145ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(port, pending); 1146ab4382d2SGreg Kroah-Hartman } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT); 1147ab4382d2SGreg Kroah-Hartman 1148ab4382d2SGreg Kroah-Hartman return pass_counter ? IRQ_HANDLED : IRQ_NONE; 1149ab4382d2SGreg Kroah-Hartman } 1150ab4382d2SGreg Kroah-Hartman 1151a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port) 1152a930e528SElen Song { 1153a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1154a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1155a930e528SElen Song 1156a930e528SElen Song dma_unmap_single(port->dev, 1157a930e528SElen Song pdc->dma_addr, 1158a930e528SElen Song pdc->dma_size, 1159a930e528SElen Song DMA_TO_DEVICE); 1160a930e528SElen Song } 1161a930e528SElen Song 1162ab4382d2SGreg Kroah-Hartman /* 1163ab4382d2SGreg Kroah-Hartman * Called from tasklet with ENDTX and TXBUFE interrupts disabled. 1164ab4382d2SGreg Kroah-Hartman */ 116564e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port) 1166ab4382d2SGreg Kroah-Hartman { 1167ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1168ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 1169ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1170ab4382d2SGreg Kroah-Hartman int count; 1171ab4382d2SGreg Kroah-Hartman 1172ab4382d2SGreg Kroah-Hartman /* nothing left to transmit? */ 1173ab4382d2SGreg Kroah-Hartman if (UART_GET_TCR(port)) 1174ab4382d2SGreg Kroah-Hartman return; 1175ab4382d2SGreg Kroah-Hartman 1176ab4382d2SGreg Kroah-Hartman xmit->tail += pdc->ofs; 1177ab4382d2SGreg Kroah-Hartman xmit->tail &= UART_XMIT_SIZE - 1; 1178ab4382d2SGreg Kroah-Hartman 1179ab4382d2SGreg Kroah-Hartman port->icount.tx += pdc->ofs; 1180ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 1181ab4382d2SGreg Kroah-Hartman 1182ab4382d2SGreg Kroah-Hartman /* more to transmit - setup next transfer */ 1183ab4382d2SGreg Kroah-Hartman 1184ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 1185ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 1186ab4382d2SGreg Kroah-Hartman 1187ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 1188ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, 1189ab4382d2SGreg Kroah-Hartman pdc->dma_addr, 1190ab4382d2SGreg Kroah-Hartman pdc->dma_size, 1191ab4382d2SGreg Kroah-Hartman DMA_TO_DEVICE); 1192ab4382d2SGreg Kroah-Hartman 1193ab4382d2SGreg Kroah-Hartman count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 1194ab4382d2SGreg Kroah-Hartman pdc->ofs = count; 1195ab4382d2SGreg Kroah-Hartman 1196ab4382d2SGreg Kroah-Hartman UART_PUT_TPR(port, pdc->dma_addr + xmit->tail); 1197ab4382d2SGreg Kroah-Hartman UART_PUT_TCR(port, count); 1198ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 1199ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); 1200ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 1201ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 1202ab4382d2SGreg Kroah-Hartman } else { 120383cac9f3SBernhard Roth if ((atmel_port->rs485.flags & SER_RS485_ENABLED) && 120483cac9f3SBernhard Roth !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) { 1205ab4382d2SGreg Kroah-Hartman /* DMA done, stop TX, start RX for RS485 */ 1206ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 1207ab4382d2SGreg Kroah-Hartman } 1208ab4382d2SGreg Kroah-Hartman } 1209ab4382d2SGreg Kroah-Hartman 1210ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 1211ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 1212ab4382d2SGreg Kroah-Hartman } 1213ab4382d2SGreg Kroah-Hartman 1214a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port) 1215a930e528SElen Song { 1216a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1217a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1218a930e528SElen Song struct circ_buf *xmit = &port->state->xmit; 1219a930e528SElen Song 1220a930e528SElen Song pdc->buf = xmit->buf; 1221a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1222a930e528SElen Song pdc->buf, 1223a930e528SElen Song UART_XMIT_SIZE, 1224a930e528SElen Song DMA_TO_DEVICE); 1225a930e528SElen Song pdc->dma_size = UART_XMIT_SIZE; 1226a930e528SElen Song pdc->ofs = 0; 1227a930e528SElen Song 1228a930e528SElen Song return 0; 1229a930e528SElen Song } 1230a930e528SElen Song 1231ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port) 1232ab4382d2SGreg Kroah-Hartman { 1233ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1234ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 1235ab4382d2SGreg Kroah-Hartman unsigned int flg; 1236ab4382d2SGreg Kroah-Hartman unsigned int status; 1237ab4382d2SGreg Kroah-Hartman 1238ab4382d2SGreg Kroah-Hartman while (ring->head != ring->tail) { 1239ab4382d2SGreg Kroah-Hartman struct atmel_uart_char c; 1240ab4382d2SGreg Kroah-Hartman 1241ab4382d2SGreg Kroah-Hartman /* Make sure c is loaded after head. */ 1242ab4382d2SGreg Kroah-Hartman smp_rmb(); 1243ab4382d2SGreg Kroah-Hartman 1244ab4382d2SGreg Kroah-Hartman c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; 1245ab4382d2SGreg Kroah-Hartman 1246ab4382d2SGreg Kroah-Hartman ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 1247ab4382d2SGreg Kroah-Hartman 1248ab4382d2SGreg Kroah-Hartman port->icount.rx++; 1249ab4382d2SGreg Kroah-Hartman status = c.status; 1250ab4382d2SGreg Kroah-Hartman flg = TTY_NORMAL; 1251ab4382d2SGreg Kroah-Hartman 1252ab4382d2SGreg Kroah-Hartman /* 1253ab4382d2SGreg Kroah-Hartman * note that the error handling code is 1254ab4382d2SGreg Kroah-Hartman * out of the main execution path 1255ab4382d2SGreg Kroah-Hartman */ 1256ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 1257ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK))) { 1258ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 1259ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 1260ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 1261ab4382d2SGreg Kroah-Hartman 1262ab4382d2SGreg Kroah-Hartman port->icount.brk++; 1263ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 1264ab4382d2SGreg Kroah-Hartman continue; 1265ab4382d2SGreg Kroah-Hartman } 1266ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 1267ab4382d2SGreg Kroah-Hartman port->icount.parity++; 1268ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 1269ab4382d2SGreg Kroah-Hartman port->icount.frame++; 1270ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 1271ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 1272ab4382d2SGreg Kroah-Hartman 1273ab4382d2SGreg Kroah-Hartman status &= port->read_status_mask; 1274ab4382d2SGreg Kroah-Hartman 1275ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) 1276ab4382d2SGreg Kroah-Hartman flg = TTY_BREAK; 1277ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_PARE) 1278ab4382d2SGreg Kroah-Hartman flg = TTY_PARITY; 1279ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_FRAME) 1280ab4382d2SGreg Kroah-Hartman flg = TTY_FRAME; 1281ab4382d2SGreg Kroah-Hartman } 1282ab4382d2SGreg Kroah-Hartman 1283ab4382d2SGreg Kroah-Hartman 1284ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c.ch)) 1285ab4382d2SGreg Kroah-Hartman continue; 1286ab4382d2SGreg Kroah-Hartman 1287ab4382d2SGreg Kroah-Hartman uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg); 1288ab4382d2SGreg Kroah-Hartman } 1289ab4382d2SGreg Kroah-Hartman 1290ab4382d2SGreg Kroah-Hartman /* 1291ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 1292ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 1293ab4382d2SGreg Kroah-Hartman */ 1294ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 12952e124b4aSJiri Slaby tty_flip_buffer_push(&port->state->port); 1296ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1297ab4382d2SGreg Kroah-Hartman } 1298ab4382d2SGreg Kroah-Hartman 1299a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port) 1300a930e528SElen Song { 1301a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1302a930e528SElen Song int i; 1303a930e528SElen Song 1304a930e528SElen Song for (i = 0; i < 2; i++) { 1305a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1306a930e528SElen Song 1307a930e528SElen Song dma_unmap_single(port->dev, 1308a930e528SElen Song pdc->dma_addr, 1309a930e528SElen Song pdc->dma_size, 1310a930e528SElen Song DMA_FROM_DEVICE); 1311a930e528SElen Song kfree(pdc->buf); 1312a930e528SElen Song } 1313a930e528SElen Song } 1314a930e528SElen Song 131564e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port) 1316ab4382d2SGreg Kroah-Hartman { 1317ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 131805c7cd39SJiri Slaby struct tty_port *tport = &port->state->port; 1319ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc; 1320ab4382d2SGreg Kroah-Hartman int rx_idx = atmel_port->pdc_rx_idx; 1321ab4382d2SGreg Kroah-Hartman unsigned int head; 1322ab4382d2SGreg Kroah-Hartman unsigned int tail; 1323ab4382d2SGreg Kroah-Hartman unsigned int count; 1324ab4382d2SGreg Kroah-Hartman 1325ab4382d2SGreg Kroah-Hartman do { 1326ab4382d2SGreg Kroah-Hartman /* Reset the UART timeout early so that we don't miss one */ 1327ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STTTO); 1328ab4382d2SGreg Kroah-Hartman 1329ab4382d2SGreg Kroah-Hartman pdc = &atmel_port->pdc_rx[rx_idx]; 1330ab4382d2SGreg Kroah-Hartman head = UART_GET_RPR(port) - pdc->dma_addr; 1331ab4382d2SGreg Kroah-Hartman tail = pdc->ofs; 1332ab4382d2SGreg Kroah-Hartman 1333ab4382d2SGreg Kroah-Hartman /* If the PDC has switched buffers, RPR won't contain 1334ab4382d2SGreg Kroah-Hartman * any address within the current buffer. Since head 1335ab4382d2SGreg Kroah-Hartman * is unsigned, we just need a one-way comparison to 1336ab4382d2SGreg Kroah-Hartman * find out. 1337ab4382d2SGreg Kroah-Hartman * 1338ab4382d2SGreg Kroah-Hartman * In this case, we just need to consume the entire 1339ab4382d2SGreg Kroah-Hartman * buffer and resubmit it for DMA. This will clear the 1340ab4382d2SGreg Kroah-Hartman * ENDRX bit as well, so that we can safely re-enable 1341ab4382d2SGreg Kroah-Hartman * all interrupts below. 1342ab4382d2SGreg Kroah-Hartman */ 1343ab4382d2SGreg Kroah-Hartman head = min(head, pdc->dma_size); 1344ab4382d2SGreg Kroah-Hartman 1345ab4382d2SGreg Kroah-Hartman if (likely(head != tail)) { 1346ab4382d2SGreg Kroah-Hartman dma_sync_single_for_cpu(port->dev, pdc->dma_addr, 1347ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1348ab4382d2SGreg Kroah-Hartman 1349ab4382d2SGreg Kroah-Hartman /* 1350ab4382d2SGreg Kroah-Hartman * head will only wrap around when we recycle 1351ab4382d2SGreg Kroah-Hartman * the DMA buffer, and when that happens, we 1352ab4382d2SGreg Kroah-Hartman * explicitly set tail to 0. So head will 1353ab4382d2SGreg Kroah-Hartman * always be greater than tail. 1354ab4382d2SGreg Kroah-Hartman */ 1355ab4382d2SGreg Kroah-Hartman count = head - tail; 1356ab4382d2SGreg Kroah-Hartman 135705c7cd39SJiri Slaby tty_insert_flip_string(tport, pdc->buf + pdc->ofs, 135805c7cd39SJiri Slaby count); 1359ab4382d2SGreg Kroah-Hartman 1360ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, pdc->dma_addr, 1361ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1362ab4382d2SGreg Kroah-Hartman 1363ab4382d2SGreg Kroah-Hartman port->icount.rx += count; 1364ab4382d2SGreg Kroah-Hartman pdc->ofs = head; 1365ab4382d2SGreg Kroah-Hartman } 1366ab4382d2SGreg Kroah-Hartman 1367ab4382d2SGreg Kroah-Hartman /* 1368ab4382d2SGreg Kroah-Hartman * If the current buffer is full, we need to check if 1369ab4382d2SGreg Kroah-Hartman * the next one contains any additional data. 1370ab4382d2SGreg Kroah-Hartman */ 1371ab4382d2SGreg Kroah-Hartman if (head >= pdc->dma_size) { 1372ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 1373ab4382d2SGreg Kroah-Hartman UART_PUT_RNPR(port, pdc->dma_addr); 1374ab4382d2SGreg Kroah-Hartman UART_PUT_RNCR(port, pdc->dma_size); 1375ab4382d2SGreg Kroah-Hartman 1376ab4382d2SGreg Kroah-Hartman rx_idx = !rx_idx; 1377ab4382d2SGreg Kroah-Hartman atmel_port->pdc_rx_idx = rx_idx; 1378ab4382d2SGreg Kroah-Hartman } 1379ab4382d2SGreg Kroah-Hartman } while (head >= pdc->dma_size); 1380ab4382d2SGreg Kroah-Hartman 1381ab4382d2SGreg Kroah-Hartman /* 1382ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 1383ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 1384ab4382d2SGreg Kroah-Hartman */ 1385ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 13862e124b4aSJiri Slaby tty_flip_buffer_push(tport); 1387ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1388ab4382d2SGreg Kroah-Hartman 1389ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1390ab4382d2SGreg Kroah-Hartman } 1391ab4382d2SGreg Kroah-Hartman 1392a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port) 1393a930e528SElen Song { 1394a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1395a930e528SElen Song int i; 1396a930e528SElen Song 1397a930e528SElen Song for (i = 0; i < 2; i++) { 1398a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1399a930e528SElen Song 1400a930e528SElen Song pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); 1401a930e528SElen Song if (pdc->buf == NULL) { 1402a930e528SElen Song if (i != 0) { 1403a930e528SElen Song dma_unmap_single(port->dev, 1404a930e528SElen Song atmel_port->pdc_rx[0].dma_addr, 1405a930e528SElen Song PDC_BUFFER_SIZE, 1406a930e528SElen Song DMA_FROM_DEVICE); 1407a930e528SElen Song kfree(atmel_port->pdc_rx[0].buf); 1408a930e528SElen Song } 1409a930e528SElen Song atmel_port->use_pdc_rx = 0; 1410a930e528SElen Song return -ENOMEM; 1411a930e528SElen Song } 1412a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1413a930e528SElen Song pdc->buf, 1414a930e528SElen Song PDC_BUFFER_SIZE, 1415a930e528SElen Song DMA_FROM_DEVICE); 1416a930e528SElen Song pdc->dma_size = PDC_BUFFER_SIZE; 1417a930e528SElen Song pdc->ofs = 0; 1418a930e528SElen Song } 1419a930e528SElen Song 1420a930e528SElen Song atmel_port->pdc_rx_idx = 0; 1421a930e528SElen Song 1422a930e528SElen Song UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr); 1423a930e528SElen Song UART_PUT_RCR(port, PDC_BUFFER_SIZE); 1424a930e528SElen Song 1425a930e528SElen Song UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr); 1426a930e528SElen Song UART_PUT_RNCR(port, PDC_BUFFER_SIZE); 1427a930e528SElen Song 1428a930e528SElen Song return 0; 1429a930e528SElen Song } 1430a930e528SElen Song 1431ab4382d2SGreg Kroah-Hartman /* 1432ab4382d2SGreg Kroah-Hartman * tasklet handling tty stuff outside the interrupt handler. 1433ab4382d2SGreg Kroah-Hartman */ 1434ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data) 1435ab4382d2SGreg Kroah-Hartman { 1436ab4382d2SGreg Kroah-Hartman struct uart_port *port = (struct uart_port *)data; 1437ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1438ab4382d2SGreg Kroah-Hartman unsigned int status; 1439ab4382d2SGreg Kroah-Hartman unsigned int status_change; 1440ab4382d2SGreg Kroah-Hartman 1441ab4382d2SGreg Kroah-Hartman /* The interrupt handler does not take the lock */ 1442ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1443ab4382d2SGreg Kroah-Hartman 1444a930e528SElen Song atmel_port->schedule_tx(port); 1445ab4382d2SGreg Kroah-Hartman 1446ab4382d2SGreg Kroah-Hartman status = atmel_port->irq_status; 1447ab4382d2SGreg Kroah-Hartman status_change = status ^ atmel_port->irq_status_prev; 1448ab4382d2SGreg Kroah-Hartman 1449ab4382d2SGreg Kroah-Hartman if (status_change & (ATMEL_US_RI | ATMEL_US_DSR 1450ab4382d2SGreg Kroah-Hartman | ATMEL_US_DCD | ATMEL_US_CTS)) { 1451ab4382d2SGreg Kroah-Hartman /* TODO: All reads to CSR will clear these interrupts! */ 1452ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_RI) 1453ab4382d2SGreg Kroah-Hartman port->icount.rng++; 1454ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_DSR) 1455ab4382d2SGreg Kroah-Hartman port->icount.dsr++; 1456ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_DCD) 1457ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); 1458ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_CTS) 1459ab4382d2SGreg Kroah-Hartman uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); 1460ab4382d2SGreg Kroah-Hartman 1461ab4382d2SGreg Kroah-Hartman wake_up_interruptible(&port->state->port.delta_msr_wait); 1462ab4382d2SGreg Kroah-Hartman 1463ab4382d2SGreg Kroah-Hartman atmel_port->irq_status_prev = status; 1464ab4382d2SGreg Kroah-Hartman } 1465ab4382d2SGreg Kroah-Hartman 1466a930e528SElen Song atmel_port->schedule_rx(port); 1467ab4382d2SGreg Kroah-Hartman 1468ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 1469ab4382d2SGreg Kroah-Hartman } 1470ab4382d2SGreg Kroah-Hartman 147133d64c4fSElen Song static int atmel_init_property(struct atmel_uart_port *atmel_port, 147233d64c4fSElen Song struct platform_device *pdev) 147333d64c4fSElen Song { 147433d64c4fSElen Song struct device_node *np = pdev->dev.of_node; 1475574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 147633d64c4fSElen Song 147733d64c4fSElen Song if (np) { 147833d64c4fSElen Song /* DMA/PDC usage specification */ 147933d64c4fSElen Song if (of_get_property(np, "atmel,use-dma-rx", NULL)) { 148033d64c4fSElen Song if (of_get_property(np, "dmas", NULL)) { 148133d64c4fSElen Song atmel_port->use_dma_rx = true; 148233d64c4fSElen Song atmel_port->use_pdc_rx = false; 148333d64c4fSElen Song } else { 148433d64c4fSElen Song atmel_port->use_dma_rx = false; 148533d64c4fSElen Song atmel_port->use_pdc_rx = true; 148633d64c4fSElen Song } 148733d64c4fSElen Song } else { 148833d64c4fSElen Song atmel_port->use_dma_rx = false; 148933d64c4fSElen Song atmel_port->use_pdc_rx = false; 149033d64c4fSElen Song } 149133d64c4fSElen Song 149233d64c4fSElen Song if (of_get_property(np, "atmel,use-dma-tx", NULL)) { 149333d64c4fSElen Song if (of_get_property(np, "dmas", NULL)) { 149433d64c4fSElen Song atmel_port->use_dma_tx = true; 149533d64c4fSElen Song atmel_port->use_pdc_tx = false; 149633d64c4fSElen Song } else { 149733d64c4fSElen Song atmel_port->use_dma_tx = false; 149833d64c4fSElen Song atmel_port->use_pdc_tx = true; 149933d64c4fSElen Song } 150033d64c4fSElen Song } else { 150133d64c4fSElen Song atmel_port->use_dma_tx = false; 150233d64c4fSElen Song atmel_port->use_pdc_tx = false; 150333d64c4fSElen Song } 150433d64c4fSElen Song 150533d64c4fSElen Song } else { 150633d64c4fSElen Song atmel_port->use_pdc_rx = pdata->use_dma_rx; 150733d64c4fSElen Song atmel_port->use_pdc_tx = pdata->use_dma_tx; 150833d64c4fSElen Song atmel_port->use_dma_rx = false; 150933d64c4fSElen Song atmel_port->use_dma_tx = false; 151033d64c4fSElen Song } 151133d64c4fSElen Song 151233d64c4fSElen Song return 0; 151333d64c4fSElen Song } 151433d64c4fSElen Song 151533d64c4fSElen Song static void atmel_init_rs485(struct atmel_uart_port *atmel_port, 151633d64c4fSElen Song struct platform_device *pdev) 151733d64c4fSElen Song { 151833d64c4fSElen Song struct device_node *np = pdev->dev.of_node; 1519574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 152033d64c4fSElen Song 152133d64c4fSElen Song if (np) { 152233d64c4fSElen Song u32 rs485_delay[2]; 152333d64c4fSElen Song /* rs485 properties */ 152433d64c4fSElen Song if (of_property_read_u32_array(np, "rs485-rts-delay", 152533d64c4fSElen Song rs485_delay, 2) == 0) { 152633d64c4fSElen Song struct serial_rs485 *rs485conf = &atmel_port->rs485; 152733d64c4fSElen Song 152833d64c4fSElen Song rs485conf->delay_rts_before_send = rs485_delay[0]; 152933d64c4fSElen Song rs485conf->delay_rts_after_send = rs485_delay[1]; 153033d64c4fSElen Song rs485conf->flags = 0; 153133d64c4fSElen Song 153233d64c4fSElen Song if (of_get_property(np, "rs485-rx-during-tx", NULL)) 153333d64c4fSElen Song rs485conf->flags |= SER_RS485_RX_DURING_TX; 153433d64c4fSElen Song 153533d64c4fSElen Song if (of_get_property(np, "linux,rs485-enabled-at-boot-time", 153633d64c4fSElen Song NULL)) 153733d64c4fSElen Song rs485conf->flags |= SER_RS485_ENABLED; 153833d64c4fSElen Song } 153933d64c4fSElen Song } else { 154033d64c4fSElen Song atmel_port->rs485 = pdata->rs485; 154133d64c4fSElen Song } 154233d64c4fSElen Song 154333d64c4fSElen Song } 154433d64c4fSElen Song 1545a930e528SElen Song static void atmel_set_ops(struct uart_port *port) 1546a930e528SElen Song { 1547a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1548a930e528SElen Song 154934df42f5SElen Song if (atmel_use_dma_rx(port)) { 155034df42f5SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_dma; 155134df42f5SElen Song atmel_port->schedule_rx = &atmel_rx_from_dma; 155234df42f5SElen Song atmel_port->release_rx = &atmel_release_rx_dma; 155334df42f5SElen Song } else if (atmel_use_pdc_rx(port)) { 1554a930e528SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_pdc; 1555a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_pdc; 1556a930e528SElen Song atmel_port->release_rx = &atmel_release_rx_pdc; 1557a930e528SElen Song } else { 1558a930e528SElen Song atmel_port->prepare_rx = NULL; 1559a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_ring; 1560a930e528SElen Song atmel_port->release_rx = NULL; 1561a930e528SElen Song } 1562a930e528SElen Song 156308f738beSElen Song if (atmel_use_dma_tx(port)) { 156408f738beSElen Song atmel_port->prepare_tx = &atmel_prepare_tx_dma; 156508f738beSElen Song atmel_port->schedule_tx = &atmel_tx_dma; 156608f738beSElen Song atmel_port->release_tx = &atmel_release_tx_dma; 156708f738beSElen Song } else if (atmel_use_pdc_tx(port)) { 1568a930e528SElen Song atmel_port->prepare_tx = &atmel_prepare_tx_pdc; 1569a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_pdc; 1570a930e528SElen Song atmel_port->release_tx = &atmel_release_tx_pdc; 1571a930e528SElen Song } else { 1572a930e528SElen Song atmel_port->prepare_tx = NULL; 1573a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_chars; 1574a930e528SElen Song atmel_port->release_tx = NULL; 1575a930e528SElen Song } 1576a930e528SElen Song } 1577a930e528SElen Song 1578ab4382d2SGreg Kroah-Hartman /* 1579055560b0SElen Song * Get ip name usart or uart 1580055560b0SElen Song */ 1581892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port) 1582055560b0SElen Song { 1583055560b0SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1584055560b0SElen Song int name = UART_GET_IP_NAME(port); 1585731d9caeSNicolas Ferre u32 version; 1586055560b0SElen Song int usart, uart; 1587055560b0SElen Song /* usart and uart ascii */ 1588055560b0SElen Song usart = 0x55534152; 1589055560b0SElen Song uart = 0x44424755; 1590055560b0SElen Song 1591055560b0SElen Song atmel_port->is_usart = false; 1592055560b0SElen Song 1593055560b0SElen Song if (name == usart) { 1594055560b0SElen Song dev_dbg(port->dev, "This is usart\n"); 1595055560b0SElen Song atmel_port->is_usart = true; 1596055560b0SElen Song } else if (name == uart) { 1597055560b0SElen Song dev_dbg(port->dev, "This is uart\n"); 1598055560b0SElen Song atmel_port->is_usart = false; 1599055560b0SElen Song } else { 1600731d9caeSNicolas Ferre /* fallback for older SoCs: use version field */ 1601731d9caeSNicolas Ferre version = UART_GET_IP_VERSION(port); 1602731d9caeSNicolas Ferre switch (version) { 1603731d9caeSNicolas Ferre case 0x302: 1604731d9caeSNicolas Ferre case 0x10213: 1605731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is usart\n"); 1606731d9caeSNicolas Ferre atmel_port->is_usart = true; 1607731d9caeSNicolas Ferre break; 1608731d9caeSNicolas Ferre case 0x203: 1609731d9caeSNicolas Ferre case 0x10202: 1610731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is uart\n"); 1611731d9caeSNicolas Ferre atmel_port->is_usart = false; 1612731d9caeSNicolas Ferre break; 1613731d9caeSNicolas Ferre default: 1614731d9caeSNicolas Ferre dev_err(port->dev, "Not supported ip name nor version, set to uart\n"); 1615731d9caeSNicolas Ferre } 1616055560b0SElen Song } 1617055560b0SElen Song } 1618055560b0SElen Song 1619*ab5e4e41SRichard Genoud static void atmel_free_gpio_irq(struct uart_port *port) 1620*ab5e4e41SRichard Genoud { 1621*ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1622*ab5e4e41SRichard Genoud enum mctrl_gpio_idx i; 1623*ab5e4e41SRichard Genoud 1624*ab5e4e41SRichard Genoud for (i = 0; i < UART_GPIO_MAX; i++) 1625*ab5e4e41SRichard Genoud if (atmel_port->gpio_irq[i] >= 0) 1626*ab5e4e41SRichard Genoud free_irq(atmel_port->gpio_irq[i], port); 1627*ab5e4e41SRichard Genoud } 1628*ab5e4e41SRichard Genoud 1629*ab5e4e41SRichard Genoud static int atmel_request_gpio_irq(struct uart_port *port) 1630*ab5e4e41SRichard Genoud { 1631*ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1632*ab5e4e41SRichard Genoud int *irq = atmel_port->gpio_irq; 1633*ab5e4e41SRichard Genoud enum mctrl_gpio_idx i; 1634*ab5e4e41SRichard Genoud int err = 0; 1635*ab5e4e41SRichard Genoud 1636*ab5e4e41SRichard Genoud for (i = 0; (i < UART_GPIO_MAX) && !err; i++) { 1637*ab5e4e41SRichard Genoud if (irq[i] < 0) 1638*ab5e4e41SRichard Genoud continue; 1639*ab5e4e41SRichard Genoud 1640*ab5e4e41SRichard Genoud irq_set_status_flags(irq[i], IRQ_NOAUTOEN); 1641*ab5e4e41SRichard Genoud err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH, 1642*ab5e4e41SRichard Genoud "atmel_serial", port); 1643*ab5e4e41SRichard Genoud if (err) 1644*ab5e4e41SRichard Genoud dev_err(port->dev, "atmel_startup - Can't get %d irq\n", 1645*ab5e4e41SRichard Genoud irq[i]); 1646*ab5e4e41SRichard Genoud } 1647*ab5e4e41SRichard Genoud 1648*ab5e4e41SRichard Genoud /* 1649*ab5e4e41SRichard Genoud * If something went wrong, rollback. 1650*ab5e4e41SRichard Genoud */ 1651*ab5e4e41SRichard Genoud while (err && (--i >= 0)) 1652*ab5e4e41SRichard Genoud if (irq[i] >= 0) 1653*ab5e4e41SRichard Genoud free_irq(irq[i], port); 1654*ab5e4e41SRichard Genoud 1655*ab5e4e41SRichard Genoud return err; 1656*ab5e4e41SRichard Genoud } 1657*ab5e4e41SRichard Genoud 1658055560b0SElen Song /* 1659ab4382d2SGreg Kroah-Hartman * Perform initialization and enable port for reception 1660ab4382d2SGreg Kroah-Hartman */ 1661ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port) 1662ab4382d2SGreg Kroah-Hartman { 166333d64c4fSElen Song struct platform_device *pdev = to_platform_device(port->dev); 1664ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1665ab4382d2SGreg Kroah-Hartman struct tty_struct *tty = port->state->port.tty; 1666ab4382d2SGreg Kroah-Hartman int retval; 1667ab4382d2SGreg Kroah-Hartman 1668ab4382d2SGreg Kroah-Hartman /* 1669ab4382d2SGreg Kroah-Hartman * Ensure that no interrupts are enabled otherwise when 1670ab4382d2SGreg Kroah-Hartman * request_irq() is called we could get stuck trying to 1671ab4382d2SGreg Kroah-Hartman * handle an unexpected interrupt 1672ab4382d2SGreg Kroah-Hartman */ 1673ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 1674*ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = false; 1675ab4382d2SGreg Kroah-Hartman 1676ab4382d2SGreg Kroah-Hartman /* 1677ab4382d2SGreg Kroah-Hartman * Allocate the IRQ 1678ab4382d2SGreg Kroah-Hartman */ 1679ab4382d2SGreg Kroah-Hartman retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, 1680ab4382d2SGreg Kroah-Hartman tty ? tty->name : "atmel_serial", port); 1681ab4382d2SGreg Kroah-Hartman if (retval) { 1682ddaa6037SRichard Genoud dev_err(port->dev, "atmel_startup - Can't get irq\n"); 1683ab4382d2SGreg Kroah-Hartman return retval; 1684ab4382d2SGreg Kroah-Hartman } 1685ab4382d2SGreg Kroah-Hartman 1686ab4382d2SGreg Kroah-Hartman /* 1687*ab5e4e41SRichard Genoud * Get the GPIO lines IRQ 1688*ab5e4e41SRichard Genoud */ 1689*ab5e4e41SRichard Genoud retval = atmel_request_gpio_irq(port); 1690*ab5e4e41SRichard Genoud if (retval) 1691*ab5e4e41SRichard Genoud goto free_irq; 1692*ab5e4e41SRichard Genoud 1693*ab5e4e41SRichard Genoud /* 1694ab4382d2SGreg Kroah-Hartman * Initialize DMA (if necessary) 1695ab4382d2SGreg Kroah-Hartman */ 169633d64c4fSElen Song atmel_init_property(atmel_port, pdev); 169733d64c4fSElen Song 1698a930e528SElen Song if (atmel_port->prepare_rx) { 1699a930e528SElen Song retval = atmel_port->prepare_rx(port); 1700a930e528SElen Song if (retval < 0) 1701a930e528SElen Song atmel_set_ops(port); 1702ab4382d2SGreg Kroah-Hartman } 1703ab4382d2SGreg Kroah-Hartman 1704a930e528SElen Song if (atmel_port->prepare_tx) { 1705a930e528SElen Song retval = atmel_port->prepare_tx(port); 1706a930e528SElen Song if (retval < 0) 1707a930e528SElen Song atmel_set_ops(port); 1708ab4382d2SGreg Kroah-Hartman } 1709ab4382d2SGreg Kroah-Hartman 1710ab4382d2SGreg Kroah-Hartman /* Save current CSR for comparison in atmel_tasklet_func() */ 1711e0b0baadSRichard Genoud atmel_port->irq_status_prev = atmel_get_lines_status(port); 1712ab4382d2SGreg Kroah-Hartman atmel_port->irq_status = atmel_port->irq_status_prev; 1713ab4382d2SGreg Kroah-Hartman 1714ab4382d2SGreg Kroah-Hartman /* 1715ab4382d2SGreg Kroah-Hartman * Finally, enable the serial port 1716ab4382d2SGreg Kroah-Hartman */ 1717ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 1718ab4382d2SGreg Kroah-Hartman /* enable xmit & rcvr */ 1719ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 1720ab4382d2SGreg Kroah-Hartman 17212e68c22fSElen Song setup_timer(&atmel_port->uart_timer, 17222e68c22fSElen Song atmel_uart_timer_callback, 17232e68c22fSElen Song (unsigned long)port); 17248bc661bfSMarek Roszko 17258bc661bfSMarek Roszko if (atmel_use_pdc_rx(port)) { 17268bc661bfSMarek Roszko /* set UART timeout */ 17278bc661bfSMarek Roszko if (!atmel_port->is_usart) { 17282e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 17292e68c22fSElen Song jiffies + uart_poll_timeout(port)); 17302e68c22fSElen Song /* set USART timeout */ 17312e68c22fSElen Song } else { 1732ab4382d2SGreg Kroah-Hartman UART_PUT_RTOR(port, PDC_RX_TIMEOUT); 1733ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STTTO); 1734ab4382d2SGreg Kroah-Hartman 1735ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 17362e68c22fSElen Song } 1737ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 1738ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); 173934df42f5SElen Song } else if (atmel_use_dma_rx(port)) { 17402e68c22fSElen Song /* set UART timeout */ 17412e68c22fSElen Song if (!atmel_port->is_usart) { 17422e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 17432e68c22fSElen Song jiffies + uart_poll_timeout(port)); 17442e68c22fSElen Song /* set USART timeout */ 17452e68c22fSElen Song } else { 174634df42f5SElen Song UART_PUT_RTOR(port, PDC_RX_TIMEOUT); 174734df42f5SElen Song UART_PUT_CR(port, ATMEL_US_STTTO); 174834df42f5SElen Song 174934df42f5SElen Song UART_PUT_IER(port, ATMEL_US_TIMEOUT); 17502e68c22fSElen Song } 1751ab4382d2SGreg Kroah-Hartman } else { 1752ab4382d2SGreg Kroah-Hartman /* enable receive only */ 1753ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_RXRDY); 1754ab4382d2SGreg Kroah-Hartman } 1755ab4382d2SGreg Kroah-Hartman 1756ab4382d2SGreg Kroah-Hartman return 0; 1757*ab5e4e41SRichard Genoud 1758*ab5e4e41SRichard Genoud free_irq: 1759*ab5e4e41SRichard Genoud free_irq(port->irq, port); 1760*ab5e4e41SRichard Genoud 1761*ab5e4e41SRichard Genoud return retval; 1762ab4382d2SGreg Kroah-Hartman } 1763ab4382d2SGreg Kroah-Hartman 1764ab4382d2SGreg Kroah-Hartman /* 1765ab4382d2SGreg Kroah-Hartman * Disable the port 1766ab4382d2SGreg Kroah-Hartman */ 1767ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port) 1768ab4382d2SGreg Kroah-Hartman { 1769ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 17700cc7c6c7SMarek Roszko 1771ab4382d2SGreg Kroah-Hartman /* 17728bc661bfSMarek Roszko * Prevent any tasklets being scheduled during 17738bc661bfSMarek Roszko * cleanup 17748bc661bfSMarek Roszko */ 17758bc661bfSMarek Roszko del_timer_sync(&atmel_port->uart_timer); 17768bc661bfSMarek Roszko 17778bc661bfSMarek Roszko /* 17780cc7c6c7SMarek Roszko * Clear out any scheduled tasklets before 17790cc7c6c7SMarek Roszko * we destroy the buffers 17800cc7c6c7SMarek Roszko */ 17810cc7c6c7SMarek Roszko tasklet_kill(&atmel_port->tasklet); 17820cc7c6c7SMarek Roszko 17830cc7c6c7SMarek Roszko /* 17840cc7c6c7SMarek Roszko * Ensure everything is stopped and 17850cc7c6c7SMarek Roszko * disable all interrupts, port and break condition. 1786ab4382d2SGreg Kroah-Hartman */ 1787ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 1788ab4382d2SGreg Kroah-Hartman atmel_stop_tx(port); 1789ab4382d2SGreg Kroah-Hartman 17900cc7c6c7SMarek Roszko UART_PUT_CR(port, ATMEL_US_RSTSTA); 17910cc7c6c7SMarek Roszko UART_PUT_IDR(port, -1); 17920cc7c6c7SMarek Roszko 17930cc7c6c7SMarek Roszko 1794ab4382d2SGreg Kroah-Hartman /* 1795ab4382d2SGreg Kroah-Hartman * Shut-down the DMA. 1796ab4382d2SGreg Kroah-Hartman */ 1797a930e528SElen Song if (atmel_port->release_rx) 1798a930e528SElen Song atmel_port->release_rx(port); 1799a930e528SElen Song if (atmel_port->release_tx) 1800a930e528SElen Song atmel_port->release_tx(port); 1801ab4382d2SGreg Kroah-Hartman 1802ab4382d2SGreg Kroah-Hartman /* 1803bb7e73c5SMark Deneen * Reset ring buffer pointers 1804bb7e73c5SMark Deneen */ 1805bb7e73c5SMark Deneen atmel_port->rx_ring.head = 0; 1806bb7e73c5SMark Deneen atmel_port->rx_ring.tail = 0; 1807bb7e73c5SMark Deneen 1808bb7e73c5SMark Deneen /* 1809*ab5e4e41SRichard Genoud * Free the interrupts 1810ab4382d2SGreg Kroah-Hartman */ 1811ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 1812*ab5e4e41SRichard Genoud atmel_free_gpio_irq(port); 1813*ab5e4e41SRichard Genoud 1814*ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = false; 1815ab4382d2SGreg Kroah-Hartman } 1816ab4382d2SGreg Kroah-Hartman 1817ab4382d2SGreg Kroah-Hartman /* 1818ab4382d2SGreg Kroah-Hartman * Flush any TX data submitted for DMA. Called when the TX circular 1819ab4382d2SGreg Kroah-Hartman * buffer is reset. 1820ab4382d2SGreg Kroah-Hartman */ 1821ab4382d2SGreg Kroah-Hartman static void atmel_flush_buffer(struct uart_port *port) 1822ab4382d2SGreg Kroah-Hartman { 1823ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1824ab4382d2SGreg Kroah-Hartman 182564e22ebeSElen Song if (atmel_use_pdc_tx(port)) { 1826ab4382d2SGreg Kroah-Hartman UART_PUT_TCR(port, 0); 1827ab4382d2SGreg Kroah-Hartman atmel_port->pdc_tx.ofs = 0; 1828ab4382d2SGreg Kroah-Hartman } 1829ab4382d2SGreg Kroah-Hartman } 1830ab4382d2SGreg Kroah-Hartman 1831ab4382d2SGreg Kroah-Hartman /* 1832ab4382d2SGreg Kroah-Hartman * Power / Clock management. 1833ab4382d2SGreg Kroah-Hartman */ 1834ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state, 1835ab4382d2SGreg Kroah-Hartman unsigned int oldstate) 1836ab4382d2SGreg Kroah-Hartman { 1837ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1838ab4382d2SGreg Kroah-Hartman 1839ab4382d2SGreg Kroah-Hartman switch (state) { 1840ab4382d2SGreg Kroah-Hartman case 0: 1841ab4382d2SGreg Kroah-Hartman /* 1842ab4382d2SGreg Kroah-Hartman * Enable the peripheral clock for this serial port. 1843ab4382d2SGreg Kroah-Hartman * This is called on uart_open() or a resume event. 1844ab4382d2SGreg Kroah-Hartman */ 184591f8c2d8SBoris BREZILLON clk_prepare_enable(atmel_port->clk); 1846ab4382d2SGreg Kroah-Hartman 1847ab4382d2SGreg Kroah-Hartman /* re-enable interrupts if we disabled some on suspend */ 1848ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->backup_imr); 1849ab4382d2SGreg Kroah-Hartman break; 1850ab4382d2SGreg Kroah-Hartman case 3: 1851ab4382d2SGreg Kroah-Hartman /* Back up the interrupt mask and disable all interrupts */ 1852ab4382d2SGreg Kroah-Hartman atmel_port->backup_imr = UART_GET_IMR(port); 1853ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 1854ab4382d2SGreg Kroah-Hartman 1855ab4382d2SGreg Kroah-Hartman /* 1856ab4382d2SGreg Kroah-Hartman * Disable the peripheral clock for this serial port. 1857ab4382d2SGreg Kroah-Hartman * This is called on uart_close() or a suspend event. 1858ab4382d2SGreg Kroah-Hartman */ 185991f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 1860ab4382d2SGreg Kroah-Hartman break; 1861ab4382d2SGreg Kroah-Hartman default: 1862ddaa6037SRichard Genoud dev_err(port->dev, "atmel_serial: unknown pm %d\n", state); 1863ab4382d2SGreg Kroah-Hartman } 1864ab4382d2SGreg Kroah-Hartman } 1865ab4382d2SGreg Kroah-Hartman 1866ab4382d2SGreg Kroah-Hartman /* 1867ab4382d2SGreg Kroah-Hartman * Change the port parameters 1868ab4382d2SGreg Kroah-Hartman */ 1869ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, 1870ab4382d2SGreg Kroah-Hartman struct ktermios *old) 1871ab4382d2SGreg Kroah-Hartman { 1872ab4382d2SGreg Kroah-Hartman unsigned long flags; 1873ab4382d2SGreg Kroah-Hartman unsigned int mode, imr, quot, baud; 1874ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1875ab4382d2SGreg Kroah-Hartman 1876ab4382d2SGreg Kroah-Hartman /* Get current mode register */ 1877ab4382d2SGreg Kroah-Hartman mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL 1878ab4382d2SGreg Kroah-Hartman | ATMEL_US_NBSTOP | ATMEL_US_PAR 1879ab4382d2SGreg Kroah-Hartman | ATMEL_US_USMODE); 1880ab4382d2SGreg Kroah-Hartman 1881ab4382d2SGreg Kroah-Hartman baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); 1882ab4382d2SGreg Kroah-Hartman quot = uart_get_divisor(port, baud); 1883ab4382d2SGreg Kroah-Hartman 1884ab4382d2SGreg Kroah-Hartman if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */ 1885ab4382d2SGreg Kroah-Hartman quot /= 8; 1886ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USCLKS_MCK_DIV8; 1887ab4382d2SGreg Kroah-Hartman } 1888ab4382d2SGreg Kroah-Hartman 1889ab4382d2SGreg Kroah-Hartman /* byte size */ 1890ab4382d2SGreg Kroah-Hartman switch (termios->c_cflag & CSIZE) { 1891ab4382d2SGreg Kroah-Hartman case CS5: 1892ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_5; 1893ab4382d2SGreg Kroah-Hartman break; 1894ab4382d2SGreg Kroah-Hartman case CS6: 1895ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_6; 1896ab4382d2SGreg Kroah-Hartman break; 1897ab4382d2SGreg Kroah-Hartman case CS7: 1898ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_7; 1899ab4382d2SGreg Kroah-Hartman break; 1900ab4382d2SGreg Kroah-Hartman default: 1901ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_8; 1902ab4382d2SGreg Kroah-Hartman break; 1903ab4382d2SGreg Kroah-Hartman } 1904ab4382d2SGreg Kroah-Hartman 1905ab4382d2SGreg Kroah-Hartman /* stop bits */ 1906ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CSTOPB) 1907ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_NBSTOP_2; 1908ab4382d2SGreg Kroah-Hartman 1909ab4382d2SGreg Kroah-Hartman /* parity */ 1910ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARENB) { 1911ab4382d2SGreg Kroah-Hartman /* Mark or Space parity */ 1912ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CMSPAR) { 1913ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARODD) 1914ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_MARK; 1915ab4382d2SGreg Kroah-Hartman else 1916ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_SPACE; 1917ab4382d2SGreg Kroah-Hartman } else if (termios->c_cflag & PARODD) 1918ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_ODD; 1919ab4382d2SGreg Kroah-Hartman else 1920ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_EVEN; 1921ab4382d2SGreg Kroah-Hartman } else 1922ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_NONE; 1923ab4382d2SGreg Kroah-Hartman 1924ab4382d2SGreg Kroah-Hartman /* hardware handshake (RTS/CTS) */ 1925ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CRTSCTS) 1926ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_HWHS; 1927ab4382d2SGreg Kroah-Hartman else 1928ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_NORMAL; 1929ab4382d2SGreg Kroah-Hartman 1930ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 1931ab4382d2SGreg Kroah-Hartman 1932ab4382d2SGreg Kroah-Hartman port->read_status_mask = ATMEL_US_OVRE; 1933ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & INPCK) 1934ab4382d2SGreg Kroah-Hartman port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 1935ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & (BRKINT | PARMRK)) 1936ab4382d2SGreg Kroah-Hartman port->read_status_mask |= ATMEL_US_RXBRK; 1937ab4382d2SGreg Kroah-Hartman 193864e22ebeSElen Song if (atmel_use_pdc_rx(port)) 1939ab4382d2SGreg Kroah-Hartman /* need to enable error interrupts */ 1940ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, port->read_status_mask); 1941ab4382d2SGreg Kroah-Hartman 1942ab4382d2SGreg Kroah-Hartman /* 1943ab4382d2SGreg Kroah-Hartman * Characters to ignore 1944ab4382d2SGreg Kroah-Hartman */ 1945ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 1946ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 1947ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 1948ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNBRK) { 1949ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_RXBRK; 1950ab4382d2SGreg Kroah-Hartman /* 1951ab4382d2SGreg Kroah-Hartman * If we're ignoring parity and break indicators, 1952ab4382d2SGreg Kroah-Hartman * ignore overruns too (for real raw support). 1953ab4382d2SGreg Kroah-Hartman */ 1954ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 1955ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_OVRE; 1956ab4382d2SGreg Kroah-Hartman } 1957ab4382d2SGreg Kroah-Hartman /* TODO: Ignore all characters if CREAD is set.*/ 1958ab4382d2SGreg Kroah-Hartman 1959ab4382d2SGreg Kroah-Hartman /* update the per-port timeout */ 1960ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, termios->c_cflag, baud); 1961ab4382d2SGreg Kroah-Hartman 1962ab4382d2SGreg Kroah-Hartman /* 1963ab4382d2SGreg Kroah-Hartman * save/disable interrupts. The tty layer will ensure that the 1964ab4382d2SGreg Kroah-Hartman * transmitter is empty if requested by the caller, so there's 1965ab4382d2SGreg Kroah-Hartman * no need to wait for it here. 1966ab4382d2SGreg Kroah-Hartman */ 1967ab4382d2SGreg Kroah-Hartman imr = UART_GET_IMR(port); 1968ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 1969ab4382d2SGreg Kroah-Hartman 1970ab4382d2SGreg Kroah-Hartman /* disable receiver and transmitter */ 1971ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS); 1972ab4382d2SGreg Kroah-Hartman 1973ab4382d2SGreg Kroah-Hartman /* Resetting serial mode to RS232 (0x0) */ 1974ab4382d2SGreg Kroah-Hartman mode &= ~ATMEL_US_USMODE; 1975ab4382d2SGreg Kroah-Hartman 1976ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_ENABLED) { 197793f3350cSClaudio Scordino if ((atmel_port->rs485.delay_rts_after_send) > 0) 1978ab4382d2SGreg Kroah-Hartman UART_PUT_TTGR(port, 1979ab4382d2SGreg Kroah-Hartman atmel_port->rs485.delay_rts_after_send); 1980ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 1981ab4382d2SGreg Kroah-Hartman } 1982ab4382d2SGreg Kroah-Hartman 1983ab4382d2SGreg Kroah-Hartman /* set the parity, stop bits and data size */ 1984ab4382d2SGreg Kroah-Hartman UART_PUT_MR(port, mode); 1985ab4382d2SGreg Kroah-Hartman 1986ab4382d2SGreg Kroah-Hartman /* set the baud rate */ 1987ab4382d2SGreg Kroah-Hartman UART_PUT_BRGR(port, quot); 1988ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 1989ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 1990ab4382d2SGreg Kroah-Hartman 1991ab4382d2SGreg Kroah-Hartman /* restore interrupts */ 1992ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, imr); 1993ab4382d2SGreg Kroah-Hartman 1994ab4382d2SGreg Kroah-Hartman /* CTS flow-control and modem-status interrupts */ 1995ab4382d2SGreg Kroah-Hartman if (UART_ENABLE_MS(port, termios->c_cflag)) 1996ab4382d2SGreg Kroah-Hartman port->ops->enable_ms(port); 1997ab4382d2SGreg Kroah-Hartman 1998ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 1999ab4382d2SGreg Kroah-Hartman } 2000ab4382d2SGreg Kroah-Hartman 200142bd7a4fSViktar Palstsiuk static void atmel_set_ldisc(struct uart_port *port, int new) 200242bd7a4fSViktar Palstsiuk { 2003b54bf3b2SJiri Slaby if (new == N_PPS) { 200442bd7a4fSViktar Palstsiuk port->flags |= UPF_HARDPPS_CD; 200542bd7a4fSViktar Palstsiuk atmel_enable_ms(port); 200642bd7a4fSViktar Palstsiuk } else { 200742bd7a4fSViktar Palstsiuk port->flags &= ~UPF_HARDPPS_CD; 200842bd7a4fSViktar Palstsiuk } 200942bd7a4fSViktar Palstsiuk } 201042bd7a4fSViktar Palstsiuk 2011ab4382d2SGreg Kroah-Hartman /* 2012ab4382d2SGreg Kroah-Hartman * Return string describing the specified port 2013ab4382d2SGreg Kroah-Hartman */ 2014ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port) 2015ab4382d2SGreg Kroah-Hartman { 2016ab4382d2SGreg Kroah-Hartman return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; 2017ab4382d2SGreg Kroah-Hartman } 2018ab4382d2SGreg Kroah-Hartman 2019ab4382d2SGreg Kroah-Hartman /* 2020ab4382d2SGreg Kroah-Hartman * Release the memory region(s) being used by 'port'. 2021ab4382d2SGreg Kroah-Hartman */ 2022ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port) 2023ab4382d2SGreg Kroah-Hartman { 2024ab4382d2SGreg Kroah-Hartman struct platform_device *pdev = to_platform_device(port->dev); 2025ab4382d2SGreg Kroah-Hartman int size = pdev->resource[0].end - pdev->resource[0].start + 1; 2026ab4382d2SGreg Kroah-Hartman 2027ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2028ab4382d2SGreg Kroah-Hartman 2029ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2030ab4382d2SGreg Kroah-Hartman iounmap(port->membase); 2031ab4382d2SGreg Kroah-Hartman port->membase = NULL; 2032ab4382d2SGreg Kroah-Hartman } 2033ab4382d2SGreg Kroah-Hartman } 2034ab4382d2SGreg Kroah-Hartman 2035ab4382d2SGreg Kroah-Hartman /* 2036ab4382d2SGreg Kroah-Hartman * Request the memory region(s) being used by 'port'. 2037ab4382d2SGreg Kroah-Hartman */ 2038ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port) 2039ab4382d2SGreg Kroah-Hartman { 2040ab4382d2SGreg Kroah-Hartman struct platform_device *pdev = to_platform_device(port->dev); 2041ab4382d2SGreg Kroah-Hartman int size = pdev->resource[0].end - pdev->resource[0].start + 1; 2042ab4382d2SGreg Kroah-Hartman 2043ab4382d2SGreg Kroah-Hartman if (!request_mem_region(port->mapbase, size, "atmel_serial")) 2044ab4382d2SGreg Kroah-Hartman return -EBUSY; 2045ab4382d2SGreg Kroah-Hartman 2046ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2047ab4382d2SGreg Kroah-Hartman port->membase = ioremap(port->mapbase, size); 2048ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2049ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2050ab4382d2SGreg Kroah-Hartman return -ENOMEM; 2051ab4382d2SGreg Kroah-Hartman } 2052ab4382d2SGreg Kroah-Hartman } 2053ab4382d2SGreg Kroah-Hartman 2054ab4382d2SGreg Kroah-Hartman return 0; 2055ab4382d2SGreg Kroah-Hartman } 2056ab4382d2SGreg Kroah-Hartman 2057ab4382d2SGreg Kroah-Hartman /* 2058ab4382d2SGreg Kroah-Hartman * Configure/autoconfigure the port. 2059ab4382d2SGreg Kroah-Hartman */ 2060ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags) 2061ab4382d2SGreg Kroah-Hartman { 2062ab4382d2SGreg Kroah-Hartman if (flags & UART_CONFIG_TYPE) { 2063ab4382d2SGreg Kroah-Hartman port->type = PORT_ATMEL; 2064ab4382d2SGreg Kroah-Hartman atmel_request_port(port); 2065ab4382d2SGreg Kroah-Hartman } 2066ab4382d2SGreg Kroah-Hartman } 2067ab4382d2SGreg Kroah-Hartman 2068ab4382d2SGreg Kroah-Hartman /* 2069ab4382d2SGreg Kroah-Hartman * Verify the new serial_struct (for TIOCSSERIAL). 2070ab4382d2SGreg Kroah-Hartman */ 2071ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser) 2072ab4382d2SGreg Kroah-Hartman { 2073ab4382d2SGreg Kroah-Hartman int ret = 0; 2074ab4382d2SGreg Kroah-Hartman if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) 2075ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2076ab4382d2SGreg Kroah-Hartman if (port->irq != ser->irq) 2077ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2078ab4382d2SGreg Kroah-Hartman if (ser->io_type != SERIAL_IO_MEM) 2079ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2080ab4382d2SGreg Kroah-Hartman if (port->uartclk / 16 != ser->baud_base) 2081ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2082ab4382d2SGreg Kroah-Hartman if ((void *)port->mapbase != ser->iomem_base) 2083ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2084ab4382d2SGreg Kroah-Hartman if (port->iobase != ser->port) 2085ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2086ab4382d2SGreg Kroah-Hartman if (ser->hub6 != 0) 2087ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2088ab4382d2SGreg Kroah-Hartman return ret; 2089ab4382d2SGreg Kroah-Hartman } 2090ab4382d2SGreg Kroah-Hartman 2091ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2092ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port) 2093ab4382d2SGreg Kroah-Hartman { 2094ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY)) 2095ab4382d2SGreg Kroah-Hartman cpu_relax(); 2096ab4382d2SGreg Kroah-Hartman 2097ab4382d2SGreg Kroah-Hartman return UART_GET_CHAR(port); 2098ab4382d2SGreg Kroah-Hartman } 2099ab4382d2SGreg Kroah-Hartman 2100ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch) 2101ab4382d2SGreg Kroah-Hartman { 2102ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY)) 2103ab4382d2SGreg Kroah-Hartman cpu_relax(); 2104ab4382d2SGreg Kroah-Hartman 2105ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, ch); 2106ab4382d2SGreg Kroah-Hartman } 2107ab4382d2SGreg Kroah-Hartman #endif 2108ab4382d2SGreg Kroah-Hartman 2109ab4382d2SGreg Kroah-Hartman static int 2110ab4382d2SGreg Kroah-Hartman atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg) 2111ab4382d2SGreg Kroah-Hartman { 2112ab4382d2SGreg Kroah-Hartman struct serial_rs485 rs485conf; 2113ab4382d2SGreg Kroah-Hartman 2114ab4382d2SGreg Kroah-Hartman switch (cmd) { 2115ab4382d2SGreg Kroah-Hartman case TIOCSRS485: 2116ab4382d2SGreg Kroah-Hartman if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg, 2117ab4382d2SGreg Kroah-Hartman sizeof(rs485conf))) 2118ab4382d2SGreg Kroah-Hartman return -EFAULT; 2119ab4382d2SGreg Kroah-Hartman 2120ab4382d2SGreg Kroah-Hartman atmel_config_rs485(port, &rs485conf); 2121ab4382d2SGreg Kroah-Hartman break; 2122ab4382d2SGreg Kroah-Hartman 2123ab4382d2SGreg Kroah-Hartman case TIOCGRS485: 2124ab4382d2SGreg Kroah-Hartman if (copy_to_user((struct serial_rs485 *) arg, 2125ab4382d2SGreg Kroah-Hartman &(to_atmel_uart_port(port)->rs485), 2126ab4382d2SGreg Kroah-Hartman sizeof(rs485conf))) 2127ab4382d2SGreg Kroah-Hartman return -EFAULT; 2128ab4382d2SGreg Kroah-Hartman break; 2129ab4382d2SGreg Kroah-Hartman 2130ab4382d2SGreg Kroah-Hartman default: 2131ab4382d2SGreg Kroah-Hartman return -ENOIOCTLCMD; 2132ab4382d2SGreg Kroah-Hartman } 2133ab4382d2SGreg Kroah-Hartman return 0; 2134ab4382d2SGreg Kroah-Hartman } 2135ab4382d2SGreg Kroah-Hartman 2136ab4382d2SGreg Kroah-Hartman 2137ab4382d2SGreg Kroah-Hartman 2138ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = { 2139ab4382d2SGreg Kroah-Hartman .tx_empty = atmel_tx_empty, 2140ab4382d2SGreg Kroah-Hartman .set_mctrl = atmel_set_mctrl, 2141ab4382d2SGreg Kroah-Hartman .get_mctrl = atmel_get_mctrl, 2142ab4382d2SGreg Kroah-Hartman .stop_tx = atmel_stop_tx, 2143ab4382d2SGreg Kroah-Hartman .start_tx = atmel_start_tx, 2144ab4382d2SGreg Kroah-Hartman .stop_rx = atmel_stop_rx, 2145ab4382d2SGreg Kroah-Hartman .enable_ms = atmel_enable_ms, 2146ab4382d2SGreg Kroah-Hartman .break_ctl = atmel_break_ctl, 2147ab4382d2SGreg Kroah-Hartman .startup = atmel_startup, 2148ab4382d2SGreg Kroah-Hartman .shutdown = atmel_shutdown, 2149ab4382d2SGreg Kroah-Hartman .flush_buffer = atmel_flush_buffer, 2150ab4382d2SGreg Kroah-Hartman .set_termios = atmel_set_termios, 215142bd7a4fSViktar Palstsiuk .set_ldisc = atmel_set_ldisc, 2152ab4382d2SGreg Kroah-Hartman .type = atmel_type, 2153ab4382d2SGreg Kroah-Hartman .release_port = atmel_release_port, 2154ab4382d2SGreg Kroah-Hartman .request_port = atmel_request_port, 2155ab4382d2SGreg Kroah-Hartman .config_port = atmel_config_port, 2156ab4382d2SGreg Kroah-Hartman .verify_port = atmel_verify_port, 2157ab4382d2SGreg Kroah-Hartman .pm = atmel_serial_pm, 2158ab4382d2SGreg Kroah-Hartman .ioctl = atmel_ioctl, 2159ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2160ab4382d2SGreg Kroah-Hartman .poll_get_char = atmel_poll_get_char, 2161ab4382d2SGreg Kroah-Hartman .poll_put_char = atmel_poll_put_char, 2162ab4382d2SGreg Kroah-Hartman #endif 2163ab4382d2SGreg Kroah-Hartman }; 2164ab4382d2SGreg Kroah-Hartman 2165ab4382d2SGreg Kroah-Hartman /* 2166ab4382d2SGreg Kroah-Hartman * Configure the port from the platform device resource info. 2167ab4382d2SGreg Kroah-Hartman */ 216891f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port, 2169ab4382d2SGreg Kroah-Hartman struct platform_device *pdev) 2170ab4382d2SGreg Kroah-Hartman { 217191f8c2d8SBoris BREZILLON int ret; 2172ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_port->uart; 2173574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 2174ab4382d2SGreg Kroah-Hartman 217533d64c4fSElen Song if (!atmel_init_property(atmel_port, pdev)) 2176a930e528SElen Song atmel_set_ops(port); 2177a930e528SElen Song 217833d64c4fSElen Song atmel_init_rs485(atmel_port, pdev); 217933d64c4fSElen Song 2180ab4382d2SGreg Kroah-Hartman port->iotype = UPIO_MEM; 2181ab4382d2SGreg Kroah-Hartman port->flags = UPF_BOOT_AUTOCONF; 2182ab4382d2SGreg Kroah-Hartman port->ops = &atmel_pops; 2183ab4382d2SGreg Kroah-Hartman port->fifosize = 1; 2184ab4382d2SGreg Kroah-Hartman port->dev = &pdev->dev; 2185ab4382d2SGreg Kroah-Hartman port->mapbase = pdev->resource[0].start; 2186ab4382d2SGreg Kroah-Hartman port->irq = pdev->resource[1].start; 2187ab4382d2SGreg Kroah-Hartman 2188ab4382d2SGreg Kroah-Hartman tasklet_init(&atmel_port->tasklet, atmel_tasklet_func, 2189ab4382d2SGreg Kroah-Hartman (unsigned long)port); 2190ab4382d2SGreg Kroah-Hartman 2191ab4382d2SGreg Kroah-Hartman memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); 2192ab4382d2SGreg Kroah-Hartman 21935fbe46b6SNicolas Ferre if (pdata && pdata->regs) { 2194ab4382d2SGreg Kroah-Hartman /* Already mapped by setup code */ 21951acfc7ecSNicolas Ferre port->membase = pdata->regs; 2196588edbf3SNicolas Ferre } else { 2197ab4382d2SGreg Kroah-Hartman port->flags |= UPF_IOREMAP; 2198ab4382d2SGreg Kroah-Hartman port->membase = NULL; 2199ab4382d2SGreg Kroah-Hartman } 2200ab4382d2SGreg Kroah-Hartman 2201ab4382d2SGreg Kroah-Hartman /* for console, the clock could already be configured */ 2202ab4382d2SGreg Kroah-Hartman if (!atmel_port->clk) { 2203ab4382d2SGreg Kroah-Hartman atmel_port->clk = clk_get(&pdev->dev, "usart"); 220491f8c2d8SBoris BREZILLON if (IS_ERR(atmel_port->clk)) { 220591f8c2d8SBoris BREZILLON ret = PTR_ERR(atmel_port->clk); 220691f8c2d8SBoris BREZILLON atmel_port->clk = NULL; 220791f8c2d8SBoris BREZILLON return ret; 220891f8c2d8SBoris BREZILLON } 220991f8c2d8SBoris BREZILLON ret = clk_prepare_enable(atmel_port->clk); 221091f8c2d8SBoris BREZILLON if (ret) { 221191f8c2d8SBoris BREZILLON clk_put(atmel_port->clk); 221291f8c2d8SBoris BREZILLON atmel_port->clk = NULL; 221391f8c2d8SBoris BREZILLON return ret; 221491f8c2d8SBoris BREZILLON } 2215ab4382d2SGreg Kroah-Hartman port->uartclk = clk_get_rate(atmel_port->clk); 221691f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 2217ab4382d2SGreg Kroah-Hartman /* only enable clock when USART is in use */ 2218ab4382d2SGreg Kroah-Hartman } 2219ab4382d2SGreg Kroah-Hartman 2220ab4382d2SGreg Kroah-Hartman /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ 2221ab4382d2SGreg Kroah-Hartman if (atmel_port->rs485.flags & SER_RS485_ENABLED) 2222ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 222364e22ebeSElen Song else if (atmel_use_pdc_tx(port)) { 2224ab4382d2SGreg Kroah-Hartman port->fifosize = PDC_BUFFER_SIZE; 2225ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; 2226ab4382d2SGreg Kroah-Hartman } else { 2227ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 2228ab4382d2SGreg Kroah-Hartman } 222991f8c2d8SBoris BREZILLON 223091f8c2d8SBoris BREZILLON return 0; 2231ab4382d2SGreg Kroah-Hartman } 2232ab4382d2SGreg Kroah-Hartman 223369f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device; /* the serial console device */ 223469f6a27bSJean-Christophe PLAGNIOL-VILLARD 2235ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2236ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch) 2237ab4382d2SGreg Kroah-Hartman { 2238ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY)) 2239ab4382d2SGreg Kroah-Hartman cpu_relax(); 2240ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, ch); 2241ab4382d2SGreg Kroah-Hartman } 2242ab4382d2SGreg Kroah-Hartman 2243ab4382d2SGreg Kroah-Hartman /* 2244ab4382d2SGreg Kroah-Hartman * Interrupts are disabled on entering 2245ab4382d2SGreg Kroah-Hartman */ 2246ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count) 2247ab4382d2SGreg Kroah-Hartman { 2248ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2249ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2250ab4382d2SGreg Kroah-Hartman unsigned int status, imr; 2251ab4382d2SGreg Kroah-Hartman unsigned int pdc_tx; 2252ab4382d2SGreg Kroah-Hartman 2253ab4382d2SGreg Kroah-Hartman /* 2254ab4382d2SGreg Kroah-Hartman * First, save IMR and then disable interrupts 2255ab4382d2SGreg Kroah-Hartman */ 2256ab4382d2SGreg Kroah-Hartman imr = UART_GET_IMR(port); 2257ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask); 2258ab4382d2SGreg Kroah-Hartman 2259ab4382d2SGreg Kroah-Hartman /* Store PDC transmit status and disable it */ 2260ab4382d2SGreg Kroah-Hartman pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN; 2261ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 2262ab4382d2SGreg Kroah-Hartman 2263ab4382d2SGreg Kroah-Hartman uart_console_write(port, s, count, atmel_console_putchar); 2264ab4382d2SGreg Kroah-Hartman 2265ab4382d2SGreg Kroah-Hartman /* 2266ab4382d2SGreg Kroah-Hartman * Finally, wait for transmitter to become empty 2267ab4382d2SGreg Kroah-Hartman * and restore IMR 2268ab4382d2SGreg Kroah-Hartman */ 2269ab4382d2SGreg Kroah-Hartman do { 2270ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 2271ab4382d2SGreg Kroah-Hartman } while (!(status & ATMEL_US_TXRDY)); 2272ab4382d2SGreg Kroah-Hartman 2273ab4382d2SGreg Kroah-Hartman /* Restore PDC transmit status */ 2274ab4382d2SGreg Kroah-Hartman if (pdc_tx) 2275ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); 2276ab4382d2SGreg Kroah-Hartman 2277ab4382d2SGreg Kroah-Hartman /* set interrupts back the way they were */ 2278ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, imr); 2279ab4382d2SGreg Kroah-Hartman } 2280ab4382d2SGreg Kroah-Hartman 2281ab4382d2SGreg Kroah-Hartman /* 2282ab4382d2SGreg Kroah-Hartman * If the port was already initialised (eg, by a boot loader), 2283ab4382d2SGreg Kroah-Hartman * try to determine the current setup. 2284ab4382d2SGreg Kroah-Hartman */ 2285ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud, 2286ab4382d2SGreg Kroah-Hartman int *parity, int *bits) 2287ab4382d2SGreg Kroah-Hartman { 2288ab4382d2SGreg Kroah-Hartman unsigned int mr, quot; 2289ab4382d2SGreg Kroah-Hartman 2290ab4382d2SGreg Kroah-Hartman /* 2291ab4382d2SGreg Kroah-Hartman * If the baud rate generator isn't running, the port wasn't 2292ab4382d2SGreg Kroah-Hartman * initialized by the boot loader. 2293ab4382d2SGreg Kroah-Hartman */ 2294ab4382d2SGreg Kroah-Hartman quot = UART_GET_BRGR(port) & ATMEL_US_CD; 2295ab4382d2SGreg Kroah-Hartman if (!quot) 2296ab4382d2SGreg Kroah-Hartman return; 2297ab4382d2SGreg Kroah-Hartman 2298ab4382d2SGreg Kroah-Hartman mr = UART_GET_MR(port) & ATMEL_US_CHRL; 2299ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_CHRL_8) 2300ab4382d2SGreg Kroah-Hartman *bits = 8; 2301ab4382d2SGreg Kroah-Hartman else 2302ab4382d2SGreg Kroah-Hartman *bits = 7; 2303ab4382d2SGreg Kroah-Hartman 2304ab4382d2SGreg Kroah-Hartman mr = UART_GET_MR(port) & ATMEL_US_PAR; 2305ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_PAR_EVEN) 2306ab4382d2SGreg Kroah-Hartman *parity = 'e'; 2307ab4382d2SGreg Kroah-Hartman else if (mr == ATMEL_US_PAR_ODD) 2308ab4382d2SGreg Kroah-Hartman *parity = 'o'; 2309ab4382d2SGreg Kroah-Hartman 2310ab4382d2SGreg Kroah-Hartman /* 2311ab4382d2SGreg Kroah-Hartman * The serial core only rounds down when matching this to a 2312ab4382d2SGreg Kroah-Hartman * supported baud rate. Make sure we don't end up slightly 2313ab4382d2SGreg Kroah-Hartman * lower than one of those, as it would make us fall through 2314ab4382d2SGreg Kroah-Hartman * to a much lower baud rate than we really want. 2315ab4382d2SGreg Kroah-Hartman */ 2316ab4382d2SGreg Kroah-Hartman *baud = port->uartclk / (16 * (quot - 1)); 2317ab4382d2SGreg Kroah-Hartman } 2318ab4382d2SGreg Kroah-Hartman 2319ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options) 2320ab4382d2SGreg Kroah-Hartman { 232191f8c2d8SBoris BREZILLON int ret; 2322ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2323ab4382d2SGreg Kroah-Hartman int baud = 115200; 2324ab4382d2SGreg Kroah-Hartman int bits = 8; 2325ab4382d2SGreg Kroah-Hartman int parity = 'n'; 2326ab4382d2SGreg Kroah-Hartman int flow = 'n'; 2327ab4382d2SGreg Kroah-Hartman 2328ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2329ab4382d2SGreg Kroah-Hartman /* Port not initialized yet - delay setup */ 2330ab4382d2SGreg Kroah-Hartman return -ENODEV; 2331ab4382d2SGreg Kroah-Hartman } 2332ab4382d2SGreg Kroah-Hartman 233391f8c2d8SBoris BREZILLON ret = clk_prepare_enable(atmel_ports[co->index].clk); 233491f8c2d8SBoris BREZILLON if (ret) 233591f8c2d8SBoris BREZILLON return ret; 2336ab4382d2SGreg Kroah-Hartman 2337ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 2338ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 2339ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 2340ab4382d2SGreg Kroah-Hartman 2341ab4382d2SGreg Kroah-Hartman if (options) 2342ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 2343ab4382d2SGreg Kroah-Hartman else 2344ab4382d2SGreg Kroah-Hartman atmel_console_get_options(port, &baud, &parity, &bits); 2345ab4382d2SGreg Kroah-Hartman 2346ab4382d2SGreg Kroah-Hartman return uart_set_options(port, co, baud, parity, bits, flow); 2347ab4382d2SGreg Kroah-Hartman } 2348ab4382d2SGreg Kroah-Hartman 2349ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart; 2350ab4382d2SGreg Kroah-Hartman 2351ab4382d2SGreg Kroah-Hartman static struct console atmel_console = { 2352ab4382d2SGreg Kroah-Hartman .name = ATMEL_DEVICENAME, 2353ab4382d2SGreg Kroah-Hartman .write = atmel_console_write, 2354ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 2355ab4382d2SGreg Kroah-Hartman .setup = atmel_console_setup, 2356ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 2357ab4382d2SGreg Kroah-Hartman .index = -1, 2358ab4382d2SGreg Kroah-Hartman .data = &atmel_uart, 2359ab4382d2SGreg Kroah-Hartman }; 2360ab4382d2SGreg Kroah-Hartman 2361ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE (&atmel_console) 2362ab4382d2SGreg Kroah-Hartman 2363ab4382d2SGreg Kroah-Hartman /* 2364ab4382d2SGreg Kroah-Hartman * Early console initialization (before VM subsystem initialized). 2365ab4382d2SGreg Kroah-Hartman */ 2366ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void) 2367ab4382d2SGreg Kroah-Hartman { 236891f8c2d8SBoris BREZILLON int ret; 2369ab4382d2SGreg Kroah-Hartman if (atmel_default_console_device) { 23700d0a3cc1SVoss, Nikolaus struct atmel_uart_data *pdata = 2371574de559SJingoo Han dev_get_platdata(&atmel_default_console_device->dev); 2372efb8d21bSLinus Torvalds int id = pdata->num; 23734cbf9f48SNicolas Ferre struct atmel_uart_port *port = &atmel_ports[id]; 23740d0a3cc1SVoss, Nikolaus 23754cbf9f48SNicolas Ferre port->backup_imr = 0; 23764cbf9f48SNicolas Ferre port->uart.line = id; 23774cbf9f48SNicolas Ferre 23784cbf9f48SNicolas Ferre add_preferred_console(ATMEL_DEVICENAME, id, NULL); 237991f8c2d8SBoris BREZILLON ret = atmel_init_port(port, atmel_default_console_device); 238091f8c2d8SBoris BREZILLON if (ret) 238191f8c2d8SBoris BREZILLON return ret; 2382ab4382d2SGreg Kroah-Hartman register_console(&atmel_console); 2383ab4382d2SGreg Kroah-Hartman } 2384ab4382d2SGreg Kroah-Hartman 2385ab4382d2SGreg Kroah-Hartman return 0; 2386ab4382d2SGreg Kroah-Hartman } 2387ab4382d2SGreg Kroah-Hartman 2388ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init); 2389ab4382d2SGreg Kroah-Hartman 2390ab4382d2SGreg Kroah-Hartman /* 2391ab4382d2SGreg Kroah-Hartman * Late console initialization. 2392ab4382d2SGreg Kroah-Hartman */ 2393ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void) 2394ab4382d2SGreg Kroah-Hartman { 2395ab4382d2SGreg Kroah-Hartman if (atmel_default_console_device 2396ab4382d2SGreg Kroah-Hartman && !(atmel_console.flags & CON_ENABLED)) 2397ab4382d2SGreg Kroah-Hartman register_console(&atmel_console); 2398ab4382d2SGreg Kroah-Hartman 2399ab4382d2SGreg Kroah-Hartman return 0; 2400ab4382d2SGreg Kroah-Hartman } 2401ab4382d2SGreg Kroah-Hartman 2402ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init); 2403ab4382d2SGreg Kroah-Hartman 2404ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 2405ab4382d2SGreg Kroah-Hartman { 2406ab4382d2SGreg Kroah-Hartman return port->cons && port->cons->index == port->line; 2407ab4382d2SGreg Kroah-Hartman } 2408ab4382d2SGreg Kroah-Hartman 2409ab4382d2SGreg Kroah-Hartman #else 2410ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE NULL 2411ab4382d2SGreg Kroah-Hartman 2412ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 2413ab4382d2SGreg Kroah-Hartman { 2414ab4382d2SGreg Kroah-Hartman return false; 2415ab4382d2SGreg Kroah-Hartman } 2416ab4382d2SGreg Kroah-Hartman #endif 2417ab4382d2SGreg Kroah-Hartman 2418ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = { 2419ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 2420ab4382d2SGreg Kroah-Hartman .driver_name = "atmel_serial", 2421ab4382d2SGreg Kroah-Hartman .dev_name = ATMEL_DEVICENAME, 2422ab4382d2SGreg Kroah-Hartman .major = SERIAL_ATMEL_MAJOR, 2423ab4382d2SGreg Kroah-Hartman .minor = MINOR_START, 2424ab4382d2SGreg Kroah-Hartman .nr = ATMEL_MAX_UART, 2425ab4382d2SGreg Kroah-Hartman .cons = ATMEL_CONSOLE_DEVICE, 2426ab4382d2SGreg Kroah-Hartman }; 2427ab4382d2SGreg Kroah-Hartman 2428ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM 2429ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void) 2430ab4382d2SGreg Kroah-Hartman { 2431ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91 2432ab4382d2SGreg Kroah-Hartman return at91_suspend_entering_slow_clock(); 2433ab4382d2SGreg Kroah-Hartman #else 2434ab4382d2SGreg Kroah-Hartman return false; 2435ab4382d2SGreg Kroah-Hartman #endif 2436ab4382d2SGreg Kroah-Hartman } 2437ab4382d2SGreg Kroah-Hartman 2438ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev, 2439ab4382d2SGreg Kroah-Hartman pm_message_t state) 2440ab4382d2SGreg Kroah-Hartman { 2441ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2442ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2443ab4382d2SGreg Kroah-Hartman 2444ab4382d2SGreg Kroah-Hartman if (atmel_is_console_port(port) && console_suspend_enabled) { 2445ab4382d2SGreg Kroah-Hartman /* Drain the TX shifter */ 2446ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) 2447ab4382d2SGreg Kroah-Hartman cpu_relax(); 2448ab4382d2SGreg Kroah-Hartman } 2449ab4382d2SGreg Kroah-Hartman 2450ab4382d2SGreg Kroah-Hartman /* we can not wake up if we're running on slow clock */ 2451ab4382d2SGreg Kroah-Hartman atmel_port->may_wakeup = device_may_wakeup(&pdev->dev); 2452ab4382d2SGreg Kroah-Hartman if (atmel_serial_clk_will_stop()) 2453ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, 0); 2454ab4382d2SGreg Kroah-Hartman 2455ab4382d2SGreg Kroah-Hartman uart_suspend_port(&atmel_uart, port); 2456ab4382d2SGreg Kroah-Hartman 2457ab4382d2SGreg Kroah-Hartman return 0; 2458ab4382d2SGreg Kroah-Hartman } 2459ab4382d2SGreg Kroah-Hartman 2460ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev) 2461ab4382d2SGreg Kroah-Hartman { 2462ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2463ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2464ab4382d2SGreg Kroah-Hartman 2465ab4382d2SGreg Kroah-Hartman uart_resume_port(&atmel_uart, port); 2466ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup); 2467ab4382d2SGreg Kroah-Hartman 2468ab4382d2SGreg Kroah-Hartman return 0; 2469ab4382d2SGreg Kroah-Hartman } 2470ab4382d2SGreg Kroah-Hartman #else 2471ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL 2472ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL 2473ab4382d2SGreg Kroah-Hartman #endif 2474ab4382d2SGreg Kroah-Hartman 2475e0b0baadSRichard Genoud static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev) 2476e0b0baadSRichard Genoud { 2477*ab5e4e41SRichard Genoud enum mctrl_gpio_idx i; 2478*ab5e4e41SRichard Genoud struct gpio_desc *gpiod; 2479*ab5e4e41SRichard Genoud 2480e0b0baadSRichard Genoud p->gpios = mctrl_gpio_init(dev, 0); 2481e0b0baadSRichard Genoud if (IS_ERR_OR_NULL(p->gpios)) 2482e0b0baadSRichard Genoud return -1; 2483e0b0baadSRichard Genoud 2484*ab5e4e41SRichard Genoud for (i = 0; i < UART_GPIO_MAX; i++) { 2485*ab5e4e41SRichard Genoud gpiod = mctrl_gpio_to_gpiod(p->gpios, i); 2486*ab5e4e41SRichard Genoud if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN)) 2487*ab5e4e41SRichard Genoud p->gpio_irq[i] = gpiod_to_irq(gpiod); 2488*ab5e4e41SRichard Genoud else 2489*ab5e4e41SRichard Genoud p->gpio_irq[i] = -EINVAL; 2490*ab5e4e41SRichard Genoud } 2491*ab5e4e41SRichard Genoud 2492e0b0baadSRichard Genoud return 0; 2493e0b0baadSRichard Genoud } 2494e0b0baadSRichard Genoud 24959671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev) 2496ab4382d2SGreg Kroah-Hartman { 2497ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *port; 24985fbe46b6SNicolas Ferre struct device_node *np = pdev->dev.of_node; 2499574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 2500ab4382d2SGreg Kroah-Hartman void *data; 25014cbf9f48SNicolas Ferre int ret = -ENODEV; 2502ab4382d2SGreg Kroah-Hartman 2503ab4382d2SGreg Kroah-Hartman BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); 2504ab4382d2SGreg Kroah-Hartman 25055fbe46b6SNicolas Ferre if (np) 25065fbe46b6SNicolas Ferre ret = of_alias_get_id(np, "serial"); 25075fbe46b6SNicolas Ferre else 25084cbf9f48SNicolas Ferre if (pdata) 25094cbf9f48SNicolas Ferre ret = pdata->num; 25104cbf9f48SNicolas Ferre 25114cbf9f48SNicolas Ferre if (ret < 0) 25125fbe46b6SNicolas Ferre /* port id not found in platform data nor device-tree aliases: 25134cbf9f48SNicolas Ferre * auto-enumerate it */ 2514503bded9SPawel Wieczorkiewicz ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART); 25154cbf9f48SNicolas Ferre 2516503bded9SPawel Wieczorkiewicz if (ret >= ATMEL_MAX_UART) { 25174cbf9f48SNicolas Ferre ret = -ENODEV; 25184cbf9f48SNicolas Ferre goto err; 25194cbf9f48SNicolas Ferre } 25204cbf9f48SNicolas Ferre 2521503bded9SPawel Wieczorkiewicz if (test_and_set_bit(ret, atmel_ports_in_use)) { 25224cbf9f48SNicolas Ferre /* port already in use */ 25234cbf9f48SNicolas Ferre ret = -EBUSY; 25244cbf9f48SNicolas Ferre goto err; 25254cbf9f48SNicolas Ferre } 25264cbf9f48SNicolas Ferre 25274cbf9f48SNicolas Ferre port = &atmel_ports[ret]; 2528ab4382d2SGreg Kroah-Hartman port->backup_imr = 0; 25294cbf9f48SNicolas Ferre port->uart.line = ret; 2530354e57f3SLinus Walleij 2531e0b0baadSRichard Genoud ret = atmel_init_gpios(port, &pdev->dev); 2532e0b0baadSRichard Genoud if (ret < 0) 2533e0b0baadSRichard Genoud dev_err(&pdev->dev, "%s", 2534e0b0baadSRichard Genoud "Failed to initialize GPIOs. The serial port may not work as expected"); 2535ab4382d2SGreg Kroah-Hartman 253691f8c2d8SBoris BREZILLON ret = atmel_init_port(port, pdev); 253791f8c2d8SBoris BREZILLON if (ret) 253891f8c2d8SBoris BREZILLON goto err; 2539ab4382d2SGreg Kroah-Hartman 254064e22ebeSElen Song if (!atmel_use_pdc_rx(&port->uart)) { 2541ab4382d2SGreg Kroah-Hartman ret = -ENOMEM; 2542ab4382d2SGreg Kroah-Hartman data = kmalloc(sizeof(struct atmel_uart_char) 2543ab4382d2SGreg Kroah-Hartman * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); 2544ab4382d2SGreg Kroah-Hartman if (!data) 2545ab4382d2SGreg Kroah-Hartman goto err_alloc_ring; 2546ab4382d2SGreg Kroah-Hartman port->rx_ring.buf = data; 2547ab4382d2SGreg Kroah-Hartman } 2548ab4382d2SGreg Kroah-Hartman 2549ab4382d2SGreg Kroah-Hartman ret = uart_add_one_port(&atmel_uart, &port->uart); 2550ab4382d2SGreg Kroah-Hartman if (ret) 2551ab4382d2SGreg Kroah-Hartman goto err_add_port; 2552ab4382d2SGreg Kroah-Hartman 2553ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2554ab4382d2SGreg Kroah-Hartman if (atmel_is_console_port(&port->uart) 2555ab4382d2SGreg Kroah-Hartman && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) { 2556ab4382d2SGreg Kroah-Hartman /* 2557ab4382d2SGreg Kroah-Hartman * The serial core enabled the clock for us, so undo 255891f8c2d8SBoris BREZILLON * the clk_prepare_enable() in atmel_console_setup() 2559ab4382d2SGreg Kroah-Hartman */ 256091f8c2d8SBoris BREZILLON clk_disable_unprepare(port->clk); 2561ab4382d2SGreg Kroah-Hartman } 2562ab4382d2SGreg Kroah-Hartman #endif 2563ab4382d2SGreg Kroah-Hartman 2564ab4382d2SGreg Kroah-Hartman device_init_wakeup(&pdev->dev, 1); 2565ab4382d2SGreg Kroah-Hartman platform_set_drvdata(pdev, port); 2566ab4382d2SGreg Kroah-Hartman 2567fc887b15SLinus Torvalds if (port->rs485.flags & SER_RS485_ENABLED) { 2568fc887b15SLinus Torvalds UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL); 2569fc887b15SLinus Torvalds UART_PUT_CR(&port->uart, ATMEL_US_RTSEN); 2570fc887b15SLinus Torvalds } 2571fc887b15SLinus Torvalds 2572055560b0SElen Song /* 2573055560b0SElen Song * Get port name of usart or uart 2574055560b0SElen Song */ 2575892db58bSNicolas Ferre atmel_get_ip_name(&port->uart); 2576055560b0SElen Song 2577ab4382d2SGreg Kroah-Hartman return 0; 2578ab4382d2SGreg Kroah-Hartman 2579ab4382d2SGreg Kroah-Hartman err_add_port: 2580ab4382d2SGreg Kroah-Hartman kfree(port->rx_ring.buf); 2581ab4382d2SGreg Kroah-Hartman port->rx_ring.buf = NULL; 2582ab4382d2SGreg Kroah-Hartman err_alloc_ring: 2583ab4382d2SGreg Kroah-Hartman if (!atmel_is_console_port(&port->uart)) { 2584ab4382d2SGreg Kroah-Hartman clk_put(port->clk); 2585ab4382d2SGreg Kroah-Hartman port->clk = NULL; 2586ab4382d2SGreg Kroah-Hartman } 25874cbf9f48SNicolas Ferre err: 2588ab4382d2SGreg Kroah-Hartman return ret; 2589ab4382d2SGreg Kroah-Hartman } 2590ab4382d2SGreg Kroah-Hartman 2591ae8d8a14SBill Pemberton static int atmel_serial_remove(struct platform_device *pdev) 2592ab4382d2SGreg Kroah-Hartman { 2593ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2594ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2595ab4382d2SGreg Kroah-Hartman int ret = 0; 2596ab4382d2SGreg Kroah-Hartman 2597f50c995fSMarek Roszko tasklet_kill(&atmel_port->tasklet); 2598f50c995fSMarek Roszko 2599ab4382d2SGreg Kroah-Hartman device_init_wakeup(&pdev->dev, 0); 2600ab4382d2SGreg Kroah-Hartman 2601ab4382d2SGreg Kroah-Hartman ret = uart_remove_one_port(&atmel_uart, port); 2602ab4382d2SGreg Kroah-Hartman 2603ab4382d2SGreg Kroah-Hartman kfree(atmel_port->rx_ring.buf); 2604ab4382d2SGreg Kroah-Hartman 2605ab4382d2SGreg Kroah-Hartman /* "port" is allocated statically, so we shouldn't free it */ 2606ab4382d2SGreg Kroah-Hartman 2607503bded9SPawel Wieczorkiewicz clear_bit(port->line, atmel_ports_in_use); 26084cbf9f48SNicolas Ferre 2609ab4382d2SGreg Kroah-Hartman clk_put(atmel_port->clk); 2610ab4382d2SGreg Kroah-Hartman 2611ab4382d2SGreg Kroah-Hartman return ret; 2612ab4382d2SGreg Kroah-Hartman } 2613ab4382d2SGreg Kroah-Hartman 2614ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = { 2615ab4382d2SGreg Kroah-Hartman .probe = atmel_serial_probe, 26162d47b716SBill Pemberton .remove = atmel_serial_remove, 2617ab4382d2SGreg Kroah-Hartman .suspend = atmel_serial_suspend, 2618ab4382d2SGreg Kroah-Hartman .resume = atmel_serial_resume, 2619ab4382d2SGreg Kroah-Hartman .driver = { 2620ab4382d2SGreg Kroah-Hartman .name = "atmel_usart", 2621ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 26225fbe46b6SNicolas Ferre .of_match_table = of_match_ptr(atmel_serial_dt_ids), 2623ab4382d2SGreg Kroah-Hartman }, 2624ab4382d2SGreg Kroah-Hartman }; 2625ab4382d2SGreg Kroah-Hartman 2626ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void) 2627ab4382d2SGreg Kroah-Hartman { 2628ab4382d2SGreg Kroah-Hartman int ret; 2629ab4382d2SGreg Kroah-Hartman 2630ab4382d2SGreg Kroah-Hartman ret = uart_register_driver(&atmel_uart); 2631ab4382d2SGreg Kroah-Hartman if (ret) 2632ab4382d2SGreg Kroah-Hartman return ret; 2633ab4382d2SGreg Kroah-Hartman 2634ab4382d2SGreg Kroah-Hartman ret = platform_driver_register(&atmel_serial_driver); 2635ab4382d2SGreg Kroah-Hartman if (ret) 2636ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&atmel_uart); 2637ab4382d2SGreg Kroah-Hartman 2638ab4382d2SGreg Kroah-Hartman return ret; 2639ab4382d2SGreg Kroah-Hartman } 2640ab4382d2SGreg Kroah-Hartman 2641ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void) 2642ab4382d2SGreg Kroah-Hartman { 2643ab4382d2SGreg Kroah-Hartman platform_driver_unregister(&atmel_serial_driver); 2644ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&atmel_uart); 2645ab4382d2SGreg Kroah-Hartman } 2646ab4382d2SGreg Kroah-Hartman 2647ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init); 2648ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit); 2649ab4382d2SGreg Kroah-Hartman 2650ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson"); 2651ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver"); 2652ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 2653ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart"); 2654