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/tty.h> 26ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h> 27ab4382d2SGreg Kroah-Hartman #include <linux/slab.h> 28ab4382d2SGreg Kroah-Hartman #include <linux/init.h> 29ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> 30ab4382d2SGreg Kroah-Hartman #include <linux/clk.h> 31ab4382d2SGreg Kroah-Hartman #include <linux/console.h> 32ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h> 33ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h> 34ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h> 355fbe46b6SNicolas Ferre #include <linux/of.h> 365fbe46b6SNicolas Ferre #include <linux/of_device.h> 37354e57f3SLinus Walleij #include <linux/of_gpio.h> 38ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h> 396b997babSVinod Koul #include <linux/dmaengine.h> 40ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h> 41ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h> 42ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h> 43bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h> 442e68c22fSElen Song #include <linux/timer.h> 45354e57f3SLinus Walleij #include <linux/gpio.h> 46e0b0baadSRichard Genoud #include <linux/gpio/consumer.h> 47e0b0baadSRichard Genoud #include <linux/err.h> 48ab5e4e41SRichard Genoud #include <linux/irq.h> 492c7af5baSBoris BREZILLON #include <linux/suspend.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 58b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */ 59b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE 8 60b5199d46SCyrille Pitchen /* 61b5199d46SCyrille Pitchen * These two offsets are substracted from the RX FIFO size to define the RTS 62b5199d46SCyrille Pitchen * high and low thresholds 63b5199d46SCyrille Pitchen */ 64b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET 16 65b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET 20 66b5199d46SCyrille Pitchen 67ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 68ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ 69ab4382d2SGreg Kroah-Hartman #endif 70ab4382d2SGreg Kroah-Hartman 71ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 72ab4382d2SGreg Kroah-Hartman 73e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h" 74e0b0baadSRichard Genoud 75ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port); 76ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port); 77ab4382d2SGreg Kroah-Hartman 78ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT 79ab4382d2SGreg Kroah-Hartman 80ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we 81ab4382d2SGreg Kroah-Hartman * should coexist with the 8250 driver, such as if we have an external 16C550 82ab4382d2SGreg Kroah-Hartman * UART. */ 83ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR 204 84ab4382d2SGreg Kroah-Hartman #define MINOR_START 154 85ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyAT" 86ab4382d2SGreg Kroah-Hartman 87ab4382d2SGreg Kroah-Hartman #else 88ab4382d2SGreg Kroah-Hartman 89ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port 90ab4382d2SGreg Kroah-Hartman * name, but it is legally reserved for the 8250 driver. */ 91ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR TTY_MAJOR 92ab4382d2SGreg Kroah-Hartman #define MINOR_START 64 93ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyS" 94ab4382d2SGreg Kroah-Hartman 95ab4382d2SGreg Kroah-Hartman #endif 96ab4382d2SGreg Kroah-Hartman 97ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT 256 98ab4382d2SGreg Kroah-Hartman 99ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer { 100ab4382d2SGreg Kroah-Hartman unsigned char *buf; 101ab4382d2SGreg Kroah-Hartman dma_addr_t dma_addr; 102ab4382d2SGreg Kroah-Hartman unsigned int dma_size; 103ab4382d2SGreg Kroah-Hartman unsigned int ofs; 104ab4382d2SGreg Kroah-Hartman }; 105ab4382d2SGreg Kroah-Hartman 106ab4382d2SGreg Kroah-Hartman struct atmel_uart_char { 107ab4382d2SGreg Kroah-Hartman u16 status; 108ab4382d2SGreg Kroah-Hartman u16 ch; 109ab4382d2SGreg Kroah-Hartman }; 110ab4382d2SGreg Kroah-Hartman 111637ba54fSLudovic Desroches /* 112637ba54fSLudovic Desroches * Be careful, the real size of the ring buffer is 113637ba54fSLudovic Desroches * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer 114637ba54fSLudovic Desroches * can contain up to 1024 characters in PIO mode and up to 4096 characters in 115637ba54fSLudovic Desroches * DMA mode. 116637ba54fSLudovic Desroches */ 117ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024 118ab4382d2SGreg Kroah-Hartman 119ab4382d2SGreg Kroah-Hartman /* 1209af92fbfSAlexandre Belloni * at91: 6 USARTs and one DBGU port (SAM9260) 1219af92fbfSAlexandre Belloni * avr32: 4 122*432f9748SAlexandre Belloni * samx7: 3 USARTs and 5 UARTs 1239af92fbfSAlexandre Belloni */ 124*432f9748SAlexandre Belloni #define ATMEL_MAX_UART 8 1259af92fbfSAlexandre Belloni 1269af92fbfSAlexandre Belloni /* 127ab4382d2SGreg Kroah-Hartman * We wrap our port structure around the generic uart_port. 128ab4382d2SGreg Kroah-Hartman */ 129ab4382d2SGreg Kroah-Hartman struct atmel_uart_port { 130ab4382d2SGreg Kroah-Hartman struct uart_port uart; /* uart */ 131ab4382d2SGreg Kroah-Hartman struct clk *clk; /* uart clock */ 132ab4382d2SGreg Kroah-Hartman int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */ 133ab4382d2SGreg Kroah-Hartman u32 backup_imr; /* IMR saved during suspend */ 134ab4382d2SGreg Kroah-Hartman int break_active; /* break being received */ 135ab4382d2SGreg Kroah-Hartman 13634df42f5SElen Song bool use_dma_rx; /* enable DMA receiver */ 13764e22ebeSElen Song bool use_pdc_rx; /* enable PDC receiver */ 138ab4382d2SGreg Kroah-Hartman short pdc_rx_idx; /* current PDC RX buffer */ 139ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ 140ab4382d2SGreg Kroah-Hartman 14108f738beSElen Song bool use_dma_tx; /* enable DMA transmitter */ 14264e22ebeSElen Song bool use_pdc_tx; /* enable PDC transmitter */ 143ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ 144ab4382d2SGreg Kroah-Hartman 14508f738beSElen Song spinlock_t lock_tx; /* port lock */ 14634df42f5SElen Song spinlock_t lock_rx; /* port lock */ 14708f738beSElen Song struct dma_chan *chan_tx; 14834df42f5SElen Song struct dma_chan *chan_rx; 14908f738beSElen Song struct dma_async_tx_descriptor *desc_tx; 15034df42f5SElen Song struct dma_async_tx_descriptor *desc_rx; 15108f738beSElen Song dma_cookie_t cookie_tx; 15234df42f5SElen Song dma_cookie_t cookie_rx; 15308f738beSElen Song struct scatterlist sg_tx; 15434df42f5SElen Song struct scatterlist sg_rx; 15500e8e658SNicolas Ferre struct tasklet_struct tasklet_rx; 15600e8e658SNicolas Ferre struct tasklet_struct tasklet_tx; 15798f2082cSNicolas Ferre atomic_t tasklet_shutdown; 158ab4382d2SGreg Kroah-Hartman unsigned int irq_status_prev; 1595f258b3eSCyrille Pitchen unsigned int tx_len; 160ab4382d2SGreg Kroah-Hartman 161ab4382d2SGreg Kroah-Hartman struct circ_buf rx_ring; 162ab4382d2SGreg Kroah-Hartman 163e0b0baadSRichard Genoud struct mctrl_gpios *gpios; 164ab4382d2SGreg Kroah-Hartman unsigned int tx_done_mask; 165b5199d46SCyrille Pitchen u32 fifo_size; 166b5199d46SCyrille Pitchen u32 rts_high; 167b5199d46SCyrille Pitchen u32 rts_low; 168ab5e4e41SRichard Genoud bool ms_irq_enabled; 1692958cceeSLudovic Desroches u32 rtor; /* address of receiver timeout register if it exists */ 1705bf5635aSLudovic Desroches bool has_frac_baudrate; 1714b769371SNicolas Ferre bool has_hw_timer; 1724b769371SNicolas Ferre struct timer_list uart_timer; 1732c7af5baSBoris BREZILLON 1742c7af5baSBoris BREZILLON bool suspended; 1752c7af5baSBoris BREZILLON unsigned int pending; 1762c7af5baSBoris BREZILLON unsigned int pending_status; 1772c7af5baSBoris BREZILLON spinlock_t lock_suspended; 1782c7af5baSBoris BREZILLON 1796a5f0e2fSAlexandre Belloni struct { 1806a5f0e2fSAlexandre Belloni u32 cr; 1816a5f0e2fSAlexandre Belloni u32 mr; 1826a5f0e2fSAlexandre Belloni u32 imr; 1836a5f0e2fSAlexandre Belloni u32 brgr; 1846a5f0e2fSAlexandre Belloni u32 rtor; 1856a5f0e2fSAlexandre Belloni u32 ttgr; 1866a5f0e2fSAlexandre Belloni u32 fmr; 1876a5f0e2fSAlexandre Belloni u32 fimr; 1886a5f0e2fSAlexandre Belloni } cache; 1896a5f0e2fSAlexandre Belloni 190a930e528SElen Song int (*prepare_rx)(struct uart_port *port); 191a930e528SElen Song int (*prepare_tx)(struct uart_port *port); 192a930e528SElen Song void (*schedule_rx)(struct uart_port *port); 193a930e528SElen Song void (*schedule_tx)(struct uart_port *port); 194a930e528SElen Song void (*release_rx)(struct uart_port *port); 195a930e528SElen Song void (*release_tx)(struct uart_port *port); 196ab4382d2SGreg Kroah-Hartman }; 197ab4382d2SGreg Kroah-Hartman 198ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; 199503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART); 200ab4382d2SGreg Kroah-Hartman 201ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ 202ab4382d2SGreg Kroah-Hartman static struct console atmel_console; 203ab4382d2SGreg Kroah-Hartman #endif 204ab4382d2SGreg Kroah-Hartman 2055fbe46b6SNicolas Ferre #if defined(CONFIG_OF) 2065fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = { 2075fbe46b6SNicolas Ferre { .compatible = "atmel,at91rm9200-usart" }, 2085fbe46b6SNicolas Ferre { .compatible = "atmel,at91sam9260-usart" }, 2095fbe46b6SNicolas Ferre { /* sentinel */ } 2105fbe46b6SNicolas Ferre }; 2115fbe46b6SNicolas Ferre #endif 2125fbe46b6SNicolas Ferre 213ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port * 214ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart) 215ab4382d2SGreg Kroah-Hartman { 216ab4382d2SGreg Kroah-Hartman return container_of(uart, struct atmel_uart_port, uart); 217ab4382d2SGreg Kroah-Hartman } 218ab4382d2SGreg Kroah-Hartman 2194e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg) 2204e7decdaSCyrille Pitchen { 2214e7decdaSCyrille Pitchen return __raw_readl(port->membase + reg); 2224e7decdaSCyrille Pitchen } 2234e7decdaSCyrille Pitchen 2244e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value) 2254e7decdaSCyrille Pitchen { 2264e7decdaSCyrille Pitchen __raw_writel(value, port->membase + reg); 2274e7decdaSCyrille Pitchen } 2284e7decdaSCyrille Pitchen 229a6499435SCyrille Pitchen #ifdef CONFIG_AVR32 230a6499435SCyrille Pitchen 231a6499435SCyrille Pitchen /* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */ 232a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port) 233b5199d46SCyrille Pitchen { 234a6499435SCyrille Pitchen return __raw_readl(port->membase + ATMEL_US_RHR); 235b5199d46SCyrille Pitchen } 236b5199d46SCyrille Pitchen 237a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value) 238b5199d46SCyrille Pitchen { 239a6499435SCyrille Pitchen __raw_writel(value, port->membase + ATMEL_US_THR); 240b5199d46SCyrille Pitchen } 241b5199d46SCyrille Pitchen 242a6499435SCyrille Pitchen #else 243a6499435SCyrille Pitchen 244a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port) 245a6499435SCyrille Pitchen { 246a6499435SCyrille Pitchen return __raw_readb(port->membase + ATMEL_US_RHR); 247a6499435SCyrille Pitchen } 248a6499435SCyrille Pitchen 249a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value) 250a6499435SCyrille Pitchen { 251a6499435SCyrille Pitchen __raw_writeb(value, port->membase + ATMEL_US_THR); 252a6499435SCyrille Pitchen } 253a6499435SCyrille Pitchen 254a6499435SCyrille Pitchen #endif 255a6499435SCyrille Pitchen 256ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC 25764e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port) 258ab4382d2SGreg Kroah-Hartman { 259ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 260ab4382d2SGreg Kroah-Hartman 26164e22ebeSElen Song return atmel_port->use_pdc_rx; 262ab4382d2SGreg Kroah-Hartman } 263ab4382d2SGreg Kroah-Hartman 26464e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port) 265ab4382d2SGreg Kroah-Hartman { 266ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 267ab4382d2SGreg Kroah-Hartman 26864e22ebeSElen Song return atmel_port->use_pdc_tx; 269ab4382d2SGreg Kroah-Hartman } 270ab4382d2SGreg Kroah-Hartman #else 27164e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port) 272ab4382d2SGreg Kroah-Hartman { 273ab4382d2SGreg Kroah-Hartman return false; 274ab4382d2SGreg Kroah-Hartman } 275ab4382d2SGreg Kroah-Hartman 27664e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port) 277ab4382d2SGreg Kroah-Hartman { 278ab4382d2SGreg Kroah-Hartman return false; 279ab4382d2SGreg Kroah-Hartman } 280ab4382d2SGreg Kroah-Hartman #endif 281ab4382d2SGreg Kroah-Hartman 28208f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port) 28308f738beSElen Song { 28408f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 28508f738beSElen Song 28608f738beSElen Song return atmel_port->use_dma_tx; 28708f738beSElen Song } 28808f738beSElen Song 28934df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port) 29034df42f5SElen Song { 29134df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 29234df42f5SElen Song 29334df42f5SElen Song return atmel_port->use_dma_rx; 29434df42f5SElen Song } 29534df42f5SElen Song 2965be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port) 2975be605acSAlexandre Belloni { 2985be605acSAlexandre Belloni struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2995be605acSAlexandre Belloni 3005be605acSAlexandre Belloni return atmel_port->fifo_size; 3015be605acSAlexandre Belloni } 3025be605acSAlexandre Belloni 30398f2082cSNicolas Ferre static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port, 30498f2082cSNicolas Ferre struct tasklet_struct *t) 30598f2082cSNicolas Ferre { 30698f2082cSNicolas Ferre if (!atomic_read(&atmel_port->tasklet_shutdown)) 30798f2082cSNicolas Ferre tasklet_schedule(t); 30898f2082cSNicolas Ferre } 30998f2082cSNicolas Ferre 310e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port) 311e0b0baadSRichard Genoud { 312e0b0baadSRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 313e0b0baadSRichard Genoud unsigned int status, ret = 0; 314e0b0baadSRichard Genoud 3154e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 316e0b0baadSRichard Genoud 317e0b0baadSRichard Genoud mctrl_gpio_get(atmel_port->gpios, &ret); 318e0b0baadSRichard Genoud 319e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 320e0b0baadSRichard Genoud UART_GPIO_CTS))) { 321e0b0baadSRichard Genoud if (ret & TIOCM_CTS) 322e0b0baadSRichard Genoud status &= ~ATMEL_US_CTS; 323e0b0baadSRichard Genoud else 324e0b0baadSRichard Genoud status |= ATMEL_US_CTS; 325e0b0baadSRichard Genoud } 326e0b0baadSRichard Genoud 327e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 328e0b0baadSRichard Genoud UART_GPIO_DSR))) { 329e0b0baadSRichard Genoud if (ret & TIOCM_DSR) 330e0b0baadSRichard Genoud status &= ~ATMEL_US_DSR; 331e0b0baadSRichard Genoud else 332e0b0baadSRichard Genoud status |= ATMEL_US_DSR; 333e0b0baadSRichard Genoud } 334e0b0baadSRichard Genoud 335e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 336e0b0baadSRichard Genoud UART_GPIO_RI))) { 337e0b0baadSRichard Genoud if (ret & TIOCM_RI) 338e0b0baadSRichard Genoud status &= ~ATMEL_US_RI; 339e0b0baadSRichard Genoud else 340e0b0baadSRichard Genoud status |= ATMEL_US_RI; 341e0b0baadSRichard Genoud } 342e0b0baadSRichard Genoud 343e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 344e0b0baadSRichard Genoud UART_GPIO_DCD))) { 345e0b0baadSRichard Genoud if (ret & TIOCM_CD) 346e0b0baadSRichard Genoud status &= ~ATMEL_US_DCD; 347e0b0baadSRichard Genoud else 348e0b0baadSRichard Genoud status |= ATMEL_US_DCD; 349e0b0baadSRichard Genoud } 350e0b0baadSRichard Genoud 351e0b0baadSRichard Genoud return status; 352e0b0baadSRichard Genoud } 353e0b0baadSRichard Genoud 354ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */ 35513bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port, 35613bd3e6fSRicardo Ribalda Delgado struct serial_rs485 *rs485conf) 357ab4382d2SGreg Kroah-Hartman { 358ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 359ab4382d2SGreg Kroah-Hartman unsigned int mode; 360ab4382d2SGreg Kroah-Hartman 361ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 3624e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 363ab4382d2SGreg Kroah-Hartman 3644e7decdaSCyrille Pitchen mode = atmel_uart_readl(port, ATMEL_US_MR); 365ab4382d2SGreg Kroah-Hartman 366ab4382d2SGreg Kroah-Hartman /* Resetting serial mode to RS232 (0x0) */ 367ab4382d2SGreg Kroah-Hartman mode &= ~ATMEL_US_USMODE; 368ab4382d2SGreg Kroah-Hartman 36913bd3e6fSRicardo Ribalda Delgado port->rs485 = *rs485conf; 370ab4382d2SGreg Kroah-Hartman 371ab4382d2SGreg Kroah-Hartman if (rs485conf->flags & SER_RS485_ENABLED) { 372ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS485\n"); 373ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 3744e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 3754e7decdaSCyrille Pitchen rs485conf->delay_rts_after_send); 376ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 377ab4382d2SGreg Kroah-Hartman } else { 378ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS232\n"); 37964e22ebeSElen Song if (atmel_use_pdc_tx(port)) 380ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | 381ab4382d2SGreg Kroah-Hartman ATMEL_US_TXBUFE; 382ab4382d2SGreg Kroah-Hartman else 383ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 384ab4382d2SGreg Kroah-Hartman } 3854e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 386ab4382d2SGreg Kroah-Hartman 387ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 3884e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 389ab4382d2SGreg Kroah-Hartman 39013bd3e6fSRicardo Ribalda Delgado return 0; 391ab4382d2SGreg Kroah-Hartman } 392ab4382d2SGreg Kroah-Hartman 393ab4382d2SGreg Kroah-Hartman /* 394ab4382d2SGreg Kroah-Hartman * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. 395ab4382d2SGreg Kroah-Hartman */ 396ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port) 397ab4382d2SGreg Kroah-Hartman { 3984e7decdaSCyrille Pitchen return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ? 3994e7decdaSCyrille Pitchen TIOCSER_TEMT : 4004e7decdaSCyrille Pitchen 0; 401ab4382d2SGreg Kroah-Hartman } 402ab4382d2SGreg Kroah-Hartman 403ab4382d2SGreg Kroah-Hartman /* 404ab4382d2SGreg Kroah-Hartman * Set state of the modem control output lines 405ab4382d2SGreg Kroah-Hartman */ 406ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) 407ab4382d2SGreg Kroah-Hartman { 408ab4382d2SGreg Kroah-Hartman unsigned int control = 0; 4094e7decdaSCyrille Pitchen unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR); 4101cf6e8fcSCyrille Pitchen unsigned int rts_paused, rts_ready; 411ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 412ab4382d2SGreg Kroah-Hartman 4131cf6e8fcSCyrille Pitchen /* override mode to RS485 if needed, otherwise keep the current mode */ 4141cf6e8fcSCyrille Pitchen if (port->rs485.flags & SER_RS485_ENABLED) { 4154e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 4164e7decdaSCyrille Pitchen port->rs485.delay_rts_after_send); 4171cf6e8fcSCyrille Pitchen mode &= ~ATMEL_US_USMODE; 4181cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_RS485; 4191cf6e8fcSCyrille Pitchen } 4201cf6e8fcSCyrille Pitchen 4211cf6e8fcSCyrille Pitchen /* set the RTS line state according to the mode */ 4221cf6e8fcSCyrille Pitchen if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 4231cf6e8fcSCyrille Pitchen /* force RTS line to high level */ 4241cf6e8fcSCyrille Pitchen rts_paused = ATMEL_US_RTSEN; 4251cf6e8fcSCyrille Pitchen 4261cf6e8fcSCyrille Pitchen /* give the control of the RTS line back to the hardware */ 4271cf6e8fcSCyrille Pitchen rts_ready = ATMEL_US_RTSDIS; 4281cf6e8fcSCyrille Pitchen } else { 4291cf6e8fcSCyrille Pitchen /* force RTS line to high level */ 4301cf6e8fcSCyrille Pitchen rts_paused = ATMEL_US_RTSDIS; 4311cf6e8fcSCyrille Pitchen 4321cf6e8fcSCyrille Pitchen /* force RTS line to low level */ 4331cf6e8fcSCyrille Pitchen rts_ready = ATMEL_US_RTSEN; 4341cf6e8fcSCyrille Pitchen } 4351cf6e8fcSCyrille Pitchen 436ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_RTS) 4371cf6e8fcSCyrille Pitchen control |= rts_ready; 438ab4382d2SGreg Kroah-Hartman else 4391cf6e8fcSCyrille Pitchen control |= rts_paused; 440ab4382d2SGreg Kroah-Hartman 441ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_DTR) 442ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTREN; 443ab4382d2SGreg Kroah-Hartman else 444ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTRDIS; 445ab4382d2SGreg Kroah-Hartman 4464e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, control); 447ab4382d2SGreg Kroah-Hartman 448e0b0baadSRichard Genoud mctrl_gpio_set(atmel_port->gpios, mctrl); 449e0b0baadSRichard Genoud 450ab4382d2SGreg Kroah-Hartman /* Local loopback mode? */ 4511cf6e8fcSCyrille Pitchen mode &= ~ATMEL_US_CHMODE; 452ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_LOOP) 453ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_LOC_LOOP; 454ab4382d2SGreg Kroah-Hartman else 455ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_NORMAL; 456ab4382d2SGreg Kroah-Hartman 4574e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 458ab4382d2SGreg Kroah-Hartman } 459ab4382d2SGreg Kroah-Hartman 460ab4382d2SGreg Kroah-Hartman /* 461ab4382d2SGreg Kroah-Hartman * Get state of the modem control input lines 462ab4382d2SGreg Kroah-Hartman */ 463ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port) 464ab4382d2SGreg Kroah-Hartman { 465e0b0baadSRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 466e0b0baadSRichard Genoud unsigned int ret = 0, status; 467ab4382d2SGreg Kroah-Hartman 4684e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 469ab4382d2SGreg Kroah-Hartman 470ab4382d2SGreg Kroah-Hartman /* 471ab4382d2SGreg Kroah-Hartman * The control signals are active low. 472ab4382d2SGreg Kroah-Hartman */ 473ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DCD)) 474ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CD; 475ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_CTS)) 476ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CTS; 477ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DSR)) 478ab4382d2SGreg Kroah-Hartman ret |= TIOCM_DSR; 479ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_RI)) 480ab4382d2SGreg Kroah-Hartman ret |= TIOCM_RI; 481ab4382d2SGreg Kroah-Hartman 482e0b0baadSRichard Genoud return mctrl_gpio_get(atmel_port->gpios, &ret); 483ab4382d2SGreg Kroah-Hartman } 484ab4382d2SGreg Kroah-Hartman 485ab4382d2SGreg Kroah-Hartman /* 486ab4382d2SGreg Kroah-Hartman * Stop transmitting. 487ab4382d2SGreg Kroah-Hartman */ 488ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port) 489ab4382d2SGreg Kroah-Hartman { 490ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 491ab4382d2SGreg Kroah-Hartman 49264e22ebeSElen Song if (atmel_use_pdc_tx(port)) { 493ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 4944e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 495ab4382d2SGreg Kroah-Hartman } 49689d82324SRichard Genoud 49789d82324SRichard Genoud /* 49889d82324SRichard Genoud * Disable the transmitter. 49989d82324SRichard Genoud * This is mandatory when DMA is used, otherwise the DMA buffer 50089d82324SRichard Genoud * is fully transmitted. 50189d82324SRichard Genoud */ 50289d82324SRichard Genoud atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS); 50389d82324SRichard Genoud 504ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 5054e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 506ab4382d2SGreg Kroah-Hartman 50713bd3e6fSRicardo Ribalda Delgado if ((port->rs485.flags & SER_RS485_ENABLED) && 50813bd3e6fSRicardo Ribalda Delgado !(port->rs485.flags & SER_RS485_RX_DURING_TX)) 509ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 510ab4382d2SGreg Kroah-Hartman } 511ab4382d2SGreg Kroah-Hartman 512ab4382d2SGreg Kroah-Hartman /* 513ab4382d2SGreg Kroah-Hartman * Start transmitting. 514ab4382d2SGreg Kroah-Hartman */ 515ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port) 516ab4382d2SGreg Kroah-Hartman { 517ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 518ab4382d2SGreg Kroah-Hartman 5190058f087SAlexandre Belloni if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR) 5200058f087SAlexandre Belloni & ATMEL_PDC_TXTEN)) 521ab4382d2SGreg Kroah-Hartman /* The transmitter is already running. Yes, we 522ab4382d2SGreg Kroah-Hartman really need this.*/ 523ab4382d2SGreg Kroah-Hartman return; 524ab4382d2SGreg Kroah-Hartman 5250058f087SAlexandre Belloni if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port)) 52613bd3e6fSRicardo Ribalda Delgado if ((port->rs485.flags & SER_RS485_ENABLED) && 52713bd3e6fSRicardo Ribalda Delgado !(port->rs485.flags & SER_RS485_RX_DURING_TX)) 528ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 529ab4382d2SGreg Kroah-Hartman 5300058f087SAlexandre Belloni if (atmel_use_pdc_tx(port)) 531ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 5324e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 5330058f087SAlexandre Belloni 534ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 5354e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 53689d82324SRichard Genoud 53789d82324SRichard Genoud /* re-enable the transmitter */ 53889d82324SRichard Genoud atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN); 539ab4382d2SGreg Kroah-Hartman } 540ab4382d2SGreg Kroah-Hartman 541ab4382d2SGreg Kroah-Hartman /* 542ab4382d2SGreg Kroah-Hartman * start receiving - port is in process of being opened. 543ab4382d2SGreg Kroah-Hartman */ 544ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port) 545ab4382d2SGreg Kroah-Hartman { 5464e7decdaSCyrille Pitchen /* reset status and receiver */ 5474e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 548ab4382d2SGreg Kroah-Hartman 5494e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN); 55057c36868SSiftar, Gabe 55164e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 552ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 5534e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 5544e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 555ab4382d2SGreg Kroah-Hartman port->read_status_mask); 5564e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); 557ab4382d2SGreg Kroah-Hartman } else { 5584e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY); 559ab4382d2SGreg Kroah-Hartman } 560ab4382d2SGreg Kroah-Hartman } 561ab4382d2SGreg Kroah-Hartman 562ab4382d2SGreg Kroah-Hartman /* 563ab4382d2SGreg Kroah-Hartman * Stop receiving - port is in process of being closed. 564ab4382d2SGreg Kroah-Hartman */ 565ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port) 566ab4382d2SGreg Kroah-Hartman { 5674e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS); 56857c36868SSiftar, Gabe 56964e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 570ab4382d2SGreg Kroah-Hartman /* disable PDC receive */ 5714e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS); 5724e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 5734e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 574ab4382d2SGreg Kroah-Hartman port->read_status_mask); 575ab4382d2SGreg Kroah-Hartman } else { 5764e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY); 577ab4382d2SGreg Kroah-Hartman } 578ab4382d2SGreg Kroah-Hartman } 579ab4382d2SGreg Kroah-Hartman 580ab4382d2SGreg Kroah-Hartman /* 581ab4382d2SGreg Kroah-Hartman * Enable modem status interrupts 582ab4382d2SGreg Kroah-Hartman */ 583ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port) 584ab4382d2SGreg Kroah-Hartman { 585ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 586ab5e4e41SRichard Genoud uint32_t ier = 0; 587ab5e4e41SRichard Genoud 588ab5e4e41SRichard Genoud /* 589ab5e4e41SRichard Genoud * Interrupt should not be enabled twice 590ab5e4e41SRichard Genoud */ 591ab5e4e41SRichard Genoud if (atmel_port->ms_irq_enabled) 592ab5e4e41SRichard Genoud return; 593ab5e4e41SRichard Genoud 594ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = true; 595ab5e4e41SRichard Genoud 59618dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) 597ab5e4e41SRichard Genoud ier |= ATMEL_US_CTSIC; 598ab5e4e41SRichard Genoud 59918dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) 600ab5e4e41SRichard Genoud ier |= ATMEL_US_DSRIC; 601ab5e4e41SRichard Genoud 60218dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) 603ab5e4e41SRichard Genoud ier |= ATMEL_US_RIIC; 604ab5e4e41SRichard Genoud 60518dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) 606ab5e4e41SRichard Genoud ier |= ATMEL_US_DCDIC; 607ab5e4e41SRichard Genoud 6084e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ier); 60918dfef9cSUwe Kleine-König 61018dfef9cSUwe Kleine-König mctrl_gpio_enable_ms(atmel_port->gpios); 611ab4382d2SGreg Kroah-Hartman } 612ab4382d2SGreg Kroah-Hartman 613ab4382d2SGreg Kroah-Hartman /* 61435b675b9SRichard Genoud * Disable modem status interrupts 61535b675b9SRichard Genoud */ 61635b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port) 61735b675b9SRichard Genoud { 61835b675b9SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 61935b675b9SRichard Genoud uint32_t idr = 0; 62035b675b9SRichard Genoud 62135b675b9SRichard Genoud /* 62235b675b9SRichard Genoud * Interrupt should not be disabled twice 62335b675b9SRichard Genoud */ 62435b675b9SRichard Genoud if (!atmel_port->ms_irq_enabled) 62535b675b9SRichard Genoud return; 62635b675b9SRichard Genoud 62735b675b9SRichard Genoud atmel_port->ms_irq_enabled = false; 62835b675b9SRichard Genoud 62918dfef9cSUwe Kleine-König mctrl_gpio_disable_ms(atmel_port->gpios); 63018dfef9cSUwe Kleine-König 63118dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) 63235b675b9SRichard Genoud idr |= ATMEL_US_CTSIC; 63335b675b9SRichard Genoud 63418dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) 63535b675b9SRichard Genoud idr |= ATMEL_US_DSRIC; 63635b675b9SRichard Genoud 63718dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) 63835b675b9SRichard Genoud idr |= ATMEL_US_RIIC; 63935b675b9SRichard Genoud 64018dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) 64135b675b9SRichard Genoud idr |= ATMEL_US_DCDIC; 64235b675b9SRichard Genoud 6434e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, idr); 64435b675b9SRichard Genoud } 64535b675b9SRichard Genoud 64635b675b9SRichard Genoud /* 647ab4382d2SGreg Kroah-Hartman * Control the transmission of a break signal 648ab4382d2SGreg Kroah-Hartman */ 649ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state) 650ab4382d2SGreg Kroah-Hartman { 651ab4382d2SGreg Kroah-Hartman if (break_state != 0) 6524e7decdaSCyrille Pitchen /* start break */ 6534e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK); 654ab4382d2SGreg Kroah-Hartman else 6554e7decdaSCyrille Pitchen /* stop break */ 6564e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK); 657ab4382d2SGreg Kroah-Hartman } 658ab4382d2SGreg Kroah-Hartman 659ab4382d2SGreg Kroah-Hartman /* 660ab4382d2SGreg Kroah-Hartman * Stores the incoming character in the ring buffer 661ab4382d2SGreg Kroah-Hartman */ 662ab4382d2SGreg Kroah-Hartman static void 663ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status, 664ab4382d2SGreg Kroah-Hartman unsigned int ch) 665ab4382d2SGreg Kroah-Hartman { 666ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 667ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 668ab4382d2SGreg Kroah-Hartman struct atmel_uart_char *c; 669ab4382d2SGreg Kroah-Hartman 670ab4382d2SGreg Kroah-Hartman if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) 671ab4382d2SGreg Kroah-Hartman /* Buffer overflow, ignore char */ 672ab4382d2SGreg Kroah-Hartman return; 673ab4382d2SGreg Kroah-Hartman 674ab4382d2SGreg Kroah-Hartman c = &((struct atmel_uart_char *)ring->buf)[ring->head]; 675ab4382d2SGreg Kroah-Hartman c->status = status; 676ab4382d2SGreg Kroah-Hartman c->ch = ch; 677ab4382d2SGreg Kroah-Hartman 678ab4382d2SGreg Kroah-Hartman /* Make sure the character is stored before we update head. */ 679ab4382d2SGreg Kroah-Hartman smp_wmb(); 680ab4382d2SGreg Kroah-Hartman 681ab4382d2SGreg Kroah-Hartman ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 682ab4382d2SGreg Kroah-Hartman } 683ab4382d2SGreg Kroah-Hartman 684ab4382d2SGreg Kroah-Hartman /* 685ab4382d2SGreg Kroah-Hartman * Deal with parity, framing and overrun errors. 686ab4382d2SGreg Kroah-Hartman */ 687ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status) 688ab4382d2SGreg Kroah-Hartman { 689ab4382d2SGreg Kroah-Hartman /* clear error */ 6904e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 691ab4382d2SGreg Kroah-Hartman 692ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 693ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 694ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 695ab4382d2SGreg Kroah-Hartman port->icount.brk++; 696ab4382d2SGreg Kroah-Hartman } 697ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 698ab4382d2SGreg Kroah-Hartman port->icount.parity++; 699ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 700ab4382d2SGreg Kroah-Hartman port->icount.frame++; 701ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 702ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 703ab4382d2SGreg Kroah-Hartman } 704ab4382d2SGreg Kroah-Hartman 705ab4382d2SGreg Kroah-Hartman /* 706ab4382d2SGreg Kroah-Hartman * Characters received (called from interrupt handler) 707ab4382d2SGreg Kroah-Hartman */ 708ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port) 709ab4382d2SGreg Kroah-Hartman { 710ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 711ab4382d2SGreg Kroah-Hartman unsigned int status, ch; 712ab4382d2SGreg Kroah-Hartman 7134e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 714ab4382d2SGreg Kroah-Hartman while (status & ATMEL_US_RXRDY) { 715a6499435SCyrille Pitchen ch = atmel_uart_read_char(port); 716ab4382d2SGreg Kroah-Hartman 717ab4382d2SGreg Kroah-Hartman /* 718ab4382d2SGreg Kroah-Hartman * note that the error handling code is 719ab4382d2SGreg Kroah-Hartman * out of the main execution path 720ab4382d2SGreg Kroah-Hartman */ 721ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 722ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK) 723ab4382d2SGreg Kroah-Hartman || atmel_port->break_active)) { 724ab4382d2SGreg Kroah-Hartman 725ab4382d2SGreg Kroah-Hartman /* clear error */ 7264e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 727ab4382d2SGreg Kroah-Hartman 728ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK 729ab4382d2SGreg Kroah-Hartman && !atmel_port->break_active) { 730ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 1; 7314e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 7324e7decdaSCyrille Pitchen ATMEL_US_RXBRK); 733ab4382d2SGreg Kroah-Hartman } else { 734ab4382d2SGreg Kroah-Hartman /* 735ab4382d2SGreg Kroah-Hartman * This is either the end-of-break 736ab4382d2SGreg Kroah-Hartman * condition or we've received at 737ab4382d2SGreg Kroah-Hartman * least one character without RXBRK 738ab4382d2SGreg Kroah-Hartman * being set. In both cases, the next 739ab4382d2SGreg Kroah-Hartman * RXBRK will indicate start-of-break. 740ab4382d2SGreg Kroah-Hartman */ 7414e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 7424e7decdaSCyrille Pitchen ATMEL_US_RXBRK); 743ab4382d2SGreg Kroah-Hartman status &= ~ATMEL_US_RXBRK; 744ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 745ab4382d2SGreg Kroah-Hartman } 746ab4382d2SGreg Kroah-Hartman } 747ab4382d2SGreg Kroah-Hartman 748ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(port, status, ch); 7494e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 750ab4382d2SGreg Kroah-Hartman } 751ab4382d2SGreg Kroah-Hartman 75298f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 753ab4382d2SGreg Kroah-Hartman } 754ab4382d2SGreg Kroah-Hartman 755ab4382d2SGreg Kroah-Hartman /* 756ab4382d2SGreg Kroah-Hartman * Transmit characters (called from tasklet with TXRDY interrupt 757ab4382d2SGreg Kroah-Hartman * disabled) 758ab4382d2SGreg Kroah-Hartman */ 759ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port) 760ab4382d2SGreg Kroah-Hartman { 761ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 762ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 763ab4382d2SGreg Kroah-Hartman 7644e7decdaSCyrille Pitchen if (port->x_char && 7654e7decdaSCyrille Pitchen (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) { 766a6499435SCyrille Pitchen atmel_uart_write_char(port, port->x_char); 767ab4382d2SGreg Kroah-Hartman port->icount.tx++; 768ab4382d2SGreg Kroah-Hartman port->x_char = 0; 769ab4382d2SGreg Kroah-Hartman } 770ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 771ab4382d2SGreg Kroah-Hartman return; 772ab4382d2SGreg Kroah-Hartman 7734e7decdaSCyrille Pitchen while (atmel_uart_readl(port, ATMEL_US_CSR) & 7744e7decdaSCyrille Pitchen atmel_port->tx_done_mask) { 775a6499435SCyrille Pitchen atmel_uart_write_char(port, xmit->buf[xmit->tail]); 776ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 777ab4382d2SGreg Kroah-Hartman port->icount.tx++; 778ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit)) 779ab4382d2SGreg Kroah-Hartman break; 780ab4382d2SGreg Kroah-Hartman } 781ab4382d2SGreg Kroah-Hartman 782ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 783ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 784ab4382d2SGreg Kroah-Hartman 785ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit)) 786ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 7874e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 7884e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 789ab4382d2SGreg Kroah-Hartman } 790ab4382d2SGreg Kroah-Hartman 79108f738beSElen Song static void atmel_complete_tx_dma(void *arg) 79208f738beSElen Song { 79308f738beSElen Song struct atmel_uart_port *atmel_port = arg; 79408f738beSElen Song struct uart_port *port = &atmel_port->uart; 79508f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 79608f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 79708f738beSElen Song unsigned long flags; 79808f738beSElen Song 79908f738beSElen Song spin_lock_irqsave(&port->lock, flags); 80008f738beSElen Song 80108f738beSElen Song if (chan) 80208f738beSElen Song dmaengine_terminate_all(chan); 8035f258b3eSCyrille Pitchen xmit->tail += atmel_port->tx_len; 80408f738beSElen Song xmit->tail &= UART_XMIT_SIZE - 1; 80508f738beSElen Song 8065f258b3eSCyrille Pitchen port->icount.tx += atmel_port->tx_len; 80708f738beSElen Song 80808f738beSElen Song spin_lock_irq(&atmel_port->lock_tx); 80908f738beSElen Song async_tx_ack(atmel_port->desc_tx); 81008f738beSElen Song atmel_port->cookie_tx = -EINVAL; 81108f738beSElen Song atmel_port->desc_tx = NULL; 81208f738beSElen Song spin_unlock_irq(&atmel_port->lock_tx); 81308f738beSElen Song 81408f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 81508f738beSElen Song uart_write_wakeup(port); 81608f738beSElen Song 8171842dc2eSCyrille Pitchen /* 8181842dc2eSCyrille Pitchen * xmit is a circular buffer so, if we have just send data from 8191842dc2eSCyrille Pitchen * xmit->tail to the end of xmit->buf, now we have to transmit the 8201842dc2eSCyrille Pitchen * remaining data from the beginning of xmit->buf to xmit->head. 8211842dc2eSCyrille Pitchen */ 82208f738beSElen Song if (!uart_circ_empty(xmit)) 82398f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); 824b389f173SRichard Genoud else if ((port->rs485.flags & SER_RS485_ENABLED) && 825b389f173SRichard Genoud !(port->rs485.flags & SER_RS485_RX_DURING_TX)) { 826b389f173SRichard Genoud /* DMA done, stop TX, start RX for RS485 */ 827b389f173SRichard Genoud atmel_start_rx(port); 828b389f173SRichard Genoud } 82908f738beSElen Song 83008f738beSElen Song spin_unlock_irqrestore(&port->lock, flags); 83108f738beSElen Song } 83208f738beSElen Song 83308f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port) 83408f738beSElen Song { 83508f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 83608f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 83708f738beSElen Song 83808f738beSElen Song if (chan) { 83908f738beSElen Song dmaengine_terminate_all(chan); 84008f738beSElen Song dma_release_channel(chan); 84108f738beSElen Song dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1, 84248479148SWolfram Sang DMA_TO_DEVICE); 84308f738beSElen Song } 84408f738beSElen Song 84508f738beSElen Song atmel_port->desc_tx = NULL; 84608f738beSElen Song atmel_port->chan_tx = NULL; 84708f738beSElen Song atmel_port->cookie_tx = -EINVAL; 84808f738beSElen Song } 84908f738beSElen Song 85008f738beSElen Song /* 85108f738beSElen Song * Called from tasklet with TXRDY interrupt is disabled. 85208f738beSElen Song */ 85308f738beSElen Song static void atmel_tx_dma(struct uart_port *port) 85408f738beSElen Song { 85508f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 85608f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 85708f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 85808f738beSElen Song struct dma_async_tx_descriptor *desc; 8595f258b3eSCyrille Pitchen struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx; 8605f258b3eSCyrille Pitchen unsigned int tx_len, part1_len, part2_len, sg_len; 8615f258b3eSCyrille Pitchen dma_addr_t phys_addr; 86208f738beSElen Song 86308f738beSElen Song /* Make sure we have an idle channel */ 86408f738beSElen Song if (atmel_port->desc_tx != NULL) 86508f738beSElen Song return; 86608f738beSElen Song 86708f738beSElen Song if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 86808f738beSElen Song /* 86908f738beSElen Song * DMA is idle now. 87008f738beSElen Song * Port xmit buffer is already mapped, 87108f738beSElen Song * and it is one page... Just adjust 87208f738beSElen Song * offsets and lengths. Since it is a circular buffer, 87308f738beSElen Song * we have to transmit till the end, and then the rest. 87408f738beSElen Song * Take the port lock to get a 87508f738beSElen Song * consistent xmit buffer state. 87608f738beSElen Song */ 8775f258b3eSCyrille Pitchen tx_len = CIRC_CNT_TO_END(xmit->head, 87808f738beSElen Song xmit->tail, 87908f738beSElen Song UART_XMIT_SIZE); 8805f258b3eSCyrille Pitchen 8815f258b3eSCyrille Pitchen if (atmel_port->fifo_size) { 8825f258b3eSCyrille Pitchen /* multi data mode */ 8835f258b3eSCyrille Pitchen part1_len = (tx_len & ~0x3); /* DWORD access */ 8845f258b3eSCyrille Pitchen part2_len = (tx_len & 0x3); /* BYTE access */ 8855f258b3eSCyrille Pitchen } else { 8865f258b3eSCyrille Pitchen /* single data (legacy) mode */ 8875f258b3eSCyrille Pitchen part1_len = 0; 8885f258b3eSCyrille Pitchen part2_len = tx_len; /* BYTE access only */ 8895f258b3eSCyrille Pitchen } 8905f258b3eSCyrille Pitchen 8915f258b3eSCyrille Pitchen sg_init_table(sgl, 2); 8925f258b3eSCyrille Pitchen sg_len = 0; 8935f258b3eSCyrille Pitchen phys_addr = sg_dma_address(sg_tx) + xmit->tail; 8945f258b3eSCyrille Pitchen if (part1_len) { 8955f258b3eSCyrille Pitchen sg = &sgl[sg_len++]; 8965f258b3eSCyrille Pitchen sg_dma_address(sg) = phys_addr; 8975f258b3eSCyrille Pitchen sg_dma_len(sg) = part1_len; 8985f258b3eSCyrille Pitchen 8995f258b3eSCyrille Pitchen phys_addr += part1_len; 9005f258b3eSCyrille Pitchen } 9015f258b3eSCyrille Pitchen 9025f258b3eSCyrille Pitchen if (part2_len) { 9035f258b3eSCyrille Pitchen sg = &sgl[sg_len++]; 9045f258b3eSCyrille Pitchen sg_dma_address(sg) = phys_addr; 9055f258b3eSCyrille Pitchen sg_dma_len(sg) = part2_len; 9065f258b3eSCyrille Pitchen } 9075f258b3eSCyrille Pitchen 9085f258b3eSCyrille Pitchen /* 9095f258b3eSCyrille Pitchen * save tx_len so atmel_complete_tx_dma() will increase 9105f258b3eSCyrille Pitchen * xmit->tail correctly 9115f258b3eSCyrille Pitchen */ 9125f258b3eSCyrille Pitchen atmel_port->tx_len = tx_len; 91308f738beSElen Song 91408f738beSElen Song desc = dmaengine_prep_slave_sg(chan, 9155f258b3eSCyrille Pitchen sgl, 9165f258b3eSCyrille Pitchen sg_len, 91708f738beSElen Song DMA_MEM_TO_DEV, 91808f738beSElen Song DMA_PREP_INTERRUPT | 91908f738beSElen Song DMA_CTRL_ACK); 92008f738beSElen Song if (!desc) { 92108f738beSElen Song dev_err(port->dev, "Failed to send via dma!\n"); 92208f738beSElen Song return; 92308f738beSElen Song } 92408f738beSElen Song 9255f258b3eSCyrille Pitchen dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE); 92608f738beSElen Song 92708f738beSElen Song atmel_port->desc_tx = desc; 92808f738beSElen Song desc->callback = atmel_complete_tx_dma; 92908f738beSElen Song desc->callback_param = atmel_port; 93008f738beSElen Song atmel_port->cookie_tx = dmaengine_submit(desc); 93108f738beSElen Song } 93208f738beSElen Song 93308f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 93408f738beSElen Song uart_write_wakeup(port); 93508f738beSElen Song } 93608f738beSElen Song 93708f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port) 93808f738beSElen Song { 93908f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 94008f738beSElen Song dma_cap_mask_t mask; 94108f738beSElen Song struct dma_slave_config config; 94208f738beSElen Song int ret, nent; 94308f738beSElen Song 94408f738beSElen Song dma_cap_zero(mask); 94508f738beSElen Song dma_cap_set(DMA_SLAVE, mask); 94608f738beSElen Song 94708f738beSElen Song atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx"); 94808f738beSElen Song if (atmel_port->chan_tx == NULL) 94908f738beSElen Song goto chan_err; 95008f738beSElen Song dev_info(port->dev, "using %s for tx DMA transfers\n", 95108f738beSElen Song dma_chan_name(atmel_port->chan_tx)); 95208f738beSElen Song 95308f738beSElen Song spin_lock_init(&atmel_port->lock_tx); 95408f738beSElen Song sg_init_table(&atmel_port->sg_tx, 1); 95508f738beSElen Song /* UART circular tx buffer is an aligned page. */ 9562c277054SLeilei Zhao BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf)); 95708f738beSElen Song sg_set_page(&atmel_port->sg_tx, 95808f738beSElen Song virt_to_page(port->state->xmit.buf), 95908f738beSElen Song UART_XMIT_SIZE, 960c8d1f022SUwe Kleine-König (unsigned long)port->state->xmit.buf & ~PAGE_MASK); 96108f738beSElen Song nent = dma_map_sg(port->dev, 96208f738beSElen Song &atmel_port->sg_tx, 96308f738beSElen Song 1, 96448479148SWolfram Sang DMA_TO_DEVICE); 96508f738beSElen Song 96608f738beSElen Song if (!nent) { 96708f738beSElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 96808f738beSElen Song goto chan_err; 96908f738beSElen Song } else { 970c8d1f022SUwe Kleine-König dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, 97108f738beSElen Song sg_dma_len(&atmel_port->sg_tx), 97208f738beSElen Song port->state->xmit.buf, 973c8d1f022SUwe Kleine-König &sg_dma_address(&atmel_port->sg_tx)); 97408f738beSElen Song } 97508f738beSElen Song 97608f738beSElen Song /* Configure the slave DMA */ 97708f738beSElen Song memset(&config, 0, sizeof(config)); 97808f738beSElen Song config.direction = DMA_MEM_TO_DEV; 9795f258b3eSCyrille Pitchen config.dst_addr_width = (atmel_port->fifo_size) ? 9805f258b3eSCyrille Pitchen DMA_SLAVE_BUSWIDTH_4_BYTES : 9815f258b3eSCyrille Pitchen DMA_SLAVE_BUSWIDTH_1_BYTE; 98208f738beSElen Song config.dst_addr = port->mapbase + ATMEL_US_THR; 983a8d4e016SLudovic Desroches config.dst_maxburst = 1; 98408f738beSElen Song 9855483c10eSMaxime Ripard ret = dmaengine_slave_config(atmel_port->chan_tx, 9865483c10eSMaxime Ripard &config); 98708f738beSElen Song if (ret) { 98808f738beSElen Song dev_err(port->dev, "DMA tx slave configuration failed\n"); 98908f738beSElen Song goto chan_err; 99008f738beSElen Song } 99108f738beSElen Song 99208f738beSElen Song return 0; 99308f738beSElen Song 99408f738beSElen Song chan_err: 99508f738beSElen Song dev_err(port->dev, "TX channel not available, switch to pio\n"); 99608f738beSElen Song atmel_port->use_dma_tx = 0; 99708f738beSElen Song if (atmel_port->chan_tx) 99808f738beSElen Song atmel_release_tx_dma(port); 99908f738beSElen Song return -EINVAL; 100008f738beSElen Song } 100108f738beSElen Song 100234df42f5SElen Song static void atmel_complete_rx_dma(void *arg) 100334df42f5SElen Song { 100434df42f5SElen Song struct uart_port *port = arg; 100534df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 100634df42f5SElen Song 100798f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 100834df42f5SElen Song } 100934df42f5SElen Song 101034df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port) 101134df42f5SElen Song { 101234df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 101334df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 101434df42f5SElen Song 101534df42f5SElen Song if (chan) { 101634df42f5SElen Song dmaengine_terminate_all(chan); 101734df42f5SElen Song dma_release_channel(chan); 101834df42f5SElen Song dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1, 101948479148SWolfram Sang DMA_FROM_DEVICE); 102034df42f5SElen Song } 102134df42f5SElen Song 102234df42f5SElen Song atmel_port->desc_rx = NULL; 102334df42f5SElen Song atmel_port->chan_rx = NULL; 102434df42f5SElen Song atmel_port->cookie_rx = -EINVAL; 102534df42f5SElen Song } 102634df42f5SElen Song 102734df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port) 102834df42f5SElen Song { 102934df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 103066f37aafSCyrille Pitchen struct tty_port *tport = &port->state->port; 103134df42f5SElen Song struct circ_buf *ring = &atmel_port->rx_ring; 103234df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 103334df42f5SElen Song struct dma_tx_state state; 103434df42f5SElen Song enum dma_status dmastat; 103566f37aafSCyrille Pitchen size_t count; 103634df42f5SElen Song 103734df42f5SElen Song 103834df42f5SElen Song /* Reset the UART timeout early so that we don't miss one */ 10394e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 104034df42f5SElen Song dmastat = dmaengine_tx_status(chan, 104134df42f5SElen Song atmel_port->cookie_rx, 104234df42f5SElen Song &state); 104334df42f5SElen Song /* Restart a new tasklet if DMA status is error */ 104434df42f5SElen Song if (dmastat == DMA_ERROR) { 104534df42f5SElen Song dev_dbg(port->dev, "Get residue error, restart tasklet\n"); 10464e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT); 104798f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 104834df42f5SElen Song return; 104934df42f5SElen Song } 105066f37aafSCyrille Pitchen 105166f37aafSCyrille Pitchen /* CPU claims ownership of RX DMA buffer */ 105266f37aafSCyrille Pitchen dma_sync_sg_for_cpu(port->dev, 105366f37aafSCyrille Pitchen &atmel_port->sg_rx, 105466f37aafSCyrille Pitchen 1, 1055485819b5SCyrille Pitchen DMA_FROM_DEVICE); 105634df42f5SElen Song 105734df42f5SElen Song /* 105866f37aafSCyrille Pitchen * ring->head points to the end of data already written by the DMA. 105966f37aafSCyrille Pitchen * ring->tail points to the beginning of data to be read by the 106066f37aafSCyrille Pitchen * framework. 106166f37aafSCyrille Pitchen * The current transfer size should not be larger than the dma buffer 106266f37aafSCyrille Pitchen * length. 106334df42f5SElen Song */ 106466f37aafSCyrille Pitchen ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue; 106566f37aafSCyrille Pitchen BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx)); 106666f37aafSCyrille Pitchen /* 106766f37aafSCyrille Pitchen * At this point ring->head may point to the first byte right after the 106866f37aafSCyrille Pitchen * last byte of the dma buffer: 106966f37aafSCyrille Pitchen * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx) 107066f37aafSCyrille Pitchen * 107166f37aafSCyrille Pitchen * However ring->tail must always points inside the dma buffer: 107266f37aafSCyrille Pitchen * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1 107366f37aafSCyrille Pitchen * 107466f37aafSCyrille Pitchen * Since we use a ring buffer, we have to handle the case 107566f37aafSCyrille Pitchen * where head is lower than tail. In such a case, we first read from 107666f37aafSCyrille Pitchen * tail to the end of the buffer then reset tail. 107766f37aafSCyrille Pitchen */ 107866f37aafSCyrille Pitchen if (ring->head < ring->tail) { 107966f37aafSCyrille Pitchen count = sg_dma_len(&atmel_port->sg_rx) - ring->tail; 108034df42f5SElen Song 108166f37aafSCyrille Pitchen tty_insert_flip_string(tport, ring->buf + ring->tail, count); 108266f37aafSCyrille Pitchen ring->tail = 0; 108334df42f5SElen Song port->icount.rx += count; 108434df42f5SElen Song } 108534df42f5SElen Song 108666f37aafSCyrille Pitchen /* Finally we read data from tail to head */ 108766f37aafSCyrille Pitchen if (ring->tail < ring->head) { 108866f37aafSCyrille Pitchen count = ring->head - ring->tail; 108966f37aafSCyrille Pitchen 109066f37aafSCyrille Pitchen tty_insert_flip_string(tport, ring->buf + ring->tail, count); 109166f37aafSCyrille Pitchen /* Wrap ring->head if needed */ 109266f37aafSCyrille Pitchen if (ring->head >= sg_dma_len(&atmel_port->sg_rx)) 109366f37aafSCyrille Pitchen ring->head = 0; 109466f37aafSCyrille Pitchen ring->tail = ring->head; 109566f37aafSCyrille Pitchen port->icount.rx += count; 109666f37aafSCyrille Pitchen } 109766f37aafSCyrille Pitchen 109866f37aafSCyrille Pitchen /* USART retreives ownership of RX DMA buffer */ 109966f37aafSCyrille Pitchen dma_sync_sg_for_device(port->dev, 110066f37aafSCyrille Pitchen &atmel_port->sg_rx, 110166f37aafSCyrille Pitchen 1, 1102485819b5SCyrille Pitchen DMA_FROM_DEVICE); 110366f37aafSCyrille Pitchen 110466f37aafSCyrille Pitchen /* 110566f37aafSCyrille Pitchen * Drop the lock here since it might end up calling 110666f37aafSCyrille Pitchen * uart_start(), which takes the lock. 110766f37aafSCyrille Pitchen */ 110866f37aafSCyrille Pitchen spin_unlock(&port->lock); 110966f37aafSCyrille Pitchen tty_flip_buffer_push(tport); 111066f37aafSCyrille Pitchen spin_lock(&port->lock); 111166f37aafSCyrille Pitchen 11124e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT); 111334df42f5SElen Song } 111434df42f5SElen Song 111534df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port) 111634df42f5SElen Song { 111734df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 111834df42f5SElen Song struct dma_async_tx_descriptor *desc; 111934df42f5SElen Song dma_cap_mask_t mask; 112034df42f5SElen Song struct dma_slave_config config; 112134df42f5SElen Song struct circ_buf *ring; 112234df42f5SElen Song int ret, nent; 112334df42f5SElen Song 112434df42f5SElen Song ring = &atmel_port->rx_ring; 112534df42f5SElen Song 112634df42f5SElen Song dma_cap_zero(mask); 112734df42f5SElen Song dma_cap_set(DMA_CYCLIC, mask); 112834df42f5SElen Song 112934df42f5SElen Song atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx"); 113034df42f5SElen Song if (atmel_port->chan_rx == NULL) 113134df42f5SElen Song goto chan_err; 113234df42f5SElen Song dev_info(port->dev, "using %s for rx DMA transfers\n", 113334df42f5SElen Song dma_chan_name(atmel_port->chan_rx)); 113434df42f5SElen Song 113534df42f5SElen Song spin_lock_init(&atmel_port->lock_rx); 113634df42f5SElen Song sg_init_table(&atmel_port->sg_rx, 1); 113734df42f5SElen Song /* UART circular rx buffer is an aligned page. */ 11382c277054SLeilei Zhao BUG_ON(!PAGE_ALIGNED(ring->buf)); 113934df42f5SElen Song sg_set_page(&atmel_port->sg_rx, 114034df42f5SElen Song virt_to_page(ring->buf), 1141a510880fSLeilei Zhao sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE, 1142c8d1f022SUwe Kleine-König (unsigned long)ring->buf & ~PAGE_MASK); 114334df42f5SElen Song nent = dma_map_sg(port->dev, 114434df42f5SElen Song &atmel_port->sg_rx, 114534df42f5SElen Song 1, 114648479148SWolfram Sang DMA_FROM_DEVICE); 114734df42f5SElen Song 114834df42f5SElen Song if (!nent) { 114934df42f5SElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 115034df42f5SElen Song goto chan_err; 115134df42f5SElen Song } else { 1152c8d1f022SUwe Kleine-König dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, 115334df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 115434df42f5SElen Song ring->buf, 1155c8d1f022SUwe Kleine-König &sg_dma_address(&atmel_port->sg_rx)); 115634df42f5SElen Song } 115734df42f5SElen Song 115834df42f5SElen Song /* Configure the slave DMA */ 115934df42f5SElen Song memset(&config, 0, sizeof(config)); 116034df42f5SElen Song config.direction = DMA_DEV_TO_MEM; 116134df42f5SElen Song config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 116234df42f5SElen Song config.src_addr = port->mapbase + ATMEL_US_RHR; 1163a8d4e016SLudovic Desroches config.src_maxburst = 1; 116434df42f5SElen Song 11655483c10eSMaxime Ripard ret = dmaengine_slave_config(atmel_port->chan_rx, 11665483c10eSMaxime Ripard &config); 116734df42f5SElen Song if (ret) { 116834df42f5SElen Song dev_err(port->dev, "DMA rx slave configuration failed\n"); 116934df42f5SElen Song goto chan_err; 117034df42f5SElen Song } 117134df42f5SElen Song /* 117234df42f5SElen Song * Prepare a cyclic dma transfer, assign 2 descriptors, 117334df42f5SElen Song * each one is half ring buffer size 117434df42f5SElen Song */ 117534df42f5SElen Song desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx, 117634df42f5SElen Song sg_dma_address(&atmel_port->sg_rx), 117734df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 117834df42f5SElen Song sg_dma_len(&atmel_port->sg_rx)/2, 117934df42f5SElen Song DMA_DEV_TO_MEM, 118034df42f5SElen Song DMA_PREP_INTERRUPT); 118134df42f5SElen Song desc->callback = atmel_complete_rx_dma; 118234df42f5SElen Song desc->callback_param = port; 118334df42f5SElen Song atmel_port->desc_rx = desc; 118434df42f5SElen Song atmel_port->cookie_rx = dmaengine_submit(desc); 118534df42f5SElen Song 118634df42f5SElen Song return 0; 118734df42f5SElen Song 118834df42f5SElen Song chan_err: 118934df42f5SElen Song dev_err(port->dev, "RX channel not available, switch to pio\n"); 119034df42f5SElen Song atmel_port->use_dma_rx = 0; 119134df42f5SElen Song if (atmel_port->chan_rx) 119234df42f5SElen Song atmel_release_rx_dma(port); 119334df42f5SElen Song return -EINVAL; 119434df42f5SElen Song } 119534df42f5SElen Song 11962e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data) 11972e68c22fSElen Song { 11982e68c22fSElen Song struct uart_port *port = (void *)data; 11992e68c22fSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 12002e68c22fSElen Song 120198f2082cSNicolas Ferre if (!atomic_read(&atmel_port->tasklet_shutdown)) { 120200e8e658SNicolas Ferre tasklet_schedule(&atmel_port->tasklet_rx); 120398f2082cSNicolas Ferre mod_timer(&atmel_port->uart_timer, 120498f2082cSNicolas Ferre jiffies + uart_poll_timeout(port)); 120598f2082cSNicolas Ferre } 12062e68c22fSElen Song } 12072e68c22fSElen Song 1208ab4382d2SGreg Kroah-Hartman /* 1209ab4382d2SGreg Kroah-Hartman * receive interrupt handler. 1210ab4382d2SGreg Kroah-Hartman */ 1211ab4382d2SGreg Kroah-Hartman static void 1212ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending) 1213ab4382d2SGreg Kroah-Hartman { 1214ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1215ab4382d2SGreg Kroah-Hartman 121664e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 1217ab4382d2SGreg Kroah-Hartman /* 1218ab4382d2SGreg Kroah-Hartman * PDC receive. Just schedule the tasklet and let it 1219ab4382d2SGreg Kroah-Hartman * figure out the details. 1220ab4382d2SGreg Kroah-Hartman * 1221ab4382d2SGreg Kroah-Hartman * TODO: We're not handling error flags correctly at 1222ab4382d2SGreg Kroah-Hartman * the moment. 1223ab4382d2SGreg Kroah-Hartman */ 1224ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) { 12254e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 12264e7decdaSCyrille Pitchen (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)); 122798f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, 122898f2082cSNicolas Ferre &atmel_port->tasklet_rx); 1229ab4382d2SGreg Kroah-Hartman } 1230ab4382d2SGreg Kroah-Hartman 1231ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE | 1232ab4382d2SGreg Kroah-Hartman ATMEL_US_FRAME | ATMEL_US_PARE)) 1233ab4382d2SGreg Kroah-Hartman atmel_pdc_rxerr(port, pending); 1234ab4382d2SGreg Kroah-Hartman } 1235ab4382d2SGreg Kroah-Hartman 123634df42f5SElen Song if (atmel_use_dma_rx(port)) { 123734df42f5SElen Song if (pending & ATMEL_US_TIMEOUT) { 12384e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 12394e7decdaSCyrille Pitchen ATMEL_US_TIMEOUT); 124098f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, 124198f2082cSNicolas Ferre &atmel_port->tasklet_rx); 124234df42f5SElen Song } 124334df42f5SElen Song } 124434df42f5SElen Song 1245ab4382d2SGreg Kroah-Hartman /* Interrupt receive */ 1246ab4382d2SGreg Kroah-Hartman if (pending & ATMEL_US_RXRDY) 1247ab4382d2SGreg Kroah-Hartman atmel_rx_chars(port); 1248ab4382d2SGreg Kroah-Hartman else if (pending & ATMEL_US_RXBRK) { 1249ab4382d2SGreg Kroah-Hartman /* 1250ab4382d2SGreg Kroah-Hartman * End of break detected. If it came along with a 1251ab4382d2SGreg Kroah-Hartman * character, atmel_rx_chars will handle it. 1252ab4382d2SGreg Kroah-Hartman */ 12534e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 12544e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK); 1255ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 1256ab4382d2SGreg Kroah-Hartman } 1257ab4382d2SGreg Kroah-Hartman } 1258ab4382d2SGreg Kroah-Hartman 1259ab4382d2SGreg Kroah-Hartman /* 1260ab4382d2SGreg Kroah-Hartman * transmit interrupt handler. (Transmit is IRQF_NODELAY safe) 1261ab4382d2SGreg Kroah-Hartman */ 1262ab4382d2SGreg Kroah-Hartman static void 1263ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending) 1264ab4382d2SGreg Kroah-Hartman { 1265ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1266ab4382d2SGreg Kroah-Hartman 1267ab4382d2SGreg Kroah-Hartman if (pending & atmel_port->tx_done_mask) { 1268ab4382d2SGreg Kroah-Hartman /* Either PDC or interrupt transmission */ 12694e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 12704e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 127198f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); 1272ab4382d2SGreg Kroah-Hartman } 1273ab4382d2SGreg Kroah-Hartman } 1274ab4382d2SGreg Kroah-Hartman 1275ab4382d2SGreg Kroah-Hartman /* 1276ab4382d2SGreg Kroah-Hartman * status flags interrupt handler. 1277ab4382d2SGreg Kroah-Hartman */ 1278ab4382d2SGreg Kroah-Hartman static void 1279ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending, 1280ab4382d2SGreg Kroah-Hartman unsigned int status) 1281ab4382d2SGreg Kroah-Hartman { 1282ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 12839205218eSNicolas Ferre unsigned int status_change; 1284ab4382d2SGreg Kroah-Hartman 1285ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC 1286ab4382d2SGreg Kroah-Hartman | ATMEL_US_CTSIC)) { 12879205218eSNicolas Ferre status_change = status ^ atmel_port->irq_status_prev; 1288d033e82dSLeilei Zhao atmel_port->irq_status_prev = status; 12899205218eSNicolas Ferre 12909205218eSNicolas Ferre if (status_change & (ATMEL_US_RI | ATMEL_US_DSR 12919205218eSNicolas Ferre | ATMEL_US_DCD | ATMEL_US_CTS)) { 12929205218eSNicolas Ferre /* TODO: All reads to CSR will clear these interrupts! */ 12939205218eSNicolas Ferre if (status_change & ATMEL_US_RI) 12949205218eSNicolas Ferre port->icount.rng++; 12959205218eSNicolas Ferre if (status_change & ATMEL_US_DSR) 12969205218eSNicolas Ferre port->icount.dsr++; 12979205218eSNicolas Ferre if (status_change & ATMEL_US_DCD) 12989205218eSNicolas Ferre uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); 12999205218eSNicolas Ferre if (status_change & ATMEL_US_CTS) 13009205218eSNicolas Ferre uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); 13019205218eSNicolas Ferre 13029205218eSNicolas Ferre wake_up_interruptible(&port->state->port.delta_msr_wait); 13039205218eSNicolas Ferre } 1304ab4382d2SGreg Kroah-Hartman } 1305ab4382d2SGreg Kroah-Hartman } 1306ab4382d2SGreg Kroah-Hartman 1307ab4382d2SGreg Kroah-Hartman /* 1308ab4382d2SGreg Kroah-Hartman * Interrupt handler 1309ab4382d2SGreg Kroah-Hartman */ 1310ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id) 1311ab4382d2SGreg Kroah-Hartman { 1312ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 1313ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 13142c7af5baSBoris BREZILLON unsigned int status, pending, mask, pass_counter = 0; 1315ab4382d2SGreg Kroah-Hartman 13162c7af5baSBoris BREZILLON spin_lock(&atmel_port->lock_suspended); 13172c7af5baSBoris BREZILLON 1318ab4382d2SGreg Kroah-Hartman do { 1319e0b0baadSRichard Genoud status = atmel_get_lines_status(port); 13204e7decdaSCyrille Pitchen mask = atmel_uart_readl(port, ATMEL_US_IMR); 13212c7af5baSBoris BREZILLON pending = status & mask; 1322ab4382d2SGreg Kroah-Hartman if (!pending) 1323ab4382d2SGreg Kroah-Hartman break; 1324ab4382d2SGreg Kroah-Hartman 13252c7af5baSBoris BREZILLON if (atmel_port->suspended) { 13262c7af5baSBoris BREZILLON atmel_port->pending |= pending; 13272c7af5baSBoris BREZILLON atmel_port->pending_status = status; 13284e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, mask); 13292c7af5baSBoris BREZILLON pm_system_wakeup(); 13302c7af5baSBoris BREZILLON break; 13312c7af5baSBoris BREZILLON } 13322c7af5baSBoris BREZILLON 1333ab4382d2SGreg Kroah-Hartman atmel_handle_receive(port, pending); 1334ab4382d2SGreg Kroah-Hartman atmel_handle_status(port, pending, status); 1335ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(port, pending); 1336ab4382d2SGreg Kroah-Hartman } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT); 1337ab4382d2SGreg Kroah-Hartman 13382c7af5baSBoris BREZILLON spin_unlock(&atmel_port->lock_suspended); 13392c7af5baSBoris BREZILLON 1340ab4382d2SGreg Kroah-Hartman return pass_counter ? IRQ_HANDLED : IRQ_NONE; 1341ab4382d2SGreg Kroah-Hartman } 1342ab4382d2SGreg Kroah-Hartman 1343a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port) 1344a930e528SElen Song { 1345a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1346a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1347a930e528SElen Song 1348a930e528SElen Song dma_unmap_single(port->dev, 1349a930e528SElen Song pdc->dma_addr, 1350a930e528SElen Song pdc->dma_size, 1351a930e528SElen Song DMA_TO_DEVICE); 1352a930e528SElen Song } 1353a930e528SElen Song 1354ab4382d2SGreg Kroah-Hartman /* 1355ab4382d2SGreg Kroah-Hartman * Called from tasklet with ENDTX and TXBUFE interrupts disabled. 1356ab4382d2SGreg Kroah-Hartman */ 135764e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port) 1358ab4382d2SGreg Kroah-Hartman { 1359ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1360ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 1361ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1362ab4382d2SGreg Kroah-Hartman int count; 1363ab4382d2SGreg Kroah-Hartman 1364ab4382d2SGreg Kroah-Hartman /* nothing left to transmit? */ 13654e7decdaSCyrille Pitchen if (atmel_uart_readl(port, ATMEL_PDC_TCR)) 1366ab4382d2SGreg Kroah-Hartman return; 1367ab4382d2SGreg Kroah-Hartman 1368ab4382d2SGreg Kroah-Hartman xmit->tail += pdc->ofs; 1369ab4382d2SGreg Kroah-Hartman xmit->tail &= UART_XMIT_SIZE - 1; 1370ab4382d2SGreg Kroah-Hartman 1371ab4382d2SGreg Kroah-Hartman port->icount.tx += pdc->ofs; 1372ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 1373ab4382d2SGreg Kroah-Hartman 1374ab4382d2SGreg Kroah-Hartman /* more to transmit - setup next transfer */ 1375ab4382d2SGreg Kroah-Hartman 1376ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 13774e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 1378ab4382d2SGreg Kroah-Hartman 1379ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 1380ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, 1381ab4382d2SGreg Kroah-Hartman pdc->dma_addr, 1382ab4382d2SGreg Kroah-Hartman pdc->dma_size, 1383ab4382d2SGreg Kroah-Hartman DMA_TO_DEVICE); 1384ab4382d2SGreg Kroah-Hartman 1385ab4382d2SGreg Kroah-Hartman count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 1386ab4382d2SGreg Kroah-Hartman pdc->ofs = count; 1387ab4382d2SGreg Kroah-Hartman 13884e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TPR, 13894e7decdaSCyrille Pitchen pdc->dma_addr + xmit->tail); 13904e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TCR, count); 1391ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 13924e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 1393ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 13944e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 13954e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 1396ab4382d2SGreg Kroah-Hartman } else { 139713bd3e6fSRicardo Ribalda Delgado if ((port->rs485.flags & SER_RS485_ENABLED) && 139813bd3e6fSRicardo Ribalda Delgado !(port->rs485.flags & SER_RS485_RX_DURING_TX)) { 1399ab4382d2SGreg Kroah-Hartman /* DMA done, stop TX, start RX for RS485 */ 1400ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 1401ab4382d2SGreg Kroah-Hartman } 1402ab4382d2SGreg Kroah-Hartman } 1403ab4382d2SGreg Kroah-Hartman 1404ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 1405ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 1406ab4382d2SGreg Kroah-Hartman } 1407ab4382d2SGreg Kroah-Hartman 1408a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port) 1409a930e528SElen Song { 1410a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1411a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1412a930e528SElen Song struct circ_buf *xmit = &port->state->xmit; 1413a930e528SElen Song 1414a930e528SElen Song pdc->buf = xmit->buf; 1415a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1416a930e528SElen Song pdc->buf, 1417a930e528SElen Song UART_XMIT_SIZE, 1418a930e528SElen Song DMA_TO_DEVICE); 1419a930e528SElen Song pdc->dma_size = UART_XMIT_SIZE; 1420a930e528SElen Song pdc->ofs = 0; 1421a930e528SElen Song 1422a930e528SElen Song return 0; 1423a930e528SElen Song } 1424a930e528SElen Song 1425ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port) 1426ab4382d2SGreg Kroah-Hartman { 1427ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1428ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 1429ab4382d2SGreg Kroah-Hartman unsigned int flg; 1430ab4382d2SGreg Kroah-Hartman unsigned int status; 1431ab4382d2SGreg Kroah-Hartman 1432ab4382d2SGreg Kroah-Hartman while (ring->head != ring->tail) { 1433ab4382d2SGreg Kroah-Hartman struct atmel_uart_char c; 1434ab4382d2SGreg Kroah-Hartman 1435ab4382d2SGreg Kroah-Hartman /* Make sure c is loaded after head. */ 1436ab4382d2SGreg Kroah-Hartman smp_rmb(); 1437ab4382d2SGreg Kroah-Hartman 1438ab4382d2SGreg Kroah-Hartman c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; 1439ab4382d2SGreg Kroah-Hartman 1440ab4382d2SGreg Kroah-Hartman ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 1441ab4382d2SGreg Kroah-Hartman 1442ab4382d2SGreg Kroah-Hartman port->icount.rx++; 1443ab4382d2SGreg Kroah-Hartman status = c.status; 1444ab4382d2SGreg Kroah-Hartman flg = TTY_NORMAL; 1445ab4382d2SGreg Kroah-Hartman 1446ab4382d2SGreg Kroah-Hartman /* 1447ab4382d2SGreg Kroah-Hartman * note that the error handling code is 1448ab4382d2SGreg Kroah-Hartman * out of the main execution path 1449ab4382d2SGreg Kroah-Hartman */ 1450ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 1451ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK))) { 1452ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 1453ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 1454ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 1455ab4382d2SGreg Kroah-Hartman 1456ab4382d2SGreg Kroah-Hartman port->icount.brk++; 1457ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 1458ab4382d2SGreg Kroah-Hartman continue; 1459ab4382d2SGreg Kroah-Hartman } 1460ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 1461ab4382d2SGreg Kroah-Hartman port->icount.parity++; 1462ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 1463ab4382d2SGreg Kroah-Hartman port->icount.frame++; 1464ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 1465ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 1466ab4382d2SGreg Kroah-Hartman 1467ab4382d2SGreg Kroah-Hartman status &= port->read_status_mask; 1468ab4382d2SGreg Kroah-Hartman 1469ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) 1470ab4382d2SGreg Kroah-Hartman flg = TTY_BREAK; 1471ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_PARE) 1472ab4382d2SGreg Kroah-Hartman flg = TTY_PARITY; 1473ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_FRAME) 1474ab4382d2SGreg Kroah-Hartman flg = TTY_FRAME; 1475ab4382d2SGreg Kroah-Hartman } 1476ab4382d2SGreg Kroah-Hartman 1477ab4382d2SGreg Kroah-Hartman 1478ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c.ch)) 1479ab4382d2SGreg Kroah-Hartman continue; 1480ab4382d2SGreg Kroah-Hartman 1481ab4382d2SGreg Kroah-Hartman uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg); 1482ab4382d2SGreg Kroah-Hartman } 1483ab4382d2SGreg Kroah-Hartman 1484ab4382d2SGreg Kroah-Hartman /* 1485ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 1486ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 1487ab4382d2SGreg Kroah-Hartman */ 1488ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 14892e124b4aSJiri Slaby tty_flip_buffer_push(&port->state->port); 1490ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1491ab4382d2SGreg Kroah-Hartman } 1492ab4382d2SGreg Kroah-Hartman 1493a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port) 1494a930e528SElen Song { 1495a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1496a930e528SElen Song int i; 1497a930e528SElen Song 1498a930e528SElen Song for (i = 0; i < 2; i++) { 1499a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1500a930e528SElen Song 1501a930e528SElen Song dma_unmap_single(port->dev, 1502a930e528SElen Song pdc->dma_addr, 1503a930e528SElen Song pdc->dma_size, 1504a930e528SElen Song DMA_FROM_DEVICE); 1505a930e528SElen Song kfree(pdc->buf); 1506a930e528SElen Song } 1507a930e528SElen Song } 1508a930e528SElen Song 150964e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port) 1510ab4382d2SGreg Kroah-Hartman { 1511ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 151205c7cd39SJiri Slaby struct tty_port *tport = &port->state->port; 1513ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc; 1514ab4382d2SGreg Kroah-Hartman int rx_idx = atmel_port->pdc_rx_idx; 1515ab4382d2SGreg Kroah-Hartman unsigned int head; 1516ab4382d2SGreg Kroah-Hartman unsigned int tail; 1517ab4382d2SGreg Kroah-Hartman unsigned int count; 1518ab4382d2SGreg Kroah-Hartman 1519ab4382d2SGreg Kroah-Hartman do { 1520ab4382d2SGreg Kroah-Hartman /* Reset the UART timeout early so that we don't miss one */ 15214e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1522ab4382d2SGreg Kroah-Hartman 1523ab4382d2SGreg Kroah-Hartman pdc = &atmel_port->pdc_rx[rx_idx]; 15244e7decdaSCyrille Pitchen head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr; 1525ab4382d2SGreg Kroah-Hartman tail = pdc->ofs; 1526ab4382d2SGreg Kroah-Hartman 1527ab4382d2SGreg Kroah-Hartman /* If the PDC has switched buffers, RPR won't contain 1528ab4382d2SGreg Kroah-Hartman * any address within the current buffer. Since head 1529ab4382d2SGreg Kroah-Hartman * is unsigned, we just need a one-way comparison to 1530ab4382d2SGreg Kroah-Hartman * find out. 1531ab4382d2SGreg Kroah-Hartman * 1532ab4382d2SGreg Kroah-Hartman * In this case, we just need to consume the entire 1533ab4382d2SGreg Kroah-Hartman * buffer and resubmit it for DMA. This will clear the 1534ab4382d2SGreg Kroah-Hartman * ENDRX bit as well, so that we can safely re-enable 1535ab4382d2SGreg Kroah-Hartman * all interrupts below. 1536ab4382d2SGreg Kroah-Hartman */ 1537ab4382d2SGreg Kroah-Hartman head = min(head, pdc->dma_size); 1538ab4382d2SGreg Kroah-Hartman 1539ab4382d2SGreg Kroah-Hartman if (likely(head != tail)) { 1540ab4382d2SGreg Kroah-Hartman dma_sync_single_for_cpu(port->dev, pdc->dma_addr, 1541ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1542ab4382d2SGreg Kroah-Hartman 1543ab4382d2SGreg Kroah-Hartman /* 1544ab4382d2SGreg Kroah-Hartman * head will only wrap around when we recycle 1545ab4382d2SGreg Kroah-Hartman * the DMA buffer, and when that happens, we 1546ab4382d2SGreg Kroah-Hartman * explicitly set tail to 0. So head will 1547ab4382d2SGreg Kroah-Hartman * always be greater than tail. 1548ab4382d2SGreg Kroah-Hartman */ 1549ab4382d2SGreg Kroah-Hartman count = head - tail; 1550ab4382d2SGreg Kroah-Hartman 155105c7cd39SJiri Slaby tty_insert_flip_string(tport, pdc->buf + pdc->ofs, 155205c7cd39SJiri Slaby count); 1553ab4382d2SGreg Kroah-Hartman 1554ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, pdc->dma_addr, 1555ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1556ab4382d2SGreg Kroah-Hartman 1557ab4382d2SGreg Kroah-Hartman port->icount.rx += count; 1558ab4382d2SGreg Kroah-Hartman pdc->ofs = head; 1559ab4382d2SGreg Kroah-Hartman } 1560ab4382d2SGreg Kroah-Hartman 1561ab4382d2SGreg Kroah-Hartman /* 1562ab4382d2SGreg Kroah-Hartman * If the current buffer is full, we need to check if 1563ab4382d2SGreg Kroah-Hartman * the next one contains any additional data. 1564ab4382d2SGreg Kroah-Hartman */ 1565ab4382d2SGreg Kroah-Hartman if (head >= pdc->dma_size) { 1566ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 15674e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr); 15684e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size); 1569ab4382d2SGreg Kroah-Hartman 1570ab4382d2SGreg Kroah-Hartman rx_idx = !rx_idx; 1571ab4382d2SGreg Kroah-Hartman atmel_port->pdc_rx_idx = rx_idx; 1572ab4382d2SGreg Kroah-Hartman } 1573ab4382d2SGreg Kroah-Hartman } while (head >= pdc->dma_size); 1574ab4382d2SGreg Kroah-Hartman 1575ab4382d2SGreg Kroah-Hartman /* 1576ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 1577ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 1578ab4382d2SGreg Kroah-Hartman */ 1579ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 15802e124b4aSJiri Slaby tty_flip_buffer_push(tport); 1581ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1582ab4382d2SGreg Kroah-Hartman 15834e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 15844e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1585ab4382d2SGreg Kroah-Hartman } 1586ab4382d2SGreg Kroah-Hartman 1587a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port) 1588a930e528SElen Song { 1589a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1590a930e528SElen Song int i; 1591a930e528SElen Song 1592a930e528SElen Song for (i = 0; i < 2; i++) { 1593a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1594a930e528SElen Song 1595a930e528SElen Song pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); 1596a930e528SElen Song if (pdc->buf == NULL) { 1597a930e528SElen Song if (i != 0) { 1598a930e528SElen Song dma_unmap_single(port->dev, 1599a930e528SElen Song atmel_port->pdc_rx[0].dma_addr, 1600a930e528SElen Song PDC_BUFFER_SIZE, 1601a930e528SElen Song DMA_FROM_DEVICE); 1602a930e528SElen Song kfree(atmel_port->pdc_rx[0].buf); 1603a930e528SElen Song } 1604a930e528SElen Song atmel_port->use_pdc_rx = 0; 1605a930e528SElen Song return -ENOMEM; 1606a930e528SElen Song } 1607a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1608a930e528SElen Song pdc->buf, 1609a930e528SElen Song PDC_BUFFER_SIZE, 1610a930e528SElen Song DMA_FROM_DEVICE); 1611a930e528SElen Song pdc->dma_size = PDC_BUFFER_SIZE; 1612a930e528SElen Song pdc->ofs = 0; 1613a930e528SElen Song } 1614a930e528SElen Song 1615a930e528SElen Song atmel_port->pdc_rx_idx = 0; 1616a930e528SElen Song 16174e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr); 16184e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE); 1619a930e528SElen Song 16204e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNPR, 16214e7decdaSCyrille Pitchen atmel_port->pdc_rx[1].dma_addr); 16224e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE); 1623a930e528SElen Song 1624a930e528SElen Song return 0; 1625a930e528SElen Song } 1626a930e528SElen Song 1627ab4382d2SGreg Kroah-Hartman /* 1628ab4382d2SGreg Kroah-Hartman * tasklet handling tty stuff outside the interrupt handler. 1629ab4382d2SGreg Kroah-Hartman */ 163000e8e658SNicolas Ferre static void atmel_tasklet_rx_func(unsigned long data) 1631ab4382d2SGreg Kroah-Hartman { 1632ab4382d2SGreg Kroah-Hartman struct uart_port *port = (struct uart_port *)data; 1633ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1634ab4382d2SGreg Kroah-Hartman 1635ab4382d2SGreg Kroah-Hartman /* The interrupt handler does not take the lock */ 1636ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1637a930e528SElen Song atmel_port->schedule_rx(port); 163800e8e658SNicolas Ferre spin_unlock(&port->lock); 163900e8e658SNicolas Ferre } 1640ab4382d2SGreg Kroah-Hartman 164100e8e658SNicolas Ferre static void atmel_tasklet_tx_func(unsigned long data) 164200e8e658SNicolas Ferre { 164300e8e658SNicolas Ferre struct uart_port *port = (struct uart_port *)data; 164400e8e658SNicolas Ferre struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 164500e8e658SNicolas Ferre 164600e8e658SNicolas Ferre /* The interrupt handler does not take the lock */ 164700e8e658SNicolas Ferre spin_lock(&port->lock); 164800e8e658SNicolas Ferre atmel_port->schedule_tx(port); 1649ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 1650ab4382d2SGreg Kroah-Hartman } 1651ab4382d2SGreg Kroah-Hartman 16524a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port, 165333d64c4fSElen Song struct platform_device *pdev) 165433d64c4fSElen Song { 165533d64c4fSElen Song struct device_node *np = pdev->dev.of_node; 1656574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 165733d64c4fSElen Song 165833d64c4fSElen Song if (np) { 165933d64c4fSElen Song /* DMA/PDC usage specification */ 1660490d5ce2SJulia Lawall if (of_property_read_bool(np, "atmel,use-dma-rx")) { 1661490d5ce2SJulia Lawall if (of_property_read_bool(np, "dmas")) { 166233d64c4fSElen Song atmel_port->use_dma_rx = true; 166333d64c4fSElen Song atmel_port->use_pdc_rx = false; 166433d64c4fSElen Song } else { 166533d64c4fSElen Song atmel_port->use_dma_rx = false; 166633d64c4fSElen Song atmel_port->use_pdc_rx = true; 166733d64c4fSElen Song } 166833d64c4fSElen Song } else { 166933d64c4fSElen Song atmel_port->use_dma_rx = false; 167033d64c4fSElen Song atmel_port->use_pdc_rx = false; 167133d64c4fSElen Song } 167233d64c4fSElen Song 1673490d5ce2SJulia Lawall if (of_property_read_bool(np, "atmel,use-dma-tx")) { 1674490d5ce2SJulia Lawall if (of_property_read_bool(np, "dmas")) { 167533d64c4fSElen Song atmel_port->use_dma_tx = true; 167633d64c4fSElen Song atmel_port->use_pdc_tx = false; 167733d64c4fSElen Song } else { 167833d64c4fSElen Song atmel_port->use_dma_tx = false; 167933d64c4fSElen Song atmel_port->use_pdc_tx = true; 168033d64c4fSElen Song } 168133d64c4fSElen Song } else { 168233d64c4fSElen Song atmel_port->use_dma_tx = false; 168333d64c4fSElen Song atmel_port->use_pdc_tx = false; 168433d64c4fSElen Song } 168533d64c4fSElen Song 168633d64c4fSElen Song } else { 168733d64c4fSElen Song atmel_port->use_pdc_rx = pdata->use_dma_rx; 168833d64c4fSElen Song atmel_port->use_pdc_tx = pdata->use_dma_tx; 168933d64c4fSElen Song atmel_port->use_dma_rx = false; 169033d64c4fSElen Song atmel_port->use_dma_tx = false; 169133d64c4fSElen Song } 169233d64c4fSElen Song 169333d64c4fSElen Song } 169433d64c4fSElen Song 169513bd3e6fSRicardo Ribalda Delgado static void atmel_init_rs485(struct uart_port *port, 169633d64c4fSElen Song struct platform_device *pdev) 169733d64c4fSElen Song { 169833d64c4fSElen Song struct device_node *np = pdev->dev.of_node; 1699574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 170033d64c4fSElen Song 170133d64c4fSElen Song if (np) { 170277bdec6fSJiri Slaby struct serial_rs485 *rs485conf = &port->rs485; 170333d64c4fSElen Song u32 rs485_delay[2]; 170433d64c4fSElen Song /* rs485 properties */ 170533d64c4fSElen Song if (of_property_read_u32_array(np, "rs485-rts-delay", 170633d64c4fSElen Song rs485_delay, 2) == 0) { 170733d64c4fSElen Song rs485conf->delay_rts_before_send = rs485_delay[0]; 170833d64c4fSElen Song rs485conf->delay_rts_after_send = rs485_delay[1]; 170933d64c4fSElen Song rs485conf->flags = 0; 171077bdec6fSJiri Slaby } 171133d64c4fSElen Song 171233d64c4fSElen Song if (of_get_property(np, "rs485-rx-during-tx", NULL)) 171333d64c4fSElen Song rs485conf->flags |= SER_RS485_RX_DURING_TX; 171433d64c4fSElen Song 171533d64c4fSElen Song if (of_get_property(np, "linux,rs485-enabled-at-boot-time", 171633d64c4fSElen Song NULL)) 171733d64c4fSElen Song rs485conf->flags |= SER_RS485_ENABLED; 171833d64c4fSElen Song } else { 171913bd3e6fSRicardo Ribalda Delgado port->rs485 = pdata->rs485; 172033d64c4fSElen Song } 172133d64c4fSElen Song 172233d64c4fSElen Song } 172333d64c4fSElen Song 1724a930e528SElen Song static void atmel_set_ops(struct uart_port *port) 1725a930e528SElen Song { 1726a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1727a930e528SElen Song 172834df42f5SElen Song if (atmel_use_dma_rx(port)) { 172934df42f5SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_dma; 173034df42f5SElen Song atmel_port->schedule_rx = &atmel_rx_from_dma; 173134df42f5SElen Song atmel_port->release_rx = &atmel_release_rx_dma; 173234df42f5SElen Song } else if (atmel_use_pdc_rx(port)) { 1733a930e528SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_pdc; 1734a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_pdc; 1735a930e528SElen Song atmel_port->release_rx = &atmel_release_rx_pdc; 1736a930e528SElen Song } else { 1737a930e528SElen Song atmel_port->prepare_rx = NULL; 1738a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_ring; 1739a930e528SElen Song atmel_port->release_rx = NULL; 1740a930e528SElen Song } 1741a930e528SElen Song 174208f738beSElen Song if (atmel_use_dma_tx(port)) { 174308f738beSElen Song atmel_port->prepare_tx = &atmel_prepare_tx_dma; 174408f738beSElen Song atmel_port->schedule_tx = &atmel_tx_dma; 174508f738beSElen Song atmel_port->release_tx = &atmel_release_tx_dma; 174608f738beSElen Song } else if (atmel_use_pdc_tx(port)) { 1747a930e528SElen Song atmel_port->prepare_tx = &atmel_prepare_tx_pdc; 1748a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_pdc; 1749a930e528SElen Song atmel_port->release_tx = &atmel_release_tx_pdc; 1750a930e528SElen Song } else { 1751a930e528SElen Song atmel_port->prepare_tx = NULL; 1752a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_chars; 1753a930e528SElen Song atmel_port->release_tx = NULL; 1754a930e528SElen Song } 1755a930e528SElen Song } 1756a930e528SElen Song 1757ab4382d2SGreg Kroah-Hartman /* 1758055560b0SElen Song * Get ip name usart or uart 1759055560b0SElen Song */ 1760892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port) 1761055560b0SElen Song { 1762055560b0SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 17634e7decdaSCyrille Pitchen int name = atmel_uart_readl(port, ATMEL_US_NAME); 1764731d9caeSNicolas Ferre u32 version; 17651d673fb9SNicolas Ferre u32 usart, dbgu_uart, new_uart; 17664b769371SNicolas Ferre /* ASCII decoding for IP version */ 17674b769371SNicolas Ferre usart = 0x55534152; /* USAR(T) */ 17684b769371SNicolas Ferre dbgu_uart = 0x44424755; /* DBGU */ 17691d673fb9SNicolas Ferre new_uart = 0x55415254; /* UART */ 1770055560b0SElen Song 17715bf5635aSLudovic Desroches /* 17725bf5635aSLudovic Desroches * Only USART devices from at91sam9260 SOC implement fractional 17732867af2dSRomain Izard * baudrate. It is available for all asynchronous modes, with the 17742867af2dSRomain Izard * following restriction: the sampling clock's duty cycle is not 17752867af2dSRomain Izard * constant. 17765bf5635aSLudovic Desroches */ 17775bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = false; 17784b769371SNicolas Ferre atmel_port->has_hw_timer = false; 1779055560b0SElen Song 17802958cceeSLudovic Desroches if (name == new_uart) { 17812958cceeSLudovic Desroches dev_dbg(port->dev, "Uart with hw timer"); 17824b769371SNicolas Ferre atmel_port->has_hw_timer = true; 17832958cceeSLudovic Desroches atmel_port->rtor = ATMEL_UA_RTOR; 17842958cceeSLudovic Desroches } else if (name == usart) { 17852958cceeSLudovic Desroches dev_dbg(port->dev, "Usart\n"); 17865bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = true; 17872958cceeSLudovic Desroches atmel_port->has_hw_timer = true; 17882958cceeSLudovic Desroches atmel_port->rtor = ATMEL_US_RTOR; 17894b769371SNicolas Ferre } else if (name == dbgu_uart) { 17904b769371SNicolas Ferre dev_dbg(port->dev, "Dbgu or uart without hw timer\n"); 1791055560b0SElen Song } else { 1792731d9caeSNicolas Ferre /* fallback for older SoCs: use version field */ 17934e7decdaSCyrille Pitchen version = atmel_uart_readl(port, ATMEL_US_VERSION); 1794731d9caeSNicolas Ferre switch (version) { 1795731d9caeSNicolas Ferre case 0x302: 1796731d9caeSNicolas Ferre case 0x10213: 1797731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is usart\n"); 17985bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = true; 17994b769371SNicolas Ferre atmel_port->has_hw_timer = true; 18002958cceeSLudovic Desroches atmel_port->rtor = ATMEL_US_RTOR; 1801731d9caeSNicolas Ferre break; 1802731d9caeSNicolas Ferre case 0x203: 1803731d9caeSNicolas Ferre case 0x10202: 1804731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is uart\n"); 1805731d9caeSNicolas Ferre break; 1806731d9caeSNicolas Ferre default: 1807731d9caeSNicolas Ferre dev_err(port->dev, "Not supported ip name nor version, set to uart\n"); 1808731d9caeSNicolas Ferre } 1809055560b0SElen Song } 1810055560b0SElen Song } 1811055560b0SElen Song 1812055560b0SElen Song /* 1813ab4382d2SGreg Kroah-Hartman * Perform initialization and enable port for reception 1814ab4382d2SGreg Kroah-Hartman */ 1815ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port) 1816ab4382d2SGreg Kroah-Hartman { 181733d64c4fSElen Song struct platform_device *pdev = to_platform_device(port->dev); 1818ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1819ab4382d2SGreg Kroah-Hartman struct tty_struct *tty = port->state->port.tty; 1820ab4382d2SGreg Kroah-Hartman int retval; 1821ab4382d2SGreg Kroah-Hartman 1822ab4382d2SGreg Kroah-Hartman /* 1823ab4382d2SGreg Kroah-Hartman * Ensure that no interrupts are enabled otherwise when 1824ab4382d2SGreg Kroah-Hartman * request_irq() is called we could get stuck trying to 1825ab4382d2SGreg Kroah-Hartman * handle an unexpected interrupt 1826ab4382d2SGreg Kroah-Hartman */ 18274e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 1828ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = false; 1829ab4382d2SGreg Kroah-Hartman 1830ab4382d2SGreg Kroah-Hartman /* 1831ab4382d2SGreg Kroah-Hartman * Allocate the IRQ 1832ab4382d2SGreg Kroah-Hartman */ 18332c7af5baSBoris BREZILLON retval = request_irq(port->irq, atmel_interrupt, 18342c7af5baSBoris BREZILLON IRQF_SHARED | IRQF_COND_SUSPEND, 1835ab4382d2SGreg Kroah-Hartman tty ? tty->name : "atmel_serial", port); 1836ab4382d2SGreg Kroah-Hartman if (retval) { 1837ddaa6037SRichard Genoud dev_err(port->dev, "atmel_startup - Can't get irq\n"); 1838ab4382d2SGreg Kroah-Hartman return retval; 1839ab4382d2SGreg Kroah-Hartman } 1840ab4382d2SGreg Kroah-Hartman 184198f2082cSNicolas Ferre atomic_set(&atmel_port->tasklet_shutdown, 0); 184298f2082cSNicolas Ferre tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func, 184398f2082cSNicolas Ferre (unsigned long)port); 184498f2082cSNicolas Ferre tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func, 184598f2082cSNicolas Ferre (unsigned long)port); 18461e125786SLeilei Zhao 1847ab5e4e41SRichard Genoud /* 1848ab4382d2SGreg Kroah-Hartman * Initialize DMA (if necessary) 1849ab4382d2SGreg Kroah-Hartman */ 185033d64c4fSElen Song atmel_init_property(atmel_port, pdev); 18514d9628a1SLeilei Zhao atmel_set_ops(port); 185233d64c4fSElen Song 1853a930e528SElen Song if (atmel_port->prepare_rx) { 1854a930e528SElen Song retval = atmel_port->prepare_rx(port); 1855a930e528SElen Song if (retval < 0) 1856a930e528SElen Song atmel_set_ops(port); 1857ab4382d2SGreg Kroah-Hartman } 1858ab4382d2SGreg Kroah-Hartman 1859a930e528SElen Song if (atmel_port->prepare_tx) { 1860a930e528SElen Song retval = atmel_port->prepare_tx(port); 1861a930e528SElen Song if (retval < 0) 1862a930e528SElen Song atmel_set_ops(port); 1863ab4382d2SGreg Kroah-Hartman } 1864ab4382d2SGreg Kroah-Hartman 1865b5199d46SCyrille Pitchen /* 1866b5199d46SCyrille Pitchen * Enable FIFO when available 1867b5199d46SCyrille Pitchen */ 1868b5199d46SCyrille Pitchen if (atmel_port->fifo_size) { 1869b5199d46SCyrille Pitchen unsigned int txrdym = ATMEL_US_ONE_DATA; 1870b5199d46SCyrille Pitchen unsigned int rxrdym = ATMEL_US_ONE_DATA; 1871b5199d46SCyrille Pitchen unsigned int fmr; 1872b5199d46SCyrille Pitchen 1873b5199d46SCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, 1874b5199d46SCyrille Pitchen ATMEL_US_FIFOEN | 1875b5199d46SCyrille Pitchen ATMEL_US_RXFCLR | 1876b5199d46SCyrille Pitchen ATMEL_US_TXFLCLR); 1877b5199d46SCyrille Pitchen 18785f258b3eSCyrille Pitchen if (atmel_use_dma_tx(port)) 18795f258b3eSCyrille Pitchen txrdym = ATMEL_US_FOUR_DATA; 18805f258b3eSCyrille Pitchen 1881b5199d46SCyrille Pitchen fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym); 1882b5199d46SCyrille Pitchen if (atmel_port->rts_high && 1883b5199d46SCyrille Pitchen atmel_port->rts_low) 1884b5199d46SCyrille Pitchen fmr |= ATMEL_US_FRTSC | 1885b5199d46SCyrille Pitchen ATMEL_US_RXFTHRES(atmel_port->rts_high) | 1886b5199d46SCyrille Pitchen ATMEL_US_RXFTHRES2(atmel_port->rts_low); 1887b5199d46SCyrille Pitchen 1888b5199d46SCyrille Pitchen atmel_uart_writel(port, ATMEL_US_FMR, fmr); 1889b5199d46SCyrille Pitchen } 1890b5199d46SCyrille Pitchen 1891ab4382d2SGreg Kroah-Hartman /* Save current CSR for comparison in atmel_tasklet_func() */ 1892e0b0baadSRichard Genoud atmel_port->irq_status_prev = atmel_get_lines_status(port); 1893ab4382d2SGreg Kroah-Hartman 1894ab4382d2SGreg Kroah-Hartman /* 1895ab4382d2SGreg Kroah-Hartman * Finally, enable the serial port 1896ab4382d2SGreg Kroah-Hartman */ 18974e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 1898ab4382d2SGreg Kroah-Hartman /* enable xmit & rcvr */ 18994e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 1900ab4382d2SGreg Kroah-Hartman 19012e68c22fSElen Song setup_timer(&atmel_port->uart_timer, 19022e68c22fSElen Song atmel_uart_timer_callback, 19032e68c22fSElen Song (unsigned long)port); 19048bc661bfSMarek Roszko 19058bc661bfSMarek Roszko if (atmel_use_pdc_rx(port)) { 19068bc661bfSMarek Roszko /* set UART timeout */ 19074b769371SNicolas Ferre if (!atmel_port->has_hw_timer) { 19082e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 19092e68c22fSElen Song jiffies + uart_poll_timeout(port)); 19102e68c22fSElen Song /* set USART timeout */ 19112e68c22fSElen Song } else { 19122958cceeSLudovic Desroches atmel_uart_writel(port, atmel_port->rtor, 19132958cceeSLudovic Desroches PDC_RX_TIMEOUT); 19144e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1915ab4382d2SGreg Kroah-Hartman 19164e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 19174e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 19182e68c22fSElen Song } 1919ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 19204e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); 192134df42f5SElen Song } else if (atmel_use_dma_rx(port)) { 19222e68c22fSElen Song /* set UART timeout */ 19234b769371SNicolas Ferre if (!atmel_port->has_hw_timer) { 19242e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 19252e68c22fSElen Song jiffies + uart_poll_timeout(port)); 19262e68c22fSElen Song /* set USART timeout */ 19272e68c22fSElen Song } else { 19282958cceeSLudovic Desroches atmel_uart_writel(port, atmel_port->rtor, 19292958cceeSLudovic Desroches PDC_RX_TIMEOUT); 19304e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 193134df42f5SElen Song 19324e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 19334e7decdaSCyrille Pitchen ATMEL_US_TIMEOUT); 19342e68c22fSElen Song } 1935ab4382d2SGreg Kroah-Hartman } else { 1936ab4382d2SGreg Kroah-Hartman /* enable receive only */ 19374e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY); 1938ab4382d2SGreg Kroah-Hartman } 1939ab4382d2SGreg Kroah-Hartman 1940ab4382d2SGreg Kroah-Hartman return 0; 1941ab4382d2SGreg Kroah-Hartman } 1942ab4382d2SGreg Kroah-Hartman 1943ab4382d2SGreg Kroah-Hartman /* 1944479e9b94SPeter Hurley * Flush any TX data submitted for DMA. Called when the TX circular 1945479e9b94SPeter Hurley * buffer is reset. 1946479e9b94SPeter Hurley */ 1947479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port) 1948479e9b94SPeter Hurley { 1949479e9b94SPeter Hurley struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1950479e9b94SPeter Hurley 1951479e9b94SPeter Hurley if (atmel_use_pdc_tx(port)) { 19524e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TCR, 0); 1953479e9b94SPeter Hurley atmel_port->pdc_tx.ofs = 0; 1954479e9b94SPeter Hurley } 1955479e9b94SPeter Hurley } 1956479e9b94SPeter Hurley 1957479e9b94SPeter Hurley /* 1958ab4382d2SGreg Kroah-Hartman * Disable the port 1959ab4382d2SGreg Kroah-Hartman */ 1960ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port) 1961ab4382d2SGreg Kroah-Hartman { 1962ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 19630cc7c6c7SMarek Roszko 19640ae9fdefSRichard Genoud /* Disable modem control lines interrupts */ 19650ae9fdefSRichard Genoud atmel_disable_ms(port); 19660ae9fdefSRichard Genoud 196798f2082cSNicolas Ferre /* Disable interrupts at device level */ 196898f2082cSNicolas Ferre atmel_uart_writel(port, ATMEL_US_IDR, -1); 196998f2082cSNicolas Ferre 197098f2082cSNicolas Ferre /* Prevent spurious interrupts from scheduling the tasklet */ 197198f2082cSNicolas Ferre atomic_inc(&atmel_port->tasklet_shutdown); 197298f2082cSNicolas Ferre 1973ab4382d2SGreg Kroah-Hartman /* 19748bc661bfSMarek Roszko * Prevent any tasklets being scheduled during 19758bc661bfSMarek Roszko * cleanup 19768bc661bfSMarek Roszko */ 19778bc661bfSMarek Roszko del_timer_sync(&atmel_port->uart_timer); 19788bc661bfSMarek Roszko 197998f2082cSNicolas Ferre /* Make sure that no interrupt is on the fly */ 198098f2082cSNicolas Ferre synchronize_irq(port->irq); 198198f2082cSNicolas Ferre 19828bc661bfSMarek Roszko /* 19830cc7c6c7SMarek Roszko * Clear out any scheduled tasklets before 19840cc7c6c7SMarek Roszko * we destroy the buffers 19850cc7c6c7SMarek Roszko */ 198600e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_rx); 198700e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_tx); 19880cc7c6c7SMarek Roszko 19890cc7c6c7SMarek Roszko /* 19900cc7c6c7SMarek Roszko * Ensure everything is stopped and 199198f2082cSNicolas Ferre * disable port and break condition. 1992ab4382d2SGreg Kroah-Hartman */ 1993ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 1994ab4382d2SGreg Kroah-Hartman atmel_stop_tx(port); 1995ab4382d2SGreg Kroah-Hartman 19964e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 19970cc7c6c7SMarek Roszko 1998ab4382d2SGreg Kroah-Hartman /* 1999ab4382d2SGreg Kroah-Hartman * Shut-down the DMA. 2000ab4382d2SGreg Kroah-Hartman */ 2001a930e528SElen Song if (atmel_port->release_rx) 2002a930e528SElen Song atmel_port->release_rx(port); 2003a930e528SElen Song if (atmel_port->release_tx) 2004a930e528SElen Song atmel_port->release_tx(port); 2005ab4382d2SGreg Kroah-Hartman 2006ab4382d2SGreg Kroah-Hartman /* 2007bb7e73c5SMark Deneen * Reset ring buffer pointers 2008bb7e73c5SMark Deneen */ 2009bb7e73c5SMark Deneen atmel_port->rx_ring.head = 0; 2010bb7e73c5SMark Deneen atmel_port->rx_ring.tail = 0; 2011bb7e73c5SMark Deneen 2012bb7e73c5SMark Deneen /* 2013ab5e4e41SRichard Genoud * Free the interrupts 2014ab4382d2SGreg Kroah-Hartman */ 2015ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 2016ab5e4e41SRichard Genoud 2017479e9b94SPeter Hurley atmel_flush_buffer(port); 2018ab4382d2SGreg Kroah-Hartman } 2019ab4382d2SGreg Kroah-Hartman 2020ab4382d2SGreg Kroah-Hartman /* 2021ab4382d2SGreg Kroah-Hartman * Power / Clock management. 2022ab4382d2SGreg Kroah-Hartman */ 2023ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state, 2024ab4382d2SGreg Kroah-Hartman unsigned int oldstate) 2025ab4382d2SGreg Kroah-Hartman { 2026ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2027ab4382d2SGreg Kroah-Hartman 2028ab4382d2SGreg Kroah-Hartman switch (state) { 2029ab4382d2SGreg Kroah-Hartman case 0: 2030ab4382d2SGreg Kroah-Hartman /* 2031ab4382d2SGreg Kroah-Hartman * Enable the peripheral clock for this serial port. 2032ab4382d2SGreg Kroah-Hartman * This is called on uart_open() or a resume event. 2033ab4382d2SGreg Kroah-Hartman */ 203491f8c2d8SBoris BREZILLON clk_prepare_enable(atmel_port->clk); 2035ab4382d2SGreg Kroah-Hartman 2036ab4382d2SGreg Kroah-Hartman /* re-enable interrupts if we disabled some on suspend */ 20374e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr); 2038ab4382d2SGreg Kroah-Hartman break; 2039ab4382d2SGreg Kroah-Hartman case 3: 2040ab4382d2SGreg Kroah-Hartman /* Back up the interrupt mask and disable all interrupts */ 20414e7decdaSCyrille Pitchen atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR); 20424e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 2043ab4382d2SGreg Kroah-Hartman 2044ab4382d2SGreg Kroah-Hartman /* 2045ab4382d2SGreg Kroah-Hartman * Disable the peripheral clock for this serial port. 2046ab4382d2SGreg Kroah-Hartman * This is called on uart_close() or a suspend event. 2047ab4382d2SGreg Kroah-Hartman */ 204891f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 2049ab4382d2SGreg Kroah-Hartman break; 2050ab4382d2SGreg Kroah-Hartman default: 2051ddaa6037SRichard Genoud dev_err(port->dev, "atmel_serial: unknown pm %d\n", state); 2052ab4382d2SGreg Kroah-Hartman } 2053ab4382d2SGreg Kroah-Hartman } 2054ab4382d2SGreg Kroah-Hartman 2055ab4382d2SGreg Kroah-Hartman /* 2056ab4382d2SGreg Kroah-Hartman * Change the port parameters 2057ab4382d2SGreg Kroah-Hartman */ 2058ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, 2059ab4382d2SGreg Kroah-Hartman struct ktermios *old) 2060ab4382d2SGreg Kroah-Hartman { 20615bf5635aSLudovic Desroches struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2062ab4382d2SGreg Kroah-Hartman unsigned long flags; 20635bf5635aSLudovic Desroches unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0; 2064ab4382d2SGreg Kroah-Hartman 20651cf6e8fcSCyrille Pitchen /* save the current mode register */ 20664e7decdaSCyrille Pitchen mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR); 20671cf6e8fcSCyrille Pitchen 20681cf6e8fcSCyrille Pitchen /* reset the mode, clock divisor, parity, stop bits and data size */ 20691cf6e8fcSCyrille Pitchen mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP | 20701cf6e8fcSCyrille Pitchen ATMEL_US_PAR | ATMEL_US_USMODE); 2071ab4382d2SGreg Kroah-Hartman 2072ab4382d2SGreg Kroah-Hartman baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); 2073ab4382d2SGreg Kroah-Hartman 2074ab4382d2SGreg Kroah-Hartman /* byte size */ 2075ab4382d2SGreg Kroah-Hartman switch (termios->c_cflag & CSIZE) { 2076ab4382d2SGreg Kroah-Hartman case CS5: 2077ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_5; 2078ab4382d2SGreg Kroah-Hartman break; 2079ab4382d2SGreg Kroah-Hartman case CS6: 2080ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_6; 2081ab4382d2SGreg Kroah-Hartman break; 2082ab4382d2SGreg Kroah-Hartman case CS7: 2083ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_7; 2084ab4382d2SGreg Kroah-Hartman break; 2085ab4382d2SGreg Kroah-Hartman default: 2086ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_8; 2087ab4382d2SGreg Kroah-Hartman break; 2088ab4382d2SGreg Kroah-Hartman } 2089ab4382d2SGreg Kroah-Hartman 2090ab4382d2SGreg Kroah-Hartman /* stop bits */ 2091ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CSTOPB) 2092ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_NBSTOP_2; 2093ab4382d2SGreg Kroah-Hartman 2094ab4382d2SGreg Kroah-Hartman /* parity */ 2095ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARENB) { 2096ab4382d2SGreg Kroah-Hartman /* Mark or Space parity */ 2097ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CMSPAR) { 2098ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARODD) 2099ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_MARK; 2100ab4382d2SGreg Kroah-Hartman else 2101ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_SPACE; 2102ab4382d2SGreg Kroah-Hartman } else if (termios->c_cflag & PARODD) 2103ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_ODD; 2104ab4382d2SGreg Kroah-Hartman else 2105ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_EVEN; 2106ab4382d2SGreg Kroah-Hartman } else 2107ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_NONE; 2108ab4382d2SGreg Kroah-Hartman 2109ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 2110ab4382d2SGreg Kroah-Hartman 2111ab4382d2SGreg Kroah-Hartman port->read_status_mask = ATMEL_US_OVRE; 2112ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & INPCK) 2113ab4382d2SGreg Kroah-Hartman port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2114ef8b9ddcSPeter Hurley if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 2115ab4382d2SGreg Kroah-Hartman port->read_status_mask |= ATMEL_US_RXBRK; 2116ab4382d2SGreg Kroah-Hartman 211764e22ebeSElen Song if (atmel_use_pdc_rx(port)) 2118ab4382d2SGreg Kroah-Hartman /* need to enable error interrupts */ 21194e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask); 2120ab4382d2SGreg Kroah-Hartman 2121ab4382d2SGreg Kroah-Hartman /* 2122ab4382d2SGreg Kroah-Hartman * Characters to ignore 2123ab4382d2SGreg Kroah-Hartman */ 2124ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 2125ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 2126ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2127ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNBRK) { 2128ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_RXBRK; 2129ab4382d2SGreg Kroah-Hartman /* 2130ab4382d2SGreg Kroah-Hartman * If we're ignoring parity and break indicators, 2131ab4382d2SGreg Kroah-Hartman * ignore overruns too (for real raw support). 2132ab4382d2SGreg Kroah-Hartman */ 2133ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 2134ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_OVRE; 2135ab4382d2SGreg Kroah-Hartman } 2136ab4382d2SGreg Kroah-Hartman /* TODO: Ignore all characters if CREAD is set.*/ 2137ab4382d2SGreg Kroah-Hartman 2138ab4382d2SGreg Kroah-Hartman /* update the per-port timeout */ 2139ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, termios->c_cflag, baud); 2140ab4382d2SGreg Kroah-Hartman 2141ab4382d2SGreg Kroah-Hartman /* 2142ab4382d2SGreg Kroah-Hartman * save/disable interrupts. The tty layer will ensure that the 2143ab4382d2SGreg Kroah-Hartman * transmitter is empty if requested by the caller, so there's 2144ab4382d2SGreg Kroah-Hartman * no need to wait for it here. 2145ab4382d2SGreg Kroah-Hartman */ 21464e7decdaSCyrille Pitchen imr = atmel_uart_readl(port, ATMEL_US_IMR); 21474e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 2148ab4382d2SGreg Kroah-Hartman 2149ab4382d2SGreg Kroah-Hartman /* disable receiver and transmitter */ 21504e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS); 2151ab4382d2SGreg Kroah-Hartman 21521cf6e8fcSCyrille Pitchen /* mode */ 215313bd3e6fSRicardo Ribalda Delgado if (port->rs485.flags & SER_RS485_ENABLED) { 21544e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 21554e7decdaSCyrille Pitchen port->rs485.delay_rts_after_send); 2156ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 21571cf6e8fcSCyrille Pitchen } else if (termios->c_cflag & CRTSCTS) { 21581cf6e8fcSCyrille Pitchen /* RS232 with hardware handshake (RTS/CTS) */ 21599bcffe75SRichard Genoud if (atmel_use_fifo(port) && 21609bcffe75SRichard Genoud !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) { 21619bcffe75SRichard Genoud /* 21629bcffe75SRichard Genoud * with ATMEL_US_USMODE_HWHS set, the controller will 21639bcffe75SRichard Genoud * be able to drive the RTS pin high/low when the RX 21649bcffe75SRichard Genoud * FIFO is above RXFTHRES/below RXFTHRES2. 21659bcffe75SRichard Genoud * It will also disable the transmitter when the CTS 21669bcffe75SRichard Genoud * pin is high. 21679bcffe75SRichard Genoud * This mode is not activated if CTS pin is a GPIO 21689bcffe75SRichard Genoud * because in this case, the transmitter is always 21699bcffe75SRichard Genoud * disabled (there must be an internal pull-up 21709bcffe75SRichard Genoud * responsible for this behaviour). 21719bcffe75SRichard Genoud * If the RTS pin is a GPIO, the controller won't be 21729bcffe75SRichard Genoud * able to drive it according to the FIFO thresholds, 21739bcffe75SRichard Genoud * but it will be handled by the driver. 21749bcffe75SRichard Genoud */ 21751cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_HWHS; 21769bcffe75SRichard Genoud } else { 21779bcffe75SRichard Genoud /* 21789bcffe75SRichard Genoud * For platforms without FIFO, the flow control is 21799bcffe75SRichard Genoud * handled by the driver. 21809bcffe75SRichard Genoud */ 21819bcffe75SRichard Genoud mode |= ATMEL_US_USMODE_NORMAL; 21825be605acSAlexandre Belloni } 21831cf6e8fcSCyrille Pitchen } else { 21841cf6e8fcSCyrille Pitchen /* RS232 without hadware handshake */ 21851cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_NORMAL; 2186ab4382d2SGreg Kroah-Hartman } 2187ab4382d2SGreg Kroah-Hartman 21881cf6e8fcSCyrille Pitchen /* set the mode, clock divisor, parity, stop bits and data size */ 21894e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 2190ab4382d2SGreg Kroah-Hartman 21911cf6e8fcSCyrille Pitchen /* 21921cf6e8fcSCyrille Pitchen * when switching the mode, set the RTS line state according to the 21931cf6e8fcSCyrille Pitchen * new mode, otherwise keep the former state 21941cf6e8fcSCyrille Pitchen */ 21951cf6e8fcSCyrille Pitchen if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) { 21961cf6e8fcSCyrille Pitchen unsigned int rts_state; 21971cf6e8fcSCyrille Pitchen 21981cf6e8fcSCyrille Pitchen if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 21991cf6e8fcSCyrille Pitchen /* let the hardware control the RTS line */ 22001cf6e8fcSCyrille Pitchen rts_state = ATMEL_US_RTSDIS; 22011cf6e8fcSCyrille Pitchen } else { 22021cf6e8fcSCyrille Pitchen /* force RTS line to low level */ 22031cf6e8fcSCyrille Pitchen rts_state = ATMEL_US_RTSEN; 22041cf6e8fcSCyrille Pitchen } 22051cf6e8fcSCyrille Pitchen 22064e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, rts_state); 22071cf6e8fcSCyrille Pitchen } 22081cf6e8fcSCyrille Pitchen 22095bf5635aSLudovic Desroches /* 22105bf5635aSLudovic Desroches * Set the baud rate: 22115bf5635aSLudovic Desroches * Fractional baudrate allows to setup output frequency more 22125bf5635aSLudovic Desroches * accurately. This feature is enabled only when using normal mode. 22135bf5635aSLudovic Desroches * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8)) 22145bf5635aSLudovic Desroches * Currently, OVER is always set to 0 so we get 221536131cdfSAlexey Starikovskiy * baudrate = selected clock / (16 * (CD + FP / 8)) 221636131cdfSAlexey Starikovskiy * then 221736131cdfSAlexey Starikovskiy * 8 CD + FP = selected clock / (2 * baudrate) 22185bf5635aSLudovic Desroches */ 22192867af2dSRomain Izard if (atmel_port->has_frac_baudrate) { 222036131cdfSAlexey Starikovskiy div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2); 222136131cdfSAlexey Starikovskiy cd = div >> 3; 222236131cdfSAlexey Starikovskiy fp = div & ATMEL_US_FP_MASK; 22235bf5635aSLudovic Desroches } else { 22245bf5635aSLudovic Desroches cd = uart_get_divisor(port, baud); 22255bf5635aSLudovic Desroches } 22265bf5635aSLudovic Desroches 22275bf5635aSLudovic Desroches if (cd > 65535) { /* BRGR is 16-bit, so switch to slower clock */ 22285bf5635aSLudovic Desroches cd /= 8; 22295bf5635aSLudovic Desroches mode |= ATMEL_US_USCLKS_MCK_DIV8; 22305bf5635aSLudovic Desroches } 22315bf5635aSLudovic Desroches quot = cd | fp << ATMEL_US_FP_OFFSET; 22325bf5635aSLudovic Desroches 22334e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_BRGR, quot); 22344e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 22354e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 2236ab4382d2SGreg Kroah-Hartman 2237ab4382d2SGreg Kroah-Hartman /* restore interrupts */ 22384e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, imr); 2239ab4382d2SGreg Kroah-Hartman 2240ab4382d2SGreg Kroah-Hartman /* CTS flow-control and modem-status interrupts */ 2241ab4382d2SGreg Kroah-Hartman if (UART_ENABLE_MS(port, termios->c_cflag)) 224235b675b9SRichard Genoud atmel_enable_ms(port); 224335b675b9SRichard Genoud else 224435b675b9SRichard Genoud atmel_disable_ms(port); 2245ab4382d2SGreg Kroah-Hartman 2246ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 2247ab4382d2SGreg Kroah-Hartman } 2248ab4382d2SGreg Kroah-Hartman 2249732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios) 225042bd7a4fSViktar Palstsiuk { 2251732a84a0SPeter Hurley if (termios->c_line == N_PPS) { 225242bd7a4fSViktar Palstsiuk port->flags |= UPF_HARDPPS_CD; 2253d41510ceSPeter Hurley spin_lock_irq(&port->lock); 225442bd7a4fSViktar Palstsiuk atmel_enable_ms(port); 2255d41510ceSPeter Hurley spin_unlock_irq(&port->lock); 225642bd7a4fSViktar Palstsiuk } else { 225742bd7a4fSViktar Palstsiuk port->flags &= ~UPF_HARDPPS_CD; 2258cab68f89SPeter Hurley if (!UART_ENABLE_MS(port, termios->c_cflag)) { 2259cab68f89SPeter Hurley spin_lock_irq(&port->lock); 2260cab68f89SPeter Hurley atmel_disable_ms(port); 2261cab68f89SPeter Hurley spin_unlock_irq(&port->lock); 2262cab68f89SPeter Hurley } 226342bd7a4fSViktar Palstsiuk } 226442bd7a4fSViktar Palstsiuk } 226542bd7a4fSViktar Palstsiuk 2266ab4382d2SGreg Kroah-Hartman /* 2267ab4382d2SGreg Kroah-Hartman * Return string describing the specified port 2268ab4382d2SGreg Kroah-Hartman */ 2269ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port) 2270ab4382d2SGreg Kroah-Hartman { 2271ab4382d2SGreg Kroah-Hartman return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; 2272ab4382d2SGreg Kroah-Hartman } 2273ab4382d2SGreg Kroah-Hartman 2274ab4382d2SGreg Kroah-Hartman /* 2275ab4382d2SGreg Kroah-Hartman * Release the memory region(s) being used by 'port'. 2276ab4382d2SGreg Kroah-Hartman */ 2277ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port) 2278ab4382d2SGreg Kroah-Hartman { 2279ab4382d2SGreg Kroah-Hartman struct platform_device *pdev = to_platform_device(port->dev); 2280ab4382d2SGreg Kroah-Hartman int size = pdev->resource[0].end - pdev->resource[0].start + 1; 2281ab4382d2SGreg Kroah-Hartman 2282ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2283ab4382d2SGreg Kroah-Hartman 2284ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2285ab4382d2SGreg Kroah-Hartman iounmap(port->membase); 2286ab4382d2SGreg Kroah-Hartman port->membase = NULL; 2287ab4382d2SGreg Kroah-Hartman } 2288ab4382d2SGreg Kroah-Hartman } 2289ab4382d2SGreg Kroah-Hartman 2290ab4382d2SGreg Kroah-Hartman /* 2291ab4382d2SGreg Kroah-Hartman * Request the memory region(s) being used by 'port'. 2292ab4382d2SGreg Kroah-Hartman */ 2293ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port) 2294ab4382d2SGreg Kroah-Hartman { 2295ab4382d2SGreg Kroah-Hartman struct platform_device *pdev = to_platform_device(port->dev); 2296ab4382d2SGreg Kroah-Hartman int size = pdev->resource[0].end - pdev->resource[0].start + 1; 2297ab4382d2SGreg Kroah-Hartman 2298ab4382d2SGreg Kroah-Hartman if (!request_mem_region(port->mapbase, size, "atmel_serial")) 2299ab4382d2SGreg Kroah-Hartman return -EBUSY; 2300ab4382d2SGreg Kroah-Hartman 2301ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2302ab4382d2SGreg Kroah-Hartman port->membase = ioremap(port->mapbase, size); 2303ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2304ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2305ab4382d2SGreg Kroah-Hartman return -ENOMEM; 2306ab4382d2SGreg Kroah-Hartman } 2307ab4382d2SGreg Kroah-Hartman } 2308ab4382d2SGreg Kroah-Hartman 2309ab4382d2SGreg Kroah-Hartman return 0; 2310ab4382d2SGreg Kroah-Hartman } 2311ab4382d2SGreg Kroah-Hartman 2312ab4382d2SGreg Kroah-Hartman /* 2313ab4382d2SGreg Kroah-Hartman * Configure/autoconfigure the port. 2314ab4382d2SGreg Kroah-Hartman */ 2315ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags) 2316ab4382d2SGreg Kroah-Hartman { 2317ab4382d2SGreg Kroah-Hartman if (flags & UART_CONFIG_TYPE) { 2318ab4382d2SGreg Kroah-Hartman port->type = PORT_ATMEL; 2319ab4382d2SGreg Kroah-Hartman atmel_request_port(port); 2320ab4382d2SGreg Kroah-Hartman } 2321ab4382d2SGreg Kroah-Hartman } 2322ab4382d2SGreg Kroah-Hartman 2323ab4382d2SGreg Kroah-Hartman /* 2324ab4382d2SGreg Kroah-Hartman * Verify the new serial_struct (for TIOCSSERIAL). 2325ab4382d2SGreg Kroah-Hartman */ 2326ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser) 2327ab4382d2SGreg Kroah-Hartman { 2328ab4382d2SGreg Kroah-Hartman int ret = 0; 2329ab4382d2SGreg Kroah-Hartman if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) 2330ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2331ab4382d2SGreg Kroah-Hartman if (port->irq != ser->irq) 2332ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2333ab4382d2SGreg Kroah-Hartman if (ser->io_type != SERIAL_IO_MEM) 2334ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2335ab4382d2SGreg Kroah-Hartman if (port->uartclk / 16 != ser->baud_base) 2336ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2337270c2adeSAndre Przywara if (port->mapbase != (unsigned long)ser->iomem_base) 2338ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2339ab4382d2SGreg Kroah-Hartman if (port->iobase != ser->port) 2340ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2341ab4382d2SGreg Kroah-Hartman if (ser->hub6 != 0) 2342ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2343ab4382d2SGreg Kroah-Hartman return ret; 2344ab4382d2SGreg Kroah-Hartman } 2345ab4382d2SGreg Kroah-Hartman 2346ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2347ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port) 2348ab4382d2SGreg Kroah-Hartman { 23494e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY)) 2350ab4382d2SGreg Kroah-Hartman cpu_relax(); 2351ab4382d2SGreg Kroah-Hartman 2352a6499435SCyrille Pitchen return atmel_uart_read_char(port); 2353ab4382d2SGreg Kroah-Hartman } 2354ab4382d2SGreg Kroah-Hartman 2355ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch) 2356ab4382d2SGreg Kroah-Hartman { 23574e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) 2358ab4382d2SGreg Kroah-Hartman cpu_relax(); 2359ab4382d2SGreg Kroah-Hartman 2360a6499435SCyrille Pitchen atmel_uart_write_char(port, ch); 2361ab4382d2SGreg Kroah-Hartman } 2362ab4382d2SGreg Kroah-Hartman #endif 2363ab4382d2SGreg Kroah-Hartman 23645c7dcdb6SJulia Lawall static const struct uart_ops atmel_pops = { 2365ab4382d2SGreg Kroah-Hartman .tx_empty = atmel_tx_empty, 2366ab4382d2SGreg Kroah-Hartman .set_mctrl = atmel_set_mctrl, 2367ab4382d2SGreg Kroah-Hartman .get_mctrl = atmel_get_mctrl, 2368ab4382d2SGreg Kroah-Hartman .stop_tx = atmel_stop_tx, 2369ab4382d2SGreg Kroah-Hartman .start_tx = atmel_start_tx, 2370ab4382d2SGreg Kroah-Hartman .stop_rx = atmel_stop_rx, 2371ab4382d2SGreg Kroah-Hartman .enable_ms = atmel_enable_ms, 2372ab4382d2SGreg Kroah-Hartman .break_ctl = atmel_break_ctl, 2373ab4382d2SGreg Kroah-Hartman .startup = atmel_startup, 2374ab4382d2SGreg Kroah-Hartman .shutdown = atmel_shutdown, 2375ab4382d2SGreg Kroah-Hartman .flush_buffer = atmel_flush_buffer, 2376ab4382d2SGreg Kroah-Hartman .set_termios = atmel_set_termios, 237742bd7a4fSViktar Palstsiuk .set_ldisc = atmel_set_ldisc, 2378ab4382d2SGreg Kroah-Hartman .type = atmel_type, 2379ab4382d2SGreg Kroah-Hartman .release_port = atmel_release_port, 2380ab4382d2SGreg Kroah-Hartman .request_port = atmel_request_port, 2381ab4382d2SGreg Kroah-Hartman .config_port = atmel_config_port, 2382ab4382d2SGreg Kroah-Hartman .verify_port = atmel_verify_port, 2383ab4382d2SGreg Kroah-Hartman .pm = atmel_serial_pm, 2384ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2385ab4382d2SGreg Kroah-Hartman .poll_get_char = atmel_poll_get_char, 2386ab4382d2SGreg Kroah-Hartman .poll_put_char = atmel_poll_put_char, 2387ab4382d2SGreg Kroah-Hartman #endif 2388ab4382d2SGreg Kroah-Hartman }; 2389ab4382d2SGreg Kroah-Hartman 2390ab4382d2SGreg Kroah-Hartman /* 2391ab4382d2SGreg Kroah-Hartman * Configure the port from the platform device resource info. 2392ab4382d2SGreg Kroah-Hartman */ 239391f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port, 2394ab4382d2SGreg Kroah-Hartman struct platform_device *pdev) 2395ab4382d2SGreg Kroah-Hartman { 239691f8c2d8SBoris BREZILLON int ret; 2397ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_port->uart; 2398574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 2399ab4382d2SGreg Kroah-Hartman 24004a1e8888SLeilei Zhao atmel_init_property(atmel_port, pdev); 2401a930e528SElen Song atmel_set_ops(port); 2402a930e528SElen Song 240313bd3e6fSRicardo Ribalda Delgado atmel_init_rs485(port, pdev); 240433d64c4fSElen Song 2405ab4382d2SGreg Kroah-Hartman port->iotype = UPIO_MEM; 2406ab4382d2SGreg Kroah-Hartman port->flags = UPF_BOOT_AUTOCONF; 2407ab4382d2SGreg Kroah-Hartman port->ops = &atmel_pops; 2408ab4382d2SGreg Kroah-Hartman port->fifosize = 1; 2409ab4382d2SGreg Kroah-Hartman port->dev = &pdev->dev; 2410ab4382d2SGreg Kroah-Hartman port->mapbase = pdev->resource[0].start; 2411ab4382d2SGreg Kroah-Hartman port->irq = pdev->resource[1].start; 241213bd3e6fSRicardo Ribalda Delgado port->rs485_config = atmel_config_rs485; 2413ab4382d2SGreg Kroah-Hartman 2414ab4382d2SGreg Kroah-Hartman memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); 2415ab4382d2SGreg Kroah-Hartman 24165fbe46b6SNicolas Ferre if (pdata && pdata->regs) { 2417ab4382d2SGreg Kroah-Hartman /* Already mapped by setup code */ 24181acfc7ecSNicolas Ferre port->membase = pdata->regs; 2419588edbf3SNicolas Ferre } else { 2420ab4382d2SGreg Kroah-Hartman port->flags |= UPF_IOREMAP; 2421ab4382d2SGreg Kroah-Hartman port->membase = NULL; 2422ab4382d2SGreg Kroah-Hartman } 2423ab4382d2SGreg Kroah-Hartman 2424ab4382d2SGreg Kroah-Hartman /* for console, the clock could already be configured */ 2425ab4382d2SGreg Kroah-Hartman if (!atmel_port->clk) { 2426ab4382d2SGreg Kroah-Hartman atmel_port->clk = clk_get(&pdev->dev, "usart"); 242791f8c2d8SBoris BREZILLON if (IS_ERR(atmel_port->clk)) { 242891f8c2d8SBoris BREZILLON ret = PTR_ERR(atmel_port->clk); 242991f8c2d8SBoris BREZILLON atmel_port->clk = NULL; 243091f8c2d8SBoris BREZILLON return ret; 243191f8c2d8SBoris BREZILLON } 243291f8c2d8SBoris BREZILLON ret = clk_prepare_enable(atmel_port->clk); 243391f8c2d8SBoris BREZILLON if (ret) { 243491f8c2d8SBoris BREZILLON clk_put(atmel_port->clk); 243591f8c2d8SBoris BREZILLON atmel_port->clk = NULL; 243691f8c2d8SBoris BREZILLON return ret; 243791f8c2d8SBoris BREZILLON } 2438ab4382d2SGreg Kroah-Hartman port->uartclk = clk_get_rate(atmel_port->clk); 243991f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 2440ab4382d2SGreg Kroah-Hartman /* only enable clock when USART is in use */ 2441ab4382d2SGreg Kroah-Hartman } 2442ab4382d2SGreg Kroah-Hartman 2443ab4382d2SGreg Kroah-Hartman /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ 244413bd3e6fSRicardo Ribalda Delgado if (port->rs485.flags & SER_RS485_ENABLED) 2445ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 244664e22ebeSElen Song else if (atmel_use_pdc_tx(port)) { 2447ab4382d2SGreg Kroah-Hartman port->fifosize = PDC_BUFFER_SIZE; 2448ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; 2449ab4382d2SGreg Kroah-Hartman } else { 2450ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 2451ab4382d2SGreg Kroah-Hartman } 245291f8c2d8SBoris BREZILLON 245391f8c2d8SBoris BREZILLON return 0; 2454ab4382d2SGreg Kroah-Hartman } 2455ab4382d2SGreg Kroah-Hartman 245669f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device; /* the serial console device */ 245769f6a27bSJean-Christophe PLAGNIOL-VILLARD 2458ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2459ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch) 2460ab4382d2SGreg Kroah-Hartman { 24614e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) 2462ab4382d2SGreg Kroah-Hartman cpu_relax(); 2463a6499435SCyrille Pitchen atmel_uart_write_char(port, ch); 2464ab4382d2SGreg Kroah-Hartman } 2465ab4382d2SGreg Kroah-Hartman 2466ab4382d2SGreg Kroah-Hartman /* 2467ab4382d2SGreg Kroah-Hartman * Interrupts are disabled on entering 2468ab4382d2SGreg Kroah-Hartman */ 2469ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count) 2470ab4382d2SGreg Kroah-Hartman { 2471ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2472ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2473ab4382d2SGreg Kroah-Hartman unsigned int status, imr; 2474ab4382d2SGreg Kroah-Hartman unsigned int pdc_tx; 2475ab4382d2SGreg Kroah-Hartman 2476ab4382d2SGreg Kroah-Hartman /* 2477ab4382d2SGreg Kroah-Hartman * First, save IMR and then disable interrupts 2478ab4382d2SGreg Kroah-Hartman */ 24794e7decdaSCyrille Pitchen imr = atmel_uart_readl(port, ATMEL_US_IMR); 24804e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 24814e7decdaSCyrille Pitchen ATMEL_US_RXRDY | atmel_port->tx_done_mask); 2482ab4382d2SGreg Kroah-Hartman 2483ab4382d2SGreg Kroah-Hartman /* Store PDC transmit status and disable it */ 24844e7decdaSCyrille Pitchen pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN; 24854e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 2486ab4382d2SGreg Kroah-Hartman 2487ab4382d2SGreg Kroah-Hartman uart_console_write(port, s, count, atmel_console_putchar); 2488ab4382d2SGreg Kroah-Hartman 2489ab4382d2SGreg Kroah-Hartman /* 2490ab4382d2SGreg Kroah-Hartman * Finally, wait for transmitter to become empty 2491ab4382d2SGreg Kroah-Hartman * and restore IMR 2492ab4382d2SGreg Kroah-Hartman */ 2493ab4382d2SGreg Kroah-Hartman do { 24944e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 2495ab4382d2SGreg Kroah-Hartman } while (!(status & ATMEL_US_TXRDY)); 2496ab4382d2SGreg Kroah-Hartman 2497ab4382d2SGreg Kroah-Hartman /* Restore PDC transmit status */ 2498ab4382d2SGreg Kroah-Hartman if (pdc_tx) 24994e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 2500ab4382d2SGreg Kroah-Hartman 2501ab4382d2SGreg Kroah-Hartman /* set interrupts back the way they were */ 25024e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, imr); 2503ab4382d2SGreg Kroah-Hartman } 2504ab4382d2SGreg Kroah-Hartman 2505ab4382d2SGreg Kroah-Hartman /* 2506ab4382d2SGreg Kroah-Hartman * If the port was already initialised (eg, by a boot loader), 2507ab4382d2SGreg Kroah-Hartman * try to determine the current setup. 2508ab4382d2SGreg Kroah-Hartman */ 2509ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud, 2510ab4382d2SGreg Kroah-Hartman int *parity, int *bits) 2511ab4382d2SGreg Kroah-Hartman { 2512ab4382d2SGreg Kroah-Hartman unsigned int mr, quot; 2513ab4382d2SGreg Kroah-Hartman 2514ab4382d2SGreg Kroah-Hartman /* 2515ab4382d2SGreg Kroah-Hartman * If the baud rate generator isn't running, the port wasn't 2516ab4382d2SGreg Kroah-Hartman * initialized by the boot loader. 2517ab4382d2SGreg Kroah-Hartman */ 25184e7decdaSCyrille Pitchen quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD; 2519ab4382d2SGreg Kroah-Hartman if (!quot) 2520ab4382d2SGreg Kroah-Hartman return; 2521ab4382d2SGreg Kroah-Hartman 25224e7decdaSCyrille Pitchen mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL; 2523ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_CHRL_8) 2524ab4382d2SGreg Kroah-Hartman *bits = 8; 2525ab4382d2SGreg Kroah-Hartman else 2526ab4382d2SGreg Kroah-Hartman *bits = 7; 2527ab4382d2SGreg Kroah-Hartman 25284e7decdaSCyrille Pitchen mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR; 2529ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_PAR_EVEN) 2530ab4382d2SGreg Kroah-Hartman *parity = 'e'; 2531ab4382d2SGreg Kroah-Hartman else if (mr == ATMEL_US_PAR_ODD) 2532ab4382d2SGreg Kroah-Hartman *parity = 'o'; 2533ab4382d2SGreg Kroah-Hartman 2534ab4382d2SGreg Kroah-Hartman /* 2535ab4382d2SGreg Kroah-Hartman * The serial core only rounds down when matching this to a 2536ab4382d2SGreg Kroah-Hartman * supported baud rate. Make sure we don't end up slightly 2537ab4382d2SGreg Kroah-Hartman * lower than one of those, as it would make us fall through 2538ab4382d2SGreg Kroah-Hartman * to a much lower baud rate than we really want. 2539ab4382d2SGreg Kroah-Hartman */ 2540ab4382d2SGreg Kroah-Hartman *baud = port->uartclk / (16 * (quot - 1)); 2541ab4382d2SGreg Kroah-Hartman } 2542ab4382d2SGreg Kroah-Hartman 2543ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options) 2544ab4382d2SGreg Kroah-Hartman { 254591f8c2d8SBoris BREZILLON int ret; 2546ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2547ab4382d2SGreg Kroah-Hartman int baud = 115200; 2548ab4382d2SGreg Kroah-Hartman int bits = 8; 2549ab4382d2SGreg Kroah-Hartman int parity = 'n'; 2550ab4382d2SGreg Kroah-Hartman int flow = 'n'; 2551ab4382d2SGreg Kroah-Hartman 2552ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2553ab4382d2SGreg Kroah-Hartman /* Port not initialized yet - delay setup */ 2554ab4382d2SGreg Kroah-Hartman return -ENODEV; 2555ab4382d2SGreg Kroah-Hartman } 2556ab4382d2SGreg Kroah-Hartman 255791f8c2d8SBoris BREZILLON ret = clk_prepare_enable(atmel_ports[co->index].clk); 255891f8c2d8SBoris BREZILLON if (ret) 255991f8c2d8SBoris BREZILLON return ret; 2560ab4382d2SGreg Kroah-Hartman 25614e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 25624e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 25634e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 2564ab4382d2SGreg Kroah-Hartman 2565ab4382d2SGreg Kroah-Hartman if (options) 2566ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 2567ab4382d2SGreg Kroah-Hartman else 2568ab4382d2SGreg Kroah-Hartman atmel_console_get_options(port, &baud, &parity, &bits); 2569ab4382d2SGreg Kroah-Hartman 2570ab4382d2SGreg Kroah-Hartman return uart_set_options(port, co, baud, parity, bits, flow); 2571ab4382d2SGreg Kroah-Hartman } 2572ab4382d2SGreg Kroah-Hartman 2573ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart; 2574ab4382d2SGreg Kroah-Hartman 2575ab4382d2SGreg Kroah-Hartman static struct console atmel_console = { 2576ab4382d2SGreg Kroah-Hartman .name = ATMEL_DEVICENAME, 2577ab4382d2SGreg Kroah-Hartman .write = atmel_console_write, 2578ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 2579ab4382d2SGreg Kroah-Hartman .setup = atmel_console_setup, 2580ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 2581ab4382d2SGreg Kroah-Hartman .index = -1, 2582ab4382d2SGreg Kroah-Hartman .data = &atmel_uart, 2583ab4382d2SGreg Kroah-Hartman }; 2584ab4382d2SGreg Kroah-Hartman 2585ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE (&atmel_console) 2586ab4382d2SGreg Kroah-Hartman 2587ab4382d2SGreg Kroah-Hartman /* 2588ab4382d2SGreg Kroah-Hartman * Early console initialization (before VM subsystem initialized). 2589ab4382d2SGreg Kroah-Hartman */ 2590ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void) 2591ab4382d2SGreg Kroah-Hartman { 259291f8c2d8SBoris BREZILLON int ret; 2593ab4382d2SGreg Kroah-Hartman if (atmel_default_console_device) { 25940d0a3cc1SVoss, Nikolaus struct atmel_uart_data *pdata = 2595574de559SJingoo Han dev_get_platdata(&atmel_default_console_device->dev); 2596efb8d21bSLinus Torvalds int id = pdata->num; 2597b78cd169SJaeden Amero struct atmel_uart_port *atmel_port = &atmel_ports[id]; 25980d0a3cc1SVoss, Nikolaus 2599b78cd169SJaeden Amero atmel_port->backup_imr = 0; 2600b78cd169SJaeden Amero atmel_port->uart.line = id; 26014cbf9f48SNicolas Ferre 26024cbf9f48SNicolas Ferre add_preferred_console(ATMEL_DEVICENAME, id, NULL); 2603b78cd169SJaeden Amero ret = atmel_init_port(atmel_port, atmel_default_console_device); 260491f8c2d8SBoris BREZILLON if (ret) 260591f8c2d8SBoris BREZILLON return ret; 2606ab4382d2SGreg Kroah-Hartman register_console(&atmel_console); 2607ab4382d2SGreg Kroah-Hartman } 2608ab4382d2SGreg Kroah-Hartman 2609ab4382d2SGreg Kroah-Hartman return 0; 2610ab4382d2SGreg Kroah-Hartman } 2611ab4382d2SGreg Kroah-Hartman 2612ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init); 2613ab4382d2SGreg Kroah-Hartman 2614ab4382d2SGreg Kroah-Hartman /* 2615ab4382d2SGreg Kroah-Hartman * Late console initialization. 2616ab4382d2SGreg Kroah-Hartman */ 2617ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void) 2618ab4382d2SGreg Kroah-Hartman { 2619ab4382d2SGreg Kroah-Hartman if (atmel_default_console_device 2620ab4382d2SGreg Kroah-Hartman && !(atmel_console.flags & CON_ENABLED)) 2621ab4382d2SGreg Kroah-Hartman register_console(&atmel_console); 2622ab4382d2SGreg Kroah-Hartman 2623ab4382d2SGreg Kroah-Hartman return 0; 2624ab4382d2SGreg Kroah-Hartman } 2625ab4382d2SGreg Kroah-Hartman 2626ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init); 2627ab4382d2SGreg Kroah-Hartman 2628ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 2629ab4382d2SGreg Kroah-Hartman { 2630ab4382d2SGreg Kroah-Hartman return port->cons && port->cons->index == port->line; 2631ab4382d2SGreg Kroah-Hartman } 2632ab4382d2SGreg Kroah-Hartman 2633ab4382d2SGreg Kroah-Hartman #else 2634ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE NULL 2635ab4382d2SGreg Kroah-Hartman 2636ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 2637ab4382d2SGreg Kroah-Hartman { 2638ab4382d2SGreg Kroah-Hartman return false; 2639ab4382d2SGreg Kroah-Hartman } 2640ab4382d2SGreg Kroah-Hartman #endif 2641ab4382d2SGreg Kroah-Hartman 2642ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = { 2643ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 2644ab4382d2SGreg Kroah-Hartman .driver_name = "atmel_serial", 2645ab4382d2SGreg Kroah-Hartman .dev_name = ATMEL_DEVICENAME, 2646ab4382d2SGreg Kroah-Hartman .major = SERIAL_ATMEL_MAJOR, 2647ab4382d2SGreg Kroah-Hartman .minor = MINOR_START, 2648ab4382d2SGreg Kroah-Hartman .nr = ATMEL_MAX_UART, 2649ab4382d2SGreg Kroah-Hartman .cons = ATMEL_CONSOLE_DEVICE, 2650ab4382d2SGreg Kroah-Hartman }; 2651ab4382d2SGreg Kroah-Hartman 2652ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM 2653ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void) 2654ab4382d2SGreg Kroah-Hartman { 2655ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91 2656ab4382d2SGreg Kroah-Hartman return at91_suspend_entering_slow_clock(); 2657ab4382d2SGreg Kroah-Hartman #else 2658ab4382d2SGreg Kroah-Hartman return false; 2659ab4382d2SGreg Kroah-Hartman #endif 2660ab4382d2SGreg Kroah-Hartman } 2661ab4382d2SGreg Kroah-Hartman 2662ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev, 2663ab4382d2SGreg Kroah-Hartman pm_message_t state) 2664ab4382d2SGreg Kroah-Hartman { 2665ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2666ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2667ab4382d2SGreg Kroah-Hartman 2668ab4382d2SGreg Kroah-Hartman if (atmel_is_console_port(port) && console_suspend_enabled) { 2669ab4382d2SGreg Kroah-Hartman /* Drain the TX shifter */ 26704e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & 26714e7decdaSCyrille Pitchen ATMEL_US_TXEMPTY)) 2672ab4382d2SGreg Kroah-Hartman cpu_relax(); 2673ab4382d2SGreg Kroah-Hartman } 2674ab4382d2SGreg Kroah-Hartman 26756a5f0e2fSAlexandre Belloni if (atmel_is_console_port(port) && !console_suspend_enabled) { 26766a5f0e2fSAlexandre Belloni /* Cache register values as we won't get a full shutdown/startup 26776a5f0e2fSAlexandre Belloni * cycle 26786a5f0e2fSAlexandre Belloni */ 26796a5f0e2fSAlexandre Belloni atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR); 26806a5f0e2fSAlexandre Belloni atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR); 26816a5f0e2fSAlexandre Belloni atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR); 26826a5f0e2fSAlexandre Belloni atmel_port->cache.rtor = atmel_uart_readl(port, 26836a5f0e2fSAlexandre Belloni atmel_port->rtor); 26846a5f0e2fSAlexandre Belloni atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR); 26856a5f0e2fSAlexandre Belloni atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR); 26866a5f0e2fSAlexandre Belloni atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR); 26876a5f0e2fSAlexandre Belloni } 26886a5f0e2fSAlexandre Belloni 2689ab4382d2SGreg Kroah-Hartman /* we can not wake up if we're running on slow clock */ 2690ab4382d2SGreg Kroah-Hartman atmel_port->may_wakeup = device_may_wakeup(&pdev->dev); 26912c7af5baSBoris BREZILLON if (atmel_serial_clk_will_stop()) { 26922c7af5baSBoris BREZILLON unsigned long flags; 26932c7af5baSBoris BREZILLON 26942c7af5baSBoris BREZILLON spin_lock_irqsave(&atmel_port->lock_suspended, flags); 26952c7af5baSBoris BREZILLON atmel_port->suspended = true; 26962c7af5baSBoris BREZILLON spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); 2697ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, 0); 26982c7af5baSBoris BREZILLON } 2699ab4382d2SGreg Kroah-Hartman 2700ab4382d2SGreg Kroah-Hartman uart_suspend_port(&atmel_uart, port); 2701ab4382d2SGreg Kroah-Hartman 2702ab4382d2SGreg Kroah-Hartman return 0; 2703ab4382d2SGreg Kroah-Hartman } 2704ab4382d2SGreg Kroah-Hartman 2705ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev) 2706ab4382d2SGreg Kroah-Hartman { 2707ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2708ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 27092c7af5baSBoris BREZILLON unsigned long flags; 27102c7af5baSBoris BREZILLON 27116a5f0e2fSAlexandre Belloni if (atmel_is_console_port(port) && !console_suspend_enabled) { 27126a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr); 27136a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr); 27146a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr); 27156a5f0e2fSAlexandre Belloni atmel_uart_writel(port, atmel_port->rtor, 27166a5f0e2fSAlexandre Belloni atmel_port->cache.rtor); 27176a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr); 27186a5f0e2fSAlexandre Belloni 27196a5f0e2fSAlexandre Belloni if (atmel_port->fifo_size) { 27206a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN | 27216a5f0e2fSAlexandre Belloni ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR); 27226a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_FMR, 27236a5f0e2fSAlexandre Belloni atmel_port->cache.fmr); 27246a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_FIER, 27256a5f0e2fSAlexandre Belloni atmel_port->cache.fimr); 27266a5f0e2fSAlexandre Belloni } 27276a5f0e2fSAlexandre Belloni atmel_start_rx(port); 27286a5f0e2fSAlexandre Belloni } 27296a5f0e2fSAlexandre Belloni 27302c7af5baSBoris BREZILLON spin_lock_irqsave(&atmel_port->lock_suspended, flags); 27312c7af5baSBoris BREZILLON if (atmel_port->pending) { 27322c7af5baSBoris BREZILLON atmel_handle_receive(port, atmel_port->pending); 27332c7af5baSBoris BREZILLON atmel_handle_status(port, atmel_port->pending, 27342c7af5baSBoris BREZILLON atmel_port->pending_status); 27352c7af5baSBoris BREZILLON atmel_handle_transmit(port, atmel_port->pending); 27362c7af5baSBoris BREZILLON atmel_port->pending = 0; 27372c7af5baSBoris BREZILLON } 27382c7af5baSBoris BREZILLON atmel_port->suspended = false; 27392c7af5baSBoris BREZILLON spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); 2740ab4382d2SGreg Kroah-Hartman 2741ab4382d2SGreg Kroah-Hartman uart_resume_port(&atmel_uart, port); 2742ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup); 2743ab4382d2SGreg Kroah-Hartman 2744ab4382d2SGreg Kroah-Hartman return 0; 2745ab4382d2SGreg Kroah-Hartman } 2746ab4382d2SGreg Kroah-Hartman #else 2747ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL 2748ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL 2749ab4382d2SGreg Kroah-Hartman #endif 2750ab4382d2SGreg Kroah-Hartman 2751b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port, 2752b5199d46SCyrille Pitchen struct platform_device *pdev) 2753b5199d46SCyrille Pitchen { 2754b78cd169SJaeden Amero atmel_port->fifo_size = 0; 2755b78cd169SJaeden Amero atmel_port->rts_low = 0; 2756b78cd169SJaeden Amero atmel_port->rts_high = 0; 2757b5199d46SCyrille Pitchen 2758b5199d46SCyrille Pitchen if (of_property_read_u32(pdev->dev.of_node, 2759b5199d46SCyrille Pitchen "atmel,fifo-size", 2760b78cd169SJaeden Amero &atmel_port->fifo_size)) 2761b5199d46SCyrille Pitchen return; 2762b5199d46SCyrille Pitchen 2763b78cd169SJaeden Amero if (!atmel_port->fifo_size) 2764b5199d46SCyrille Pitchen return; 2765b5199d46SCyrille Pitchen 2766b78cd169SJaeden Amero if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) { 2767b78cd169SJaeden Amero atmel_port->fifo_size = 0; 2768b5199d46SCyrille Pitchen dev_err(&pdev->dev, "Invalid FIFO size\n"); 2769b5199d46SCyrille Pitchen return; 2770b5199d46SCyrille Pitchen } 2771b5199d46SCyrille Pitchen 2772b5199d46SCyrille Pitchen /* 2773b5199d46SCyrille Pitchen * 0 <= rts_low <= rts_high <= fifo_size 2774b5199d46SCyrille Pitchen * Once their CTS line asserted by the remote peer, some x86 UARTs tend 2775b5199d46SCyrille Pitchen * to flush their internal TX FIFO, commonly up to 16 data, before 2776b5199d46SCyrille Pitchen * actually stopping to send new data. So we try to set the RTS High 2777b5199d46SCyrille Pitchen * Threshold to a reasonably high value respecting this 16 data 2778b5199d46SCyrille Pitchen * empirical rule when possible. 2779b5199d46SCyrille Pitchen */ 2780b78cd169SJaeden Amero atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1, 2781b78cd169SJaeden Amero atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET); 2782b78cd169SJaeden Amero atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2, 2783b78cd169SJaeden Amero atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET); 2784b5199d46SCyrille Pitchen 2785b5199d46SCyrille Pitchen dev_info(&pdev->dev, "Using FIFO (%u data)\n", 2786b78cd169SJaeden Amero atmel_port->fifo_size); 2787b5199d46SCyrille Pitchen dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n", 2788b78cd169SJaeden Amero atmel_port->rts_high); 2789b5199d46SCyrille Pitchen dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n", 2790b78cd169SJaeden Amero atmel_port->rts_low); 2791b5199d46SCyrille Pitchen } 2792b5199d46SCyrille Pitchen 27939671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev) 2794ab4382d2SGreg Kroah-Hartman { 2795b78cd169SJaeden Amero struct atmel_uart_port *atmel_port; 27965fbe46b6SNicolas Ferre struct device_node *np = pdev->dev.of_node; 2797574de559SJingoo Han struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); 2798ab4382d2SGreg Kroah-Hartman void *data; 27994cbf9f48SNicolas Ferre int ret = -ENODEV; 2800bd737f87SRicardo Ribalda Delgado bool rs485_enabled; 2801ab4382d2SGreg Kroah-Hartman 2802ab4382d2SGreg Kroah-Hartman BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); 2803ab4382d2SGreg Kroah-Hartman 28045fbe46b6SNicolas Ferre if (np) 28055fbe46b6SNicolas Ferre ret = of_alias_get_id(np, "serial"); 28065fbe46b6SNicolas Ferre else 28074cbf9f48SNicolas Ferre if (pdata) 28084cbf9f48SNicolas Ferre ret = pdata->num; 28094cbf9f48SNicolas Ferre 28104cbf9f48SNicolas Ferre if (ret < 0) 28115fbe46b6SNicolas Ferre /* port id not found in platform data nor device-tree aliases: 28124cbf9f48SNicolas Ferre * auto-enumerate it */ 2813503bded9SPawel Wieczorkiewicz ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART); 28144cbf9f48SNicolas Ferre 2815503bded9SPawel Wieczorkiewicz if (ret >= ATMEL_MAX_UART) { 28164cbf9f48SNicolas Ferre ret = -ENODEV; 28174cbf9f48SNicolas Ferre goto err; 28184cbf9f48SNicolas Ferre } 28194cbf9f48SNicolas Ferre 2820503bded9SPawel Wieczorkiewicz if (test_and_set_bit(ret, atmel_ports_in_use)) { 28214cbf9f48SNicolas Ferre /* port already in use */ 28224cbf9f48SNicolas Ferre ret = -EBUSY; 28234cbf9f48SNicolas Ferre goto err; 28244cbf9f48SNicolas Ferre } 28254cbf9f48SNicolas Ferre 2826b78cd169SJaeden Amero atmel_port = &atmel_ports[ret]; 2827b78cd169SJaeden Amero atmel_port->backup_imr = 0; 2828b78cd169SJaeden Amero atmel_port->uart.line = ret; 2829b78cd169SJaeden Amero atmel_serial_probe_fifos(atmel_port, pdev); 2830354e57f3SLinus Walleij 283198f2082cSNicolas Ferre atomic_set(&atmel_port->tasklet_shutdown, 0); 2832b78cd169SJaeden Amero spin_lock_init(&atmel_port->lock_suspended); 28332c7af5baSBoris BREZILLON 2834b78cd169SJaeden Amero ret = atmel_init_port(atmel_port, pdev); 283591f8c2d8SBoris BREZILLON if (ret) 28366fbb9bdfSCyrille Pitchen goto err_clear_bit; 2837ab4382d2SGreg Kroah-Hartman 2838b78cd169SJaeden Amero atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0); 2839b78cd169SJaeden Amero if (IS_ERR(atmel_port->gpios)) { 2840b78cd169SJaeden Amero ret = PTR_ERR(atmel_port->gpios); 284118dfef9cSUwe Kleine-König goto err_clear_bit; 284218dfef9cSUwe Kleine-König } 284318dfef9cSUwe Kleine-König 2844b78cd169SJaeden Amero if (!atmel_use_pdc_rx(&atmel_port->uart)) { 2845ab4382d2SGreg Kroah-Hartman ret = -ENOMEM; 2846ab4382d2SGreg Kroah-Hartman data = kmalloc(sizeof(struct atmel_uart_char) 2847ab4382d2SGreg Kroah-Hartman * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); 2848ab4382d2SGreg Kroah-Hartman if (!data) 2849ab4382d2SGreg Kroah-Hartman goto err_alloc_ring; 2850b78cd169SJaeden Amero atmel_port->rx_ring.buf = data; 2851ab4382d2SGreg Kroah-Hartman } 2852ab4382d2SGreg Kroah-Hartman 2853b78cd169SJaeden Amero rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED; 2854bd737f87SRicardo Ribalda Delgado 2855b78cd169SJaeden Amero ret = uart_add_one_port(&atmel_uart, &atmel_port->uart); 2856ab4382d2SGreg Kroah-Hartman if (ret) 2857ab4382d2SGreg Kroah-Hartman goto err_add_port; 2858ab4382d2SGreg Kroah-Hartman 2859ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2860b78cd169SJaeden Amero if (atmel_is_console_port(&atmel_port->uart) 2861ab4382d2SGreg Kroah-Hartman && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) { 2862ab4382d2SGreg Kroah-Hartman /* 2863ab4382d2SGreg Kroah-Hartman * The serial core enabled the clock for us, so undo 286491f8c2d8SBoris BREZILLON * the clk_prepare_enable() in atmel_console_setup() 2865ab4382d2SGreg Kroah-Hartman */ 2866b78cd169SJaeden Amero clk_disable_unprepare(atmel_port->clk); 2867ab4382d2SGreg Kroah-Hartman } 2868ab4382d2SGreg Kroah-Hartman #endif 2869ab4382d2SGreg Kroah-Hartman 2870ab4382d2SGreg Kroah-Hartman device_init_wakeup(&pdev->dev, 1); 2871b78cd169SJaeden Amero platform_set_drvdata(pdev, atmel_port); 2872ab4382d2SGreg Kroah-Hartman 2873d4f64187SCyrille Pitchen /* 2874d4f64187SCyrille Pitchen * The peripheral clock has been disabled by atmel_init_port(): 2875d4f64187SCyrille Pitchen * enable it before accessing I/O registers 2876d4f64187SCyrille Pitchen */ 2877b78cd169SJaeden Amero clk_prepare_enable(atmel_port->clk); 2878d4f64187SCyrille Pitchen 2879bd737f87SRicardo Ribalda Delgado if (rs485_enabled) { 2880b78cd169SJaeden Amero atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR, 28814e7decdaSCyrille Pitchen ATMEL_US_USMODE_NORMAL); 2882b78cd169SJaeden Amero atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR, 2883b78cd169SJaeden Amero ATMEL_US_RTSEN); 2884fc887b15SLinus Torvalds } 2885fc887b15SLinus Torvalds 2886055560b0SElen Song /* 2887055560b0SElen Song * Get port name of usart or uart 2888055560b0SElen Song */ 2889b78cd169SJaeden Amero atmel_get_ip_name(&atmel_port->uart); 2890055560b0SElen Song 2891d4f64187SCyrille Pitchen /* 2892d4f64187SCyrille Pitchen * The peripheral clock can now safely be disabled till the port 2893d4f64187SCyrille Pitchen * is used 2894d4f64187SCyrille Pitchen */ 2895b78cd169SJaeden Amero clk_disable_unprepare(atmel_port->clk); 2896d4f64187SCyrille Pitchen 2897ab4382d2SGreg Kroah-Hartman return 0; 2898ab4382d2SGreg Kroah-Hartman 2899ab4382d2SGreg Kroah-Hartman err_add_port: 2900b78cd169SJaeden Amero kfree(atmel_port->rx_ring.buf); 2901b78cd169SJaeden Amero atmel_port->rx_ring.buf = NULL; 2902ab4382d2SGreg Kroah-Hartman err_alloc_ring: 2903b78cd169SJaeden Amero if (!atmel_is_console_port(&atmel_port->uart)) { 2904b78cd169SJaeden Amero clk_put(atmel_port->clk); 2905b78cd169SJaeden Amero atmel_port->clk = NULL; 2906ab4382d2SGreg Kroah-Hartman } 29076fbb9bdfSCyrille Pitchen err_clear_bit: 2908b78cd169SJaeden Amero clear_bit(atmel_port->uart.line, atmel_ports_in_use); 29094cbf9f48SNicolas Ferre err: 2910ab4382d2SGreg Kroah-Hartman return ret; 2911ab4382d2SGreg Kroah-Hartman } 2912ab4382d2SGreg Kroah-Hartman 2913f4a8ab04SRomain Izard /* 2914f4a8ab04SRomain Izard * Even if the driver is not modular, it makes sense to be able to 2915f4a8ab04SRomain Izard * unbind a device: there can be many bound devices, and there are 2916f4a8ab04SRomain Izard * situations where dynamic binding and unbinding can be useful. 2917f4a8ab04SRomain Izard * 2918f4a8ab04SRomain Izard * For example, a connected device can require a specific firmware update 2919f4a8ab04SRomain Izard * protocol that needs bitbanging on IO lines, but use the regular serial 2920f4a8ab04SRomain Izard * port in the normal case. 2921f4a8ab04SRomain Izard */ 2922f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev) 2923f4a8ab04SRomain Izard { 2924f4a8ab04SRomain Izard struct uart_port *port = platform_get_drvdata(pdev); 2925f4a8ab04SRomain Izard struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2926f4a8ab04SRomain Izard int ret = 0; 2927f4a8ab04SRomain Izard 292800e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_rx); 292900e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_tx); 2930f4a8ab04SRomain Izard 2931f4a8ab04SRomain Izard device_init_wakeup(&pdev->dev, 0); 2932f4a8ab04SRomain Izard 2933f4a8ab04SRomain Izard ret = uart_remove_one_port(&atmel_uart, port); 2934f4a8ab04SRomain Izard 2935f4a8ab04SRomain Izard kfree(atmel_port->rx_ring.buf); 2936f4a8ab04SRomain Izard 2937f4a8ab04SRomain Izard /* "port" is allocated statically, so we shouldn't free it */ 2938f4a8ab04SRomain Izard 2939f4a8ab04SRomain Izard clear_bit(port->line, atmel_ports_in_use); 2940f4a8ab04SRomain Izard 2941f4a8ab04SRomain Izard clk_put(atmel_port->clk); 2942f4a8ab04SRomain Izard atmel_port->clk = NULL; 2943f4a8ab04SRomain Izard 2944f4a8ab04SRomain Izard return ret; 2945f4a8ab04SRomain Izard } 2946f4a8ab04SRomain Izard 2947ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = { 2948ab4382d2SGreg Kroah-Hartman .probe = atmel_serial_probe, 2949f4a8ab04SRomain Izard .remove = atmel_serial_remove, 2950ab4382d2SGreg Kroah-Hartman .suspend = atmel_serial_suspend, 2951ab4382d2SGreg Kroah-Hartman .resume = atmel_serial_resume, 2952ab4382d2SGreg Kroah-Hartman .driver = { 2953ab4382d2SGreg Kroah-Hartman .name = "atmel_usart", 29545fbe46b6SNicolas Ferre .of_match_table = of_match_ptr(atmel_serial_dt_ids), 2955ab4382d2SGreg Kroah-Hartman }, 2956ab4382d2SGreg Kroah-Hartman }; 2957ab4382d2SGreg Kroah-Hartman 2958ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void) 2959ab4382d2SGreg Kroah-Hartman { 2960ab4382d2SGreg Kroah-Hartman int ret; 2961ab4382d2SGreg Kroah-Hartman 2962ab4382d2SGreg Kroah-Hartman ret = uart_register_driver(&atmel_uart); 2963ab4382d2SGreg Kroah-Hartman if (ret) 2964ab4382d2SGreg Kroah-Hartman return ret; 2965ab4382d2SGreg Kroah-Hartman 2966ab4382d2SGreg Kroah-Hartman ret = platform_driver_register(&atmel_serial_driver); 2967ab4382d2SGreg Kroah-Hartman if (ret) 2968ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&atmel_uart); 2969ab4382d2SGreg Kroah-Hartman 2970ab4382d2SGreg Kroah-Hartman return ret; 2971ab4382d2SGreg Kroah-Hartman } 2972c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init); 2973