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> 406b997babSVinod Koul #include <linux/dmaengine.h> 41ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h> 42ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h> 43ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h> 44bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h> 452e68c22fSElen Song #include <linux/timer.h> 46354e57f3SLinus Walleij #include <linux/gpio.h> 47e0b0baadSRichard Genoud #include <linux/gpio/consumer.h> 48e0b0baadSRichard Genoud #include <linux/err.h> 49ab5e4e41SRichard Genoud #include <linux/irq.h> 50ab4382d2SGreg Kroah-Hartman 51ab4382d2SGreg Kroah-Hartman #include <asm/io.h> 52ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h> 53ab4382d2SGreg Kroah-Hartman 54ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE 512 55ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */ 56ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */ 57ab4382d2SGreg Kroah-Hartman 58ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 59ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ 60ab4382d2SGreg Kroah-Hartman #endif 61ab4382d2SGreg Kroah-Hartman 62ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 63ab4382d2SGreg Kroah-Hartman 64e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h" 65e0b0baadSRichard Genoud 66ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port); 67ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port); 68ab4382d2SGreg Kroah-Hartman 69ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT 70ab4382d2SGreg Kroah-Hartman 71ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we 72ab4382d2SGreg Kroah-Hartman * should coexist with the 8250 driver, such as if we have an external 16C550 73ab4382d2SGreg Kroah-Hartman * UART. */ 74ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR 204 75ab4382d2SGreg Kroah-Hartman #define MINOR_START 154 76ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyAT" 77ab4382d2SGreg Kroah-Hartman 78ab4382d2SGreg Kroah-Hartman #else 79ab4382d2SGreg Kroah-Hartman 80ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port 81ab4382d2SGreg Kroah-Hartman * name, but it is legally reserved for the 8250 driver. */ 82ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR TTY_MAJOR 83ab4382d2SGreg Kroah-Hartman #define MINOR_START 64 84ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyS" 85ab4382d2SGreg Kroah-Hartman 86ab4382d2SGreg Kroah-Hartman #endif 87ab4382d2SGreg Kroah-Hartman 88ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT 256 89ab4382d2SGreg Kroah-Hartman 90ab4382d2SGreg Kroah-Hartman /* UART registers. CR is write-only, hence no GET macro */ 91ab4382d2SGreg Kroah-Hartman #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR) 92ab4382d2SGreg Kroah-Hartman #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR) 93ab4382d2SGreg Kroah-Hartman #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR) 94ab4382d2SGreg Kroah-Hartman #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER) 95ab4382d2SGreg Kroah-Hartman #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR) 96ab4382d2SGreg Kroah-Hartman #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR) 97ab4382d2SGreg Kroah-Hartman #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR) 98ab4382d2SGreg Kroah-Hartman #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR) 99ab4382d2SGreg Kroah-Hartman #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR) 100ab4382d2SGreg Kroah-Hartman #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR) 101ab4382d2SGreg Kroah-Hartman #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR) 102ab4382d2SGreg Kroah-Hartman #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR) 103ab4382d2SGreg Kroah-Hartman #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR) 104055560b0SElen Song #define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME) 105731d9caeSNicolas Ferre #define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION) 106ab4382d2SGreg Kroah-Hartman 107ab4382d2SGreg Kroah-Hartman /* PDC registers */ 108ab4382d2SGreg Kroah-Hartman #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR) 109ab4382d2SGreg Kroah-Hartman #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR) 110ab4382d2SGreg Kroah-Hartman 111ab4382d2SGreg Kroah-Hartman #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR) 112ab4382d2SGreg Kroah-Hartman #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR) 113ab4382d2SGreg Kroah-Hartman #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR) 114ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR) 115ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR) 116ab4382d2SGreg Kroah-Hartman 117ab4382d2SGreg Kroah-Hartman #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR) 118ab4382d2SGreg Kroah-Hartman #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR) 119ab4382d2SGreg Kroah-Hartman #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR) 120ab4382d2SGreg Kroah-Hartman 121ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer { 122ab4382d2SGreg Kroah-Hartman unsigned char *buf; 123ab4382d2SGreg Kroah-Hartman dma_addr_t dma_addr; 124ab4382d2SGreg Kroah-Hartman unsigned int dma_size; 125ab4382d2SGreg Kroah-Hartman unsigned int ofs; 126ab4382d2SGreg Kroah-Hartman }; 127ab4382d2SGreg Kroah-Hartman 128ab4382d2SGreg Kroah-Hartman struct atmel_uart_char { 129ab4382d2SGreg Kroah-Hartman u16 status; 130ab4382d2SGreg Kroah-Hartman u16 ch; 131ab4382d2SGreg Kroah-Hartman }; 132ab4382d2SGreg Kroah-Hartman 133ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024 134ab4382d2SGreg Kroah-Hartman 135ab4382d2SGreg Kroah-Hartman /* 136ab4382d2SGreg Kroah-Hartman * We wrap our port structure around the generic uart_port. 137ab4382d2SGreg Kroah-Hartman */ 138ab4382d2SGreg Kroah-Hartman struct atmel_uart_port { 139ab4382d2SGreg Kroah-Hartman struct uart_port uart; /* uart */ 140ab4382d2SGreg Kroah-Hartman struct clk *clk; /* uart clock */ 141ab4382d2SGreg Kroah-Hartman int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */ 142ab4382d2SGreg Kroah-Hartman u32 backup_imr; /* IMR saved during suspend */ 143ab4382d2SGreg Kroah-Hartman int break_active; /* break being received */ 144ab4382d2SGreg Kroah-Hartman 14534df42f5SElen Song bool use_dma_rx; /* enable DMA receiver */ 14664e22ebeSElen Song bool use_pdc_rx; /* enable PDC receiver */ 147ab4382d2SGreg Kroah-Hartman short pdc_rx_idx; /* current PDC RX buffer */ 148ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ 149ab4382d2SGreg Kroah-Hartman 15008f738beSElen Song bool use_dma_tx; /* enable DMA transmitter */ 15164e22ebeSElen Song bool use_pdc_tx; /* enable PDC transmitter */ 152ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ 153ab4382d2SGreg Kroah-Hartman 15408f738beSElen Song spinlock_t lock_tx; /* port lock */ 15534df42f5SElen Song spinlock_t lock_rx; /* port lock */ 15608f738beSElen Song struct dma_chan *chan_tx; 15734df42f5SElen Song struct dma_chan *chan_rx; 15808f738beSElen Song struct dma_async_tx_descriptor *desc_tx; 15934df42f5SElen Song struct dma_async_tx_descriptor *desc_rx; 16008f738beSElen Song dma_cookie_t cookie_tx; 16134df42f5SElen Song dma_cookie_t cookie_rx; 16208f738beSElen Song struct scatterlist sg_tx; 16334df42f5SElen Song struct scatterlist sg_rx; 164ab4382d2SGreg Kroah-Hartman struct tasklet_struct tasklet; 165ab4382d2SGreg Kroah-Hartman unsigned int irq_status; 166ab4382d2SGreg Kroah-Hartman unsigned int irq_status_prev; 167ab4382d2SGreg Kroah-Hartman 168ab4382d2SGreg Kroah-Hartman struct circ_buf rx_ring; 169ab4382d2SGreg Kroah-Hartman 170e0b0baadSRichard Genoud struct mctrl_gpios *gpios; 171ab5e4e41SRichard Genoud int gpio_irq[UART_GPIO_MAX]; 172ab4382d2SGreg Kroah-Hartman unsigned int tx_done_mask; 173ab5e4e41SRichard 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 */ 29213bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port, 29313bd3e6fSRicardo Ribalda Delgado struct serial_rs485 *rs485conf) 294ab4382d2SGreg Kroah-Hartman { 295ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 296ab4382d2SGreg Kroah-Hartman unsigned int mode; 297ab4382d2SGreg Kroah-Hartman 298ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 299ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, atmel_port->tx_done_mask); 300ab4382d2SGreg Kroah-Hartman 301ab4382d2SGreg Kroah-Hartman mode = UART_GET_MR(port); 302ab4382d2SGreg Kroah-Hartman 303ab4382d2SGreg Kroah-Hartman /* Resetting serial mode to RS232 (0x0) */ 304ab4382d2SGreg Kroah-Hartman mode &= ~ATMEL_US_USMODE; 305ab4382d2SGreg Kroah-Hartman 30613bd3e6fSRicardo Ribalda Delgado port->rs485 = *rs485conf; 307ab4382d2SGreg Kroah-Hartman 308ab4382d2SGreg Kroah-Hartman if (rs485conf->flags & SER_RS485_ENABLED) { 309ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS485\n"); 310ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 31193f3350cSClaudio Scordino if ((rs485conf->delay_rts_after_send) > 0) 312ab4382d2SGreg Kroah-Hartman UART_PUT_TTGR(port, rs485conf->delay_rts_after_send); 313ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 314ab4382d2SGreg Kroah-Hartman } else { 315ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS232\n"); 31664e22ebeSElen Song if (atmel_use_pdc_tx(port)) 317ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | 318ab4382d2SGreg Kroah-Hartman ATMEL_US_TXBUFE; 319ab4382d2SGreg Kroah-Hartman else 320ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 321ab4382d2SGreg Kroah-Hartman } 322ab4382d2SGreg Kroah-Hartman UART_PUT_MR(port, mode); 323ab4382d2SGreg Kroah-Hartman 324ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 325ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 326ab4382d2SGreg Kroah-Hartman 32713bd3e6fSRicardo Ribalda Delgado return 0; 328ab4382d2SGreg Kroah-Hartman } 329ab4382d2SGreg Kroah-Hartman 330ab4382d2SGreg Kroah-Hartman /* 331ab4382d2SGreg Kroah-Hartman * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. 332ab4382d2SGreg Kroah-Hartman */ 333ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port) 334ab4382d2SGreg Kroah-Hartman { 335ab4382d2SGreg Kroah-Hartman return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0; 336ab4382d2SGreg Kroah-Hartman } 337ab4382d2SGreg Kroah-Hartman 338ab4382d2SGreg Kroah-Hartman /* 339ab4382d2SGreg Kroah-Hartman * Set state of the modem control output lines 340ab4382d2SGreg Kroah-Hartman */ 341ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) 342ab4382d2SGreg Kroah-Hartman { 343ab4382d2SGreg Kroah-Hartman unsigned int control = 0; 3441cf6e8fcSCyrille Pitchen unsigned int mode = UART_GET_MR(port); 3451cf6e8fcSCyrille Pitchen unsigned int rts_paused, rts_ready; 346ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 347ab4382d2SGreg Kroah-Hartman 3481cf6e8fcSCyrille Pitchen /* override mode to RS485 if needed, otherwise keep the current mode */ 3491cf6e8fcSCyrille Pitchen if (port->rs485.flags & SER_RS485_ENABLED) { 3501cf6e8fcSCyrille Pitchen if ((port->rs485.delay_rts_after_send) > 0) 3511cf6e8fcSCyrille Pitchen UART_PUT_TTGR(port, port->rs485.delay_rts_after_send); 3521cf6e8fcSCyrille Pitchen mode &= ~ATMEL_US_USMODE; 3531cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_RS485; 3541cf6e8fcSCyrille Pitchen } 3551cf6e8fcSCyrille Pitchen 3561cf6e8fcSCyrille Pitchen /* set the RTS line state according to the mode */ 3571cf6e8fcSCyrille Pitchen if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 3581cf6e8fcSCyrille Pitchen /* force RTS line to high level */ 3591cf6e8fcSCyrille Pitchen rts_paused = ATMEL_US_RTSEN; 3601cf6e8fcSCyrille Pitchen 3611cf6e8fcSCyrille Pitchen /* give the control of the RTS line back to the hardware */ 3621cf6e8fcSCyrille Pitchen rts_ready = ATMEL_US_RTSDIS; 3631cf6e8fcSCyrille Pitchen } else { 3641cf6e8fcSCyrille Pitchen /* force RTS line to high level */ 3651cf6e8fcSCyrille Pitchen rts_paused = ATMEL_US_RTSDIS; 3661cf6e8fcSCyrille Pitchen 3671cf6e8fcSCyrille Pitchen /* force RTS line to low level */ 3681cf6e8fcSCyrille Pitchen rts_ready = ATMEL_US_RTSEN; 3691cf6e8fcSCyrille Pitchen } 3701cf6e8fcSCyrille Pitchen 371ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_RTS) 3721cf6e8fcSCyrille Pitchen control |= rts_ready; 373ab4382d2SGreg Kroah-Hartman else 3741cf6e8fcSCyrille Pitchen control |= rts_paused; 375ab4382d2SGreg Kroah-Hartman 376ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_DTR) 377ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTREN; 378ab4382d2SGreg Kroah-Hartman else 379ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTRDIS; 380ab4382d2SGreg Kroah-Hartman 381ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, control); 382ab4382d2SGreg Kroah-Hartman 383e0b0baadSRichard Genoud mctrl_gpio_set(atmel_port->gpios, mctrl); 384e0b0baadSRichard Genoud 385ab4382d2SGreg Kroah-Hartman /* Local loopback mode? */ 3861cf6e8fcSCyrille Pitchen mode &= ~ATMEL_US_CHMODE; 387ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_LOOP) 388ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_LOC_LOOP; 389ab4382d2SGreg Kroah-Hartman else 390ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_NORMAL; 391ab4382d2SGreg Kroah-Hartman 392ab4382d2SGreg Kroah-Hartman UART_PUT_MR(port, mode); 393ab4382d2SGreg Kroah-Hartman } 394ab4382d2SGreg Kroah-Hartman 395ab4382d2SGreg Kroah-Hartman /* 396ab4382d2SGreg Kroah-Hartman * Get state of the modem control input lines 397ab4382d2SGreg Kroah-Hartman */ 398ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port) 399ab4382d2SGreg Kroah-Hartman { 400e0b0baadSRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 401e0b0baadSRichard Genoud unsigned int ret = 0, status; 402ab4382d2SGreg Kroah-Hartman 403ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 404ab4382d2SGreg Kroah-Hartman 405ab4382d2SGreg Kroah-Hartman /* 406ab4382d2SGreg Kroah-Hartman * The control signals are active low. 407ab4382d2SGreg Kroah-Hartman */ 408ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DCD)) 409ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CD; 410ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_CTS)) 411ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CTS; 412ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DSR)) 413ab4382d2SGreg Kroah-Hartman ret |= TIOCM_DSR; 414ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_RI)) 415ab4382d2SGreg Kroah-Hartman ret |= TIOCM_RI; 416ab4382d2SGreg Kroah-Hartman 417e0b0baadSRichard Genoud return mctrl_gpio_get(atmel_port->gpios, &ret); 418ab4382d2SGreg Kroah-Hartman } 419ab4382d2SGreg Kroah-Hartman 420ab4382d2SGreg Kroah-Hartman /* 421ab4382d2SGreg Kroah-Hartman * Stop transmitting. 422ab4382d2SGreg Kroah-Hartman */ 423ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port) 424ab4382d2SGreg Kroah-Hartman { 425ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 426ab4382d2SGreg Kroah-Hartman 42764e22ebeSElen Song if (atmel_use_pdc_tx(port)) { 428ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 429ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 430ab4382d2SGreg Kroah-Hartman } 431ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 432ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, atmel_port->tx_done_mask); 433ab4382d2SGreg Kroah-Hartman 43413bd3e6fSRicardo Ribalda Delgado if ((port->rs485.flags & SER_RS485_ENABLED) && 43513bd3e6fSRicardo Ribalda Delgado !(port->rs485.flags & SER_RS485_RX_DURING_TX)) 436ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 437ab4382d2SGreg Kroah-Hartman } 438ab4382d2SGreg Kroah-Hartman 439ab4382d2SGreg Kroah-Hartman /* 440ab4382d2SGreg Kroah-Hartman * Start transmitting. 441ab4382d2SGreg Kroah-Hartman */ 442ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port) 443ab4382d2SGreg Kroah-Hartman { 444ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 445ab4382d2SGreg Kroah-Hartman 44664e22ebeSElen Song if (atmel_use_pdc_tx(port)) { 447ab4382d2SGreg Kroah-Hartman if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN) 448ab4382d2SGreg Kroah-Hartman /* The transmitter is already running. Yes, we 449ab4382d2SGreg Kroah-Hartman really need this.*/ 450ab4382d2SGreg Kroah-Hartman return; 451ab4382d2SGreg Kroah-Hartman 45213bd3e6fSRicardo Ribalda Delgado if ((port->rs485.flags & SER_RS485_ENABLED) && 45313bd3e6fSRicardo Ribalda Delgado !(port->rs485.flags & SER_RS485_RX_DURING_TX)) 454ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 455ab4382d2SGreg Kroah-Hartman 456ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 457ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); 458ab4382d2SGreg Kroah-Hartman } 459ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 460ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 461ab4382d2SGreg Kroah-Hartman } 462ab4382d2SGreg Kroah-Hartman 463ab4382d2SGreg Kroah-Hartman /* 464ab4382d2SGreg Kroah-Hartman * start receiving - port is in process of being opened. 465ab4382d2SGreg Kroah-Hartman */ 466ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port) 467ab4382d2SGreg Kroah-Hartman { 468ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */ 469ab4382d2SGreg Kroah-Hartman 47057c36868SSiftar, Gabe UART_PUT_CR(port, ATMEL_US_RXEN); 47157c36868SSiftar, Gabe 47264e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 473ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 474ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 475ab4382d2SGreg Kroah-Hartman port->read_status_mask); 476ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); 477ab4382d2SGreg Kroah-Hartman } else { 478ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_RXRDY); 479ab4382d2SGreg Kroah-Hartman } 480ab4382d2SGreg Kroah-Hartman } 481ab4382d2SGreg Kroah-Hartman 482ab4382d2SGreg Kroah-Hartman /* 483ab4382d2SGreg Kroah-Hartman * Stop receiving - port is in process of being closed. 484ab4382d2SGreg Kroah-Hartman */ 485ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port) 486ab4382d2SGreg Kroah-Hartman { 48757c36868SSiftar, Gabe UART_PUT_CR(port, ATMEL_US_RXDIS); 48857c36868SSiftar, Gabe 48964e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 490ab4382d2SGreg Kroah-Hartman /* disable PDC receive */ 491ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS); 492ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 493ab4382d2SGreg Kroah-Hartman port->read_status_mask); 494ab4382d2SGreg Kroah-Hartman } else { 495ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXRDY); 496ab4382d2SGreg Kroah-Hartman } 497ab4382d2SGreg Kroah-Hartman } 498ab4382d2SGreg Kroah-Hartman 499ab4382d2SGreg Kroah-Hartman /* 500ab4382d2SGreg Kroah-Hartman * Enable modem status interrupts 501ab4382d2SGreg Kroah-Hartman */ 502ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port) 503ab4382d2SGreg Kroah-Hartman { 504ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 505ab5e4e41SRichard Genoud uint32_t ier = 0; 506ab5e4e41SRichard Genoud 507ab5e4e41SRichard Genoud /* 508ab5e4e41SRichard Genoud * Interrupt should not be enabled twice 509ab5e4e41SRichard Genoud */ 510ab5e4e41SRichard Genoud if (atmel_port->ms_irq_enabled) 511ab5e4e41SRichard Genoud return; 512ab5e4e41SRichard Genoud 513ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = true; 514ab5e4e41SRichard Genoud 515ab5e4e41SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0) 516ab5e4e41SRichard Genoud enable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]); 517ab5e4e41SRichard Genoud else 518ab5e4e41SRichard Genoud ier |= ATMEL_US_CTSIC; 519ab5e4e41SRichard Genoud 520ab5e4e41SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0) 521ab5e4e41SRichard Genoud enable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]); 522ab5e4e41SRichard Genoud else 523ab5e4e41SRichard Genoud ier |= ATMEL_US_DSRIC; 524ab5e4e41SRichard Genoud 525ab5e4e41SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0) 526ab5e4e41SRichard Genoud enable_irq(atmel_port->gpio_irq[UART_GPIO_RI]); 527ab5e4e41SRichard Genoud else 528ab5e4e41SRichard Genoud ier |= ATMEL_US_RIIC; 529ab5e4e41SRichard Genoud 530ab5e4e41SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0) 531ab5e4e41SRichard Genoud enable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]); 532ab5e4e41SRichard Genoud else 533ab5e4e41SRichard Genoud ier |= ATMEL_US_DCDIC; 534ab5e4e41SRichard Genoud 535ab5e4e41SRichard Genoud UART_PUT_IER(port, ier); 536ab4382d2SGreg Kroah-Hartman } 537ab4382d2SGreg Kroah-Hartman 538ab4382d2SGreg Kroah-Hartman /* 53935b675b9SRichard Genoud * Disable modem status interrupts 54035b675b9SRichard Genoud */ 54135b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port) 54235b675b9SRichard Genoud { 54335b675b9SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 54435b675b9SRichard Genoud uint32_t idr = 0; 54535b675b9SRichard Genoud 54635b675b9SRichard Genoud /* 54735b675b9SRichard Genoud * Interrupt should not be disabled twice 54835b675b9SRichard Genoud */ 54935b675b9SRichard Genoud if (!atmel_port->ms_irq_enabled) 55035b675b9SRichard Genoud return; 55135b675b9SRichard Genoud 55235b675b9SRichard Genoud atmel_port->ms_irq_enabled = false; 55335b675b9SRichard Genoud 55435b675b9SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0) 55535b675b9SRichard Genoud disable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]); 55635b675b9SRichard Genoud else 55735b675b9SRichard Genoud idr |= ATMEL_US_CTSIC; 55835b675b9SRichard Genoud 55935b675b9SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0) 56035b675b9SRichard Genoud disable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]); 56135b675b9SRichard Genoud else 56235b675b9SRichard Genoud idr |= ATMEL_US_DSRIC; 56335b675b9SRichard Genoud 56435b675b9SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0) 56535b675b9SRichard Genoud disable_irq(atmel_port->gpio_irq[UART_GPIO_RI]); 56635b675b9SRichard Genoud else 56735b675b9SRichard Genoud idr |= ATMEL_US_RIIC; 56835b675b9SRichard Genoud 56935b675b9SRichard Genoud if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0) 57035b675b9SRichard Genoud disable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]); 57135b675b9SRichard Genoud else 57235b675b9SRichard Genoud idr |= ATMEL_US_DCDIC; 57335b675b9SRichard Genoud 57435b675b9SRichard Genoud UART_PUT_IDR(port, idr); 57535b675b9SRichard Genoud } 57635b675b9SRichard Genoud 57735b675b9SRichard Genoud /* 578ab4382d2SGreg Kroah-Hartman * Control the transmission of a break signal 579ab4382d2SGreg Kroah-Hartman */ 580ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state) 581ab4382d2SGreg Kroah-Hartman { 582ab4382d2SGreg Kroah-Hartman if (break_state != 0) 583ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */ 584ab4382d2SGreg Kroah-Hartman else 585ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */ 586ab4382d2SGreg Kroah-Hartman } 587ab4382d2SGreg Kroah-Hartman 588ab4382d2SGreg Kroah-Hartman /* 589ab4382d2SGreg Kroah-Hartman * Stores the incoming character in the ring buffer 590ab4382d2SGreg Kroah-Hartman */ 591ab4382d2SGreg Kroah-Hartman static void 592ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status, 593ab4382d2SGreg Kroah-Hartman unsigned int ch) 594ab4382d2SGreg Kroah-Hartman { 595ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 596ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 597ab4382d2SGreg Kroah-Hartman struct atmel_uart_char *c; 598ab4382d2SGreg Kroah-Hartman 599ab4382d2SGreg Kroah-Hartman if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) 600ab4382d2SGreg Kroah-Hartman /* Buffer overflow, ignore char */ 601ab4382d2SGreg Kroah-Hartman return; 602ab4382d2SGreg Kroah-Hartman 603ab4382d2SGreg Kroah-Hartman c = &((struct atmel_uart_char *)ring->buf)[ring->head]; 604ab4382d2SGreg Kroah-Hartman c->status = status; 605ab4382d2SGreg Kroah-Hartman c->ch = ch; 606ab4382d2SGreg Kroah-Hartman 607ab4382d2SGreg Kroah-Hartman /* Make sure the character is stored before we update head. */ 608ab4382d2SGreg Kroah-Hartman smp_wmb(); 609ab4382d2SGreg Kroah-Hartman 610ab4382d2SGreg Kroah-Hartman ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 611ab4382d2SGreg Kroah-Hartman } 612ab4382d2SGreg Kroah-Hartman 613ab4382d2SGreg Kroah-Hartman /* 614ab4382d2SGreg Kroah-Hartman * Deal with parity, framing and overrun errors. 615ab4382d2SGreg Kroah-Hartman */ 616ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status) 617ab4382d2SGreg Kroah-Hartman { 618ab4382d2SGreg Kroah-Hartman /* clear error */ 619ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); 620ab4382d2SGreg Kroah-Hartman 621ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 622ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 623ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 624ab4382d2SGreg Kroah-Hartman port->icount.brk++; 625ab4382d2SGreg Kroah-Hartman } 626ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 627ab4382d2SGreg Kroah-Hartman port->icount.parity++; 628ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 629ab4382d2SGreg Kroah-Hartman port->icount.frame++; 630ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 631ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 632ab4382d2SGreg Kroah-Hartman } 633ab4382d2SGreg Kroah-Hartman 634ab4382d2SGreg Kroah-Hartman /* 635ab4382d2SGreg Kroah-Hartman * Characters received (called from interrupt handler) 636ab4382d2SGreg Kroah-Hartman */ 637ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port) 638ab4382d2SGreg Kroah-Hartman { 639ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 640ab4382d2SGreg Kroah-Hartman unsigned int status, ch; 641ab4382d2SGreg Kroah-Hartman 642ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 643ab4382d2SGreg Kroah-Hartman while (status & ATMEL_US_RXRDY) { 644ab4382d2SGreg Kroah-Hartman ch = UART_GET_CHAR(port); 645ab4382d2SGreg Kroah-Hartman 646ab4382d2SGreg Kroah-Hartman /* 647ab4382d2SGreg Kroah-Hartman * note that the error handling code is 648ab4382d2SGreg Kroah-Hartman * out of the main execution path 649ab4382d2SGreg Kroah-Hartman */ 650ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 651ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK) 652ab4382d2SGreg Kroah-Hartman || atmel_port->break_active)) { 653ab4382d2SGreg Kroah-Hartman 654ab4382d2SGreg Kroah-Hartman /* clear error */ 655ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); 656ab4382d2SGreg Kroah-Hartman 657ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK 658ab4382d2SGreg Kroah-Hartman && !atmel_port->break_active) { 659ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 1; 660ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_RXBRK); 661ab4382d2SGreg Kroah-Hartman } else { 662ab4382d2SGreg Kroah-Hartman /* 663ab4382d2SGreg Kroah-Hartman * This is either the end-of-break 664ab4382d2SGreg Kroah-Hartman * condition or we've received at 665ab4382d2SGreg Kroah-Hartman * least one character without RXBRK 666ab4382d2SGreg Kroah-Hartman * being set. In both cases, the next 667ab4382d2SGreg Kroah-Hartman * RXBRK will indicate start-of-break. 668ab4382d2SGreg Kroah-Hartman */ 669ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXBRK); 670ab4382d2SGreg Kroah-Hartman status &= ~ATMEL_US_RXBRK; 671ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 672ab4382d2SGreg Kroah-Hartman } 673ab4382d2SGreg Kroah-Hartman } 674ab4382d2SGreg Kroah-Hartman 675ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(port, status, ch); 676ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 677ab4382d2SGreg Kroah-Hartman } 678ab4382d2SGreg Kroah-Hartman 679ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 680ab4382d2SGreg Kroah-Hartman } 681ab4382d2SGreg Kroah-Hartman 682ab4382d2SGreg Kroah-Hartman /* 683ab4382d2SGreg Kroah-Hartman * Transmit characters (called from tasklet with TXRDY interrupt 684ab4382d2SGreg Kroah-Hartman * disabled) 685ab4382d2SGreg Kroah-Hartman */ 686ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port) 687ab4382d2SGreg Kroah-Hartman { 688ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 689ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 690ab4382d2SGreg Kroah-Hartman 691ab4382d2SGreg Kroah-Hartman if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) { 692ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, port->x_char); 693ab4382d2SGreg Kroah-Hartman port->icount.tx++; 694ab4382d2SGreg Kroah-Hartman port->x_char = 0; 695ab4382d2SGreg Kroah-Hartman } 696ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 697ab4382d2SGreg Kroah-Hartman return; 698ab4382d2SGreg Kroah-Hartman 699ab4382d2SGreg Kroah-Hartman while (UART_GET_CSR(port) & atmel_port->tx_done_mask) { 700ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, xmit->buf[xmit->tail]); 701ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 702ab4382d2SGreg Kroah-Hartman port->icount.tx++; 703ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit)) 704ab4382d2SGreg Kroah-Hartman break; 705ab4382d2SGreg Kroah-Hartman } 706ab4382d2SGreg Kroah-Hartman 707ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 708ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 709ab4382d2SGreg Kroah-Hartman 710ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit)) 711ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 712ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 713ab4382d2SGreg Kroah-Hartman } 714ab4382d2SGreg Kroah-Hartman 71508f738beSElen Song static void atmel_complete_tx_dma(void *arg) 71608f738beSElen Song { 71708f738beSElen Song struct atmel_uart_port *atmel_port = arg; 71808f738beSElen Song struct uart_port *port = &atmel_port->uart; 71908f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 72008f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 72108f738beSElen Song unsigned long flags; 72208f738beSElen Song 72308f738beSElen Song spin_lock_irqsave(&port->lock, flags); 72408f738beSElen Song 72508f738beSElen Song if (chan) 72608f738beSElen Song dmaengine_terminate_all(chan); 72708f738beSElen Song xmit->tail += sg_dma_len(&atmel_port->sg_tx); 72808f738beSElen Song xmit->tail &= UART_XMIT_SIZE - 1; 72908f738beSElen Song 73008f738beSElen Song port->icount.tx += sg_dma_len(&atmel_port->sg_tx); 73108f738beSElen Song 73208f738beSElen Song spin_lock_irq(&atmel_port->lock_tx); 73308f738beSElen Song async_tx_ack(atmel_port->desc_tx); 73408f738beSElen Song atmel_port->cookie_tx = -EINVAL; 73508f738beSElen Song atmel_port->desc_tx = NULL; 73608f738beSElen Song spin_unlock_irq(&atmel_port->lock_tx); 73708f738beSElen Song 73808f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 73908f738beSElen Song uart_write_wakeup(port); 74008f738beSElen Song 7411842dc2eSCyrille Pitchen /* 7421842dc2eSCyrille Pitchen * xmit is a circular buffer so, if we have just send data from 7431842dc2eSCyrille Pitchen * xmit->tail to the end of xmit->buf, now we have to transmit the 7441842dc2eSCyrille Pitchen * remaining data from the beginning of xmit->buf to xmit->head. 7451842dc2eSCyrille Pitchen */ 74608f738beSElen Song if (!uart_circ_empty(xmit)) 74708f738beSElen Song tasklet_schedule(&atmel_port->tasklet); 74808f738beSElen Song 74908f738beSElen Song spin_unlock_irqrestore(&port->lock, flags); 75008f738beSElen Song } 75108f738beSElen Song 75208f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port) 75308f738beSElen Song { 75408f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 75508f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 75608f738beSElen Song 75708f738beSElen Song if (chan) { 75808f738beSElen Song dmaengine_terminate_all(chan); 75908f738beSElen Song dma_release_channel(chan); 76008f738beSElen Song dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1, 76148479148SWolfram Sang DMA_TO_DEVICE); 76208f738beSElen Song } 76308f738beSElen Song 76408f738beSElen Song atmel_port->desc_tx = NULL; 76508f738beSElen Song atmel_port->chan_tx = NULL; 76608f738beSElen Song atmel_port->cookie_tx = -EINVAL; 76708f738beSElen Song } 76808f738beSElen Song 76908f738beSElen Song /* 77008f738beSElen Song * Called from tasklet with TXRDY interrupt is disabled. 77108f738beSElen Song */ 77208f738beSElen Song static void atmel_tx_dma(struct uart_port *port) 77308f738beSElen Song { 77408f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 77508f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 77608f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 77708f738beSElen Song struct dma_async_tx_descriptor *desc; 77808f738beSElen Song struct scatterlist *sg = &atmel_port->sg_tx; 77908f738beSElen Song 78008f738beSElen Song /* Make sure we have an idle channel */ 78108f738beSElen Song if (atmel_port->desc_tx != NULL) 78208f738beSElen Song return; 78308f738beSElen Song 78408f738beSElen Song if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 78508f738beSElen Song /* 78608f738beSElen Song * DMA is idle now. 78708f738beSElen Song * Port xmit buffer is already mapped, 78808f738beSElen Song * and it is one page... Just adjust 78908f738beSElen Song * offsets and lengths. Since it is a circular buffer, 79008f738beSElen Song * we have to transmit till the end, and then the rest. 79108f738beSElen Song * Take the port lock to get a 79208f738beSElen Song * consistent xmit buffer state. 79308f738beSElen Song */ 79408f738beSElen Song sg->offset = xmit->tail & (UART_XMIT_SIZE - 1); 79508f738beSElen Song sg_dma_address(sg) = (sg_dma_address(sg) & 79608f738beSElen Song ~(UART_XMIT_SIZE - 1)) 79708f738beSElen Song + sg->offset; 79808f738beSElen Song sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head, 79908f738beSElen Song xmit->tail, 80008f738beSElen Song UART_XMIT_SIZE); 80108f738beSElen Song BUG_ON(!sg_dma_len(sg)); 80208f738beSElen Song 80308f738beSElen Song desc = dmaengine_prep_slave_sg(chan, 80408f738beSElen Song sg, 80508f738beSElen Song 1, 80608f738beSElen Song DMA_MEM_TO_DEV, 80708f738beSElen Song DMA_PREP_INTERRUPT | 80808f738beSElen Song DMA_CTRL_ACK); 80908f738beSElen Song if (!desc) { 81008f738beSElen Song dev_err(port->dev, "Failed to send via dma!\n"); 81108f738beSElen Song return; 81208f738beSElen Song } 81308f738beSElen Song 814485819b5SCyrille Pitchen dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE); 81508f738beSElen Song 81608f738beSElen Song atmel_port->desc_tx = desc; 81708f738beSElen Song desc->callback = atmel_complete_tx_dma; 81808f738beSElen Song desc->callback_param = atmel_port; 81908f738beSElen Song atmel_port->cookie_tx = dmaengine_submit(desc); 82008f738beSElen Song 82108f738beSElen Song } else { 82213bd3e6fSRicardo Ribalda Delgado if (port->rs485.flags & SER_RS485_ENABLED) { 82308f738beSElen Song /* DMA done, stop TX, start RX for RS485 */ 82408f738beSElen Song atmel_start_rx(port); 82508f738beSElen Song } 82608f738beSElen Song } 82708f738beSElen Song 82808f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 82908f738beSElen Song uart_write_wakeup(port); 83008f738beSElen Song } 83108f738beSElen Song 83208f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port) 83308f738beSElen Song { 83408f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 83508f738beSElen Song dma_cap_mask_t mask; 83608f738beSElen Song struct dma_slave_config config; 83708f738beSElen Song int ret, nent; 83808f738beSElen Song 83908f738beSElen Song dma_cap_zero(mask); 84008f738beSElen Song dma_cap_set(DMA_SLAVE, mask); 84108f738beSElen Song 84208f738beSElen Song atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx"); 84308f738beSElen Song if (atmel_port->chan_tx == NULL) 84408f738beSElen Song goto chan_err; 84508f738beSElen Song dev_info(port->dev, "using %s for tx DMA transfers\n", 84608f738beSElen Song dma_chan_name(atmel_port->chan_tx)); 84708f738beSElen Song 84808f738beSElen Song spin_lock_init(&atmel_port->lock_tx); 84908f738beSElen Song sg_init_table(&atmel_port->sg_tx, 1); 85008f738beSElen Song /* UART circular tx buffer is an aligned page. */ 8512c277054SLeilei Zhao BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf)); 85208f738beSElen Song sg_set_page(&atmel_port->sg_tx, 85308f738beSElen Song virt_to_page(port->state->xmit.buf), 85408f738beSElen Song UART_XMIT_SIZE, 85508f738beSElen Song (int)port->state->xmit.buf & ~PAGE_MASK); 85608f738beSElen Song nent = dma_map_sg(port->dev, 85708f738beSElen Song &atmel_port->sg_tx, 85808f738beSElen Song 1, 85948479148SWolfram Sang DMA_TO_DEVICE); 86008f738beSElen Song 86108f738beSElen Song if (!nent) { 86208f738beSElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 86308f738beSElen Song goto chan_err; 86408f738beSElen Song } else { 86508f738beSElen Song dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__, 86608f738beSElen Song sg_dma_len(&atmel_port->sg_tx), 86708f738beSElen Song port->state->xmit.buf, 86808f738beSElen Song sg_dma_address(&atmel_port->sg_tx)); 86908f738beSElen Song } 87008f738beSElen Song 87108f738beSElen Song /* Configure the slave DMA */ 87208f738beSElen Song memset(&config, 0, sizeof(config)); 87308f738beSElen Song config.direction = DMA_MEM_TO_DEV; 87408f738beSElen Song config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 87508f738beSElen Song config.dst_addr = port->mapbase + ATMEL_US_THR; 87608f738beSElen Song 8775483c10eSMaxime Ripard ret = dmaengine_slave_config(atmel_port->chan_tx, 8785483c10eSMaxime Ripard &config); 87908f738beSElen Song if (ret) { 88008f738beSElen Song dev_err(port->dev, "DMA tx slave configuration failed\n"); 88108f738beSElen Song goto chan_err; 88208f738beSElen Song } 88308f738beSElen Song 88408f738beSElen Song return 0; 88508f738beSElen Song 88608f738beSElen Song chan_err: 88708f738beSElen Song dev_err(port->dev, "TX channel not available, switch to pio\n"); 88808f738beSElen Song atmel_port->use_dma_tx = 0; 88908f738beSElen Song if (atmel_port->chan_tx) 89008f738beSElen Song atmel_release_tx_dma(port); 89108f738beSElen Song return -EINVAL; 89208f738beSElen Song } 89308f738beSElen Song 89434df42f5SElen Song static void atmel_complete_rx_dma(void *arg) 89534df42f5SElen Song { 89634df42f5SElen Song struct uart_port *port = arg; 89734df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 89834df42f5SElen Song 89934df42f5SElen Song tasklet_schedule(&atmel_port->tasklet); 90034df42f5SElen Song } 90134df42f5SElen Song 90234df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port) 90334df42f5SElen Song { 90434df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 90534df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 90634df42f5SElen Song 90734df42f5SElen Song if (chan) { 90834df42f5SElen Song dmaengine_terminate_all(chan); 90934df42f5SElen Song dma_release_channel(chan); 91034df42f5SElen Song dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1, 91148479148SWolfram Sang DMA_FROM_DEVICE); 91234df42f5SElen Song } 91334df42f5SElen Song 91434df42f5SElen Song atmel_port->desc_rx = NULL; 91534df42f5SElen Song atmel_port->chan_rx = NULL; 91634df42f5SElen Song atmel_port->cookie_rx = -EINVAL; 91734df42f5SElen Song } 91834df42f5SElen Song 91934df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port) 92034df42f5SElen Song { 92134df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 92266f37aafSCyrille Pitchen struct tty_port *tport = &port->state->port; 92334df42f5SElen Song struct circ_buf *ring = &atmel_port->rx_ring; 92434df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 92534df42f5SElen Song struct dma_tx_state state; 92634df42f5SElen Song enum dma_status dmastat; 92766f37aafSCyrille Pitchen size_t count; 92834df42f5SElen Song 92934df42f5SElen Song 93034df42f5SElen Song /* Reset the UART timeout early so that we don't miss one */ 93134df42f5SElen Song UART_PUT_CR(port, ATMEL_US_STTTO); 93234df42f5SElen Song dmastat = dmaengine_tx_status(chan, 93334df42f5SElen Song atmel_port->cookie_rx, 93434df42f5SElen Song &state); 93534df42f5SElen Song /* Restart a new tasklet if DMA status is error */ 93634df42f5SElen Song if (dmastat == DMA_ERROR) { 93734df42f5SElen Song dev_dbg(port->dev, "Get residue error, restart tasklet\n"); 93834df42f5SElen Song UART_PUT_IER(port, ATMEL_US_TIMEOUT); 93934df42f5SElen Song tasklet_schedule(&atmel_port->tasklet); 94034df42f5SElen Song return; 94134df42f5SElen Song } 94266f37aafSCyrille Pitchen 94366f37aafSCyrille Pitchen /* CPU claims ownership of RX DMA buffer */ 94466f37aafSCyrille Pitchen dma_sync_sg_for_cpu(port->dev, 94566f37aafSCyrille Pitchen &atmel_port->sg_rx, 94666f37aafSCyrille Pitchen 1, 947485819b5SCyrille Pitchen DMA_FROM_DEVICE); 94834df42f5SElen Song 94934df42f5SElen Song /* 95066f37aafSCyrille Pitchen * ring->head points to the end of data already written by the DMA. 95166f37aafSCyrille Pitchen * ring->tail points to the beginning of data to be read by the 95266f37aafSCyrille Pitchen * framework. 95366f37aafSCyrille Pitchen * The current transfer size should not be larger than the dma buffer 95466f37aafSCyrille Pitchen * length. 95534df42f5SElen Song */ 95666f37aafSCyrille Pitchen ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue; 95766f37aafSCyrille Pitchen BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx)); 95866f37aafSCyrille Pitchen /* 95966f37aafSCyrille Pitchen * At this point ring->head may point to the first byte right after the 96066f37aafSCyrille Pitchen * last byte of the dma buffer: 96166f37aafSCyrille Pitchen * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx) 96266f37aafSCyrille Pitchen * 96366f37aafSCyrille Pitchen * However ring->tail must always points inside the dma buffer: 96466f37aafSCyrille Pitchen * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1 96566f37aafSCyrille Pitchen * 96666f37aafSCyrille Pitchen * Since we use a ring buffer, we have to handle the case 96766f37aafSCyrille Pitchen * where head is lower than tail. In such a case, we first read from 96866f37aafSCyrille Pitchen * tail to the end of the buffer then reset tail. 96966f37aafSCyrille Pitchen */ 97066f37aafSCyrille Pitchen if (ring->head < ring->tail) { 97166f37aafSCyrille Pitchen count = sg_dma_len(&atmel_port->sg_rx) - ring->tail; 97234df42f5SElen Song 97366f37aafSCyrille Pitchen tty_insert_flip_string(tport, ring->buf + ring->tail, count); 97466f37aafSCyrille Pitchen ring->tail = 0; 97534df42f5SElen Song port->icount.rx += count; 97634df42f5SElen Song } 97734df42f5SElen Song 97866f37aafSCyrille Pitchen /* Finally we read data from tail to head */ 97966f37aafSCyrille Pitchen if (ring->tail < ring->head) { 98066f37aafSCyrille Pitchen count = ring->head - ring->tail; 98166f37aafSCyrille Pitchen 98266f37aafSCyrille Pitchen tty_insert_flip_string(tport, ring->buf + ring->tail, count); 98366f37aafSCyrille Pitchen /* Wrap ring->head if needed */ 98466f37aafSCyrille Pitchen if (ring->head >= sg_dma_len(&atmel_port->sg_rx)) 98566f37aafSCyrille Pitchen ring->head = 0; 98666f37aafSCyrille Pitchen ring->tail = ring->head; 98766f37aafSCyrille Pitchen port->icount.rx += count; 98866f37aafSCyrille Pitchen } 98966f37aafSCyrille Pitchen 99066f37aafSCyrille Pitchen /* USART retreives ownership of RX DMA buffer */ 99166f37aafSCyrille Pitchen dma_sync_sg_for_device(port->dev, 99266f37aafSCyrille Pitchen &atmel_port->sg_rx, 99366f37aafSCyrille Pitchen 1, 994485819b5SCyrille Pitchen DMA_FROM_DEVICE); 99566f37aafSCyrille Pitchen 99666f37aafSCyrille Pitchen /* 99766f37aafSCyrille Pitchen * Drop the lock here since it might end up calling 99866f37aafSCyrille Pitchen * uart_start(), which takes the lock. 99966f37aafSCyrille Pitchen */ 100066f37aafSCyrille Pitchen spin_unlock(&port->lock); 100166f37aafSCyrille Pitchen tty_flip_buffer_push(tport); 100266f37aafSCyrille Pitchen spin_lock(&port->lock); 100366f37aafSCyrille Pitchen 100434df42f5SElen Song UART_PUT_IER(port, ATMEL_US_TIMEOUT); 100534df42f5SElen Song } 100634df42f5SElen Song 100734df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port) 100834df42f5SElen Song { 100934df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 101034df42f5SElen Song struct dma_async_tx_descriptor *desc; 101134df42f5SElen Song dma_cap_mask_t mask; 101234df42f5SElen Song struct dma_slave_config config; 101334df42f5SElen Song struct circ_buf *ring; 101434df42f5SElen Song int ret, nent; 101534df42f5SElen Song 101634df42f5SElen Song ring = &atmel_port->rx_ring; 101734df42f5SElen Song 101834df42f5SElen Song dma_cap_zero(mask); 101934df42f5SElen Song dma_cap_set(DMA_CYCLIC, mask); 102034df42f5SElen Song 102134df42f5SElen Song atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx"); 102234df42f5SElen Song if (atmel_port->chan_rx == NULL) 102334df42f5SElen Song goto chan_err; 102434df42f5SElen Song dev_info(port->dev, "using %s for rx DMA transfers\n", 102534df42f5SElen Song dma_chan_name(atmel_port->chan_rx)); 102634df42f5SElen Song 102734df42f5SElen Song spin_lock_init(&atmel_port->lock_rx); 102834df42f5SElen Song sg_init_table(&atmel_port->sg_rx, 1); 102934df42f5SElen Song /* UART circular rx buffer is an aligned page. */ 10302c277054SLeilei Zhao BUG_ON(!PAGE_ALIGNED(ring->buf)); 103134df42f5SElen Song sg_set_page(&atmel_port->sg_rx, 103234df42f5SElen Song virt_to_page(ring->buf), 1033*a510880fSLeilei Zhao sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE, 103434df42f5SElen Song (int)ring->buf & ~PAGE_MASK); 103534df42f5SElen Song nent = dma_map_sg(port->dev, 103634df42f5SElen Song &atmel_port->sg_rx, 103734df42f5SElen Song 1, 103848479148SWolfram Sang DMA_FROM_DEVICE); 103934df42f5SElen Song 104034df42f5SElen Song if (!nent) { 104134df42f5SElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 104234df42f5SElen Song goto chan_err; 104334df42f5SElen Song } else { 104434df42f5SElen Song dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__, 104534df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 104634df42f5SElen Song ring->buf, 104734df42f5SElen Song sg_dma_address(&atmel_port->sg_rx)); 104834df42f5SElen Song } 104934df42f5SElen Song 105034df42f5SElen Song /* Configure the slave DMA */ 105134df42f5SElen Song memset(&config, 0, sizeof(config)); 105234df42f5SElen Song config.direction = DMA_DEV_TO_MEM; 105334df42f5SElen Song config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 105434df42f5SElen Song config.src_addr = port->mapbase + ATMEL_US_RHR; 105534df42f5SElen Song 10565483c10eSMaxime Ripard ret = dmaengine_slave_config(atmel_port->chan_rx, 10575483c10eSMaxime Ripard &config); 105834df42f5SElen Song if (ret) { 105934df42f5SElen Song dev_err(port->dev, "DMA rx slave configuration failed\n"); 106034df42f5SElen Song goto chan_err; 106134df42f5SElen Song } 106234df42f5SElen Song /* 106334df42f5SElen Song * Prepare a cyclic dma transfer, assign 2 descriptors, 106434df42f5SElen Song * each one is half ring buffer size 106534df42f5SElen Song */ 106634df42f5SElen Song desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx, 106734df42f5SElen Song sg_dma_address(&atmel_port->sg_rx), 106834df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 106934df42f5SElen Song sg_dma_len(&atmel_port->sg_rx)/2, 107034df42f5SElen Song DMA_DEV_TO_MEM, 107134df42f5SElen Song DMA_PREP_INTERRUPT); 107234df42f5SElen Song desc->callback = atmel_complete_rx_dma; 107334df42f5SElen Song desc->callback_param = port; 107434df42f5SElen Song atmel_port->desc_rx = desc; 107534df42f5SElen Song atmel_port->cookie_rx = dmaengine_submit(desc); 107634df42f5SElen Song 107734df42f5SElen Song return 0; 107834df42f5SElen Song 107934df42f5SElen Song chan_err: 108034df42f5SElen Song dev_err(port->dev, "RX channel not available, switch to pio\n"); 108134df42f5SElen Song atmel_port->use_dma_rx = 0; 108234df42f5SElen Song if (atmel_port->chan_rx) 108334df42f5SElen Song atmel_release_rx_dma(port); 108434df42f5SElen Song return -EINVAL; 108534df42f5SElen Song } 108634df42f5SElen Song 10872e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data) 10882e68c22fSElen Song { 10892e68c22fSElen Song struct uart_port *port = (void *)data; 10902e68c22fSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 10912e68c22fSElen Song 10922e68c22fSElen Song tasklet_schedule(&atmel_port->tasklet); 10932e68c22fSElen Song mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port)); 10942e68c22fSElen Song } 10952e68c22fSElen Song 1096ab4382d2SGreg Kroah-Hartman /* 1097ab4382d2SGreg Kroah-Hartman * receive interrupt handler. 1098ab4382d2SGreg Kroah-Hartman */ 1099ab4382d2SGreg Kroah-Hartman static void 1100ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending) 1101ab4382d2SGreg Kroah-Hartman { 1102ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1103ab4382d2SGreg Kroah-Hartman 110464e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 1105ab4382d2SGreg Kroah-Hartman /* 1106ab4382d2SGreg Kroah-Hartman * PDC receive. Just schedule the tasklet and let it 1107ab4382d2SGreg Kroah-Hartman * figure out the details. 1108ab4382d2SGreg Kroah-Hartman * 1109ab4382d2SGreg Kroah-Hartman * TODO: We're not handling error flags correctly at 1110ab4382d2SGreg Kroah-Hartman * the moment. 1111ab4382d2SGreg Kroah-Hartman */ 1112ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) { 1113ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, (ATMEL_US_ENDRX 1114ab4382d2SGreg Kroah-Hartman | ATMEL_US_TIMEOUT)); 1115ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 1116ab4382d2SGreg Kroah-Hartman } 1117ab4382d2SGreg Kroah-Hartman 1118ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE | 1119ab4382d2SGreg Kroah-Hartman ATMEL_US_FRAME | ATMEL_US_PARE)) 1120ab4382d2SGreg Kroah-Hartman atmel_pdc_rxerr(port, pending); 1121ab4382d2SGreg Kroah-Hartman } 1122ab4382d2SGreg Kroah-Hartman 112334df42f5SElen Song if (atmel_use_dma_rx(port)) { 112434df42f5SElen Song if (pending & ATMEL_US_TIMEOUT) { 112534df42f5SElen Song UART_PUT_IDR(port, ATMEL_US_TIMEOUT); 112634df42f5SElen Song tasklet_schedule(&atmel_port->tasklet); 112734df42f5SElen Song } 112834df42f5SElen Song } 112934df42f5SElen Song 1130ab4382d2SGreg Kroah-Hartman /* Interrupt receive */ 1131ab4382d2SGreg Kroah-Hartman if (pending & ATMEL_US_RXRDY) 1132ab4382d2SGreg Kroah-Hartman atmel_rx_chars(port); 1133ab4382d2SGreg Kroah-Hartman else if (pending & ATMEL_US_RXBRK) { 1134ab4382d2SGreg Kroah-Hartman /* 1135ab4382d2SGreg Kroah-Hartman * End of break detected. If it came along with a 1136ab4382d2SGreg Kroah-Hartman * character, atmel_rx_chars will handle it. 1137ab4382d2SGreg Kroah-Hartman */ 1138ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA); 1139ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXBRK); 1140ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 1141ab4382d2SGreg Kroah-Hartman } 1142ab4382d2SGreg Kroah-Hartman } 1143ab4382d2SGreg Kroah-Hartman 1144ab4382d2SGreg Kroah-Hartman /* 1145ab4382d2SGreg Kroah-Hartman * transmit interrupt handler. (Transmit is IRQF_NODELAY safe) 1146ab4382d2SGreg Kroah-Hartman */ 1147ab4382d2SGreg Kroah-Hartman static void 1148ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending) 1149ab4382d2SGreg Kroah-Hartman { 1150ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1151ab4382d2SGreg Kroah-Hartman 1152ab4382d2SGreg Kroah-Hartman if (pending & atmel_port->tx_done_mask) { 1153ab4382d2SGreg Kroah-Hartman /* Either PDC or interrupt transmission */ 1154ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, atmel_port->tx_done_mask); 1155ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 1156ab4382d2SGreg Kroah-Hartman } 1157ab4382d2SGreg Kroah-Hartman } 1158ab4382d2SGreg Kroah-Hartman 1159ab4382d2SGreg Kroah-Hartman /* 1160ab4382d2SGreg Kroah-Hartman * status flags interrupt handler. 1161ab4382d2SGreg Kroah-Hartman */ 1162ab4382d2SGreg Kroah-Hartman static void 1163ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending, 1164ab4382d2SGreg Kroah-Hartman unsigned int status) 1165ab4382d2SGreg Kroah-Hartman { 1166ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1167ab4382d2SGreg Kroah-Hartman 1168ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC 1169ab4382d2SGreg Kroah-Hartman | ATMEL_US_CTSIC)) { 1170ab4382d2SGreg Kroah-Hartman atmel_port->irq_status = status; 1171ab4382d2SGreg Kroah-Hartman tasklet_schedule(&atmel_port->tasklet); 1172ab4382d2SGreg Kroah-Hartman } 1173ab4382d2SGreg Kroah-Hartman } 1174ab4382d2SGreg Kroah-Hartman 1175ab4382d2SGreg Kroah-Hartman /* 1176ab4382d2SGreg Kroah-Hartman * Interrupt handler 1177ab4382d2SGreg Kroah-Hartman */ 1178ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id) 1179ab4382d2SGreg Kroah-Hartman { 1180ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 1181ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1182ab4382d2SGreg Kroah-Hartman unsigned int status, pending, pass_counter = 0; 1183ab5e4e41SRichard Genoud bool gpio_handled = false; 1184ab4382d2SGreg Kroah-Hartman 1185ab4382d2SGreg Kroah-Hartman do { 1186e0b0baadSRichard Genoud status = atmel_get_lines_status(port); 1187ab4382d2SGreg Kroah-Hartman pending = status & UART_GET_IMR(port); 1188ab5e4e41SRichard Genoud if (!gpio_handled) { 1189ab5e4e41SRichard Genoud /* 1190ab5e4e41SRichard Genoud * Dealing with GPIO interrupt 1191ab5e4e41SRichard Genoud */ 1192ab5e4e41SRichard Genoud if (irq == atmel_port->gpio_irq[UART_GPIO_CTS]) 1193ab5e4e41SRichard Genoud pending |= ATMEL_US_CTSIC; 1194ab5e4e41SRichard Genoud 1195ab5e4e41SRichard Genoud if (irq == atmel_port->gpio_irq[UART_GPIO_DSR]) 1196ab5e4e41SRichard Genoud pending |= ATMEL_US_DSRIC; 1197ab5e4e41SRichard Genoud 1198ab5e4e41SRichard Genoud if (irq == atmel_port->gpio_irq[UART_GPIO_RI]) 1199ab5e4e41SRichard Genoud pending |= ATMEL_US_RIIC; 1200ab5e4e41SRichard Genoud 1201ab5e4e41SRichard Genoud if (irq == atmel_port->gpio_irq[UART_GPIO_DCD]) 1202ab5e4e41SRichard Genoud pending |= ATMEL_US_DCDIC; 1203ab5e4e41SRichard Genoud 1204ab5e4e41SRichard Genoud gpio_handled = true; 1205ab5e4e41SRichard Genoud } 1206ab4382d2SGreg Kroah-Hartman if (!pending) 1207ab4382d2SGreg Kroah-Hartman break; 1208ab4382d2SGreg Kroah-Hartman 1209ab4382d2SGreg Kroah-Hartman atmel_handle_receive(port, pending); 1210ab4382d2SGreg Kroah-Hartman atmel_handle_status(port, pending, status); 1211ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(port, pending); 1212ab4382d2SGreg Kroah-Hartman } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT); 1213ab4382d2SGreg Kroah-Hartman 1214ab4382d2SGreg Kroah-Hartman return pass_counter ? IRQ_HANDLED : IRQ_NONE; 1215ab4382d2SGreg Kroah-Hartman } 1216ab4382d2SGreg Kroah-Hartman 1217a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port) 1218a930e528SElen Song { 1219a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1220a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1221a930e528SElen Song 1222a930e528SElen Song dma_unmap_single(port->dev, 1223a930e528SElen Song pdc->dma_addr, 1224a930e528SElen Song pdc->dma_size, 1225a930e528SElen Song DMA_TO_DEVICE); 1226a930e528SElen Song } 1227a930e528SElen Song 1228ab4382d2SGreg Kroah-Hartman /* 1229ab4382d2SGreg Kroah-Hartman * Called from tasklet with ENDTX and TXBUFE interrupts disabled. 1230ab4382d2SGreg Kroah-Hartman */ 123164e22ebeSElen Song static void atmel_tx_pdc(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 *xmit = &port->state->xmit; 1235ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1236ab4382d2SGreg Kroah-Hartman int count; 1237ab4382d2SGreg Kroah-Hartman 1238ab4382d2SGreg Kroah-Hartman /* nothing left to transmit? */ 1239ab4382d2SGreg Kroah-Hartman if (UART_GET_TCR(port)) 1240ab4382d2SGreg Kroah-Hartman return; 1241ab4382d2SGreg Kroah-Hartman 1242ab4382d2SGreg Kroah-Hartman xmit->tail += pdc->ofs; 1243ab4382d2SGreg Kroah-Hartman xmit->tail &= UART_XMIT_SIZE - 1; 1244ab4382d2SGreg Kroah-Hartman 1245ab4382d2SGreg Kroah-Hartman port->icount.tx += pdc->ofs; 1246ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 1247ab4382d2SGreg Kroah-Hartman 1248ab4382d2SGreg Kroah-Hartman /* more to transmit - setup next transfer */ 1249ab4382d2SGreg Kroah-Hartman 1250ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 1251ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 1252ab4382d2SGreg Kroah-Hartman 1253ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 1254ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, 1255ab4382d2SGreg Kroah-Hartman pdc->dma_addr, 1256ab4382d2SGreg Kroah-Hartman pdc->dma_size, 1257ab4382d2SGreg Kroah-Hartman DMA_TO_DEVICE); 1258ab4382d2SGreg Kroah-Hartman 1259ab4382d2SGreg Kroah-Hartman count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 1260ab4382d2SGreg Kroah-Hartman pdc->ofs = count; 1261ab4382d2SGreg Kroah-Hartman 1262ab4382d2SGreg Kroah-Hartman UART_PUT_TPR(port, pdc->dma_addr + xmit->tail); 1263ab4382d2SGreg Kroah-Hartman UART_PUT_TCR(port, count); 1264ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 1265ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); 1266ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 1267ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->tx_done_mask); 1268ab4382d2SGreg Kroah-Hartman } else { 126913bd3e6fSRicardo Ribalda Delgado if ((port->rs485.flags & SER_RS485_ENABLED) && 127013bd3e6fSRicardo Ribalda Delgado !(port->rs485.flags & SER_RS485_RX_DURING_TX)) { 1271ab4382d2SGreg Kroah-Hartman /* DMA done, stop TX, start RX for RS485 */ 1272ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 1273ab4382d2SGreg Kroah-Hartman } 1274ab4382d2SGreg Kroah-Hartman } 1275ab4382d2SGreg Kroah-Hartman 1276ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 1277ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 1278ab4382d2SGreg Kroah-Hartman } 1279ab4382d2SGreg Kroah-Hartman 1280a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port) 1281a930e528SElen Song { 1282a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1283a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1284a930e528SElen Song struct circ_buf *xmit = &port->state->xmit; 1285a930e528SElen Song 1286a930e528SElen Song pdc->buf = xmit->buf; 1287a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1288a930e528SElen Song pdc->buf, 1289a930e528SElen Song UART_XMIT_SIZE, 1290a930e528SElen Song DMA_TO_DEVICE); 1291a930e528SElen Song pdc->dma_size = UART_XMIT_SIZE; 1292a930e528SElen Song pdc->ofs = 0; 1293a930e528SElen Song 1294a930e528SElen Song return 0; 1295a930e528SElen Song } 1296a930e528SElen Song 1297ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port) 1298ab4382d2SGreg Kroah-Hartman { 1299ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1300ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 1301ab4382d2SGreg Kroah-Hartman unsigned int flg; 1302ab4382d2SGreg Kroah-Hartman unsigned int status; 1303ab4382d2SGreg Kroah-Hartman 1304ab4382d2SGreg Kroah-Hartman while (ring->head != ring->tail) { 1305ab4382d2SGreg Kroah-Hartman struct atmel_uart_char c; 1306ab4382d2SGreg Kroah-Hartman 1307ab4382d2SGreg Kroah-Hartman /* Make sure c is loaded after head. */ 1308ab4382d2SGreg Kroah-Hartman smp_rmb(); 1309ab4382d2SGreg Kroah-Hartman 1310ab4382d2SGreg Kroah-Hartman c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; 1311ab4382d2SGreg Kroah-Hartman 1312ab4382d2SGreg Kroah-Hartman ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 1313ab4382d2SGreg Kroah-Hartman 1314ab4382d2SGreg Kroah-Hartman port->icount.rx++; 1315ab4382d2SGreg Kroah-Hartman status = c.status; 1316ab4382d2SGreg Kroah-Hartman flg = TTY_NORMAL; 1317ab4382d2SGreg Kroah-Hartman 1318ab4382d2SGreg Kroah-Hartman /* 1319ab4382d2SGreg Kroah-Hartman * note that the error handling code is 1320ab4382d2SGreg Kroah-Hartman * out of the main execution path 1321ab4382d2SGreg Kroah-Hartman */ 1322ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 1323ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK))) { 1324ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 1325ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 1326ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 1327ab4382d2SGreg Kroah-Hartman 1328ab4382d2SGreg Kroah-Hartman port->icount.brk++; 1329ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 1330ab4382d2SGreg Kroah-Hartman continue; 1331ab4382d2SGreg Kroah-Hartman } 1332ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 1333ab4382d2SGreg Kroah-Hartman port->icount.parity++; 1334ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 1335ab4382d2SGreg Kroah-Hartman port->icount.frame++; 1336ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 1337ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 1338ab4382d2SGreg Kroah-Hartman 1339ab4382d2SGreg Kroah-Hartman status &= port->read_status_mask; 1340ab4382d2SGreg Kroah-Hartman 1341ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) 1342ab4382d2SGreg Kroah-Hartman flg = TTY_BREAK; 1343ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_PARE) 1344ab4382d2SGreg Kroah-Hartman flg = TTY_PARITY; 1345ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_FRAME) 1346ab4382d2SGreg Kroah-Hartman flg = TTY_FRAME; 1347ab4382d2SGreg Kroah-Hartman } 1348ab4382d2SGreg Kroah-Hartman 1349ab4382d2SGreg Kroah-Hartman 1350ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c.ch)) 1351ab4382d2SGreg Kroah-Hartman continue; 1352ab4382d2SGreg Kroah-Hartman 1353ab4382d2SGreg Kroah-Hartman uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg); 1354ab4382d2SGreg Kroah-Hartman } 1355ab4382d2SGreg Kroah-Hartman 1356ab4382d2SGreg Kroah-Hartman /* 1357ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 1358ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 1359ab4382d2SGreg Kroah-Hartman */ 1360ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 13612e124b4aSJiri Slaby tty_flip_buffer_push(&port->state->port); 1362ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1363ab4382d2SGreg Kroah-Hartman } 1364ab4382d2SGreg Kroah-Hartman 1365a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port) 1366a930e528SElen Song { 1367a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1368a930e528SElen Song int i; 1369a930e528SElen Song 1370a930e528SElen Song for (i = 0; i < 2; i++) { 1371a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1372a930e528SElen Song 1373a930e528SElen Song dma_unmap_single(port->dev, 1374a930e528SElen Song pdc->dma_addr, 1375a930e528SElen Song pdc->dma_size, 1376a930e528SElen Song DMA_FROM_DEVICE); 1377a930e528SElen Song kfree(pdc->buf); 1378a930e528SElen Song } 1379a930e528SElen Song } 1380a930e528SElen Song 138164e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port) 1382ab4382d2SGreg Kroah-Hartman { 1383ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 138405c7cd39SJiri Slaby struct tty_port *tport = &port->state->port; 1385ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc; 1386ab4382d2SGreg Kroah-Hartman int rx_idx = atmel_port->pdc_rx_idx; 1387ab4382d2SGreg Kroah-Hartman unsigned int head; 1388ab4382d2SGreg Kroah-Hartman unsigned int tail; 1389ab4382d2SGreg Kroah-Hartman unsigned int count; 1390ab4382d2SGreg Kroah-Hartman 1391ab4382d2SGreg Kroah-Hartman do { 1392ab4382d2SGreg Kroah-Hartman /* Reset the UART timeout early so that we don't miss one */ 1393ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STTTO); 1394ab4382d2SGreg Kroah-Hartman 1395ab4382d2SGreg Kroah-Hartman pdc = &atmel_port->pdc_rx[rx_idx]; 1396ab4382d2SGreg Kroah-Hartman head = UART_GET_RPR(port) - pdc->dma_addr; 1397ab4382d2SGreg Kroah-Hartman tail = pdc->ofs; 1398ab4382d2SGreg Kroah-Hartman 1399ab4382d2SGreg Kroah-Hartman /* If the PDC has switched buffers, RPR won't contain 1400ab4382d2SGreg Kroah-Hartman * any address within the current buffer. Since head 1401ab4382d2SGreg Kroah-Hartman * is unsigned, we just need a one-way comparison to 1402ab4382d2SGreg Kroah-Hartman * find out. 1403ab4382d2SGreg Kroah-Hartman * 1404ab4382d2SGreg Kroah-Hartman * In this case, we just need to consume the entire 1405ab4382d2SGreg Kroah-Hartman * buffer and resubmit it for DMA. This will clear the 1406ab4382d2SGreg Kroah-Hartman * ENDRX bit as well, so that we can safely re-enable 1407ab4382d2SGreg Kroah-Hartman * all interrupts below. 1408ab4382d2SGreg Kroah-Hartman */ 1409ab4382d2SGreg Kroah-Hartman head = min(head, pdc->dma_size); 1410ab4382d2SGreg Kroah-Hartman 1411ab4382d2SGreg Kroah-Hartman if (likely(head != tail)) { 1412ab4382d2SGreg Kroah-Hartman dma_sync_single_for_cpu(port->dev, pdc->dma_addr, 1413ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1414ab4382d2SGreg Kroah-Hartman 1415ab4382d2SGreg Kroah-Hartman /* 1416ab4382d2SGreg Kroah-Hartman * head will only wrap around when we recycle 1417ab4382d2SGreg Kroah-Hartman * the DMA buffer, and when that happens, we 1418ab4382d2SGreg Kroah-Hartman * explicitly set tail to 0. So head will 1419ab4382d2SGreg Kroah-Hartman * always be greater than tail. 1420ab4382d2SGreg Kroah-Hartman */ 1421ab4382d2SGreg Kroah-Hartman count = head - tail; 1422ab4382d2SGreg Kroah-Hartman 142305c7cd39SJiri Slaby tty_insert_flip_string(tport, pdc->buf + pdc->ofs, 142405c7cd39SJiri Slaby count); 1425ab4382d2SGreg Kroah-Hartman 1426ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, pdc->dma_addr, 1427ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1428ab4382d2SGreg Kroah-Hartman 1429ab4382d2SGreg Kroah-Hartman port->icount.rx += count; 1430ab4382d2SGreg Kroah-Hartman pdc->ofs = head; 1431ab4382d2SGreg Kroah-Hartman } 1432ab4382d2SGreg Kroah-Hartman 1433ab4382d2SGreg Kroah-Hartman /* 1434ab4382d2SGreg Kroah-Hartman * If the current buffer is full, we need to check if 1435ab4382d2SGreg Kroah-Hartman * the next one contains any additional data. 1436ab4382d2SGreg Kroah-Hartman */ 1437ab4382d2SGreg Kroah-Hartman if (head >= pdc->dma_size) { 1438ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 1439ab4382d2SGreg Kroah-Hartman UART_PUT_RNPR(port, pdc->dma_addr); 1440ab4382d2SGreg Kroah-Hartman UART_PUT_RNCR(port, pdc->dma_size); 1441ab4382d2SGreg Kroah-Hartman 1442ab4382d2SGreg Kroah-Hartman rx_idx = !rx_idx; 1443ab4382d2SGreg Kroah-Hartman atmel_port->pdc_rx_idx = rx_idx; 1444ab4382d2SGreg Kroah-Hartman } 1445ab4382d2SGreg Kroah-Hartman } while (head >= pdc->dma_size); 1446ab4382d2SGreg Kroah-Hartman 1447ab4382d2SGreg Kroah-Hartman /* 1448ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 1449ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 1450ab4382d2SGreg Kroah-Hartman */ 1451ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 14522e124b4aSJiri Slaby tty_flip_buffer_push(tport); 1453ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1454ab4382d2SGreg Kroah-Hartman 1455ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1456ab4382d2SGreg Kroah-Hartman } 1457ab4382d2SGreg Kroah-Hartman 1458a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port) 1459a930e528SElen Song { 1460a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1461a930e528SElen Song int i; 1462a930e528SElen Song 1463a930e528SElen Song for (i = 0; i < 2; i++) { 1464a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1465a930e528SElen Song 1466a930e528SElen Song pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); 1467a930e528SElen Song if (pdc->buf == NULL) { 1468a930e528SElen Song if (i != 0) { 1469a930e528SElen Song dma_unmap_single(port->dev, 1470a930e528SElen Song atmel_port->pdc_rx[0].dma_addr, 1471a930e528SElen Song PDC_BUFFER_SIZE, 1472a930e528SElen Song DMA_FROM_DEVICE); 1473a930e528SElen Song kfree(atmel_port->pdc_rx[0].buf); 1474a930e528SElen Song } 1475a930e528SElen Song atmel_port->use_pdc_rx = 0; 1476a930e528SElen Song return -ENOMEM; 1477a930e528SElen Song } 1478a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1479a930e528SElen Song pdc->buf, 1480a930e528SElen Song PDC_BUFFER_SIZE, 1481a930e528SElen Song DMA_FROM_DEVICE); 1482a930e528SElen Song pdc->dma_size = PDC_BUFFER_SIZE; 1483a930e528SElen Song pdc->ofs = 0; 1484a930e528SElen Song } 1485a930e528SElen Song 1486a930e528SElen Song atmel_port->pdc_rx_idx = 0; 1487a930e528SElen Song 1488a930e528SElen Song UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr); 1489a930e528SElen Song UART_PUT_RCR(port, PDC_BUFFER_SIZE); 1490a930e528SElen Song 1491a930e528SElen Song UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr); 1492a930e528SElen Song UART_PUT_RNCR(port, PDC_BUFFER_SIZE); 1493a930e528SElen Song 1494a930e528SElen Song return 0; 1495a930e528SElen Song } 1496a930e528SElen Song 1497ab4382d2SGreg Kroah-Hartman /* 1498ab4382d2SGreg Kroah-Hartman * tasklet handling tty stuff outside the interrupt handler. 1499ab4382d2SGreg Kroah-Hartman */ 1500ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data) 1501ab4382d2SGreg Kroah-Hartman { 1502ab4382d2SGreg Kroah-Hartman struct uart_port *port = (struct uart_port *)data; 1503ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1504ab4382d2SGreg Kroah-Hartman unsigned int status; 1505ab4382d2SGreg Kroah-Hartman unsigned int status_change; 1506ab4382d2SGreg Kroah-Hartman 1507ab4382d2SGreg Kroah-Hartman /* The interrupt handler does not take the lock */ 1508ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1509ab4382d2SGreg Kroah-Hartman 1510a930e528SElen Song atmel_port->schedule_tx(port); 1511ab4382d2SGreg Kroah-Hartman 1512ab4382d2SGreg Kroah-Hartman status = atmel_port->irq_status; 1513ab4382d2SGreg Kroah-Hartman status_change = status ^ atmel_port->irq_status_prev; 1514ab4382d2SGreg Kroah-Hartman 1515ab4382d2SGreg Kroah-Hartman if (status_change & (ATMEL_US_RI | ATMEL_US_DSR 1516ab4382d2SGreg Kroah-Hartman | ATMEL_US_DCD | ATMEL_US_CTS)) { 1517ab4382d2SGreg Kroah-Hartman /* TODO: All reads to CSR will clear these interrupts! */ 1518ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_RI) 1519ab4382d2SGreg Kroah-Hartman port->icount.rng++; 1520ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_DSR) 1521ab4382d2SGreg Kroah-Hartman port->icount.dsr++; 1522ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_DCD) 1523ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); 1524ab4382d2SGreg Kroah-Hartman if (status_change & ATMEL_US_CTS) 1525ab4382d2SGreg Kroah-Hartman uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); 1526ab4382d2SGreg Kroah-Hartman 1527ab4382d2SGreg Kroah-Hartman wake_up_interruptible(&port->state->port.delta_msr_wait); 1528ab4382d2SGreg Kroah-Hartman 1529ab4382d2SGreg Kroah-Hartman atmel_port->irq_status_prev = status; 1530ab4382d2SGreg Kroah-Hartman } 1531ab4382d2SGreg Kroah-Hartman 1532a930e528SElen Song atmel_port->schedule_rx(port); 1533ab4382d2SGreg Kroah-Hartman 1534ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 1535ab4382d2SGreg Kroah-Hartman } 1536ab4382d2SGreg Kroah-Hartman 153733d64c4fSElen Song static int atmel_init_property(struct atmel_uart_port *atmel_port, 153833d64c4fSElen Song struct platform_device *pdev) 153933d64c4fSElen Song { 154033d64c4fSElen Song struct device_node *np = pdev->dev.of_node; 1541574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 154233d64c4fSElen Song 154333d64c4fSElen Song if (np) { 154433d64c4fSElen Song /* DMA/PDC usage specification */ 154533d64c4fSElen Song if (of_get_property(np, "atmel,use-dma-rx", NULL)) { 154633d64c4fSElen Song if (of_get_property(np, "dmas", NULL)) { 154733d64c4fSElen Song atmel_port->use_dma_rx = true; 154833d64c4fSElen Song atmel_port->use_pdc_rx = false; 154933d64c4fSElen Song } else { 155033d64c4fSElen Song atmel_port->use_dma_rx = false; 155133d64c4fSElen Song atmel_port->use_pdc_rx = true; 155233d64c4fSElen Song } 155333d64c4fSElen Song } else { 155433d64c4fSElen Song atmel_port->use_dma_rx = false; 155533d64c4fSElen Song atmel_port->use_pdc_rx = false; 155633d64c4fSElen Song } 155733d64c4fSElen Song 155833d64c4fSElen Song if (of_get_property(np, "atmel,use-dma-tx", NULL)) { 155933d64c4fSElen Song if (of_get_property(np, "dmas", NULL)) { 156033d64c4fSElen Song atmel_port->use_dma_tx = true; 156133d64c4fSElen Song atmel_port->use_pdc_tx = false; 156233d64c4fSElen Song } else { 156333d64c4fSElen Song atmel_port->use_dma_tx = false; 156433d64c4fSElen Song atmel_port->use_pdc_tx = true; 156533d64c4fSElen Song } 156633d64c4fSElen Song } else { 156733d64c4fSElen Song atmel_port->use_dma_tx = false; 156833d64c4fSElen Song atmel_port->use_pdc_tx = false; 156933d64c4fSElen Song } 157033d64c4fSElen Song 157133d64c4fSElen Song } else { 157233d64c4fSElen Song atmel_port->use_pdc_rx = pdata->use_dma_rx; 157333d64c4fSElen Song atmel_port->use_pdc_tx = pdata->use_dma_tx; 157433d64c4fSElen Song atmel_port->use_dma_rx = false; 157533d64c4fSElen Song atmel_port->use_dma_tx = false; 157633d64c4fSElen Song } 157733d64c4fSElen Song 157833d64c4fSElen Song return 0; 157933d64c4fSElen Song } 158033d64c4fSElen Song 158113bd3e6fSRicardo Ribalda Delgado static void atmel_init_rs485(struct uart_port *port, 158233d64c4fSElen Song struct platform_device *pdev) 158333d64c4fSElen Song { 158433d64c4fSElen Song struct device_node *np = pdev->dev.of_node; 1585574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 158633d64c4fSElen Song 158733d64c4fSElen Song if (np) { 158833d64c4fSElen Song u32 rs485_delay[2]; 158933d64c4fSElen Song /* rs485 properties */ 159033d64c4fSElen Song if (of_property_read_u32_array(np, "rs485-rts-delay", 159133d64c4fSElen Song rs485_delay, 2) == 0) { 159213bd3e6fSRicardo Ribalda Delgado struct serial_rs485 *rs485conf = &port->rs485; 159333d64c4fSElen Song 159433d64c4fSElen Song rs485conf->delay_rts_before_send = rs485_delay[0]; 159533d64c4fSElen Song rs485conf->delay_rts_after_send = rs485_delay[1]; 159633d64c4fSElen Song rs485conf->flags = 0; 159733d64c4fSElen Song 159833d64c4fSElen Song if (of_get_property(np, "rs485-rx-during-tx", NULL)) 159933d64c4fSElen Song rs485conf->flags |= SER_RS485_RX_DURING_TX; 160033d64c4fSElen Song 160133d64c4fSElen Song if (of_get_property(np, "linux,rs485-enabled-at-boot-time", 160233d64c4fSElen Song NULL)) 160333d64c4fSElen Song rs485conf->flags |= SER_RS485_ENABLED; 160433d64c4fSElen Song } 160533d64c4fSElen Song } else { 160613bd3e6fSRicardo Ribalda Delgado port->rs485 = pdata->rs485; 160733d64c4fSElen Song } 160833d64c4fSElen Song 160933d64c4fSElen Song } 161033d64c4fSElen Song 1611a930e528SElen Song static void atmel_set_ops(struct uart_port *port) 1612a930e528SElen Song { 1613a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1614a930e528SElen Song 161534df42f5SElen Song if (atmel_use_dma_rx(port)) { 161634df42f5SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_dma; 161734df42f5SElen Song atmel_port->schedule_rx = &atmel_rx_from_dma; 161834df42f5SElen Song atmel_port->release_rx = &atmel_release_rx_dma; 161934df42f5SElen Song } else if (atmel_use_pdc_rx(port)) { 1620a930e528SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_pdc; 1621a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_pdc; 1622a930e528SElen Song atmel_port->release_rx = &atmel_release_rx_pdc; 1623a930e528SElen Song } else { 1624a930e528SElen Song atmel_port->prepare_rx = NULL; 1625a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_ring; 1626a930e528SElen Song atmel_port->release_rx = NULL; 1627a930e528SElen Song } 1628a930e528SElen Song 162908f738beSElen Song if (atmel_use_dma_tx(port)) { 163008f738beSElen Song atmel_port->prepare_tx = &atmel_prepare_tx_dma; 163108f738beSElen Song atmel_port->schedule_tx = &atmel_tx_dma; 163208f738beSElen Song atmel_port->release_tx = &atmel_release_tx_dma; 163308f738beSElen Song } else if (atmel_use_pdc_tx(port)) { 1634a930e528SElen Song atmel_port->prepare_tx = &atmel_prepare_tx_pdc; 1635a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_pdc; 1636a930e528SElen Song atmel_port->release_tx = &atmel_release_tx_pdc; 1637a930e528SElen Song } else { 1638a930e528SElen Song atmel_port->prepare_tx = NULL; 1639a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_chars; 1640a930e528SElen Song atmel_port->release_tx = NULL; 1641a930e528SElen Song } 1642a930e528SElen Song } 1643a930e528SElen Song 1644ab4382d2SGreg Kroah-Hartman /* 1645055560b0SElen Song * Get ip name usart or uart 1646055560b0SElen Song */ 1647892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port) 1648055560b0SElen Song { 1649055560b0SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1650055560b0SElen Song int name = UART_GET_IP_NAME(port); 1651731d9caeSNicolas Ferre u32 version; 1652055560b0SElen Song int usart, uart; 1653055560b0SElen Song /* usart and uart ascii */ 1654055560b0SElen Song usart = 0x55534152; 1655055560b0SElen Song uart = 0x44424755; 1656055560b0SElen Song 1657055560b0SElen Song atmel_port->is_usart = false; 1658055560b0SElen Song 1659055560b0SElen Song if (name == usart) { 1660055560b0SElen Song dev_dbg(port->dev, "This is usart\n"); 1661055560b0SElen Song atmel_port->is_usart = true; 1662055560b0SElen Song } else if (name == uart) { 1663055560b0SElen Song dev_dbg(port->dev, "This is uart\n"); 1664055560b0SElen Song atmel_port->is_usart = false; 1665055560b0SElen Song } else { 1666731d9caeSNicolas Ferre /* fallback for older SoCs: use version field */ 1667731d9caeSNicolas Ferre version = UART_GET_IP_VERSION(port); 1668731d9caeSNicolas Ferre switch (version) { 1669731d9caeSNicolas Ferre case 0x302: 1670731d9caeSNicolas Ferre case 0x10213: 1671731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is usart\n"); 1672731d9caeSNicolas Ferre atmel_port->is_usart = true; 1673731d9caeSNicolas Ferre break; 1674731d9caeSNicolas Ferre case 0x203: 1675731d9caeSNicolas Ferre case 0x10202: 1676731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is uart\n"); 1677731d9caeSNicolas Ferre atmel_port->is_usart = false; 1678731d9caeSNicolas Ferre break; 1679731d9caeSNicolas Ferre default: 1680731d9caeSNicolas Ferre dev_err(port->dev, "Not supported ip name nor version, set to uart\n"); 1681731d9caeSNicolas Ferre } 1682055560b0SElen Song } 1683055560b0SElen Song } 1684055560b0SElen Song 1685ab5e4e41SRichard Genoud static void atmel_free_gpio_irq(struct uart_port *port) 1686ab5e4e41SRichard Genoud { 1687ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1688ab5e4e41SRichard Genoud enum mctrl_gpio_idx i; 1689ab5e4e41SRichard Genoud 1690ab5e4e41SRichard Genoud for (i = 0; i < UART_GPIO_MAX; i++) 1691ab5e4e41SRichard Genoud if (atmel_port->gpio_irq[i] >= 0) 1692ab5e4e41SRichard Genoud free_irq(atmel_port->gpio_irq[i], port); 1693ab5e4e41SRichard Genoud } 1694ab5e4e41SRichard Genoud 1695ab5e4e41SRichard Genoud static int atmel_request_gpio_irq(struct uart_port *port) 1696ab5e4e41SRichard Genoud { 1697ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1698ab5e4e41SRichard Genoud int *irq = atmel_port->gpio_irq; 1699ab5e4e41SRichard Genoud enum mctrl_gpio_idx i; 1700ab5e4e41SRichard Genoud int err = 0; 1701ab5e4e41SRichard Genoud 1702ab5e4e41SRichard Genoud for (i = 0; (i < UART_GPIO_MAX) && !err; i++) { 1703ab5e4e41SRichard Genoud if (irq[i] < 0) 1704ab5e4e41SRichard Genoud continue; 1705ab5e4e41SRichard Genoud 1706ab5e4e41SRichard Genoud irq_set_status_flags(irq[i], IRQ_NOAUTOEN); 1707ab5e4e41SRichard Genoud err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH, 1708ab5e4e41SRichard Genoud "atmel_serial", port); 1709ab5e4e41SRichard Genoud if (err) 1710ab5e4e41SRichard Genoud dev_err(port->dev, "atmel_startup - Can't get %d irq\n", 1711ab5e4e41SRichard Genoud irq[i]); 1712ab5e4e41SRichard Genoud } 1713ab5e4e41SRichard Genoud 1714ab5e4e41SRichard Genoud /* 1715ab5e4e41SRichard Genoud * If something went wrong, rollback. 1716ab5e4e41SRichard Genoud */ 1717ab5e4e41SRichard Genoud while (err && (--i >= 0)) 1718ab5e4e41SRichard Genoud if (irq[i] >= 0) 1719ab5e4e41SRichard Genoud free_irq(irq[i], port); 1720ab5e4e41SRichard Genoud 1721ab5e4e41SRichard Genoud return err; 1722ab5e4e41SRichard Genoud } 1723ab5e4e41SRichard Genoud 1724055560b0SElen Song /* 1725ab4382d2SGreg Kroah-Hartman * Perform initialization and enable port for reception 1726ab4382d2SGreg Kroah-Hartman */ 1727ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port) 1728ab4382d2SGreg Kroah-Hartman { 172933d64c4fSElen Song struct platform_device *pdev = to_platform_device(port->dev); 1730ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1731ab4382d2SGreg Kroah-Hartman struct tty_struct *tty = port->state->port.tty; 1732ab4382d2SGreg Kroah-Hartman int retval; 1733ab4382d2SGreg Kroah-Hartman 1734ab4382d2SGreg Kroah-Hartman /* 1735ab4382d2SGreg Kroah-Hartman * Ensure that no interrupts are enabled otherwise when 1736ab4382d2SGreg Kroah-Hartman * request_irq() is called we could get stuck trying to 1737ab4382d2SGreg Kroah-Hartman * handle an unexpected interrupt 1738ab4382d2SGreg Kroah-Hartman */ 1739ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 1740ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = false; 1741ab4382d2SGreg Kroah-Hartman 1742ab4382d2SGreg Kroah-Hartman /* 1743ab4382d2SGreg Kroah-Hartman * Allocate the IRQ 1744ab4382d2SGreg Kroah-Hartman */ 1745ab4382d2SGreg Kroah-Hartman retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, 1746ab4382d2SGreg Kroah-Hartman tty ? tty->name : "atmel_serial", port); 1747ab4382d2SGreg Kroah-Hartman if (retval) { 1748ddaa6037SRichard Genoud dev_err(port->dev, "atmel_startup - Can't get irq\n"); 1749ab4382d2SGreg Kroah-Hartman return retval; 1750ab4382d2SGreg Kroah-Hartman } 1751ab4382d2SGreg Kroah-Hartman 1752ab4382d2SGreg Kroah-Hartman /* 1753ab5e4e41SRichard Genoud * Get the GPIO lines IRQ 1754ab5e4e41SRichard Genoud */ 1755ab5e4e41SRichard Genoud retval = atmel_request_gpio_irq(port); 1756ab5e4e41SRichard Genoud if (retval) 1757ab5e4e41SRichard Genoud goto free_irq; 1758ab5e4e41SRichard Genoud 1759ab5e4e41SRichard Genoud /* 1760ab4382d2SGreg Kroah-Hartman * Initialize DMA (if necessary) 1761ab4382d2SGreg Kroah-Hartman */ 176233d64c4fSElen Song atmel_init_property(atmel_port, pdev); 176333d64c4fSElen Song 1764a930e528SElen Song if (atmel_port->prepare_rx) { 1765a930e528SElen Song retval = atmel_port->prepare_rx(port); 1766a930e528SElen Song if (retval < 0) 1767a930e528SElen Song atmel_set_ops(port); 1768ab4382d2SGreg Kroah-Hartman } 1769ab4382d2SGreg Kroah-Hartman 1770a930e528SElen Song if (atmel_port->prepare_tx) { 1771a930e528SElen Song retval = atmel_port->prepare_tx(port); 1772a930e528SElen Song if (retval < 0) 1773a930e528SElen Song atmel_set_ops(port); 1774ab4382d2SGreg Kroah-Hartman } 1775ab4382d2SGreg Kroah-Hartman 1776ab4382d2SGreg Kroah-Hartman /* Save current CSR for comparison in atmel_tasklet_func() */ 1777e0b0baadSRichard Genoud atmel_port->irq_status_prev = atmel_get_lines_status(port); 1778ab4382d2SGreg Kroah-Hartman atmel_port->irq_status = atmel_port->irq_status_prev; 1779ab4382d2SGreg Kroah-Hartman 1780ab4382d2SGreg Kroah-Hartman /* 1781ab4382d2SGreg Kroah-Hartman * Finally, enable the serial port 1782ab4382d2SGreg Kroah-Hartman */ 1783ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 1784ab4382d2SGreg Kroah-Hartman /* enable xmit & rcvr */ 1785ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 1786ab4382d2SGreg Kroah-Hartman 17872e68c22fSElen Song setup_timer(&atmel_port->uart_timer, 17882e68c22fSElen Song atmel_uart_timer_callback, 17892e68c22fSElen Song (unsigned long)port); 17908bc661bfSMarek Roszko 17918bc661bfSMarek Roszko if (atmel_use_pdc_rx(port)) { 17928bc661bfSMarek Roszko /* set UART timeout */ 17938bc661bfSMarek Roszko if (!atmel_port->is_usart) { 17942e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 17952e68c22fSElen Song jiffies + uart_poll_timeout(port)); 17962e68c22fSElen Song /* set USART timeout */ 17972e68c22fSElen Song } else { 1798ab4382d2SGreg Kroah-Hartman UART_PUT_RTOR(port, PDC_RX_TIMEOUT); 1799ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_STTTO); 1800ab4382d2SGreg Kroah-Hartman 1801ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 18022e68c22fSElen Song } 1803ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 1804ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); 180534df42f5SElen Song } else if (atmel_use_dma_rx(port)) { 18062e68c22fSElen Song /* set UART timeout */ 18072e68c22fSElen Song if (!atmel_port->is_usart) { 18082e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 18092e68c22fSElen Song jiffies + uart_poll_timeout(port)); 18102e68c22fSElen Song /* set USART timeout */ 18112e68c22fSElen Song } else { 181234df42f5SElen Song UART_PUT_RTOR(port, PDC_RX_TIMEOUT); 181334df42f5SElen Song UART_PUT_CR(port, ATMEL_US_STTTO); 181434df42f5SElen Song 181534df42f5SElen Song UART_PUT_IER(port, ATMEL_US_TIMEOUT); 18162e68c22fSElen Song } 1817ab4382d2SGreg Kroah-Hartman } else { 1818ab4382d2SGreg Kroah-Hartman /* enable receive only */ 1819ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, ATMEL_US_RXRDY); 1820ab4382d2SGreg Kroah-Hartman } 1821ab4382d2SGreg Kroah-Hartman 1822ab4382d2SGreg Kroah-Hartman return 0; 1823ab5e4e41SRichard Genoud 1824ab5e4e41SRichard Genoud free_irq: 1825ab5e4e41SRichard Genoud free_irq(port->irq, port); 1826ab5e4e41SRichard Genoud 1827ab5e4e41SRichard Genoud return retval; 1828ab4382d2SGreg Kroah-Hartman } 1829ab4382d2SGreg Kroah-Hartman 1830ab4382d2SGreg Kroah-Hartman /* 1831479e9b94SPeter Hurley * Flush any TX data submitted for DMA. Called when the TX circular 1832479e9b94SPeter Hurley * buffer is reset. 1833479e9b94SPeter Hurley */ 1834479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port) 1835479e9b94SPeter Hurley { 1836479e9b94SPeter Hurley struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1837479e9b94SPeter Hurley 1838479e9b94SPeter Hurley if (atmel_use_pdc_tx(port)) { 1839479e9b94SPeter Hurley UART_PUT_TCR(port, 0); 1840479e9b94SPeter Hurley atmel_port->pdc_tx.ofs = 0; 1841479e9b94SPeter Hurley } 1842479e9b94SPeter Hurley } 1843479e9b94SPeter Hurley 1844479e9b94SPeter Hurley /* 1845ab4382d2SGreg Kroah-Hartman * Disable the port 1846ab4382d2SGreg Kroah-Hartman */ 1847ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port) 1848ab4382d2SGreg Kroah-Hartman { 1849ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 18500cc7c6c7SMarek Roszko 1851ab4382d2SGreg Kroah-Hartman /* 18528bc661bfSMarek Roszko * Prevent any tasklets being scheduled during 18538bc661bfSMarek Roszko * cleanup 18548bc661bfSMarek Roszko */ 18558bc661bfSMarek Roszko del_timer_sync(&atmel_port->uart_timer); 18568bc661bfSMarek Roszko 18578bc661bfSMarek Roszko /* 18580cc7c6c7SMarek Roszko * Clear out any scheduled tasklets before 18590cc7c6c7SMarek Roszko * we destroy the buffers 18600cc7c6c7SMarek Roszko */ 18610cc7c6c7SMarek Roszko tasklet_kill(&atmel_port->tasklet); 18620cc7c6c7SMarek Roszko 18630cc7c6c7SMarek Roszko /* 18640cc7c6c7SMarek Roszko * Ensure everything is stopped and 18650cc7c6c7SMarek Roszko * disable all interrupts, port and break condition. 1866ab4382d2SGreg Kroah-Hartman */ 1867ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 1868ab4382d2SGreg Kroah-Hartman atmel_stop_tx(port); 1869ab4382d2SGreg Kroah-Hartman 18700cc7c6c7SMarek Roszko UART_PUT_CR(port, ATMEL_US_RSTSTA); 18710cc7c6c7SMarek Roszko UART_PUT_IDR(port, -1); 18720cc7c6c7SMarek Roszko 18730cc7c6c7SMarek Roszko 1874ab4382d2SGreg Kroah-Hartman /* 1875ab4382d2SGreg Kroah-Hartman * Shut-down the DMA. 1876ab4382d2SGreg Kroah-Hartman */ 1877a930e528SElen Song if (atmel_port->release_rx) 1878a930e528SElen Song atmel_port->release_rx(port); 1879a930e528SElen Song if (atmel_port->release_tx) 1880a930e528SElen Song atmel_port->release_tx(port); 1881ab4382d2SGreg Kroah-Hartman 1882ab4382d2SGreg Kroah-Hartman /* 1883bb7e73c5SMark Deneen * Reset ring buffer pointers 1884bb7e73c5SMark Deneen */ 1885bb7e73c5SMark Deneen atmel_port->rx_ring.head = 0; 1886bb7e73c5SMark Deneen atmel_port->rx_ring.tail = 0; 1887bb7e73c5SMark Deneen 1888bb7e73c5SMark Deneen /* 1889ab5e4e41SRichard Genoud * Free the interrupts 1890ab4382d2SGreg Kroah-Hartman */ 1891ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 1892ab5e4e41SRichard Genoud atmel_free_gpio_irq(port); 1893ab5e4e41SRichard Genoud 1894ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = false; 1895ab4382d2SGreg Kroah-Hartman 1896479e9b94SPeter Hurley atmel_flush_buffer(port); 1897ab4382d2SGreg Kroah-Hartman } 1898ab4382d2SGreg Kroah-Hartman 1899ab4382d2SGreg Kroah-Hartman /* 1900ab4382d2SGreg Kroah-Hartman * Power / Clock management. 1901ab4382d2SGreg Kroah-Hartman */ 1902ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state, 1903ab4382d2SGreg Kroah-Hartman unsigned int oldstate) 1904ab4382d2SGreg Kroah-Hartman { 1905ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1906ab4382d2SGreg Kroah-Hartman 1907ab4382d2SGreg Kroah-Hartman switch (state) { 1908ab4382d2SGreg Kroah-Hartman case 0: 1909ab4382d2SGreg Kroah-Hartman /* 1910ab4382d2SGreg Kroah-Hartman * Enable the peripheral clock for this serial port. 1911ab4382d2SGreg Kroah-Hartman * This is called on uart_open() or a resume event. 1912ab4382d2SGreg Kroah-Hartman */ 191391f8c2d8SBoris BREZILLON clk_prepare_enable(atmel_port->clk); 1914ab4382d2SGreg Kroah-Hartman 1915ab4382d2SGreg Kroah-Hartman /* re-enable interrupts if we disabled some on suspend */ 1916ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, atmel_port->backup_imr); 1917ab4382d2SGreg Kroah-Hartman break; 1918ab4382d2SGreg Kroah-Hartman case 3: 1919ab4382d2SGreg Kroah-Hartman /* Back up the interrupt mask and disable all interrupts */ 1920ab4382d2SGreg Kroah-Hartman atmel_port->backup_imr = UART_GET_IMR(port); 1921ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 1922ab4382d2SGreg Kroah-Hartman 1923ab4382d2SGreg Kroah-Hartman /* 1924ab4382d2SGreg Kroah-Hartman * Disable the peripheral clock for this serial port. 1925ab4382d2SGreg Kroah-Hartman * This is called on uart_close() or a suspend event. 1926ab4382d2SGreg Kroah-Hartman */ 192791f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 1928ab4382d2SGreg Kroah-Hartman break; 1929ab4382d2SGreg Kroah-Hartman default: 1930ddaa6037SRichard Genoud dev_err(port->dev, "atmel_serial: unknown pm %d\n", state); 1931ab4382d2SGreg Kroah-Hartman } 1932ab4382d2SGreg Kroah-Hartman } 1933ab4382d2SGreg Kroah-Hartman 1934ab4382d2SGreg Kroah-Hartman /* 1935ab4382d2SGreg Kroah-Hartman * Change the port parameters 1936ab4382d2SGreg Kroah-Hartman */ 1937ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, 1938ab4382d2SGreg Kroah-Hartman struct ktermios *old) 1939ab4382d2SGreg Kroah-Hartman { 1940ab4382d2SGreg Kroah-Hartman unsigned long flags; 19411cf6e8fcSCyrille Pitchen unsigned int old_mode, mode, imr, quot, baud; 1942ab4382d2SGreg Kroah-Hartman 19431cf6e8fcSCyrille Pitchen /* save the current mode register */ 19441cf6e8fcSCyrille Pitchen mode = old_mode = UART_GET_MR(port); 19451cf6e8fcSCyrille Pitchen 19461cf6e8fcSCyrille Pitchen /* reset the mode, clock divisor, parity, stop bits and data size */ 19471cf6e8fcSCyrille Pitchen mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP | 19481cf6e8fcSCyrille Pitchen ATMEL_US_PAR | ATMEL_US_USMODE); 1949ab4382d2SGreg Kroah-Hartman 1950ab4382d2SGreg Kroah-Hartman baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); 1951ab4382d2SGreg Kroah-Hartman quot = uart_get_divisor(port, baud); 1952ab4382d2SGreg Kroah-Hartman 1953ab4382d2SGreg Kroah-Hartman if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */ 1954ab4382d2SGreg Kroah-Hartman quot /= 8; 1955ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USCLKS_MCK_DIV8; 1956ab4382d2SGreg Kroah-Hartman } 1957ab4382d2SGreg Kroah-Hartman 1958ab4382d2SGreg Kroah-Hartman /* byte size */ 1959ab4382d2SGreg Kroah-Hartman switch (termios->c_cflag & CSIZE) { 1960ab4382d2SGreg Kroah-Hartman case CS5: 1961ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_5; 1962ab4382d2SGreg Kroah-Hartman break; 1963ab4382d2SGreg Kroah-Hartman case CS6: 1964ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_6; 1965ab4382d2SGreg Kroah-Hartman break; 1966ab4382d2SGreg Kroah-Hartman case CS7: 1967ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_7; 1968ab4382d2SGreg Kroah-Hartman break; 1969ab4382d2SGreg Kroah-Hartman default: 1970ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_8; 1971ab4382d2SGreg Kroah-Hartman break; 1972ab4382d2SGreg Kroah-Hartman } 1973ab4382d2SGreg Kroah-Hartman 1974ab4382d2SGreg Kroah-Hartman /* stop bits */ 1975ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CSTOPB) 1976ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_NBSTOP_2; 1977ab4382d2SGreg Kroah-Hartman 1978ab4382d2SGreg Kroah-Hartman /* parity */ 1979ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARENB) { 1980ab4382d2SGreg Kroah-Hartman /* Mark or Space parity */ 1981ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CMSPAR) { 1982ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARODD) 1983ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_MARK; 1984ab4382d2SGreg Kroah-Hartman else 1985ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_SPACE; 1986ab4382d2SGreg Kroah-Hartman } else if (termios->c_cflag & PARODD) 1987ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_ODD; 1988ab4382d2SGreg Kroah-Hartman else 1989ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_EVEN; 1990ab4382d2SGreg Kroah-Hartman } else 1991ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_NONE; 1992ab4382d2SGreg Kroah-Hartman 1993ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 1994ab4382d2SGreg Kroah-Hartman 1995ab4382d2SGreg Kroah-Hartman port->read_status_mask = ATMEL_US_OVRE; 1996ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & INPCK) 1997ab4382d2SGreg Kroah-Hartman port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 1998ef8b9ddcSPeter Hurley if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 1999ab4382d2SGreg Kroah-Hartman port->read_status_mask |= ATMEL_US_RXBRK; 2000ab4382d2SGreg Kroah-Hartman 200164e22ebeSElen Song if (atmel_use_pdc_rx(port)) 2002ab4382d2SGreg Kroah-Hartman /* need to enable error interrupts */ 2003ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, port->read_status_mask); 2004ab4382d2SGreg Kroah-Hartman 2005ab4382d2SGreg Kroah-Hartman /* 2006ab4382d2SGreg Kroah-Hartman * Characters to ignore 2007ab4382d2SGreg Kroah-Hartman */ 2008ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 2009ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 2010ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2011ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNBRK) { 2012ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_RXBRK; 2013ab4382d2SGreg Kroah-Hartman /* 2014ab4382d2SGreg Kroah-Hartman * If we're ignoring parity and break indicators, 2015ab4382d2SGreg Kroah-Hartman * ignore overruns too (for real raw support). 2016ab4382d2SGreg Kroah-Hartman */ 2017ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 2018ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_OVRE; 2019ab4382d2SGreg Kroah-Hartman } 2020ab4382d2SGreg Kroah-Hartman /* TODO: Ignore all characters if CREAD is set.*/ 2021ab4382d2SGreg Kroah-Hartman 2022ab4382d2SGreg Kroah-Hartman /* update the per-port timeout */ 2023ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, termios->c_cflag, baud); 2024ab4382d2SGreg Kroah-Hartman 2025ab4382d2SGreg Kroah-Hartman /* 2026ab4382d2SGreg Kroah-Hartman * save/disable interrupts. The tty layer will ensure that the 2027ab4382d2SGreg Kroah-Hartman * transmitter is empty if requested by the caller, so there's 2028ab4382d2SGreg Kroah-Hartman * no need to wait for it here. 2029ab4382d2SGreg Kroah-Hartman */ 2030ab4382d2SGreg Kroah-Hartman imr = UART_GET_IMR(port); 2031ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 2032ab4382d2SGreg Kroah-Hartman 2033ab4382d2SGreg Kroah-Hartman /* disable receiver and transmitter */ 2034ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS); 2035ab4382d2SGreg Kroah-Hartman 20361cf6e8fcSCyrille Pitchen /* mode */ 203713bd3e6fSRicardo Ribalda Delgado if (port->rs485.flags & SER_RS485_ENABLED) { 203813bd3e6fSRicardo Ribalda Delgado if ((port->rs485.delay_rts_after_send) > 0) 203913bd3e6fSRicardo Ribalda Delgado UART_PUT_TTGR(port, port->rs485.delay_rts_after_send); 2040ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 20411cf6e8fcSCyrille Pitchen } else if (termios->c_cflag & CRTSCTS) { 20421cf6e8fcSCyrille Pitchen /* RS232 with hardware handshake (RTS/CTS) */ 20431cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_HWHS; 20441cf6e8fcSCyrille Pitchen } else { 20451cf6e8fcSCyrille Pitchen /* RS232 without hadware handshake */ 20461cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_NORMAL; 2047ab4382d2SGreg Kroah-Hartman } 2048ab4382d2SGreg Kroah-Hartman 20491cf6e8fcSCyrille Pitchen /* set the mode, clock divisor, parity, stop bits and data size */ 2050ab4382d2SGreg Kroah-Hartman UART_PUT_MR(port, mode); 2051ab4382d2SGreg Kroah-Hartman 20521cf6e8fcSCyrille Pitchen /* 20531cf6e8fcSCyrille Pitchen * when switching the mode, set the RTS line state according to the 20541cf6e8fcSCyrille Pitchen * new mode, otherwise keep the former state 20551cf6e8fcSCyrille Pitchen */ 20561cf6e8fcSCyrille Pitchen if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) { 20571cf6e8fcSCyrille Pitchen unsigned int rts_state; 20581cf6e8fcSCyrille Pitchen 20591cf6e8fcSCyrille Pitchen if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 20601cf6e8fcSCyrille Pitchen /* let the hardware control the RTS line */ 20611cf6e8fcSCyrille Pitchen rts_state = ATMEL_US_RTSDIS; 20621cf6e8fcSCyrille Pitchen } else { 20631cf6e8fcSCyrille Pitchen /* force RTS line to low level */ 20641cf6e8fcSCyrille Pitchen rts_state = ATMEL_US_RTSEN; 20651cf6e8fcSCyrille Pitchen } 20661cf6e8fcSCyrille Pitchen 20671cf6e8fcSCyrille Pitchen UART_PUT_CR(port, rts_state); 20681cf6e8fcSCyrille Pitchen } 20691cf6e8fcSCyrille Pitchen 2070ab4382d2SGreg Kroah-Hartman /* set the baud rate */ 2071ab4382d2SGreg Kroah-Hartman UART_PUT_BRGR(port, quot); 2072ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 2073ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 2074ab4382d2SGreg Kroah-Hartman 2075ab4382d2SGreg Kroah-Hartman /* restore interrupts */ 2076ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, imr); 2077ab4382d2SGreg Kroah-Hartman 2078ab4382d2SGreg Kroah-Hartman /* CTS flow-control and modem-status interrupts */ 2079ab4382d2SGreg Kroah-Hartman if (UART_ENABLE_MS(port, termios->c_cflag)) 208035b675b9SRichard Genoud atmel_enable_ms(port); 208135b675b9SRichard Genoud else 208235b675b9SRichard Genoud atmel_disable_ms(port); 2083ab4382d2SGreg Kroah-Hartman 2084ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 2085ab4382d2SGreg Kroah-Hartman } 2086ab4382d2SGreg Kroah-Hartman 2087732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios) 208842bd7a4fSViktar Palstsiuk { 2089732a84a0SPeter Hurley if (termios->c_line == N_PPS) { 209042bd7a4fSViktar Palstsiuk port->flags |= UPF_HARDPPS_CD; 2091d41510ceSPeter Hurley spin_lock_irq(&port->lock); 209242bd7a4fSViktar Palstsiuk atmel_enable_ms(port); 2093d41510ceSPeter Hurley spin_unlock_irq(&port->lock); 209442bd7a4fSViktar Palstsiuk } else { 209542bd7a4fSViktar Palstsiuk port->flags &= ~UPF_HARDPPS_CD; 2096cab68f89SPeter Hurley if (!UART_ENABLE_MS(port, termios->c_cflag)) { 2097cab68f89SPeter Hurley spin_lock_irq(&port->lock); 2098cab68f89SPeter Hurley atmel_disable_ms(port); 2099cab68f89SPeter Hurley spin_unlock_irq(&port->lock); 2100cab68f89SPeter Hurley } 210142bd7a4fSViktar Palstsiuk } 210242bd7a4fSViktar Palstsiuk } 210342bd7a4fSViktar Palstsiuk 2104ab4382d2SGreg Kroah-Hartman /* 2105ab4382d2SGreg Kroah-Hartman * Return string describing the specified port 2106ab4382d2SGreg Kroah-Hartman */ 2107ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port) 2108ab4382d2SGreg Kroah-Hartman { 2109ab4382d2SGreg Kroah-Hartman return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; 2110ab4382d2SGreg Kroah-Hartman } 2111ab4382d2SGreg Kroah-Hartman 2112ab4382d2SGreg Kroah-Hartman /* 2113ab4382d2SGreg Kroah-Hartman * Release the memory region(s) being used by 'port'. 2114ab4382d2SGreg Kroah-Hartman */ 2115ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port) 2116ab4382d2SGreg Kroah-Hartman { 2117ab4382d2SGreg Kroah-Hartman struct platform_device *pdev = to_platform_device(port->dev); 2118ab4382d2SGreg Kroah-Hartman int size = pdev->resource[0].end - pdev->resource[0].start + 1; 2119ab4382d2SGreg Kroah-Hartman 2120ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2121ab4382d2SGreg Kroah-Hartman 2122ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2123ab4382d2SGreg Kroah-Hartman iounmap(port->membase); 2124ab4382d2SGreg Kroah-Hartman port->membase = NULL; 2125ab4382d2SGreg Kroah-Hartman } 2126ab4382d2SGreg Kroah-Hartman } 2127ab4382d2SGreg Kroah-Hartman 2128ab4382d2SGreg Kroah-Hartman /* 2129ab4382d2SGreg Kroah-Hartman * Request the memory region(s) being used by 'port'. 2130ab4382d2SGreg Kroah-Hartman */ 2131ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port) 2132ab4382d2SGreg Kroah-Hartman { 2133ab4382d2SGreg Kroah-Hartman struct platform_device *pdev = to_platform_device(port->dev); 2134ab4382d2SGreg Kroah-Hartman int size = pdev->resource[0].end - pdev->resource[0].start + 1; 2135ab4382d2SGreg Kroah-Hartman 2136ab4382d2SGreg Kroah-Hartman if (!request_mem_region(port->mapbase, size, "atmel_serial")) 2137ab4382d2SGreg Kroah-Hartman return -EBUSY; 2138ab4382d2SGreg Kroah-Hartman 2139ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2140ab4382d2SGreg Kroah-Hartman port->membase = ioremap(port->mapbase, size); 2141ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2142ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2143ab4382d2SGreg Kroah-Hartman return -ENOMEM; 2144ab4382d2SGreg Kroah-Hartman } 2145ab4382d2SGreg Kroah-Hartman } 2146ab4382d2SGreg Kroah-Hartman 2147ab4382d2SGreg Kroah-Hartman return 0; 2148ab4382d2SGreg Kroah-Hartman } 2149ab4382d2SGreg Kroah-Hartman 2150ab4382d2SGreg Kroah-Hartman /* 2151ab4382d2SGreg Kroah-Hartman * Configure/autoconfigure the port. 2152ab4382d2SGreg Kroah-Hartman */ 2153ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags) 2154ab4382d2SGreg Kroah-Hartman { 2155ab4382d2SGreg Kroah-Hartman if (flags & UART_CONFIG_TYPE) { 2156ab4382d2SGreg Kroah-Hartman port->type = PORT_ATMEL; 2157ab4382d2SGreg Kroah-Hartman atmel_request_port(port); 2158ab4382d2SGreg Kroah-Hartman } 2159ab4382d2SGreg Kroah-Hartman } 2160ab4382d2SGreg Kroah-Hartman 2161ab4382d2SGreg Kroah-Hartman /* 2162ab4382d2SGreg Kroah-Hartman * Verify the new serial_struct (for TIOCSSERIAL). 2163ab4382d2SGreg Kroah-Hartman */ 2164ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser) 2165ab4382d2SGreg Kroah-Hartman { 2166ab4382d2SGreg Kroah-Hartman int ret = 0; 2167ab4382d2SGreg Kroah-Hartman if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) 2168ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2169ab4382d2SGreg Kroah-Hartman if (port->irq != ser->irq) 2170ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2171ab4382d2SGreg Kroah-Hartman if (ser->io_type != SERIAL_IO_MEM) 2172ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2173ab4382d2SGreg Kroah-Hartman if (port->uartclk / 16 != ser->baud_base) 2174ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2175ab4382d2SGreg Kroah-Hartman if ((void *)port->mapbase != ser->iomem_base) 2176ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2177ab4382d2SGreg Kroah-Hartman if (port->iobase != ser->port) 2178ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2179ab4382d2SGreg Kroah-Hartman if (ser->hub6 != 0) 2180ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2181ab4382d2SGreg Kroah-Hartman return ret; 2182ab4382d2SGreg Kroah-Hartman } 2183ab4382d2SGreg Kroah-Hartman 2184ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2185ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port) 2186ab4382d2SGreg Kroah-Hartman { 2187ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY)) 2188ab4382d2SGreg Kroah-Hartman cpu_relax(); 2189ab4382d2SGreg Kroah-Hartman 2190ab4382d2SGreg Kroah-Hartman return UART_GET_CHAR(port); 2191ab4382d2SGreg Kroah-Hartman } 2192ab4382d2SGreg Kroah-Hartman 2193ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch) 2194ab4382d2SGreg Kroah-Hartman { 2195ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY)) 2196ab4382d2SGreg Kroah-Hartman cpu_relax(); 2197ab4382d2SGreg Kroah-Hartman 2198ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, ch); 2199ab4382d2SGreg Kroah-Hartman } 2200ab4382d2SGreg Kroah-Hartman #endif 2201ab4382d2SGreg Kroah-Hartman 2202ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = { 2203ab4382d2SGreg Kroah-Hartman .tx_empty = atmel_tx_empty, 2204ab4382d2SGreg Kroah-Hartman .set_mctrl = atmel_set_mctrl, 2205ab4382d2SGreg Kroah-Hartman .get_mctrl = atmel_get_mctrl, 2206ab4382d2SGreg Kroah-Hartman .stop_tx = atmel_stop_tx, 2207ab4382d2SGreg Kroah-Hartman .start_tx = atmel_start_tx, 2208ab4382d2SGreg Kroah-Hartman .stop_rx = atmel_stop_rx, 2209ab4382d2SGreg Kroah-Hartman .enable_ms = atmel_enable_ms, 2210ab4382d2SGreg Kroah-Hartman .break_ctl = atmel_break_ctl, 2211ab4382d2SGreg Kroah-Hartman .startup = atmel_startup, 2212ab4382d2SGreg Kroah-Hartman .shutdown = atmel_shutdown, 2213ab4382d2SGreg Kroah-Hartman .flush_buffer = atmel_flush_buffer, 2214ab4382d2SGreg Kroah-Hartman .set_termios = atmel_set_termios, 221542bd7a4fSViktar Palstsiuk .set_ldisc = atmel_set_ldisc, 2216ab4382d2SGreg Kroah-Hartman .type = atmel_type, 2217ab4382d2SGreg Kroah-Hartman .release_port = atmel_release_port, 2218ab4382d2SGreg Kroah-Hartman .request_port = atmel_request_port, 2219ab4382d2SGreg Kroah-Hartman .config_port = atmel_config_port, 2220ab4382d2SGreg Kroah-Hartman .verify_port = atmel_verify_port, 2221ab4382d2SGreg Kroah-Hartman .pm = atmel_serial_pm, 2222ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2223ab4382d2SGreg Kroah-Hartman .poll_get_char = atmel_poll_get_char, 2224ab4382d2SGreg Kroah-Hartman .poll_put_char = atmel_poll_put_char, 2225ab4382d2SGreg Kroah-Hartman #endif 2226ab4382d2SGreg Kroah-Hartman }; 2227ab4382d2SGreg Kroah-Hartman 2228ab4382d2SGreg Kroah-Hartman /* 2229ab4382d2SGreg Kroah-Hartman * Configure the port from the platform device resource info. 2230ab4382d2SGreg Kroah-Hartman */ 223191f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port, 2232ab4382d2SGreg Kroah-Hartman struct platform_device *pdev) 2233ab4382d2SGreg Kroah-Hartman { 223491f8c2d8SBoris BREZILLON int ret; 2235ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_port->uart; 2236574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 2237ab4382d2SGreg Kroah-Hartman 223833d64c4fSElen Song if (!atmel_init_property(atmel_port, pdev)) 2239a930e528SElen Song atmel_set_ops(port); 2240a930e528SElen Song 224113bd3e6fSRicardo Ribalda Delgado atmel_init_rs485(port, pdev); 224233d64c4fSElen Song 2243ab4382d2SGreg Kroah-Hartman port->iotype = UPIO_MEM; 2244ab4382d2SGreg Kroah-Hartman port->flags = UPF_BOOT_AUTOCONF; 2245ab4382d2SGreg Kroah-Hartman port->ops = &atmel_pops; 2246ab4382d2SGreg Kroah-Hartman port->fifosize = 1; 2247ab4382d2SGreg Kroah-Hartman port->dev = &pdev->dev; 2248ab4382d2SGreg Kroah-Hartman port->mapbase = pdev->resource[0].start; 2249ab4382d2SGreg Kroah-Hartman port->irq = pdev->resource[1].start; 225013bd3e6fSRicardo Ribalda Delgado port->rs485_config = atmel_config_rs485; 2251ab4382d2SGreg Kroah-Hartman 2252ab4382d2SGreg Kroah-Hartman tasklet_init(&atmel_port->tasklet, atmel_tasklet_func, 2253ab4382d2SGreg Kroah-Hartman (unsigned long)port); 2254ab4382d2SGreg Kroah-Hartman 2255ab4382d2SGreg Kroah-Hartman memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); 2256ab4382d2SGreg Kroah-Hartman 22575fbe46b6SNicolas Ferre if (pdata && pdata->regs) { 2258ab4382d2SGreg Kroah-Hartman /* Already mapped by setup code */ 22591acfc7ecSNicolas Ferre port->membase = pdata->regs; 2260588edbf3SNicolas Ferre } else { 2261ab4382d2SGreg Kroah-Hartman port->flags |= UPF_IOREMAP; 2262ab4382d2SGreg Kroah-Hartman port->membase = NULL; 2263ab4382d2SGreg Kroah-Hartman } 2264ab4382d2SGreg Kroah-Hartman 2265ab4382d2SGreg Kroah-Hartman /* for console, the clock could already be configured */ 2266ab4382d2SGreg Kroah-Hartman if (!atmel_port->clk) { 2267ab4382d2SGreg Kroah-Hartman atmel_port->clk = clk_get(&pdev->dev, "usart"); 226891f8c2d8SBoris BREZILLON if (IS_ERR(atmel_port->clk)) { 226991f8c2d8SBoris BREZILLON ret = PTR_ERR(atmel_port->clk); 227091f8c2d8SBoris BREZILLON atmel_port->clk = NULL; 227191f8c2d8SBoris BREZILLON return ret; 227291f8c2d8SBoris BREZILLON } 227391f8c2d8SBoris BREZILLON ret = clk_prepare_enable(atmel_port->clk); 227491f8c2d8SBoris BREZILLON if (ret) { 227591f8c2d8SBoris BREZILLON clk_put(atmel_port->clk); 227691f8c2d8SBoris BREZILLON atmel_port->clk = NULL; 227791f8c2d8SBoris BREZILLON return ret; 227891f8c2d8SBoris BREZILLON } 2279ab4382d2SGreg Kroah-Hartman port->uartclk = clk_get_rate(atmel_port->clk); 228091f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 2281ab4382d2SGreg Kroah-Hartman /* only enable clock when USART is in use */ 2282ab4382d2SGreg Kroah-Hartman } 2283ab4382d2SGreg Kroah-Hartman 2284ab4382d2SGreg Kroah-Hartman /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ 228513bd3e6fSRicardo Ribalda Delgado if (port->rs485.flags & SER_RS485_ENABLED) 2286ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 228764e22ebeSElen Song else if (atmel_use_pdc_tx(port)) { 2288ab4382d2SGreg Kroah-Hartman port->fifosize = PDC_BUFFER_SIZE; 2289ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; 2290ab4382d2SGreg Kroah-Hartman } else { 2291ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 2292ab4382d2SGreg Kroah-Hartman } 229391f8c2d8SBoris BREZILLON 229491f8c2d8SBoris BREZILLON return 0; 2295ab4382d2SGreg Kroah-Hartman } 2296ab4382d2SGreg Kroah-Hartman 229769f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device; /* the serial console device */ 229869f6a27bSJean-Christophe PLAGNIOL-VILLARD 2299ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2300ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch) 2301ab4382d2SGreg Kroah-Hartman { 2302ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY)) 2303ab4382d2SGreg Kroah-Hartman cpu_relax(); 2304ab4382d2SGreg Kroah-Hartman UART_PUT_CHAR(port, ch); 2305ab4382d2SGreg Kroah-Hartman } 2306ab4382d2SGreg Kroah-Hartman 2307ab4382d2SGreg Kroah-Hartman /* 2308ab4382d2SGreg Kroah-Hartman * Interrupts are disabled on entering 2309ab4382d2SGreg Kroah-Hartman */ 2310ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count) 2311ab4382d2SGreg Kroah-Hartman { 2312ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2313ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2314ab4382d2SGreg Kroah-Hartman unsigned int status, imr; 2315ab4382d2SGreg Kroah-Hartman unsigned int pdc_tx; 2316ab4382d2SGreg Kroah-Hartman 2317ab4382d2SGreg Kroah-Hartman /* 2318ab4382d2SGreg Kroah-Hartman * First, save IMR and then disable interrupts 2319ab4382d2SGreg Kroah-Hartman */ 2320ab4382d2SGreg Kroah-Hartman imr = UART_GET_IMR(port); 2321ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask); 2322ab4382d2SGreg Kroah-Hartman 2323ab4382d2SGreg Kroah-Hartman /* Store PDC transmit status and disable it */ 2324ab4382d2SGreg Kroah-Hartman pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN; 2325ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 2326ab4382d2SGreg Kroah-Hartman 2327ab4382d2SGreg Kroah-Hartman uart_console_write(port, s, count, atmel_console_putchar); 2328ab4382d2SGreg Kroah-Hartman 2329ab4382d2SGreg Kroah-Hartman /* 2330ab4382d2SGreg Kroah-Hartman * Finally, wait for transmitter to become empty 2331ab4382d2SGreg Kroah-Hartman * and restore IMR 2332ab4382d2SGreg Kroah-Hartman */ 2333ab4382d2SGreg Kroah-Hartman do { 2334ab4382d2SGreg Kroah-Hartman status = UART_GET_CSR(port); 2335ab4382d2SGreg Kroah-Hartman } while (!(status & ATMEL_US_TXRDY)); 2336ab4382d2SGreg Kroah-Hartman 2337ab4382d2SGreg Kroah-Hartman /* Restore PDC transmit status */ 2338ab4382d2SGreg Kroah-Hartman if (pdc_tx) 2339ab4382d2SGreg Kroah-Hartman UART_PUT_PTCR(port, ATMEL_PDC_TXTEN); 2340ab4382d2SGreg Kroah-Hartman 2341ab4382d2SGreg Kroah-Hartman /* set interrupts back the way they were */ 2342ab4382d2SGreg Kroah-Hartman UART_PUT_IER(port, imr); 2343ab4382d2SGreg Kroah-Hartman } 2344ab4382d2SGreg Kroah-Hartman 2345ab4382d2SGreg Kroah-Hartman /* 2346ab4382d2SGreg Kroah-Hartman * If the port was already initialised (eg, by a boot loader), 2347ab4382d2SGreg Kroah-Hartman * try to determine the current setup. 2348ab4382d2SGreg Kroah-Hartman */ 2349ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud, 2350ab4382d2SGreg Kroah-Hartman int *parity, int *bits) 2351ab4382d2SGreg Kroah-Hartman { 2352ab4382d2SGreg Kroah-Hartman unsigned int mr, quot; 2353ab4382d2SGreg Kroah-Hartman 2354ab4382d2SGreg Kroah-Hartman /* 2355ab4382d2SGreg Kroah-Hartman * If the baud rate generator isn't running, the port wasn't 2356ab4382d2SGreg Kroah-Hartman * initialized by the boot loader. 2357ab4382d2SGreg Kroah-Hartman */ 2358ab4382d2SGreg Kroah-Hartman quot = UART_GET_BRGR(port) & ATMEL_US_CD; 2359ab4382d2SGreg Kroah-Hartman if (!quot) 2360ab4382d2SGreg Kroah-Hartman return; 2361ab4382d2SGreg Kroah-Hartman 2362ab4382d2SGreg Kroah-Hartman mr = UART_GET_MR(port) & ATMEL_US_CHRL; 2363ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_CHRL_8) 2364ab4382d2SGreg Kroah-Hartman *bits = 8; 2365ab4382d2SGreg Kroah-Hartman else 2366ab4382d2SGreg Kroah-Hartman *bits = 7; 2367ab4382d2SGreg Kroah-Hartman 2368ab4382d2SGreg Kroah-Hartman mr = UART_GET_MR(port) & ATMEL_US_PAR; 2369ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_PAR_EVEN) 2370ab4382d2SGreg Kroah-Hartman *parity = 'e'; 2371ab4382d2SGreg Kroah-Hartman else if (mr == ATMEL_US_PAR_ODD) 2372ab4382d2SGreg Kroah-Hartman *parity = 'o'; 2373ab4382d2SGreg Kroah-Hartman 2374ab4382d2SGreg Kroah-Hartman /* 2375ab4382d2SGreg Kroah-Hartman * The serial core only rounds down when matching this to a 2376ab4382d2SGreg Kroah-Hartman * supported baud rate. Make sure we don't end up slightly 2377ab4382d2SGreg Kroah-Hartman * lower than one of those, as it would make us fall through 2378ab4382d2SGreg Kroah-Hartman * to a much lower baud rate than we really want. 2379ab4382d2SGreg Kroah-Hartman */ 2380ab4382d2SGreg Kroah-Hartman *baud = port->uartclk / (16 * (quot - 1)); 2381ab4382d2SGreg Kroah-Hartman } 2382ab4382d2SGreg Kroah-Hartman 2383ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options) 2384ab4382d2SGreg Kroah-Hartman { 238591f8c2d8SBoris BREZILLON int ret; 2386ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2387ab4382d2SGreg Kroah-Hartman int baud = 115200; 2388ab4382d2SGreg Kroah-Hartman int bits = 8; 2389ab4382d2SGreg Kroah-Hartman int parity = 'n'; 2390ab4382d2SGreg Kroah-Hartman int flow = 'n'; 2391ab4382d2SGreg Kroah-Hartman 2392ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2393ab4382d2SGreg Kroah-Hartman /* Port not initialized yet - delay setup */ 2394ab4382d2SGreg Kroah-Hartman return -ENODEV; 2395ab4382d2SGreg Kroah-Hartman } 2396ab4382d2SGreg Kroah-Hartman 239791f8c2d8SBoris BREZILLON ret = clk_prepare_enable(atmel_ports[co->index].clk); 239891f8c2d8SBoris BREZILLON if (ret) 239991f8c2d8SBoris BREZILLON return ret; 2400ab4382d2SGreg Kroah-Hartman 2401ab4382d2SGreg Kroah-Hartman UART_PUT_IDR(port, -1); 2402ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 2403ab4382d2SGreg Kroah-Hartman UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 2404ab4382d2SGreg Kroah-Hartman 2405ab4382d2SGreg Kroah-Hartman if (options) 2406ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 2407ab4382d2SGreg Kroah-Hartman else 2408ab4382d2SGreg Kroah-Hartman atmel_console_get_options(port, &baud, &parity, &bits); 2409ab4382d2SGreg Kroah-Hartman 2410ab4382d2SGreg Kroah-Hartman return uart_set_options(port, co, baud, parity, bits, flow); 2411ab4382d2SGreg Kroah-Hartman } 2412ab4382d2SGreg Kroah-Hartman 2413ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart; 2414ab4382d2SGreg Kroah-Hartman 2415ab4382d2SGreg Kroah-Hartman static struct console atmel_console = { 2416ab4382d2SGreg Kroah-Hartman .name = ATMEL_DEVICENAME, 2417ab4382d2SGreg Kroah-Hartman .write = atmel_console_write, 2418ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 2419ab4382d2SGreg Kroah-Hartman .setup = atmel_console_setup, 2420ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 2421ab4382d2SGreg Kroah-Hartman .index = -1, 2422ab4382d2SGreg Kroah-Hartman .data = &atmel_uart, 2423ab4382d2SGreg Kroah-Hartman }; 2424ab4382d2SGreg Kroah-Hartman 2425ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE (&atmel_console) 2426ab4382d2SGreg Kroah-Hartman 2427ab4382d2SGreg Kroah-Hartman /* 2428ab4382d2SGreg Kroah-Hartman * Early console initialization (before VM subsystem initialized). 2429ab4382d2SGreg Kroah-Hartman */ 2430ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void) 2431ab4382d2SGreg Kroah-Hartman { 243291f8c2d8SBoris BREZILLON int ret; 2433ab4382d2SGreg Kroah-Hartman if (atmel_default_console_device) { 24340d0a3cc1SVoss, Nikolaus struct atmel_uart_data *pdata = 2435574de559SJingoo Han dev_get_platdata(&atmel_default_console_device->dev); 2436efb8d21bSLinus Torvalds int id = pdata->num; 24374cbf9f48SNicolas Ferre struct atmel_uart_port *port = &atmel_ports[id]; 24380d0a3cc1SVoss, Nikolaus 24394cbf9f48SNicolas Ferre port->backup_imr = 0; 24404cbf9f48SNicolas Ferre port->uart.line = id; 24414cbf9f48SNicolas Ferre 24424cbf9f48SNicolas Ferre add_preferred_console(ATMEL_DEVICENAME, id, NULL); 244391f8c2d8SBoris BREZILLON ret = atmel_init_port(port, atmel_default_console_device); 244491f8c2d8SBoris BREZILLON if (ret) 244591f8c2d8SBoris BREZILLON return ret; 2446ab4382d2SGreg Kroah-Hartman register_console(&atmel_console); 2447ab4382d2SGreg Kroah-Hartman } 2448ab4382d2SGreg Kroah-Hartman 2449ab4382d2SGreg Kroah-Hartman return 0; 2450ab4382d2SGreg Kroah-Hartman } 2451ab4382d2SGreg Kroah-Hartman 2452ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init); 2453ab4382d2SGreg Kroah-Hartman 2454ab4382d2SGreg Kroah-Hartman /* 2455ab4382d2SGreg Kroah-Hartman * Late console initialization. 2456ab4382d2SGreg Kroah-Hartman */ 2457ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void) 2458ab4382d2SGreg Kroah-Hartman { 2459ab4382d2SGreg Kroah-Hartman if (atmel_default_console_device 2460ab4382d2SGreg Kroah-Hartman && !(atmel_console.flags & CON_ENABLED)) 2461ab4382d2SGreg Kroah-Hartman register_console(&atmel_console); 2462ab4382d2SGreg Kroah-Hartman 2463ab4382d2SGreg Kroah-Hartman return 0; 2464ab4382d2SGreg Kroah-Hartman } 2465ab4382d2SGreg Kroah-Hartman 2466ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init); 2467ab4382d2SGreg Kroah-Hartman 2468ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 2469ab4382d2SGreg Kroah-Hartman { 2470ab4382d2SGreg Kroah-Hartman return port->cons && port->cons->index == port->line; 2471ab4382d2SGreg Kroah-Hartman } 2472ab4382d2SGreg Kroah-Hartman 2473ab4382d2SGreg Kroah-Hartman #else 2474ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE NULL 2475ab4382d2SGreg Kroah-Hartman 2476ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 2477ab4382d2SGreg Kroah-Hartman { 2478ab4382d2SGreg Kroah-Hartman return false; 2479ab4382d2SGreg Kroah-Hartman } 2480ab4382d2SGreg Kroah-Hartman #endif 2481ab4382d2SGreg Kroah-Hartman 2482ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = { 2483ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 2484ab4382d2SGreg Kroah-Hartman .driver_name = "atmel_serial", 2485ab4382d2SGreg Kroah-Hartman .dev_name = ATMEL_DEVICENAME, 2486ab4382d2SGreg Kroah-Hartman .major = SERIAL_ATMEL_MAJOR, 2487ab4382d2SGreg Kroah-Hartman .minor = MINOR_START, 2488ab4382d2SGreg Kroah-Hartman .nr = ATMEL_MAX_UART, 2489ab4382d2SGreg Kroah-Hartman .cons = ATMEL_CONSOLE_DEVICE, 2490ab4382d2SGreg Kroah-Hartman }; 2491ab4382d2SGreg Kroah-Hartman 2492ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM 2493ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void) 2494ab4382d2SGreg Kroah-Hartman { 2495ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91 2496ab4382d2SGreg Kroah-Hartman return at91_suspend_entering_slow_clock(); 2497ab4382d2SGreg Kroah-Hartman #else 2498ab4382d2SGreg Kroah-Hartman return false; 2499ab4382d2SGreg Kroah-Hartman #endif 2500ab4382d2SGreg Kroah-Hartman } 2501ab4382d2SGreg Kroah-Hartman 2502ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev, 2503ab4382d2SGreg Kroah-Hartman pm_message_t state) 2504ab4382d2SGreg Kroah-Hartman { 2505ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2506ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2507ab4382d2SGreg Kroah-Hartman 2508ab4382d2SGreg Kroah-Hartman if (atmel_is_console_port(port) && console_suspend_enabled) { 2509ab4382d2SGreg Kroah-Hartman /* Drain the TX shifter */ 2510ab4382d2SGreg Kroah-Hartman while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) 2511ab4382d2SGreg Kroah-Hartman cpu_relax(); 2512ab4382d2SGreg Kroah-Hartman } 2513ab4382d2SGreg Kroah-Hartman 2514ab4382d2SGreg Kroah-Hartman /* we can not wake up if we're running on slow clock */ 2515ab4382d2SGreg Kroah-Hartman atmel_port->may_wakeup = device_may_wakeup(&pdev->dev); 2516ab4382d2SGreg Kroah-Hartman if (atmel_serial_clk_will_stop()) 2517ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, 0); 2518ab4382d2SGreg Kroah-Hartman 2519ab4382d2SGreg Kroah-Hartman uart_suspend_port(&atmel_uart, port); 2520ab4382d2SGreg Kroah-Hartman 2521ab4382d2SGreg Kroah-Hartman return 0; 2522ab4382d2SGreg Kroah-Hartman } 2523ab4382d2SGreg Kroah-Hartman 2524ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev) 2525ab4382d2SGreg Kroah-Hartman { 2526ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2527ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2528ab4382d2SGreg Kroah-Hartman 2529ab4382d2SGreg Kroah-Hartman uart_resume_port(&atmel_uart, port); 2530ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup); 2531ab4382d2SGreg Kroah-Hartman 2532ab4382d2SGreg Kroah-Hartman return 0; 2533ab4382d2SGreg Kroah-Hartman } 2534ab4382d2SGreg Kroah-Hartman #else 2535ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL 2536ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL 2537ab4382d2SGreg Kroah-Hartman #endif 2538ab4382d2SGreg Kroah-Hartman 2539e0b0baadSRichard Genoud static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev) 2540e0b0baadSRichard Genoud { 2541ab5e4e41SRichard Genoud enum mctrl_gpio_idx i; 2542ab5e4e41SRichard Genoud struct gpio_desc *gpiod; 2543ab5e4e41SRichard Genoud 2544e0b0baadSRichard Genoud p->gpios = mctrl_gpio_init(dev, 0); 2545722ccf41SUwe Kleine-König if (IS_ERR(p->gpios)) 2546722ccf41SUwe Kleine-König return PTR_ERR(p->gpios); 2547e0b0baadSRichard Genoud 2548ab5e4e41SRichard Genoud for (i = 0; i < UART_GPIO_MAX; i++) { 2549ab5e4e41SRichard Genoud gpiod = mctrl_gpio_to_gpiod(p->gpios, i); 2550ab5e4e41SRichard Genoud if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN)) 2551ab5e4e41SRichard Genoud p->gpio_irq[i] = gpiod_to_irq(gpiod); 2552ab5e4e41SRichard Genoud else 2553ab5e4e41SRichard Genoud p->gpio_irq[i] = -EINVAL; 2554ab5e4e41SRichard Genoud } 2555ab5e4e41SRichard Genoud 2556e0b0baadSRichard Genoud return 0; 2557e0b0baadSRichard Genoud } 2558e0b0baadSRichard Genoud 25599671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev) 2560ab4382d2SGreg Kroah-Hartman { 2561ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *port; 25625fbe46b6SNicolas Ferre struct device_node *np = pdev->dev.of_node; 2563574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 2564ab4382d2SGreg Kroah-Hartman void *data; 25654cbf9f48SNicolas Ferre int ret = -ENODEV; 2566bd737f87SRicardo Ribalda Delgado bool rs485_enabled; 2567ab4382d2SGreg Kroah-Hartman 2568ab4382d2SGreg Kroah-Hartman BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); 2569ab4382d2SGreg Kroah-Hartman 25705fbe46b6SNicolas Ferre if (np) 25715fbe46b6SNicolas Ferre ret = of_alias_get_id(np, "serial"); 25725fbe46b6SNicolas Ferre else 25734cbf9f48SNicolas Ferre if (pdata) 25744cbf9f48SNicolas Ferre ret = pdata->num; 25754cbf9f48SNicolas Ferre 25764cbf9f48SNicolas Ferre if (ret < 0) 25775fbe46b6SNicolas Ferre /* port id not found in platform data nor device-tree aliases: 25784cbf9f48SNicolas Ferre * auto-enumerate it */ 2579503bded9SPawel Wieczorkiewicz ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART); 25804cbf9f48SNicolas Ferre 2581503bded9SPawel Wieczorkiewicz if (ret >= ATMEL_MAX_UART) { 25824cbf9f48SNicolas Ferre ret = -ENODEV; 25834cbf9f48SNicolas Ferre goto err; 25844cbf9f48SNicolas Ferre } 25854cbf9f48SNicolas Ferre 2586503bded9SPawel Wieczorkiewicz if (test_and_set_bit(ret, atmel_ports_in_use)) { 25874cbf9f48SNicolas Ferre /* port already in use */ 25884cbf9f48SNicolas Ferre ret = -EBUSY; 25894cbf9f48SNicolas Ferre goto err; 25904cbf9f48SNicolas Ferre } 25914cbf9f48SNicolas Ferre 25924cbf9f48SNicolas Ferre port = &atmel_ports[ret]; 2593ab4382d2SGreg Kroah-Hartman port->backup_imr = 0; 25944cbf9f48SNicolas Ferre port->uart.line = ret; 2595354e57f3SLinus Walleij 2596e0b0baadSRichard Genoud ret = atmel_init_gpios(port, &pdev->dev); 2597722ccf41SUwe Kleine-König if (ret < 0) { 2598722ccf41SUwe Kleine-König dev_err(&pdev->dev, "Failed to initialize GPIOs."); 2599722ccf41SUwe Kleine-König goto err; 2600722ccf41SUwe Kleine-König } 2601ab4382d2SGreg Kroah-Hartman 260291f8c2d8SBoris BREZILLON ret = atmel_init_port(port, pdev); 260391f8c2d8SBoris BREZILLON if (ret) 26046fbb9bdfSCyrille Pitchen goto err_clear_bit; 2605ab4382d2SGreg Kroah-Hartman 260664e22ebeSElen Song if (!atmel_use_pdc_rx(&port->uart)) { 2607ab4382d2SGreg Kroah-Hartman ret = -ENOMEM; 2608ab4382d2SGreg Kroah-Hartman data = kmalloc(sizeof(struct atmel_uart_char) 2609ab4382d2SGreg Kroah-Hartman * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); 2610ab4382d2SGreg Kroah-Hartman if (!data) 2611ab4382d2SGreg Kroah-Hartman goto err_alloc_ring; 2612ab4382d2SGreg Kroah-Hartman port->rx_ring.buf = data; 2613ab4382d2SGreg Kroah-Hartman } 2614ab4382d2SGreg Kroah-Hartman 2615bd737f87SRicardo Ribalda Delgado rs485_enabled = port->uart.rs485.flags & SER_RS485_ENABLED; 2616bd737f87SRicardo Ribalda Delgado 2617ab4382d2SGreg Kroah-Hartman ret = uart_add_one_port(&atmel_uart, &port->uart); 2618ab4382d2SGreg Kroah-Hartman if (ret) 2619ab4382d2SGreg Kroah-Hartman goto err_add_port; 2620ab4382d2SGreg Kroah-Hartman 2621ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2622ab4382d2SGreg Kroah-Hartman if (atmel_is_console_port(&port->uart) 2623ab4382d2SGreg Kroah-Hartman && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) { 2624ab4382d2SGreg Kroah-Hartman /* 2625ab4382d2SGreg Kroah-Hartman * The serial core enabled the clock for us, so undo 262691f8c2d8SBoris BREZILLON * the clk_prepare_enable() in atmel_console_setup() 2627ab4382d2SGreg Kroah-Hartman */ 262891f8c2d8SBoris BREZILLON clk_disable_unprepare(port->clk); 2629ab4382d2SGreg Kroah-Hartman } 2630ab4382d2SGreg Kroah-Hartman #endif 2631ab4382d2SGreg Kroah-Hartman 2632ab4382d2SGreg Kroah-Hartman device_init_wakeup(&pdev->dev, 1); 2633ab4382d2SGreg Kroah-Hartman platform_set_drvdata(pdev, port); 2634ab4382d2SGreg Kroah-Hartman 2635d4f64187SCyrille Pitchen /* 2636d4f64187SCyrille Pitchen * The peripheral clock has been disabled by atmel_init_port(): 2637d4f64187SCyrille Pitchen * enable it before accessing I/O registers 2638d4f64187SCyrille Pitchen */ 2639d4f64187SCyrille Pitchen clk_prepare_enable(port->clk); 2640d4f64187SCyrille Pitchen 2641bd737f87SRicardo Ribalda Delgado if (rs485_enabled) { 2642fc887b15SLinus Torvalds UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL); 2643fc887b15SLinus Torvalds UART_PUT_CR(&port->uart, ATMEL_US_RTSEN); 2644fc887b15SLinus Torvalds } 2645fc887b15SLinus Torvalds 2646055560b0SElen Song /* 2647055560b0SElen Song * Get port name of usart or uart 2648055560b0SElen Song */ 2649892db58bSNicolas Ferre atmel_get_ip_name(&port->uart); 2650055560b0SElen Song 2651d4f64187SCyrille Pitchen /* 2652d4f64187SCyrille Pitchen * The peripheral clock can now safely be disabled till the port 2653d4f64187SCyrille Pitchen * is used 2654d4f64187SCyrille Pitchen */ 2655d4f64187SCyrille Pitchen clk_disable_unprepare(port->clk); 2656d4f64187SCyrille Pitchen 2657ab4382d2SGreg Kroah-Hartman return 0; 2658ab4382d2SGreg Kroah-Hartman 2659ab4382d2SGreg Kroah-Hartman err_add_port: 2660ab4382d2SGreg Kroah-Hartman kfree(port->rx_ring.buf); 2661ab4382d2SGreg Kroah-Hartman port->rx_ring.buf = NULL; 2662ab4382d2SGreg Kroah-Hartman err_alloc_ring: 2663ab4382d2SGreg Kroah-Hartman if (!atmel_is_console_port(&port->uart)) { 2664ab4382d2SGreg Kroah-Hartman clk_put(port->clk); 2665ab4382d2SGreg Kroah-Hartman port->clk = NULL; 2666ab4382d2SGreg Kroah-Hartman } 26676fbb9bdfSCyrille Pitchen err_clear_bit: 26686fbb9bdfSCyrille Pitchen clear_bit(port->uart.line, atmel_ports_in_use); 26694cbf9f48SNicolas Ferre err: 2670ab4382d2SGreg Kroah-Hartman return ret; 2671ab4382d2SGreg Kroah-Hartman } 2672ab4382d2SGreg Kroah-Hartman 2673ae8d8a14SBill Pemberton static int atmel_serial_remove(struct platform_device *pdev) 2674ab4382d2SGreg Kroah-Hartman { 2675ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2676ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2677ab4382d2SGreg Kroah-Hartman int ret = 0; 2678ab4382d2SGreg Kroah-Hartman 2679f50c995fSMarek Roszko tasklet_kill(&atmel_port->tasklet); 2680f50c995fSMarek Roszko 2681ab4382d2SGreg Kroah-Hartman device_init_wakeup(&pdev->dev, 0); 2682ab4382d2SGreg Kroah-Hartman 2683ab4382d2SGreg Kroah-Hartman ret = uart_remove_one_port(&atmel_uart, port); 2684ab4382d2SGreg Kroah-Hartman 2685ab4382d2SGreg Kroah-Hartman kfree(atmel_port->rx_ring.buf); 2686ab4382d2SGreg Kroah-Hartman 2687ab4382d2SGreg Kroah-Hartman /* "port" is allocated statically, so we shouldn't free it */ 2688ab4382d2SGreg Kroah-Hartman 2689503bded9SPawel Wieczorkiewicz clear_bit(port->line, atmel_ports_in_use); 26904cbf9f48SNicolas Ferre 2691ab4382d2SGreg Kroah-Hartman clk_put(atmel_port->clk); 2692ab4382d2SGreg Kroah-Hartman 2693ab4382d2SGreg Kroah-Hartman return ret; 2694ab4382d2SGreg Kroah-Hartman } 2695ab4382d2SGreg Kroah-Hartman 2696ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = { 2697ab4382d2SGreg Kroah-Hartman .probe = atmel_serial_probe, 26982d47b716SBill Pemberton .remove = atmel_serial_remove, 2699ab4382d2SGreg Kroah-Hartman .suspend = atmel_serial_suspend, 2700ab4382d2SGreg Kroah-Hartman .resume = atmel_serial_resume, 2701ab4382d2SGreg Kroah-Hartman .driver = { 2702ab4382d2SGreg Kroah-Hartman .name = "atmel_usart", 27035fbe46b6SNicolas Ferre .of_match_table = of_match_ptr(atmel_serial_dt_ids), 2704ab4382d2SGreg Kroah-Hartman }, 2705ab4382d2SGreg Kroah-Hartman }; 2706ab4382d2SGreg Kroah-Hartman 2707ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void) 2708ab4382d2SGreg Kroah-Hartman { 2709ab4382d2SGreg Kroah-Hartman int ret; 2710ab4382d2SGreg Kroah-Hartman 2711ab4382d2SGreg Kroah-Hartman ret = uart_register_driver(&atmel_uart); 2712ab4382d2SGreg Kroah-Hartman if (ret) 2713ab4382d2SGreg Kroah-Hartman return ret; 2714ab4382d2SGreg Kroah-Hartman 2715ab4382d2SGreg Kroah-Hartman ret = platform_driver_register(&atmel_serial_driver); 2716ab4382d2SGreg Kroah-Hartman if (ret) 2717ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&atmel_uart); 2718ab4382d2SGreg Kroah-Hartman 2719ab4382d2SGreg Kroah-Hartman return ret; 2720ab4382d2SGreg Kroah-Hartman } 2721ab4382d2SGreg Kroah-Hartman 2722ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void) 2723ab4382d2SGreg Kroah-Hartman { 2724ab4382d2SGreg Kroah-Hartman platform_driver_unregister(&atmel_serial_driver); 2725ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&atmel_uart); 2726ab4382d2SGreg Kroah-Hartman } 2727ab4382d2SGreg Kroah-Hartman 2728ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init); 2729ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit); 2730ab4382d2SGreg Kroah-Hartman 2731ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson"); 2732ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver"); 2733ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 2734ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart"); 2735